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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/* $OpenBSD: util.c,v 1.4 2016/09/30 11:57:57 reyk Exp $ */
/*
* Copyright (c) 2013-2016 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <event.h>
#include "switchd.h"
extern int debug;
extern int verbose;
void
socket_set_blockmode(int fd, enum blockmodes bm)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
fatal("fcntl F_GETFL");
if (bm == BM_NONBLOCK)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
fatal("fcntl F_SETFL");
}
int
accept4_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
int flags, int reserve, volatile int *counter)
{
int fd;
if (getdtablecount() + reserve + *counter >= getdtablesize()) {
errno = EMFILE;
return (-1);
}
if ((fd = accept4(sockfd, addr, addrlen, flags)) != -1) {
(*counter)++;
DPRINTF("%s: inflight incremented, now %d",__func__, *counter);
}
return (fd);
}
in_port_t
socket_getport(struct sockaddr_storage *ss)
{
switch (ss->ss_family) {
case AF_INET:
return (ntohs(((struct sockaddr_in *)ss)->sin_port));
case AF_INET6:
return (ntohs(((struct sockaddr_in6 *)ss)->sin6_port));
default:
return (0);
}
/* NOTREACHED */
return (0);
}
int
sockaddr_cmp(struct sockaddr *a, struct sockaddr *b, int prefixlen)
{
struct sockaddr_in *a4, *b4;
struct sockaddr_in6 *a6, *b6;
struct sockaddr_un *au, *bu;
uint32_t av[4], bv[4], mv[4];
if (a->sa_family == AF_UNSPEC || b->sa_family == AF_UNSPEC)
return (0);
else if (a->sa_family > b->sa_family)
return (1);
else if (a->sa_family < b->sa_family)
return (-1);
if (prefixlen == -1)
memset(&mv, 0xff, sizeof(mv));
switch (a->sa_family) {
case AF_INET:
a4 = (struct sockaddr_in *)a;
b4 = (struct sockaddr_in *)b;
av[0] = a4->sin_addr.s_addr;
bv[0] = b4->sin_addr.s_addr;
if (prefixlen != -1)
mv[0] = prefixlen2mask(prefixlen);
if ((av[0] & mv[0]) > (bv[0] & mv[0]))
return (1);
if ((av[0] & mv[0]) < (bv[0] & mv[0]))
return (-1);
break;
case AF_INET6:
a6 = (struct sockaddr_in6 *)a;
b6 = (struct sockaddr_in6 *)b;
memcpy(&av, &a6->sin6_addr.s6_addr, 16);
memcpy(&bv, &b6->sin6_addr.s6_addr, 16);
if (prefixlen != -1)
prefixlen2mask6(prefixlen, mv);
if ((av[3] & mv[3]) > (bv[3] & mv[3]))
return (1);
if ((av[3] & mv[3]) < (bv[3] & mv[3]))
return (-1);
if ((av[2] & mv[2]) > (bv[2] & mv[2]))
return (1);
if ((av[2] & mv[2]) < (bv[2] & mv[2]))
return (-1);
if ((av[1] & mv[1]) > (bv[1] & mv[1]))
return (1);
if ((av[1] & mv[1]) < (bv[1] & mv[1]))
return (-1);
if ((av[0] & mv[0]) > (bv[0] & mv[0]))
return (1);
if ((av[0] & mv[0]) < (bv[0] & mv[0]))
return (-1);
break;
case AF_UNIX:
au = (struct sockaddr_un *)a;
bu = (struct sockaddr_un *)b;
return (strcmp(au->sun_path, bu->sun_path));
}
return (0);
}
uint32_t
prefixlen2mask(uint8_t prefixlen)
{
if (prefixlen == 0)
return (0);
if (prefixlen > 32)
prefixlen = 32;
return (htonl(0xffffffff << (32 - prefixlen)));
}
struct in6_addr *
prefixlen2mask6(uint8_t prefixlen, uint32_t *mask)
{
static struct in6_addr s6;
int i;
if (prefixlen > 128)
prefixlen = 128;
bzero(&s6, sizeof(s6));
for (i = 0; i < prefixlen / 8; i++)
s6.s6_addr[i] = 0xff;
i = prefixlen % 8;
if (i)
s6.s6_addr[prefixlen / 8] = 0xff00 >> i;
memcpy(mask, &s6, sizeof(s6));
return (&s6);
}
const char *
print_ether(const uint8_t *ea)
{
static char sbuf[SWITCHD_CYCLE_BUFFERS]
[ETHER_ADDR_LEN * 2 + 5 + 1];
static int idx = 0;
size_t len;
char *buf;
buf = sbuf[idx];
len = sizeof(sbuf[idx]);
if (++idx >= SWITCHD_CYCLE_BUFFERS)
idx = 0;
snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
return (buf);
}
const char *
print_host(struct sockaddr_storage *ss, char *buf, size_t len)
{
static char sbuf[SWITCHD_CYCLE_BUFFERS][NI_MAXHOST + 7];
struct sockaddr_un *un;
static int idx = 0;
char pbuf[7];
in_port_t port;
if (buf == NULL) {
buf = sbuf[idx];
len = sizeof(sbuf[idx]);
if (++idx >= SWITCHD_CYCLE_BUFFERS)
idx = 0;
}
if (ss->ss_family == AF_UNSPEC) {
strlcpy(buf, "any", len);
return (buf);
} else if (ss->ss_family == AF_UNIX) {
un = (struct sockaddr_un *)ss;
strlcpy(buf, un->sun_path, len);
return (buf);
}
if (getnameinfo((struct sockaddr *)ss, ss->ss_len,
buf, len, NULL, 0, NI_NUMERICHOST) != 0) {
buf[0] = '\0';
return (NULL);
}
if ((port = socket_getport(ss)) != 0) {
snprintf(pbuf, sizeof(pbuf), ":%d", port);
(void)strlcat(buf, pbuf, len);
}
return (buf);
}
const char *
print_map(unsigned int type, struct constmap *map)
{
unsigned int i;
static char buf[SWITCHD_CYCLE_BUFFERS][32];
static int idx = 0;
const char *name = NULL;
if (idx >= SWITCHD_CYCLE_BUFFERS)
idx = 0;
bzero(buf[idx], sizeof(buf[idx]));
for (i = 0; map[i].cm_name != NULL; i++) {
if (map[i].cm_type == type) {
name = map[i].cm_name;
break;
}
}
if (name == NULL)
snprintf(buf[idx], sizeof(buf[idx]), "<%u>", type);
else
strlcpy(buf[idx], name, sizeof(buf[idx]));
return (buf[idx++]);
}
void
getmonotime(struct timeval *tv)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
fatal("clock_gettime");
TIMESPEC_TO_TIMEVAL(tv, &ts);
}
void
print_debug(const char *emsg, ...)
{
va_list ap;
if (debug && verbose > 2) {
va_start(ap, emsg);
vfprintf(stderr, emsg, ap);
va_end(ap);
}
}
void
print_verbose(const char *emsg, ...)
{
va_list ap;
if (verbose) {
va_start(ap, emsg);
vfprintf(stderr, emsg, ap);
va_end(ap);
}
}
void
print_hex(uint8_t *buf, off_t offset, size_t length)
{
unsigned int i;
extern int verbose;
if (verbose < 3 || !length)
return;
for (i = 0; i < length; i++) {
if (i && (i % 4) == 0) {
if ((i % 32) == 0)
print_debug("\n");
else
print_debug(" ");
}
print_debug("%02x", buf[offset + i]);
}
print_debug("\n");
}
int
parsehostport(const char *str, struct sockaddr *sa, socklen_t salen)
{
char buf[NI_MAXHOST + NI_MAXSERV + 8], *servp, *nodep;
struct addrinfo hints, *ai;
if (strlcpy(buf, str, sizeof(buf)) >= sizeof(buf))
return (-1);
memset(&hints, 0, sizeof(hints));
hints.ai_flags = 0;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (buf[0] == '[' &&
(servp = strchr(buf, ']')) != NULL &&
(*(servp + 1) == '\0' || *(servp + 1) == ':')) {
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
nodep = buf + 1;
*servp++ = '\0';
} else {
nodep = buf;
servp = strrchr(nodep, ':');
}
if (servp != NULL) {
*servp = '\0';
servp++;
} else
servp = NULL;
if (getaddrinfo(nodep, servp, &hints, &ai) != 0)
return (-1);
if (salen < ai->ai_addrlen) {
freeaddrinfo(ai);
return (-1);
}
memset(sa, 0, salen);
memcpy(sa, ai->ai_addr, ai->ai_addrlen);
return (0);
}
|