2 * ircd-ratbox: A slightly useful ircd.
3 * listener.c: Listens on a port.
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
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.
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.
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
24 * $Id: listener.c 3460 2007-05-18 20:31:33Z jilles $
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
34 #include "ircd_defs.h"
38 #include "s_newconf.h"
48 #define INADDR_NONE ((unsigned int) 0xffffffff)
51 #if defined(NO_IN6ADDR_ANY) && defined(IPV6)
52 static const struct in6_addr in6addr_any
=
53 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
56 static PF accept_connection
;
58 static listener_t
*ListenerPollList
= NULL
;
61 make_listener(struct irc_sockaddr_storage
*addr
)
63 listener_t
*listener
= (listener_t
*) MyMalloc(sizeof(listener_t
));
64 s_assert(0 != listener
);
66 listener
->name
= me
.name
;
68 memcpy(&listener
->addr
, addr
, sizeof(struct irc_sockaddr_storage
));
69 listener
->next
= NULL
;
74 free_listener(listener_t
*listener
)
76 s_assert(NULL
!= listener
);
80 * remove from listener list
82 if(listener
== ListenerPollList
)
83 ListenerPollList
= listener
->next
;
86 listener_t
*prev
= ListenerPollList
;
87 for (; prev
; prev
= prev
->next
)
89 if(listener
== prev
->next
)
91 prev
->next
= listener
->next
;
101 #define PORTNAMELEN 6 /* ":31337" */
104 * get_listener_name - return displayable listener name and port
105 * returns "host.foo.org:6667" for a given listener
108 get_listener_name(const listener_t
*listener
)
110 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
113 s_assert(NULL
!= listener
);
118 if(listener
->addr
.ss_family
== AF_INET6
)
119 port
= ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
122 port
= ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
124 ircsnprintf(buf
, sizeof(buf
), "%s[%s/%u]", me
.name
, listener
->name
, port
);
129 * show_ports - send port listing to a client
130 * inputs - pointer to client to show ports to
132 * side effects - show ports
135 show_ports(struct Client
*source_p
)
137 listener_t
*listener
= 0;
139 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
141 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
142 form_str(RPL_STATSPLINE
), 'P',
144 ntohs(listener
->addr
.ss_family
== AF_INET
? ((struct sockaddr_in
*)&listener
->addr
)->sin_port
:
145 ((struct sockaddr_in6
*)&listener
->addr
)->sin6_port
),
147 ntohs(((struct sockaddr_in
*)&listener
->addr
)->sin_port
),
149 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
150 listener
->ref_count
, (listener
->active
) ? "active" : "disabled");
155 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
156 * bind it to the port given in 'port' and listen to it
157 * returns true (1) if successful false (0) on error.
159 * If the operating system has a define for SOMAXCONN, use it, otherwise
160 * use RATBOX_SOMAXCONN
163 #undef RATBOX_SOMAXCONN
164 #define RATBOX_SOMAXCONN SOMAXCONN
168 inetport(listener_t
*listener
)
174 * At first, open a new socket
177 fd
= comm_socket(listener
->addr
.ss_family
, SOCK_STREAM
, 0, "Listener socket");
180 if(listener
->addr
.ss_family
== AF_INET6
)
182 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
183 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
185 inetntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
186 listener
->name
= listener
->vhost
;
191 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
192 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
194 inetntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
195 listener
->name
= listener
->vhost
;
200 * At one point, we enforced a strange arbitrary limit here.
201 * We no longer do this, and just check if the fd is valid or not.
206 report_error("opening listener socket %s:%s",
207 get_listener_name(listener
),
208 get_listener_name(listener
), errno
);
213 * XXX - we don't want to do all this crap for a listener
214 * set_sock_opts(listener);
216 if(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
218 report_error("setting SO_REUSEADDR for listener %s:%s",
219 get_listener_name(listener
),
220 get_listener_name(listener
), errno
);
226 * Bind a port to listen for new connections if port is non-null,
227 * else assume it is already open and try get something from it.
230 if(bind(fd
, (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(listener
->addr
)))
232 report_error("binding listener socket %s:%s",
233 get_listener_name(listener
),
234 get_listener_name(listener
), errno
);
239 if(listen(fd
, RATBOX_SOMAXCONN
))
241 report_error("listen failed for %s:%s",
242 get_listener_name(listener
),
243 get_listener_name(listener
), errno
);
250 /* Listen completion events are READ events .. */
252 accept_connection(fd
, listener
);
257 find_listener(struct irc_sockaddr_storage
*addr
)
259 listener_t
*listener
= NULL
;
260 listener_t
*last_closed
= NULL
;
262 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
264 if(addr
->ss_family
!= listener
->addr
.ss_family
)
267 switch(addr
->ss_family
)
271 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
272 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
273 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
274 in4
->sin_port
== lin4
->sin_port
)
276 if(listener
->fd
== -1)
277 last_closed
= listener
;
286 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
287 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
288 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
289 in6
->sin6_port
== lin6
->sin6_port
)
291 if(listener
->fd
== -1)
292 last_closed
= listener
;
310 * add_listener- create a new listener
311 * port - the port number to listen on
312 * vhost_ip - if non-null must contain a valid IP address string in
313 * the format "255.255.255.255"
316 add_listener(int port
, const char *vhost_ip
, int family
)
318 listener_t
*listener
;
319 struct irc_sockaddr_storage vaddr
;
322 * if no port in conf line, don't bother
326 memset(&vaddr
, 0, sizeof(vaddr
));
327 vaddr
.ss_family
= family
;
331 if(family
== AF_INET
)
333 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
339 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
349 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
353 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
363 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in
));
364 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
368 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in6
));
369 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
375 if((listener
= find_listener(&vaddr
)))
377 if(listener
->fd
> -1)
382 listener
= make_listener(&vaddr
);
383 listener
->next
= ListenerPollList
;
384 ListenerPollList
= listener
;
389 if(inetport(listener
))
390 listener
->active
= 1;
392 close_listener(listener
);
396 * close_listener - close a single listener
399 close_listener(listener_t
*listener
)
401 s_assert(listener
!= NULL
);
404 if(listener
->fd
>= 0)
406 comm_close(listener
->fd
);
410 listener
->active
= 0;
412 if(listener
->ref_count
)
415 free_listener(listener
);
419 * close_listeners - close and free all listeners that are not being used
424 listener_t
*listener
;
425 listener_t
*listener_next
= 0;
427 * close all 'extra' listening ports we have
429 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
431 listener_next
= listener
->next
;
432 close_listener(listener
);
436 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
439 * add_connection - creates a client which has just connected to us on
440 * the given fd. The sockhost field is initialized with the ip# of the host.
441 * The client is sent to the auth module for verification, and not put in
442 * any client list yet.
445 add_connection(listener_t
*listener
, int fd
, struct sockaddr
*sai
, int exempt
)
447 struct Client
*new_client
;
448 s_assert(NULL
!= listener
);
451 * get the client socket name from the socket
452 * the client has already been checked out in accept_connection
454 new_client
= make_client(NULL
);
456 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct irc_sockaddr_storage
));
459 * copy address to 'sockhost' as a string, copy it to host too
460 * so we have something valid to put into error messages...
462 inetntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
463 sizeof(new_client
->sockhost
));
466 strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
468 new_client
->localClient
->F
= comm_add_fd(fd
);
470 new_client
->localClient
->listener
= listener
;
471 ++listener
->ref_count
;
475 if(check_reject(new_client
))
477 if(add_unknown_ip(new_client
))
481 start_auth(new_client
);
486 accept_connection(int pfd
, void *data
)
488 static time_t last_oper_notice
= 0;
489 struct irc_sockaddr_storage sai
;
490 socklen_t addrlen
= sizeof(sai
);
492 listener_t
*listener
= data
;
493 struct ConfItem
*aconf
;
496 s_assert(listener
!= NULL
);
500 * There may be many reasons for error return, but
501 * in otherwise correctly working environment the
502 * probable cause is running out of file descriptors
503 * (EMFILE, ENFILE or others?). The man pages for
504 * accept don't seem to list these as possible,
505 * although it's obvious that it may happen here.
506 * Thus no specific errors are tested at this
507 * point, just assume that connections cannot
508 * be accepted until some old is closed first.
511 fd
= comm_accept(listener
->fd
, (struct sockaddr
*)&sai
, &addrlen
);
514 /* Re-register a new IO request for the next accept .. */
515 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
516 COMM_SELECT_READ
, accept_connection
, listener
, 0);
520 /* This needs to be done here, otherwise we break dlines */
521 mangle_mapped_sockaddr((struct sockaddr
*)&sai
);
524 * check for connection limit
525 * TBD: this is stupid... either we have a socket or we don't. -nenolod
527 if((comm_get_maxconnections() - 10) < fd
)
529 ++ServerStats
->is_ref
;
531 * slow down the whining to opers bit
533 if((last_oper_notice
+ 20) <= CurrentTime
)
535 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
536 "All connections in use. (%s)",
537 get_listener_name(listener
));
538 last_oper_notice
= CurrentTime
;
541 write(fd
, "ERROR :All connections in use\r\n", 32);
543 /* Re-register a new IO request for the next accept .. */
544 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
545 COMM_SELECT_READ
, accept_connection
, listener
, 0);
549 /* Do an initial check we aren't connecting too fast or with too many
551 aconf
= find_dline((struct sockaddr
*) &sai
, sai
.ss_family
);
552 /* check it wasn't an exempt */
553 if (aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
) == 0)
555 ServerStats
->is_ref
++;
557 if(ConfigFileEntry
.dline_with_reason
)
559 if (ircsnprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", aconf
->passwd
) >= (sizeof(buf
)-1))
561 buf
[sizeof(buf
) - 3] = '\r';
562 buf
[sizeof(buf
) - 2] = '\n';
563 buf
[sizeof(buf
) - 1] = '\0';
567 ircsprintf(buf
, "ERROR :You have been D-lined.\r\n");
569 write(fd
, buf
, strlen(buf
));
572 /* Re-register a new IO request for the next accept .. */
573 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
574 COMM_SELECT_READ
, accept_connection
, listener
, 0);
578 ServerStats
->is_ac
++;
579 add_connection(listener
, fd
, (struct sockaddr
*)&sai
, aconf
? 1 : 0);
581 /* Re-register a new IO request for the next accept .. */
582 comm_setselect(listener
->fd
, FDLIST_SERVICE
, COMM_SELECT_READ
,
583 accept_connection
, listener
, 0);