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

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

Issue 1158923005: Use the exact-width integer types defined in <stdint.h> rather than (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweak comments. Exclude mime_sniffer*. Rebase. Created 5 years, 6 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
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 <errno.h> 7 #include <errno.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 const base::string16 www(base::ASCIIToUTF16("www.")); 260 const base::string16 www(base::ASCIIToUTF16("www."));
261 return StartsWith(text, www, true) ? text.substr(www.length()) : text; 261 return StartsWith(text, www, true) ? text.substr(www.length()) : text;
262 } 262 }
263 263
264 base::string16 StripWWWFromHost(const GURL& url) { 264 base::string16 StripWWWFromHost(const GURL& url) {
265 DCHECK(url.is_valid()); 265 DCHECK(url.is_valid());
266 return StripWWW(base::ASCIIToUTF16(url.host())); 266 return StripWWW(base::ASCIIToUTF16(url.host()));
267 } 267 }
268 268
269 bool IsPortValid(int port) { 269 bool IsPortValid(int port) {
270 return port >= 0 && port <= std::numeric_limits<uint16>::max(); 270 return port >= 0 && port <= std::numeric_limits<uint16_t>::max();
271 } 271 }
272 272
273 bool IsPortAllowedByDefault(int port) { 273 bool IsPortAllowedByDefault(int port) {
274 int array_size = arraysize(kRestrictedPorts); 274 int array_size = arraysize(kRestrictedPorts);
275 for (int i = 0; i < array_size; i++) { 275 for (int i = 0; i < array_size; i++) {
276 if (kRestrictedPorts[i] == port) { 276 if (kRestrictedPorts[i] == port) {
277 return false; 277 return false;
278 } 278 }
279 } 279 }
280 return IsPortValid(port); 280 return IsPortValid(port);
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 450
451 void SockaddrStorage::operator=(const SockaddrStorage& other) { 451 void SockaddrStorage::operator=(const SockaddrStorage& other) {
452 addr_len = other.addr_len; 452 addr_len = other.addr_len;
453 // addr is already set to &this->addr_storage by default ctor. 453 // addr is already set to &this->addr_storage by default ctor.
454 memcpy(addr, other.addr, addr_len); 454 memcpy(addr, other.addr, addr_len);
455 } 455 }
456 456
457 // Extracts the address and port portions of a sockaddr. 457 // Extracts the address and port portions of a sockaddr.
458 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, 458 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
459 socklen_t sock_addr_len, 459 socklen_t sock_addr_len,
460 const uint8** address, 460 const uint8_t** address,
461 size_t* address_len, 461 size_t* address_len,
462 uint16* port) { 462 uint16_t* port) {
463 if (sock_addr->sa_family == AF_INET) { 463 if (sock_addr->sa_family == AF_INET) {
464 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) 464 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
465 return false; 465 return false;
466 const struct sockaddr_in* addr = 466 const struct sockaddr_in* addr =
467 reinterpret_cast<const struct sockaddr_in*>(sock_addr); 467 reinterpret_cast<const struct sockaddr_in*>(sock_addr);
468 *address = reinterpret_cast<const uint8*>(&addr->sin_addr); 468 *address = reinterpret_cast<const uint8_t*>(&addr->sin_addr);
469 *address_len = kIPv4AddressSize; 469 *address_len = kIPv4AddressSize;
470 if (port) 470 if (port)
471 *port = base::NetToHost16(addr->sin_port); 471 *port = base::NetToHost16(addr->sin_port);
472 return true; 472 return true;
473 } 473 }
474 474
475 if (sock_addr->sa_family == AF_INET6) { 475 if (sock_addr->sa_family == AF_INET6) {
476 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) 476 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
477 return false; 477 return false;
478 const struct sockaddr_in6* addr = 478 const struct sockaddr_in6* addr =
479 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); 479 reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
480 *address = reinterpret_cast<const uint8*>(&addr->sin6_addr); 480 *address = reinterpret_cast<const uint8_t*>(&addr->sin6_addr);
481 *address_len = kIPv6AddressSize; 481 *address_len = kIPv6AddressSize;
482 if (port) 482 if (port)
483 *port = base::NetToHost16(addr->sin6_port); 483 *port = base::NetToHost16(addr->sin6_port);
484 return true; 484 return true;
485 } 485 }
486 486
487 #if defined(OS_WIN) 487 #if defined(OS_WIN)
488 if (sock_addr->sa_family == AF_BTH) { 488 if (sock_addr->sa_family == AF_BTH) {
489 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) 489 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
490 return false; 490 return false;
491 const SOCKADDR_BTH* addr = 491 const SOCKADDR_BTH* addr =
492 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr); 492 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
493 *address = reinterpret_cast<const uint8*>(&addr->btAddr); 493 *address = reinterpret_cast<const uint8_t*>(&addr->btAddr);
494 *address_len = kBluetoothAddressSize; 494 *address_len = kBluetoothAddressSize;
495 if (port) 495 if (port)
496 *port = static_cast<uint16>(addr->port); 496 *port = static_cast<uint16_t>(addr->port);
497 return true; 497 return true;
498 } 498 }
499 #endif 499 #endif
500 500
501 return false; // Unrecognized |sa_family|. 501 return false; // Unrecognized |sa_family|.
502 } 502 }
503 503
504 std::string NetAddressToString(const struct sockaddr* sa, 504 std::string NetAddressToString(const struct sockaddr* sa,
505 socklen_t sock_addr_len) { 505 socklen_t sock_addr_len) {
506 const uint8* address; 506 const uint8_t* address;
507 size_t address_len; 507 size_t address_len;
508 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 508 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
509 &address_len, NULL)) { 509 &address_len, NULL)) {
510 NOTREACHED(); 510 NOTREACHED();
511 return std::string(); 511 return std::string();
512 } 512 }
513 return IPAddressToString(address, address_len); 513 return IPAddressToString(address, address_len);
514 } 514 }
515 515
516 std::string NetAddressToStringWithPort(const struct sockaddr* sa, 516 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
517 socklen_t sock_addr_len) { 517 socklen_t sock_addr_len) {
518 const uint8* address; 518 const uint8_t* address;
519 size_t address_len; 519 size_t address_len;
520 uint16 port; 520 uint16_t port;
521 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 521 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
522 &address_len, &port)) { 522 &address_len, &port)) {
523 NOTREACHED(); 523 NOTREACHED();
524 return std::string(); 524 return std::string();
525 } 525 }
526 return IPAddressToStringWithPort(address, address_len, port); 526 return IPAddressToStringWithPort(address, address_len, port);
527 } 527 }
528 528
529 std::string GetHostName() { 529 std::string GetHostName() {
530 #if defined(OS_NACL) 530 #if defined(OS_NACL)
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 return AF_UNSPEC; 689 return AF_UNSPEC;
690 case ADDRESS_FAMILY_IPV4: 690 case ADDRESS_FAMILY_IPV4:
691 return AF_INET; 691 return AF_INET;
692 case ADDRESS_FAMILY_IPV6: 692 case ADDRESS_FAMILY_IPV6:
693 return AF_INET6; 693 return AF_INET6;
694 } 694 }
695 NOTREACHED(); 695 NOTREACHED();
696 return AF_UNSPEC; 696 return AF_UNSPEC;
697 } 697 }
698 698
699 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 699 const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
700 socklen_t address_len) { 700 socklen_t address_len) {
701 if (address->sa_family == AF_INET) { 701 if (address->sa_family == AF_INET) {
702 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len)); 702 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
703 const struct sockaddr_in* sockaddr = 703 const struct sockaddr_in* sockaddr =
704 reinterpret_cast<const struct sockaddr_in*>(address); 704 reinterpret_cast<const struct sockaddr_in*>(address);
705 return &sockaddr->sin_port; 705 return &sockaddr->sin_port;
706 } else if (address->sa_family == AF_INET6) { 706 } else if (address->sa_family == AF_INET6) {
707 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len)); 707 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
708 const struct sockaddr_in6* sockaddr = 708 const struct sockaddr_in6* sockaddr =
709 reinterpret_cast<const struct sockaddr_in6*>(address); 709 reinterpret_cast<const struct sockaddr_in6*>(address);
710 return &sockaddr->sin6_port; 710 return &sockaddr->sin6_port;
711 } else { 711 } else {
712 NOTREACHED(); 712 NOTREACHED();
713 return NULL; 713 return NULL;
714 } 714 }
715 } 715 }
716 716
717 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 717 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
718 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 718 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
719 if (!port_field) 719 if (!port_field)
720 return -1; 720 return -1;
721 return base::NetToHost16(*port_field); 721 return base::NetToHost16(*port_field);
722 } 722 }
723 723
724 bool IsLocalhost(const std::string& host) { 724 bool IsLocalhost(const std::string& host) {
725 if (host == "localhost" || host == "localhost.localdomain" || 725 if (host == "localhost" || host == "localhost.localdomain" ||
726 host == "localhost6" || host == "localhost6.localdomain6" || 726 host == "localhost6" || host == "localhost6.localdomain6" ||
727 IsLocalhostTLD(host)) 727 IsLocalhostTLD(host))
728 return true; 728 return true;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 }; 789 };
790 const std::string& host = url.host(); 790 const std::string& host = url.host();
791 for (const char* suffix : kGoogleHostSuffixes) { 791 for (const char* suffix : kGoogleHostSuffixes) {
792 if (EndsWith(host, suffix, false)) 792 if (EndsWith(host, suffix, false))
793 return true; 793 return true;
794 } 794 }
795 return false; 795 return false;
796 } 796 }
797 797
798 } // namespace net 798 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_icu.cc » ('j') | net/cert/crl_set_storage.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698