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 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <winsock2.h> | 9 #include <winsock2.h> |
10 #include <ws2tcpip.h> | 10 #include <ws2tcpip.h> |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // call into base. So we implement the entire interface here, using the thunk | 47 // call into base. So we implement the entire interface here, using the thunk |
48 // namespace so it magically gets hooked up in the proper places. | 48 // namespace so it magically gets hooked up in the proper places. |
49 | 49 |
50 namespace ppapi { | 50 namespace ppapi { |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 // Define our own net-host-net conversion, rather than reuse the one in | 54 // Define our own net-host-net conversion, rather than reuse the one in |
55 // base/sys_byteorder.h, to simplify the NaCl port. NaCl has no byte swap | 55 // base/sys_byteorder.h, to simplify the NaCl port. NaCl has no byte swap |
56 // primitives. | 56 // primitives. |
57 uint16 ConvertNetEndian16(uint16 x) { | 57 uint16 ConvertFromNetEndian16(uint16 x) { |
58 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 58 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
59 return (x << 8) | (x >> 8); | 59 return (x << 8) | (x >> 8); |
60 #else | 60 #else |
| 61 return x; |
| 62 #endif |
| 63 } |
| 64 |
| 65 uint16 ConvertToNetEndian16(uint16 x) { |
| 66 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 67 return (x << 8) | (x >> 8); |
| 68 #else |
61 return x; | 69 return x; |
62 #endif | 70 #endif |
63 } | 71 } |
64 | 72 |
65 static const size_t kIPv4AddressSize = 4; | 73 static const size_t kIPv4AddressSize = 4; |
66 static const size_t kIPv6AddressSize = 16; | 74 static const size_t kIPv6AddressSize = 16; |
67 | 75 |
68 // This structure is a platform-independent representation of a network address. | 76 // This structure is a platform-independent representation of a network address. |
69 // It is a private format that we embed in PP_NetAddress_Private and is NOT part | 77 // It is a private format that we embed in PP_NetAddress_Private and is NOT part |
70 // of the stable Pepper API. | 78 // of the stable Pepper API. |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 } | 262 } |
255 } | 263 } |
256 | 264 |
257 bool need_sep = false; // Whether the next item needs a ':' to separate. | 265 bool need_sep = false; // Whether the next item needs a ':' to separate. |
258 for (int i = 0; i < 8;) { | 266 for (int i = 0; i < 8;) { |
259 if (longest_length > 1 && i == longest_start) { | 267 if (longest_length > 1 && i == longest_start) { |
260 description.append("::"); | 268 description.append("::"); |
261 need_sep = false; | 269 need_sep = false; |
262 i += longest_length; | 270 i += longest_length; |
263 } else { | 271 } else { |
264 uint16_t v = ConvertNetEndian16(address16[i]); | 272 uint16_t v = ConvertFromNetEndian16(address16[i]); |
265 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); | 273 base::StringAppendF(&description, need_sep ? ":%x" : "%x", v); |
266 need_sep = true; | 274 need_sep = true; |
267 i++; | 275 i++; |
268 } | 276 } |
269 } | 277 } |
270 } | 278 } |
271 | 279 |
272 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". | 280 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". |
273 if (net_addr->scope_id != 0) | 281 if (net_addr->scope_id != 0) |
274 base::StringAppendF(&description, "%%%u", net_addr->scope_id); | 282 base::StringAppendF(&description, "%%%u", net_addr->scope_id); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 | 420 |
413 // Our platform neutral format stores ports in host order, not net order, | 421 // Our platform neutral format stores ports in host order, not net order, |
414 // so convert them here. | 422 // so convert them here. |
415 NetAddress* net_addr = InitNetAddress(addr); | 423 NetAddress* net_addr = InitNetAddress(addr); |
416 switch (sa->sa_family) { | 424 switch (sa->sa_family) { |
417 case AF_INET: { | 425 case AF_INET: { |
418 const struct sockaddr_in* addr4 = | 426 const struct sockaddr_in* addr4 = |
419 reinterpret_cast<const struct sockaddr_in*>(sa); | 427 reinterpret_cast<const struct sockaddr_in*>(sa); |
420 net_addr->is_valid = true; | 428 net_addr->is_valid = true; |
421 net_addr->is_ipv6 = false; | 429 net_addr->is_ipv6 = false; |
422 net_addr->port = ConvertNetEndian16(addr4->sin_port); | 430 net_addr->port = ConvertFromNetEndian16(addr4->sin_port); |
423 memcpy(net_addr->address, &addr4->sin_addr.s_addr, kIPv4AddressSize); | 431 memcpy(net_addr->address, &addr4->sin_addr.s_addr, kIPv4AddressSize); |
424 break; | 432 break; |
425 } | 433 } |
426 case AF_INET6: { | 434 case AF_INET6: { |
427 const struct sockaddr_in6* addr6 = | 435 const struct sockaddr_in6* addr6 = |
428 reinterpret_cast<const struct sockaddr_in6*>(sa); | 436 reinterpret_cast<const struct sockaddr_in6*>(sa); |
429 net_addr->is_valid = true; | 437 net_addr->is_valid = true; |
430 net_addr->is_ipv6 = true; | 438 net_addr->is_ipv6 = true; |
431 net_addr->port = ConvertNetEndian16(addr6->sin6_port); | 439 net_addr->port = ConvertFromNetEndian16(addr6->sin6_port); |
432 net_addr->flow_info = addr6->sin6_flowinfo; | 440 net_addr->flow_info = addr6->sin6_flowinfo; |
433 net_addr->scope_id = addr6->sin6_scope_id; | 441 net_addr->scope_id = addr6->sin6_scope_id; |
434 memcpy(net_addr->address, addr6->sin6_addr.s6_addr, kIPv6AddressSize); | 442 memcpy(net_addr->address, addr6->sin6_addr.s6_addr, kIPv6AddressSize); |
435 break; | 443 break; |
436 } | 444 } |
437 default: | 445 default: |
438 // InitNetAddress sets net_addr->is_valid to false. | 446 // InitNetAddress sets net_addr->is_valid to false. |
439 return false; | 447 return false; |
440 } | 448 } |
441 return true;} | 449 return true;} |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 | 510 |
503 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, | 511 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, |
504 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: | 512 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: |
505 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't | 513 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't |
506 // display the scope). | 514 // display the scope). |
507 if (net_addr->is_ipv6) | 515 if (net_addr->is_ipv6) |
508 return ConvertIPv6AddressToString(net_addr, include_port); | 516 return ConvertIPv6AddressToString(net_addr, include_port); |
509 return ConvertIPv4AddressToString(net_addr, include_port); | 517 return ConvertIPv4AddressToString(net_addr, include_port); |
510 } | 518 } |
511 | 519 |
| 520 // static |
| 521 void NetAddressPrivateImpl::CreateNetAddressPrivateFromIPv4Address( |
| 522 const PP_NetAddress_IPv4_Dev& ipv4_addr, |
| 523 PP_NetAddress_Private* addr) { |
| 524 CreateFromIPv4Address(ipv4_addr.addr, ConvertFromNetEndian16(ipv4_addr.port), |
| 525 addr); |
| 526 } |
| 527 |
| 528 // static |
| 529 void NetAddressPrivateImpl::CreateNetAddressPrivateFromIPv6Address( |
| 530 const PP_NetAddress_IPv6_Dev& ipv6_addr, |
| 531 PP_NetAddress_Private* addr) { |
| 532 CreateFromIPv6Address(ipv6_addr.addr, 0, |
| 533 ConvertFromNetEndian16(ipv6_addr.port), addr); |
| 534 } |
| 535 |
| 536 // static |
| 537 PP_NetAddress_Family_Dev NetAddressPrivateImpl::GetFamilyFromNetAddressPrivate( |
| 538 const PP_NetAddress_Private& addr) { |
| 539 const NetAddress* net_addr = ToNetAddress(&addr); |
| 540 if (!IsValid(net_addr)) |
| 541 return PP_NETADDRESS_FAMILY_UNSPECIFIED; |
| 542 return net_addr->is_ipv6 ? PP_NETADDRESS_FAMILY_IPV6 : |
| 543 PP_NETADDRESS_FAMILY_IPV4; |
| 544 } |
| 545 |
| 546 // static |
| 547 bool NetAddressPrivateImpl::DescribeNetAddressPrivateAsIPv4Address( |
| 548 const PP_NetAddress_Private& addr, |
| 549 PP_NetAddress_IPv4_Dev* ipv4_addr) { |
| 550 if (!ipv4_addr) |
| 551 return false; |
| 552 |
| 553 const NetAddress* net_addr = ToNetAddress(&addr); |
| 554 if (!IsValid(net_addr) || net_addr->is_ipv6) |
| 555 return false; |
| 556 |
| 557 ipv4_addr->port = ConvertToNetEndian16(net_addr->port); |
| 558 |
| 559 COMPILE_ASSERT(sizeof(ipv4_addr->addr) == kIPv4AddressSize, |
| 560 mismatched_IPv4_address_size); |
| 561 memcpy(ipv4_addr->addr, net_addr->address, kIPv4AddressSize); |
| 562 |
| 563 ipv4_addr->unused_padding = 0; |
| 564 return true; |
| 565 } |
| 566 |
| 567 // static |
| 568 bool NetAddressPrivateImpl::DescribeNetAddressPrivateAsIPv6Address( |
| 569 const PP_NetAddress_Private& addr, |
| 570 PP_NetAddress_IPv6_Dev* ipv6_addr) { |
| 571 if (!ipv6_addr) |
| 572 return false; |
| 573 |
| 574 const NetAddress* net_addr = ToNetAddress(&addr); |
| 575 if (!IsValid(net_addr) || !net_addr->is_ipv6) |
| 576 return false; |
| 577 |
| 578 ipv6_addr->port = ConvertToNetEndian16(net_addr->port); |
| 579 |
| 580 COMPILE_ASSERT(sizeof(ipv6_addr->addr) == kIPv6AddressSize, |
| 581 mismatched_IPv6_address_size); |
| 582 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize); |
| 583 |
| 584 ipv6_addr->unused_padding = 0; |
| 585 return true; |
| 586 } |
| 587 |
512 } // namespace ppapi | 588 } // namespace ppapi |
OLD | NEW |