]>
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; | |
db137867 AC |
66 | struct tm tmr; |
67 | tp = gmtime_r(&t, &tmr); | |
3c586ccf | 68 | |
db137867 AC |
69 | if(buf == NULL) |
70 | { | |
71 | p = timex; | |
72 | tlen = sizeof(timex); | |
3202e249 VY |
73 | } |
74 | else | |
75 | { | |
db137867 AC |
76 | p = buf; |
77 | tlen = len; | |
78 | } | |
79 | ||
56c1612f AC |
80 | if(rb_unlikely(tp == NULL)) |
81 | { | |
82 | rb_strlcpy(p, "", tlen); | |
83 | return (p); | |
84 | } | |
85 | ||
5203cba5 | 86 | snprintf(p, tlen, "%s %s %d %02u:%02u:%02u %d", |
3202e249 VY |
87 | s_weekdays[tp->tm_wday], s_month[tp->tm_mon], |
88 | tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, tp->tm_year + 1900); | |
89 | return (p); | |
db137867 AC |
90 | } |
91 | ||
92 | ||
93 | /* I hate this..but its sort of ircd standard now.. */ | |
94 | char * | |
95 | rb_date(const time_t t, char *buf, size_t len) | |
96 | { | |
97 | struct tm *gm; | |
db137867 AC |
98 | struct tm gmbuf; |
99 | gm = gmtime_r(&t, &gmbuf); | |
db137867 | 100 | |
c2ac22cc | 101 | if(rb_unlikely(gm == NULL)) |
db137867 | 102 | { |
3202e249 VY |
103 | rb_strlcpy(buf, "", len); |
104 | return (buf); | |
db137867 | 105 | } |
3202e249 | 106 | |
5203cba5 | 107 | snprintf(buf, len, "%s %s %d %d -- %02u:%02u:%02u +00:00", |
3202e249 VY |
108 | weekdays[gm->tm_wday], months[gm->tm_mon], gm->tm_mday, |
109 | gm->tm_year + 1900, gm->tm_hour, gm->tm_min, gm->tm_sec); | |
110 | return (buf); | |
db137867 AC |
111 | } |
112 | ||
113 | time_t | |
114 | rb_current_time(void) | |
115 | { | |
116 | return rb_time.tv_sec; | |
117 | } | |
118 | ||
119 | const struct timeval * | |
120 | rb_current_time_tv(void) | |
121 | { | |
122 | return &rb_time; | |
123 | } | |
124 | ||
125 | void | |
126 | rb_lib_log(const char *format, ...) | |
127 | { | |
128 | va_list args; | |
129 | if(rb_log == NULL) | |
130 | return; | |
131 | va_start(args, format); | |
5203cba5 | 132 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
133 | va_end(args); |
134 | rb_log(errbuf); | |
135 | } | |
136 | ||
137 | void | |
138 | rb_lib_die(const char *format, ...) | |
139 | { | |
140 | va_list args; | |
141 | if(rb_die == NULL) | |
142 | abort(); | |
143 | va_start(args, format); | |
5203cba5 | 144 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
145 | va_end(args); |
146 | rb_die(errbuf); | |
147 | } | |
148 | ||
149 | void | |
150 | rb_lib_restart(const char *format, ...) | |
151 | { | |
152 | va_list args; | |
153 | if(rb_restart == NULL) | |
154 | abort(); | |
155 | va_start(args, format); | |
5203cba5 | 156 | vsnprintf(errbuf, sizeof(errbuf), format, args); |
db137867 AC |
157 | va_end(args); |
158 | rb_restart(errbuf); | |
6993ae27 | 159 | abort(); |
db137867 AC |
160 | } |
161 | ||
162 | void | |
163 | rb_set_time(void) | |
164 | { | |
165 | struct timeval newtime; | |
166 | ||
c2ac22cc | 167 | if(rb_unlikely(rb_gettimeofday(&newtime, NULL) == -1)) |
db137867 AC |
168 | { |
169 | rb_lib_log("Clock Failure (%s)", strerror(errno)); | |
170 | rb_lib_restart("Clock Failure"); | |
171 | } | |
172 | ||
173 | if(newtime.tv_sec < rb_time.tv_sec) | |
174 | rb_set_back_events(rb_time.tv_sec - newtime.tv_sec); | |
175 | ||
176 | memcpy(&rb_time, &newtime, sizeof(struct timeval)); | |
177 | } | |
178 | ||
fe037171 | 179 | extern const char *librb_serno; |
3202e249 | 180 | |
db137867 AC |
181 | const char * |
182 | rb_lib_version(void) | |
183 | { | |
c4c1bf3b | 184 | static char version_info[640]; |
030272f3 VY |
185 | char ssl_info[512]; |
186 | rb_get_ssl_info(ssl_info, sizeof(ssl_info)); | |
fe037171 | 187 | snprintf(version_info, sizeof(version_info), "librb version: %s - %s", librb_serno, ssl_info); |
030272f3 | 188 | return version_info; |
db137867 AC |
189 | } |
190 | ||
191 | void | |
3202e249 VY |
192 | rb_lib_init(log_cb * ilog, restart_cb * irestart, die_cb * idie, int closeall, int maxcon, |
193 | size_t dh_size, size_t fd_heap_size) | |
db137867 AC |
194 | { |
195 | rb_set_time(); | |
196 | rb_log = ilog; | |
197 | rb_restart = irestart; | |
198 | rb_die = idie; | |
199 | rb_event_init(); | |
200 | rb_init_bh(); | |
201 | rb_fdlist_init(closeall, maxcon, fd_heap_size); | |
202 | rb_init_netio(); | |
203 | rb_init_rb_dlink_nodes(dh_size); | |
204 | if(rb_io_supports_event()) | |
205 | { | |
206 | rb_io_init_event(); | |
207 | } | |
208 | } | |
209 | ||
210 | void | |
211 | rb_lib_loop(long delay) | |
212 | { | |
213 | time_t next; | |
214 | rb_set_time(); | |
215 | ||
216 | if(rb_io_supports_event()) | |
217 | { | |
db137867 AC |
218 | while(1) |
219 | rb_select(-1); | |
220 | } | |
3202e249 | 221 | |
db137867 AC |
222 | |
223 | while(1) | |
224 | { | |
225 | if(delay == 0) | |
226 | { | |
3202e249 | 227 | if((next = rb_event_next()) > 0) |
db137867 AC |
228 | { |
229 | next -= rb_current_time(); | |
230 | if(next <= 0) | |
231 | next = 1000; | |
3202e249 | 232 | else |
db137867 AC |
233 | next *= 1000; |
234 | } | |
235 | else | |
236 | next = -1; | |
237 | rb_select(next); | |
3202e249 VY |
238 | } |
239 | else | |
db137867 AC |
240 | rb_select(delay); |
241 | rb_event_run(); | |
242 | } | |
243 | } | |
244 | ||
3202e249 VY |
245 | char * |
246 | rb_strtok_r(char *s, const char *delim, char **save) | |
db137867 AC |
247 | { |
248 | return strtok_r(s, delim, save); | |
249 | } | |
db137867 AC |
250 | |
251 | static const char base64_table[] = | |
252 | { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
3202e249 VY |
253 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
254 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
255 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
256 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' | |
257 | }; | |
db137867 AC |
258 | |
259 | static const char base64_pad = '='; | |
260 | ||
261 | static const short base64_reverse_table[256] = { | |
262 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
263 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
264 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, | |
265 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, | |
3202e249 | 266 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
db137867 AC |
267 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
268 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
269 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | |
270 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
271 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
272 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
273 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
274 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
275 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
276 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
277 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 | |
278 | }; | |
279 | ||
280 | unsigned char * | |
281 | rb_base64_encode(const unsigned char *str, int length) | |
282 | { | |
283 | const unsigned char *current = str; | |
284 | unsigned char *p; | |
285 | unsigned char *result; | |
286 | ||
3202e249 VY |
287 | if((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) |
288 | { | |
db137867 AC |
289 | return NULL; |
290 | } | |
291 | ||
292 | result = rb_malloc(((length + 2) / 3) * 5); | |
293 | p = result; | |
294 | ||
3202e249 VY |
295 | while(length > 2) |
296 | { | |
db137867 AC |
297 | *p++ = base64_table[current[0] >> 2]; |
298 | *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; | |
299 | *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; | |
300 | *p++ = base64_table[current[2] & 0x3f]; | |
301 | ||
302 | current += 3; | |
3202e249 | 303 | length -= 3; |
db137867 AC |
304 | } |
305 | ||
3202e249 VY |
306 | if(length != 0) |
307 | { | |
db137867 | 308 | *p++ = base64_table[current[0] >> 2]; |
3202e249 VY |
309 | if(length > 1) |
310 | { | |
db137867 AC |
311 | *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; |
312 | *p++ = base64_table[(current[1] & 0x0f) << 2]; | |
313 | *p++ = base64_pad; | |
3202e249 VY |
314 | } |
315 | else | |
316 | { | |
db137867 AC |
317 | *p++ = base64_table[(current[0] & 0x03) << 4]; |
318 | *p++ = base64_pad; | |
319 | *p++ = base64_pad; | |
320 | } | |
321 | } | |
322 | *p = '\0'; | |
323 | return result; | |
324 | } | |
325 | ||
326 | unsigned char * | |
327 | rb_base64_decode(const unsigned char *str, int length, int *ret) | |
328 | { | |
329 | const unsigned char *current = str; | |
330 | int ch, i = 0, j = 0, k; | |
331 | unsigned char *result; | |
3202e249 | 332 | |
db137867 AC |
333 | result = rb_malloc(length + 1); |
334 | ||
3202e249 VY |
335 | while((ch = *current++) != '\0' && length-- > 0) |
336 | { | |
337 | if(ch == base64_pad) | |
338 | break; | |
db137867 AC |
339 | |
340 | ch = base64_reverse_table[ch]; | |
3202e249 VY |
341 | if(ch < 0) |
342 | continue; | |
db137867 | 343 | |
3202e249 VY |
344 | switch (i % 4) |
345 | { | |
db137867 AC |
346 | case 0: |
347 | result[j] = ch << 2; | |
348 | break; | |
349 | case 1: | |
350 | result[j++] |= ch >> 4; | |
351 | result[j] = (ch & 0x0f) << 4; | |
352 | break; | |
353 | case 2: | |
3202e249 | 354 | result[j++] |= ch >> 2; |
db137867 AC |
355 | result[j] = (ch & 0x03) << 6; |
356 | break; | |
357 | case 3: | |
358 | result[j++] |= ch; | |
359 | break; | |
360 | } | |
361 | i++; | |
362 | } | |
363 | ||
364 | k = j; | |
365 | ||
3202e249 VY |
366 | if(ch == base64_pad) |
367 | { | |
368 | switch (i % 4) | |
369 | { | |
db137867 AC |
370 | case 1: |
371 | free(result); | |
372 | return NULL; | |
373 | case 2: | |
374 | k++; | |
8c04f896 | 375 | /* FALLTHROUGH */ |
db137867 AC |
376 | case 3: |
377 | result[k++] = 0; | |
378 | } | |
379 | } | |
380 | result[j] = '\0'; | |
381 | *ret = j; | |
382 | return result; | |
383 | } |