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