Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / listener.c
1 /************************************************************************
2  *   IRC - Internet Relay Chat, src/listener.c
3  *   Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net>
4  *
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)
8  *   any later version.
9  *
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.
14  *
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.
18  *
19  *  $Id$
20  */
21 #include "listener.h"
22 #include "client.h"
23 #include "ircd.h"
24 #include "ircd_alloc.h"
25 #include "ircd_osdep.h"
26 #include "ircd_reply.h"
27 #include "ircd_string.h"
28 #include "numeric.h"
29 #include "s_bsd.h"
30 #include "s_conf.h"
31 #include "s_misc.h"
32 #include "send.h"
33 #include "sprintf_irc.h"
34 #include "sys.h"         /* MAXCLIENTS */
35
36 #include <assert.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <netdb.h>
43 #include <sys/socket.h>
44 #include <arpa/inet.h>
45
46 #ifndef INADDR_NONE
47 #define INADDR_NONE ((unsigned int) 0xffffffff)
48 #endif
49
50 int tos_server = 0x08; // Low delay
51 int tos_client = 0x08; // Low delay
52
53 struct Listener* ListenerPollList = 0;
54
55 static struct Listener* make_listener(int port, struct in_addr addr)
56 {
57   struct Listener* listener = 
58     (struct Listener*) MyMalloc(sizeof(struct Listener));
59   assert(0 != listener);
60
61   memset(listener, 0, sizeof(struct Listener));
62
63   listener->fd          = -1;
64   listener->port        = port;
65   listener->addr.s_addr = addr.s_addr;
66
67 #ifdef NULL_POINTER_NOT_ZERO
68   listener->next = NULL;
69   listener->conf = NULL;
70 #endif
71   return listener;
72 }
73
74 static void free_listener(struct Listener* listener)
75 {
76   assert(0 != listener);
77   MyFree(listener);
78 }
79
80 #define PORTNAMELEN 10  /* ":31337" */
81
82 /*
83  * get_listener_name - return displayable listener name and port
84  * returns "host.foo.org:6667" for a given listener
85  */
86 const char* get_listener_name(const struct Listener* listener)
87 {
88   static char buf[HOSTLEN + PORTNAMELEN + 4];
89   assert(0 != listener);
90   sprintf_irc(buf, "%s:%u", cli_name(&me), listener->port);
91   return buf;
92 }
93
94 /*
95  * count_listener_memory - count memory and listeners
96  */
97 void count_listener_memory(int* count_out, size_t* size_out)
98 {
99   struct Listener* l;
100   int              count = 0;
101   assert(0 != count_out);
102   assert(0 != size_out);
103   for (l = ListenerPollList; l; l = l->next)
104     ++count;
105   *count_out = count;
106   *size_out  = count * sizeof(struct Listener);
107 }
108   
109 /*
110  * show_ports - send port listing to a client
111  * inputs       - pointer to client to show ports to
112  * output       - none
113  * side effects - show ports
114  * author       - Dianora
115  */
116 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
117 {
118   struct Listener* listener = 0;
119   char             flags[8];
120   assert(0 != sptr);
121
122   for (listener = ListenerPollList; listener; listener = listener->next) {
123     if (port && port != listener->port)
124       continue;
125     flags[0] = (listener->server) ? 'S' : 'C';
126     if (listener->hidden) {
127       if (!show_hidden)
128         continue;
129       flags[1] = 'H';
130       flags[2] = '\0';
131     }
132     else
133       flags[1] = '\0';
134
135     send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
136                flags, (listener->active) ? "active" : "disabled");
137     if (--count == 0)
138       break;
139   }
140 }
141
142 /*
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.
146  *
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
150  */
151 #ifdef SOMAXCONN
152 #define HYBRID_SOMAXCONN SOMAXCONN
153 #else
154 #define HYBRID_SOMAXCONN 64
155 #endif
156
157 static int inetport(struct Listener* listener)
158 {
159   struct sockaddr_in sin;
160   int                fd;
161
162   /*
163    * At first, open a new socket
164    */
165   if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
166     report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
167     return 0;
168   }
169   else if (fd > MAXCLIENTS - 1) {
170     report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
171     close(fd);
172     return 0;
173   }
174
175   if (!os_set_reuseaddr(fd)) {
176     report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
177     close(fd);
178     return 0;
179   }
180   /*
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.
183    */
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);
188
189   if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
190     report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
191     close(fd);
192     return 0;
193   }
194   /*
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
200    */
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);
203     close(fd);
204     return 0;
205   }
206   if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
207     report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
208     close(fd);
209     return 0;
210   }
211   /*
212    * XXX - this should always work, performance will suck if it doesn't
213    */
214   if (!os_set_nonblocking(fd)) {
215     report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
216     close(fd);
217     return 0;
218   }
219   /*
220    * Set the TOS bits - this is nonfatal if it doesn't stick.
221    */
222   if (!os_set_tos(fd,(listener->server) ? tos_server : tos_client)) {
223     report_error(TOS_ERROR_MSG, get_listener_name(listener), errno);
224   }
225   listener->fd = fd;
226
227   return 1;
228 }
229
230 /*
231  * find_listener - find a listener in the list
232  *
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)
235  */
236 static struct Listener* find_listener(int port, struct in_addr addr)
237 {
238   struct Listener* listener;
239   for (listener = ListenerPollList; listener; listener = listener->next) {
240     if (port == listener->port && addr.s_addr == listener->addr.s_addr)
241       return listener;
242   }
243   return 0;
244 }
245
246 /*
247  * set_listener_mask - set the connection mask for this listener
248  */
249 static void set_listener_mask(struct Listener* listener, const char* mask)
250 {
251   int  ad[4];
252   char ipname[20];
253
254   assert(0 != listener);
255
256   if (EmptyString(mask) || 0 == strcmp(mask, "*")) {
257     listener->mask.s_addr = 0;
258     return;
259   }
260   ad[0] = ad[1] = ad[2] = ad[3] = 0;
261   /*
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 :-)
265    */
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);
269 }
270
271 /*
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
274  */
275 static int connection_allowed(const char* addr, const char* mask)
276 {
277   int i = 4;
278   for ( ; i > 0; --i) {
279     if (*mask && *addr != *mask)
280       break;
281     ++addr;
282     ++mask;
283   }
284   return (0 == i);
285 }
286
287
288 /*
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"
293  */
294 void add_listener(int port, const char* vhost_ip, const char* mask,
295                   int is_server, int is_hidden) 
296 {
297   struct Listener* listener;
298   struct in_addr   vaddr;
299
300   /*
301    * if no port in conf line, don't bother
302    */
303   if (0 == port)
304     return;
305
306   vaddr.s_addr = INADDR_ANY;
307
308   if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
309     vaddr.s_addr = inet_addr(vhost_ip);
310     if (INADDR_NONE == vaddr.s_addr)
311       return;
312   }
313
314   if ((listener = find_listener(port, vaddr))) {
315     /*
316      * set active flag and change connect mask here, it's the only thing 
317      * that can change on a rehash
318      */
319     listener->active = 1;
320     set_listener_mask(listener, mask);
321     listener->hidden = is_hidden;
322     listener->server = is_server;
323     return;
324   }
325
326   listener = make_listener(port, vaddr);
327
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; 
335   }
336   else
337     free_listener(listener);
338 }
339
340 /*
341  * mark_listeners_closing - iterate through listeners and mark them as
342  * inactive
343  */
344 void mark_listeners_closing(void)
345 {
346   struct Listener* listener;
347   for (listener = ListenerPollList; listener; listener = listener->next)
348     listener->active = 0;
349 }
350
351 /*
352  * close_listener - close a single listener
353  */
354 void close_listener(struct Listener* listener)
355 {
356   assert(0 != listener);
357   /*
358    * remove from listener list
359    */
360   if (listener == ListenerPollList)
361     ListenerPollList = listener->next;
362   else {
363     struct Listener* prev = ListenerPollList;
364     for ( ; prev; prev = prev->next) {
365       if (listener == prev->next) {
366         prev->next = listener->next;
367         break; 
368       }
369     }
370   }
371   if (-1 < listener->fd)
372     close(listener->fd);
373   free_listener(listener);
374 }
375  
376 /*
377  * close_listeners - close and free all listeners that are not being used
378  */
379 void close_listeners()
380 {
381   struct Listener* listener;
382   struct Listener* listener_next = 0;
383   /*
384    * close all 'extra' listening ports we have
385    */
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);
390   }
391 }
392
393 void release_listener(struct Listener* listener)
394 {
395   assert(0 != listener);
396   assert(0 < listener->ref_count);
397   if (0 == --listener->ref_count && !listener->active)
398     close_listener(listener);
399 }
400
401 /*
402  * accept_connection - accept a connection on a listener
403  */
404 void accept_connection(struct Listener* listener)
405 {
406   struct sockaddr_in addr = { 0 };
407   unsigned int       addrlen = sizeof(struct sockaddr_in);
408   int                fd;
409
410   assert(0 != listener);
411
412   listener->last_accept = CurrentTime;
413   /*
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.
423    */
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.
429      */
430     sendto_opmask_butone(0, SNO_TCPCOMMON, "Unable to accept connection: %m");
431     return;
432   }
433   /*
434    * check for connection limit
435    */
436   if (fd > MAXCLIENTS - 1) {
437     ++ServerStats->is_ref;
438     send(fd, "ERROR :All connections in use\r\n", 32, 0);
439     close(fd);
440     return;
441   }
442   /*
443    * check to see if listener is shutting down
444    */
445   if (!listener->active) {
446     ++ServerStats->is_ref;
447     send(fd, "ERROR :Use another port\r\n", 25, 0);
448     close(fd);
449     return;
450   }
451   /*
452    * check to see if connection is allowed for this address mask
453    */
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);
457     close(fd);
458     return;
459   }
460 #if 0
461   /*
462    * check conf for ip address access
463    */
464   if (!conf_connect_allowed(addr.sin_addr)) {
465     ++ServerStats->is_ref;
466     send(fd, "ERROR :Not authorized\r\n", 23, 0);
467     close(fd);
468     return;
469   }
470 #endif
471   ++ServerStats->is_ac;
472   nextping = CurrentTime;
473
474   add_connection(listener, fd);
475 }
476
477