Repository navigation
Expand file tree
/
Copy pathaddress.cpp
More file actions
239 lines (213 loc) · 8.41 KB
/
Copy pathaddress.cpp
File metadata and controls
239 lines (213 loc) · 8.41 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
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the file
LICENSE in the distribution; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
// TASK-119 step 1: the text conversion and parsing of the net
// address vocabulary (net/address.hpp). One platform divergence
// point, mirroring io_poll_sys.hpp's dispatch: inet_pton/inet_ntop
// come from arpa/inet.h on POSIX and ws2tcpip.h on Windows. The
// prefix-bits representation and this control flow are a fresh v3
// implementation -- no token-run is shared with the v2 octet-mask
// parser (detail/ip_representation.cpp), which stays untouched for
// the v2 surface.
#include <cstring>
#include <string>
#include <httpserver/net/address.hpp>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#include <httpserver/detail/net_address.hpp>
namespace httpserver {
namespace net {
namespace {
constexpr std::size_t kMaxLiteral = 45;
// NUL-terminates @p text into @p buf (sized for the longest literal);
// false when the text cannot fit, so the platform parser never reads
// past its input.
bool zcopy(std::string_view text, char (&buf)[kMaxLiteral + 1]) noexcept {
if (text.empty() || text.size() > kMaxLiteral) return false;
std::memcpy(buf, text.data(), text.size());
buf[text.size()] = '\0';
return true;
}
// Decimal prefix length ("/n"): one to three digits, no leading zero
// unless the value is exactly "0", at most @p limit. False otherwise.
bool parse_prefix_len(std::string_view digits, unsigned limit, unsigned& out) noexcept {
if (digits.empty() || digits.size() > 3) return false;
if (digits.size() > 1 && digits[0] == '0') return false;
unsigned value = 0;
for (const char c : digits) {
if (c < '0' || c > '9') return false;
value = value * 10u + static_cast<unsigned>(c - '0');
}
if (value > limit) return false;
out = value;
return true;
}
// Splits "literal/n" once; false when '/' appears more than once or
// either side is empty (a dangling "/" or "/8" is malformed).
bool split_cidr(std::string_view text, std::string_view& literal, std::string_view& suffix) noexcept {
const std::size_t slash = text.find('/');
if (slash == std::string_view::npos) {
literal = text;
return true;
}
if (slash == 0 || text.find('/', slash + 1) != std::string_view::npos) {
return false;
}
literal = text.substr(0, slash);
suffix = text.substr(slash + 1);
return !suffix.empty();
}
// The IPv4 mapped-space offset: a /n IPv4 pattern carries 96 + n.
constexpr unsigned k_v4_offset = 96;
// "a.b.c.*": exactly four segments with the last one '*'. The base
// literal replaces the wildcard with a zero; the prefix keeps the
// first three octets (96 + 24 bits).
std::optional<address_pattern> parse_v4_wildcard(std::string_view text) {
if (!text.ends_with(".*")) return std::nullopt;
const std::string literal(text.substr(0, text.size() - 2));
const std::optional<address> base = parse_address(literal + ".0");
if (!base.has_value()) return std::nullopt;
// Reject any further wildcard inside the surviving segments
// (the middle-wildcard delta).
if (literal.find('*') != std::string::npos) return std::nullopt;
address_pattern out;
out.base = *base;
out.prefix_bits = k_v4_offset + 24;
return out;
}
// One IPv4 pattern: trailing wildcard, CIDR, or exact literal.
std::optional<address_pattern> parse_v4(std::string_view text) {
if (const std::optional<address_pattern> wildcard = parse_v4_wildcard(text); wildcard.has_value()) {
return wildcard;
}
std::string_view literal;
std::string_view suffix;
if (!split_cidr(text, literal, suffix)) return std::nullopt;
const std::optional<address> base = parse_address(literal);
if (!base.has_value() || base->family != address_family::ipv4) {
return std::nullopt;
}
address_pattern out;
out.base = *base;
out.prefix_bits = k_v4_offset + 32;
if (suffix.empty()) return out; // exact literal
unsigned bits = 0;
if (!parse_prefix_len(suffix, 32, bits)) return std::nullopt;
out.prefix_bits = k_v4_offset + bits;
return out;
}
// One IPv6 pattern: exact literal or CIDR. Wildcards are not part of
// the IPv6 spelling (CIDR is the compression form). A v4-mapped
// literal normalizes to family ipv4 and stays an exact pattern.
std::optional<address_pattern> parse_v6(std::string_view text) {
if (text.find('*') != std::string_view::npos) return std::nullopt;
std::string_view literal;
std::string_view suffix;
if (!split_cidr(text, literal, suffix)) return std::nullopt;
const std::optional<address> base = parse_address(literal);
if (!base.has_value()) return std::nullopt;
address_pattern out;
out.base = *base;
out.prefix_bits = 128;
if (!suffix.empty()) {
if (base->family != address_family::ipv6) return std::nullopt;
unsigned bits = 0;
if (!parse_prefix_len(suffix, 128, bits)) return std::nullopt;
out.prefix_bits = bits;
return out;
}
return out;
}
} // namespace
std::optional<address> parse_address(std::string_view text) {
char buf[kMaxLiteral + 1];
if (!zcopy(text, buf)) return std::nullopt;
if (text.find(':') == std::string_view::npos) {
unsigned char raw[4];
if (::inet_pton(AF_INET, buf, raw) == 1) {
return detail::address_from_bytes(address_family::ipv4, reinterpret_cast<const std::byte*>(raw));
}
return std::nullopt;
}
unsigned char raw[16];
if (::inet_pton(AF_INET6, buf, raw) == 1) {
return detail::address_from_bytes(address_family::ipv6, reinterpret_cast<const std::byte*>(raw));
}
return std::nullopt;
}
std::optional<address_pattern> parse_pattern(std::string_view text) {
if (text.find(':') == std::string_view::npos) {
return parse_v4(text);
}
return parse_v6(text);
}
bool address_pattern::matches(const address& candidate) const noexcept {
if (base.family == address_family::unspec) return false;
if (candidate.family != base.family) return false;
const unsigned whole = prefix_bits / 8;
for (unsigned i = 0; i < whole; ++i) {
if (base.bytes[i] != candidate.bytes[i]) return false;
}
const unsigned leftover = prefix_bits % 8;
if (leftover == 0) return true;
const unsigned mask = (0xffu << (8u - leftover)) & 0xffu;
const unsigned want = static_cast<unsigned>(base.bytes[whole]) & mask;
const unsigned have = static_cast<unsigned>(candidate.bytes[whole]) & mask;
return want == have;
}
std::string address::to_string() const {
if (family == address_family::ipv4) {
char buf[INET_ADDRSTRLEN] = {0};
unsigned char raw[4];
std::memcpy(raw, bytes.data() + 12, 4);
if (::inet_ntop(AF_INET, raw, buf, sizeof buf) != nullptr) {
return std::string(buf);
}
return std::string();
}
if (family == address_family::ipv6) {
char buf[INET6_ADDRSTRLEN] = {0};
unsigned char raw[16];
std::memcpy(raw, bytes.data(), 16);
if (::inet_ntop(AF_INET6, raw, buf, sizeof buf) != nullptr) {
return std::string(buf);
}
return std::string();
}
return std::string();
}
std::string peer_address::to_string() const {
const std::string text = address.to_string();
if (text.empty()) return text;
if (address.family == address_family::ipv6) {
return "[" + text + "]:" + std::to_string(port);
}
return text + ":" + std::to_string(port);
}
} // namespace net
} // namespace httpserver