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

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

Issue 10309002: Reimplements net::AddressList without struct addrinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: get_canonical_name -> canonical_name. iterator to indexing Created 8 years, 7 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/net_util.h ('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 2247 matching lines...) Expand 10 before | Expand all | Expand 10 after
2258 if (remaining_bits != 0) { 2258 if (remaining_bits != 0) {
2259 unsigned char mask = 0xFF << (8 - remaining_bits); 2259 unsigned char mask = 0xFF << (8 - remaining_bits);
2260 int i = num_entire_bytes_in_prefix; 2260 int i = num_entire_bytes_in_prefix;
2261 if ((ip_number[i] & mask) != (ip_prefix[i] & mask)) 2261 if ((ip_number[i] & mask) != (ip_prefix[i] & mask))
2262 return false; 2262 return false;
2263 } 2263 }
2264 2264
2265 return true; 2265 return true;
2266 } 2266 }
2267 2267
2268 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info,
2269 bool recursive) {
2270 DCHECK(info);
2271 struct addrinfo* copy = new addrinfo;
2272
2273 // Copy all the fields (some of these are pointers, we will fix that next).
2274 memcpy(copy, info, sizeof(addrinfo));
2275
2276 // ai_canonname is a NULL-terminated string.
2277 if (info->ai_canonname) {
2278 copy->ai_canonname = base::strdup(info->ai_canonname);
2279 }
2280
2281 // ai_addr is a buffer of length ai_addrlen.
2282 if (info->ai_addr) {
2283 copy->ai_addr = reinterpret_cast<sockaddr *>(new char[info->ai_addrlen]);
2284 memcpy(copy->ai_addr, info->ai_addr, info->ai_addrlen);
2285 }
2286
2287 // Recursive copy.
2288 if (recursive && info->ai_next)
2289 copy->ai_next = CreateCopyOfAddrinfo(info->ai_next, recursive);
2290 else
2291 copy->ai_next = NULL;
2292
2293 return copy;
2294 }
2295
2296 void FreeCopyOfAddrinfo(struct addrinfo* info) {
2297 DCHECK(info);
2298 if (info->ai_canonname)
2299 free(info->ai_canonname); // Allocated by strdup.
2300
2301 if (info->ai_addr)
2302 delete [] reinterpret_cast<char*>(info->ai_addr);
2303
2304 struct addrinfo* next = info->ai_next;
2305
2306 delete info;
2307
2308 // Recursive free.
2309 if (next)
2310 FreeCopyOfAddrinfo(next);
2311 }
2312
2313 // Returns the port field of the sockaddr in |info|.
2314 uint16* GetPortFieldFromAddrinfo(struct addrinfo* info) {
2315 const struct addrinfo* const_info = info;
2316 const uint16* port_field = GetPortFieldFromAddrinfo(const_info);
2317 return const_cast<uint16*>(port_field);
2318 }
2319
2320 const uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info) {
2321 DCHECK(info);
2322 const struct sockaddr* address = info->ai_addr;
2323 DCHECK(address);
2324 DCHECK_EQ(info->ai_family, address->sa_family);
2325 return GetPortFieldFromSockaddr(address, info->ai_addrlen);
2326 }
2327
2328 uint16 GetPortFromAddrinfo(const struct addrinfo* info) {
2329 const uint16* port_field = GetPortFieldFromAddrinfo(info);
2330 if (!port_field)
2331 return -1;
2332 return base::NetToHost16(*port_field);
2333 }
2334
2335 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 2268 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
2336 socklen_t address_len) { 2269 socklen_t address_len) {
2337 if (address->sa_family == AF_INET) { 2270 if (address->sa_family == AF_INET) {
2338 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); 2271 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
2339 const struct sockaddr_in* sockaddr = 2272 const struct sockaddr_in* sockaddr =
2340 reinterpret_cast<const struct sockaddr_in*>(address); 2273 reinterpret_cast<const struct sockaddr_in*>(address);
2341 return &sockaddr->sin_port; 2274 return &sockaddr->sin_port;
2342 } else if (address->sa_family == AF_INET6) { 2275 } else if (address->sa_family == AF_INET6) {
2343 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); 2276 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
2344 const struct sockaddr_in6* sockaddr = 2277 const struct sockaddr_in6* sockaddr =
2345 reinterpret_cast<const struct sockaddr_in6*>(address); 2278 reinterpret_cast<const struct sockaddr_in6*>(address);
2346 return &sockaddr->sin6_port; 2279 return &sockaddr->sin6_port;
2347 } else { 2280 } else {
2348 NOTREACHED(); 2281 NOTREACHED();
2349 return NULL; 2282 return NULL;
2350 } 2283 }
2351 } 2284 }
2352 2285
2353 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 2286 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
2354 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 2287 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
2355 if (!port_field) 2288 if (!port_field)
2356 return -1; 2289 return -1;
2357 return base::NetToHost16(*port_field); 2290 return base::NetToHost16(*port_field);
2358 } 2291 }
2359 2292
2360 // Assign |port| to each address in the linked list starting from |head|.
2361 void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port) {
2362 DCHECK(head);
2363 for (struct addrinfo* ai = head; ai; ai = ai->ai_next) {
2364 uint16* port_field = GetPortFieldFromAddrinfo(ai);
2365 if (port_field)
2366 *port_field = base::HostToNet16(port);
2367 }
2368 }
2369
2370 bool IsLocalhost(const std::string& host) { 2293 bool IsLocalhost(const std::string& host) {
2371 if (host == "localhost" || 2294 if (host == "localhost" ||
2372 host == "localhost.localdomain" || 2295 host == "localhost.localdomain" ||
2373 host == "localhost6" || 2296 host == "localhost6" ||
2374 host == "localhost6.localdomain6") 2297 host == "localhost6.localdomain6")
2375 return true; 2298 return true;
2376 2299
2377 IPAddressNumber ip_number; 2300 IPAddressNumber ip_number;
2378 if (ParseIPLiteralToNumber(host, &ip_number)) { 2301 if (ParseIPLiteralToNumber(host, &ip_number)) {
2379 size_t size = ip_number.size(); 2302 size_t size = ip_number.size();
(...skipping 26 matching lines...) Expand all
2406 2329
2407 NetworkInterface::NetworkInterface(const std::string& name, 2330 NetworkInterface::NetworkInterface(const std::string& name,
2408 const IPAddressNumber& address) 2331 const IPAddressNumber& address)
2409 : name(name), address(address) { 2332 : name(name), address(address) {
2410 } 2333 }
2411 2334
2412 NetworkInterface::~NetworkInterface() { 2335 NetworkInterface::~NetworkInterface() {
2413 } 2336 }
2414 2337
2415 } // namespace net 2338 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698