]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/util.h
1 // SPDX-License-Identifier: MIT
3 Copyright (c) 2007, 2008 by Juliusz Chroboczek
4 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
11 #include "babel_main.h"
15 DECLARE_MGROUP(BABELD
);
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__*/
26 #define DO_NTOHS(_d, _s) \
28 memcpy(&(_dd), (_s), 2); \
29 _d = ntohs(_dd); } while(0)
30 #define DO_NTOHL(_d, _s) \
32 memcpy(&(_dd), (_s), 4); \
33 _d = ntohl(_dd); } while(0)
34 #define DO_HTONS(_d, _s) \
35 do { unsigned short _dd; \
37 memcpy((_d), &(_dd), 2); } while(0)
38 #define DO_HTONL(_d, _s) \
41 memcpy((_d), &(_dd), 4); } while(0)
45 seqno_compare(unsigned short s1
, unsigned short s2
)
50 return ((s2
- s1
) & 0x8000) ? 1 : -1;
54 seqno_minus(unsigned short s1
, unsigned short s2
)
56 return (short)((s1
- s2
) & 0xFFFF);
59 static inline unsigned short
60 seqno_plus(unsigned short s
, int plus
)
62 return ((s
+ plus
) & 0xFFFF);
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
)
70 return (unsigned int) (t
.tv_sec
* 1000000 + t
.tv_usec
);
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
)
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
)
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
,
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
);
105 extern const unsigned char v4prefix
[16];
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. */
112 #define debugf(...) do {} while(0)
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)
125 #define debugf(level, ...) \
127 if(UNLIKELY(debug & level)) zlog_debug(__VA_ARGS__); \
130 #endif /* NO_DEBUG */
132 #endif /* BABEL_UTIL_H */