| 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) |
| 8 #include <windows.h> |
| 9 #include <ws2tcpip.h> |
| 10 #elif defined(OS_POSIX) |
| 11 #include <sys/socket.h> |
| 12 #include <sys/types.h> |
| 13 #endif |
| 14 |
| 7 #include <string.h> | 15 #include <string.h> |
| 8 | 16 |
| 9 #include <string> | 17 #include <string> |
| 10 | 18 |
| 11 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 12 #include "base/logging.h" | 20 #include "base/logging.h" |
| 13 #include "base/stringprintf.h" | 21 #include "base/stringprintf.h" |
| 14 #include "base/sys_byteorder.h" | 22 #include "base/sys_byteorder.h" |
| 15 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 16 #include "net/base/address_list.h" | |
| 17 #include "net/base/ip_endpoint.h" | |
| 18 #include "net/base/net_util.h" | |
| 19 #include "ppapi/c/pp_var.h" | 24 #include "ppapi/c/pp_var.h" |
| 20 #include "ppapi/c/private/ppb_net_address_private.h" | 25 #include "ppapi/c/private/ppb_net_address_private.h" |
| 21 #include "ppapi/shared_impl/var.h" | 26 #include "ppapi/shared_impl/var.h" |
| 22 #include "ppapi/thunk/thunk.h" | 27 #include "ppapi/thunk/thunk.h" |
| 23 | 28 |
| 24 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 25 // This is a bit evil, but it's standard operating procedure for |s6_addr|.... | 30 // This is a bit evil, but it's standard operating procedure for |s6_addr|.... |
| 26 #define s6_addr16 __u6_addr.__u6_addr16 | 31 #define s6_addr16 __u6_addr.__u6_addr16 |
| 27 #endif | 32 #endif |
| 28 | 33 |
| 29 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 30 // The type of |sockaddr::sa_family|. | 35 // The type of |sockaddr::sa_family|. |
| 31 typedef ADDRESS_FAMILY sa_family_t; | 36 typedef ADDRESS_FAMILY sa_family_t; |
| 32 | 37 |
| 33 #define s6_addr16 u.Word | 38 #define s6_addr16 u.Word |
| 34 #define ntohs(x) _byteswap_ushort(x) | 39 #define ntohs(x) _byteswap_ushort(x) |
| 35 #define htons(x) _byteswap_ushort(x) | 40 #define htons(x) _byteswap_ushort(x) |
| 36 #endif // OS_WIN | 41 #endif // defined(OS_WIN) |
| 37 | 42 |
| 38 // The net address interface doesn't have a normal C -> C++ thunk since it | 43 // The net address interface doesn't have a normal C -> C++ thunk since it |
| 39 // doesn't actually have any proxy wrapping or associated objects; it's just a | 44 // doesn't actually have any proxy wrapping or associated objects; it's just a |
| 40 // call into base. So we implement the entire interface here, using the thunk | 45 // call into base. So we implement the entire interface here, using the thunk |
| 41 // namespace so it magically gets hooked up in the proper places. | 46 // namespace so it magically gets hooked up in the proper places. |
| 42 | 47 |
| 43 namespace ppapi { | 48 namespace ppapi { |
| 44 | 49 |
| 45 namespace { | 50 namespace { |
| 46 | 51 |
| 52 static const size_t kIPv4AddressSize = 4; |
| 53 static const size_t kIPv6AddressSize = 16; |
| 54 |
| 55 bool IPEndPointToSockaddr(const std::vector<unsigned char>& address, |
| 56 int port, |
| 57 sockaddr* sa) { |
| 58 DCHECK(sa); |
| 59 switch (address.size()) { |
| 60 case kIPv4AddressSize: { |
| 61 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(sa); |
| 62 memset(addr, 0, sizeof(struct sockaddr_in)); |
| 63 addr->sin_family = AF_INET; |
| 64 addr->sin_port = base::HostToNet16(port); |
| 65 memcpy(&addr->sin_addr, &address[0], static_cast<int>(address.size())); |
| 66 break; |
| 67 } |
| 68 case kIPv6AddressSize: { |
| 69 struct sockaddr_in6* addr6 = |
| 70 reinterpret_cast<struct sockaddr_in6*>(sa); |
| 71 memset(addr6, 0, sizeof(struct sockaddr_in6)); |
| 72 addr6->sin6_family = AF_INET6; |
| 73 addr6->sin6_port = base::HostToNet16(port); |
| 74 memcpy(&addr6->sin6_addr, &address[0], static_cast<int>(address.size())); |
| 75 break; |
| 76 } |
| 77 default: |
| 78 return false; |
| 79 } |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool SockaddrToIPEndPoint(const sockaddr& sa, |
| 84 std::vector<unsigned char>* address, |
| 85 int* port) { |
| 86 DCHECK(address); |
| 87 DCHECK(port); |
| 88 switch (sa.sa_family) { |
| 89 case AF_INET: { |
| 90 const struct sockaddr_in* addr = |
| 91 reinterpret_cast<const struct sockaddr_in*>(&sa); |
| 92 *port = base::NetToHost16(addr->sin_port); |
| 93 const char* bytes = reinterpret_cast<const char*>(&addr->sin_addr); |
| 94 address->assign(&bytes[0], &bytes[kIPv4AddressSize]); |
| 95 break; |
| 96 } |
| 97 case AF_INET6: { |
| 98 const struct sockaddr_in6* addr = |
| 99 reinterpret_cast<const struct sockaddr_in6*>(&sa); |
| 100 *port = base::NetToHost16(addr->sin6_port); |
| 101 const char* bytes = reinterpret_cast<const char*>(&addr->sin6_addr); |
| 102 address->assign(&bytes[0], &bytes[kIPv6AddressSize]); |
| 103 break; |
| 104 } |
| 105 default: |
| 106 return false; |
| 107 } |
| 108 return true; |
| 109 } |
| 110 |
| 47 // This assert fails on OpenBSD for an unknown reason at the moment. | 111 // This assert fails on OpenBSD for an unknown reason at the moment. |
| 48 #if !defined(OS_OPENBSD) | 112 #if !defined(OS_OPENBSD) |
| 49 // Make sure the storage in |PP_NetAddress_Private| is big enough. (Do it here | 113 // Make sure the storage in |PP_NetAddress_Private| is big enough. (Do it here |
| 50 // since the data is opaque elsewhere.) | 114 // since the data is opaque elsewhere.) |
| 51 COMPILE_ASSERT(sizeof(reinterpret_cast<PP_NetAddress_Private*>(0)->data) >= | 115 COMPILE_ASSERT(sizeof(reinterpret_cast<PP_NetAddress_Private*>(0)->data) >= |
| 52 sizeof(sockaddr_storage), PP_NetAddress_Private_data_too_small); | 116 sizeof(sockaddr_storage), PP_NetAddress_Private_data_too_small); |
| 53 #endif | 117 #endif |
| 54 | 118 |
| 55 sa_family_t GetFamilyInternal(const PP_NetAddress_Private* addr) { | 119 sa_family_t GetFamilyInternal(const PP_NetAddress_Private* addr) { |
| 56 return reinterpret_cast<const sockaddr*>(addr->data)->sa_family; | 120 return reinterpret_cast<const sockaddr*>(addr->data)->sa_family; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 reinterpret_cast<const sockaddr_in6*>(addr1->data); | 234 reinterpret_cast<const sockaddr_in6*>(addr1->data); |
| 171 const sockaddr_in6* a2 = | 235 const sockaddr_in6* a2 = |
| 172 reinterpret_cast<const sockaddr_in6*>(addr2->data); | 236 reinterpret_cast<const sockaddr_in6*>(addr2->data); |
| 173 return PP_FromBool(a1->sin6_port == a2->sin6_port); | 237 return PP_FromBool(a1->sin6_port == a2->sin6_port); |
| 174 } | 238 } |
| 175 default: | 239 default: |
| 176 return PP_FALSE; | 240 return PP_FALSE; |
| 177 } | 241 } |
| 178 } | 242 } |
| 179 | 243 |
| 180 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 181 std::string ConvertIPv4AddressToString(const sockaddr_in* a, | 244 std::string ConvertIPv4AddressToString(const sockaddr_in* a, |
| 182 bool include_port) { | 245 bool include_port) { |
| 183 unsigned ip = base::NetToHost32(a->sin_addr.s_addr); | 246 unsigned ip = base::NetToHost32(a->sin_addr.s_addr); |
| 184 unsigned port = base::NetToHost16(a->sin_port); | 247 unsigned port = base::NetToHost16(a->sin_port); |
| 185 std::string description = base::StringPrintf( | 248 std::string description = base::StringPrintf( |
| 186 "%u.%u.%u.%u", | 249 "%u.%u.%u.%u", |
| 187 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); | 250 (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); |
| 188 if (include_port) | 251 if (include_port) |
| 189 base::StringAppendF(&description, ":%u", port); | 252 base::StringAppendF(&description, ":%u", port); |
| 190 return description; | 253 return description; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 321 |
| 259 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". | 322 // Nonzero scopes, e.g., 123, are indicated by appending, e.g., "%123". |
| 260 if (scope != 0) | 323 if (scope != 0) |
| 261 base::StringAppendF(&description, "%%%u", scope); | 324 base::StringAppendF(&description, "%%%u", scope); |
| 262 | 325 |
| 263 if (include_port) | 326 if (include_port) |
| 264 base::StringAppendF(&description, "]:%u", port); | 327 base::StringAppendF(&description, "]:%u", port); |
| 265 | 328 |
| 266 return description; | 329 return description; |
| 267 } | 330 } |
| 268 #endif // OS_WIN || OS_MACOSX | |
| 269 | 331 |
| 270 PP_Var Describe(PP_Module /*module*/, | 332 PP_Var Describe(PP_Module /*module*/, |
| 271 const struct PP_NetAddress_Private* addr, | 333 const struct PP_NetAddress_Private* addr, |
| 272 PP_Bool include_port) { | 334 PP_Bool include_port) { |
| 273 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) | 335 if (!NetAddressPrivateImpl::ValidateNetAddress(*addr)) |
| 274 return PP_MakeUndefined(); | 336 return PP_MakeUndefined(); |
| 275 | 337 |
| 276 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 277 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, | 338 // On Windows, |NetAddressToString()| doesn't work in the sandbox. On Mac, |
| 278 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: | 339 // the output isn't consistent with RFC 5952, at least on Mac OS 10.6: |
| 279 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't | 340 // |getnameinfo()| collapses length-one runs of zeros (and also doesn't |
| 280 // display the scope). | 341 // display the scope). |
| 281 // TODO(viettrungluu): Consider switching to this on Linux. | |
| 282 switch (GetFamilyInternal(addr)) { | 342 switch (GetFamilyInternal(addr)) { |
| 283 case AF_INET: { | 343 case AF_INET: { |
| 284 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); | 344 const sockaddr_in* a = reinterpret_cast<const sockaddr_in*>(addr->data); |
| 285 return StringVar::StringToPPVar( | 345 return StringVar::StringToPPVar( |
| 286 ConvertIPv4AddressToString(a, !!include_port)); | 346 ConvertIPv4AddressToString(a, !!include_port)); |
| 287 } | 347 } |
| 288 case AF_INET6: { | 348 case AF_INET6: { |
| 289 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); | 349 const sockaddr_in6* a = reinterpret_cast<const sockaddr_in6*>(addr->data); |
| 290 return StringVar::StringToPPVar( | 350 return StringVar::StringToPPVar( |
| 291 ConvertIPv6AddressToString(a, !!include_port)); | 351 ConvertIPv6AddressToString(a, !!include_port)); |
| 292 } | 352 } |
| 293 default: | 353 default: |
| 294 NOTREACHED(); | 354 NOTREACHED(); |
| 295 break; | 355 break; |
| 296 } | 356 } |
| 297 return PP_MakeUndefined(); | 357 return PP_MakeUndefined(); |
| 298 #else | |
| 299 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data); | |
| 300 socklen_t l = addr->size; | |
| 301 std::string description = | |
| 302 include_port ? net::NetAddressToStringWithPort(a, l) : | |
| 303 net::NetAddressToString(a, l); | |
| 304 return StringVar::StringToPPVar(description); | |
| 305 #endif | |
| 306 } | 358 } |
| 307 | 359 |
| 308 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, | 360 PP_Bool ReplacePort(const struct PP_NetAddress_Private* src_addr, |
| 309 uint16_t port, | 361 uint16_t port, |
| 310 struct PP_NetAddress_Private* dest_addr) { | 362 struct PP_NetAddress_Private* dest_addr) { |
| 311 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) | 363 if (!NetAddressPrivateImpl::ValidateNetAddress(*src_addr)) |
| 312 return PP_FALSE; | 364 return PP_FALSE; |
| 313 | 365 |
| 314 switch (GetFamilyInternal(src_addr)) { | 366 switch (GetFamilyInternal(src_addr)) { |
| 315 case AF_INET: { | 367 case AF_INET: { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 return false; | 514 return false; |
| 463 | 515 |
| 464 CHECK_LE(sa_length, sizeof(net_addr->data)); | 516 CHECK_LE(sa_length, sizeof(net_addr->data)); |
| 465 net_addr->size = sa_length; | 517 net_addr->size = sa_length; |
| 466 memcpy(net_addr->data, sa, net_addr->size); | 518 memcpy(net_addr->data, sa, net_addr->size); |
| 467 return true; | 519 return true; |
| 468 } | 520 } |
| 469 | 521 |
| 470 // static | 522 // static |
| 471 bool NetAddressPrivateImpl::IPEndPointToNetAddress( | 523 bool NetAddressPrivateImpl::IPEndPointToNetAddress( |
| 472 const net::IPEndPoint& ip, | 524 const std::vector<unsigned char>& address, |
| 525 int port, |
| 473 PP_NetAddress_Private* net_addr) { | 526 PP_NetAddress_Private* net_addr) { |
| 474 net::SockaddrStorage storage; | 527 sockaddr_storage storage; |
| 475 | 528 sockaddr* sa = reinterpret_cast<sockaddr*>(&storage); |
| 476 return ip.ToSockAddr(storage.addr, &storage.addr_len) && | 529 if (!IPEndPointToSockaddr(address, port, sa)) |
| 477 SockaddrToNetAddress(storage.addr, storage.addr_len, net_addr); | 530 return false; |
| 478 } | 531 return SockaddrToNetAddress(sa, sizeof(storage), net_addr); |
| 479 | |
| 480 // static | |
| 481 bool NetAddressPrivateImpl::AddressListToNetAddress( | |
| 482 const net::AddressList& address_list, | |
| 483 PP_NetAddress_Private* net_addr) { | |
| 484 return !address_list.empty() && IPEndPointToNetAddress(address_list.front(), | |
| 485 net_addr); | |
| 486 } | 532 } |
| 487 | 533 |
| 488 // static | 534 // static |
| 489 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( | 535 bool NetAddressPrivateImpl::NetAddressToIPEndPoint( |
| 490 const PP_NetAddress_Private& net_addr, | 536 const PP_NetAddress_Private& net_addr, |
| 491 net::IPEndPoint* ip_end_point) { | 537 std::vector<unsigned char>* address, |
| 492 if (!ip_end_point || !ValidateNetAddress(net_addr)) | 538 int* port) { |
| 539 if (!address || !port || !ValidateNetAddress(net_addr)) |
| 493 return false; | 540 return false; |
| 494 | 541 |
| 495 if (!ip_end_point->FromSockAddr( | 542 return SockaddrToIPEndPoint( |
| 496 reinterpret_cast<const sockaddr*>(net_addr.data), net_addr.size)) { | 543 reinterpret_cast<const sockaddr&>(*net_addr.data), address, port); |
| 497 return false; | |
| 498 } | |
| 499 | |
| 500 return true; | |
| 501 } | |
| 502 | |
| 503 // static | |
| 504 bool NetAddressPrivateImpl::NetAddressToAddressList( | |
| 505 const PP_NetAddress_Private& net_addr, net::AddressList* address_list) { | |
| 506 if (!address_list) | |
| 507 return false; | |
| 508 | |
| 509 net::IPEndPoint ip_end_point; | |
| 510 if (!NetAddressToIPEndPoint(net_addr, &ip_end_point)) | |
| 511 return false; | |
| 512 | |
| 513 *address_list = net::AddressList(ip_end_point); | |
| 514 return true; | |
| 515 } | 544 } |
| 516 | 545 |
| 517 } // namespace ppapi | 546 } // namespace ppapi |
| OLD | NEW |