]>
Commit | Line | Data |
---|---|---|
54ac8b60 VY |
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" | |
4562c604 | 31 | #include "match.h" |
54ac8b60 VY |
32 | #include "ircd.h" |
33 | #include "ircd_defs.h" | |
34 | #include "numeric.h" | |
35 | #include "s_conf.h" | |
36 | #include "s_newconf.h" | |
37 | #include "s_stats.h" | |
38 | #include "send.h" | |
39 | #include "s_auth.h" | |
40 | #include "reject.h" | |
41 | #include "s_conf.h" | |
42 | #include "hostmask.h" | |
c6d72037 VY |
43 | #include "sslproc.h" |
44 | #include "hash.h" | |
77d3d2db KB |
45 | #include "s_assert.h" |
46 | #include "logger.h" | |
54ac8b60 VY |
47 | |
48 | #ifndef INADDR_NONE | |
49 | #define INADDR_NONE ((unsigned int) 0xffffffff) | |
50 | #endif | |
51 | ||
9ea3ea10 | 52 | #if defined(NO_IN6ADDR_ANY) && defined(RB_IPV6) |
54ac8b60 VY |
53 | static const struct in6_addr in6addr_any = |
54 | { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }; | |
55 | #endif | |
56 | ||
8f103562 | 57 | static struct Listener *ListenerPollList = NULL; |
1087485c | 58 | static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data); |
8e09c4a2 | 59 | static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data); |
54ac8b60 | 60 | |
8f103562 | 61 | static struct Listener * |
e7046ee5 | 62 | make_listener(struct rb_sockaddr_storage *addr) |
54ac8b60 | 63 | { |
8f103562 | 64 | struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener)); |
54ac8b60 | 65 | s_assert(0 != listener); |
54ac8b60 | 66 | listener->name = me.name; |
f691939a VY |
67 | listener->F = NULL; |
68 | ||
e7046ee5 | 69 | memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage)); |
54ac8b60 VY |
70 | listener->next = NULL; |
71 | return listener; | |
72 | } | |
73 | ||
74 | void | |
8f103562 | 75 | free_listener(struct Listener *listener) |
54ac8b60 VY |
76 | { |
77 | s_assert(NULL != listener); | |
78 | if(listener == NULL) | |
79 | return; | |
80 | /* | |
81 | * remove from listener list | |
82 | */ | |
83 | if(listener == ListenerPollList) | |
84 | ListenerPollList = listener->next; | |
85 | else | |
86 | { | |
8f103562 | 87 | struct Listener *prev = ListenerPollList; |
54ac8b60 VY |
88 | for (; prev; prev = prev->next) |
89 | { | |
90 | if(listener == prev->next) | |
91 | { | |
92 | prev->next = listener->next; | |
93 | break; | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | /* free */ | |
99 | rb_free(listener); | |
100 | } | |
101 | ||
102 | #define PORTNAMELEN 6 /* ":31337" */ | |
103 | ||
40173bcb JT |
104 | /* |
105 | * get_listener_port - return displayable listener port | |
106 | */ | |
107 | static uint16_t | |
108 | get_listener_port(const struct Listener *listener) | |
109 | { | |
110 | #ifdef RB_IPV6 | |
111 | if(listener->addr.ss_family == AF_INET6) | |
112 | return ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port); | |
113 | else | |
114 | #endif | |
115 | return ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port); | |
116 | } | |
117 | ||
54ac8b60 VY |
118 | /* |
119 | * get_listener_name - return displayable listener name and port | |
120 | * returns "host.foo.org:6667" for a given listener | |
121 | */ | |
122 | const char * | |
8f103562 | 123 | get_listener_name(const struct Listener *listener) |
54ac8b60 VY |
124 | { |
125 | static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4]; | |
54ac8b60 VY |
126 | |
127 | s_assert(NULL != listener); | |
128 | if(listener == NULL) | |
129 | return NULL; | |
130 | ||
40173bcb JT |
131 | rb_snprintf(buf, sizeof(buf), "%s[%s/%u]", |
132 | me.name, listener->name, get_listener_port(listener)); | |
54ac8b60 VY |
133 | return buf; |
134 | } | |
135 | ||
136 | /* | |
137 | * show_ports - send port listing to a client | |
138 | * inputs - pointer to client to show ports to | |
139 | * output - none | |
140 | * side effects - show ports | |
141 | */ | |
142 | void | |
143 | show_ports(struct Client *source_p) | |
144 | { | |
8f103562 | 145 | struct Listener *listener = 0; |
54ac8b60 VY |
146 | |
147 | for (listener = ListenerPollList; listener; listener = listener->next) | |
148 | { | |
149 | sendto_one_numeric(source_p, RPL_STATSPLINE, | |
40173bcb JT |
150 | form_str(RPL_STATSPLINE), 'P', |
151 | get_listener_port(listener), | |
54ac8b60 | 152 | IsOperAdmin(source_p) ? listener->name : me.name, |
8bd5767b | 153 | listener->ref_count, (listener->active) ? "active" : "disabled", |
c6d72037 | 154 | listener->ssl ? " ssl" : ""); |
54ac8b60 VY |
155 | } |
156 | } | |
157 | ||
158 | /* | |
159 | * inetport - create a listener socket in the AF_INET or AF_INET6 domain, | |
160 | * bind it to the port given in 'port' and listen to it | |
161 | * returns true (1) if successful false (0) on error. | |
162 | * | |
163 | * If the operating system has a define for SOMAXCONN, use it, otherwise | |
164 | * use RATBOX_SOMAXCONN | |
165 | */ | |
166 | #ifdef SOMAXCONN | |
167 | #undef RATBOX_SOMAXCONN | |
168 | #define RATBOX_SOMAXCONN SOMAXCONN | |
169 | #endif | |
170 | ||
171 | static int | |
8f103562 | 172 | inetport(struct Listener *listener) |
54ac8b60 | 173 | { |
f691939a | 174 | rb_fde_t *F; |
54ac8b60 | 175 | int opt = 1; |
40173bcb | 176 | const char *errstr; |
54ac8b60 VY |
177 | |
178 | /* | |
179 | * At first, open a new socket | |
180 | */ | |
181 | ||
f691939a | 182 | F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket"); |
54ac8b60 | 183 | |
9ea3ea10 | 184 | #ifdef RB_IPV6 |
54ac8b60 VY |
185 | if(listener->addr.ss_family == AF_INET6) |
186 | { | |
187 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr; | |
188 | if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any)) | |
189 | { | |
caa4d9d2 | 190 | rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost)); |
54ac8b60 VY |
191 | listener->name = listener->vhost; |
192 | } | |
193 | } else | |
194 | #endif | |
195 | { | |
196 | struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr; | |
197 | if(in->sin_addr.s_addr != INADDR_ANY) | |
198 | { | |
caa4d9d2 | 199 | rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost)); |
54ac8b60 VY |
200 | listener->name = listener->vhost; |
201 | } | |
202 | } | |
203 | ||
1087485c JT |
204 | if(F == NULL) |
205 | { | |
40173bcb JT |
206 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
207 | "Cannot open socket for listener on port %d", | |
208 | get_listener_port(listener)); | |
209 | ilog(L_MAIN, "Cannot open socket for listener %s", | |
210 | get_listener_name(listener)); | |
1087485c JT |
211 | return 0; |
212 | } | |
213 | else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/ | |
214 | { | |
825ddf13 | 215 | ilog_error("no more connections left for listener"); |
40173bcb JT |
216 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
217 | "No more connections left for listener on port %d", | |
218 | get_listener_port(listener)); | |
219 | ilog(L_MAIN, "No more connections left for listener %s", | |
220 | get_listener_name(listener)); | |
1087485c JT |
221 | rb_close(F); |
222 | return 0; | |
54ac8b60 VY |
223 | } |
224 | ||
1087485c JT |
225 | /* |
226 | * XXX - we don't want to do all this crap for a listener | |
227 | * set_sock_opts(listener); | |
228 | */ | |
229 | if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt))) | |
230 | { | |
40173bcb JT |
231 | errstr = strerror(rb_get_sockerr(F)); |
232 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
233 | "Cannot set SO_REUSEADDR for listener on port %d: %s", | |
234 | get_listener_port(listener), errstr); | |
235 | ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s", | |
236 | get_listener_name(listener), errstr); | |
1087485c JT |
237 | rb_close(F); |
238 | return 0; | |
54ac8b60 VY |
239 | } |
240 | ||
1087485c JT |
241 | /* |
242 | * Bind a port to listen for new connections if port is non-null, | |
243 | * else assume it is already open and try get something from it. | |
244 | */ | |
245 | ||
246 | if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr))) | |
247 | { | |
40173bcb JT |
248 | errstr = strerror(rb_get_sockerr(F)); |
249 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
250 | "Cannot bind for listener on port %d: %s", | |
251 | get_listener_port(listener), errstr); | |
252 | ilog(L_MAIN, "Cannot bind for listener %s: %s", | |
253 | get_listener_name(listener), errstr); | |
1087485c JT |
254 | rb_close(F); |
255 | return 0; | |
54ac8b60 VY |
256 | } |
257 | ||
02270e96 | 258 | if(rb_listen(F, RATBOX_SOMAXCONN, listener->defer_accept)) |
1087485c | 259 | { |
40173bcb JT |
260 | errstr = strerror(rb_get_sockerr(F)); |
261 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
262 | "Cannot listen() for listener on port %d: %s", | |
263 | get_listener_port(listener), errstr); | |
264 | ilog(L_MAIN, "Cannot listen() for listener %s: %s", | |
265 | get_listener_name(listener), errstr); | |
1087485c JT |
266 | rb_close(F); |
267 | return 0; | |
54ac8b60 VY |
268 | } |
269 | ||
1087485c JT |
270 | listener->F = F; |
271 | ||
f691939a | 272 | rb_accept_tcp(listener->F, accept_precallback, accept_callback, listener); |
54ac8b60 VY |
273 | return 1; |
274 | } | |
275 | ||
8f103562 | 276 | static struct Listener * |
e7046ee5 | 277 | find_listener(struct rb_sockaddr_storage *addr) |
54ac8b60 | 278 | { |
8f103562 JT |
279 | struct Listener *listener = NULL; |
280 | struct Listener *last_closed = NULL; | |
54ac8b60 VY |
281 | |
282 | for (listener = ListenerPollList; listener; listener = listener->next) | |
283 | { | |
284 | if(addr->ss_family != listener->addr.ss_family) | |
285 | continue; | |
286 | ||
287 | switch(addr->ss_family) | |
288 | { | |
289 | case AF_INET: | |
290 | { | |
291 | struct sockaddr_in *in4 = (struct sockaddr_in *)addr; | |
292 | struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr; | |
293 | if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr && | |
294 | in4->sin_port == lin4->sin_port ) | |
295 | { | |
1087485c JT |
296 | if(listener->F == NULL) |
297 | last_closed = listener; | |
298 | else | |
54ac8b60 VY |
299 | return(listener); |
300 | } | |
301 | break; | |
302 | } | |
9ea3ea10 | 303 | #ifdef RB_IPV6 |
54ac8b60 VY |
304 | case AF_INET6: |
305 | { | |
306 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; | |
307 | struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr; | |
308 | if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) && | |
309 | in6->sin6_port == lin6->sin6_port) | |
310 | { | |
1087485c JT |
311 | if(listener->F == NULL) |
312 | last_closed = listener; | |
313 | else | |
54ac8b60 VY |
314 | return(listener); |
315 | } | |
316 | break; | |
317 | ||
318 | } | |
319 | #endif | |
320 | ||
321 | default: | |
322 | break; | |
323 | } | |
324 | } | |
325 | return last_closed; | |
326 | } | |
327 | ||
328 | ||
329 | /* | |
330 | * add_listener- create a new listener | |
331 | * port - the port number to listen on | |
332 | * vhost_ip - if non-null must contain a valid IP address string in | |
333 | * the format "255.255.255.255" | |
334 | */ | |
335 | void | |
02270e96 | 336 | add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept) |
54ac8b60 | 337 | { |
8f103562 | 338 | struct Listener *listener; |
e7046ee5 | 339 | struct rb_sockaddr_storage vaddr; |
54ac8b60 VY |
340 | |
341 | /* | |
342 | * if no port in conf line, don't bother | |
343 | */ | |
344 | if(port == 0) | |
345 | return; | |
346 | memset(&vaddr, 0, sizeof(vaddr)); | |
347 | vaddr.ss_family = family; | |
348 | ||
349 | if(vhost_ip != NULL) | |
350 | { | |
351 | if(family == AF_INET) | |
352 | { | |
caa4d9d2 | 353 | if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0) |
54ac8b60 VY |
354 | return; |
355 | } | |
9ea3ea10 | 356 | #ifdef RB_IPV6 |
54ac8b60 VY |
357 | else |
358 | { | |
caa4d9d2 | 359 | if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0) |
54ac8b60 VY |
360 | return; |
361 | ||
362 | } | |
363 | #endif | |
364 | } else | |
365 | { | |
366 | switch(family) | |
367 | { | |
368 | case AF_INET: | |
369 | ((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY; | |
370 | break; | |
9ea3ea10 | 371 | #ifdef RB_IPV6 |
54ac8b60 VY |
372 | case AF_INET6: |
373 | memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr)); | |
374 | break; | |
375 | default: | |
376 | return; | |
377 | #endif | |
378 | } | |
379 | } | |
380 | switch(family) | |
381 | { | |
382 | case AF_INET: | |
99c4835f | 383 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in)); |
54ac8b60 VY |
384 | ((struct sockaddr_in *)&vaddr)->sin_port = htons(port); |
385 | break; | |
9ea3ea10 | 386 | #ifdef RB_IPV6 |
54ac8b60 | 387 | case AF_INET6: |
99c4835f | 388 | SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6)); |
54ac8b60 VY |
389 | ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port); |
390 | break; | |
391 | #endif | |
392 | default: | |
393 | break; | |
394 | } | |
395 | if((listener = find_listener(&vaddr))) | |
396 | { | |
1087485c | 397 | if(listener->F != NULL) |
54ac8b60 VY |
398 | return; |
399 | } | |
400 | else | |
401 | { | |
402 | listener = make_listener(&vaddr); | |
403 | listener->next = ListenerPollList; | |
404 | ListenerPollList = listener; | |
405 | } | |
406 | ||
f691939a | 407 | listener->F = NULL; |
c6d72037 | 408 | listener->ssl = ssl; |
02270e96 | 409 | listener->defer_accept = defer_accept; |
54ac8b60 VY |
410 | |
411 | if(inetport(listener)) | |
412 | listener->active = 1; | |
413 | else | |
414 | close_listener(listener); | |
415 | } | |
416 | ||
417 | /* | |
418 | * close_listener - close a single listener | |
419 | */ | |
420 | void | |
8f103562 | 421 | close_listener(struct Listener *listener) |
54ac8b60 VY |
422 | { |
423 | s_assert(listener != NULL); | |
424 | if(listener == NULL) | |
425 | return; | |
1087485c JT |
426 | if(listener->F != NULL) |
427 | { | |
428 | rb_close(listener->F); | |
429 | listener->F = NULL; | |
54ac8b60 VY |
430 | } |
431 | ||
432 | listener->active = 0; | |
433 | ||
434 | if(listener->ref_count) | |
435 | return; | |
436 | ||
437 | free_listener(listener); | |
438 | } | |
439 | ||
440 | /* | |
441 | * close_listeners - close and free all listeners that are not being used | |
442 | */ | |
443 | void | |
444 | close_listeners() | |
445 | { | |
8f103562 JT |
446 | struct Listener *listener; |
447 | struct Listener *listener_next = 0; | |
54ac8b60 VY |
448 | /* |
449 | * close all 'extra' listening ports we have | |
450 | */ | |
451 | for (listener = ListenerPollList; listener; listener = listener_next) | |
452 | { | |
453 | listener_next = listener->next; | |
454 | close_listener(listener); | |
455 | } | |
456 | } | |
457 | ||
458 | #define DLINE_WARNING "ERROR :You have been D-lined.\r\n" | |
459 | ||
460 | /* | |
461 | * add_connection - creates a client which has just connected to us on | |
462 | * the given fd. The sockhost field is initialized with the ip# of the host. | |
463 | * The client is sent to the auth module for verification, and not put in | |
464 | * any client list yet. | |
465 | */ | |
1087485c | 466 | static void |
3540120a | 467 | add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai, void *ssl_ctl) |
54ac8b60 VY |
468 | { |
469 | struct Client *new_client; | |
470 | s_assert(NULL != listener); | |
471 | ||
472 | /* | |
473 | * get the client socket name from the socket | |
474 | * the client has already been checked out in accept_connection | |
475 | */ | |
476 | new_client = make_client(NULL); | |
477 | ||
e7046ee5 | 478 | memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage)); |
3540120a | 479 | memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage)); |
54ac8b60 VY |
480 | |
481 | /* | |
482 | * copy address to 'sockhost' as a string, copy it to host too | |
483 | * so we have something valid to put into error messages... | |
484 | */ | |
caa4d9d2 | 485 | rb_inet_ntop_sock((struct sockaddr *)&new_client->localClient->ip, new_client->sockhost, |
54ac8b60 VY |
486 | sizeof(new_client->sockhost)); |
487 | ||
488 | ||
f427c8b0 | 489 | rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
54ac8b60 | 490 | |
99c4835f | 491 | new_client->localClient->F = F; |
c6d72037 | 492 | add_to_cli_fd_hash(new_client); |
54ac8b60 | 493 | new_client->localClient->listener = listener; |
8bd5767b JT |
494 | new_client->localClient->ssl_ctl = ssl_ctl; |
495 | if(ssl_ctl != NULL || rb_fd_ssl(F)) | |
c6d72037 VY |
496 | SetSSL(new_client); |
497 | ||
54ac8b60 VY |
498 | ++listener->ref_count; |
499 | ||
54ac8b60 VY |
500 | start_auth(new_client); |
501 | } | |
0d89d5cd | 502 | |
43946961 JT |
503 | static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n"; |
504 | ||
1087485c JT |
505 | static int |
506 | accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
507 | { | |
508 | struct Listener *listener = (struct Listener *)data; | |
509 | char buf[BUFSIZE]; | |
510 | struct ConfItem *aconf; | |
511 | static time_t last_oper_notice = 0; | |
b52c2949 | 512 | int len; |
1087485c | 513 | |
8bd5767b JT |
514 | if(listener->ssl && (!ssl_ok || !get_ssld_count())) |
515 | { | |
516 | rb_close(F); | |
517 | return 0; | |
c6d72037 VY |
518 | } |
519 | ||
1087485c JT |
520 | if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus */ |
521 | { | |
47adde3d | 522 | ++ServerStats.is_ref; |
1087485c JT |
523 | /* |
524 | * slow down the whining to opers bit | |
525 | */ | |
526 | if((last_oper_notice + 20) <= rb_current_time()) | |
527 | { | |
26716d6d | 528 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
1087485c JT |
529 | "All connections in use. (%s)", |
530 | get_listener_name(listener)); | |
531 | last_oper_notice = rb_current_time(); | |
532 | } | |
533 | ||
534 | rb_write(F, "ERROR :All connections in use\r\n", 32); | |
535 | rb_close(F); | |
1087485c JT |
536 | return 0; |
537 | } | |
538 | ||
41d7fefa | 539 | aconf = find_dline(addr, addr->sa_family); |
1087485c JT |
540 | if(aconf != NULL && (aconf->status & CONF_EXEMPTDLINE)) |
541 | return 1; | |
542 | ||
543 | /* Do an initial check we aren't connecting too fast or with too many | |
544 | * from this IP... */ | |
545 | if(aconf != NULL) | |
546 | { | |
47adde3d | 547 | ServerStats.is_ref++; |
1087485c JT |
548 | |
549 | if(ConfigFileEntry.dline_with_reason) | |
550 | { | |
a12ad044 | 551 | len = rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf)); |
b52c2949 | 552 | if (len >= (int)(sizeof(buf)-1)) |
1087485c JT |
553 | { |
554 | buf[sizeof(buf) - 3] = '\r'; | |
555 | buf[sizeof(buf) - 2] = '\n'; | |
556 | buf[sizeof(buf) - 1] = '\0'; | |
557 | } | |
558 | } | |
559 | else | |
560 | strcpy(buf, "ERROR :You have been D-lined.\r\n"); | |
561 | ||
562 | rb_write(F, buf, strlen(buf)); | |
563 | rb_close(F); | |
564 | return 0; | |
565 | } | |
566 | ||
43946961 JT |
567 | if(check_reject(F, addr)) |
568 | return 0; | |
569 | ||
570 | if(throttle_add(addr)) | |
571 | { | |
572 | rb_write(F, toofast, strlen(toofast)); | |
573 | rb_close(F); | |
574 | return 0; | |
575 | } | |
576 | ||
1087485c JT |
577 | return 1; |
578 | } | |
579 | ||
8bd5767b JT |
580 | static void |
581 | accept_ssld(rb_fde_t *F, struct sockaddr *addr, struct sockaddr *laddr, struct Listener *listener) | |
582 | { | |
583 | ssl_ctl_t *ctl; | |
584 | rb_fde_t *xF[2]; | |
6388eda6 JT |
585 | if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1) |
586 | { | |
587 | ilog_error("creating SSL/TLS socket pairs"); | |
588 | rb_close(F); | |
589 | return; | |
590 | } | |
8bd5767b | 591 | ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */ |
3540120a | 592 | add_connection(listener, xF[0], addr, laddr, ctl); |
c6d72037 VY |
593 | } |
594 | ||
1087485c JT |
595 | static void |
596 | accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data) | |
597 | { | |
598 | struct Listener *listener = data; | |
599 | struct rb_sockaddr_storage lip; | |
600 | unsigned int locallen = sizeof(struct rb_sockaddr_storage); | |
601 | ||
47adde3d | 602 | ServerStats.is_ac++; |
1087485c JT |
603 | |
604 | if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0) | |
605 | { | |
d60a42a2 | 606 | /* this can fail if the connection disappeared in the meantime */ |
1087485c | 607 | rb_close(F); |
9692f954 | 608 | return; |
1087485c JT |
609 | } |
610 | ||
8bd5767b JT |
611 | if(listener->ssl) |
612 | accept_ssld(F, addr, (struct sockaddr *)&lip, listener); | |
613 | else | |
3540120a | 614 | add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL); |
0d89d5cd | 615 | } |