odhcp6c: Replace strerror(errno) with %m
[project/odhcp6c.git] / src / odhcp6c.c
1 /**
2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <time.h>
17 #include <errno.h>
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <syslog.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <stdbool.h>
29
30 #include <net/if.h>
31 #include <sys/syscall.h>
32 #include <arpa/inet.h>
33 #include <linux/if_addr.h>
34
35 #include "odhcp6c.h"
36 #include "ra.h"
37
38 #ifndef IN6_IS_ADDR_UNIQUELOCAL
39 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
40 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
41 == htonl (0xfc000000))
42 #endif
43
44 static void sighandler(int signal);
45 static int usage(void);
46
47 static uint8_t *state_data[_STATE_MAX] = {NULL};
48 static size_t state_len[_STATE_MAX] = {0};
49
50 static volatile bool signal_io = false;
51 static volatile bool signal_usr1 = false;
52 static volatile bool signal_usr2 = false;
53 static volatile bool signal_term = false;
54
55 static int urandom_fd = -1, allow_slaac_only = 0;
56 static bool bound = false, release = true, ra = false;
57 static time_t last_update = 0;
58 static char *ifname = NULL;
59
60 static unsigned int script_sync_delay = 10;
61 static unsigned int script_accu_delay = 1;
62
63 int main(_unused int argc, char* const argv[])
64 {
65 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
66 // Allocate resources
67 const char *pidfile = NULL;
68 const char *script = "/usr/sbin/odhcp6c-update";
69 ssize_t l;
70 uint8_t buf[134];
71 char *optpos;
72 uint16_t opttype;
73 uint16_t optlen;
74 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
75 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE;
76 int ia_pd_iaid_index = 0;
77 int sol_timeout = DHCPV6_SOL_MAX_RT;
78 int verbosity = 0;
79 bool help = false, daemonize = false;
80 int logopt = LOG_PID;
81 int c;
82 unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
83 unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME;
84 unsigned int ra_holdoff_interval = RA_MIN_ADV_INTERVAL;
85
86 while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
87 switch (c) {
88 case 'S':
89 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
90 break;
91
92 case 'N':
93 if (!strcmp(optarg, "force")) {
94 ia_na_mode = IA_MODE_FORCE;
95 allow_slaac_only = -1;
96 } else if (!strcmp(optarg, "none"))
97 ia_na_mode = IA_MODE_NONE;
98 else if (!strcmp(optarg, "try"))
99 ia_na_mode = IA_MODE_TRY;
100 else
101 help = true;
102 break;
103
104 case 'V':
105 l = script_unhexlify(buf, sizeof(buf), optarg);
106 if (!l)
107 help = true;
108
109 odhcp6c_add_state(STATE_VENDORCLASS, buf, l);
110 break;
111
112 case 'P':
113 if (ia_pd_mode == IA_MODE_NONE)
114 ia_pd_mode = IA_MODE_TRY;
115
116 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
117 allow_slaac_only = 10;
118
119 char *iaid_begin;
120 int iaid_len = 0;
121 int prefix_length = strtoul(optarg, &iaid_begin, 10);
122
123 if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') {
124 syslog(LOG_ERR, "invalid argument: '%s'", optarg);
125 return 1;
126 }
127
128 struct odhcp6c_request_prefix prefix = { 0, prefix_length };
129
130 if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1)
131 memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len);
132 else if (*iaid_begin == ':')
133 prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16));
134 else
135 prefix.iaid = htonl(++ia_pd_iaid_index);
136
137 odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix));
138 break;
139
140 case 'F':
141 allow_slaac_only = -1;
142 ia_pd_mode = IA_MODE_FORCE;
143 break;
144
145 case 'c':
146 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
147 if (l > 0) {
148 buf[0] = 0;
149 buf[1] = DHCPV6_OPT_CLIENTID;
150 buf[2] = 0;
151 buf[3] = l;
152 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
153 } else
154 help = true;
155 break;
156
157 case 'i':
158 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
159 help = true;
160 break;
161
162 case 'r':
163 optpos = optarg;
164 while (optpos[0]) {
165 opttype = htons(strtoul(optarg, &optpos, 10));
166 if (optpos == optarg)
167 break;
168 else if (optpos[0])
169 optarg = &optpos[1];
170
171 odhcp6c_add_state(STATE_ORO, &opttype, 2);
172 }
173 break;
174
175 case 'R':
176 client_options |= DHCPV6_STRICT_OPTIONS;
177 break;
178
179 case 'u':
180 optlen = htons(strlen(optarg));
181 odhcp6c_add_state(STATE_USERCLASS, &optlen, 2);
182 odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg));
183 break;
184
185 case 's':
186 script = optarg;
187 break;
188
189 case 'k':
190 release = false;
191 break;
192
193 case 't':
194 sol_timeout = atoi(optarg);
195 break;
196
197 case 'm':
198 ra_holdoff_interval = atoi(optarg);
199 break;
200
201 case 'L':
202 ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME;
203 break;
204
205 case 'e':
206 logopt |= LOG_PERROR;
207 break;
208
209 case 'd':
210 daemonize = true;
211 break;
212
213 case 'p':
214 pidfile = optarg;
215 break;
216
217 case 'f':
218 client_options &= ~DHCPV6_CLIENT_FQDN;
219 break;
220
221 case 'a':
222 client_options &= ~DHCPV6_ACCEPT_RECONFIGURE;
223 break;
224
225 case 'v':
226 ++verbosity;
227 break;
228
229 default:
230 help = true;
231 break;
232 }
233 }
234
235 if (allow_slaac_only > 0)
236 script_sync_delay = allow_slaac_only;
237
238 openlog("odhcp6c", logopt, LOG_DAEMON);
239 if (!verbosity)
240 setlogmask(LOG_UPTO(LOG_WARNING));
241
242 ifname = argv[optind];
243
244 if (help || !ifname)
245 return usage();
246
247 signal(SIGIO, sighandler);
248 signal(SIGHUP, sighandler);
249 signal(SIGINT, sighandler);
250 signal(SIGTERM, sighandler);
251 signal(SIGUSR1, sighandler);
252 signal(SIGUSR2, sighandler);
253
254 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
255 init_dhcpv6(ifname, client_options, sol_timeout) ||
256 ra_init(ifname, &ifid, ra_options, ra_holdoff_interval) ||
257 script_init(script, ifname)) {
258 syslog(LOG_ERR, "failed to initialize: %m");
259 return 3;
260 }
261
262 if (daemonize) {
263 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
264 if (daemon(0, 0)) {
265 syslog(LOG_ERR, "Failed to daemonize: %m");
266 return 4;
267 }
268
269 if (!pidfile) {
270 snprintf((char*)buf, sizeof(buf), "/var/run/odhcp6c.%s.pid", ifname);
271 pidfile = (char*)buf;
272 }
273
274 FILE *fp = fopen(pidfile, "w");
275 if (fp) {
276 fprintf(fp, "%i\n", getpid());
277 fclose(fp);
278 }
279 }
280
281 script_call("started", 0, false);
282
283 while (!signal_term) { // Main logic
284 odhcp6c_clear_state(STATE_SERVER_ID);
285 odhcp6c_clear_state(STATE_SERVER_ADDR);
286 odhcp6c_clear_state(STATE_IA_NA);
287 odhcp6c_clear_state(STATE_IA_PD);
288 odhcp6c_clear_state(STATE_SNTP_IP);
289 odhcp6c_clear_state(STATE_NTP_IP);
290 odhcp6c_clear_state(STATE_NTP_FQDN);
291 odhcp6c_clear_state(STATE_SIP_IP);
292 odhcp6c_clear_state(STATE_SIP_FQDN);
293 bound = false;
294
295 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
296
297 signal_usr1 = signal_usr2 = false;
298 int mode = dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
299 if (mode != DHCPV6_STATELESS)
300 mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
301
302 odhcp6c_signal_process();
303
304 if (mode < 0)
305 continue;
306
307 do {
308 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
309 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
310 bool signalled = odhcp6c_signal_process();
311
312 if (res > 0)
313 break;
314 else if (signalled) {
315 mode = -1;
316 break;
317 }
318
319 mode = dhcpv6_promote_server_cand();
320 } while (mode > DHCPV6_UNKNOWN);
321
322 if (mode < 0)
323 continue;
324
325 switch (mode) {
326 case DHCPV6_STATELESS:
327 bound = true;
328 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
329
330 while (!signal_usr2 && !signal_term) {
331 signal_usr1 = false;
332 script_call("informed", script_sync_delay, true);
333
334 int res = dhcpv6_poll_reconfigure();
335 odhcp6c_signal_process();
336
337 if (res > 0)
338 continue;
339
340 if (signal_usr1) {
341 signal_usr1 = false; // Acknowledged
342 continue;
343 }
344
345 if (signal_usr2 || signal_term)
346 break;
347
348 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
349 odhcp6c_signal_process();
350
351 if (signal_usr1)
352 continue;
353 else if (res < 0)
354 break;
355 }
356 break;
357
358 case DHCPV6_STATEFUL:
359 bound = true;
360 script_call("bound", script_sync_delay, true);
361 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
362
363 while (!signal_usr2 && !signal_term) {
364 // Renew Cycle
365 // Wait for T1 to expire or until we get a reconfigure
366 int res = dhcpv6_poll_reconfigure();
367 odhcp6c_signal_process();
368 if (res > 0) {
369 script_call("updated", 0, false);
370 continue;
371 }
372
373 // Handle signal, if necessary
374 if (signal_usr1)
375 signal_usr1 = false; // Acknowledged
376
377 if (signal_usr2 || signal_term)
378 break; // Other signal type
379
380 // Send renew as T1 expired
381 res = dhcpv6_request(DHCPV6_MSG_RENEW);
382 odhcp6c_signal_process();
383
384 if (res > 0) { // Renew was succesfull
385 // Publish updates
386 script_call("updated", 0, false);
387 continue; // Renew was successful
388 }
389
390 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
391 odhcp6c_clear_state(STATE_SERVER_ADDR);
392
393 size_t ia_pd_len, ia_na_len;
394 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
395 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
396
397 if (ia_pd_len == 0 && ia_na_len == 0)
398 break;
399
400 // If we have IAs, try rebind otherwise restart
401 res = dhcpv6_request(DHCPV6_MSG_REBIND);
402 odhcp6c_signal_process();
403
404 if (res > 0)
405 script_call("rebound", 0, true);
406 else
407 break;
408 }
409 break;
410
411 default:
412 break;
413 }
414
415 odhcp6c_expire();
416
417 size_t ia_pd_len, ia_na_len, server_id_len;
418 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
419 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
420 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
421
422 // Add all prefixes to lost prefixes
423 bound = false;
424 script_call("unbound", 0, true);
425
426 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
427 dhcpv6_request(DHCPV6_MSG_RELEASE);
428
429 odhcp6c_clear_state(STATE_IA_NA);
430 odhcp6c_clear_state(STATE_IA_PD);
431 }
432
433 script_call("stopped", 0, true);
434
435 return 0;
436 }
437
438 static int usage(void)
439 {
440 const char buf[] =
441 "Usage: odhcp6c [options] <interface>\n"
442 "\nFeature options:\n"
443 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
444 " -N <mode> Mode for requesting addresses [try|force|none]\n"
445 " -P <length> Request IPv6-Prefix (0 = auto)\n"
446 " -F Force IPv6-Prefix\n"
447 " -V <class> Set vendor-class option (base-16 encoded)\n"
448 " -u <user-class> Set user-class option string\n"
449 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
450 " -i <iface-id> Use a custom interface identifier for RA handling\n"
451 " -r <options> Options to be requested (comma-separated)\n"
452 " -R Do not request any options except those specified with -r\n"
453 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
454 " -a Don't send Accept Reconfigure option\n"
455 " -f Don't send Client FQDN option\n"
456 " -k Don't send a RELEASE when stopping\n"
457 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
458 " -m <seconds> Minimum time between accepting RA updates (3)\n"
459 " -L Ignore default lifetime for RDNSS records\n"
460 "\nInvocation options:\n"
461 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
462 " -d Daemonize\n"
463 " -e Write logmessages to stderr\n"
464 " -v Increase logging verbosity\n"
465 " -h Show this help\n\n";
466 fputs(buf, stderr);
467
468 return 1;
469 }
470
471 // Don't want to pull-in librt and libpthread just for a monotonic clock...
472 uint64_t odhcp6c_get_milli_time(void)
473 {
474 struct timespec t = {0, 0};
475 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
476
477 return ((uint64_t)t.tv_sec) * 1000 + ((uint64_t)t.tv_nsec) / 1000000;
478 }
479
480 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
481 {
482 if (len == 0)
483 return state_data[state] + state_len[state];
484 else if (state_len[state] + len > 1024)
485 return NULL;
486
487 uint8_t *n = realloc(state_data[state], state_len[state] + len);
488
489 if (n || state_len[state] + len == 0) {
490 state_data[state] = n;
491 n += state_len[state];
492 state_len[state] += len;
493 }
494
495 return n;
496 }
497
498 bool odhcp6c_signal_process(void)
499 {
500 while (signal_io) {
501 signal_io = false;
502
503 bool ra_updated = ra_process();
504
505 if (ra_link_up()) {
506 signal_usr2 = true;
507 ra = false;
508 }
509
510 if (ra_updated && (bound || allow_slaac_only >= 0)) {
511 script_call("ra-updated", (!ra && !bound) ?
512 script_sync_delay : script_accu_delay, false);
513 ra = true;
514 }
515 }
516
517 return signal_usr1 || signal_usr2 || signal_term;
518 }
519
520 void odhcp6c_clear_state(enum odhcp6c_state state)
521 {
522 state_len[state] = 0;
523 }
524
525 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
526 {
527 uint8_t *n = odhcp6c_resize_state(state, len);
528
529 if (n)
530 memcpy(n, data, len);
531 }
532
533 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
534 {
535 ssize_t len_after = state_len[state] - offset;
536 if (len_after < 0)
537 return -1;
538
539 uint8_t *n = odhcp6c_resize_state(state, len);
540
541 if (n) {
542 uint8_t *sdata = state_data[state];
543
544 memmove(sdata + offset + len, sdata + offset, len_after);
545 memcpy(sdata + offset, data, len);
546 }
547
548 return 0;
549 }
550
551 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
552 {
553 uint8_t *data = state_data[state];
554 ssize_t len_after = state_len[state] - (offset + len);
555
556 if (len_after < 0)
557 return state_len[state];
558
559 memmove(data + offset, data + offset + len, len_after);
560
561 return state_len[state] -= len;
562 }
563
564 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
565 {
566 *len = state_len[state];
567 void *data = state_data[state];
568
569 state_len[state] = 0;
570 state_data[state] = NULL;
571
572 return data;
573 }
574
575 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
576 {
577 *len = state_len[state];
578
579 return state_data[state];
580 }
581
582 static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
583 {
584 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + ((new->length + 7) / 8);
585 uint8_t *start = odhcp6c_get_state(state, &len);
586
587 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
588 (uint8_t*)c < &start[len] &&
589 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
590 c = odhcp6c_next_entry(c)) {
591 if (!memcmp(c, new, cmplen) && !memcmp(c->auxtarget, new->auxtarget, new->auxlen))
592 return c;
593 }
594
595 return NULL;
596 }
597
598 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
599 uint32_t safe, unsigned int holdoff_interval)
600 {
601 size_t len;
602 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
603 uint8_t *start = odhcp6c_get_state(state, &len);
604
605 if (x && x->valid > new->valid && new->valid < safe)
606 new->valid = safe;
607
608 if (new->valid > 0) {
609 if (x) {
610 if (holdoff_interval && new->valid >= x->valid &&
611 new->valid != UINT32_MAX &&
612 new->valid - x->valid < holdoff_interval &&
613 new->preferred >= x->preferred &&
614 new->preferred != UINT32_MAX &&
615 new->preferred - x->preferred < holdoff_interval)
616 return false;
617
618 x->valid = new->valid;
619 x->preferred = new->preferred;
620 x->t1 = new->t1;
621 x->t2 = new->t2;
622 x->iaid = new->iaid;
623 } else
624 odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
625 } else if (x)
626 odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
627
628 return true;
629 }
630
631 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
632 {
633 size_t len;
634 uint8_t *start = odhcp6c_get_state(state, &len);
635
636 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
637 (uint8_t*)c < &start[len] &&
638 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
639 ) {
640 if (c->t1 < elapsed)
641 c->t1 = 0;
642 else if (c->t1 != UINT32_MAX)
643 c->t1 -= elapsed;
644
645 if (c->t2 < elapsed)
646 c->t2 = 0;
647 else if (c->t2 != UINT32_MAX)
648 c->t2 -= elapsed;
649
650 if (c->preferred < elapsed)
651 c->preferred = 0;
652 else if (c->preferred != UINT32_MAX)
653 c->preferred -= elapsed;
654
655 if (c->valid < elapsed)
656 c->valid = 0;
657 else if (c->valid != UINT32_MAX)
658 c->valid -= elapsed;
659
660 if (!c->valid) {
661 odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
662 start = odhcp6c_get_state(state, &len);
663 } else
664 c = odhcp6c_next_entry(c);
665 }
666 }
667
668 void odhcp6c_expire(void)
669 {
670 time_t now = odhcp6c_get_milli_time() / 1000;
671 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
672
673 last_update = now;
674
675 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
676 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
677 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
678 odhcp6c_expire_list(STATE_RA_SEARCH, elapsed);
679 odhcp6c_expire_list(STATE_IA_NA, elapsed);
680 odhcp6c_expire_list(STATE_IA_PD, elapsed);
681 }
682
683 uint32_t odhcp6c_elapsed(void)
684 {
685 return odhcp6c_get_milli_time() / 1000 - last_update;
686 }
687
688 int odhcp6c_random(void *buf, size_t len)
689 {
690 return read(urandom_fd, buf, len);
691 }
692
693 bool odhcp6c_is_bound(void)
694 {
695 return bound;
696 }
697
698 bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
699 {
700 FILE *fd = fopen("/proc/net/if_inet6", "r");
701 int len;
702 char buf[256];
703
704 if (fd == NULL)
705 return false;
706
707 while (fgets(buf, sizeof(buf), fd)) {
708 struct in6_addr inet6_addr;
709 uint32_t flags, dummy;
710 unsigned int i;
711 char name[IF_NAMESIZE], addr_buf[33];
712
713 len = strlen(buf);
714
715 if ((len <= 0) || buf[len - 1] != '\n')
716 return false;
717
718 buf[--len] = '\0';
719
720 if (sscanf(buf, "%s %x %x %x %x %s",
721 addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
722 return false;
723
724 if (strcmp(name, ifname) ||
725 (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
726 continue;
727
728 for (i = 0; i < strlen(addr_buf); i++) {
729 if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
730 return false;
731 }
732
733 memset(&inet6_addr, 0, sizeof(inet6_addr));
734 for (i = 0; i < (strlen(addr_buf) / 2); i++) {
735 unsigned char byte;
736 static const char hex[] = "0123456789abcdef";
737 byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
738 (index(hex, addr_buf[i * 2 + 1]) - hex);
739 inet6_addr.s6_addr[i] = byte;
740 }
741
742 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
743 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr)))
744 return true;
745 }
746
747 return false;
748 }
749
750 static void sighandler(int signal)
751 {
752 if (signal == SIGUSR1)
753 signal_usr1 = true;
754 else if (signal == SIGUSR2)
755 signal_usr2 = true;
756 else if (signal == SIGIO)
757 signal_io = true;
758 else
759 signal_term = true;
760 }