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/strings/string_piece.h" | |
| 13 #include "net/base/net_export.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 class NET_EXPORT IPAddress { | |
| 18 public: | |
| 19 static const size_t kIPv4AddressSize; | |
| 20 static const size_t kIPv6AddressSize; | |
| 21 | |
| 22 // Creates a zero-sized, invalid address. | |
| 23 IPAddress(); | |
| 24 IPAddress(const uint8_t* address, size_t address_len); | |
|
eroman
2015/11/30 22:12:33
Please add a comment explaining this creates a cop
eroman
2015/11/30 22:15:23
By "byte order" I really meant to say "network byt
martijnc
2015/12/01 22:09:34
Done.
| |
| 25 ~IPAddress(); | |
| 26 | |
| 27 // Returns true if the IP has |kIPv4AddressSize| elements. | |
| 28 bool IsIPv4() const; | |
| 29 | |
| 30 // Returns true if the IP has |kIPv6AddressSize| elements. | |
| 31 bool IsIPv6() const; | |
| 32 | |
| 33 // Returns true if an IP address hostname is in a range reserved by the IANA. | |
| 34 // Works with both IPv4 and IPv6 addresses, and only compares against a given | |
| 35 // protocols's reserved ranges. | |
| 36 bool IsReserved() const; | |
| 37 | |
| 38 // Returns true if |ip_address_| is an IPv4-mapped IPv6 address. | |
| 39 bool IsIPv4Mapped() const; | |
| 40 | |
| 41 // The size in bytes of |ip_address_|. | |
| 42 size_t size() const { return ip_address_.size(); } | |
| 43 | |
| 44 // Returns the canonical string representation of an IP address. | |
| 45 // For example: "192.168.0.1" or "::1". | |
|
eroman
2015/11/30 22:12:33
Please also add a mention that it is incorrect to
martijnc
2015/12/01 22:09:34
Done.
| |
| 46 std::string ToString() const; | |
| 47 | |
| 48 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. | |
| 49 // Returns true on success and fills |ip_address| with the numeric value. | |
| 50 static bool FromIPLiteral(const base::StringPiece& ip_literal, | |
| 51 IPAddress* ip_address) WARN_UNUSED_RESULT; | |
| 52 | |
| 53 // Returns the underlying byte vector. | |
| 54 const std::vector<uint8_t>& bytes() const { return ip_address_; }; | |
| 55 | |
| 56 bool operator==(const IPAddress& that) const; | |
| 57 bool operator<(const IPAddress& that) const; | |
| 58 | |
| 59 private: | |
| 60 friend class IPAddressTest; | |
|
eroman
2015/11/30 22:12:33
Delete, I don't believe this is needed anymore.
martijnc
2015/12/01 22:09:34
Done.
| |
| 61 | |
| 62 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address | |
| 63 // will have length kIPv6AddressSize. | |
| 64 std::vector<uint8_t> ip_address_; | |
| 65 }; | |
| 66 | |
| 67 } // namespace net | |
| 68 | |
| 69 #endif // NET_BASE_IP_ADDRESS_NET_H_ | |
| OLD | NEW |