diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-22 22:41:06 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-22 22:41:06 +0000 |
commit | b13deef887384a4d8ff51ff6027128d0ffeeacfc (patch) | |
tree | a62578ff4d767ccc81657c50d97514086ec3c311 | |
parent | f497f0c6437d077911e6569789616754528e8a51 (diff) | |
download | chromium_src-b13deef887384a4d8ff51ff6027128d0ffeeacfc.zip chromium_src-b13deef887384a4d8ff51ff6027128d0ffeeacfc.tar.gz chromium_src-b13deef887384a4d8ff51ff6027128d0ffeeacfc.tar.bz2 |
Add CreateFromIPv[46]Address() in PPB_NetAddress_Private.
Review URL: http://codereview.chromium.org/9722008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128322 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/test/ui/ppapi_uitest.cc | 2 | ||||
-rw-r--r-- | ppapi/api/private/ppb_net_address_private.idl | 76 | ||||
-rw-r--r-- | ppapi/c/private/ppb_net_address_private.h | 55 | ||||
-rw-r--r-- | ppapi/cpp/private/net_address_private.cc | 85 | ||||
-rw-r--r-- | ppapi/cpp/private/net_address_private.h | 13 | ||||
-rw-r--r-- | ppapi/shared_impl/private/net_address_private_impl.cc | 58 | ||||
-rw-r--r-- | ppapi/tests/test_net_address_private.cc | 121 | ||||
-rw-r--r-- | ppapi/tests/test_net_address_private.h | 1 | ||||
-rw-r--r-- | ppapi/thunk/interfaces_ppb_private.h | 2 |
9 files changed, 311 insertions, 102 deletions
diff --git a/chrome/test/ui/ppapi_uitest.cc b/chrome/test/ui/ppapi_uitest.cc index 020c390..8f868df 100644 --- a/chrome/test/ui/ppapi_uitest.cc +++ b/chrome/test/ui/ppapi_uitest.cc @@ -779,6 +779,7 @@ TEST_PPAPI_IN_PROCESS(NetAddressPrivate_DescribeIPv6) TEST_PPAPI_IN_PROCESS(NetAddressPrivate_GetFamily) TEST_PPAPI_IN_PROCESS(NetAddressPrivate_GetPort) TEST_PPAPI_IN_PROCESS(NetAddressPrivate_GetAddress) +TEST_PPAPI_IN_PROCESS(NetAddressPrivate_GetScopeID) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_AreEqual) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_AreHostsEqual) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_Describe) @@ -788,6 +789,7 @@ TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_DescribeIPv6) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_GetFamily) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_GetPort) TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_GetAddress) +TEST_PPAPI_OUT_OF_PROCESS(NetAddressPrivate_GetScopeID) // Frequently timing out on Windows. http://crbug.com/115440 #if defined(OS_WIN) diff --git a/ppapi/api/private/ppb_net_address_private.idl b/ppapi/api/private/ppb_net_address_private.idl index 01a370b..222097f 100644 --- a/ppapi/api/private/ppb_net_address_private.idl +++ b/ppapi/api/private/ppb_net_address_private.idl @@ -9,7 +9,8 @@ label Chrome { M17 = 0.1, - M19 = 1.0 + M19_0 = 1.0, + M19_1 = 1.1 }; [assert_size(4)] @@ -29,7 +30,8 @@ enum PP_NetAddressFamily_Private { }; /** - * This is an opaque type holding a network address. + * This is an opaque type holding a network address. Plugins must + * never access members of this struct directly. */ [assert_size(132)] struct PP_NetAddress_Private { @@ -43,60 +45,84 @@ struct PP_NetAddress_Private { */ interface PPB_NetAddress_Private { /** - * Returns PP_TRUE if the two addresses are equal (host and port). - */ + * Returns PP_TRUE if the two addresses are equal (host and port). + */ PP_Bool AreEqual([in] PP_NetAddress_Private addr1, [in] PP_NetAddress_Private addr2); /** - * Returns PP_TRUE if the two addresses refer to the same host. - */ + * Returns PP_TRUE if the two addresses refer to the same host. + */ PP_Bool AreHostsEqual([in] PP_NetAddress_Private addr1, [in] PP_NetAddress_Private addr2); /** - * Returns a human-readable description of the network address, optionally - * including the port (e.g., "192.168.0.1", "192.168.0.1:99", or "[::1]:80"), - * or an undefined var on failure. - */ + * Returns a human-readable description of the network address, optionally + * including the port (e.g., "192.168.0.1", "192.168.0.1:99", or "[::1]:80"), + * or an undefined var on failure. + */ PP_Var Describe([in] PP_Module module, [in] PP_NetAddress_Private addr, [in] PP_Bool include_port); /** - * Replaces the port in the given source address. Returns PP_TRUE on success. - */ + * Replaces the port in the given source address. Returns PP_TRUE on success. + */ PP_Bool ReplacePort([in] PP_NetAddress_Private src_addr, [in] uint16_t port, - [out] PP_NetAddress_Private dest_addr); + [out] PP_NetAddress_Private addr_out); /** - * Gets the "any" address (for IPv4 or IPv6); for use with UDP Bind. - */ + * Gets the "any" address (for IPv4 or IPv6); for use with UDP Bind. + */ void GetAnyAddress([in] PP_Bool is_ipv6, [out] PP_NetAddress_Private addr); /** - * Gets the address family. - */ + * Gets the address family. + */ [version=1.0] PP_NetAddressFamily_Private GetFamily([in] PP_NetAddress_Private addr); /** - * Gets the port. The port is returned in host byte order. - */ + * Gets the port. The port is returned in host byte order. + */ [version=1.0] uint16_t GetPort([in] PP_NetAddress_Private addr); /** - * Gets the address. The output, address, must be large enough for the - * current socket family. The output will be the binary representation of an - * address for the current socket family. For IPv4 and IPv6 the address is in - * network byte order. PP_TRUE is returned if the address was successfully - * retrieved. - */ + * Gets the address. The output, address, must be large enough for the + * current socket family. The output will be the binary representation of an + * address for the current socket family. For IPv4 and IPv6 the address is in + * network byte order. PP_TRUE is returned if the address was successfully + * retrieved. + */ [version=1.0] PP_Bool GetAddress([in] PP_NetAddress_Private addr, [out] mem_t address, [in] uint16_t address_size); + + /** + * Returns ScopeID for IPv6 addresses or 0 for IPv4. + */ + [version=1.1] + uint32_t GetScopeID([in] PP_NetAddress_Private addr); + + /** + * Creates NetAddress with the specified IPv4 address and port + * number. + */ + [version=1.1] + void CreateFromIPv4Address([in] uint8_t[4] ip, + [in] uint16_t port, + [out] PP_NetAddress_Private addr_out); + /** + * Creates NetAddress with the specified IPv6 address, scope_id and + * port number. + */ + [version=1.1] + void CreateFromIPv6Address([in] uint8_t[16] ip, + [in] uint32_t scope_id, + [in] uint16_t port, + [out] PP_NetAddress_Private addr_out); }; diff --git a/ppapi/c/private/ppb_net_address_private.h b/ppapi/c/private/ppb_net_address_private.h index 2929681..d5a5350 100644 --- a/ppapi/c/private/ppb_net_address_private.h +++ b/ppapi/c/private/ppb_net_address_private.h @@ -4,7 +4,7 @@ */ /* From private/ppb_net_address_private.idl, - * modified Tue Feb 14 17:56:23 2012. + * modified Wed Mar 21 11:10:47 2012. */ #ifndef PPAPI_C_PRIVATE_PPB_NET_ADDRESS_PRIVATE_H_ @@ -18,7 +18,8 @@ #define PPB_NETADDRESS_PRIVATE_INTERFACE_0_1 "PPB_NetAddress_Private;0.1" #define PPB_NETADDRESS_PRIVATE_INTERFACE_1_0 "PPB_NetAddress_Private;1.0" -#define PPB_NETADDRESS_PRIVATE_INTERFACE PPB_NETADDRESS_PRIVATE_INTERFACE_1_0 +#define PPB_NETADDRESS_PRIVATE_INTERFACE_1_1 "PPB_NetAddress_Private;1.1" +#define PPB_NETADDRESS_PRIVATE_INTERFACE PPB_NETADDRESS_PRIVATE_INTERFACE_1_1 /** * @file @@ -54,7 +55,8 @@ PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_NetAddressFamily_Private, 4); * @{ */ /** - * This is an opaque type holding a network address. + * This is an opaque type holding a network address. Plugins must + * never access members of this struct directly. */ struct PP_NetAddress_Private { uint32_t size; @@ -73,7 +75,7 @@ PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(PP_NetAddress_Private, 132); * The <code>PPB_NetAddress_Private</code> interface provides operations on * network addresses. */ -struct PPB_NetAddress_Private_1_0 { +struct PPB_NetAddress_Private_1_1 { /** * Returns PP_TRUE if the two addresses are equal (host and port). */ @@ -97,7 +99,7 @@ struct PPB_NetAddress_Private_1_0 { */ PP_Bool (*ReplacePort)(const struct PP_NetAddress_Private* src_addr, uint16_t port, - struct PP_NetAddress_Private* dest_addr); + struct PP_NetAddress_Private* addr_out); /** * Gets the "any" address (for IPv4 or IPv6); for use with UDP Bind. */ @@ -121,9 +123,28 @@ struct PPB_NetAddress_Private_1_0 { PP_Bool (*GetAddress)(const struct PP_NetAddress_Private* addr, void* address, uint16_t address_size); + /** + * Returns ScopeID for IPv6 addresses or 0 for IPv4. + */ + uint32_t (*GetScopeID)(const struct PP_NetAddress_Private* addr); + /** + * Creates NetAddress with the specified IPv4 address and port + * number. + */ + void (*CreateFromIPv4Address)(const uint8_t ip[4], + uint16_t port, + struct PP_NetAddress_Private* addr_out); + /** + * Creates NetAddress with the specified IPv6 address, scope_id and + * port number. + */ + void (*CreateFromIPv6Address)(const uint8_t ip[16], + uint32_t scope_id, + uint16_t port, + struct PP_NetAddress_Private* addr_out); }; -typedef struct PPB_NetAddress_Private_1_0 PPB_NetAddress_Private; +typedef struct PPB_NetAddress_Private_1_1 PPB_NetAddress_Private; struct PPB_NetAddress_Private_0_1 { PP_Bool (*AreEqual)(const struct PP_NetAddress_Private* addr1, @@ -135,8 +156,28 @@ struct PPB_NetAddress_Private_0_1 { PP_Bool include_port); PP_Bool (*ReplacePort)(const struct PP_NetAddress_Private* src_addr, uint16_t port, - struct PP_NetAddress_Private* dest_addr); + struct PP_NetAddress_Private* addr_out); + void (*GetAnyAddress)(PP_Bool is_ipv6, struct PP_NetAddress_Private* addr); +}; + +struct PPB_NetAddress_Private_1_0 { + PP_Bool (*AreEqual)(const struct PP_NetAddress_Private* addr1, + const struct PP_NetAddress_Private* addr2); + PP_Bool (*AreHostsEqual)(const struct PP_NetAddress_Private* addr1, + const struct PP_NetAddress_Private* addr2); + struct PP_Var (*Describe)(PP_Module module, + const struct PP_NetAddress_Private* addr, + PP_Bool include_port); + PP_Bool (*ReplacePort)(const struct PP_NetAddress_Private* src_addr, + uint16_t port, + struct PP_NetAddress_Private* addr_out); void (*GetAnyAddress)(PP_Bool is_ipv6, struct PP_NetAddress_Private* addr); + PP_NetAddressFamily_Private (*GetFamily)( + const struct PP_NetAddress_Private* addr); + uint16_t (*GetPort)(const struct PP_NetAddress_Private* addr); + PP_Bool (*GetAddress)(const struct PP_NetAddress_Private* addr, + void* address, + uint16_t address_size); }; /** * @} diff --git a/ppapi/cpp/private/net_address_private.cc b/ppapi/cpp/private/net_address_private.cc index 1da2115..0e881c7 100644 --- a/ppapi/cpp/private/net_address_private.cc +++ b/ppapi/cpp/private/net_address_private.cc @@ -13,6 +13,10 @@ namespace pp { namespace { +template <> const char* interface_name<PPB_NetAddress_Private_1_1>() { + return PPB_NETADDRESS_PRIVATE_INTERFACE_1_1; +} + template <> const char* interface_name<PPB_NetAddress_Private_1_0>() { return PPB_NETADDRESS_PRIVATE_INTERFACE_1_0; } @@ -25,13 +29,18 @@ template <> const char* interface_name<PPB_NetAddress_Private_0_1>() { // static bool NetAddressPrivate::IsAvailable() { - return has_interface<PPB_NetAddress_Private_1_0>() || - has_interface<PPB_NetAddress_Private_0_1>(); + return has_interface<PPB_NetAddress_Private_1_1>() || + has_interface<PPB_NetAddress_Private_1_0>() || + has_interface<PPB_NetAddress_Private_0_1>(); } // static bool NetAddressPrivate::AreEqual(const PP_NetAddress_Private& addr1, const PP_NetAddress_Private& addr2) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + return !!get_interface<PPB_NetAddress_Private_1_1>()->AreEqual(&addr1, + &addr2); + } if (has_interface<PPB_NetAddress_Private_1_0>()) { return !!get_interface<PPB_NetAddress_Private_1_0>()->AreEqual(&addr1, &addr2); @@ -46,6 +55,10 @@ bool NetAddressPrivate::AreEqual(const PP_NetAddress_Private& addr1, // static bool NetAddressPrivate::AreHostsEqual(const PP_NetAddress_Private& addr1, const PP_NetAddress_Private& addr2) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + return !!get_interface<PPB_NetAddress_Private_1_1>()->AreHostsEqual(&addr1, + &addr2); + } if (has_interface<PPB_NetAddress_Private_1_0>()) { return !!get_interface<PPB_NetAddress_Private_1_0>()->AreHostsEqual(&addr1, &addr2); @@ -65,7 +78,12 @@ std::string NetAddressPrivate::Describe(const PP_NetAddress_Private& addr, return std::string(); PP_Var result_pp_var = PP_MakeUndefined(); - if (has_interface<PPB_NetAddress_Private_1_0>()) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + result_pp_var = get_interface<PPB_NetAddress_Private_1_1>()->Describe( + module->pp_module(), + &addr, + PP_FromBool(include_port)); + } else if (has_interface<PPB_NetAddress_Private_1_0>()) { result_pp_var = get_interface<PPB_NetAddress_Private_1_0>()->Describe( module->pp_module(), &addr, @@ -85,6 +103,11 @@ std::string NetAddressPrivate::Describe(const PP_NetAddress_Private& addr, bool NetAddressPrivate::ReplacePort(const PP_NetAddress_Private& addr_in, uint16_t port, PP_NetAddress_Private* addr_out) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + return !!get_interface<PPB_NetAddress_Private_1_1>()->ReplacePort(&addr_in, + port, + addr_out); + } if (has_interface<PPB_NetAddress_Private_1_0>()) { return !!get_interface<PPB_NetAddress_Private_1_0>()->ReplacePort(&addr_in, port, @@ -99,22 +122,32 @@ bool NetAddressPrivate::ReplacePort(const PP_NetAddress_Private& addr_in, } // static -void NetAddressPrivate::GetAnyAddress(bool is_ipv6, +bool NetAddressPrivate::GetAnyAddress(bool is_ipv6, PP_NetAddress_Private* addr) { - if (has_interface<PPB_NetAddress_Private_1_0>()) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + get_interface<PPB_NetAddress_Private_1_1>()->GetAnyAddress( + PP_FromBool(is_ipv6), + addr); + return true; + } else if (has_interface<PPB_NetAddress_Private_1_0>()) { get_interface<PPB_NetAddress_Private_1_0>()->GetAnyAddress( PP_FromBool(is_ipv6), addr); + return true; } else if (has_interface<PPB_NetAddress_Private_0_1>()) { get_interface<PPB_NetAddress_Private_0_1>()->GetAnyAddress( PP_FromBool(is_ipv6), addr); + return true; } + return false; } // static PP_NetAddressFamily_Private NetAddressPrivate::GetFamily( const PP_NetAddress_Private& addr) { + if (has_interface<PPB_NetAddress_Private_1_1>()) + return get_interface<PPB_NetAddress_Private_1_1>()->GetFamily(&addr); if (has_interface<PPB_NetAddress_Private_1_0>()) return get_interface<PPB_NetAddress_Private_1_0>()->GetFamily(&addr); return PP_NETADDRESSFAMILY_UNSPECIFIED; @@ -122,6 +155,8 @@ PP_NetAddressFamily_Private NetAddressPrivate::GetFamily( // static uint16_t NetAddressPrivate::GetPort(const PP_NetAddress_Private& addr) { + if (has_interface<PPB_NetAddress_Private_1_1>()) + return get_interface<PPB_NetAddress_Private_1_1>()->GetPort(&addr); if (has_interface<PPB_NetAddress_Private_1_0>()) return get_interface<PPB_NetAddress_Private_1_0>()->GetPort(&addr); return 0; @@ -131,6 +166,12 @@ uint16_t NetAddressPrivate::GetPort(const PP_NetAddress_Private& addr) { bool NetAddressPrivate::GetAddress(const PP_NetAddress_Private& addr, void* address, uint16_t address_size) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + return PP_ToBool(get_interface<PPB_NetAddress_Private_1_1>()->GetAddress( + &addr, + address, + address_size)); + } if (has_interface<PPB_NetAddress_Private_1_0>()) { return PP_ToBool(get_interface<PPB_NetAddress_Private_1_0>()->GetAddress( &addr, @@ -140,4 +181,38 @@ bool NetAddressPrivate::GetAddress(const PP_NetAddress_Private& addr, return false; } +// static +uint32_t NetAddressPrivate::GetScopeID(const PP_NetAddress_Private& addr) { + if (has_interface<PPB_NetAddress_Private_1_1>()) + return get_interface<PPB_NetAddress_Private_1_1>()->GetScopeID(&addr); + return 0; +} + +// static +bool NetAddressPrivate::CreateFromIPv4Address( + const uint8_t ip[4], + uint16_t port, + struct PP_NetAddress_Private* addr_out) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + get_interface<PPB_NetAddress_Private_1_1>()->CreateFromIPv4Address( + ip, port, addr_out); + return true; + } + return false; +} + +// static +bool NetAddressPrivate::CreateFromIPv6Address( + const uint8_t ip[16], + uint32_t scope_id, + uint16_t port, + struct PP_NetAddress_Private* addr_out) { + if (has_interface<PPB_NetAddress_Private_1_1>()) { + get_interface<PPB_NetAddress_Private_1_1>()->CreateFromIPv6Address( + ip, scope_id, port, addr_out); + return true; + } + return false; +} + } // namespace pp diff --git a/ppapi/cpp/private/net_address_private.h b/ppapi/cpp/private/net_address_private.h index c242175..5ad50d7 100644 --- a/ppapi/cpp/private/net_address_private.h +++ b/ppapi/cpp/private/net_address_private.h @@ -26,12 +26,21 @@ class NetAddressPrivate { static bool ReplacePort(const PP_NetAddress_Private& addr_in, uint16_t port, PP_NetAddress_Private* addr_out); - static void GetAnyAddress(bool is_ipv6, PP_NetAddress_Private* addr); + static bool GetAnyAddress(bool is_ipv6, PP_NetAddress_Private* addr); static PP_NetAddressFamily_Private GetFamily( const PP_NetAddress_Private& addr); static uint16_t GetPort(const PP_NetAddress_Private& addr); static bool GetAddress(const PP_NetAddress_Private& addr, - void* address, uint16_t address_size); + void* address, + uint16_t address_size); + static uint32_t GetScopeID(const PP_NetAddress_Private& addr); + static bool CreateFromIPv4Address(const uint8_t ip[4], + uint16_t port, + struct PP_NetAddress_Private* addr_out); + static bool CreateFromIPv6Address(const uint8_t ip[16], + uint32_t scope_id, + uint16_t port, + struct PP_NetAddress_Private* addr_out); }; } // namespace pp diff --git a/ppapi/shared_impl/private/net_address_private_impl.cc b/ppapi/shared_impl/private/net_address_private_impl.cc index 9d7ac2f..06875cb 100644 --- a/ppapi/shared_impl/private/net_address_private_impl.cc +++ b/ppapi/shared_impl/private/net_address_private_impl.cc @@ -89,7 +89,7 @@ PP_Bool GetAddress(const PP_NetAddress_Private* addr, switch (GetFamilyInternal(addr)) { case AF_INET: { const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); - if (address_size >= sizeof(a->sin_addr.s_addr)) { + if (address_size >= sizeof(a->sin_addr.s_addr)) { memcpy(address, &(a->sin_addr.s_addr), sizeof(a->sin_addr.s_addr)); return PP_TRUE; } @@ -110,6 +110,17 @@ PP_Bool GetAddress(const PP_NetAddress_Private* addr, return PP_FALSE; } +uint32_t GetScopeID(const PP_NetAddress_Private* addr) { + switch (GetFamilyInternal(addr)) { + case AF_INET6: { + const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); + return a->sin6_scope_id; + } + default: + return 0; + } +} + PP_Bool AreHostsEqual(const PP_NetAddress_Private* addr1, const PP_NetAddress_Private* addr2) { if (!NetAddressPrivateImpl::ValidateNetAddress(*addr1) || @@ -332,6 +343,32 @@ void GetAnyAddress(PP_Bool is_ipv6, PP_NetAddress_Private* addr) { } } +void CreateFromIPv4Address(const uint8_t ip[4], + uint16_t port, + struct PP_NetAddress_Private* addr_out) { + memset(addr_out->data, 0, + arraysize(addr_out->data) * sizeof(addr_out->data[0])); + sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr_out->data); + addr_out->size = sizeof(*a); + a->sin_family = AF_INET; + memcpy(&(a->sin_addr), ip, sizeof(a->sin_addr)); + a->sin_port = htons(port); +} + +void CreateFromIPv6Address(const uint8_t ip[16], + uint32_t scope_id, + uint16_t port, + struct PP_NetAddress_Private* addr_out) { + memset(addr_out->data, 0, + arraysize(addr_out->data) * sizeof(addr_out->data[0])); + sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr_out->data); + addr_out->size = sizeof(*a); + a->sin6_family = AF_INET6; + memcpy(&(a->sin6_addr), ip, sizeof(a->sin6_addr)); + a->sin6_port = htons(port); + a->sin6_scope_id = scope_id; +} + const PPB_NetAddress_Private_0_1 net_address_private_interface_0_1 = { &AreEqual, &AreHostsEqual, @@ -351,6 +388,20 @@ const PPB_NetAddress_Private_1_0 net_address_private_interface_1_0 = { &GetAddress }; +const PPB_NetAddress_Private_1_1 net_address_private_interface_1_1 = { + &AreEqual, + &AreHostsEqual, + &Describe, + &ReplacePort, + &GetAnyAddress, + &GetFamily, + &GetPort, + &GetAddress, + &GetScopeID, + &CreateFromIPv4Address, + &CreateFromIPv6Address +}; + } // namespace namespace thunk { @@ -365,6 +416,11 @@ GetPPB_NetAddress_Private_1_0_Thunk() { return &net_address_private_interface_1_0; } +PPAPI_THUNK_EXPORT const PPB_NetAddress_Private_1_1* +GetPPB_NetAddress_Private_1_1_Thunk() { + return &net_address_private_interface_1_1; +} + } // namespace thunk // static diff --git a/ppapi/tests/test_net_address_private.cc b/ppapi/tests/test_net_address_private.cc index bdb14d6..7b35e66 100644 --- a/ppapi/tests/test_net_address_private.cc +++ b/ppapi/tests/test_net_address_private.cc @@ -11,55 +11,25 @@ #include "ppapi/tests/test_utils.h" #include "ppapi/tests/testing_instance.h" -// Other than |GetAnyAddress()|, there's no way to actually get -// |PP_NetAddress_Private| structs from just this interface. We'll cheat and -// synthesize some. - -#if defined(PPAPI_POSIX) -#include <arpa/inet.h> -#include <netdb.h> -#include <netinet/in.h> -#include <sys/socket.h> -#endif - -#if defined(PPAPI_OS_MACOSX) -// This is a bit evil, but it's standard operating procedure for |s6_addr|.... -#define s6_addr16 __u6_addr.__u6_addr16 -#endif - -#if defined(PPAPI_OS_WIN) -#include <ws2tcpip.h> - -#define s6_addr16 u.Word -#endif - using pp::NetAddressPrivate; namespace { -// |host| should be an IP address represented as text, e.g., "192.168.0.1". -PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { - PP_NetAddress_Private addr = PP_NetAddress_Private(); - addr.size = sizeof(sockaddr_in); - sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data); - a->sin_family = AF_INET; - a->sin_port = htons(port); - a->sin_addr.s_addr = inet_addr(host); +PP_NetAddress_Private MakeIPv4NetAddress(const uint8_t host[4], int port) { + PP_NetAddress_Private addr; + NetAddressPrivate::CreateFromIPv4Address(host, port, &addr); return addr; } -// |host| should be an array of eight 16-bit numbers. -PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[], uint16_t port, +PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[8], uint16_t port, uint32_t scope_id) { PP_NetAddress_Private addr = PP_NetAddress_Private(); - addr.size = sizeof(sockaddr_in6); - sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr.data); - a->sin6_family = AF_INET6; - a->sin6_port = htons(port); - a->sin6_flowinfo = 0; - for (int i = 0; i < 8; i++) - a->sin6_addr.s6_addr16[i] = htons(host[i]); - a->sin6_scope_id = scope_id; + uint8_t ip[16]; + for(int i = 0; i < 8; ++i) { + ip[i * 2] = host[i] >> 8; + ip[i * 2 + 1] = host[i] & 0xff; + } + NetAddressPrivate::CreateFromIPv6Address(ip, scope_id, port, &addr); return addr; } @@ -85,6 +55,7 @@ void TestNetAddressPrivate::RunTests(const std::string& filter) { RUN_TEST(GetFamily, filter); RUN_TEST(GetPort, filter); RUN_TEST(GetAddress, filter); + RUN_TEST(GetScopeID, filter); } std::string TestNetAddressPrivate::TestAreEqual() { @@ -92,14 +63,16 @@ std::string TestNetAddressPrivate::TestAreEqual() { PP_NetAddress_Private invalid = PP_NetAddress_Private(); ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid)); - PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80)); ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid)); - PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234); + PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234); ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, localhost_1234)); - PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80); + uint8_t other_ip[4] = { 192, 168, 0, 1 }; + PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80); ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, other_80)); PASS(); @@ -110,14 +83,16 @@ std::string TestNetAddressPrivate::TestAreHostsEqual() { PP_NetAddress_Private invalid = PP_NetAddress_Private(); ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(invalid, invalid)); - PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_80)); ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, invalid)); - PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234); + PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234); ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_1234)); - PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80); + uint8_t other_ip[4] = { 192, 168, 0, 1 }; + PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80); ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, other_80)); PASS(); @@ -128,16 +103,18 @@ std::string TestNetAddressPrivate::TestDescribe() { ASSERT_EQ("", NetAddressPrivate::Describe(invalid, false)); ASSERT_EQ("", NetAddressPrivate::Describe(invalid, true)); - PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_80, false)); ASSERT_EQ("127.0.0.1:80", NetAddressPrivate::Describe(localhost_80, true)); - PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234); + PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234); ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_1234, false)); ASSERT_EQ("127.0.0.1:1234", NetAddressPrivate::Describe(localhost_1234, true)); - PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80); + uint8_t other_ip[4] = { 192, 168, 0, 1 }; + PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80); ASSERT_EQ("192.168.0.1", NetAddressPrivate::Describe(other_80, false)); ASSERT_EQ("192.168.0.1:80", NetAddressPrivate::Describe(other_80, true)); @@ -151,9 +128,10 @@ std::string TestNetAddressPrivate::TestReplacePort() { PP_NetAddress_Private invalid = PP_NetAddress_Private(); ASSERT_FALSE(NetAddressPrivate::ReplacePort(invalid, 1234, &result)); - PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_TRUE(NetAddressPrivate::ReplacePort(localhost_80, 1234, &result)); - PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234); + PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234); ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_1234)); // Test that having the out param being the same as the in param works @@ -251,20 +229,20 @@ std::string TestNetAddressPrivate::TestDescribeIPv6() { } std::string TestNetAddressPrivate::TestGetFamily() { - PP_NetAddress_Private ipv4 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private ipv4 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_EQ(NetAddressPrivate::GetFamily(ipv4), PP_NETADDRESSFAMILY_IPV4); uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }; - PP_NetAddress_Private ipv6 = MakeIPv6NetAddress(ipv6_address, - 123, - 0); + PP_NetAddress_Private ipv6 = MakeIPv6NetAddress(ipv6_address, 123, 0); ASSERT_EQ(NetAddressPrivate::GetFamily(ipv6), PP_NETADDRESSFAMILY_IPV6); PASS(); } std::string TestNetAddressPrivate::TestGetPort() { - PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80); ASSERT_EQ(NetAddressPrivate::GetPort(localhost_80), 80); uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }; @@ -284,14 +262,13 @@ std::string TestNetAddressPrivate::TestGetAddress() { const int addr_storage_len = 16; unsigned char addr_storage[addr_storage_len]; - const char* ipv4_addr = "127.0.0.1"; - uint32_t ipv4_addr_long = inet_addr(ipv4_addr); + const uint8_t ipv4_addr[4] = { 127, 0, 0, 1 }; PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(ipv4_addr, 80); memset(addr_storage, 0, addr_storage_len); ASSERT_TRUE(NetAddressPrivate::GetAddress(localhost_80, addr_storage, addr_storage_len)); - ASSERT_EQ(memcmp(addr_storage, &ipv4_addr_long, 4), 0); + ASSERT_EQ(memcmp(addr_storage, &ipv4_addr, 4), 0); // Insufficient storage for address. ASSERT_FALSE(NetAddressPrivate::GetAddress(localhost_80, @@ -304,14 +281,17 @@ std::string TestNetAddressPrivate::TestGetAddress() { 0); // Ensure the ipv6 address is transformed properly into network order. - for (int i = 0; i < 8; i++) - ipv6_address[i] = htons(ipv6_address[i]); + uint8_t ipv6_bytes[16]; + for(int i = 0; i < 8; ++i) { + ipv6_bytes[i * 2] = ipv6_address[i] >> 8; + ipv6_bytes[i * 2 + 1] = ipv6_address[i] & 0xFF; + } memset(addr_storage, 0, addr_storage_len); ASSERT_TRUE(NetAddressPrivate::GetAddress(ipv6_addr, addr_storage, addr_storage_len)); - ASSERT_EQ(memcmp(addr_storage, ipv6_address, 16), 0); + ASSERT_EQ(memcmp(addr_storage, ipv6_bytes, 16), 0); // Insufficient storage for address. ASSERT_FALSE(NetAddressPrivate::GetAddress(ipv6_addr, @@ -320,3 +300,20 @@ std::string TestNetAddressPrivate::TestGetAddress() { PASS(); } + +std::string TestNetAddressPrivate::TestGetScopeID() { + uint8_t localhost_ip[4] = { 127, 0, 0, 1 }; + PP_NetAddress_Private ipv4 = MakeIPv4NetAddress(localhost_ip, 80); + ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv4), 0); + + uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }; + + PP_NetAddress_Private ipv6_123 = MakeIPv6NetAddress(ipv6_address, 0, 123); + ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv6_123), 123); + + PP_NetAddress_Private ipv6_max = + MakeIPv6NetAddress(ipv6_address, 0, 0xFFFFFFFF); + ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv6_max), 0xFFFFFFFF); + + PASS(); +} diff --git a/ppapi/tests/test_net_address_private.h b/ppapi/tests/test_net_address_private.h index 1aa9c04..0033df9 100644 --- a/ppapi/tests/test_net_address_private.h +++ b/ppapi/tests/test_net_address_private.h @@ -27,6 +27,7 @@ class TestNetAddressPrivate : public TestCase { std::string TestGetFamily(); std::string TestGetPort(); std::string TestGetAddress(); + std::string TestGetScopeID(); }; #endif // PAPPI_TESTS_TEST_NET_ADDRESS_PRIVATE_H_ diff --git a/ppapi/thunk/interfaces_ppb_private.h b/ppapi/thunk/interfaces_ppb_private.h index f6cec07..de2d194 100644 --- a/ppapi/thunk/interfaces_ppb_private.h +++ b/ppapi/thunk/interfaces_ppb_private.h @@ -35,6 +35,8 @@ PROXIED_IFACE(NoAPIName, PPB_NETADDRESS_PRIVATE_INTERFACE_0_1, PPB_NetAddress_Private_0_1) PROXIED_IFACE(NoAPIName, PPB_NETADDRESS_PRIVATE_INTERFACE_1_0, PPB_NetAddress_Private_1_0) +PROXIED_IFACE(NoAPIName, PPB_NETADDRESS_PRIVATE_INTERFACE_1_1, + PPB_NetAddress_Private_1_1) PROXIED_IFACE(PPB_Talk_Private, PPB_TALK_PRIVATE_INTERFACE_1_0, PPB_Talk_Private_1_0) PROXIED_IFACE(PPB_TCPServerSocket_Private, |