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

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: Created 6 years, 2 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
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 29 matching lines...) Expand all
183 // ETag: "6d0b8-947-24f35ec0"\n 183 // ETag: "6d0b8-947-24f35ec0"\n
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(),
194 base::CaseInsensitiveCompareASCII<char>()); 194 headers.end(),
195 match.begin(),
196 match.end(),
197 base::CaseInsensitiveCompareASCII<char>());
195 198
196 if (begin == headers.end()) 199 if (begin == headers.end())
197 return std::string(); 200 return std::string();
198 201
199 begin += match.length(); 202 begin += match.length();
200 203
201 std::string ret; 204 std::string ret;
202 base::TrimWhitespace(std::string(begin, 205 base::TrimWhitespace(
203 std::find(begin, headers.end(), '\n')), 206 std::string(begin, std::find(begin, headers.end(), '\n')),
204 base::TRIM_ALL, &ret); 207 base::TRIM_ALL,
208 &ret);
205 return ret; 209 return ret;
206 } 210 }
207 211
208 std::string CanonicalizeHost(const std::string& host, 212 std::string CanonicalizeHost(const std::string& host,
209 url::CanonHostInfo* host_info) { 213 url::CanonHostInfo* host_info) {
210 // Try to canonicalize the host. 214 // Try to canonicalize the host.
211 const url::Component raw_host_component(0, static_cast<int>(host.length())); 215 const url::Component raw_host_component(0, static_cast<int>(host.length()));
212 std::string canon_host; 216 std::string canon_host;
213 url::StdStringCanonOutput canon_host_output(&canon_host); 217 url::StdStringCanonOutput canon_host_output(&canon_host);
214 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component, 218 url::CanonicalizeHostVerbose(
215 &canon_host_output, host_info); 219 host.c_str(), raw_host_component, &canon_host_output, host_info);
216 220
217 if (host_info->out_host.is_nonempty() && 221 if (host_info->out_host.is_nonempty() &&
218 host_info->family != url::CanonHostInfo::BROKEN) { 222 host_info->family != url::CanonHostInfo::BROKEN) {
219 // Success! Assert that there's no extra garbage. 223 // Success! Assert that there's no extra garbage.
220 canon_host_output.Complete(); 224 canon_host_output.Complete();
221 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length())); 225 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
222 } else { 226 } else {
223 // Empty host, or canonicalization failed. We'll return empty. 227 // Empty host, or canonicalization failed. We'll return empty.
224 canon_host.clear(); 228 canon_host.clear();
225 } 229 }
226 230
227 return canon_host; 231 return canon_host;
228 } 232 }
229 233
230 std::string GetDirectoryListingHeader(const base::string16& title) { 234 std::string GetDirectoryListingHeader(const base::string16& title) {
231 static const base::StringPiece header( 235 static const base::StringPiece header(
232 NetModule::GetResource(IDR_DIR_HEADER_HTML)); 236 NetModule::GetResource(IDR_DIR_HEADER_HTML));
233 // This can be null in unit tests. 237 // This can be null in unit tests.
234 DLOG_IF(WARNING, header.empty()) << 238 DLOG_IF(WARNING, header.empty())
235 "Missing resource: directory listing header"; 239 << "Missing resource: directory listing header";
236 240
237 std::string result; 241 std::string result;
238 if (!header.empty()) 242 if (!header.empty())
239 result.assign(header.data(), header.size()); 243 result.assign(header.data(), header.size());
240 244
241 result.append("<script>start("); 245 result.append("<script>start(");
242 base::EscapeJSONString(title, true, &result); 246 base::EscapeJSONString(title, true, &result);
243 result.append(");</script>\n"); 247 result.append(");</script>\n");
244 248
245 return result; 249 return result;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 return false; 305 return false;
302 } 306 }
303 } 307 }
304 return true; 308 return true;
305 } 309 }
306 310
307 bool IsPortAllowedByFtp(int port) { 311 bool IsPortAllowedByFtp(int port) {
308 int array_size = arraysize(kAllowedFtpPorts); 312 int array_size = arraysize(kAllowedFtpPorts);
309 for (int i = 0; i < array_size; i++) { 313 for (int i = 0; i < array_size; i++) {
310 if (kAllowedFtpPorts[i] == port) { 314 if (kAllowedFtpPorts[i] == port) {
311 return true; 315 return true;
312 } 316 }
313 } 317 }
314 // Port not explicitly allowed by FTP, so return the default restrictions. 318 // Port not explicitly allowed by FTP, so return the default restrictions.
315 return IsPortAllowedByDefault(port); 319 return IsPortAllowedByDefault(port);
316 } 320 }
317 321
318 bool IsPortAllowedByOverride(int port) { 322 bool IsPortAllowedByOverride(int port) {
319 if (g_explicitly_allowed_ports.Get().empty()) 323 if (g_explicitly_allowed_ports.Get().empty())
320 return false; 324 return false;
321 325
(...skipping 22 matching lines...) Expand all
344 // When using url, we use char*. 348 // When using url, we use char*.
345 const char* auth_begin = &(*host_and_port_begin); 349 const char* auth_begin = &(*host_and_port_begin);
346 int auth_len = host_and_port_end - host_and_port_begin; 350 int auth_len = host_and_port_end - host_and_port_begin;
347 351
348 url::Component auth_component(0, auth_len); 352 url::Component auth_component(0, auth_len);
349 url::Component username_component; 353 url::Component username_component;
350 url::Component password_component; 354 url::Component password_component;
351 url::Component hostname_component; 355 url::Component hostname_component;
352 url::Component port_component; 356 url::Component port_component;
353 357
354 url::ParseAuthority(auth_begin, auth_component, &username_component, 358 url::ParseAuthority(auth_begin,
355 &password_component, &hostname_component, &port_component); 359 auth_component,
360 &username_component,
361 &password_component,
362 &hostname_component,
363 &port_component);
356 364
357 // There shouldn't be a username/password. 365 // There shouldn't be a username/password.
358 if (username_component.is_valid() || password_component.is_valid()) 366 if (username_component.is_valid() || password_component.is_valid())
359 return false; 367 return false;
360 368
361 if (!hostname_component.is_nonempty()) 369 if (!hostname_component.is_nonempty())
362 return false; // Failed parsing. 370 return false; // Failed parsing.
363 371
364 int parsed_port_number = -1; 372 int parsed_port_number = -1;
365 if (port_component.is_nonempty()) { 373 if (port_component.is_nonempty()) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 bool ParseHostAndPort(const std::string& host_and_port, 409 bool ParseHostAndPort(const std::string& host_and_port,
402 std::string* host, 410 std::string* host,
403 int* port) { 411 int* port) {
404 return ParseHostAndPort( 412 return ParseHostAndPort(
405 host_and_port.begin(), host_and_port.end(), host, port); 413 host_and_port.begin(), host_and_port.end(), host, port);
406 } 414 }
407 415
408 std::string GetHostAndPort(const GURL& url) { 416 std::string GetHostAndPort(const GURL& url) {
409 // For IPv6 literals, GURL::host() already includes the brackets so it is 417 // For IPv6 literals, GURL::host() already includes the brackets so it is
410 // safe to just append a colon. 418 // safe to just append a colon.
411 return base::StringPrintf("%s:%d", url.host().c_str(), 419 return base::StringPrintf(
412 url.EffectiveIntPort()); 420 "%s:%d", url.host().c_str(), url.EffectiveIntPort());
413 } 421 }
414 422
415 std::string GetHostAndOptionalPort(const GURL& url) { 423 std::string GetHostAndOptionalPort(const GURL& url) {
416 // For IPv6 literals, GURL::host() already includes the brackets 424 // For IPv6 literals, GURL::host() already includes the brackets
417 // so it is safe to just append a colon. 425 // so it is safe to just append a colon.
418 if (url.has_port()) 426 if (url.has_port())
419 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); 427 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
420 return url.host(); 428 return url.host();
421 } 429 }
422 430
423 bool IsHostnameNonUnique(const std::string& hostname) { 431 bool IsHostnameNonUnique(const std::string& hostname) {
424 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address. 432 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
425 const std::string host_or_ip = hostname.find(':') != std::string::npos ? 433 const std::string host_or_ip =
426 "[" + hostname + "]" : hostname; 434 hostname.find(':') != std::string::npos ? "[" + hostname + "]" : hostname;
427 url::CanonHostInfo host_info; 435 url::CanonHostInfo host_info;
428 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info); 436 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
429 437
430 // If canonicalization fails, then the input is truly malformed. However, 438 // If canonicalization fails, then the input is truly malformed. However,
431 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique. 439 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
432 if (canonical_name.empty()) 440 if (canonical_name.empty())
433 return false; 441 return false;
434 442
435 // If |hostname| is an IP address, check to see if it's in an IANA-reserved 443 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
436 // range. 444 // range.
437 if (host_info.IsIPAddress()) { 445 if (host_info.IsIPAddress()) {
438 IPAddressNumber host_addr; 446 IPAddressNumber host_addr;
439 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin, 447 if (!ParseIPLiteralToNumber(
440 host_info.out_host.len), 448 hostname.substr(host_info.out_host.begin, host_info.out_host.len),
441 &host_addr)) { 449 &host_addr)) {
442 return false; 450 return false;
443 } 451 }
444 switch (host_info.family) { 452 switch (host_info.family) {
445 case url::CanonHostInfo::IPV4: 453 case url::CanonHostInfo::IPV4:
446 case url::CanonHostInfo::IPV6: 454 case url::CanonHostInfo::IPV6:
447 return IsIPAddressReserved(host_addr); 455 return IsIPAddressReserved(host_addr);
448 case url::CanonHostInfo::NEUTRAL: 456 case url::CanonHostInfo::NEUTRAL:
449 case url::CanonHostInfo::BROKEN: 457 case url::CanonHostInfo::BROKEN:
450 return false; 458 return false;
451 } 459 }
(...skipping 17 matching lines...) Expand all
469 // Don't compare IPv4 and IPv6 addresses (they have different range 477 // Don't compare IPv4 and IPv6 addresses (they have different range
470 // reservations). Keep separate reservation arrays for each IP type, and 478 // reservations). Keep separate reservation arrays for each IP type, and
471 // consolidate adjacent reserved ranges within a reservation array when 479 // consolidate adjacent reserved ranges within a reservation array when
472 // possible. 480 // possible.
473 // Sources for info: 481 // Sources for info:
474 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml 482 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
475 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml 483 // 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: 484 // 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. 485 // 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) { 486 bool IsIPAddressReserved(const IPAddressNumber& host_addr) {
479 static const unsigned char kReservedIPv4[][5] = { 487 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 }, 488 {10, 0, 0, 0, 8},
481 { 169,254,0,0,16 }, { 172,16,0,0,12 }, { 192,0,2,0,24 }, 489 {100, 64, 0, 0, 10},
482 { 192,88,99,0,24 }, { 192,168,0,0,16 }, { 198,18,0,0,15 }, 490 {127, 0, 0, 0, 8},
483 { 198,51,100,0,24 }, { 203,0,113,0,24 }, { 224,0,0,0,3 } 491 {169, 254, 0, 0, 16},
484 }; 492 {172, 16, 0, 0, 12},
493 {192, 0, 2, 0, 24},
494 {192, 88, 99, 0, 24},
495 {192, 168, 0, 0, 16},
496 {198, 18, 0, 0, 15},
497 {198, 51, 100, 0, 24},
498 {203, 0, 113, 0, 24},
499 {224, 0, 0, 0, 3}};
Ryan Sleevi 2014/10/11 02:04:47 Another weird formatting
jkarlin 2014/10/16 00:19:32 Issued: https://code.google.com/p/chromium/issues/
jkarlin 2014/11/11 19:01:44 Feedback on the bug was that this one really could
485 static const unsigned char kReservedIPv6[][17] = { 500 static const unsigned char kReservedIPv6[][17] = {
486 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8 }, 501 {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 }, 502 {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 }, 503 {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 }, 504 {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 }, 505 {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 }, 506 {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 }, 507 {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 }, 508 {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 }, 509 {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 }, 510 {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 }, 511 {0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
497 }; 512 };
498 size_t array_size = 0; 513 size_t array_size = 0;
499 const unsigned char* array = NULL; 514 const unsigned char* array = NULL;
500 switch (host_addr.size()) { 515 switch (host_addr.size()) {
501 case kIPv4AddressSize: 516 case kIPv4AddressSize:
502 array_size = arraysize(kReservedIPv4); 517 array_size = arraysize(kReservedIPv4);
503 array = kReservedIPv4[0]; 518 array = kReservedIPv4[0];
504 break; 519 break;
505 case kIPv6AddressSize: 520 case kIPv6AddressSize:
506 array_size = arraysize(kReservedIPv6); 521 array_size = arraysize(kReservedIPv6);
507 array = kReservedIPv6[0]; 522 array = kReservedIPv6[0];
508 break; 523 break;
509 } 524 }
510 if (!array) 525 if (!array)
511 return false; 526 return false;
512 size_t width = host_addr.size() + 1; 527 size_t width = host_addr.size() + 1;
513 for (size_t i = 0; i < array_size; ++i, array += width) { 528 for (size_t i = 0; i < array_size; ++i, array += width) {
514 if (IPNumberPrefixCheck(host_addr, array, array[width-1])) 529 if (IPNumberPrefixCheck(host_addr, array, array[width - 1]))
515 return true; 530 return true;
516 } 531 }
517 return false; 532 return false;
518 } 533 }
519 534
520 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other) 535 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other)
521 : addr_len(other.addr_len), 536 : addr_len(other.addr_len),
522 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) { 537 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {
523 memcpy(addr, other.addr, addr_len); 538 memcpy(addr, other.addr, addr_len);
524 } 539 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 *address_len = kIPv6AddressSize; 571 *address_len = kIPv6AddressSize;
557 if (port) 572 if (port)
558 *port = base::NetToHost16(addr->sin6_port); 573 *port = base::NetToHost16(addr->sin6_port);
559 return true; 574 return true;
560 } 575 }
561 576
562 #if defined(OS_WIN) 577 #if defined(OS_WIN)
563 if (sock_addr->sa_family == AF_BTH) { 578 if (sock_addr->sa_family == AF_BTH) {
564 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) 579 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
565 return false; 580 return false;
566 const SOCKADDR_BTH* addr = 581 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); 582 *address = reinterpret_cast<const uint8*>(&addr->btAddr);
569 *address_len = kBluetoothAddressSize; 583 *address_len = kBluetoothAddressSize;
570 if (port) 584 if (port)
571 *port = addr->port; 585 *port = addr->port;
572 return true; 586 return true;
573 } 587 }
574 #endif 588 #endif
575 589
576 return false; // Unrecognized |sa_family|. 590 return false; // Unrecognized |sa_family|.
577 } 591 }
578 592
579 std::string IPAddressToString(const uint8* address, 593 std::string IPAddressToString(const uint8* address, size_t address_len) {
580 size_t address_len) {
581 std::string str; 594 std::string str;
582 url::StdStringCanonOutput output(&str); 595 url::StdStringCanonOutput output(&str);
583 596
584 if (address_len == kIPv4AddressSize) { 597 if (address_len == kIPv4AddressSize) {
585 url::AppendIPv4Address(address, &output); 598 url::AppendIPv4Address(address, &output);
586 } else if (address_len == kIPv6AddressSize) { 599 } else if (address_len == kIPv6AddressSize) {
587 url::AppendIPv6Address(address, &output); 600 url::AppendIPv6Address(address, &output);
588 } else { 601 } else {
589 CHECK(false) << "Invalid IP address with length: " << address_len; 602 CHECK(false) << "Invalid IP address with length: " << address_len;
590 } 603 }
(...skipping 11 matching lines...) Expand all
602 // Need to bracket IPv6 addresses since they contain colons. 615 // Need to bracket IPv6 addresses since they contain colons.
603 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); 616 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
604 } 617 }
605 return base::StringPrintf("%s:%d", address_str.c_str(), port); 618 return base::StringPrintf("%s:%d", address_str.c_str(), port);
606 } 619 }
607 620
608 std::string NetAddressToString(const struct sockaddr* sa, 621 std::string NetAddressToString(const struct sockaddr* sa,
609 socklen_t sock_addr_len) { 622 socklen_t sock_addr_len) {
610 const uint8* address; 623 const uint8* address;
611 size_t address_len; 624 size_t address_len;
612 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 625 if (!GetIPAddressFromSockAddr(
613 &address_len, NULL)) { 626 sa, sock_addr_len, &address, &address_len, NULL)) {
614 NOTREACHED(); 627 NOTREACHED();
615 return std::string(); 628 return std::string();
616 } 629 }
617 return IPAddressToString(address, address_len); 630 return IPAddressToString(address, address_len);
618 } 631 }
619 632
620 std::string NetAddressToStringWithPort(const struct sockaddr* sa, 633 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
621 socklen_t sock_addr_len) { 634 socklen_t sock_addr_len) {
622 const uint8* address; 635 const uint8* address;
623 size_t address_len; 636 size_t address_len;
624 uint16 port; 637 uint16 port;
625 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 638 if (!GetIPAddressFromSockAddr(
626 &address_len, &port)) { 639 sa, sock_addr_len, &address, &address_len, &port)) {
627 NOTREACHED(); 640 NOTREACHED();
628 return std::string(); 641 return std::string();
629 } 642 }
630 return IPAddressToStringWithPort(address, address_len, port); 643 return IPAddressToStringWithPort(address, address_len, port);
631 } 644 }
632 645
633 std::string IPAddressToString(const IPAddressNumber& addr) { 646 std::string IPAddressToString(const IPAddressNumber& addr) {
634 return IPAddressToString(&addr.front(), addr.size()); 647 return IPAddressToString(&addr.front(), addr.size());
635 } 648 }
636 649
637 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, 650 std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
638 uint16 port) { 651 uint16 port) {
639 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 652 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
640 } 653 }
641 654
642 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 655 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
643 return std::string(reinterpret_cast<const char *>(&addr.front()), 656 return std::string(reinterpret_cast<const char*>(&addr.front()), addr.size());
644 addr.size());
645 } 657 }
646 658
647 std::string GetHostName() { 659 std::string GetHostName() {
648 #if defined(OS_NACL) 660 #if defined(OS_NACL)
649 NOTIMPLEMENTED(); 661 NOTIMPLEMENTED();
650 return std::string(); 662 return std::string();
651 #else // defined(OS_NACL) 663 #else // defined(OS_NACL)
652 #if defined(OS_WIN) 664 #if defined(OS_WIN)
653 EnsureWinsockInit(); 665 EnsureWinsockInit();
654 #endif 666 #endif
(...skipping 18 matching lines...) Expand all
673 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags); 685 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags);
674 } 686 }
675 687
676 std::string GetHostOrSpecFromURL(const GURL& url) { 688 std::string GetHostOrSpecFromURL(const GURL& url) {
677 return url.has_host() ? TrimEndingDot(url.host()) : url.spec(); 689 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
678 } 690 }
679 691
680 bool CanStripTrailingSlash(const GURL& url) { 692 bool CanStripTrailingSlash(const GURL& url) {
681 // Omit the path only for standard, non-file URLs with nothing but "/" after 693 // Omit the path only for standard, non-file URLs with nothing but "/" after
682 // the hostname. 694 // the hostname.
683 return url.IsStandard() && !url.SchemeIsFile() && 695 return url.IsStandard() && !url.SchemeIsFile() && !url.SchemeIsFileSystem() &&
684 !url.SchemeIsFileSystem() && !url.has_query() && !url.has_ref() 696 !url.has_query() && !url.has_ref() && url.path() == "/";
685 && url.path() == "/";
686 } 697 }
687 698
688 GURL SimplifyUrlForRequest(const GURL& url) { 699 GURL SimplifyUrlForRequest(const GURL& url) {
689 DCHECK(url.is_valid()); 700 DCHECK(url.is_valid());
690 GURL::Replacements replacements; 701 GURL::Replacements replacements;
691 replacements.ClearUsername(); 702 replacements.ClearUsername();
692 replacements.ClearPassword(); 703 replacements.ClearPassword();
693 replacements.ClearRef(); 704 replacements.ClearRef();
694 return url.ReplaceComponents(replacements); 705 return url.ReplaceComponents(replacements);
695 } 706 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 return false; 758 return false;
748 #elif defined(OS_POSIX) 759 #elif defined(OS_POSIX)
749 struct ifaddrs* interface_addr = NULL; 760 struct ifaddrs* interface_addr = NULL;
750 int rv = getifaddrs(&interface_addr); 761 int rv = getifaddrs(&interface_addr);
751 if (rv != 0) { 762 if (rv != 0) {
752 DVLOG(1) << "getifaddrs() failed with errno = " << errno; 763 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
753 return false; 764 return false;
754 } 765 }
755 766
756 bool result = true; 767 bool result = true;
757 for (struct ifaddrs* interface = interface_addr; 768 for (struct ifaddrs* interface = interface_addr; interface != NULL;
758 interface != NULL;
759 interface = interface->ifa_next) { 769 interface = interface->ifa_next) {
760 if (!(IFF_UP & interface->ifa_flags)) 770 if (!(IFF_UP & interface->ifa_flags))
761 continue; 771 continue;
762 if (IFF_LOOPBACK & interface->ifa_flags) 772 if (IFF_LOOPBACK & interface->ifa_flags)
763 continue; 773 continue;
764 const struct sockaddr* addr = interface->ifa_addr; 774 const struct sockaddr* addr = interface->ifa_addr;
765 if (!addr) 775 if (!addr)
766 continue; 776 continue;
767 if (addr->sa_family == AF_INET6) { 777 if (addr->sa_family == AF_INET6) {
768 // Safe cast since this is AF_INET6. 778 // Safe cast since this is AF_INET6.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 IPAddressNumber* ip_number) { 850 IPAddressNumber* ip_number) {
841 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains 851 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains
842 // a colon however, it must be an IPv6 address. 852 // a colon however, it must be an IPv6 address.
843 if (ip_literal.find(':') != std::string::npos) { 853 if (ip_literal.find(':') != std::string::npos) {
844 // GURL expects IPv6 hostnames to be surrounded with brackets. 854 // GURL expects IPv6 hostnames to be surrounded with brackets.
845 std::string host_brackets = "[" + ip_literal + "]"; 855 std::string host_brackets = "[" + ip_literal + "]";
846 url::Component host_comp(0, host_brackets.size()); 856 url::Component host_comp(0, host_brackets.size());
847 857
848 // Try parsing the hostname as an IPv6 literal. 858 // Try parsing the hostname as an IPv6 literal.
849 ip_number->resize(16); // 128 bits. 859 ip_number->resize(16); // 128 bits.
850 return url::IPv6AddressToNumber(host_brackets.data(), host_comp, 860 return url::IPv6AddressToNumber(
851 &(*ip_number)[0]); 861 host_brackets.data(), host_comp, &(*ip_number)[0]);
852 } 862 }
853 863
854 // Otherwise the string is an IPv4 address. 864 // Otherwise the string is an IPv4 address.
855 ip_number->resize(4); // 32 bits. 865 ip_number->resize(4); // 32 bits.
856 url::Component host_comp(0, ip_literal.size()); 866 url::Component host_comp(0, ip_literal.size());
857 int num_components; 867 int num_components;
858 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( 868 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
859 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components); 869 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components);
860 return family == url::CanonHostInfo::IPV4; 870 return family == url::CanonHostInfo::IPV4;
861 } 871 }
862 872
863 namespace { 873 namespace {
864 874
865 const unsigned char kIPv4MappedPrefix[] = 875 const unsigned char kIPv4MappedPrefix[] =
866 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }; 876 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF};
867 } 877 }
868 878
869 IPAddressNumber ConvertIPv4NumberToIPv6Number( 879 IPAddressNumber ConvertIPv4NumberToIPv6Number(
870 const IPAddressNumber& ipv4_number) { 880 const IPAddressNumber& ipv4_number) {
871 DCHECK(ipv4_number.size() == 4); 881 DCHECK(ipv4_number.size() == 4);
872 882
873 // IPv4-mapped addresses are formed by: 883 // IPv4-mapped addresses are formed by:
874 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>. 884 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>.
875 IPAddressNumber ipv6_number; 885 IPAddressNumber ipv6_number;
876 ipv6_number.reserve(16); 886 ipv6_number.reserve(16);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 DCHECK(ip_number.size() == 4 || ip_number.size() == 16); 943 DCHECK(ip_number.size() == 4 || ip_number.size() == 16);
934 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16); 944 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16);
935 945
936 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8); 946 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8);
937 947
938 // In case we have an IPv6 / IPv4 mismatch, convert the IPv4 addresses to 948 // In case we have an IPv6 / IPv4 mismatch, convert the IPv4 addresses to
939 // IPv6 addresses in order to do the comparison. 949 // IPv6 addresses in order to do the comparison.
940 if (ip_number.size() != ip_prefix.size()) { 950 if (ip_number.size() != ip_prefix.size()) {
941 if (ip_number.size() == 4) { 951 if (ip_number.size() == 4) {
942 return IPNumberMatchesPrefix(ConvertIPv4NumberToIPv6Number(ip_number), 952 return IPNumberMatchesPrefix(ConvertIPv4NumberToIPv6Number(ip_number),
943 ip_prefix, prefix_length_in_bits); 953 ip_prefix,
954 prefix_length_in_bits);
944 } 955 }
945 return IPNumberMatchesPrefix(ip_number, 956 return IPNumberMatchesPrefix(ip_number,
946 ConvertIPv4NumberToIPv6Number(ip_prefix), 957 ConvertIPv4NumberToIPv6Number(ip_prefix),
947 96 + prefix_length_in_bits); 958 96 + prefix_length_in_bits);
948 } 959 }
949 960
950 return IPNumberPrefixCheck(ip_number, &ip_prefix[0], prefix_length_in_bits); 961 return IPNumberPrefixCheck(ip_number, &ip_prefix[0], prefix_length_in_bits);
951 } 962 }
952 963
953 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 964 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
(...skipping 15 matching lines...) Expand all
969 } 980 }
970 981
971 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 982 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
972 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 983 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
973 if (!port_field) 984 if (!port_field)
974 return -1; 985 return -1;
975 return base::NetToHost16(*port_field); 986 return base::NetToHost16(*port_field);
976 } 987 }
977 988
978 bool IsLocalhost(const std::string& host) { 989 bool IsLocalhost(const std::string& host) {
979 if (host == "localhost" || 990 if (host == "localhost" || host == "localhost.localdomain" ||
980 host == "localhost.localdomain" || 991 host == "localhost6" || host == "localhost6.localdomain6")
981 host == "localhost6" ||
982 host == "localhost6.localdomain6")
983 return true; 992 return true;
984 993
985 IPAddressNumber ip_number; 994 IPAddressNumber ip_number;
986 if (ParseIPLiteralToNumber(host, &ip_number)) { 995 if (ParseIPLiteralToNumber(host, &ip_number)) {
987 size_t size = ip_number.size(); 996 size_t size = ip_number.size();
988 switch (size) { 997 switch (size) {
989 case kIPv4AddressSize: { 998 case kIPv4AddressSize: {
990 IPAddressNumber localhost_prefix; 999 IPAddressNumber localhost_prefix;
991 localhost_prefix.push_back(127); 1000 localhost_prefix.push_back(127);
992 for (int i = 0; i < 3; ++i) { 1001 for (int i = 0; i < 3; ++i) {
(...skipping 10 matching lines...) Expand all
1003 1012
1004 default: 1013 default:
1005 NOTREACHED(); 1014 NOTREACHED();
1006 } 1015 }
1007 } 1016 }
1008 1017
1009 return false; 1018 return false;
1010 } 1019 }
1011 1020
1012 NetworkInterface::NetworkInterface() 1021 NetworkInterface::NetworkInterface()
1013 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), 1022 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), network_prefix(0) {
1014 network_prefix(0) {
1015 } 1023 }
1016 1024
1017 NetworkInterface::NetworkInterface(const std::string& name, 1025 NetworkInterface::NetworkInterface(const std::string& name,
1018 const std::string& friendly_name, 1026 const std::string& friendly_name,
1019 uint32 interface_index, 1027 uint32 interface_index,
1020 NetworkChangeNotifier::ConnectionType type, 1028 NetworkChangeNotifier::ConnectionType type,
1021 const IPAddressNumber& address, 1029 const IPAddressNumber& address,
1022 uint32 network_prefix, 1030 uint32 network_prefix,
1023 int ip_address_attributes) 1031 int ip_address_attributes)
1024 : name(name), 1032 : name(name),
(...skipping 27 matching lines...) Expand all
1052 1060
1053 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 1061 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
1054 IPAddressNumber all_ones(mask.size(), 0xFF); 1062 IPAddressNumber all_ones(mask.size(), 0xFF);
1055 return CommonPrefixLength(mask, all_ones); 1063 return CommonPrefixLength(mask, all_ones);
1056 } 1064 }
1057 1065
1058 ScopedWifiOptions::~ScopedWifiOptions() { 1066 ScopedWifiOptions::~ScopedWifiOptions() {
1059 } 1067 }
1060 1068
1061 } // namespace net 1069 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698