Home | History | Annotate | Download | only in url
      1 // Copyright 2013 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 URL_URL_CANON_IP_H_
      6 #define URL_URL_CANON_IP_H_
      7 
      8 #include "base/strings/string16.h"
      9 #include "url/url_canon.h"
     10 #include "url/url_export.h"
     11 #include "url/url_parse.h"
     12 
     13 namespace url_canon {
     14 
     15 // Writes the given IPv4 address to |output|.
     16 URL_EXPORT void AppendIPv4Address(const unsigned char address[4],
     17                                   CanonOutput* output);
     18 
     19 // Writes the given IPv6 address to |output|.
     20 URL_EXPORT void AppendIPv6Address(const unsigned char address[16],
     21                                   CanonOutput* output);
     22 
     23 // Searches the host name for the portions of the IPv4 address. On success,
     24 // each component will be placed into |components| and it will return true.
     25 // It will return false if the host can not be separated as an IPv4 address
     26 // or if there are any non-7-bit characters or other characters that can not
     27 // be in an IP address. (This is important so we fail as early as possible for
     28 // common non-IP hostnames.)
     29 //
     30 // Not all components may exist. If there are only 3 components, for example,
     31 // the last one will have a length of -1 or 0 to indicate it does not exist.
     32 //
     33 // Note that many platform's inet_addr will ignore everything after a space
     34 // in certain curcumstances if the stuff before the space looks like an IP
     35 // address. IE6 is included in this. We do NOT handle this case. In many cases,
     36 // the browser's canonicalization will get run before this which converts
     37 // spaces to %20 (in the case of IE7) or rejects them (in the case of
     38 // Mozilla), so this code path never gets hit. Our host canonicalization will
     39 // notice these spaces and escape them, which will make IP address finding
     40 // fail. This seems like better behavior than stripping after a space.
     41 URL_EXPORT bool FindIPv4Components(const char* spec,
     42                                    const url_parse::Component& host,
     43                                    url_parse::Component components[4]);
     44 URL_EXPORT bool FindIPv4Components(const base::char16* spec,
     45                                    const url_parse::Component& host,
     46                                    url_parse::Component components[4]);
     47 
     48 // Converts an IPv4 address to a 32-bit number (network byte order).
     49 //
     50 // Possible return values:
     51 //   IPV4    - IPv4 address was successfully parsed.
     52 //   BROKEN  - Input was formatted like an IPv4 address, but overflow occurred
     53 //             during parsing.
     54 //   NEUTRAL - Input couldn't possibly be interpreted as an IPv4 address.
     55 //             It might be an IPv6 address, or a hostname.
     56 //
     57 // On success, |num_ipv4_components| will be populated with the number of
     58 // components in the IPv4 address.
     59 URL_EXPORT CanonHostInfo::Family IPv4AddressToNumber(
     60     const char* spec,
     61     const url_parse::Component& host,
     62     unsigned char address[4],
     63     int* num_ipv4_components);
     64 URL_EXPORT CanonHostInfo::Family IPv4AddressToNumber(
     65     const base::char16* spec,
     66     const url_parse::Component& host,
     67     unsigned char address[4],
     68     int* num_ipv4_components);
     69 
     70 // Converts an IPv6 address to a 128-bit number (network byte order), returning
     71 // true on success. False means that the input was not a valid IPv6 address.
     72 //
     73 // NOTE that |host| is expected to be surrounded by square brackets.
     74 // i.e. "[::1]" rather than "::1".
     75 URL_EXPORT bool IPv6AddressToNumber(const char* spec,
     76                                     const url_parse::Component& host,
     77                                     unsigned char address[16]);
     78 URL_EXPORT bool IPv6AddressToNumber(const base::char16* spec,
     79                                     const url_parse::Component& host,
     80                                     unsigned char address[16]);
     81 
     82 }  // namespace url_canon
     83 
     84 #endif  // URL_URL_CANON_IP_H_
     85