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
32 #include "ircd_defs.h"
35 #include "s_newconf.h"
46 #define INADDR_NONE ((unsigned int) 0xffffffff)
49 #if defined(NO_IN6ADDR_ANY) && defined(RB_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 struct Listener
*ListenerPollList
= NULL
;
55 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
56 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
58 static struct Listener
*
59 make_listener(struct rb_sockaddr_storage
*addr
)
61 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
62 s_assert(0 != listener
);
63 listener
->name
= me
.name
;
66 memcpy(&listener
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
67 listener
->next
= NULL
;
72 free_listener(struct Listener
*listener
)
74 s_assert(NULL
!= listener
);
78 * remove from listener list
80 if(listener
== ListenerPollList
)
81 ListenerPollList
= listener
->next
;
84 struct Listener
*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 struct Listener
*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 rb_snprintf(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 struct Listener
*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",
149 listener
->ssl
? " ssl" : "");
154 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
155 * bind it to the port given in 'port' and listen to it
156 * returns true (1) if successful false (0) on error.
158 * If the operating system has a define for SOMAXCONN, use it, otherwise
159 * use RATBOX_SOMAXCONN
162 #undef RATBOX_SOMAXCONN
163 #define RATBOX_SOMAXCONN SOMAXCONN
167 inetport(struct Listener
*listener
)
173 * At first, open a new socket
176 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
179 if(listener
->addr
.ss_family
== AF_INET6
)
181 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
182 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
184 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
185 listener
->name
= listener
->vhost
;
190 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
191 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
193 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
194 listener
->name
= listener
->vhost
;
200 ilog_error("opening listener socket");
203 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
205 ilog_error("no more connections left for listener");
211 * XXX - we don't want to do all this crap for a listener
212 * set_sock_opts(listener);
214 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
216 ilog_error("setting SO_REUSEADDR for listener");
222 * Bind a port to listen for new connections if port is non-null,
223 * else assume it is already open and try get something from it.
226 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
228 ilog_error("binding listener socket");
233 if(rb_listen(F
, RATBOX_SOMAXCONN
))
235 ilog_error("listen()");
242 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
246 static struct Listener
*
247 find_listener(struct rb_sockaddr_storage
*addr
)
249 struct Listener
*listener
= NULL
;
250 struct Listener
*last_closed
= NULL
;
252 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
254 if(addr
->ss_family
!= listener
->addr
.ss_family
)
257 switch(addr
->ss_family
)
261 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
262 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
263 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
264 in4
->sin_port
== lin4
->sin_port
)
266 if(listener
->F
== NULL
)
267 last_closed
= listener
;
276 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
277 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
278 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
279 in6
->sin6_port
== lin6
->sin6_port
)
281 if(listener
->F
== NULL
)
282 last_closed
= listener
;
300 * add_listener- create a new listener
301 * port - the port number to listen on
302 * vhost_ip - if non-null must contain a valid IP address string in
303 * the format "255.255.255.255"
306 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
)
308 struct Listener
*listener
;
309 struct rb_sockaddr_storage vaddr
;
312 * if no port in conf line, don't bother
316 memset(&vaddr
, 0, sizeof(vaddr
));
317 vaddr
.ss_family
= family
;
321 if(family
== AF_INET
)
323 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
329 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
339 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
343 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
353 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
354 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
358 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
359 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
365 if((listener
= find_listener(&vaddr
)))
367 if(listener
->F
!= NULL
)
372 listener
= make_listener(&vaddr
);
373 listener
->next
= ListenerPollList
;
374 ListenerPollList
= listener
;
380 if(inetport(listener
))
381 listener
->active
= 1;
383 close_listener(listener
);
387 * close_listener - close a single listener
390 close_listener(struct Listener
*listener
)
392 s_assert(listener
!= NULL
);
395 if(listener
->F
!= NULL
)
397 rb_close(listener
->F
);
401 listener
->active
= 0;
403 if(listener
->ref_count
)
406 free_listener(listener
);
410 * close_listeners - close and free all listeners that are not being used
415 struct Listener
*listener
;
416 struct Listener
*listener_next
= 0;
418 * close all 'extra' listening ports we have
420 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
422 listener_next
= listener
->next
;
423 close_listener(listener
);
427 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
430 * add_connection - creates a client which has just connected to us on
431 * the given fd. The sockhost field is initialized with the ip# of the host.
432 * The client is sent to the auth module for verification, and not put in
433 * any client list yet.
436 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
, void *ssl_ctl
)
438 struct Client
*new_client
;
439 s_assert(NULL
!= listener
);
442 * get the client socket name from the socket
443 * the client has already been checked out in accept_connection
445 new_client
= make_client(NULL
);
447 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
448 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
451 * copy address to 'sockhost' as a string, copy it to host too
452 * so we have something valid to put into error messages...
454 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
455 sizeof(new_client
->sockhost
));
458 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
460 new_client
->localClient
->F
= F
;
461 add_to_cli_fd_hash(new_client
);
462 new_client
->localClient
->listener
= listener
;
463 new_client
->localClient
->ssl_ctl
= ssl_ctl
;
464 if(ssl_ctl
!= NULL
|| rb_fd_ssl(F
))
467 ++listener
->ref_count
;
469 start_auth(new_client
);
472 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
475 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
477 struct Listener
*listener
= (struct Listener
*)data
;
479 struct ConfItem
*aconf
;
480 static time_t last_oper_notice
= 0;
483 if(listener
->ssl
&& (!ssl_ok
|| !get_ssld_count()))
489 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
491 ++ServerStats
.is_ref
;
493 * slow down the whining to opers bit
495 if((last_oper_notice
+ 20) <= rb_current_time())
497 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
498 "All connections in use. (%s)",
499 get_listener_name(listener
));
500 last_oper_notice
= rb_current_time();
503 rb_write(F
, "ERROR :All connections in use\r\n", 32);
505 /* Re-register a new IO request for the next accept .. */
509 aconf
= find_dline(addr
, addr
->sa_family
);
510 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
513 /* Do an initial check we aren't connecting too fast or with too many
517 ServerStats
.is_ref
++;
519 if(ConfigFileEntry
.dline_with_reason
)
521 len
= rb_snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
522 if (len
>= (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 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
557 ilog_error("creating SSL/TLS socket pairs");
561 ctl
= start_ssld_accept(F
, xF
[1], rb_get_fd(xF
[0])); /* this will close F for us */
562 add_connection(listener
, xF
[0], addr
, laddr
, ctl
);
566 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
568 struct Listener
*listener
= data
;
569 struct rb_sockaddr_storage lip
;
570 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
574 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
576 /* this can fail if the connection disappeared in the meantime */
582 accept_ssld(F
, addr
, (struct sockaddr
*)&lip
, listener
);
584 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
, NULL
);