1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_BASE_NET_UTIL_H_ 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_BASE_NET_UTIL_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 1172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#if defined(OS_WIN) 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h> 1372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <ws2tcpip.h> 1472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#elif defined(OS_POSIX) 1572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <sys/socket.h> 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include <list> 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string> 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <set> 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector> 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h" 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string16.h" 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/base/escape.h" 262557749644f9d25af9721533322db19197c49b49Kristian Monsen#include "net/base/net_export.h" 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct addrinfo; 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass FilePath; 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass GURL; 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace base { 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass Time; 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace url_canon { 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct CanonHostInfo; 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace url_parse { 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct Parsed; 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace net { 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Used by FormatUrl to specify handling of certain parts of the url. 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtypedef uint32 FormatUrlType; 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtypedef uint32 FormatUrlTypes; 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 50513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Used by GetHeaderParamValue to determine how to handle quotes in the value. 51513209b27ff55e2841eac0e4120199c23acce758Ben Murdochclass QuoteRule { 52513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch public: 53513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch enum Type { 54513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch KEEP_OUTER_QUOTES, 55513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch REMOVE_OUTER_QUOTES, 56513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch }; 57513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 58513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch private: 59513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch QuoteRule(); 60513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch}; 61513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Nothing is ommitted. 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochextern const FormatUrlType kFormatUrlOmitNothing; 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// If set, any username and password are removed. 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochextern const FormatUrlType kFormatUrlOmitUsernamePassword; 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// If the scheme is 'http://', it's removed. 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochextern const FormatUrlType kFormatUrlOmitHTTP; 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Omits the path if it is just a slash and there is no query or ref. This is 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// meaningful for non-file "standard" URLs. 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochextern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname; 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Convenience for omitting all unecessary types. 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochextern const FormatUrlType kFormatUrlOmitAll; 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Holds a list of ports that should be accepted despite bans. 7921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsenextern std::multiset<int> explicitly_allowed_ports; 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Given the full path to a file name, creates a file: URL. The returned URL 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// may not be valid if the input is malformed. 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottGURL FilePathToFileURL(const FilePath& path); 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts a file: URL back to a filename that can be passed to the OS. The 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// file URL must be well-formed (GURL::is_valid() must return true); we don't 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// handle degenerate cases here. Returns true on success, false if it isn't a 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// valid file URL. On failure, *file_path will be empty. 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool FileURLToFilePath(const GURL& url, FilePath* file_path); 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Splits an input of the form <host>[":"<port>] into its consitituent parts. 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Saves the result into |*host| and |*port|. If the input did not have 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the optional port, sets |*port| to -1. 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the parsing was successful, false otherwise. 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The returned host is NOT canonicalized, and may be invalid. If <host> is 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// an IPv6 literal address, the returned host includes the square brackets. 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ParseHostAndPort(std::string::const_iterator host_and_port_begin, 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string::const_iterator host_and_port_end, 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string* host, 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int* port); 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ParseHostAndPort(const std::string& host_and_port, 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string* host, 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int* port); 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns a host:port string for the given URL. 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetHostAndPort(const GURL& url); 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns a host[:port] string for the given URL, where the port is omitted 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// if it is the default for the URL's scheme. 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetHostAndOptionalPort(const GURL& url); 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the string representation of an address, like "192.168.0.1". 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns empty string on failure. 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string NetAddressToString(const struct addrinfo* net_address); 11572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstd::string NetAddressToString(const struct sockaddr* net_address, 11672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen socklen_t address_len); 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Same as NetAddressToString, but additionally includes the port number. For 119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// example: "192.168.0.1:99" or "[::1]:80". 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstd::string NetAddressToStringWithPort(const struct addrinfo* net_address); 12172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstd::string NetAddressToStringWithPort(const struct sockaddr* net_address, 12272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen socklen_t address_len); 123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the hostname of the current system. Returns empty string on failure. 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetHostName(); 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Extracts the unescaped username/password from |url|, saving the results 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// into |*username| and |*password|. 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid GetIdentityFromURL(const GURL& url, 1303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick string16* username, 1313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick string16* password); 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns either the host from |url|, or, if the host is empty, the full spec. 134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstd::string GetHostOrSpecFromURL(const GURL& url); 135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Return the value of the HTTP response header with name 'name'. 'headers' 137ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// should be in the format that URLRequest::GetResponseHeaders() returns. 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the empty string if the header is not found. 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::wstring GetSpecificHeader(const std::wstring& headers, 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::wstring& name); 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetSpecificHeader(const std::string& headers, 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& name); 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Return the value of the HTTP response header field's parameter named 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 'param_name'. Returns the empty string if the parameter is not found or is 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// improperly formatted. 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::wstring GetHeaderParamValue(const std::wstring& field, 148513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch const std::wstring& param_name, 149513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch QuoteRule::Type quote_rule); 150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetHeaderParamValue(const std::string& field, 151513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch const std::string& param_name, 152513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch QuoteRule::Type quote_rule); 153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Return the filename extracted from Content-Disposition header. The following 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// formats are tried in order listed below: 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 157513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// 1. RFC 5987 158513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// 2. RFC 2047 159513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// 3. Raw-8bit-characters : 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// a. UTF-8, b. referrer_charset, c. default os codepage. 161513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// 4. %-escaped UTF-8. 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 163513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// In step 3, if referrer_charset is empty(i.e. unknown), 3b is skipped. 164513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// In step 4, the fallback charsets tried in step 3 are not tried. We 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// can consider doing that later. 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 167513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// When a param value is ASCII, but is not in format #2 or format #4 above, 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// it is returned as it is unless it's pretty close to two supported 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// formats but not well-formed. In that case, an empty string is returned. 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// In any case, a caller must check for the empty return value and resort to 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// another means to get a filename (e.g. url). 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This function does not do any escaping and callers are responsible for 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit. 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// TODO(jungshik): revisit this issue. At the moment, the only caller 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// net_util::GetSuggestedFilename and it calls ReplaceIllegalCharacters. The 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// other caller is a unit test. Need to figure out expose this function only to 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// net_util_unittest. 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetFileNameFromCD(const std::string& header, 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& referrer_charset); 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts the given host name to unicode characters. This can be called for 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// any host name, if the input is not IDN or is invalid in some way, we'll just 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// return the ASCII source so it is still usable. 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The input should be the canonicalized ASCII host name from GURL. This 190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// function does NOT accept UTF-8! Its length must also be given (this is 191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// designed to work on the substring of the host out of a URL spec). 192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |languages| is a comma separated list of ISO 639 language codes. It 194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is used to determine whether a hostname is 'comprehensible' to a user 195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// who understands languages listed. |host| will be converted to a 196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// human-readable form (Unicode) ONLY when each component of |host| is 197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// regarded as 'comprehensible'. Scipt-mixing is not allowed except that 198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Latin letters in the ASCII range can be mixed with a limited set of 199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// script-language pairs (currently Han, Kana and Hangul for zh,ja and ko). 200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// When |languages| is empty, even that mixing is not allowed. 201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 202ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// (|offset[s]_for_adjustment|) specifies one or more offsets into the original 203ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// |url|'s spec(); each offset will be adjusted to point at the same logical 204ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// place in the result strings during decoding. If this isn't possible because 205ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// an offset points past the end of |host| or into the middle of a punycode 206ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// sequence, the offending offset will be set to std::wstring::npos. 207ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// |offset[s]_for_adjustment| may be NULL. 2082557749644f9d25af9721533322db19197c49b49Kristian MonsenNET_EXPORT std::wstring IDNToUnicode(const char* host, 209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t host_len, 210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::wstring& languages, 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t* offset_for_adjustment); 212ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstd::wstring IDNToUnicodeWithOffsets( 213ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const char* host, 214ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen size_t host_len, 215ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::wstring& languages, 216ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::vector<size_t>* offsets_for_adjustment); 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Canonicalizes |host| and returns it. Also fills |host_info| with 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// IP address information. |host_info| must not be NULL. 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string CanonicalizeHost(const std::string& host, 221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott url_canon::CanonHostInfo* host_info); 222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string CanonicalizeHost(const std::wstring& host, 223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott url_canon::CanonHostInfo* host_info); 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if |host| is not an IP address and is compliant with a set of 226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// rules based on RFC 1738 and tweaked to be compatible with the real world. 227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The rules are: 228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * One or more components separated by '.' 229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Each component begins and ends with an alphanumeric character 230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Each component contains only alphanumeric characters and '-' or '_' 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * The last component does not begin with a digit 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Optional trailing dot after last component (means "treat as FQDN") 233c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// If |desired_tld| is non-NULL, the host will only be considered invalid if 234c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// appending it as a trailing component still results in an invalid host. This 235c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// helps us avoid marking as "invalid" user attempts to open "www.401k.com" by 236c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// typing 4-0-1-k-<ctrl>+<enter>. 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// NOTE: You should only pass in hosts that have been returned from 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// CanonicalizeHost(), or you may not get accurate results. 240c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool IsCanonicalizedHostCompliant(const std::string& host, 241c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const std::string& desired_tld); 242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Call these functions to get the html snippet for a directory listing. 244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The return values of both functions are in UTF-8. 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetDirectoryListingHeader(const string16& title); 246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Given the name of a file in a directory (ftp or local) and 248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// other information (is_dir, size, modification time), it returns 249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the html snippet to add the entry for the file to the directory listing. 250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Currently, it's a script tag containing a call to a Javascript function 251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |addRow|. 252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |name| is the file name to be displayed. |raw_bytes| will be used 254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// as the actual target of the link (so for example, ftp links should use 255c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name| 256c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// will be used. 257c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 258c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Both |name| and |raw_bytes| are escaped internally. 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstd::string GetDirectoryListingEntry(const string16& name, 260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& raw_bytes, 261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool is_dir, int64 size, 262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time modified); 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If text starts with "www." it is removed, otherwise text is returned 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// unmodified. 2663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickstring16 StripWWW(const string16& text); 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Gets the filename from the raw Content-Disposition header (as read from the 269c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// network). Otherwise uses the last path component name or hostname from 270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |url|. If there is no filename or it can't be used, the given |default_name|, 271c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// will be used unless it is empty. 272c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 273c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note: it's possible for the suggested filename to be empty (e.g., 274c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// file:///). referrer_charset is used as one of charsets 275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// to interpret a raw 8bit string in C-D header (after interpreting 276c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// as UTF-8 fails). See the comment for GetFilenameFromCD for more details. 27772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstring16 GetSuggestedFilename(const GURL& url, 278c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& content_disposition, 279c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::string& referrer_charset, 28072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const string16& default_name); 281c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 282c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Checks the given port against a list of ports which are restricted by 283c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// default. Returns true if the port is allowed, false if it is restricted. 284c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool IsPortAllowedByDefault(int port); 285c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 286c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Checks the given port against a list of ports which are restricted by the 287c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// FTP protocol. Returns true if the port is allowed, false if it is 288c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// restricted. 289c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool IsPortAllowedByFtp(int port); 290c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 291c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Check if banned |port| has been overriden by an entry in 292c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |explicitly_allowed_ports_|. 293c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool IsPortAllowedByOverride(int port); 294c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 295c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Set socket to non-blocking mode 296c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint SetNonBlocking(int fd); 297c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 298c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Appends the given part of the original URL to the output string formatted for 299c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the user. The given parsed structure will be updated. The host name formatter 300c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// also takes the same accept languages component as ElideURL. |new_parsed| may 301c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// be null. 302ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// 303ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// (|offset[s]_for_adjustment|) specifies one or more offsets into the original 304ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// |url|'s spec(); each offset will be adjusted to point at the same logical 305ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// place in the result strings after reformatting of the host. If this isn't 306ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// possible because an offset points past the end of the host or into the middle 307ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// of a multi-character sequence, the offending offset will be set to 308ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// std::wstring::npos. |offset[s]_for_adjustment| may be NULL. 309c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid AppendFormattedHost(const GURL& url, 310c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const std::wstring& languages, 311c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::wstring* output, 312c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott url_parse::Parsed* new_parsed, 313c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t* offset_for_adjustment); 314ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid AppendFormattedHostWithOffsets( 315ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const GURL& url, 316ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::wstring& languages, 317ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::wstring* output, 318ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen url_parse::Parsed* new_parsed, 319ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::vector<size_t>* offsets_for_adjustment); 320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 321c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Creates a string representation of |url|. The IDN host name may be in Unicode 322c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// if |languages| accepts the Unicode representation. |format_type| is a bitmask 323c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean 324c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the URL for human readability. You will generally want |UnescapeRule::SPACES| 325c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// for display to the user if you can handle spaces, or |UnescapeRule::NORMAL| 326c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// if not. If the path part and the query part seem to be encoded in %-encoded 327c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// UTF-8, decodes %-encoding and UTF-8. 328c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The last three parameters may be NULL. 330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |new_parsed| will be set to the parsing parameters of the resultant URL. 331c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |prefix_end| will be the length before the hostname of the resultant URL. 332ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// 333ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// (|offset[s]_for_adjustment|) specifies one or more offsets into the original 334ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// |url|'s spec(); each offset will be modified to reflect changes this function 335ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// makes to the output string. For example, if |url| is "http://a:b@c.com/", 336ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// |omit_username_password| is true, and an offset is 12 (the offset of '.'), 337ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// then on return the output string will be "http://c.com/" and the offset will 338ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// be 8. If an offset cannot be successfully adjusted (e.g. because it points 339c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// into the middle of a component that was entirely removed, past the end of the 340c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// string, or into the middle of an encoding sequence), it will be set to 3413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// string16::npos. 3423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickstring16 FormatUrl(const GURL& url, 3433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick const std::string& languages, 3443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick FormatUrlTypes format_types, 3453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick UnescapeRule::Type unescape_rules, 3463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick url_parse::Parsed* new_parsed, 3473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick size_t* prefix_end, 3483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick size_t* offset_for_adjustment); 349ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstring16 FormatUrlWithOffsets(const GURL& url, 350ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::string& languages, 351ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen FormatUrlTypes format_types, 352ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen UnescapeRule::Type unescape_rules, 353ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen url_parse::Parsed* new_parsed, 354ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen size_t* prefix_end, 355ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::vector<size_t>* offsets_for_adjustment); 356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 357c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This is a convenience function for FormatUrl() with 358c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical 359c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// set of flags for "URLs to display to the user". You should be cautious about 360c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// using this for URLs which will be parsed or sent to other applications. 3613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickinline string16 FormatUrl(const GURL& url, const std::string& languages) { 362c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES, 363c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch NULL, NULL, NULL); 364c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 365c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 366c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns whether FormatUrl() would strip a trailing slash from |url|, given a 367c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// format flag including kFormatUrlOmitTrailingSlashOnBareHostname. 368c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool CanStripTrailingSlash(const GURL& url); 369c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 370c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Strip the portions of |url| that aren't core to the network request. 371c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// - user name / password 372c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// - reference section 373c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottGURL SimplifyUrlForRequest(const GURL& url); 374c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 3753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickvoid SetExplicitlyAllowedPorts(const std::string& allowed_ports); 376c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 37721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsenclass ScopedPortException { 37821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen public: 37921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen ScopedPortException(int port); 38021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen ~ScopedPortException(); 38121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 38221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen private: 38321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen int port_; 38421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 38521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen DISALLOW_COPY_AND_ASSIGN(ScopedPortException); 38621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen}; 38721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 388c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Perform a simplistic test to see if IPv6 is supported by trying to create an 389c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// IPv6 socket. 390c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// TODO(jar): Make test more in-depth as needed. 391c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool IPv6Supported(); 392c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 3933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns true if it can determine that only loopback addresses are configured. 3943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// i.e. if only 127.0.0.1 and ::1 are routable. 3953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickbool HaveOnlyLoopbackAddresses(); 3963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick 397c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// IPAddressNumber is used to represent an IP address's numeric value as an 398c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// array of bytes, from most significant to least significant. This is the 399c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// network byte ordering. 400c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 401c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// IPv4 addresses will have length 4, whereas IPv6 address will have length 16. 402c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtypedef std::vector<unsigned char> IPAddressNumber; 403c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 404ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstatic const size_t kIPv4AddressSize = 4; 405ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstatic const size_t kIPv6AddressSize = 16; 406ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 407c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Parses an IP address literal (either IPv4 or IPv6) to its numeric value. 408c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns true on success and fills |ip_number| with the numeric value. 409c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool ParseIPLiteralToNumber(const std::string& ip_literal, 410c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch IPAddressNumber* ip_number); 411c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 412c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Converts an IPv4 address to an IPv4-mapped IPv6 address. 413c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// For example 192.168.0.1 would be converted to ::ffff:192.168.0.1. 414c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochIPAddressNumber ConvertIPv4NumberToIPv6Number( 415c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const IPAddressNumber& ipv4_number); 416c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 417c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Parses an IP block specifier from CIDR notation to an 418c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// (IP address, prefix length) pair. Returns true on success and fills 419c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |*ip_number| with the numeric value of the IP address and sets 420c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |*prefix_length_in_bits| with the length of the prefix. 421c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 422c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: 423c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 424c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 10.10.3.1/20 425c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// a:b:c::/46 426c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// ::1/128 427c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool ParseCIDRBlock(const std::string& cidr_literal, 428c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch IPAddressNumber* ip_number, 429c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t* prefix_length_in_bits); 430c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 431c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Compares an IP address to see if it falls within the specified IP block. 432c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns true if it does, false otherwise. 433c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 434c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any 435c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// IP address whose |prefix_length_in_bits| most significant bits match 436c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |ip_prefix| will be matched. 437c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 438c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// In cases when an IPv4 address is being compared to an IPv6 address prefix 439c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// and vice versa, the IPv4 addresses will be converted to IPv4-mapped 440c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// (IPv6) addresses. 441c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochbool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, 442c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const IPAddressNumber& ip_prefix, 443c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch size_t prefix_length_in_bits); 444c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 445ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Makes a copy of |info|. The dynamically-allocated parts are copied as well. 446ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// If |recursive| is true, chained entries via ai_next are copied too. 447ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// The copy returned by this function should be freed using 448ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// FreeCopyOfAddrinfo(), and NOT freeaddrinfo(). 449ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstruct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info, 450ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool recursive); 451ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 452ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Frees an addrinfo that was created by CreateCopyOfAddrinfo(). 453ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid FreeCopyOfAddrinfo(struct addrinfo* info); 454ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 455c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns the port field of the sockaddr in |info|. 45672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenconst uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info); 45772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenuint16* GetPortFieldFromAddrinfo(struct addrinfo* info); 458c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 459c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns the value of |info's| port (in host byte ordering). 460c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochint GetPortFromAddrinfo(const struct addrinfo* info); 461c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 46272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Same except for struct sockaddr. 46372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenconst uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, 46472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen socklen_t address_len); 46572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint GetPortFromSockaddr(const struct sockaddr* address, 46672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen socklen_t address_len); 46772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 468ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Returns true if |host| is one of the names (e.g. "localhost") or IP 469ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback. 470ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// 471ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Note that this function does not check for IP addresses other than 472ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// the above, although other IP addresses may point to the local 473ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// machine. 474ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool IsLocalhost(const std::string& host); 475ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 476ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// struct that is used by GetNetworkList() to represent a network 477ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// interface. 478ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstruct NetworkInterface { 479ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen NetworkInterface(); 480ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen NetworkInterface(const std::string& name, const IPAddressNumber& address); 481ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen ~NetworkInterface(); 482ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 483ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen std::string name; 484ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen IPAddressNumber address; 485ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}; 486ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 487ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentypedef std::list<NetworkInterface> NetworkInterfaceList; 488ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 489ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Returns list of network interfaces except loopback interface. If an 490ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// interface has more than one address, a separate entry is added to 491ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// the list for each address. 492ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Can be called only on a thread that allows IO. 493ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool GetNetworkList(NetworkInterfaceList* networks); 494ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 495ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Private adjustment function called by std::transform which sets the offset 496ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// to npos if the offset occurs at or before |component_start|, otherwise don't 497ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// alter the offset. Exposed here for unit testing. 498ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstruct ClampComponentOffset { 499ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen explicit ClampComponentOffset(size_t component_start); 500ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen size_t operator()(size_t offset); 501ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 502ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const size_t component_start; 503ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}; 504ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen 505c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace net 506c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 507c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // NET_BASE_NET_UTIL_H_ 508