1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef URL_URL_CANON_IP_H_
31 #define URL_URL_CANON_IP_H_
33 #include "base/string16.h"
34 #include "url/url_canon.h"
35 #include "url/url_common.h"
36 #include "url/url_parse.h"
40 // Writes the given IPv4 address to |output|.
41 GURL_API
void AppendIPv4Address(const unsigned char address
[4],
44 // Writes the given IPv6 address to |output|.
45 GURL_API
void AppendIPv6Address(const unsigned char address
[16],
48 // Searches the host name for the portions of the IPv4 address. On success,
49 // each component will be placed into |components| and it will return true.
50 // It will return false if the host can not be separated as an IPv4 address
51 // or if there are any non-7-bit characters or other characters that can not
52 // be in an IP address. (This is important so we fail as early as possible for
53 // common non-IP hostnames.)
55 // Not all components may exist. If there are only 3 components, for example,
56 // the last one will have a length of -1 or 0 to indicate it does not exist.
58 // Note that many platform's inet_addr will ignore everything after a space
59 // in certain curcumstances if the stuff before the space looks like an IP
60 // address. IE6 is included in this. We do NOT handle this case. In many cases,
61 // the browser's canonicalization will get run before this which converts
62 // spaces to %20 (in the case of IE7) or rejects them (in the case of
63 // Mozilla), so this code path never gets hit. Our host canonicalization will
64 // notice these spaces and escape them, which will make IP address finding
65 // fail. This seems like better behavior than stripping after a space.
66 GURL_API
bool FindIPv4Components(const char* spec
,
67 const url_parse::Component
& host
,
68 url_parse::Component components
[4]);
69 GURL_API
bool FindIPv4Components(const char16
* spec
,
70 const url_parse::Component
& host
,
71 url_parse::Component components
[4]);
73 // Converts an IPv4 address to a 32-bit number (network byte order).
75 // Possible return values:
76 // IPV4 - IPv4 address was successfully parsed.
77 // BROKEN - Input was formatted like an IPv4 address, but overflow occurred
79 // NEUTRAL - Input couldn't possibly be interpreted as an IPv4 address.
80 // It might be an IPv6 address, or a hostname.
82 // On success, |num_ipv4_components| will be populated with the number of
83 // components in the IPv4 address.
84 GURL_API
CanonHostInfo::Family
IPv4AddressToNumber(
86 const url_parse::Component
& host
,
87 unsigned char address
[4],
88 int* num_ipv4_components
);
89 GURL_API
CanonHostInfo::Family
IPv4AddressToNumber(
91 const url_parse::Component
& host
,
92 unsigned char address
[4],
93 int* num_ipv4_components
);
95 // Converts an IPv6 address to a 128-bit number (network byte order), returning
96 // true on success. False means that the input was not a valid IPv6 address.
98 // NOTE that |host| is expected to be surrounded by square brackets.
99 // i.e. "[::1]" rather than "::1".
100 GURL_API
bool IPv6AddressToNumber(const char* spec
,
101 const url_parse::Component
& host
,
102 unsigned char address
[16]);
103 GURL_API
bool IPv6AddressToNumber(const char16
* spec
,
104 const url_parse::Component
& host
,
105 unsigned char address
[16]);
107 } // namespace url_canon
109 #endif // URL_URL_CANON_IP_H_