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
8 * Copyright (C) 2021 Ariadne Conill
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include "ircd_defs.h"
35 #include "s_newconf.h"
47 static rb_dlink_list listener_list
= {};
48 static int accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
49 static void accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
);
50 static SSL_OPEN_CB accept_sslcallback
;
52 static struct Listener
*
53 make_listener(struct rb_sockaddr_storage
*addr
)
55 struct Listener
*listener
= (struct Listener
*) rb_malloc(sizeof(struct Listener
));
56 s_assert(0 != listener
);
57 listener
->name
= me
.name
;
60 memcpy(&listener
->addr
, addr
, sizeof(listener
->addr
));
65 free_listener(struct Listener
*listener
)
67 s_assert(NULL
!= listener
);
71 rb_dlinkDelete(&listener
->lnode
, &listener_list
);
75 #define PORTNAMELEN 6 /* ":31337" */
78 * get_listener_port - return displayable listener port
81 get_listener_port(const struct Listener
*listener
)
83 return ntohs(GET_SS_PORT(&listener
->addr
[0]));
87 * get_listener_name - return displayable listener name and port
90 get_listener_name(const struct Listener
*listener
)
92 static char buf
[BUFSIZE
];
94 snprintf(buf
, sizeof(buf
), "%s[%s/%u]",
95 me
.name
, listener
->name
, get_listener_port(listener
));
100 * show_ports - send port listing to a client
101 * inputs - pointer to client to show ports to
103 * side effects - show ports
106 show_ports(struct Client
*source_p
)
110 RB_DLINK_FOREACH(n
, listener_list
.head
)
112 struct Listener
*listener
= n
->data
;
114 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
115 form_str(RPL_STATSPLINE
), 'P',
116 get_listener_port(listener
),
117 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
118 listener
->ref_count
, (listener
->active
) ? "active" : "disabled",
119 listener
->sctp
? " sctp" : " tcp",
120 listener
->ssl
? " ssl" : "");
125 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
126 * bind it to the port given in 'port' and listen to it
127 * returns true (1) if successful false (0) on error.
131 inetport(struct Listener
*listener
)
137 if (listener
->sctp
) {
139 /* only AF_INET6 sockets can have both AF_INET and AF_INET6 addresses */
140 F
= rb_socket(AF_INET6
, SOCK_STREAM
, IPPROTO_SCTP
, "Listener socket");
145 F
= rb_socket(GET_SS_FAMILY(&listener
->addr
[0]), SOCK_STREAM
, IPPROTO_TCP
, "Listener socket");
148 memset(listener
->vhost
, 0, sizeof(listener
->vhost
));
150 if (GET_SS_FAMILY(&listener
->addr
[0]) == AF_INET6
) {
151 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
[0];
152 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
153 } else if (GET_SS_FAMILY(&listener
->addr
[0]) == AF_INET
) {
154 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
[0];
155 rb_inet_ntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
158 if (GET_SS_FAMILY(&listener
->addr
[1]) == AF_INET6
) {
159 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
[1];
160 strncat(listener
->vhost
, "&", sizeof(listener
->vhost
));
161 rb_inet_ntop(AF_INET6
, &in6
->sin6_addr
, &listener
->vhost
[strlen(listener
->vhost
)], sizeof(listener
->vhost
) - strlen(listener
->vhost
));
162 } else if (GET_SS_FAMILY(&listener
->addr
[1]) == AF_INET
) {
163 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
[1];
164 strncat(listener
->vhost
, "&", sizeof(listener
->vhost
));
165 rb_inet_ntop(AF_INET
, &in
->sin_addr
, &listener
->vhost
[strlen(listener
->vhost
)], sizeof(listener
->vhost
) - strlen(listener
->vhost
));
168 if (listener
->vhost
[0] != '\0') {
169 listener
->name
= listener
->vhost
;
173 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
174 "Cannot open socket for listener on %s port %d",
175 listener
->sctp
? "SCTP" : "TCP",
176 get_listener_port(listener
));
177 ilog(L_MAIN
, "Cannot open socket for %s listener %s",
178 listener
->sctp
? "SCTP" : "TCP",
179 get_listener_name(listener
));
183 if (listener
->sctp
) {
184 ret
= rb_sctp_bindx(F
, listener
->addr
, ARRAY_SIZE(listener
->addr
));
186 ret
= rb_bind(F
, (struct sockaddr
*)&listener
->addr
[0]);
190 errstr
= strerror(rb_get_sockerr(F
));
191 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
192 "Cannot bind for listener on %s port %d: %s",
193 listener
->sctp
? "SCTP" : "TCP",
194 get_listener_port(listener
), errstr
);
195 ilog(L_MAIN
, "Cannot bind for %s listener %s: %s",
196 listener
->sctp
? "SCTP" : "TCP",
197 get_listener_name(listener
), errstr
);
202 if(rb_listen(F
, SOMAXCONN
, listener
->defer_accept
))
204 errstr
= strerror(rb_get_sockerr(F
));
205 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
206 "Cannot listen() for listener on %s port %d: %s",
207 listener
->sctp
? "SCTP" : "TCP",
208 get_listener_port(listener
), errstr
);
209 ilog(L_MAIN
, "Cannot listen() for %s listener %s: %s",
210 listener
->sctp
? "SCTP" : "TCP",
211 get_listener_name(listener
), errstr
);
218 rb_accept_tcp(listener
->F
, accept_precallback
, accept_callback
, listener
);
222 static struct Listener
*
223 find_listener(struct rb_sockaddr_storage
*addr
, int sctp
)
226 struct Listener
*last_closed
= NULL
;
228 RB_DLINK_FOREACH(n
, listener_list
.head
) {
229 struct Listener
*listener
= n
->data
;
231 if (listener
->sctp
!= sctp
)
234 for (int i
= 0; i
< ARRAY_SIZE(listener
->addr
); i
++) {
235 if (GET_SS_FAMILY(&addr
[i
]) != GET_SS_FAMILY(&listener
->addr
[i
]))
238 switch (GET_SS_FAMILY(&addr
[i
])) {
241 struct sockaddr_in
*in4
= (struct sockaddr_in
*)&addr
[i
];
242 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
[i
];
243 if(in4
->sin_addr
.s_addr
!= lin4
->sin_addr
.s_addr
||
244 in4
->sin_port
!= lin4
->sin_port
)
253 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&addr
[i
];
254 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
[i
];
255 if (!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) ||
256 in6
->sin6_port
!= lin6
->sin6_port
)
268 if (listener
->F
== NULL
) {
269 last_closed
= listener
;
282 * add_tcp_listener- create a new listener
283 * port - the port number to listen on
284 * vhost_ip - if non-null must contain a valid IP address string in
285 * the format "255.255.255.255"
288 add_tcp_listener(int port
, const char *vhost_ip
, int family
, int ssl
, int defer_accept
, int wsock
)
290 struct Listener
*listener
;
291 struct rb_sockaddr_storage vaddr
[ARRAY_SIZE(listener
->addr
)];
294 * if no port in conf line, don't bother
298 memset(&vaddr
, 0, sizeof(vaddr
));
299 SET_SS_FAMILY(&vaddr
[0], AF_UNSPEC
);
300 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_storage
));
301 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
302 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
304 if (vhost_ip
!= NULL
) {
305 if (family
== AF_INET
) {
306 if (rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
[0])->sin_addr
) <= 0)
309 if (rb_inet_pton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
) <= 0)
315 ((struct sockaddr_in
*)&vaddr
[0])->sin_addr
.s_addr
= INADDR_ANY
;
318 memcpy(&((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
326 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in
));
327 SET_SS_FAMILY(&vaddr
[0], AF_INET
);
328 SET_SS_PORT(&vaddr
[0], htons(port
));
331 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in6
));
332 SET_SS_FAMILY(&vaddr
[0], AF_INET6
);
333 SET_SS_PORT(&vaddr
[0], htons(port
));
338 if ((listener
= find_listener(vaddr
, 0))) {
339 if (listener
->F
!= NULL
)
342 listener
= make_listener(vaddr
);
343 rb_dlinkAdd(listener
, &listener
->lnode
, &listener_list
);
348 listener
->defer_accept
= defer_accept
;
350 listener
->wsock
= wsock
;
352 if (inetport(listener
)) {
353 listener
->active
= 1;
355 close_listener(listener
);
360 * add_sctp_listener- create a new listener
361 * port - the port number to listen on
362 * vhost_ip1/2 - if non-null must contain a valid IP address string
365 add_sctp_listener(int port
, const char *vhost_ip1
, const char *vhost_ip2
, int ssl
, int wsock
)
367 struct Listener
*listener
;
368 struct rb_sockaddr_storage vaddr
[ARRAY_SIZE(listener
->addr
)];
371 * if no port in conf line, don't bother
375 memset(&vaddr
, 0, sizeof(vaddr
));
377 if (vhost_ip1
!= NULL
) {
378 if (rb_inet_pton_sock(vhost_ip1
, &vaddr
[0]) <= 0)
381 if (vhost_ip2
!= NULL
) {
382 if (rb_inet_pton_sock(vhost_ip2
, &vaddr
[1]) <= 0)
385 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
386 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
389 if (GET_SS_FAMILY(&vaddr
[0]) == AF_INET
&& GET_SS_FAMILY(&vaddr
[1]) == AF_INET6
) {
390 /* always put INET6 first */
391 struct rb_sockaddr_storage tmp
;
397 memcpy(&((struct sockaddr_in6
*)&vaddr
[0])->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
398 SET_SS_FAMILY(&vaddr
[0], AF_INET6
);
399 SET_SS_LEN(&vaddr
[0], sizeof(struct sockaddr_in6
));
401 SET_SS_FAMILY(&vaddr
[1], AF_UNSPEC
);
402 SET_SS_LEN(&vaddr
[1], sizeof(struct sockaddr_storage
));
405 SET_SS_PORT(&vaddr
[0], htons(port
));
406 SET_SS_PORT(&vaddr
[1], htons(port
));
408 if ((listener
= find_listener(vaddr
, 1))) {
409 if(listener
->F
!= NULL
)
412 listener
= make_listener(vaddr
);
413 rb_dlinkAdd(listener
, &listener
->lnode
, &listener_list
);
418 listener
->defer_accept
= 0;
420 listener
->wsock
= wsock
;
422 if (inetport(listener
)) {
423 listener
->active
= 1;
425 close_listener(listener
);
430 * close_listener - close a single listener
433 close_listener(struct Listener
*listener
)
435 s_assert(listener
!= NULL
);
438 if(listener
->F
!= NULL
)
440 rb_close(listener
->F
);
444 listener
->active
= 0;
446 if(listener
->ref_count
)
449 free_listener(listener
);
453 * close_listeners - close and free all listeners that are not being used
456 close_listeners(void)
458 rb_dlink_node
*n
, *tn
;
461 * close all 'extra' listening ports we have
463 RB_DLINK_FOREACH_SAFE(n
, tn
, listener_list
.head
)
465 struct Listener
*listener
= n
->data
;
467 close_listener(listener
);
470 rb_close_pending_fds();
474 * add_connection - creates a client which has just connected to us on
475 * the given fd. The sockhost field is initialized with the ip# of the host.
476 * The client is sent to the auth module for verification, and not put in
477 * any client list yet.
480 add_connection(struct Listener
*listener
, rb_fde_t
*F
, struct sockaddr
*sai
, struct sockaddr
*lai
)
482 struct Client
*new_client
;
484 s_assert(NULL
!= listener
);
487 * get the client socket name from the socket
488 * the client has already been checked out in accept_connection
490 new_client
= make_client(NULL
);
491 new_client
->localClient
->F
= F
;
493 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct rb_sockaddr_storage
));
494 memcpy(&new_client
->preClient
->lip
, lai
, sizeof(struct rb_sockaddr_storage
));
497 * copy address to 'sockhost' as a string, copy it to host too
498 * so we have something valid to put into error messages...
500 rb_inet_ntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
501 sizeof(new_client
->sockhost
));
503 rb_strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
505 if (listener
->sctp
) {
512 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming ssld Connection") == -1)
514 SetIOError(new_client
);
515 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
518 new_client
->localClient
->ssl_callback
= accept_sslcallback
;
520 new_client
->localClient
->ssl_ctl
= start_ssld_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
521 if(new_client
->localClient
->ssl_ctl
== NULL
)
523 SetIOError(new_client
);
524 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
528 new_client
->localClient
->F
= F
;
530 SetSecure(new_client
);
534 struct ConfItem
*aconf
;
535 aconf
= find_conf_by_address(NULL
, NULL
, NULL
, sai
, CONF_SECURE
| 1, sai
->sa_family
, NULL
, NULL
);
538 SetSecure(new_client
);
544 if(rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF
[0], &xF
[1], "Incoming wsockd Connection") == -1)
546 SetIOError(new_client
);
547 exit_client(new_client
, new_client
, new_client
, "Fatal Error");
550 new_client
->localClient
->ws_ctl
= start_wsockd_accept(F
, xF
[1], connid_get(new_client
)); /* this will close F for us */
551 if(new_client
->localClient
->ws_ctl
== NULL
)
553 SetIOError(new_client
);
554 exit_client(new_client
, new_client
, new_client
, "Service Unavailable");
558 new_client
->localClient
->F
= F
;
561 new_client
->localClient
->listener
= listener
;
563 ++listener
->ref_count
;
565 authd_initiate_client(new_client
, defer
);
569 accept_sslcallback(struct Client
*client_p
, int status
)
571 authd_deferred_client(client_p
);
572 return 0; /* use default handler if status != RB_OK */
575 static const char *toofast
= "ERROR :Reconnecting too fast, throttled.\r\n";
578 accept_precallback(rb_fde_t
*F
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
580 struct Listener
*listener
= (struct Listener
*)data
;
582 struct ConfItem
*aconf
;
583 static time_t last_oper_notice
= 0;
586 if(listener
->ssl
&& (!ircd_ssl_ok
|| !get_ssld_count()))
592 if((maxconnections
- 10) < rb_get_fd(F
)) /* XXX this is kinda bogus */
594 ++ServerStats
.is_ref
;
596 * slow down the whining to opers bit
598 if((last_oper_notice
+ 20) <= rb_current_time())
600 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
601 "All connections in use. (%s)",
602 get_listener_name(listener
));
603 last_oper_notice
= rb_current_time();
606 rb_write(F
, "ERROR :All connections in use\r\n", 31);
611 aconf
= find_dline(addr
, addr
->sa_family
);
612 if(aconf
!= NULL
&& (aconf
->status
& CONF_EXEMPTDLINE
))
615 /* Do an initial check we aren't connecting too fast or with too many
619 ServerStats
.is_ref
++;
621 if(ConfigFileEntry
.dline_with_reason
)
623 len
= snprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf
));
624 if (len
>= (int)(sizeof(buf
)-1))
626 buf
[sizeof(buf
) - 3] = '\r';
627 buf
[sizeof(buf
) - 2] = '\n';
628 buf
[sizeof(buf
) - 1] = '\0';
632 strcpy(buf
, "ERROR :You have been D-lined.\r\n");
634 rb_write(F
, buf
, strlen(buf
));
639 if(check_reject(F
, addr
)) {
640 /* Reject the connection without closing the socket
641 * because it is now on the delay_exit list. */
645 if(throttle_add(addr
))
647 rb_write(F
, toofast
, strlen(toofast
));
656 accept_callback(rb_fde_t
*F
, int status
, struct sockaddr
*addr
, rb_socklen_t addrlen
, void *data
)
658 struct Listener
*listener
= data
;
659 struct rb_sockaddr_storage lip
;
660 unsigned int locallen
= sizeof(struct rb_sockaddr_storage
);
664 if(getsockname(rb_get_fd(F
), (struct sockaddr
*) &lip
, &locallen
) < 0)
666 /* this can fail if the connection disappeared in the meantime */
671 add_connection(listener
, F
, addr
, (struct sockaddr
*)&lip
);