]>
jfr.im git - solanum.git/blob - libratbox/src/ratbox_lib.c
2 * ircd-ratbox: A slightly useful ircd.
3 * ratbox_lib.c: libircd initialization functions at the like
5 * Copyright (C) 2005,2006 ircd-ratbox development team
6 * Copyright (C) 2005,2006 Aaron Sethman <androsyn@ratbox.org>
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.
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.
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
23 * $Id: ratbox_lib.c 25038 2008-01-23 16:03:08Z androsyn $
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
28 #include <commio-int.h>
30 static log_cb
*rb_log
;
31 static restart_cb
*rb_restart
;
32 static die_cb
*rb_die
;
34 static struct timeval rb_time
;
35 static char errbuf
[512];
37 /* this doesn't do locales...oh well i guess */
39 static const char *months
[] = {
40 "January", "February", "March", "April",
41 "May", "June", "July", "August",
42 "September", "October", "November", "December"
45 static const char *weekdays
[] = {
46 "Sunday", "Monday", "Tuesday", "Wednesday",
47 "Thursday", "Friday", "Saturday"
50 static const char *s_month
[] = {
51 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
52 "Aug", "Sep", "Oct", "Nov", "Dec"
55 static const char *s_weekdays
[] = {
56 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
60 rb_ctime(const time_t t
, char *buf
, size_t len
)
64 static char timex
[128];
66 #if defined(HAVE_GMTIME_R)
68 tp
= gmtime_r(&t
, &tmr
);
72 if(unlikely(tp
== NULL
))
87 rb_snprintf(p
, tlen
, "%s %s %d %02u:%02u:%02u %d",
88 s_weekdays
[tp
->tm_wday
], s_month
[tp
->tm_mon
],
89 tp
->tm_mday
, tp
->tm_hour
, tp
->tm_min
, tp
->tm_sec
,
95 /* I hate this..but its sort of ircd standard now.. */
97 rb_date(const time_t t
, char *buf
, size_t len
)
100 #if defined(HAVE_GMTIME_R)
102 gm
= gmtime_r(&t
, &gmbuf
);
107 if(unlikely(gm
== NULL
))
109 rb_strlcpy(buf
, "", len
);
113 rb_snprintf(buf
, len
, "%s %s %d %d -- %02u:%02u:%02u +00:00",
114 weekdays
[gm
->tm_wday
], months
[gm
->tm_mon
], gm
->tm_mday
,
115 gm
->tm_year
+ 1900, gm
->tm_hour
, gm
->tm_min
, gm
->tm_sec
);
120 rb_current_time(void)
122 return rb_time
.tv_sec
;
125 const struct timeval
*
126 rb_current_time_tv(void)
132 rb_lib_log(const char *format
, ...)
137 va_start(args
, format
);
138 rb_vsnprintf(errbuf
, sizeof(errbuf
), format
, args
);
144 rb_lib_die(const char *format
, ...)
149 va_start(args
, format
);
150 rb_vsnprintf(errbuf
, sizeof(errbuf
), format
, args
);
156 rb_lib_restart(const char *format
, ...)
159 if(rb_restart
== NULL
)
161 va_start(args
, format
);
162 rb_vsnprintf(errbuf
, sizeof(errbuf
), format
, args
);
170 struct timeval newtime
;
172 if(unlikely(rb_gettimeofday(&newtime
, NULL
) == -1))
174 rb_lib_log("Clock Failure (%s)", strerror(errno
));
175 rb_lib_restart("Clock Failure");
178 if(newtime
.tv_sec
< rb_time
.tv_sec
)
179 rb_set_back_events(rb_time
.tv_sec
- newtime
.tv_sec
);
181 memcpy(&rb_time
, &newtime
, sizeof(struct timeval
));
187 static const char *id
= "$Rev: 25038 $";
192 rb_lib_init(log_cb
*ilog
, restart_cb
*irestart
, die_cb
*idie
, int closeall
, int maxcon
, size_t dh_size
, size_t fd_heap_size
)
196 rb_restart
= irestart
;
200 rb_fdlist_init(closeall
, maxcon
, fd_heap_size
);
202 rb_init_rb_dlink_nodes(dh_size
);
203 if(rb_io_supports_event())
210 rb_lib_loop(long delay
)
215 if(rb_io_supports_event())
228 if((next
= rb_event_next()) > 0)
230 next
-= rb_current_time();
245 #ifndef HAVE_STRTOK_R
247 rb_strtok_r (char *s
, const char *delim
, char **save
)
254 /* Scan leading delimiters. */
255 s
+= strspn(s
, delim
);
264 s
= strpbrk(token
, delim
);
267 *save
= (token
+ strlen(token
));
277 *rb_strtok_r(char *s
, const char *delim
, char **save
)
279 return strtok_r(s
, delim
, save
);
284 static const char base64_table
[] =
285 { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
286 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
287 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
288 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
289 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
292 static const char base64_pad
= '=';
294 static const short base64_reverse_table
[256] = {
295 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
296 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
297 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
298 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
299 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
300 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
301 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
302 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
303 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1,
307 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
308 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
309 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
310 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
314 rb_base64_encode(const unsigned char *str
, int length
)
316 const unsigned char *current
= str
;
318 unsigned char *result
;
320 if ((length
+ 2) < 0 || ((length
+ 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
324 result
= rb_malloc(((length
+ 2) / 3) * 5);
329 *p
++ = base64_table
[current
[0] >> 2];
330 *p
++ = base64_table
[((current
[0] & 0x03) << 4) + (current
[1] >> 4)];
331 *p
++ = base64_table
[((current
[1] & 0x0f) << 2) + (current
[2] >> 6)];
332 *p
++ = base64_table
[current
[2] & 0x3f];
339 *p
++ = base64_table
[current
[0] >> 2];
341 *p
++ = base64_table
[((current
[0] & 0x03) << 4) + (current
[1] >> 4)];
342 *p
++ = base64_table
[(current
[1] & 0x0f) << 2];
345 *p
++ = base64_table
[(current
[0] & 0x03) << 4];
355 rb_base64_decode(const unsigned char *str
, int length
, int *ret
)
357 const unsigned char *current
= str
;
358 int ch
, i
= 0, j
= 0, k
;
359 unsigned char *result
;
361 result
= rb_malloc(length
+ 1);
363 while ((ch
= *current
++) != '\0' && length
-- > 0) {
364 if (ch
== base64_pad
) break;
366 ch
= base64_reverse_table
[ch
];
367 if (ch
< 0) continue;
374 result
[j
++] |= ch
>> 4;
375 result
[j
] = (ch
& 0x0f) << 4;
378 result
[j
++] |= ch
>>2;
379 result
[j
] = (ch
& 0x03) << 6;
390 if (ch
== base64_pad
) {