Chromium Code Reviews| Index: net/base/ip_address.h |
| diff --git a/net/base/ip_address.h b/net/base/ip_address.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e79ab936e4733535ba17ef0f9a3eb0a5edd24746 |
| --- /dev/null |
| +++ b/net/base/ip_address.h |
| @@ -0,0 +1,155 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_BASE_IP_ADDRESS_NET_H_ |
| +#define NET_BASE_IP_ADDRESS_NET_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/compiler_specific.h" |
| +#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)
|
| +#include "base/strings/string_piece.h" |
| +#include "net/base/address_family.h" |
| +#include "net/base/ip_address_number.h" |
| +#include "net/base/net_export.h" |
| +#include "net/base/sys_addrinfo.h" |
| + |
| +namespace net { |
| + |
| +class NET_EXPORT IPAddress { |
| + public: |
| + IPAddress(); |
|
eroman
2015/11/24 00:21:49
Please document what default construction does (wi
martijnc
2015/11/26 22:10:31
Done.
|
| + ~IPAddress(); |
| + |
| + 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.
|
| + static const size_t kIPv6AddressSize = 16; |
| + |
| + // Returns true if an IP address hostname is in a range reserved by the IANA. |
| + // Works with both IPv4 and IPv6 addresses, and only compares against a given |
| + // protocols's reserved ranges. |
| + bool IsIPAddressReserved() const; |
| + |
| + // Returns true if the IP has |kIPv4AddressSize| elements. |
| + bool IsIPv4() const { return ip_address_.size() == kIPv4AddressSize; } |
| + |
| + // Returns true if the IP has |kIPv6AddressSize| elements. |
| + bool IsIPv6() const { return ip_address_.size() == kIPv6AddressSize; } |
| + |
| + // The size in bytes of |ip_address_|. |
| + size_t size() const { return ip_address_.size(); } |
| + |
| + // 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.
|
| + // For example: "192.168.0.1" or "::1". |
| + std::string ToString() const; |
| + |
| + // Returns the string representation of an IP address along with its port. |
| + // For example: "192.168.0.1:99" or "[::1]:80". |
| + 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.
|
| + |
| + // Returns the address as a sequence of bytes in network-byte-order. |
| + 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).
|
| + |
| + // Returns number of matching initial bits between the addresses |other|. |
| + unsigned CommonPrefixLength(const IPAddress& other) const; |
| + |
| + // Computes the number of leading 1-bits in the address. |
| + unsigned MaskPrefixLength() const; |
| + |
| + // Returns true if |ip_address_| is an IPv4-mapped IPv6 address. |
| + bool IsIPv4Mapped() const; |
| + |
| + // Returns AddressFamily of the address. |
| + AddressFamily GetAddressFamily() const; |
| + |
| + // Compares an IP address to see if it falls within the specified IP block. |
| + // Returns true if it does, false otherwise. |
| + // |
| + // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any |
| + // IP address whose |prefix_length_in_bits| most significant bits match |
| + // |ip_prefix| will be matched. |
| + // |
| + // In cases when an IPv4 address is being compared to an IPv6 address prefix |
| + // and vice versa, the IPv4 addresses will be converted to IPv4-mapped |
| + // (IPv6) addresses. |
| + bool MatchesPrefix(const IPAddress& ip_prefix, |
| + size_t prefix_length_in_bits) WARN_UNUSED_RESULT; |
| + |
| + // Convert to a provided sockaddr struct. |
| + // |address| is the sockaddr to copy into. Should be at least |
| + // sizeof(struct sockaddr_storage) bytes. |address_length| is an |
| + // input/output parameter. On input, it is the size of data in |address| |
| + // available. On output, it is the size of the address that was copied into |
| + // |address|. Returns true on success, false on failure. |
| + bool ToSockAddrWithPort(struct sockaddr* address, |
| + socklen_t* address_length, |
| + int port) const; |
| + |
| + // Parses a URL-safe IP literal (see RFC 3986, Sec 3.2.2) to its numeric |
| + // value. |
| + // Returns true on success, and fills |ip_address| with the numeric value |
| + static bool FromURLHostname(const std::string& hostname, |
| + IPAddress* ip_address) WARN_UNUSED_RESULT; |
| + |
| + // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. |
| + // Returns true on success and fills |ip_address| with the numeric value. |
| + static bool FromIPLiteral(const base::StringPiece& ip_literal, |
| + IPAddress* ip_adress) WARN_UNUSED_RESULT; |
| + |
| + // Parses an IP block specifier from CIDR notation to an |
| + // (IP address, prefix length) pair. Returns true on success and fills |
| + // |*ip_address| with the numeric value of the IP address and sets |
| + // |*prefix_length_in_bits| with the length of the prefix. |
| + // |
| + // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: |
| + // |
| + // 10.10.3.1/20 |
| + // a:b:c::/46 |
| + // ::1/128 |
| + static bool FromCIDRBlock(const std::string& cidr_literal, |
| + IPAddress* ip_address, |
| + size_t* prefix_length_in_bits); |
| + |
| + // Creates an IPAddress from a legacy IPAddressNumber. |
| + // TODO(Martijnc): Remove when migration is complete. https://crbug.com/496258 |
| + static IPAddress FromLegacy(const IPAddressNumber& ip_address); |
| + |
| + // Converts an IPv4 address to an IPv4-mapped IPv6 address. |
| + // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1. |
| + static IPAddress ConvertIPv4ToIPv6(const IPAddress& ipv4_address); |
| + |
| + // Converts an IPv4-mapped IPv6 address to IPv4 address. Should only be called |
| + // on IPv4-mapped IPv6 addresses. |
| + static IPAddress ConvertIPv4MappedToIPv4(const IPAddress& address); |
| + |
| + bool operator==(const IPAddress& that) const; |
| + bool operator!=(const IPAddress& that) const; |
| + bool operator<(const IPAddress& that) const; |
| + 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)
|
| + |
| + private: |
| + friend class IPAddressTest; |
| + |
| + // TODO(Martijnc): Remove this when all IPEndPoint consumers are moved to the |
| + // new IPAddress class. https://crbug.com/496258 |
| + friend class IPEndPoint; |
| + |
| + IPAddress(std::vector<unsigned char>& ip_address); |
| + |
| + bool IPAddressPrefixCheck(const IPAddress* ip_prefix, |
| + size_t prefix_length_in_bits) const; |
| + |
| + bool IPAddressPrefixCheck(const unsigned char* ip_prefix, |
| + size_t prefix_length_in_bits) const; |
| + |
| + // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address |
| + // will have length kIPv6AddressSize. |
| + std::vector<unsigned char> ip_address_; |
|
eroman
2015/11/24 00:21:49
uint8_t
martijnc
2015/11/26 22:10:31
Done.
|
| + |
| + 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.
|
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_BASE_IP_ADDRESS_NET_H_ |