1 /************************************************************************
2 * IRC - Internet Relay Chat, src/listener.c
3 * Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "ircd_alloc.h"
27 #include "ircd_events.h"
28 #include "ircd_features.h"
29 #include "ircd_osdep.h"
30 #include "ircd_reply.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
38 #include "sys.h" /* MAXCLIENTS */
47 #include <sys/socket.h>
48 #include <arpa/inet.h>
51 #define INADDR_NONE ((unsigned int) 0xffffffff)
54 struct Listener* ListenerPollList = 0;
56 static void accept_connection(struct Event* ev);
58 static struct Listener* make_listener(int port, struct in_addr addr)
60 struct Listener* listener =
61 (struct Listener*) MyMalloc(sizeof(struct Listener));
62 assert(0 != listener);
64 memset(listener, 0, sizeof(struct Listener));
67 listener->port = port;
68 listener->addr.s_addr = addr.s_addr;
70 #ifdef NULL_POINTER_NOT_ZERO
71 listener->next = NULL;
72 listener->conf = NULL;
77 static void free_listener(struct Listener* listener)
79 assert(0 != listener);
83 #define PORTNAMELEN 10 /* ":31337" */
86 * get_listener_name - return displayable listener name and port
87 * returns "host.foo.org:6667" for a given listener
89 const char* get_listener_name(const struct Listener* listener)
91 static char buf[HOSTLEN + PORTNAMELEN + 4];
92 assert(0 != listener);
93 ircd_snprintf(0, buf, sizeof(buf), "%s:%u", cli_name(&me), listener->port);
98 * count_listener_memory - count memory and listeners
100 void count_listener_memory(int* count_out, size_t* size_out)
104 assert(0 != count_out);
105 assert(0 != size_out);
106 for (l = ListenerPollList; l; l = l->next)
109 *size_out = count * sizeof(struct Listener);
113 * show_ports - send port listing to a client
114 * inputs - pointer to client to show ports to
116 * side effects - show ports
119 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
121 struct Listener* listener = 0;
125 for (listener = ListenerPollList; listener; listener = listener->next) {
126 if (port && port != listener->port)
128 flags[0] = (listener->server) ? 'S' : 'C';
129 if (listener->hidden) {
138 send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
139 flags, (listener->active) ? "active" : "disabled");
146 * inetport - create a listener socket in the AF_INET domain,
147 * bind it to the port given in 'port' and listen to it
148 * returns true (1) if successful false (0) on error.
150 * If the operating system has a define for SOMAXCONN, use it, otherwise
151 * use HYBRID_SOMAXCONN -Dianora
152 * NOTE: Do this in os_xxxx.c files
155 #define HYBRID_SOMAXCONN SOMAXCONN
157 #define HYBRID_SOMAXCONN 64
160 static int inetport(struct Listener* listener)
162 struct sockaddr_in sin;
166 * At first, open a new socket
168 if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
169 report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
172 else if (fd > MAXCLIENTS - 1) {
173 report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
178 if (!os_set_reuseaddr(fd)) {
179 report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
184 * Bind a port to listen for new connections if port is non-null,
185 * else assume it is already open and try get something from it.
187 memset(&sin, 0, sizeof(sin));
188 sin.sin_family = AF_INET;
189 sin.sin_addr = listener->addr;
190 sin.sin_port = htons(listener->port);
192 if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
193 report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
198 * Set the buffer sizes for the listener. Accepted connections
199 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
200 * The window size is set during the SYN ACK so setting it anywhere
201 * else has no effect whatsoever on the connection.
202 * NOTE: this must be set before listen is called
204 if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
205 report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
209 if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
210 report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
215 * XXX - this should always work, performance will suck if it doesn't
217 if (!os_set_nonblocking(fd)) {
218 report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
223 * Set the TOS bits - this is nonfatal if it doesn't stick.
225 if (!os_set_tos(fd,feature_int((listener->server)?FEAT_TOS_SERVER : FEAT_TOS_CLIENT))) {
226 report_error(TOS_ERROR_MSG, get_listener_name(listener), errno);
229 if (!socket_add(&listener->socket, accept_connection, (void*) listener,
230 SS_LISTENING, 0, fd)) {
231 /* Error should already have been reported to the logs */
242 * find_listener - find a listener in the list
244 * XXX - this function does N comparisons so if the list is huge
245 * we may want to do something else for this. (rehash and init use this)
247 static struct Listener* find_listener(int port, struct in_addr addr)
249 struct Listener* listener;
250 for (listener = ListenerPollList; listener; listener = listener->next) {
251 if (port == listener->port && addr.s_addr == listener->addr.s_addr)
258 * set_listener_mask - set the connection mask for this listener
260 static void set_listener_mask(struct Listener* listener, const char* mask)
265 assert(0 != listener);
267 if (EmptyString(mask) || 0 == strcmp(mask, "*")) {
268 listener->mask.s_addr = 0;
271 ad[0] = ad[1] = ad[2] = ad[3] = 0;
273 * do it this way because building ip# from separate values for each
274 * byte requires endian knowledge or some nasty messing. Also means
275 * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
277 sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
278 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1], ad[2],
280 listener->mask.s_addr = inet_addr(ipname);
284 * connection_allowed - spin through mask and addr passed to see if connect
285 * allowed on a listener, uses mask generated by set_listener_mask
287 static int connection_allowed(const char* addr, const char* mask)
290 for ( ; i > 0; --i) {
291 if (*mask && *addr != *mask)
301 * add_listener- create a new listener
302 * port - the port number to listen on
303 * vhost_ip - if non-null must contain a valid IP address string in
304 * the format "255.255.255.255"
306 void add_listener(int port, const char* vhost_ip, const char* mask,
307 int is_server, int is_hidden)
309 struct Listener* listener;
310 struct in_addr vaddr;
313 * if no port in conf line, don't bother
318 vaddr.s_addr = INADDR_ANY;
320 if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
321 vaddr.s_addr = inet_addr(vhost_ip);
322 if (INADDR_NONE == vaddr.s_addr)
326 if ((listener = find_listener(port, vaddr))) {
328 * set active flag and change connect mask here, it's the only thing
329 * that can change on a rehash
331 listener->active = 1;
332 set_listener_mask(listener, mask);
333 listener->hidden = is_hidden;
334 listener->server = is_server;
338 listener = make_listener(port, vaddr);
340 if (inetport(listener)) {
341 listener->active = 1;
342 set_listener_mask(listener, mask);
343 listener->hidden = is_hidden;
344 listener->server = is_server;
345 listener->next = ListenerPollList;
346 ListenerPollList = listener;
349 free_listener(listener);
353 * mark_listeners_closing - iterate through listeners and mark them as
356 void mark_listeners_closing(void)
358 struct Listener* listener;
359 for (listener = ListenerPollList; listener; listener = listener->next)
360 listener->active = 0;
364 * close_listener - close a single listener
366 void close_listener(struct Listener* listener)
368 assert(0 != listener);
370 * remove from listener list
372 if (listener == ListenerPollList)
373 ListenerPollList = listener->next;
375 struct Listener* prev = ListenerPollList;
376 for ( ; prev; prev = prev->next) {
377 if (listener == prev->next) {
378 prev->next = listener->next;
383 if (-1 < listener->fd)
385 socket_del(&listener->socket);
389 * close_listeners - close and free all listeners that are not being used
391 void close_listeners()
393 struct Listener* listener;
394 struct Listener* listener_next = 0;
396 * close all 'extra' listening ports we have
398 for (listener = ListenerPollList; listener; listener = listener_next) {
399 listener_next = listener->next;
400 if (0 == listener->active && 0 == listener->ref_count)
401 close_listener(listener);
405 void release_listener(struct Listener* listener)
407 assert(0 != listener);
408 assert(0 < listener->ref_count);
409 if (0 == --listener->ref_count && !listener->active)
410 close_listener(listener);
414 * accept_connection - accept a connection on a listener
416 static void accept_connection(struct Event* ev)
418 struct Listener* listener;
419 struct sockaddr_in addr = { 0 };
420 unsigned int addrlen = sizeof(struct sockaddr_in);
423 assert(0 != ev_socket(ev));
424 assert(0 != s_data(ev_socket(ev)));
426 listener = s_data(ev_socket(ev));
428 if (ev_type(ev) == ET_DESTROY) /* being destroyed */
429 free_listener(listener);
431 assert(ev_type(ev) == ET_ACCEPT);
433 listener->last_accept = CurrentTime;
435 * There may be many reasons for error return, but
436 * in otherwise correctly working environment the
437 * probable cause is running out of file descriptors
438 * (EMFILE, ENFILE or others?). The man pages for
439 * accept don't seem to list these as possible,
440 * although it's obvious that it may happen here.
441 * Thus no specific errors are tested at this
442 * point, just assume that connections cannot
443 * be accepted until some old is closed first.
445 if (-1 == (fd = accept(listener->fd, (struct sockaddr*) &addr,
447 /* Lotsa admins seem to have problems with not giving enough file
448 * descriptors to their server so we'll add a generic warning mechanism
449 * here. If it turns out too many messages are generated for
450 * meaningless reasons we can filter them back.
452 sendto_opmask_butone(0, SNO_TCPCOMMON,
453 "Unable to accept connection: %m");
457 * check for connection limit
459 if (fd > MAXCLIENTS - 1) {
460 ++ServerStats->is_ref;
461 send(fd, "ERROR :All connections in use\r\n", 32, 0);
466 * check to see if listener is shutting down
468 if (!listener->active) {
469 ++ServerStats->is_ref;
470 send(fd, "ERROR :Use another port\r\n", 25, 0);
475 * check to see if connection is allowed for this address mask
477 if (!connection_allowed((const char*) &addr,
478 (const char*) &listener->mask)) {
479 ++ServerStats->is_ref;
480 send(fd, "ERROR :Use another port\r\n", 25, 0);
484 ++ServerStats->is_ac;
485 /* nextping = CurrentTime; */
487 add_connection(listener, fd);