Remove unused var from OffTheRecordProfileImplTest.GetHostZoomMap
[chromium-blink-merge.git] / net / base / net_util.cc
blob941dca9ab53ba1a4cb3214712a4ae7e16cf7ee62
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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
7 #include <errno.h>
8 #include <string.h>
10 #include <algorithm>
11 #include <iterator>
12 #include <set>
14 #include "build/build_config.h"
16 #if defined(OS_WIN)
17 #include <windows.h>
18 #include <iphlpapi.h>
19 #include <winsock2.h>
20 #include <ws2bth.h>
21 #pragma comment(lib, "iphlpapi.lib")
22 #elif defined(OS_POSIX)
23 #include <fcntl.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <unistd.h>
27 #if !defined(OS_NACL)
28 #include <net/if.h>
29 #if !defined(OS_ANDROID)
30 #include <ifaddrs.h>
31 #endif // !defined(OS_NACL)
32 #endif // !defined(OS_ANDROID)
33 #endif // defined(OS_POSIX)
35 #include "base/basictypes.h"
36 #include "base/json/string_escape.h"
37 #include "base/lazy_instance.h"
38 #include "base/logging.h"
39 #include "base/strings/string_number_conversions.h"
40 #include "base/strings/string_piece.h"
41 #include "base/strings/string_split.h"
42 #include "base/strings/string_util.h"
43 #include "base/strings/stringprintf.h"
44 #include "base/strings/utf_string_conversions.h"
45 #include "base/sys_byteorder.h"
46 #include "base/values.h"
47 #include "grit/net_resources.h"
48 #include "url/gurl.h"
49 #include "url/url_canon.h"
50 #include "url/url_canon_ip.h"
51 #include "url/url_parse.h"
52 #include "net/base/dns_util.h"
53 #include "net/base/net_module.h"
54 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
55 #include "net/http/http_content_disposition.h"
57 #if defined(OS_ANDROID)
58 #include "net/android/network_library.h"
59 #endif
60 #if defined(OS_WIN)
61 #include "net/base/winsock_init.h"
62 #endif
64 namespace net {
66 namespace {
68 // The general list of blocked ports. Will be blocked unless a specific
69 // protocol overrides it. (Ex: ftp can use ports 20 and 21)
70 static const int kRestrictedPorts[] = {
71 1, // tcpmux
72 7, // echo
73 9, // discard
74 11, // systat
75 13, // daytime
76 15, // netstat
77 17, // qotd
78 19, // chargen
79 20, // ftp data
80 21, // ftp access
81 22, // ssh
82 23, // telnet
83 25, // smtp
84 37, // time
85 42, // name
86 43, // nicname
87 53, // domain
88 77, // priv-rjs
89 79, // finger
90 87, // ttylink
91 95, // supdup
92 101, // hostriame
93 102, // iso-tsap
94 103, // gppitnp
95 104, // acr-nema
96 109, // pop2
97 110, // pop3
98 111, // sunrpc
99 113, // auth
100 115, // sftp
101 117, // uucp-path
102 119, // nntp
103 123, // NTP
104 135, // loc-srv /epmap
105 139, // netbios
106 143, // imap2
107 179, // BGP
108 389, // ldap
109 465, // smtp+ssl
110 512, // print / exec
111 513, // login
112 514, // shell
113 515, // printer
114 526, // tempo
115 530, // courier
116 531, // chat
117 532, // netnews
118 540, // uucp
119 556, // remotefs
120 563, // nntp+ssl
121 587, // stmp?
122 601, // ??
123 636, // ldap+ssl
124 993, // ldap+ssl
125 995, // pop3+ssl
126 2049, // nfs
127 3659, // apple-sasl / PasswordServer
128 4045, // lockd
129 6000, // X11
130 6665, // Alternate IRC [Apple addition]
131 6666, // Alternate IRC [Apple addition]
132 6667, // Standard IRC [Apple addition]
133 6668, // Alternate IRC [Apple addition]
134 6669, // Alternate IRC [Apple addition]
135 0xFFFF, // Used to block all invalid port numbers (see
136 // third_party/WebKit/Source/platform/weborigin/KURL.cpp,
137 // KURL::port())
140 // FTP overrides the following restricted ports.
141 static const int kAllowedFtpPorts[] = {
142 21, // ftp data
143 22, // ssh
146 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number,
147 const unsigned char* ip_prefix,
148 size_t prefix_length_in_bits) {
149 // Compare all the bytes that fall entirely within the prefix.
150 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8;
151 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) {
152 if (ip_number[i] != ip_prefix[i])
153 return false;
156 // In case the prefix was not a multiple of 8, there will be 1 byte
157 // which is only partially masked.
158 int remaining_bits = prefix_length_in_bits % 8;
159 if (remaining_bits != 0) {
160 unsigned char mask = 0xFF << (8 - remaining_bits);
161 int i = num_entire_bytes_in_prefix;
162 if ((ip_number[i] & mask) != (ip_prefix[i] & mask))
163 return false;
165 return true;
168 } // namespace
170 static base::LazyInstance<std::multiset<int> >::Leaky
171 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER;
173 size_t GetCountOfExplicitlyAllowedPorts() {
174 return g_explicitly_allowed_ports.Get().size();
177 std::string GetSpecificHeader(const std::string& headers,
178 const std::string& name) {
179 // We want to grab the Value from the "Key: Value" pairs in the headers,
180 // which should look like this (no leading spaces, \n-separated) (we format
181 // them this way in url_request_inet.cc):
182 // HTTP/1.1 200 OK\n
183 // ETag: "6d0b8-947-24f35ec0"\n
184 // Content-Length: 2375\n
185 // Content-Type: text/html; charset=UTF-8\n
186 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n
187 if (headers.empty())
188 return std::string();
190 std::string match('\n' + name + ':');
192 std::string::const_iterator begin =
193 std::search(headers.begin(), headers.end(), match.begin(), match.end(),
194 base::CaseInsensitiveCompareASCII<char>());
196 if (begin == headers.end())
197 return std::string();
199 begin += match.length();
201 std::string ret;
202 base::TrimWhitespace(std::string(begin,
203 std::find(begin, headers.end(), '\n')),
204 base::TRIM_ALL, &ret);
205 return ret;
208 std::string CanonicalizeHost(const std::string& host,
209 url::CanonHostInfo* host_info) {
210 // Try to canonicalize the host.
211 const url::Component raw_host_component(0, static_cast<int>(host.length()));
212 std::string canon_host;
213 url::StdStringCanonOutput canon_host_output(&canon_host);
214 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component,
215 &canon_host_output, host_info);
217 if (host_info->out_host.is_nonempty() &&
218 host_info->family != url::CanonHostInfo::BROKEN) {
219 // Success! Assert that there's no extra garbage.
220 canon_host_output.Complete();
221 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
222 } else {
223 // Empty host, or canonicalization failed. We'll return empty.
224 canon_host.clear();
227 return canon_host;
230 std::string GetDirectoryListingHeader(const base::string16& title) {
231 static const base::StringPiece header(
232 NetModule::GetResource(IDR_DIR_HEADER_HTML));
233 // This can be null in unit tests.
234 DLOG_IF(WARNING, header.empty()) <<
235 "Missing resource: directory listing header";
237 std::string result;
238 if (!header.empty())
239 result.assign(header.data(), header.size());
241 result.append("<script>start(");
242 base::EscapeJSONString(title, true, &result);
243 result.append(");</script>\n");
245 return result;
248 inline bool IsHostCharAlphanumeric(char c) {
249 // We can just check lowercase because uppercase characters have already been
250 // normalized.
251 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'));
254 bool IsCanonicalizedHostCompliant(const std::string& host,
255 const std::string& desired_tld) {
256 if (host.empty())
257 return false;
259 bool in_component = false;
260 bool most_recent_component_started_alphanumeric = false;
261 bool last_char_was_underscore = false;
263 for (std::string::const_iterator i(host.begin()); i != host.end(); ++i) {
264 const char c = *i;
265 if (!in_component) {
266 most_recent_component_started_alphanumeric = IsHostCharAlphanumeric(c);
267 if (!most_recent_component_started_alphanumeric && (c != '-'))
268 return false;
269 in_component = true;
270 } else {
271 if (c == '.') {
272 if (last_char_was_underscore)
273 return false;
274 in_component = false;
275 } else if (IsHostCharAlphanumeric(c) || (c == '-')) {
276 last_char_was_underscore = false;
277 } else if (c == '_') {
278 last_char_was_underscore = true;
279 } else {
280 return false;
285 return most_recent_component_started_alphanumeric ||
286 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0]));
289 base::string16 StripWWW(const base::string16& text) {
290 const base::string16 www(base::ASCIIToUTF16("www."));
291 return StartsWith(text, www, true) ? text.substr(www.length()) : text;
294 base::string16 StripWWWFromHost(const GURL& url) {
295 DCHECK(url.is_valid());
296 return StripWWW(base::ASCIIToUTF16(url.host()));
299 bool IsPortAllowedByDefault(int port) {
300 int array_size = arraysize(kRestrictedPorts);
301 for (int i = 0; i < array_size; i++) {
302 if (kRestrictedPorts[i] == port) {
303 return false;
306 return true;
309 bool IsPortAllowedByFtp(int port) {
310 int array_size = arraysize(kAllowedFtpPorts);
311 for (int i = 0; i < array_size; i++) {
312 if (kAllowedFtpPorts[i] == port) {
313 return true;
316 // Port not explicitly allowed by FTP, so return the default restrictions.
317 return IsPortAllowedByDefault(port);
320 bool IsPortAllowedByOverride(int port) {
321 if (g_explicitly_allowed_ports.Get().empty())
322 return false;
324 return g_explicitly_allowed_ports.Get().count(port) > 0;
327 int SetNonBlocking(int fd) {
328 #if defined(OS_WIN)
329 unsigned long no_block = 1;
330 return ioctlsocket(fd, FIONBIO, &no_block);
331 #elif defined(OS_POSIX)
332 int flags = fcntl(fd, F_GETFL, 0);
333 if (-1 == flags)
334 return flags;
335 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
336 #endif
339 bool ParseHostAndPort(std::string::const_iterator host_and_port_begin,
340 std::string::const_iterator host_and_port_end,
341 std::string* host,
342 int* port) {
343 if (host_and_port_begin >= host_and_port_end)
344 return false;
346 // When using url, we use char*.
347 const char* auth_begin = &(*host_and_port_begin);
348 int auth_len = host_and_port_end - host_and_port_begin;
350 url::Component auth_component(0, auth_len);
351 url::Component username_component;
352 url::Component password_component;
353 url::Component hostname_component;
354 url::Component port_component;
356 url::ParseAuthority(auth_begin, auth_component, &username_component,
357 &password_component, &hostname_component, &port_component);
359 // There shouldn't be a username/password.
360 if (username_component.is_valid() || password_component.is_valid())
361 return false;
363 if (!hostname_component.is_nonempty())
364 return false; // Failed parsing.
366 int parsed_port_number = -1;
367 if (port_component.is_nonempty()) {
368 parsed_port_number = url::ParsePort(auth_begin, port_component);
370 // If parsing failed, port_number will be either PORT_INVALID or
371 // PORT_UNSPECIFIED, both of which are negative.
372 if (parsed_port_number < 0)
373 return false; // Failed parsing the port number.
376 if (port_component.len == 0)
377 return false; // Reject inputs like "foo:"
379 // Pass results back to caller.
380 host->assign(auth_begin + hostname_component.begin, hostname_component.len);
381 *port = parsed_port_number;
383 return true; // Success.
386 bool ParseHostAndPort(const std::string& host_and_port,
387 std::string* host,
388 int* port) {
389 return ParseHostAndPort(
390 host_and_port.begin(), host_and_port.end(), host, port);
393 std::string GetHostAndPort(const GURL& url) {
394 // For IPv6 literals, GURL::host() already includes the brackets so it is
395 // safe to just append a colon.
396 return base::StringPrintf("%s:%d", url.host().c_str(),
397 url.EffectiveIntPort());
400 std::string GetHostAndOptionalPort(const GURL& url) {
401 // For IPv6 literals, GURL::host() already includes the brackets
402 // so it is safe to just append a colon.
403 if (url.has_port())
404 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
405 return url.host();
408 bool IsHostnameNonUnique(const std::string& hostname) {
409 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
410 const std::string host_or_ip = hostname.find(':') != std::string::npos ?
411 "[" + hostname + "]" : hostname;
412 url::CanonHostInfo host_info;
413 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
415 // If canonicalization fails, then the input is truly malformed. However,
416 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
417 if (canonical_name.empty())
418 return false;
420 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
421 // range.
422 if (host_info.IsIPAddress()) {
423 IPAddressNumber host_addr;
424 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin,
425 host_info.out_host.len),
426 &host_addr)) {
427 return false;
429 switch (host_info.family) {
430 case url::CanonHostInfo::IPV4:
431 case url::CanonHostInfo::IPV6:
432 return IsIPAddressReserved(host_addr);
433 case url::CanonHostInfo::NEUTRAL:
434 case url::CanonHostInfo::BROKEN:
435 return false;
439 // Check for a registry controlled portion of |hostname|, ignoring private
440 // registries, as they already chain to ICANN-administered registries,
441 // and explicitly ignoring unknown registries.
443 // Note: This means that as new gTLDs are introduced on the Internet, they
444 // will be treated as non-unique until the registry controlled domain list
445 // is updated. However, because gTLDs are expected to provide significant
446 // advance notice to deprecate older versions of this code, this an
447 // acceptable tradeoff.
448 return 0 == registry_controlled_domains::GetRegistryLength(
449 canonical_name,
450 registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
451 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
454 // Don't compare IPv4 and IPv6 addresses (they have different range
455 // reservations). Keep separate reservation arrays for each IP type, and
456 // consolidate adjacent reserved ranges within a reservation array when
457 // possible.
458 // Sources for info:
459 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
460 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
461 // They're formatted here with the prefix as the last element. For example:
462 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10.
463 bool IsIPAddressReserved(const IPAddressNumber& host_addr) {
464 static const unsigned char kReservedIPv4[][5] = {
465 { 0,0,0,0,8 }, { 10,0,0,0,8 }, { 100,64,0,0,10 }, { 127,0,0,0,8 },
466 { 169,254,0,0,16 }, { 172,16,0,0,12 }, { 192,0,2,0,24 },
467 { 192,88,99,0,24 }, { 192,168,0,0,16 }, { 198,18,0,0,15 },
468 { 198,51,100,0,24 }, { 203,0,113,0,24 }, { 224,0,0,0,3 }
470 static const unsigned char kReservedIPv6[][17] = {
471 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8 },
472 { 0x40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 },
473 { 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 },
474 { 0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 },
475 { 0xe0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
476 { 0xf0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5 },
477 { 0xf8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6 },
478 { 0xfc,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7 },
479 { 0xfe,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 },
480 { 0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 },
481 { 0xfe,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 },
483 size_t array_size = 0;
484 const unsigned char* array = NULL;
485 switch (host_addr.size()) {
486 case kIPv4AddressSize:
487 array_size = arraysize(kReservedIPv4);
488 array = kReservedIPv4[0];
489 break;
490 case kIPv6AddressSize:
491 array_size = arraysize(kReservedIPv6);
492 array = kReservedIPv6[0];
493 break;
495 if (!array)
496 return false;
497 size_t width = host_addr.size() + 1;
498 for (size_t i = 0; i < array_size; ++i, array += width) {
499 if (IPNumberPrefixCheck(host_addr, array, array[width-1]))
500 return true;
502 return false;
505 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other)
506 : addr_len(other.addr_len),
507 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {
508 memcpy(addr, other.addr, addr_len);
511 void SockaddrStorage::operator=(const SockaddrStorage& other) {
512 addr_len = other.addr_len;
513 // addr is already set to &this->addr_storage by default ctor.
514 memcpy(addr, other.addr, addr_len);
517 // Extracts the address and port portions of a sockaddr.
518 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
519 socklen_t sock_addr_len,
520 const uint8** address,
521 size_t* address_len,
522 uint16* port) {
523 if (sock_addr->sa_family == AF_INET) {
524 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in)))
525 return false;
526 const struct sockaddr_in* addr =
527 reinterpret_cast<const struct sockaddr_in*>(sock_addr);
528 *address = reinterpret_cast<const uint8*>(&addr->sin_addr);
529 *address_len = kIPv4AddressSize;
530 if (port)
531 *port = base::NetToHost16(addr->sin_port);
532 return true;
535 if (sock_addr->sa_family == AF_INET6) {
536 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6)))
537 return false;
538 const struct sockaddr_in6* addr =
539 reinterpret_cast<const struct sockaddr_in6*>(sock_addr);
540 *address = reinterpret_cast<const uint8*>(&addr->sin6_addr);
541 *address_len = kIPv6AddressSize;
542 if (port)
543 *port = base::NetToHost16(addr->sin6_port);
544 return true;
547 #if defined(OS_WIN)
548 if (sock_addr->sa_family == AF_BTH) {
549 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
550 return false;
551 const SOCKADDR_BTH* addr =
552 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
553 *address = reinterpret_cast<const uint8*>(&addr->btAddr);
554 *address_len = kBluetoothAddressSize;
555 if (port)
556 *port = addr->port;
557 return true;
559 #endif
561 return false; // Unrecognized |sa_family|.
564 std::string IPAddressToString(const uint8* address,
565 size_t address_len) {
566 std::string str;
567 url::StdStringCanonOutput output(&str);
569 if (address_len == kIPv4AddressSize) {
570 url::AppendIPv4Address(address, &output);
571 } else if (address_len == kIPv6AddressSize) {
572 url::AppendIPv6Address(address, &output);
573 } else {
574 CHECK(false) << "Invalid IP address with length: " << address_len;
577 output.Complete();
578 return str;
581 std::string IPAddressToStringWithPort(const uint8* address,
582 size_t address_len,
583 uint16 port) {
584 std::string address_str = IPAddressToString(address, address_len);
586 if (address_len == kIPv6AddressSize) {
587 // Need to bracket IPv6 addresses since they contain colons.
588 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
590 return base::StringPrintf("%s:%d", address_str.c_str(), port);
593 std::string NetAddressToString(const struct sockaddr* sa,
594 socklen_t sock_addr_len) {
595 const uint8* address;
596 size_t address_len;
597 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
598 &address_len, NULL)) {
599 NOTREACHED();
600 return std::string();
602 return IPAddressToString(address, address_len);
605 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
606 socklen_t sock_addr_len) {
607 const uint8* address;
608 size_t address_len;
609 uint16 port;
610 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address,
611 &address_len, &port)) {
612 NOTREACHED();
613 return std::string();
615 return IPAddressToStringWithPort(address, address_len, port);
618 std::string IPAddressToString(const IPAddressNumber& addr) {
619 return IPAddressToString(&addr.front(), addr.size());
622 std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
623 uint16 port) {
624 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
627 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
628 return std::string(reinterpret_cast<const char *>(&addr.front()),
629 addr.size());
632 std::string GetHostName() {
633 #if defined(OS_NACL)
634 NOTIMPLEMENTED();
635 return std::string();
636 #else // defined(OS_NACL)
637 #if defined(OS_WIN)
638 EnsureWinsockInit();
639 #endif
641 // Host names are limited to 255 bytes.
642 char buffer[256];
643 int result = gethostname(buffer, sizeof(buffer));
644 if (result != 0) {
645 DVLOG(1) << "gethostname() failed with " << result;
646 buffer[0] = '\0';
648 return std::string(buffer);
649 #endif // !defined(OS_NACL)
652 void GetIdentityFromURL(const GURL& url,
653 base::string16* username,
654 base::string16* password) {
655 UnescapeRule::Type flags =
656 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS;
657 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags);
658 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags);
661 std::string GetHostOrSpecFromURL(const GURL& url) {
662 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
665 bool CanStripTrailingSlash(const GURL& url) {
666 // Omit the path only for standard, non-file URLs with nothing but "/" after
667 // the hostname.
668 return url.IsStandard() && !url.SchemeIsFile() &&
669 !url.SchemeIsFileSystem() && !url.has_query() && !url.has_ref()
670 && url.path() == "/";
673 GURL SimplifyUrlForRequest(const GURL& url) {
674 DCHECK(url.is_valid());
675 GURL::Replacements replacements;
676 replacements.ClearUsername();
677 replacements.ClearPassword();
678 replacements.ClearRef();
679 return url.ReplaceComponents(replacements);
682 // Specifies a comma separated list of port numbers that should be accepted
683 // despite bans. If the string is invalid no allowed ports are stored.
684 void SetExplicitlyAllowedPorts(const std::string& allowed_ports) {
685 if (allowed_ports.empty())
686 return;
688 std::multiset<int> ports;
689 size_t last = 0;
690 size_t size = allowed_ports.size();
691 // The comma delimiter.
692 const std::string::value_type kComma = ',';
694 // Overflow is still possible for evil user inputs.
695 for (size_t i = 0; i <= size; ++i) {
696 // The string should be composed of only digits and commas.
697 if (i != size && !IsAsciiDigit(allowed_ports[i]) &&
698 (allowed_ports[i] != kComma))
699 return;
700 if (i == size || allowed_ports[i] == kComma) {
701 if (i > last) {
702 int port;
703 base::StringToInt(base::StringPiece(allowed_ports.begin() + last,
704 allowed_ports.begin() + i),
705 &port);
706 ports.insert(port);
708 last = i + 1;
711 g_explicitly_allowed_ports.Get() = ports;
714 ScopedPortException::ScopedPortException(int port) : port_(port) {
715 g_explicitly_allowed_ports.Get().insert(port);
718 ScopedPortException::~ScopedPortException() {
719 std::multiset<int>::iterator it =
720 g_explicitly_allowed_ports.Get().find(port_);
721 if (it != g_explicitly_allowed_ports.Get().end())
722 g_explicitly_allowed_ports.Get().erase(it);
723 else
724 NOTREACHED();
727 bool HaveOnlyLoopbackAddresses() {
728 #if defined(OS_ANDROID)
729 return android::HaveOnlyLoopbackAddresses();
730 #elif defined(OS_NACL)
731 NOTIMPLEMENTED();
732 return false;
733 #elif defined(OS_POSIX)
734 struct ifaddrs* interface_addr = NULL;
735 int rv = getifaddrs(&interface_addr);
736 if (rv != 0) {
737 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
738 return false;
741 bool result = true;
742 for (struct ifaddrs* interface = interface_addr;
743 interface != NULL;
744 interface = interface->ifa_next) {
745 if (!(IFF_UP & interface->ifa_flags))
746 continue;
747 if (IFF_LOOPBACK & interface->ifa_flags)
748 continue;
749 const struct sockaddr* addr = interface->ifa_addr;
750 if (!addr)
751 continue;
752 if (addr->sa_family == AF_INET6) {
753 // Safe cast since this is AF_INET6.
754 const struct sockaddr_in6* addr_in6 =
755 reinterpret_cast<const struct sockaddr_in6*>(addr);
756 const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
757 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
758 continue;
760 if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
761 continue;
763 result = false;
764 break;
766 freeifaddrs(interface_addr);
767 return result;
768 #elif defined(OS_WIN)
769 // TODO(wtc): implement with the GetAdaptersAddresses function.
770 NOTIMPLEMENTED();
771 return false;
772 #else
773 NOTIMPLEMENTED();
774 return false;
775 #endif // defined(various platforms)
778 AddressFamily GetAddressFamily(const IPAddressNumber& address) {
779 switch (address.size()) {
780 case kIPv4AddressSize:
781 return ADDRESS_FAMILY_IPV4;
782 case kIPv6AddressSize:
783 return ADDRESS_FAMILY_IPV6;
784 default:
785 return ADDRESS_FAMILY_UNSPECIFIED;
789 int ConvertAddressFamily(AddressFamily address_family) {
790 switch (address_family) {
791 case ADDRESS_FAMILY_UNSPECIFIED:
792 return AF_UNSPEC;
793 case ADDRESS_FAMILY_IPV4:
794 return AF_INET;
795 case ADDRESS_FAMILY_IPV6:
796 return AF_INET6;
798 NOTREACHED();
799 return AF_UNSPEC;
802 bool ParseIPLiteralToNumber(const std::string& ip_literal,
803 IPAddressNumber* ip_number) {
804 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains
805 // a colon however, it must be an IPv6 address.
806 if (ip_literal.find(':') != std::string::npos) {
807 // GURL expects IPv6 hostnames to be surrounded with brackets.
808 std::string host_brackets = "[" + ip_literal + "]";
809 url::Component host_comp(0, host_brackets.size());
811 // Try parsing the hostname as an IPv6 literal.
812 ip_number->resize(16); // 128 bits.
813 return url::IPv6AddressToNumber(host_brackets.data(), host_comp,
814 &(*ip_number)[0]);
817 // Otherwise the string is an IPv4 address.
818 ip_number->resize(4); // 32 bits.
819 url::Component host_comp(0, ip_literal.size());
820 int num_components;
821 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
822 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components);
823 return family == url::CanonHostInfo::IPV4;
826 namespace {
828 const unsigned char kIPv4MappedPrefix[] =
829 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
832 IPAddressNumber ConvertIPv4NumberToIPv6Number(
833 const IPAddressNumber& ipv4_number) {
834 DCHECK(ipv4_number.size() == 4);
836 // IPv4-mapped addresses are formed by:
837 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>.
838 IPAddressNumber ipv6_number;
839 ipv6_number.reserve(16);
840 ipv6_number.insert(ipv6_number.end(),
841 kIPv4MappedPrefix,
842 kIPv4MappedPrefix + arraysize(kIPv4MappedPrefix));
843 ipv6_number.insert(ipv6_number.end(), ipv4_number.begin(), ipv4_number.end());
844 return ipv6_number;
847 bool IsIPv4Mapped(const IPAddressNumber& address) {
848 if (address.size() != kIPv6AddressSize)
849 return false;
850 return std::equal(address.begin(),
851 address.begin() + arraysize(kIPv4MappedPrefix),
852 kIPv4MappedPrefix);
855 IPAddressNumber ConvertIPv4MappedToIPv4(const IPAddressNumber& address) {
856 DCHECK(IsIPv4Mapped(address));
857 return IPAddressNumber(address.begin() + arraysize(kIPv4MappedPrefix),
858 address.end());
861 bool ParseCIDRBlock(const std::string& cidr_literal,
862 IPAddressNumber* ip_number,
863 size_t* prefix_length_in_bits) {
864 // We expect CIDR notation to match one of these two templates:
865 // <IPv4-literal> "/" <number of bits>
866 // <IPv6-literal> "/" <number of bits>
868 std::vector<std::string> parts;
869 base::SplitString(cidr_literal, '/', &parts);
870 if (parts.size() != 2)
871 return false;
873 // Parse the IP address.
874 if (!ParseIPLiteralToNumber(parts[0], ip_number))
875 return false;
877 // Parse the prefix length.
878 int number_of_bits = -1;
879 if (!base::StringToInt(parts[1], &number_of_bits))
880 return false;
882 // Make sure the prefix length is in a valid range.
883 if (number_of_bits < 0 ||
884 number_of_bits > static_cast<int>(ip_number->size() * 8))
885 return false;
887 *prefix_length_in_bits = static_cast<size_t>(number_of_bits);
888 return true;
891 bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
892 const IPAddressNumber& ip_prefix,
893 size_t prefix_length_in_bits) {
894 // Both the input IP address and the prefix IP address should be
895 // either IPv4 or IPv6.
896 DCHECK(ip_number.size() == 4 || ip_number.size() == 16);
897 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16);
899 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8);
901 // In case we have an IPv6 / IPv4 mismatch, convert the IPv4 addresses to
902 // IPv6 addresses in order to do the comparison.
903 if (ip_number.size() != ip_prefix.size()) {
904 if (ip_number.size() == 4) {
905 return IPNumberMatchesPrefix(ConvertIPv4NumberToIPv6Number(ip_number),
906 ip_prefix, prefix_length_in_bits);
908 return IPNumberMatchesPrefix(ip_number,
909 ConvertIPv4NumberToIPv6Number(ip_prefix),
910 96 + prefix_length_in_bits);
913 return IPNumberPrefixCheck(ip_number, &ip_prefix[0], prefix_length_in_bits);
916 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
917 socklen_t address_len) {
918 if (address->sa_family == AF_INET) {
919 DCHECK_LE(sizeof(sockaddr_in), static_cast<size_t>(address_len));
920 const struct sockaddr_in* sockaddr =
921 reinterpret_cast<const struct sockaddr_in*>(address);
922 return &sockaddr->sin_port;
923 } else if (address->sa_family == AF_INET6) {
924 DCHECK_LE(sizeof(sockaddr_in6), static_cast<size_t>(address_len));
925 const struct sockaddr_in6* sockaddr =
926 reinterpret_cast<const struct sockaddr_in6*>(address);
927 return &sockaddr->sin6_port;
928 } else {
929 NOTREACHED();
930 return NULL;
934 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
935 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
936 if (!port_field)
937 return -1;
938 return base::NetToHost16(*port_field);
941 bool IsLocalhost(const std::string& host) {
942 if (host == "localhost" ||
943 host == "localhost.localdomain" ||
944 host == "localhost6" ||
945 host == "localhost6.localdomain6")
946 return true;
948 IPAddressNumber ip_number;
949 if (ParseIPLiteralToNumber(host, &ip_number)) {
950 size_t size = ip_number.size();
951 switch (size) {
952 case kIPv4AddressSize: {
953 IPAddressNumber localhost_prefix;
954 localhost_prefix.push_back(127);
955 for (int i = 0; i < 3; ++i) {
956 localhost_prefix.push_back(0);
958 return IPNumberMatchesPrefix(ip_number, localhost_prefix, 8);
961 case kIPv6AddressSize: {
962 struct in6_addr sin6_addr;
963 memcpy(&sin6_addr, &ip_number[0], kIPv6AddressSize);
964 return !!IN6_IS_ADDR_LOOPBACK(&sin6_addr);
967 default:
968 NOTREACHED();
972 return false;
975 NetworkInterface::NetworkInterface()
976 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN),
977 network_prefix(0) {
980 NetworkInterface::NetworkInterface(const std::string& name,
981 const std::string& friendly_name,
982 uint32 interface_index,
983 NetworkChangeNotifier::ConnectionType type,
984 const IPAddressNumber& address,
985 size_t network_prefix)
986 : name(name),
987 friendly_name(friendly_name),
988 interface_index(interface_index),
989 type(type),
990 address(address),
991 network_prefix(network_prefix) {
994 NetworkInterface::~NetworkInterface() {
997 unsigned CommonPrefixLength(const IPAddressNumber& a1,
998 const IPAddressNumber& a2) {
999 DCHECK_EQ(a1.size(), a2.size());
1000 for (size_t i = 0; i < a1.size(); ++i) {
1001 unsigned diff = a1[i] ^ a2[i];
1002 if (!diff)
1003 continue;
1004 for (unsigned j = 0; j < CHAR_BIT; ++j) {
1005 if (diff & (1 << (CHAR_BIT - 1)))
1006 return i * CHAR_BIT + j;
1007 diff <<= 1;
1009 NOTREACHED();
1011 return a1.size() * CHAR_BIT;
1014 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
1015 IPAddressNumber all_ones(mask.size(), 0xFF);
1016 return CommonPrefixLength(mask, all_ones);
1019 } // namespace net