util.h 3.63 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1
/*
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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.
*/

23
int seqno_compare(unsigned short s1, unsigned short s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
24
    ATTRIBUTE ((const));
25
int seqno_minus(unsigned short s1, unsigned short s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
26
    ATTRIBUTE ((const));
27 28
unsigned short seqno_plus(unsigned short s, int plus)
    ATTRIBUTE ((const));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
29 30 31 32
void timeval_minus(struct timeval *d,
                   const struct timeval *s1, const struct timeval *s2);
int timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
    ATTRIBUTE ((pure));
33 34
void timeval_plus_msec(struct timeval *d,
                       const struct timeval *s, int msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
35 36 37
int timeval_compare(const struct timeval *s1, const struct timeval *s2)
    ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
38
void timeval_min_sec(struct timeval *d, time_t secs);
39
int parse_msec(const char *string) ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
40 41
void do_debugf(int leve, const char *format, ...)
    ATTRIBUTE ((format (printf, 2, 3)));
42 43 44
int in_prefix(const unsigned char *address,
              const unsigned char *prefix, unsigned char plen)
    ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
45 46
const unsigned char *mask_prefix(unsigned char *ret,
                                 const unsigned char *prefix,
47
                                 unsigned char plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
48
const char *format_address(const unsigned char *address);
49
const char *format_prefix(const unsigned char *address, unsigned char prefix);
50 51 52
int parse_address(const char *address, unsigned char *addr_r, int *af_r);
int parse_net(const char *net, unsigned char *prefix_r, unsigned char *plen_r,
              int *af_r);
53
int wait_for_fd(int direction, int fd, int msecs);
54 55
int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure));
int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
56
void v4tov6(unsigned char *dst, const unsigned char *src);
57
int daemonise(void);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
58 59 60

/* If debugging is disabled, we want to avoid calling format_address
   for every omitted debugging message.  So debug is a macro.  But
61
   vararg macros are not portable. */
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
62 63 64
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define debugf(...) \
    do { \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
65 66 67 68 69
        if(debug >= 2) do_debugf(2, __VA_ARGS__);        \
    } while(0)
#define kdebugf(...) \
    do { \
        if(debug >= 3) do_debugf(3, __VA_ARGS__);        \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
70
    } while(0)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
71
#elif defined __GNUC__
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
72 73
#define debugf(_args...) \
    do { \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
74 75 76 77 78
        if(debug >= 2) do_debugf(2, _args);      \
    } while(0)
#define kdebugf(_args...) \
    do { \
        if(debug >= 3) do_debugf(3, _args);      \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
79 80
    } while(0)
#else
81 82 83
/* Disable debugging code */
static void debugf(const char *format, ...) { return; }
static void kdebugf(const char *format, ...) { return; }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
84 85
#endif