]> jfr.im git - solanum.git/blob - libratbox/include/ratbox_lib.h
d99a229c83151253fd8ae02affc8711f96537346
[solanum.git] / libratbox / include / ratbox_lib.h
1 /*
2 * $Id: ratbox_lib.h 26256 2008-12-10 04:05:26Z androsyn $
3 */
4
5 #ifndef RB_LIB_H
6 #define RB_LIB_H 1
7
8 #include <librb-config.h>
9 #include <stdio.h>
10 #include <limits.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <ctype.h>
16
17 #ifdef HAVE_LINUX_TCP_H
18 # include <linux/tcp.h>
19 #endif
20
21 #ifdef __GNUC__
22 #undef alloca
23 #define alloca __builtin_alloca
24 #else
25 # ifdef _MSC_VER
26 # include <malloc.h>
27 # define alloca _alloca
28 # else
29 # if RB_HAVE_ALLOCA_H
30 # include <alloca.h>
31 # else
32 # ifdef _AIX
33 #pragma alloca
34 # else
35 # ifndef alloca /* predefined by HP cc +Olibcalls */
36 char *alloca();
37 # endif
38 # endif
39 # endif
40 # endif
41 #endif /* __GNUC__ */
42
43 #ifdef __GNUC__
44
45 #ifdef rb_likely
46 #undef rb_likely
47 #endif
48 #ifdef rb_unlikely
49 #undef rb_unlikely
50 #endif
51
52 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
53 # define __builtin_expect(x, expected_value) (x)
54 #endif
55
56 #define rb_likely(x) __builtin_expect(!!(x), 1)
57 #define rb_unlikely(x) __builtin_expect(!!(x), 0)
58
59 #else /* !__GNUC__ */
60
61 #define UNUSED(x) x
62
63 #ifdef rb_likely
64 #undef rb_likely
65 #endif
66 #ifdef rb_unlikely
67 #undef rb_unlikely
68 #endif
69 #define rb_likely(x) (x)
70 #define rb_unlikely(x) (x)
71 #endif
72
73
74
75 #ifdef _WIN32
76 #include <process.h>
77
78 #ifndef PATH_MAX
79 #define PATH_MAX 128
80 #endif
81
82 #ifdef strerror
83 #undef strerror
84 #endif
85
86 #define strerror(x) rb_strerror(x)
87 char *rb_strerror(int error);
88
89
90 #define ENOBUFS WSAENOBUFS
91 #define EINPROGRESS WSAEINPROGRESS
92 #define EWOULDBLOCK WSAEWOULDBLOCK
93 #define EMSGSIZE WSAEMSGSIZE
94 #define EALREADY WSAEALREADY
95 #define EISCONN WSAEISCONN
96 #define EADDRINUSE WSAEADDRINUSE
97 #define EAFNOSUPPORT WSAEAFNOSUPPORT
98
99 #define pipe(x) _pipe(x, 1024, O_BINARY)
100 #define ioctl(x,y,z) ioctlsocket(x,y, (u_long *)z)
101
102 #define WNOHANG 1
103
104 #ifndef SIGKILL
105 #define SIGKILL SIGTERM
106 #endif
107
108 #endif /* _WIN32 */
109
110
111
112 #ifndef HOSTIPLEN
113 #define HOSTIPLEN 53
114 #endif
115
116 #ifdef __GNUC__
117 #define slrb_assert(expr) do \
118 if(rb_unlikely(!(expr))) { \
119 rb_lib_log( \
120 "file: %s line: %d (%s): Assertion failed: (%s)", \
121 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
122 } \
123 while(0)
124 #else
125 #define slrb_assert(expr) do \
126 if(rb_unlikely(!(expr))) { \
127 rb_lib_log( \
128 "file: %s line: %d: Assertion failed: (%s)", \
129 __FILE__, __LINE__, #expr); \
130 } \
131 while(0)
132 #endif
133
134 #ifdef SOFT_ASSERT
135 #define lrb_assert(expr) slrb_assert(expr)
136 #else
137 #define lrb_assert(expr) do { slrb_assert(expr); assert(expr); } while(0)
138 #endif
139
140 #ifdef RB_SOCKADDR_HAS_SA_LEN
141 #define ss_len sa_len
142 #endif
143
144 #define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
145 #define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
146 #ifdef RB_SOCKADDR_HAS_SA_LEN
147 #define SET_SS_LEN(x, y) do { \
148 struct sockaddr *storage; \
149 storage = ((struct sockaddr *)(x));\
150 storage->sa_len = (y); \
151 } while (0)
152 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
153 #else /* !RB_SOCKADDR_HAS_SA_LEN */
154 #define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
155 #ifdef RB_IPV6
156 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
157 #else
158 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : 0)
159 #endif
160 #endif
161
162 #ifndef INADDRSZ
163 #define INADDRSZ 4
164 #endif
165
166 #ifndef IN6ADDRSZ
167 #define IN6ADDRSZ 16
168 #endif
169
170 #ifndef INT16SZ
171 #define INT16SZ 2
172 #endif
173
174 #ifndef UINT16_MAX
175 #define UINT16_MAX (65535U)
176 #endif
177
178
179 typedef void log_cb(const char *buffer);
180 typedef void restart_cb(const char *buffer);
181 typedef void die_cb(const char *buffer);
182
183 char *rb_ctime(const time_t, char *, size_t);
184 char *rb_date(const time_t, char *, size_t);
185 void rb_lib_log(const char *, ...);
186 void rb_lib_restart(const char *, ...);
187 void rb_lib_die(const char *, ...);
188 void rb_set_time(void);
189 const char *rb_lib_version(void);
190
191 void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
192 size_t dh_size, size_t fd_heap_size);
193 void rb_lib_loop(long delay);
194
195 time_t rb_current_time(void);
196 const struct timeval *rb_current_time_tv(void);
197 pid_t rb_spawn_process(const char *, const char **);
198
199 char *rb_strtok_r(char *, const char *, char **);
200
201 int rb_gettimeofday(struct timeval *, void *);
202
203 void rb_sleep(unsigned int seconds, unsigned int useconds);
204 char *rb_crypt(const char *, const char *);
205
206 unsigned char *rb_base64_encode(const unsigned char *str, int length);
207 unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
208 int rb_kill(pid_t, int);
209 char *rb_strerror(int);
210
211 int rb_setenv(const char *, const char *, int);
212
213 pid_t rb_waitpid(pid_t pid, int *status, int options);
214 pid_t rb_getpid(void);
215 //unsigned int rb_geteuid(void);
216
217
218 #include <rb_tools.h>
219 #include <rb_memory.h>
220 #include <rb_commio.h>
221 #include <rb_balloc.h>
222 #include <rb_linebuf.h>
223 #include <rb_event.h>
224 #include <rb_helper.h>
225 #include <rb_rawbuf.h>
226 #include <rb_patricia.h>
227
228 #endif