forked from radsecproxy/radsecproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostport.c
351 lines (312 loc) · 10.4 KB
/
hostport.c
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
347
348
349
350
351
/* Copyright (c) 2007-2009, UNINETT AS
* Copyright (c) 2012,2016 NORDUnet A/S
* Copyright (c) 2023, SWITCH */
/* See LICENSE for licensing information. */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include "debug.h"
#include "util.h"
#include "list.h"
#include "hostport.h"
void freehostport(struct hostportres *hp) {
if (hp) {
free(hp->host);
free(hp->port);
if (hp->addrinfo)
freeaddrinfo(hp->addrinfo);
free(hp);
}
}
static int parsehostport(struct hostportres *hp, char *hostport, char *default_port) {
char *p, *field;
int ipv6 = 0;
if (!hostport) {
hp->port = default_port ? stringcopy(default_port, 0) : NULL;
return 1;
}
p = hostport;
/* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
if (*p == '[') {
p++;
field = p;
for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
if (*p != ']') {
debug(DBG_ERR, "no ] matching initial [");
return 0;
}
ipv6 = 1;
} else {
field = p;
for (; *p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
}
if (field == p) {
debug(DBG_ERR, "missing host/address");
return 0;
}
hp->host = stringcopy(field, p - field);
if (ipv6) {
p++;
if (*p && *p != ':' && *p != '/' && *p != ' ' && *p != '\t' && *p != '\n') {
debug(DBG_ERR, "unexpected character after ]");
return 0;
}
}
if (*p == ':') {
/* port number or service name is specified */;
field = ++p;
for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
if (field == p) {
debug(DBG_ERR, "syntax error, : but no following port");
return 0;
}
hp->port = stringcopy(field, p - field);
} else
hp->port = default_port ? stringcopy(default_port, 0) : NULL;
return 1;
}
struct hostportres *newhostport(char *hostport, char *default_port, uint8_t prefixok) {
struct hostportres *hp;
char *slash, *s;
int plen;
hp = malloc(sizeof(struct hostportres));
if (!hp) {
debug(DBG_ERR, "resolve_newhostport: malloc failed");
goto errexit;
}
memset(hp, 0, sizeof(struct hostportres));
if (!parsehostport(hp, hostport, default_port))
goto errexit;
if (hp->host && !strcmp(hp->host, "*")) {
free(hp->host);
hp->host = NULL;
}
slash = hostport ? strchr(hostport, '/') : NULL;
if (slash) {
if (!prefixok) {
debug(DBG_WARN, "newhostport: prefix not allowed here (%s)", hp->host);
goto errexit;
}
s = slash + 1;
if (!*s) {
debug(DBG_WARN, "newhostport: prefix length must be specified after the / in %s", hp->host);
goto errexit;
}
for (; *s; s++)
if (*s < '0' || *s > '9') {
debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
goto errexit;
}
plen = atoi(slash + 1);
if (plen < 0 || plen > 128) {
debug(DBG_WARN, "newhostport: %s in %s is not a valid prefix length", slash + 1, hp->host);
goto errexit;
}
hp->prefixlen = plen;
} else
hp->prefixlen = 255;
return hp;
errexit:
freehostport(hp);
return NULL;
}
int resolvehostport(struct hostportres *hp, int af, int socktype, uint8_t passive) {
struct addrinfo hints, *res;
char tmp[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = socktype;
hints.ai_family = af;
if (passive)
hints.ai_flags = AI_PASSIVE;
if (!hp->host && !hp->port) {
/* getaddrinfo() doesn't like host and port to be NULL */
if (getaddrinfo(hp->host, "1812" /* can be anything */, &hints, &hp->addrinfo)) {
debug(DBG_WARN, "resolvehostport: can't resolve (null) port (null)");
goto errexit;
}
for (res = hp->addrinfo; res; res = res->ai_next)
port_set(res->ai_addr, 0);
} else {
if (hp->prefixlen != 255)
hints.ai_flags |= AI_NUMERICHOST;
if (getaddrinfo(hp->host, hp->port, &hints, &hp->addrinfo)) {
debug(DBG_WARN, "resolvehostport: can't resolve %s port %s", hp->host ? hp->host : "(null)", hp->port ? hp->port : "(null)");
goto errexit;
}
if (hp->prefixlen != 255) {
switch (hp->addrinfo->ai_family) {
case AF_INET:
if (hp->prefixlen > 32) {
debug(DBG_WARN, "resolvehostport: prefix length must be <= 32 in %s", hp->host);
goto errexit;
}
break;
case AF_INET6:
break;
default:
debug(DBG_WARN, "resolvehostport: prefix must be IPv4 or IPv6 in %s", hp->host);
goto errexit;
}
}
}
debug(DBG_DBG, "%s: %s -> %s", __func__,
(hp->host ? hp->host : "(src info not available)"),
((hp->addrinfo && hp->addrinfo->ai_addr) ?
addr2string(hp->addrinfo->ai_addr, tmp, sizeof(tmp)) : "(dst info not available)"));
return 1;
errexit:
if (hp->addrinfo)
freeaddrinfo(hp->addrinfo);
return 0;
}
int addhostport(struct list **hostports, char **hostport, char *portdefault, uint8_t prefixok) {
struct hostportres *hp;
int i;
if (!*hostports) {
*hostports = list_create();
if (!*hostports) {
debug(DBG_ERR, "addhostport: malloc failed");
return 0;
}
}
for (i = 0; hostport[i]; i++) {
hp = newhostport(hostport[i], portdefault, prefixok);
if (!hp)
return 0;
if (!list_push(*hostports, hp)) {
freehostport(hp);
debug(DBG_ERR, "addhostport: malloc failed");
return 0;
}
}
return 1;
}
void freehostports(struct list *hostports) {
struct hostportres *hp;
while ((hp = (struct hostportres *)list_shift(hostports)))
freehostport(hp);
list_destroy(hostports);
}
int resolvehostports(struct list *hostports, int af, int socktype) {
struct list_node *entry;
struct hostportres *hp;
for (entry = list_first(hostports); entry; entry = list_next(entry)) {
hp = (struct hostportres *)entry->data;
if (!hp->addrinfo && !resolvehostport(hp, af, socktype, 0))
return 0;
}
return 1;
}
struct addrinfo *resolvepassiveaddrinfo(char **hostport, int af, char *default_port, int socktype) {
struct addrinfo *ai = NULL, *last_ai = NULL;
int i;
char *any[2] = {"*", NULL};
if(!hostport)
hostport = any;
for (i = 0; hostport[i]; i++) {
struct hostportres *hp = newhostport(hostport[i], default_port, 0);
if (hp && resolvehostport(hp, af, socktype, 1)) {
if (!ai) {
ai = last_ai = hp->addrinfo;
} else {
last_ai->ai_next = hp->addrinfo;
}
while (last_ai->ai_next)
last_ai = last_ai->ai_next;
hp->addrinfo = NULL;
}
freehostport(hp);
}
return ai;
}
/* returns 1 if the len first bits are equal, else 0 */
static int prefixmatch(void *a1, void *a2, uint8_t len) {
static uint8_t mask[] = { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
uint8_t r, l = len / 8;
if (l && memcmp(a1, a2, l))
return 0;
r = len % 8;
if (!r)
return 1;
return (((uint8_t *)a1)[l] & mask[r]) == (((uint8_t *)a2)[l] & mask[r]);
}
int _internal_addressmatches(struct list *hostports, struct sockaddr *addr, uint8_t prefixlen, uint8_t checkport, struct hostportres **hpreturn) {
struct sockaddr_in6 *sa6 = NULL;
struct in_addr *a4 = NULL;
struct addrinfo *res;
struct list_node *entry;
struct hostportres *hp = NULL;
if (addr->sa_family == AF_INET6) {
sa6 = (struct sockaddr_in6 *)addr;
if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
sa6 = NULL;
}
} else
a4 = &((struct sockaddr_in *)addr)->sin_addr;
for (entry = list_first(hostports); entry; entry = list_next(entry)) {
hp = (struct hostportres *)entry->data;
for (res = hp->addrinfo; res; res = res->ai_next) {
if (hp->prefixlen >= (res->ai_family == AF_INET ? 32 : 128) && prefixlen >= (a4 ? 32 : 128)) {
if ((a4 && res->ai_family == AF_INET &&
!memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4) &&
(!checkport || ((struct sockaddr_in *)res->ai_addr)->sin_port ==
((struct sockaddr_in *)addr)->sin_port)) ||
(sa6 && res->ai_family == AF_INET6 &&
!memcmp(&sa6->sin6_addr,
&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16) &&
(!checkport || ((struct sockaddr_in6 *)res->ai_addr)->sin6_port ==
((struct sockaddr_in6 *)addr)->sin6_port))) {
if (hpreturn) *hpreturn = hp;
return 1;
}
} else if (hp->prefixlen <= prefixlen) {
if ((a4 && res->ai_family == AF_INET &&
prefixmatch(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, hp->prefixlen)) ||
(sa6 && res->ai_family == AF_INET6 &&
prefixmatch(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, hp->prefixlen))) {
if (hpreturn) *hpreturn = hp;
return 1;
}
}
}
}
return 0;
}
int hostportmatches(struct list *hostports, struct list *matchhostports, uint8_t checkport) {
struct list_node *entry;
struct hostportres *match;
struct addrinfo *res;
for (entry = list_first(matchhostports); entry; entry = list_next(entry)) {
match = (struct hostportres *)entry->data;
for (res = match->addrinfo; res; res = res->ai_next) {
if (_internal_addressmatches(hostports, res->ai_addr, match->prefixlen, checkport, NULL))
return 1;
}
}
return 0;
}
int addressmatches(struct list *hostports, struct sockaddr *addr, uint8_t checkport, struct hostportres **hp) {
return _internal_addressmatches(hostports, addr, 255, checkport, hp);
}
int connecttcphostlist(struct list *hostports, struct addrinfo *src, struct hostportres** hpreturn) {
int s;
struct list_node *entry;
struct hostportres *hp = NULL;
for (entry = list_first(hostports); entry; entry = list_next(entry)) {
hp = (struct hostportres *)entry->data;
debug(DBG_WARN, "connecttcphostlist: trying to open TCP connection to %s port %s", hp->host, hp->port);
if ((s = connecttcp(hp->addrinfo, src, list_count(hostports) > 1 ? 5 : 30)) >= 0) {
debug(DBG_WARN, "connecttcphostlist: TCP connection to %s port %s up", hp->host, hp->port);
if (hpreturn) *hpreturn = hp;
return s;
}
}
debug(DBG_ERR, "connecttcphostlist: failed");
return -1;
}
/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */