Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / base / net_util.h
blobc74dfd2ea48486b08e16be9fdb700b9d99f06bd0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_BASE_NET_UTIL_H_
6 #define NET_BASE_NET_UTIL_H_
8 #include "build/build_config.h"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #include <ws2tcpip.h>
13 #elif defined(OS_POSIX)
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #endif
18 #include <string>
19 #include <vector>
21 #include "base/basictypes.h"
22 #include "base/strings/string16.h"
23 #include "base/strings/utf_offset_string_conversions.h"
24 #include "net/base/address_family.h"
25 #include "net/base/escape.h"
26 #include "net/base/net_export.h"
27 #include "net/base/network_change_notifier.h"
29 class GURL;
31 namespace base {
32 class Time;
35 namespace url {
36 struct CanonHostInfo;
37 struct Parsed;
40 namespace net {
42 // Used by FormatUrl to specify handling of certain parts of the url.
43 typedef uint32 FormatUrlType;
44 typedef uint32 FormatUrlTypes;
46 // IPAddressNumber is used to represent an IP address's numeric value as an
47 // array of bytes, from most significant to least significant. This is the
48 // network byte ordering.
50 // IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
51 typedef std::vector<unsigned char> IPAddressNumber;
52 typedef std::vector<IPAddressNumber> IPAddressList;
54 static const size_t kIPv4AddressSize = 4;
55 static const size_t kIPv6AddressSize = 16;
56 #if defined(OS_WIN)
57 // Bluetooth address size. Windows Bluetooth is supported via winsock.
58 static const size_t kBluetoothAddressSize = 6;
59 #endif
61 // Nothing is ommitted.
62 NET_EXPORT extern const FormatUrlType kFormatUrlOmitNothing;
64 // If set, any username and password are removed.
65 NET_EXPORT extern const FormatUrlType kFormatUrlOmitUsernamePassword;
67 // If the scheme is 'http://', it's removed.
68 NET_EXPORT extern const FormatUrlType kFormatUrlOmitHTTP;
70 // Omits the path if it is just a slash and there is no query or ref. This is
71 // meaningful for non-file "standard" URLs.
72 NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname;
74 // Convenience for omitting all unecessary types.
75 NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll;
77 // Returns the number of explicitly allowed ports; for testing.
78 NET_EXPORT_PRIVATE extern size_t GetCountOfExplicitlyAllowedPorts();
80 // Splits an input of the form <host>[":"<port>] into its consitituent parts.
81 // Saves the result into |*host| and |*port|. If the input did not have
82 // the optional port, sets |*port| to -1.
83 // Returns true if the parsing was successful, false otherwise.
84 // The returned host is NOT canonicalized, and may be invalid. If <host> is
85 // an IPv6 literal address, the returned host includes the square brackets.
86 NET_EXPORT bool ParseHostAndPort(
87 std::string::const_iterator host_and_port_begin,
88 std::string::const_iterator host_and_port_end,
89 std::string* host,
90 int* port);
91 NET_EXPORT bool ParseHostAndPort(
92 const std::string& host_and_port,
93 std::string* host,
94 int* port);
96 // Returns a host:port string for the given URL.
97 NET_EXPORT std::string GetHostAndPort(const GURL& url);
99 // Returns a host[:port] string for the given URL, where the port is omitted
100 // if it is the default for the URL's scheme.
101 NET_EXPORT_PRIVATE std::string GetHostAndOptionalPort(const GURL& url);
103 // Returns true if |hostname| contains a non-registerable or non-assignable
104 // domain name (eg: a gTLD that has not been assigned by IANA) or an IP address
105 // that falls in an IANA-reserved range.
106 NET_EXPORT bool IsHostnameNonUnique(const std::string& hostname);
108 // Returns true if an IP address hostname is in a range reserved by the IANA.
109 // Works with both IPv4 and IPv6 addresses, and only compares against a given
110 // protocols's reserved ranges.
111 NET_EXPORT bool IsIPAddressReserved(const IPAddressNumber& address);
113 // Convenience struct for when you need a |struct sockaddr|.
114 struct SockaddrStorage {
115 SockaddrStorage() : addr_len(sizeof(addr_storage)),
116 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {}
117 SockaddrStorage(const SockaddrStorage& other);
118 void operator=(const SockaddrStorage& other);
120 struct sockaddr_storage addr_storage;
121 socklen_t addr_len;
122 struct sockaddr* const addr;
125 // Extracts the IP address and port portions of a sockaddr. |port| is optional,
126 // and will not be filled in if NULL.
127 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
128 socklen_t sock_addr_len,
129 const unsigned char** address,
130 size_t* address_len,
131 uint16* port);
133 // Returns the string representation of an IP address.
134 // For example: "192.168.0.1" or "::1".
135 NET_EXPORT std::string IPAddressToString(const uint8* address,
136 size_t address_len);
138 // Returns the string representation of an IP address along with its port.
139 // For example: "192.168.0.1:99" or "[::1]:80".
140 NET_EXPORT std::string IPAddressToStringWithPort(const uint8* address,
141 size_t address_len,
142 uint16 port);
144 // Same as IPAddressToString() but for a sockaddr. This output will not include
145 // the IPv6 scope ID.
146 NET_EXPORT std::string NetAddressToString(const struct sockaddr* sa,
147 socklen_t sock_addr_len);
149 // Same as IPAddressToStringWithPort() but for a sockaddr. This output will not
150 // include the IPv6 scope ID.
151 NET_EXPORT std::string NetAddressToStringWithPort(const struct sockaddr* sa,
152 socklen_t sock_addr_len);
154 // Same as IPAddressToString() but for an IPAddressNumber.
155 NET_EXPORT std::string IPAddressToString(const IPAddressNumber& addr);
157 // Same as IPAddressToStringWithPort() but for an IPAddressNumber.
158 NET_EXPORT std::string IPAddressToStringWithPort(
159 const IPAddressNumber& addr, uint16 port);
161 // Returns the address as a sequence of bytes in network-byte-order.
162 NET_EXPORT std::string IPAddressToPackedString(const IPAddressNumber& addr);
164 // Returns the hostname of the current system. Returns empty string on failure.
165 NET_EXPORT std::string GetHostName();
167 // Extracts the unescaped username/password from |url|, saving the results
168 // into |*username| and |*password|.
169 NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
170 base::string16* username,
171 base::string16* password);
173 // Returns either the host from |url|, or, if the host is empty, the full spec.
174 NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
176 // Return the value of the HTTP response header with name 'name'. 'headers'
177 // should be in the format that URLRequest::GetResponseHeaders() returns.
178 // Returns the empty string if the header is not found.
179 NET_EXPORT std::string GetSpecificHeader(const std::string& headers,
180 const std::string& name);
182 // Converts the given host name to unicode characters. This can be called for
183 // any host name, if the input is not IDN or is invalid in some way, we'll just
184 // return the ASCII source so it is still usable.
186 // The input should be the canonicalized ASCII host name from GURL. This
187 // function does NOT accept UTF-8!
189 // |languages| is a comma separated list of ISO 639 language codes. It
190 // is used to determine whether a hostname is 'comprehensible' to a user
191 // who understands languages listed. |host| will be converted to a
192 // human-readable form (Unicode) ONLY when each component of |host| is
193 // regarded as 'comprehensible'. Scipt-mixing is not allowed except that
194 // Latin letters in the ASCII range can be mixed with a limited set of
195 // script-language pairs (currently Han, Kana and Hangul for zh,ja and ko).
196 // When |languages| is empty, even that mixing is not allowed.
197 NET_EXPORT base::string16 IDNToUnicode(const std::string& host,
198 const std::string& languages);
200 // Canonicalizes |host| and returns it. Also fills |host_info| with
201 // IP address information. |host_info| must not be NULL.
202 NET_EXPORT std::string CanonicalizeHost(const std::string& host,
203 url::CanonHostInfo* host_info);
205 // Returns true if |host| is not an IP address and is compliant with a set of
206 // rules based on RFC 1738 and tweaked to be compatible with the real world.
207 // The rules are:
208 // * One or more components separated by '.'
209 // * Each component begins with an alphanumeric character or '-'
210 // * Each component contains only alphanumeric characters and '-' or '_'
211 // * Each component ends with an alphanumeric character or '-'
212 // * The last component begins with an alphanumeric character
213 // * Optional trailing dot after last component (means "treat as FQDN")
214 // If |desired_tld| is non-NULL, the host will only be considered invalid if
215 // appending it as a trailing component still results in an invalid host. This
216 // helps us avoid marking as "invalid" user attempts to open, say, "www.-9.com"
217 // by typing -, 9, <ctrl>+<enter>.
219 // NOTE: You should only pass in hosts that have been returned from
220 // CanonicalizeHost(), or you may not get accurate results.
221 NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host,
222 const std::string& desired_tld);
224 // Call these functions to get the html snippet for a directory listing.
225 // The return values of both functions are in UTF-8.
226 NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);
228 // Given the name of a file in a directory (ftp or local) and
229 // other information (is_dir, size, modification time), it returns
230 // the html snippet to add the entry for the file to the directory listing.
231 // Currently, it's a script tag containing a call to a Javascript function
232 // |addRow|.
234 // |name| is the file name to be displayed. |raw_bytes| will be used
235 // as the actual target of the link (so for example, ftp links should use
236 // server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
237 // will be used.
239 // Both |name| and |raw_bytes| are escaped internally.
240 NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
241 const std::string& raw_bytes,
242 bool is_dir, int64 size,
243 base::Time modified);
245 // If text starts with "www." it is removed, otherwise text is returned
246 // unmodified.
247 NET_EXPORT base::string16 StripWWW(const base::string16& text);
249 // Runs |url|'s host through StripWWW(). |url| must be valid.
250 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url);
252 // Checks |port| against a list of ports which are restricted by default.
253 // Returns true if |port| is allowed, false if it is restricted.
254 NET_EXPORT bool IsPortAllowedByDefault(int port);
256 // Checks |port| against a list of ports which are restricted by the FTP
257 // protocol. Returns true if |port| is allowed, false if it is restricted.
258 NET_EXPORT_PRIVATE bool IsPortAllowedByFtp(int port);
260 // Check if banned |port| has been overriden by an entry in
261 // |explicitly_allowed_ports_|.
262 NET_EXPORT_PRIVATE bool IsPortAllowedByOverride(int port);
264 // Set socket to non-blocking mode
265 NET_EXPORT int SetNonBlocking(int fd);
267 // Formats the host in |url| and appends it to |output|. The host formatter
268 // takes the same accept languages component as ElideURL().
269 NET_EXPORT void AppendFormattedHost(const GURL& url,
270 const std::string& languages,
271 base::string16* output);
273 // Creates a string representation of |url|. The IDN host name may be in Unicode
274 // if |languages| accepts the Unicode representation. |format_type| is a bitmask
275 // of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean
276 // the URL for human readability. You will generally want |UnescapeRule::SPACES|
277 // for display to the user if you can handle spaces, or |UnescapeRule::NORMAL|
278 // if not. If the path part and the query part seem to be encoded in %-encoded
279 // UTF-8, decodes %-encoding and UTF-8.
281 // The last three parameters may be NULL.
283 // |new_parsed| will be set to the parsing parameters of the resultant URL.
285 // |prefix_end| will be the length before the hostname of the resultant URL.
287 // |offset[s]_for_adjustment| specifies one or more offsets into the original
288 // URL, representing insertion or selection points between characters: if the
289 // input is "http://foo.com/", offset 0 is before the entire URL, offset 7 is
290 // between the scheme and the host, and offset 15 is after the end of the URL.
291 // Valid input offsets range from 0 to the length of the input URL string. On
292 // exit, each offset will have been modified to reflect any changes made to the
293 // output string. For example, if |url| is "http://a:b@c.com/",
294 // |omit_username_password| is true, and an offset is 12 (pointing between 'c'
295 // and '.'), then on return the output string will be "http://c.com/" and the
296 // offset will be 8. If an offset cannot be successfully adjusted (e.g. because
297 // it points into the middle of a component that was entirely removed or into
298 // the middle of an encoding sequence), it will be set to base::string16::npos.
299 // For consistency, if an input offset points between the scheme and the
300 // username/password, and both are removed, on output this offset will be 0
301 // rather than npos; this means that offsets at the starts and ends of removed
302 // components are always transformed the same way regardless of what other
303 // components are adjacent.
304 NET_EXPORT base::string16 FormatUrl(const GURL& url,
305 const std::string& languages,
306 FormatUrlTypes format_types,
307 UnescapeRule::Type unescape_rules,
308 url::Parsed* new_parsed,
309 size_t* prefix_end,
310 size_t* offset_for_adjustment);
311 NET_EXPORT base::string16 FormatUrlWithOffsets(
312 const GURL& url,
313 const std::string& languages,
314 FormatUrlTypes format_types,
315 UnescapeRule::Type unescape_rules,
316 url::Parsed* new_parsed,
317 size_t* prefix_end,
318 std::vector<size_t>* offsets_for_adjustment);
319 // This function is like those above except it takes |adjustments| rather
320 // than |offset[s]_for_adjustment|. |adjustments| will be set to reflect all
321 // the transformations that happened to |url| to convert it into the returned
322 // value.
323 NET_EXPORT base::string16 FormatUrlWithAdjustments(
324 const GURL& url,
325 const std::string& languages,
326 FormatUrlTypes format_types,
327 UnescapeRule::Type unescape_rules,
328 url::Parsed* new_parsed,
329 size_t* prefix_end,
330 base::OffsetAdjuster::Adjustments* adjustments);
332 // This is a convenience function for FormatUrl() with
333 // format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical
334 // set of flags for "URLs to display to the user". You should be cautious about
335 // using this for URLs which will be parsed or sent to other applications.
336 inline base::string16 FormatUrl(const GURL& url, const std::string& languages) {
337 return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES,
338 NULL, NULL, NULL);
341 // Returns whether FormatUrl() would strip a trailing slash from |url|, given a
342 // format flag including kFormatUrlOmitTrailingSlashOnBareHostname.
343 NET_EXPORT bool CanStripTrailingSlash(const GURL& url);
345 // Strip the portions of |url| that aren't core to the network request.
346 // - user name / password
347 // - reference section
348 NET_EXPORT_PRIVATE GURL SimplifyUrlForRequest(const GURL& url);
350 NET_EXPORT void SetExplicitlyAllowedPorts(const std::string& allowed_ports);
352 class NET_EXPORT ScopedPortException {
353 public:
354 explicit ScopedPortException(int port);
355 ~ScopedPortException();
357 private:
358 int port_;
360 DISALLOW_COPY_AND_ASSIGN(ScopedPortException);
363 // Returns true if it can determine that only loopback addresses are configured.
364 // i.e. if only 127.0.0.1 and ::1 are routable.
365 // Also returns false if it cannot determine this.
366 bool HaveOnlyLoopbackAddresses();
368 // Returns AddressFamily of the address.
369 NET_EXPORT_PRIVATE AddressFamily GetAddressFamily(
370 const IPAddressNumber& address);
372 // Maps the given AddressFamily to either AF_INET, AF_INET6 or AF_UNSPEC.
373 NET_EXPORT_PRIVATE int ConvertAddressFamily(AddressFamily address_family);
375 // Parses a URL-safe IP literal (see RFC 3986, Sec 3.2.2) to its numeric value.
376 // Returns true on success, and fills |ip_number| with the numeric value
377 NET_EXPORT bool ParseURLHostnameToNumber(const std::string& hostname,
378 IPAddressNumber* ip_number);
380 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
381 // Returns true on success and fills |ip_number| with the numeric value.
382 NET_EXPORT bool ParseIPLiteralToNumber(const std::string& ip_literal,
383 IPAddressNumber* ip_number);
385 // Converts an IPv4 address to an IPv4-mapped IPv6 address.
386 // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1.
387 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4NumberToIPv6Number(
388 const IPAddressNumber& ipv4_number);
390 // Returns true iff |address| is an IPv4-mapped IPv6 address.
391 NET_EXPORT_PRIVATE bool IsIPv4Mapped(const IPAddressNumber& address);
393 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called
394 // on IPv4-mapped IPv6 addresses.
395 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4MappedToIPv4(
396 const IPAddressNumber& address);
398 // Parses an IP block specifier from CIDR notation to an
399 // (IP address, prefix length) pair. Returns true on success and fills
400 // |*ip_number| with the numeric value of the IP address and sets
401 // |*prefix_length_in_bits| with the length of the prefix.
403 // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
405 // 10.10.3.1/20
406 // a:b:c::/46
407 // ::1/128
408 NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal,
409 IPAddressNumber* ip_number,
410 size_t* prefix_length_in_bits);
412 // Compares an IP address to see if it falls within the specified IP block.
413 // Returns true if it does, false otherwise.
415 // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
416 // IP address whose |prefix_length_in_bits| most significant bits match
417 // |ip_prefix| will be matched.
419 // In cases when an IPv4 address is being compared to an IPv6 address prefix
420 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped
421 // (IPv6) addresses.
422 NET_EXPORT_PRIVATE bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
423 const IPAddressNumber& ip_prefix,
424 size_t prefix_length_in_bits);
426 // Retuns the port field of the |sockaddr|.
427 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
428 socklen_t address_len);
429 // Returns the value of port in |sockaddr| (in host byte ordering).
430 NET_EXPORT_PRIVATE int GetPortFromSockaddr(const struct sockaddr* address,
431 socklen_t address_len);
433 // Returns true if |host| is one of the names (e.g. "localhost") or IP
434 // addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback.
436 // Note that this function does not check for IP addresses other than
437 // the above, although other IP addresses may point to the local
438 // machine.
439 NET_EXPORT_PRIVATE bool IsLocalhost(const std::string& host);
441 // struct that is used by GetNetworkList() to represent a network
442 // interface.
443 struct NET_EXPORT NetworkInterface {
444 NetworkInterface();
445 NetworkInterface(const std::string& name,
446 const std::string& friendly_name,
447 uint32 interface_index,
448 NetworkChangeNotifier::ConnectionType type,
449 const IPAddressNumber& address,
450 size_t network_prefix);
451 ~NetworkInterface();
453 std::string name;
454 std::string friendly_name; // Same as |name| on non-Windows.
455 uint32 interface_index; // Always 0 on Android.
456 NetworkChangeNotifier::ConnectionType type;
457 IPAddressNumber address;
458 size_t network_prefix;
461 typedef std::vector<NetworkInterface> NetworkInterfaceList;
463 // Policy settings to include/exclude network interfaces.
464 enum HostAddressSelectionPolicy {
465 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES = 0x0,
466 EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES = 0x1,
467 // Include temp address only when interface has both permanent and
468 // temp addresses.
469 INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE = 0x2,
472 // Returns list of network interfaces except loopback interface. If an
473 // interface has more than one address, a separate entry is added to
474 // the list for each address.
475 // Can be called only on a thread that allows IO.
476 NET_EXPORT bool GetNetworkList(NetworkInterfaceList* networks,
477 int policy);
479 // General category of the IEEE 802.11 (wifi) physical layer operating mode.
480 enum WifiPHYLayerProtocol {
481 // No wifi support or no associated AP.
482 WIFI_PHY_LAYER_PROTOCOL_NONE,
483 // An obsolete modes introduced by the original 802.11, e.g. IR, FHSS.
484 WIFI_PHY_LAYER_PROTOCOL_ANCIENT,
485 // 802.11a, OFDM-based rates.
486 WIFI_PHY_LAYER_PROTOCOL_A,
487 // 802.11b, DSSS or HR DSSS.
488 WIFI_PHY_LAYER_PROTOCOL_B,
489 // 802.11g, same rates as 802.11a but compatible with 802.11b.
490 WIFI_PHY_LAYER_PROTOCOL_G,
491 // 802.11n, HT rates.
492 WIFI_PHY_LAYER_PROTOCOL_N,
493 // Unclassified mode or failure to identify.
494 WIFI_PHY_LAYER_PROTOCOL_UNKNOWN
497 // Characterize the PHY mode of the currently associated access point.
498 // Currently only available on OS_WIN.
499 NET_EXPORT WifiPHYLayerProtocol GetWifiPHYLayerProtocol();
501 // Returns number of matching initial bits between the addresses |a1| and |a2|.
502 unsigned CommonPrefixLength(const IPAddressNumber& a1,
503 const IPAddressNumber& a2);
505 // Computes the number of leading 1-bits in |mask|.
506 unsigned MaskPrefixLength(const IPAddressNumber& mask);
508 // Differentiated Services Code Point.
509 // See http://tools.ietf.org/html/rfc2474 for details.
510 enum DiffServCodePoint {
511 DSCP_NO_CHANGE = -1,
512 DSCP_FIRST = DSCP_NO_CHANGE,
513 DSCP_DEFAULT = 0, // Same as DSCP_CS0
514 DSCP_CS0 = 0, // The default
515 DSCP_CS1 = 8, // Bulk/background traffic
516 DSCP_AF11 = 10,
517 DSCP_AF12 = 12,
518 DSCP_AF13 = 14,
519 DSCP_CS2 = 16,
520 DSCP_AF21 = 18,
521 DSCP_AF22 = 20,
522 DSCP_AF23 = 22,
523 DSCP_CS3 = 24,
524 DSCP_AF31 = 26,
525 DSCP_AF32 = 28,
526 DSCP_AF33 = 30,
527 DSCP_CS4 = 32,
528 DSCP_AF41 = 34, // Video
529 DSCP_AF42 = 36, // Video
530 DSCP_AF43 = 38, // Video
531 DSCP_CS5 = 40, // Video
532 DSCP_EF = 46, // Voice
533 DSCP_CS6 = 48, // Voice
534 DSCP_CS7 = 56, // Control messages
535 DSCP_LAST = DSCP_CS7
538 } // namespace net
540 #endif // NET_BASE_NET_UTIL_H_