ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/listener.c
Revision: 1798
Committed: Sun Mar 31 17:09:50 2013 UTC (10 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 11035 byte(s)
Log Message:
- Cleanup/reorganize header file layout
- Fixed naming convention in some places

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * listener.c: Listens on a port.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26 michael 1011 #include "list.h"
27 adx 30 #include "listener.h"
28     #include "client.h"
29     #include "fdlist.h"
30     #include "irc_string.h"
31     #include "ircd.h"
32     #include "ircd_defs.h"
33     #include "s_bsd.h"
34     #include "numeric.h"
35 michael 1309 #include "conf.h"
36 adx 30 #include "send.h"
37     #include "memory.h"
38    
39     static PF accept_connection;
40    
41     static dlink_list ListenerPollList = { NULL, NULL, 0 };
42     static void close_listener(struct Listener *listener);
43    
44     static struct Listener *
45     make_listener(int port, struct irc_ssaddr *addr)
46     {
47     struct Listener *listener = MyMalloc(sizeof(struct Listener));
48    
49     listener->port = port;
50     memcpy(&listener->addr, addr, sizeof(struct irc_ssaddr));
51    
52     return listener;
53     }
54    
55     void
56     free_listener(struct Listener *listener)
57     {
58     assert(listener != NULL);
59    
60 michael 1798 dlinkDelete(&listener->node, &ListenerPollList);
61 adx 30 MyFree(listener);
62     }
63    
64     /*
65     * get_listener_name - return displayable listener name and port
66 michael 1242 * returns "host.foo.org/6667" for a given listener
67 adx 30 */
68     const char *
69 michael 1123 get_listener_name(const struct Listener *const listener)
70 adx 30 {
71     static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
72    
73 michael 1123 snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name,
74     listener->name, listener->port);
75 michael 900 return buf;
76 adx 30 }
77    
78     /* show_ports()
79     *
80     * inputs - pointer to client to show ports to
81     * output - none
82     * side effects - send port listing to a client
83     */
84     void
85     show_ports(struct Client *source_p)
86     {
87 michael 900 char buf[6];
88 adx 30 char *p = NULL;
89     dlink_node *ptr;
90    
91     DLINK_FOREACH(ptr, ListenerPollList.head)
92     {
93     const struct Listener *listener = ptr->data;
94     p = buf;
95    
96     if (listener->flags & LISTENER_HIDDEN) {
97 michael 1219 if (!HasUMode(source_p, UMODE_ADMIN))
98 adx 30 continue;
99     *p++ = 'H';
100     }
101    
102 michael 900 if (listener->flags & LISTENER_SERVER)
103     *p++ = 'S';
104 adx 30 if (listener->flags & LISTENER_SSL)
105     *p++ = 's';
106     *p = '\0';
107     sendto_one(source_p, form_str(RPL_STATSPLINE),
108     me.name, source_p->name, 'P', listener->port,
109 michael 1219 HasUMode(source_p, UMODE_ADMIN) ? listener->name : me.name,
110 adx 30 listener->ref_count, buf,
111     listener->active ? "active" : "disabled");
112     }
113     }
114    
115     /*
116     * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
117     * bind it to the port given in 'port' and listen to it
118     * returns true (1) if successful false (0) on error.
119     *
120     * If the operating system has a define for SOMAXCONN, use it, otherwise
121     * use HYBRID_SOMAXCONN
122     */
123     #ifdef SOMAXCONN
124     #undef HYBRID_SOMAXCONN
125     #define HYBRID_SOMAXCONN SOMAXCONN
126     #endif
127    
128     static int
129     inetport(struct Listener *listener)
130     {
131     struct irc_ssaddr lsin;
132     socklen_t opt = 1;
133    
134 michael 1123 memset(&lsin, 0, sizeof(lsin));
135     memcpy(&lsin, &listener->addr, sizeof(lsin));
136    
137     getnameinfo((struct sockaddr *)&lsin, lsin.ss_len, listener->name,
138     sizeof(listener->name), NULL, 0, NI_NUMERICHOST);
139    
140 adx 30 /*
141     * At first, open a new socket
142     */
143     if (comm_open(&listener->fd, listener->addr.ss.ss_family, SOCK_STREAM, 0,
144     "Listener socket") == -1)
145     {
146     report_error(L_ALL, "opening listener socket %s:%s",
147     get_listener_name(listener), errno);
148     return 0;
149     }
150    
151     if (setsockopt(listener->fd.fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
152     {
153     report_error(L_ALL, "setting SO_REUSEADDR for listener %s:%s",
154     get_listener_name(listener), errno);
155     fd_close(&listener->fd);
156 michael 900 return 0;
157 adx 30 }
158    
159     /*
160     * Bind a port to listen for new connections if port is non-null,
161     * else assume it is already open and try get something from it.
162     */
163     lsin.ss_port = htons(listener->port);
164    
165     if (bind(listener->fd.fd, (struct sockaddr *)&lsin, lsin.ss_len))
166     {
167     report_error(L_ALL, "binding listener socket %s:%s",
168     get_listener_name(listener), errno);
169     fd_close(&listener->fd);
170 michael 900 return 0;
171 adx 30 }
172    
173     if (listen(listener->fd.fd, HYBRID_SOMAXCONN))
174     {
175     report_error(L_ALL, "listen failed for %s:%s",
176     get_listener_name(listener), errno);
177     fd_close(&listener->fd);
178 michael 900 return 0;
179 adx 30 }
180    
181     /* Listen completion events are READ events .. */
182    
183     accept_connection(&listener->fd, listener);
184     return 1;
185     }
186    
187     static struct Listener *
188     find_listener(int port, struct irc_ssaddr *addr)
189     {
190     dlink_node *ptr;
191     struct Listener *listener = NULL;
192     struct Listener *last_closed = NULL;
193    
194     DLINK_FOREACH(ptr, ListenerPollList.head)
195     {
196     listener = ptr->data;
197    
198     if ((port == listener->port) &&
199     (!memcmp(addr, &listener->addr, sizeof(struct irc_ssaddr))))
200     {
201     /* Try to return an open listener, otherwise reuse a closed one */
202     if (!listener->fd.flags.open)
203     last_closed = listener;
204     else
205     return (listener);
206     }
207     }
208    
209     return (last_closed);
210     }
211    
212     /*
213     * add_listener- create a new listener
214     * port - the port number to listen on
215     * vhost_ip - if non-null must contain a valid IP address string in
216     * the format "255.255.255.255"
217     */
218     void
219     add_listener(int port, const char *vhost_ip, unsigned int flags)
220     {
221     struct Listener *listener;
222     struct irc_ssaddr vaddr;
223     struct addrinfo hints, *res;
224     char portname[PORTNAMELEN + 1];
225     #ifdef IPV6
226     static short int pass = 0; /* if ipv6 and no address specified we need to
227     have two listeners; one for each protocol. */
228     #endif
229    
230     /*
231     * if no or invalid port in conf line, don't bother
232     */
233     if (!(port > 0 && port <= 0xFFFF))
234     return;
235    
236     memset(&vaddr, 0, sizeof(vaddr));
237    
238     /* Set up the hints structure */
239     memset(&hints, 0, sizeof(hints));
240     hints.ai_family = AF_UNSPEC;
241     hints.ai_socktype = SOCK_STREAM;
242     /* Get us ready for a bind() and don't bother doing dns lookup */
243     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
244    
245     #ifdef IPV6
246     if (ServerInfo.can_use_v6)
247     {
248 michael 1123 snprintf(portname, sizeof(portname), "%d", port);
249     getaddrinfo("::", portname, &hints, &res);
250 adx 30 vaddr.ss.ss_family = AF_INET6;
251     assert(res != NULL);
252    
253     memcpy((struct sockaddr*)&vaddr, res->ai_addr, res->ai_addrlen);
254     vaddr.ss_port = port;
255     vaddr.ss_len = res->ai_addrlen;
256 michael 1123 freeaddrinfo(res);
257 adx 30 }
258     else
259     #endif
260     {
261     struct sockaddr_in *v4 = (struct sockaddr_in*) &vaddr;
262     v4->sin_addr.s_addr = INADDR_ANY;
263     vaddr.ss.ss_family = AF_INET;
264     vaddr.ss_len = sizeof(struct sockaddr_in);
265     v4->sin_port = htons(port);
266     }
267    
268     snprintf(portname, PORTNAMELEN, "%d", port);
269    
270 michael 1669 if (!EmptyString(vhost_ip))
271 adx 30 {
272 michael 1123 if (getaddrinfo(vhost_ip, portname, &hints, &res))
273     return;
274 adx 30
275     assert(res != NULL);
276    
277     memcpy((struct sockaddr*)&vaddr, res->ai_addr, res->ai_addrlen);
278     vaddr.ss_port = port;
279     vaddr.ss_len = res->ai_addrlen;
280 michael 1123 freeaddrinfo(res);
281 adx 30 }
282     #ifdef IPV6
283     else if (pass == 0 && ServerInfo.can_use_v6)
284     {
285     /* add the ipv4 listener if we havent already */
286     pass = 1;
287     add_listener(port, "0.0.0.0", flags);
288     }
289     pass = 0;
290     #endif
291    
292     if ((listener = find_listener(port, &vaddr)))
293     {
294     listener->flags = flags;
295     if (listener->fd.flags.open)
296     return;
297     }
298     else
299     {
300     listener = make_listener(port, &vaddr);
301 michael 1798 dlinkAdd(listener, &listener->node, &ListenerPollList);
302 adx 30 listener->flags = flags;
303     }
304    
305     if (inetport(listener))
306     listener->active = 1;
307     else
308     close_listener(listener);
309     }
310    
311     /*
312     * close_listener - close a single listener
313     */
314     static void
315     close_listener(struct Listener *listener)
316     {
317     assert(listener != NULL);
318    
319     if (listener == NULL)
320     return;
321    
322     if (listener->fd.flags.open)
323     fd_close(&listener->fd);
324    
325     listener->active = 0;
326    
327     if (listener->ref_count)
328     return;
329    
330     free_listener(listener);
331     }
332    
333     /*
334     * close_listeners - close and free all listeners that are not being used
335     */
336     void
337     close_listeners(void)
338     {
339 michael 1123 dlink_node *ptr = NULL, *next_ptr = NULL;
340 adx 30
341     /* close all 'extra' listening ports we have */
342     DLINK_FOREACH_SAFE(ptr, next_ptr, ListenerPollList.head)
343     close_listener(ptr->data);
344     }
345    
346     #define TOOFAST_WARNING "ERROR :Trying to reconnect too fast.\r\n"
347     #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
348    
349     static void
350     accept_connection(fde_t *pfd, void *data)
351     {
352     static time_t last_oper_notice = 0;
353     struct irc_ssaddr addr;
354     int fd;
355     int pe;
356     struct Listener *listener = data;
357    
358     memset(&addr, 0, sizeof(addr));
359    
360     assert(listener != NULL);
361    
362     /* There may be many reasons for error return, but
363     * in otherwise correctly working environment the
364     * probable cause is running out of file descriptors
365     * (EMFILE, ENFILE or others?). The man pages for
366     * accept don't seem to list these as possible,
367     * although it's obvious that it may happen here.
368     * Thus no specific errors are tested at this
369     * point, just assume that connections cannot
370     * be accepted until some old is closed first.
371     */
372 michael 563 while ((fd = comm_accept(listener, &addr)) != -1)
373 adx 30 {
374     /*
375     * check for connection limit
376     */
377     if (number_fd > hard_fdlimit - 10)
378     {
379 michael 896 ++ServerStats.is_ref;
380 michael 563
381 adx 30 /*
382     * slow down the whining to opers bit
383     */
384     if ((last_oper_notice + 20) <= CurrentTime)
385     {
386 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
387     "All connections in use. (%s)",
388 adx 30 get_listener_name(listener));
389     last_oper_notice = CurrentTime;
390     }
391    
392     if (!(listener->flags & LISTENER_SSL))
393     send(fd, "ERROR :All connections in use\r\n", 32, 0);
394 michael 1001
395 adx 30 close(fd);
396     break; /* jump out and re-register a new io request */
397     }
398    
399 michael 563 /*
400     * Do an initial check we aren't connecting too fast or with too many
401     * from this IP...
402     */
403     if ((pe = conf_connect_allowed(&addr, addr.ss.ss_family)) != 0)
404 adx 30 {
405 michael 896 ++ServerStats.is_ref;
406    
407 adx 30 if (!(listener->flags & LISTENER_SSL))
408     switch (pe)
409     {
410     case BANNED_CLIENT:
411     send(fd, DLINE_WARNING, sizeof(DLINE_WARNING)-1, 0);
412     break;
413     case TOO_FAST:
414     send(fd, TOOFAST_WARNING, sizeof(TOOFAST_WARNING)-1, 0);
415     break;
416     }
417    
418     close(fd);
419     continue; /* drop the one and keep on clearing the queue */
420     }
421    
422 michael 896 ++ServerStats.is_ac;
423 michael 549 add_connection(listener, &addr, fd);
424 adx 30 }
425    
426     /* Re-register a new IO request for the next accept .. */
427     comm_setselect(&listener->fd, COMM_SELECT_READ, accept_connection,
428     listener, 0);
429     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision