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>
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.
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.
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
27 #define READBUF_SIZE 16384
30 static void setup_signals(void);
33 static inline uint32_t
34 buf_to_uint32(uint8_t *buf
)
37 memcpy(&x
, buf
, sizeof(x
));
42 uint32_to_buf(uint8_t *buf
, uint32_t x
)
44 memcpy(buf
, &x
, sizeof(x
));
48 typedef struct _mod_ctl_buf
53 rb_fde_t
*F
[MAXPASSFD
];
57 typedef struct _mod_ctl
67 static mod_ctl_t
*mod_ctl
;
73 rawbuf_head_t
*modbuf_out
;
74 rawbuf_head_t
*plainbuf_out
;
88 #define FLAG_CORK 0x01
89 #define FLAG_DEAD 0x02
90 #define FLAG_WSOCK 0x04
92 #define IsCork(x) ((x)->flags & FLAG_CORK)
93 #define IsDead(x) ((x)->flags & FLAG_DEAD)
94 #define IsWS(x) ((x)->flags & FLAG_WSOCK)
96 #define SetCork(x) ((x)->flags |= FLAG_CORK)
97 #define SetDead(x) ((x)->flags |= FLAG_DEAD)
98 #define SetWS(x) ((x)->flags |= FLAG_WSOCK)
100 #define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
101 #define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
102 #define ClearWS(x) ((x)->flags &= ~FLAG_WSOCK)
105 #define WAIT_PLAIN 0x1
107 #define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
108 #define HASH_WALK_END }
109 #define CONN_HASH_SIZE 2000
110 #define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
112 static rb_dlink_list connid_hash_table
[CONN_HASH_SIZE
];
113 static rb_dlink_list dead_list
;
117 dummy_handler(int sig
)
127 struct sigaction act
;
130 act
.sa_handler
= SIG_IGN
;
131 sigemptyset(&act
.sa_mask
);
132 sigaddset(&act
.sa_mask
, SIGPIPE
);
133 sigaddset(&act
.sa_mask
, SIGALRM
);
135 sigaddset(&act
.sa_mask
, SIGTRAP
);
139 sigaddset(&act
.sa_mask
, SIGWINCH
);
140 sigaction(SIGWINCH
, &act
, 0);
142 sigaction(SIGPIPE
, &act
, 0);
144 sigaction(SIGTRAP
, &act
, 0);
147 act
.sa_handler
= dummy_handler
;
148 sigaction(SIGALRM
, &act
, 0);
155 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
158 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
160 return limit
.rlim_cur
;
162 #endif /* RLIMIT_FD_MAX */
163 return MAXCONNECTIONS
;
167 conn_find_by_id(uint32_t id
)
172 RB_DLINK_FOREACH(ptr
, (connid_hash(id
))->head
)
175 if(conn
->id
== id
&& !IsDead(conn
))
182 conn_add_id_hash(conn_t
* conn
, uint32_t id
)
185 rb_dlinkAdd(conn
, &conn
->node
, connid_hash(id
));
189 free_conn(conn_t
* conn
)
191 rb_free_rawbuffer(conn
->modbuf_out
);
192 rb_free_rawbuffer(conn
->plainbuf_out
);
197 clean_dead_conns(void *unused
)
200 rb_dlink_node
*ptr
, *next
;
202 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
208 dead_list
.tail
= dead_list
.head
= NULL
;
212 make_conn(mod_ctl_t
* ctl
, rb_fde_t
*mod_fd
, rb_fde_t
*plain_fd
)
214 conn_t
*conn
= rb_malloc(sizeof(conn_t
));
216 conn
->modbuf_out
= rb_new_rawbuffer();
217 conn
->plainbuf_out
= rb_new_rawbuffer();
218 conn
->mod_fd
= mod_fd
;
219 conn
->plain_fd
= plain_fd
;
228 cleanup_bad_message(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
232 /* XXX should log this somehow */
233 for (i
= 0; i
< ctlb
->nfds
; i
++)
234 rb_close(ctlb
->F
[i
]);
238 wsock_process_accept(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
243 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
245 id
= buf_to_uint32(&ctlb
->buf
[1]);
246 conn_add_id_hash(conn
, id
);
249 if(rb_get_type(conn
->mod_fd
) & RB_FD_UNKNOWN
)
250 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
252 if(rb_get_type(conn
->plain_fd
) == RB_FD_UNKNOWN
)
253 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
259 mod_process_cmd_recv(mod_ctl_t
* ctl
)
261 rb_dlink_node
*ptr
, *next
;
262 mod_ctl_buf_t
*ctl_buf
;
264 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
268 switch (*ctl_buf
->buf
)
272 if (ctl_buf
->nfds
!= 2 || ctl_buf
->buflen
!= 5)
274 cleanup_bad_message(ctl
, ctl_buf
);
277 wsock_process_accept(ctl
, ctl_buf
);
282 /* Log unknown commands */
284 rb_dlinkDelete(ptr
, &ctl
->readq
);
285 rb_free(ctl_buf
->buf
);
292 mod_read_ctl(rb_fde_t
*F
, void *data
)
294 mod_ctl_buf_t
*ctl_buf
;
295 mod_ctl_t
*ctl
= data
;
301 ctl_buf
= rb_malloc(sizeof(mod_ctl_buf_t
));
302 ctl_buf
->buf
= rb_malloc(READBUF_SIZE
);
303 ctl_buf
->buflen
= READBUF_SIZE
;
304 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl_buf
->F
,
308 rb_free(ctl_buf
->buf
);
313 ctl_buf
->buflen
= retlen
;
314 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
315 for (i
= 0; i
< MAXPASSFD
&& ctl_buf
->F
[i
] != NULL
; i
++)
322 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
325 mod_process_cmd_recv(ctl
);
326 rb_setselect(ctl
->F
, RB_SELECT_READ
, mod_read_ctl
, ctl
);
330 mod_write_ctl(rb_fde_t
*F
, void *data
)
332 mod_ctl_t
*ctl
= data
;
333 mod_ctl_buf_t
*ctl_buf
;
334 rb_dlink_node
*ptr
, *next
;
337 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
340 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
,
341 ctl_buf
->buflen
, ppid
);
344 rb_dlinkDelete(ptr
, &ctl
->writeq
);
345 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
346 rb_close(ctl_buf
->F
[x
]);
347 rb_free(ctl_buf
->buf
);
351 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
355 if(rb_dlink_list_length(&ctl
->writeq
) > 0)
356 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, mod_write_ctl
, ctl
);
360 read_pipe_ctl(rb_fde_t
*F
, void *data
)
362 char inbuf
[READBUF_SIZE
];
364 while((retlen
= rb_read(F
, inbuf
, sizeof(inbuf
))) > 0)
366 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
368 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
370 rb_setselect(F
, RB_SELECT_READ
, read_pipe_ctl
, NULL
);
374 main(int argc
, char **argv
)
376 const char *s_ctlfd
, *s_pipe
, *s_pid
;
377 int ctlfd
, pipefd
, x
, maxfd
;
380 s_ctlfd
= getenv("CTL_FD");
381 s_pipe
= getenv("CTL_PIPE");
382 s_pid
= getenv("CTL_PPID");
384 if(s_ctlfd
== NULL
|| s_pipe
== NULL
|| s_pid
== NULL
)
387 "This is the charybdis wsockd for internal ircd use.\n");
389 "You aren't supposed to run me directly. Exiting.\n");
393 ctlfd
= atoi(s_ctlfd
);
394 pipefd
= atoi(s_pipe
);
398 for(x
= 0; x
< maxfd
; x
++)
400 if(x
!= ctlfd
&& x
!= pipefd
&& x
> 2)
403 x
= open("/dev/null", O_RDWR
);
407 if(ctlfd
!= 0 && pipefd
!= 0)
409 if(ctlfd
!= 1 && pipefd
!= 1)
411 if(ctlfd
!= 2 && pipefd
!= 2)
418 rb_lib_init(NULL
, NULL
, NULL
, 0, maxfd
, 1024, 4096);
419 rb_init_rawbuffers(1024);
421 mod_ctl
= rb_malloc(sizeof(mod_ctl_t
));
422 mod_ctl
->F
= rb_open(ctlfd
, RB_FD_SOCKET
, "ircd control socket");
423 mod_ctl
->F_pipe
= rb_open(pipefd
, RB_FD_PIPE
, "ircd pipe");
424 rb_set_nb(mod_ctl
->F
);
425 rb_set_nb(mod_ctl
->F_pipe
);
426 rb_event_addish("clean_dead_conns", clean_dead_conns
, NULL
, 10);
427 read_pipe_ctl(mod_ctl
->F_pipe
, NULL
);
428 mod_read_ctl(mod_ctl
->F
, mod_ctl
);