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"
46 #if defined(NO_IN6ADDR_ANY)
47 static const struct in6_addr in6addr_any
=
48 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
51 static struct Listener
*ListenerPollList
= NULL
;
52 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
53 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
54 static SSL_OPEN_CB accept_sslcallback
;
56 static struct Listener
*
57 make_listener(struct rb_sockaddr_storage
*addr
)
59 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
60 s_assert(0 != listener
);
61 listener
->name
= me
.name
;
64 memcpy(&listener
->addr
, addr
, sizeof(struct rb_sockaddr_storage
));
65 listener
->next
= NULL
;
70 free_listener(struct Listener
*listener
)
72 s_assert(NULL
!= listener
);
76 * remove from listener list
78 if(listener
== ListenerPollList
)
79 ListenerPollList
= listener
->next
;
82 struct Listener
*prev
= ListenerPollList
;
83 for (; prev
; prev
= prev
->next
)
85 if(listener
== prev
->next
)
87 prev
->next
= listener
->next
;
97 #define PORTNAMELEN 6 /* ":31337" */
100 * get_listener_port - return displayable listener port
103 get_listener_port(const struct Listener
*listener
)
105 return ntohs(GET_SS_PORT(&listener
->addr
));
109 * get_listener_name - return displayable listener name and port
110 * returns "host.foo.org:6667" for a given listener
113 get_listener_name(const struct Listener
*listener
)
115 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
117 s_assert(NULL
!= listener
);
121 snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
122 me
.name
, listener
->name
, get_listener_port(listener
));
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',
141 get_listener_port(listener
),
142 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
143 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
144 listener
->ssl
? " ssl" : "");
149 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
150 * bind it to the port given in 'port' and listen to it
151 * returns true (1) if successful false (0) on error.
155 inetport(struct Listener
*listener
)
162 * At first, open a new socket
165 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
), SOCK_STREAM
, 0, "Listener socket");
167 if(GET_SS_FAMILY(&listener
->addr
) == AF_INET6
)
169 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
170 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
172 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
173 listener
->name
= listener
->vhost
;
176 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
177 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
179 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
180 listener
->name
= listener
->vhost
;
186 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
187 "Cannot open socket for listener on port %d",
188 get_listener_port(listener
));
189 ilog(L_MAIN
, "Cannot open socket for listener %s",
190 get_listener_name(listener
));
193 else if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus*/
195 ilog_error("no more connections left for listener");
196 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
197 "No more connections left for listener on port %d",
198 get_listener_port(listener
));
199 ilog(L_MAIN
, "No more connections left for listener %s",
200 get_listener_name(listener
));
206 * XXX - we don't want to do all this crap for a listener
207 * set_sock_opts(listener);
209 * FIXME - doesn't this belong in librb? --Elizafox
211 if(setsockopt(rb_get_fd(F
), SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
213 errstr
= strerror(rb_get_sockerr(F
));
214 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
215 "Cannot set SO_REUSEADDR for listener on port %d: %s",
216 get_listener_port(listener
), errstr
);
217 ilog(L_MAIN
, "Cannot set SO_REUSEADDR for listener %s: %s",
218 get_listener_name(listener
), errstr
);
223 /* FIXME - doesn't this belong in librb? --Elizafox */
224 if(bind(rb_get_fd(F
), (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(&listener
->addr
)))
226 errstr
= strerror(rb_get_sockerr(F
));
227 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
228 "Cannot bind for listener on port %d: %s",
229 get_listener_port(listener
), errstr
);
230 ilog(L_MAIN
, "Cannot bind for listener %s: %s",
231 get_listener_name(listener
), errstr
);
236 if(rb_listen(F
, SOMAXCONN
, listener
->defer_accept
))
238 errstr
= strerror(rb_get_sockerr(F
));
239 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
240 "Cannot listen() for listener on port %d: %s",
241 get_listener_port(listener
), errstr
);
242 ilog(L_MAIN
, "Cannot listen() for listener %s: %s",
243 get_listener_name(listener
), errstr
);
250 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
254 static struct Listener
*
255 find_listener(struct rb_sockaddr_storage
*addr
)
257 struct Listener
*listener
= NULL
;
258 struct Listener
*last_closed
= NULL
;
260 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
262 if(GET_SS_FAMILY(addr
) != GET_SS_FAMILY(&listener
->addr
))
265 switch(GET_SS_FAMILY(addr
))
269 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
270 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
271 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
272 in4
->sin_port
== lin4
->sin_port
)
274 if(listener
->F
== NULL
)
275 last_closed
= listener
;
283 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
284 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
285 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
286 in6
->sin6_port
== lin6
->sin6_port
)
288 if(listener
->F
== NULL
)
289 last_closed
= listener
;
306 * add_listener- create a new listener
307 * port - the port number to listen on
308 * vhost_ip - if non-null must contain a valid IP address string in
309 * the format "255.255.255.255"
312 add_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
, int wsock
)
314 struct Listener
*listener
;
315 struct rb_sockaddr_storage vaddr
;
318 * if no port in conf line, don't bother
322 memset(&vaddr
, 0, sizeof(vaddr
));
323 SET_SS_FAMILY(&vaddr
, family
);
327 if(family
== AF_INET
)
329 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
334 if(rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
343 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
346 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
355 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in
));
356 SET_SS_FAMILY(&vaddr
, AF_INET
);
357 SET_SS_PORT(&vaddr
, htons(port
));
360 SET_SS_LEN(&vaddr
, sizeof(struct sockaddr_in6
));
361 SET_SS_FAMILY(&vaddr
, AF_INET6
);
362 SET_SS_PORT(&vaddr
, htons(port
));
367 if((listener
= find_listener(&vaddr
)))
369 if(listener
->F
!= NULL
)
374 listener
= make_listener(&vaddr
);
375 listener
->next
= ListenerPollList
;
376 ListenerPollList
= listener
;
381 listener
->defer_accept
= defer_accept
;
382 listener
->wsock
= wsock
;
384 if(inetport(listener
))
385 listener
->active
= 1;
387 close_listener(listener
);
391 * close_listener - close a single listener
394 close_listener(struct Listener
*listener
)
396 s_assert(listener
!= NULL
);
399 if(listener
->F
!= NULL
)
401 rb_close(listener
->F
);
405 listener
->active
= 0;
407 if(listener
->ref_count
)
410 free_listener(listener
);
414 * close_listeners - close and free all listeners that are not being used
419 struct Listener
*listener
;
420 struct Listener
*listener_next
= 0;
422 * close all 'extra' listening ports we have
424 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
426 listener_next
= listener
->next
;
427 close_listener(listener
);
432 * add_connection - creates a client which has just connected to us on
433 * the given fd. The sockhost field is initialized with the ip# of the host.
434 * The client is sent to the auth module for verification, and not put in
435 * any client list yet.
438 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
440 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
);
449 new_client
->localClient
->F
= F
;
451 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
452 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
455 * copy address to 'sockhost' as a string, copy it to host too
456 * so we have something valid to put into error messages...
458 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
459 sizeof(new_client
->sockhost
));
461 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
466 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
468 SetIOError(new_client
);
469 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
472 new_client
->localClient
->ssl_callback
= accept_sslcallback
;
474 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
475 if(new_client
->localClient
->ssl_ctl
== NULL
)
477 SetIOError(new_client
);
478 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
482 new_client
->localClient
->F
= F
;
489 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming wsockd Connection") == -1)
491 SetIOError(new_client
);
492 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
495 new_client
->localClient
->ws_ctl
= start_wsockd_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
496 if(new_client
->localClient
->ws_ctl
== NULL
)
498 SetIOError(new_client
);
499 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
503 new_client
->localClient
->F
= F
;
506 new_client
->localClient
->listener
= listener
;
508 ++listener
->ref_count
;
510 authd_initiate_client(new_client
, defer
);
514 accept_sslcallback(struct Client
*client_p
, int status
)
516 authd_deferred_client(client_p
);
517 return 0; /* use default handler if status != RB_OK */
520 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
523 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
525 struct Listener
*listener
= (struct Listener
*)data
;
527 struct ConfItem
*aconf
;
528 static time_t last_oper_notice
= 0;
531 if(listener
->ssl
&& (!ircd_ssl_ok
|| !get_ssld_count()))
537 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
539 ++ServerStats
.is_ref
;
541 * slow down the whining to opers bit
543 if((last_oper_notice
+ 20) <= rb_current_time())
545 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
546 "All connections in use. (%s)",
547 get_listener_name(listener
));
548 last_oper_notice
= rb_current_time();
551 rb_write(F
, "ERROR :All connections in use\r\n", 31);
556 aconf
= find_dline(addr
, addr
->sa_family
);
557 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
560 /* Do an initial check we aren't connecting too fast or with too many
564 ServerStats
.is_ref
++;
566 if(ConfigFileEntry
.dline_with_reason
)
568 len
= snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
569 if (len
>= (int)(sizeof(buf
)-1))
571 buf
[sizeof(buf
) - 3] = '\r';
572 buf
[sizeof(buf
) - 2] = '\n';
573 buf
[sizeof(buf
) - 1] = '\0';
577 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
579 rb_write(F
, buf
, strlen(buf
));
584 if(check_reject(F
, addr
)) {
585 /* Reject the connection without closing the socket
586 * because it is now on the delay_exit list. */
590 if(throttle_add(addr
))
592 rb_write(F
, toofast
, strlen(toofast
));
601 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
603 struct Listener
*listener
= data
;
604 struct rb_sockaddr_storage lip
;
605 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
609 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
611 /* this can fail if the connection disappeared in the meantime */
616 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);