| 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 #include <stddef.h> |
| 8 #include <windows.h> | |
| 9 #include <winsock2.h> | |
| 10 #include <ws2tcpip.h> | |
| 11 #elif defined(OS_POSIX) && !defined(OS_NACL) | |
| 12 #include <arpa/inet.h> | |
| 13 #include <sys/socket.h> | |
| 14 #include <sys/types.h> | |
| 15 #endif | |
| 16 | |
| 17 #include <string.h> | 8 #include <string.h> |
| 18 | 9 |
| 19 #include <string> | 10 #include <string> |
| 20 | 11 |
| 21 #include "base/basictypes.h" | |
| 22 #include "base/logging.h" | 12 #include "base/logging.h" |
| 23 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 24 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 25 #include "ppapi/c/pp_var.h" | 15 #include "ppapi/c/pp_var.h" |
| 26 #include "ppapi/c/private/ppb_net_address_private.h" | 16 #include "ppapi/c/private/ppb_net_address_private.h" |
| 27 #include "ppapi/shared_impl/proxy_lock.h" | 17 #include "ppapi/shared_impl/proxy_lock.h" |
| 28 #include "ppapi/shared_impl/var.h" | 18 #include "ppapi/shared_impl/var.h" |
| 29 #include "ppapi/thunk/thunk.h" | 19 #include "ppapi/thunk/thunk.h" |
| 30 | 20 |
| 21 #if defined(OS_WIN) |
| 22 #include <windows.h> |
| 23 #include <winsock2.h> |
| 24 #include <ws2tcpip.h> |
| 25 #elif defined(OS_POSIX) && !defined(OS_NACL) |
| 26 #include <arpa/inet.h> |
| 27 #include <sys/socket.h> |
| 28 #include <sys/types.h> |
| 29 #endif |
| 30 |
| 31 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
| 32 // This is a bit evil, but it's standard operating procedure for |s6_addr|.... | 32 // This is a bit evil, but it's standard operating procedure for |s6_addr|.... |
| 33 #define s6_addr16 __u6_addr.__u6_addr16 | 33 #define s6_addr16 __u6_addr.__u6_addr16 |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
| 37 // The type of |sockaddr::sa_family|. | 37 // The type of |sockaddr::sa_family|. |
| 38 typedef ADDRESS_FAMILY sa_family_t; | 38 typedef ADDRESS_FAMILY sa_family_t; |
| 39 | 39 |
| 40 #define s6_addr16 u.Word | 40 #define s6_addr16 u.Word |
| 41 #define ntohs(x) _byteswap_ushort(x) | 41 #define ntohs(x) _byteswap_ushort(x) |
| 42 #define htons(x) _byteswap_ushort(x) | 42 #define htons(x) _byteswap_ushort(x) |
| 43 #endif // defined(OS_WIN) | 43 #endif // defined(OS_WIN) |
| 44 | 44 |
| 45 // The net address interface doesn't have a normal C -> C++ thunk since it | 45 // The net address interface doesn't have a normal C -> C++ thunk since it |
| 46 // doesn't actually have any proxy wrapping or associated objects; it's just a | 46 // doesn't actually have any proxy wrapping or associated objects; it's just a |
| 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 ConvertFromNetEndian16(uint16 x) { | 57 uint16_t ConvertFromNetEndian16(uint16_t 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; | 61 return x; |
| 62 #endif | 62 #endif |
| 63 } | 63 } |
| 64 | 64 |
| 65 uint16 ConvertToNetEndian16(uint16 x) { | 65 uint16_t ConvertToNetEndian16(uint16_t x) { |
| 66 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 66 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 67 return (x << 8) | (x >> 8); | 67 return (x << 8) | (x >> 8); |
| 68 #else | 68 #else |
| 69 return x; | 69 return x; |
| 70 #endif | 70 #endif |
| 71 } | 71 } |
| 72 | 72 |
| 73 static const size_t kIPv4AddressSize = 4; | 73 static const size_t kIPv4AddressSize = 4; |
| 74 static const size_t kIPv6AddressSize = 16; | 74 static const size_t kIPv6AddressSize = 16; |
| 75 | 75 |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 } | 444 } |
| 445 default: | 445 default: |
| 446 // InitNetAddress sets net_addr->is_valid to false. | 446 // InitNetAddress sets net_addr->is_valid to false. |
| 447 return false; | 447 return false; |
| 448 } | 448 } |
| 449 return true;} | 449 return true;} |
| 450 | 450 |
| 451 // static | 451 // static |
| 452 bool NetAddressPrivateImpl::IPEndPointToNetAddress( | 452 bool NetAddressPrivateImpl::IPEndPointToNetAddress( |
| 453 const std::vector<unsigned char>& address, | 453 const std::vector<unsigned char>& address, |
| 454 uint16 port, | 454 uint16_t port, |
| 455 PP_NetAddress_Private* addr) { | 455 PP_NetAddress_Private* addr) { |
| 456 if (!addr) | 456 if (!addr) |
| 457 return false; | 457 return false; |
| 458 | 458 |
| 459 NetAddress* net_addr = InitNetAddress(addr); | 459 NetAddress* net_addr = InitNetAddress(addr); |
| 460 switch (address.size()) { | 460 switch (address.size()) { |
| 461 case kIPv4AddressSize: { | 461 case kIPv4AddressSize: { |
| 462 net_addr->is_valid = true; | 462 net_addr->is_valid = true; |
| 463 net_addr->is_ipv6 = false; | 463 net_addr->is_ipv6 = false; |
| 464 net_addr->port = port; | 464 net_addr->port = port; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 477 return false; | 477 return false; |
| 478 } | 478 } |
| 479 | 479 |
| 480 return true; | 480 return true; |
| 481 } | 481 } |
| 482 | 482 |
| 483 // static | 483 // static |
| 484 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( | 484 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( |
| 485 const PP_NetAddress_Private& addr, | 485 const PP_NetAddress_Private& addr, |
| 486 std::vector<unsigned char>* address, | 486 std::vector<unsigned char>* address, |
| 487 uint16* port) { | 487 uint16_t* port) { |
| 488 if (!address || !port) | 488 if (!address || !port) |
| 489 return false; | 489 return false; |
| 490 | 490 |
| 491 const NetAddress* net_addr = ToNetAddress(&addr); | 491 const NetAddress* net_addr = ToNetAddress(&addr); |
| 492 if (!IsValid(net_addr)) | 492 if (!IsValid(net_addr)) |
| 493 return false; | 493 return false; |
| 494 | 494 |
| 495 *port = net_addr->port; | 495 *port = net_addr->port; |
| 496 size_t address_size = GetAddressSize(net_addr); | 496 size_t address_size = GetAddressSize(net_addr); |
| 497 address->assign(&net_addr->address[0], &net_addr->address[address_size]); | 497 address->assign(&net_addr->address[0], &net_addr->address[address_size]); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 ipv6_addr->port = ConvertToNetEndian16(net_addr->port); | 583 ipv6_addr->port = ConvertToNetEndian16(net_addr->port); |
| 584 | 584 |
| 585 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize, | 585 static_assert(sizeof(ipv6_addr->addr) == kIPv6AddressSize, |
| 586 "mismatched IPv6 address size"); | 586 "mismatched IPv6 address size"); |
| 587 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize); | 587 memcpy(ipv6_addr->addr, net_addr->address, kIPv6AddressSize); |
| 588 | 588 |
| 589 return true; | 589 return true; |
| 590 } | 590 } |
| 591 | 591 |
| 592 } // namespace ppapi | 592 } // namespace ppapi |
| OLD | NEW |