]> jfr.im git - solanum.git/blame - librb/include/rb_lib.h
wsockd: avoid clang static analysis warning
[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
17e4e6af
AC
65#ifdef _WIN32
66#define rb_get_errno() do { errno = WSAGetLastError(); WSASetLastError(errno); } while(0)
67typedef SOCKET rb_platform_fd_t;
ac2f2189 68#define RB_PATH_SEPARATOR '\\'
17e4e6af
AC
69#else
70#define rb_get_errno()
71typedef int rb_platform_fd_t;
ac2f2189 72#define RB_PATH_SEPARATOR '/'
17e4e6af 73#endif
db137867 74
3202e249 75#ifdef _WIN32
db137867
AC
76#include <process.h>
77
d74fa5b5
JT
78#ifndef PATH_MAX
79#define PATH_MAX 128
db137867
AC
80#endif
81
db137867
AC
82#ifdef strerror
83#undef strerror
84#endif
85
3202e249
VY
86#define strerror(x) rb_strerror(x)
87char *rb_strerror(int error);
db137867 88
38b68802 89#undef ENOBUFS
db137867 90#define ENOBUFS WSAENOBUFS
b68dee9b 91
38b68802 92#undef EINPROGRESS
db137867 93#define EINPROGRESS WSAEINPROGRESS
b68dee9b 94
38b68802 95#undef EWOULDBLOCK
db137867 96#define EWOULDBLOCK WSAEWOULDBLOCK
b68dee9b 97
38b68802 98#undef EMSGSIZE
db137867 99#define EMSGSIZE WSAEMSGSIZE
b68dee9b 100
38b68802 101#undef EALREADY
db137867 102#define EALREADY WSAEALREADY
b68dee9b 103
38b68802 104#undef EISCONN
db137867 105#define EISCONN WSAEISCONN
b68dee9b 106
38b68802 107#undef EADDRINUSE
db137867 108#define EADDRINUSE WSAEADDRINUSE
b68dee9b 109
38b68802 110#undef EAFNOSUPPORT
db137867
AC
111#define EAFNOSUPPORT WSAEAFNOSUPPORT
112
113#define pipe(x) _pipe(x, 1024, O_BINARY)
491b3b39 114#define ioctl(x,y,z) ioctlsocket(x,y, (unsigned long *)z)
db137867 115
db137867 116#define WNOHANG 1
db137867
AC
117
118#ifndef SIGKILL
119#define SIGKILL SIGTERM
120#endif
121
3202e249 122#endif /* _WIN32 */
db137867
AC
123
124
125
126#ifndef HOSTIPLEN
127#define HOSTIPLEN 53
128#endif
129
db137867 130#ifdef __GNUC__
3202e249 131#define slrb_assert(expr) do \
c2ac22cc
VY
132 if(rb_unlikely(!(expr))) { \
133 rb_lib_log( \
db137867
AC
134 "file: %s line: %d (%s): Assertion failed: (%s)", \
135 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
db137867
AC
136 } \
137 while(0)
138#else
3202e249 139#define slrb_assert(expr) do \
c2ac22cc 140 if(rb_unlikely(!(expr))) { \
29c92cf9 141 rb_lib_log( \
db137867
AC
142 "file: %s line: %d: Assertion failed: (%s)", \
143 __FILE__, __LINE__, #expr); \
db137867
AC
144 } \
145 while(0)
146#endif
3202e249
VY
147
148#ifdef SOFT_ASSERT
149#define lrb_assert(expr) slrb_assert(expr)
db137867 150#else
3202e249 151#define lrb_assert(expr) do { slrb_assert(expr); assert(expr); } while(0)
db137867
AC
152#endif
153
9d9a4f60 154#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867
AC
155#define ss_len sa_len
156#endif
157
3202e249
VY
158#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
159#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
9d9a4f60 160#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867 161#define SET_SS_LEN(x, y) do { \
d86692fa
EM
162 struct sockaddr *_storage; \
163 _storage = ((struct sockaddr *)(x));\
164 _storage->sa_len = (y); \
db137867
AC
165 } while (0)
166#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
9d9a4f60 167#else /* !RB_SOCKADDR_HAS_SA_LEN */
db137867
AC
168#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
169#ifdef RB_IPV6
170#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
171#else
172#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : 0)
173#endif
174#endif
175
d86692fa
EM
176#ifdef RB_IPV6
177#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
178#define SET_SS_PORT(x, y) do { \
179 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
180 ((struct sockaddr_in *)(x))->sin_port = (y); \
181 } else { \
182 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
183 } \
184 } while (0)
185#else
186#define GET_SS_PORT(x) (((struct sockaddr_in *)(x))->sin_port)
187#define SET_SS_PORT(x, y) (((struct sockaddr_in *)(x))->sin_port = y)
188#endif
189
db137867
AC
190#ifndef INADDRSZ
191#define INADDRSZ 4
192#endif
193
194#ifndef IN6ADDRSZ
195#define IN6ADDRSZ 16
196#endif
197
198#ifndef INT16SZ
199#define INT16SZ 2
200#endif
201
41aed6bb
SA
202#ifndef UINT16_MAX
203#define UINT16_MAX (65535U)
204#endif
205
7c7cf006
SA
206#ifndef UINT32_MAX
207#define UINT32_MAX (4294967295U)
208#endif
209
db137867
AC
210
211typedef void log_cb(const char *buffer);
212typedef void restart_cb(const char *buffer);
213typedef void die_cb(const char *buffer);
214
215char *rb_ctime(const time_t, char *, size_t);
216char *rb_date(const time_t, char *, size_t);
217void rb_lib_log(const char *, ...);
92706fd5 218void rb_lib_restart(const char *, ...) __attribute__((noreturn));
db137867
AC
219void rb_lib_die(const char *, ...);
220void rb_set_time(void);
221const char *rb_lib_version(void);
222
223void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
224 size_t dh_size, size_t fd_heap_size);
92706fd5 225void rb_lib_loop(long delay) __attribute__((noreturn));
db137867
AC
226
227time_t rb_current_time(void);
228const struct timeval *rb_current_time_tv(void);
229pid_t rb_spawn_process(const char *, const char **);
230
231char *rb_strtok_r(char *, const char *, char **);
232
233int rb_gettimeofday(struct timeval *, void *);
234
235void rb_sleep(unsigned int seconds, unsigned int useconds);
236char *rb_crypt(const char *, const char *);
237
238unsigned char *rb_base64_encode(const unsigned char *str, int length);
239unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
3202e249
VY
240int rb_kill(pid_t, int);
241char *rb_strerror(int);
db137867 242
3202e249 243int rb_setenv(const char *, const char *, int);
db137867 244
3202e249
VY
245pid_t rb_waitpid(pid_t pid, int *status, int options);
246pid_t rb_getpid(void);
247//unsigned int rb_geteuid(void);
db137867 248
030272f3 249
db137867
AC
250#include <rb_tools.h>
251#include <rb_memory.h>
252#include <rb_commio.h>
253#include <rb_balloc.h>
254#include <rb_linebuf.h>
db137867
AC
255#include <rb_event.h>
256#include <rb_helper.h>
257#include <rb_rawbuf.h>
258#include <rb_patricia.h>
259
260#endif