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

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

Issue 642403002: git cl format the first third of the net/base directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit Created 6 years, 1 month 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
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 #include "net/base/winsock_init.h" 61 #include "net/base/winsock_init.h"
62 #endif 62 #endif
63 63
64 namespace net { 64 namespace net {
65 65
66 namespace { 66 namespace {
67 67
68 // The general list of blocked ports. Will be blocked unless a specific 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) 69 // protocol overrides it. (Ex: ftp can use ports 20 and 21)
70 static const int kRestrictedPorts[] = { 70 static const int kRestrictedPorts[] = {
71 1, // tcpmux 71 1, // tcpmux
72 7, // echo 72 7, // echo
73 9, // discard 73 9, // discard
74 11, // systat 74 11, // systat
75 13, // daytime 75 13, // daytime
76 15, // netstat 76 15, // netstat
77 17, // qotd 77 17, // qotd
78 19, // chargen 78 19, // chargen
79 20, // ftp data 79 20, // ftp data
80 21, // ftp access 80 21, // ftp access
81 22, // ssh 81 22, // ssh
82 23, // telnet 82 23, // telnet
83 25, // smtp 83 25, // smtp
84 37, // time 84 37, // time
85 42, // name 85 42, // name
86 43, // nicname 86 43, // nicname
87 53, // domain 87 53, // domain
88 77, // priv-rjs 88 77, // priv-rjs
89 79, // finger 89 79, // finger
90 87, // ttylink 90 87, // ttylink
91 95, // supdup 91 95, // supdup
92 101, // hostriame 92 101, // hostriame
93 102, // iso-tsap 93 102, // iso-tsap
94 103, // gppitnp 94 103, // gppitnp
95 104, // acr-nema 95 104, // acr-nema
96 109, // pop2 96 109, // pop2
97 110, // pop3 97 110, // pop3
98 111, // sunrpc 98 111, // sunrpc
99 113, // auth 99 113, // auth
100 115, // sftp 100 115, // sftp
101 117, // uucp-path 101 117, // uucp-path
102 119, // nntp 102 119, // nntp
103 123, // NTP 103 123, // NTP
104 135, // loc-srv /epmap 104 135, // loc-srv /epmap
105 139, // netbios 105 139, // netbios
106 143, // imap2 106 143, // imap2
107 179, // BGP 107 179, // BGP
108 389, // ldap 108 389, // ldap
109 465, // smtp+ssl 109 465, // smtp+ssl
110 512, // print / exec 110 512, // print / exec
111 513, // login 111 513, // login
112 514, // shell 112 514, // shell
113 515, // printer 113 515, // printer
114 526, // tempo 114 526, // tempo
115 530, // courier 115 530, // courier
116 531, // chat 116 531, // chat
117 532, // netnews 117 532, // netnews
118 540, // uucp 118 540, // uucp
119 556, // remotefs 119 556, // remotefs
120 563, // nntp+ssl 120 563, // nntp+ssl
121 587, // stmp? 121 587, // stmp?
122 601, // ?? 122 601, // ??
123 636, // ldap+ssl 123 636, // ldap+ssl
124 993, // ldap+ssl 124 993, // ldap+ssl
125 995, // pop3+ssl 125 995, // pop3+ssl
126 2049, // nfs 126 2049, // nfs
127 3659, // apple-sasl / PasswordServer 127 3659, // apple-sasl / PasswordServer
128 4045, // lockd 128 4045, // lockd
129 6000, // X11 129 6000, // X11
130 6665, // Alternate IRC [Apple addition] 130 6665, // Alternate IRC [Apple addition]
131 6666, // Alternate IRC [Apple addition] 131 6666, // Alternate IRC [Apple addition]
132 6667, // Standard IRC [Apple addition] 132 6667, // Standard IRC [Apple addition]
133 6668, // Alternate IRC [Apple addition] 133 6668, // Alternate IRC [Apple addition]
134 6669, // Alternate IRC [Apple addition] 134 6669, // Alternate IRC [Apple addition]
135 0xFFFF, // Used to block all invalid port numbers (see 135 0xFFFF, // Used to block all invalid port numbers (see
136 // third_party/WebKit/Source/platform/weborigin/KURL.cpp, 136 // third_party/WebKit/Source/platform/weborigin/KURL.cpp,
137 // KURL::port()) 137 // KURL::port())
138 }; 138 };
139 139
140 // FTP overrides the following restricted ports. 140 // FTP overrides the following restricted ports.
141 static const int kAllowedFtpPorts[] = { 141 static const int kAllowedFtpPorts[] = {
142 21, // ftp data 142 21, // ftp data
143 22, // ssh 143 22, // ssh
144 }; 144 };
145 145
146 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, 146 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number,
147 const unsigned char* ip_prefix, 147 const unsigned char* ip_prefix,
148 size_t prefix_length_in_bits) { 148 size_t prefix_length_in_bits) {
149 // Compare all the bytes that fall entirely within the prefix. 149 // Compare all the bytes that fall entirely within the prefix.
150 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; 150 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8;
151 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { 151 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) {
152 if (ip_number[i] != ip_prefix[i]) 152 if (ip_number[i] != ip_prefix[i])
153 return false; 153 return false;
(...skipping 30 matching lines...) Expand all
184 // Content-Length: 2375\n 184 // Content-Length: 2375\n
185 // Content-Type: text/html; charset=UTF-8\n 185 // Content-Type: text/html; charset=UTF-8\n
186 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n 186 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n
187 if (headers.empty()) 187 if (headers.empty())
188 return std::string(); 188 return std::string();
189 189
190 std::string match('\n' + name + ':'); 190 std::string match('\n' + name + ':');
191 191
192 std::string::const_iterator begin = 192 std::string::const_iterator begin =
193 std::search(headers.begin(), headers.end(), match.begin(), match.end(), 193 std::search(headers.begin(), headers.end(), match.begin(), match.end(),
194 base::CaseInsensitiveCompareASCII<char>()); 194 base::CaseInsensitiveCompareASCII<char>());
195 195
196 if (begin == headers.end()) 196 if (begin == headers.end())
197 return std::string(); 197 return std::string();
198 198
199 begin += match.length(); 199 begin += match.length();
200 200
201 std::string ret; 201 std::string ret;
202 base::TrimWhitespace(std::string(begin, 202 base::TrimWhitespace(
203 std::find(begin, headers.end(), '\n')), 203 std::string(begin, std::find(begin, headers.end(), '\n')), base::TRIM_ALL,
204 base::TRIM_ALL, &ret); 204 &ret);
205 return ret; 205 return ret;
206 } 206 }
207 207
208 std::string CanonicalizeHost(const std::string& host, 208 std::string CanonicalizeHost(const std::string& host,
209 url::CanonHostInfo* host_info) { 209 url::CanonHostInfo* host_info) {
210 // Try to canonicalize the host. 210 // Try to canonicalize the host.
211 const url::Component raw_host_component(0, static_cast<int>(host.length())); 211 const url::Component raw_host_component(0, static_cast<int>(host.length()));
212 std::string canon_host; 212 std::string canon_host;
213 url::StdStringCanonOutput canon_host_output(&canon_host); 213 url::StdStringCanonOutput canon_host_output(&canon_host);
214 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component, 214 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component,
215 &canon_host_output, host_info); 215 &canon_host_output, host_info);
216 216
217 if (host_info->out_host.is_nonempty() && 217 if (host_info->out_host.is_nonempty() &&
218 host_info->family != url::CanonHostInfo::BROKEN) { 218 host_info->family != url::CanonHostInfo::BROKEN) {
219 // Success! Assert that there's no extra garbage. 219 // Success! Assert that there's no extra garbage.
220 canon_host_output.Complete(); 220 canon_host_output.Complete();
221 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length())); 221 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
222 } else { 222 } else {
223 // Empty host, or canonicalization failed. We'll return empty. 223 // Empty host, or canonicalization failed. We'll return empty.
224 canon_host.clear(); 224 canon_host.clear();
225 } 225 }
226 226
227 return canon_host; 227 return canon_host;
228 } 228 }
229 229
230 std::string GetDirectoryListingHeader(const base::string16& title) { 230 std::string GetDirectoryListingHeader(const base::string16& title) {
231 static const base::StringPiece header( 231 static const base::StringPiece header(
232 NetModule::GetResource(IDR_DIR_HEADER_HTML)); 232 NetModule::GetResource(IDR_DIR_HEADER_HTML));
233 // This can be null in unit tests. 233 // This can be null in unit tests.
234 DLOG_IF(WARNING, header.empty()) << 234 DLOG_IF(WARNING, header.empty())
235 "Missing resource: directory listing header"; 235 << "Missing resource: directory listing header";
236 236
237 std::string result; 237 std::string result;
238 if (!header.empty()) 238 if (!header.empty())
239 result.assign(header.data(), header.size()); 239 result.assign(header.data(), header.size());
240 240
241 result.append("<script>start("); 241 result.append("<script>start(");
242 base::EscapeJSONString(title, true, &result); 242 base::EscapeJSONString(title, true, &result);
243 result.append(");</script>\n"); 243 result.append(");</script>\n");
244 244
245 return result; 245 return result;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 return false; 301 return false;
302 } 302 }
303 } 303 }
304 return true; 304 return true;
305 } 305 }
306 306
307 bool IsPortAllowedByFtp(int port) { 307 bool IsPortAllowedByFtp(int port) {
308 int array_size = arraysize(kAllowedFtpPorts); 308 int array_size = arraysize(kAllowedFtpPorts);
309 for (int i = 0; i < array_size; i++) { 309 for (int i = 0; i < array_size; i++) {
310 if (kAllowedFtpPorts[i] == port) { 310 if (kAllowedFtpPorts[i] == port) {
311 return true; 311 return true;
312 } 312 }
313 } 313 }
314 // Port not explicitly allowed by FTP, so return the default restrictions. 314 // Port not explicitly allowed by FTP, so return the default restrictions.
315 return IsPortAllowedByDefault(port); 315 return IsPortAllowedByDefault(port);
316 } 316 }
317 317
318 bool IsPortAllowedByOverride(int port) { 318 bool IsPortAllowedByOverride(int port) {
319 if (g_explicitly_allowed_ports.Get().empty()) 319 if (g_explicitly_allowed_ports.Get().empty())
320 return false; 320 return false;
321 321
(...skipping 23 matching lines...) Expand all
345 const char* auth_begin = &(*host_and_port_begin); 345 const char* auth_begin = &(*host_and_port_begin);
346 int auth_len = host_and_port_end - host_and_port_begin; 346 int auth_len = host_and_port_end - host_and_port_begin;
347 347
348 url::Component auth_component(0, auth_len); 348 url::Component auth_component(0, auth_len);
349 url::Component username_component; 349 url::Component username_component;
350 url::Component password_component; 350 url::Component password_component;
351 url::Component hostname_component; 351 url::Component hostname_component;
352 url::Component port_component; 352 url::Component port_component;
353 353
354 url::ParseAuthority(auth_begin, auth_component, &username_component, 354 url::ParseAuthority(auth_begin, auth_component, &username_component,
355 &password_component, &hostname_component, &port_component); 355 &password_component, &hostname_component,
356 &port_component);
356 357
357 // There shouldn't be a username/password. 358 // There shouldn't be a username/password.
358 if (username_component.is_valid() || password_component.is_valid()) 359 if (username_component.is_valid() || password_component.is_valid())
359 return false; 360 return false;
360 361
361 if (!hostname_component.is_nonempty()) 362 if (!hostname_component.is_nonempty())
362 return false; // Failed parsing. 363 return false; // Failed parsing.
363 364
364 int parsed_port_number = -1; 365 int parsed_port_number = -1;
365 if (port_component.is_nonempty()) { 366 if (port_component.is_nonempty()) {
366 parsed_port_number = url::ParsePort(auth_begin, port_component); 367 parsed_port_number = url::ParsePort(auth_begin, port_component);
367 368
368 // If parsing failed, port_number will be either PORT_INVALID or 369 // If parsing failed, port_number will be either PORT_INVALID or
369 // PORT_UNSPECIFIED, both of which are negative. 370 // PORT_UNSPECIFIED, both of which are negative.
370 if (parsed_port_number < 0) 371 if (parsed_port_number < 0)
371 return false; // Failed parsing the port number. 372 return false; // Failed parsing the port number.
372 } 373 }
373 374
374 if (port_component.len == 0) 375 if (port_component.len == 0)
375 return false; // Reject inputs like "foo:" 376 return false; // Reject inputs like "foo:"
376 377
377 unsigned char tmp_ipv6_addr[16]; 378 unsigned char tmp_ipv6_addr[16];
378 379
379 // If the hostname starts with a bracket, it is either an IPv6 literal or 380 // If the hostname starts with a bracket, it is either an IPv6 literal or
380 // invalid. If it is an IPv6 literal then strip the brackets. 381 // invalid. If it is an IPv6 literal then strip the brackets.
381 if (hostname_component.len > 0 && 382 if (hostname_component.len > 0 &&
382 auth_begin[hostname_component.begin] == '[') { 383 auth_begin[hostname_component.begin] == '[') {
383 if (auth_begin[hostname_component.end() - 1] == ']' && 384 if (auth_begin[hostname_component.end() - 1] == ']' &&
384 url::IPv6AddressToNumber( 385 url::IPv6AddressToNumber(auth_begin, hostname_component,
385 auth_begin, hostname_component, tmp_ipv6_addr)) { 386 tmp_ipv6_addr)) {
386 // Strip the brackets. 387 // Strip the brackets.
387 hostname_component.begin++; 388 hostname_component.begin++;
388 hostname_component.len -= 2; 389 hostname_component.len -= 2;
389 } else { 390 } else {
390 return false; 391 return false;
391 } 392 }
392 } 393 }
393 394
394 // Pass results back to caller. 395 // Pass results back to caller.
395 host->assign(auth_begin + hostname_component.begin, hostname_component.len); 396 host->assign(auth_begin + hostname_component.begin, hostname_component.len);
396 *port = parsed_port_number; 397 *port = parsed_port_number;
397 398
398 return true; // Success. 399 return true; // Success.
399 } 400 }
400 401
401 bool ParseHostAndPort(const std::string& host_and_port, 402 bool ParseHostAndPort(const std::string& host_and_port,
402 std::string* host, 403 std::string* host,
403 int* port) { 404 int* port) {
404 return ParseHostAndPort( 405 return ParseHostAndPort(host_and_port.begin(), host_and_port.end(), host,
405 host_and_port.begin(), host_and_port.end(), host, port); 406 port);
406 } 407 }
407 408
408 std::string GetHostAndPort(const GURL& url) { 409 std::string GetHostAndPort(const GURL& url) {
409 // For IPv6 literals, GURL::host() already includes the brackets so it is 410 // For IPv6 literals, GURL::host() already includes the brackets so it is
410 // safe to just append a colon. 411 // safe to just append a colon.
411 return base::StringPrintf("%s:%d", url.host().c_str(), 412 return base::StringPrintf("%s:%d", url.host().c_str(),
412 url.EffectiveIntPort()); 413 url.EffectiveIntPort());
413 } 414 }
414 415
415 std::string GetHostAndOptionalPort(const GURL& url) { 416 std::string GetHostAndOptionalPort(const GURL& url) {
416 // For IPv6 literals, GURL::host() already includes the brackets 417 // For IPv6 literals, GURL::host() already includes the brackets
417 // so it is safe to just append a colon. 418 // so it is safe to just append a colon.
418 if (url.has_port()) 419 if (url.has_port())
419 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); 420 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
420 return url.host(); 421 return url.host();
421 } 422 }
422 423
423 bool IsHostnameNonUnique(const std::string& hostname) { 424 bool IsHostnameNonUnique(const std::string& hostname) {
424 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address. 425 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
425 const std::string host_or_ip = hostname.find(':') != std::string::npos ? 426 const std::string host_or_ip =
426 "[" + hostname + "]" : hostname; 427 hostname.find(':') != std::string::npos ? "[" + hostname + "]" : hostname;
427 url::CanonHostInfo host_info; 428 url::CanonHostInfo host_info;
428 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info); 429 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
429 430
430 // If canonicalization fails, then the input is truly malformed. However, 431 // If canonicalization fails, then the input is truly malformed. However,
431 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique. 432 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
432 if (canonical_name.empty()) 433 if (canonical_name.empty())
433 return false; 434 return false;
434 435
435 // If |hostname| is an IP address, check to see if it's in an IANA-reserved 436 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
436 // range. 437 // range.
437 if (host_info.IsIPAddress()) { 438 if (host_info.IsIPAddress()) {
438 IPAddressNumber host_addr; 439 IPAddressNumber host_addr;
439 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin, 440 if (!ParseIPLiteralToNumber(
440 host_info.out_host.len), 441 hostname.substr(host_info.out_host.begin, host_info.out_host.len),
441 &host_addr)) { 442 &host_addr)) {
442 return false; 443 return false;
443 } 444 }
444 switch (host_info.family) { 445 switch (host_info.family) {
445 case url::CanonHostInfo::IPV4: 446 case url::CanonHostInfo::IPV4:
446 case url::CanonHostInfo::IPV6: 447 case url::CanonHostInfo::IPV6:
447 return IsIPAddressReserved(host_addr); 448 return IsIPAddressReserved(host_addr);
448 case url::CanonHostInfo::NEUTRAL: 449 case url::CanonHostInfo::NEUTRAL:
449 case url::CanonHostInfo::BROKEN: 450 case url::CanonHostInfo::BROKEN:
450 return false; 451 return false;
451 } 452 }
(...skipping 17 matching lines...) Expand all
469 // Don't compare IPv4 and IPv6 addresses (they have different range 470 // Don't compare IPv4 and IPv6 addresses (they have different range
470 // reservations). Keep separate reservation arrays for each IP type, and 471 // reservations). Keep separate reservation arrays for each IP type, and
471 // consolidate adjacent reserved ranges within a reservation array when 472 // consolidate adjacent reserved ranges within a reservation array when
472 // possible. 473 // possible.
473 // Sources for info: 474 // Sources for info:
474 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml 475 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
475 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml 476 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
476 // They're formatted here with the prefix as the last element. For example: 477 // They're formatted here with the prefix as the last element. For example:
477 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10. 478 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10.
478 bool IsIPAddressReserved(const IPAddressNumber& host_addr) { 479 bool IsIPAddressReserved(const IPAddressNumber& host_addr) {
479 static const unsigned char kReservedIPv4[][5] = { 480 static const unsigned char kReservedIPv4[][5] = {{0, 0, 0, 0, 8},
480 { 0,0,0,0,8 }, { 10,0,0,0,8 }, { 100,64,0,0,10 }, { 127,0,0,0,8 }, 481 {10, 0, 0, 0, 8},
481 { 169,254,0,0,16 }, { 172,16,0,0,12 }, { 192,0,2,0,24 }, 482 {100, 64, 0, 0, 10},
482 { 192,88,99,0,24 }, { 192,168,0,0,16 }, { 198,18,0,0,15 }, 483 {127, 0, 0, 0, 8},
483 { 198,51,100,0,24 }, { 203,0,113,0,24 }, { 224,0,0,0,3 } 484 {169, 254, 0, 0, 16},
484 }; 485 {172, 16, 0, 0, 12},
486 {192, 0, 2, 0, 24},
487 {192, 88, 99, 0, 24},
488 {192, 168, 0, 0, 16},
489 {198, 18, 0, 0, 15},
490 {198, 51, 100, 0, 24},
491 {203, 0, 113, 0, 24},
492 {224, 0, 0, 0, 3}};
485 static const unsigned char kReservedIPv6[][17] = { 493 static const unsigned char kReservedIPv6[][17] = {
486 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8 }, 494 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8},
487 { 0x40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, 495 {0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
488 { 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, 496 {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
489 { 0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 }, 497 {0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
490 { 0xe0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 }, 498 {0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
491 { 0xf0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5 }, 499 {0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
492 { 0xf8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6 }, 500 {0xf8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
493 { 0xfc,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7 }, 501 {0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7},
494 { 0xfe,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 }, 502 {0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9},
495 { 0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, 503 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
496 { 0xfe,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, 504 {0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
497 }; 505 };
498 size_t array_size = 0; 506 size_t array_size = 0;
499 const unsigned char* array = NULL; 507 const unsigned char* array = NULL;
500 switch (host_addr.size()) { 508 switch (host_addr.size()) {
501 case kIPv4AddressSize: 509 case kIPv4AddressSize:
502 array_size = arraysize(kReservedIPv4); 510 array_size = arraysize(kReservedIPv4);
503 array = kReservedIPv4[0]; 511 array = kReservedIPv4[0];
504 break; 512 break;
505 case kIPv6AddressSize: 513 case kIPv6AddressSize:
506 array_size = arraysize(kReservedIPv6); 514 array_size = arraysize(kReservedIPv6);
507 array = kReservedIPv6[0]; 515 array = kReservedIPv6[0];
508 break; 516 break;
509 } 517 }
510 if (!array) 518 if (!array)
511 return false; 519 return false;
512 size_t width = host_addr.size() + 1; 520 size_t width = host_addr.size() + 1;
513 for (size_t i = 0; i < array_size; ++i, array += width) { 521 for (size_t i = 0; i < array_size; ++i, array += width) {
514 if (IPNumberPrefixCheck(host_addr, array, array[width-1])) 522 if (IPNumberPrefixCheck(host_addr, array, array[width - 1]))
515 return true; 523 return true;
516 } 524 }
517 return false; 525 return false;
518 } 526 }
519 527
520 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other) 528 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other)
521 : addr_len(other.addr_len), 529 : addr_len(other.addr_len),
522 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) { 530 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {
523 memcpy(addr, other.addr, addr_len); 531 memcpy(addr, other.addr, addr_len);
524 } 532 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 *address_len = kIPv6AddressSize; 564 *address_len = kIPv6AddressSize;
557 if (port) 565 if (port)
558 *port = base::NetToHost16(addr->sin6_port); 566 *port = base::NetToHost16(addr->sin6_port);
559 return true; 567 return true;
560 } 568 }
561 569
562 #if defined(OS_WIN) 570 #if defined(OS_WIN)
563 if (sock_addr->sa_family == AF_BTH) { 571 if (sock_addr->sa_family == AF_BTH) {
564 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) 572 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
565 return false; 573 return false;
566 const SOCKADDR_BTH* addr = 574 const SOCKADDR_BTH* addr = reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
567 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
568 *address = reinterpret_cast<const uint8*>(&addr->btAddr); 575 *address = reinterpret_cast<const uint8*>(&addr->btAddr);
569 *address_len = kBluetoothAddressSize; 576 *address_len = kBluetoothAddressSize;
570 if (port) 577 if (port)
571 *port = addr->port; 578 *port = addr->port;
572 return true; 579 return true;
573 } 580 }
574 #endif 581 #endif
575 582
576 return false; // Unrecognized |sa_family|. 583 return false; // Unrecognized |sa_family|.
577 } 584 }
578 585
579 std::string IPAddressToString(const uint8* address, 586 std::string IPAddressToString(const uint8* address, size_t address_len) {
580 size_t address_len) {
581 std::string str; 587 std::string str;
582 url::StdStringCanonOutput output(&str); 588 url::StdStringCanonOutput output(&str);
583 589
584 if (address_len == kIPv4AddressSize) { 590 if (address_len == kIPv4AddressSize) {
585 url::AppendIPv4Address(address, &output); 591 url::AppendIPv4Address(address, &output);
586 } else if (address_len == kIPv6AddressSize) { 592 } else if (address_len == kIPv6AddressSize) {
587 url::AppendIPv6Address(address, &output); 593 url::AppendIPv6Address(address, &output);
588 } else { 594 } else {
589 CHECK(false) << "Invalid IP address with length: " << address_len; 595 CHECK(false) << "Invalid IP address with length: " << address_len;
590 } 596 }
(...skipping 11 matching lines...) Expand all
602 // Need to bracket IPv6 addresses since they contain colons. 608 // Need to bracket IPv6 addresses since they contain colons.
603 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); 609 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
604 } 610 }
605 return base::StringPrintf("%s:%d", address_str.c_str(), port); 611 return base::StringPrintf("%s:%d", address_str.c_str(), port);
606 } 612 }
607 613
608 std::string NetAddressToString(const struct sockaddr* sa, 614 std::string NetAddressToString(const struct sockaddr* sa,
609 socklen_t sock_addr_len) { 615 socklen_t sock_addr_len) {
610 const uint8* address; 616 const uint8* address;
611 size_t address_len; 617 size_t address_len;
612 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 618 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, &address_len,
613 &address_len, NULL)) { 619 NULL)) {
614 NOTREACHED(); 620 NOTREACHED();
615 return std::string(); 621 return std::string();
616 } 622 }
617 return IPAddressToString(address, address_len); 623 return IPAddressToString(address, address_len);
618 } 624 }
619 625
620 std::string NetAddressToStringWithPort(const struct sockaddr* sa, 626 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
621 socklen_t sock_addr_len) { 627 socklen_t sock_addr_len) {
622 const uint8* address; 628 const uint8* address;
623 size_t address_len; 629 size_t address_len;
624 uint16 port; 630 uint16 port;
625 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 631 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, &address_len,
626 &address_len, &port)) { 632 &port)) {
627 NOTREACHED(); 633 NOTREACHED();
628 return std::string(); 634 return std::string();
629 } 635 }
630 return IPAddressToStringWithPort(address, address_len, port); 636 return IPAddressToStringWithPort(address, address_len, port);
631 } 637 }
632 638
633 std::string IPAddressToString(const IPAddressNumber& addr) { 639 std::string IPAddressToString(const IPAddressNumber& addr) {
634 return IPAddressToString(&addr.front(), addr.size()); 640 return IPAddressToString(&addr.front(), addr.size());
635 } 641 }
636 642
637 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, 643 std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
638 uint16 port) { 644 uint16 port) {
639 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 645 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
640 } 646 }
641 647
642 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 648 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
643 return std::string(reinterpret_cast<const char *>(&addr.front()), 649 return std::string(reinterpret_cast<const char*>(&addr.front()), addr.size());
644 addr.size());
645 } 650 }
646 651
647 std::string GetHostName() { 652 std::string GetHostName() {
648 #if defined(OS_NACL) 653 #if defined(OS_NACL)
649 NOTIMPLEMENTED(); 654 NOTIMPLEMENTED();
650 return std::string(); 655 return std::string();
651 #else // defined(OS_NACL) 656 #else // defined(OS_NACL)
652 #if defined(OS_WIN) 657 #if defined(OS_WIN)
653 EnsureWinsockInit(); 658 EnsureWinsockInit();
654 #endif 659 #endif
(...skipping 18 matching lines...) Expand all
673 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags); 678 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags);
674 } 679 }
675 680
676 std::string GetHostOrSpecFromURL(const GURL& url) { 681 std::string GetHostOrSpecFromURL(const GURL& url) {
677 return url.has_host() ? TrimEndingDot(url.host()) : url.spec(); 682 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
678 } 683 }
679 684
680 bool CanStripTrailingSlash(const GURL& url) { 685 bool CanStripTrailingSlash(const GURL& url) {
681 // Omit the path only for standard, non-file URLs with nothing but "/" after 686 // Omit the path only for standard, non-file URLs with nothing but "/" after
682 // the hostname. 687 // the hostname.
683 return url.IsStandard() && !url.SchemeIsFile() && 688 return url.IsStandard() && !url.SchemeIsFile() && !url.SchemeIsFileSystem() &&
684 !url.SchemeIsFileSystem() && !url.has_query() && !url.has_ref() 689 !url.has_query() && !url.has_ref() && url.path() == "/";
685 && url.path() == "/";
686 } 690 }
687 691
688 GURL SimplifyUrlForRequest(const GURL& url) { 692 GURL SimplifyUrlForRequest(const GURL& url) {
689 DCHECK(url.is_valid()); 693 DCHECK(url.is_valid());
690 GURL::Replacements replacements; 694 GURL::Replacements replacements;
691 replacements.ClearUsername(); 695 replacements.ClearUsername();
692 replacements.ClearPassword(); 696 replacements.ClearPassword();
693 replacements.ClearRef(); 697 replacements.ClearRef();
694 return url.ReplaceComponents(replacements); 698 return url.ReplaceComponents(replacements);
695 } 699 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 return false; 751 return false;
748 #elif defined(OS_POSIX) 752 #elif defined(OS_POSIX)
749 struct ifaddrs* interface_addr = NULL; 753 struct ifaddrs* interface_addr = NULL;
750 int rv = getifaddrs(&interface_addr); 754 int rv = getifaddrs(&interface_addr);
751 if (rv != 0) { 755 if (rv != 0) {
752 DVLOG(1) << "getifaddrs() failed with errno = " << errno; 756 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
753 return false; 757 return false;
754 } 758 }
755 759
756 bool result = true; 760 bool result = true;
757 for (struct ifaddrs* interface = interface_addr; 761 for (struct ifaddrs* interface = interface_addr; interface != NULL;
758 interface != NULL;
759 interface = interface->ifa_next) { 762 interface = interface->ifa_next) {
760 if (!(IFF_UP & interface->ifa_flags)) 763 if (!(IFF_UP & interface->ifa_flags))
761 continue; 764 continue;
762 if (IFF_LOOPBACK & interface->ifa_flags) 765 if (IFF_LOOPBACK & interface->ifa_flags)
763 continue; 766 continue;
764 const struct sockaddr* addr = interface->ifa_addr; 767 const struct sockaddr* addr = interface->ifa_addr;
765 if (!addr) 768 if (!addr)
766 continue; 769 continue;
767 if (addr->sa_family == AF_INET6) { 770 if (addr->sa_family == AF_INET6) {
768 // Safe cast since this is AF_INET6. 771 // Safe cast since this is AF_INET6.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 bool ParseURLHostnameToNumber(const std::string& hostname, 820 bool ParseURLHostnameToNumber(const std::string& hostname,
818 IPAddressNumber* ip_number) { 821 IPAddressNumber* ip_number) {
819 // |hostname| is an already canoncalized hostname, conforming to RFC 3986. 822 // |hostname| is an already canoncalized hostname, conforming to RFC 3986.
820 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with 823 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with
821 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952. 824 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952.
822 url::Component host_comp(0, hostname.size()); 825 url::Component host_comp(0, hostname.size());
823 826
824 // If it has a bracket, try parsing it as an IPv6 address. 827 // If it has a bracket, try parsing it as an IPv6 address.
825 if (hostname[0] == '[') { 828 if (hostname[0] == '[') {
826 ip_number->resize(16); // 128 bits. 829 ip_number->resize(16); // 128 bits.
827 return url::IPv6AddressToNumber( 830 return url::IPv6AddressToNumber(hostname.data(), host_comp,
828 hostname.data(), host_comp, &(*ip_number)[0]); 831 &(*ip_number)[0]);
829 } 832 }
830 833
831 // Otherwise, try IPv4. 834 // Otherwise, try IPv4.
832 ip_number->resize(4); // 32 bits. 835 ip_number->resize(4); // 32 bits.
833 int num_components; 836 int num_components;
834 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( 837 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
835 hostname.data(), host_comp, &(*ip_number)[0], &num_components); 838 hostname.data(), host_comp, &(*ip_number)[0], &num_components);
836 return family == url::CanonHostInfo::IPV4; 839 return family == url::CanonHostInfo::IPV4;
837 } 840 }
838 841
(...skipping 17 matching lines...) Expand all
856 url::Component host_comp(0, ip_literal.size()); 859 url::Component host_comp(0, ip_literal.size());
857 int num_components; 860 int num_components;
858 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( 861 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
859 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components); 862 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components);
860 return family == url::CanonHostInfo::IPV4; 863 return family == url::CanonHostInfo::IPV4;
861 } 864 }
862 865
863 namespace { 866 namespace {
864 867
865 const unsigned char kIPv4MappedPrefix[] = 868 const unsigned char kIPv4MappedPrefix[] =
866 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }; 869 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF};
867 } 870 }
868 871
869 IPAddressNumber ConvertIPv4NumberToIPv6Number( 872 IPAddressNumber ConvertIPv4NumberToIPv6Number(
870 const IPAddressNumber& ipv4_number) { 873 const IPAddressNumber& ipv4_number) {
871 DCHECK(ipv4_number.size() == 4); 874 DCHECK(ipv4_number.size() == 4);
872 875
873 // IPv4-mapped addresses are formed by: 876 // IPv4-mapped addresses are formed by:
874 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>. 877 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>.
875 IPAddressNumber ipv6_number; 878 IPAddressNumber ipv6_number;
876 ipv6_number.reserve(16); 879 ipv6_number.reserve(16);
877 ipv6_number.insert(ipv6_number.end(), 880 ipv6_number.insert(ipv6_number.end(), kIPv4MappedPrefix,
878 kIPv4MappedPrefix,
879 kIPv4MappedPrefix + arraysize(kIPv4MappedPrefix)); 881 kIPv4MappedPrefix + arraysize(kIPv4MappedPrefix));
880 ipv6_number.insert(ipv6_number.end(), ipv4_number.begin(), ipv4_number.end()); 882 ipv6_number.insert(ipv6_number.end(), ipv4_number.begin(), ipv4_number.end());
881 return ipv6_number; 883 return ipv6_number;
882 } 884 }
883 885
884 bool IsIPv4Mapped(const IPAddressNumber& address) { 886 bool IsIPv4Mapped(const IPAddressNumber& address) {
885 if (address.size() != kIPv6AddressSize) 887 if (address.size() != kIPv6AddressSize)
886 return false; 888 return false;
887 return std::equal(address.begin(), 889 return std::equal(address.begin(),
888 address.begin() + arraysize(kIPv4MappedPrefix), 890 address.begin() + arraysize(kIPv4MappedPrefix),
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 } 971 }
970 972
971 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 973 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
972 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 974 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
973 if (!port_field) 975 if (!port_field)
974 return -1; 976 return -1;
975 return base::NetToHost16(*port_field); 977 return base::NetToHost16(*port_field);
976 } 978 }
977 979
978 bool IsLocalhost(const std::string& host) { 980 bool IsLocalhost(const std::string& host) {
979 if (host == "localhost" || 981 if (host == "localhost" || host == "localhost.localdomain" ||
980 host == "localhost.localdomain" || 982 host == "localhost6" || host == "localhost6.localdomain6")
981 host == "localhost6" ||
982 host == "localhost6.localdomain6")
983 return true; 983 return true;
984 984
985 IPAddressNumber ip_number; 985 IPAddressNumber ip_number;
986 if (ParseIPLiteralToNumber(host, &ip_number)) { 986 if (ParseIPLiteralToNumber(host, &ip_number)) {
987 size_t size = ip_number.size(); 987 size_t size = ip_number.size();
988 switch (size) { 988 switch (size) {
989 case kIPv4AddressSize: { 989 case kIPv4AddressSize: {
990 IPAddressNumber localhost_prefix; 990 IPAddressNumber localhost_prefix;
991 localhost_prefix.push_back(127); 991 localhost_prefix.push_back(127);
992 for (int i = 0; i < 3; ++i) { 992 for (int i = 0; i < 3; ++i) {
(...skipping 10 matching lines...) Expand all
1003 1003
1004 default: 1004 default:
1005 NOTREACHED(); 1005 NOTREACHED();
1006 } 1006 }
1007 } 1007 }
1008 1008
1009 return false; 1009 return false;
1010 } 1010 }
1011 1011
1012 NetworkInterface::NetworkInterface() 1012 NetworkInterface::NetworkInterface()
1013 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), 1013 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), network_prefix(0) {
1014 network_prefix(0) {
1015 } 1014 }
1016 1015
1017 NetworkInterface::NetworkInterface(const std::string& name, 1016 NetworkInterface::NetworkInterface(const std::string& name,
1018 const std::string& friendly_name, 1017 const std::string& friendly_name,
1019 uint32 interface_index, 1018 uint32 interface_index,
1020 NetworkChangeNotifier::ConnectionType type, 1019 NetworkChangeNotifier::ConnectionType type,
1021 const IPAddressNumber& address, 1020 const IPAddressNumber& address,
1022 uint32 network_prefix, 1021 uint32 network_prefix,
1023 int ip_address_attributes) 1022 int ip_address_attributes)
1024 : name(name), 1023 : name(name),
(...skipping 27 matching lines...) Expand all
1052 1051
1053 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 1052 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
1054 IPAddressNumber all_ones(mask.size(), 0xFF); 1053 IPAddressNumber all_ones(mask.size(), 0xFF);
1055 return CommonPrefixLength(mask, all_ones); 1054 return CommonPrefixLength(mask, all_ones);
1056 } 1055 }
1057 1056
1058 ScopedWifiOptions::~ScopedWifiOptions() { 1057 ScopedWifiOptions::~ScopedWifiOptions() {
1059 } 1058 }
1060 1059
1061 } // namespace net 1060 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698