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
29 #include "irc_string.h"
30 #include "sprintf_irc.h"
32 #include "ircd_defs.h"
36 #include "s_newconf.h"
44 #define INADDR_NONE ((unsigned int) 0xffffffff)
47 #if defined(NO_IN6ADDR_ANY) && defined(IPV6)
48 static const struct in6_addr in6addr_any
=
49 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
52 static PF accept_connection
;
54 static listener_t
*ListenerPollList
= NULL
;
57 make_listener(struct irc_sockaddr_storage
*addr
)
59 listener_t
*listener
= (listener_t
*) MyMalloc(sizeof(listener_t
));
60 s_assert(0 != listener
);
62 listener
->name
= me
.name
;
64 memcpy(&listener
->addr
, addr
, sizeof(struct irc_sockaddr_storage
));
65 listener
->next
= NULL
;
70 free_listener(listener_t
*listener
)
72 s_assert(NULL
!= listener
);
76 * remove from listener list
78 if(listener
== ListenerPollList
)
79 ListenerPollList
= listener
->next
;
82 listener_t
*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_name - return displayable listener name and port
101 * returns "host.foo.org:6667" for a given listener
104 get_listener_name(const listener_t
*listener
)
106 static char buf
[HOSTLEN
+ HOSTLEN
+ PORTNAMELEN
+ 4];
109 s_assert(NULL
!= listener
);
114 if(listener
->addr
.ss_family
== AF_INET6
)
115 port
= ntohs(((const struct sockaddr_in6
*)&listener
->addr
)->sin6_port
);
118 port
= ntohs(((const struct sockaddr_in
*)&listener
->addr
)->sin_port
);
120 ircsnprintf(buf
, sizeof(buf
), "%s[%s/%u]", me
.name
, listener
->name
, port
);
125 * show_ports - send port listing to a client
126 * inputs - pointer to client to show ports to
128 * side effects - show ports
131 show_ports(struct Client
*source_p
)
133 listener_t
*listener
= 0;
135 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
137 sendto_one_numeric(source_p
, RPL_STATSPLINE
,
138 form_str(RPL_STATSPLINE
), 'P',
140 ntohs(listener
->addr
.ss_family
== AF_INET
? ((struct sockaddr_in
*)&listener
->addr
)->sin_port
:
141 ((struct sockaddr_in6
*)&listener
->addr
)->sin6_port
),
143 ntohs(((struct sockaddr_in
*)&listener
->addr
)->sin_port
),
145 IsOperAdmin(source_p
) ? listener
->name
: me
.name
,
146 listener
->ref_count
, (listener
->active
) ? "active" : "disabled");
151 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
152 * bind it to the port given in 'port' and listen to it
153 * returns true (1) if successful false (0) on error.
155 * If the operating system has a define for SOMAXCONN, use it, otherwise
156 * use RATBOX_SOMAXCONN
159 #undef RATBOX_SOMAXCONN
160 #define RATBOX_SOMAXCONN SOMAXCONN
164 inetport(listener_t
*listener
)
170 * At first, open a new socket
173 fd
= comm_socket(listener
->addr
.ss_family
, SOCK_STREAM
, 0, "Listener socket");
176 if(listener
->addr
.ss_family
== AF_INET6
)
178 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)&listener
->addr
;
179 if(!IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &in6addr_any
))
181 inetntop(AF_INET6
, &in6
->sin6_addr
, listener
->vhost
, sizeof(listener
->vhost
));
182 listener
->name
= listener
->vhost
;
187 struct sockaddr_in
*in
= (struct sockaddr_in
*)&listener
->addr
;
188 if(in
->sin_addr
.s_addr
!= INADDR_ANY
)
190 inetntop(AF_INET
, &in
->sin_addr
, listener
->vhost
, sizeof(listener
->vhost
));
191 listener
->name
= listener
->vhost
;
198 report_error("opening listener socket %s:%s",
199 get_listener_name(listener
),
200 get_listener_name(listener
), errno
);
203 else if((HARD_FDLIMIT
- 10) < fd
)
205 report_error("no more connections left for listener %s:%s",
206 get_listener_name(listener
),
207 get_listener_name(listener
), errno
);
212 * XXX - we don't want to do all this crap for a listener
213 * set_sock_opts(listener);
215 if(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &opt
, sizeof(opt
)))
217 report_error("setting SO_REUSEADDR for listener %s:%s",
218 get_listener_name(listener
),
219 get_listener_name(listener
), errno
);
225 * Bind a port to listen for new connections if port is non-null,
226 * else assume it is already open and try get something from it.
229 if(bind(fd
, (struct sockaddr
*) &listener
->addr
, GET_SS_LEN(listener
->addr
)))
231 report_error("binding listener socket %s:%s",
232 get_listener_name(listener
),
233 get_listener_name(listener
), errno
);
238 if(listen(fd
, RATBOX_SOMAXCONN
))
240 report_error("listen failed for %s:%s",
241 get_listener_name(listener
),
242 get_listener_name(listener
), errno
);
249 /* Listen completion events are READ events .. */
251 accept_connection(fd
, listener
);
256 find_listener(struct irc_sockaddr_storage
*addr
)
258 listener_t
*listener
= NULL
;
259 listener_t
*last_closed
= NULL
;
261 for (listener
= ListenerPollList
; listener
; listener
= listener
->next
)
263 if(addr
->ss_family
!= listener
->addr
.ss_family
)
266 switch(addr
->ss_family
)
270 struct sockaddr_in
*in4
= (struct sockaddr_in
*)addr
;
271 struct sockaddr_in
*lin4
= (struct sockaddr_in
*)&listener
->addr
;
272 if(in4
->sin_addr
.s_addr
== lin4
->sin_addr
.s_addr
&&
273 in4
->sin_port
== lin4
->sin_port
)
275 if(listener
->fd
== -1)
276 last_closed
= listener
;
285 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
286 struct sockaddr_in6
*lin6
=(struct sockaddr_in6
*)&listener
->addr
;
287 if(IN6_ARE_ADDR_EQUAL(&in6
->sin6_addr
, &lin6
->sin6_addr
) &&
288 in6
->sin6_port
== lin6
->sin6_port
)
290 if(listener
->fd
== -1)
291 last_closed
= listener
;
309 * add_listener- create a new listener
310 * port - the port number to listen on
311 * vhost_ip - if non-null must contain a valid IP address string in
312 * the format "255.255.255.255"
315 add_listener(int port
, const char *vhost_ip
, int family
)
317 listener_t
*listener
;
318 struct irc_sockaddr_storage vaddr
;
321 * if no port in conf line, don't bother
325 memset(&vaddr
, 0, sizeof(vaddr
));
326 vaddr
.ss_family
= family
;
330 if(family
== AF_INET
)
332 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in
*)&vaddr
)->sin_addr
) <= 0)
338 if(inetpton(family
, vhost_ip
, &((struct sockaddr_in6
*)&vaddr
)->sin6_addr
) <= 0)
348 ((struct sockaddr_in
*)&vaddr
)->sin_addr
.s_addr
= INADDR_ANY
;
352 memcpy(&((struct sockaddr_in6
*)&vaddr
)->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
));
362 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in
));
363 ((struct sockaddr_in
*)&vaddr
)->sin_port
= htons(port
);
367 SET_SS_LEN(vaddr
, sizeof(struct sockaddr_in6
));
368 ((struct sockaddr_in6
*)&vaddr
)->sin6_port
= htons(port
);
374 if((listener
= find_listener(&vaddr
)))
376 if(listener
->fd
> -1)
381 listener
= make_listener(&vaddr
);
382 listener
->next
= ListenerPollList
;
383 ListenerPollList
= listener
;
388 if(inetport(listener
))
389 listener
->active
= 1;
391 close_listener(listener
);
395 * close_listener - close a single listener
398 close_listener(listener_t
*listener
)
400 s_assert(listener
!= NULL
);
403 if(listener
->fd
>= 0)
405 comm_close(listener
->fd
);
409 listener
->active
= 0;
411 if(listener
->ref_count
)
414 free_listener(listener
);
418 * close_listeners - close and free all listeners that are not being used
423 listener_t
*listener
;
424 listener_t
*listener_next
= 0;
426 * close all 'extra' listening ports we have
428 for (listener
= ListenerPollList
; listener
; listener
= listener_next
)
430 listener_next
= listener
->next
;
431 close_listener(listener
);
435 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
438 * add_connection - creates a client which has just connected to us on
439 * the given fd. The sockhost field is initialized with the ip# of the host.
440 * The client is sent to the auth module for verification, and not put in
441 * any client list yet.
444 add_connection(listener_t
*listener
, int fd
, struct sockaddr
*sai
)
446 struct Client
*new_client
;
447 s_assert(NULL
!= listener
);
450 * get the client socket name from the socket
451 * the client has already been checked out in accept_connection
453 new_client
= make_client(NULL
);
455 memcpy(&new_client
->localClient
->ip
, sai
, sizeof(struct irc_sockaddr_storage
));
458 * copy address to 'sockhost' as a string, copy it to host too
459 * so we have something valid to put into error messages...
461 inetntop_sock((struct sockaddr
*)&new_client
->localClient
->ip
, new_client
->sockhost
,
462 sizeof(new_client
->sockhost
));
465 strlcpy(new_client
->host
, new_client
->sockhost
, sizeof(new_client
->host
));
468 if(new_client
->localClient
->ip
.ss_family
== AF_INET6
&& ConfigFileEntry
.dot_in_ip6_addr
== 1)
470 strlcat(new_client
->host
, ".", sizeof(new_client
->host
));
474 new_client
->localClient
->fd
= fd
;
476 new_client
->localClient
->listener
= listener
;
477 ++listener
->ref_count
;
479 if(check_reject(new_client
))
481 start_auth(new_client
);
486 accept_connection(int pfd
, void *data
)
488 static time_t last_oper_notice
= 0;
490 struct irc_sockaddr_storage sai
;
491 socklen_t addrlen
= sizeof(sai
);
493 listener_t
*listener
= data
;
494 struct ConfItem
*aconf
;
497 s_assert(listener
!= NULL
);
501 * There may be many reasons for error return, but
502 * in otherwise correctly working environment the
503 * probable cause is running out of file descriptors
504 * (EMFILE, ENFILE or others?). The man pages for
505 * accept don't seem to list these as possible,
506 * although it's obvious that it may happen here.
507 * Thus no specific errors are tested at this
508 * point, just assume that connections cannot
509 * be accepted until some old is closed first.
512 fd
= comm_accept(listener
->fd
, (struct sockaddr
*)&sai
, &addrlen
);
515 /* Re-register a new IO request for the next accept .. */
516 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
517 COMM_SELECT_READ
, accept_connection
, listener
, 0);
521 /* This needs to be done here, otherwise we break dlines */
522 mangle_mapped_sockaddr((struct sockaddr
*)&sai
);
525 * check for connection limit
527 if((MAXCONNECTIONS
- 10) < fd
)
529 ++ServerStats
->is_ref
;
531 * slow down the whining to opers bit
533 if((last_oper_notice
+ 20) <= CurrentTime
)
535 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
536 "All connections in use. (%s)",
537 get_listener_name(listener
));
538 last_oper_notice
= CurrentTime
;
541 write(fd
, "ERROR :All connections in use\r\n", 32);
543 /* Re-register a new IO request for the next accept .. */
544 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
545 COMM_SELECT_READ
, accept_connection
, listener
, 0);
549 /* Do an initial check we aren't connecting too fast or with too many
551 if((aconf
= conf_connect_allowed((struct sockaddr
*)&sai
, sai
.ss_family
)) != NULL
)
553 ServerStats
->is_ref
++;
555 if(ConfigFileEntry
.dline_with_reason
)
557 if (ircsnprintf(buf
, sizeof(buf
), "ERROR :*** Banned: %s\r\n", aconf
->passwd
) >= (sizeof(buf
)-1))
559 buf
[sizeof(buf
) - 3] = '\r';
560 buf
[sizeof(buf
) - 2] = '\n';
561 buf
[sizeof(buf
) - 1] = '\0';
565 ircsprintf(buf
, "ERROR :You have been D-lined.\r\n");
567 write(fd
, buf
, strlen(buf
));
570 /* Re-register a new IO request for the next accept .. */
571 comm_setselect(listener
->fd
, FDLIST_SERVICE
,
572 COMM_SELECT_READ
, accept_connection
, listener
, 0);
576 ServerStats
->is_ac
++;
577 add_connection(listener
, fd
, (struct sockaddr
*)&sai
);
579 /* Re-register a new IO request for the next accept .. */
580 comm_setselect(listener
->fd
, FDLIST_SERVICE
, COMM_SELECT_READ
,
581 accept_connection
, listener
, 0);