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 "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 #include "unicode/regex.h" | 69 #include "unicode/regex.h" |
70 #include "unicode/ucnv.h" | 70 #include "unicode/ucnv.h" |
71 #include "unicode/uidna.h" | 71 #include "unicode/uidna.h" |
72 #include "unicode/ulocdata.h" | 72 #include "unicode/ulocdata.h" |
73 #include "unicode/uniset.h" | 73 #include "unicode/uniset.h" |
74 #include "unicode/uscript.h" | 74 #include "unicode/uscript.h" |
75 #include "unicode/uset.h" | 75 #include "unicode/uset.h" |
76 | 76 |
77 using base::Time; | 77 using base::Time; |
78 | 78 |
79 #if defined(OS_WIN) | |
80 // Allow htons/ntohs to be called without requiring ws2_32.dll to be loaded, | |
81 // which isn't available in Chrome's sandbox. See crbug.com/116591. | |
82 // TODO(wez): Replace these calls with base::htons() etc when available. | |
83 #define ntohs(x) _byteswap_ushort(x) | |
84 #define htons(x) _byteswap_ushort(x) | |
85 #endif // OS_WIN | |
86 | |
87 namespace net { | 79 namespace net { |
88 | 80 |
89 namespace { | 81 namespace { |
90 | 82 |
91 // what we prepend to get a file URL | 83 // what we prepend to get a file URL |
92 static const FilePath::CharType kFileURLPrefix[] = | 84 static const FilePath::CharType kFileURLPrefix[] = |
93 FILE_PATH_LITERAL("file:///"); | 85 FILE_PATH_LITERAL("file:///"); |
94 | 86 |
95 // The general list of blocked ports. Will be blocked unless a specific | 87 // The general list of blocked ports. Will be blocked unless a specific |
96 // protocol overrides it. (Ex: ftp can use ports 20 and 21) | 88 // protocol overrides it. (Ex: ftp can use ports 20 and 21) |
(...skipping 2231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2328 const struct sockaddr* address = info->ai_addr; | 2320 const struct sockaddr* address = info->ai_addr; |
2329 DCHECK(address); | 2321 DCHECK(address); |
2330 DCHECK_EQ(info->ai_family, address->sa_family); | 2322 DCHECK_EQ(info->ai_family, address->sa_family); |
2331 return GetPortFieldFromSockaddr(address, info->ai_addrlen); | 2323 return GetPortFieldFromSockaddr(address, info->ai_addrlen); |
2332 } | 2324 } |
2333 | 2325 |
2334 uint16 GetPortFromAddrinfo(const struct addrinfo* info) { | 2326 uint16 GetPortFromAddrinfo(const struct addrinfo* info) { |
2335 const uint16* port_field = GetPortFieldFromAddrinfo(info); | 2327 const uint16* port_field = GetPortFieldFromAddrinfo(info); |
2336 if (!port_field) | 2328 if (!port_field) |
2337 return -1; | 2329 return -1; |
2338 return ntohs(*port_field); | 2330 return base::NetToHost16(*port_field); |
2339 } | 2331 } |
2340 | 2332 |
2341 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, | 2333 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, |
2342 socklen_t address_len) { | 2334 socklen_t address_len) { |
2343 if (address->sa_family == AF_INET) { | 2335 if (address->sa_family == AF_INET) { |
2344 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); | 2336 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); |
2345 const struct sockaddr_in* sockaddr = | 2337 const struct sockaddr_in* sockaddr = |
2346 reinterpret_cast<const struct sockaddr_in*>(address); | 2338 reinterpret_cast<const struct sockaddr_in*>(address); |
2347 return &sockaddr->sin_port; | 2339 return &sockaddr->sin_port; |
2348 } else if (address->sa_family == AF_INET6) { | 2340 } else if (address->sa_family == AF_INET6) { |
2349 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); | 2341 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); |
2350 const struct sockaddr_in6* sockaddr = | 2342 const struct sockaddr_in6* sockaddr = |
2351 reinterpret_cast<const struct sockaddr_in6*>(address); | 2343 reinterpret_cast<const struct sockaddr_in6*>(address); |
2352 return &sockaddr->sin6_port; | 2344 return &sockaddr->sin6_port; |
2353 } else { | 2345 } else { |
2354 NOTREACHED(); | 2346 NOTREACHED(); |
2355 return NULL; | 2347 return NULL; |
2356 } | 2348 } |
2357 } | 2349 } |
2358 | 2350 |
2359 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { | 2351 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { |
2360 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); | 2352 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); |
2361 if (!port_field) | 2353 if (!port_field) |
2362 return -1; | 2354 return -1; |
2363 return ntohs(*port_field); | 2355 return base::NetToHost16(*port_field); |
2364 } | 2356 } |
2365 | 2357 |
2366 // Assign |port| to each address in the linked list starting from |head|. | 2358 // Assign |port| to each address in the linked list starting from |head|. |
2367 void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) { | 2359 void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) { |
2368 DCHECK(head); | 2360 DCHECK(head); |
2369 for (struct addrinfo* ai = head; ai; ai = ai->ai_next) { | 2361 for (struct addrinfo* ai = head; ai; ai = ai->ai_next) { |
2370 uint16* port_field = GetPortFieldFromAddrinfo(ai); | 2362 uint16* port_field = GetPortFieldFromAddrinfo(ai); |
2371 if (port_field) | 2363 if (port_field) |
2372 *port_field = htons(port); | 2364 *port_field = base::HostToNet16(port); |
2373 } | 2365 } |
2374 } | 2366 } |
2375 | 2367 |
2376 bool IsLocalhost(const std::string& host) { | 2368 bool IsLocalhost(const std::string& host) { |
2377 if (host == "localhost" || | 2369 if (host == "localhost" || |
2378 host == "localhost.localdomain" || | 2370 host == "localhost.localdomain" || |
2379 host == "localhost6" || | 2371 host == "localhost6" || |
2380 host == "localhost6.localdomain6") | 2372 host == "localhost6.localdomain6") |
2381 return true; | 2373 return true; |
2382 | 2374 |
(...skipping 29 matching lines...) Expand all Loading... |
2412 | 2404 |
2413 NetworkInterface::NetworkInterface(const std::string& name, | 2405 NetworkInterface::NetworkInterface(const std::string& name, |
2414 const IPAddressNumber& address) | 2406 const IPAddressNumber& address) |
2415 : name(name), address(address) { | 2407 : name(name), address(address) { |
2416 } | 2408 } |
2417 | 2409 |
2418 NetworkInterface::~NetworkInterface() { | 2410 NetworkInterface::~NetworkInterface() { |
2419 } | 2411 } |
2420 | 2412 |
2421 } // namespace net | 2413 } // namespace net |
OLD | NEW |