Statistics
| Branch: | Revision:

iof-bird-daemon / lib / birdlib.h @ 5e173e9f

History | View | Annotate | Download (3.75 KB)

1
/*
2
 *        BIRD Library
3
 *
4
 *        (c) 1998--2004 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#ifndef _BIRD_BIRDLIB_H_
10
#define _BIRD_BIRDLIB_H_
11

    
12
#include "timer.h"
13
#include "alloca.h"
14

    
15
/* Ugly structure offset handling macros */
16

    
17
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
18
#define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
19
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
20

    
21
/* Utility macros */
22

    
23
#define MIN_(a,b) (((a)<(b))?(a):(b))
24
#define MAX_(a,b) (((a)>(b))?(a):(b))
25

    
26
#ifndef PARSER
27
#undef MIN
28
#undef MAX
29
#define MIN(a,b) MIN_(a,b)
30
#define MAX(a,b) MAX_(a,b)
31
#endif
32

    
33
#define U64(c) UINT64_C(c)
34
#define ABS(a)   ((a)>=0 ? (a) : -(a))
35
#define DELTA(a,b) (((a)>=(b))?(a)-(b):(b)-(a))
36
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
37

    
38
static inline int uint_cmp(uint i1, uint i2)
39
{ return (int)(i1 > i2) - (int)(i1 < i2); }
40

    
41
static inline int u64_cmp(u64 i1, u64 i2)
42
{ return (int)(i1 > i2) - (int)(i1 < i2); }
43

    
44

    
45
/* Bitfield macros */
46

    
47
/* b is u32 array (or ptr), l is size of it in bits (multiple of 32), p is 0..(l-1) */
48
#define BIT32_VAL(p)                (((u32) 1) << ((p) % 32))
49
#define BIT32_TEST(b,p)                ((b)[(p)/32] & BIT32_VAL(p))
50
#define BIT32_SET(b,p)                ((b)[(p)/32] |= BIT32_VAL(p))
51
#define BIT32_CLR(b,p)                ((b)[(p)/32] &= ~BIT32_VAL(p))
52
#define BIT32_ZERO(b,l)                memset((b), 0, (l)/8)
53

    
54
#ifndef NULL
55
#define NULL ((void *) 0)
56
#endif
57

    
58
#ifndef IPV6
59
#define IP_VERSION 4
60
#else
61
#define IP_VERSION 6
62
#endif
63

    
64

    
65
/* Macros for gcc attributes */
66

    
67
#define NORET __attribute__((noreturn))
68
#define UNUSED __attribute__((unused))
69

    
70

    
71
/* Microsecond time */
72

    
73
typedef s64 btime;
74

    
75
#define S_        *1000000
76
#define MS_        *1000
77
#define US_        *1
78
#define TO_S        /1000000
79
#define TO_MS        /1000
80
#define TO_US        /1
81

    
82
#ifndef PARSER
83
#define S        S_
84
#define MS        MS_
85
#define US        US_
86
#endif
87

    
88

    
89
/* Rate limiting */
90

    
91
struct tbf {
92
  bird_clock_t timestamp;                /* Last update */
93
  u16 count;                                /* Available tokens */
94
  u16 burst;                                /* Max number of tokens */
95
  u16 rate;                                /* Rate of replenishment */
96
  u16 mark;                                /* Whether last op was limited */
97
};
98

    
99
/* Default TBF values for rate limiting log messages */
100
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
101

    
102
void tbf_update(struct tbf *f);
103

    
104
static inline int
105
tbf_limit(struct tbf *f)
106
{
107
  tbf_update(f);
108

    
109
  if (!f->count)
110
  {
111
    f->mark = 1;
112
    return 1;
113
  }
114

    
115
  f->count--;
116
  f->mark = 0;
117
  return 0;
118
}
119

    
120

    
121
/* Logging and dying */
122

    
123
typedef struct buffer {
124
  byte *start;
125
  byte *pos;
126
  byte *end;
127
} buffer;
128

    
129
#define STACK_BUFFER_INIT(buf,size)                \
130
  do {                                                \
131
    buf.start = alloca(size);                        \
132
    buf.pos = buf.start;                        \
133
    buf.end = buf.start + size;                        \
134
  } while(0)
135

    
136
#define LOG_BUFFER_INIT(buf)                        \
137
  STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
138

    
139
#define LOG_BUFFER_SIZE 1024
140

    
141
#define log log_msg
142
void log_commit(int class, buffer *buf);
143
void log_msg(const char *msg, ...);
144
void log_rl(struct tbf *rl, const char *msg, ...);
145
void die(const char *msg, ...) NORET;
146
void bug(const char *msg, ...) NORET;
147

    
148
#define L_DEBUG "\001"                        /* Debugging messages */
149
#define L_TRACE "\002"                        /* Protocol tracing */
150
#define L_INFO "\003"                        /* Informational messages */
151
#define L_REMOTE "\004"                        /* Remote protocol errors */
152
#define L_WARN "\005"                        /* Local warnings */
153
#define L_ERR "\006"                        /* Local errors */
154
#define L_AUTH "\007"                        /* Authorization failed etc. */
155
#define L_FATAL "\010"                        /* Fatal errors */
156
#define L_BUG "\011"                        /* BIRD bugs */
157

    
158
void debug(const char *msg, ...);                /* Printf to debug output */
159

    
160
/* Debugging */
161

    
162
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
163
#define DBG(x, y...) debug(x, ##y)
164
#else
165
#define DBG(x, y...) do { } while(0)
166
#endif
167

    
168
#ifdef DEBUGGING
169
#define ASSERT(x) do { if (!(x)) bug("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
170
#else
171
#define ASSERT(x) do { } while(0)
172
#endif
173

    
174
/* Pseudorandom numbers */
175

    
176
u32 random_u32(void);
177

    
178
#endif