ce7ddc7065a8aa66762fe2cb36cddde00f95634f
[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 struct Listener* ListenerPollList = 0;
51
52 static struct Listener* make_listener(int port, struct in_addr addr)
53 {
54   struct Listener* listener = 
55     (struct Listener*) MyMalloc(sizeof(struct Listener));
56   assert(0 != listener);
57
58   memset(listener, 0, sizeof(struct Listener));
59
60   listener->fd          = -1;
61   listener->port        = port;
62   listener->addr.s_addr = addr.s_addr;
63
64 #ifdef NULL_POINTER_NOT_ZERO
65   listener->next = NULL;
66   listener->conf = NULL;
67 #endif
68   return listener;
69 }
70
71 static void free_listener(struct Listener* listener)
72 {
73   assert(0 != listener);
74   MyFree(listener);
75 }
76
77 #define PORTNAMELEN 10  /* ":31337" */
78
79 /*
80  * get_listener_name - return displayable listener name and port
81  * returns "host.foo.org:6667" for a given listener
82  */
83 const char* get_listener_name(const struct Listener* listener)
84 {
85   static char buf[HOSTLEN + PORTNAMELEN + 4];
86   assert(0 != listener);
87   sprintf_irc(buf, "%s:%u", me.name, listener->port);
88   return buf;
89 }
90
91 /*
92  * count_listener_memory - count memory and listeners
93  */
94 void count_listener_memory(int* count_out, size_t* size_out)
95 {
96   struct Listener* l;
97   int              count = 0;
98   assert(0 != count_out);
99   assert(0 != size_out);
100   for (l = ListenerPollList; l; l = l->next)
101     ++count;
102   *count_out = count;
103   *size_out  = count * sizeof(struct Listener);
104 }
105   
106 /*
107  * show_ports - send port listing to a client
108  * inputs       - pointer to client to show ports to
109  * output       - none
110  * side effects - show ports
111  * author       - Dianora
112  */
113 void show_ports(struct Client* sptr, int show_hidden, int port, int count)
114 {
115   struct Listener* listener = 0;
116   char             flags[8];
117   assert(0 != sptr);
118
119   for (listener = ListenerPollList; listener; listener = listener->next) {
120     if (port && port != listener->port)
121       continue;
122     flags[0] = (listener->server) ? 'S' : 'C';
123     if (listener->hidden) {
124       if (!show_hidden)
125         continue;
126       flags[1] = 'H';
127       flags[2] = '\0';
128     }
129     else
130       flags[1] = '\0';
131
132     send_reply(sptr, RPL_STATSPLINE, listener->port, listener->ref_count,
133                flags, (listener->active) ? "active" : "disabled");
134     if (--count == 0)
135       break;
136   }
137 }
138
139 /*
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.
143  *
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
147  */
148 #ifdef SOMAXCONN
149 #define HYBRID_SOMAXCONN SOMAXCONN
150 #else
151 #define HYBRID_SOMAXCONN 64
152 #endif
153
154 static int inetport(struct Listener* listener)
155 {
156   struct sockaddr_in sin;
157   int                fd;
158
159   /*
160    * At first, open a new socket
161    */
162   if (-1 == (fd = socket(AF_INET, SOCK_STREAM, 0))) {
163     report_error(SOCKET_ERROR_MSG, get_listener_name(listener), errno);
164     return 0;
165   }
166   else if (fd > MAXCLIENTS - 1) {
167     report_error(CONNLIMIT_ERROR_MSG, get_listener_name(listener), 0);
168     close(fd);
169     return 0;
170   }
171
172   if (!os_set_reuseaddr(fd)) {
173     report_error(REUSEADDR_ERROR_MSG, get_listener_name(listener), errno);
174     close(fd);
175     return 0;
176   }
177   /*
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.
180    */
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);
185
186   if (bind(fd, (struct sockaddr*) &sin, sizeof(sin))) {
187     report_error(BIND_ERROR_MSG, get_listener_name(listener), errno);
188     close(fd);
189     return 0;
190   }
191   /*
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
197    */
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);
200     close(fd);
201     return 0;
202   }
203   if (!os_set_listen(fd, HYBRID_SOMAXCONN)) {
204     report_error(LISTEN_ERROR_MSG, get_listener_name(listener), errno);
205     close(fd);
206     return 0;
207   }
208   /*
209    * XXX - this should always work, performance will suck if it doesn't
210    */
211   if (!os_set_nonblocking(fd)) {
212     report_error(NONB_ERROR_MSG, get_listener_name(listener), errno);
213     close(fd);
214     return 0;
215   }
216   listener->fd = fd;
217
218   return 1;
219 }
220
221 /*
222  * find_listener - find a listener in the list
223  *
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)
226  */
227 static struct Listener* find_listener(int port, struct in_addr addr)
228 {
229   struct Listener* listener;
230   for (listener = ListenerPollList; listener; listener = listener->next) {
231     if (port == listener->port && addr.s_addr == listener->addr.s_addr)
232       return listener;
233   }
234   return 0;
235 }
236
237 /*
238  * set_listener_mask - set the connection mask for this listener
239  */
240 static void set_listener_mask(struct Listener* listener, const char* mask)
241 {
242   int  ad[4];
243   char ipname[20];
244
245   assert(0 != listener);
246
247   if (EmptyString(mask) && strcmp(mask,"*")!=0) {
248     listener->mask.s_addr = 0;
249     return;
250   }
251   ad[0] = ad[1] = ad[2] = ad[3] = 0;
252   /*
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 :-)
256    */
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);
260 }
261
262 /*
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
265  */
266 static int connection_allowed(const char* addr, const char* mask)
267 {
268   int i = 4;
269   for ( ; i > 0; --i) {
270     if (*mask && *addr != *mask)
271       break;
272     ++addr;
273     ++mask;
274   }
275   return (0 == i);
276 }
277
278
279 /*
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"
284  */
285 void add_listener(int port, const char* vhost_ip, const char* mask,
286                   int is_server, int is_hidden) 
287 {
288   struct Listener* listener;
289   struct in_addr   vaddr;
290
291   /*
292    * if no port in conf line, don't bother
293    */
294   if (0 == port)
295     return;
296
297   vaddr.s_addr = INADDR_ANY;
298
299   if (!EmptyString(vhost_ip) && strcmp(vhost_ip,"*") != 0) {
300     vaddr.s_addr = inet_addr(vhost_ip);
301     if (INADDR_NONE == vaddr.s_addr)
302       return;
303   }
304
305   if ((listener = find_listener(port, vaddr))) {
306     /*
307      * set active flag and change connect mask here, it's the only thing 
308      * that can change on a rehash
309      */
310     listener->active = 1;
311     set_listener_mask(listener, mask);
312     listener->hidden = is_hidden;
313     listener->server = is_server;
314     return;
315   }
316
317   listener = make_listener(port, vaddr);
318
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; 
326   }
327   else
328     free_listener(listener);
329 }
330
331 /*
332  * mark_listeners_closing - iterate through listeners and mark them as
333  * inactive
334  */
335 void mark_listeners_closing(void)
336 {
337   struct Listener* listener;
338   for (listener = ListenerPollList; listener; listener = listener->next)
339     listener->active = 0;
340 }
341
342 /*
343  * close_listener - close a single listener
344  */
345 void close_listener(struct Listener* listener)
346 {
347   assert(0 != listener);
348   /*
349    * remove from listener list
350    */
351   if (listener == ListenerPollList)
352     ListenerPollList = listener->next;
353   else {
354     struct Listener* prev = ListenerPollList;
355     for ( ; prev; prev = prev->next) {
356       if (listener == prev->next) {
357         prev->next = listener->next;
358         break; 
359       }
360     }
361   }
362   if (-1 < listener->fd)
363     close(listener->fd);
364   free_listener(listener);
365 }
366  
367 /*
368  * close_listeners - close and free all listeners that are not being used
369  */
370 void close_listeners()
371 {
372   struct Listener* listener;
373   struct Listener* listener_next = 0;
374   /*
375    * close all 'extra' listening ports we have
376    */
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);
381   }
382 }
383
384 void release_listener(struct Listener* listener)
385 {
386   assert(0 != listener);
387   assert(0 < listener->ref_count);
388   if (0 == --listener->ref_count && !listener->active)
389     close_listener(listener);
390 }
391
392 /*
393  * accept_connection - accept a connection on a listener
394  */
395 void accept_connection(struct Listener* listener)
396 {
397   struct sockaddr_in addr = { 0 };
398   unsigned int       addrlen = sizeof(struct sockaddr_in);
399   int                fd;
400
401   assert(0 != listener);
402
403   listener->last_accept = CurrentTime;
404   /*
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.
414    */
415   if (-1 == (fd = accept(listener->fd, (struct sockaddr*) &addr, &addrlen)))
416     return;
417   /*
418    * check for connection limit
419    */
420   if (fd > MAXCLIENTS - 1) {
421     ++ServerStats->is_ref;
422     send(fd, "ERROR :All connections in use\r\n", 32, 0);
423     close(fd);
424     return;
425   }
426   /*
427    * check to see if listener is shutting down
428    */
429   if (!listener->active) {
430     ++ServerStats->is_ref;
431     send(fd, "ERROR :Use another port\r\n", 25, 0);
432     close(fd);
433     return;
434   }
435   /*
436    * check to see if connection is allowed for this address mask
437    */
438   if (!connection_allowed((const char*) &addr, (const char*) &listener->mask)) {
439     ++ServerStats->is_ref;
440     send(fd, "ERROR :Use another port\r\n", 25, 0);
441     close(fd);
442     return;
443   }
444 #if 0
445   /*
446    * check conf for ip address access
447    */
448   if (!conf_connect_allowed(addr.sin_addr)) {
449     ++ServerStats->is_ref;
450     send(fd, "ERROR :Not authorized\r\n", 23, 0);
451     close(fd);
452     return;
453   }
454 #endif
455   ++ServerStats->is_ac;
456   nextping = CurrentTime;
457
458   add_connection(listener, fd);
459 }
460
461