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 3354 2007-04-03 09:21:31Z nenolod $
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
34 #include "ircd_defs.h"
38 #include "s_newconf.h"
46 #define INADDR_NONE ((unsigned int) 0xffffffff)
49 #if defined(NO_IN6ADDR_ANY) && defined(IPV6)
50 static const struct in6_addr in6addr_any
=
51 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
54 static PF accept_connection
;
56 static listener_t
*ListenerPollList
= NULL
;
59 make_listener(struct irc_sockaddr_storage
*addr
)
61 listener_t
*listener
= (listener_t
*) MyMalloc(sizeof(listener_t
));
62 s_assert(0 != listener
);
64 listener
->name
= me
.name
;
66 memcpy(&listener
->addr
, addr
, sizeof(struct irc_sockaddr_storage
));
67 listener
->next
= NULL
;
72 free_listener(listener_t
*listener
)
74 s_assert(NULL
!= listener
);
78 * remove from listener list
80 if(listener
== ListenerPollList
)
81 ListenerPollList
= listener
->next
;
84 listener_t
*prev
= ListenerPollList
;
85 for (; prev
; prev
= prev
->next
)
87 if(listener
== prev
->next
)
89 prev
->next
= listener
->next
;
99 #define PORTNAMELEN 6 /* ":31337" */
102 * get_listener_name - return displayable listener name and port
103 * returns "host.foo.org:6667" for a given listener
106 get_listener_name(const listener_t
*listener
)
108 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
111 s_assert(NULL
!= listener
);
116 if(listener
->addr
.ss_family
== AF_INET6
)
117 port
= ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
120 port
= ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
122 ircsnprintf(buf
, sizeof(buf
), "%s[%s/%u]", me
.name
, listener
->name
, port
);
127 * show_ports - send port listing to a client
128 * inputs - pointer to client to show ports to
130 * side effects - show ports
133 show_ports(struct Client
*source_p
)
135 listener_t
*listener
= 0;
137 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
139 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
140 form_str(RPL_STATSPLINE
), 'P',
142 ntohs(listener
->addr
.ss_family
== AF_INET
? ((struct sockaddr_in
*)&listener
->addr
)->sin_port
:
143 ((struct sockaddr_in6
*)&listener
->addr
)->sin6_port
),
145 ntohs(((struct sockaddr_in
*)&listener
->addr
)->sin_port
),
147 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
148 listener
->ref_count
, (listener
->active
) ? "active" : "disabled");
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.
157 * If the operating system has a define for SOMAXCONN, use it, otherwise
158 * use RATBOX_SOMAXCONN
161 #undef RATBOX_SOMAXCONN
162 #define RATBOX_SOMAXCONN SOMAXCONN
166 inetport(listener_t
*listener
)
172 * At first, open a new socket
175 fd
= comm_socket(listener
->addr
.ss_family
, SOCK_STREAM
, 0, "Listener socket");
178 if(listener
->addr
.ss_family
== AF_INET6
)
180 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
181 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
183 inetntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
184 listener
->name
= listener
->vhost
;
189 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
190 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
192 inetntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
193 listener
->name
= listener
->vhost
;
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.
204 report_error("opening listener socket %s:%s",
205 get_listener_name(listener
),
206 get_listener_name(listener
), errno
);
211 * XXX - we don't want to do all this crap for a listener
212 * set_sock_opts(listener);
214 if(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
216 report_error("setting SO_REUSEADDR for listener %s:%s",
217 get_listener_name(listener
),
218 get_listener_name(listener
), errno
);
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.
228 if(bind(fd
, (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(listener
->addr
)))
230 report_error("binding listener socket %s:%s",
231 get_listener_name(listener
),
232 get_listener_name(listener
), errno
);
237 if(listen(fd
, RATBOX_SOMAXCONN
))
239 report_error("listen failed for %s:%s",
240 get_listener_name(listener
),
241 get_listener_name(listener
), errno
);
248 /* Listen completion events are READ events .. */
250 accept_connection(fd
, listener
);
255 find_listener(struct irc_sockaddr_storage
*addr
)
257 listener_t
*listener
= NULL
;
258 listener_t
*last_closed
= NULL
;
260 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
262 if(addr
->ss_family
!= listener
->addr
.ss_family
)
265 switch(addr
->ss_family
)
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
)
274 if(listener
->fd
== -1)
275 last_closed
= listener
;
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
)
289 if(listener
->fd
== -1)
290 last_closed
= listener
;
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"
314 add_listener(int port
, const char *vhost_ip
, int family
)
316 listener_t
*listener
;
317 struct irc_sockaddr_storage vaddr
;
320 * if no port in conf line, don't bother
324 memset(&vaddr
, 0, sizeof(vaddr
));
325 vaddr
.ss_family
= family
;
329 if(family
== AF_INET
)
331 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
337 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
347 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
351 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
361 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in
));
362 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
366 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in6
));
367 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
373 if((listener
= find_listener(&vaddr
)))
375 if(listener
->fd
> -1)
380 listener
= make_listener(&vaddr
);
381 listener
->next
= ListenerPollList
;
382 ListenerPollList
= listener
;
387 if(inetport(listener
))
388 listener
->active
= 1;
390 close_listener(listener
);
394 * close_listener - close a single listener
397 close_listener(listener_t
*listener
)
399 s_assert(listener
!= NULL
);
402 if(listener
->fd
>= 0)
404 comm_close(listener
->fd
);
408 listener
->active
= 0;
410 if(listener
->ref_count
)
413 free_listener(listener
);
417 * close_listeners - close and free all listeners that are not being used
422 listener_t
*listener
;
423 listener_t
*listener_next
= 0;
425 * close all 'extra' listening ports we have
427 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
429 listener_next
= listener
->next
;
430 close_listener(listener
);
434 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
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.
443 add_connection(listener_t
*listener
, int fd
, struct sockaddr
*sai
)
445 struct Client
*new_client
;
446 s_assert(NULL
!= listener
);
449 * get the client socket name from the socket
450 * the client has already been checked out in accept_connection
452 new_client
= make_client(NULL
);
454 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct irc_sockaddr_storage
));
457 * copy address to 'sockhost' as a string, copy it to host too
458 * so we have something valid to put into error messages...
460 inetntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
461 sizeof(new_client
->sockhost
));
464 strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
467 if(new_client
->localClient
->ip
.ss_family
== AF_INET6
&& ConfigFileEntry
.dot_in_ip6_addr
== 1)
469 strlcat(new_client
->host
, ".", sizeof(new_client
->host
));
473 new_client
->localClient
->fd
= fd
;
475 new_client
->localClient
->listener
= listener
;
476 ++listener
->ref_count
;
478 if(check_reject(new_client
))
480 start_auth(new_client
);
485 accept_connection(int pfd
, void *data
)
487 static time_t last_oper_notice
= 0;
488 struct irc_sockaddr_storage sai
;
489 socklen_t addrlen
= sizeof(sai
);
491 listener_t
*listener
= data
;
492 struct ConfItem
*aconf
;
495 s_assert(listener
!= NULL
);
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.
510 fd
= comm_accept(listener
->fd
, (struct sockaddr
*)&sai
, &addrlen
);
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);
519 /* This needs to be done here, otherwise we break dlines */
520 mangle_mapped_sockaddr((struct sockaddr
*)&sai
);
523 * check for connection limit
524 * TBD: this is stupid... either we have a socket or we don't. -nenolod
526 if((comm_get_maxconnections() - 10) < fd
)
528 ++ServerStats
->is_ref
;
530 * slow down the whining to opers bit
532 if((last_oper_notice
+ 20) <= CurrentTime
)
534 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
535 "All connections in use. (%s)",
536 get_listener_name(listener
));
537 last_oper_notice
= CurrentTime
;
540 write(fd
, "ERROR :All connections in use\r\n", 32);
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);
548 /* Do an initial check we aren't connecting too fast or with too many
550 if((aconf
= conf_connect_allowed((struct sockaddr
*)&sai
, sai
.ss_family
)) != NULL
)
552 ServerStats
->is_ref
++;
554 if(ConfigFileEntry
.dline_with_reason
)
556 if (ircsnprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", aconf
->passwd
) >= (sizeof(buf
)-1))
558 buf
[sizeof(buf
) - 3] = '\r';
559 buf
[sizeof(buf
) - 2] = '\n';
560 buf
[sizeof(buf
) - 1] = '\0';
564 ircsprintf(buf
, "ERROR :You have been D-lined.\r\n");
566 write(fd
, buf
, strlen(buf
));
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);
575 ServerStats
->is_ac
++;
576 add_connection(listener
, fd
, (struct sockaddr
*)&sai
);
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);