]> jfr.im git - solanum.git/blame - wsockd/wsockd.c
wsockd: add win32 strcasestr()
[solanum.git] / wsockd / wsockd.c
CommitLineData
caebeeca
AC
1/*
2 * wsockd.c: charybdis websockets helper
3 * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
4 * Copyright (C) 2007 ircd-ratbox development team
5 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23#include "stdinc.h"
1160f6c9 24#include "sha1.h"
caebeeca
AC
25
26#define MAXPASSFD 4
27#ifndef READBUF_SIZE
28#define READBUF_SIZE 16384
29#endif
30
1160f6c9
AC
31#define WEBSOCKET_SERVER_KEY "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
32#define WEBSOCKET_ANSWER_STRING_1 "HTTP/1.1 101 Switching Protocols\r\nAccess-Control-Allow-Origin: *\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "
33#define WEBSOCKET_ANSWER_STRING_2 "\r\n\r\n"
34
caebeeca
AC
35static void setup_signals(void);
36static pid_t ppid;
37
38static inline uint32_t
39buf_to_uint32(uint8_t *buf)
40{
41 uint32_t x;
42 memcpy(&x, buf, sizeof(x));
43 return x;
44}
45
46static inline void
47uint32_to_buf(uint8_t *buf, uint32_t x)
48{
49 memcpy(buf, &x, sizeof(x));
50 return;
51}
52
53typedef struct _mod_ctl_buf
54{
55 rb_dlink_node node;
56 uint8_t *buf;
57 size_t buflen;
58 rb_fde_t *F[MAXPASSFD];
59 int nfds;
60} mod_ctl_buf_t;
61
62typedef struct _mod_ctl
63{
64 rb_dlink_node node;
65 int cli_count;
66 rb_fde_t *F;
67 rb_fde_t *F_pipe;
68 rb_dlink_list readq;
69 rb_dlink_list writeq;
70} mod_ctl_t;
71
72static mod_ctl_t *mod_ctl;
73
74typedef struct _conn
75{
76 rb_dlink_node node;
77 mod_ctl_t *ctl;
1c8c63cb 78
1160f6c9
AC
79 rawbuf_head_t *modbuf_out;
80 rawbuf_head_t *modbuf_in;
1c8c63cb
AC
81
82 buf_head_t plainbuf_out;
83 buf_head_t plainbuf_in;
caebeeca
AC
84
85 uint32_t id;
86
87 rb_fde_t *mod_fd;
88 rb_fde_t *plain_fd;
89 uint64_t mod_out;
90 uint64_t mod_in;
91 uint64_t plain_in;
92 uint64_t plain_out;
93 uint8_t flags;
1160f6c9
AC
94
95 char client_key[37]; /* maximum 36 bytes + nul */
caebeeca
AC
96} conn_t;
97
37052804
AC
98#ifdef _WIN32
99char *
100strcasestr(const char *s, *find)
101{
102 char c, sc;
103 size_t len;
104
105 if ((c = *find++) != 0) {
106 c = tolower((unsigned char)c);
107 len = strlen(find);
108 do {
109 do {
110 if ((sc = *s++) == 0)
111 return (NULL);
112 } while ((char)tolower((unsigned char)sc) != c);
113 } while (strnicmp(s, find, len) != 0);
114 s--;
115 }
116 return ((char *)s);
117}
118#endif
119
1160f6c9
AC
120static void close_conn(conn_t * conn, int wait_plain, const char *fmt, ...);
121static void conn_mod_read_cb(rb_fde_t *fd, void *data);
122static void conn_plain_read_cb(rb_fde_t *fd, void *data);
123
caebeeca
AC
124#define FLAG_CORK 0x01
125#define FLAG_DEAD 0x02
126#define FLAG_WSOCK 0x04
1160f6c9 127#define FLAG_KEYED 0x08
caebeeca
AC
128
129#define IsCork(x) ((x)->flags & FLAG_CORK)
130#define IsDead(x) ((x)->flags & FLAG_DEAD)
131#define IsWS(x) ((x)->flags & FLAG_WSOCK)
1160f6c9 132#define IsKeyed(x) ((x)->flags & FLAG_KEYED)
caebeeca
AC
133
134#define SetCork(x) ((x)->flags |= FLAG_CORK)
135#define SetDead(x) ((x)->flags |= FLAG_DEAD)
136#define SetWS(x) ((x)->flags |= FLAG_WSOCK)
1160f6c9 137#define SetKeyed(x) ((x)->flags |= FLAG_KEYED)
caebeeca
AC
138
139#define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
140#define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
141#define ClearWS(x) ((x)->flags &= ~FLAG_WSOCK)
1160f6c9 142#define ClearKeyed(x) ((x)->flags &= ~FLAG_KEYED)
caebeeca
AC
143
144#define NO_WAIT 0x0
145#define WAIT_PLAIN 0x1
146
147#define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
148#define HASH_WALK_END }
149#define CONN_HASH_SIZE 2000
150#define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
151
1160f6c9
AC
152static const char *remote_closed = "Remote host closed the connection";
153
caebeeca
AC
154static rb_dlink_list connid_hash_table[CONN_HASH_SIZE];
155static rb_dlink_list dead_list;
156
05e0aa9a
AC
157static void conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data);
158
caebeeca
AC
159#ifndef _WIN32
160static void
161dummy_handler(int sig)
162{
163 return;
164}
165#endif
166
167static void
168setup_signals()
169{
170#ifndef _WIN32
171 struct sigaction act;
172
173 act.sa_flags = 0;
174 act.sa_handler = SIG_IGN;
175 sigemptyset(&act.sa_mask);
176 sigaddset(&act.sa_mask, SIGPIPE);
177 sigaddset(&act.sa_mask, SIGALRM);
178#ifdef SIGTRAP
179 sigaddset(&act.sa_mask, SIGTRAP);
180#endif
181
182#ifdef SIGWINCH
183 sigaddset(&act.sa_mask, SIGWINCH);
184 sigaction(SIGWINCH, &act, 0);
185#endif
186 sigaction(SIGPIPE, &act, 0);
187#ifdef SIGTRAP
188 sigaction(SIGTRAP, &act, 0);
189#endif
190
191 act.sa_handler = dummy_handler;
192 sigaction(SIGALRM, &act, 0);
193#endif
194}
195
196static int
197maxconn(void)
198{
199#if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
200 struct rlimit limit;
201
202 if(!getrlimit(RLIMIT_NOFILE, &limit))
203 {
204 return limit.rlim_cur;
205 }
206#endif /* RLIMIT_FD_MAX */
207 return MAXCONNECTIONS;
208}
209
210static conn_t *
211conn_find_by_id(uint32_t id)
212{
213 rb_dlink_node *ptr;
214 conn_t *conn;
215
216 RB_DLINK_FOREACH(ptr, (connid_hash(id))->head)
217 {
218 conn = ptr->data;
219 if(conn->id == id && !IsDead(conn))
220 return conn;
221 }
222 return NULL;
223}
224
225static void
226conn_add_id_hash(conn_t * conn, uint32_t id)
227{
228 conn->id = id;
229 rb_dlinkAdd(conn, &conn->node, connid_hash(id));
230}
231
232static void
233free_conn(conn_t * conn)
234{
1c8c63cb
AC
235 rb_linebuf_donebuf(&conn->plainbuf_in);
236 rb_linebuf_donebuf(&conn->plainbuf_out);
237
1160f6c9
AC
238 rb_free_rawbuffer(conn->modbuf_in);
239 rb_free_rawbuffer(conn->modbuf_out);
1c8c63cb 240
caebeeca
AC
241 rb_free(conn);
242}
243
244static void
245clean_dead_conns(void *unused)
246{
247 conn_t *conn;
248 rb_dlink_node *ptr, *next;
249
250 RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
251 {
252 conn = ptr->data;
253 free_conn(conn);
254 }
255
256 dead_list.tail = dead_list.head = NULL;
257}
258
1160f6c9
AC
259static void
260conn_mod_write_sendq(rb_fde_t *fd, void *data)
261{
262 conn_t *conn = data;
263 const char *err;
264 int retlen;
265
266 if(IsDead(conn))
267 return;
268
269 while((retlen = rb_rawbuf_flush(conn->modbuf_out, fd)) > 0)
270 conn->mod_out += retlen;
271
272 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
273 {
274 if(retlen == 0)
275 close_conn(conn, WAIT_PLAIN, "%s", remote_closed);
276 err = strerror(errno);
277 close_conn(conn, WAIT_PLAIN, "Write error: %s", err);
278 return;
279 }
280
281 if(rb_rawbuf_length(conn->modbuf_out) > 0)
282 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, conn_mod_write_sendq, conn);
283 else
284 rb_setselect(conn->mod_fd, RB_SELECT_WRITE, NULL, NULL);
285
286 if(IsCork(conn) && rb_rawbuf_length(conn->modbuf_out) == 0)
287 {
288 ClearCork(conn);
289 conn_plain_read_cb(conn->plain_fd, conn);
290 }
291}
292
293static void
294conn_mod_write(conn_t * conn, void *data, size_t len)
295{
296 if(IsDead(conn)) /* no point in queueing to a dead man */
297 return;
298 rb_rawbuf_append(conn->modbuf_out, data, len);
299}
300
f297042b
AC
301static void
302conn_mod_write_frame(conn_t * conn, void *data, size_t len)
303{
304 if(IsDead(conn)) /* no point in queueing to a dead man */
305 return;
306}
307
1160f6c9
AC
308static void
309conn_plain_write(conn_t * conn, void *data, size_t len)
310{
311 if(IsDead(conn)) /* again no point in queueing to dead men */
312 return;
313 rb_linebuf_put(&conn->plainbuf_out, data, len);
314}
315
05e0aa9a
AC
316static void
317mod_write_ctl(rb_fde_t *F, void *data)
318{
319 mod_ctl_t *ctl = data;
320 mod_ctl_buf_t *ctl_buf;
321 rb_dlink_node *ptr, *next;
322 int retlen, x;
323
324 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
325 {
326 ctl_buf = ptr->data;
327 retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf,
328 ctl_buf->buflen, ppid);
329 if(retlen > 0)
330 {
331 rb_dlinkDelete(ptr, &ctl->writeq);
332 for(x = 0; x < ctl_buf->nfds; x++)
333 rb_close(ctl_buf->F[x]);
334 rb_free(ctl_buf->buf);
335 rb_free(ctl_buf);
336
337 }
338 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
339 exit(0);
340
341 }
342 if(rb_dlink_list_length(&ctl->writeq) > 0)
343 rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl);
344}
345
346static void
347mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len)
348{
349 mod_ctl_buf_t *ctl_buf;
350 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
351 ctl_buf->buf = rb_malloc(len);
352 ctl_buf->buflen = len;
353 memcpy(ctl_buf->buf, data, len);
354 ctl_buf->nfds = 0;
355 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
356 mod_write_ctl(ctl->F, ctl);
357}
358
359static void
360close_conn(conn_t * conn, int wait_plain, const char *fmt, ...)
361{
362 va_list ap;
363 char reason[128]; /* must always be under 250 bytes */
364 uint8_t buf[256];
365 int len;
366 if(IsDead(conn))
367 return;
368
1160f6c9 369 rb_rawbuf_flush(conn->modbuf_out, conn->mod_fd);
1c8c63cb 370 rb_linebuf_flush(conn->plain_fd, &conn->plainbuf_out);
05e0aa9a
AC
371 rb_close(conn->mod_fd);
372 SetDead(conn);
373
374 rb_dlinkDelete(&conn->node, connid_hash(conn->id));
375
376 if(!wait_plain || fmt == NULL)
377 {
378 rb_close(conn->plain_fd);
379 rb_dlinkAdd(conn, &conn->node, &dead_list);
380 return;
381 }
382
383 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
384 rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
385
386 va_start(ap, fmt);
387 vsnprintf(reason, sizeof(reason), fmt, ap);
388 va_end(ap);
389
390 buf[0] = 'D';
391 uint32_to_buf(&buf[1], conn->id);
392 rb_strlcpy((char *) &buf[5], reason, sizeof(buf) - 5);
393 len = (strlen(reason) + 1) + 5;
394 mod_cmd_write_queue(conn->ctl, buf, len);
395}
396
caebeeca
AC
397static conn_t *
398make_conn(mod_ctl_t * ctl, rb_fde_t *mod_fd, rb_fde_t *plain_fd)
399{
400 conn_t *conn = rb_malloc(sizeof(conn_t));
401 conn->ctl = ctl;
caebeeca
AC
402 conn->mod_fd = mod_fd;
403 conn->plain_fd = plain_fd;
404 conn->id = -1;
caebeeca
AC
405 rb_set_nb(mod_fd);
406 rb_set_nb(plain_fd);
1c8c63cb
AC
407
408 rb_linebuf_newbuf(&conn->plainbuf_in);
409 rb_linebuf_newbuf(&conn->plainbuf_out);
410
1160f6c9
AC
411 conn->modbuf_in = rb_new_rawbuffer();
412 conn->modbuf_out = rb_new_rawbuffer();
1c8c63cb 413
caebeeca
AC
414 return conn;
415}
416
417static void
418cleanup_bad_message(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
419{
420 int i;
421
422 /* XXX should log this somehow */
423 for (i = 0; i < ctlb->nfds; i++)
424 rb_close(ctlb->F[i]);
425}
426
1c8c63cb
AC
427static void
428conn_mod_handshake_process(conn_t *conn)
429{
430 char inbuf[READBUF_SIZE];
431
432 while (1)
433 {
1160f6c9
AC
434 char *p = NULL;
435
436 size_t dolen = rb_rawbuf_get(conn->modbuf_in, inbuf, sizeof inbuf);
1c8c63cb
AC
437 if (!dolen)
438 break;
1160f6c9
AC
439
440 if ((p = strcasestr(inbuf, "Sec-WebSocket-Key:")) != NULL)
441 {
442 char *start, *end;
443
444 start = p + strlen("Sec-WebSocket-Key:");
445
446 for (; start < (inbuf + READBUF_SIZE) && *start; start++)
447 {
448 if (*start != ' ' && *start != '\t')
449 break;
450 }
451
452 for (end = start; end < (inbuf + READBUF_SIZE) && *end; end++)
453 {
454 if (*end == '\r' || *end == '\n')
455 {
456 *end = '\0';
457 break;
458 }
459 }
460
461 rb_strlcpy(conn->client_key, start, sizeof(conn->client_key));
462 SetKeyed(conn);
463 }
464 }
465
466 if (IsKeyed(conn))
467 {
468 SHA1 sha1;
469 uint8_t digest[SHA1_DIGEST_LENGTH];
470 char *resp;
471
472 sha1_init(&sha1);
473 sha1_update(&sha1, (uint8_t *) conn->client_key, strlen(conn->client_key));
474 sha1_update(&sha1, (uint8_t *) WEBSOCKET_SERVER_KEY, strlen(WEBSOCKET_SERVER_KEY));
475 sha1_final(&sha1, digest);
476
477 resp = (char *) rb_base64_encode(digest, SHA1_DIGEST_LENGTH);
478
479 conn_mod_write(conn, WEBSOCKET_ANSWER_STRING_1, strlen(WEBSOCKET_ANSWER_STRING_1));
480 conn_mod_write(conn, resp, strlen(resp));
481 conn_mod_write(conn, WEBSOCKET_ANSWER_STRING_2, strlen(WEBSOCKET_ANSWER_STRING_2));
482
483 rb_free(resp);
1c8c63cb 484 }
1160f6c9
AC
485
486 conn_mod_write_sendq(conn->mod_fd, conn);
1c8c63cb
AC
487}
488
caebeeca 489static void
f297042b 490conn_mod_read_cb(rb_fde_t *fd, void *data)
05e0aa9a
AC
491{
492 char inbuf[READBUF_SIZE];
493 conn_t *conn = data;
494 int length = 0;
495 if (conn == NULL)
496 return;
497
498 if (IsDead(conn))
499 return;
500
501 while (1)
502 {
503 if (IsDead(conn))
504 return;
505
1c8c63cb
AC
506 length = rb_read(fd, inbuf, sizeof(inbuf));
507
508 if (length < 0)
509 {
510 if (rb_ignore_errno(errno))
f297042b 511 rb_setselect(fd, RB_SELECT_READ, conn_mod_read_cb, conn);
1c8c63cb
AC
512 else
513 close_conn(conn, NO_WAIT, "Connection closed");
514
515 return;
516 }
517 else if (length == 0)
05e0aa9a
AC
518 {
519 close_conn(conn, NO_WAIT, "Connection closed");
520 return;
521 }
1c8c63cb 522
1160f6c9 523 rb_rawbuf_append(conn->modbuf_in, inbuf, length);
f297042b
AC
524 if (!IsKeyed(conn))
525 conn_mod_handshake_process(conn);
1c8c63cb
AC
526
527 if (length < sizeof(inbuf))
528 {
f297042b 529 rb_setselect(fd, RB_SELECT_READ, conn_mod_read_cb, conn);
1c8c63cb
AC
530 return;
531 }
05e0aa9a
AC
532 }
533}
534
f297042b
AC
535static bool
536plain_check_cork(conn_t * conn)
537{
538 if(rb_rawbuf_length(conn->modbuf_out) >= 4096)
539 {
540 /* if we have over 4k pending outbound, don't read until
541 * we've cleared the queue */
542 SetCork(conn);
543 rb_setselect(conn->plain_fd, RB_SELECT_READ, NULL, NULL);
544 /* try to write */
545 conn_mod_write_sendq(conn->mod_fd, conn);
546 return true;
547 }
548
549 return false;
550}
551
05e0aa9a 552static void
f297042b 553conn_plain_process_recvq(conn_t *conn)
05e0aa9a 554{
f297042b
AC
555 char inbuf[READBUF_SIZE];
556
557 while (1)
558 {
559 size_t dolen = rb_linebuf_get(&conn->plainbuf_in, inbuf, sizeof inbuf, LINEBUF_COMPLETE, LINEBUF_PARSED);
560 if (!dolen)
561 break;
562
563 conn_mod_write_frame(conn, inbuf, dolen);
564 }
05e0aa9a
AC
565}
566
567static void
568conn_plain_read_cb(rb_fde_t *fd, void *data)
569{
f297042b
AC
570 char inbuf[READBUF_SIZE];
571 conn_t *conn = data;
572 int length = 0;
573 if(conn == NULL)
574 return;
575
576 if(IsDead(conn))
577 return;
578
579 if(plain_check_cork(conn))
580 return;
581
582 while(1)
583 {
584 if(IsDead(conn))
585 return;
586
587 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
588
589 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
590 {
591 close_conn(conn, NO_WAIT, NULL);
592 return;
593 }
594
595 if(length < 0)
596 {
597 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_cb, conn);
598 conn_plain_process_recvq(conn);
599 return;
600 }
601 conn->plain_in += length;
602
603 (void) rb_linebuf_parse(&conn->plainbuf_in, inbuf, sizeof(inbuf), 0);
604
605 if(IsDead(conn))
606 return;
607 if(plain_check_cork(conn))
608 return;
609 }
05e0aa9a
AC
610}
611
612static void
613conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data)
614{
615 char inbuf[READBUF_SIZE];
616 conn_t *conn = data;
617 int length = 0;
618
619 if(conn == NULL)
620 return;
621
622 while(1)
623 {
624 length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
625
626 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
627 {
628 rb_close(conn->plain_fd);
629 rb_dlinkAdd(conn, &conn->node, &dead_list);
630 return;
631 }
632
633 if(length < 0)
634 {
635 rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
636 return;
637 }
638 }
639}
640
641static void
642wsock_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
caebeeca
AC
643{
644 conn_t *conn;
645 uint32_t id;
646
647 conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
648
649 id = buf_to_uint32(&ctlb->buf[1]);
650 conn_add_id_hash(conn, id);
651 SetWS(conn);
652
653 if(rb_get_type(conn->mod_fd) & RB_FD_UNKNOWN)
654 rb_set_type(conn->mod_fd, RB_FD_SOCKET);
655
656 if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
657 rb_set_type(conn->plain_fd, RB_FD_SOCKET);
658
f297042b 659 conn_mod_read_cb(conn->mod_fd, conn);
caebeeca
AC
660}
661
662static void
663mod_process_cmd_recv(mod_ctl_t * ctl)
664{
665 rb_dlink_node *ptr, *next;
666 mod_ctl_buf_t *ctl_buf;
667
668 RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
669 {
670 ctl_buf = ptr->data;
671
672 switch (*ctl_buf->buf)
673 {
674 case 'A':
675 {
676 if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
677 {
678 cleanup_bad_message(ctl, ctl_buf);
679 break;
680 }
05e0aa9a 681 wsock_process(ctl, ctl_buf);
caebeeca
AC
682 break;
683 }
684 default:
685 break;
686 /* Log unknown commands */
687 }
688 rb_dlinkDelete(ptr, &ctl->readq);
689 rb_free(ctl_buf->buf);
690 rb_free(ctl_buf);
691 }
692
693}
694
695static void
696mod_read_ctl(rb_fde_t *F, void *data)
697{
698 mod_ctl_buf_t *ctl_buf;
699 mod_ctl_t *ctl = data;
700 int retlen;
701 int i;
702
703 do
704 {
705 ctl_buf = rb_malloc(sizeof(mod_ctl_buf_t));
706 ctl_buf->buf = rb_malloc(READBUF_SIZE);
707 ctl_buf->buflen = READBUF_SIZE;
708 retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, ctl_buf->buflen, ctl_buf->F,
709 MAXPASSFD);
710 if(retlen <= 0)
711 {
712 rb_free(ctl_buf->buf);
713 rb_free(ctl_buf);
714 }
715 else
716 {
717 ctl_buf->buflen = retlen;
718 rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
719 for (i = 0; i < MAXPASSFD && ctl_buf->F[i] != NULL; i++)
720 ;
721 ctl_buf->nfds = i;
722 }
723 }
724 while(retlen > 0);
725
726 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
727 exit(0);
728
729 mod_process_cmd_recv(ctl);
730 rb_setselect(ctl->F, RB_SELECT_READ, mod_read_ctl, ctl);
731}
732
caebeeca
AC
733static void
734read_pipe_ctl(rb_fde_t *F, void *data)
735{
736 char inbuf[READBUF_SIZE];
737 int retlen;
738 while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0)
739 {
740 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
741 }
742 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
743 exit(0);
744 rb_setselect(F, RB_SELECT_READ, read_pipe_ctl, NULL);
745}
746
747int
748main(int argc, char **argv)
749{
750 const char *s_ctlfd, *s_pipe, *s_pid;
751 int ctlfd, pipefd, x, maxfd;
752 maxfd = maxconn();
753
754 s_ctlfd = getenv("CTL_FD");
755 s_pipe = getenv("CTL_PIPE");
756 s_pid = getenv("CTL_PPID");
757
758 if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL)
759 {
760 fprintf(stderr,
761 "This is the charybdis wsockd for internal ircd use.\n");
762 fprintf(stderr,
763 "You aren't supposed to run me directly. Exiting.\n");
764 exit(1);
765 }
766
767 ctlfd = atoi(s_ctlfd);
768 pipefd = atoi(s_pipe);
769 ppid = atoi(s_pid);
770 x = 0;
771#ifndef _WIN32
772 for(x = 0; x < maxfd; x++)
773 {
774 if(x != ctlfd && x != pipefd && x > 2)
775 close(x);
776 }
777 x = open("/dev/null", O_RDWR);
778
779 if(x >= 0)
780 {
781 if(ctlfd != 0 && pipefd != 0)
782 dup2(x, 0);
783 if(ctlfd != 1 && pipefd != 1)
784 dup2(x, 1);
785 if(ctlfd != 2 && pipefd != 2)
786 dup2(x, 2);
787 if(x > 2)
788 close(x);
789 }
790#endif
791 setup_signals();
792 rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096);
1c8c63cb 793 rb_linebuf_init(4096);
1160f6c9 794 rb_init_rawbuffers(4096);
caebeeca
AC
795
796 mod_ctl = rb_malloc(sizeof(mod_ctl_t));
797 mod_ctl->F = rb_open(ctlfd, RB_FD_SOCKET, "ircd control socket");
798 mod_ctl->F_pipe = rb_open(pipefd, RB_FD_PIPE, "ircd pipe");
799 rb_set_nb(mod_ctl->F);
800 rb_set_nb(mod_ctl->F_pipe);
801 rb_event_addish("clean_dead_conns", clean_dead_conns, NULL, 10);
802 read_pipe_ctl(mod_ctl->F_pipe, NULL);
803 mod_read_ctl(mod_ctl->F, mod_ctl);
804
805 rb_lib_loop(0);
806 return 0;
807}