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 = strlen(kLocalhostTLD); | |
Ryan Sleevi
2015/02/27 03:21:10
DANGER: strlen is a function call, which violates
estark
2015/02/27 05:00:47
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) { | |
Ryan Sleevi
2015/02/27 03:21:10
You can avoid the double comparison
if (host.empt
estark
2015/02/27 05:00:47
Done.
| |
1022 size_t host_length = host.length(); | |
1023 | |
1024 // First try to match against ".localhost", then against ".localhost." | |
1025 // with the trailing dot. | |
1026 | |
1027 if (host_length < kLocalhostTLDLength - 1) | |
1028 return false; | |
1029 if (host.compare(host_length - kLocalhostTLDLength + 1, std::string::npos, | |
1030 kLocalhostTLD, 0, kLocalhostTLDLength - 1) == 0) | |
1031 return true; | |
1032 | |
1033 if (host_length < kLocalhostTLDLength) | |
1034 return false; | |
1035 return host.compare(host_length - kLocalhostTLDLength, std::string::npos, | |
1036 kLocalhostTLD) == 0; | |
1037 } | |
1038 | |
1017 NetworkInterface::NetworkInterface() | 1039 NetworkInterface::NetworkInterface() |
1018 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { | 1040 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { |
1019 } | 1041 } |
1020 | 1042 |
1021 NetworkInterface::NetworkInterface(const std::string& name, | 1043 NetworkInterface::NetworkInterface(const std::string& name, |
1022 const std::string& friendly_name, | 1044 const std::string& friendly_name, |
1023 uint32 interface_index, | 1045 uint32 interface_index, |
1024 NetworkChangeNotifier::ConnectionType type, | 1046 NetworkChangeNotifier::ConnectionType type, |
1025 const IPAddressNumber& address, | 1047 const IPAddressNumber& address, |
1026 uint32 prefix_length, | 1048 uint32 prefix_length, |
(...skipping 29 matching lines...) Expand all Loading... | |
1056 | 1078 |
1057 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1079 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
1058 IPAddressNumber all_ones(mask.size(), 0xFF); | 1080 IPAddressNumber all_ones(mask.size(), 0xFF); |
1059 return CommonPrefixLength(mask, all_ones); | 1081 return CommonPrefixLength(mask, all_ones); |
1060 } | 1082 } |
1061 | 1083 |
1062 ScopedWifiOptions::~ScopedWifiOptions() { | 1084 ScopedWifiOptions::~ScopedWifiOptions() { |
1063 } | 1085 } |
1064 | 1086 |
1065 } // namespace net | 1087 } // namespace net |
OLD | NEW |