]> jfr.im git - irc/rqf/shadowircd.git/blame - src/listener.c
[svn] - fix a bug here
[irc/rqf/shadowircd.git] / src / listener.c
CommitLineData
212380e3 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 *
6fcb8629 24 * $Id: listener.c 3354 2007-04-03 09:21:31Z nenolod $
212380e3 25 */
26
27#include "stdinc.h"
28#include "setup.h"
29#include "listener.h"
30#include "client.h"
31#include "irc_string.h"
32#include "sprintf_irc.h"
33#include "ircd.h"
34#include "ircd_defs.h"
35#include "numeric.h"
36#include "commio.h"
37#include "s_conf.h"
38#include "s_newconf.h"
39#include "s_stats.h"
40#include "send.h"
41#include "memory.h"
42#include "s_auth.h"
43#include "reject.h"
44
45#ifndef INADDR_NONE
46#define INADDR_NONE ((unsigned int) 0xffffffff)
47#endif
48
49#if defined(NO_IN6ADDR_ANY) && defined(IPV6)
50static const struct in6_addr in6addr_any =
51{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
52#endif
53
54static PF accept_connection;
55
56static listener_t *ListenerPollList = NULL;
57
58static listener_t *
59make_listener(struct irc_sockaddr_storage *addr)
60{
61 listener_t *listener = (listener_t *) MyMalloc(sizeof(listener_t));
62 s_assert(0 != listener);
63
64 listener->name = me.name;
65 listener->fd = -1;
66 memcpy(&listener->addr, addr, sizeof(struct irc_sockaddr_storage));
67 listener->next = NULL;
68 return listener;
69}
70
71void
72free_listener(listener_t *listener)
73{
74 s_assert(NULL != listener);
75 if(listener == NULL)
76 return;
77 /*
78 * remove from listener list
79 */
80 if(listener == ListenerPollList)
81 ListenerPollList = listener->next;
82 else
83 {
84 listener_t *prev = ListenerPollList;
85 for (; prev; prev = prev->next)
86 {
87 if(listener == prev->next)
88 {
89 prev->next = listener->next;
90 break;
91 }
92 }
93 }
94
95 /* free */
96 MyFree(listener);
97}
98
99#define PORTNAMELEN 6 /* ":31337" */
100
101/*
102 * get_listener_name - return displayable listener name and port
103 * returns "host.foo.org:6667" for a given listener
104 */
105const char *
106get_listener_name(const listener_t *listener)
107{
108 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
109 int port = 0;
110
111 s_assert(NULL != listener);
112 if(listener == NULL)
113 return NULL;
114
115#ifdef IPV6
116 if(listener->addr.ss_family == AF_INET6)
117 port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
118 else
119#endif
120 port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
121
122 ircsnprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
123 return buf;
124}
125
126/*
127 * show_ports - send port listing to a client
128 * inputs - pointer to client to show ports to
129 * output - none
130 * side effects - show ports
131 */
132void
133show_ports(struct Client *source_p)
134{
135 listener_t *listener = 0;
136
137 for (listener = ListenerPollList; listener; listener = listener->next)
138 {
139 sendto_one_numeric(source_p, RPL_STATSPLINE,
140 form_str(RPL_STATSPLINE), 'P',
141#ifdef IPV6
142 ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
143 ((struct sockaddr_in6 *)&listener->addr)->sin6_port),
144#else
145 ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
146#endif
147 IsOperAdmin(source_p) ? listener->name : me.name,
148 listener->ref_count, (listener->active) ? "active" : "disabled");
149 }
150}
151
152/*
153 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
154 * bind it to the port given in 'port' and listen to it
155 * returns true (1) if successful false (0) on error.
156 *
157 * If the operating system has a define for SOMAXCONN, use it, otherwise
158 * use RATBOX_SOMAXCONN
159 */
160#ifdef SOMAXCONN
161#undef RATBOX_SOMAXCONN
162#define RATBOX_SOMAXCONN SOMAXCONN
163#endif
164
165static int
166inetport(listener_t *listener)
167{
168 int fd;
169 int opt = 1;
170
171 /*
172 * At first, open a new socket
173 */
174
175 fd = comm_socket(listener->addr.ss_family, SOCK_STREAM, 0, "Listener socket");
176
177#ifdef IPV6
178 if(listener->addr.ss_family == AF_INET6)
179 {
180 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
181 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
182 {
183 inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
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 {
192 inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
193 listener->name = listener->vhost;
194 }
195 }
196
6fcb8629 197 /*
198 * At one point, we enforced a strange arbitrary limit here.
199 * We no longer do this, and just check if the fd is valid or not.
200 * -nenolod
201 */
212380e3 202 if(fd == -1)
203 {
204 report_error("opening listener socket %s:%s",
205 get_listener_name(listener),
206 get_listener_name(listener), errno);
207 return 0;
208 }
6fcb8629 209
212380e3 210 /*
211 * XXX - we don't want to do all this crap for a listener
212 * set_sock_opts(listener);
213 */
214 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
215 {
216 report_error("setting SO_REUSEADDR for listener %s:%s",
217 get_listener_name(listener),
218 get_listener_name(listener), errno);
219 comm_close(fd);
220 return 0;
221 }
222
223 /*
224 * Bind a port to listen for new connections if port is non-null,
225 * else assume it is already open and try get something from it.
226 */
227
228 if(bind(fd, (struct sockaddr *) &listener->addr, GET_SS_LEN(listener->addr)))
229 {
230 report_error("binding listener socket %s:%s",
231 get_listener_name(listener),
232 get_listener_name(listener), errno);
233 comm_close(fd);
234 return 0;
235 }
236
237 if(listen(fd, RATBOX_SOMAXCONN))
238 {
239 report_error("listen failed for %s:%s",
240 get_listener_name(listener),
241 get_listener_name(listener), errno);
242 comm_close(fd);
243 return 0;
244 }
245
246 listener->fd = fd;
247
248 /* Listen completion events are READ events .. */
249
250 accept_connection(fd, listener);
251 return 1;
252}
253
254static listener_t *
255find_listener(struct irc_sockaddr_storage *addr)
256{
257 listener_t *listener = NULL;
258 listener_t *last_closed = NULL;
259
260 for (listener = ListenerPollList; listener; listener = listener->next)
261 {
262 if(addr->ss_family != listener->addr.ss_family)
263 continue;
264
265 switch(addr->ss_family)
266 {
267 case AF_INET:
268 {
269 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
270 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
271 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
272 in4->sin_port == lin4->sin_port )
273 {
274 if(listener->fd == -1)
275 last_closed = listener;
276 else
277 return(listener);
278 }
279 break;
280 }
281#ifdef IPV6
282 case AF_INET6:
283 {
284 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
285 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
286 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
287 in6->sin6_port == lin6->sin6_port)
288 {
289 if(listener->fd == -1)
290 last_closed = listener;
291 else
292 return(listener);
293 }
294 break;
295
296 }
297#endif
298
299 default:
300 break;
301 }
302 }
303 return last_closed;
304}
305
306
307/*
308 * add_listener- create a new listener
309 * port - the port number to listen on
310 * vhost_ip - if non-null must contain a valid IP address string in
311 * the format "255.255.255.255"
312 */
313void
314add_listener(int port, const char *vhost_ip, int family)
315{
316 listener_t *listener;
317 struct irc_sockaddr_storage vaddr;
318
319 /*
320 * if no port in conf line, don't bother
321 */
322 if(port == 0)
323 return;
324 memset(&vaddr, 0, sizeof(vaddr));
325 vaddr.ss_family = family;
326
327 if(vhost_ip != NULL)
328 {
329 if(family == AF_INET)
330 {
331 if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
332 return;
333 }
334#ifdef IPV6
335 else
336 {
337 if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
338 return;
339
340 }
341#endif
342 } else
343 {
344 switch(family)
345 {
346 case AF_INET:
347 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
348 break;
349#ifdef IPV6
350 case AF_INET6:
351 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
352 break;
353 default:
354 return;
355#endif
356 }
357 }
358 switch(family)
359 {
360 case AF_INET:
361 SET_SS_LEN(vaddr, sizeof(struct sockaddr_in));
362 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
363 break;
364#ifdef IPV6
365 case AF_INET6:
366 SET_SS_LEN(vaddr, sizeof(struct sockaddr_in6));
367 ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
368 break;
369#endif
370 default:
371 break;
372 }
373 if((listener = find_listener(&vaddr)))
374 {
375 if(listener->fd > -1)
376 return;
377 }
378 else
379 {
380 listener = make_listener(&vaddr);
381 listener->next = ListenerPollList;
382 ListenerPollList = listener;
383 }
384
385 listener->fd = -1;
386
387 if(inetport(listener))
388 listener->active = 1;
389 else
390 close_listener(listener);
391}
392
393/*
394 * close_listener - close a single listener
395 */
396void
397close_listener(listener_t *listener)
398{
399 s_assert(listener != NULL);
400 if(listener == NULL)
401 return;
402 if(listener->fd >= 0)
403 {
404 comm_close(listener->fd);
405 listener->fd = -1;
406 }
407
408 listener->active = 0;
409
410 if(listener->ref_count)
411 return;
412
413 free_listener(listener);
414}
415
416/*
417 * close_listeners - close and free all listeners that are not being used
418 */
419void
420close_listeners()
421{
422 listener_t *listener;
423 listener_t *listener_next = 0;
424 /*
425 * close all 'extra' listening ports we have
426 */
427 for (listener = ListenerPollList; listener; listener = listener_next)
428 {
429 listener_next = listener->next;
430 close_listener(listener);
431 }
432}
433
434#define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
435
436/*
437 * add_connection - creates a client which has just connected to us on
438 * the given fd. The sockhost field is initialized with the ip# of the host.
439 * The client is sent to the auth module for verification, and not put in
440 * any client list yet.
441 */
442static void
443add_connection(listener_t *listener, int fd, struct sockaddr *sai)
444{
445 struct Client *new_client;
446 s_assert(NULL != listener);
447
448 /*
449 * get the client socket name from the socket
450 * the client has already been checked out in accept_connection
451 */
452 new_client = make_client(NULL);
453
454 memcpy(&new_client->localClient->ip, sai, sizeof(struct irc_sockaddr_storage));
455
456 /*
457 * copy address to 'sockhost' as a string, copy it to host too
458 * so we have something valid to put into error messages...
459 */
460 inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
461 sizeof(new_client->sockhost));
462
463
464 strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
465
466#ifdef IPV6
467 if(new_client->localClient->ip.ss_family == AF_INET6 && ConfigFileEntry.dot_in_ip6_addr == 1)
468 {
469 strlcat(new_client->host, ".", sizeof(new_client->host));
470 }
471#endif
472
473 new_client->localClient->fd = fd;
474
475 new_client->localClient->listener = listener;
476 ++listener->ref_count;
477
478 if(check_reject(new_client))
479 return;
480 start_auth(new_client);
481}
482
483
484static void
485accept_connection(int pfd, void *data)
486{
487 static time_t last_oper_notice = 0;
212380e3 488 struct irc_sockaddr_storage sai;
489 socklen_t addrlen = sizeof(sai);
490 int fd;
491 listener_t *listener = data;
6fcb8629 492 struct ConfItem *aconf;
493 char buf[BUFSIZE];
494
212380e3 495 s_assert(listener != NULL);
496 if(listener == NULL)
497 return;
498 /*
499 * There may be many reasons for error return, but
500 * in otherwise correctly working environment the
501 * probable cause is running out of file descriptors
502 * (EMFILE, ENFILE or others?). The man pages for
503 * accept don't seem to list these as possible,
504 * although it's obvious that it may happen here.
505 * Thus no specific errors are tested at this
506 * point, just assume that connections cannot
507 * be accepted until some old is closed first.
508 */
509
510 fd = comm_accept(listener->fd, (struct sockaddr *)&sai, &addrlen);
511 if(fd < 0)
512 {
513 /* Re-register a new IO request for the next accept .. */
514 comm_setselect(listener->fd, FDLIST_SERVICE,
515 COMM_SELECT_READ, accept_connection, listener, 0);
516 return;
517 }
518
519 /* This needs to be done here, otherwise we break dlines */
520 mangle_mapped_sockaddr((struct sockaddr *)&sai);
521
522 /*
523 * check for connection limit
6fcb8629 524 * TBD: this is stupid... either we have a socket or we don't. -nenolod
212380e3 525 */
6fcb8629 526 if((comm_get_maxconnections() - 10) < fd)
212380e3 527 {
528 ++ServerStats->is_ref;
529 /*
530 * slow down the whining to opers bit
531 */
532 if((last_oper_notice + 20) <= CurrentTime)
533 {
534 sendto_realops_snomask(SNO_GENERAL, L_ALL,
535 "All connections in use. (%s)",
536 get_listener_name(listener));
537 last_oper_notice = CurrentTime;
538 }
539
540 write(fd, "ERROR :All connections in use\r\n", 32);
541 comm_close(fd);
542 /* Re-register a new IO request for the next accept .. */
543 comm_setselect(listener->fd, FDLIST_SERVICE,
544 COMM_SELECT_READ, accept_connection, listener, 0);
545 return;
546 }
547
548 /* Do an initial check we aren't connecting too fast or with too many
549 * from this IP... */
550 if((aconf = conf_connect_allowed((struct sockaddr *)&sai, sai.ss_family)) != NULL)
551 {
552 ServerStats->is_ref++;
553
6fcb8629 554 if(ConfigFileEntry.dline_with_reason)
555 {
556 if (ircsnprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (sizeof(buf)-1))
557 {
558 buf[sizeof(buf) - 3] = '\r';
559 buf[sizeof(buf) - 2] = '\n';
560 buf[sizeof(buf) - 1] = '\0';
561 }
562 }
563 else
564 ircsprintf(buf, "ERROR :You have been D-lined.\r\n");
212380e3 565
6fcb8629 566 write(fd, buf, strlen(buf));
212380e3 567 comm_close(fd);
568
569 /* Re-register a new IO request for the next accept .. */
570 comm_setselect(listener->fd, FDLIST_SERVICE,
571 COMM_SELECT_READ, accept_connection, listener, 0);
572 return;
573 }
574
575 ServerStats->is_ac++;
576 add_connection(listener, fd, (struct sockaddr *)&sai);
577
578 /* Re-register a new IO request for the next accept .. */
579 comm_setselect(listener->fd, FDLIST_SERVICE, COMM_SELECT_READ,
580 accept_connection, listener, 0);
581}