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

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

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
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 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <iterator> 10 #include <iterator>
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 #include "net/base/winsock_init.h" 60 #include "net/base/winsock_init.h"
61 #endif 61 #endif
62 62
63 namespace net { 63 namespace net {
64 64
65 namespace { 65 namespace {
66 66
67 // The general list of blocked ports. Will be blocked unless a specific 67 // The general list of blocked ports. Will be blocked unless a specific
68 // protocol overrides it. (Ex: ftp can use ports 20 and 21) 68 // protocol overrides it. (Ex: ftp can use ports 20 and 21)
69 static const int kRestrictedPorts[] = { 69 static const int kRestrictedPorts[] = {
70 1, // tcpmux 70 1, // tcpmux
71 7, // echo 71 7, // echo
72 9, // discard 72 9, // discard
73 11, // systat 73 11, // systat
74 13, // daytime 74 13, // daytime
75 15, // netstat 75 15, // netstat
76 17, // qotd 76 17, // qotd
77 19, // chargen 77 19, // chargen
78 20, // ftp data 78 20, // ftp data
79 21, // ftp access 79 21, // ftp access
80 22, // ssh 80 22, // ssh
81 23, // telnet 81 23, // telnet
82 25, // smtp 82 25, // smtp
83 37, // time 83 37, // time
84 42, // name 84 42, // name
85 43, // nicname 85 43, // nicname
86 53, // domain 86 53, // domain
87 77, // priv-rjs 87 77, // priv-rjs
88 79, // finger 88 79, // finger
89 87, // ttylink 89 87, // ttylink
90 95, // supdup 90 95, // supdup
91 101, // hostriame 91 101, // hostriame
92 102, // iso-tsap 92 102, // iso-tsap
93 103, // gppitnp 93 103, // gppitnp
94 104, // acr-nema 94 104, // acr-nema
95 109, // pop2 95 109, // pop2
96 110, // pop3 96 110, // pop3
97 111, // sunrpc 97 111, // sunrpc
98 113, // auth 98 113, // auth
99 115, // sftp 99 115, // sftp
100 117, // uucp-path 100 117, // uucp-path
101 119, // nntp 101 119, // nntp
102 123, // NTP 102 123, // NTP
103 135, // loc-srv /epmap 103 135, // loc-srv /epmap
104 139, // netbios 104 139, // netbios
105 143, // imap2 105 143, // imap2
106 179, // BGP 106 179, // BGP
107 389, // ldap 107 389, // ldap
108 465, // smtp+ssl 108 465, // smtp+ssl
109 512, // print / exec 109 512, // print / exec
110 513, // login 110 513, // login
111 514, // shell 111 514, // shell
112 515, // printer 112 515, // printer
113 526, // tempo 113 526, // tempo
114 530, // courier 114 530, // courier
115 531, // chat 115 531, // chat
116 532, // netnews 116 532, // netnews
117 540, // uucp 117 540, // uucp
118 556, // remotefs 118 556, // remotefs
119 563, // nntp+ssl 119 563, // nntp+ssl
120 587, // stmp? 120 587, // stmp?
121 601, // ?? 121 601, // ??
122 636, // ldap+ssl 122 636, // ldap+ssl
123 993, // ldap+ssl 123 993, // ldap+ssl
124 995, // pop3+ssl 124 995, // pop3+ssl
125 2049, // nfs 125 2049, // nfs
126 3659, // apple-sasl / PasswordServer 126 3659, // apple-sasl / PasswordServer
127 4045, // lockd 127 4045, // lockd
128 6000, // X11 128 6000, // X11
129 6665, // Alternate IRC [Apple addition] 129 6665, // Alternate IRC [Apple addition]
130 6666, // Alternate IRC [Apple addition] 130 6666, // Alternate IRC [Apple addition]
131 6667, // Standard IRC [Apple addition] 131 6667, // Standard IRC [Apple addition]
132 6668, // Alternate IRC [Apple addition] 132 6668, // Alternate IRC [Apple addition]
133 6669, // Alternate IRC [Apple addition] 133 6669, // Alternate IRC [Apple addition]
134 0xFFFF, // Used to block all invalid port numbers (see 134 0xFFFF, // Used to block all invalid port numbers (see
135 // third_party/WebKit/Source/platform/weborigin/KURL.cpp, 135 // third_party/WebKit/Source/platform/weborigin/KURL.cpp,
136 // KURL::port()) 136 // KURL::port())
137 }; 137 };
138 138
139 // FTP overrides the following restricted ports. 139 // FTP overrides the following restricted ports.
140 static const int kAllowedFtpPorts[] = { 140 static const int kAllowedFtpPorts[] = {
141 21, // ftp data 141 21, // ftp data
142 22, // ssh 142 22, // ssh
143 }; 143 };
144 144
145 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, 145 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number,
146 const unsigned char* ip_prefix, 146 const unsigned char* ip_prefix,
147 size_t prefix_length_in_bits) { 147 size_t prefix_length_in_bits) {
148 // Compare all the bytes that fall entirely within the prefix. 148 // Compare all the bytes that fall entirely within the prefix.
149 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; 149 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8;
150 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { 150 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) {
151 if (ip_number[i] != ip_prefix[i]) 151 if (ip_number[i] != ip_prefix[i])
152 return false; 152 return false;
(...skipping 29 matching lines...) Expand all
182 // ETag: "6d0b8-947-24f35ec0"\n 182 // ETag: "6d0b8-947-24f35ec0"\n
183 // Content-Length: 2375\n 183 // Content-Length: 2375\n
184 // Content-Type: text/html; charset=UTF-8\n 184 // Content-Type: text/html; charset=UTF-8\n
185 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n 185 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n
186 if (headers.empty()) 186 if (headers.empty())
187 return std::string(); 187 return std::string();
188 188
189 std::string match('\n' + name + ':'); 189 std::string match('\n' + name + ':');
190 190
191 std::string::const_iterator begin = 191 std::string::const_iterator begin =
192 std::search(headers.begin(), headers.end(), match.begin(), match.end(), 192 std::search(headers.begin(),
193 base::CaseInsensitiveCompareASCII<char>()); 193 headers.end(),
194 match.begin(),
195 match.end(),
196 base::CaseInsensitiveCompareASCII<char>());
194 197
195 if (begin == headers.end()) 198 if (begin == headers.end())
196 return std::string(); 199 return std::string();
197 200
198 begin += match.length(); 201 begin += match.length();
199 202
200 std::string ret; 203 std::string ret;
201 base::TrimWhitespace(std::string(begin, 204 base::TrimWhitespace(
202 std::find(begin, headers.end(), '\n')), 205 std::string(begin, std::find(begin, headers.end(), '\n')),
203 base::TRIM_ALL, &ret); 206 base::TRIM_ALL,
207 &ret);
204 return ret; 208 return ret;
205 } 209 }
206 210
207 std::string CanonicalizeHost(const std::string& host, 211 std::string CanonicalizeHost(const std::string& host,
208 url::CanonHostInfo* host_info) { 212 url::CanonHostInfo* host_info) {
209 // Try to canonicalize the host. 213 // Try to canonicalize the host.
210 const url::Component raw_host_component(0, static_cast<int>(host.length())); 214 const url::Component raw_host_component(0, static_cast<int>(host.length()));
211 std::string canon_host; 215 std::string canon_host;
212 url::StdStringCanonOutput canon_host_output(&canon_host); 216 url::StdStringCanonOutput canon_host_output(&canon_host);
213 url::CanonicalizeHostVerbose(host.c_str(), raw_host_component, 217 url::CanonicalizeHostVerbose(
214 &canon_host_output, host_info); 218 host.c_str(), raw_host_component, &canon_host_output, host_info);
215 219
216 if (host_info->out_host.is_nonempty() && 220 if (host_info->out_host.is_nonempty() &&
217 host_info->family != url::CanonHostInfo::BROKEN) { 221 host_info->family != url::CanonHostInfo::BROKEN) {
218 // Success! Assert that there's no extra garbage. 222 // Success! Assert that there's no extra garbage.
219 canon_host_output.Complete(); 223 canon_host_output.Complete();
220 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length())); 224 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
221 } else { 225 } else {
222 // Empty host, or canonicalization failed. We'll return empty. 226 // Empty host, or canonicalization failed. We'll return empty.
223 canon_host.clear(); 227 canon_host.clear();
224 } 228 }
225 229
226 return canon_host; 230 return canon_host;
227 } 231 }
228 232
229 std::string GetDirectoryListingHeader(const base::string16& title) { 233 std::string GetDirectoryListingHeader(const base::string16& title) {
230 static const base::StringPiece header( 234 static const base::StringPiece header(
231 NetModule::GetResource(IDR_DIR_HEADER_HTML)); 235 NetModule::GetResource(IDR_DIR_HEADER_HTML));
232 // This can be null in unit tests. 236 // This can be null in unit tests.
233 DLOG_IF(WARNING, header.empty()) << 237 DLOG_IF(WARNING, header.empty())
234 "Missing resource: directory listing header"; 238 << "Missing resource: directory listing header";
235 239
236 std::string result; 240 std::string result;
237 if (!header.empty()) 241 if (!header.empty())
238 result.assign(header.data(), header.size()); 242 result.assign(header.data(), header.size());
239 243
240 result.append("<script>start("); 244 result.append("<script>start(");
241 base::EscapeJSONString(title, true, &result); 245 base::EscapeJSONString(title, true, &result);
242 result.append(");</script>\n"); 246 result.append(");</script>\n");
243 247
244 return result; 248 return result;
(...skipping 30 matching lines...) Expand all
275 last_char_was_underscore = false; 279 last_char_was_underscore = false;
276 } else if (c == '_') { 280 } else if (c == '_') {
277 last_char_was_underscore = true; 281 last_char_was_underscore = true;
278 } else { 282 } else {
279 return false; 283 return false;
280 } 284 }
281 } 285 }
282 } 286 }
283 287
284 return most_recent_component_started_alphanumeric || 288 return most_recent_component_started_alphanumeric ||
285 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0])); 289 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0]));
286 } 290 }
287 291
288 base::string16 StripWWW(const base::string16& text) { 292 base::string16 StripWWW(const base::string16& text) {
289 const base::string16 www(base::ASCIIToUTF16("www.")); 293 const base::string16 www(base::ASCIIToUTF16("www."));
290 return StartsWith(text, www, true) ? text.substr(www.length()) : text; 294 return StartsWith(text, www, true) ? text.substr(www.length()) : text;
291 } 295 }
292 296
293 base::string16 StripWWWFromHost(const GURL& url) { 297 base::string16 StripWWWFromHost(const GURL& url) {
294 DCHECK(url.is_valid()); 298 DCHECK(url.is_valid());
295 return StripWWW(base::ASCIIToUTF16(url.host())); 299 return StripWWW(base::ASCIIToUTF16(url.host()));
296 } 300 }
297 301
298 bool IsPortAllowedByDefault(int port) { 302 bool IsPortAllowedByDefault(int port) {
299 int array_size = arraysize(kRestrictedPorts); 303 int array_size = arraysize(kRestrictedPorts);
300 for (int i = 0; i < array_size; i++) { 304 for (int i = 0; i < array_size; i++) {
301 if (kRestrictedPorts[i] == port) { 305 if (kRestrictedPorts[i] == port) {
302 return false; 306 return false;
303 } 307 }
304 } 308 }
305 return true; 309 return true;
306 } 310 }
307 311
308 bool IsPortAllowedByFtp(int port) { 312 bool IsPortAllowedByFtp(int port) {
309 int array_size = arraysize(kAllowedFtpPorts); 313 int array_size = arraysize(kAllowedFtpPorts);
310 for (int i = 0; i < array_size; i++) { 314 for (int i = 0; i < array_size; i++) {
311 if (kAllowedFtpPorts[i] == port) { 315 if (kAllowedFtpPorts[i] == port) {
312 return true; 316 return true;
313 } 317 }
314 } 318 }
315 // Port not explicitly allowed by FTP, so return the default restrictions. 319 // Port not explicitly allowed by FTP, so return the default restrictions.
316 return IsPortAllowedByDefault(port); 320 return IsPortAllowedByDefault(port);
317 } 321 }
318 322
319 bool IsPortAllowedByOverride(int port) { 323 bool IsPortAllowedByOverride(int port) {
320 if (g_explicitly_allowed_ports.Get().empty()) 324 if (g_explicitly_allowed_ports.Get().empty())
321 return false; 325 return false;
322 326
(...skipping 22 matching lines...) Expand all
345 // When using url, we use char*. 349 // When using url, we use char*.
346 const char* auth_begin = &(*host_and_port_begin); 350 const char* auth_begin = &(*host_and_port_begin);
347 int auth_len = host_and_port_end - host_and_port_begin; 351 int auth_len = host_and_port_end - host_and_port_begin;
348 352
349 url::Component auth_component(0, auth_len); 353 url::Component auth_component(0, auth_len);
350 url::Component username_component; 354 url::Component username_component;
351 url::Component password_component; 355 url::Component password_component;
352 url::Component hostname_component; 356 url::Component hostname_component;
353 url::Component port_component; 357 url::Component port_component;
354 358
355 url::ParseAuthority(auth_begin, auth_component, &username_component, 359 url::ParseAuthority(auth_begin,
356 &password_component, &hostname_component, &port_component); 360 auth_component,
361 &username_component,
362 &password_component,
363 &hostname_component,
364 &port_component);
357 365
358 // There shouldn't be a username/password. 366 // There shouldn't be a username/password.
359 if (username_component.is_valid() || password_component.is_valid()) 367 if (username_component.is_valid() || password_component.is_valid())
360 return false; 368 return false;
361 369
362 if (!hostname_component.is_nonempty()) 370 if (!hostname_component.is_nonempty())
363 return false; // Failed parsing. 371 return false; // Failed parsing.
364 372
365 int parsed_port_number = -1; 373 int parsed_port_number = -1;
366 if (port_component.is_nonempty()) { 374 if (port_component.is_nonempty()) {
(...skipping 18 matching lines...) Expand all
385 bool ParseHostAndPort(const std::string& host_and_port, 393 bool ParseHostAndPort(const std::string& host_and_port,
386 std::string* host, 394 std::string* host,
387 int* port) { 395 int* port) {
388 return ParseHostAndPort( 396 return ParseHostAndPort(
389 host_and_port.begin(), host_and_port.end(), host, port); 397 host_and_port.begin(), host_and_port.end(), host, port);
390 } 398 }
391 399
392 std::string GetHostAndPort(const GURL& url) { 400 std::string GetHostAndPort(const GURL& url) {
393 // For IPv6 literals, GURL::host() already includes the brackets so it is 401 // For IPv6 literals, GURL::host() already includes the brackets so it is
394 // safe to just append a colon. 402 // safe to just append a colon.
395 return base::StringPrintf("%s:%d", url.host().c_str(), 403 return base::StringPrintf(
396 url.EffectiveIntPort()); 404 "%s:%d", url.host().c_str(), url.EffectiveIntPort());
397 } 405 }
398 406
399 std::string GetHostAndOptionalPort(const GURL& url) { 407 std::string GetHostAndOptionalPort(const GURL& url) {
400 // For IPv6 literals, GURL::host() already includes the brackets 408 // For IPv6 literals, GURL::host() already includes the brackets
401 // so it is safe to just append a colon. 409 // so it is safe to just append a colon.
402 if (url.has_port()) 410 if (url.has_port())
403 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); 411 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str());
404 return url.host(); 412 return url.host();
405 } 413 }
406 414
407 bool IsHostnameNonUnique(const std::string& hostname) { 415 bool IsHostnameNonUnique(const std::string& hostname) {
408 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address. 416 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
409 const std::string host_or_ip = hostname.find(':') != std::string::npos ? 417 const std::string host_or_ip =
410 "[" + hostname + "]" : hostname; 418 hostname.find(':') != std::string::npos ? "[" + hostname + "]" : hostname;
411 url::CanonHostInfo host_info; 419 url::CanonHostInfo host_info;
412 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info); 420 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
413 421
414 // If canonicalization fails, then the input is truly malformed. However, 422 // If canonicalization fails, then the input is truly malformed. However,
415 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique. 423 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
416 if (canonical_name.empty()) 424 if (canonical_name.empty())
417 return false; 425 return false;
418 426
419 // If |hostname| is an IP address, check to see if it's in an IANA-reserved 427 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
420 // range. 428 // range.
421 if (host_info.IsIPAddress()) { 429 if (host_info.IsIPAddress()) {
422 IPAddressNumber host_addr; 430 IPAddressNumber host_addr;
423 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin, 431 if (!ParseIPLiteralToNumber(
424 host_info.out_host.len), 432 hostname.substr(host_info.out_host.begin, host_info.out_host.len),
425 &host_addr)) { 433 &host_addr)) {
426 return false; 434 return false;
427 } 435 }
428 switch (host_info.family) { 436 switch (host_info.family) {
429 case url::CanonHostInfo::IPV4: 437 case url::CanonHostInfo::IPV4:
430 case url::CanonHostInfo::IPV6: 438 case url::CanonHostInfo::IPV6:
431 return IsIPAddressReserved(host_addr); 439 return IsIPAddressReserved(host_addr);
432 case url::CanonHostInfo::NEUTRAL: 440 case url::CanonHostInfo::NEUTRAL:
433 case url::CanonHostInfo::BROKEN: 441 case url::CanonHostInfo::BROKEN:
434 return false; 442 return false;
435 } 443 }
(...skipping 17 matching lines...) Expand all
453 // Don't compare IPv4 and IPv6 addresses (they have different range 461 // Don't compare IPv4 and IPv6 addresses (they have different range
454 // reservations). Keep separate reservation arrays for each IP type, and 462 // reservations). Keep separate reservation arrays for each IP type, and
455 // consolidate adjacent reserved ranges within a reservation array when 463 // consolidate adjacent reserved ranges within a reservation array when
456 // possible. 464 // possible.
457 // Sources for info: 465 // Sources for info:
458 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml 466 // www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml
459 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml 467 // www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
460 // They're formatted here with the prefix as the last element. For example: 468 // They're formatted here with the prefix as the last element. For example:
461 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10. 469 // 10.0.0.0/8 becomes 10,0,0,0,8 and fec0::/10 becomes 0xfe,0xc0,0,0,0...,10.
462 bool IsIPAddressReserved(const IPAddressNumber& host_addr) { 470 bool IsIPAddressReserved(const IPAddressNumber& host_addr) {
463 static const unsigned char kReservedIPv4[][5] = { 471 static const unsigned char kReservedIPv4[][5] = {{0, 0, 0, 0, 8},
464 { 0,0,0,0,8 }, { 10,0,0,0,8 }, { 100,64,0,0,10 }, { 127,0,0,0,8 }, 472 {10, 0, 0, 0, 8},
465 { 169,254,0,0,16 }, { 172,16,0,0,12 }, { 192,0,2,0,24 }, 473 {100, 64, 0, 0, 10},
466 { 192,88,99,0,24 }, { 192,168,0,0,16 }, { 198,18,0,0,15 }, 474 {127, 0, 0, 0, 8},
467 { 198,51,100,0,24 }, { 203,0,113,0,24 }, { 224,0,0,0,3 } 475 {169, 254, 0, 0, 16},
468 }; 476 {172, 16, 0, 0, 12},
477 {192, 0, 2, 0, 24},
478 {192, 88, 99, 0, 24},
479 {192, 168, 0, 0, 16},
480 {198, 18, 0, 0, 15},
481 {198, 51, 100, 0, 24},
482 {203, 0, 113, 0, 24},
483 {224, 0, 0, 0, 3}};
469 static const unsigned char kReservedIPv6[][17] = { 484 static const unsigned char kReservedIPv6[][17] = {
470 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8 }, 485 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8},
471 { 0x40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, 486 {0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
472 { 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 }, 487 {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
473 { 0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3 }, 488 {0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
474 { 0xe0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 }, 489 {0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
475 { 0xf0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5 }, 490 {0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
476 { 0xf8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6 }, 491 {0xf8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
477 { 0xfc,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7 }, 492 {0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7},
478 { 0xfe,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9 }, 493 {0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9},
479 { 0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, 494 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
480 { 0xfe,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10 }, 495 {0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
481 }; 496 };
482 size_t array_size = 0; 497 size_t array_size = 0;
483 const unsigned char* array = NULL; 498 const unsigned char* array = NULL;
484 switch (host_addr.size()) { 499 switch (host_addr.size()) {
485 case kIPv4AddressSize: 500 case kIPv4AddressSize:
486 array_size = arraysize(kReservedIPv4); 501 array_size = arraysize(kReservedIPv4);
487 array = kReservedIPv4[0]; 502 array = kReservedIPv4[0];
488 break; 503 break;
489 case kIPv6AddressSize: 504 case kIPv6AddressSize:
490 array_size = arraysize(kReservedIPv6); 505 array_size = arraysize(kReservedIPv6);
491 array = kReservedIPv6[0]; 506 array = kReservedIPv6[0];
492 break; 507 break;
493 } 508 }
494 if (!array) 509 if (!array)
495 return false; 510 return false;
496 size_t width = host_addr.size() + 1; 511 size_t width = host_addr.size() + 1;
497 for (size_t i = 0; i < array_size; ++i, array += width) { 512 for (size_t i = 0; i < array_size; ++i, array += width) {
498 if (IPNumberPrefixCheck(host_addr, array, array[width-1])) 513 if (IPNumberPrefixCheck(host_addr, array, array[width - 1]))
499 return true; 514 return true;
500 } 515 }
501 return false; 516 return false;
502 } 517 }
503 518
504 // Extracts the address and port portions of a sockaddr. 519 // Extracts the address and port portions of a sockaddr.
505 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, 520 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
506 socklen_t sock_addr_len, 521 socklen_t sock_addr_len,
507 const uint8** address, 522 const uint8** address,
508 size_t* address_len, 523 size_t* address_len,
(...skipping 19 matching lines...) Expand all
528 *address_len = kIPv6AddressSize; 543 *address_len = kIPv6AddressSize;
529 if (port) 544 if (port)
530 *port = base::NetToHost16(addr->sin6_port); 545 *port = base::NetToHost16(addr->sin6_port);
531 return true; 546 return true;
532 } 547 }
533 548
534 #if defined(OS_WIN) 549 #if defined(OS_WIN)
535 if (sock_addr->sa_family == AF_BTH) { 550 if (sock_addr->sa_family == AF_BTH) {
536 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) 551 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH)))
537 return false; 552 return false;
538 const SOCKADDR_BTH* addr = 553 const SOCKADDR_BTH* addr = reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
539 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr);
540 *address = reinterpret_cast<const uint8*>(&addr->btAddr); 554 *address = reinterpret_cast<const uint8*>(&addr->btAddr);
541 *address_len = kBluetoothAddressSize; 555 *address_len = kBluetoothAddressSize;
542 if (port) 556 if (port)
543 *port = addr->port; 557 *port = addr->port;
544 return true; 558 return true;
545 } 559 }
546 #endif 560 #endif
547 561
548 return false; // Unrecognized |sa_family|. 562 return false; // Unrecognized |sa_family|.
549 } 563 }
550 564
551 std::string IPAddressToString(const uint8* address, 565 std::string IPAddressToString(const uint8* address, size_t address_len) {
552 size_t address_len) {
553 std::string str; 566 std::string str;
554 url::StdStringCanonOutput output(&str); 567 url::StdStringCanonOutput output(&str);
555 568
556 if (address_len == kIPv4AddressSize) { 569 if (address_len == kIPv4AddressSize) {
557 url::AppendIPv4Address(address, &output); 570 url::AppendIPv4Address(address, &output);
558 } else if (address_len == kIPv6AddressSize) { 571 } else if (address_len == kIPv6AddressSize) {
559 url::AppendIPv6Address(address, &output); 572 url::AppendIPv6Address(address, &output);
560 } else { 573 } else {
561 CHECK(false) << "Invalid IP address with length: " << address_len; 574 CHECK(false) << "Invalid IP address with length: " << address_len;
562 } 575 }
(...skipping 11 matching lines...) Expand all
574 // Need to bracket IPv6 addresses since they contain colons. 587 // Need to bracket IPv6 addresses since they contain colons.
575 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); 588 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
576 } 589 }
577 return base::StringPrintf("%s:%d", address_str.c_str(), port); 590 return base::StringPrintf("%s:%d", address_str.c_str(), port);
578 } 591 }
579 592
580 std::string NetAddressToString(const struct sockaddr* sa, 593 std::string NetAddressToString(const struct sockaddr* sa,
581 socklen_t sock_addr_len) { 594 socklen_t sock_addr_len) {
582 const uint8* address; 595 const uint8* address;
583 size_t address_len; 596 size_t address_len;
584 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 597 if (!GetIPAddressFromSockAddr(
585 &address_len, NULL)) { 598 sa, sock_addr_len, &address, &address_len, NULL)) {
586 NOTREACHED(); 599 NOTREACHED();
587 return std::string(); 600 return std::string();
588 } 601 }
589 return IPAddressToString(address, address_len); 602 return IPAddressToString(address, address_len);
590 } 603 }
591 604
592 std::string NetAddressToStringWithPort(const struct sockaddr* sa, 605 std::string NetAddressToStringWithPort(const struct sockaddr* sa,
593 socklen_t sock_addr_len) { 606 socklen_t sock_addr_len) {
594 const uint8* address; 607 const uint8* address;
595 size_t address_len; 608 size_t address_len;
596 uint16 port; 609 uint16 port;
597 if (!GetIPAddressFromSockAddr(sa, sock_addr_len, &address, 610 if (!GetIPAddressFromSockAddr(
598 &address_len, &port)) { 611 sa, sock_addr_len, &address, &address_len, &port)) {
599 NOTREACHED(); 612 NOTREACHED();
600 return std::string(); 613 return std::string();
601 } 614 }
602 return IPAddressToStringWithPort(address, address_len, port); 615 return IPAddressToStringWithPort(address, address_len, port);
603 } 616 }
604 617
605 std::string IPAddressToString(const IPAddressNumber& addr) { 618 std::string IPAddressToString(const IPAddressNumber& addr) {
606 return IPAddressToString(&addr.front(), addr.size()); 619 return IPAddressToString(&addr.front(), addr.size());
607 } 620 }
608 621
609 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, 622 std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
610 uint16 port) { 623 uint16 port) {
611 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 624 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
612 } 625 }
613 626
614 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 627 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
615 return std::string(reinterpret_cast<const char *>(&addr.front()), 628 return std::string(reinterpret_cast<const char*>(&addr.front()), addr.size());
616 addr.size());
617 } 629 }
618 630
619 std::string GetHostName() { 631 std::string GetHostName() {
620 #if defined(OS_NACL) 632 #if defined(OS_NACL)
621 NOTIMPLEMENTED(); 633 NOTIMPLEMENTED();
622 return std::string(); 634 return std::string();
623 #else // defined(OS_NACL) 635 #else // defined(OS_NACL)
624 #if defined(OS_WIN) 636 #if defined(OS_WIN)
625 EnsureWinsockInit(); 637 EnsureWinsockInit();
626 #endif 638 #endif
(...skipping 18 matching lines...) Expand all
645 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags); 657 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags);
646 } 658 }
647 659
648 std::string GetHostOrSpecFromURL(const GURL& url) { 660 std::string GetHostOrSpecFromURL(const GURL& url) {
649 return url.has_host() ? TrimEndingDot(url.host()) : url.spec(); 661 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
650 } 662 }
651 663
652 bool CanStripTrailingSlash(const GURL& url) { 664 bool CanStripTrailingSlash(const GURL& url) {
653 // Omit the path only for standard, non-file URLs with nothing but "/" after 665 // Omit the path only for standard, non-file URLs with nothing but "/" after
654 // the hostname. 666 // the hostname.
655 return url.IsStandard() && !url.SchemeIsFile() && 667 return url.IsStandard() && !url.SchemeIsFile() && !url.SchemeIsFileSystem() &&
656 !url.SchemeIsFileSystem() && !url.has_query() && !url.has_ref() 668 !url.has_query() && !url.has_ref() && url.path() == "/";
657 && url.path() == "/";
658 } 669 }
659 670
660 GURL SimplifyUrlForRequest(const GURL& url) { 671 GURL SimplifyUrlForRequest(const GURL& url) {
661 DCHECK(url.is_valid()); 672 DCHECK(url.is_valid());
662 GURL::Replacements replacements; 673 GURL::Replacements replacements;
663 replacements.ClearUsername(); 674 replacements.ClearUsername();
664 replacements.ClearPassword(); 675 replacements.ClearPassword();
665 replacements.ClearRef(); 676 replacements.ClearRef();
666 return url.ReplaceComponents(replacements); 677 return url.ReplaceComponents(replacements);
667 } 678 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 return false; 730 return false;
720 #elif defined(OS_POSIX) 731 #elif defined(OS_POSIX)
721 struct ifaddrs* interface_addr = NULL; 732 struct ifaddrs* interface_addr = NULL;
722 int rv = getifaddrs(&interface_addr); 733 int rv = getifaddrs(&interface_addr);
723 if (rv != 0) { 734 if (rv != 0) {
724 DVLOG(1) << "getifaddrs() failed with errno = " << errno; 735 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
725 return false; 736 return false;
726 } 737 }
727 738
728 bool result = true; 739 bool result = true;
729 for (struct ifaddrs* interface = interface_addr; 740 for (struct ifaddrs* interface = interface_addr; interface != NULL;
730 interface != NULL;
731 interface = interface->ifa_next) { 741 interface = interface->ifa_next) {
732 if (!(IFF_UP & interface->ifa_flags)) 742 if (!(IFF_UP & interface->ifa_flags))
733 continue; 743 continue;
734 if (IFF_LOOPBACK & interface->ifa_flags) 744 if (IFF_LOOPBACK & interface->ifa_flags)
735 continue; 745 continue;
736 const struct sockaddr* addr = interface->ifa_addr; 746 const struct sockaddr* addr = interface->ifa_addr;
737 if (!addr) 747 if (!addr)
738 continue; 748 continue;
739 if (addr->sa_family == AF_INET6) { 749 if (addr->sa_family == AF_INET6) {
740 // Safe cast since this is AF_INET6. 750 // Safe cast since this is AF_INET6.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 IPAddressNumber* ip_number) { 800 IPAddressNumber* ip_number) {
791 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains 801 // |ip_literal| could be either a IPv4 or an IPv6 literal. If it contains
792 // a colon however, it must be an IPv6 address. 802 // a colon however, it must be an IPv6 address.
793 if (ip_literal.find(':') != std::string::npos) { 803 if (ip_literal.find(':') != std::string::npos) {
794 // GURL expects IPv6 hostnames to be surrounded with brackets. 804 // GURL expects IPv6 hostnames to be surrounded with brackets.
795 std::string host_brackets = "[" + ip_literal + "]"; 805 std::string host_brackets = "[" + ip_literal + "]";
796 url::Component host_comp(0, host_brackets.size()); 806 url::Component host_comp(0, host_brackets.size());
797 807
798 // Try parsing the hostname as an IPv6 literal. 808 // Try parsing the hostname as an IPv6 literal.
799 ip_number->resize(16); // 128 bits. 809 ip_number->resize(16); // 128 bits.
800 return url::IPv6AddressToNumber(host_brackets.data(), host_comp, 810 return url::IPv6AddressToNumber(
801 &(*ip_number)[0]); 811 host_brackets.data(), host_comp, &(*ip_number)[0]);
802 } 812 }
803 813
804 // Otherwise the string is an IPv4 address. 814 // Otherwise the string is an IPv4 address.
805 ip_number->resize(4); // 32 bits. 815 ip_number->resize(4); // 32 bits.
806 url::Component host_comp(0, ip_literal.size()); 816 url::Component host_comp(0, ip_literal.size());
807 int num_components; 817 int num_components;
808 url::CanonHostInfo::Family family = url::IPv4AddressToNumber( 818 url::CanonHostInfo::Family family = url::IPv4AddressToNumber(
809 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components); 819 ip_literal.data(), host_comp, &(*ip_number)[0], &num_components);
810 return family == url::CanonHostInfo::IPV4; 820 return family == url::CanonHostInfo::IPV4;
811 } 821 }
812 822
813 namespace { 823 namespace {
814 824
815 const unsigned char kIPv4MappedPrefix[] = 825 const unsigned char kIPv4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
816 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }; 826 0, 0, 0, 0, 0xFF, 0xFF};
817 } 827 }
818 828
819 IPAddressNumber ConvertIPv4NumberToIPv6Number( 829 IPAddressNumber ConvertIPv4NumberToIPv6Number(
820 const IPAddressNumber& ipv4_number) { 830 const IPAddressNumber& ipv4_number) {
821 DCHECK(ipv4_number.size() == 4); 831 DCHECK(ipv4_number.size() == 4);
822 832
823 // IPv4-mapped addresses are formed by: 833 // IPv4-mapped addresses are formed by:
824 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>. 834 // <80 bits of zeros> + <16 bits of ones> + <32-bit IPv4 address>.
825 IPAddressNumber ipv6_number; 835 IPAddressNumber ipv6_number;
826 ipv6_number.reserve(16); 836 ipv6_number.reserve(16);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 DCHECK(ip_number.size() == 4 || ip_number.size() == 16); 893 DCHECK(ip_number.size() == 4 || ip_number.size() == 16);
884 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16); 894 DCHECK(ip_prefix.size() == 4 || ip_prefix.size() == 16);
885 895
886 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8); 896 DCHECK_LE(prefix_length_in_bits, ip_prefix.size() * 8);
887 897
888 // In case we have an IPv6 / IPv4 mismatch, convert the IPv4 addresses to 898 // In case we have an IPv6 / IPv4 mismatch, convert the IPv4 addresses to
889 // IPv6 addresses in order to do the comparison. 899 // IPv6 addresses in order to do the comparison.
890 if (ip_number.size() != ip_prefix.size()) { 900 if (ip_number.size() != ip_prefix.size()) {
891 if (ip_number.size() == 4) { 901 if (ip_number.size() == 4) {
892 return IPNumberMatchesPrefix(ConvertIPv4NumberToIPv6Number(ip_number), 902 return IPNumberMatchesPrefix(ConvertIPv4NumberToIPv6Number(ip_number),
893 ip_prefix, prefix_length_in_bits); 903 ip_prefix,
904 prefix_length_in_bits);
894 } 905 }
895 return IPNumberMatchesPrefix(ip_number, 906 return IPNumberMatchesPrefix(ip_number,
896 ConvertIPv4NumberToIPv6Number(ip_prefix), 907 ConvertIPv4NumberToIPv6Number(ip_prefix),
897 96 + prefix_length_in_bits); 908 96 + prefix_length_in_bits);
898 } 909 }
899 910
900 return IPNumberPrefixCheck(ip_number, &ip_prefix[0], prefix_length_in_bits); 911 return IPNumberPrefixCheck(ip_number, &ip_prefix[0], prefix_length_in_bits);
901 } 912 }
902 913
903 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 914 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
(...skipping 15 matching lines...) Expand all
919 } 930 }
920 931
921 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 932 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
922 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len); 933 const uint16* port_field = GetPortFieldFromSockaddr(address, address_len);
923 if (!port_field) 934 if (!port_field)
924 return -1; 935 return -1;
925 return base::NetToHost16(*port_field); 936 return base::NetToHost16(*port_field);
926 } 937 }
927 938
928 bool IsLocalhost(const std::string& host) { 939 bool IsLocalhost(const std::string& host) {
929 if (host == "localhost" || 940 if (host == "localhost" || host == "localhost.localdomain" ||
930 host == "localhost.localdomain" || 941 host == "localhost6" || host == "localhost6.localdomain6")
931 host == "localhost6" ||
932 host == "localhost6.localdomain6")
933 return true; 942 return true;
934 943
935 IPAddressNumber ip_number; 944 IPAddressNumber ip_number;
936 if (ParseIPLiteralToNumber(host, &ip_number)) { 945 if (ParseIPLiteralToNumber(host, &ip_number)) {
937 size_t size = ip_number.size(); 946 size_t size = ip_number.size();
938 switch (size) { 947 switch (size) {
939 case kIPv4AddressSize: { 948 case kIPv4AddressSize: {
940 IPAddressNumber localhost_prefix; 949 IPAddressNumber localhost_prefix;
941 localhost_prefix.push_back(127); 950 localhost_prefix.push_back(127);
942 for (int i = 0; i < 3; ++i) { 951 for (int i = 0; i < 3; ++i) {
(...skipping 10 matching lines...) Expand all
953 962
954 default: 963 default:
955 NOTREACHED(); 964 NOTREACHED();
956 } 965 }
957 } 966 }
958 967
959 return false; 968 return false;
960 } 969 }
961 970
962 NetworkInterface::NetworkInterface() 971 NetworkInterface::NetworkInterface()
963 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), 972 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), network_prefix(0) {
964 network_prefix(0) {
965 } 973 }
966 974
967 NetworkInterface::NetworkInterface(const std::string& name, 975 NetworkInterface::NetworkInterface(const std::string& name,
968 const std::string& friendly_name, 976 const std::string& friendly_name,
969 uint32 interface_index, 977 uint32 interface_index,
970 NetworkChangeNotifier::ConnectionType type, 978 NetworkChangeNotifier::ConnectionType type,
971 const IPAddressNumber& address, 979 const IPAddressNumber& address,
972 size_t network_prefix) 980 size_t network_prefix)
973 : name(name), 981 : name(name),
974 friendly_name(friendly_name), 982 friendly_name(friendly_name),
(...skipping 22 matching lines...) Expand all
997 } 1005 }
998 return a1.size() * CHAR_BIT; 1006 return a1.size() * CHAR_BIT;
999 } 1007 }
1000 1008
1001 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 1009 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
1002 IPAddressNumber all_ones(mask.size(), 0xFF); 1010 IPAddressNumber all_ones(mask.size(), 0xFF);
1003 return CommonPrefixLength(mask, all_ones); 1011 return CommonPrefixLength(mask, all_ones);
1004 } 1012 }
1005 1013
1006 } // namespace net 1014 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698