• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
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 Component& host,
43                                    Component components[4]);
44 URL_EXPORT bool FindIPv4Components(const base::char16* spec,
45                                    const Component& host,
46                                    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(const char* spec,
60                                                      const Component& host,
61                                                      unsigned char address[4],
62                                                      int* num_ipv4_components);
63 URL_EXPORT CanonHostInfo::Family IPv4AddressToNumber(const base::char16* spec,
64                                                      const Component& host,
65                                                      unsigned char address[4],
66                                                      int* num_ipv4_components);
67 
68 // Converts an IPv6 address to a 128-bit number (network byte order), returning
69 // true on success. False means that the input was not a valid IPv6 address.
70 //
71 // NOTE that |host| is expected to be surrounded by square brackets.
72 // i.e. "[::1]" rather than "::1".
73 URL_EXPORT bool IPv6AddressToNumber(const char* spec,
74                                     const Component& host,
75                                     unsigned char address[16]);
76 URL_EXPORT bool IPv6AddressToNumber(const base::char16* spec,
77                                     const Component& host,
78                                     unsigned char address[16]);
79 
80 }  // namespace url
81 
82 #endif  // URL_URL_CANON_IP_H_
83