Home | History | Annotate | Download | only in base
      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.
      4 
      5 #ifndef NET_BASE_NET_UTIL_H_
      6 #define NET_BASE_NET_UTIL_H_
      7 
      8 #include "build/build_config.h"
      9 
     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
     17 
     18 #include <string>
     19 #include <vector>
     20 
     21 #include "base/basictypes.h"
     22 #include "base/strings/string16.h"
     23 #include "net/base/address_family.h"
     24 #include "net/base/escape.h"
     25 #include "net/base/net_export.h"
     26 #include "net/base/net_log.h"
     27 
     28 class GURL;
     29 
     30 namespace base {
     31 class FilePath;
     32 class Time;
     33 }
     34 
     35 namespace url_canon {
     36 struct CanonHostInfo;
     37 }
     38 
     39 namespace url_parse {
     40 struct Parsed;
     41 }
     42 
     43 namespace net {
     44 
     45 // Used by FormatUrl to specify handling of certain parts of the url.
     46 typedef uint32 FormatUrlType;
     47 typedef uint32 FormatUrlTypes;
     48 
     49 // IPAddressNumber is used to represent an IP address's numeric value as an
     50 // array of bytes, from most significant to least significant. This is the
     51 // network byte ordering.
     52 //
     53 // IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
     54 typedef std::vector<unsigned char> IPAddressNumber;
     55 typedef std::vector<IPAddressNumber> IPAddressList;
     56 
     57 static const size_t kIPv4AddressSize = 4;
     58 static const size_t kIPv6AddressSize = 16;
     59 
     60 // Nothing is ommitted.
     61 NET_EXPORT extern const FormatUrlType kFormatUrlOmitNothing;
     62 
     63 // If set, any username and password are removed.
     64 NET_EXPORT extern const FormatUrlType kFormatUrlOmitUsernamePassword;
     65 
     66 // If the scheme is 'http://', it's removed.
     67 NET_EXPORT extern const FormatUrlType kFormatUrlOmitHTTP;
     68 
     69 // Omits the path if it is just a slash and there is no query or ref.  This is
     70 // meaningful for non-file "standard" URLs.
     71 NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname;
     72 
     73 // Convenience for omitting all unecessary types.
     74 NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll;
     75 
     76 // Returns the number of explicitly allowed ports; for testing.
     77 NET_EXPORT_PRIVATE extern size_t GetCountOfExplicitlyAllowedPorts();
     78 
     79 // Given the full path to a file name, creates a file: URL. The returned URL
     80 // may not be valid if the input is malformed.
     81 NET_EXPORT GURL FilePathToFileURL(const base::FilePath& path);
     82 
     83 // Converts a file: URL back to a filename that can be passed to the OS. The
     84 // file URL must be well-formed (GURL::is_valid() must return true); we don't
     85 // handle degenerate cases here. Returns true on success, false if it isn't a
     86 // valid file URL. On failure, *file_path will be empty.
     87 NET_EXPORT bool FileURLToFilePath(const GURL& url, base::FilePath* file_path);
     88 
     89 // Splits an input of the form <host>[":"<port>] into its consitituent parts.
     90 // Saves the result into |*host| and |*port|. If the input did not have
     91 // the optional port, sets |*port| to -1.
     92 // Returns true if the parsing was successful, false otherwise.
     93 // The returned host is NOT canonicalized, and may be invalid. If <host> is
     94 // an IPv6 literal address, the returned host includes the square brackets.
     95 NET_EXPORT bool ParseHostAndPort(
     96     std::string::const_iterator host_and_port_begin,
     97     std::string::const_iterator host_and_port_end,
     98     std::string* host,
     99     int* port);
    100 NET_EXPORT bool ParseHostAndPort(
    101     const std::string& host_and_port,
    102     std::string* host,
    103     int* port);
    104 
    105 // Returns a host:port string for the given URL.
    106 NET_EXPORT std::string GetHostAndPort(const GURL& url);
    107 
    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.
    110 NET_EXPORT_PRIVATE std::string GetHostAndOptionalPort(const GURL& url);
    111 
    112 // Returns true if |hostname| contains a non-registerable or non-assignable
    113 // domain name (eg: a gTLD that has not been assigned by IANA) or an IP address
    114 // that falls in an IANA-reserved range.
    115 NET_EXPORT bool IsHostnameNonUnique(const std::string& hostname);
    116 
    117 // Returns true if an IP address hostname is in a range reserved by the IANA.
    118 // Works with both IPv4 and IPv6 addresses, and only compares against a given
    119 // protocols's reserved ranges.
    120 NET_EXPORT bool IsIPAddressReserved(const IPAddressNumber& address);
    121 
    122 // Convenience struct for when you need a |struct sockaddr|.
    123 struct SockaddrStorage {
    124   SockaddrStorage() : addr_len(sizeof(addr_storage)),
    125                       addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) {}
    126   struct sockaddr_storage addr_storage;
    127   socklen_t addr_len;
    128   struct sockaddr* const addr;
    129 };
    130 
    131 // Extracts the IP address and port portions of a sockaddr. |port| is optional,
    132 // and will not be filled in if NULL.
    133 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr,
    134                               socklen_t sock_addr_len,
    135                               const unsigned char** address,
    136                               size_t* address_len,
    137                               uint16* port);
    138 
    139 // Returns the string representation of an IP address.
    140 // For example: "192.168.0.1" or "::1".
    141 NET_EXPORT std::string IPAddressToString(const uint8* address,
    142                                          size_t address_len);
    143 
    144 // Returns the string representation of an IP address along with its port.
    145 // For example: "192.168.0.1:99" or "[::1]:80".
    146 NET_EXPORT std::string IPAddressToStringWithPort(const uint8* address,
    147                                                  size_t address_len,
    148                                                  uint16 port);
    149 
    150 // Same as IPAddressToString() but for a sockaddr. This output will not include
    151 // the IPv6 scope ID.
    152 NET_EXPORT std::string NetAddressToString(const struct sockaddr* sa,
    153                                           socklen_t sock_addr_len);
    154 
    155 // Same as IPAddressToStringWithPort() but for a sockaddr. This output will not
    156 // include the IPv6 scope ID.
    157 NET_EXPORT std::string NetAddressToStringWithPort(const struct sockaddr* sa,
    158                                                   socklen_t sock_addr_len);
    159 
    160 // Same as IPAddressToString() but for an IPAddressNumber.
    161 NET_EXPORT std::string IPAddressToString(const IPAddressNumber& addr);
    162 
    163 // Same as IPAddressToStringWithPort() but for an IPAddressNumber.
    164 NET_EXPORT std::string IPAddressToStringWithPort(
    165     const IPAddressNumber& addr, uint16 port);
    166 
    167 // Returns the address as a sequence of bytes in network-byte-order.
    168 NET_EXPORT std::string IPAddressToPackedString(const IPAddressNumber& addr);
    169 
    170 // Returns the hostname of the current system. Returns empty string on failure.
    171 NET_EXPORT std::string GetHostName();
    172 
    173 // Extracts the unescaped username/password from |url|, saving the results
    174 // into |*username| and |*password|.
    175 NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
    176                                            base::string16* username,
    177                                            base::string16* password);
    178 
    179 // Returns either the host from |url|, or, if the host is empty, the full spec.
    180 NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
    181 
    182 // Return the value of the HTTP response header with name 'name'.  'headers'
    183 // should be in the format that URLRequest::GetResponseHeaders() returns.
    184 // Returns the empty string if the header is not found.
    185 NET_EXPORT std::string GetSpecificHeader(const std::string& headers,
    186                                          const std::string& name);
    187 
    188 // Converts the given host name to unicode characters. This can be called for
    189 // any host name, if the input is not IDN or is invalid in some way, we'll just
    190 // return the ASCII source so it is still usable.
    191 //
    192 // The input should be the canonicalized ASCII host name from GURL. This
    193 // function does NOT accept UTF-8!
    194 //
    195 // |languages| is a comma separated list of ISO 639 language codes. It
    196 // is used to determine whether a hostname is 'comprehensible' to a user
    197 // who understands languages listed. |host| will be converted to a
    198 // human-readable form (Unicode) ONLY when each component of |host| is
    199 // regarded as 'comprehensible'. Scipt-mixing is not allowed except that
    200 // Latin letters in the ASCII range can be mixed with a limited set of
    201 // script-language pairs (currently Han, Kana and Hangul for zh,ja and ko).
    202 // When |languages| is empty, even that mixing is not allowed.
    203 NET_EXPORT base::string16 IDNToUnicode(const std::string& host,
    204                                        const std::string& languages);
    205 
    206 // Canonicalizes |host| and returns it.  Also fills |host_info| with
    207 // IP address information.  |host_info| must not be NULL.
    208 NET_EXPORT std::string CanonicalizeHost(const std::string& host,
    209                                         url_canon::CanonHostInfo* host_info);
    210 
    211 // Returns true if |host| is not an IP address and is compliant with a set of
    212 // rules based on RFC 1738 and tweaked to be compatible with the real world.
    213 // The rules are:
    214 //   * One or more components separated by '.'
    215 //   * Each component begins with an alphanumeric character or '-'
    216 //   * Each component contains only alphanumeric characters and '-' or '_'
    217 //   * Each component ends with an alphanumeric character or '-'
    218 //   * The last component begins with an alphanumeric character
    219 //   * Optional trailing dot after last component (means "treat as FQDN")
    220 // If |desired_tld| is non-NULL, the host will only be considered invalid if
    221 // appending it as a trailing component still results in an invalid host.  This
    222 // helps us avoid marking as "invalid" user attempts to open, say, "www.-9.com"
    223 // by typing -, 9, <ctrl>+<enter>.
    224 //
    225 // NOTE: You should only pass in hosts that have been returned from
    226 // CanonicalizeHost(), or you may not get accurate results.
    227 NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host,
    228                                              const std::string& desired_tld);
    229 
    230 // Call these functions to get the html snippet for a directory listing.
    231 // The return values of both functions are in UTF-8.
    232 NET_EXPORT std::string GetDirectoryListingHeader(const base::string16& title);
    233 
    234 // Given the name of a file in a directory (ftp or local) and
    235 // other information (is_dir, size, modification time), it returns
    236 // the html snippet to add the entry for the file to the directory listing.
    237 // Currently, it's a script tag containing a call to a Javascript function
    238 // |addRow|.
    239 //
    240 // |name| is the file name to be displayed. |raw_bytes| will be used
    241 // as the actual target of the link (so for example, ftp links should use
    242 // server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name|
    243 // will be used.
    244 //
    245 // Both |name| and |raw_bytes| are escaped internally.
    246 NET_EXPORT std::string GetDirectoryListingEntry(const base::string16& name,
    247                                                 const std::string& raw_bytes,
    248                                                 bool is_dir, int64 size,
    249                                                 base::Time modified);
    250 
    251 // If text starts with "www." it is removed, otherwise text is returned
    252 // unmodified.
    253 NET_EXPORT base::string16 StripWWW(const base::string16& text);
    254 
    255 // Runs |url|'s host through StripWWW().  |url| must be valid.
    256 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url);
    257 
    258 // Generates a filename using the first successful method from the following (in
    259 // order):
    260 //
    261 // 1) The raw Content-Disposition header in |content_disposition| as read from
    262 //    the network.  |referrer_charset| is used to decode non-ASCII strings.
    263 // 2) |suggested_name| if specified.  |suggested_name| is assumed to be in
    264 //    UTF-8.
    265 // 3) The filename extracted from the |url|.  |referrer_charset| will be used to
    266 //    interpret the URL if there are non-ascii characters.
    267 // 4) |default_name|.  If non-empty, |default_name| is assumed to be a filename
    268 //    and shouldn't contain a path.  |default_name| is not subject to validation
    269 //    or sanitization, and therefore shouldn't be a user supplied string.
    270 // 5) The hostname portion from the |url|
    271 //
    272 // Then, leading and trailing '.'s will be removed.  On Windows, trailing spaces
    273 // are also removed.  The string "download" is the final fallback if no filename
    274 // is found or the filename is empty.
    275 //
    276 // Any illegal characters in the filename will be replaced by '-'.  If the
    277 // filename doesn't contain an extension, and a |mime_type| is specified, the
    278 // preferred extension for the |mime_type| will be appended to the filename.
    279 // The resulting filename is then checked against a list of reserved names on
    280 // Windows.  If the name is reserved, an underscore will be prepended to the
    281 // filename.
    282 //
    283 // Note: |mime_type| should only be specified if this function is called from a
    284 // thread that allows IO.
    285 NET_EXPORT base::string16 GetSuggestedFilename(
    286     const GURL& url,
    287     const std::string& content_disposition,
    288     const std::string& referrer_charset,
    289     const std::string& suggested_name,
    290     const std::string& mime_type,
    291     const std::string& default_name);
    292 
    293 // Similar to GetSuggestedFilename(), but returns a FilePath.
    294 NET_EXPORT base::FilePath GenerateFileName(
    295     const GURL& url,
    296     const std::string& content_disposition,
    297     const std::string& referrer_charset,
    298     const std::string& suggested_name,
    299     const std::string& mime_type,
    300     const std::string& default_name);
    301 
    302 // Valid components:
    303 // * are not empty
    304 // * are not Windows reserved names (CON, NUL.zip, etc.)
    305 // * do not have trailing separators
    306 // * do not equal kCurrentDirectory
    307 // * do not reference the parent directory
    308 // * do not contain illegal characters
    309 // * do not end with Windows shell-integrated extensions (even on posix)
    310 // * do not begin with '.' (which would hide them in most file managers)
    311 // * do not end with ' ' or '.'
    312 NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component);
    313 
    314 // Basenames of valid relative paths are IsSafePortableBasename(), and internal
    315 // path components of valid relative paths are valid path components as
    316 // described above IsSafePortableBasename(). Valid relative paths are not
    317 // absolute paths.
    318 NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path);
    319 
    320 // Ensures that the filename and extension is safe to use in the filesystem.
    321 //
    322 // Assumes that |file_path| already contains a valid path or file name.  On
    323 // Windows if the extension causes the file to have an unsafe interaction with
    324 // the shell (see net_util::IsShellIntegratedExtension()), then it will be
    325 // replaced by the string 'download'.  If |file_path| doesn't contain an
    326 // extension or |ignore_extension| is true then the preferred extension, if one
    327 // exists, for |mime_type| will be used as the extension.
    328 //
    329 // On Windows, the filename will be checked against a set of reserved names, and
    330 // if so, an underscore will be prepended to the name.
    331 //
    332 // |file_name| can either be just the file name or it can be a full path to a
    333 // file.
    334 //
    335 // Note: |mime_type| should only be non-empty if this function is called from a
    336 // thread that allows IO.
    337 NET_EXPORT void GenerateSafeFileName(const std::string& mime_type,
    338                                      bool ignore_extension,
    339                                      base::FilePath* file_path);
    340 
    341 // Checks |port| against a list of ports which are restricted by default.
    342 // Returns true if |port| is allowed, false if it is restricted.
    343 NET_EXPORT bool IsPortAllowedByDefault(int port);
    344 
    345 // Checks |port| against a list of ports which are restricted by the FTP
    346 // protocol.  Returns true if |port| is allowed, false if it is restricted.
    347 NET_EXPORT_PRIVATE bool IsPortAllowedByFtp(int port);
    348 
    349 // Check if banned |port| has been overriden by an entry in
    350 // |explicitly_allowed_ports_|.
    351 NET_EXPORT_PRIVATE bool IsPortAllowedByOverride(int port);
    352 
    353 // Set socket to non-blocking mode
    354 NET_EXPORT int SetNonBlocking(int fd);
    355 
    356 // Formats the host in |url| and appends it to |output|.  The host formatter
    357 // takes the same accept languages component as ElideURL().
    358 NET_EXPORT void AppendFormattedHost(const GURL& url,
    359                                     const std::string& languages,
    360                                     base::string16* output);
    361 
    362 // Creates a string representation of |url|. The IDN host name may be in Unicode
    363 // if |languages| accepts the Unicode representation. |format_type| is a bitmask
    364 // of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean
    365 // the URL for human readability. You will generally want |UnescapeRule::SPACES|
    366 // for display to the user if you can handle spaces, or |UnescapeRule::NORMAL|
    367 // if not. If the path part and the query part seem to be encoded in %-encoded
    368 // UTF-8, decodes %-encoding and UTF-8.
    369 //
    370 // The last three parameters may be NULL.
    371 //
    372 // |new_parsed| will be set to the parsing parameters of the resultant URL.
    373 //
    374 // |prefix_end| will be the length before the hostname of the resultant URL.
    375 //
    376 // |offset[s]_for_adjustment| specifies one or more offsets into the original
    377 // URL, representing insertion or selection points between characters: if the
    378 // input is "http://foo.com/", offset 0 is before the entire URL, offset 7 is
    379 // between the scheme and the host, and offset 15 is after the end of the URL.
    380 // Valid input offsets range from 0 to the length of the input URL string.  On
    381 // exit, each offset will have been modified to reflect any changes made to the
    382 // output string.  For example, if |url| is "http://a:b@c.com/",
    383 // |omit_username_password| is true, and an offset is 12 (pointing between 'c'
    384 // and '.'), then on return the output string will be "http://c.com/" and the
    385 // offset will be 8.  If an offset cannot be successfully adjusted (e.g. because
    386 // it points into the middle of a component that was entirely removed or into
    387 // the middle of an encoding sequence), it will be set to base::string16::npos.
    388 // For consistency, if an input offset points between the scheme and the
    389 // username/password, and both are removed, on output this offset will be 0
    390 // rather than npos; this means that offsets at the starts and ends of removed
    391 // components are always transformed the same way regardless of what other
    392 // components are adjacent.
    393 NET_EXPORT base::string16 FormatUrl(const GURL& url,
    394                                     const std::string& languages,
    395                                     FormatUrlTypes format_types,
    396                                     UnescapeRule::Type unescape_rules,
    397                                     url_parse::Parsed* new_parsed,
    398                                     size_t* prefix_end,
    399                                     size_t* offset_for_adjustment);
    400 NET_EXPORT base::string16 FormatUrlWithOffsets(
    401     const GURL& url,
    402     const std::string& languages,
    403     FormatUrlTypes format_types,
    404     UnescapeRule::Type unescape_rules,
    405     url_parse::Parsed* new_parsed,
    406     size_t* prefix_end,
    407     std::vector<size_t>* offsets_for_adjustment);
    408 
    409 // This is a convenience function for FormatUrl() with
    410 // format_types = kFormatUrlOmitAll and unescape = SPACES.  This is the typical
    411 // set of flags for "URLs to display to the user".  You should be cautious about
    412 // using this for URLs which will be parsed or sent to other applications.
    413 inline base::string16 FormatUrl(const GURL& url, const std::string& languages) {
    414   return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES,
    415                    NULL, NULL, NULL);
    416 }
    417 
    418 // Returns whether FormatUrl() would strip a trailing slash from |url|, given a
    419 // format flag including kFormatUrlOmitTrailingSlashOnBareHostname.
    420 NET_EXPORT bool CanStripTrailingSlash(const GURL& url);
    421 
    422 // Strip the portions of |url| that aren't core to the network request.
    423 //   - user name / password
    424 //   - reference section
    425 NET_EXPORT_PRIVATE GURL SimplifyUrlForRequest(const GURL& url);
    426 
    427 NET_EXPORT void SetExplicitlyAllowedPorts(const std::string& allowed_ports);
    428 
    429 class NET_EXPORT ScopedPortException {
    430  public:
    431   explicit ScopedPortException(int port);
    432   ~ScopedPortException();
    433 
    434  private:
    435   int port_;
    436 
    437   DISALLOW_COPY_AND_ASSIGN(ScopedPortException);
    438 };
    439 
    440 // Returns true if it can determine that only loopback addresses are configured.
    441 // i.e. if only 127.0.0.1 and ::1 are routable.
    442 // Also returns false if it cannot determine this.
    443 bool HaveOnlyLoopbackAddresses();
    444 
    445 // Returns AddressFamily of the address.
    446 NET_EXPORT_PRIVATE AddressFamily GetAddressFamily(
    447     const IPAddressNumber& address);
    448 
    449 // Maps the given AddressFamily to either AF_INET, AF_INET6 or AF_UNSPEC.
    450 NET_EXPORT_PRIVATE int ConvertAddressFamily(AddressFamily address_family);
    451 
    452 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
    453 // Returns true on success and fills |ip_number| with the numeric value.
    454 NET_EXPORT_PRIVATE bool ParseIPLiteralToNumber(const std::string& ip_literal,
    455                                                IPAddressNumber* ip_number);
    456 
    457 // Converts an IPv4 address to an IPv4-mapped IPv6 address.
    458 // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1.
    459 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4NumberToIPv6Number(
    460     const IPAddressNumber& ipv4_number);
    461 
    462 // Returns true iff |address| is an IPv4-mapped IPv6 address.
    463 NET_EXPORT_PRIVATE bool IsIPv4Mapped(const IPAddressNumber& address);
    464 
    465 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called
    466 // on IPv4-mapped IPv6 addresses.
    467 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4MappedToIPv4(
    468     const IPAddressNumber& address);
    469 
    470 // Parses an IP block specifier from CIDR notation to an
    471 // (IP address, prefix length) pair. Returns true on success and fills
    472 // |*ip_number| with the numeric value of the IP address and sets
    473 // |*prefix_length_in_bits| with the length of the prefix.
    474 //
    475 // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
    476 //
    477 //    10.10.3.1/20
    478 //    a:b:c::/46
    479 //    ::1/128
    480 NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal,
    481                                IPAddressNumber* ip_number,
    482                                size_t* prefix_length_in_bits);
    483 
    484 // Compares an IP address to see if it falls within the specified IP block.
    485 // Returns true if it does, false otherwise.
    486 //
    487 // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
    488 // IP address whose |prefix_length_in_bits| most significant bits match
    489 // |ip_prefix| will be matched.
    490 //
    491 // In cases when an IPv4 address is being compared to an IPv6 address prefix
    492 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped
    493 // (IPv6) addresses.
    494 NET_EXPORT_PRIVATE bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
    495                                               const IPAddressNumber& ip_prefix,
    496                                               size_t prefix_length_in_bits);
    497 
    498 // Retuns the port field of the |sockaddr|.
    499 const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address,
    500                                        socklen_t address_len);
    501 // Returns the value of port in |sockaddr| (in host byte ordering).
    502 NET_EXPORT_PRIVATE int GetPortFromSockaddr(const struct sockaddr* address,
    503                                            socklen_t address_len);
    504 
    505 // Returns true if |host| is one of the names (e.g. "localhost") or IP
    506 // addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback.
    507 //
    508 // Note that this function does not check for IP addresses other than
    509 // the above, although other IP addresses may point to the local
    510 // machine.
    511 NET_EXPORT_PRIVATE bool IsLocalhost(const std::string& host);
    512 
    513 // struct that is used by GetNetworkList() to represent a network
    514 // interface.
    515 struct NET_EXPORT NetworkInterface {
    516   NetworkInterface();
    517   NetworkInterface(const std::string& name,
    518                    uint32 interface_index,
    519                    const IPAddressNumber& address,
    520                    size_t network_prefix);
    521   ~NetworkInterface();
    522 
    523   std::string name;
    524   uint32 interface_index;  // Always 0 on Android.
    525   IPAddressNumber address;
    526   size_t network_prefix;
    527 };
    528 
    529 typedef std::vector<NetworkInterface> NetworkInterfaceList;
    530 
    531 // Returns list of network interfaces except loopback interface. If an
    532 // interface has more than one address, a separate entry is added to
    533 // the list for each address.
    534 // Can be called only on a thread that allows IO.
    535 NET_EXPORT bool GetNetworkList(NetworkInterfaceList* networks);
    536 
    537 // General category of the IEEE 802.11 (wifi) physical layer operating mode.
    538 enum WifiPHYLayerProtocol {
    539   // No wifi support or no associated AP.
    540   WIFI_PHY_LAYER_PROTOCOL_NONE,
    541   // An obsolete modes introduced by the original 802.11, e.g. IR, FHSS,
    542   WIFI_PHY_LAYER_PROTOCOL_ANCIENT,
    543   // 802.11a, OFDM-based rates.
    544   WIFI_PHY_LAYER_PROTOCOL_A,
    545   // 802.11b, DSSS or HR DSSS.
    546   WIFI_PHY_LAYER_PROTOCOL_B,
    547   // 802.11g, same rates as 802.11a but compatible with 802.11b.
    548   WIFI_PHY_LAYER_PROTOCOL_G,
    549   // 802.11n, HT rates.
    550   WIFI_PHY_LAYER_PROTOCOL_N,
    551   // Unclassified mode or failure to identify.
    552   WIFI_PHY_LAYER_PROTOCOL_UNKNOWN
    553 };
    554 
    555 // Characterize the PHY mode of the currently associated access point.
    556 // Currently only available on OS_WIN.
    557 NET_EXPORT WifiPHYLayerProtocol GetWifiPHYLayerProtocol();
    558 
    559 // Returns number of matching initial bits between the addresses |a1| and |a2|.
    560 unsigned CommonPrefixLength(const IPAddressNumber& a1,
    561                             const IPAddressNumber& a2);
    562 
    563 // Computes the number of leading 1-bits in |mask|.
    564 unsigned MaskPrefixLength(const IPAddressNumber& mask);
    565 
    566 // Differentiated Services Code Point.
    567 // See http://tools.ietf.org/html/rfc2474 for details.
    568 enum DiffServCodePoint {
    569   DSCP_NO_CHANGE = -1,
    570   DSCP_DEFAULT = 0,  // Same as DSCP_CS0
    571   DSCP_CS0  = 0,   // The default
    572   DSCP_CS1  = 8,   // Bulk/background traffic
    573   DSCP_AF11 = 10,
    574   DSCP_AF12 = 12,
    575   DSCP_AF13 = 14,
    576   DSCP_CS2  = 16,
    577   DSCP_AF21 = 18,
    578   DSCP_AF22 = 20,
    579   DSCP_AF23 = 22,
    580   DSCP_CS3  = 24,
    581   DSCP_AF31 = 26,
    582   DSCP_AF32 = 28,
    583   DSCP_AF33 = 30,
    584   DSCP_CS4  = 32,
    585   DSCP_AF41 = 34,  // Video
    586   DSCP_AF42 = 36,  // Video
    587   DSCP_AF43 = 38,  // Video
    588   DSCP_CS5  = 40,  // Video
    589   DSCP_EF   = 46,  // Voice
    590   DSCP_CS6  = 48,  // Voice
    591   DSCP_CS7  = 56,  // Control messages
    592 };
    593 
    594 }  // namespace net
    595 
    596 #endif  // NET_BASE_NET_UTIL_H_
    597