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