1998-04-22 20:58:34 +08:00
|
|
|
/*
|
|
|
|
* BIRD Library
|
|
|
|
*
|
2004-06-01 05:02:09 +08:00
|
|
|
* (c) 1998--2004 Martin Mares <mj@ucw.cz>
|
1998-04-22 20:58:34 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_BIRDLIB_H_
|
|
|
|
#define _BIRD_BIRDLIB_H_
|
|
|
|
|
2009-02-26 21:23:54 +08:00
|
|
|
#include "timer.h"
|
2013-10-06 02:12:28 +08:00
|
|
|
#include "alloca.h"
|
2009-02-26 21:23:54 +08:00
|
|
|
|
1998-04-22 20:58:34 +08:00
|
|
|
/* Ugly structure offset handling macros */
|
|
|
|
|
2009-07-24 04:21:17 +08:00
|
|
|
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
|
1998-04-22 20:58:34 +08:00
|
|
|
#define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
|
2004-06-01 18:28:25 +08:00
|
|
|
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
|
1998-05-04 00:43:39 +08:00
|
|
|
|
1999-02-06 05:29:19 +08:00
|
|
|
/* Utility macros */
|
|
|
|
|
2013-11-23 04:59:43 +08:00
|
|
|
#define MIN_(a,b) (((a)<(b))?(a):(b))
|
|
|
|
#define MAX_(a,b) (((a)>(b))?(a):(b))
|
2013-10-06 02:12:28 +08:00
|
|
|
|
|
|
|
#ifndef PARSER
|
2013-11-20 05:33:48 +08:00
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
2013-11-23 04:59:43 +08:00
|
|
|
#define MIN(a,b) MIN_(a,b)
|
|
|
|
#define MAX(a,b) MAX_(a,b)
|
2011-03-13 18:33:50 +08:00
|
|
|
#endif
|
|
|
|
|
2002-11-13 16:47:19 +08:00
|
|
|
#define ABS(a) ((a)>=0 ? (a) : -(a))
|
2000-04-12 21:21:23 +08:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
|
1998-07-10 03:36:05 +08:00
|
|
|
|
1999-03-30 03:13:36 +08:00
|
|
|
#ifndef NULL
|
|
|
|
#define NULL ((void *) 0)
|
|
|
|
#endif
|
|
|
|
|
2013-10-02 17:42:46 +08:00
|
|
|
#ifndef IPV6
|
|
|
|
#define IP_VERSION 4
|
|
|
|
#else
|
|
|
|
#define IP_VERSION 6
|
|
|
|
#endif
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
|
2004-06-05 17:01:12 +08:00
|
|
|
/* Macros for gcc attributes */
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
#define NORET __attribute__((noreturn))
|
2004-06-01 05:02:09 +08:00
|
|
|
#define UNUSED __attribute__((unused))
|
1998-04-22 20:58:34 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
|
|
|
|
/* Microsecond time */
|
|
|
|
|
|
|
|
typedef s64 btime;
|
|
|
|
|
2013-11-23 04:59:43 +08:00
|
|
|
#define S_ *1000000
|
|
|
|
#define MS_ *1000
|
|
|
|
#define US_ *1
|
2013-10-06 02:12:28 +08:00
|
|
|
#define TO_S /1000000
|
|
|
|
#define TO_MS /1000
|
|
|
|
#define TO_US /1
|
|
|
|
|
|
|
|
#ifndef PARSER
|
2013-11-23 04:59:43 +08:00
|
|
|
#define S S_
|
|
|
|
#define MS MS_
|
|
|
|
#define US US_
|
2013-10-06 02:12:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-10-02 17:41:34 +08:00
|
|
|
/* Rate limiting */
|
|
|
|
|
|
|
|
struct tbf {
|
|
|
|
bird_clock_t timestamp; /* Last update */
|
|
|
|
u16 count; /* Available tokens */
|
|
|
|
u16 burst; /* Max number of tokens */
|
|
|
|
u16 rate; /* Rate of replenishment */
|
|
|
|
u16 mark; /* Whether last op was limited */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Default TBF values for rate limiting log messages */
|
|
|
|
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
|
|
|
|
|
|
|
|
void tbf_update(struct tbf *f);
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
tbf_limit(struct tbf *f)
|
|
|
|
{
|
|
|
|
tbf_update(f);
|
|
|
|
|
|
|
|
if (!f->count)
|
|
|
|
{
|
|
|
|
f->mark = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->count--;
|
|
|
|
f->mark = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-04-23 22:01:15 +08:00
|
|
|
/* Logging and dying */
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
typedef struct buffer {
|
|
|
|
byte *start;
|
|
|
|
byte *pos;
|
|
|
|
byte *end;
|
|
|
|
} buffer;
|
|
|
|
|
|
|
|
#define STACK_BUFFER_INIT(buf,size) \
|
|
|
|
do { \
|
|
|
|
buf.start = alloca(size); \
|
|
|
|
buf.pos = buf.start; \
|
|
|
|
buf.end = buf.start + size; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define LOG_BUFFER_INIT(buf) \
|
|
|
|
STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
|
|
|
|
|
|
|
|
#define LOG_BUFFER_SIZE 1024
|
|
|
|
|
2004-06-05 17:05:12 +08:00
|
|
|
#define log log_msg
|
2013-10-06 02:12:28 +08:00
|
|
|
void log_commit(int class, buffer *buf);
|
2004-06-05 17:05:12 +08:00
|
|
|
void log_msg(char *msg, ...);
|
2014-10-02 17:41:34 +08:00
|
|
|
void log_rl(struct tbf *rl, char *msg, ...);
|
1998-07-10 16:39:34 +08:00
|
|
|
void die(char *msg, ...) NORET;
|
1998-12-20 22:24:35 +08:00
|
|
|
void bug(char *msg, ...) NORET;
|
1998-04-23 22:01:15 +08:00
|
|
|
|
|
|
|
#define L_DEBUG "\001" /* Debugging messages */
|
1998-12-20 22:24:35 +08:00
|
|
|
#define L_TRACE "\002" /* Protocol tracing */
|
|
|
|
#define L_INFO "\003" /* Informational messages */
|
|
|
|
#define L_REMOTE "\004" /* Remote protocol errors */
|
2001-02-20 17:49:19 +08:00
|
|
|
#define L_WARN "\005" /* Local warnings */
|
|
|
|
#define L_ERR "\006" /* Local errors */
|
|
|
|
#define L_AUTH "\007" /* Authorization failed etc. */
|
|
|
|
#define L_FATAL "\010" /* Fatal errors */
|
|
|
|
#define L_BUG "\011" /* BIRD bugs */
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
void debug(char *msg, ...); /* Printf to debug output */
|
1998-04-23 22:01:15 +08:00
|
|
|
|
|
|
|
/* Debugging */
|
|
|
|
|
2000-03-08 05:04:14 +08:00
|
|
|
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
|
1998-05-27 05:36:17 +08:00
|
|
|
#define DBG(x, y...) debug(x, ##y)
|
1998-04-23 22:01:15 +08:00
|
|
|
#else
|
2000-03-13 05:47:25 +08:00
|
|
|
#define DBG(x, y...) do { } while(0)
|
1998-04-23 22:01:15 +08:00
|
|
|
#endif
|
|
|
|
|
1998-12-20 22:24:35 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
#define ASSERT(x) do { if (!(x)) bug("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
|
|
|
#else
|
|
|
|
#define ASSERT(x) do { } while(0)
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 04:47:40 +08:00
|
|
|
/* Pseudorandom numbers */
|
|
|
|
|
|
|
|
u32 random_u32(void);
|
|
|
|
|
1998-04-22 20:58:34 +08:00
|
|
|
#endif
|