]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: MIT | |
2 | /* | |
3 | Copyright (c) 2007, 2008 by Juliusz Chroboczek | |
4 | Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek | |
5 | */ | |
6 | ||
7 | #ifndef BABEL_UTIL_H | |
8 | #define BABEL_UTIL_H | |
9 | ||
10 | #include "babeld.h" | |
11 | #include "babel_main.h" | |
12 | #include "log.h" | |
13 | #include "memory.h" | |
14 | ||
15 | DECLARE_MGROUP(BABELD); | |
16 | ||
17 | #if defined(i386) || defined(__mc68020__) || defined(__x86_64__) | |
18 | #define DO_NTOHS(_d, _s) do{ _d = ntohs(*(const unsigned short*)(_s)); }while(0) | |
19 | #define DO_NTOHL(_d, _s) do{ _d = ntohl(*(const unsigned*)(_s)); } while(0) | |
20 | #define DO_HTONS(_d, _s) do{ *(unsigned short*)(_d) = htons(_s); } while(0) | |
21 | #define DO_HTONL(_d, _s) do{ *(unsigned*)(_d) = htonl(_s); } while(0) | |
22 | /* Some versions of gcc seem to be buggy, and ignore the packed attribute. | |
23 | Disable this code until the issue is clarified. */ | |
24 | /* #elif defined __GNUC__*/ | |
25 | #else | |
26 | #define DO_NTOHS(_d, _s) \ | |
27 | do { short _dd; \ | |
28 | memcpy(&(_dd), (_s), 2); \ | |
29 | _d = ntohs(_dd); } while(0) | |
30 | #define DO_NTOHL(_d, _s) \ | |
31 | do { int _dd; \ | |
32 | memcpy(&(_dd), (_s), 4); \ | |
33 | _d = ntohl(_dd); } while(0) | |
34 | #define DO_HTONS(_d, _s) \ | |
35 | do { unsigned short _dd; \ | |
36 | _dd = htons(_s); \ | |
37 | memcpy((_d), &(_dd), 2); } while(0) | |
38 | #define DO_HTONL(_d, _s) \ | |
39 | do { unsigned _dd; \ | |
40 | _dd = htonl(_s); \ | |
41 | memcpy((_d), &(_dd), 4); } while(0) | |
42 | #endif | |
43 | ||
44 | static inline int | |
45 | seqno_compare(unsigned short s1, unsigned short s2) | |
46 | { | |
47 | if(s1 == s2) | |
48 | return 0; | |
49 | else | |
50 | return ((s2 - s1) & 0x8000) ? 1 : -1; | |
51 | } | |
52 | ||
53 | static inline short | |
54 | seqno_minus(unsigned short s1, unsigned short s2) | |
55 | { | |
56 | return (short)((s1 - s2) & 0xFFFF); | |
57 | } | |
58 | ||
59 | static inline unsigned short | |
60 | seqno_plus(unsigned short s, int plus) | |
61 | { | |
62 | return ((s + plus) & 0xFFFF); | |
63 | } | |
64 | ||
65 | /* Returns a time in microseconds on 32 bits (thus modulo 2^32, | |
66 | i.e. about 4295 seconds). */ | |
67 | static inline unsigned int | |
68 | time_us(const struct timeval t) | |
69 | { | |
70 | return (unsigned int) (t.tv_sec * 1000000 + t.tv_usec); | |
71 | } | |
72 | ||
73 | int roughly(int value); | |
74 | void timeval_minus(struct timeval *d, | |
75 | const struct timeval *s1, const struct timeval *s2); | |
76 | unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2) | |
77 | ATTRIBUTE ((pure)); | |
78 | void timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs); | |
79 | void set_timeout (struct timeval *timeout, int msecs); | |
80 | int timeval_compare(const struct timeval *s1, const struct timeval *s2) | |
81 | ATTRIBUTE ((pure)); | |
82 | void timeval_min(struct timeval *d, const struct timeval *s); | |
83 | void timeval_min_sec(struct timeval *d, time_t secs); | |
84 | int parse_nat(const char *string) ATTRIBUTE ((pure)); | |
85 | int parse_msec(const char *string) ATTRIBUTE ((pure)); | |
86 | unsigned char *mask_prefix(unsigned char *restrict ret, | |
87 | const unsigned char *restrict prefix, | |
88 | unsigned char plen); | |
89 | const char *format_address(const unsigned char *address); | |
90 | const char *format_prefix(const unsigned char *address, unsigned char prefix); | |
91 | const char *format_eui64(const unsigned char *eui); | |
92 | const char *format_thousands(unsigned int value); | |
93 | int parse_address(const char *address, unsigned char *addr_r, int *af_r); | |
94 | int parse_eui64(const char *eui, unsigned char *eui_r); | |
95 | int wait_for_fd(int direction, int fd, int msecs); | |
96 | int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure)); | |
97 | int linklocal(const unsigned char *address) ATTRIBUTE ((pure)); | |
98 | int v4mapped(const unsigned char *address) ATTRIBUTE ((pure)); | |
99 | void v4tov6(unsigned char *dst, const unsigned char *src); | |
100 | void inaddr_to_uchar(unsigned char *dest, const struct in_addr *src); | |
101 | void uchar_to_inaddr(struct in_addr *dest, const unsigned char *src); | |
102 | void in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src); | |
103 | void uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src); | |
104 | int daemonise(void); | |
105 | extern const unsigned char v4prefix[16]; | |
106 | ||
107 | /* If debugging is disabled, we want to avoid calling format_address | |
108 | for every omitted debugging message. So debug is a macro. But | |
109 | vararg macros are not portable. */ | |
110 | #if defined NO_DEBUG | |
111 | ||
112 | #define debugf(...) do {} while(0) | |
113 | ||
114 | #else /* NO_DEBUG */ | |
115 | ||
116 | /* some levels */ | |
117 | #define BABEL_DEBUG_COMMON (1 << 0) | |
118 | #define BABEL_DEBUG_KERNEL (1 << 1) | |
119 | #define BABEL_DEBUG_FILTER (1 << 2) | |
120 | #define BABEL_DEBUG_TIMEOUT (1 << 3) | |
121 | #define BABEL_DEBUG_IF (1 << 4) | |
122 | #define BABEL_DEBUG_ROUTE (1 << 5) | |
123 | #define BABEL_DEBUG_ALL (0xFFFF) | |
124 | ||
125 | #define debugf(level, ...) \ | |
126 | do { \ | |
127 | if (unlikely(debug & level)) \ | |
128 | zlog_debug(__VA_ARGS__); \ | |
129 | } while (0) | |
130 | ||
131 | #endif /* NO_DEBUG */ | |
132 | ||
133 | #endif /* BABEL_UTIL_H */ |