Blame view

util.c 12.6 KB
Juliusz Chroboczek committed
1
/*
Juliusz Chroboczek committed
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek committed
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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
Juliusz Chroboczek committed
29
#include <unistd.h>
Juliusz Chroboczek committed
30
#include <limits.h>
Matthieu Boutier committed
31
#include <assert.h>
Juliusz Chroboczek committed
32 33 34

#include <sys/types.h>
#include <sys/socket.h>
dermiste committed
35
#include <netinet/in.h>
Juliusz Chroboczek committed
36 37
#include <arpa/inet.h>

Juliusz Chroboczek committed
38
#include "babeld.h"
Juliusz Chroboczek committed
39
#include "util.h"
Juliusz Chroboczek committed
40

Juliusz Chroboczek committed
41 42
int
roughly(int value)
Juliusz Chroboczek committed
43
{
Juliusz Chroboczek committed
44 45 46 47 48 49
    if(value < 0)
        return -roughly(-value);
    else if(value <= 1)
        return value;
    else
        return value * 3 / 4 + random() % (value / 2);
Juliusz Chroboczek committed
50 51
}

Juliusz Chroboczek committed
52 53 54 55
void
timeval_minus(struct timeval *d,
              const struct timeval *s1, const struct timeval *s2)
{
Gabriel Kerneis committed
56
    if(s1->tv_usec >= s2->tv_usec) {
Juliusz Chroboczek committed
57 58 59 60 61 62 63 64
        d->tv_usec = s1->tv_usec - s2->tv_usec;
        d->tv_sec = s1->tv_sec - s2->tv_sec;
    } else {
        d->tv_usec = s1->tv_usec + 1000000 - s2->tv_usec;
        d->tv_sec = s1->tv_sec - s2->tv_sec - 1;
    }
}

Juliusz Chroboczek committed
65
unsigned
Juliusz Chroboczek committed
66 67
timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
{
Juliusz Chroboczek committed
68 69 70 71
    if(s1->tv_sec < s2->tv_sec)
        return 0;

    /* Avoid overflow. */
Juliusz Chroboczek committed
72 73 74
    if(s1->tv_sec - s2->tv_sec > 2000000)
        return 2000000000;

Juliusz Chroboczek committed
75
    if(s1->tv_sec > s2->tv_sec)
Juliusz Chroboczek committed
76 77 78
        return
            (unsigned)((unsigned)(s1->tv_sec - s2->tv_sec) * 1000 +
                       ((int)s1->tv_usec - s2->tv_usec) / 1000);
Juliusz Chroboczek committed
79 80 81 82 83

    if(s1->tv_usec <= s2->tv_usec)
        return 0;

    return (unsigned)(s1->tv_usec - s2->tv_usec) / 1000u;
Juliusz Chroboczek committed
84 85
}

Juliusz Chroboczek committed
86
void
Juliusz Chroboczek committed
87
timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs)
Juliusz Chroboczek committed
88 89 90 91 92 93 94 95 96 97 98 99
{
    int usecs;
    d->tv_sec = s->tv_sec + msecs / 1000;
    usecs = s->tv_usec + (msecs % 1000) * 1000;
    if(usecs < 1000000) {
        d->tv_usec = usecs;
    } else {
        d->tv_usec = usecs - 1000000;
        d->tv_sec++;
    }
}

Juliusz Chroboczek committed
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
int
timeval_compare(const struct timeval *s1, const struct timeval *s2)
{
    if(s1->tv_sec < s2->tv_sec)
        return -1;
    else if(s1->tv_sec > s2->tv_sec)
        return 1;
    else if(s1->tv_usec < s2->tv_usec)
        return -1;
    else if(s1->tv_usec > s2->tv_usec)
        return 1;
    else
        return 0;
}

/* {0, 0} represents infinity */
void
timeval_min(struct timeval *d, const struct timeval *s)
{
    if(s->tv_sec == 0)
        return;

    if(d->tv_sec == 0 || timeval_compare(d, s) > 0) {
        *d = *s;
    }
}

