]> git.proxmox.com Git - mirror_frr.git/blame - babeld/util.h
Merge pull request #13484 from sri-mohan1/srib-ldpd
[mirror_frr.git] / babeld / util.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: MIT
ca10883e
DS
2/*
3Copyright (c) 2007, 2008 by Juliusz Chroboczek
4Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
ca10883e
DS
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"
8b7454e9
DL
13#include "memory.h"
14
bf8d3d6a 15DECLARE_MGROUP(BABELD);
ca10883e
DS
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
44static inline int
45seqno_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
53static inline short
54seqno_minus(unsigned short s1, unsigned short s2)
55{
56 return (short)((s1 - s2) & 0xFFFF);
57}
58
59static inline unsigned short
60seqno_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). */
67static inline unsigned int
68time_us(const struct timeval t)
69{
70 return (unsigned int) (t.tv_sec * 1000000 + t.tv_usec);
71}
72
73int roughly(int value);
74void timeval_minus(struct timeval *d,
75 const struct timeval *s1, const struct timeval *s2);
76unsigned timeval_minus_msec(const struct timeval *s1, const struct timeval *s2)
77 ATTRIBUTE ((pure));
78void timeval_add_msec(struct timeval *d, const struct timeval *s, int msecs);
79void set_timeout (struct timeval *timeout, int msecs);
80int timeval_compare(const struct timeval *s1, const struct timeval *s2)
81 ATTRIBUTE ((pure));
82void timeval_min(struct timeval *d, const struct timeval *s);
83void timeval_min_sec(struct timeval *d, time_t secs);
84int parse_nat(const char *string) ATTRIBUTE ((pure));
85int parse_msec(const char *string) ATTRIBUTE ((pure));
ca10883e
DS
86unsigned char *mask_prefix(unsigned char *restrict ret,
87 const unsigned char *restrict prefix,
88 unsigned char plen);
89const char *format_address(const unsigned char *address);
90const char *format_prefix(const unsigned char *address, unsigned char prefix);
91const char *format_eui64(const unsigned char *eui);
92const char *format_thousands(unsigned int value);
93int parse_address(const char *address, unsigned char *addr_r, int *af_r);
94int parse_eui64(const char *eui, unsigned char *eui_r);
95int wait_for_fd(int direction, int fd, int msecs);
96int martian_prefix(const unsigned char *prefix, int plen) ATTRIBUTE ((pure));
97int linklocal(const unsigned char *address) ATTRIBUTE ((pure));
98int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
99void v4tov6(unsigned char *dst, const unsigned char *src);
100void inaddr_to_uchar(unsigned char *dest, const struct in_addr *src);
101void uchar_to_inaddr(struct in_addr *dest, const unsigned char *src);
102void in6addr_to_uchar(unsigned char *dest, const struct in6_addr *src);
103void uchar_to_in6addr(struct in6_addr *dest, const unsigned char *src);
104int daemonise(void);
b0ba81f8 105extern const unsigned char v4prefix[16];
ca10883e 106
7d552fe6 107static inline bool
108is_default(const unsigned char *prefix, int plen)
109{
110 return plen == 0 || (plen == 96 && v4mapped(prefix));
111}
112
ca10883e
DS
113/* If debugging is disabled, we want to avoid calling format_address
114 for every omitted debugging message. So debug is a macro. But
115 vararg macros are not portable. */
116#if defined NO_DEBUG
117
ca10883e 118#define debugf(...) do {} while(0)
ca10883e
DS
119
120#else /* NO_DEBUG */
121
122/* some levels */
123#define BABEL_DEBUG_COMMON (1 << 0)
124#define BABEL_DEBUG_KERNEL (1 << 1)
125#define BABEL_DEBUG_FILTER (1 << 2)
126#define BABEL_DEBUG_TIMEOUT (1 << 3)
127#define BABEL_DEBUG_IF (1 << 4)
128#define BABEL_DEBUG_ROUTE (1 << 5)
129#define BABEL_DEBUG_ALL (0xFFFF)
130
6a72124d
DS
131#define debugf(level, ...) \
132 do { \
133 if (unlikely(debug & level)) \
134 zlog_debug(__VA_ARGS__); \
135 } while (0)
ca10883e
DS
136
137#endif /* NO_DEBUG */
138
139#endif /* BABEL_UTIL_H */