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

Side by Side Diff: net/base/network_interfaces.h

Issue 1154323007: Split network_interfaces.h off of net_utils.h. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ip_util
Patch Set: address matt's comments Created 5 years, 6 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
« no previous file with comments | « net/base/net_util_win.cc ('k') | net/base/network_interfaces.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef NET_BASE_NET_UTIL_H_ 5 #ifndef NET_BASE_NETWORK_INTERFACES_H_
6 #define NET_BASE_NET_UTIL_H_ 6 #define NET_BASE_NETWORK_INTERFACES_H_
7 7
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 9
10 #if defined(OS_WIN) 10 #if defined(OS_WIN)
11 #include <windows.h> 11 #include <windows.h>
12 #include <ws2tcpip.h> 12 #include <ws2tcpip.h>
13 #elif defined(OS_POSIX) 13 #elif defined(OS_POSIX)
14 #include <sys/types.h> 14 #include <sys/types.h>
15 #include <sys/socket.h> 15 #include <sys/socket.h>
16 #endif 16 #endif
(...skipping 16 matching lines...) Expand all
33 class Time; 33 class Time;
34 } 34 }
35 35
36 namespace url { 36 namespace url {
37 struct CanonHostInfo; 37 struct CanonHostInfo;
38 struct Parsed; 38 struct Parsed;
39 } 39 }
40 40
41 namespace net { 41 namespace net {
42 42
43 // Used by FormatUrl to specify handling of certain parts of the url.
44 typedef uint32 FormatUrlType;
45 typedef uint32 FormatUrlTypes;
46
47 #if defined(OS_WIN)
48 // Bluetooth address size. Windows Bluetooth is supported via winsock.
49 static const size_t kBluetoothAddressSize = 6;
50 #endif
51
52 // Nothing is ommitted.
53 NET_EXPORT extern const FormatUrlType kFormatUrlOmitNothing;
54
55 // If set, any username and password are removed.
56 NET_EXPORT extern const FormatUrlType kFormatUrlOmitUsernamePassword;
57
58 // If the scheme is 'http://', it's removed.
59 NET_EXPORT extern const FormatUrlType kFormatUrlOmitHTTP;
60
61 // Omits the path if it is just a slash and there is no query or ref. This is
62 // meaningful for non-file "standard" URLs.
63 NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname;
64
65 // Convenience for omitting all unecessary types.
66 NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll;
67
68 // Returns the number of explicitly allowed ports; for testing.
69 NET_EXPORT_PRIVATE extern size_t GetCountOfExplicitlyAllowedPorts();
70
71 // Splits an input of the form <host>[":"<port>] into its consitituent parts.
72 // Saves the result into |*host| and |*port|. If the input did not have
73 // the optional port, sets |*port| to -1.
74 // Returns true if the parsing was successful, false otherwise.
75 // The returned host is NOT canonicalized, and may be invalid.
76 //
77 // IPv6 literals must be specified in a bracketed form, for instance:
78 // [::1]:90 and [::1]
79 //
80 // The resultant |*host| in both cases will be "::1" (not bracketed).
81 NET_EXPORT bool ParseHostAndPort(
82 std::string::const_iterator host_and_port_begin,
83 std::string::const_iterator host_and_port_end,
84 std::string* host,
85 int* port);
86 NET_EXPORT bool ParseHostAndPort(
87 const std::string& host_and_port,
88 std::string* host,
89 int* port);
90
91 // Returns a host:port string for the given URL.
92 NET_EXPORT std::string GetHostAndPort(const GURL& url);
93
94 // Returns a host[:port] string for the given URL, where the port is omitted
95 // if it is the default for the URL's scheme.
96 NET_EXPORT_PRIVATE std::string GetHostAndOptionalPort(const GURL& url);
97
98 // Returns true if |hostname| contains a non-registerable or non-assignable
99 // domain name (eg: a gTLD that has not been assigned by IANA) or an IP address
100 // that falls in an IANA-reserved range.
101 NET_EXPORT bool IsHostnameNonUnique(const std::string& hostname);
102
103 // Convenience struct for when you need a |struct sockaddr|.
104 struct SockaddrStorage {
105 SockaddrStorage() : addr_len(sizeof(addr_storage)),
106 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {}
107 SockaddrStorage(const SockaddrStorage& other);
108 void operator=(const SockaddrStorage& other);
109
110 struct sockaddr_storage addr_storage;
111 socklen_t addr_len;
112 struct sockaddr* const addr;
113 };
114
115 // Extracts the IP address and port portions of a sockaddr. |port| is optional,
116 // and will not be filled in if NULL.
117 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
118 socklen_t sock_addr_len,
119 const unsigned char** address,
120 size_t* address_len,
121 uint16* port);
122
123 // Same as IPAddressToString() but for a sockaddr. This output will not include
124 // the IPv6 scope ID.
125 NET_EXPORT std::string NetAddressToString(const struct sockaddr* sa,
126 socklen_t sock_addr_len);
127
128 // Same as IPAddressToStringWithPort() but for a sockaddr. This output will not
129 // include the IPv6 scope ID.
130 NET_EXPORT std::string NetAddressToStringWithPort(const struct sockaddr* sa,
131 socklen_t sock_addr_len);
132
133 // Returns the hostname of the current system. Returns empty string on failure.
134 NET_EXPORT std::string GetHostName();
135
136 // Extracts the unescaped username/password from |url|, saving the results
137 // into |*username| and |*password|.
138 NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
139 base::string16* username,
140 base::string16* password);
141
142 // Returns either the host from |url|, or, if the host is empty, the full spec.
143 NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
144
145 // Return the value of the HTTP response header with name 'name'. 'headers'
146 // should be in the format that URLRequest::GetResponseHeaders() returns.
147 // Returns the empty string if the header is not found.
148 NET_EXPORT std::string GetSpecificHeader(const std::string& headers,
149 const std::string& name);
150
151 // Converts the given host name to unicode characters. This can be called for
152 // any host name, if the input is not IDN or is invalid in some way, we'll just
153 // return the ASCII source so it is still usable.
154 //
155 // The input should be the canonicalized ASCII host name from GURL. This
156 // function does NOT accept UTF-8!
157 //
158 // |languages| is a comma separated list of ISO 639 language codes. It
159 // is used to determine whether a hostname is 'comprehensible' to a user
160 // who understands languages listed. |host| will be converted to a
161 // human-readable form (Unicode) ONLY when each component of |host| is
162 // regarded as 'comprehensible'. Scipt-mixing is not allowed except that
163 // Latin letters in the ASCII range can be mixed with a limited set of
164 // script-language pairs (currently Han, Kana and Hangul for zh,ja and ko).
165 // When |languages| is empty, even that mixing is not allowed.
166 NET_EXPORT base::string16 IDNToUnicode(const std::string& host,
167 const std::string& languages);
168
169 // Canonicalizes |host| and returns it. Also fills |host_info| with
170 // IP address information. |host_info| must not be NULL.
171 NET_EXPORT std::string CanonicalizeHost(const std::string& host,
172 url::CanonHostInfo* host_info);
173
174 // Returns true if |host| is not an IP address and is compliant with a set of
175 // rules based on RFC 1738 and tweaked to be compatible with the real world.
176 // The rules are:
177 // * One or more components separated by '.'
178 // * Each component contains only alphanumeric characters and '-' or '_'
179 // * The last component begins with an alphanumeric character
180 // * Optional trailing dot after last component (means "treat as FQDN")
181 //
182 // NOTE: You should only pass in hosts that have been returned from
183 // CanonicalizeHost(), or you may not get accurate results.
184 NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host);
185
186 // Call these functions to get the html snippet for a directory listing.
187 // The return values of both functions are in UTF-8.
188 NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);
189
190 // Given the name of a file in a directory (ftp or local) and
191 // other information (is_dir, size, modification time), it returns
192 // the html snippet to add the entry for the file to the directory listing.
193 // Currently, it's a script tag containing a call to a Javascript function
194 // |addRow|.
195 //
196 // |name| is the file name to be displayed. |raw_bytes| will be used
197 // as the actual target of the link (so for example, ftp links should use
198 // server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
199 // will be used.
200 //
201 // Both |name| and |raw_bytes| are escaped internally.
202 NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
203 const std::string& raw_bytes,
204 bool is_dir, int64 size,
205 base::Time modified);
206
207 // If text starts with "www." it is removed, otherwise text is returned
208 // unmodified.
209 NET_EXPORT base::string16 StripWWW(const base::string16& text);
210
211 // Runs |url|'s host through StripWWW(). |url| must be valid.
212 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url);
213
214 // Checks if |port| is in the valid range (0 to 65535, though 0 is technically
215 // reserved). Should be used before casting a port to a uint16.
216 NET_EXPORT bool IsPortValid(int port);
217
218 // Checks |port| against a list of ports which are restricted by default.
219 // Returns true if |port| is allowed, false if it is restricted.
220 NET_EXPORT bool IsPortAllowedByDefault(int port);
221
222 // Checks |port| against a list of ports which are restricted by the FTP
223 // protocol. Returns true if |port| is allowed, false if it is restricted.
224 NET_EXPORT_PRIVATE bool IsPortAllowedByFtp(int port);
225
226 // Check if banned |port| has been overriden by an entry in
227 // |explicitly_allowed_ports_|.
228 NET_EXPORT_PRIVATE bool IsPortAllowedByOverride(int port);
229
230 // Set socket to non-blocking mode
231 NET_EXPORT int SetNonBlocking(int fd);
232
233 // Formats the host in |url| and appends it to |output|. The host formatter
234 // takes the same accept languages component as ElideURL().
235 NET_EXPORT void AppendFormattedHost(const GURL& url,
236 const std::string& languages,
237 base::string16* output);
238
239 // Creates a string representation of |url|. The IDN host name may be in Unicode
240 // if |languages| accepts the Unicode representation. |format_type| is a bitmask
241 // of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean
242 // the URL for human readability. You will generally want |UnescapeRule::SPACES|
243 // for display to the user if you can handle spaces, or |UnescapeRule::NORMAL|
244 // if not. If the path part and the query part seem to be encoded in %-encoded
245 // UTF-8, decodes %-encoding and UTF-8.
246 //
247 // The last three parameters may be NULL.
248 //
249 // |new_parsed| will be set to the parsing parameters of the resultant URL.
250 //
251 // |prefix_end| will be the length before the hostname of the resultant URL.
252 //
253 // |offset[s]_for_adjustment| specifies one or more offsets into the original
254 // URL, representing insertion or selection points between characters: if the
255 // input is "http://foo.com/", offset 0 is before the entire URL, offset 7 is
256 // between the scheme and the host, and offset 15 is after the end of the URL.
257 // Valid input offsets range from 0 to the length of the input URL string. On
258 // exit, each offset will have been modified to reflect any changes made to the
259 // output string. For example, if |url| is "http://a:b@c.com/",
260 // |omit_username_password| is true, and an offset is 12 (pointing between 'c'
261 // and '.'), then on return the output string will be "http://c.com/" and the
262 // offset will be 8. If an offset cannot be successfully adjusted (e.g. because
263 // it points into the middle of a component that was entirely removed or into
264 // the middle of an encoding sequence), it will be set to base::string16::npos.
265 // For consistency, if an input offset points between the scheme and the
266 // username/password, and both are removed, on output this offset will be 0
267 // rather than npos; this means that offsets at the starts and ends of removed
268 // components are always transformed the same way regardless of what other
269 // components are adjacent.
270 NET_EXPORT base::string16 FormatUrl(const GURL& url,
271 const std::string& languages,
272 FormatUrlTypes format_types,
273 UnescapeRule::Type unescape_rules,
274 url::Parsed* new_parsed,
275 size_t* prefix_end,
276 size_t* offset_for_adjustment);
277 NET_EXPORT base::string16 FormatUrlWithOffsets(
278 const GURL& url,
279 const std::string& languages,
280 FormatUrlTypes format_types,
281 UnescapeRule::Type unescape_rules,
282 url::Parsed* new_parsed,
283 size_t* prefix_end,
284 std::vector<size_t>* offsets_for_adjustment);
285 // This function is like those above except it takes |adjustments| rather
286 // than |offset[s]_for_adjustment|. |adjustments| will be set to reflect all
287 // the transformations that happened to |url| to convert it into the returned
288 // value.
289 NET_EXPORT base::string16 FormatUrlWithAdjustments(
290 const GURL& url,
291 const std::string& languages,
292 FormatUrlTypes format_types,
293 UnescapeRule::Type unescape_rules,
294 url::Parsed* new_parsed,
295 size_t* prefix_end,
296 base::OffsetAdjuster::Adjustments* adjustments);
297
298 // This is a convenience function for FormatUrl() with
299 // format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical
300 // set of flags for "URLs to display to the user". You should be cautious about
301 // using this for URLs which will be parsed or sent to other applications.
302 inline base::string16 FormatUrl(const GURL& url, const std::string& languages) {
303 return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES,
304 NULL, NULL, NULL);
305 }
306
307 // Returns whether FormatUrl() would strip a trailing slash from |url|, given a
308 // format flag including kFormatUrlOmitTrailingSlashOnBareHostname.
309 NET_EXPORT bool CanStripTrailingSlash(const GURL& url);
310
311 // Strip the portions of |url| that aren't core to the network request.
312 // - user name / password
313 // - reference section
314 NET_EXPORT_PRIVATE GURL SimplifyUrlForRequest(const GURL& url);
315
316 NET_EXPORT void SetExplicitlyAllowedPorts(const std::string& allowed_ports);
317
318 class NET_EXPORT ScopedPortException {
319 public:
320 explicit ScopedPortException(int port);
321 ~ScopedPortException();
322
323 private:
324 int port_;
325
326 DISALLOW_COPY_AND_ASSIGN(ScopedPortException);
327 };
328
329 // Returns true if it can determine that only loopback addresses are configured.
330 // i.e. if only 127.0.0.1 and ::1 are routable.
331 // Also returns false if it cannot determine this.
332 bool HaveOnlyLoopbackAddresses();
333
334 // Returns AddressFamily of the address.
335 NET_EXPORT_PRIVATE AddressFamily GetAddressFamily(
336 const IPAddressNumber& address);
337
338 // Maps the given AddressFamily to either AF_INET, AF_INET6 or AF_UNSPEC.
339 NET_EXPORT_PRIVATE int ConvertAddressFamily(AddressFamily address_family);
340
341 // Retuns the port field of the |sockaddr|.
342 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
343 socklen_t address_len);
344 // Returns the value of port in |sockaddr| (in host byte ordering).
345 NET_EXPORT_PRIVATE int GetPortFromSockaddr(const struct sockaddr* address,
346 socklen_t address_len);
347
348 // Returns true if |host| is one of the names (e.g. "localhost") or IP
349 // addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback.
350 //
351 // Note that this function does not check for IP addresses other than
352 // the above, although other IP addresses may point to the local
353 // machine.
354 NET_EXPORT_PRIVATE bool IsLocalhost(const std::string& host);
355
356 NET_EXPORT_PRIVATE bool IsLocalhostTLD(const std::string& host);
357
358 // Returns true if the url's host is a Google server. This should only be used
359 // for histograms and shouldn't be used to affect behavior.
360 NET_EXPORT_PRIVATE bool HasGoogleHost(const GURL& url);
361
362 // A subset of IP address attributes which are actionable by the
363 // application layer. Currently unimplemented for all hosts;
364 // IP_ADDRESS_ATTRIBUTE_NONE is always returned.
365 enum IPAddressAttributes {
366 IP_ADDRESS_ATTRIBUTE_NONE = 0,
367
368 // A temporary address is dynamic by nature and will not contain MAC
369 // address. Presence of MAC address in IPv6 addresses can be used to
370 // track an endpoint and cause privacy concern. Please refer to
371 // RFC4941.
372 IP_ADDRESS_ATTRIBUTE_TEMPORARY = 1 << 0,
373
374 // A temporary address could become deprecated once the preferred
375 // lifetime is reached. It is still valid but shouldn't be used to
376 // create new connections.
377 IP_ADDRESS_ATTRIBUTE_DEPRECATED = 1 << 1,
378 };
379
380 // struct that is used by GetNetworkList() to represent a network 43 // struct that is used by GetNetworkList() to represent a network
381 // interface. 44 // interface.
382 struct NET_EXPORT NetworkInterface { 45 struct NET_EXPORT NetworkInterface {
383 NetworkInterface(); 46 NetworkInterface();
384 NetworkInterface(const std::string& name, 47 NetworkInterface(const std::string& name,
385 const std::string& friendly_name, 48 const std::string& friendly_name,
386 uint32 interface_index, 49 uint32 interface_index,
387 NetworkChangeNotifier::ConnectionType type, 50 NetworkChangeNotifier::ConnectionType type,
388 const IPAddressNumber& address, 51 const IPAddressNumber& address,
389 uint32 prefix_length, 52 uint32 prefix_length,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 private: 119 private:
457 DISALLOW_COPY_AND_ASSIGN(ScopedWifiOptions); 120 DISALLOW_COPY_AND_ASSIGN(ScopedWifiOptions);
458 }; 121 };
459 122
460 // Set temporary options on all wifi interfaces. 123 // Set temporary options on all wifi interfaces.
461 // |options| is an ORed bitfield of WifiOptions. 124 // |options| is an ORed bitfield of WifiOptions.
462 // Options are automatically disabled when the scoped pointer 125 // Options are automatically disabled when the scoped pointer
463 // is freed. Currently only available on OS_WIN. 126 // is freed. Currently only available on OS_WIN.
464 NET_EXPORT scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options); 127 NET_EXPORT scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options);
465 128
466 // Differentiated Services Code Point.
467 // See http://tools.ietf.org/html/rfc2474 for details.
468 enum DiffServCodePoint {
469 DSCP_NO_CHANGE = -1,
470 DSCP_FIRST = DSCP_NO_CHANGE,
471 DSCP_DEFAULT = 0, // Same as DSCP_CS0
472 DSCP_CS0 = 0, // The default
473 DSCP_CS1 = 8, // Bulk/background traffic
474 DSCP_AF11 = 10,
475 DSCP_AF12 = 12,
476 DSCP_AF13 = 14,
477 DSCP_CS2 = 16,
478 DSCP_AF21 = 18,
479 DSCP_AF22 = 20,
480 DSCP_AF23 = 22,
481 DSCP_CS3 = 24,
482 DSCP_AF31 = 26,
483 DSCP_AF32 = 28,
484 DSCP_AF33 = 30,
485 DSCP_CS4 = 32,
486 DSCP_AF41 = 34, // Video
487 DSCP_AF42 = 36, // Video
488 DSCP_AF43 = 38, // Video
489 DSCP_CS5 = 40, // Video
490 DSCP_EF = 46, // Voice
491 DSCP_CS6 = 48, // Voice
492 DSCP_CS7 = 56, // Control messages
493 DSCP_LAST = DSCP_CS7
494 };
495
496 } // namespace net 129 } // namespace net
497 130
498 #endif // NET_BASE_NET_UTIL_H_ 131 #endif // NET_BASE_NETWORK_INTERFACES_H_
OLDNEW
« no previous file with comments | « net/base/net_util_win.cc ('k') | net/base/network_interfaces.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698