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 $
33 #include "ircd_defs.h"
36 #include "s_newconf.h"
47 #define INADDR_NONE ((unsigned int) 0xffffffff)
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 } } };
55 static listener_t
*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
);
60 make_listener(struct rb_sockaddr_storage
*addr
)
62 listener_t
*listener
= (listener_t
*) rb_malloc(sizeof(listener_t
));
63 s_assert(0 != listener
);
64 listener
->name
= me
.name
;
67 memcpy(&listener
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
68 listener
->next
= NULL
;
73 free_listener(listener_t
*listener
)
75 s_assert(NULL
!= listener
);
79 * remove from listener list
81 if(listener
== ListenerPollList
)
82 ListenerPollList
= listener
->next
;
85 listener_t
*prev
= ListenerPollList
;
86 for (; prev
; prev
= prev
->next
)
88 if(listener
== prev
->next
)
90 prev
->next
= listener
->next
;
100 #define PORTNAMELEN 6 /* ":31337" */
103 * get_listener_name - return displayable listener name and port
104 * returns "host.foo.org:6667" for a given listener
107 get_listener_name(const listener_t
*listener
)
109 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
112 s_assert(NULL
!= listener
);
117 if(listener
->addr
.ss_family
== AF_INET6
)
118 port
= ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
121 port
= ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
123 rb_snprintf(buf
, sizeof(buf
), "%s[%s/%u]", me
.name
, listener
->name
, port
);
128 * show_ports - send port listing to a client
129 * inputs - pointer to client to show ports to
131 * side effects - show ports
134 show_ports(struct Client
*source_p
)
136 listener_t
*listener
= 0;
138 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
140 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
141 form_str(RPL_STATSPLINE
), 'P',
143 ntohs(listener
->addr
.ss_family
== AF_INET
? ((struct sockaddr_in
*)&listener
->addr
)->sin_port
:
144 ((struct sockaddr_in6
*)&listener
->addr
)->sin6_port
),
146 ntohs(((struct sockaddr_in
*)&listener
->addr
)->sin_port
),
148 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
149 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
150 listener
->ssl
? " ssl" : "");
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
)
175 * At first, open a new socket
178 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
181 if(listener
->addr
.ss_family
== AF_INET6
)
183 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
184 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
186 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
187 listener
->name
= listener
->vhost
;
192 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
193 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
195 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
196 listener
->name
= listener
->vhost
;
202 ilog_error("opening listener socket");
205 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
207 ilog_error("no more connections left for listener");
213 * XXX - we don't want to do all this crap for a listener
214 * set_sock_opts(listener);
216 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
218 ilog_error("setting SO_REUSEADDR for listener");
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(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
230 ilog_error("binding listener socket");
235 if((ret
= rb_listen(F
, RATBOX_SOMAXCONN
)))
237 ilog_error("listen()");
244 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
249 find_listener(struct rb_sockaddr_storage
*addr
)
251 listener_t
*listener
= NULL
;
252 listener_t
*last_closed
= NULL
;
254 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
256 if(addr
->ss_family
!= listener
->addr
.ss_family
)
259 switch(addr
->ss_family
)
263 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
264 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
265 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
266 in4
->sin_port
== lin4
->sin_port
)
268 if(listener
->F
== NULL
)
269 last_closed
= listener
;
278 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
279 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
280 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
281 in6
->sin6_port
== lin6
->sin6_port
)
283 if(listener
->F
== NULL
)
284 last_closed
= listener
;
302 * add_listener- create a new listener
303 * port - the port number to listen on
304 * vhost_ip - if non-null must contain a valid IP address string in
305 * the format "255.255.255.255"
308 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
)
310 listener_t
*listener
;
311 struct rb_sockaddr_storage vaddr
;
314 * if no port in conf line, don't bother
318 memset(&vaddr
, 0, sizeof(vaddr
));
319 vaddr
.ss_family
= family
;
323 if(family
== AF_INET
)
325 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
331 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
341 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
345 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
355 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
356 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
360 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
361 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
367 if((listener
= find_listener(&vaddr
)))
369 if(listener
->F
!= NULL
)
374 listener
= make_listener(&vaddr
);
375 listener
->next
= ListenerPollList
;
376 ListenerPollList
= listener
;
382 if(inetport(listener
))
383 listener
->active
= 1;
385 close_listener(listener
);
389 * close_listener - close a single listener
392 close_listener(listener_t
*listener
)
394 s_assert(listener
!= NULL
);
397 if(listener
->F
!= NULL
)
399 rb_close(listener
->F
);
403 listener
->active
= 0;
405 if(listener
->ref_count
)
408 free_listener(listener
);
412 * close_listeners - close and free all listeners that are not being used
417 listener_t
*listener
;
418 listener_t
*listener_next
= 0;
420 * close all 'extra' listening ports we have
422 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
424 listener_next
= listener
->next
;
425 close_listener(listener
);
429 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
432 * add_connection - creates a client which has just connected to us on
433 * the given fd. The sockhost field is initialized with the ip# of the host.
434 * The client is sent to the auth module for verification, and not put in
435 * any client list yet.
438 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
, void *ssl_ctl
)
440 struct Client
*new_client
;
441 s_assert(NULL
!= listener
);
444 * get the client socket name from the socket
445 * the client has already been checked out in accept_connection
447 new_client
= make_client(NULL
);
449 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
450 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
453 * copy address to 'sockhost' as a string, copy it to host too
454 * so we have something valid to put into error messages...
456 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
457 sizeof(new_client
->sockhost
));
460 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
462 new_client
->localClient
->F
= F
;
463 add_to_cli_fd_hash(new_client
);
464 new_client
->localClient
->listener
= listener
;
465 new_client
->localClient
->ssl_ctl
= ssl_ctl
;
466 if(ssl_ctl
!= NULL
|| rb_fd_ssl(F
))
469 ++listener
->ref_count
;
471 start_auth(new_client
);
474 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
477 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
479 struct Listener
*listener
= (struct Listener
*)data
;
481 struct ConfItem
*aconf
;
482 static time_t last_oper_notice
= 0;
484 if(listener
->ssl
&& (!ssl_ok
|| !get_ssld_count()))
490 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
492 ++ServerStats
.is_ref
;
494 * slow down the whining to opers bit
496 if((last_oper_notice
+ 20) <= rb_current_time())
498 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
499 "All connections in use. (%s)",
500 get_listener_name(listener
));
501 last_oper_notice
= rb_current_time();
504 rb_write(F
, "ERROR :All connections in use\r\n", 32);
506 /* Re-register a new IO request for the next accept .. */
510 aconf
= find_dline(addr
, addr
->sa_family
);
511 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
514 /* Do an initial check we aren't connecting too fast or with too many
518 ServerStats
.is_ref
++;
520 if(ConfigFileEntry
.dline_with_reason
)
522 if (rb_snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", aconf
->passwd
) >= (int)(sizeof(buf
)-1))
524 buf
[sizeof(buf
) - 3] = '\r';
525 buf
[sizeof(buf
) - 2] = '\n';
526 buf
[sizeof(buf
) - 1] = '\0';
530 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
532 rb_write(F
, buf
, strlen(buf
));
537 if(check_reject(F
, addr
))
540 if(throttle_add(addr
))
542 rb_write(F
, toofast
, strlen(toofast
));
551 accept_ssld(rb_fde_t
*F
, struct sockaddr
*addr
, struct sockaddr
*laddr
, struct Listener
*listener
)
555 rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection");
556 ctl
= start_ssld_accept(F
, xF
[1], rb_get_fd(xF
[0])); /* this will close F for us */
557 add_connection(listener
, xF
[0], addr
, laddr
, ctl
);
561 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
563 struct Listener
*listener
= data
;
564 struct rb_sockaddr_storage lip
;
565 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
569 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
571 /* this shouldn't fail so... */
572 /* XXX add logging of this */
577 accept_ssld(F
, addr
, (struct sockaddr
*)&lip
, listener
);
579 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
, NULL
);