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

Side by Side Diff: webrtc/base/natsocketfactory.cc

Issue 2936553003: Adding PortAllocator option to support cases where sockets can't be bound. (Closed)
Patch Set: Comment fixes Created 3 years, 6 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 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 90
91 SocketAddress GetRemoteAddress() const override { 91 SocketAddress GetRemoteAddress() const override {
92 return remote_addr_; // will be NIL if not connected 92 return remote_addr_; // will be NIL if not connected
93 } 93 }
94 94
95 int Bind(const SocketAddress& addr) override { 95 int Bind(const SocketAddress& addr) override {
96 if (socket_) { // already bound, bubble up error 96 if (socket_) { // already bound, bubble up error
97 return -1; 97 return -1;
98 } 98 }
99 99
100 int result; 100 return BindInternal(addr);
101 socket_ = sf_->CreateInternalSocket(family_, type_, addr, &server_addr_);
102 result = (socket_) ? socket_->Bind(addr) : -1;
103 if (result >= 0) {
104 socket_->SignalConnectEvent.connect(this, &NATSocket::OnConnectEvent);
105 socket_->SignalReadEvent.connect(this, &NATSocket::OnReadEvent);
106 socket_->SignalWriteEvent.connect(this, &NATSocket::OnWriteEvent);
107 socket_->SignalCloseEvent.connect(this, &NATSocket::OnCloseEvent);
108 } else {
109 server_addr_.Clear();
110 delete socket_;
111 socket_ = nullptr;
112 }
113
114 return result;
115 } 101 }
116 102
117 int Connect(const SocketAddress& addr) override { 103 int Connect(const SocketAddress& addr) override {
118 if (!socket_) { // socket must be bound, for now 104 int result = 0;
119 return -1; 105 // If we're not already bound (meaning |socket_| is null), bind to ANY
106 // address.
107 if (!socket_) {
108 result = BindInternal(SocketAddress(GetAnyIP(family_), 0));
109 if (result < 0) {
110 return result;
111 }
120 } 112 }
121 113
122 int result = 0;
123 if (type_ == SOCK_STREAM) { 114 if (type_ == SOCK_STREAM) {
124 result = socket_->Connect(server_addr_.IsNil() ? addr : server_addr_); 115 result = socket_->Connect(server_addr_.IsNil() ? addr : server_addr_);
125 } else { 116 } else {
126 connected_ = true; 117 connected_ = true;
127 } 118 }
128 119
129 if (result >= 0) { 120 if (result >= 0) {
130 remote_addr_ = addr; 121 remote_addr_ = addr;
131 } 122 }
132 123
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 socket_ = nullptr; 209 socket_ = nullptr;
219 } 210 }
220 } 211 }
221 return result; 212 return result;
222 } 213 }
223 214
224 int Listen(int backlog) override { return socket_->Listen(backlog); } 215 int Listen(int backlog) override { return socket_->Listen(backlog); }
225 AsyncSocket* Accept(SocketAddress* paddr) override { 216 AsyncSocket* Accept(SocketAddress* paddr) override {
226 return socket_->Accept(paddr); 217 return socket_->Accept(paddr);
227 } 218 }
228 int GetError() const override { return socket_->GetError(); } 219 int GetError() const override {
229 void SetError(int error) override { socket_->SetError(error); } 220 return socket_ ? socket_->GetError() : error_;
pthatcher1 2017/06/13 19:52:49 Is setting and getting error_ sufficient? Why do
Taylor Brandstetter 2017/06/13 22:11:17 GetError needs to return the inner socket's error
221 }
222 void SetError(int error) override {
223 if (socket_) {
224 socket_->SetError(error);
225 } else {
226 error_ = error;
227 }
228 }
230 ConnState GetState() const override { 229 ConnState GetState() const override {
231 return connected_ ? CS_CONNECTED : CS_CLOSED; 230 return connected_ ? CS_CONNECTED : CS_CLOSED;
232 } 231 }
233 int GetOption(Option opt, int* value) override { 232 int GetOption(Option opt, int* value) override {
234 return socket_->GetOption(opt, value); 233 return socket_->GetOption(opt, value);
235 } 234 }
236 int SetOption(Option opt, int value) override { 235 int SetOption(Option opt, int value) override {
237 return socket_->SetOption(opt, value); 236 return socket_->SetOption(opt, value);
238 } 237 }
239 238
(...skipping 19 matching lines...) Expand all
259 void OnWriteEvent(AsyncSocket* socket) { 258 void OnWriteEvent(AsyncSocket* socket) {
260 RTC_DCHECK(socket == socket_); 259 RTC_DCHECK(socket == socket_);
261 SignalWriteEvent(this); 260 SignalWriteEvent(this);
262 } 261 }
263 void OnCloseEvent(AsyncSocket* socket, int error) { 262 void OnCloseEvent(AsyncSocket* socket, int error) {
264 RTC_DCHECK(socket == socket_); 263 RTC_DCHECK(socket == socket_);
265 SignalCloseEvent(this, error); 264 SignalCloseEvent(this, error);
266 } 265 }
267 266
268 private: 267 private:
268 int BindInternal(const SocketAddress& addr) {
269 RTC_DCHECK(!socket_);
270
271 int result;
272 socket_ = sf_->CreateInternalSocket(family_, type_, addr, &server_addr_);
273 result = (socket_) ? socket_->Bind(addr) : -1;
274 if (result >= 0) {
275 socket_->SignalConnectEvent.connect(this, &NATSocket::OnConnectEvent);
276 socket_->SignalReadEvent.connect(this, &NATSocket::OnReadEvent);
277 socket_->SignalWriteEvent.connect(this, &NATSocket::OnWriteEvent);
278 socket_->SignalCloseEvent.connect(this, &NATSocket::OnCloseEvent);
279 } else {
280 server_addr_.Clear();
281 delete socket_;
282 socket_ = nullptr;
283 }
284
285 return result;
286 }
287
269 // Makes sure the buffer is at least the given size. 288 // Makes sure the buffer is at least the given size.
270 void Grow(size_t new_size) { 289 void Grow(size_t new_size) {
271 if (size_ < new_size) { 290 if (size_ < new_size) {
272 delete[] buf_; 291 delete[] buf_;
273 size_ = new_size; 292 size_ = new_size;
274 buf_ = new char[size_]; 293 buf_ = new char[size_];
275 } 294 }
276 } 295 }
277 296
278 // Sends the destination address to the server to tell it to connect. 297 // Sends the destination address to the server to tell it to connect.
(...skipping 16 matching lines...) Expand all
295 } 314 }
296 } 315 }
297 316
298 NATInternalSocketFactory* sf_; 317 NATInternalSocketFactory* sf_;
299 int family_; 318 int family_;
300 int type_; 319 int type_;
301 bool connected_; 320 bool connected_;
302 SocketAddress remote_addr_; 321 SocketAddress remote_addr_;
303 SocketAddress server_addr_; // address of the NAT server 322 SocketAddress server_addr_; // address of the NAT server
304 AsyncSocket* socket_; 323 AsyncSocket* socket_;
324 // Need to hold error in case it occurs before the socket is created.
325 int error_ = 0;
305 char* buf_; 326 char* buf_;
306 size_t size_; 327 size_t size_;
307 }; 328 };
308 329
309 // NATSocketFactory 330 // NATSocketFactory
310 NATSocketFactory::NATSocketFactory(SocketFactory* factory, 331 NATSocketFactory::NATSocketFactory(SocketFactory* factory,
311 const SocketAddress& nat_udp_addr, 332 const SocketAddress& nat_udp_addr,
312 const SocketAddress& nat_tcp_addr) 333 const SocketAddress& nat_tcp_addr)
313 : factory_(factory), nat_udp_addr_(nat_udp_addr), 334 : factory_(factory), nat_udp_addr_(nat_udp_addr),
314 nat_tcp_addr_(nat_tcp_addr) { 335 nat_tcp_addr_(nat_tcp_addr) {
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( 521 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient(
501 const SocketAddress& int_ip) { 522 const SocketAddress& int_ip) {
502 Translator* nat = nullptr; 523 Translator* nat = nullptr;
503 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { 524 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) {
504 nat = it->second->FindClient(int_ip); 525 nat = it->second->FindClient(int_ip);
505 } 526 }
506 return nat; 527 return nat;
507 } 528 }
508 529
509 } // namespace rtc 530 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698