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 static struct Listener
*ListenerPollList
= NULL
;
47 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
48 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
49 static SSL_OPEN_CB accept_sslcallback
;
51 static struct Listener
*
52 make_listener(struct rb_sockaddr_storage
*addr
)
54 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
55 s_assert(0 != listener
);
56 listener
->name
= me
.name
;
59 memcpy(&listener
->addr
, addr
, sizeof(listener
->addr
));
60 listener
->next
= NULL
;
65 free_listener(struct Listener
*listener
)
67 s_assert(NULL
!= listener
);
71 * remove from listener list
73 if(listener
== ListenerPollList
)
74 ListenerPollList
= listener
->next
;
77 struct Listener
*prev
= ListenerPollList
;
78 for (; prev
; prev
= prev
->next
)
80 if(listener
== prev
->next
)
82 prev
->next
= listener
->next
;
92 #define PORTNAMELEN 6 /* ":31337" */
95 * get_listener_port - return displayable listener port
98 get_listener_port(const struct Listener
*listener
)
100 return ntohs(GET_SS_PORT(&listener
->addr
[0]));
104 * get_listener_name - return displayable listener name and port
107 get_listener_name(const struct Listener
*listener
)
109 static char buf
[BUFSIZE
];
111 snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
112 me
.name
, listener
->name
, get_listener_port(listener
));
117 * show_ports - send port listing to a client
118 * inputs - pointer to client to show ports to
120 * side effects - show ports
123 show_ports(struct Client
*source_p
)
125 struct Listener
*listener
= 0;
127 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
129 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
130 form_str(RPL_STATSPLINE
), 'P',
131 get_listener_port(listener
),
132 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
133 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
134 listener
->sctp
? " sctp" : " tcp",
135 listener
->ssl
? " ssl" : "");
140 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
141 * bind it to the port given in 'port' and listen to it
142 * returns true (1) if successful false (0) on error.
146 inetport(struct Listener
*listener
)
152 if (listener
->sctp
) {
154 /* only AF_INET6 sockets can have both AF_INET and AF_INET6 addresses */
155 F
= rb_socket(AF_INET6
, SOCK_STREAM
, IPPROTO_SCTP
, "Listener socket");
160 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
[0]), SOCK_STREAM
, IPPROTO_TCP
, "Listener socket");
163 memset(listener
->vhost
, 0, sizeof(listener
->vhost
));
165 if (GET_SS_FAMILY(&listener
->addr
[0]) == AF_INET6
) {
166 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
[0];
167 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
168 } else if (GET_SS_FAMILY(&listener
->addr
[0]) == AF_INET
) {
169 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
[0];
170 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
173 if (GET_SS_FAMILY(&listener
->addr
[1]) == AF_INET6
) {
174 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
[1];
175 strncat(listener
->vhost
, "&", sizeof(listener
->vhost
));
176 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, &listener
->vhost
[strlen(listener
->vhost
)], sizeof(listener
->vhost
) - strlen(listener
->vhost
));
177 } else if (GET_SS_FAMILY(&listener
->addr
[1]) == AF_INET
) {
178 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
[1];
179 strncat(listener
->vhost
, "&", sizeof(listener
->vhost
));
180 rb_inet_ntop(AF_INET
, &in
->sin_addr
, &listener
->vhost
[strlen(listener
->vhost
)], sizeof(listener
->vhost
) - strlen(listener
->vhost
));
183 if (listener
->vhost
[0] != '\0') {
184 listener
->name
= listener
->vhost
;
188 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
189 "Cannot open socket for listener on %s port %d",
190 listener
->sctp
? "SCTP" : "TCP",
191 get_listener_port(listener
));
192 ilog(L_MAIN
, "Cannot open socket for %s listener %s",
193 listener
->sctp
? "SCTP" : "TCP",
194 get_listener_name(listener
));
198 if (listener
->sctp
) {
199 ret
= rb_sctp_bindx(F
, listener
->addr
, ARRAY_SIZE(listener
->addr
));
201 ret
= rb_bind(F
, (struct sockaddr
*)&listener
->addr
[0]);
205 errstr
= strerror(rb_get_sockerr(F
));
206 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
207 "Cannot bind for listener on %s port %d: %s",
208 listener
->sctp
? "SCTP" : "TCP",
209 get_listener_port(listener
), errstr
);
210 ilog(L_MAIN
, "Cannot bind for %s listener %s: %s",
211 listener
->sctp
? "SCTP" : "TCP",
212 get_listener_name(listener
), errstr
);
217 if(rb_listen(F
, SOMAXCONN
, listener
->defer_accept
))
219 errstr
= strerror(rb_get_sockerr(F
));
220 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
221 "Cannot listen() for listener on %s port %d: %s",
222 listener
->sctp
? "SCTP" : "TCP",
223 get_listener_port(listener
), errstr
);
224 ilog(L_MAIN
, "Cannot listen() for %s listener %s: %s",
225 listener
->sctp
? "SCTP" : "TCP",
226 get_listener_name(listener
), errstr
);
233 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
237 static struct Listener
*
238 find_listener(struct rb_sockaddr_storage
*addr
, int sctp
)
240 struct Listener
*listener
= NULL
;
241 struct Listener
*last_closed
= NULL
;
243 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
) {
244 if (listener
->sctp
!= sctp
)
247 for (int i
= 0; i
< ARRAY_SIZE(listener
->addr
); i
++) {
248 if (GET_SS_FAMILY(&addr
[i
]) != GET_SS_FAMILY(&listener
->addr
[i
]))
251 switch (GET_SS_FAMILY(&addr
[i
])) {
254 struct sockaddr_in
*in4
= (struct sockaddr_in
*)&addr
[i
];
255 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
[i
];
256 if(in4
->sin_addr
.s_addr
!= lin4
->sin_addr
.s_addr
||
257 in4
->sin_port
!= lin4
->sin_port
)
266 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&addr
[i
];
267 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
[i
];
268 if (!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) ||
269 in6
->sin6_port
!= lin6
->sin6_port
)
281 if (listener
->F
== NULL
) {
282 last_closed
= listener
;
295 * add_tcp_listener- create a new listener
296 * port - the port number to listen on
297 * vhost_ip - if non-null must contain a valid IP address string in
298 * the format "255.255.255.255"
301 add_tcp_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
, int wsock
)
303 struct Listener
*listener
;
304 struct rb_sockaddr_storage vaddr
[ARRAY_SIZE(listener
->addr
)];
307 * if no port in conf line, don't bother
311 memset(&vaddr
, 0, sizeof(vaddr
));
312 SET_SS_FAMILY(&vaddr
[0], AF_UNSPEC
);
313 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_storage
));
314 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
315 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
317 if (vhost_ip
!= NULL
) {
318 if (family
== AF_INET
) {
319 if (rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
[0])->sin_addr
) <= 0)
322 if (rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
) <= 0)
328 ((struct sockaddr_in
*)&vaddr
[0])->sin_addr
.s_addr
= INADDR_ANY
;
331 memcpy(&((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
339 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in
));
340 SET_SS_FAMILY(&vaddr
[0], AF_INET
);
341 SET_SS_PORT(&vaddr
[0], htons(port
));
344 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in6
));
345 SET_SS_FAMILY(&vaddr
[0], AF_INET6
);
346 SET_SS_PORT(&vaddr
[0], htons(port
));
351 if ((listener
= find_listener(vaddr
, 0))) {
352 if (listener
->F
!= NULL
)
355 listener
= make_listener(vaddr
);
356 listener
->next
= ListenerPollList
;
357 ListenerPollList
= listener
;
362 listener
->defer_accept
= defer_accept
;
364 listener
->wsock
= wsock
;
366 if (inetport(listener
)) {
367 listener
->active
= 1;
369 close_listener(listener
);
374 * add_sctp_listener- create a new listener
375 * port - the port number to listen on
376 * vhost_ip1/2 - if non-null must contain a valid IP address string
379 add_sctp_listener(int port
, const char *vhost_ip1
, const char *vhost_ip2
, int ssl
, int wsock
)
381 struct Listener
*listener
;
382 struct rb_sockaddr_storage vaddr
[ARRAY_SIZE(listener
->addr
)];
385 * if no port in conf line, don't bother
389 memset(&vaddr
, 0, sizeof(vaddr
));
391 if (vhost_ip1
!= NULL
) {
392 if (rb_inet_pton_sock(vhost_ip1
, (struct sockaddr
*)&vaddr
[0]) <= 0)
395 if (vhost_ip2
!= NULL
) {
396 if (rb_inet_pton_sock(vhost_ip2
, (struct sockaddr
*)&vaddr
[1]) <= 0)
399 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
400 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
403 if (GET_SS_FAMILY(&vaddr
[0]) == AF_INET
&& GET_SS_FAMILY(&vaddr
[1]) == AF_INET6
) {
404 /* always put INET6 first */
405 struct rb_sockaddr_storage tmp
;
411 memcpy(&((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
412 SET_SS_FAMILY(&vaddr
[0], AF_INET6
);
413 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in6
));
415 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
416 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
419 SET_SS_PORT(&vaddr
[0], htons(port
));
420 SET_SS_PORT(&vaddr
[1], htons(port
));
422 if ((listener
= find_listener(vaddr
, 1))) {
423 if(listener
->F
!= NULL
)
426 listener
= make_listener(vaddr
);
427 listener
->next
= ListenerPollList
;
428 ListenerPollList
= listener
;
433 listener
->defer_accept
= 0;
435 listener
->wsock
= wsock
;
437 if (inetport(listener
)) {
438 listener
->active
= 1;
440 close_listener(listener
);
445 * close_listener - close a single listener
448 close_listener(struct Listener
*listener
)
450 s_assert(listener
!= NULL
);
453 if(listener
->F
!= NULL
)
455 rb_close(listener
->F
);
459 listener
->active
= 0;
461 if(listener
->ref_count
)
464 free_listener(listener
);
468 * close_listeners - close and free all listeners that are not being used
473 struct Listener
*listener
;
474 struct Listener
*listener_next
= 0;
476 * close all 'extra' listening ports we have
478 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
480 listener_next
= listener
->next
;
481 close_listener(listener
);
486 * add_connection - creates a client which has just connected to us on
487 * the given fd. The sockhost field is initialized with the ip# of the host.
488 * The client is sent to the auth module for verification, and not put in
489 * any client list yet.
492 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
494 struct Client
*new_client
;
496 s_assert(NULL
!= listener
);
499 * get the client socket name from the socket
500 * the client has already been checked out in accept_connection
502 new_client
= make_client(NULL
);
503 new_client
->localClient
->F
= F
;
505 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
506 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
509 * copy address to 'sockhost' as a string, copy it to host too
510 * so we have something valid to put into error messages...
512 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
513 sizeof(new_client
->sockhost
));
515 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
517 if (listener
->sctp
) {
524 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
526 SetIOError(new_client
);
527 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
530 new_client
->localClient
->ssl_callback
= accept_sslcallback
;
532 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
533 if(new_client
->localClient
->ssl_ctl
== NULL
)
535 SetIOError(new_client
);
536 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
540 new_client
->localClient
->F
= F
;
547 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming wsockd Connection") == -1)
549 SetIOError(new_client
);
550 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
553 new_client
->localClient
->ws_ctl
= start_wsockd_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
554 if(new_client
->localClient
->ws_ctl
== NULL
)
556 SetIOError(new_client
);
557 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
561 new_client
->localClient
->F
= F
;
564 new_client
->localClient
->listener
= listener
;
566 ++listener
->ref_count
;
568 authd_initiate_client(new_client
, defer
);
572 accept_sslcallback(struct Client
*client_p
, int status
)
574 authd_deferred_client(client_p
);
575 return 0; /* use default handler if status != RB_OK */
578 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
581 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
583 struct Listener
*listener
= (struct Listener
*)data
;
585 struct ConfItem
*aconf
;
586 static time_t last_oper_notice
= 0;
589 if(listener
->ssl
&& (!ircd_ssl_ok
|| !get_ssld_count()))
595 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
597 ++ServerStats
.is_ref
;
599 * slow down the whining to opers bit
601 if((last_oper_notice
+ 20) <= rb_current_time())
603 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
604 "All connections in use. (%s)",
605 get_listener_name(listener
));
606 last_oper_notice
= rb_current_time();
609 rb_write(F
, "ERROR :All connections in use\r\n", 31);
614 aconf
= find_dline(addr
, addr
->sa_family
);
615 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
618 /* Do an initial check we aren't connecting too fast or with too many
622 ServerStats
.is_ref
++;
624 if(ConfigFileEntry
.dline_with_reason
)
626 len
= snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
627 if (len
>= (int)(sizeof(buf
)-1))
629 buf
[sizeof(buf
) - 3] = '\r';
630 buf
[sizeof(buf
) - 2] = '\n';
631 buf
[sizeof(buf
) - 1] = '\0';
635 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
637 rb_write(F
, buf
, strlen(buf
));
642 if(check_reject(F
, addr
)) {
643 /* Reject the connection without closing the socket
644 * because it is now on the delay_exit list. */
648 if(throttle_add(addr
))
650 rb_write(F
, toofast
, strlen(toofast
));
659 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
661 struct Listener
*listener
= data
;
662 struct rb_sockaddr_storage lip
;
663 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
667 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
669 /* this can fail if the connection disappeared in the meantime */
674 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);