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.
24 #include "ircd_alloc.h"
25 #include "ircd_osdep.h"
26 #include "ircd_reply.h"
27 #include "ircd_string.h"
33 #include "sprintf_irc.h"
34 #include "sys.h" /* MAXCLIENTS */
43 #include <sys/socket.h>
44 #include <arpa/inet.h>
47 #define INADDR_NONE ((unsigned int) 0xffffffff)
50 struct Listener* ListenerPollList = 0;
52 static struct Listener* make_listener(int port, struct in_addr addr)
54 struct Listener* listener =
55 (struct Listener*) MyMalloc(sizeof(struct Listener));
56 assert(0 != listener);
58 memset(listener, 0, sizeof(struct Listener));
61 listener->port = port;
62 listener->addr.s_addr = addr.s_addr;
64 #ifdef NULL_POINTER_NOT_ZERO
65 listener->next = NULL;
66 listener->conf = NULL;
71 static void free_listener(struct Listener* listener)
73 assert(0 != listener);
77 #define PORTNAMELEN 10 /* ":31337" */
80 * get_listener_name - return displayable listener name and port
81 * returns "host.foo.org:6667" for a given listener
83 const char* get_listener_name(const struct Listener* listener)
85 static char buf[HOSTLEN + PORTNAMELEN + 4];
86 assert(0 != listener);
87 sprintf_irc(buf, "%s:%u", me.name, listener->port);
92 * count_listener_memory - count memory and listeners
94 void count_listener_memory(int* count_out, size_t* size_out)
98 assert(0 != count_out);
99 assert(0 != size_out);
100 for (l = ListenerPollList; l; l = l->next)
103 *size_out = count * sizeof(struct Listener);
107 * show_ports - send port listing to a client
108 * inputs - pointer to client to show ports to
110 * side effects - show ports
113 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
115 struct Listener* listener = 0;
119 for (listener = ListenerPollList; listener; listener = listener->next) {
120 if (port && port != listener->port)
122 flags[0] = (listener->server) ? 'S' : 'C';
123 if (listener->hidden) {
132 send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
133 flags, (listener->active) ? "active" : "disabled");
140 * inetport - create a listener socket in the AF_INET domain,
141 * bind it to the port given in 'port' and listen to it
142 * returns true (1) if successful false (0) on error.
144 * If the operating system has a define for SOMAXCONN, use it, otherwise
145 * use HYBRID_SOMAXCONN -Dianora
146 * NOTE: Do this in os_xxxx.c files
149 #define HYBRID_SOMAXCONN SOMAXCONN
151 #define HYBRID_SOMAXCONN 64
154 static int inetport(struct Listener* listener)
156 struct sockaddr_in sin;
160 * At first, open a new socket
162 if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
163 report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
166 else if (fd > MAXCLIENTS - 1) {
167 report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
172 if (!os_set_reuseaddr(fd)) {
173 report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
178 * Bind a port to listen for new connections if port is non-null,
179 * else assume it is already open and try get something from it.
181 memset(&sin, 0, sizeof(sin));
182 sin.sin_family = AF_INET;
183 sin.sin_addr = listener->addr;
184 sin.sin_port = htons(listener->port);
186 if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
187 report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
192 * Set the buffer sizes for the listener. Accepted connections
193 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
194 * The window size is set during the SYN ACK so setting it anywhere
195 * else has no effect whatsoever on the connection.
196 * NOTE: this must be set before listen is called
198 if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
199 report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
203 if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
204 report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
209 * XXX - this should always work, performance will suck if it doesn't
211 if (!os_set_nonblocking(fd)) {
212 report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
222 * find_listener - find a listener in the list
224 * XXX - this function does N comparisons so if the list is huge
225 * we may want to do something else for this. (rehash and init use this)
227 static struct Listener* find_listener(int port, struct in_addr addr)
229 struct Listener* listener;
230 for (listener = ListenerPollList; listener; listener = listener->next) {
231 if (port == listener->port && addr.s_addr == listener->addr.s_addr)
238 * set_listener_mask - set the connection mask for this listener
240 static void set_listener_mask(struct Listener* listener, const char* mask)
245 assert(0 != listener);
247 if (EmptyString(mask) || 0 != strcmp(mask, "*")) {
248 listener->mask.s_addr = 0;
251 ad[0] = ad[1] = ad[2] = ad[3] = 0;
253 * do it this way because building ip# from separate values for each
254 * byte requires endian knowledge or some nasty messing. Also means
255 * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
257 sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
258 sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
259 listener->mask.s_addr = inet_addr(ipname);
263 * connection_allowed - spin through mask and addr passed to see if connect
264 * allowed on a listener, uses mask generated by set_listener_mask
266 static int connection_allowed(const char* addr, const char* mask)
269 for ( ; i > 0; --i) {
270 if (*mask && *addr != *mask)
280 * add_listener- create a new listener
281 * port - the port number to listen on
282 * vhost_ip - if non-null must contain a valid IP address string in
283 * the format "255.255.255.255"
285 void add_listener(int port, const char* vhost_ip, const char* mask,
286 int is_server, int is_hidden)
288 struct Listener* listener;
289 struct in_addr vaddr;
292 * if no port in conf line, don't bother
297 vaddr.s_addr = INADDR_ANY;
299 if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
300 vaddr.s_addr = inet_addr(vhost_ip);
301 if (INADDR_NONE == vaddr.s_addr)
305 if ((listener = find_listener(port, vaddr))) {
307 * set active flag and change connect mask here, it's the only thing
308 * that can change on a rehash
310 listener->active = 1;
311 set_listener_mask(listener, mask);
312 listener->hidden = is_hidden;
313 listener->server = is_server;
317 listener = make_listener(port, vaddr);
319 if (inetport(listener)) {
320 listener->active = 1;
321 set_listener_mask(listener, mask);
322 listener->hidden = is_hidden;
323 listener->server = is_server;
324 listener->next = ListenerPollList;
325 ListenerPollList = listener;
328 free_listener(listener);
332 * mark_listeners_closing - iterate through listeners and mark them as
335 void mark_listeners_closing(void)
337 struct Listener* listener;
338 for (listener = ListenerPollList; listener; listener = listener->next)
339 listener->active = 0;
343 * close_listener - close a single listener
345 void close_listener(struct Listener* listener)
347 assert(0 != listener);
349 * remove from listener list
351 if (listener == ListenerPollList)
352 ListenerPollList = listener->next;
354 struct Listener* prev = ListenerPollList;
355 for ( ; prev; prev = prev->next) {
356 if (listener == prev->next) {
357 prev->next = listener->next;
362 if (-1 < listener->fd)
364 free_listener(listener);
368 * close_listeners - close and free all listeners that are not being used
370 void close_listeners()
372 struct Listener* listener;
373 struct Listener* listener_next = 0;
375 * close all 'extra' listening ports we have
377 for (listener = ListenerPollList; listener; listener = listener_next) {
378 listener_next = listener->next;
379 if (0 == listener->active && 0 == listener->ref_count)
380 close_listener(listener);
384 void release_listener(struct Listener* listener)
386 assert(0 != listener);
387 assert(0 < listener->ref_count);
388 if (0 == --listener->ref_count && !listener->active)
389 close_listener(listener);
393 * accept_connection - accept a connection on a listener
395 void accept_connection(struct Listener* listener)
397 struct sockaddr_in addr = { 0 };
398 unsigned int addrlen = sizeof(struct sockaddr_in);
401 assert(0 != listener);
403 listener->last_accept = CurrentTime;
405 * There may be many reasons for error return, but
406 * in otherwise correctly working environment the
407 * probable cause is running out of file descriptors
408 * (EMFILE, ENFILE or others?). The man pages for
409 * accept don't seem to list these as possible,
410 * although it's obvious that it may happen here.
411 * Thus no specific errors are tested at this
412 * point, just assume that connections cannot
413 * be accepted until some old is closed first.
415 if (-1 == (fd = accept(listener->fd, (struct sockaddr*) &addr, &addrlen))) {
419 * check for connection limit
421 if (fd > MAXCLIENTS - 1) {
422 ++ServerStats->is_ref;
423 send(fd, "ERROR :All connections in use\r\n", 32, 0);
428 * check to see if listener is shutting down
430 if (!listener->active) {
431 ++ServerStats->is_ref;
432 send(fd, "ERROR :Use another port\r\n", 25, 0);
437 * check to see if connection is allowed for this address mask
439 if (!connection_allowed((const char*) &addr, (const char*) &listener->mask)) {
440 ++ServerStats->is_ref;
441 send(fd, "ERROR :Use another port\r\n", 25, 0);
447 * check conf for ip address access
449 if (!conf_connect_allowed(addr.sin_addr)) {
450 ++ServerStats->is_ref;
451 send(fd, "ERROR :Not authorized\r\n", 23, 0);
456 ++ServerStats->is_ac;
457 nextping = CurrentTime;
459 add_connection(listener, fd);