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
31 #include "ircd_defs.h"
34 #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 struct Listener
*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
);
60 static struct Listener
*
61 make_listener(struct rb_sockaddr_storage
*addr
)
63 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
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(struct Listener
*listener
)
76 s_assert(NULL
!= listener
);
80 * remove from listener list
82 if(listener
== ListenerPollList
)
83 ListenerPollList
= listener
->next
;
86 struct Listener
*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_port - return displayable listener port
107 get_listener_port(const struct Listener
*listener
)
109 return ntohs(GET_SS_PORT(&listener
->addr
));
113 * get_listener_name - return displayable listener name and port
114 * returns "host.foo.org:6667" for a given listener
117 get_listener_name(const struct Listener
*listener
)
119 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
121 s_assert(NULL
!= listener
);
125 snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
126 me
.name
, listener
->name
, get_listener_port(listener
));
131 * show_ports - send port listing to a client
132 * inputs - pointer to client to show ports to
134 * side effects - show ports
137 show_ports(struct Client
*source_p
)
139 struct Listener
*listener
= 0;
141 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
143 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
144 form_str(RPL_STATSPLINE
), 'P',
145 get_listener_port(listener
),
146 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
147 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
148 listener
->ssl
? " ssl" : "");
153 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
154 * bind it to the port given in 'port' and listen to it
155 * returns true (1) if successful false (0) on error.
157 * If the operating system has a define for SOMAXCONN, use it, otherwise
158 * use CHARYBDIS_SOMAXCONN
161 #undef CHARYBDIS_SOMAXCONN
162 #define CHARYBDIS_SOMAXCONN SOMAXCONN
166 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(GET_SS_FAMILY(&listener
->addr
) == 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 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
201 "Cannot open socket for listener on port %d",
202 get_listener_port(listener
));
203 ilog(L_MAIN
, "Cannot open socket for listener %s",
204 get_listener_name(listener
));
207 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
209 ilog_error("no more connections left for listener");
210 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
211 "No more connections left for listener on port %d",
212 get_listener_port(listener
));
213 ilog(L_MAIN
, "No more connections left for listener %s",
214 get_listener_name(listener
));
220 * XXX - we don't want to do all this crap for a listener
221 * set_sock_opts(listener);
223 * FIXME - doesn't this belong in librb? --Elizafox
225 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
227 errstr
= strerror(rb_get_sockerr(F
));
228 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
229 "Cannot set SO_REUSEADDR for listener on port %d: %s",
230 get_listener_port(listener
), errstr
);
231 ilog(L_MAIN
, "Cannot set SO_REUSEADDR for listener %s: %s",
232 get_listener_name(listener
), errstr
);
237 /* FIXME - doesn't this belong in librb? --Elizafox */
238 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
240 errstr
= strerror(rb_get_sockerr(F
));
241 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
242 "Cannot bind for listener on port %d: %s",
243 get_listener_port(listener
), errstr
);
244 ilog(L_MAIN
, "Cannot bind for listener %s: %s",
245 get_listener_name(listener
), errstr
);
250 if(rb_listen(F
, CHARYBDIS_SOMAXCONN
, listener
->defer_accept
))
252 errstr
= strerror(rb_get_sockerr(F
));
253 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
254 "Cannot listen() for listener on port %d: %s",
255 get_listener_port(listener
), errstr
);
256 ilog(L_MAIN
, "Cannot listen() for listener %s: %s",
257 get_listener_name(listener
), errstr
);
264 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
268 static struct Listener
*
269 find_listener(struct rb_sockaddr_storage
*addr
)
271 struct Listener
*listener
= NULL
;
272 struct Listener
*last_closed
= NULL
;
274 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
276 if(GET_SS_FAMILY(addr
) != GET_SS_FAMILY(&listener
->addr
))
279 switch(GET_SS_FAMILY(addr
))
283 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
284 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
285 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
286 in4
->sin_port
== lin4
->sin_port
)
288 if(listener
->F
== NULL
)
289 last_closed
= listener
;
298 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
299 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
300 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
301 in6
->sin6_port
== lin6
->sin6_port
)
303 if(listener
->F
== NULL
)
304 last_closed
= listener
;
322 * add_listener- create a new listener
323 * port - the port number to listen on
324 * vhost_ip - if non-null must contain a valid IP address string in
325 * the format "255.255.255.255"
328 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
, int wsock
)
330 struct Listener
*listener
;
331 struct rb_sockaddr_storage vaddr
;
334 * if no port in conf line, don't bother
338 memset(&vaddr
, 0, sizeof(vaddr
));
339 SET_SS_FAMILY(&vaddr
, family
);
343 if(family
== AF_INET
)
345 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
351 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
361 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
365 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
375 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
376 SET_SS_FAMILY(&vaddr
, AF_INET
);
377 SET_SS_PORT(&vaddr
, htons(port
));
381 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
382 SET_SS_FAMILY(&vaddr
, AF_INET6
);
383 SET_SS_PORT(&vaddr
, htons(port
));
389 if((listener
= find_listener(&vaddr
)))
391 if(listener
->F
!= NULL
)
396 listener
= make_listener(&vaddr
);
397 listener
->next
= ListenerPollList
;
398 ListenerPollList
= listener
;
403 listener
->defer_accept
= defer_accept
;
404 listener
->wsock
= wsock
;
406 if(inetport(listener
))
407 listener
->active
= 1;
409 close_listener(listener
);
413 * close_listener - close a single listener
416 close_listener(struct Listener
*listener
)
418 s_assert(listener
!= NULL
);
421 if(listener
->F
!= NULL
)
423 rb_close(listener
->F
);
427 listener
->active
= 0;
429 if(listener
->ref_count
)
432 free_listener(listener
);
436 * close_listeners - close and free all listeners that are not being used
441 struct Listener
*listener
;
442 struct Listener
*listener_next
= 0;
444 * close all 'extra' listening ports we have
446 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
448 listener_next
= listener
->next
;
449 close_listener(listener
);
453 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
456 * add_connection - creates a client which has just connected to us on
457 * the given fd. The sockhost field is initialized with the ip# of the host.
458 * The client is sent to the auth module for verification, and not put in
459 * any client list yet.
462 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
464 struct Client
*new_client
;
465 s_assert(NULL
!= listener
);
468 * get the client socket name from the socket
469 * the client has already been checked out in accept_connection
471 new_client
= make_client(NULL
);
476 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
478 free_client(new_client
);
481 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
482 if(new_client
->localClient
->ssl_ctl
== NULL
)
484 free_client(new_client
);
494 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming wsockd Connection") == -1)
496 free_client(new_client
);
499 new_client
->localClient
->ws_ctl
= start_wsockd_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
500 if(new_client
->localClient
->ws_ctl
== NULL
)
502 free_client(new_client
);
508 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
509 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
512 * copy address to 'sockhost' as a string, copy it to host too
513 * so we have something valid to put into error messages...
515 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
516 sizeof(new_client
->sockhost
));
519 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
521 new_client
->localClient
->F
= F
;
522 new_client
->localClient
->listener
= listener
;
524 ++listener
->ref_count
;
526 authd_initiate_client(new_client
);
529 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
532 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
534 struct Listener
*listener
= (struct Listener
*)data
;
536 struct ConfItem
*aconf
;
537 static time_t last_oper_notice
= 0;
540 if(listener
->ssl
&& (!ircd_ssl_ok
|| !get_ssld_count()))
546 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
548 ++ServerStats
.is_ref
;
550 * slow down the whining to opers bit
552 if((last_oper_notice
+ 20) <= rb_current_time())
554 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
555 "All connections in use. (%s)",
556 get_listener_name(listener
));
557 last_oper_notice
= rb_current_time();
560 rb_write(F
, "ERROR :All connections in use\r\n", 31);
565 aconf
= find_dline(addr
, addr
->sa_family
);
566 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
569 /* Do an initial check we aren't connecting too fast or with too many
573 ServerStats
.is_ref
++;
575 if(ConfigFileEntry
.dline_with_reason
)
577 len
= snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
578 if (len
>= (int)(sizeof(buf
)-1))
580 buf
[sizeof(buf
) - 3] = '\r';
581 buf
[sizeof(buf
) - 2] = '\n';
582 buf
[sizeof(buf
) - 1] = '\0';
586 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
588 rb_write(F
, buf
, strlen(buf
));
593 if(check_reject(F
, addr
))
596 if(throttle_add(addr
))
598 rb_write(F
, toofast
, strlen(toofast
));
607 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
609 struct Listener
*listener
= data
;
610 struct rb_sockaddr_storage lip
;
611 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
615 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
617 /* this can fail if the connection disappeared in the meantime */
622 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);