blob: 961093266c600b4820d27293a5c93ab25091c8a9 [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]13677b82011-05-18 18:29:3626#include "net/base/net_api.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]13677b82011-05-18 18:29:3663NET_API extern const FormatUrlType kFormatUrlOmitNothing;
[email protected]69c579e2010-04-23 20:01:0064
65// If set, any username and password are removed.
[email protected]13677b82011-05-18 18:29:3666NET_API extern const FormatUrlType kFormatUrlOmitUsernamePassword;
[email protected]69c579e2010-04-23 20:01:0067
68// If the scheme is 'http://', it's removed.
[email protected]13677b82011-05-18 18:29:3669NET_API 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]13677b82011-05-18 18:29:3673NET_API 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]13677b82011-05-18 18:29:3676NET_API 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]13677b82011-05-18 18:29:3679NET_TEST 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]13677b82011-05-18 18:29:3683NET_API 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]13677b82011-05-18 18:29:3689NET_API 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]13677b82011-05-18 18:29:3697NET_TEST bool ParseHostAndPort(std::string::const_iterator host_and_port_begin,
98 std::string::const_iterator host_and_port_end,
99 std::string* host,
100 int* port);
101NET_TEST bool ParseHostAndPort(const std::string& host_and_port,
102 std::string* host,
103 int* port);
[email protected]71e4573a2009-05-21 22:03:00104
105// Returns a host:port string for the given URL.
[email protected]13677b82011-05-18 18:29:36106NET_API std::string GetHostAndPort(const GURL& url);
[email protected]71e4573a2009-05-21 22:03:00107
108// Returns a host[:port] string for the given URL, where the port is omitted
109// if it is the default for the URL's scheme.
[email protected]13677b82011-05-18 18:29:36110NET_TEST std::string GetHostAndOptionalPort(const GURL& url);
[email protected]f6fb2de2009-02-19 08:11:42111
[email protected]50d7d7282009-03-02 21:45:18112// Returns the string representation of an address, like "192.168.0.1".
113// Returns empty string on failure.
[email protected]13677b82011-05-18 18:29:36114NET_API std::string NetAddressToString(const struct addrinfo* net_address);
115NET_API std::string NetAddressToString(const struct sockaddr* net_address,
116 socklen_t address_len);
[email protected]50d7d7282009-03-02 21:45:18117
[email protected]d1388f42010-06-16 03:14:42118// Same as NetAddressToString, but additionally includes the port number. For
119// example: "192.168.0.1:99" or "[::1]:80".
[email protected]13677b82011-05-18 18:29:36120NET_API std::string NetAddressToStringWithPort(
121 const struct addrinfo* net_address);
122NET_API std::string NetAddressToStringWithPort(
123 const struct sockaddr* net_address,
124 socklen_t address_len);
[email protected]d1388f42010-06-16 03:14:42125
[email protected]eba291342009-03-03 21:30:46126// Returns the hostname of the current system. Returns empty string on failure.
[email protected]13677b82011-05-18 18:29:36127NET_API std::string GetHostName();
[email protected]eba291342009-03-03 21:30:46128
[email protected]99d69352009-09-16 00:20:29129// Extracts the unescaped username/password from |url|, saving the results
130// into |*username| and |*password|.
[email protected]13677b82011-05-18 18:29:36131NET_TEST void GetIdentityFromURL(const GURL& url,
[email protected]13c8a092010-07-29 06:15:44132 string16* username,
133 string16* password);
[email protected]99d69352009-09-16 00:20:29134
[email protected]9d797f32010-04-23 07:17:54135// Returns either the host from |url|, or, if the host is empty, the full spec.
[email protected]13677b82011-05-18 18:29:36136NET_API std::string GetHostOrSpecFromURL(const GURL& url);
[email protected]9d797f32010-04-23 07:17:54137
initial.commit586acc5fe2008-07-26 22:42:52138// Return the value of the HTTP response header with name 'name'. 'headers'
[email protected]8effd3f62011-03-25 16:29:07139// should be in the format that URLRequest::GetResponseHeaders() returns.
initial.commit586acc5fe2008-07-26 22:42:52140// Returns the empty string if the header is not found.
[email protected]13677b82011-05-18 18:29:36141NET_API std::string GetSpecificHeader(const std::string& headers,
142 const std::string& name);
initial.commit586acc5fe2008-07-26 22:42:52143
144// Return the value of the HTTP response header field's parameter named
145// 'param_name'. Returns the empty string if the parameter is not found or is
146// improperly formatted.
[email protected]13677b82011-05-18 18:29:36147NET_API std::string GetHeaderParamValue(const std::string& header,
148 const std::string& param_name,
149 QuoteRule::Type quote_rule);
initial.commit586acc5fe2008-07-26 22:42:52150
[email protected]c9825a42009-05-01 22:51:50151// Return the filename extracted from Content-Disposition header. The following
152// formats are tried in order listed below:
initial.commit586acc5fe2008-07-26 22:42:52153//
[email protected]c09fb1c72010-11-03 23:25:55154// 1. RFC 5987
155// 2. RFC 2047
156// 3. Raw-8bit-characters :
[email protected]c9825a42009-05-01 22:51:50157// a. UTF-8, b. referrer_charset, c. default os codepage.
[email protected]c09fb1c72010-11-03 23:25:55158// 4. %-escaped UTF-8.
[email protected]c9825a42009-05-01 22:51:50159//
[email protected]c09fb1c72010-11-03 23:25:55160// In step 3, if referrer_charset is empty(i.e. unknown), 3b is skipped.
161// In step 4, the fallback charsets tried in step 3 are not tried. We
[email protected]c9825a42009-05-01 22:51:50162// can consider doing that later.
163//
[email protected]c09fb1c72010-11-03 23:25:55164// When a param value is ASCII, but is not in format #2 or format #4 above,
[email protected]c9825a42009-05-01 22:51:50165// it is returned as it is unless it's pretty close to two supported
initial.commit586acc5fe2008-07-26 22:42:52166// formats but not well-formed. In that case, an empty string is returned.
167//
168// In any case, a caller must check for the empty return value and resort to
169// another means to get a filename (e.g. url).
170//
171// This function does not do any escaping and callers are responsible for
172// escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit.
173//
174// TODO(jungshik): revisit this issue. At the moment, the only caller
175// net_util::GetSuggestedFilename and it calls ReplaceIllegalCharacters. The
176// other caller is a unit test. Need to figure out expose this function only to
177// net_util_unittest.
178//
[email protected]13677b82011-05-18 18:29:36179NET_TEST std::string GetFileNameFromCD(const std::string& header,
180 const std::string& referrer_charset);
initial.commit586acc5fe2008-07-26 22:42:52181
[email protected]ce85f602009-11-07 01:34:53182// 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.
initial.commit586acc5fe2008-07-26 22:42:52185//
186// The input should be the canonicalized ASCII host name from GURL. This
[email protected]04866c42011-05-03 20:03:50187// function does NOT accept UTF-8!
initial.commit586acc5fe2008-07-26 22:42:52188//
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.
[email protected]13677b82011-05-18 18:29:36197NET_API string16 IDNToUnicode(const std::string& host,
198 const std::string& languages);
initial.commit586acc5fe2008-07-26 22:42:52199
[email protected]01dbd932009-06-23 22:52:42200// Canonicalizes |host| and returns it. Also fills |host_info| with
201// IP address information. |host_info| must not be NULL.
202std::string CanonicalizeHost(const std::string& host,
203 url_canon::CanonHostInfo* host_info);
initial.commit586acc5fe2008-07-26 22:42:52204
[email protected]833fa262009-10-27 21:06:11205// 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:
[email protected]96be43e92009-10-16 19:49:22208// * One or more components separated by '.'
209// * Each component begins and ends with an alphanumeric character
[email protected]833fa262009-10-27 21:06:11210// * Each component contains only alphanumeric characters and '-' or '_'
[email protected]96be43e92009-10-16 19:49:22211// * The last component does not begin with a digit
[email protected]d120c3722009-11-03 18:17:26212// * Optional trailing dot after last component (means "treat as FQDN")
[email protected]7e563812010-03-22 20:05:59213// If |desired_tld| is non-NULL, the host will only be considered invalid if
214// appending it as a trailing component still results in an invalid host. This
215// helps us avoid marking as "invalid" user attempts to open "www.401k.com" by
216// typing 4-0-1-k-<ctrl>+<enter>.
[email protected]96be43e92009-10-16 19:49:22217//
218// NOTE: You should only pass in hosts that have been returned from
219// CanonicalizeHost(), or you may not get accurate results.
[email protected]13677b82011-05-18 18:29:36220NET_API bool IsCanonicalizedHostCompliant(const std::string& host,
221 const std::string& desired_tld);
[email protected]96be43e92009-10-16 19:49:22222
[email protected]5420bc1e2009-07-09 22:48:16223// Call these functions to get the html snippet for a directory listing.
224// The return values of both functions are in UTF-8.
225std::string GetDirectoryListingHeader(const string16& title);
226
227// Given the name of a file in a directory (ftp or local) and
228// other information (is_dir, size, modification time), it returns
229// the html snippet to add the entry for the file to the directory listing.
230// Currently, it's a script tag containing a call to a Javascript function
231// |addRow|.
232//
[email protected]193c3512010-05-11 09:19:30233// |name| is the file name to be displayed. |raw_bytes| will be used
234// as the actual target of the link (so for example, ftp links should use
235// server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
236// will be used.
237//
238// Both |name| and |raw_bytes| are escaped internally.
[email protected]13677b82011-05-18 18:29:36239NET_API std::string GetDirectoryListingEntry(const string16& name,
240 const std::string& raw_bytes,
241 bool is_dir, int64 size,
242 base::Time modified);
initial.commit586acc5fe2008-07-26 22:42:52243
244// If text starts with "www." it is removed, otherwise text is returned
245// unmodified.
[email protected]13677b82011-05-18 18:29:36246NET_API string16 StripWWW(const string16& text);
initial.commit586acc5fe2008-07-26 22:42:52247
248// Gets the filename from the raw Content-Disposition header (as read from the
249// network). Otherwise uses the last path component name or hostname from
[email protected]630947c2009-11-04 18:37:31250// |url|. If there is no filename or it can't be used, the given |default_name|,
251// will be used unless it is empty.
[email protected]de2943352009-10-22 23:06:12252
253// Note: it's possible for the suggested filename to be empty (e.g.,
[email protected]13c34d12009-09-14 20:51:04254// file:///). referrer_charset is used as one of charsets
[email protected]c9825a42009-05-01 22:51:50255// to interpret a raw 8bit string in C-D header (after interpreting
256// as UTF-8 fails). See the comment for GetFilenameFromCD for more details.
[email protected]13677b82011-05-18 18:29:36257NET_API string16 GetSuggestedFilename(const GURL& url,
258 const std::string& content_disposition,
259 const std::string& referrer_charset,
260 const string16& default_name);
initial.commit586acc5fe2008-07-26 22:42:52261
262// Checks the given port against a list of ports which are restricted by
263// default. Returns true if the port is allowed, false if it is restricted.
264bool IsPortAllowedByDefault(int port);
265
266// Checks the given port against a list of ports which are restricted by the
267// FTP protocol. Returns true if the port is allowed, false if it is
268// restricted.
269bool IsPortAllowedByFtp(int port);
270
[email protected]d95fa182009-09-09 17:01:16271// Check if banned |port| has been overriden by an entry in
272// |explicitly_allowed_ports_|.
273bool IsPortAllowedByOverride(int port);
274
[email protected]f6f1ba3cf2008-11-11 01:06:15275// Set socket to non-blocking mode
276int SetNonBlocking(int fd);
277
[email protected]04866c42011-05-03 20:03:50278// Formats the host in |url| and appends it to |output|. The host formatter
279// takes the same accept languages component as ElideURL().
[email protected]ce85f602009-11-07 01:34:53280void AppendFormattedHost(const GURL& url,
[email protected]04866c42011-05-03 20:03:50281 const std::string& languages,
282 string16* output);
[email protected]f9fe8632009-05-22 18:15:24283
[email protected]ce85f602009-11-07 01:34:53284// Creates a string representation of |url|. The IDN host name may be in Unicode
[email protected]69c579e2010-04-23 20:01:00285// if |languages| accepts the Unicode representation. |format_type| is a bitmask
286// of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean
287// the URL for human readability. You will generally want |UnescapeRule::SPACES|
288// for display to the user if you can handle spaces, or |UnescapeRule::NORMAL|
289// if not. If the path part and the query part seem to be encoded in %-encoded
290// UTF-8, decodes %-encoding and UTF-8.
[email protected]ce85f602009-11-07 01:34:53291//
292// The last three parameters may be NULL.
293// |new_parsed| will be set to the parsing parameters of the resultant URL.
[email protected]a23de8572009-06-03 02:16:32294// |prefix_end| will be the length before the hostname of the resultant URL.
[email protected]421de2ab2011-04-13 18:43:05295//
296// (|offset[s]_for_adjustment|) specifies one or more offsets into the original
297// |url|'s spec(); each offset will be modified to reflect changes this function
298// makes to the output string. For example, if |url| is "http://a:[email protected]/",
299// |omit_username_password| is true, and an offset is 12 (the offset of '.'),
300// then on return the output string will be "http://c.com/" and the offset will
301// be 8. If an offset cannot be successfully adjusted (e.g. because it points
[email protected]ce85f602009-11-07 01:34:53302// into the middle of a component that was entirely removed, past the end of the
303// string, or into the middle of an encoding sequence), it will be set to
[email protected]9f284f132010-08-31 06:14:17304// string16::npos.
[email protected]13677b82011-05-18 18:29:36305NET_API string16 FormatUrl(const GURL& url,
306 const std::string& languages,
307 FormatUrlTypes format_types,
308 UnescapeRule::Type unescape_rules,
309 url_parse::Parsed* new_parsed,
310 size_t* prefix_end,
311 size_t* offset_for_adjustment);
312NET_API string16 FormatUrlWithOffsets(
313 const GURL& url,
314 const std::string& languages,
315 FormatUrlTypes format_types,
316 UnescapeRule::Type unescape_rules,
317 url_parse::Parsed* new_parsed,
318 size_t* prefix_end,
319 std::vector<size_t>* offsets_for_adjustment);
[email protected]f9fe8632009-05-22 18:15:24320
[email protected]79845ef2010-06-02 02:37:40321// This is a convenience function for FormatUrl() with
322// format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical
323// set of flags for "URLs to display to the user". You should be cautious about
324// using this for URLs which will be parsed or sent to other applications.
[email protected]1f659d972010-08-30 04:49:56325inline string16 FormatUrl(const GURL& url, const std::string& languages) {
326 return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES,
327 NULL, NULL, NULL);
328}
[email protected]f9fe8632009-05-22 18:15:24329
[email protected]79845ef2010-06-02 02:37:40330// Returns whether FormatUrl() would strip a trailing slash from |url|, given a
331// format flag including kFormatUrlOmitTrailingSlashOnBareHostname.
332bool CanStripTrailingSlash(const GURL& url);
333
[email protected]7ec7c182009-08-04 02:33:58334// Strip the portions of |url| that aren't core to the network request.
335// - user name / password
336// - reference section
[email protected]13677b82011-05-18 18:29:36337NET_TEST GURL SimplifyUrlForRequest(const GURL& url);
[email protected]7ec7c182009-08-04 02:33:58338
[email protected]13677b82011-05-18 18:29:36339NET_API void SetExplicitlyAllowedPorts(const std::string& allowed_ports);
[email protected]d95fa182009-09-09 17:01:16340
[email protected]13677b82011-05-18 18:29:36341class NET_API ScopedPortException {
[email protected]bfba3652010-12-10 14:43:00342 public:
343 ScopedPortException(int port);
344 ~ScopedPortException();
345
346 private:
347 int port_;
348
349 DISALLOW_COPY_AND_ASSIGN(ScopedPortException);
350};
351
[email protected]32eaa332010-02-08 22:15:54352// Perform a simplistic test to see if IPv6 is supported by trying to create an
353// IPv6 socket.
354// TODO(jar): Make test more in-depth as needed.
355bool IPv6Supported();
356
[email protected]2f3bc65c2010-07-23 17:47:10357// Returns true if it can determine that only loopback addresses are configured.
358// i.e. if only 127.0.0.1 and ::1 are routable.
359bool HaveOnlyLoopbackAddresses();
360
[email protected]54392832010-06-08 23:25:04361// IPAddressNumber is used to represent an IP address's numeric value as an
362// array of bytes, from most significant to least significant. This is the
363// network byte ordering.
364//
365// IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
366typedef std::vector<unsigned char> IPAddressNumber;
367
[email protected]611cc902011-03-19 00:36:39368static const size_t kIPv4AddressSize = 4;
369static const size_t kIPv6AddressSize = 16;
370
[email protected]54392832010-06-08 23:25:04371// Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
372// Returns true on success and fills |ip_number| with the numeric value.
[email protected]13677b82011-05-18 18:29:36373NET_TEST bool ParseIPLiteralToNumber(const std::string& ip_literal,
374 IPAddressNumber* ip_number);
[email protected]54392832010-06-08 23:25:04375
376// Converts an IPv4 address to an IPv4-mapped IPv6 address.
377// For example 192.168.0.1 would be converted to ::ffff:192.168.0.1.
[email protected]13677b82011-05-18 18:29:36378NET_TEST IPAddressNumber ConvertIPv4NumberToIPv6Number(
[email protected]54392832010-06-08 23:25:04379 const IPAddressNumber& ipv4_number);
380
381// Parses an IP block specifier from CIDR notation to an
382// (IP address, prefix length) pair. Returns true on success and fills
383// |*ip_number| with the numeric value of the IP address and sets
384// |*prefix_length_in_bits| with the length of the prefix.
385//
386// CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
387//
388// 10.10.3.1/20
389// a:b:c::/46
390// ::1/128
[email protected]13677b82011-05-18 18:29:36391NET_API bool ParseCIDRBlock(const std::string& cidr_literal,
392 IPAddressNumber* ip_number,
393 size_t* prefix_length_in_bits);
[email protected]54392832010-06-08 23:25:04394
395// Compares an IP address to see if it falls within the specified IP block.
396// Returns true if it does, false otherwise.
397//
398// The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
399// IP address whose |prefix_length_in_bits| most significant bits match
400// |ip_prefix| will be matched.
401//
402// In cases when an IPv4 address is being compared to an IPv6 address prefix
403// and vice versa, the IPv4 addresses will be converted to IPv4-mapped
404// (IPv6) addresses.
[email protected]13677b82011-05-18 18:29:36405NET_TEST bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
406 const IPAddressNumber& ip_prefix,
407 size_t prefix_length_in_bits);
[email protected]54392832010-06-08 23:25:04408
[email protected]0d1f4c42011-05-05 15:27:50409// Makes a copy of |info|. The dynamically-allocated parts are copied as well.
410// If |recursive| is true, chained entries via ai_next are copied too.
411// The copy returned by this function should be freed using
412// FreeCopyOfAddrinfo(), and NOT freeaddrinfo().
413struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info,
414 bool recursive);
415
416// Frees an addrinfo that was created by CreateCopyOfAddrinfo().
417void FreeCopyOfAddrinfo(struct addrinfo* info);
418
[email protected]d1388f42010-06-16 03:14:42419// Returns the port field of the sockaddr in |info|.
[email protected]93e79012011-01-26 04:13:59420const uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info);
421uint16* GetPortFieldFromAddrinfo(struct addrinfo* info);
[email protected]d1388f42010-06-16 03:14:42422
423// Returns the value of |info's| port (in host byte ordering).
[email protected]fe89ea72011-05-12 02:02:40424uint16 GetPortFromAddrinfo(const struct addrinfo* info);
[email protected]d1388f42010-06-16 03:14:42425
[email protected]93e79012011-01-26 04:13:59426// Same except for struct sockaddr.
427const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
428 socklen_t address_len);
[email protected]13677b82011-05-18 18:29:36429NET_TEST int GetPortFromSockaddr(const struct sockaddr* address,
430 socklen_t address_len);
[email protected]93e79012011-01-26 04:13:59431
[email protected]fe89ea72011-05-12 02:02:40432// Sets every addrinfo in the linked list |head| as having a port field of
433// |port|.
[email protected]13677b82011-05-18 18:29:36434NET_TEST void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port);
[email protected]fe89ea72011-05-12 02:02:40435
[email protected]2fd33ee92011-03-25 22:30:21436// Returns true if |host| is one of the names (e.g. "localhost") or IP
437// addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback.
438//
439// Note that this function does not check for IP addresses other than
440// the above, although other IP addresses may point to the local
441// machine.
[email protected]13677b82011-05-18 18:29:36442NET_TEST bool IsLocalhost(const std::string& host);
[email protected]2fd33ee92011-03-25 22:30:21443
[email protected]611cc902011-03-19 00:36:39444// struct that is used by GetNetworkList() to represent a network
445// interface.
[email protected]13677b82011-05-18 18:29:36446struct NET_API NetworkInterface {
[email protected]611cc902011-03-19 00:36:39447 NetworkInterface();
448 NetworkInterface(const std::string& name, const IPAddressNumber& address);
449 ~NetworkInterface();
450
451 std::string name;
452 IPAddressNumber address;
453};
454
[email protected]91ed478b2011-04-21 23:20:07455typedef std::vector<NetworkInterface> NetworkInterfaceList;
[email protected]611cc902011-03-19 00:36:39456
457// Returns list of network interfaces except loopback interface. If an
458// interface has more than one address, a separate entry is added to
459// the list for each address.
460// Can be called only on a thread that allows IO.
[email protected]13677b82011-05-18 18:29:36461NET_API bool GetNetworkList(NetworkInterfaceList* networks);
[email protected]611cc902011-03-19 00:36:39462
[email protected]8ac1a752008-07-31 19:40:37463} // namespace net
initial.commit586acc5fe2008-07-26 22:42:52464
[email protected]de2943352009-10-22 23:06:12465#endif // NET_BASE_NET_UTIL_H_