]> jfr.im git - solanum.git/blame - librb/include/rb_lib.h
Remove ancient portability code (#361)
[solanum.git] / librb / include / rb_lib.h
CommitLineData
db137867
AC
1#ifndef RB_LIB_H
2#define RB_LIB_H 1
3
4#include <librb-config.h>
5#include <stdio.h>
6#include <limits.h>
7#include <stdarg.h>
8#include <assert.h>
9#include <fcntl.h>
10#include <signal.h>
11#include <ctype.h>
12
13#ifdef __GNUC__
14#undef alloca
15#define alloca __builtin_alloca
16#else
17# ifdef _MSC_VER
18# include <malloc.h>
19# define alloca _alloca
20# else
21# if RB_HAVE_ALLOCA_H
22# include <alloca.h>
23# else
24# ifdef _AIX
25#pragma alloca
26# else
27# ifndef alloca /* predefined by HP cc +Olibcalls */
28char *alloca();
29# endif
30# endif
31# endif
32# endif
33#endif /* __GNUC__ */
34
35#ifdef __GNUC__
36
c2ac22cc
VY
37#ifdef rb_likely
38#undef rb_likely
db137867 39#endif
c2ac22cc
VY
40#ifdef rb_unlikely
41#undef rb_unlikely
db137867
AC
42#endif
43
44#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
45# define __builtin_expect(x, expected_value) (x)
46#endif
47
c2ac22cc
VY
48#define rb_likely(x) __builtin_expect(!!(x), 1)
49#define rb_unlikely(x) __builtin_expect(!!(x), 0)
db137867
AC
50
51#else /* !__GNUC__ */
52
53#define UNUSED(x) x
54
c2ac22cc
VY
55#ifdef rb_likely
56#undef rb_likely
db137867 57#endif
c2ac22cc
VY
58#ifdef rb_unlikely
59#undef rb_unlikely
db137867 60#endif
c2ac22cc
VY
61#define rb_likely(x) (x)
62#define rb_unlikely(x) (x)
db137867
AC
63#endif
64
db137867
AC
65#ifndef HOSTIPLEN
66#define HOSTIPLEN 53
67#endif
68
7a06833f
SA
69
70/* For those unfamiliar with GNU format attributes, a is the 1 based
71 * argument number of the format string, and b is the 1 based argument
72 * number of the variadic ... */
73#ifdef __GNUC__
74#define AFP(a,b) __attribute__((format (printf, a, b)))
75#else
76#define AFP(a,b)
77#endif
78
79
db137867 80#ifdef __GNUC__
b5ad4cdf
SA
81#define slrb_assert(expr) ( \
82 rb_likely((expr)) || ( \
c2ac22cc 83 rb_lib_log( \
db137867 84 "file: %s line: %d (%s): Assertion failed: (%s)", \
b5ad4cdf
SA
85 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
86 )
db137867 87#else
b5ad4cdf
SA
88#define slrb_assert(expr) ( \
89 rb_likely((expr)) || ( \
29c92cf9 90 rb_lib_log( \
db137867 91 "file: %s line: %d: Assertion failed: (%s)", \
b5ad4cdf
SA
92 __FILE__, __LINE__, #expr), 0) \
93 )
db137867 94#endif
3202e249 95
b5ad4cdf 96/* evaluates to true if assertion fails */
3202e249 97#ifdef SOFT_ASSERT
b5ad4cdf 98#define lrb_assert(expr) (!slrb_assert(expr))
db137867 99#else
b5ad4cdf 100#define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
db137867
AC
101#endif
102
9d9a4f60 103#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867
AC
104#define ss_len sa_len
105#endif
106
3202e249
VY
107#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
108#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
9d9a4f60 109#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867 110#define SET_SS_LEN(x, y) do { \
d86692fa
EM
111 struct sockaddr *_storage; \
112 _storage = ((struct sockaddr *)(x));\
113 _storage->sa_len = (y); \
db137867
AC
114 } while (0)
115#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
9d9a4f60 116#else /* !RB_SOCKADDR_HAS_SA_LEN */
db137867 117#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
db137867 118#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
db137867
AC
119#endif
120
d86692fa
EM
121#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
122#define SET_SS_PORT(x, y) do { \
123 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
124 ((struct sockaddr_in *)(x))->sin_port = (y); \
125 } else { \
126 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
127 } \
128 } while (0)
d86692fa 129
db137867
AC
130#ifndef INADDRSZ
131#define INADDRSZ 4
132#endif
133
134#ifndef IN6ADDRSZ
135#define IN6ADDRSZ 16
136#endif
137
138#ifndef INT16SZ
139#define INT16SZ 2
140#endif
141
41aed6bb
SA
142#ifndef UINT16_MAX
143#define UINT16_MAX (65535U)
144#endif
145
7c7cf006
SA
146#ifndef UINT32_MAX
147#define UINT32_MAX (4294967295U)
148#endif
149
db137867
AC
150
151typedef void log_cb(const char *buffer);
152typedef void restart_cb(const char *buffer);
153typedef void die_cb(const char *buffer);
154
155char *rb_ctime(const time_t, char *, size_t);
156char *rb_date(const time_t, char *, size_t);
157void rb_lib_log(const char *, ...);
92706fd5 158void rb_lib_restart(const char *, ...) __attribute__((noreturn));
db137867
AC
159void rb_lib_die(const char *, ...);
160void rb_set_time(void);
161const char *rb_lib_version(void);
162
163void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
164 size_t dh_size, size_t fd_heap_size);
92706fd5 165void rb_lib_loop(long delay) __attribute__((noreturn));
db137867
AC
166
167time_t rb_current_time(void);
168const struct timeval *rb_current_time_tv(void);
169pid_t rb_spawn_process(const char *, const char **);
170
171char *rb_strtok_r(char *, const char *, char **);
172
173int rb_gettimeofday(struct timeval *, void *);
174
175void rb_sleep(unsigned int seconds, unsigned int useconds);
176char *rb_crypt(const char *, const char *);
177
178unsigned char *rb_base64_encode(const unsigned char *str, int length);
179unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
3202e249
VY
180int rb_kill(pid_t, int);
181char *rb_strerror(int);
db137867 182
3202e249 183int rb_setenv(const char *, const char *, int);
db137867 184
3202e249
VY
185pid_t rb_waitpid(pid_t pid, int *status, int options);
186pid_t rb_getpid(void);
187//unsigned int rb_geteuid(void);
db137867 188
030272f3 189
db137867
AC
190#include <rb_tools.h>
191#include <rb_memory.h>
192#include <rb_commio.h>
193#include <rb_balloc.h>
194#include <rb_linebuf.h>
db137867
AC
195#include <rb_event.h>
196#include <rb_helper.h>
197#include <rb_rawbuf.h>
198#include <rb_patricia.h>
199
200#endif