]> jfr.im git - irc/rqf/shadowircd.git/blob - src/listener.c
Oups, change for last commit
[irc/rqf/shadowircd.git] / src / listener.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * listener.c: Listens on a port.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: listener.c 3460 2007-05-18 20:31:33Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "setup.h"
29 #include "listener.h"
30 #include "client.h"
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
33 #include "ircd.h"
34 #include "ircd_defs.h"
35 #include "numeric.h"
36 #include "s_conf.h"
37 #include "s_newconf.h"
38 #include "s_stats.h"
39 #include "send.h"
40 #include "s_auth.h"
41 #include "reject.h"
42 #include "s_conf.h"
43 #include "hostmask.h"
44
45 #ifndef INADDR_NONE
46 #define INADDR_NONE ((unsigned int) 0xffffffff)
47 #endif
48
49 #if defined(NO_IN6ADDR_ANY) && defined(IPV6)
50 static const struct in6_addr in6addr_any =
51 { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
52 #endif
53
54 static listener_t *ListenerPollList = NULL;
55 static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
56 static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
57
58 static listener_t *
59 make_listener(struct rb_sockaddr_storage *addr)
60 {
61 listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
62 s_assert(0 != listener);
63 listener->name = me.name;
64 listener->F = NULL;
65
66 memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
67 listener->next = NULL;
68 return listener;
69 }
70
71 void
72 free_listener(listener_t *listener)
73 {
74 s_assert(NULL != listener);
75 if(listener == NULL)
76 return;
77 /*
78 * remove from listener list
79 */
80 if(listener == ListenerPollList)
81 ListenerPollList = listener->next;
82 else
83 {
84 listener_t *prev = ListenerPollList;
85 for (; prev; prev = prev->next)
86 {
87 if(listener == prev->next)
88 {
89 prev->next = listener->next;
90 break;
91 }
92 }
93 }
94
95 /* free */
96 rb_free(listener);
97 }
98
99 #define PORTNAMELEN 6 /* ":31337" */
100
101 /*
102 * get_listener_name - return displayable listener name and port
103 * returns "host.foo.org:6667" for a given listener
104 */
105 const char *
106 get_listener_name(const listener_t *listener)
107 {
108 static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
109 int port = 0;
110
111 s_assert(NULL != listener);
112 if(listener == NULL)
113 return NULL;
114
115 #ifdef IPV6
116 if(listener->addr.ss_family == AF_INET6)
117 port = ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
118 else
119 #endif
120 port = ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
121
122 rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", me.name, listener->name, port);
123 return buf;
124 }
125
126 /*
127 * show_ports - send port listing to a client
128 * inputs - pointer to client to show ports to
129 * output - none
130 * side effects - show ports
131 */
132 void
133 show_ports(struct Client *source_p)
134 {
135 listener_t *listener = 0;
136
137 for (listener = ListenerPollList; listener; listener = listener->next)
138 {
139 sendto_one_numeric(source_p, RPL_STATSPLINE,
140 form_str(RPL_STATSPLINE), 'P',
141 #ifdef IPV6
142 ntohs(listener->addr.ss_family == AF_INET ? ((struct sockaddr_in *)&listener->addr)->sin_port :
143 ((struct sockaddr_in6 *)&listener->addr)->sin6_port),
144 #else
145 ntohs(((struct sockaddr_in *)&listener->addr)->sin_port),
146 #endif
147 IsOperAdmin(source_p) ? listener->name : me.name,
148 listener->ref_count, (listener->active) ? "active" : "disabled");
149 }
150 }
151
152 /*
153 * inetport - create a listener socket in the AF_INET or AF_INET6 domain,
154 * bind it to the port given in 'port' and listen to it
155 * returns true (1) if successful false (0) on error.
156 *
157 * If the operating system has a define for SOMAXCONN, use it, otherwise
158 * use RATBOX_SOMAXCONN
159 */
160 #ifdef SOMAXCONN
161 #undef RATBOX_SOMAXCONN
162 #define RATBOX_SOMAXCONN SOMAXCONN
163 #endif
164
165 static int
166 inetport(listener_t *listener)
167 {
168 rb_fde_t *F;
169 int ret;
170 int opt = 1;
171
172 /*
173 * At first, open a new socket
174 */
175
176 F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
177
178 #ifdef IPV6
179 if(listener->addr.ss_family == AF_INET6)
180 {
181 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
182 if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
183 {
184 inetntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
185 listener->name = listener->vhost;
186 }
187 } else
188 #endif
189 {
190 struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
191 if(in->sin_addr.s_addr != INADDR_ANY)
192 {
193 inetntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
194 listener->name = listener->vhost;
195 }
196 }
197
198 if(F == NULL)
199 {
200 report_error("opening listener socket %s:%s",
201 get_listener_name(listener),
202 get_listener_name(listener), errno);
203 return 0;
204 }
205 else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
206 {
207 report_error("no more connections left for listener %s:%s",
208 get_listener_name(listener),
209 get_listener_name(listener), errno);
210 rb_close(F);
211 return 0;
212 }
213
214 /*
215 * XXX - we don't want to do all this crap for a listener
216 * set_sock_opts(listener);
217 */
218 if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
219 {
220 report_error("setting SO_REUSEADDR for listener %s:%s",
221 get_listener_name(listener),
222 get_listener_name(listener), errno);
223 rb_close(F);
224 return 0;
225 }
226
227 /*
228 * Bind a port to listen for new connections if port is non-null,
229 * else assume it is already open and try get something from it.
230 */
231
232 if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
233 {
234 report_error("binding listener socket %s:%s",
235 get_listener_name(listener),
236 get_listener_name(listener), errno);
237 rb_close(F);
238 return 0;
239 }
240
241 if((ret = rb_listen(F, RATBOX_SOMAXCONN)))
242 {
243 report_error("listen failed for %s:%s",
244 get_listener_name(listener),
245 get_listener_name(listener), errno);
246 rb_close(F);
247 return 0;
248 }
249
250 listener->F = F;
251
252 rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener);
253 return 1;
254 }
255
256 static listener_t *
257 find_listener(struct rb_sockaddr_storage *addr)
258 {
259 listener_t *listener = NULL;
260 listener_t *last_closed = NULL;
261
262 for (listener = ListenerPollList; listener; listener = listener->next)
263 {
264 if(addr->ss_family != listener->addr.ss_family)
265 continue;
266
267 switch(addr->ss_family)
268 {
269 case AF_INET:
270 {
271 struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
272 struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
273 if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
274 in4->sin_port == lin4->sin_port )
275 {
276 if(listener->F == NULL)
277 last_closed = listener;
278 else
279 return(listener);
280 }
281 break;
282 }
283 #ifdef IPV6
284 case AF_INET6:
285 {
286 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
287 struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
288 if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
289 in6->sin6_port == lin6->sin6_port)
290 {
291 if(listener->F == NULL)
292 last_closed = listener;
293 else
294 return(listener);
295 }
296 break;
297
298 }
299 #endif
300
301 default:
302 break;
303 }
304 }
305 return last_closed;
306 }
307
308
309 /*
310 * add_listener- create a new listener
311 * port - the port number to listen on
312 * vhost_ip - if non-null must contain a valid IP address string in
313 * the format "255.255.255.255"
314 */
315 void
316 add_listener(int port, const char *vhost_ip, int family)
317 {
318 listener_t *listener;
319 struct rb_sockaddr_storage vaddr;
320
321 /*
322 * if no port in conf line, don't bother
323 */
324 if(port == 0)
325 return;
326 memset(&vaddr, 0, sizeof(vaddr));
327 vaddr.ss_family = family;
328
329 if(vhost_ip != NULL)
330 {
331 if(family == AF_INET)
332 {
333 if(inetpton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
334 return;
335 }
336 #ifdef IPV6
337 else
338 {
339 if(inetpton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
340 return;
341
342 }
343 #endif
344 } else
345 {
346 switch(family)
347 {
348 case AF_INET:
349 ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
350 break;
351 #ifdef IPV6
352 case AF_INET6:
353 memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
354 break;
355 default:
356 return;
357 #endif
358 }
359 }
360 switch(family)
361 {
362 case AF_INET:
363 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
364 ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
365 break;
366 #ifdef IPV6
367 case AF_INET6:
368 SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
369 ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
370 break;
371 #endif
372 default:
373 break;
374 }
375 if((listener = find_listener(&vaddr)))
376 {
377 if(listener->F != NULL)
378 return;
379 }
380 else
381 {
382 listener = make_listener(&vaddr);
383 listener->next = ListenerPollList;
384 ListenerPollList = listener;
385 }
386
387 listener->F = NULL;
388
389 if(inetport(listener))
390 listener->active = 1;
391 else
392 close_listener(listener);
393 }
394
395 /*
396 * close_listener - close a single listener
397 */
398 void
399 close_listener(listener_t *listener)
400 {
401 s_assert(listener != NULL);
402 if(listener == NULL)
403 return;
404 if(listener->F != NULL)
405 {
406 rb_close(listener->F);
407 listener->F = NULL;
408 }
409
410 listener->active = 0;
411
412 if(listener->ref_count)
413 return;
414
415 free_listener(listener);
416 }
417
418 /*
419 * close_listeners - close and free all listeners that are not being used
420 */
421 void
422 close_listeners()
423 {
424 listener_t *listener;
425 listener_t *listener_next = 0;
426 /*
427 * close all 'extra' listening ports we have
428 */
429 for (listener = ListenerPollList; listener; listener = listener_next)
430 {
431 listener_next = listener->next;
432 close_listener(listener);
433 }
434 }
435
436 #define DLINE_WARNING "ERROR :You have been D-lined.\r\n"
437
438 /*
439 * add_connection - creates a client which has just connected to us on
440 * the given fd. The sockhost field is initialized with the ip# of the host.
441 * The client is sent to the auth module for verification, and not put in
442 * any client list yet.
443 */
444 static void
445 add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, int exempt)
446 {
447 struct Client *new_client;
448 s_assert(NULL != listener);
449
450 /*
451 * get the client socket name from the socket
452 * the client has already been checked out in accept_connection
453 */
454 new_client = make_client(NULL);
455
456 memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
457
458 /*
459 * copy address to 'sockhost' as a string, copy it to host too
460 * so we have something valid to put into error messages...
461 */
462 inetntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost,
463 sizeof(new_client->sockhost));
464
465
466 strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
467
468 new_client->localClient->F = F;
469
470 new_client->localClient->listener = listener;
471 ++listener->ref_count;
472
473 if(!exempt)
474 {
475 if(check_reject(new_client))
476 return;
477 if(add_unknown_ip(new_client))
478 return;
479 }
480
481 start_auth(new_client);
482 }
483
484 static int
485 accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
486 {
487 struct Listener *listener = (struct Listener *)data;
488 char buf[BUFSIZE];
489 struct ConfItem *aconf;
490 static time_t last_oper_notice = 0;
491
492 if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */
493 {
494 ++ServerStats->is_ref;
495 /*
496 * slow down the whining to opers bit
497 */
498 if((last_oper_notice + 20) <= rb_current_time())
499 {
500 sendto_realops_flags(SNO_GENERAL, L_ALL,
501 "All connections in use. (%s)",
502 get_listener_name(listener));
503 last_oper_notice = rb_current_time();
504 }
505
506 rb_write(F, "ERROR :All connections in use\r\n", 32);
507 rb_close(F);
508 /* Re-register a new IO request for the next accept .. */
509 return 0;
510 }
511
512 aconf = find_dline(addr, AF_INET);
513 if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE))
514 return 1;
515
516 /* Do an initial check we aren't connecting too fast or with too many
517 * from this IP... */
518 if(aconf != NULL)
519 {
520 ServerStats->is_ref++;
521
522 if(ConfigFileEntry.dline_with_reason)
523 {
524 if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))
525 {
526 buf[sizeof(buf) - 3] = '\r';
527 buf[sizeof(buf) - 2] = '\n';
528 buf[sizeof(buf) - 1] = '\0';
529 }
530 }
531 else
532 strcpy(buf, "ERROR :You have been D-lined.\r\n");
533
534 rb_write(F, buf, strlen(buf));
535 rb_close(F);
536 return 0;
537 }
538
539 return 1;
540 }
541
542 static void
543 accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
544 {
545 struct Listener *listener = data;
546 struct rb_sockaddr_storage lip;
547 unsigned int locallen = sizeof(struct rb_sockaddr_storage);
548
549 ServerStats->is_ac++;
550
551 if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
552 {
553 /* this shouldn't fail so... */
554 /* XXX add logging of this */
555 rb_close(F);
556 }
557
558 add_connection(listener, F, addr, 1);
559 }