-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolver.cpp
More file actions
262 lines (225 loc) · 7.49 KB
/
Copy pathresolver.cpp
File metadata and controls
262 lines (225 loc) · 7.49 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2024-2025 Lars-Christian Schulz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "scion/resolver.hpp"
#include "scion/details/debug.hpp"
#include <ares.h>
#include <boost/algorithm/string.hpp>
#if _WIN32
constexpr int ns_c_in = 1;
constexpr int ns_t_txt = 16;
#else
#include <arpa/nameser.h>
#endif
#include <fstream>
namespace scion {
#if _WIN32
const char* HOSTS_FILE = "%ProgramFiles%\\scion\\hosts";
#else
const char* HOSTS_FILE = "/etc/scion/hosts";
#endif
} // namespace scion
struct CAresErrorCategory : public std::error_category
{
const char* name() const noexcept override
{
return "c-ares";
}
std::string message(int code) const override
{
return std::string(ares_strerror(code));
}
bool equivalent(int code, const std::error_condition& cond) const noexcept override
{
using scion::ErrorCondition;
if (cond.category() == scion::scion_error_condition()) {
const auto value = static_cast<ErrorCondition>(cond.value());
switch (value) {
case ErrorCondition::Ok:
return code == ARES_SUCCESS;
case ErrorCondition::Cancelled:
return code == ARES_EDESTRUCTION || code == ARES_ECANCELLED;
case ErrorCondition::Timeout:
return code == ARES_ETIMEOUT;
case ErrorCondition::LogicError:
return code == ARES_ENOTINITIALIZED;
case ErrorCondition::InvalidArgument:
return code == ARES_EBADNAME;
case ErrorCondition::NameNotFound:
return code == ARES_ENOTFOUND;
case ErrorCondition::RemoteError:
return code == ARES_ENODATA || code == ARES_EFORMERR
|| code == ARES_ESERVFAIL || code == ARES_ENOTIMP
|| code == ARES_EREFUSED;
default:
return false;
}
}
return false;
}
};
static CAresErrorCategory cAresErrorCategory;
const std::error_category& scion::cares_error_category()
{
return cAresErrorCategory;
}
std::error_code make_error_code(ares_status_t code)
{
return {static_cast<int>(code), cAresErrorCategory};
}
namespace std {
template <> struct is_error_code_enum<ares_status_t> : true_type {};
}
namespace scion {
//////////////
// Resolver //
//////////////
class Resolver::Ares
{
public:
ares_channel_t* channel = nullptr;
public:
constexpr Ares() = default;
~Ares() { destroy(); }
Ares(const Ares&) = delete;
Ares(Ares&&) = delete;
std::error_code initialize() noexcept
{
if (channel) return ErrorCode::Ok;
auto res = (ares_status_t)ares_library_init(ARES_LIB_INIT_ALL);
if (res != ARES_SUCCESS) return res;
if (!ares_threadsafety()) {
ares_library_cleanup();
SCION_DEBUG_PRINT("c-ares not compiled with thread support\n");
return ErrorCode::LogicError;
}
ares_options opts = {};
opts.evsys = ARES_EVSYS_DEFAULT;
int optmask = ARES_OPT_EVENT_THREAD;
res = (ares_status_t)ares_init_options(&channel, &opts, optmask);
if (res != ARES_SUCCESS) {
ares_library_cleanup();
return res;
}
return res;
};
void destroy() noexcept
{
if (!channel) return;
ares_destroy(channel);
channel = nullptr;
ares_library_cleanup();
}
};
Resolver::Resolver()
: hostsFile(HOSTS_FILE)
{}
Resolver::~Resolver() = default;
std::error_code Resolver::initialize()
{
if (!ares) ares = std::make_unique<Ares>();
return ares->initialize();
}
void Resolver::cancel() const
{
ares_cancel(ares->channel);
}
void Resolver::aresQueryTXT(const std::string& name, txtCallback* cb, void* arg) const
{
ares_query(ares->channel, name.c_str(), ns_c_in, ns_t_txt, cb, arg);
}
auto Resolver::parseTxtRecords(int status, unsigned char* abuf, int alen) -> Maybe<AddressSet>
{
Maybe<AddressSet> result;
if (status) {
result = Error((ares_status_t)status);
} else {
ares_txt_reply* reply = nullptr;
status = ares_parse_txt_reply(abuf, alen, &reply);
if (status) {
result = Error((ares_status_t)status);
} else {
result = Error(ErrorCode::NameNotFound);
for (ares_txt_reply* record = reply; record; record = record->next) {
std::string_view sv(reinterpret_cast<char*>(record->txt), record->length);
parseScionRecord(sv, result);
}
ares_free_data(reply);
}
}
return result;
}
bool Resolver::parseScionRecord(std::string_view record, Maybe<AddressSet>& out)
{
if (record.starts_with("scion=")) {
record.remove_prefix(6);
auto addr = ScIPAddress::Parse(record);
if (addr.has_value()) {
if (!out.has_value()) {
out = std::vector<ScIPAddress>();
}
out->push_back(*addr);
return true;
}
}
return false;
}
////////////////////
// Free Functions //
////////////////////
Maybe<std::vector<ScIPAddress>> queryHostsFile(
std::string_view name,
const std::filesystem::path& hostsFilePath)
{
Maybe<std::vector<ScIPAddress>> result = Error(ErrorCode::NameNotFound);
std::fstream file(hostsFilePath);
if (!file.is_open()) return result;
auto finder = boost::algorithm::token_finder([] (char c) -> bool { return std::isspace(c); } );
std::string line;
while (std::getline(file, line)) {
auto part = boost::algorithm::make_split_iterator(line, finder);
decltype(part) end;
if (part == end) continue; // empty line
{
std::string_view sv(part->begin(), part->end());
if (sv.starts_with('#')) continue; // entire line is comment
if (sv != name) continue; // not the name we're looking for
++part;
}
for (; part != end; ++part) {
std::string_view sv(part->begin(), part->end());
if (sv.starts_with('#')) break; // trailing comment
auto addr = ScIPAddress::Parse(sv);
if (addr.has_value()) {
if (!result.has_value())
result = std::vector<ScIPAddress>();
result->push_back(*addr);
}
}
};
return result;
}
void* details::resolverGetChannel(const Resolver& resolver)
{
if (resolver.ares)
return resolver.ares->channel;
else
return nullptr;
}
} // namespace scion