Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(597)

Side by Side Diff: net/base/net_util.cc

Issue 9716020: Add base::HostToNetXX() & NetToHostXX(), and use them to replace htonX() & ntohX() in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/listen_socket_unittest.cc ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 2240 matching lines...) Expand 10 before | Expand all | Expand 10 after
2337 const struct sockaddr* address = info->ai_addr; 2329 const struct sockaddr* address = info->ai_addr;
2338 DCHECK(address); 2330 DCHECK(address);
2339 DCHECK_EQ(info->ai_family, address->sa_family); 2331 DCHECK_EQ(info->ai_family, address->sa_family);
2340 return GetPortFieldFromSockaddr(address, info->ai_addrlen); 2332 return GetPortFieldFromSockaddr(address, info->ai_addrlen);
2341 } 2333 }
2342 2334
2343 uint16 GetPortFromAddrinfo(const struct addrinfo* info) { 2335 uint16 GetPortFromAddrinfo(const struct addrinfo* info) {
2344 const uint16* port_field = GetPortFieldFromAddrinfo(info); 2336 const uint16* port_field = GetPortFieldFromAddrinfo(info);
2345 if (!port_field) 2337 if (!port_field)
2346 return -1; 2338 return -1;
2347 return ntohs(*port_field); 2339 return base::NetToHost16(*port_field);
2348 } 2340 }
2349 2341
2350 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 2342 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
2351 socklen_t address_len) { 2343 socklen_t address_len) {
2352 if (address->sa_family == AF_INET) { 2344 if (address->sa_family == AF_INET) {
2353 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); 2345 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
2354 const struct sockaddr_in* sockaddr = 2346 const struct sockaddr_in* sockaddr =
2355 reinterpret_cast<const struct sockaddr_in*>(address); 2347 reinterpret_cast<const struct sockaddr_in*>(address);
2356 return &sockaddr->sin_port; 2348 return &sockaddr->sin_port;
2357 } else if (address->sa_family == AF_INET6) { 2349 } else if (address->sa_family == AF_INET6) {
2358 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); 2350 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
2359 const struct sockaddr_in6* sockaddr = 2351 const struct sockaddr_in6* sockaddr =
2360 reinterpret_cast<const struct sockaddr_in6*>(address); 2352 reinterpret_cast<const struct sockaddr_in6*>(address);
2361 return &sockaddr->sin6_port; 2353 return &sockaddr->sin6_port;
2362 } else { 2354 } else {
2363 NOTREACHED(); 2355 NOTREACHED();
2364 return NULL; 2356 return NULL;
2365 } 2357 }
2366 } 2358 }
2367 2359
2368 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 2360 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
2369 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 2361 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
2370 if (!port_field) 2362 if (!port_field)
2371 return -1; 2363 return -1;
2372 return ntohs(*port_field); 2364 return base::NetToHost16(*port_field);
2373 } 2365 }
2374 2366
2375 // Assign |port| to each address in the linked list starting from |head|. 2367 // Assign |port| to each address in the linked list starting from |head|.
2376 void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) { 2368 void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) {
2377 DCHECK(head); 2369 DCHECK(head);
2378 for (struct addrinfo* ai = head; ai; ai = ai->ai_next) { 2370 for (struct addrinfo* ai = head; ai; ai = ai->ai_next) {
2379 uint16* port_field = GetPortFieldFromAddrinfo(ai); 2371 uint16* port_field = GetPortFieldFromAddrinfo(ai);
2380 if (port_field) 2372 if (port_field)
2381 *port_field = htons(port); 2373 *port_field = base::HostToNet16(port);
2382 } 2374 }
2383 } 2375 }
2384 2376
2385 bool IsLocalhost(const std::string& host) { 2377 bool IsLocalhost(const std::string& host) {
2386 if (host == "localhost" || 2378 if (host == "localhost" ||
2387 host == "localhost.localdomain" || 2379 host == "localhost.localdomain" ||
2388 host == "localhost6" || 2380 host == "localhost6" ||
2389 host == "localhost6.localdomain6") 2381 host == "localhost6.localdomain6")
2390 return true; 2382 return true;
2391 2383
(...skipping 29 matching lines...) Expand all
2421 2413
2422 NetworkInterface::NetworkInterface(const std::string& name, 2414 NetworkInterface::NetworkInterface(const std::string& name,
2423 const IPAddressNumber& address) 2415 const IPAddressNumber& address)
2424 : name(name), address(address) { 2416 : name(name), address(address) {
2425 } 2417 }
2426 2418
2427 NetworkInterface::~NetworkInterface() { 2419 NetworkInterface::~NetworkInterface() {
2428 } 2420 }
2429 2421
2430 } // namespace net 2422 } // namespace net
OLDNEW
« no previous file with comments | « net/base/listen_socket_unittest.cc ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698