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