util.h 4.86 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#if defined __GNUC__
struct __us { unsigned short x __attribute__((packed)); };
#define DO_NTOHS(_d, _s) \
    do { _d = ntohs(((const struct __us*)(_s))->x); } while(0)
#define DO_HTONS(_d, _s) \
    do { ((struct __us*)(_d))->x = htons(_s); } while(0)
#else
#define DO_NTOHS(_d, _s) \
    do { short _dd; \
         memcpy(&(_dd), (_s), 2); \
         _d = ntohs(_dd); } while(0)
#define DO_HTONS(_d, _s) \
    do { unsigned short _dd; \
         _dd = htons(_s); \
         memcpy((_d), &(_dd), 2); } while(0)
#endif

40 41 42 43 44 45 46 47 48
static inline int
seqno_compare(unsigned short s1, unsigned short s2)
{
    if(s1 == s2)
        return 0;
    else
        return ((s2 - s1) & 0x8000) ? 1 : -1;
}

49
static inline short
50 51
seqno_minus(unsigned short s1, unsigned short s2)
{
52
    return (short)((s1 - s2) & 0xFFFF);
53 54 55 56 57 58 59 60
}

static inline unsigned short
seqno_plus(unsigned short s, int plus)
{
    return ((s + plus) & 0xFFFF);
}

61
unsigned roughly(unsigned value);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
62 63
void timeval_minus(struct timeval *d,
                   const struct timeval *s1, const struct timeval *s2);
64
unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
65
    ATTRIBUTE ((pure));
66 67
void timeval_plus_msec(struct timeval *d,
                       const struct timeval *s, int msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
68 69 70
int timeval_compare(const struct timeval *s1, const struct timeval *s2)
    ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
71
void timeval_min_sec(struct timeval *d, time_t secs);
72
int parse_msec(const char *string) ATTRIBUTE ((pure));
73
void do_debugf(int level, const char *format, ...)
74
    ATTRIBUTE ((format (printf, 2, 3))) COLD;
75 76
int in_prefix(const unsigned char *restrict address,
              const unsigned char *restrict prefix, unsigned char plen)
77
    ATTRIBUTE ((pure));
78 79 80
unsigned char *mask_prefix(unsigned char *restrict ret,
                           const unsigned char *restrict prefix,
                           unsigned char plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
81
const char *format_address(const unsigned char *address);
82
const char *format_prefix(const unsigned char *address, unsigned char prefix);
83
const char *format_eui64(const unsigned char *eui);
84 85 86
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);
87
int parse_eui64(const char *eui, unsigned char *eui_r);
88
int wait_for_fd(int direction, int fd, int msecs);
89 90
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
91
void v4tov6(unsigned char *dst, const unsigned char *src);
92
int daemonise(void);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
93 94 95

/* If debugging is disabled, we want to avoid calling format_address
   for every omitted debugging message.  So debug is a macro.  But
96
   vararg macros are not portable. */
97
#if defined NO_DEBUG
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define debugf(...) do {} while(0)
#define kdebugf(...) do {} while(0)
#elif defined __GNUC__
#define debugf(_args...) do {} while(0)
#define kdebugf(_args...) do {} while(0)
#else
static inline void debugf(const char *format, ...) { return; }
static inline void kdebugf(const char *format, ...) { return; }
#endif

#else

#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
113 114
#define debugf(...) \
    do { \
115
        if(UNLIKELY(debug >= 2)) do_debugf(2, __VA_ARGS__);     \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
116 117 118
    } while(0)
#define kdebugf(...) \
    do { \
119
        if(UNLIKELY(debug >= 3)) do_debugf(3, __VA_ARGS__);     \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
120
    } while(0)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
121
#elif defined __GNUC__
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
122 123
#define debugf(_args...) \
    do { \
124
        if(UNLIKELY(debug >= 2)) do_debugf(2, _args);   \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
125 126 127
    } while(0)
#define kdebugf(_args...) \
    do { \
128
        if(UNLIKELY(debug >= 3)) do_debugf(3, _args);   \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
129 130
    } while(0)
#else
131 132 133 134
static inline void debugf(const char *format, ...) { return; }
static inline void kdebugf(const char *format, ...) { return; }
#endif

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
135 136
#endif