Commit | Line | Data |
---|---|---|
b2a56f7c SE |
1 | /* |
2 | * util.c | |
3 | * - output and logging support | |
4 | * - program lifetime support | |
5 | * - IP address and subnet munging routines | |
6 | * - MPI convenience functions | |
7 | */ | |
8 | /* | |
c215a4bc IJ |
9 | * This file is part of secnet. |
10 | * See README for full list of copyright holders. | |
2fe58dfd | 11 | * |
c215a4bc IJ |
12 | * secnet is free software; you can redistribute it and/or modify it |
13 | * under the terms of the GNU General Public License as published by | |
9c6a8729 | 14 | * the Free Software Foundation; either version 3 of the License, or |
c215a4bc IJ |
15 | * (at your option) any later version. |
16 | * | |
17 | * secnet is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * version 3 along with secnet; if not, see | |
24 | * https://www.gnu.org/licenses/gpl.html. | |
2fe58dfd SE |
25 | */ |
26 | ||
8689b3a9 | 27 | #include "secnet.h" |
2fe58dfd | 28 | #include <stdio.h> |
2fe58dfd SE |
29 | #include <string.h> |
30 | #include <errno.h> | |
2fe58dfd | 31 | #include <unistd.h> |
59635212 | 32 | #include <limits.h> |
2fe58dfd | 33 | #include <assert.h> |
4efd681a | 34 | #include <sys/wait.h> |
61dbc9e0 | 35 | #include <adns.h> |
2fe58dfd | 36 | #include "util.h" |
59635212 | 37 | #include "unaligned.h" |
8534d602 | 38 | #include "magic.h" |
bb839899 | 39 | #include "ipaddr.h" |
2fe58dfd SE |
40 | |
41 | #define MIN_BUFFER_SIZE 64 | |
42 | #define DEFAULT_BUFFER_SIZE 4096 | |
43 | #define MAX_BUFFER_SIZE 131072 | |
44 | ||
fe5e9cc4 | 45 | static const char *hexdigits="0123456789abcdef"; |
2fe58dfd | 46 | |
7138d0c5 | 47 | uint32_t current_phase=0; |
2fe58dfd SE |
48 | |
49 | struct phase_hook { | |
50 | hook_fn *fn; | |
51 | void *state; | |
a614cf77 | 52 | LIST_ENTRY(phase_hook) entry; |
2fe58dfd SE |
53 | }; |
54 | ||
a614cf77 | 55 | static LIST_HEAD(, phase_hook) hooks[NR_PHASES]; |
2fe58dfd | 56 | |
fe5e9cc4 | 57 | char *safe_strdup(const char *s, const char *message) |
2fe58dfd SE |
58 | { |
59 | char *d; | |
60 | d=strdup(s); | |
61 | if (!d) { | |
779837e1 | 62 | fatal_perror("%s",message); |
2fe58dfd SE |
63 | } |
64 | return d; | |
65 | } | |
66 | ||
fe5e9cc4 | 67 | void *safe_malloc(size_t size, const char *message) |
2fe58dfd SE |
68 | { |
69 | void *r; | |
6f146b5d IJ |
70 | if (!size) |
71 | return 0; | |
2fe58dfd SE |
72 | r=malloc(size); |
73 | if (!r) { | |
779837e1 | 74 | fatal_perror("%s",message); |
2fe58dfd SE |
75 | } |
76 | return r; | |
77 | } | |
6f146b5d IJ |
78 | void *safe_realloc_ary(void *p, size_t size, size_t count, |
79 | const char *message) { | |
bb9d0561 IJ |
80 | if (count >= INT_MAX/size) { |
81 | fatal("array allocation overflow: %s", message); | |
82 | } | |
6f146b5d IJ |
83 | assert(size && count); |
84 | p = realloc(p, size*count); | |
85 | if (!p) | |
86 | fatal_perror("%s", message); | |
87 | return p; | |
88 | } | |
89 | ||
90 | void *safe_malloc_ary(size_t size, size_t count, const char *message) { | |
91 | if (!size || !count) | |
92 | return 0; | |
93 | return safe_realloc_ary(0,size,count,message); | |
bb9d0561 | 94 | } |
2fe58dfd | 95 | |
36a81f9c | 96 | void hex_encode(const uint8_t *bin, int binsize, char *buff) |
2fe58dfd | 97 | { |
2fe58dfd SE |
98 | int i; |
99 | ||
2fe58dfd SE |
100 | for (i=0; i<binsize; i++) { |
101 | buff[i*2]=hexdigits[(bin[i] & 0xf0) >> 4]; | |
102 | buff[i*2+1]=hexdigits[(bin[i] & 0xf)]; | |
103 | } | |
104 | buff[binsize*2]=0; | |
36a81f9c IJ |
105 | } |
106 | ||
107 | string_t hex_encode_alloc(const uint8_t *bin, int binsize) | |
108 | { | |
109 | char *buff; | |
110 | ||
111 | buff=safe_malloc(hex_encode_size(binsize),"hex_encode"); | |
112 | hex_encode(bin,binsize,buff); | |
2fe58dfd SE |
113 | return buff; |
114 | } | |
115 | ||
116 | static uint8_t hexval(uint8_t c) | |
117 | { | |
118 | switch (c) { | |
119 | case '0': return 0; | |
120 | case '1': return 1; | |
121 | case '2': return 2; | |
122 | case '3': return 3; | |
123 | case '4': return 4; | |
124 | case '5': return 5; | |
125 | case '6': return 6; | |
126 | case '7': return 7; | |
127 | case '8': return 8; | |
128 | case '9': return 9; | |
129 | case 'a': return 10; | |
130 | case 'A': return 10; | |
131 | case 'b': return 11; | |
132 | case 'B': return 11; | |
133 | case 'c': return 12; | |
134 | case 'C': return 12; | |
135 | case 'd': return 13; | |
136 | case 'D': return 13; | |
137 | case 'e': return 14; | |
138 | case 'E': return 14; | |
139 | case 'f': return 15; | |
140 | case 'F': return 15; | |
141 | } | |
142 | return -1; | |
143 | } | |
144 | ||
7be31e47 MW |
145 | bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen, |
146 | cstring_t hb, bool_t allow_odd_nibble) | |
2fe58dfd | 147 | { |
7be31e47 MW |
148 | int i = 0, j = 0, l = strlen(hb), hi, lo; |
149 | bool_t ok = False; | |
150 | ||
151 | if (!l || !buflen) { ok = !l; goto done; } | |
2fe58dfd SE |
152 | if (l&1) { |
153 | /* The number starts with a half-byte */ | |
7be31e47 MW |
154 | if (!allow_odd_nibble) goto done; |
155 | lo = hexval(hb[j++]); if (lo < 0) goto done; | |
156 | buffer[i++] = lo; | |
2fe58dfd | 157 | } |
7be31e47 MW |
158 | for (; hb[j] && i < buflen; i++) { |
159 | hi = hexval(hb[j++]); | |
160 | lo = hexval(hb[j++]); | |
161 | if (hi < 0 || lo < 0) goto done; | |
162 | buffer[i] = (hi << 4) | lo; | |
2fe58dfd | 163 | } |
7be31e47 MW |
164 | ok = !hb[j]; |
165 | done: | |
166 | *outlen = i; | |
167 | return ok; | |
168 | } | |
169 | ||
7be31e47 MW |
170 | void read_mpbin(MP_INT *a, uint8_t *bin, int binsize) |
171 | { | |
36a81f9c | 172 | char *buff = hex_encode_alloc(bin, binsize); |
7be31e47 MW |
173 | mpz_set_str(a, buff, 16); |
174 | free(buff); | |
175 | } | |
176 | ||
7be31e47 MW |
177 | char *write_mpstring(MP_INT *a) |
178 | { | |
179 | char *buff; | |
180 | ||
181 | buff=safe_malloc(mpz_sizeinbase(a,16)+2,"write_mpstring"); | |
182 | mpz_get_str(buff, 16, a); | |
183 | return buff; | |
184 | } | |
185 | ||
7be31e47 MW |
186 | int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen) |
187 | { | |
188 | char *hb = write_mpstring(a); | |
189 | int32_t len; | |
190 | hex_decode(buffer, buflen, &len, hb, True); | |
2fe58dfd | 191 | free(hb); |
7be31e47 | 192 | return len; |
2fe58dfd SE |
193 | } |
194 | ||
f54d5ada IJ |
195 | #define DEFINE_SETFDFLAG(fn,FL,FLAG) \ |
196 | void fn(int fd) { \ | |
197 | int r=fcntl(fd, F_GET##FL); \ | |
198 | if (r<0) fatal_perror("fcntl(,F_GET" #FL ") failed"); \ | |
199 | r=fcntl(fd, F_SET##FL, r|FLAG); \ | |
200 | if (r<0) fatal_perror("fcntl(,F_SET" #FL ",|" #FLAG ") failed"); \ | |
4fb0f88d IJ |
201 | } |
202 | ||
f54d5ada IJ |
203 | DEFINE_SETFDFLAG(setcloexec,FD,FD_CLOEXEC); |
204 | DEFINE_SETFDFLAG(setnonblock,FL,O_NONBLOCK); | |
205 | ||
6a06198c IJ |
206 | void pipe_cloexec(int fd[2]) { |
207 | int r=pipe(fd); | |
208 | if (r) fatal_perror("pipe"); | |
209 | setcloexec(fd[0]); | |
210 | setcloexec(fd[1]); | |
211 | } | |
212 | ||
fe5e9cc4 | 213 | static const char *phases[NR_PHASES]={ |
2fe58dfd SE |
214 | "PHASE_INIT", |
215 | "PHASE_GETOPTS", | |
216 | "PHASE_READCONFIG", | |
217 | "PHASE_SETUP", | |
7b1a9fb7 | 218 | "PHASE_DAEMONIZE", |
baa06aeb | 219 | "PHASE_GETRESOURCES", |
2fe58dfd SE |
220 | "PHASE_DROPPRIV", |
221 | "PHASE_RUN", | |
32654a31 IJ |
222 | "PHASE_SHUTDOWN", |
223 | "PHASE_CHILDPERSIST" | |
2fe58dfd SE |
224 | }; |
225 | ||
226 | void enter_phase(uint32_t new_phase) | |
227 | { | |
228 | struct phase_hook *i; | |
229 | ||
a614cf77 | 230 | if (!LIST_EMPTY(&hooks[new_phase])) |
baa06aeb | 231 | Message(M_DEBUG_PHASE,"Running hooks for %s...\n", phases[new_phase]); |
2fe58dfd SE |
232 | current_phase=new_phase; |
233 | ||
a614cf77 | 234 | LIST_FOREACH(i, &hooks[new_phase], entry) |
2fe58dfd | 235 | i->fn(i->state, new_phase); |
baa06aeb | 236 | Message(M_DEBUG_PHASE,"Now in %s\n",phases[new_phase]); |
2fe58dfd SE |
237 | } |
238 | ||
a614cf77 IJ |
239 | void phase_hooks_init(void) |
240 | { | |
241 | int i; | |
242 | for (i=0; i<NR_PHASES; i++) | |
243 | LIST_INIT(&hooks[i]); | |
244 | } | |
245 | ||
c72aa743 IJ |
246 | void clear_phase_hooks(uint32_t phase) |
247 | { | |
248 | struct phase_hook *h, *htmp; | |
249 | LIST_FOREACH_SAFE(h, &hooks[phase], entry, htmp) | |
250 | free(h); | |
251 | LIST_INIT(&hooks[phase]); | |
252 | } | |
253 | ||
2fe58dfd SE |
254 | bool_t add_hook(uint32_t phase, hook_fn *fn, void *state) |
255 | { | |
256 | struct phase_hook *h; | |
257 | ||
b7886fd4 | 258 | NEW(h); |
2fe58dfd SE |
259 | h->fn=fn; |
260 | h->state=state; | |
a614cf77 | 261 | LIST_INSERT_HEAD(&hooks[phase],h,entry); |
2fe58dfd SE |
262 | return True; |
263 | } | |
264 | ||
265 | bool_t remove_hook(uint32_t phase, hook_fn *fn, void *state) | |
266 | { | |
4f5e39ec | 267 | fatal("remove_hook: not implemented"); |
2fe58dfd SE |
268 | |
269 | return False; | |
270 | } | |
271 | ||
59938e0e IJ |
272 | void vslilog(struct log_if *lf, int priority, const char *message, va_list ap) |
273 | { | |
779837e1 | 274 | lf->vlogfn(lf->st,priority,message,ap); |
59938e0e IJ |
275 | } |
276 | ||
040ee979 | 277 | void slilog(struct log_if *lf, int priority, const char *message, ...) |
2fe58dfd SE |
278 | { |
279 | va_list ap; | |
280 | ||
281 | va_start(ap,message); | |
59938e0e | 282 | vslilog(lf,priority,message,ap); |
2fe58dfd SE |
283 | va_end(ap); |
284 | } | |
285 | ||
286 | struct buffer { | |
287 | closure_t cl; | |
288 | struct buffer_if ops; | |
289 | }; | |
290 | ||
fe5e9cc4 | 291 | void buffer_assert_free(struct buffer_if *buffer, cstring_t file, |
1caa23ff | 292 | int line) |
2fe58dfd SE |
293 | { |
294 | if (!buffer->free) { | |
28db900b IJ |
295 | fprintf(stderr,"secnet: BUF_ASSERT_FREE, %s line %d, owned by %s", |
296 | file,line,buffer->owner); | |
297 | assert(!"buffer_assert_free failure"); | |
2fe58dfd SE |
298 | } |
299 | } | |
300 | ||
fe5e9cc4 | 301 | void buffer_assert_used(struct buffer_if *buffer, cstring_t file, |
1caa23ff | 302 | int line) |
2fe58dfd SE |
303 | { |
304 | if (buffer->free) { | |
28db900b IJ |
305 | fprintf(stderr,"secnet: BUF_ASSERT_USED, %s line %d, last owned by %s", |
306 | file,line,buffer->owner); | |
307 | assert(!"buffer_assert_used failure"); | |
2fe58dfd SE |
308 | } |
309 | } | |
310 | ||
1caa23ff | 311 | void buffer_init(struct buffer_if *buffer, int32_t max_start_pad) |
2fe58dfd | 312 | { |
10068344 | 313 | assert(max_start_pad<=buffer->alloclen); |
2fe58dfd SE |
314 | buffer->start=buffer->base+max_start_pad; |
315 | buffer->size=0; | |
316 | } | |
317 | ||
46d06c39 IJ |
318 | void buffer_destroy(struct buffer_if *buf) |
319 | { | |
320 | BUF_ASSERT_FREE(buf); | |
321 | free(buf->base); | |
322 | buf->start=buf->base=0; | |
323 | buf->size=buf->alloclen=0; | |
324 | } | |
325 | ||
1caa23ff | 326 | void *buf_append(struct buffer_if *buf, int32_t amount) { |
2fe58dfd | 327 | void *p; |
92795040 | 328 | assert(amount <= buf_remaining_space(buf)); |
2fe58dfd SE |
329 | p=buf->start + buf->size; |
330 | buf->size+=amount; | |
331 | return p; | |
332 | } | |
333 | ||
1caa23ff | 334 | void *buf_prepend(struct buffer_if *buf, int32_t amount) { |
59230b9b | 335 | assert(amount <= buf->start - buf->base); |
2fe58dfd SE |
336 | buf->size+=amount; |
337 | return buf->start-=amount; | |
338 | } | |
339 | ||
1caa23ff | 340 | void *buf_unappend(struct buffer_if *buf, int32_t amount) { |
2fe58dfd SE |
341 | if (buf->size < amount) return 0; |
342 | return buf->start+(buf->size-=amount); | |
343 | } | |
344 | ||
1caa23ff | 345 | void *buf_unprepend(struct buffer_if *buf, int32_t amount) { |
2fe58dfd | 346 | void *p; |
20138876 | 347 | if (buf->size < amount) return 0; |
2fe58dfd SE |
348 | p=buf->start; |
349 | buf->start+=amount; | |
350 | buf->size-=amount; | |
351 | return p; | |
352 | } | |
353 | ||
fe5e9cc4 | 354 | void buf_append_string(struct buffer_if *buf, cstring_t s) |
2fe58dfd | 355 | { |
1caa23ff | 356 | size_t len; |
2fe58dfd SE |
357 | |
358 | len=strlen(s); | |
59230b9b | 359 | /* fixme: if string is longer than 65535, result is a corrupted packet */ |
59635212 | 360 | buf_append_uint16(buf,len); |
4f28e77e | 361 | BUF_ADD_BYTES(append,buf,s,len); |
2fe58dfd SE |
362 | } |
363 | ||
1caa23ff | 364 | void buffer_new(struct buffer_if *buf, int32_t len) |
2fe58dfd SE |
365 | { |
366 | buf->free=True; | |
367 | buf->owner=NULL; | |
368 | buf->flags=0; | |
369 | buf->loc.file=NULL; | |
370 | buf->loc.line=0; | |
371 | buf->size=0; | |
10068344 | 372 | buf->alloclen=len; |
2fe58dfd SE |
373 | buf->start=NULL; |
374 | buf->base=safe_malloc(len,"buffer_new"); | |
375 | } | |
376 | ||
28db900b IJ |
377 | void buffer_readonly_view(struct buffer_if *buf, const void *data, int32_t len) |
378 | { | |
379 | buf->free=False; | |
380 | buf->owner="READONLY"; | |
381 | buf->flags=0; | |
382 | buf->loc.file=NULL; | |
383 | buf->loc.line=0; | |
10068344 | 384 | buf->size=buf->alloclen=len; |
28db900b IJ |
385 | buf->base=buf->start=(uint8_t*)data; |
386 | } | |
387 | ||
388 | void buffer_readonly_clone(struct buffer_if *out, const struct buffer_if *in) | |
389 | { | |
390 | buffer_readonly_view(out,in->start,in->size); | |
391 | } | |
392 | ||
05f39b4d IJ |
393 | void buffer_copy(struct buffer_if *dst, const struct buffer_if *src) |
394 | { | |
10068344 IJ |
395 | if (dst->alloclen < src->alloclen) { |
396 | dst->base=realloc(dst->base,src->alloclen); | |
05f39b4d | 397 | if (!dst->base) fatal_perror("buffer_copy"); |
10068344 | 398 | dst->alloclen = src->alloclen; |
05f39b4d IJ |
399 | } |
400 | dst->start = dst->base + (src->start - src->base); | |
401 | dst->size = src->size; | |
402 | memcpy(dst->start, src->start, dst->size); | |
403 | } | |
404 | ||
2fe58dfd SE |
405 | static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context, |
406 | list_t *args) | |
407 | { | |
408 | struct buffer *st; | |
409 | item_t *item; | |
410 | dict_t *dict; | |
411 | bool_t lockdown=False; | |
4efd681a | 412 | uint32_t len=DEFAULT_BUFFER_SIZE; |
2fe58dfd | 413 | |
b7886fd4 | 414 | NEW(st); |
2fe58dfd SE |
415 | st->cl.description="buffer"; |
416 | st->cl.type=CL_BUFFER; | |
417 | st->cl.apply=NULL; | |
418 | st->cl.interface=&st->ops; | |
2fe58dfd SE |
419 | |
420 | /* First argument, if present, is buffer length */ | |
421 | item=list_elem(args,0); | |
422 | if (item) { | |
423 | if (item->type!=t_number) { | |
424 | cfgfatal(st->ops.loc,"buffer","first parameter must be a " | |
425 | "number (buffer size)\n"); | |
426 | } | |
4efd681a SE |
427 | len=item->data.number; |
428 | if (len<MIN_BUFFER_SIZE) { | |
2fe58dfd SE |
429 | cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n"); |
430 | } | |
4efd681a | 431 | if (len>MAX_BUFFER_SIZE) { |
2fe58dfd SE |
432 | cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n"); |
433 | } | |
434 | } | |
435 | /* Second argument, if present, is a dictionary */ | |
436 | item=list_elem(args,1); | |
437 | if (item) { | |
438 | if (item->type!=t_dict) { | |
439 | cfgfatal(st->ops.loc,"buffer","second parameter must be a " | |
440 | "dictionary\n"); | |
441 | } | |
442 | dict=item->data.dict; | |
443 | lockdown=dict_read_bool(dict,"lockdown",False,"buffer",st->ops.loc, | |
444 | False); | |
445 | } | |
446 | ||
4efd681a | 447 | buffer_new(&st->ops,len); |
2fe58dfd | 448 | if (lockdown) { |
70dc107b | 449 | /* XXX mlock the buffer if possible */ |
2fe58dfd SE |
450 | } |
451 | ||
452 | return new_closure(&st->cl); | |
453 | } | |
454 | ||
8534d602 IJ |
455 | void send_nak(const struct comm_addr *dest, uint32_t our_index, |
456 | uint32_t their_index, uint32_t msgtype, | |
457 | struct buffer_if *buf, const char *logwhy) | |
458 | { | |
3abd18e8 | 459 | buffer_init(buf,calculate_max_start_pad()); |
8534d602 IJ |
460 | buf_append_uint32(buf,their_index); |
461 | buf_append_uint32(buf,our_index); | |
462 | buf_append_uint32(buf,LABEL_NAK); | |
463 | if (logwhy) | |
464 | Message(M_INFO,"%s: %08"PRIx32"<-%08"PRIx32": %08"PRIx32":" | |
465 | " %s; sending NAK\n", | |
1a448682 | 466 | comm_addr_to_string(dest), |
8534d602 | 467 | our_index, their_index, msgtype, logwhy); |
2d80199d | 468 | dest->comm->sendmsg(dest->comm->st, buf, dest, 0); |
8534d602 IJ |
469 | } |
470 | ||
5ad34db2 IJ |
471 | int consttime_memeq(const void *s1in, const void *s2in, size_t n) |
472 | { | |
473 | const uint8_t *s1=s1in, *s2=s2in; | |
474 | register volatile uint8_t accumulator=0; | |
475 | ||
476 | while (n-- > 0) { | |
477 | accumulator |= (*s1++ ^ *s2++); | |
478 | } | |
479 | accumulator |= accumulator >> 4; /* constant-time */ | |
480 | accumulator |= accumulator >> 2; /* boolean canonicalisation */ | |
481 | accumulator |= accumulator >> 1; | |
482 | accumulator &= 1; | |
483 | accumulator ^= 1; | |
484 | return accumulator; | |
485 | } | |
486 | ||
2fe58dfd SE |
487 | void util_module(dict_t *dict) |
488 | { | |
2fe58dfd SE |
489 | add_closure(dict,"sysbuffer",buffer_apply); |
490 | } | |
3abd18e8 IJ |
491 | |
492 | void update_max_start_pad(int32_t *our_module_global, int32_t our_instance) | |
493 | { | |
494 | if (*our_module_global < our_instance) | |
495 | *our_module_global=our_instance; | |
496 | } | |
497 | ||
498 | int32_t transform_max_start_pad, comm_max_start_pad; | |
499 | ||
500 | int32_t calculate_max_start_pad(void) | |
501 | { | |
502 | return | |
503 | site_max_start_pad + | |
504 | transform_max_start_pad + | |
505 | comm_max_start_pad; | |
506 | } | |
ff1dcd86 IJ |
507 | |
508 | void vslilog_part(struct log_if *lf, int priority, const char *message, va_list ap) | |
509 | { | |
510 | char *buff=lf->buff; | |
511 | size_t bp; | |
512 | char *nlp; | |
513 | ||
514 | bp=strlen(buff); | |
515 | assert(bp < LOG_MESSAGE_BUFLEN); | |
516 | vsnprintf(buff+bp,LOG_MESSAGE_BUFLEN-bp,message,ap); | |
517 | buff[LOG_MESSAGE_BUFLEN-1] = '\n'; | |
518 | buff[LOG_MESSAGE_BUFLEN] = '\0'; | |
519 | /* Each line is sent separately */ | |
520 | while ((nlp=strchr(buff,'\n'))) { | |
521 | *nlp=0; | |
522 | slilog(lf,priority,"%s",buff); | |
523 | memmove(buff,nlp+1,strlen(nlp+1)+1); | |
524 | } | |
525 | } | |
526 | ||
527 | extern void slilog_part(struct log_if *lf, int priority, const char *message, ...) | |
528 | { | |
529 | va_list ap; | |
530 | va_start(ap,message); | |
531 | vslilog_part(lf,priority,message,ap); | |
532 | va_end(ap); | |
533 | } | |
a32d56fb | 534 | |
bb839899 IJ |
535 | void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia, |
536 | const char *desc) | |
537 | { | |
538 | #ifndef CONFIG_IPV6 | |
539 | ||
540 | ia->sin.sin_family=AF_INET; | |
4bdeab80 | 541 | ia->sin.sin_addr.s_addr=htonl(string_item_to_ipaddr(item,desc)); |
656df7e2 | 542 | ia->sin.sin_port=htons(port); |
bb839899 IJ |
543 | |
544 | #else /* CONFIG_IPV6 => we have adns_text2addr */ | |
545 | ||
546 | if (item->type!=t_string) | |
547 | cfgfatal(item->loc,desc,"expecting a string IP (v4 or v6) address\n"); | |
548 | socklen_t salen=sizeof(*ia); | |
549 | int r=adns_text2addr(item->data.string, port, | |
550 | adns_qf_addrlit_ipv4_quadonly, | |
551 | &ia->sa, &salen); | |
552 | assert(r!=ENOSPC); | |
553 | if (r) cfgfatal(item->loc,desc,"invalid IP (v4 or v6) address: %s\n", | |
554 | strerror(r)); | |
555 | ||
556 | #endif /* CONFIG_IPV6 */ | |
557 | } | |
558 | ||
5e7a63be | 559 | #define IADDR_NBUFS 8 |
771a583a | 560 | |
a32d56fb IJ |
561 | const char *iaddr_to_string(const union iaddr *ia) |
562 | { | |
61dbc9e0 IJ |
563 | #ifndef CONFIG_IPV6 |
564 | ||
5e7a63be | 565 | SBUF_DEFINE(IADDR_NBUFS, 100); |
61dbc9e0 | 566 | |
a32d56fb IJ |
567 | assert(ia->sa.sa_family == AF_INET); |
568 | ||
5f37eb10 | 569 | snprintf(SBUF, sizeof(SBUF), "[%s]:%d", |
a32d56fb IJ |
570 | inet_ntoa(ia->sin.sin_addr), |
571 | ntohs(ia->sin.sin_port)); | |
61dbc9e0 IJ |
572 | |
573 | #else /* CONFIG_IPV6 => we have adns_addr2text */ | |
574 | ||
5e7a63be | 575 | SBUF_DEFINE(IADDR_NBUFS, 1+ADNS_ADDR2TEXT_BUFLEN+20); |
61dbc9e0 IJ |
576 | |
577 | int port; | |
578 | ||
5f37eb10 | 579 | char *addrbuf = SBUF; |
61dbc9e0 IJ |
580 | *addrbuf++ = '['; |
581 | int addrbuflen = ADNS_ADDR2TEXT_BUFLEN; | |
582 | ||
583 | int r = adns_addr2text(&ia->sa, 0, addrbuf, &addrbuflen, &port); | |
584 | if (r) { | |
585 | const char fmt[]= "scoped IPv6 addr, error: %.*s"; | |
586 | sprintf(addrbuf, fmt, | |
887c6ac7 | 587 | (int)(ADNS_ADDR2TEXT_BUFLEN - sizeof(fmt)) /* underestimate */, |
61dbc9e0 IJ |
588 | strerror(r)); |
589 | } | |
590 | ||
591 | char *portbuf = addrbuf; | |
592 | int addrl = strlen(addrbuf); | |
593 | portbuf += addrl; | |
594 | ||
5f37eb10 | 595 | snprintf(portbuf, sizeof(SBUF)-addrl, "]:%d", port); |
61dbc9e0 IJ |
596 | |
597 | #endif /* CONFIG_IPV6 */ | |
598 | ||
5f37eb10 | 599 | return SBUF; |
a32d56fb IJ |
600 | } |
601 | ||
9c44ef13 IJ |
602 | bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib, |
603 | bool_t ignoreport) | |
a32d56fb IJ |
604 | { |
605 | if (ia->sa.sa_family != ib->sa.sa_family) | |
606 | return 0; | |
607 | switch (ia->sa.sa_family) { | |
608 | case AF_INET: | |
609 | return ia->sin.sin_addr.s_addr == ib->sin.sin_addr.s_addr | |
9c44ef13 IJ |
610 | && (ignoreport || |
611 | ia->sin.sin_port == ib->sin.sin_port); | |
61dbc9e0 IJ |
612 | #ifdef CONFIG_IPV6 |
613 | case AF_INET6: | |
614 | return !memcmp(&ia->sin6.sin6_addr, &ib->sin6.sin6_addr, 16) | |
9c44ef13 IJ |
615 | && ia->sin6.sin6_scope_id == ib->sin6.sin6_scope_id |
616 | && (ignoreport || | |
617 | ia->sin6.sin6_port == ib->sin6.sin6_port) | |
61dbc9e0 IJ |
618 | /* we ignore the flowinfo field */; |
619 | #endif /* CONFIG_IPV6 */ | |
a32d56fb IJ |
620 | default: |
621 | abort(); | |
622 | } | |
623 | } | |
624 | ||
625 | int iaddr_socklen(const union iaddr *ia) | |
626 | { | |
627 | switch (ia->sa.sa_family) { | |
628 | case AF_INET: return sizeof(ia->sin); | |
61dbc9e0 IJ |
629 | #ifdef CONFIG_IPV6 |
630 | case AF_INET6: return sizeof(ia->sin6); | |
631 | #endif /* CONFIG_IPV6 */ | |
a32d56fb IJ |
632 | default: abort(); |
633 | } | |
634 | } | |
a0fac2f1 | 635 | |
ae5ae3bf IJ |
636 | const char *pollbadbit(int revents) |
637 | { | |
638 | #define BADBIT(b) \ | |
639 | if ((revents & b)) return #b | |
640 | BADBIT(POLLERR); | |
641 | BADBIT(POLLHUP); | |
642 | /* POLLNVAL is handled by the event loop - see afterpoll_fn comment */ | |
643 | #undef BADBIT | |
644 | return 0; | |
645 | } | |
646 | ||
a0fac2f1 IJ |
647 | enum async_linebuf_result |
648 | async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf, | |
649 | const char **emsg_out) | |
650 | { | |
651 | int revents=pfd->revents; | |
652 | ||
653 | #define BAD(m) do{ *emsg_out=(m); return async_linebuf_broken; }while(0) | |
ae5ae3bf IJ |
654 | |
655 | const char *badbit=pollbadbit(revents); | |
656 | if (badbit) BAD(badbit); | |
a0fac2f1 IJ |
657 | |
658 | if (!(revents & POLLIN)) | |
659 | return async_linebuf_nothing; | |
660 | ||
661 | /* | |
662 | * Data structure: A line which has been returned to the user is | |
663 | * stored in buf at base before start. But we retain the usual | |
664 | * buffer meaning of size. So: | |
665 | * | |
666 | * | returned : | input read, | unused | | |
667 | * | to user : \0 | awaiting | buffer | | |
668 | * | : | processing | space | | |
669 | * | : | | | | |
670 | * ^base ^start ^start+size ^base+alloclen | |
671 | */ | |
672 | ||
673 | BUF_ASSERT_USED(buf); | |
674 | ||
675 | /* firstly, eat any previous */ | |
676 | if (buf->start != buf->base) { | |
677 | memmove(buf->base,buf->start,buf->size); | |
678 | buf->start=buf->base; | |
679 | } | |
680 | ||
681 | uint8_t *searched=buf->base; | |
682 | ||
683 | /* | |
684 | * During the workings here we do not use start. We set start | |
685 | * when we return some actual data. So we have this: | |
686 | * | |
687 | * | searched | read, might | unused | | |
688 | * | for \n | contain \n | buffer | | |
689 | * | none found | but not \0 | space | | |
690 | * | | | | | |
691 | * ^base ^searched ^base+size ^base+alloclen | |
692 | * [^start] ^dataend | |
693 | * | |
694 | */ | |
695 | for (;;) { | |
696 | uint8_t *dataend=buf->base+buf->size; | |
697 | char *newline=memchr(searched,'\n',dataend-searched); | |
698 | if (newline) { | |
699 | *newline=0; | |
700 | buf->start=newline+1; | |
701 | buf->size=dataend-buf->start; | |
702 | return async_linebuf_ok; | |
703 | } | |
704 | searched=dataend; | |
705 | ssize_t space=(buf->base+buf->alloclen)-dataend; | |
706 | if (!space) BAD("input line too long"); | |
707 | ssize_t r=read(pfd->fd,searched,space); | |
708 | if (r==0) { | |
709 | *searched=0; | |
710 | *emsg_out=buf->size?"no newline at eof":0; | |
711 | buf->start=searched+1; | |
712 | buf->size=0; | |
713 | return async_linebuf_eof; | |
714 | } | |
715 | if (r<0) { | |
716 | if (errno==EINTR) | |
717 | continue; | |
718 | if (iswouldblock(errno)) | |
719 | return async_linebuf_nothing; | |
720 | BAD(strerror(errno)); | |
721 | } | |
722 | assert(r<=space); | |
723 | if (memchr(searched,0,r)) BAD("nul in input data"); | |
724 | buf->size+=r; | |
725 | } | |
726 | ||
727 | #undef BAD | |
728 | } |