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