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_string.h"
32 #include "sprintf_irc.h"
33 #include "sys.h" /* MAXCLIENTS */
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
46 #define INADDR_NONE ((unsigned int) 0xffffffff)
49 struct Listener* ListenerPollList = 0;
51 static struct Listener* make_listener(int port, struct in_addr addr)
53 struct Listener* listener =
54 (struct Listener*) MyMalloc(sizeof(struct Listener));
55 assert(0 != listener);
57 memset(listener, 0, sizeof(struct Listener));
60 listener->port = port;
61 listener->addr.s_addr = addr.s_addr;
63 #ifdef NULL_POINTER_NOT_ZERO
64 listener->next = NULL;
65 listener->conf = NULL;
70 static void free_listener(struct Listener* listener)
72 assert(0 != listener);
76 #define PORTNAMELEN 10 /* ":31337" */
79 * get_listener_name - return displayable listener name and port
80 * returns "host.foo.org:6667" for a given listener
82 const char* get_listener_name(const struct Listener* listener)
84 static char buf[HOSTLEN + PORTNAMELEN + 4];
85 assert(0 != listener);
86 sprintf_irc(buf, "%s:%u", me.name, listener->port);
91 * count_listener_memory - count memory and listeners
93 void count_listener_memory(int* count_out, size_t* size_out)
97 assert(0 != count_out);
98 assert(0 != size_out);
99 for (l = ListenerPollList; l; l = l->next)
102 *size_out = count * sizeof(struct Listener);
106 * show_ports - send port listing to a client
107 * inputs - pointer to client to show ports to
109 * side effects - show ports
112 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
114 struct Listener* listener = 0;
118 for (listener = ListenerPollList; listener; listener = listener->next) {
119 if (port && port != listener->port)
121 flags[0] = (listener->server) ? 'S' : 'C';
122 if (listener->hidden) {
131 sendto_one(sptr, rpl_str(RPL_STATSPLINE),
132 me.name, sptr->name, listener->port,
133 listener->ref_count, flags,
134 (listener->active) ? "active" : "disabled");
141 * inetport - create a listener socket in the AF_INET domain,
142 * bind it to the port given in 'port' and listen to it
143 * returns true (1) if successful false (0) on error.
145 * If the operating system has a define for SOMAXCONN, use it, otherwise
146 * use HYBRID_SOMAXCONN -Dianora
147 * NOTE: Do this in os_xxxx.c files
150 #define HYBRID_SOMAXCONN SOMAXCONN
152 #define HYBRID_SOMAXCONN 64
155 static int inetport(struct Listener* listener)
157 struct sockaddr_in sin;
161 * At first, open a new socket
163 if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
164 report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
167 else if (fd > MAXCLIENTS - 1) {
168 report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
173 if (!os_set_reuseaddr(fd)) {
174 report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
179 * Bind a port to listen for new connections if port is non-null,
180 * else assume it is already open and try get something from it.
182 memset(&sin, 0, sizeof(sin));
183 sin.sin_family = AF_INET;
184 sin.sin_addr = listener->addr;
185 sin.sin_port = htons(listener->port);
187 if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
188 report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
193 * Set the buffer sizes for the listener. Accepted connections
194 * inherit the accepting sockets settings for SO_RCVBUF S_SNDBUF
195 * The window size is set during the SYN ACK so setting it anywhere
196 * else has no effect whatsoever on the connection.
197 * NOTE: this must be set before listen is called
199 if (!os_set_sockbufs(fd, (listener->server) ? SERVER_TCP_WINDOW : CLIENT_TCP_WINDOW)) {
200 report_error(SETBUFS_ERROR_MSG, get_listener_name(listener), errno);
204 if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
205 report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
210 * XXX - this should always work, performance will suck if it doesn't
212 if (!os_set_nonblocking(fd)) {
213 report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
223 * find_listener - find a listener in the list
225 * XXX - this function does N comparisons so if the list is huge
226 * we may want to do something else for this. (rehash and init use this)
228 static struct Listener* find_listener(int port, struct in_addr addr)
230 struct Listener* listener;
231 for (listener = ListenerPollList; listener; listener = listener->next) {
232 if (port == listener->port && addr.s_addr == listener->addr.s_addr)
239 * set_listener_mask - set the connection mask for this listener
241 static void set_listener_mask(struct Listener* listener, const char* mask)
246 assert(0 != listener);
248 if (EmptyString(mask) && strcmp(mask,"*")!=0) {
249 listener->mask.s_addr = 0;
252 ad[0] = ad[1] = ad[2] = ad[3] = 0;
254 * do it this way because building ip# from separate values for each
255 * byte requires endian knowledge or some nasty messing. Also means
256 * easy conversion of "*" 0.0.0.0 or 134.* to 134.0.0.0 :-)
258 sscanf(mask, "%d.%d.%d.%d", &ad[0], &ad[1], &ad[2], &ad[3]);
259 sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
260 listener->mask.s_addr = inet_addr(ipname);
264 * connection_allowed - spin through mask and addr passed to see if connect
265 * allowed on a listener, uses mask generated by set_listener_mask
267 static int connection_allowed(const char* addr, const char* mask)
270 for ( ; i > 0; --i) {
271 if (*mask && *addr != *mask)
281 * add_listener- create a new listener
282 * port - the port number to listen on
283 * vhost_ip - if non-null must contain a valid IP address string in
284 * the format "255.255.255.255"
286 void add_listener(int port, const char* vhost_ip, const char* mask,
287 int is_server, int is_hidden)
289 struct Listener* listener;
290 struct in_addr vaddr;
293 * if no port in conf line, don't bother
298 vaddr.s_addr = INADDR_ANY;
300 if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
301 vaddr.s_addr = inet_addr(vhost_ip);
302 if (INADDR_NONE == vaddr.s_addr)
306 if ((listener = find_listener(port, vaddr))) {
308 * set active flag and change connect mask here, it's the only thing
309 * that can change on a rehash
311 listener->active = 1;
312 set_listener_mask(listener, mask);
313 listener->hidden = is_hidden;
314 listener->server = is_server;
318 listener = make_listener(port, vaddr);
320 if (inetport(listener)) {
321 listener->active = 1;
322 set_listener_mask(listener, mask);
323 listener->hidden = is_hidden;
324 listener->server = is_server;
325 listener->next = ListenerPollList;
326 ListenerPollList = listener;
329 free_listener(listener);
333 * mark_listeners_closing - iterate through listeners and mark them as
336 void mark_listeners_closing(void)
338 struct Listener* listener;
339 for (listener = ListenerPollList; listener; listener = listener->next)
340 listener->active = 0;
344 * close_listener - close a single listener
346 void close_listener(struct Listener* listener)
348 assert(0 != listener);
350 * remove from listener list
352 if (listener == ListenerPollList)
353 ListenerPollList = listener->next;
355 struct Listener* prev = ListenerPollList;
356 for ( ; prev; prev = prev->next) {
357 if (listener == prev->next) {
358 prev->next = listener->next;
363 if (-1 < listener->fd)
365 free_listener(listener);
369 * close_listeners - close and free all listeners that are not being used
371 void close_listeners()
373 struct Listener* listener;
374 struct Listener* listener_next = 0;
376 * close all 'extra' listening ports we have
378 for (listener = ListenerPollList; listener; listener = listener_next) {
379 listener_next = listener->next;
380 if (0 == listener->active && 0 == listener->ref_count)
381 close_listener(listener);
385 void release_listener(struct Listener* listener)
387 assert(0 != listener);
388 assert(0 < listener->ref_count);
389 if (0 == --listener->ref_count && !listener->active)
390 close_listener(listener);
394 * accept_connection - accept a connection on a listener
396 void accept_connection(struct Listener* listener)
398 struct sockaddr_in addr;
399 size_t addrlen = sizeof(struct sockaddr_in);
402 assert(0 != listener);
404 listener->last_accept = CurrentTime;
406 * There may be many reasons for error return, but
407 * in otherwise correctly working environment the
408 * probable cause is running out of file descriptors
409 * (EMFILE, ENFILE or others?). The man pages for
410 * accept don't seem to list these as possible,
411 * although it's obvious that it may happen here.
412 * Thus no specific errors are tested at this
413 * point, just assume that connections cannot
414 * be accepted until some old is closed first.
416 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);