-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
334 lines (267 loc) · 6.24 KB
/
utils.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <[email protected]>
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <libubox/utils.h>
#include "unetd.h"
int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
int default_port, int idx)
{
struct addrinfo hints = {
.ai_flags = AI_ADDRCONFIG,
.ai_family = af,
};
char *buf = strdup(str);
char *host = buf, *port;
struct addrinfo *ai, *ai_cur;
int n_res;
int ret = -1;
memset(dest, 0, sizeof(*dest));
if (*host == '[') {
if (af == AF_INET)
goto out;
host++;
port = strchr(host, ']');
if (!port)
goto out;
*(port++) = 0;
if (!*port)
port = NULL;
else if (*port == ':')
port++;
else
goto out;
hints.ai_family = AF_INET6;
hints.ai_flags |= AI_NUMERICHOST;
} else {
host = buf;
port = strchr(host, ':');
if (port)
*(port++) = 0;
}
if (getaddrinfo(host, port, &hints, &ai) || !ai)
goto out;
while (1) {
ai_cur = ai;
for (n_res = 0; ai_cur; ai_cur = ai_cur->ai_next, n_res++)
if (!idx--)
goto found;
idx %= n_res;
}
found:
if (ai_cur->ai_addrlen > sizeof(*dest))
goto free_ai;
memcpy(dest, ai_cur->ai_addr, ai_cur->ai_addrlen);
if (!port)
dest->in.sin_port = htons(default_port);
ret = 0;
free_ai:
freeaddrinfo(ai);
out:
free(buf);
return ret;
}
int network_get_subnet(int af, union network_addr *addr, int *mask, const char *str)
{
char *buf = strdup(str);
char *sep, *end;
int ret = -1;
if (af == AF_INET6)
*mask = 128;
else
*mask = 32;
sep = strchr(buf, '/');
if (sep) {
unsigned long val;
*(sep++) = 0;
val = strtoul(sep, &end, 0);
if ((end && *end) || val > *mask)
goto out;
*mask = val;
}
if (inet_pton(af, buf, addr) == 1)
ret = 0;
out:
free(buf);
return ret;
}
int network_get_local_addr(void *local, const union network_endpoint *target)
{
union network_endpoint ep = {};
socklen_t len;
int ret = -1;
int fd;
memset(local, 0, sizeof(union network_addr));
if (target->sa.sa_family == AF_INET6)
len = sizeof(ep.in6);
else
len = sizeof(ep.in);
fd = socket(target->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0)
return -1;
if (connect(fd, (const struct sockaddr *)target, len))
goto out;
len = sizeof(ep);
if (getsockname(fd, &ep.sa, &len))
goto out;
if (ep.sa.sa_family == AF_INET6)
memcpy(local, &ep.in6.sin6_addr, sizeof(ep.in6.sin6_addr));
else
memcpy(local, &ep.in.sin_addr, sizeof(ep.in.sin_addr));
ret = 0;
out:
close(fd);
return ret;
}
void *unet_read_file(const char *name, size_t *len)
{
struct stat st;
void *data;
FILE *f;
f = fopen(name, "r");
if (!f)
goto error;
if (fstat(fileno(f), &st) < 0)
goto close;
if (*len && st.st_size > *len)
goto close;
data = malloc(st.st_size);
if (!data)
goto close;
if (fread(data, 1, st.st_size, f) != st.st_size) {
free(data);
goto close;
}
fclose(f);
*len = st.st_size;
return data;
close:
fclose(f);
error:
*len = 0;
return NULL;
}
uint64_t unet_gettime(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec;
}
static inline uint32_t
csum_tcpudp_nofold(uint32_t saddr, uint32_t daddr, uint8_t proto, uint32_t len)
{
uint64_t sum = 0;
sum += saddr;
sum += daddr;
#if __BYTE_ORDER == __LITTLE_ENDIAN
sum += (proto + len) << 8;
#else
sum += proto + len;
#endif
sum = (sum & 0xffffffff) + (sum >> 32);
sum = (sum & 0xffffffff) + (sum >> 32);
return (uint32_t)sum;
}
static inline uint32_t csum_add(uint32_t sum, uint32_t addend)
{
sum += addend;
return sum + (sum < addend);
}
static inline uint16_t csum_fold(uint32_t sum)
{
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return (uint16_t)~sum;
}
static uint32_t csum_partial(const void *buf, int len)
{
const uint16_t *data = buf;
uint32_t sum = 0;
while (len > 1) {
sum += *data++;
len -= 2;
}
if (len == 1)
#if __BYTE_ORDER == __LITTLE_ENDIAN
sum += *(uint8_t *)data;
#else
sum += *(uint8_t *)data << 8;
#endif
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return sum;
}
static void fixup_udpv4(void *hdr, size_t hdrlen, const void *data, size_t len)
{
struct ip *ip = hdr;
struct udphdr *udp = hdr + ip->ip_hl * 4;
uint16_t udp_len = sizeof(*udp) + len;
uint32_t sum;
if ((void *)&udp[1] > hdr + hdrlen)
return;
udp->uh_sum = 0;
udp->uh_ulen = htons(udp_len);
sum = csum_tcpudp_nofold(*(uint32_t *)&ip->ip_src, *(uint32_t *)&ip->ip_dst,
ip->ip_p, udp_len);
sum = csum_add(sum, csum_partial(udp, sizeof(*udp)));
sum = csum_add(sum, csum_partial(data, len));
udp->uh_sum = csum_fold(sum);
ip->ip_len = htons(hdrlen + len);
ip->ip_sum = 0;
ip->ip_sum = csum_fold(csum_partial(ip, sizeof(*ip)));
#ifdef __APPLE__
ip->ip_len = hdrlen + len;
#endif
}
static void fixup_udpv6(void *hdr, size_t hdrlen, const void *data, size_t len)
{
struct ip6_hdr *ip = hdr;
struct udphdr *udp = hdr + sizeof(*ip);
uint16_t udp_len = htons(sizeof(*udp) + len);
if ((void *)&udp[1] > hdr + hdrlen)
return;
ip->ip6_plen = htons(sizeof(*udp) + len);
udp->uh_sum = 0;
udp->uh_ulen = udp_len;
udp->uh_sum = csum_fold(csum_partial(hdr, sizeof(*ip) + sizeof(*udp)));
#ifdef __APPLE__
ip->ip6_plen = sizeof(*udp) + len;
#endif
}
static void fixup_ip_udp_header(void *hdr, size_t hdrlen, const void *data, size_t len)
{
if (hdrlen >= sizeof(struct ip6_hdr) + sizeof(struct udphdr))
fixup_udpv6(hdr, hdrlen, data, len);
else if (hdrlen >= sizeof(struct ip) + sizeof(struct udphdr))
fixup_udpv4(hdr, hdrlen, data, len);
}
int sendto_rawudp(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen,
const void *data, size_t len)
{
const struct sockaddr *sa = addr;
struct iovec iov[2] = {
{ .iov_base = ip_hdr, .iov_len = ip_hdrlen },
{ .iov_base = (void *)data, .iov_len = len }
};
struct msghdr msg = {
.msg_name = (void *)addr,
.msg_iov = iov,
.msg_iovlen = ARRAY_SIZE(iov),
};
if (sa->sa_family == AF_INET6)
msg.msg_namelen = sizeof(struct sockaddr_in6);
else
msg.msg_namelen = sizeof(struct sockaddr_in);
fixup_ip_udp_header(ip_hdr, ip_hdrlen, data, len);
return sendmsg(fd, &msg, 0);
}