| OLD | NEW |
| 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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 if (result >= 0) { | 172 if (result >= 0) { |
| 173 ASSERT(remote_addr == server_addr_); | 173 ASSERT(remote_addr == server_addr_); |
| 174 | 174 |
| 175 // TODO: we need better framing so we know how many bytes we can | 175 // TODO: we need better framing so we know how many bytes we can |
| 176 // return before we need to read the next address. For UDP, this will be | 176 // return before we need to read the next address. For UDP, this will be |
| 177 // fine as long as the reader always reads everything in the packet. | 177 // fine as long as the reader always reads everything in the packet. |
| 178 ASSERT((size_t)result < size_); | 178 ASSERT((size_t)result < size_); |
| 179 | 179 |
| 180 // Decode the wire packet into the actual results. | 180 // Decode the wire packet into the actual results. |
| 181 SocketAddress real_remote_addr; | 181 SocketAddress real_remote_addr; |
| 182 size_t addrlength = | 182 size_t addrlength = UnpackAddressFromNAT(buf_, result, &real_remote_addr); |
| 183 UnpackAddressFromNAT(buf_, result, &real_remote_addr); | |
| 184 memcpy(data, buf_ + addrlength, result - addrlength); | 183 memcpy(data, buf_ + addrlength, result - addrlength); |
| 185 | 184 |
| 186 // Make sure this packet should be delivered before returning it. | 185 // Make sure this packet should be delivered before returning it. |
| 187 if (!connected_ || (real_remote_addr == remote_addr_)) { | 186 if (!connected_ || (real_remote_addr == remote_addr_)) { |
| 188 if (out_addr) | 187 if (out_addr) |
| 189 *out_addr = real_remote_addr; | 188 *out_addr = real_remote_addr; |
| 190 result = result - static_cast<int>(addrlength); | 189 result = result - static_cast<int>(addrlength); |
| 191 } else { | 190 } else { |
| 192 LOG(LS_ERROR) << "Dropping packet from unknown remote address: " | 191 LOG(LS_ERROR) << "Dropping packet from unknown remote address: " |
| 193 << real_remote_addr.ToString(); | 192 << real_remote_addr.ToString(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 223 } | 222 } |
| 224 int EstimateMTU(uint16* mtu) override { return socket_->EstimateMTU(mtu); } | 223 int EstimateMTU(uint16* mtu) override { return socket_->EstimateMTU(mtu); } |
| 225 int GetOption(Option opt, int* value) override { | 224 int GetOption(Option opt, int* value) override { |
| 226 return socket_->GetOption(opt, value); | 225 return socket_->GetOption(opt, value); |
| 227 } | 226 } |
| 228 int SetOption(Option opt, int value) override { | 227 int SetOption(Option opt, int value) override { |
| 229 return socket_->SetOption(opt, value); | 228 return socket_->SetOption(opt, value); |
| 230 } | 229 } |
| 231 | 230 |
| 232 void OnConnectEvent(AsyncSocket* socket) { | 231 void OnConnectEvent(AsyncSocket* socket) { |
| 233 // If we're NATed, we need to send a request with the real addr to use. | 232 // If we're NATed, we need to send a message with the real addr to use. |
| 234 ASSERT(socket == socket_); | 233 ASSERT(socket == socket_); |
| 235 if (server_addr_.IsNil()) { | 234 if (server_addr_.IsNil()) { |
| 236 connected_ = true; | 235 connected_ = true; |
| 237 SignalConnectEvent(this); | 236 SignalConnectEvent(this); |
| 238 } else { | 237 } else { |
| 239 SendConnectRequest(); | 238 SendConnectRequest(); |
| 240 } | 239 } |
| 241 } | 240 } |
| 242 void OnReadEvent(AsyncSocket* socket) { | 241 void OnReadEvent(AsyncSocket* socket) { |
| 243 // If we're NATed, we need to process the connect reply. | 242 // If we're NATed, we need to process the connect reply. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 262 void Grow(size_t new_size) { | 261 void Grow(size_t new_size) { |
| 263 if (size_ < new_size) { | 262 if (size_ < new_size) { |
| 264 delete[] buf_; | 263 delete[] buf_; |
| 265 size_ = new_size; | 264 size_ = new_size; |
| 266 buf_ = new char[size_]; | 265 buf_ = new char[size_]; |
| 267 } | 266 } |
| 268 } | 267 } |
| 269 | 268 |
| 270 // Sends the destination address to the server to tell it to connect. | 269 // Sends the destination address to the server to tell it to connect. |
| 271 void SendConnectRequest() { | 270 void SendConnectRequest() { |
| 272 char buf[256]; | 271 char buf[kNATEncodedIPv6AddressSize]; |
| 273 size_t length = PackAddressForNAT(buf, ARRAY_SIZE(buf), remote_addr_); | 272 size_t length = PackAddressForNAT(buf, ARRAY_SIZE(buf), remote_addr_); |
| 274 socket_->Send(buf, length); | 273 socket_->Send(buf, length); |
| 275 } | 274 } |
| 276 | 275 |
| 277 // Handles the byte sent back from the server and fires the appropriate event. | 276 // Handles the byte sent back from the server and fires the appropriate event. |
| 278 void HandleConnectReply() { | 277 void HandleConnectReply() { |
| 279 char code; | 278 char code; |
| 280 socket_->Recv(&code, sizeof(code)); | 279 socket_->Recv(&code, sizeof(code)); |
| 281 if (code == 0) { | 280 if (code == 0) { |
| 281 connected_ = true; |
| 282 SignalConnectEvent(this); | 282 SignalConnectEvent(this); |
| 283 } else { | 283 } else { |
| 284 Close(); | 284 Close(); |
| 285 SignalCloseEvent(this, code); | 285 SignalCloseEvent(this, code); |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 NATInternalSocketFactory* sf_; | 289 NATInternalSocketFactory* sf_; |
| 290 int family_; | 290 int family_; |
| 291 int type_; | 291 int type_; |
| 292 bool connected_; | 292 bool connected_; |
| 293 SocketAddress remote_addr_; | 293 SocketAddress remote_addr_; |
| 294 SocketAddress server_addr_; // address of the NAT server | 294 SocketAddress server_addr_; // address of the NAT server |
| 295 AsyncSocket* socket_; | 295 AsyncSocket* socket_; |
| 296 char* buf_; | 296 char* buf_; |
| 297 size_t size_; | 297 size_t size_; |
| 298 }; | 298 }; |
| 299 | 299 |
| 300 // NATSocketFactory | 300 // NATSocketFactory |
| 301 NATSocketFactory::NATSocketFactory(SocketFactory* factory, | 301 NATSocketFactory::NATSocketFactory(SocketFactory* factory, |
| 302 const SocketAddress& nat_addr) | 302 const SocketAddress& nat_udp_addr, |
| 303 : factory_(factory), nat_addr_(nat_addr) { | 303 const SocketAddress& nat_tcp_addr) |
| 304 : factory_(factory), nat_udp_addr_(nat_udp_addr), |
| 305 nat_tcp_addr_(nat_tcp_addr) { |
| 304 } | 306 } |
| 305 | 307 |
| 306 Socket* NATSocketFactory::CreateSocket(int type) { | 308 Socket* NATSocketFactory::CreateSocket(int type) { |
| 307 return CreateSocket(AF_INET, type); | 309 return CreateSocket(AF_INET, type); |
| 308 } | 310 } |
| 309 | 311 |
| 310 Socket* NATSocketFactory::CreateSocket(int family, int type) { | 312 Socket* NATSocketFactory::CreateSocket(int family, int type) { |
| 311 return new NATSocket(this, family, type); | 313 return new NATSocket(this, family, type); |
| 312 } | 314 } |
| 313 | 315 |
| 314 AsyncSocket* NATSocketFactory::CreateAsyncSocket(int type) { | 316 AsyncSocket* NATSocketFactory::CreateAsyncSocket(int type) { |
| 315 return CreateAsyncSocket(AF_INET, type); | 317 return CreateAsyncSocket(AF_INET, type); |
| 316 } | 318 } |
| 317 | 319 |
| 318 AsyncSocket* NATSocketFactory::CreateAsyncSocket(int family, int type) { | 320 AsyncSocket* NATSocketFactory::CreateAsyncSocket(int family, int type) { |
| 319 return new NATSocket(this, family, type); | 321 return new NATSocket(this, family, type); |
| 320 } | 322 } |
| 321 | 323 |
| 322 AsyncSocket* NATSocketFactory::CreateInternalSocket(int family, int type, | 324 AsyncSocket* NATSocketFactory::CreateInternalSocket(int family, int type, |
| 323 const SocketAddress& local_addr, SocketAddress* nat_addr) { | 325 const SocketAddress& local_addr, SocketAddress* nat_addr) { |
| 324 *nat_addr = nat_addr_; | 326 if (type == SOCK_STREAM) { |
| 327 *nat_addr = nat_tcp_addr_; |
| 328 } else { |
| 329 *nat_addr = nat_udp_addr_; |
| 330 } |
| 325 return factory_->CreateAsyncSocket(family, type); | 331 return factory_->CreateAsyncSocket(family, type); |
| 326 } | 332 } |
| 327 | 333 |
| 328 // NATSocketServer | 334 // NATSocketServer |
| 329 NATSocketServer::NATSocketServer(SocketServer* server) | 335 NATSocketServer::NATSocketServer(SocketServer* server) |
| 330 : server_(server), msg_queue_(NULL) { | 336 : server_(server), msg_queue_(NULL) { |
| 331 } | 337 } |
| 332 | 338 |
| 333 NATSocketServer::Translator* NATSocketServer::GetTranslator( | 339 NATSocketServer::Translator* NATSocketServer::GetTranslator( |
| 334 const SocketAddress& ext_ip) { | 340 const SocketAddress& ext_ip) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 server_->WakeUp(); | 384 server_->WakeUp(); |
| 379 } | 385 } |
| 380 | 386 |
| 381 AsyncSocket* NATSocketServer::CreateInternalSocket(int family, int type, | 387 AsyncSocket* NATSocketServer::CreateInternalSocket(int family, int type, |
| 382 const SocketAddress& local_addr, SocketAddress* nat_addr) { | 388 const SocketAddress& local_addr, SocketAddress* nat_addr) { |
| 383 AsyncSocket* socket = NULL; | 389 AsyncSocket* socket = NULL; |
| 384 Translator* nat = nats_.FindClient(local_addr); | 390 Translator* nat = nats_.FindClient(local_addr); |
| 385 if (nat) { | 391 if (nat) { |
| 386 socket = nat->internal_factory()->CreateAsyncSocket(family, type); | 392 socket = nat->internal_factory()->CreateAsyncSocket(family, type); |
| 387 *nat_addr = (type == SOCK_STREAM) ? | 393 *nat_addr = (type == SOCK_STREAM) ? |
| 388 nat->internal_tcp_address() : nat->internal_address(); | 394 nat->internal_tcp_address() : nat->internal_udp_address(); |
| 389 } else { | 395 } else { |
| 390 socket = server_->CreateAsyncSocket(family, type); | 396 socket = server_->CreateAsyncSocket(family, type); |
| 391 } | 397 } |
| 392 return socket; | 398 return socket; |
| 393 } | 399 } |
| 394 | 400 |
| 395 // NATSocketServer::Translator | 401 // NATSocketServer::Translator |
| 396 NATSocketServer::Translator::Translator( | 402 NATSocketServer::Translator::Translator( |
| 397 NATSocketServer* server, NATType type, const SocketAddress& int_ip, | 403 NATSocketServer* server, NATType type, const SocketAddress& int_ip, |
| 398 SocketFactory* ext_factory, const SocketAddress& ext_ip) | 404 SocketFactory* ext_factory, const SocketAddress& ext_ip) |
| 399 : server_(server) { | 405 : server_(server) { |
| 400 // Create a new private network, and a NATServer running on the private | 406 // Create a new private network, and a NATServer running on the private |
| 401 // network that bridges to the external network. Also tell the private | 407 // network that bridges to the external network. Also tell the private |
| 402 // network to use the same message queue as us. | 408 // network to use the same message queue as us. |
| 403 VirtualSocketServer* internal_server = new VirtualSocketServer(server_); | 409 VirtualSocketServer* internal_server = new VirtualSocketServer(server_); |
| 404 internal_server->SetMessageQueue(server_->queue()); | 410 internal_server->SetMessageQueue(server_->queue()); |
| 405 internal_factory_.reset(internal_server); | 411 internal_factory_.reset(internal_server); |
| 406 nat_server_.reset(new NATServer(type, internal_server, int_ip, | 412 nat_server_.reset(new NATServer(type, internal_server, int_ip, int_ip, |
| 407 ext_factory, ext_ip)); | 413 ext_factory, ext_ip)); |
| 408 } | 414 } |
| 409 | 415 |
| 410 NATSocketServer::Translator::~Translator() = default; | 416 NATSocketServer::Translator::~Translator() = default; |
| 411 | 417 |
| 412 NATSocketServer::Translator* NATSocketServer::Translator::GetTranslator( | 418 NATSocketServer::Translator* NATSocketServer::Translator::GetTranslator( |
| 413 const SocketAddress& ext_ip) { | 419 const SocketAddress& ext_ip) { |
| 414 return nats_.Get(ext_ip); | 420 return nats_.Get(ext_ip); |
| 415 } | 421 } |
| 416 | 422 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( | 492 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( |
| 487 const SocketAddress& int_ip) { | 493 const SocketAddress& int_ip) { |
| 488 Translator* nat = NULL; | 494 Translator* nat = NULL; |
| 489 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { | 495 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { |
| 490 nat = it->second->FindClient(int_ip); | 496 nat = it->second->FindClient(int_ip); |
| 491 } | 497 } |
| 492 return nat; | 498 return nat; |
| 493 } | 499 } |
| 494 | 500 |
| 495 } // namespace rtc | 501 } // namespace rtc |
| OLD | NEW |