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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 // third_party/WebKit/Source/platform/weborigin/KURL.cpp, | 137 // third_party/WebKit/Source/platform/weborigin/KURL.cpp, |
138 // KURL::port()) | 138 // KURL::port()) |
139 }; | 139 }; |
140 | 140 |
141 // FTP overrides the following restricted ports. | 141 // FTP overrides the following restricted ports. |
142 static const int kAllowedFtpPorts[] = { | 142 static const int kAllowedFtpPorts[] = { |
143 21, // ftp data | 143 21, // ftp data |
144 22, // ssh | 144 22, // ssh |
145 }; | 145 }; |
146 | 146 |
147 static const char kLocalhostTLD[] = ".localhost"; | |
148 static const size_t kLocalhostTLDLength = arraysize(kLocalhostTLD) - 1; | |
Ryan Sleevi
2015/03/24 23:15:20
Turns out you could move these to line 1024, since
estark
2015/03/25 23:53:33
Done.
| |
149 | |
147 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, | 150 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, |
148 const unsigned char* ip_prefix, | 151 const unsigned char* ip_prefix, |
149 size_t prefix_length_in_bits) { | 152 size_t prefix_length_in_bits) { |
150 // Compare all the bytes that fall entirely within the prefix. | 153 // Compare all the bytes that fall entirely within the prefix. |
151 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; | 154 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
152 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { | 155 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { |
153 if (ip_number[i] != ip_prefix[i]) | 156 if (ip_number[i] != ip_prefix[i]) |
154 return false; | 157 return false; |
155 } | 158 } |
156 | 159 |
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
977 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); | 980 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); |
978 if (!port_field) | 981 if (!port_field) |
979 return -1; | 982 return -1; |
980 return base::NetToHost16(*port_field); | 983 return base::NetToHost16(*port_field); |
981 } | 984 } |
982 | 985 |
983 bool IsLocalhost(const std::string& host) { | 986 bool IsLocalhost(const std::string& host) { |
984 if (host == "localhost" || | 987 if (host == "localhost" || |
985 host == "localhost.localdomain" || | 988 host == "localhost.localdomain" || |
986 host == "localhost6" || | 989 host == "localhost6" || |
987 host == "localhost6.localdomain6") | 990 host == "localhost6.localdomain6" || |
991 IsLocalhostTLD(host)) | |
988 return true; | 992 return true; |
989 | 993 |
990 IPAddressNumber ip_number; | 994 IPAddressNumber ip_number; |
991 if (ParseIPLiteralToNumber(host, &ip_number)) { | 995 if (ParseIPLiteralToNumber(host, &ip_number)) { |
992 size_t size = ip_number.size(); | 996 size_t size = ip_number.size(); |
993 switch (size) { | 997 switch (size) { |
994 case kIPv4AddressSize: { | 998 case kIPv4AddressSize: { |
995 IPAddressNumber localhost_prefix; | 999 IPAddressNumber localhost_prefix; |
996 localhost_prefix.push_back(127); | 1000 localhost_prefix.push_back(127); |
997 for (int i = 0; i < 3; ++i) { | 1001 for (int i = 0; i < 3; ++i) { |
998 localhost_prefix.push_back(0); | 1002 localhost_prefix.push_back(0); |
999 } | 1003 } |
1000 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8); | 1004 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8); |
1001 } | 1005 } |
1002 | 1006 |
1003 case kIPv6AddressSize: { | 1007 case kIPv6AddressSize: { |
1004 struct in6_addr sin6_addr; | 1008 struct in6_addr sin6_addr; |
1005 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize); | 1009 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize); |
1006 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr); | 1010 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr); |
1007 } | 1011 } |
1008 | 1012 |
1009 default: | 1013 default: |
1010 NOTREACHED(); | 1014 NOTREACHED(); |
1011 } | 1015 } |
1012 } | 1016 } |
1013 | 1017 |
1014 return false; | 1018 return false; |
1015 } | 1019 } |
1016 | 1020 |
1021 bool IsLocalhostTLD(const std::string& host) { | |
1022 if (host.empty()) | |
1023 return false; | |
1024 | |
1025 size_t host_len = host.size(); | |
1026 if (*host.rbegin() == '.') | |
1027 --host_len; | |
1028 if (host_len < kLocalhostTLDLength) | |
1029 return false; | |
1030 | |
1031 const char* host_suffix = host.data() + host_len - kLocalhostTLDLength; | |
1032 return base::strncasecmp(host_suffix, kLocalhostTLD, kLocalhostTLDLength) == | |
1033 0; | |
1034 } | |
1035 | |
1017 NetworkInterface::NetworkInterface() | 1036 NetworkInterface::NetworkInterface() |
1018 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { | 1037 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { |
1019 } | 1038 } |
1020 | 1039 |
1021 NetworkInterface::NetworkInterface(const std::string& name, | 1040 NetworkInterface::NetworkInterface(const std::string& name, |
1022 const std::string& friendly_name, | 1041 const std::string& friendly_name, |
1023 uint32 interface_index, | 1042 uint32 interface_index, |
1024 NetworkChangeNotifier::ConnectionType type, | 1043 NetworkChangeNotifier::ConnectionType type, |
1025 const IPAddressNumber& address, | 1044 const IPAddressNumber& address, |
1026 uint32 prefix_length, | 1045 uint32 prefix_length, |
(...skipping 29 matching lines...) Expand all Loading... | |
1056 | 1075 |
1057 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1076 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
1058 IPAddressNumber all_ones(mask.size(), 0xFF); | 1077 IPAddressNumber all_ones(mask.size(), 0xFF); |
1059 return CommonPrefixLength(mask, all_ones); | 1078 return CommonPrefixLength(mask, all_ones); |
1060 } | 1079 } |
1061 | 1080 |
1062 ScopedWifiOptions::~ScopedWifiOptions() { | 1081 ScopedWifiOptions::~ScopedWifiOptions() { |
1063 } | 1082 } |
1064 | 1083 |
1065 } // namespace net | 1084 } // namespace net |
OLD | NEW |