]>
Commit | Line | Data |
---|---|---|
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) do \ | |
132 | if(rb_unlikely(!(expr))) { \ | |
133 | rb_lib_log( \ | |
134 | "file: %s line: %d (%s): Assertion failed: (%s)", \ | |
135 | __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \ | |
136 | } \ | |
137 | while(0) | |
138 | #else | |
139 | #define slrb_assert(expr) do \ | |
140 | if(rb_unlikely(!(expr))) { \ | |
141 | rb_lib_log( \ | |
142 | "file: %s line: %d: Assertion failed: (%s)", \ | |
143 | __FILE__, __LINE__, #expr); \ | |
144 | } \ | |
145 | while(0) | |
146 | #endif | |
147 | ||
148 | #ifdef SOFT_ASSERT | |
149 | #define lrb_assert(expr) slrb_assert(expr) | |
150 | #else | |
151 | #define lrb_assert(expr) do { slrb_assert(expr); assert(expr); } while(0) | |
152 | #endif | |
153 | ||
154 | #ifdef RB_SOCKADDR_HAS_SA_LEN | |
155 | #define ss_len sa_len | |
156 | #endif | |
157 | ||
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) | |
160 | #ifdef RB_SOCKADDR_HAS_SA_LEN | |
161 | #define SET_SS_LEN(x, y) do { \ | |
162 | struct sockaddr *_storage; \ | |
163 | _storage = ((struct sockaddr *)(x));\ | |
164 | _storage->sa_len = (y); \ | |
165 | } while (0) | |
166 | #define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len) | |
167 | #else /* !RB_SOCKADDR_HAS_SA_LEN */ | |
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 | ||
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 | ||
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 | ||
202 | #ifndef UINT16_MAX | |
203 | #define UINT16_MAX (65535U) | |
204 | #endif | |
205 | ||
206 | #ifndef UINT32_MAX | |
207 | #define UINT32_MAX (4294967295U) | |
208 | #endif | |
209 | ||
210 | ||
211 | typedef void log_cb(const char *buffer); | |
212 | typedef void restart_cb(const char *buffer); | |
213 | typedef void die_cb(const char *buffer); | |
214 | ||
215 | char *rb_ctime(const time_t, char *, size_t); | |
216 | char *rb_date(const time_t, char *, size_t); | |
217 | void rb_lib_log(const char *, ...); | |
218 | void rb_lib_restart(const char *, ...) __attribute__((noreturn)); | |
219 | void rb_lib_die(const char *, ...); | |
220 | void rb_set_time(void); | |
221 | const char *rb_lib_version(void); | |
222 | ||
223 | void 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); | |
225 | void rb_lib_loop(long delay) __attribute__((noreturn)); | |
226 | ||
227 | time_t rb_current_time(void); | |
228 | const struct timeval *rb_current_time_tv(void); | |
229 | pid_t rb_spawn_process(const char *, const char **); | |
230 | ||
231 | char *rb_strtok_r(char *, const char *, char **); | |
232 | ||
233 | int rb_gettimeofday(struct timeval *, void *); | |
234 | ||
235 | void rb_sleep(unsigned int seconds, unsigned int useconds); | |
236 | char *rb_crypt(const char *, const char *); | |
237 | ||
238 | unsigned char *rb_base64_encode(const unsigned char *str, int length); | |
239 | unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret); | |
240 | int rb_kill(pid_t, int); | |
241 | char *rb_strerror(int); | |
242 | ||
243 | int rb_setenv(const char *, const char *, int); | |
244 | ||
245 | pid_t rb_waitpid(pid_t pid, int *status, int options); | |
246 | pid_t rb_getpid(void); | |
247 | //unsigned int rb_geteuid(void); | |
248 | ||
249 | ||
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> | |
255 | #include <rb_event.h> | |
256 | #include <rb_helper.h> | |
257 | #include <rb_rawbuf.h> | |
258 | #include <rb_patricia.h> | |
259 | ||
260 | #endif |