1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// 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.
#include "net/base/ip_address.h"
#include "net/base/ip_address_number.h"
#include "url/gurl.h"
#include "url/url_canon_ip.h"
namespace net {
const size_t IPAddress::kIPv4AddressSize = 4;
const size_t IPAddress::kIPv6AddressSize = 16;
IPAddress::IPAddress() {}
IPAddress::IPAddress(const IPAddressNumber& address) : ip_address_(address) {}
IPAddress::IPAddress(const uint8_t* address, size_t address_len)
: ip_address_(address, address + address_len) {}
IPAddress::IPAddress(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) {
ip_address_.reserve(4);
ip_address_.push_back(b0);
ip_address_.push_back(b1);
ip_address_.push_back(b2);
ip_address_.push_back(b3);
}
IPAddress::~IPAddress() {}
bool IPAddress::IsIPv4() const {
return ip_address_.size() == kIPv4AddressSize;
}
bool IPAddress::IsIPv6() const {
return ip_address_.size() == kIPv6AddressSize;
}
bool IPAddress::IsValid() const {
return IsIPv4() || IsIPv6();
}
bool IPAddress::IsReserved() const {
return IsIPAddressReserved(ip_address_);
}
bool IPAddress::IsZero() const {
for (auto x : ip_address_) {
if (x != 0)
return false;
}
return !empty();
}
bool IPAddress::IsIPv4MappedIPv6() const {
return net::IsIPv4Mapped(ip_address_);
}
std::string IPAddress::ToString() const {
return IPAddressToString(ip_address_);
}
bool IPAddress::AssignFromIPLiteral(const base::StringPiece& ip_literal) {
std::vector<uint8_t> number;
if (!ParseIPLiteralToNumber(ip_literal, &number))
return false;
std::swap(number, ip_address_);
return true;
}
bool IPAddress::operator==(const IPAddress& that) const {
return ip_address_ == that.ip_address_;
}
bool IPAddress::operator!=(const IPAddress& that) const {
return ip_address_ != that.ip_address_;
}
bool IPAddress::operator<(const IPAddress& that) const {
// Sort IPv4 before IPv6.
if (ip_address_.size() != that.ip_address_.size()) {
return ip_address_.size() < that.ip_address_.size();
}
return ip_address_ < that.ip_address_;
}
std::string IPAddressToStringWithPort(const IPAddress& address, uint16_t port) {
return IPAddressToStringWithPort(address.bytes(), port);
}
std::string IPAddressToPackedString(const IPAddress& address) {
return IPAddressToPackedString(address.bytes());
}
IPAddress ConvertIPv4ToIPv4MappedIPv6(const IPAddress& address) {
return IPAddress(ConvertIPv4NumberToIPv6Number(address.bytes()));
}
IPAddress ConvertIPv4MappedIPv6ToIPv4(const IPAddress& address) {
return IPAddress(ConvertIPv4MappedToIPv4(address.bytes()));
}
bool IPAddressMatchesPrefix(const IPAddress& ip_address,
const IPAddress& ip_prefix,
size_t prefix_length_in_bits) {
return IPNumberMatchesPrefix(ip_address.bytes(), ip_prefix.bytes(),
prefix_length_in_bits);
}
} // namespace net
|