void
Juliusz Chroboczek committed
128
timeval_min_sec(struct timeval *d, time_t secs)
Juliusz Chroboczek committed
129 130 131 132 133 134 135
{
    if(d->tv_sec == 0 || d->tv_sec > secs) {
        d->tv_sec = secs;
        d->tv_usec = random() % 1000000;
    }
}

Juliusz Chroboczek committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* There's no good name for a positive int in C, call it nat. */
int
parse_nat(const char *string)
{
    long l;
    char *end;

    l = strtol(string, &end, 0);

    while(*end == ' ' || *end == '\t')
        end++;
    if(*end != '\0')
        return -1;

    if(l < 0 || l > INT_MAX)
        return -1;

    return (int)l;
}

Baptiste Jonglez committed
156 157
/* Given a fixed-point string such as "42.1337", returns 1000 times
   the value of the string, here 42133. */
Juliusz Chroboczek committed
158
int
Baptiste Jonglez committed
159
parse_thousands(const char *string)
Juliusz Chroboczek committed
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
{
    unsigned int in, fl;
    int i, j;

    in = fl = 0;
    i = 0;
    while(string[i] == ' ' || string[i] == '\t')
        i++;
    while(string[i] >= '0' && string[i] <= '9') {
        in = in * 10 + string[i] - '0';
        i++;
    }
    if(string[i] == '.') {
        i++;
        j = 0;
        while(string[i] >= '0' && string[i] <= '9') {
            fl = fl * 10 + string[i] - '0';
            i++;
            j++;
        }

        while(j > 3) {
            fl /= 10;
            j--;
        }
        while(j < 3) {
            fl *= 10;
            j++;
        }
    }

    while(string[i] == ' ' || string[i] == '\t')
        i++;

    if(string[i] == '\0')
        return in * 1000 + fl;

    return -1;
}

Juliusz Chroboczek committed
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
int
h2i(char c)
{
    if(c >= '0' && c <= '9')
	return c - '0';
    else if(c >= 'a' && c <= 'f')
	return c - 'a' + 10;
    else if(c >= 'A' && c <= 'F')
	return c - 'A' + 10;
    else
	return -1;
}

int
fromhex(unsigned char *dest, char *src, int n)
{
    int i;
    if(n % 2 != 0)
	return -1;
    for(i = 0; i < n/2; i++) {
	int a, b;
	a = h2i(src[i*2]);
	if(a < 0)
	    return -1;
	b = h2i(src[i*2 + 1]);
	if(b < 0)
	    return -1;
	dest[i] = a*16 + b;
    }
    return n/2;
}

Juliusz Chroboczek committed
232
void
Juliusz Chroboczek committed
233
do_debugf(int level, const char *format, ...)
Juliusz Chroboczek committed
234 235 236
{
    va_list args;
    va_start(args, format);
Juliusz Chroboczek committed
237
    if(debug >= level) {
Juliusz Chroboczek committed
238
        vfprintf(stderr, format, args);
Juliusz Chroboczek committed
239 240
        fflush(stderr);
    }
Juliusz Chroboczek committed
241 242 243
    va_end(args);
}

Juliusz Chroboczek committed
244
int
Juliusz Chroboczek committed
245 246
in_prefix(const unsigned char *restrict address,
          const unsigned char *restrict prefix, unsigned char plen)
Juliusz Chroboczek committed
247 248 249 250 251 252 253 254 255
{
    unsigned char m;

    if(plen > 128)
        plen = 128;

    if(memcmp(address, prefix, plen / 8) != 0)
        return 0;

Juliusz Chroboczek committed
256 257 258
    if(plen % 8 == 0)
        return 1;

Juliusz Chroboczek committed
259 260 261 262 263
    m = 0xFF << (8 - (plen % 8));

    return ((address[plen / 8] & m) == (prefix[plen / 8] & m));
}

Juliusz Chroboczek committed
264
unsigned char *
Juliusz Chroboczek committed
265 266
normalize_prefix(unsigned char *restrict ret,
                 const unsigned char *restrict prefix, unsigned char plen)
Juliusz Chroboczek committed
267
{
Juliusz Chroboczek committed
268 269 270 271
    if(plen >= 128) {
        memcpy(ret, prefix, 16);
        return ret;
    }
Juliusz Chroboczek committed
272 273 274 275

    memset(ret, 0, 16);
    memcpy(ret, prefix, plen / 8);
    if(plen % 8 != 0)
Juliusz Chroboczek committed
276 277
        ret[plen / 8] =
            (prefix[plen / 8] & ((0xFF << (8 - (plen % 8))) & 0xFF));
Juliusz Chroboczek committed
278
    return ret;
Juliusz Chroboczek committed
279 280
}

Dave Taht committed
281
const unsigned char v4prefix[16] =
Juliusz Chroboczek committed
282 283
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };

Juliusz Chroboczek committed
284 285 286
static const unsigned char llprefix[16] =
    {0xFE, 0x80};

Juliusz Chroboczek committed
287 288 289 290 291 292
const char *
format_address(const unsigned char *address)
{
    static char buf[4][INET6_ADDRSTRLEN];
    static int i = 0;
    i = (i + 1) % 4;
Juliusz Chroboczek committed
293
    if(v4mapped(address))
Matthieu Boutier committed
294
        inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
Juliusz Chroboczek committed
295
    else
Matthieu Boutier committed
296
        inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
Juliusz Chroboczek committed
297
    return buf[i];
Juliusz Chroboczek committed
298 299
}

Juliusz Chroboczek committed
300 301 302 303 304 305 306
const char *
format_prefix(const unsigned char *prefix, unsigned char plen)
{
    static char buf[4][INET6_ADDRSTRLEN + 4];
    static int i = 0;
    int n;
    i = (i + 1) % 4;
Juliusz Chroboczek committed
307 308 309 310 311 312 313 314 315
    if(plen >= 96 && v4mapped(prefix)) {
        inet_ntop(AF_INET, prefix + 12, buf[i], INET6_ADDRSTRLEN);
        n = strlen(buf[i]);
        snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen - 96);
    } else {
        inet_ntop(AF_INET6, prefix, buf[i], INET6_ADDRSTRLEN);
        n = strlen(buf[i]);
        snprintf(buf[i] + n, INET6_ADDRSTRLEN + 4 - n, "/%d", plen);
    }
Juliusz Chroboczek committed
316 317 318
    return buf[i];
}

Juliusz Chroboczek committed
319 320 321 322 323 324 325 326 327 328 329 330
const char *
format_eui64(const unsigned char *eui)
{
    static char buf[4][28];
    static int i = 0;
    i = (i + 1) % 4;
    snprintf(buf[i], 28, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
             eui[0], eui[1], eui[2], eui[3],
             eui[4], eui[5], eui[6], eui[7]);
    return buf[i];
}

Baptiste Jonglez committed
331 332 333
const char *
format_thousands(unsigned int value)
{
Juliusz Chroboczek committed
334
    static char buf[4][15];
Baptiste Jonglez committed
335 336
    static int i = 0;
    i = (i + 1) % 4;
Antonin Décimo committed
337
    snprintf(buf[i], 15, "%u.%.3u", value / 1000, value % 1000);
Baptiste Jonglez committed
338 339 340
    return buf[i];
}

Juliusz Chroboczek committed
341
int
Juliusz Chroboczek committed
342
parse_address(const char *address, unsigned char *addr_r, int *af_r)
Juliusz Chroboczek committed
343
{
Juliusz Chroboczek committed
344
    struct in_addr ina;
Juliusz Chroboczek committed
345 346
    struct in6_addr ina6;
    int rc;
Juliusz Chroboczek committed
347 348 349 350 351

    rc = inet_pton(AF_INET, address, &ina);
    if(rc > 0) {
        memcpy(addr_r, v4prefix, 12);
        memcpy(addr_r + 12, &ina, 4);
Juliusz Chroboczek committed
352
        if(af_r) *af_r = AF_INET;
Juliusz Chroboczek committed
353 354 355
        return 0;
    }

Juliusz Chroboczek committed
356 357 358
    rc = inet_pton(AF_INET6, address, &ina6);
    if(rc > 0) {
        memcpy(addr_r, &ina6, 16);
Juliusz Chroboczek committed
359
        if(af_r) *af_r = AF_INET6;
Juliusz Chroboczek committed
360 361 362 363 364 365 366
        return 0;
    }

    return -1;
}

int
Juliusz Chroboczek committed
367 368
parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
          int *af_r)
Juliusz Chroboczek committed
369 370 371 372
{
    char buf[INET6_ADDRSTRLEN];
    char *slash, *end;
    unsigned char prefix[16];
Juliusz Chroboczek committed
373
    long plen;
Juliusz Chroboczek committed
374
    int af;
Juliusz Chroboczek committed
375 376
    struct in_addr ina;
    struct in6_addr ina6;
Juliusz Chroboczek committed
377 378 379 380 381
    int rc;

    if(strcmp(net, "default") == 0) {
        memset(prefix, 0, 16);
        plen = 0;
Juliusz Chroboczek committed
382
        af = AF_INET6;
Juliusz Chroboczek committed
383 384 385
    } else {
        slash = strchr(net, '/');
        if(slash == NULL) {
Juliusz Chroboczek committed
386
            rc = parse_address(net, prefix, &af);
Juliusz Chroboczek committed
387 388 389 390 391 392 393 394
            if(rc < 0)
                return rc;
            plen = 128;
        } else {
            if(slash - net >= INET6_ADDRSTRLEN)
                return -1;
            memcpy(buf, net, slash - net);
            buf[slash - net] = '\0';
Juliusz Chroboczek committed
395 396 397 398 399 400 401 402
            rc = inet_pton(AF_INET, buf, &ina);
            if(rc > 0) {
                memcpy(prefix, v4prefix, 12);
                memcpy(prefix + 12, &ina, 4);
                plen = strtol(slash + 1, &end, 0);
                if(*end != '\0' || plen < 0 || plen > 32)
                    return -1;
                plen += 96;
Juliusz Chroboczek committed
403
                af = AF_INET;
Juliusz Chroboczek committed
404 405 406 407 408 409 410
            } else {
                rc = inet_pton(AF_INET6, buf, &ina6);
                if(rc > 0) {
                    memcpy(prefix, &ina6, 16);
                    plen = strtol(slash + 1, &end, 0);
                    if(*end != '\0' || plen < 0 || plen > 128)
                        return -1;
Juliusz Chroboczek committed
411
                    af = AF_INET6;
Juliusz Chroboczek committed
412 413 414 415
                } else {
                    return -1;
                }
            }
Juliusz Chroboczek committed
416 417
        }
    }
Juliusz Chroboczek committed
418
    normalize_prefix(prefix_r, prefix, plen);
Juliusz Chroboczek committed
419
    *plen_r = plen;
Juliusz Chroboczek committed
420
    if(af_r) *af_r = af;
Juliusz Chroboczek committed
421 422
    return 0;
}
Juliusz Chroboczek committed
423

Juliusz Chroboczek committed
424 425 426 427 428 429 430
int
parse_eui64(const char *eui, unsigned char *eui_r)
{
    int n;
    n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
               &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
               &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
Juliusz Chroboczek committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    if(n == 8)
        return 0;

    n = sscanf(eui, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx",
               &eui_r[0], &eui_r[1], &eui_r[2], &eui_r[3],
               &eui_r[4], &eui_r[5], &eui_r[6], &eui_r[7]);
    if(n == 8)
        return 0;

    n = sscanf(eui, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
               &eui_r[0], &eui_r[1], &eui_r[2],
               &eui_r[5], &eui_r[6], &eui_r[7]);
    if(n == 6) {
        eui_r[3] = 0xFF;
        eui_r[4] = 0xFE;
        return 0;
    }
    return -1;
Juliusz Chroboczek committed
449 450
}

