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 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
);
59 static struct Listener
*
60 make_listener(struct rb_sockaddr_storage
*addr
)
62 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
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(struct Listener
*listener
)
75 s_assert(NULL
!= listener
);
79 * remove from listener list
81 if(listener
== ListenerPollList
)
82 ListenerPollList
= listener
->next
;
85 struct Listener
*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 struct Listener
*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 struct Listener
*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(struct Listener
*listener
)
174 * At first, open a new socket
177 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), 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 rb_inet_ntop(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 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
195 listener
->name
= listener
->vhost
;
201 ilog_error("opening listener socket");
204 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
206 ilog_error("no more connections left for listener");
212 * XXX - we don't want to do all this crap for a listener
213 * set_sock_opts(listener);
215 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
217 ilog_error("setting SO_REUSEADDR for listener");
223 * Bind a port to listen for new connections if port is non-null,
224 * else assume it is already open and try get something from it.
227 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
229 ilog_error("binding listener socket");
234 if(rb_listen(F
, RATBOX_SOMAXCONN
))
236 ilog_error("listen()");
243 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
247 static struct Listener
*
248 find_listener(struct rb_sockaddr_storage
*addr
)
250 struct Listener
*listener
= NULL
;
251 struct Listener
*last_closed
= NULL
;
253 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
255 if(addr
->ss_family
!= listener
->addr
.ss_family
)
258 switch(addr
->ss_family
)
262 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
263 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
264 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
265 in4
->sin_port
== lin4
->sin_port
)
267 if(listener
->F
== NULL
)
268 last_closed
= listener
;
277 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
278 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
279 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
280 in6
->sin6_port
== lin6
->sin6_port
)
282 if(listener
->F
== NULL
)
283 last_closed
= listener
;
301 * add_listener- create a new listener
302 * port - the port number to listen on
303 * vhost_ip - if non-null must contain a valid IP address string in
304 * the format "255.255.255.255"
307 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
)
309 struct Listener
*listener
;
310 struct rb_sockaddr_storage vaddr
;
313 * if no port in conf line, don't bother
317 memset(&vaddr
, 0, sizeof(vaddr
));
318 vaddr
.ss_family
= family
;
322 if(family
== AF_INET
)
324 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
330 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
340 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
344 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
354 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
355 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
359 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
360 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
366 if((listener
= find_listener(&vaddr
)))
368 if(listener
->F
!= NULL
)
373 listener
= make_listener(&vaddr
);
374 listener
->next
= ListenerPollList
;
375 ListenerPollList
= listener
;
381 if(inetport(listener
))
382 listener
->active
= 1;
384 close_listener(listener
);
388 * close_listener - close a single listener
391 close_listener(struct Listener
*listener
)
393 s_assert(listener
!= NULL
);
396 if(listener
->F
!= NULL
)
398 rb_close(listener
->F
);
402 listener
->active
= 0;
404 if(listener
->ref_count
)
407 free_listener(listener
);
411 * close_listeners - close and free all listeners that are not being used
416 struct Listener
*listener
;
417 struct Listener
*listener_next
= 0;
419 * close all 'extra' listening ports we have
421 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
423 listener_next
= listener
->next
;
424 close_listener(listener
);
428 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
431 * add_connection - creates a client which has just connected to us on
432 * the given fd. The sockhost field is initialized with the ip# of the host.
433 * The client is sent to the auth module for verification, and not put in
434 * any client list yet.
437 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
, void *ssl_ctl
)
439 struct Client
*new_client
;
440 s_assert(NULL
!= listener
);
443 * get the client socket name from the socket
444 * the client has already been checked out in accept_connection
446 new_client
= make_client(NULL
);
448 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
449 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
452 * copy address to 'sockhost' as a string, copy it to host too
453 * so we have something valid to put into error messages...
455 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
456 sizeof(new_client
->sockhost
));
459 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
461 new_client
->localClient
->F
= F
;
462 add_to_cli_fd_hash(new_client
);
463 new_client
->localClient
->listener
= listener
;
464 new_client
->localClient
->ssl_ctl
= ssl_ctl
;
465 if(ssl_ctl
!= NULL
|| rb_fd_ssl(F
))
468 ++listener
->ref_count
;
470 start_auth(new_client
);
473 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
476 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
478 struct Listener
*listener
= (struct Listener
*)data
;
480 struct ConfItem
*aconf
;
481 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 len
= rb_snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
523 if (len
>= (int)(sizeof(buf
)-1))
525 buf
[sizeof(buf
) - 3] = '\r';
526 buf
[sizeof(buf
) - 2] = '\n';
527 buf
[sizeof(buf
) - 1] = '\0';
531 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
533 rb_write(F
, buf
, strlen(buf
));
538 if(check_reject(F
, addr
))
541 if(throttle_add(addr
))
543 rb_write(F
, toofast
, strlen(toofast
));
552 accept_ssld(rb_fde_t
*F
, struct sockaddr
*addr
, struct sockaddr
*laddr
, struct Listener
*listener
)
556 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
558 ilog_error("creating SSL/TLS socket pairs");
562 ctl
= start_ssld_accept(F
, xF
[1], rb_get_fd(xF
[0])); /* this will close F for us */
563 add_connection(listener
, xF
[0], addr
, laddr
, ctl
);
567 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
569 struct Listener
*listener
= data
;
570 struct rb_sockaddr_storage lip
;
571 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
575 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
577 /* this can fail if the connection disappeared in the meantime */
583 accept_ssld(F
, addr
, (struct sockaddr
*)&lip
, listener
);
585 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
, NULL
);