10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2013 The Chromium Authors. All rights reserved.
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Use of this source code is governed by a BSD-style license that can be
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// found in the LICENSE file.
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef URL_URL_CANON_IP_H_
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define URL_URL_CANON_IP_H_
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "base/strings/string16.h"
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "url/url_canon.h"
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "url/url_export.h"
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "url/url_parse.h"
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace url {
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Writes the given IPv4 address to |output|.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongURL_EXPORT void AppendIPv4Address(const unsigned char address[4],
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                  CanonOutput* output);
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Writes the given IPv6 address to |output|.
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongURL_EXPORT void AppendIPv6Address(const unsigned char address[16],
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                  CanonOutput* output);
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Searches the host name for the portions of the IPv4 address. On success,
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// each component will be placed into |components| and it will return true.
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// It will return false if the host can not be separated as an IPv4 address
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// or if there are any non-7-bit characters or other characters that can not
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// be in an IP address. (This is important so we fail as early as possible for
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// common non-IP hostnames.)
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Not all components may exist. If there are only 3 components, for example,
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the last one will have a length of -1 or 0 to indicate it does not exist.
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Note that many platform's inet_addr will ignore everything after a space
34399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// in certain curcumstances if the stuff before the space looks like an IP
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// address. IE6 is included in this. We do NOT handle this case. In many cases,
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the browser's canonicalization will get run before this which converts
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// spaces to %20 (in the case of IE7) or rejects them (in the case of
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Mozilla), so this code path never gets hit. Our host canonicalization will
39399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// notice these spaces and escape them, which will make IP address finding
401d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// fail. This seems like better behavior than stripping after a space.
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongURL_EXPORT bool FindIPv4Components(const char* spec,
421d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling                                   const Component& host,
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                   Component components[4]);
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongURL_EXPORT bool FindIPv4Components(const base::char16* spec,
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                   const Component& host,
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                                   Component components[4]);
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
48399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// Converts an IPv4 address to a 32-bit number (network byte order).
49399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger//
50399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// Possible return values:
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   IPV4    - IPv4 address was successfully parsed.
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   BROKEN  - Input was formatted like an IPv4 address, but overflow occurred
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//             during parsing.
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   NEUTRAL - Input couldn't possibly be interpreted as an IPv4 address.
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//             It might be an IPv6 address, or a hostname.
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
57399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// On success, |num_ipv4_components| will be populated with the number of
58399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// components in the IPv4 address.
59399f7d09e0c45af54b77b4ab9508d6f23759b927Scott EttingerURL_EXPORT CanonHostInfo::Family IPv4AddressToNumber(const char* spec,
60399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     const Component& host,
61399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     unsigned char address[4],
62399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     int* num_ipv4_components);
6379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos HernandezURL_EXPORT CanonHostInfo::Family IPv4AddressToNumber(const base::char16* spec,
64399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     const Component& host,
65399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     unsigned char address[4],
66399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                                     int* num_ipv4_components);
67399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
68399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// Converts an IPv6 address to a 128-bit number (network byte order), returning
69399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// true on success. False means that the input was not a valid IPv6 address.
70399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger//
71399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// NOTE that |host| is expected to be surrounded by square brackets.
72399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger// i.e. "[::1]" rather than "::1".
73399f7d09e0c45af54b77b4ab9508d6f23759b927Scott EttingerURL_EXPORT bool IPv6AddressToNumber(const char* spec,
74399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                    const Component& host,
75399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                    unsigned char address[16]);
76399f7d09e0c45af54b77b4ab9508d6f23759b927Scott EttingerURL_EXPORT bool IPv6AddressToNumber(const base::char16* spec,
77399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                    const Component& host,
78399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger                                    unsigned char address[16]);
79399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
80399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger}  // namespace url
81399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger
82399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger#endif  // URL_URL_CANON_IP_H_
83399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger