util.c 8.68 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
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
/*
Copyright (c) 2007 by Juliusz Chroboczek

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>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

34
#include "babel.h"
35
#include "util.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
36 37

int
38
seqno_compare(unsigned short s1, unsigned short s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
39 40 41
{
    if(s1 == s2)
        return 0;
42
    else if(((s2 - s1) & 0xFFFF) < 0x8000)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
43 44 45 46 47 48
        return -1;
    else
        return 1;
}

int
49
seqno_minus(unsigned short s1, unsigned short s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
50 51 52
{
    if(s1 == s2)
        return 0;
53 54
    else if(((s2 - s1) & 0xFFFF) < 0x8000)
        return -(int)((s2 - s1) & 0xFFFF);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
55
    else
56
        return ((s1 - s2) & 0xFFFF);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
57 58
}

59 60 61 62 63 64
unsigned short
seqno_plus(unsigned short s, int plus)
{
    return ((s + plus) & 0xFFFF);
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
void
timeval_minus(struct timeval *d,
              const struct timeval *s1, const struct timeval *s2)
{
    if(s1->tv_usec > s2->tv_usec) {
        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;
    }
}

int
timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
{
    return (s1->tv_sec - s2->tv_sec) * 1000 +
        (s1->tv_usec - s2->tv_usec) / 1000;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98
void
timeval_plus_msec(struct timeval *d, const struct timeval *s, int msecs)
{
    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's avatar
Juliusz Chroboczek committed
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
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
timeval_min_sec(struct timeval *d, int secs)
{
    if(d->tv_sec == 0 || d->tv_sec > secs) {
        d->tv_sec = secs;
        d->tv_usec = random() % 1000000;
    }
}

void
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
136
do_debugf(int level, const char *format, ...)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
137 138 139
{
    va_list args;
    va_start(args, format);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
140
    if(debug >= level)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
141 142 143 144 145
        vfprintf(stderr, format, args);
    va_end(args);
    fflush(stderr);
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
int
in_prefix(const unsigned char *address,
          const unsigned char *prefix, unsigned char plen)
{
    unsigned char m;

    if(plen > 128)
        plen = 128;

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

    m = 0xFF << (8 - (plen % 8));

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

const unsigned char *
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
164 165
mask_prefix(unsigned char *ret,
            const unsigned char *prefix, unsigned char plen)
166 167 168 169 170 171 172
{
    if(plen > 128)
        plen = 128;

    memset(ret, 0, 16);
    memcpy(ret, prefix, plen / 8);
    if(plen % 8 != 0)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
173
        ret[plen / 8] = (prefix[plen / 8] & (0xFF << (8 - (plen % 8))));
174 175 176
    return (const unsigned char *)ret;
}

177 178 179
static unsigned char v4prefix[16] =
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
180 181 182 183 184 185
const char *
format_address(const unsigned char *address)
{
    static char buf[4][INET6_ADDRSTRLEN];
    static int i = 0;
    i = (i + 1) % 4;
186 187 188 189 190
    if(v4mapped(address))
       inet_ntop(AF_INET, address + 12, buf[i], INET6_ADDRSTRLEN);
    else
       inet_ntop(AF_INET6, address, buf[i], INET6_ADDRSTRLEN);
    return buf[i];
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
191 192
}

193 194 195 196 197 198 199
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;
200 201 202 203 204 205 206 207 208
    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);
    }
209 210 211
    return buf[i];
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
212
int
213
parse_address(const char *address, unsigned char *addr_r, int *af_r)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
214
{
215
    struct in_addr ina;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
216 217
    struct in6_addr ina6;
    int rc;
218 219 220 221 222

    rc = inet_pton(AF_INET, address, &ina);
    if(rc > 0) {
        memcpy(addr_r, v4prefix, 12);
        memcpy(addr_r + 12, &ina, 4);
223
        if(af_r) *af_r = AF_INET;
224 225 226
        return 0;
    }

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
227 228 229
    rc = inet_pton(AF_INET6, address, &ina6);
    if(rc > 0) {
        memcpy(addr_r, &ina6, 16);
230
        if(af_r) *af_r = AF_INET6;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
231 232 233 234 235 236 237
        return 0;
    }

    return -1;
}

int
238 239
parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
          int *af_r)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
240 241 242 243
{
    char buf[INET6_ADDRSTRLEN];
    char *slash, *end;
    unsigned char prefix[16];
244
    long plen;
245
    int af;
246 247
    struct in_addr ina;
    struct in6_addr ina6;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
248 249 250 251 252 253 254 255
    int rc;

    if(strcmp(net, "default") == 0) {
        memset(prefix, 0, 16);
        plen = 0;
    } else {
        slash = strchr(net, '/');
        if(slash == NULL) {
256
            rc = parse_address(net, prefix, &af);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
257 258 259 260 261 262 263 264
            if(rc < 0)
                return rc;
            plen = 128;
        } else {
            if(slash - net >= INET6_ADDRSTRLEN)
                return -1;
            memcpy(buf, net, slash - net);
            buf[slash - net] = '\0';
265 266 267 268 269 270 271 272
            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;
273
                af = AF_INET;
274 275 276 277 278 279 280
            } 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;
281
                    af = AF_INET6;
282 283 284 285
                } else {
                    return -1;
                }
            }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
286 287
        }
    }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
288
    mask_prefix(prefix_r, prefix, plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
289
    *plen_r = plen;
290
    if(af_r) *af_r = af;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
291 292
    return 0;
}
293 294 295 296 297 298 299 300 301

int
wait_for_fd(int direction, int fd, int msecs)
{
    fd_set fds;
    int rc;
    struct timeval tv;

    tv.tv_sec = msecs / 1000;
302
    tv.tv_usec = (msecs % 1000) * 1000;
303 304 305 306 307 308 309 310 311 312

    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;
}
313 314 315 316 317 318 319

int
martian_prefix(const unsigned char *prefix, int plen)
{
    return
        (plen >= 8 && prefix[0] == 0xFF) ||
        (plen >= 10 && prefix[0] == 0xFE && (prefix[1] & 0xC0) == 0x80) ||
320 321
        (plen >= 128 && memcmp(prefix, zeroes, 15) == 0 &&
         (prefix[15] == 0 || prefix[15] == 1)) ||
322
        (plen >= 96 && v4mapped(prefix) &&
323 324 325
         ((plen >= 104 && (prefix[12] == 127 || prefix[12] == 0)) ||
          (plen >= 100 &&
           ((prefix[12] & 0xF0) == 0xF0 || (prefix[12] & 0xF0) == 0xE0))));
326
}
327 328 329 330 331 332

int
v4mapped(const unsigned char *address)
{
    return in_prefix(address, v4prefix, 96);
}
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
333 334 335 336 337 338 339

void
v4tov6(unsigned char *dst, const unsigned char *src)
{
    memcpy(dst, v4prefix, 12);
    memcpy(dst + 12, src, 4);
}
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

char *
parse_ifflags(unsigned int flags)
{
    static char buf[512];
    buf[0] = '\0';
    if (flags & IFF_UP)
        strcat(buf, "UP ");
    if (flags & IFF_BROADCAST)
        strcat(buf, "BROADCAST ");
    if (flags & IFF_RUNNING)
        strcat(buf, "RUNNING ");
    if (flags & IFF_MULTICAST)
        strcat(buf, "MULTICAST ");
    return buf;
}