components: Fix 'gn check' errors for user_prefs.
[chromium-blink-merge.git] / net / base / ip_address_number.h
blobf8adb05ca6185fd0ff2dcd46ec4ad7b35bddd668
1 // Copyright (c) 2012 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 NET_BASE_IP_ADDRESS_NUMBER_H_
6 #define NET_BASE_IP_ADDRESS_NUMBER_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "net/base/net_export.h"
14 namespace net {
16 // IPAddressNumber is used to represent an IP address's numeric value as an
17 // array of bytes, from most significant to least significant. This is the
18 // network byte ordering.
20 // IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
21 typedef std::vector<unsigned char>
22 IPAddressNumber; // This is also duplicated in net_util.h
23 typedef std::vector<IPAddressNumber> IPAddressList;
25 static const size_t kIPv4AddressSize = 4;
26 static const size_t kIPv6AddressSize = 16;
28 // Returns true if an IP address hostname is in a range reserved by the IANA.
29 // Works with both IPv4 and IPv6 addresses, and only compares against a given
30 // protocols's reserved ranges.
31 NET_EXPORT bool IsIPAddressReserved(const IPAddressNumber& address);
33 // Returns the string representation of an IP address.
34 // For example: "192.168.0.1" or "::1".
35 NET_EXPORT std::string IPAddressToString(const uint8_t* address,
36 size_t address_len);
38 // Returns the string representation of an IP address along with its port.
39 // For example: "192.168.0.1:99" or "[::1]:80".
40 NET_EXPORT std::string IPAddressToStringWithPort(const uint8_t* address,
41 size_t address_len,
42 uint16_t port);
44 // Same as IPAddressToString() but for an IPAddressNumber.
45 NET_EXPORT std::string IPAddressToString(const IPAddressNumber& addr);
47 // Same as IPAddressToStringWithPort() but for an IPAddressNumber.
48 NET_EXPORT std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
49 uint16_t port);
51 // Returns the address as a sequence of bytes in network-byte-order.
52 NET_EXPORT std::string IPAddressToPackedString(const IPAddressNumber& addr);
54 // Parses a URL-safe IP literal (see RFC 3986, Sec 3.2.2) to its numeric value.
55 // Returns true on success, and fills |ip_number| with the numeric value
56 NET_EXPORT bool ParseURLHostnameToNumber(const std::string& hostname,
57 IPAddressNumber* ip_number);
59 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
60 // Returns true on success and fills |ip_number| with the numeric value.
61 NET_EXPORT bool ParseIPLiteralToNumber(const std::string& ip_literal,
62 IPAddressNumber* ip_number);
64 // Converts an IPv4 address to an IPv4-mapped IPv6 address.
65 // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1.
66 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4NumberToIPv6Number(
67 const IPAddressNumber& ipv4_number);
69 // Returns true iff |address| is an IPv4-mapped IPv6 address.
70 NET_EXPORT_PRIVATE bool IsIPv4Mapped(const IPAddressNumber& address);
72 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called
73 // on IPv4-mapped IPv6 addresses.
74 NET_EXPORT_PRIVATE IPAddressNumber ConvertIPv4MappedToIPv4(
75 const IPAddressNumber& address);
77 // Parses an IP block specifier from CIDR notation to an
78 // (IP address, prefix length) pair. Returns true on success and fills
79 // |*ip_number| with the numeric value of the IP address and sets
80 // |*prefix_length_in_bits| with the length of the prefix.
82 // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
84 // 10.10.3.1/20
85 // a:b:c::/46
86 // ::1/128
87 NET_EXPORT bool ParseCIDRBlock(const std::string& cidr_literal,
88 IPAddressNumber* ip_number,
89 size_t* prefix_length_in_bits);
91 // Compares an IP address to see if it falls within the specified IP block.
92 // Returns true if it does, false otherwise.
94 // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
95 // IP address whose |prefix_length_in_bits| most significant bits match
96 // |ip_prefix| will be matched.
98 // In cases when an IPv4 address is being compared to an IPv6 address prefix
99 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped
100 // (IPv6) addresses.
101 NET_EXPORT_PRIVATE bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
102 const IPAddressNumber& ip_prefix,
103 size_t prefix_length_in_bits);
105 // Returns number of matching initial bits between the addresses |a1| and |a2|.
106 unsigned CommonPrefixLength(const IPAddressNumber& a1,
107 const IPAddressNumber& a2);
109 // Computes the number of leading 1-bits in |mask|.
110 unsigned MaskPrefixLength(const IPAddressNumber& mask);
112 } // namespace net
114 #endif // NET_BASE_IP_ADDRESS_NUMBER_H_