Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / net / base / net_util.h
blob597e52e2f71efb7ca43d30687c7ee9b2d21f9d55
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"
28 // TODO(eroman): Remove this header and require consumers to include it
29 // directly.
30 #include "net/base/network_interfaces.h"
32 class GURL;
34 namespace base {
35 class Time;
38 namespace url {
39 struct CanonHostInfo;
40 struct Parsed;
43 namespace net {
45 class AddressList;
47 // This is a "forward declaration" to avoid including ip_address_number.h
48 // Keep this in sync.
49 typedef std::vector<unsigned char> IPAddressNumber;
51 #if defined(OS_WIN)
52 // Bluetooth address size. Windows Bluetooth is supported via winsock.
53 static const size_t kBluetoothAddressSize = 6;
54 #endif
56 // Splits an input of the form <host>[":"<port>] into its consitituent parts.
57 // Saves the result into |*host| and |*port|. If the input did not have
58 // the optional port, sets |*port| to -1.
59 // Returns true if the parsing was successful, false otherwise.
60 // The returned host is NOT canonicalized, and may be invalid.
62 // IPv6 literals must be specified in a bracketed form, for instance:
63 // [::1]:90 and [::1]
65 // The resultant |*host| in both cases will be "::1" (not bracketed).
66 NET_EXPORT bool ParseHostAndPort(
67 std::string::const_iterator host_and_port_begin,
68 std::string::const_iterator host_and_port_end,
69 std::string* host,
70 int* port);
71 NET_EXPORT bool ParseHostAndPort(
72 const std::string& host_and_port,
73 std::string* host,
74 int* port);
76 // Returns a host:port string for the given URL.
77 NET_EXPORT std::string GetHostAndPort(const GURL& url);
79 // Returns a host[:port] string for the given URL, where the port is omitted
80 // if it is the default for the URL's scheme.
81 NET_EXPORT_PRIVATE std::string GetHostAndOptionalPort(const GURL& url);
83 // Returns true if |hostname| contains a non-registerable or non-assignable
84 // domain name (eg: a gTLD that has not been assigned by IANA) or an IP address
85 // that falls in an IANA-reserved range.
86 NET_EXPORT bool IsHostnameNonUnique(const std::string& hostname);
88 // Convenience struct for when you need a |struct sockaddr|.
89 struct SockaddrStorage {
90 SockaddrStorage() : addr_len(sizeof(addr_storage)),
91 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {}
92 SockaddrStorage(const SockaddrStorage& other);
93 void operator=(const SockaddrStorage& other);
95 struct sockaddr_storage addr_storage;
96 socklen_t addr_len;
97 struct sockaddr* const addr;
100 // Extracts the IP address and port portions of a sockaddr. |port| is optional,
101 // and will not be filled in if NULL.
102 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
103 socklen_t sock_addr_len,
104 const unsigned char** address,
105 size_t* address_len,
106 uint16_t* port);
108 // Same as IPAddressToString() but for a sockaddr. This output will not include
109 // the IPv6 scope ID.
110 NET_EXPORT std::string NetAddressToString(const struct sockaddr* sa,
111 socklen_t sock_addr_len);
113 // Same as IPAddressToStringWithPort() but for a sockaddr. This output will not
114 // include the IPv6 scope ID.
115 NET_EXPORT std::string NetAddressToStringWithPort(const struct sockaddr* sa,
116 socklen_t sock_addr_len);
118 // Returns the hostname of the current system. Returns empty string on failure.
119 NET_EXPORT std::string GetHostName();
121 // Extracts the unescaped username/password from |url|, saving the results
122 // into |*username| and |*password|.
123 NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
124 base::string16* username,
125 base::string16* password);
127 // Returns either the host from |url|, or, if the host is empty, the full spec.
128 NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
130 // Canonicalizes |host| and returns it. Also fills |host_info| with
131 // IP address information. |host_info| must not be NULL.
132 NET_EXPORT std::string CanonicalizeHost(const std::string& host,
133 url::CanonHostInfo* host_info);
135 // Returns true if |host| is not an IP address and is compliant with a set of
136 // rules based on RFC 1738 and tweaked to be compatible with the real world.
137 // The rules are:
138 // * One or more components separated by '.'
139 // * Each component contains only alphanumeric characters and '-' or '_'
140 // * The last component begins with an alphanumeric character
141 // * Optional trailing dot after last component (means "treat as FQDN")
143 // NOTE: You should only pass in hosts that have been returned from
144 // CanonicalizeHost(), or you may not get accurate results.
145 NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host);
147 // Call these functions to get the html snippet for a directory listing.
148 // The return values of both functions are in UTF-8.
149 NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);
151 // Given the name of a file in a directory (ftp or local) and
152 // other information (is_dir, size, modification time), it returns
153 // the html snippet to add the entry for the file to the directory listing.
154 // Currently, it's a script tag containing a call to a Javascript function
155 // |addRow|.
157 // |name| is the file name to be displayed. |raw_bytes| will be used
158 // as the actual target of the link (so for example, ftp links should use
159 // server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
160 // will be used.
162 // Both |name| and |raw_bytes| are escaped internally.
163 NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
164 const std::string& raw_bytes,
165 bool is_dir,
166 int64_t size,
167 base::Time modified);
169 // If text starts with "www." it is removed, otherwise text is returned
170 // unmodified.
171 NET_EXPORT base::string16 StripWWW(const base::string16& text);
173 // Runs |url|'s host through StripWWW(). |url| must be valid.
174 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url);
176 // Set socket to non-blocking mode
177 NET_EXPORT int SetNonBlocking(int fd);
179 // Strip the portions of |url| that aren't core to the network request.
180 // - user name / password
181 // - reference section
182 NET_EXPORT_PRIVATE GURL SimplifyUrlForRequest(const GURL& url);
184 // Returns true if it can determine that only loopback addresses are configured.
185 // i.e. if only 127.0.0.1 and ::1 are routable.
186 // Also returns false if it cannot determine this.
187 bool HaveOnlyLoopbackAddresses();
189 // Returns AddressFamily of the address.
190 NET_EXPORT_PRIVATE AddressFamily GetAddressFamily(
191 const IPAddressNumber& address);
193 // Maps the given AddressFamily to either AF_INET, AF_INET6 or AF_UNSPEC.
194 NET_EXPORT_PRIVATE int ConvertAddressFamily(AddressFamily address_family);
196 // Retuns the port field of the |sockaddr|.
197 const uint16_t* GetPortFieldFromSockaddr(const struct sockaddr* address,
198 socklen_t address_len);
199 // Returns the value of port in |sockaddr| (in host byte ordering).
200 NET_EXPORT_PRIVATE int GetPortFromSockaddr(const struct sockaddr* address,
201 socklen_t address_len);
203 // Resolves a local hostname (such as "localhost" or "localhost6") into
204 // IP endpoints with the given port. Returns true if |host| is a local
205 // hostname and false otherwise. Special IPv6 names (e.g. "localhost6")
206 // will resolve to an IPv6 address only, whereas other names will
207 // resolve to both IPv4 and IPv6.
208 NET_EXPORT_PRIVATE bool ResolveLocalHostname(const std::string& host,
209 uint16_t port,
210 AddressList* address_list);
212 // Returns true if |host| is one of the local hostnames
213 // (e.g. "localhost") or IP addresses (IPv4 127.0.0.0/8 or IPv6 ::1).
215 // Note that this function does not check for IP addresses other than
216 // the above, although other IP addresses may point to the local
217 // machine.
218 NET_EXPORT_PRIVATE bool IsLocalhost(const std::string& host);
220 NET_EXPORT_PRIVATE bool IsLocalhostTLD(const std::string& host);
222 // Returns true if the url's host is a Google server. This should only be used
223 // for histograms and shouldn't be used to affect behavior.
224 NET_EXPORT_PRIVATE bool HasGoogleHost(const GURL& url);
226 // A subset of IP address attributes which are actionable by the
227 // application layer. Currently unimplemented for all hosts;
228 // IP_ADDRESS_ATTRIBUTE_NONE is always returned.
229 enum IPAddressAttributes {
230 IP_ADDRESS_ATTRIBUTE_NONE = 0,
232 // A temporary address is dynamic by nature and will not contain MAC
233 // address. Presence of MAC address in IPv6 addresses can be used to
234 // track an endpoint and cause privacy concern. Please refer to
235 // RFC4941.
236 IP_ADDRESS_ATTRIBUTE_TEMPORARY = 1 << 0,
238 // A temporary address could become deprecated once the preferred
239 // lifetime is reached. It is still valid but shouldn't be used to
240 // create new connections.
241 IP_ADDRESS_ATTRIBUTE_DEPRECATED = 1 << 1,
244 // Differentiated Services Code Point.
245 // See http://tools.ietf.org/html/rfc2474 for details.
246 enum DiffServCodePoint {
247 DSCP_NO_CHANGE = -1,
248 DSCP_FIRST = DSCP_NO_CHANGE,
249 DSCP_DEFAULT = 0, // Same as DSCP_CS0
250 DSCP_CS0 = 0, // The default
251 DSCP_CS1 = 8, // Bulk/background traffic
252 DSCP_AF11 = 10,
253 DSCP_AF12 = 12,
254 DSCP_AF13 = 14,
255 DSCP_CS2 = 16,
256 DSCP_AF21 = 18,
257 DSCP_AF22 = 20,
258 DSCP_AF23 = 22,
259 DSCP_CS3 = 24,
260 DSCP_AF31 = 26,
261 DSCP_AF32 = 28,
262 DSCP_AF33 = 30,
263 DSCP_CS4 = 32,
264 DSCP_AF41 = 34, // Video
265 DSCP_AF42 = 36, // Video
266 DSCP_AF43 = 38, // Video
267 DSCP_CS5 = 40, // Video
268 DSCP_EF = 46, // Voice
269 DSCP_CS6 = 48, // Voice
270 DSCP_CS7 = 56, // Control messages
271 DSCP_LAST = DSCP_CS7
274 } // namespace net
276 #endif // NET_BASE_NET_UTIL_H_