]>
Commit | Line | Data |
---|---|---|
db137867 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
fe037171 | 3 | * rb_lib.c: libircd initialization functions at the like |
db137867 AC |
4 | * |
5 | * Copyright (C) 2005,2006 ircd-ratbox development team | |
6 | * Copyright (C) 2005,2006 Aaron Sethman <androsyn@ratbox.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
21 | * USA | |
22 | * | |
db137867 AC |
23 | */ |
24 | ||
fe037171 EM |
25 | #include <librb_config.h> |
26 | #include <rb_lib.h> | |
db137867 | 27 | #include <commio-int.h> |
030272f3 | 28 | #include <commio-ssl.h> |
db137867 AC |
29 | |
30 | static log_cb *rb_log; | |
31 | static restart_cb *rb_restart; | |
32 | static die_cb *rb_die; | |
33 | ||
34 | static struct timeval rb_time; | |
35 | static char errbuf[512]; | |
36 | ||
37 | /* this doesn't do locales...oh well i guess */ | |
38 | ||
39 | static const char *months[] = { | |
3202e249 VY |
40 | "January", "February", "March", "April", |
41 | "May", "June", "July", "August", | |
42 | "September", "October", "November", "December" | |
db137867 AC |
43 | }; |
44 | ||
45 | static const char *weekdays[] = { | |
3202e249 VY |
46 | "Sunday", "Monday", "Tuesday", "Wednesday", |
47 | "Thursday", "Friday", "Saturday" | |
db137867 AC |
48 | }; |
49 | ||
50 | static const char *s_month[] = { | |
51 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", | |
52 | "Aug", "Sep", "Oct", "Nov", "Dec" | |
3202e249 | 53 | }; |
db137867 AC |
54 | |
55 | static const char *s_weekdays[] = { | |
56 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | |
57 | }; | |
58 | ||
59 | char * | |
60 | rb_ctime(const time_t t, char *buf, size_t len) | |
61 | { | |
62 | char *p; | |
63 | struct tm *tp; | |
64 | static char timex[128]; | |
65 | size_t tlen; | |
66 | #if defined(HAVE_GMTIME_R) | |
67 | struct tm tmr; | |
68 | tp = gmtime_r(&t, &tmr); | |
69 | #else | |
70 | tp = gmtime(&t); | |
71 | #endif | |
db137867 AC |
72 | if(buf == NULL) |
73 | { | |
74 | p = timex; | |
75 | tlen = sizeof(timex); | |
3202e249 VY |
76 | } |
77 | else | |
78 | { | |
db137867 AC |
79 | p = buf; |
80 | tlen = len; | |
81 | } | |
82 | ||
56c1612f AC |
83 | if(rb_unlikely(tp == NULL)) |
84 | { | |
85 | rb_strlcpy(p, "", tlen); | |
86 | return (p); | |
87 | } | |
88 | ||
5203cba5 | 89 | snprintf(p, tlen, "%s %s %d %02u:%02u:%02u %d", |
3202e249 VY |
90 | s_weekdays[tp->tm_wday], s_month[tp->tm_mon], |
91 | tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, tp->tm_year + 1900); | |
92 | return (p); | |
db137867 AC |
93 | } |
94 | ||
95 | ||
96 | /* I hate this..but its sort of ircd standard now.. */ | |
97 | char * | |
98 | rb_date(const time_t t, char *buf, size_t len) | |
99 | { | |
100 | struct tm *gm; | |
101 | #if defined(HAVE_GMTIME_R) | |
102 | struct tm gmbuf; | |
103 | gm = gmtime_r(&t, &gmbuf); | |
104 | #else | |
105 | gm = gmtime(&t); | |
106 | #endif | |
107 | ||
c2ac22cc | 108 | if(rb_unlikely(gm == NULL)) |
db137867 | 109 | { |
3202e249 VY |
110 | rb_strlcpy(buf, "", len); |
111 | return (buf); | |
db137867 | 112 | } |
3202e249 | 113 | |
5203cba5 | 114 | snprintf(buf, len, "%s %s %d %d -- %02u:%02u:%02u +00:00", |
3202e249 VY |
115 | weekdays[gm->tm_wday], months[gm->tm_mon], gm->tm_mday, |
116 | gm->tm_year + 1900, gm->tm_hour, gm->tm_min, gm->tm_sec); | |
117 | return (buf); | |
db137867 AC |
118 | } |
119 | ||
120 | time_t | |
121 | rb_current_time(void) | |
122 | { | |
123 | return rb_time.tv_sec; | |
124 | } | |
125 | ||
126 | const struct timeval * | |
127 | rb_current_time_tv(void) | |
128 | { | |
129 | return &rb_time; | |
130 | } | |
131 | ||
132 | void | |
133 | rb_lib_log(const char *format, ...) | |
134 | { | |
135 | va_list args; | |
136 | if(rb_log == NULL) | |
137 | return; | |
138 | va_start(args, format); | |
5203cba5 | 139 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
140 | va_end(args); |
141 | rb_log(errbuf); | |
142 | } | |
143 | ||
144 | void | |
145 | rb_lib_die(const char *format, ...) | |
146 | { | |
147 | va_list args; | |
148 | if(rb_die == NULL) | |
149 | abort(); | |
150 | va_start(args, format); | |
5203cba5 | 151 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
152 | va_end(args); |
153 | rb_die(errbuf); | |
154 | } | |
155 | ||
156 | void | |
157 | rb_lib_restart(const char *format, ...) | |
158 | { | |
159 | va_list args; | |
160 | if(rb_restart == NULL) | |
161 | abort(); | |
162 | va_start(args, format); | |
5203cba5 | 163 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
164 | va_end(args); |
165 | rb_restart(errbuf); | |
6993ae27 | 166 | abort(); |
db137867 AC |
167 | } |
168 | ||
169 | void | |
170 | rb_set_time(void) | |
171 | { | |
172 | struct timeval newtime; | |
173 | ||
c2ac22cc | 174 | if(rb_unlikely(rb_gettimeofday(&newtime, NULL) == -1)) |
db137867 AC |
175 | { |
176 | rb_lib_log("Clock Failure (%s)", strerror(errno)); | |
177 | rb_lib_restart("Clock Failure"); | |
178 | } | |
179 | ||
180 | if(newtime.tv_sec < rb_time.tv_sec) | |
181 | rb_set_back_events(rb_time.tv_sec - newtime.tv_sec); | |
182 | ||
183 | memcpy(&rb_time, &newtime, sizeof(struct timeval)); | |
184 | } | |
185 | ||
fe037171 | 186 | extern const char *librb_serno; |
3202e249 | 187 | |
db137867 AC |
188 | const char * |
189 | rb_lib_version(void) | |
190 | { | |
030272f3 VY |
191 | static char version_info[512]; |
192 | char ssl_info[512]; | |
193 | rb_get_ssl_info(ssl_info, sizeof(ssl_info)); | |
fe037171 | 194 | snprintf(version_info, sizeof(version_info), "librb version: %s - %s", librb_serno, ssl_info); |
030272f3 | 195 | return version_info; |
db137867 AC |
196 | } |
197 | ||
198 | void | |
3202e249 VY |
199 | rb_lib_init(log_cb * ilog, restart_cb * irestart, die_cb * idie, int closeall, int maxcon, |
200 | size_t dh_size, size_t fd_heap_size) | |
db137867 AC |
201 | { |
202 | rb_set_time(); | |
203 | rb_log = ilog; | |
204 | rb_restart = irestart; | |
205 | rb_die = idie; | |
206 | rb_event_init(); | |
207 | rb_init_bh(); | |
208 | rb_fdlist_init(closeall, maxcon, fd_heap_size); | |
209 | rb_init_netio(); | |
210 | rb_init_rb_dlink_nodes(dh_size); | |
211 | if(rb_io_supports_event()) | |
212 | { | |
213 | rb_io_init_event(); | |
214 | } | |
215 | } | |
216 | ||
217 | void | |
218 | rb_lib_loop(long delay) | |
219 | { | |
220 | time_t next; | |
221 | rb_set_time(); | |
222 | ||
223 | if(rb_io_supports_event()) | |
224 | { | |
225 | if(delay == 0) | |
226 | delay = -1; | |
227 | while(1) | |
228 | rb_select(-1); | |
229 | } | |
3202e249 | 230 | |
db137867 AC |
231 | |
232 | while(1) | |
233 | { | |
234 | if(delay == 0) | |
235 | { | |
3202e249 | 236 | if((next = rb_event_next()) > 0) |
db137867 AC |
237 | { |
238 | next -= rb_current_time(); | |
239 | if(next <= 0) | |
240 | next = 1000; | |
3202e249 | 241 | else |
db137867 AC |
242 | next *= 1000; |
243 | } | |
244 | else | |
245 | next = -1; | |
246 | rb_select(next); | |
3202e249 VY |
247 | } |
248 | else | |
db137867 AC |
249 | rb_select(delay); |
250 | rb_event_run(); | |
251 | } | |
252 | } | |
253 | ||
254 | #ifndef HAVE_STRTOK_R | |
255 | char * | |
3202e249 | 256 | rb_strtok_r(char *s, const char *delim, char **save) |
db137867 AC |
257 | { |
258 | char *token; | |
259 | ||
3202e249 | 260 | if(s == NULL) |
db137867 AC |
261 | s = *save; |
262 | ||
263 | /* Scan leading delimiters. */ | |
264 | s += strspn(s, delim); | |
265 | ||
3202e249 | 266 | if(*s == '\0') |
db137867 AC |
267 | { |
268 | *save = s; | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | token = s; | |
273 | s = strpbrk(token, delim); | |
3202e249 VY |
274 | |
275 | if(s == NULL) | |
db137867 AC |
276 | *save = (token + strlen(token)); |
277 | else | |
278 | { | |
3202e249 | 279 | *s = '\0'; |
db137867 AC |
280 | *save = s + 1; |
281 | } | |
282 | return token; | |
283 | } | |
284 | #else | |
3202e249 VY |
285 | char * |
286 | rb_strtok_r(char *s, const char *delim, char **save) | |
db137867 AC |
287 | { |
288 | return strtok_r(s, delim, save); | |
289 | } | |
290 | #endif | |
291 | ||
292 | ||
293 | static const char base64_table[] = | |
294 | { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
3202e249 VY |
295 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
296 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
297 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
298 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' | |
299 | }; | |
db137867 AC |
300 | |
301 | static const char base64_pad = '='; | |
302 | ||
303 | static const short base64_reverse_table[256] = { | |
304 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
305 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
306 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, | |
307 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, | |
3202e249 | 308 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
db137867 AC |
309 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
310 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
311 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | |
312 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
313 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
314 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
315 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
316 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
317 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
318 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
319 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 | |
320 | }; | |
321 | ||
322 | unsigned char * | |
323 | rb_base64_encode(const unsigned char *str, int length) | |
324 | { | |
325 | const unsigned char *current = str; | |
326 | unsigned char *p; | |
327 | unsigned char *result; | |
328 | ||
3202e249 VY |
329 | if((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) |
330 | { | |
db137867 AC |
331 | return NULL; |
332 | } | |
333 | ||
334 | result = rb_malloc(((length + 2) / 3) * 5); | |
335 | p = result; | |
336 | ||
3202e249 VY |
337 | while(length > 2) |
338 | { | |
db137867 AC |
339 | *p++ = base64_table[current[0] >> 2]; |
340 | *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; | |
341 | *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; | |
342 | *p++ = base64_table[current[2] & 0x3f]; | |
343 | ||
344 | current += 3; | |
3202e249 | 345 | length -= 3; |
db137867 AC |
346 | } |
347 | ||
3202e249 VY |
348 | if(length != 0) |
349 | { | |
db137867 | 350 | *p++ = base64_table[current[0] >> 2]; |
3202e249 VY |
351 | if(length > 1) |
352 | { | |
db137867 AC |
353 | *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; |
354 | *p++ = base64_table[(current[1] & 0x0f) << 2]; | |
355 | *p++ = base64_pad; | |
3202e249 VY |
356 | } |
357 | else | |
358 | { | |
db137867 AC |
359 | *p++ = base64_table[(current[0] & 0x03) << 4]; |
360 | *p++ = base64_pad; | |
361 | *p++ = base64_pad; | |
362 | } | |
363 | } | |
364 | *p = '\0'; | |
365 | return result; | |
366 | } | |
367 | ||
368 | unsigned char * | |
369 | rb_base64_decode(const unsigned char *str, int length, int *ret) | |
370 | { | |
371 | const unsigned char *current = str; | |
372 | int ch, i = 0, j = 0, k; | |
373 | unsigned char *result; | |
3202e249 | 374 | |
db137867 AC |
375 | result = rb_malloc(length + 1); |
376 | ||
3202e249 VY |
377 | while((ch = *current++) != '\0' && length-- > 0) |
378 | { | |
379 | if(ch == base64_pad) | |
380 | break; | |
db137867 AC |
381 | |
382 | ch = base64_reverse_table[ch]; | |
3202e249 VY |
383 | if(ch < 0) |
384 | continue; | |
db137867 | 385 | |
3202e249 VY |
386 | switch (i % 4) |
387 | { | |
db137867 AC |
388 | case 0: |
389 | result[j] = ch << 2; | |
390 | break; | |
391 | case 1: | |
392 | result[j++] |= ch >> 4; | |
393 | result[j] = (ch & 0x0f) << 4; | |
394 | break; | |
395 | case 2: | |
3202e249 | 396 | result[j++] |= ch >> 2; |
db137867 AC |
397 | result[j] = (ch & 0x03) << 6; |
398 | break; | |
399 | case 3: | |
400 | result[j++] |= ch; | |
401 | break; | |
402 | } | |
403 | i++; | |
404 | } | |
405 | ||
406 | k = j; | |
407 | ||
3202e249 VY |
408 | if(ch == base64_pad) |
409 | { | |
410 | switch (i % 4) | |
411 | { | |
db137867 AC |
412 | case 1: |
413 | free(result); | |
414 | return NULL; | |
415 | case 2: | |
416 | k++; | |
8c04f896 | 417 | /* FALLTHROUGH */ |
db137867 AC |
418 | case 3: |
419 | result[k++] = 0; | |
420 | } | |
421 | } | |
422 | result[j] = '\0'; | |
423 | *ret = j; | |
424 | return result; | |
425 | } |