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