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"
37 #include "s_newconf.h"
48 #define INADDR_NONE ((unsigned int) 0xffffffff)
51 #if defined(NO_IN6ADDR_ANY) && defined(RB_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 listener_t
*ListenerPollList
= NULL
;
57 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
58 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
61 make_listener(struct rb_sockaddr_storage
*addr
)
63 listener_t
*listener
= (listener_t
*) rb_malloc(sizeof(listener_t
));
64 s_assert(0 != listener
);
65 listener
->name
= me
.name
;
68 memcpy(&listener
->addr
, addr
, sizeof(struct rb_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 rb_snprintf(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",
151 listener
->ssl
? " ssl" : "");
156 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
157 * bind it to the port given in 'port' and listen to it
158 * returns true (1) if successful false (0) on error.
160 * If the operating system has a define for SOMAXCONN, use it, otherwise
161 * use RATBOX_SOMAXCONN
164 #undef RATBOX_SOMAXCONN
165 #define RATBOX_SOMAXCONN SOMAXCONN
169 inetport(listener_t
*listener
)
176 * At first, open a new socket
179 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
182 if(listener
->addr
.ss_family
== AF_INET6
)
184 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
185 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
187 inetntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
188 listener
->name
= listener
->vhost
;
193 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
194 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
196 inetntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
197 listener
->name
= listener
->vhost
;
203 ilog_error("opening listener socket");
206 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
208 ilog_error("no more connections left for listener");
214 * XXX - we don't want to do all this crap for a listener
215 * set_sock_opts(listener);
217 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
219 ilog_error("setting SO_REUSEADDR for listener");
225 * Bind a port to listen for new connections if port is non-null,
226 * else assume it is already open and try get something from it.
229 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
231 ilog_error("binding listener socket");
236 if((ret
= rb_listen(F
, RATBOX_SOMAXCONN
)))
238 ilog_error("listen()");
245 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
250 find_listener(struct rb_sockaddr_storage
*addr
)
252 listener_t
*listener
= NULL
;
253 listener_t
*last_closed
= NULL
;
255 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
257 if(addr
->ss_family
!= listener
->addr
.ss_family
)
260 switch(addr
->ss_family
)
264 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
265 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
266 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
267 in4
->sin_port
== lin4
->sin_port
)
269 if(listener
->F
== NULL
)
270 last_closed
= listener
;
279 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
280 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
281 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
282 in6
->sin6_port
== lin6
->sin6_port
)
284 if(listener
->F
== NULL
)
285 last_closed
= listener
;
303 * add_listener- create a new listener
304 * port - the port number to listen on
305 * vhost_ip - if non-null must contain a valid IP address string in
306 * the format "255.255.255.255"
309 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
)
311 listener_t
*listener
;
312 struct rb_sockaddr_storage vaddr
;
315 * if no port in conf line, don't bother
319 memset(&vaddr
, 0, sizeof(vaddr
));
320 vaddr
.ss_family
= family
;
324 if(family
== AF_INET
)
326 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
332 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
342 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
346 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
356 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
357 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
361 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
362 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
368 if((listener
= find_listener(&vaddr
)))
370 if(listener
->F
!= NULL
)
375 listener
= make_listener(&vaddr
);
376 listener
->next
= ListenerPollList
;
377 ListenerPollList
= listener
;
383 if(inetport(listener
))
384 listener
->active
= 1;
386 close_listener(listener
);
390 * close_listener - close a single listener
393 close_listener(listener_t
*listener
)
395 s_assert(listener
!= NULL
);
398 if(listener
->F
!= NULL
)
400 rb_close(listener
->F
);
404 listener
->active
= 0;
406 if(listener
->ref_count
)
409 free_listener(listener
);
413 * close_listeners - close and free all listeners that are not being used
418 listener_t
*listener
;
419 listener_t
*listener_next
= 0;
421 * close all 'extra' listening ports we have
423 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
425 listener_next
= listener
->next
;
426 close_listener(listener
);
430 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
433 * add_connection - creates a client which has just connected to us on
434 * the given fd. The sockhost field is initialized with the ip# of the host.
435 * The client is sent to the auth module for verification, and not put in
436 * any client list yet.
439 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, void *ssl_ctl
, int exempt
)
441 struct Client
*new_client
;
442 s_assert(NULL
!= listener
);
445 * get the client socket name from the socket
446 * the client has already been checked out in accept_connection
448 new_client
= make_client(NULL
);
450 memcpy(&new_client
->localClient
->ip
, sai
, 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 inetntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
457 sizeof(new_client
->sockhost
));
460 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
;
473 if(check_reject(new_client
))
475 if(add_unknown_ip(new_client
))
479 start_auth(new_client
);
483 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
485 struct Listener
*listener
= (struct Listener
*)data
;
487 struct ConfItem
*aconf
;
488 static time_t last_oper_notice
= 0;
490 if(listener
->ssl
&& (!ssl_ok
|| !get_ssld_count()))
496 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
498 ++ServerStats
.is_ref
;
500 * slow down the whining to opers bit
502 if((last_oper_notice
+ 20) <= rb_current_time())
504 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
505 "All connections in use. (%s)",
506 get_listener_name(listener
));
507 last_oper_notice
= rb_current_time();
510 rb_write(F
, "ERROR :All connections in use\r\n", 32);
512 /* Re-register a new IO request for the next accept .. */
516 aconf
= find_dline(addr
, AF_INET
);
517 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
520 /* Do an initial check we aren't connecting too fast or with too many
524 ServerStats
.is_ref
++;
526 if(ConfigFileEntry
.dline_with_reason
)
528 if (rb_snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", aconf
->passwd
) >= (int)(sizeof(buf
)-1))
530 buf
[sizeof(buf
) - 3] = '\r';
531 buf
[sizeof(buf
) - 2] = '\n';
532 buf
[sizeof(buf
) - 1] = '\0';
536 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
538 rb_write(F
, buf
, strlen(buf
));
547 accept_ssld(rb_fde_t
*F
, struct sockaddr
*addr
, struct sockaddr
*laddr
, struct Listener
*listener
)
551 rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection");
552 ctl
= start_ssld_accept(F
, xF
[1], rb_get_fd(xF
[0])); /* this will close F for us */
553 add_connection(listener
, xF
[0], addr
, ctl
, 1);
557 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
559 struct Listener
*listener
= data
;
560 struct rb_sockaddr_storage lip
;
561 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
565 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
567 /* this shouldn't fail so... */
568 /* XXX add logging of this */
573 accept_ssld(F
, addr
, (struct sockaddr
*)&lip
, listener
);
575 add_connection(listener
, F
, addr
, NULL
, 1);