| 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| is an already canoncalized hostname, conforming to RFC 3986. | |
| 805 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with | |
| 806 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952. | |
| 807 url::Component host_comp(0, hostname.size()); | |
| 808 | |
| 809 // If it has a bracket, try parsing it as an IPv6 address. | |
| 810 if (hostname[0] == '[') { | |
| 811 ip_number->resize(16); // 128 bits. | |
| 812 return url::IPv6AddressToNumber( | |
| 813 hostname.data(), host_comp, &(*ip_number)[0]); | |
| 814 } | |
| 815 | |
| 816 // Otherwise, try IPv4. | |
| 817 ip_number->resize(4); // 32 bits. | |
| 818 int num_components; | |
| 819 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( | |
| 820 hostname.data(), host_comp, &(*ip_number)[0], &num_components); | |
| 821 return family == url::CanonHostInfo::IPV4; | |
| 822 } | |
| 823 | |
| 824 bool ParseIPLiteralToNumber(const std::string& ip_literal, | 802 bool ParseIPLiteralToNumber(const std::string& ip_literal, |
| 825 IPAddressNumber* ip_number) { | 803 IPAddressNumber* ip_number) { |
| 826 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains | 804 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains |
| 827 // a colon however, it must be an IPv6 address. | 805 // a colon however, it must be an IPv6 address. |
| 828 if (ip_literal.find(':') != std::string::npos) { | 806 if (ip_literal.find(':') != std::string::npos) { |
| 829 // GURL expects IPv6 hostnames to be surrounded with brackets. | 807 // GURL expects IPv6 hostnames to be surrounded with brackets. |
| 830 std::string host_brackets = "[" + ip_literal + "]"; | 808 std::string host_brackets = "[" + ip_literal + "]"; |
| 831 url::Component host_comp(0, host_brackets.size()); | 809 url::Component host_comp(0, host_brackets.size()); |
| 832 | 810 |
| 833 // Try parsing the hostname as an IPv6 literal. | 811 // Try parsing the hostname as an IPv6 literal. |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 } | 1010 } |
| 1033 return a1.size() * CHAR_BIT; | 1011 return a1.size() * CHAR_BIT; |
| 1034 } | 1012 } |
| 1035 | 1013 |
| 1036 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1014 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
| 1037 IPAddressNumber all_ones(mask.size(), 0xFF); | 1015 IPAddressNumber all_ones(mask.size(), 0xFF); |
| 1038 return CommonPrefixLength(mask, all_ones); | 1016 return CommonPrefixLength(mask, all_ones); |
| 1039 } | 1017 } |
| 1040 | 1018 |
| 1041 } // namespace net | 1019 } // namespace net |
| OLD | NEW |