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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright (c) 2019 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/dns/address_info.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/sys_byteorder.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h"
namespace net {
namespace {
const addrinfo* Next(const addrinfo* ai) {
return ai->ai_next;
}
} // namespace
//// iterator
AddressInfo::const_iterator::const_iterator(const addrinfo* ai) : ai_(ai) {}
bool AddressInfo::const_iterator::operator!=(
const AddressInfo::const_iterator& o) const {
return ai_ != o.ai_;
}
AddressInfo::const_iterator& AddressInfo::const_iterator::operator++() {
ai_ = Next(ai_);
return *this;
}
const addrinfo* AddressInfo::const_iterator::operator->() const {
return ai_;
}
const addrinfo& AddressInfo::const_iterator::operator*() const {
return *ai_;
}
//// constructors
AddressInfo::AddressInfoAndResult AddressInfo::Get(
const std::string& host,
const addrinfo& hints,
std::unique_ptr<AddrInfoGetter> getter) {
if (getter == nullptr)
getter = std::make_unique<AddrInfoGetter>();
int err = OK;
int os_error = 0;
addrinfo* ai = getter->getaddrinfo(host, &hints, &os_error);
if (!ai) {
err = ERR_NAME_NOT_RESOLVED;
// If the call to getaddrinfo() failed because of a system error, report
// it separately from ERR_NAME_NOT_RESOLVED.
#if defined(OS_WIN)
if (os_error != WSAHOST_NOT_FOUND && os_error != WSANO_DATA)
err = ERR_NAME_RESOLUTION_FAILED;
#elif defined(OS_ANDROID)
// Workaround for Android's getaddrinfo leaving ai==nullptr without an
// error.
// http://crbug.com/134142
err = ERR_NAME_NOT_RESOLVED;
#elif defined(OS_POSIX) && !defined(OS_FREEBSD)
if (os_error != EAI_NONAME && os_error != EAI_NODATA)
err = ERR_NAME_RESOLUTION_FAILED;
#endif
return AddressInfoAndResult(base::Optional<AddressInfo>(), err, os_error);
}
return AddressInfoAndResult(
base::Optional<AddressInfo>(AddressInfo(ai, std::move(getter))), OK, 0);
}
AddressInfo::AddressInfo(AddressInfo&& other)
: ai_(other.ai_), getter_(std::move(other.getter_)) {
other.ai_ = nullptr;
}
AddressInfo& AddressInfo::operator=(AddressInfo&& other) {
ai_ = other.ai_;
other.ai_ = nullptr;
getter_ = std::move(other.getter_);
return *this;
}
AddressInfo::~AddressInfo() {
if (ai_)
getter_->freeaddrinfo(ai_);
}
//// public methods
AddressInfo::const_iterator AddressInfo::begin() const {
return const_iterator(ai_);
}
AddressInfo::const_iterator AddressInfo::end() const {
return const_iterator(nullptr);
}
base::Optional<std::string> AddressInfo::GetCanonicalName() const {
return (ai_->ai_canonname != nullptr)
? base::Optional<std::string>(std::string(ai_->ai_canonname))
: base::Optional<std::string>();
}
bool AddressInfo::IsAllLocalhostOfOneFamily() const {
bool saw_v4_localhost = false;
bool saw_v6_localhost = false;
const auto* ai = ai_;
for (; ai != nullptr; ai = Next(ai)) {
switch (ai->ai_family) {
case AF_INET: {
const struct sockaddr_in* addr_in =
reinterpret_cast<struct sockaddr_in*>(ai->ai_addr);
if ((base::NetToHost32(addr_in->sin_addr.s_addr) & 0xff000000) ==
0x7f000000)
saw_v4_localhost = true;
else
return false;
break;
}
case AF_INET6: {
const struct sockaddr_in6* addr_in6 =
reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr);
if (IN6_IS_ADDR_LOOPBACK(&addr_in6->sin6_addr))
saw_v6_localhost = true;
else
return false;
break;
}
default:
NOTREACHED();
return false;
}
}
return saw_v4_localhost != saw_v6_localhost;
}
AddressList AddressInfo::CreateAddressList() const {
AddressList list;
auto canonical_name = GetCanonicalName();
if (canonical_name)
list.set_canonical_name(*canonical_name);
for (auto&& ai : *this) {
IPEndPoint ipe;
// NOTE: Ignoring non-INET* families.
if (ipe.FromSockAddr(ai.ai_addr, ai.ai_addrlen))
list.push_back(ipe);
else
DLOG(WARNING) << "Unknown family found in addrinfo: " << ai.ai_family;
}
return list;
}
//// private methods
AddressInfo::AddressInfo(addrinfo* ai, std::unique_ptr<AddrInfoGetter> getter)
: ai_(ai), getter_(std::move(getter)) {}
//// AddrInfoGetter
AddrInfoGetter::AddrInfoGetter() = default;
AddrInfoGetter::~AddrInfoGetter() = default;
addrinfo* AddrInfoGetter::getaddrinfo(const std::string& host,
const addrinfo* hints,
int* out_os_error) {
addrinfo* ai;
*out_os_error = ::getaddrinfo(host.c_str(), nullptr, hints, &ai);
if (*out_os_error) {
#if defined(OS_WIN)
*out_os_error = WSAGetLastError();
#endif
return nullptr;
}
return ai;
}
void AddrInfoGetter::freeaddrinfo(addrinfo* ai) {
::freeaddrinfo(ai);
}
} // namespace net
|