util.h 3.65 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
int roughly(int value);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
30 31 32 33
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));
34 35
void timeval_plus_msec(struct timeval *d,
                       const struct timeval *s, int msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
36 37 38
int timeval_compare(const struct timeval *s1, const struct timeval *s2)
    ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
39
void timeval_min_sec(struct timeval *d, time_t secs);
40
int parse_msec(const char *string) ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
41 42
void do_debugf(int leve, const char *format, ...)
    ATTRIBUTE ((format (printf, 2, 3)));
43 44 45
int in_prefix(const unsigned char *address,
              const unsigned char *prefix, unsigned char plen)
    ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
46 47
const unsigned char *mask_prefix(unsigned char *ret,
                                 const unsigned char *prefix,
48
                                 unsigned char plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
49
const char *format_address(const unsigned char *address);
50
const char *format_prefix(const unsigned char *address, unsigned char prefix);
51 52 53
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);
54
int wait_for_fd(int direction, int fd, int msecs);
55 56
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
57
void v4tov6(unsigned char *dst, const unsigned char *src);
58
int daemonise(void);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
59 60 61

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