Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 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. | |
| 4 | |
| 5 #ifndef NET_BASE_IP_ADDRESS_NET_H_ | |
| 6 #define NET_BASE_IP_ADDRESS_NET_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/gtest_prod_util.h" | |
|
eroman
2015/11/24 00:21:49
Why is this included? (I see only "friend" in the
martijnc
2015/11/26 22:10:31
Removed. (Was leftover from some earlier testing)
| |
| 13 #include "base/strings/string_piece.h" | |
| 14 #include "net/base/address_family.h" | |
| 15 #include "net/base/ip_address_number.h" | |
| 16 #include "net/base/net_export.h" | |
| 17 #include "net/base/sys_addrinfo.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 class NET_EXPORT IPAddress { | |
| 22 public: | |
| 23 IPAddress(); | |
|
eroman
2015/11/24 00:21:49
Please document what default construction does (wi
martijnc
2015/11/26 22:10:31
Done.
| |
| 24 ~IPAddress(); | |
| 25 | |
| 26 static const size_t kIPv4AddressSize = 4; | |
|
eroman
2015/11/24 00:21:49
I believe the conclusion in https://groups.google.
martijnc
2015/11/26 22:10:31
Done.
| |
| 27 static const size_t kIPv6AddressSize = 16; | |
| 28 | |
| 29 // Returns true if an IP address hostname is in a range reserved by the IANA. | |
| 30 // Works with both IPv4 and IPv6 addresses, and only compares against a given | |
| 31 // protocols's reserved ranges. | |
| 32 bool IsIPAddressReserved() const; | |
| 33 | |
| 34 // Returns true if the IP has |kIPv4AddressSize| elements. | |
| 35 bool IsIPv4() const { return ip_address_.size() == kIPv4AddressSize; } | |
| 36 | |
| 37 // Returns true if the IP has |kIPv6AddressSize| elements. | |
| 38 bool IsIPv6() const { return ip_address_.size() == kIPv6AddressSize; } | |
| 39 | |
| 40 // The size in bytes of |ip_address_|. | |
| 41 size_t size() const { return ip_address_.size(); } | |
| 42 | |
| 43 // Returns the string representation of an IP address. | |
|
eroman
2015/11/24 00:21:49
nit: Instead of "the string representation" can y
martijnc
2015/11/26 22:10:31
Done.
| |
| 44 // For example: "192.168.0.1" or "::1". | |
| 45 std::string ToString() const; | |
| 46 | |
| 47 // Returns the string representation of an IP address along with its port. | |
| 48 // For example: "192.168.0.1:99" or "[::1]:80". | |
| 49 std::string ToStringWithPort(uint16_t port) const; | |
|
eroman
2015/11/24 00:21:49
I don't think this function belongs here, as this
martijnc
2015/11/26 22:10:31
Removed.
| |
| 50 | |
| 51 // Returns the address as a sequence of bytes in network-byte-order. | |
| 52 std::string ToPackedString() const; | |
|
eroman
2015/11/24 00:21:49
Rather than copy into a string, why not something
martijnc
2015/11/26 22:10:31
Done (removed).
| |
| 53 | |
| 54 // Returns number of matching initial bits between the addresses |other|. | |
| 55 unsigned CommonPrefixLength(const IPAddress& other) const; | |
| 56 | |
| 57 // Computes the number of leading 1-bits in the address. | |
| 58 unsigned MaskPrefixLength() const; | |
| 59 | |
| 60 // Returns true if |ip_address_| is an IPv4-mapped IPv6 address. | |
| 61 bool IsIPv4Mapped() const; | |
| 62 | |
| 63 // Returns AddressFamily of the address. | |
| 64 AddressFamily GetAddressFamily() const; | |
| 65 | |
| 66 // Compares an IP address to see if it falls within the specified IP block. | |
| 67 // Returns true if it does, false otherwise. | |
| 68 // | |
| 69 // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any | |
| 70 // IP address whose |prefix_length_in_bits| most significant bits match | |
| 71 // |ip_prefix| will be matched. | |
| 72 // | |
| 73 // In cases when an IPv4 address is being compared to an IPv6 address prefix | |
| 74 // and vice versa, the IPv4 addresses will be converted to IPv4-mapped | |
| 75 // (IPv6) addresses. | |
| 76 bool MatchesPrefix(const IPAddress& ip_prefix, | |
| 77 size_t prefix_length_in_bits) WARN_UNUSED_RESULT; | |
| 78 | |
| 79 // Convert to a provided sockaddr struct. | |
| 80 // |address| is the sockaddr to copy into. Should be at least | |
| 81 // sizeof(struct sockaddr_storage) bytes. |address_length| is an | |
| 82 // input/output parameter. On input, it is the size of data in |address| | |
| 83 // available. On output, it is the size of the address that was copied into | |
| 84 // |address|. Returns true on success, false on failure. | |
| 85 bool ToSockAddrWithPort(struct sockaddr* address, | |
| 86 socklen_t* address_length, | |
| 87 int port) const; | |
| 88 | |
| 89 // Parses a URL-safe IP literal (see RFC 3986, Sec 3.2.2) to its numeric | |
| 90 // value. | |
| 91 // Returns true on success, and fills |ip_address| with the numeric value | |
| 92 static bool FromURLHostname(const std::string& hostname, | |
| 93 IPAddress* ip_address) WARN_UNUSED_RESULT; | |
| 94 | |
| 95 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. | |
| 96 // Returns true on success and fills |ip_address| with the numeric value. | |
| 97 static bool FromIPLiteral(const base::StringPiece& ip_literal, | |
| 98 IPAddress* ip_adress) WARN_UNUSED_RESULT; | |
| 99 | |
| 100 // Parses an IP block specifier from CIDR notation to an | |
| 101 // (IP address, prefix length) pair. Returns true on success and fills | |
| 102 // |*ip_address| with the numeric value of the IP address and sets | |
| 103 // |*prefix_length_in_bits| with the length of the prefix. | |
| 104 // | |
| 105 // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: | |
| 106 // | |
| 107 // 10.10.3.1/20 | |
| 108 // a:b:c::/46 | |
| 109 // ::1/128 | |
| 110 static bool FromCIDRBlock(const std::string& cidr_literal, | |
| 111 IPAddress* ip_address, | |
| 112 size_t* prefix_length_in_bits); | |
| 113 | |
| 114 // Creates an IPAddress from a legacy IPAddressNumber. | |
| 115 // TODO(Martijnc): Remove when migration is complete. https://crbug.com/496258 | |
| 116 static IPAddress FromLegacy(const IPAddressNumber& ip_address); | |
| 117 | |
| 118 // Converts an IPv4 address to an IPv4-mapped IPv6 address. | |
| 119 // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1. | |
| 120 static IPAddress ConvertIPv4ToIPv6(const IPAddress& ipv4_address); | |
| 121 | |
| 122 // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called | |
| 123 // on IPv4-mapped IPv6 addresses. | |
| 124 static IPAddress ConvertIPv4MappedToIPv4(const IPAddress& address); | |
| 125 | |
| 126 bool operator==(const IPAddress& that) const; | |
| 127 bool operator!=(const IPAddress& that) const; | |
| 128 bool operator<(const IPAddress& that) const; | |
| 129 bool operator>(const IPAddress& that) const; | |
|
eroman
2015/11/24 00:21:49
Why provide != and > ?
martijnc
2015/11/26 22:10:31
Removed. (added them initially for completeness)
| |
| 130 | |
| 131 private: | |
| 132 friend class IPAddressTest; | |
| 133 | |
| 134 // TODO(Martijnc): Remove this when all IPEndPoint consumers are moved to the | |
| 135 // new IPAddress class. https://crbug.com/496258 | |
| 136 friend class IPEndPoint; | |
| 137 | |
| 138 IPAddress(std::vector<unsigned char>& ip_address); | |
| 139 | |
| 140 bool IPAddressPrefixCheck(const IPAddress* ip_prefix, | |
| 141 size_t prefix_length_in_bits) const; | |
| 142 | |
| 143 bool IPAddressPrefixCheck(const unsigned char* ip_prefix, | |
| 144 size_t prefix_length_in_bits) const; | |
| 145 | |
| 146 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | |
| 147 // will have length kIPv6AddressSize. | |
| 148 std::vector<unsigned char> ip_address_; | |
|
eroman
2015/11/24 00:21:49
uint8_t
martijnc
2015/11/26 22:10:31
Done.
| |
| 149 | |
| 150 static const unsigned char kIPv4MappedPrefix[]; | |
|
eroman
2015/11/24 00:21:49
This can go into the .cc file as either a static v
martijnc
2015/11/26 22:10:31
Done.
| |
| 151 }; | |
| 152 | |
| 153 } // namespace net | |
| 154 | |
| 155 #endif // NET_BASE_IP_ADDRESS_NET_H_ | |
| OLD | NEW |