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

Side by Side Diff: webrtc/p2p/base/basicpacketsocketfactory.cc

Issue 1556743002: Bind a socket to a network if the network handle is set. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 11 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 /* 1 /*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/p2p/base/basicpacketsocketfactory.h" 11 #include "webrtc/p2p/base/basicpacketsocketfactory.h"
12 12
13 #include "webrtc/p2p/base/asyncstuntcpsocket.h" 13 #include "webrtc/p2p/base/asyncstuntcpsocket.h"
14 #include "webrtc/p2p/base/stun.h" 14 #include "webrtc/p2p/base/stun.h"
15 #include "webrtc/base/asynctcpsocket.h" 15 #include "webrtc/base/asynctcpsocket.h"
16 #include "webrtc/base/asyncudpsocket.h" 16 #include "webrtc/base/asyncudpsocket.h"
17 #include "webrtc/base/logging.h" 17 #include "webrtc/base/logging.h"
18 #include "webrtc/base/nethelpers.h" 18 #include "webrtc/base/nethelpers.h"
19 #include "webrtc/base/network.h"
19 #include "webrtc/base/physicalsocketserver.h" 20 #include "webrtc/base/physicalsocketserver.h"
20 #include "webrtc/base/scoped_ptr.h" 21 #include "webrtc/base/scoped_ptr.h"
21 #include "webrtc/base/socketadapters.h" 22 #include "webrtc/base/socketadapters.h"
22 #include "webrtc/base/ssladapter.h" 23 #include "webrtc/base/ssladapter.h"
23 #include "webrtc/base/thread.h" 24 #include "webrtc/base/thread.h"
24 25
25 namespace rtc { 26 namespace rtc {
26 27
27 BasicPacketSocketFactory::BasicPacketSocketFactory() 28 BasicPacketSocketFactory::BasicPacketSocketFactory()
28 : thread_(Thread::Current()), 29 : thread_(Thread::Current()),
(...skipping 11 matching lines...) Expand all
40 socket_factory_(socket_factory) { 41 socket_factory_(socket_factory) {
41 } 42 }
42 43
43 BasicPacketSocketFactory::~BasicPacketSocketFactory() { 44 BasicPacketSocketFactory::~BasicPacketSocketFactory() {
44 } 45 }
45 46
46 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket( 47 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
47 const SocketAddress& address, 48 const SocketAddress& address,
48 uint16_t min_port, 49 uint16_t min_port,
49 uint16_t max_port) { 50 uint16_t max_port) {
51 return CreateUdpSocketOnNetwork(address, min_port, max_port, nullptr);
52 }
53
54 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocketOnNetwork(
55 const SocketAddress& address,
56 uint16_t min_port,
57 uint16_t max_port,
58 const Network* network) {
50 // UDP sockets are simple. 59 // UDP sockets are simple.
51 rtc::AsyncSocket* socket = 60 rtc::AsyncSocket* socket =
52 socket_factory()->CreateAsyncSocket( 61 socket_factory()->CreateAsyncSocket(
53 address.family(), SOCK_DGRAM); 62 address.family(), SOCK_DGRAM);
54 if (!socket) { 63 if (!socket) {
55 return NULL; 64 return NULL;
56 } 65 }
66 if (network && network->IsHandleValid()) {
67 int res = socket->BindToNetwork(network->handle());
68 if (res < 0) {
69 LOG(LS_WARNING) << "Network bind failed with error " << res;
70 }
71 }
57 if (BindSocket(socket, address, min_port, max_port) < 0) { 72 if (BindSocket(socket, address, min_port, max_port) < 0) {
58 LOG(LS_ERROR) << "UDP bind failed with error " 73 LOG(LS_ERROR) << "UDP bind failed with error "
59 << socket->GetError(); 74 << socket->GetError();
60 delete socket; 75 delete socket;
61 return NULL; 76 return NULL;
62 } 77 }
63 return new rtc::AsyncUDPSocket(socket); 78 return new rtc::AsyncUDPSocket(socket);
64 } 79 }
65 80
66 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( 81 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 if (min_port == 0 && max_port == 0) { 202 if (min_port == 0 && max_port == 0) {
188 // If there's no port range, let the OS pick a port for us. 203 // If there's no port range, let the OS pick a port for us.
189 ret = socket->Bind(local_address); 204 ret = socket->Bind(local_address);
190 } else { 205 } else {
191 // Otherwise, try to find a port in the provided range. 206 // Otherwise, try to find a port in the provided range.
192 for (int port = min_port; ret < 0 && port <= max_port; ++port) { 207 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
193 ret = socket->Bind(rtc::SocketAddress(local_address.ipaddr(), 208 ret = socket->Bind(rtc::SocketAddress(local_address.ipaddr(),
194 port)); 209 port));
195 } 210 }
196 } 211 }
197 return ret; 212 return ret;
pthatcher1 2016/01/06 21:58:46 So, I think we can do something here to avoid need
honghaiz3 2016/01/12 20:36:42 Step 2: I think ::bind should be called anyway, no
198 } 213 }
199 214
200 SocketFactory* BasicPacketSocketFactory::socket_factory() { 215 SocketFactory* BasicPacketSocketFactory::socket_factory() {
201 if (thread_) { 216 if (thread_) {
202 ASSERT(thread_ == Thread::Current()); 217 ASSERT(thread_ == Thread::Current());
203 return thread_->socketserver(); 218 return thread_->socketserver();
204 } else { 219 } else {
205 return socket_factory_; 220 return socket_factory_;
206 } 221 }
207 } 222 }
208 223
209 } // namespace rtc 224 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698