]> jfr.im git - solanum.git/blame - ircd/listener.c
Add new sockaddr_storage port retrieval/setting macros
[solanum.git] / ircd / listener.c
CommitLineData
54ac8b60
VY
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * listener.c: Listens on a port.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
54ac8b60
VY
23 */
24
25#include "stdinc.h"
26#include "setup.h"
27#include "listener.h"
28#include "client.h"
4562c604 29#include "match.h"
54ac8b60
VY
30#include "ircd.h"
31#include "ircd_defs.h"
32#include "numeric.h"
33#include "s_conf.h"
34#include "s_newconf.h"
35#include "s_stats.h"
36#include "send.h"
d3f6b808 37#include "authd.h"
54ac8b60
VY
38#include "reject.h"
39#include "s_conf.h"
40#include "hostmask.h"
c6d72037
VY
41#include "sslproc.h"
42#include "hash.h"
77d3d2db
KB
43#include "s_assert.h"
44#include "logger.h"
54ac8b60
VY
45
46#ifndef INADDR_NONE
47#define INADDR_NONE ((unsigned int) 0xffffffff)
48#endif
49
9ea3ea10 50#if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
54ac8b60
VY
51static const struct in6_addr in6addr_any =
52{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
55abcbb2 53#endif
54ac8b60 54
8f103562 55static struct Listener *ListenerPollList = NULL;
1087485c 56static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
8e09c4a2 57static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
54ac8b60 58
8f103562 59static struct Listener *
e7046ee5 60make_listener(struct rb_sockaddr_storage *addr)
54ac8b60 61{
8f103562 62 struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
54ac8b60 63 s_assert(0 != listener);
54ac8b60 64 listener->name = me.name;
f691939a
VY
65 listener->F = NULL;
66
e7046ee5 67 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
54ac8b60
VY
68 listener->next = NULL;
69 return listener;
70}
71
72void
8f103562 73free_listener(struct Listener *listener)
54ac8b60
VY
74{
75 s_assert(NULL != listener);
76 if(listener == NULL)
77 return;
78 /*
79 * remove from listener list
80 */
81 if(listener == ListenerPollList)
82 ListenerPollList = listener->next;
83 else
84 {
8f103562 85 struct Listener *prev = ListenerPollList;
54ac8b60
VY
86 for (; prev; prev = prev->next)
87 {
88 if(listener == prev->next)
89 {
90 prev->next = listener->next;
91 break;
92 }
93 }
94 }
95
96 /* free */
97 rb_free(listener);
98}
99
100#define PORTNAMELEN 6 /* ":31337" */
101
40173bcb
JT
102/*
103 * get_listener_port - return displayable listener port
104 */
105static uint16_t
106get_listener_port(const struct Listener *listener)
107{
d86692fa 108 return ntohs(GET_SS_PORT(&listener->addr));
40173bcb
JT
109}
110
54ac8b60
VY
111/*
112 * get_listener_name - return displayable listener name and port
113 * returns "host.foo.org:6667" for a given listener
114 */
115const char *
8f103562 116get_listener_name(const struct Listener *listener)
54ac8b60
VY
117{
118 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
54ac8b60
VY
119
120 s_assert(NULL != listener);
121 if(listener == NULL)
122 return NULL;
123
5203cba5 124 snprintf(buf, sizeof(buf), "%s[%s/%u]",
40173bcb 125 me.name, listener->name, get_listener_port(listener));
54ac8b60
VY
126 return buf;
127}
128
129/*
130 * show_ports - send port listing to a client
131 * inputs - pointer to client to show ports to
132 * output - none
133 * side effects - show ports
134 */
135void
136show_ports(struct Client *source_p)
137{
8f103562 138 struct Listener *listener = 0;
54ac8b60
VY
139
140 for (listener = ListenerPollList; listener; listener = listener->next)
141 {
55abcbb2 142 sendto_one_numeric(source_p, RPL_STATSPLINE,
40173bcb
JT
143 form_str(RPL_STATSPLINE), 'P',
144 get_listener_port(listener),
54ac8b60 145 IsOperAdmin(source_p) ? listener->name : me.name,
8bd5767b 146 listener->ref_count, (listener->active) ? "active" : "disabled",
c6d72037 147 listener->ssl ? " ssl" : "");
54ac8b60
VY
148 }
149}
150
151/*
152 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
153 * bind it to the port given in 'port' and listen to it
154 * returns true (1) if successful false (0) on error.
155 *
156 * If the operating system has a define for SOMAXCONN, use it, otherwise
638d2862 157 * use CHARYBDIS_SOMAXCONN
54ac8b60
VY
158 */
159#ifdef SOMAXCONN
638d2862
EM
160#undef CHARYBDIS_SOMAXCONN
161#define CHARYBDIS_SOMAXCONN SOMAXCONN
54ac8b60
VY
162#endif
163
164static int
8f103562 165inetport(struct Listener *listener)
54ac8b60 166{
f691939a 167 rb_fde_t *F;
54ac8b60 168 int opt = 1;
40173bcb 169 const char *errstr;
54ac8b60
VY
170
171 /*
172 * At first, open a new socket
173 */
55abcbb2 174
f691939a 175 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
54ac8b60 176
9ea3ea10 177#ifdef RB_IPV6
e867208d 178 if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
54ac8b60
VY
179 {
180 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
181 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
182 {
caa4d9d2 183 rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60
VY
184 listener->name = listener->vhost;
185 }
186 } else
187#endif
188 {
189 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
190 if(in->sin_addr.s_addr != INADDR_ANY)
191 {
caa4d9d2 192 rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
54ac8b60 193 listener->name = listener->vhost;
55abcbb2 194 }
54ac8b60
VY
195 }
196
1087485c
JT
197 if(F == NULL)
198 {
40173bcb
JT
199 sendto_realops_snomask(SNO_GENERAL, L_ALL,
200 "Cannot open socket for listener on port %d",
201 get_listener_port(listener));
202 ilog(L_MAIN, "Cannot open socket for listener %s",
203 get_listener_name(listener));
1087485c
JT
204 return 0;
205 }
206 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
207 {
825ddf13 208 ilog_error("no more connections left for listener");
40173bcb
JT
209 sendto_realops_snomask(SNO_GENERAL, L_ALL,
210 "No more connections left for listener on port %d",
211 get_listener_port(listener));
212 ilog(L_MAIN, "No more connections left for listener %s",
213 get_listener_name(listener));
1087485c
JT
214 rb_close(F);
215 return 0;
54ac8b60
VY
216 }
217
1087485c
JT
218 /*
219 * XXX - we don't want to do all this crap for a listener
220 * set_sock_opts(listener);
bf3ecca2
EM
221 *
222 * FIXME - doesn't this belong in librb? --Elizafox
1087485c
JT
223 */
224 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
225 {
40173bcb
JT
226 errstr = strerror(rb_get_sockerr(F));
227 sendto_realops_snomask(SNO_GENERAL, L_ALL,
228 "Cannot set SO_REUSEADDR for listener on port %d: %s",
229 get_listener_port(listener), errstr);
230 ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s",
231 get_listener_name(listener), errstr);
1087485c
JT
232 rb_close(F);
233 return 0;
54ac8b60
VY
234 }
235
bf3ecca2 236 /* FIXME - doesn't this belong in librb? --Elizafox */
1087485c
JT
237 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
238 {
40173bcb
JT
239 errstr = strerror(rb_get_sockerr(F));
240 sendto_realops_snomask(SNO_GENERAL, L_ALL,
241 "Cannot bind for listener on port %d: %s",
242 get_listener_port(listener), errstr);
243 ilog(L_MAIN, "Cannot bind for listener %s: %s",
244 get_listener_name(listener), errstr);
1087485c
JT
245 rb_close(F);
246 return 0;
54ac8b60
VY
247 }
248
638d2862 249 if(rb_listen(F, CHARYBDIS_SOMAXCONN, listener->defer_accept))
1087485c 250 {
40173bcb
JT
251 errstr = strerror(rb_get_sockerr(F));
252 sendto_realops_snomask(SNO_GENERAL, L_ALL,
253 "Cannot listen() for listener on port %d: %s",
254 get_listener_port(listener), errstr);
255 ilog(L_MAIN, "Cannot listen() for listener %s: %s",
256 get_listener_name(listener), errstr);
1087485c
JT
257 rb_close(F);
258 return 0;
54ac8b60
VY
259 }
260
1087485c
JT
261 listener->F = F;
262
f691939a 263 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
54ac8b60
VY
264 return 1;
265}
266
8f103562 267static struct Listener *
e7046ee5 268find_listener(struct rb_sockaddr_storage *addr)
54ac8b60 269{
8f103562
JT
270 struct Listener *listener = NULL;
271 struct Listener *last_closed = NULL;
54ac8b60
VY
272
273 for (listener = ListenerPollList; listener; listener = listener->next)
274 {
e867208d 275 if(GET_SS_FAMILY(addr) != GET_SS_FAMILY(&listener->addr))
54ac8b60 276 continue;
55abcbb2 277
a7fb2693 278 switch(GET_SS_FAMILY(addr))
54ac8b60
VY
279 {
280 case AF_INET:
281 {
282 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
283 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
55abcbb2 284 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
54ac8b60
VY
285 in4->sin_port == lin4->sin_port )
286 {
1087485c
JT
287 if(listener->F == NULL)
288 last_closed = listener;
289 else
54ac8b60
VY
290 return(listener);
291 }
292 break;
293 }
9ea3ea10 294#ifdef RB_IPV6
54ac8b60
VY
295 case AF_INET6:
296 {
297 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
298 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
299 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
300 in6->sin6_port == lin6->sin6_port)
301 {
1087485c
JT
302 if(listener->F == NULL)
303 last_closed = listener;
304 else
54ac8b60
VY
305 return(listener);
306 }
307 break;
55abcbb2 308
54ac8b60
VY
309 }
310#endif
311
312 default:
313 break;
314 }
315 }
316 return last_closed;
317}
318
319
320/*
321 * add_listener- create a new listener
322 * port - the port number to listen on
323 * vhost_ip - if non-null must contain a valid IP address string in
324 * the format "255.255.255.255"
325 */
326void
02270e96 327add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
54ac8b60 328{
8f103562 329 struct Listener *listener;
e7046ee5 330 struct rb_sockaddr_storage vaddr;
54ac8b60
VY
331
332 /*
333 * if no port in conf line, don't bother
334 */
335 if(port == 0)
336 return;
337 memset(&vaddr, 0, sizeof(vaddr));
e867208d 338 SET_SS_FAMILY(&vaddr, family);
54ac8b60
VY
339
340 if(vhost_ip != NULL)
341 {
342 if(family == AF_INET)
343 {
caa4d9d2 344 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
54ac8b60 345 return;
55abcbb2 346 }
9ea3ea10 347#ifdef RB_IPV6
54ac8b60
VY
348 else
349 {
caa4d9d2 350 if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
54ac8b60 351 return;
55abcbb2 352
54ac8b60
VY
353 }
354#endif
355 } else
356 {
357 switch(family)
358 {
359 case AF_INET:
360 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
361 break;
9ea3ea10 362#ifdef RB_IPV6
54ac8b60
VY
363 case AF_INET6:
364 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
365 break;
366 default:
367 return;
368#endif
55abcbb2 369 }
54ac8b60
VY
370 }
371 switch(family)
372 {
373 case AF_INET:
99c4835f 374 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
d86692fa
EM
375 SET_SS_FAMILY(&vaddr, AF_INET);
376 SET_SS_PORT(&vaddr, htons(port));
54ac8b60 377 break;
9ea3ea10 378#ifdef RB_IPV6
54ac8b60 379 case AF_INET6:
99c4835f 380 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
d86692fa
EM
381 SET_SS_FAMILY(&vaddr, AF_INET6);
382 SET_SS_PORT(&vaddr, htons(port));
54ac8b60
VY
383 break;
384#endif
385 default:
386 break;
387 }
388 if((listener = find_listener(&vaddr)))
389 {
1087485c 390 if(listener->F != NULL)
54ac8b60
VY
391 return;
392 }
393 else
394 {
395 listener = make_listener(&vaddr);
396 listener->next = ListenerPollList;
397 ListenerPollList = listener;
398 }
399
f691939a 400 listener->F = NULL;
c6d72037 401 listener->ssl = ssl;
02270e96 402 listener->defer_accept = defer_accept;
54ac8b60
VY
403
404 if(inetport(listener))
405 listener->active = 1;
406 else
407 close_listener(listener);
408}
409
410/*
411 * close_listener - close a single listener
412 */
413void
8f103562 414close_listener(struct Listener *listener)
54ac8b60
VY
415{
416 s_assert(listener != NULL);
417 if(listener == NULL)
418 return;
1087485c
JT
419 if(listener->F != NULL)
420 {
421 rb_close(listener->F);
422 listener->F = NULL;
54ac8b60
VY
423 }
424
425 listener->active = 0;
426
427 if(listener->ref_count)
428 return;
429
430 free_listener(listener);
431}
432
433/*
434 * close_listeners - close and free all listeners that are not being used
435 */
436void
437close_listeners()
438{
8f103562
JT
439 struct Listener *listener;
440 struct Listener *listener_next = 0;
54ac8b60
VY
441 /*
442 * close all 'extra' listening ports we have
443 */
444 for (listener = ListenerPollList; listener; listener = listener_next)
445 {
446 listener_next = listener->next;
447 close_listener(listener);
448 }
449}
450
451#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
452
453/*
55abcbb2 454 * add_connection - creates a client which has just connected to us on
54ac8b60
VY
455 * the given fd. The sockhost field is initialized with the ip# of the host.
456 * The client is sent to the auth module for verification, and not put in
457 * any client list yet.
458 */
1087485c 459static void
b5b4a0e7 460add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai)
54ac8b60
VY
461{
462 struct Client *new_client;
463 s_assert(NULL != listener);
464
55abcbb2 465 /*
54ac8b60
VY
466 * get the client socket name from the socket
467 * the client has already been checked out in accept_connection
468 */
469 new_client = make_client(NULL);
470
b5b4a0e7
AC
471 if (listener->ssl)
472 {
473 rb_fde_t *xF[2];
474 if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
475 {
476 free_client(new_client);
477 return;
478 }
de7cf7e0 479 new_client->localClient->ssl_ctl = start_ssld_accept(F, xF[1], connid_get(new_client)); /* this will close F for us */
b5b4a0e7
AC
480 if(new_client->localClient->ssl_ctl == NULL)
481 {
482 free_client(new_client);
483 return;
484 }
485 F = xF[0];
486 SetSSL(new_client);
487 }
488
e7046ee5 489 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
3540120a 490 memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
54ac8b60 491
55abcbb2 492 /*
54ac8b60
VY
493 * copy address to 'sockhost' as a string, copy it to host too
494 * so we have something valid to put into error messages...
495 */
55abcbb2 496 rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
54ac8b60
VY
497 sizeof(new_client->sockhost));
498
499
f427c8b0 500 rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
54ac8b60 501
99c4835f 502 new_client->localClient->F = F;
54ac8b60 503 new_client->localClient->listener = listener;
c6d72037 504
54ac8b60
VY
505 ++listener->ref_count;
506
d3f6b808 507 authd_initiate_client(new_client);
54ac8b60 508}
0d89d5cd 509
43946961
JT
510static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
511
1087485c
JT
512static int
513accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
514{
515 struct Listener *listener = (struct Listener *)data;
516 char buf[BUFSIZE];
517 struct ConfItem *aconf;
518 static time_t last_oper_notice = 0;
b52c2949 519 int len;
1087485c 520
bfc44622 521 if(listener->ssl && (!ircd_ssl_ok || !get_ssld_count()))
8bd5767b
JT
522 {
523 rb_close(F);
524 return 0;
c6d72037
VY
525 }
526
1087485c
JT
527 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
528 {
47adde3d 529 ++ServerStats.is_ref;
1087485c
JT
530 /*
531 * slow down the whining to opers bit
532 */
533 if((last_oper_notice + 20) <= rb_current_time())
534 {
26716d6d 535 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1087485c
JT
536 "All connections in use. (%s)",
537 get_listener_name(listener));
538 last_oper_notice = rb_current_time();
539 }
55abcbb2 540
76d82c19 541 rb_write(F, "ERROR :All connections in use\r\n", 31);
1087485c 542 rb_close(F);
1087485c
JT
543 return 0;
544 }
545
41d7fefa 546 aconf = find_dline(addr, addr->sa_family);
1087485c
JT
547 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
548 return 1;
55abcbb2 549
1087485c
JT
550 /* Do an initial check we aren't connecting too fast or with too many
551 * from this IP... */
552 if(aconf != NULL)
553 {
47adde3d 554 ServerStats.is_ref++;
55abcbb2 555
1087485c
JT
556 if(ConfigFileEntry.dline_with_reason)
557 {
5203cba5 558 len = snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
b52c2949 559 if (len >= (int)(sizeof(buf)-1))
1087485c
JT
560 {
561 buf[sizeof(buf) - 3] = '\r';
562 buf[sizeof(buf) - 2] = '\n';
563 buf[sizeof(buf) - 1] = '\0';
564 }
565 }
566 else
567 strcpy(buf, "ERROR :You have been D-lined.\r\n");
55abcbb2 568
1087485c
JT
569 rb_write(F, buf, strlen(buf));
570 rb_close(F);
571 return 0;
572 }
573
43946961
JT
574 if(check_reject(F, addr))
575 return 0;
55abcbb2 576
43946961
JT
577 if(throttle_add(addr))
578 {
579 rb_write(F, toofast, strlen(toofast));
580 rb_close(F);
581 return 0;
582 }
583
1087485c
JT
584 return 1;
585}
586
587static void
588accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
589{
590 struct Listener *listener = data;
591 struct rb_sockaddr_storage lip;
592 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
55abcbb2 593
47adde3d 594 ServerStats.is_ac++;
1087485c
JT
595
596 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
597 {
d60a42a2 598 /* this can fail if the connection disappeared in the meantime */
1087485c 599 rb_close(F);
9692f954 600 return;
1087485c 601 }
55abcbb2 602
b5b4a0e7 603 add_connection(listener, F, addr, (struct sockaddr *)&lip);
0d89d5cd 604}