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 #include "net/base/ip_address.h" |
| 6 |
| 7 #include "net/base/ip_address_number.h" |
| 8 #include "url/gurl.h" |
| 9 #include "url/url_canon_ip.h" |
| 10 |
| 11 namespace net { |
| 12 |
| 13 const size_t IPAddress::kIPv4AddressSize = 4; |
| 14 const size_t IPAddress::kIPv6AddressSize = 16; |
| 15 |
| 16 IPAddress::IPAddress() {} |
| 17 |
| 18 IPAddress::~IPAddress() {} |
| 19 |
| 20 IPAddress::IPAddress(const uint8_t* address, size_t address_len) |
| 21 : ip_address_(address, address + address_len) {} |
| 22 |
| 23 bool IPAddress::IsIPv4() const { |
| 24 return ip_address_.size() == kIPv4AddressSize; |
| 25 } |
| 26 |
| 27 bool IPAddress::IsIPv6() const { |
| 28 return ip_address_.size() == kIPv6AddressSize; |
| 29 } |
| 30 |
| 31 bool IPAddress::IsReserved() const { |
| 32 return IsIPAddressReserved(ip_address_); |
| 33 } |
| 34 |
| 35 bool IPAddress::IsIPv4Mapped() const { |
| 36 return net::IsIPv4Mapped(ip_address_); |
| 37 } |
| 38 |
| 39 std::string IPAddress::ToString() const { |
| 40 return IPAddressToString(ip_address_); |
| 41 } |
| 42 |
| 43 // static |
| 44 bool IPAddress::FromIPLiteral(const base::StringPiece& ip_literal, |
| 45 IPAddress* ip_address) { |
| 46 std::vector<uint8_t> number; |
| 47 if (!ParseIPLiteralToNumber(ip_literal, &number)) |
| 48 return false; |
| 49 |
| 50 std::swap(number, ip_address->ip_address_); |
| 51 return true; |
| 52 } |
| 53 |
| 54 bool IPAddress::operator==(const IPAddress& that) const { |
| 55 return ip_address_ == that.ip_address_; |
| 56 } |
| 57 |
| 58 bool IPAddress::operator<(const IPAddress& that) const { |
| 59 // Sort IPv4 before IPv6. |
| 60 if (ip_address_.size() != that.ip_address_.size()) { |
| 61 return ip_address_.size() < that.ip_address_.size(); |
| 62 } |
| 63 |
| 64 return ip_address_ < that.ip_address_; |
| 65 } |
| 66 |
| 67 } // namespace net |
OLD | NEW |