Chromium Code Reviews| 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 792 return AF_UNSPEC; | 792 return AF_UNSPEC; |
| 793 case ADDRESS_FAMILY_IPV4: | 793 case ADDRESS_FAMILY_IPV4: |
| 794 return AF_INET; | 794 return AF_INET; |
| 795 case ADDRESS_FAMILY_IPV6: | 795 case ADDRESS_FAMILY_IPV6: |
| 796 return AF_INET6; | 796 return AF_INET6; |
| 797 } | 797 } |
| 798 NOTREACHED(); | 798 NOTREACHED(); |
| 799 return AF_UNSPEC; | 799 return AF_UNSPEC; |
| 800 } | 800 } |
| 801 | 801 |
| 802 bool ParseURLHostnameToNumber(const std::string& hostname, | |
| 803 IPAddressNumber* ip_number) { | |
| 804 // |hostname| must be a canonicalized hostname with brackets and everything. | |
|
Ryan Sleevi
2014/08/06 19:42:12
"with brackets and everything" is a bit too colloq
Mike West
2014/08/08 08:15:03
Done.
| |
| 805 // Otherwise, explosions. | |
| 806 url::Component host_comp(0, hostname.size()); | |
| 807 | |
| 808 // If it has a bracket, try parsing it as an IPv6 address. | |
| 809 if (hostname[0] == '[') { | |
| 810 ip_number->resize(16); // 128 bits. | |
| 811 return url::IPv6AddressToNumber( | |
| 812 hostname.data(), host_comp, &(*ip_number)[0]); | |
| 813 } | |
| 814 | |
| 815 // Otherwise, try IPv4. | |
| 816 ip_number->resize(4); // 32 bits. | |
| 817 int num_components; | |
| 818 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( | |
| 819 hostname.data(), host_comp, &(*ip_number)[0], &num_components); | |
| 820 return family == url::CanonHostInfo::IPV4; | |
| 821 } | |
| 822 | |
| 802 bool ParseIPLiteralToNumber(const std::string& ip_literal, | 823 bool ParseIPLiteralToNumber(const std::string& ip_literal, |
| 803 IPAddressNumber* ip_number) { | 824 IPAddressNumber* ip_number) { |
| 804 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains | 825 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains |
| 805 // a colon however, it must be an IPv6 address. | 826 // a colon however, it must be an IPv6 address. |
| 806 if (ip_literal.find(':') != std::string::npos) { | 827 if (ip_literal.find(':') != std::string::npos) { |
| 807 // GURL expects IPv6 hostnames to be surrounded with brackets. | 828 // GURL expects IPv6 hostnames to be surrounded with brackets. |
| 808 std::string host_brackets = "[" + ip_literal + "]"; | 829 std::string host_brackets = "[" + ip_literal + "]"; |
| 809 url::Component host_comp(0, host_brackets.size()); | 830 url::Component host_comp(0, host_brackets.size()); |
| 810 | 831 |
| 811 // Try parsing the hostname as an IPv6 literal. | 832 // Try parsing the hostname as an IPv6 literal. |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1010 } | 1031 } |
| 1011 return a1.size() * CHAR_BIT; | 1032 return a1.size() * CHAR_BIT; |
| 1012 } | 1033 } |
| 1013 | 1034 |
| 1014 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1035 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
| 1015 IPAddressNumber all_ones(mask.size(), 0xFF); | 1036 IPAddressNumber all_ones(mask.size(), 0xFF); |
| 1016 return CommonPrefixLength(mask, all_ones); | 1037 return CommonPrefixLength(mask, all_ones); |
| 1017 } | 1038 } |
| 1018 | 1039 |
| 1019 } // namespace net | 1040 } // namespace net |
| OLD | NEW |