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"
39 #include "sys.h" /* MAXCLIENTS */
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
52 #define INADDR_NONE ((unsigned int) 0xffffffff)
55 struct Listener* ListenerPollList = 0;
57 static void accept_connection(struct Event* ev);
59 static struct Listener* make_listener(int port, struct in_addr addr)
61 struct Listener* listener =
62 (struct Listener*) MyMalloc(sizeof(struct Listener));
63 assert(0 != listener);
65 memset(listener, 0, sizeof(struct Listener));
68 listener->port = port;
69 listener->addr.s_addr = addr.s_addr;
71 #ifdef NULL_POINTER_NOT_ZERO
72 listener->next = NULL;
73 listener->conf = NULL;
78 static void free_listener(struct Listener* listener)
80 assert(0 != listener);
84 #define PORTNAMELEN 10 /* ":31337" */
87 * get_listener_name - return displayable listener name and port
88 * returns "host.foo.org:6667" for a given listener
90 const char* get_listener_name(const struct Listener* listener)
92 static char buf[HOSTLEN + PORTNAMELEN + 4];
93 assert(0 != listener);
94 ircd_snprintf(0, buf, sizeof(buf), "%s:%u", cli_name(&me), listener->port);
99 * count_listener_memory - count memory and listeners
101 void count_listener_memory(int* count_out, size_t* size_out)
105 assert(0 != count_out);
106 assert(0 != size_out);
107 for (l = ListenerPollList; l; l = l->next)
110 *size_out = count * sizeof(struct Listener);
114 * show_ports - send port listing to a client
115 * inputs - pointer to client to show ports to
117 * side effects - show ports
120 void show_ports(struct Client* sptr, struct StatDesc* sd, int stat,
123 struct Listener *listener = 0;
125 int show_hidden = IsOper(sptr);
126 int count = (IsOper(sptr) || MyUser(sptr)) ? 100 : 8;
134 for (listener = ListenerPollList; listener; listener = listener->next) {
135 if (port && port != listener->port)
137 flags[0] = (listener->server) ? 'S' : 'C';
138 if (listener->hidden) {
147 send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
148 flags, (listener->active) ? "active" : "disabled");
155 * inetport - create a listener socket in the AF_INET domain,
156 * bind it to the port given in 'port' and listen to it
157 * returns true (1) if successful false (0) on error.
159 * If the operating system has a define for SOMAXCONN, use it, otherwise
160 * use HYBRID_SOMAXCONN -Dianora
161 * NOTE: Do this in os_xxxx.c files
164 #define HYBRID_SOMAXCONN SOMAXCONN
166 #define HYBRID_SOMAXCONN 64
169 static int inetport(struct Listener* listener)
171 struct sockaddr_in sin;
175 * At first, open a new socket
177 if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
178 report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
181 else if (fd > MAXCLIENTS - 1) {
182 report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
187 if (!os_set_reuseaddr(fd)) {
188 report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
193 * Bind a port to listen for new connections if port is non-null,
194 * else assume it is already open and try get something from it.
196 memset(&sin, 0, sizeof(sin));
197 sin.sin_family = AF_INET;
198 sin.sin_addr = listener->addr;
199 sin.sin_port = htons(listener->port);
201 if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
202 report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
207 * Set the buffer sizes for the listener. Accepted connections
208 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
209 * The window size is set during the SYN ACK so setting it anywhere
210 * else has no effect whatsoever on the connection.
211 * NOTE: this must be set before listen is called
213 if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
214 report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
218 if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
219 report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
224 * XXX - this should always work, performance will suck if it doesn't
226 if (!os_set_nonblocking(fd)) {
227 report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
232 * Set the TOS bits - this is nonfatal if it doesn't stick.
234 if (!os_set_tos(fd,feature_int((listener->server)?FEAT_TOS_SERVER : FEAT_TOS_CLIENT))) {
235 report_error(TOS_ERROR_MSG, get_listener_name(listener), errno);
238 if (!socket_add(&listener->socket, accept_connection, (void*) listener,
239 SS_LISTENING, 0, fd)) {
240 /* Error should already have been reported to the logs */
251 * find_listener - find a listener in the list
253 * XXX - this function does N comparisons so if the list is huge
254 * we may want to do something else for this. (rehash and init use this)
256 static struct Listener* find_listener(int port, struct in_addr addr)
258 struct Listener* listener;
259 for (listener = ListenerPollList; listener; listener = listener->next) {
260 if (port == listener->port && addr.s_addr == listener->addr.s_addr)
267 * set_listener_mask - set the connection mask for this listener
269 static void set_listener_mask(struct Listener* listener, const char* mask)
274 assert(0 != listener);
276 if (EmptyString(mask) || 0 == strcmp(mask, "*")) {
277 listener->mask.s_addr = 0;
280 ad[0] = ad[1] = ad[2] = ad[3] = 0;
282 * do it this way because building ip# from separate values for each
283 * byte requires endian knowledge or some nasty messing. Also means
284 * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
286 sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
287 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1], ad[2],
289 listener->mask.s_addr = inet_addr(ipname);
293 * connection_allowed - spin through mask and addr passed to see if connect
294 * allowed on a listener, uses mask generated by set_listener_mask
296 static int connection_allowed(const char* addr, const char* mask)
299 for ( ; i > 0; --i) {
300 if (*mask && *addr != *mask)
310 * add_listener- create a new listener
311 * port - the port number to listen on
312 * vhost_ip - if non-null must contain a valid IP address string in
313 * the format "255.255.255.255"
315 void add_listener(int port, const char* vhost_ip, const char* mask,
316 int is_server, int is_hidden)
318 struct Listener* listener;
319 struct in_addr vaddr;
322 * if no port in conf line, don't bother
327 vaddr.s_addr = INADDR_ANY;
329 if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
330 vaddr.s_addr = inet_addr(vhost_ip);
331 if (INADDR_NONE == vaddr.s_addr)
335 if ((listener = find_listener(port, vaddr))) {
337 * set active flag and change connect mask here, it's the only thing
338 * that can change on a rehash
340 listener->active = 1;
341 set_listener_mask(listener, mask);
342 listener->hidden = is_hidden;
343 listener->server = is_server;
347 listener = make_listener(port, vaddr);
349 if (inetport(listener)) {
350 listener->active = 1;
351 set_listener_mask(listener, mask);
352 listener->hidden = is_hidden;
353 listener->server = is_server;
354 listener->next = ListenerPollList;
355 ListenerPollList = listener;
358 free_listener(listener);
362 * mark_listeners_closing - iterate through listeners and mark them as
365 void mark_listeners_closing(void)
367 struct Listener* listener;
368 for (listener = ListenerPollList; listener; listener = listener->next)
369 listener->active = 0;
373 * close_listener - close a single listener
375 void close_listener(struct Listener* listener)
377 assert(0 != listener);
379 * remove from listener list
381 if (listener == ListenerPollList)
382 ListenerPollList = listener->next;
384 struct Listener* prev = ListenerPollList;
385 for ( ; prev; prev = prev->next) {
386 if (listener == prev->next) {
387 prev->next = listener->next;
392 if (-1 < listener->fd)
394 socket_del(&listener->socket);
398 * close_listeners - close and free all listeners that are not being used
400 void close_listeners()
402 struct Listener* listener;
403 struct Listener* listener_next = 0;
405 * close all 'extra' listening ports we have
407 for (listener = ListenerPollList; listener; listener = listener_next) {
408 listener_next = listener->next;
409 if (0 == listener->active && 0 == listener->ref_count)
410 close_listener(listener);
414 void release_listener(struct Listener* listener)
416 assert(0 != listener);
417 assert(0 < listener->ref_count);
418 if (0 == --listener->ref_count && !listener->active)
419 close_listener(listener);
423 * accept_connection - accept a connection on a listener
425 static void accept_connection(struct Event* ev)
427 struct Listener* listener;
428 struct sockaddr_in addr = { 0 };
429 unsigned int addrlen = sizeof(struct sockaddr_in);
432 assert(0 != ev_socket(ev));
433 assert(0 != s_data(ev_socket(ev)));
435 listener = s_data(ev_socket(ev));
437 if (ev_type(ev) == ET_DESTROY) /* being destroyed */
438 free_listener(listener);
440 assert(ev_type(ev) == ET_ACCEPT || ev_type(ev) == ET_ERROR);
442 listener->last_accept = CurrentTime;
444 * There may be many reasons for error return, but
445 * in otherwise correctly working environment the
446 * probable cause is running out of file descriptors
447 * (EMFILE, ENFILE or others?). The man pages for
448 * accept don't seem to list these as possible,
449 * although it's obvious that it may happen here.
450 * Thus no specific errors are tested at this
451 * point, just assume that connections cannot
452 * be accepted until some old is closed first.
454 * This piece of code implements multi-accept, based
455 * on the idea that poll/select can only be efficient,
456 * if we succeed in handling all available events,
457 * i.e. accept all pending connections.
459 * http://www.hpl.hp.com/techreports/2000/HPL-2000-174.html
463 if ((fd = accept(listener->fd, (struct sockaddr*) &addr, &addrlen))
466 if (errno == EAGAIN ||
468 errno == EWOULDBLOCK)
471 /* Lotsa admins seem to have problems with not giving enough file
472 * descriptors to their server so we'll add a generic warning mechanism
473 * here. If it turns out too many messages are generated for
474 * meaningless reasons we can filter them back.
476 sendto_opmask_butone(0, SNO_TCPCOMMON,
477 "Unable to accept connection: %m");
481 * check for connection limit. If this fd exceeds the limit,
482 * all further accept()ed connections will also exceed it.
483 * Enable the server to clear out other connections before
484 * continuing to accept() new connections.
486 if (fd > MAXCLIENTS - 1)
488 ++ServerStats->is_ref;
489 send(fd, "ERROR :All connections in use\r\n", 32, 0);
494 * check to see if listener is shutting down. Continue
495 * to accept(), because it makes sense to clear our the
496 * socket's queue as fast as possible.
498 if (!listener->active)
500 ++ServerStats->is_ref;
501 send(fd, "ERROR :Use another port\r\n", 25, 0);
506 * check to see if connection is allowed for this address mask
508 if (!connection_allowed((const char*) &addr,
509 (const char*) &listener->mask))
511 ++ServerStats->is_ref;
512 send(fd, "ERROR :Use another port\r\n", 25, 0);
516 ++ServerStats->is_ac;
517 /* nextping = CurrentTime; */
518 add_connection(listener, fd);