Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: content/browser/renderer_host/p2p/socket_host_udp.cc

Issue 2140693002: Support port range for IPC P2P UDP sockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
13 #include "build/build_config.h" 14 #include "build/build_config.h"
14 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" 15 #include "content/browser/renderer_host/p2p/socket_host_throttler.h"
15 #include "content/common/p2p_messages.h" 16 #include "content/common/p2p_messages.h"
16 #include "content/public/browser/content_browser_client.h" 17 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h" 18 #include "content/public/common/content_client.h"
18 #include "ipc/ipc_sender.h" 19 #include "ipc/ipc_sender.h"
19 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "third_party/webrtc/media/base/rtputils.h" 22 #include "third_party/webrtc/media/base/rtputils.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 id(id) { 84 id(id) {
84 memcpy(data->data(), &content[0], size); 85 memcpy(data->data(), &content[0], size);
85 } 86 }
86 87
87 P2PSocketHostUdp::PendingPacket::PendingPacket(const PendingPacket& other) = 88 P2PSocketHostUdp::PendingPacket::PendingPacket(const PendingPacket& other) =
88 default; 89 default;
89 90
90 P2PSocketHostUdp::PendingPacket::~PendingPacket() { 91 P2PSocketHostUdp::PendingPacket::~PendingPacket() {
91 } 92 }
92 93
94 P2PSocketHostUdp::P2PSocketHostUdp(
95 IPC::Sender* message_sender,
96 int socket_id,
97 P2PMessageThrottler* throttler,
98 const DatagramServerSocketFactory& socket_factory)
99 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::UDP),
100 socket_(socket_factory.Run()),
101 send_pending_(false),
102 last_dscp_(net::DSCP_CS0),
103 throttler_(throttler),
104 send_buffer_size_(0),
105 socket_factory_(socket_factory) {}
106
93 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender, 107 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender,
94 int socket_id, 108 int socket_id,
95 P2PMessageThrottler* throttler) 109 P2PMessageThrottler* throttler)
96 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::UDP), 110 : P2PSocketHostUdp(message_sender,
97 send_pending_(false), 111 socket_id,
98 last_dscp_(net::DSCP_CS0), 112 throttler,
99 throttler_(throttler), 113 base::Bind(&P2PSocketHostUdp::DefaultSocketFactory)) {}
100 send_buffer_size_(0) {
101 net::UDPServerSocket* socket = new net::UDPServerSocket(
102 GetContentClient()->browser()->GetNetLog(), net::NetLog::Source());
103 #if defined(OS_WIN)
104 socket->UseNonBlockingIO();
105 #endif
106 socket_.reset(socket);
107 }
108 114
109 P2PSocketHostUdp::~P2PSocketHostUdp() { 115 P2PSocketHostUdp::~P2PSocketHostUdp() {
110 if (state_ == STATE_OPEN) { 116 if (state_ == STATE_OPEN) {
111 DCHECK(socket_.get()); 117 DCHECK(socket_.get());
112 socket_.reset(); 118 socket_.reset();
113 } 119 }
114 } 120 }
115 121
116 void P2PSocketHostUdp::SetSendBufferSize() { 122 void P2PSocketHostUdp::SetSendBufferSize() {
117 unsigned int send_buffer_size = 0; 123 unsigned int send_buffer_size = 0;
118 124
119 base::StringToUint( 125 base::StringToUint(
120 base::FieldTrialList::FindFullName("WebRTC-SystemUDPSendSocketSize"), 126 base::FieldTrialList::FindFullName("WebRTC-SystemUDPSendSocketSize"),
121 &send_buffer_size); 127 &send_buffer_size);
122 128
123 if (send_buffer_size > 0) { 129 if (send_buffer_size > 0) {
124 if (!SetOption(P2P_SOCKET_OPT_SNDBUF, send_buffer_size)) { 130 if (!SetOption(P2P_SOCKET_OPT_SNDBUF, send_buffer_size)) {
125 LOG(WARNING) << "Failed to set socket send buffer size to " 131 LOG(WARNING) << "Failed to set socket send buffer size to "
126 << send_buffer_size; 132 << send_buffer_size;
127 } else { 133 } else {
128 send_buffer_size_ = send_buffer_size; 134 send_buffer_size_ = send_buffer_size;
129 } 135 }
130 } 136 }
131 } 137 }
132 138
133 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address, 139 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address,
140 uint16_t min_port,
141 uint16_t max_port,
134 const P2PHostAndIPEndPoint& remote_address) { 142 const P2PHostAndIPEndPoint& remote_address) {
135 DCHECK_EQ(state_, STATE_UNINITIALIZED); 143 DCHECK_EQ(state_, STATE_UNINITIALIZED);
144 DCHECK(local_address.port() == 0 || (min_port == 0 && max_port == 0));
136 145
137 int result = socket_->Listen(local_address); 146 int result = -1;
147 if (min_port == 0) {
148 result = socket_->Listen(local_address);
149 } else {
150 for (unsigned port = min_port; port <= max_port && result < 0; ++port) {
151 result = socket_->Listen(net::IPEndPoint(local_address.address(), port));
152 if (result < 0 && port != max_port)
153 socket_ = socket_factory_.Run();
154 }
155 }
138 if (result < 0) { 156 if (result < 0) {
139 LOG(ERROR) << "bind() to " << local_address.ToString() 157 LOG(ERROR) << "bind() to " << local_address.address().ToString()
158 << (min_port == 0
159 ? base::StringPrintf(":%d", local_address.port())
160 : base::StringPrintf(", port range [%d-%d]", min_port,
161 max_port))
140 << " failed: " << result; 162 << " failed: " << result;
141 OnError(); 163 OnError();
142 return false; 164 return false;
143 } 165 }
144 166
145 // Setting recv socket buffer size. 167 // Setting recv socket buffer size.
146 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) { 168 if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) {
147 LOG(WARNING) << "Failed to set socket receive buffer size to " 169 LOG(WARNING) << "Failed to set socket receive buffer size to "
148 << kRecvSocketBufferSize; 170 << kRecvSocketBufferSize;
149 } 171 }
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 return socket_->SetSendBufferSize(value) == net::OK; 415 return socket_->SetSendBufferSize(value) == net::OK;
394 case P2P_SOCKET_OPT_DSCP: 416 case P2P_SOCKET_OPT_DSCP:
395 return (net::OK == socket_->SetDiffServCodePoint( 417 return (net::OK == socket_->SetDiffServCodePoint(
396 static_cast<net::DiffServCodePoint>(value))) ? true : false; 418 static_cast<net::DiffServCodePoint>(value))) ? true : false;
397 default: 419 default:
398 NOTREACHED(); 420 NOTREACHED();
399 return false; 421 return false;
400 } 422 }
401 } 423 }
402 424
425 // static
426 std::unique_ptr<net::DatagramServerSocket>
427 P2PSocketHostUdp::DefaultSocketFactory() {
428 std::unique_ptr<net::DatagramServerSocket> socket(new net::UDPServerSocket(
429 GetContentClient()->browser()->GetNetLog(), net::NetLog::Source()));
430 #if defined(OS_WIN)
431 socket->UseNonBlockingIO();
432 #endif
433
434 return socket;
435 }
436
403 } // namespace content 437 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698