]> jfr.im git - solanum.git/blob - librb/include/rb_lib.h
linebuf: increase buffer size to accommodate IRCv3 tags
[solanum.git] / librb / include / rb_lib.h
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 */
28 char *alloca();
29 # endif
30 # endif
31 # endif
32 # endif
33 #endif /* __GNUC__ */
34
35 #ifdef __GNUC__
36
37 #ifdef rb_likely
38 #undef rb_likely
39 #endif
40 #ifdef rb_unlikely
41 #undef rb_unlikely
42 #endif
43
44 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
45 # define __builtin_expect(x, expected_value) (x)
46 #endif
47
48 #define rb_likely(x) __builtin_expect(!!(x), 1)
49 #define rb_unlikely(x) __builtin_expect(!!(x), 0)
50
51 #else /* !__GNUC__ */
52
53 #define UNUSED(x) x
54
55 #ifdef rb_likely
56 #undef rb_likely
57 #endif
58 #ifdef rb_unlikely
59 #undef rb_unlikely
60 #endif
61 #define rb_likely(x) (x)
62 #define rb_unlikely(x) (x)
63 #endif
64
65 #ifdef _WIN32
66 #define rb_get_errno() do { errno = WSAGetLastError(); WSASetLastError(errno); } while(0)
67 typedef SOCKET rb_platform_fd_t;
68 #define RB_PATH_SEPARATOR '\\'
69 #else
70 #define rb_get_errno()
71 typedef int rb_platform_fd_t;
72 #define RB_PATH_SEPARATOR '/'
73 #endif
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 #undef ENOBUFS
90 #define ENOBUFS WSAENOBUFS
91
92 #undef EINPROGRESS
93 #define EINPROGRESS WSAEINPROGRESS
94
95 #undef EWOULDBLOCK
96 #define EWOULDBLOCK WSAEWOULDBLOCK
97
98 #undef EMSGSIZE
99 #define EMSGSIZE WSAEMSGSIZE
100
101 #undef EALREADY
102 #define EALREADY WSAEALREADY
103
104 #undef EISCONN
105 #define EISCONN WSAEISCONN
106
107 #undef EADDRINUSE
108 #define EADDRINUSE WSAEADDRINUSE
109
110 #undef EAFNOSUPPORT
111 #define EAFNOSUPPORT WSAEAFNOSUPPORT
112
113 #define pipe(x) _pipe(x, 1024, O_BINARY)
114 #define ioctl(x,y,z) ioctlsocket(x,y, (unsigned long *)z)
115
116 #define WNOHANG 1
117
118 #ifndef SIGKILL
119 #define SIGKILL SIGTERM
120 #endif
121
122 #endif /* _WIN32 */
123
124
125
126 #ifndef HOSTIPLEN
127 #define HOSTIPLEN 53
128 #endif
129
130 #ifdef __GNUC__
131 #define slrb_assert(expr) ( \
132 rb_likely((expr)) || ( \
133 rb_lib_log( \
134 "file: %s line: %d (%s): Assertion failed: (%s)", \
135 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
136 )
137 #else
138 #define slrb_assert(expr) ( \
139 rb_likely((expr)) || ( \
140 rb_lib_log( \
141 "file: %s line: %d: Assertion failed: (%s)", \
142 __FILE__, __LINE__, #expr), 0) \
143 )
144 #endif
145
146 /* evaluates to true if assertion fails */
147 #ifdef SOFT_ASSERT
148 #define lrb_assert(expr) (!slrb_assert(expr))
149 #else
150 #define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
151 #endif
152
153 #ifdef RB_SOCKADDR_HAS_SA_LEN
154 #define ss_len sa_len
155 #endif
156
157 #define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
158 #define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
159 #ifdef RB_SOCKADDR_HAS_SA_LEN
160 #define SET_SS_LEN(x, y) do { \
161 struct sockaddr *_storage; \
162 _storage = ((struct sockaddr *)(x));\
163 _storage->sa_len = (y); \
164 } while (0)
165 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
166 #else /* !RB_SOCKADDR_HAS_SA_LEN */
167 #define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
168 #ifdef RB_IPV6
169 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
170 #else
171 #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : 0)
172 #endif
173 #endif
174
175 #ifdef RB_IPV6
176 #define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
177 #define SET_SS_PORT(x, y) do { \
178 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
179 ((struct sockaddr_in *)(x))->sin_port = (y); \
180 } else { \
181 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
182 } \
183 } while (0)
184 #else
185 #define GET_SS_PORT(x) (((struct sockaddr_in *)(x))->sin_port)
186 #define SET_SS_PORT(x, y) (((struct sockaddr_in *)(x))->sin_port = y)
187 #endif
188
189 #ifndef INADDRSZ
190 #define INADDRSZ 4
191 #endif
192
193 #ifndef IN6ADDRSZ
194 #define IN6ADDRSZ 16
195 #endif
196
197 #ifndef INT16SZ
198 #define INT16SZ 2
199 #endif
200
201 #ifndef UINT16_MAX
202 #define UINT16_MAX (65535U)
203 #endif
204
205 #ifndef UINT32_MAX
206 #define UINT32_MAX (4294967295U)
207 #endif
208
209
210 typedef void log_cb(const char *buffer);
211 typedef void restart_cb(const char *buffer);
212 typedef void die_cb(const char *buffer);
213
214 char *rb_ctime(const time_t, char *, size_t);
215 char *rb_date(const time_t, char *, size_t);
216 void rb_lib_log(const char *, ...);
217 void rb_lib_restart(const char *, ...) __attribute__((noreturn));
218 void rb_lib_die(const char *, ...);
219 void rb_set_time(void);
220 const char *rb_lib_version(void);
221
222 void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
223 size_t dh_size, size_t fd_heap_size);
224 void rb_lib_loop(long delay) __attribute__((noreturn));
225
226 time_t rb_current_time(void);
227 const struct timeval *rb_current_time_tv(void);
228 pid_t rb_spawn_process(const char *, const char **);
229
230 char *rb_strtok_r(char *, const char *, char **);
231
232 int rb_gettimeofday(struct timeval *, void *);
233
234 void rb_sleep(unsigned int seconds, unsigned int useconds);
235 char *rb_crypt(const char *, const char *);
236
237 unsigned char *rb_base64_encode(const unsigned char *str, int length);
238 unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
239 int rb_kill(pid_t, int);
240 char *rb_strerror(int);
241
242 int rb_setenv(const char *, const char *, int);
243
244 pid_t rb_waitpid(pid_t pid, int *status, int options);
245 pid_t rb_getpid(void);
246 //unsigned int rb_geteuid(void);
247
248
249 #include <rb_tools.h>
250 #include <rb_memory.h>
251 #include <rb_commio.h>
252 #include <rb_balloc.h>
253 #include <rb_linebuf.h>
254 #include <rb_event.h>
255 #include <rb_helper.h>
256 #include <rb_rawbuf.h>
257 #include <rb_patricia.h>
258
259 #endif