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