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"
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_port - return displayable listener port
106 get_listener_port(const struct Listener
*listener
)
109 if(listener
->addr
.ss_family
== AF_INET6
)
110 return ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
113 return ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
117 * get_listener_name - return displayable listener name and port
118 * returns "host.foo.org:6667" for a given listener
121 get_listener_name(const struct Listener
*listener
)
123 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
125 s_assert(NULL
!= listener
);
129 snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
130 me
.name
, listener
->name
, get_listener_port(listener
));
135 * show_ports - send port listing to a client
136 * inputs - pointer to client to show ports to
138 * side effects - show ports
141 show_ports(struct Client
*source_p
)
143 struct Listener
*listener
= 0;
145 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
147 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
148 form_str(RPL_STATSPLINE
), 'P',
149 get_listener_port(listener
),
150 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
151 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
152 listener
->ssl
? " ssl" : "");
157 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
158 * bind it to the port given in 'port' and listen to it
159 * returns true (1) if successful false (0) on error.
161 * If the operating system has a define for SOMAXCONN, use it, otherwise
162 * use CHARYBDIS_SOMAXCONN
165 #undef CHARYBDIS_SOMAXCONN
166 #define CHARYBDIS_SOMAXCONN SOMAXCONN
170 inetport(struct Listener
*listener
)
177 * At first, open a new socket
180 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
183 if(listener
->addr
.ss_family
== AF_INET6
)
185 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
186 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
188 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
189 listener
->name
= listener
->vhost
;
194 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
195 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
197 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
198 listener
->name
= listener
->vhost
;
204 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
205 "Cannot open socket for listener on port %d",
206 get_listener_port(listener
));
207 ilog(L_MAIN
, "Cannot open socket for listener %s",
208 get_listener_name(listener
));
211 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
213 ilog_error("no more connections left for listener");
214 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
215 "No more connections left for listener on port %d",
216 get_listener_port(listener
));
217 ilog(L_MAIN
, "No more connections left for listener %s",
218 get_listener_name(listener
));
224 * XXX - we don't want to do all this crap for a listener
225 * set_sock_opts(listener);
227 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
229 errstr
= strerror(rb_get_sockerr(F
));
230 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
231 "Cannot set SO_REUSEADDR for listener on port %d: %s",
232 get_listener_port(listener
), errstr
);
233 ilog(L_MAIN
, "Cannot set SO_REUSEADDR for listener %s: %s",
234 get_listener_name(listener
), errstr
);
240 * Bind a port to listen for new connections if port is non-null,
241 * else assume it is already open and try get something from it.
244 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
246 errstr
= strerror(rb_get_sockerr(F
));
247 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
248 "Cannot bind for listener on port %d: %s",
249 get_listener_port(listener
), errstr
);
250 ilog(L_MAIN
, "Cannot bind for listener %s: %s",
251 get_listener_name(listener
), errstr
);
256 if(rb_listen(F
, CHARYBDIS_SOMAXCONN
, listener
->defer_accept
))
258 errstr
= strerror(rb_get_sockerr(F
));
259 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
260 "Cannot listen() for listener on port %d: %s",
261 get_listener_port(listener
), errstr
);
262 ilog(L_MAIN
, "Cannot listen() for listener %s: %s",
263 get_listener_name(listener
), errstr
);
270 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
274 static struct Listener
*
275 find_listener(struct rb_sockaddr_storage
*addr
)
277 struct Listener
*listener
= NULL
;
278 struct Listener
*last_closed
= NULL
;
280 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
282 if(addr
->ss_family
!= listener
->addr
.ss_family
)
285 switch(addr
->ss_family
)
289 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
290 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
291 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
292 in4
->sin_port
== lin4
->sin_port
)
294 if(listener
->F
== NULL
)
295 last_closed
= listener
;
304 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
305 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
306 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
307 in6
->sin6_port
== lin6
->sin6_port
)
309 if(listener
->F
== NULL
)
310 last_closed
= listener
;
328 * add_listener- create a new listener
329 * port - the port number to listen on
330 * vhost_ip - if non-null must contain a valid IP address string in
331 * the format "255.255.255.255"
334 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
)
336 struct Listener
*listener
;
337 struct rb_sockaddr_storage vaddr
;
340 * if no port in conf line, don't bother
344 memset(&vaddr
, 0, sizeof(vaddr
));
345 vaddr
.ss_family
= family
;
349 if(family
== AF_INET
)
351 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
357 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
367 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
371 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
381 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
382 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
386 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
387 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
393 if((listener
= find_listener(&vaddr
)))
395 if(listener
->F
!= NULL
)
400 listener
= make_listener(&vaddr
);
401 listener
->next
= ListenerPollList
;
402 ListenerPollList
= listener
;
407 listener
->defer_accept
= defer_accept
;
409 if(inetport(listener
))
410 listener
->active
= 1;
412 close_listener(listener
);
416 * close_listener - close a single listener
419 close_listener(struct Listener
*listener
)
421 s_assert(listener
!= NULL
);
424 if(listener
->F
!= NULL
)
426 rb_close(listener
->F
);
430 listener
->active
= 0;
432 if(listener
->ref_count
)
435 free_listener(listener
);
439 * close_listeners - close and free all listeners that are not being used
444 struct Listener
*listener
;
445 struct Listener
*listener_next
= 0;
447 * close all 'extra' listening ports we have
449 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
451 listener_next
= listener
->next
;
452 close_listener(listener
);
456 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
459 * add_connection - creates a client which has just connected to us on
460 * the given fd. The sockhost field is initialized with the ip# of the host.
461 * The client is sent to the auth module for verification, and not put in
462 * any client list yet.
465 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
467 struct Client
*new_client
;
468 s_assert(NULL
!= listener
);
471 * get the client socket name from the socket
472 * the client has already been checked out in accept_connection
474 new_client
= make_client(NULL
);
479 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
481 free_client(new_client
);
484 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], new_client
->localClient
->connid
); /* this will close F for us */
485 if(new_client
->localClient
->ssl_ctl
== NULL
)
487 free_client(new_client
);
494 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
495 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
498 * copy address to 'sockhost' as a string, copy it to host too
499 * so we have something valid to put into error messages...
501 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
502 sizeof(new_client
->sockhost
));
505 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
507 new_client
->localClient
->F
= F
;
508 new_client
->localClient
->listener
= listener
;
510 ++listener
->ref_count
;
512 start_auth(new_client
);
515 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
518 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
520 struct Listener
*listener
= (struct Listener
*)data
;
522 struct ConfItem
*aconf
;
523 static time_t last_oper_notice
= 0;
526 if(listener
->ssl
&& (!ssl_ok
|| !get_ssld_count()))
532 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
534 ++ServerStats
.is_ref
;
536 * slow down the whining to opers bit
538 if((last_oper_notice
+ 20) <= rb_current_time())
540 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
541 "All connections in use. (%s)",
542 get_listener_name(listener
));
543 last_oper_notice
= rb_current_time();
546 rb_write(F
, "ERROR :All connections in use\r\n", 31);
551 aconf
= find_dline(addr
, addr
->sa_family
);
552 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
555 /* Do an initial check we aren't connecting too fast or with too many
559 ServerStats
.is_ref
++;
561 if(ConfigFileEntry
.dline_with_reason
)
563 len
= snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
564 if (len
>= (int)(sizeof(buf
)-1))
566 buf
[sizeof(buf
) - 3] = '\r';
567 buf
[sizeof(buf
) - 2] = '\n';
568 buf
[sizeof(buf
) - 1] = '\0';
572 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
574 rb_write(F
, buf
, strlen(buf
));
579 if(check_reject(F
, addr
))
582 if(throttle_add(addr
))
584 rb_write(F
, toofast
, strlen(toofast
));
593 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
595 struct Listener
*listener
= data
;
596 struct rb_sockaddr_storage lip
;
597 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
601 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
603 /* this can fail if the connection disappeared in the meantime */
608 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);