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 |
11 #include "webrtc/base/natsocketfactory.h" | 11 #include "webrtc/base/natsocketfactory.h" |
12 | 12 |
13 #include "webrtc/base/logging.h" | 13 #include "webrtc/base/logging.h" |
14 #include "webrtc/base/natserver.h" | 14 #include "webrtc/base/natserver.h" |
15 #include "webrtc/base/virtualsocketserver.h" | 15 #include "webrtc/base/virtualsocketserver.h" |
16 | 16 |
17 namespace rtc { | 17 namespace rtc { |
18 | 18 |
19 // Packs the given socketaddress into the buffer in buf, in the quasi-STUN | 19 // Packs the given socketaddress into the buffer in buf, in the quasi-STUN |
20 // format that the natserver uses. | 20 // format that the natserver uses. |
21 // Returns 0 if an invalid address is passed. | 21 // Returns 0 if an invalid address is passed. |
22 size_t PackAddressForNAT(char* buf, size_t buf_size, | 22 size_t PackAddressForNAT(char* buf, size_t buf_size, |
23 const SocketAddress& remote_addr) { | 23 const SocketAddress& remote_addr) { |
24 const IPAddress& ip = remote_addr.ipaddr(); | 24 const IPAddress& ip = remote_addr.ipaddr(); |
25 int family = ip.family(); | 25 int family = ip.family(); |
26 buf[0] = 0; | 26 buf[0] = 0; |
27 buf[1] = family; | 27 buf[1] = family; |
28 // Writes the port. | 28 // Writes the port. |
29 *(reinterpret_cast<uint16*>(&buf[2])) = HostToNetwork16(remote_addr.port()); | 29 *(reinterpret_cast<uint16_t*>(&buf[2])) = HostToNetwork16(remote_addr.port()); |
30 if (family == AF_INET) { | 30 if (family == AF_INET) { |
31 ASSERT(buf_size >= kNATEncodedIPv4AddressSize); | 31 ASSERT(buf_size >= kNATEncodedIPv4AddressSize); |
32 in_addr v4addr = ip.ipv4_address(); | 32 in_addr v4addr = ip.ipv4_address(); |
33 memcpy(&buf[4], &v4addr, kNATEncodedIPv4AddressSize - 4); | 33 memcpy(&buf[4], &v4addr, kNATEncodedIPv4AddressSize - 4); |
34 return kNATEncodedIPv4AddressSize; | 34 return kNATEncodedIPv4AddressSize; |
35 } else if (family == AF_INET6) { | 35 } else if (family == AF_INET6) { |
36 ASSERT(buf_size >= kNATEncodedIPv6AddressSize); | 36 ASSERT(buf_size >= kNATEncodedIPv6AddressSize); |
37 in6_addr v6addr = ip.ipv6_address(); | 37 in6_addr v6addr = ip.ipv6_address(); |
38 memcpy(&buf[4], &v6addr, kNATEncodedIPv6AddressSize - 4); | 38 memcpy(&buf[4], &v6addr, kNATEncodedIPv6AddressSize - 4); |
39 return kNATEncodedIPv6AddressSize; | 39 return kNATEncodedIPv6AddressSize; |
40 } | 40 } |
41 return 0U; | 41 return 0U; |
42 } | 42 } |
43 | 43 |
44 // Decodes the remote address from a packet that has been encoded with the nat's | 44 // Decodes the remote address from a packet that has been encoded with the nat's |
45 // quasi-STUN format. Returns the length of the address (i.e., the offset into | 45 // quasi-STUN format. Returns the length of the address (i.e., the offset into |
46 // data where the original packet starts). | 46 // data where the original packet starts). |
47 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size, | 47 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size, |
48 SocketAddress* remote_addr) { | 48 SocketAddress* remote_addr) { |
49 ASSERT(buf_size >= 8); | 49 ASSERT(buf_size >= 8); |
50 ASSERT(buf[0] == 0); | 50 ASSERT(buf[0] == 0); |
51 int family = buf[1]; | 51 int family = buf[1]; |
52 uint16 port = NetworkToHost16(*(reinterpret_cast<const uint16*>(&buf[2]))); | 52 uint16_t port = |
| 53 NetworkToHost16(*(reinterpret_cast<const uint16_t*>(&buf[2]))); |
53 if (family == AF_INET) { | 54 if (family == AF_INET) { |
54 const in_addr* v4addr = reinterpret_cast<const in_addr*>(&buf[4]); | 55 const in_addr* v4addr = reinterpret_cast<const in_addr*>(&buf[4]); |
55 *remote_addr = SocketAddress(IPAddress(*v4addr), port); | 56 *remote_addr = SocketAddress(IPAddress(*v4addr), port); |
56 return kNATEncodedIPv4AddressSize; | 57 return kNATEncodedIPv4AddressSize; |
57 } else if (family == AF_INET6) { | 58 } else if (family == AF_INET6) { |
58 ASSERT(buf_size >= 20); | 59 ASSERT(buf_size >= 20); |
59 const in6_addr* v6addr = reinterpret_cast<const in6_addr*>(&buf[4]); | 60 const in6_addr* v6addr = reinterpret_cast<const in6_addr*>(&buf[4]); |
60 *remote_addr = SocketAddress(IPAddress(*v6addr), port); | 61 *remote_addr = SocketAddress(IPAddress(*v6addr), port); |
61 return kNATEncodedIPv6AddressSize; | 62 return kNATEncodedIPv6AddressSize; |
62 } | 63 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 214 |
214 int Listen(int backlog) override { return socket_->Listen(backlog); } | 215 int Listen(int backlog) override { return socket_->Listen(backlog); } |
215 AsyncSocket* Accept(SocketAddress* paddr) override { | 216 AsyncSocket* Accept(SocketAddress* paddr) override { |
216 return socket_->Accept(paddr); | 217 return socket_->Accept(paddr); |
217 } | 218 } |
218 int GetError() const override { return socket_->GetError(); } | 219 int GetError() const override { return socket_->GetError(); } |
219 void SetError(int error) override { socket_->SetError(error); } | 220 void SetError(int error) override { socket_->SetError(error); } |
220 ConnState GetState() const override { | 221 ConnState GetState() const override { |
221 return connected_ ? CS_CONNECTED : CS_CLOSED; | 222 return connected_ ? CS_CONNECTED : CS_CLOSED; |
222 } | 223 } |
223 int EstimateMTU(uint16* mtu) override { return socket_->EstimateMTU(mtu); } | 224 int EstimateMTU(uint16_t* mtu) override { return socket_->EstimateMTU(mtu); } |
224 int GetOption(Option opt, int* value) override { | 225 int GetOption(Option opt, int* value) override { |
225 return socket_->GetOption(opt, value); | 226 return socket_->GetOption(opt, value); |
226 } | 227 } |
227 int SetOption(Option opt, int value) override { | 228 int SetOption(Option opt, int value) override { |
228 return socket_->SetOption(opt, value); | 229 return socket_->SetOption(opt, value); |
229 } | 230 } |
230 | 231 |
231 void OnConnectEvent(AsyncSocket* socket) { | 232 void OnConnectEvent(AsyncSocket* socket) { |
232 // If we're NATed, we need to send a message with the real addr to use. | 233 // If we're NATed, we need to send a message with the real addr to use. |
233 ASSERT(socket == socket_); | 234 ASSERT(socket == socket_); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( | 493 NATSocketServer::Translator* NATSocketServer::TranslatorMap::FindClient( |
493 const SocketAddress& int_ip) { | 494 const SocketAddress& int_ip) { |
494 Translator* nat = NULL; | 495 Translator* nat = NULL; |
495 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { | 496 for (TranslatorMap::iterator it = begin(); it != end() && !nat; ++it) { |
496 nat = it->second->FindClient(int_ip); | 497 nat = it->second->FindClient(int_ip); |
497 } | 498 } |
498 return nat; | 499 return nat; |
499 } | 500 } |
500 | 501 |
501 } // namespace rtc | 502 } // namespace rtc |
OLD | NEW |