OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/shared_impl/private/net_address_private_impl.h" | 5 #include "ppapi/shared_impl/private/net_address_private_impl.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... | |
41 namespace { | 41 namespace { |
42 | 42 |
43 // This assert fails on OpenBSD for an unknown reason at the moment. | 43 // This assert fails on OpenBSD for an unknown reason at the moment. |
44 #if !defined(OS_OPENBSD) | 44 #if !defined(OS_OPENBSD) |
45 // Make sure the storage in |PP_NetAddress_Private| is big enough. (Do it here | 45 // Make sure the storage in |PP_NetAddress_Private| is big enough. (Do it here |
46 // since the data is opaque elsewhere.) | 46 // since the data is opaque elsewhere.) |
47 COMPILE_ASSERT(sizeof(reinterpret_cast<PP_NetAddress_Private*>(0)->data) >= | 47 COMPILE_ASSERT(sizeof(reinterpret_cast<PP_NetAddress_Private*>(0)->data) >= |
48 sizeof(sockaddr_storage), PP_NetAddress_Private_data_too_small); | 48 sizeof(sockaddr_storage), PP_NetAddress_Private_data_too_small); |
49 #endif | 49 #endif |
50 | 50 |
51 uint16_t GetFamily(const PP_NetAddress_Private* addr) { | 51 PP_AddressFamily_Private GetFamily(const PP_NetAddress_Private* addr) { |
52 return reinterpret_cast<const sockaddr*>(addr->data)->sa_family; | 52 switch (reinterpret_cast<const sockaddr*>(addr->data)->sa_family) { |
53 case AF_INET: | |
54 return PP_ADDRESSFAMILY_IPV4; | |
55 case AF_INET6: | |
56 return PP_ADDRESSFAMILY_IPV6; | |
57 default: | |
58 return PP_ADDRESSFAMILY_UNSPECIFIED; | |
59 } | |
53 } | 60 } |
54 | 61 |
55 uint16_t GetPort(const PP_NetAddress_Private* addr) { | 62 uint16_t GetPort(const PP_NetAddress_Private* addr) { |
56 if (GetFamily(addr) == AF_INET) { | 63 if (GetFamily(addr) == PP_ADDRESSFAMILY_IPV4) { |
viettrungluu
2012/02/15 01:43:03
Now that GetFamily() has a nontrivial cost, you sh
yzshen1
2012/02/15 18:40:55
Done. (I did both.)
On 2012/02/15 01:43:03, viett
| |
57 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); | 64 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); |
58 return ntohs(a->sin_port); | 65 return ntohs(a->sin_port); |
59 } else if (GetFamily(addr) == AF_INET6) { | 66 } else if (GetFamily(addr) == PP_ADDRESSFAMILY_IPV6) { |
60 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); | 67 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); |
61 return ntohs(a->sin6_port); | 68 return ntohs(a->sin6_port); |
62 } | 69 } |
63 | 70 |
64 return 0; | 71 return 0; |
65 } | 72 } |
66 | 73 |
67 PP_Bool GetAddress(const PP_NetAddress_Private* addr, | 74 PP_Bool GetAddress(const PP_NetAddress_Private* addr, |
68 void* address, | 75 void* address, |
69 uint16_t address_size) { | 76 uint16_t address_size) { |
70 if (GetFamily(addr) == AF_INET) { | 77 if (GetFamily(addr) == PP_ADDRESSFAMILY_IPV4) { |
viettrungluu
2012/02/15 01:43:03
"
yzshen1
2012/02/15 18:40:55
Done.
| |
71 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); | 78 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); |
72 if (address_size >= sizeof(a->sin_addr.s_addr)) { | 79 if (address_size >= sizeof(a->sin_addr.s_addr)) { |
73 memcpy(address, &(a->sin_addr.s_addr), sizeof(a->sin_addr.s_addr)); | 80 memcpy(address, &(a->sin_addr.s_addr), sizeof(a->sin_addr.s_addr)); |
74 return PP_TRUE; | 81 return PP_TRUE; |
75 } | 82 } |
76 } else if (GetFamily(addr) == AF_INET6) { | 83 } else if (GetFamily(addr) == PP_ADDRESSFAMILY_IPV6) { |
77 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); | 84 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); |
78 if (address_size >= sizeof(a->sin6_addr.s6_addr)) { | 85 if (address_size >= sizeof(a->sin6_addr.s6_addr)) { |
79 memcpy(address, &(a->sin6_addr.s6_addr), sizeof(a->sin6_addr.s6_addr)); | 86 memcpy(address, &(a->sin6_addr.s6_addr), sizeof(a->sin6_addr.s6_addr)); |
80 return PP_TRUE; | 87 return PP_TRUE; |
81 } | 88 } |
82 } | 89 } |
83 | 90 |
84 return PP_FALSE; | 91 return PP_FALSE; |
85 } | 92 } |
86 | 93 |
87 PP_Bool AreHostsEqual(const PP_NetAddress_Private* addr1, | 94 PP_Bool AreHostsEqual(const PP_NetAddress_Private* addr1, |
88 const PP_NetAddress_Private* addr2) { | 95 const PP_NetAddress_Private* addr2) { |
89 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr1) || | 96 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr1) || |
90 !NetAddressPrivateImpl::ValidateNetAddress(*addr2)) | 97 !NetAddressPrivateImpl::ValidateNetAddress(*addr2)) |
91 return PP_FALSE; | 98 return PP_FALSE; |
92 | 99 |
93 if (GetFamily(addr1) != GetFamily(addr2)) | 100 if (GetFamily(addr1) != GetFamily(addr2)) |
viettrungluu
2012/02/15 01:43:03
Similarly, save the family of addr1 and compare it
yzshen1
2012/02/15 18:40:55
Done.
| |
94 return PP_FALSE; | 101 return PP_FALSE; |
95 | 102 |
96 if (GetFamily(addr1) == AF_INET) { | 103 if (GetFamily(addr1) == PP_ADDRESSFAMILY_IPV4) { |
97 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data); | 104 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data); |
98 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data); | 105 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data); |
99 return PP_FromBool(a1->sin_addr.s_addr == a2->sin_addr.s_addr); | 106 return PP_FromBool(a1->sin_addr.s_addr == a2->sin_addr.s_addr); |
100 } | 107 } |
101 | 108 |
102 if (GetFamily(addr1) == AF_INET6) { | 109 if (GetFamily(addr1) == PP_ADDRESSFAMILY_IPV6) { |
103 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); | 110 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); |
104 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); | 111 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); |
105 return PP_FromBool(a1->sin6_flowinfo == a2->sin6_flowinfo && | 112 return PP_FromBool(a1->sin6_flowinfo == a2->sin6_flowinfo && |
106 memcmp(&a1->sin6_addr, &a2->sin6_addr, | 113 memcmp(&a1->sin6_addr, &a2->sin6_addr, |
107 sizeof(a1->sin6_addr)) == 0 && | 114 sizeof(a1->sin6_addr)) == 0 && |
108 a1->sin6_scope_id == a2->sin6_scope_id); | 115 a1->sin6_scope_id == a2->sin6_scope_id); |
109 } | 116 } |
110 | 117 |
111 return PP_FALSE; | 118 return PP_FALSE; |
112 } | 119 } |
113 | 120 |
114 PP_Bool AreEqual(const PP_NetAddress_Private* addr1, | 121 PP_Bool AreEqual(const PP_NetAddress_Private* addr1, |
115 const PP_NetAddress_Private* addr2) { | 122 const PP_NetAddress_Private* addr2) { |
116 // |AreHostsEqual()| will also validate the addresses and return false if | 123 // |AreHostsEqual()| will also validate the addresses and return false if |
117 // either is invalid. | 124 // either is invalid. |
118 if (!AreHostsEqual(addr1, addr2)) | 125 if (!AreHostsEqual(addr1, addr2)) |
119 return PP_FALSE; | 126 return PP_FALSE; |
120 | 127 |
121 // Note: Here, we know that |addr1| and |addr2| have the same family. | 128 // Note: Here, we know that |addr1| and |addr2| have the same family. |
122 if (GetFamily(addr1) == AF_INET) { | 129 if (GetFamily(addr1) == PP_ADDRESSFAMILY_IPV4) { |
123 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data); | 130 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data); |
124 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data); | 131 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data); |
125 return PP_FromBool(a1->sin_port == a2->sin_port); | 132 return PP_FromBool(a1->sin_port == a2->sin_port); |
126 } | 133 } |
127 | 134 |
128 if (GetFamily(addr1) == AF_INET6) { | 135 if (GetFamily(addr1) == PP_ADDRESSFAMILY_IPV6) { |
129 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); | 136 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data); |
130 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); | 137 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data); |
131 return PP_FromBool(a1->sin6_port == a2->sin6_port); | 138 return PP_FromBool(a1->sin6_port == a2->sin6_port); |
132 } | 139 } |
133 | 140 |
134 return PP_FALSE; | 141 return PP_FALSE; |
135 } | 142 } |
136 | 143 |
137 #if defined(OS_WIN) || defined(OS_MACOSX) | 144 #if defined(OS_WIN) || defined(OS_MACOSX) |
138 std::string ConvertIPv4AddressToString(const sockaddr_in* a, | 145 std::string ConvertIPv4AddressToString(const sockaddr_in* a, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) | 237 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) |
231 return PP_MakeUndefined(); | 238 return PP_MakeUndefined(); |
232 | 239 |
233 #if defined(OS_WIN) || defined(OS_MACOSX) | 240 #if defined(OS_WIN) || defined(OS_MACOSX) |
234 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, | 241 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, |
235 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: | 242 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: |
236 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't | 243 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't |
237 // display the scope). | 244 // display the scope). |
238 // TODO(viettrungluu): Consider switching to this on Linux. | 245 // TODO(viettrungluu): Consider switching to this on Linux. |
239 switch (GetFamily(addr)) { | 246 switch (GetFamily(addr)) { |
240 case AF_INET: { | 247 case PP_ADDRESSFAMILY_IPV4: { |
241 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); | 248 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); |
242 return StringVar::StringToPPVar( | 249 return StringVar::StringToPPVar( |
243 ConvertIPv4AddressToString(a, !!include_port)); | 250 ConvertIPv4AddressToString(a, !!include_port)); |
244 } | 251 } |
245 case AF_INET6: { | 252 case PP_ADDRESSFAMILY_IPV6: { |
246 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); | 253 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); |
247 return StringVar::StringToPPVar( | 254 return StringVar::StringToPPVar( |
248 ConvertIPv6AddressToString(a, !!include_port)); | 255 ConvertIPv6AddressToString(a, !!include_port)); |
249 } | 256 } |
250 default: | 257 default: |
251 NOTREACHED(); | 258 NOTREACHED(); |
252 break; | 259 break; |
253 } | 260 } |
254 return PP_MakeUndefined(); | 261 return PP_MakeUndefined(); |
255 #else | 262 #else |
256 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data); | 263 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data); |
257 socklen_t l = addr->size; | 264 socklen_t l = addr->size; |
258 std::string description = | 265 std::string description = |
259 include_port ? net::NetAddressToStringWithPort(a, l) : | 266 include_port ? net::NetAddressToStringWithPort(a, l) : |
260 net::NetAddressToString(a, l); | 267 net::NetAddressToString(a, l); |
261 return StringVar::StringToPPVar(description); | 268 return StringVar::StringToPPVar(description); |
262 #endif | 269 #endif |
263 } | 270 } |
264 | 271 |
265 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, | 272 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, |
266 uint16_t port, | 273 uint16_t port, |
267 struct PP_NetAddress_Private* dest_addr) { | 274 struct PP_NetAddress_Private* dest_addr) { |
268 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) | 275 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) |
269 return PP_FALSE; | 276 return PP_FALSE; |
270 | 277 |
271 if (GetFamily(src_addr) == AF_INET) { | 278 if (GetFamily(src_addr) == PP_ADDRESSFAMILY_IPV4) { |
viettrungluu
2012/02/15 01:43:03
"
yzshen1
2012/02/15 18:40:55
Done.
| |
272 memmove(dest_addr, src_addr, sizeof(*src_addr)); | 279 memmove(dest_addr, src_addr, sizeof(*src_addr)); |
273 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port); | 280 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port); |
274 return PP_TRUE; | 281 return PP_TRUE; |
275 } | 282 } |
276 | 283 |
277 if (GetFamily(src_addr) == AF_INET6) { | 284 if (GetFamily(src_addr) == PP_ADDRESSFAMILY_IPV6) { |
278 memmove(dest_addr, src_addr, sizeof(*src_addr)); | 285 memmove(dest_addr, src_addr, sizeof(*src_addr)); |
279 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port); | 286 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port); |
280 return PP_TRUE; | 287 return PP_TRUE; |
281 } | 288 } |
282 | 289 |
283 return PP_FALSE; | 290 return PP_FALSE; |
284 } | 291 } |
285 | 292 |
286 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) { | 293 void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) { |
287 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0])); | 294 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0])); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 // static | 343 // static |
337 const PP_NetAddress_Private NetAddressPrivateImpl::kInvalidNetAddress = { 0 }; | 344 const PP_NetAddress_Private NetAddressPrivateImpl::kInvalidNetAddress = { 0 }; |
338 | 345 |
339 // static | 346 // static |
340 bool NetAddressPrivateImpl::ValidateNetAddress( | 347 bool NetAddressPrivateImpl::ValidateNetAddress( |
341 const PP_NetAddress_Private& addr) { | 348 const PP_NetAddress_Private& addr) { |
342 if (addr.size < sizeof(reinterpret_cast<sockaddr*>(0)->sa_family)) | 349 if (addr.size < sizeof(reinterpret_cast<sockaddr*>(0)->sa_family)) |
343 return false; | 350 return false; |
344 | 351 |
345 // TODO(viettrungluu): more careful validation? | 352 // TODO(viettrungluu): more careful validation? |
346 // Just do a size check for AF_INET. | 353 // Just do a size check for IPv4. |
347 if (GetFamily(&addr) == AF_INET && addr.size >= sizeof(sockaddr_in)) | 354 if (GetFamily(&addr) == PP_ADDRESSFAMILY_IPV4 && |
viettrungluu
2012/02/15 01:43:03
"
yzshen1
2012/02/15 18:40:55
Done.
| |
355 addr.size >= sizeof(sockaddr_in)) { | |
348 return true; | 356 return true; |
357 } | |
349 | 358 |
350 // Ditto for AF_INET6. | 359 // Ditto for IPv6. |
351 if (GetFamily(&addr) == AF_INET6 && addr.size >= sizeof(sockaddr_in6)) | 360 if (GetFamily(&addr) == PP_ADDRESSFAMILY_IPV6 && |
361 addr.size >= sizeof(sockaddr_in6)) { | |
352 return true; | 362 return true; |
363 } | |
353 | 364 |
354 // Reject everything else. | 365 // Reject everything else. |
355 return false; | 366 return false; |
356 } | 367 } |
357 | 368 |
358 // static | 369 // static |
359 bool NetAddressPrivateImpl::SockaddrToNetAddress( | 370 bool NetAddressPrivateImpl::SockaddrToNetAddress( |
360 const sockaddr* sa, | 371 const sockaddr* sa, |
361 uint32_t sa_length, | 372 uint32_t sa_length, |
362 PP_NetAddress_Private* net_addr) { | 373 PP_NetAddress_Private* net_addr) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 net::IPEndPoint ip_end_point; | 425 net::IPEndPoint ip_end_point; |
415 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point)) | 426 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point)) |
416 return false; | 427 return false; |
417 | 428 |
418 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(), | 429 *address_list = net::AddressList::CreateFromIPAddress(ip_end_point.address(), |
419 ip_end_point.port()); | 430 ip_end_point.port()); |
420 return true; | 431 return true; |
421 } | 432 } |
422 | 433 |
423 } // namespace ppapi | 434 } // namespace ppapi |
OLD | NEW |