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_features.h"
28 #include "ircd_osdep.h"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
36 #include "sprintf_irc.h"
37 #include "sys.h" /* MAXCLIENTS */
46 #include <sys/socket.h>
47 #include <arpa/inet.h>
50 #define INADDR_NONE ((unsigned int) 0xffffffff)
53 struct Listener* ListenerPollList = 0;
55 static struct Listener* make_listener(int port, struct in_addr addr)
57 struct Listener* listener =
58 (struct Listener*) MyMalloc(sizeof(struct Listener));
59 assert(0 != listener);
61 memset(listener, 0, sizeof(struct Listener));
64 listener->port = port;
65 listener->addr.s_addr = addr.s_addr;
67 #ifdef NULL_POINTER_NOT_ZERO
68 listener->next = NULL;
69 listener->conf = NULL;
74 static void free_listener(struct Listener* listener)
76 assert(0 != listener);
80 #define PORTNAMELEN 10 /* ":31337" */
83 * get_listener_name - return displayable listener name and port
84 * returns "host.foo.org:6667" for a given listener
86 const char* get_listener_name(const struct Listener* listener)
88 static char buf[HOSTLEN + PORTNAMELEN + 4];
89 assert(0 != listener);
90 sprintf_irc(buf, "%s:%u", cli_name(&me), listener->port);
95 * count_listener_memory - count memory and listeners
97 void count_listener_memory(int* count_out, size_t* size_out)
101 assert(0 != count_out);
102 assert(0 != size_out);
103 for (l = ListenerPollList; l; l = l->next)
106 *size_out = count * sizeof(struct Listener);
110 * show_ports - send port listing to a client
111 * inputs - pointer to client to show ports to
113 * side effects - show ports
116 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
118 struct Listener* listener = 0;
122 for (listener = ListenerPollList; listener; listener = listener->next) {
123 if (port && port != listener->port)
125 flags[0] = (listener->server) ? 'S' : 'C';
126 if (listener->hidden) {
135 send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
136 flags, (listener->active) ? "active" : "disabled");
143 * inetport - create a listener socket in the AF_INET domain,
144 * bind it to the port given in 'port' and listen to it
145 * returns true (1) if successful false (0) on error.
147 * If the operating system has a define for SOMAXCONN, use it, otherwise
148 * use HYBRID_SOMAXCONN -Dianora
149 * NOTE: Do this in os_xxxx.c files
152 #define HYBRID_SOMAXCONN SOMAXCONN
154 #define HYBRID_SOMAXCONN 64
157 static int inetport(struct Listener* listener)
159 struct sockaddr_in sin;
163 * At first, open a new socket
165 if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
166 report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
169 else if (fd > MAXCLIENTS - 1) {
170 report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
175 if (!os_set_reuseaddr(fd)) {
176 report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
181 * Bind a port to listen for new connections if port is non-null,
182 * else assume it is already open and try get something from it.
184 memset(&sin, 0, sizeof(sin));
185 sin.sin_family = AF_INET;
186 sin.sin_addr = listener->addr;
187 sin.sin_port = htons(listener->port);
189 if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
190 report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
195 * Set the buffer sizes for the listener. Accepted connections
196 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
197 * The window size is set during the SYN ACK so setting it anywhere
198 * else has no effect whatsoever on the connection.
199 * NOTE: this must be set before listen is called
201 if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
202 report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
206 if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
207 report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
212 * XXX - this should always work, performance will suck if it doesn't
214 if (!os_set_nonblocking(fd)) {
215 report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
220 * Set the TOS bits - this is nonfatal if it doesn't stick.
222 if (!os_set_tos(fd,feature_int((listener->server)?FEAT_TOS_SERVER : FEAT_TOS_CLIENT))) {
223 report_error(TOS_ERROR_MSG, get_listener_name(listener), errno);
231 * find_listener - find a listener in the list
233 * XXX - this function does N comparisons so if the list is huge
234 * we may want to do something else for this. (rehash and init use this)
236 static struct Listener* find_listener(int port, struct in_addr addr)
238 struct Listener* listener;
239 for (listener = ListenerPollList; listener; listener = listener->next) {
240 if (port == listener->port && addr.s_addr == listener->addr.s_addr)
247 * set_listener_mask - set the connection mask for this listener
249 static void set_listener_mask(struct Listener* listener, const char* mask)
254 assert(0 != listener);
256 if (EmptyString(mask) || 0 == strcmp(mask, "*")) {
257 listener->mask.s_addr = 0;
260 ad[0] = ad[1] = ad[2] = ad[3] = 0;
262 * do it this way because building ip# from separate values for each
263 * byte requires endian knowledge or some nasty messing. Also means
264 * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
266 sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
267 sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
268 listener->mask.s_addr = inet_addr(ipname);
272 * connection_allowed - spin through mask and addr passed to see if connect
273 * allowed on a listener, uses mask generated by set_listener_mask
275 static int connection_allowed(const char* addr, const char* mask)
278 for ( ; i > 0; --i) {
279 if (*mask && *addr != *mask)
289 * add_listener- create a new listener
290 * port - the port number to listen on
291 * vhost_ip - if non-null must contain a valid IP address string in
292 * the format "255.255.255.255"
294 void add_listener(int port, const char* vhost_ip, const char* mask,
295 int is_server, int is_hidden)
297 struct Listener* listener;
298 struct in_addr vaddr;
301 * if no port in conf line, don't bother
306 vaddr.s_addr = INADDR_ANY;
308 if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
309 vaddr.s_addr = inet_addr(vhost_ip);
310 if (INADDR_NONE == vaddr.s_addr)
314 if ((listener = find_listener(port, vaddr))) {
316 * set active flag and change connect mask here, it's the only thing
317 * that can change on a rehash
319 listener->active = 1;
320 set_listener_mask(listener, mask);
321 listener->hidden = is_hidden;
322 listener->server = is_server;
326 listener = make_listener(port, vaddr);
328 if (inetport(listener)) {
329 listener->active = 1;
330 set_listener_mask(listener, mask);
331 listener->hidden = is_hidden;
332 listener->server = is_server;
333 listener->next = ListenerPollList;
334 ListenerPollList = listener;
337 free_listener(listener);
341 * mark_listeners_closing - iterate through listeners and mark them as
344 void mark_listeners_closing(void)
346 struct Listener* listener;
347 for (listener = ListenerPollList; listener; listener = listener->next)
348 listener->active = 0;
352 * close_listener - close a single listener
354 void close_listener(struct Listener* listener)
356 assert(0 != listener);
358 * remove from listener list
360 if (listener == ListenerPollList)
361 ListenerPollList = listener->next;
363 struct Listener* prev = ListenerPollList;
364 for ( ; prev; prev = prev->next) {
365 if (listener == prev->next) {
366 prev->next = listener->next;
371 if (-1 < listener->fd)
373 free_listener(listener);
377 * close_listeners - close and free all listeners that are not being used
379 void close_listeners()
381 struct Listener* listener;
382 struct Listener* listener_next = 0;
384 * close all 'extra' listening ports we have
386 for (listener = ListenerPollList; listener; listener = listener_next) {
387 listener_next = listener->next;
388 if (0 == listener->active && 0 == listener->ref_count)
389 close_listener(listener);
393 void release_listener(struct Listener* listener)
395 assert(0 != listener);
396 assert(0 < listener->ref_count);
397 if (0 == --listener->ref_count && !listener->active)
398 close_listener(listener);
402 * accept_connection - accept a connection on a listener
404 void accept_connection(struct Listener* listener)
406 struct sockaddr_in addr = { 0 };
407 unsigned int addrlen = sizeof(struct sockaddr_in);
410 assert(0 != listener);
412 listener->last_accept = CurrentTime;
414 * There may be many reasons for error return, but
415 * in otherwise correctly working environment the
416 * probable cause is running out of file descriptors
417 * (EMFILE, ENFILE or others?). The man pages for
418 * accept don't seem to list these as possible,
419 * although it's obvious that it may happen here.
420 * Thus no specific errors are tested at this
421 * point, just assume that connections cannot
422 * be accepted until some old is closed first.
424 if (-1 == (fd = accept(listener->fd, (struct sockaddr*) &addr, &addrlen))) {
425 /* Lotsa admins seem to have problems with not giving enough file descriptors
426 * to their server so we'll add a generic warning mechanism here. If it
427 * turns out too many messages are generated for meaningless reasons we
428 * can filter them back.
430 sendto_opmask_butone(0, SNO_TCPCOMMON, "Unable to accept connection: %m");
434 * check for connection limit
436 if (fd > MAXCLIENTS - 1) {
437 ++ServerStats->is_ref;
438 send(fd, "ERROR :All connections in use\r\n", 32, 0);
443 * check to see if listener is shutting down
445 if (!listener->active) {
446 ++ServerStats->is_ref;
447 send(fd, "ERROR :Use another port\r\n", 25, 0);
452 * check to see if connection is allowed for this address mask
454 if (!connection_allowed((const char*) &addr, (const char*) &listener->mask)) {
455 ++ServerStats->is_ref;
456 send(fd, "ERROR :Use another port\r\n", 25, 0);
462 * check conf for ip address access
464 if (!conf_connect_allowed(addr.sin_addr)) {
465 ++ServerStats->is_ref;
466 send(fd, "ERROR :Not authorized\r\n", 23, 0);
471 ++ServerStats->is_ac;
472 nextping = CurrentTime;
474 add_connection(listener, fd);