]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/util.h
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
3 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "babel_main.h"
31 #if defined(i386) || defined(__mc68020__) || defined(__x86_64__)
32 #define DO_NTOHS(_d, _s) do{ _d = ntohs(*(const unsigned short*)(_s)); }while(0)
33 #define DO_NTOHL(_d, _s) do{ _d = ntohl(*(const unsigned*)(_s)); } while(0)
34 #define DO_HTONS(_d, _s) do{ *(unsigned short*)(_d) = htons(_s); } while(0)
35 #define DO_HTONL(_d, _s) do{ *(unsigned*)(_d) = htonl(_s); } while(0)
36 /* Some versions of gcc seem to be buggy, and ignore the packed attribute.
37 Disable this code until the issue is clarified. */
38 /* #elif defined __GNUC__*/
40 #define DO_NTOHS(_d, _s) \
42 memcpy(&(_dd), (_s), 2); \
43 _d = ntohs(_dd); } while(0)
44 #define DO_NTOHL(_d, _s) \
46 memcpy(&(_dd), (_s), 4); \
47 _d = ntohl(_dd); } while(0)
48 #define DO_HTONS(_d, _s) \
49 do { unsigned short _dd; \
51 memcpy((_d), &(_dd), 2); } while(0)
52 #define DO_HTONL(_d, _s) \
55 memcpy((_d), &(_dd), 4); } while(0)
59 seqno_compare(unsigned short s1
, unsigned short s2
)
64 return ((s2
- s1
) & 0x8000) ? 1 : -1;
68 seqno_minus(unsigned short s1
, unsigned short s2
)
70 return (short)((s1
- s2
) & 0xFFFF);
73 static inline unsigned short
74 seqno_plus(unsigned short s
, int plus
)
76 return ((s
+ plus
) & 0xFFFF);
79 /* Returns a time in microseconds on 32 bits (thus modulo 2^32,
80 i.e. about 4295 seconds). */
81 static inline unsigned int
82 time_us(const struct timeval t
)
84 return (unsigned int) (t
.tv_sec
* 1000000 + t
.tv_usec
);
87 int roughly(int value
);
88 void timeval_minus(struct timeval
*d
,
89 const struct timeval
*s1
, const struct timeval
*s2
);
90 unsigned timeval_minus_msec(const struct timeval
*s1
, const struct timeval
*s2
)
92 void timeval_add_msec(struct timeval
*d
, const struct timeval
*s
, int msecs
);
93 void set_timeout (struct timeval
*timeout
, int msecs
);
94 int timeval_compare(const struct timeval
*s1
, const struct timeval
*s2
)
96 void timeval_min(struct timeval
*d
, const struct timeval
*s
);
97 void timeval_min_sec(struct timeval
*d
, time_t secs
);
98 int parse_nat(const char *string
) ATTRIBUTE ((pure
));
99 int parse_msec(const char *string
) ATTRIBUTE ((pure
));
100 int in_prefix(const unsigned char *restrict address
,
101 const unsigned char *restrict prefix
, unsigned char plen
)
103 unsigned char *mask_prefix(unsigned char *restrict ret
,
104 const unsigned char *restrict prefix
,
106 const char *format_address(const unsigned char *address
);
107 const char *format_prefix(const unsigned char *address
, unsigned char prefix
);
108 const char *format_eui64(const unsigned char *eui
);
109 const char *format_thousands(unsigned int value
);
110 int parse_address(const char *address
, unsigned char *addr_r
, int *af_r
);
111 int parse_eui64(const char *eui
, unsigned char *eui_r
);
112 int wait_for_fd(int direction
, int fd
, int msecs
);
113 int martian_prefix(const unsigned char *prefix
, int plen
) ATTRIBUTE ((pure
));
114 int linklocal(const unsigned char *address
) ATTRIBUTE ((pure
));
115 int v4mapped(const unsigned char *address
) ATTRIBUTE ((pure
));
116 void v4tov6(unsigned char *dst
, const unsigned char *src
);
117 void inaddr_to_uchar(unsigned char *dest
, const struct in_addr
*src
);
118 void uchar_to_inaddr(struct in_addr
*dest
, const unsigned char *src
);
119 void in6addr_to_uchar(unsigned char *dest
, const struct in6_addr
*src
);
120 void uchar_to_in6addr(struct in6_addr
*dest
, const unsigned char *src
);
122 const unsigned char v4prefix
[16];
124 /* If debugging is disabled, we want to avoid calling format_address
125 for every omitted debugging message. So debug is a macro. But
126 vararg macros are not portable. */
129 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
130 #define debugf(...) do {} while(0)
131 #elif defined __GNUC__
132 #define debugf(_args...) do {} while(0)
134 static inline void debugf(int level
, const char *format
, ...) { return; }
140 #define BABEL_DEBUG_COMMON (1 << 0)
141 #define BABEL_DEBUG_KERNEL (1 << 1)
142 #define BABEL_DEBUG_FILTER (1 << 2)
143 #define BABEL_DEBUG_TIMEOUT (1 << 3)
144 #define BABEL_DEBUG_IF (1 << 4)
145 #define BABEL_DEBUG_ROUTE (1 << 5)
146 #define BABEL_DEBUG_ALL (0xFFFF)
148 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
149 #define debugf(level, ...) \
151 if(UNLIKELY(debug & level)) zlog_debug(__VA_ARGS__); \
153 #elif defined __GNUC__
154 #define debugf(level, _args...) \
156 if(UNLIKELY(debug & level)) zlog_debug(_args); \
159 static inline void debugf(int level
, const char *format
, ...) { return; }
162 #endif /* NO_DEBUG */
164 #endif /* BABEL_UTIL_H */