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

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

Issue 938093003: Always treat .localhost as loopback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move constants, add a test case, style fix Created 5 years, 9 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
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 963 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 } 974 }
975 975
976 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 976 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
977 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 977 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
978 if (!port_field) 978 if (!port_field)
979 return -1; 979 return -1;
980 return base::NetToHost16(*port_field); 980 return base::NetToHost16(*port_field);
981 } 981 }
982 982
983 bool IsLocalhost(const std::string& host) { 983 bool IsLocalhost(const std::string& host) {
984 if (host == "localhost" || 984 if (host == "localhost" || host == "localhost.localdomain" ||
985 host == "localhost.localdomain" || 985 host == "localhost6" || host == "localhost6.localdomain6" ||
986 host == "localhost6" || 986 IsLocalhostTLD(host))
987 host == "localhost6.localdomain6")
988 return true; 987 return true;
989 988
990 IPAddressNumber ip_number; 989 IPAddressNumber ip_number;
991 if (ParseIPLiteralToNumber(host, &ip_number)) { 990 if (ParseIPLiteralToNumber(host, &ip_number)) {
992 size_t size = ip_number.size(); 991 size_t size = ip_number.size();
993 switch (size) { 992 switch (size) {
994 case kIPv4AddressSize: { 993 case kIPv4AddressSize: {
995 IPAddressNumber localhost_prefix; 994 IPAddressNumber localhost_prefix;
996 localhost_prefix.push_back(127); 995 localhost_prefix.push_back(127);
997 for (int i = 0; i < 3; ++i) { 996 for (int i = 0; i < 3; ++i) {
998 localhost_prefix.push_back(0); 997 localhost_prefix.push_back(0);
999 } 998 }
1000 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8); 999 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8);
1001 } 1000 }
1002 1001
1003 case kIPv6AddressSize: { 1002 case kIPv6AddressSize: {
1004 struct in6_addr sin6_addr; 1003 struct in6_addr sin6_addr;
1005 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize); 1004 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize);
1006 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr); 1005 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr);
1007 } 1006 }
1008 1007
1009 default: 1008 default:
1010 NOTREACHED(); 1009 NOTREACHED();
1011 } 1010 }
1012 } 1011 }
1013 1012
1014 return false; 1013 return false;
1015 } 1014 }
1016 1015
1016 bool IsLocalhostTLD(const std::string& host) {
1017 const char kLocalhostTLD[] = ".localhost";
1018 const size_t kLocalhostTLDLength = arraysize(kLocalhostTLD) - 1;
1019
1020 if (host.empty())
1021 return false;
1022
1023 size_t host_len = host.size();
1024 if (*host.rbegin() == '.')
Deprecated (see juliatuttle) 2015/03/26 17:04:36 Nit: host.back()? I think we're using C++11 now, s
Ryan Sleevi 2015/03/26 17:10:56 language, not library, so rbegin is needed
1025 --host_len;
1026 if (host_len < kLocalhostTLDLength)
1027 return false;
1028
1029 const char* host_suffix = host.data() + host_len - kLocalhostTLDLength;
1030 return base::strncasecmp(host_suffix, kLocalhostTLD, kLocalhostTLDLength) ==
1031 0;
1032 }
1033
1017 NetworkInterface::NetworkInterface() 1034 NetworkInterface::NetworkInterface()
1018 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { 1035 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
1019 } 1036 }
1020 1037
1021 NetworkInterface::NetworkInterface(const std::string& name, 1038 NetworkInterface::NetworkInterface(const std::string& name,
1022 const std::string& friendly_name, 1039 const std::string& friendly_name,
1023 uint32 interface_index, 1040 uint32 interface_index,
1024 NetworkChangeNotifier::ConnectionType type, 1041 NetworkChangeNotifier::ConnectionType type,
1025 const IPAddressNumber& address, 1042 const IPAddressNumber& address,
1026 uint32 prefix_length, 1043 uint32 prefix_length,
(...skipping 29 matching lines...) Expand all
1056 1073
1057 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 1074 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
1058 IPAddressNumber all_ones(mask.size(), 0xFF); 1075 IPAddressNumber all_ones(mask.size(), 0xFF);
1059 return CommonPrefixLength(mask, all_ones); 1076 return CommonPrefixLength(mask, all_ones);
1060 } 1077 }
1061 1078
1062 ScopedWifiOptions::~ScopedWifiOptions() { 1079 ScopedWifiOptions::~ScopedWifiOptions() {
1063 } 1080 }
1064 1081
1065 } // namespace net 1082 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698