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