2008-04-01 16:52:26 +00:00
|
|
|
#ifndef RB_LIB_H
|
|
|
|
#define RB_LIB_H 1
|
|
|
|
|
|
|
|
#include <librb-config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#undef alloca
|
|
|
|
#define alloca __builtin_alloca
|
|
|
|
#else
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# include <malloc.h>
|
|
|
|
# define alloca _alloca
|
|
|
|
# else
|
|
|
|
# if RB_HAVE_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
# else
|
|
|
|
# ifdef _AIX
|
|
|
|
#pragma alloca
|
|
|
|
# else
|
|
|
|
# ifndef alloca /* predefined by HP cc +Olibcalls */
|
|
|
|
char *alloca();
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
2008-06-25 05:28:30 +00:00
|
|
|
#ifdef rb_likely
|
|
|
|
#undef rb_likely
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
2008-06-25 05:28:30 +00:00
|
|
|
#ifdef rb_unlikely
|
|
|
|
#undef rb_unlikely
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
|
|
|
|
# define __builtin_expect(x, expected_value) (x)
|
|
|
|
#endif
|
|
|
|
|
2008-06-25 05:28:30 +00:00
|
|
|
#define rb_likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define rb_unlikely(x) __builtin_expect(!!(x), 0)
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
#else /* !__GNUC__ */
|
|
|
|
|
|
|
|
#define UNUSED(x) x
|
|
|
|
|
2008-06-25 05:28:30 +00:00
|
|
|
#ifdef rb_likely
|
|
|
|
#undef rb_likely
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
2008-06-25 05:28:30 +00:00
|
|
|
#ifdef rb_unlikely
|
|
|
|
#undef rb_unlikely
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
2008-06-25 05:28:30 +00:00
|
|
|
#define rb_likely(x) (x)
|
|
|
|
#define rb_unlikely(x) (x)
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HOSTIPLEN
|
|
|
|
#define HOSTIPLEN 53
|
|
|
|
#endif
|
|
|
|
|
2017-07-30 12:47:27 +00:00
|
|
|
|
|
|
|
/* For those unfamiliar with GNU format attributes, a is the 1 based
|
|
|
|
* argument number of the format string, and b is the 1 based argument
|
|
|
|
* number of the variadic ... */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define AFP(a,b) __attribute__((format (printf, a, b)))
|
|
|
|
#else
|
|
|
|
#define AFP(a,b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
#ifdef __GNUC__
|
2017-07-29 16:37:53 +00:00
|
|
|
#define slrb_assert(expr) ( \
|
|
|
|
rb_likely((expr)) || ( \
|
2008-06-25 05:28:30 +00:00
|
|
|
rb_lib_log( \
|
2008-04-01 16:52:26 +00:00
|
|
|
"file: %s line: %d (%s): Assertion failed: (%s)", \
|
2017-07-29 16:37:53 +00:00
|
|
|
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
|
|
|
|
)
|
2008-04-01 16:52:26 +00:00
|
|
|
#else
|
2017-07-29 16:37:53 +00:00
|
|
|
#define slrb_assert(expr) ( \
|
|
|
|
rb_likely((expr)) || ( \
|
2015-04-20 05:55:20 +00:00
|
|
|
rb_lib_log( \
|
2008-04-01 16:52:26 +00:00
|
|
|
"file: %s line: %d: Assertion failed: (%s)", \
|
2017-07-29 16:37:53 +00:00
|
|
|
__FILE__, __LINE__, #expr), 0) \
|
|
|
|
)
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
2008-12-02 23:49:39 +00:00
|
|
|
|
2017-07-29 16:37:53 +00:00
|
|
|
/* evaluates to true if assertion fails */
|
2008-12-02 23:49:39 +00:00
|
|
|
#ifdef SOFT_ASSERT
|
2017-07-29 16:37:53 +00:00
|
|
|
#define lrb_assert(expr) (!slrb_assert(expr))
|
2008-04-01 16:52:26 +00:00
|
|
|
#else
|
2017-07-29 16:37:53 +00:00
|
|
|
#define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
|
2008-04-01 16:52:26 +00:00
|
|
|
#endif
|
|
|
|
|
2008-04-05 19:08:48 +00:00
|
|
|
#ifdef RB_SOCKADDR_HAS_SA_LEN
|
2008-04-01 16:52:26 +00:00
|
|
|
#define ss_len sa_len
|
|
|
|
#endif
|
|
|
|
|
2008-12-02 23:49:39 +00:00
|
|
|
#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
|
|
|
|
#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
|
2008-04-05 19:08:48 +00:00
|
|
|
#ifdef RB_SOCKADDR_HAS_SA_LEN
|
2008-04-01 16:52:26 +00:00
|
|
|
#define SET_SS_LEN(x, y) do { \
|
2016-03-31 08:00:29 +00:00
|
|
|
struct sockaddr *_storage; \
|
|
|
|
_storage = ((struct sockaddr *)(x));\
|
|
|
|
_storage->sa_len = (y); \
|
2008-04-01 16:52:26 +00:00
|
|
|
} while (0)
|
|
|
|
#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
|
2008-04-05 19:08:48 +00:00
|
|
|
#else /* !RB_SOCKADDR_HAS_SA_LEN */
|
2008-04-01 16:52:26 +00:00
|
|
|
#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
|
|
|
|
#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
|
|
|
|
#endif
|
|
|
|
|
2016-03-31 08:00:29 +00:00
|
|
|
#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
|
|
|
|
#define SET_SS_PORT(x, y) do { \
|
|
|
|
if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
|
|
|
|
((struct sockaddr_in *)(x))->sin_port = (y); \
|
|
|
|
} else { \
|
|
|
|
((struct sockaddr_in6 *)(x))->sin6_port = (y); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
#ifndef INADDRSZ
|
|
|
|
#define INADDRSZ 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IN6ADDRSZ
|
|
|
|
#define IN6ADDRSZ 16
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INT16SZ
|
|
|
|
#define INT16SZ 2
|
|
|
|
#endif
|
|
|
|
|
2016-02-06 23:51:07 +00:00
|
|
|
#ifndef UINT16_MAX
|
|
|
|
#define UINT16_MAX (65535U)
|
|
|
|
#endif
|
|
|
|
|
2016-05-12 11:35:06 +00:00
|
|
|
#ifndef UINT32_MAX
|
|
|
|
#define UINT32_MAX (4294967295U)
|
|
|
|
#endif
|
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
typedef void log_cb(const char *buffer);
|
|
|
|
typedef void restart_cb(const char *buffer);
|
|
|
|
typedef void die_cb(const char *buffer);
|
|
|
|
|
|
|
|
char *rb_ctime(const time_t, char *, size_t);
|
|
|
|
char *rb_date(const time_t, char *, size_t);
|
|
|
|
void rb_lib_log(const char *, ...);
|
2016-06-01 19:50:09 +00:00
|
|
|
void rb_lib_restart(const char *, ...) __attribute__((noreturn));
|
2008-04-01 16:52:26 +00:00
|
|
|
void rb_lib_die(const char *, ...);
|
|
|
|
void rb_set_time(void);
|
|
|
|
const char *rb_lib_version(void);
|
|
|
|
|
|
|
|
void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
|
|
|
|
size_t dh_size, size_t fd_heap_size);
|
2016-06-01 19:50:09 +00:00
|
|
|
void rb_lib_loop(long delay) __attribute__((noreturn));
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
time_t rb_current_time(void);
|
|
|
|
const struct timeval *rb_current_time_tv(void);
|
|
|
|
pid_t rb_spawn_process(const char *, const char **);
|
|
|
|
|
|
|
|
char *rb_strtok_r(char *, const char *, char **);
|
|
|
|
|
|
|
|
int rb_gettimeofday(struct timeval *, void *);
|
|
|
|
|
|
|
|
void rb_sleep(unsigned int seconds, unsigned int useconds);
|
|
|
|
char *rb_crypt(const char *, const char *);
|
|
|
|
|
|
|
|
unsigned char *rb_base64_encode(const unsigned char *str, int length);
|
|
|
|
unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
|
2008-12-02 23:49:39 +00:00
|
|
|
int rb_kill(pid_t, int);
|
|
|
|
char *rb_strerror(int);
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2008-12-02 23:49:39 +00:00
|
|
|
int rb_setenv(const char *, const char *, int);
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2008-12-02 23:49:39 +00:00
|
|
|
pid_t rb_waitpid(pid_t pid, int *status, int options);
|
|
|
|
pid_t rb_getpid(void);
|
|
|
|
//unsigned int rb_geteuid(void);
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2008-12-22 09:49:01 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
#include <rb_tools.h>
|
|
|
|
#include <rb_memory.h>
|
|
|
|
#include <rb_commio.h>
|
|
|
|
#include <rb_balloc.h>
|
|
|
|
#include <rb_linebuf.h>
|
|
|
|
#include <rb_event.h>
|
|
|
|
#include <rb_helper.h>
|
|
|
|
#include <rb_rawbuf.h>
|
|
|
|
#include <rb_patricia.h>
|
|
|
|
|
|
|
|
#endif
|