util.h 6.06 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
#if defined(i386) || defined(__mc68020__) || defined(__x86_64__)
24 25 26 27
#define DO_NTOHS(_d, _s) do{ _d = ntohs(*(const unsigned short*)(_s)); }while(0)
#define DO_NTOHL(_d, _s) do{ _d = ntohl(*(const unsigned*)(_s)); } while(0)
#define DO_HTONS(_d, _s) do{ *(unsigned short*)(_d) = htons(_s); } while(0)
#define DO_HTONL(_d, _s) do{ *(unsigned*)(_d) = htonl(_s); } while(0)
28 29 30
/* Some versions of gcc seem to be buggy, and ignore the packed attribute.
   Disable this code until the issue is clarified. */
/* #elif defined __GNUC__*/
31 32
#else
#define DO_NTOHS(_d, _s) \
33
    do { unsigned short _dd; \
34 35
         memcpy(&(_dd), (_s), 2); \
         _d = ntohs(_dd); } while(0)
36
#define DO_NTOHL(_d, _s) \
37
    do { unsigned int _dd; \
38 39
         memcpy(&(_dd), (_s), 4); \
         _d = ntohl(_dd); } while(0)
40 41 42 43
#define DO_HTONS(_d, _s) \
    do { unsigned short _dd; \
         _dd = htons(_s); \
         memcpy((_d), &(_dd), 2); } while(0)
44 45 46 47
#define DO_HTONL(_d, _s) \
    do { unsigned _dd; \
         _dd = htonl(_s); \
         memcpy((_d), &(_dd), 4); } while(0)
48 49
#endif

50 51 52 53 54 55 56 57 58
static inline int
seqno_compare(unsigned short s1, unsigned short s2)
{
    if(s1 == s2)
        return 0;
    else
        return ((s2 - s1) & 0x8000) ? 1 : -1;
}

59
static inline short
60 61
seqno_minus(unsigned short s1, unsigned short s2)
{
62
    return (short)((s1 - s2) & 0xFFFF);
63 64 65 66 67 68 69 70
}

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

71 72 73 74 75 76 77 78
/* Returns a time in microseconds on 32 bits (thus modulo 2^32,
   i.e. about 4295 seconds). */
static inline unsigned int
time_us(const struct timeval t)
{
    return (unsigned int) (t.tv_sec * 1000000 + t.tv_usec);
}

79
int roughly(int value);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
80 81
void timeval_minus(struct timeval *d,
                   const struct timeval *s1, const struct timeval *s2);
82
unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
83
    ATTRIBUTE ((pure));
84 85
void timeval_add_msec(struct timeval *d,
                      const struct timeval *s, int msecs);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
86 87 88
int timeval_compare(const struct timeval *s1, const struct timeval *s2)
    ATTRIBUTE ((pure));
void timeval_min(struct timeval *d, const struct timeval *s);
89
void timeval_min_sec(struct timeval *d, time_t secs);
90
int parse_nat(const char *string) ATTRIBUTE ((pure));
91
int parse_thousands(const char *string) ATTRIBUTE ((pure));
92
void do_debugf(int level, const char *format, ...)
93
    ATTRIBUTE ((format (printf, 2, 3))) COLD;
94 95
int in_prefix(const unsigned char *restrict address,
              const unsigned char *restrict prefix, unsigned char plen)
96
    ATTRIBUTE ((pure));
97 98 99
unsigned char *mask_prefix(unsigned char *restrict ret,
                           const unsigned char *restrict prefix,
                           unsigned char plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
100
const char *format_address(const unsigned char *address);
101
const char *format_prefix(const unsigned char *address, unsigned char prefix);
102
const char *format_eui64(const unsigned char *eui);
103
const char *format_thousands(unsigned int value);
104 105 106
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);
107
int parse_eui64(const char *eui, unsigned char *eui_r);
108
int wait_for_fd(int direction, int fd, int msecs);
109
int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure));
110
int linklocal(const unsigned char *address) ATTRIBUTE ((pure));
111
int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
112
void v4tov6(unsigned char *dst, const unsigned char *src);
113
int daemonise(void);
114
int set_src_prefix(unsigned char *src_addr, unsigned char *src_plen);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
115

116
enum prefix_status {
117 118 119 120
    PST_EQUALS = 0,
    PST_DISJOINT,
    PST_MORE_SPECIFIC,
    PST_LESS_SPECIFIC
121 122 123 124 125
};
enum prefix_status
prefix_cmp(const unsigned char *p1, unsigned char plen1,
           const unsigned char *p2, unsigned char plen2);

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
126 127
/* If debugging is disabled, we want to avoid calling format_address
   for every omitted debugging message.  So debug is a macro.  But
128
   vararg macros are not portable. */
129
#if defined NO_DEBUG
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

#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
145 146
#define debugf(...) \
    do { \
147
        if(UNLIKELY(debug >= 2)) do_debugf(2, __VA_ARGS__);     \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
148 149 150
    } while(0)
#define kdebugf(...) \
    do { \
151
        if(UNLIKELY(debug >= 3)) do_debugf(3, __VA_ARGS__);     \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
152
    } while(0)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
153
#elif defined __GNUC__
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
154 155
#define debugf(_args...) \
    do { \
156
        if(UNLIKELY(debug >= 2)) do_debugf(2, _args);   \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
157 158 159
    } while(0)
#define kdebugf(_args...) \
    do { \
160
        if(UNLIKELY(debug >= 3)) do_debugf(3, _args);   \
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
161 162
    } while(0)
#else
163 164 165 166
static inline void debugf(const char *format, ...) { return; }
static inline void kdebugf(const char *format, ...) { return; }
#endif

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
167 168
#endif