Juliusz Chroboczek committed
451 452 453 454 455 456 457 458
int
wait_for_fd(int direction, int fd, int msecs)
{
    fd_set fds;
    int rc;
    struct timeval tv;

    tv.tv_sec = msecs / 1000;
Julien Cristau committed
459
    tv.tv_usec = (msecs % 1000) * 1000;
Juliusz Chroboczek committed
460 461 462 463 464 465 466 467 468 469

    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    if(direction)
        rc = select(fd + 1, NULL, &fds, NULL, &tv);
    else
        rc = select(fd + 1, &fds, NULL, NULL, &tv);

    return rc;
}
Juliusz Chroboczek committed
470 471 472 473

int
martian_prefix(const unsigned char *prefix, int plen)
{
Juliusz Chroboczek committed
474
    static const unsigned char ones[4] = {0xFF, 0xFF, 0xFF, 0xFF};
Juliusz Chroboczek committed
475 476 477
    return
        (plen >= 8 && prefix[0] == 0xFF) ||
        (plen >= 10 && prefix[0] == 0xFE && (prefix[1] & 0xC0) == 0x80) ||
Juliusz Chroboczek committed
478 479
        (plen >= 128 && memcmp(prefix, zeroes, 15) == 0 &&
         (prefix[15] == 0 || prefix[15] == 1)) ||
Juliusz Chroboczek committed
480
        (plen >= 96 && v4mapped(prefix) &&
Juliusz Chroboczek committed
481
         ((plen >= 104 && (prefix[12] == 127 || prefix[12] == 0)) ||
Juliusz Chroboczek committed
482 483
          (plen >= 100 && (prefix[12] & 0xF0) == 0xE0) ||
          (plen >= 128 && memcmp(prefix + 12, ones, 4) == 0)));
Juliusz Chroboczek committed
484
}
Juliusz Chroboczek committed
485

Juliusz Chroboczek committed
486 487 488 489 490 491
int
linklocal(const unsigned char *address)
{
    return memcmp(address, llprefix, 8) == 0;
}

Juliusz Chroboczek committed
492 493 494
int
v4mapped(const unsigned char *address)
{
Juliusz Chroboczek committed
495
    return memcmp(address, v4prefix, 12) == 0;
Juliusz Chroboczek committed
496
}
Juliusz Chroboczek committed
497 498 499 500 501 502 503

void
v4tov6(unsigned char *dst, const unsigned char *src)
{
    memcpy(dst, v4prefix, 12);
    memcpy(dst + 12, src, 4);
}
Julien Cristau committed
504

Juliusz Chroboczek committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
int
daemonise()
{
    int rc;

    fflush(stdout);
    fflush(stderr);

    rc = fork();
    if(rc < 0)
        return -1;

    if(rc > 0)
        exit(0);

    rc = setsid();
    if(rc < 0)
        return -1;

    return 1;
}
Matthieu Boutier committed
526 527 528 529 530

enum prefix_status
prefix_cmp(const unsigned char *p1, unsigned char plen1,
           const unsigned char *p2, unsigned char plen2)
{
Juliusz Chroboczek committed
531
    int plen = MIN(plen1, plen2);
Matthieu Boutier committed
532

Juliusz Chroboczek committed
533 534 535 536 537 538
    if(memcmp(p1, p2, plen / 8) != 0)
        return PST_DISJOINT;

    if(plen % 8 != 0) {
        int i = plen / 8 + 1;
        unsigned char mask = (0xFF << (plen % 8)) & 0xFF;
Matthieu Boutier committed
539 540 541 542 543 544
        if((p1[i] & mask) != (p2[i] & mask))
            return PST_DISJOINT;
    }

    if(plen1 < plen2)
        return PST_LESS_SPECIFIC;
Juliusz Chroboczek committed
545
    else if(plen1 > plen2)
Matthieu Boutier committed
546
        return PST_MORE_SPECIFIC;
Juliusz Chroboczek committed
547 548
    else
        return PST_EQUALS;
Matthieu Boutier committed
549
}