OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/quic/quic_socket_address_coder.h" | 5 #include "net/quic/quic_socket_address_coder.h" |
6 | 6 |
7 #include "net/base/sys_addrinfo.h" | 7 #include "net/base/sys_addrinfo.h" |
8 | 8 |
9 using std::string; | 9 using std::string; |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // For convenience, the values of these constants match the values of AF_INET | 15 // For convenience, the values of these constants match the values of AF_INET |
16 // and AF_INET6 on Linux. | 16 // and AF_INET6 on Linux. |
17 const uint16 kIPv4 = 2; | 17 const uint16_t kIPv4 = 2; |
18 const uint16 kIPv6 = 10; | 18 const uint16_t kIPv6 = 10; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 QuicSocketAddressCoder::QuicSocketAddressCoder() {} | 22 QuicSocketAddressCoder::QuicSocketAddressCoder() {} |
23 | 23 |
24 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint& address) | 24 QuicSocketAddressCoder::QuicSocketAddressCoder(const IPEndPoint& address) |
25 : address_(address) {} | 25 : address_(address) {} |
26 | 26 |
27 QuicSocketAddressCoder::~QuicSocketAddressCoder() {} | 27 QuicSocketAddressCoder::~QuicSocketAddressCoder() {} |
28 | 28 |
29 string QuicSocketAddressCoder::Encode() const { | 29 string QuicSocketAddressCoder::Encode() const { |
30 string serialized; | 30 string serialized; |
31 uint16 address_family; | 31 uint16_t address_family; |
32 switch (address_.GetSockAddrFamily()) { | 32 switch (address_.GetSockAddrFamily()) { |
33 case AF_INET: | 33 case AF_INET: |
34 address_family = kIPv4; | 34 address_family = kIPv4; |
35 break; | 35 break; |
36 case AF_INET6: | 36 case AF_INET6: |
37 address_family = kIPv6; | 37 address_family = kIPv6; |
38 break; | 38 break; |
39 default: | 39 default: |
40 return serialized; | 40 return serialized; |
41 } | 41 } |
42 serialized.append(reinterpret_cast<const char*>(&address_family), | 42 serialized.append(reinterpret_cast<const char*>(&address_family), |
43 sizeof(address_family)); | 43 sizeof(address_family)); |
44 serialized.append(IPAddressToPackedString(address_.address())); | 44 serialized.append(IPAddressToPackedString(address_.address())); |
45 uint16 port = address_.port(); | 45 uint16_t port = address_.port(); |
46 serialized.append(reinterpret_cast<const char*>(&port), sizeof(port)); | 46 serialized.append(reinterpret_cast<const char*>(&port), sizeof(port)); |
47 return serialized; | 47 return serialized; |
48 } | 48 } |
49 | 49 |
50 bool QuicSocketAddressCoder::Decode(const char* data, size_t length) { | 50 bool QuicSocketAddressCoder::Decode(const char* data, size_t length) { |
51 uint16 address_family; | 51 uint16_t address_family; |
52 if (length < sizeof(address_family)) { | 52 if (length < sizeof(address_family)) { |
53 return false; | 53 return false; |
54 } | 54 } |
55 memcpy(&address_family, data, sizeof(address_family)); | 55 memcpy(&address_family, data, sizeof(address_family)); |
56 data += sizeof(address_family); | 56 data += sizeof(address_family); |
57 length -= sizeof(address_family); | 57 length -= sizeof(address_family); |
58 | 58 |
59 size_t ip_length; | 59 size_t ip_length; |
60 switch (address_family) { | 60 switch (address_family) { |
61 case kIPv4: | 61 case kIPv4: |
62 ip_length = kIPv4AddressSize; | 62 ip_length = kIPv4AddressSize; |
63 break; | 63 break; |
64 case kIPv6: | 64 case kIPv6: |
65 ip_length = kIPv6AddressSize; | 65 ip_length = kIPv6AddressSize; |
66 break; | 66 break; |
67 default: | 67 default: |
68 return false; | 68 return false; |
69 } | 69 } |
70 if (length < ip_length) { | 70 if (length < ip_length) { |
71 return false; | 71 return false; |
72 } | 72 } |
73 IPAddressNumber ip(ip_length); | 73 IPAddressNumber ip(ip_length); |
74 memcpy(&ip[0], data, ip_length); | 74 memcpy(&ip[0], data, ip_length); |
75 data += ip_length; | 75 data += ip_length; |
76 length -= ip_length; | 76 length -= ip_length; |
77 | 77 |
78 uint16 port; | 78 uint16_t port; |
79 if (length != sizeof(port)) { | 79 if (length != sizeof(port)) { |
80 return false; | 80 return false; |
81 } | 81 } |
82 memcpy(&port, data, length); | 82 memcpy(&port, data, length); |
83 | 83 |
84 address_ = IPEndPoint(ip, port); | 84 address_ = IPEndPoint(ip, port); |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 } // namespace net | 88 } // namespace net |
OLD | NEW |