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"
49 #define INADDR_NONE ((unsigned int) 0xffffffff)
52 #if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6)
53 static const struct in6_addr in6addr_any
=
54 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
57 static struct Listener
*ListenerPollList
= NULL
;
58 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
59 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
61 static struct Listener
*
62 make_listener(struct rb_sockaddr_storage
*addr
)
64 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
65 s_assert(0 != listener
);
66 listener
->name
= me
.name
;
69 memcpy(&listener
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
70 listener
->next
= NULL
;
75 free_listener(struct Listener
*listener
)
77 s_assert(NULL
!= listener
);
81 * remove from listener list
83 if(listener
== ListenerPollList
)
84 ListenerPollList
= listener
->next
;
87 struct Listener
*prev
= ListenerPollList
;
88 for (; prev
; prev
= prev
->next
)
90 if(listener
== prev
->next
)
92 prev
->next
= listener
->next
;
102 #define PORTNAMELEN 6 /* ":31337" */
105 * get_listener_port - return displayable listener port
108 get_listener_port(const struct Listener
*listener
)
111 if(listener
->addr
.ss_family
== AF_INET6
)
112 return ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
115 return ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
119 * get_listener_name - return displayable listener name and port
120 * returns "host.foo.org:6667" for a given listener
123 get_listener_name(const struct Listener
*listener
)
125 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
127 s_assert(NULL
!= listener
);
131 rb_snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
132 me
.name
, listener
->name
, get_listener_port(listener
));
137 * show_ports - send port listing to a client
138 * inputs - pointer to client to show ports to
140 * side effects - show ports
143 show_ports(struct Client
*source_p
)
145 struct Listener
*listener
= 0;
147 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
149 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
150 form_str(RPL_STATSPLINE
), 'P',
151 get_listener_port(listener
),
152 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
153 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
154 listener
->ssl
? " ssl" : "");
159 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
160 * bind it to the port given in 'port' and listen to it
161 * returns true (1) if successful false (0) on error.
163 * If the operating system has a define for SOMAXCONN, use it, otherwise
164 * use RATBOX_SOMAXCONN
167 #undef RATBOX_SOMAXCONN
168 #define RATBOX_SOMAXCONN SOMAXCONN
172 inetport(struct Listener
*listener
)
179 * At first, open a new socket
182 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
185 if(listener
->addr
.ss_family
== AF_INET6
)
187 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
188 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
190 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
191 listener
->name
= listener
->vhost
;
196 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
197 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
199 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
200 listener
->name
= listener
->vhost
;
206 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
207 "Cannot open socket for listener on port %d",
208 get_listener_port(listener
));
209 ilog(L_MAIN
, "Cannot open socket for listener %s",
210 get_listener_name(listener
));
213 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
215 ilog_error("no more connections left for listener");
216 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
217 "No more connections left for listener on port %d",
218 get_listener_port(listener
));
219 ilog(L_MAIN
, "No more connections left for listener %s",
220 get_listener_name(listener
));
226 * XXX - we don't want to do all this crap for a listener
227 * set_sock_opts(listener);
229 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
231 errstr
= strerror(rb_get_sockerr(F
));
232 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
233 "Cannot set SO_REUSEADDR for listener on port %d: %s",
234 get_listener_port(listener
), errstr
);
235 ilog(L_MAIN
, "Cannot set SO_REUSEADDR for listener %s: %s",
236 get_listener_name(listener
), errstr
);
242 * Bind a port to listen for new connections if port is non-null,
243 * else assume it is already open and try get something from it.
246 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
248 errstr
= strerror(rb_get_sockerr(F
));
249 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
250 "Cannot bind for listener on port %d: %s",
251 get_listener_port(listener
), errstr
);
252 ilog(L_MAIN
, "Cannot bind for listener %s: %s",
253 get_listener_name(listener
), errstr
);
258 if(rb_listen(F
, RATBOX_SOMAXCONN
, listener
->defer_accept
))
260 errstr
= strerror(rb_get_sockerr(F
));
261 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
262 "Cannot listen() for listener on port %d: %s",
263 get_listener_port(listener
), errstr
);
264 ilog(L_MAIN
, "Cannot listen() for listener %s: %s",
265 get_listener_name(listener
), errstr
);
272 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
276 static struct Listener
*
277 find_listener(struct rb_sockaddr_storage
*addr
)
279 struct Listener
*listener
= NULL
;
280 struct Listener
*last_closed
= NULL
;
282 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
284 if(addr
->ss_family
!= listener
->addr
.ss_family
)
287 switch(addr
->ss_family
)
291 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
292 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
293 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
294 in4
->sin_port
== lin4
->sin_port
)
296 if(listener
->F
== NULL
)
297 last_closed
= listener
;
306 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
307 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
308 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
309 in6
->sin6_port
== lin6
->sin6_port
)
311 if(listener
->F
== NULL
)
312 last_closed
= listener
;
330 * add_listener- create a new listener
331 * port - the port number to listen on
332 * vhost_ip - if non-null must contain a valid IP address string in
333 * the format "255.255.255.255"
336 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
)
338 struct Listener
*listener
;
339 struct rb_sockaddr_storage vaddr
;
342 * if no port in conf line, don't bother
346 memset(&vaddr
, 0, sizeof(vaddr
));
347 vaddr
.ss_family
= family
;
351 if(family
== AF_INET
)
353 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
359 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
369 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
373 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
383 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
384 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
388 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
389 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
395 if((listener
= find_listener(&vaddr
)))
397 if(listener
->F
!= NULL
)
402 listener
= make_listener(&vaddr
);
403 listener
->next
= ListenerPollList
;
404 ListenerPollList
= listener
;
409 listener
->defer_accept
= defer_accept
;
411 if(inetport(listener
))
412 listener
->active
= 1;
414 close_listener(listener
);
418 * close_listener - close a single listener
421 close_listener(struct Listener
*listener
)
423 s_assert(listener
!= NULL
);
426 if(listener
->F
!= NULL
)
428 rb_close(listener
->F
);
432 listener
->active
= 0;
434 if(listener
->ref_count
)
437 free_listener(listener
);
441 * close_listeners - close and free all listeners that are not being used
446 struct Listener
*listener
;
447 struct Listener
*listener_next
= 0;
449 * close all 'extra' listening ports we have
451 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
453 listener_next
= listener
->next
;
454 close_listener(listener
);
458 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
461 * add_connection - creates a client which has just connected to us on
462 * the given fd. The sockhost field is initialized with the ip# of the host.
463 * The client is sent to the auth module for verification, and not put in
464 * any client list yet.
467 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
469 struct Client
*new_client
;
470 s_assert(NULL
!= listener
);
473 * get the client socket name from the socket
474 * the client has already been checked out in accept_connection
476 new_client
= make_client(NULL
);
481 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
483 free_client(new_client
);
486 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], new_client
->localClient
->connid
); /* this will close F for us */
487 if(new_client
->localClient
->ssl_ctl
== NULL
)
489 free_client(new_client
);
496 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
497 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
500 * copy address to 'sockhost' as a string, copy it to host too
501 * so we have something valid to put into error messages...
503 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
504 sizeof(new_client
->sockhost
));
507 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
509 new_client
->localClient
->F
= F
;
510 new_client
->localClient
->listener
= listener
;
512 ++listener
->ref_count
;
514 start_auth(new_client
);
517 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
520 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
522 struct Listener
*listener
= (struct Listener
*)data
;
524 struct ConfItem
*aconf
;
525 static time_t last_oper_notice
= 0;
528 if(listener
->ssl
&& (!ssl_ok
|| !get_ssld_count()))
534 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
536 ++ServerStats
.is_ref
;
538 * slow down the whining to opers bit
540 if((last_oper_notice
+ 20) <= rb_current_time())
542 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
543 "All connections in use. (%s)",
544 get_listener_name(listener
));
545 last_oper_notice
= rb_current_time();
548 rb_write(F
, "ERROR :All connections in use\r\n", 31);
553 aconf
= find_dline(addr
, addr
->sa_family
);
554 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
557 /* Do an initial check we aren't connecting too fast or with too many
561 ServerStats
.is_ref
++;
563 if(ConfigFileEntry
.dline_with_reason
)
565 len
= rb_snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
566 if (len
>= (int)(sizeof(buf
)-1))
568 buf
[sizeof(buf
) - 3] = '\r';
569 buf
[sizeof(buf
) - 2] = '\n';
570 buf
[sizeof(buf
) - 1] = '\0';
574 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
576 rb_write(F
, buf
, strlen(buf
));
581 if(check_reject(F
, addr
))
584 if(throttle_add(addr
))
586 rb_write(F
, toofast
, strlen(toofast
));
595 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
597 struct Listener
*listener
= data
;
598 struct rb_sockaddr_storage lip
;
599 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
603 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
605 /* this can fail if the connection disappeared in the meantime */
610 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);