blob: 76e49f6ce5e145873f1ca6082090e80d9f6994da [file] [log] [blame]
[email protected]611cc902011-03-19 00:36:391// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]de2943352009-10-22 23:06:125#ifndef NET_BASE_NET_UTIL_H_
6#define NET_BASE_NET_UTIL_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit586acc5fe2008-07-26 22:42:528
[email protected]e537c352008-08-20 21:42:179#include "build/build_config.h"
10
[email protected]93e79012011-01-26 04:13:5911#if defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:5212#include <windows.h>
[email protected]93e79012011-01-26 04:13:5913#include <ws2tcpip.h>
14#elif defined(OS_POSIX)
15#include <sys/socket.h>
[email protected]e537c352008-08-20 21:42:1716#endif
initial.commit586acc5fe2008-07-26 22:42:5217
[email protected]611cc902011-03-19 00:36:3918#include <list>
[email protected]d16659f2009-05-18 16:47:3219#include <string>
[email protected]d95fa182009-09-09 17:01:1620#include <set>
[email protected]54392832010-06-08 23:25:0421#include <vector>
[email protected]d16659f2009-05-18 16:47:3222
initial.commit586acc5fe2008-07-26 22:42:5223#include "base/basictypes.h"
[email protected]5420bc1e2009-07-09 22:48:1624#include "base/string16.h"
[email protected]a23de8572009-06-03 02:16:3225#include "net/base/escape.h"
[email protected]172da1b2011-08-12 15:52:2626#include "net/base/net_export.h"
initial.commit586acc5fe2008-07-26 22:42:5227
[email protected]50d7d7282009-03-02 21:45:1828struct addrinfo;
[email protected]498c1a6b22008-11-24 23:37:0429class FilePath;
initial.commit586acc5fe2008-07-26 22:42:5230class GURL;
31
[email protected]13ef7c02008-11-20 22:30:1332namespace base {
33class Time;
34}
35
[email protected]01dbd932009-06-23 22:52:4236namespace url_canon {
37struct CanonHostInfo;
38}
39
[email protected]f9fe8632009-05-22 18:15:2440namespace url_parse {
41struct Parsed;
42}
43
[email protected]8ac1a752008-07-31 19:40:3744namespace net {
initial.commit586acc5fe2008-07-26 22:42:5245
[email protected]69c579e2010-04-23 20:01:0046// Used by FormatUrl to specify handling of certain parts of the url.
47typedef uint32 FormatUrlType;
48typedef uint32 FormatUrlTypes;
49
[email protected]c09fb1c72010-11-03 23:25:5550// Used by GetHeaderParamValue to determine how to handle quotes in the value.
51class QuoteRule {
52 public:
53 enum Type {
54 KEEP_OUTER_QUOTES,
55 REMOVE_OUTER_QUOTES,
56 };
57
58 private:
59 QuoteRule();
60};
61
[email protected]69c579e2010-04-23 20:01:0062// Nothing is ommitted.
[email protected]172da1b2011-08-12 15:52:2663NET_EXPORT extern const FormatUrlType kFormatUrlOmitNothing;
[email protected]69c579e2010-04-23 20:01:0064
65// If set, any username and password are removed.
[email protected]172da1b2011-08-12 15:52:2666NET_EXPORT extern const FormatUrlType kFormatUrlOmitUsernamePassword;
[email protected]69c579e2010-04-23 20:01:0067
68// If the scheme is 'http://', it's removed.
[email protected]172da1b2011-08-12 15:52:2669NET_EXPORT extern const FormatUrlType kFormatUrlOmitHTTP;
[email protected]69c579e2010-04-23 20:01:0070
[email protected]79845ef2010-06-02 02:37:4071// Omits the path if it is just a slash and there is no query or ref. This is
72// meaningful for non-file "standard" URLs.
[email protected]172da1b2011-08-12 15:52:2673NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname;
[email protected]79845ef2010-06-02 02:37:4074
[email protected]69c579e2010-04-23 20:01:0075// Convenience for omitting all unecessary types.
[email protected]172da1b2011-08-12 15:52:2676NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll;
[email protected]69c579e2010-04-23 20:01:0077
[email protected]d95fa182009-09-09 17:01:1678// Holds a list of ports that should be accepted despite bans.
[email protected]172da1b2011-08-12 15:52:2679NET_EXPORT_PRIVATE extern std::multiset<int> explicitly_allowed_ports;
[email protected]d95fa182009-09-09 17:01:1680
initial.commit586acc5fe2008-07-26 22:42:5281// Given the full path to a file name, creates a file: URL. The returned URL
82// may not be valid if the input is malformed.
[email protected]172da1b2011-08-12 15:52:2683NET_EXPORT GURL FilePathToFileURL(const FilePath& path);
initial.commit586acc5fe2008-07-26 22:42:5284
85// Converts a file: URL back to a filename that can be passed to the OS. The
86// file URL must be well-formed (GURL::is_valid() must return true); we don't
87// handle degenerate cases here. Returns true on success, false if it isn't a
88// valid file URL. On failure, *file_path will be empty.
[email protected]172da1b2011-08-12 15:52:2689NET_EXPORT bool FileURLToFilePath(const GURL& url, FilePath* file_path);
initial.commit586acc5fe2008-07-26 22:42:5290
[email protected]71e4573a2009-05-21 22:03:0091// Splits an input of the form <host>[":"<port>] into its consitituent parts.
[email protected]f6fb2de2009-02-19 08:11:4292// Saves the result into |*host| and |*port|. If the input did not have
93// the optional port, sets |*port| to -1.
94// Returns true if the parsing was successful, false otherwise.
[email protected]d16659f2009-05-18 16:47:3295// The returned host is NOT canonicalized, and may be invalid. If <host> is
96// an IPv6 literal address, the returned host includes the square brackets.
[email protected]172da1b2011-08-12 15:52:2697NET_EXPORT_PRIVATE bool ParseHostAndPort(
98 std::string::const_iterator host_and_port_begin,
99 std::string::const_iterator host_and_port_end,
100 std::string* host,
101 int* port);
102NET_EXPORT_PRIVATE bool ParseHostAndPort(
103 const std::string& host_and_port,
104 std::string* host,
105 int* port);
[email protected]71e4573a2009-05-21 22:03:00106
107// Returns a host:port string for the given URL.
[email protected]172da1b2011-08-12 15:52:26108NET_EXPORT std::string GetHostAndPort(const GURL& url);
[email protected]71e4573a2009-05-21 22:03:00109
110// Returns a host[:port] string for the given URL, where the port is omitted
111// if it is the default for the URL's scheme.
[email protected]172da1b2011-08-12 15:52:26112NET_EXPORT_PRIVATE std::string GetHostAndOptionalPort(const GURL& url);
[email protected]f6fb2de2009-02-19 08:11:42113
[email protected]50d7d7282009-03-02 21:45:18114// Returns the string representation of an address, like "192.168.0.1".
115// Returns empty string on failure.
[email protected]172da1b2011-08-12 15:52:26116NET_EXPORT std::string NetAddressToString(const struct addrinfo* net_address);
117NET_EXPORT std::string NetAddressToString(const struct sockaddr* net_address,
118 socklen_t address_len);
[email protected]50d7d7282009-03-02 21:45:18119
[email protected]d1388f42010-06-16 03:14:42120// Same as NetAddressToString, but additionally includes the port number. For
121// example: "192.168.0.1:99" or "[::1]:80".
[email protected]172da1b2011-08-12 15:52:26122NET_EXPORT std::string NetAddressToStringWithPort(
[email protected]13677b82011-05-18 18:29:36123 const struct addrinfo* net_address);
[email protected]172da1b2011-08-12 15:52:26124NET_EXPORT std::string NetAddressToStringWithPort(
[email protected]13677b82011-05-18 18:29:36125 const struct sockaddr* net_address,
126 socklen_t address_len);
[email protected]d1388f42010-06-16 03:14:42127
[email protected]eba291342009-03-03 21:30:46128// Returns the hostname of the current system. Returns empty string on failure.
[email protected]172da1b2011-08-12 15:52:26129NET_EXPORT std::string GetHostName();
[email protected]eba291342009-03-03 21:30:46130
[email protected]99d69352009-09-16 00:20:29131// Extracts the unescaped username/password from |url|, saving the results
132// into |*username| and |*password|.
[email protected]172da1b2011-08-12 15:52:26133NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
[email protected]13c8a092010-07-29 06:15:44134 string16* username,
135 string16* password);
[email protected]99d69352009-09-16 00:20:29136
[email protected]9d797f32010-04-23 07:17:54137// Returns either the host from |url|, or, if the host is empty, the full spec.
[email protected]172da1b2011-08-12 15:52:26138NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
[email protected]9d797f32010-04-23 07:17:54139
initial.commit586acc5fe2008-07-26 22:42:52140// Return the value of the HTTP response header with name 'name'. 'headers'
[email protected]8effd3f62011-03-25 16:29:07141// should be in the format that URLRequest::GetResponseHeaders() returns.
initial.commit586acc5fe2008-07-26 22:42:52142// Returns the empty string if the header is not found.
[email protected]172da1b2011-08-12 15:52:26143NET_EXPORT std::string GetSpecificHeader(const std::string& headers,
144 const std::string& name);
initial.commit586acc5fe2008-07-26 22:42:52145
146// Return the value of the HTTP response header field's parameter named
147// 'param_name'. Returns the empty string if the parameter is not found or is
148// improperly formatted.
[email protected]172da1b2011-08-12 15:52:26149NET_EXPORT std::string GetHeaderParamValue(const std::string& header,
150 const std::string& param_name,
151 QuoteRule::Type quote_rule);
initial.commit586acc5fe2008-07-26 22:42:52152
[email protected]c9825a42009-05-01 22:51:50153// Return the filename extracted from Content-Disposition header. The following
154// formats are tried in order listed below:
initial.commit586acc5fe2008-07-26 22:42:52155//
[email protected]c09fb1c72010-11-03 23:25:55156// 1. RFC 5987
157// 2. RFC 2047
158// 3. Raw-8bit-characters :
[email protected]c9825a42009-05-01 22:51:50159// a. UTF-8, b. referrer_charset, c. default os codepage.
[email protected]c09fb1c72010-11-03 23:25:55160// 4. %-escaped UTF-8.
[email protected]c9825a42009-05-01 22:51:50161//
[email protected]c09fb1c72010-11-03 23:25:55162// In step 3, if referrer_charset is empty(i.e. unknown), 3b is skipped.
163// In step 4, the fallback charsets tried in step 3 are not tried. We
[email protected]c9825a42009-05-01 22:51:50164// can consider doing that later.
165//
[email protected]c09fb1c72010-11-03 23:25:55166// When a param value is ASCII, but is not in format #2 or format #4 above,
[email protected]c9825a42009-05-01 22:51:50167// it is returned as it is unless it's pretty close to two supported
initial.commit586acc5fe2008-07-26 22:42:52168// formats but not well-formed. In that case, an empty string is returned.
169//
170// In any case, a caller must check for the empty return value and resort to
171// another means to get a filename (e.g. url).
172//
173// This function does not do any escaping and callers are responsible for
174// escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit.
[email protected]172da1b2011-08-12 15:52:26175NET_EXPORT_PRIVATE std::string GetFileNameFromCD(
176 const std::string& header,
177 const std::string& referrer_charset);
initial.commit586acc5fe2008-07-26 22:42:52178
[email protected]ce85f602009-11-07 01:34:53179// Converts the given host name to unicode characters. This can be called for
180// any host name, if the input is not IDN or is invalid in some way, we'll just
181// return the ASCII source so it is still usable.
initial.commit586acc5fe2008-07-26 22:42:52182//
183// The input should be the canonicalized ASCII host name from GURL. This
[email protected]04866c42011-05-03 20:03:50184// function does NOT accept UTF-8!
initial.commit586acc5fe2008-07-26 22:42:52185//
186// |languages| is a comma separated list of ISO 639 language codes. It
187// is used to determine whether a hostname is 'comprehensible' to a user
188// who understands languages listed. |host| will be converted to a
189// human-readable form (Unicode) ONLY when each component of |host| is
190// regarded as 'comprehensible'. Scipt-mixing is not allowed except that
191// Latin letters in the ASCII range can be mixed with a limited set of
192// script-language pairs (currently Han, Kana and Hangul for zh,ja and ko).
193// When |languages| is empty, even that mixing is not allowed.
[email protected]172da1b2011-08-12 15:52:26194NET_EXPORT string16 IDNToUnicode(const std::string& host,
195 const std::string& languages);
initial.commit586acc5fe2008-07-26 22:42:52196
[email protected]01dbd932009-06-23 22:52:42197// Canonicalizes |host| and returns it. Also fills |host_info| with
198// IP address information. |host_info| must not be NULL.
[email protected]172da1b2011-08-12 15:52:26199NET_EXPORT std::string CanonicalizeHost(const std::string& host,
200 url_canon::CanonHostInfo* host_info);
initial.commit586acc5fe2008-07-26 22:42:52201
[email protected]833fa262009-10-27 21:06:11202// Returns true if |host| is not an IP address and is compliant with a set of
203// rules based on RFC 1738 and tweaked to be compatible with the real world.
204// The rules are:
[email protected]96be43e92009-10-16 19:49:22205// * One or more components separated by '.'
[email protected]b26a4d92011-06-21 22:11:00206// * Each component begins with an alphanumeric character or '-'
[email protected]833fa262009-10-27 21:06:11207// * Each component contains only alphanumeric characters and '-' or '_'
[email protected]b26a4d92011-06-21 22:11:00208// * Each component ends with an alphanumeric character
209// * The last component begins with an alphabetic character
[email protected]d120c3722009-11-03 18:17:26210// * Optional trailing dot after last component (means "treat as FQDN")
[email protected]7e563812010-03-22 20:05:59211// If |desired_tld| is non-NULL, the host will only be considered invalid if
212// appending it as a trailing component still results in an invalid host. This
213// helps us avoid marking as "invalid" user attempts to open "www.401k.com" by
214// typing 4-0-1-k-<ctrl>+<enter>.
[email protected]96be43e92009-10-16 19:49:22215//
216// NOTE: You should only pass in hosts that have been returned from
217// CanonicalizeHost(), or you may not get accurate results.
[email protected]172da1b2011-08-12 15:52:26218NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host,
219 const std::string& desired_tld);
[email protected]96be43e92009-10-16 19:49:22220
[email protected]5420bc1e2009-07-09 22:48:16221// Call these functions to get the html snippet for a directory listing.
222// The return values of both functions are in UTF-8.
[email protected]172da1b2011-08-12 15:52:26223NET_EXPORT std::string GetDirectoryListingHeader(const string16& title);
[email protected]5420bc1e2009-07-09 22:48:16224
225// Given the name of a file in a directory (ftp or local) and
226// other information (is_dir, size, modification time), it returns
227// the html snippet to add the entry for the file to the directory listing.
228// Currently, it's a script tag containing a call to a Javascript function
229// |addRow|.
230//
[email protected]193c3512010-05-11 09:19:30231// |name| is the file name to be displayed. |raw_bytes| will be used
232// as the actual target of the link (so for example, ftp links should use
233// server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
234// will be used.
235//
236// Both |name| and |raw_bytes| are escaped internally.
[email protected]172da1b2011-08-12 15:52:26237NET_EXPORT std::string GetDirectoryListingEntry(const string16& name,
238 const std::string& raw_bytes,
239 bool is_dir, int64 size,
240 base::Time modified);
initial.commit586acc5fe2008-07-26 22:42:52241
242// If text starts with "www." it is removed, otherwise text is returned
243// unmodified.
[email protected]172da1b2011-08-12 15:52:26244NET_EXPORT string16 StripWWW(const string16& text);
initial.commit586acc5fe2008-07-26 22:42:52245
[email protected]cc099a32011-05-18 22:55:09246// Gets the filename in the following order:
247// 1) the raw Content-Disposition header (as read from the network).
248// |referrer_charset| is used as one of charsets to interpret a raw 8bit
249// string in C-D header (after interpreting as UTF-8 fails).
250// See the comment for GetFilenameFromCD for more details.
251// 2) the suggested name
252// 3) the last path component name or hostname from |url|
253// 4) the given |default_name|
254// 5) the hard-coded name "download", as the last resort
[email protected]172da1b2011-08-12 15:52:26255NET_EXPORT string16 GetSuggestedFilename(const GURL& url,
256 const std::string& content_disposition,
257 const std::string& referrer_charset,
258 const std::string& suggested_name,
259 const string16& default_name);
initial.commit586acc5fe2008-07-26 22:42:52260
[email protected]30219d62011-08-09 18:56:45261// Generate a filename based on a HTTP request.
262//
263// The |url|, |content_disposition|, |referrer_charset|, |suggested_name|, and
264// |default_name| parameters will be used with GetSuggestedFilename() to
265// generate a filename. The resulting filename will be passed in along with the
266// |mime_type| to GenerateSafeFileName() to generate the returned filename.
[email protected]172da1b2011-08-12 15:52:26267NET_EXPORT FilePath GenerateFileName(const GURL& url,
268 const std::string& content_disposition,
269 const std::string& referrer_charset,
270 const std::string& suggested_name,
271 const std::string& mime_type,
272 const string16& default_name);
[email protected]30219d62011-08-09 18:56:45273
274// Ensures that the filename and extension is safe to use in the filesystem.
275//
276// Assumes that |file_path| already contains a valid path or file name. On
277// Windows if the extension causes the file to have an unsafe interaction with
278// the shell (see net_util::IsShellIntegratedExtension()), then it will be
279// replaced by the string 'download'. If |file_path| doesn't contain an
280// extension and |mime_type| is non-empty, the preferred extension for
281// |mime_type| will be used as the extension.
282//
283// On Windows, the filename will be checked against a set of reserved names, and
284// if so, an underscore will be prepended to the name.
285//
286// |file_name| can either be just the file name or it can be a full path to a
287// file.
288//
289// Note: |mime_type| should only be non-empty if this function is called from a
290// thread that allows IO.
[email protected]172da1b2011-08-12 15:52:26291NET_EXPORT void GenerateSafeFileName(const std::string& mime_type,
292 FilePath* file_path);
[email protected]30219d62011-08-09 18:56:45293
initial.commit586acc5fe2008-07-26 22:42:52294// Checks the given port against a list of ports which are restricted by
295// default. Returns true if the port is allowed, false if it is restricted.
296bool IsPortAllowedByDefault(int port);
297
298// Checks the given port against a list of ports which are restricted by the
299// FTP protocol. Returns true if the port is allowed, false if it is
300// restricted.
301bool IsPortAllowedByFtp(int port);
302
[email protected]d95fa182009-09-09 17:01:16303// Check if banned |port| has been overriden by an entry in
304// |explicitly_allowed_ports_|.
305bool IsPortAllowedByOverride(int port);
306
[email protected]f6f1ba3cf2008-11-11 01:06:15307// Set socket to non-blocking mode
[email protected]172da1b2011-08-12 15:52:26308NET_EXPORT int SetNonBlocking(int fd);
[email protected]f6f1ba3cf2008-11-11 01:06:15309
[email protected]04866c42011-05-03 20:03:50310// Formats the host in |url| and appends it to |output|. The host formatter
311// takes the same accept languages component as ElideURL().
[email protected]172da1b2011-08-12 15:52:26312NET_EXPORT void AppendFormattedHost(const GURL& url,
313 const std::string& languages,
314 string16* output);
[email protected]f9fe8632009-05-22 18:15:24315
[email protected]ce85f602009-11-07 01:34:53316// Creates a string representation of |url|. The IDN host name may be in Unicode
[email protected]69c579e2010-04-23 20:01:00317// if |languages| accepts the Unicode representation. |format_type| is a bitmask
318// of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean
319// the URL for human readability. You will generally want |UnescapeRule::SPACES|
320// for display to the user if you can handle spaces, or |UnescapeRule::NORMAL|
321// if not. If the path part and the query part seem to be encoded in %-encoded
322// UTF-8, decodes %-encoding and UTF-8.
[email protected]ce85f602009-11-07 01:34:53323//
324// The last three parameters may be NULL.
325// |new_parsed| will be set to the parsing parameters of the resultant URL.
[email protected]a23de8572009-06-03 02:16:32326// |prefix_end| will be the length before the hostname of the resultant URL.
[email protected]421de2ab2011-04-13 18:43:05327//
328// (|offset[s]_for_adjustment|) specifies one or more offsets into the original
329// |url|'s spec(); each offset will be modified to reflect changes this function
330// makes to the output string. For example, if |url| is "http://a:[email protected]/",
331// |omit_username_password| is true, and an offset is 12 (the offset of '.'),
332// then on return the output string will be "http://c.com/" and the offset will
333// be 8. If an offset cannot be successfully adjusted (e.g. because it points
[email protected]ce85f602009-11-07 01:34:53334// into the middle of a component that was entirely removed, past the end of the
335// string, or into the middle of an encoding sequence), it will be set to
[email protected]9f284f132010-08-31 06:14:17336// string16::npos.
[email protected]172da1b2011-08-12 15:52:26337NET_EXPORT string16 FormatUrl(const GURL& url,
338 const std::string& languages,
339 FormatUrlTypes format_types,
340 UnescapeRule::Type unescape_rules,
341 url_parse::Parsed* new_parsed,
342 size_t* prefix_end,
343 size_t* offset_for_adjustment);
344NET_EXPORT string16 FormatUrlWithOffsets(
[email protected]13677b82011-05-18 18:29:36345 const GURL& url,
346 const std::string& languages,
347 FormatUrlTypes format_types,
348 UnescapeRule::Type unescape_rules,
349 url_parse::Parsed* new_parsed,
350 size_t* prefix_end,
351 std::vector<size_t>* offsets_for_adjustment);
[email protected]f9fe8632009-05-22 18:15:24352
[email protected]79845ef2010-06-02 02:37:40353// This is a convenience function for FormatUrl() with
354// format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical
355// set of flags for "URLs to display to the user". You should be cautious about
356// using this for URLs which will be parsed or sent to other applications.
[email protected]1f659d972010-08-30 04:49:56357inline string16 FormatUrl(const GURL& url, const std::string& languages) {
358 return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES,
359 NULL, NULL, NULL);
360}
[email protected]f9fe8632009-05-22 18:15:24361
[email protected]79845ef2010-06-02 02:37:40362// Returns whether FormatUrl() would strip a trailing slash from |url|, given a
363// format flag including kFormatUrlOmitTrailingSlashOnBareHostname.
[email protected]172da1b2011-08-12 15:52:26364NET_EXPORT bool CanStripTrailingSlash(const GURL& url);
[email protected]79845ef2010-06-02 02:37:40365
[email protected]7ec7c182009-08-04 02:33:58366// Strip the portions of |url| that aren't core to the network request.
367// - user name / password
368// - reference section
[email protected]172da1b2011-08-12 15:52:26369NET_EXPORT_PRIVATE GURL SimplifyUrlForRequest(const GURL& url);
[email protected]7ec7c182009-08-04 02:33:58370
[email protected]172da1b2011-08-12 15:52:26371NET_EXPORT void SetExplicitlyAllowedPorts(const std::string& allowed_ports);
[email protected]d95fa182009-09-09 17:01:16372
[email protected]172da1b2011-08-12 15:52:26373class NET_EXPORT ScopedPortException {
[email protected]bfba3652010-12-10 14:43:00374 public:
375 ScopedPortException(int port);
376 ~ScopedPortException();
377
378 private:
379 int port_;
380
381 DISALLOW_COPY_AND_ASSIGN(ScopedPortException);
382};
383
[email protected]32eaa332010-02-08 22:15:54384// Perform a simplistic test to see if IPv6 is supported by trying to create an
385// IPv6 socket.
386// TODO(jar): Make test more in-depth as needed.
[email protected]172da1b2011-08-12 15:52:26387NET_EXPORT bool IPv6Supported();
[email protected]32eaa332010-02-08 22:15:54388
[email protected]2f3bc65c2010-07-23 17:47:10389// Returns true if it can determine that only loopback addresses are configured.
390// i.e. if only 127.0.0.1 and ::1 are routable.
391bool HaveOnlyLoopbackAddresses();
392
[email protected]54392832010-06-08 23:25:04393// IPAddressNumber is used to represent an IP address's numeric value as an
394// array of bytes, from most significant to least significant. This is the
395// network byte ordering.
396//
397// IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
398typedef std::vector<unsigned char> IPAddressNumber;
[email protected]d3911162011-07-18 15:12:46399typedef std::vector<IPAddressNumber> IPAddressList;
[email protected]54392832010-06-08 23:25:04400
[email protected]611cc902011-03-19 00:36:39401static const size_t kIPv4AddressSize = 4;
402static const size_t kIPv6AddressSize = 16;
403
[email protected]54392832010-06-08 23:25:04404// Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
405// Returns true on success and fills |ip_number| with the numeric value.
[email protected]172da1b2011-08-12 15:52:26406NET_EXPORT_PRIVATE bool ParseIPLiteralToNumber(const std::string& ip_literal,
407 IPAddressNumber* ip_number);
[email protected]54392832010-06-08 23:25:04408
409// Converts an IPv4 address to an IPv4-mapped IPv6 address.
410// For example 192.168.0.1 would be converted to ::ffff:192.168.0.1.
[email protected]172da1b2011-08-12 15:52:26411NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4NumberToIPv6Number(
[email protected]54392832010-06-08 23:25:04412 const IPAddressNumber& ipv4_number);
413
414// Parses an IP block specifier from CIDR notation to an
415// (IP address, prefix length) pair. Returns true on success and fills
416// |*ip_number| with the numeric value of the IP address and sets
417// |*prefix_length_in_bits| with the length of the prefix.
418//
419// CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
420//
421// 10.10.3.1/20
422// a:b:c::/46
423// ::1/128
[email protected]172da1b2011-08-12 15:52:26424NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal,
425 IPAddressNumber* ip_number,
426 size_t* prefix_length_in_bits);
[email protected]54392832010-06-08 23:25:04427
428// Compares an IP address to see if it falls within the specified IP block.
429// Returns true if it does, false otherwise.
430//
431// The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
432// IP address whose |prefix_length_in_bits| most significant bits match
433// |ip_prefix| will be matched.
434//
435// In cases when an IPv4 address is being compared to an IPv6 address prefix
436// and vice versa, the IPv4 addresses will be converted to IPv4-mapped
437// (IPv6) addresses.
[email protected]172da1b2011-08-12 15:52:26438NET_EXPORT_PRIVATE bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
439 const IPAddressNumber& ip_prefix,
440 size_t prefix_length_in_bits);
[email protected]54392832010-06-08 23:25:04441
[email protected]0d1f4c42011-05-05 15:27:50442// Makes a copy of |info|. The dynamically-allocated parts are copied as well.
443// If |recursive| is true, chained entries via ai_next are copied too.
444// The copy returned by this function should be freed using
445// FreeCopyOfAddrinfo(), and NOT freeaddrinfo().
446struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info,
447 bool recursive);
448
449// Frees an addrinfo that was created by CreateCopyOfAddrinfo().
450void FreeCopyOfAddrinfo(struct addrinfo* info);
451
[email protected]d1388f42010-06-16 03:14:42452// Returns the port field of the sockaddr in |info|.
[email protected]93e79012011-01-26 04:13:59453const uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info);
454uint16* GetPortFieldFromAddrinfo(struct addrinfo* info);
[email protected]d1388f42010-06-16 03:14:42455
456// Returns the value of |info's| port (in host byte ordering).
[email protected]fe89ea72011-05-12 02:02:40457uint16 GetPortFromAddrinfo(const struct addrinfo* info);
[email protected]d1388f42010-06-16 03:14:42458
[email protected]93e79012011-01-26 04:13:59459// Same except for struct sockaddr.
460const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
461 socklen_t address_len);
[email protected]172da1b2011-08-12 15:52:26462NET_EXPORT_PRIVATE int GetPortFromSockaddr(const struct sockaddr* address,
463 socklen_t address_len);
[email protected]93e79012011-01-26 04:13:59464
[email protected]fe89ea72011-05-12 02:02:40465// Sets every addrinfo in the linked list |head| as having a port field of
466// |port|.
[email protected]172da1b2011-08-12 15:52:26467NET_EXPORT_PRIVATE void SetPortForAllAddrinfos(struct addrinfo* head,
468 uint16 port);
[email protected]fe89ea72011-05-12 02:02:40469
[email protected]2fd33ee92011-03-25 22:30:21470// Returns true if |host| is one of the names (e.g. "localhost") or IP
471// addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback.
472//
473// Note that this function does not check for IP addresses other than
474// the above, although other IP addresses may point to the local
475// machine.
[email protected]172da1b2011-08-12 15:52:26476NET_EXPORT_PRIVATE bool IsLocalhost(const std::string& host);
[email protected]2fd33ee92011-03-25 22:30:21477
[email protected]611cc902011-03-19 00:36:39478// struct that is used by GetNetworkList() to represent a network
479// interface.
[email protected]172da1b2011-08-12 15:52:26480struct NET_EXPORT NetworkInterface {
[email protected]611cc902011-03-19 00:36:39481 NetworkInterface();
482 NetworkInterface(const std::string& name, const IPAddressNumber& address);
483 ~NetworkInterface();
484
485 std::string name;
486 IPAddressNumber address;
487};
488
[email protected]91ed478b2011-04-21 23:20:07489typedef std::vector<NetworkInterface> NetworkInterfaceList;
[email protected]611cc902011-03-19 00:36:39490
491// Returns list of network interfaces except loopback interface. If an
492// interface has more than one address, a separate entry is added to
493// the list for each address.
494// Can be called only on a thread that allows IO.
[email protected]172da1b2011-08-12 15:52:26495NET_EXPORT bool GetNetworkList(NetworkInterfaceList* networks);
[email protected]611cc902011-03-19 00:36:39496
[email protected]8ac1a752008-07-31 19:40:37497} // namespace net
initial.commit586acc5fe2008-07-26 22:42:52498
[email protected]de2943352009-10-22 23:06:12499#endif // NET_BASE_NET_UTIL_H_