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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* Copyright (C) Axel Duch
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#include <nxt_http_route_addr.h>
#if (NXT_INET6)
static nxt_bool_t nxt_valid_ipv6_blocks(u_char *c, size_t len);
#endif
nxt_int_t
nxt_http_route_addr_pattern_parse(nxt_mp_t *mp,
nxt_http_route_addr_pattern_t *pattern, nxt_conf_value_t *cv)
{
u_char *delim;
nxt_int_t ret, cidr_prefix;
nxt_str_t addr, port;
nxt_http_route_addr_base_t *base;
nxt_http_route_addr_range_t *inet;
if (nxt_conf_type(cv) != NXT_CONF_STRING) {
return NXT_ADDR_PATTERN_CV_TYPE_ERROR;
}
nxt_conf_get_string(cv, &addr);
base = &pattern->base;
if (addr.length > 0 && addr.start[0] == '!') {
addr.start++;
addr.length--;
base->negative = 1;
} else {
base->negative = 0;
}
if (nxt_str_eq(&addr, "unix", 4)) {
#if (NXT_HAVE_UNIX_DOMAIN)
base->addr_family = AF_UNIX;
return NXT_OK;
#else
return NXT_ADDR_PATTERN_NO_UNIX_ERROR;
#endif
}
if (nxt_slow_path(addr.length < 2)) {
return NXT_ADDR_PATTERN_LENGTH_ERROR;
}
nxt_str_null(&port);
if (addr.start[0] == '*' && addr.start[1] == ':') {
port.start = addr.start + 2;
port.length = addr.length - 2;
base->addr_family = AF_UNSPEC;
base->match_type = NXT_HTTP_ROUTE_ADDR_ANY;
goto parse_port;
}
if (nxt_inet6_probe(&addr)) {
#if (NXT_INET6)
u_char *end;
uint8_t i;
nxt_int_t len;
nxt_http_route_in6_addr_range_t *inet6;
base->addr_family = AF_INET6;
if (addr.start[0] == '[') {
addr.start++;
addr.length--;
end = addr.start + addr.length;
port.start = nxt_rmemstrn(addr.start, end, "]:", 2);
if (nxt_slow_path(port.start == NULL)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
addr.length = port.start - addr.start;
port.start += nxt_length("]:");
port.length = end - port.start;
}
inet6 = &pattern->addr.v6;
delim = nxt_memchr(addr.start, '-', addr.length);
if (delim != NULL) {
len = delim - addr.start;
if (nxt_slow_path(!nxt_valid_ipv6_blocks(addr.start, len))) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
ret = nxt_inet6_addr(&inet6->start, addr.start, len);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
len = addr.start + addr.length - delim - 1;
if (nxt_slow_path(!nxt_valid_ipv6_blocks(delim + 1, len))) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
ret = nxt_inet6_addr(&inet6->end, delim + 1, len);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
if (nxt_slow_path(memcmp(&inet6->start, &inet6->end,
sizeof(struct in6_addr)) > 0))
{
return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR;
}
base->match_type = NXT_HTTP_ROUTE_ADDR_RANGE;
goto parse_port;
}
delim = nxt_memchr(addr.start, '/', addr.length);
if (delim != NULL) {
cidr_prefix = nxt_int_parse(delim + 1,
addr.start + addr.length - (delim + 1));
if (nxt_slow_path(cidr_prefix < 0 || cidr_prefix > 128)) {
return NXT_ADDR_PATTERN_CIDR_ERROR;
}
addr.length = delim - addr.start;
if (nxt_slow_path(!nxt_valid_ipv6_blocks(addr.start,
addr.length)))
{
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
ret = nxt_inet6_addr(&inet6->start, addr.start, addr.length);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
if (nxt_slow_path(cidr_prefix == 0)) {
base->match_type = NXT_HTTP_ROUTE_ADDR_ANY;
goto parse_port;
}
if (nxt_slow_path(cidr_prefix == 128)) {
base->match_type = NXT_HTTP_ROUTE_ADDR_EXACT;
goto parse_port;
}
base->match_type = NXT_HTTP_ROUTE_ADDR_CIDR;
for (i = 0; i < sizeof(struct in6_addr); i++) {
if (cidr_prefix >= 8) {
inet6->end.s6_addr[i] = 0xFF;
cidr_prefix -= 8;
continue;
}
if (cidr_prefix > 0) {
inet6->end.s6_addr[i] = 0xFF & (0xFF << (8 - cidr_prefix));
inet6->start.s6_addr[i] &= inet6->end.s6_addr[i];
cidr_prefix = 0;
continue;
}
inet6->start.s6_addr[i] = 0;
inet6->end.s6_addr[i] = 0;
}
goto parse_port;
}
base->match_type = NXT_HTTP_ROUTE_ADDR_EXACT;
if (nxt_slow_path(!nxt_valid_ipv6_blocks(addr.start, addr.length))) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
ret = nxt_inet6_addr(&inet6->start, addr.start, addr.length);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
goto parse_port;
#endif
return NXT_ADDR_PATTERN_NO_IPv6_ERROR;
}
base->addr_family = AF_INET;
delim = nxt_memchr(addr.start, ':', addr.length);
if (delim != NULL) {
port.start = delim + 1;
port.length = addr.start + addr.length - port.start;
addr.length = delim - addr.start;
}
inet = &pattern->addr.v4;
delim = nxt_memchr(addr.start, '-', addr.length);
if (delim != NULL) {
inet->start = nxt_inet_addr(addr.start, delim - addr.start);
if (nxt_slow_path(inet->start == INADDR_NONE)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
inet->end = nxt_inet_addr(delim + 1,
addr.start + addr.length - (delim + 1));
if (nxt_slow_path(inet->end == INADDR_NONE)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
if (nxt_slow_path(memcmp(&inet->start, &inet->end,
sizeof(struct in_addr)) > 0))
{
return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR;
}
base->match_type = NXT_HTTP_ROUTE_ADDR_RANGE;
goto parse_port;
}
delim = nxt_memchr(addr.start, '/', addr.length);
if (delim != NULL) {
cidr_prefix = nxt_int_parse(delim + 1,
addr.start + addr.length - (delim + 1));
if (nxt_slow_path(cidr_prefix < 0 || cidr_prefix > 32)) {
return NXT_ADDR_PATTERN_CIDR_ERROR;
}
addr.length = delim - addr.start;
inet->end = htonl(0xFFFFFFFF & (0xFFFFFFFFULL << (32 - cidr_prefix)));
inet->start = nxt_inet_addr(addr.start, addr.length) & inet->end;
if (nxt_slow_path(inet->start == INADDR_NONE)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
if (cidr_prefix == 0) {
base->match_type = NXT_HTTP_ROUTE_ADDR_ANY;
goto parse_port;
}
if (cidr_prefix < 32) {
base->match_type = NXT_HTTP_ROUTE_ADDR_CIDR;
goto parse_port;
}
}
inet->start = nxt_inet_addr(addr.start, addr.length);
if (nxt_slow_path(inet->start == INADDR_NONE)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
base->match_type = NXT_HTTP_ROUTE_ADDR_EXACT;
parse_port:
if (port.length == 0) {
if (nxt_slow_path(port.start != NULL)) {
return NXT_ADDR_PATTERN_FORMAT_ERROR;
}
base->port.start = 0;
base->port.end = 65535;
return NXT_OK;
}
delim = nxt_memchr(port.start, '-', port.length - 1);
if (delim != NULL) {
ret = nxt_int_parse(port.start, delim - port.start);
if (nxt_slow_path(ret < 0 || ret > 65535)) {
return NXT_ADDR_PATTERN_PORT_ERROR;
}
base->port.start = ret;
ret = nxt_int_parse(delim + 1, port.start + port.length - (delim + 1));
if (nxt_slow_path(ret < base->port.start || ret > 65535)) {
return NXT_ADDR_PATTERN_PORT_ERROR;
}
base->port.end = ret;
} else {
ret = nxt_int_parse(port.start, port.length);
if (nxt_slow_path(ret < 0 || ret > 65535)) {
return NXT_ADDR_PATTERN_PORT_ERROR;
}
base->port.start = ret;
base->port.end = ret;
}
return NXT_OK;
}
#if (NXT_INET6)
static nxt_bool_t
nxt_valid_ipv6_blocks(u_char *c, size_t len)
{
u_char *end;
nxt_uint_t colon_gap;
end = c + len;
colon_gap = 0;
while (c != end) {
if (*c == ':') {
colon_gap = 0;
c++;
continue;
}
colon_gap++;
c++;
if (nxt_slow_path(colon_gap > 4)) {
return 0;
}
}
return 1;
}
#endif
|