ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 2725
Committed: Sun Dec 29 13:01:00 2013 UTC (11 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 21890 byte(s)
Log Message:
- Fixed bug where ircd didn't timeout SSL connections that haven't
  finished the SSL handshake. Reported by Adam.

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * s_bsd.c: Network functions.
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 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <netinet/tcp.h>
29 #include "list.h"
30 #include "fdlist.h"
31 #include "s_bsd.h"
32 #include "client.h"
33 #include "dbuf.h"
34 #include "event.h"
35 #include "irc_string.h"
36 #include "ircd.h"
37 #include "listener.h"
38 #include "numeric.h"
39 #include "packet.h"
40 #include "irc_res.h"
41 #include "restart.h"
42 #include "s_auth.h"
43 #include "conf.h"
44 #include "log.h"
45 #include "s_serv.h"
46 #include "send.h"
47 #include "memory.h"
48 #include "s_user.h"
49 #include "hook.h"
50
51 static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
52 "Error during DNS lookup", "connect timeout", "Error during connect()",
53 "Comm Error" };
54
55 static void comm_connect_callback(fde_t *, int);
56 static PF comm_connect_timeout;
57 static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
58 static PF comm_connect_tryconnect;
59
60
61 /* check_can_use_v6()
62 * Check if the system can open AF_INET6 sockets
63 */
64 void
65 check_can_use_v6(void)
66 {
67 #ifdef IPV6
68 int v6;
69
70 if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
71 ServerInfo.can_use_v6 = 0;
72 else
73 {
74 ServerInfo.can_use_v6 = 1;
75 close(v6);
76 }
77 #else
78 ServerInfo.can_use_v6 = 0;
79 #endif
80 }
81
82 /* get_sockerr - get the error value from the socket or the current errno
83 *
84 * Get the *real* error from the socket (well try to anyway..).
85 * This may only work when SO_DEBUG is enabled but its worth the
86 * gamble anyway.
87 */
88 int
89 get_sockerr(int fd)
90 {
91 int errtmp = errno;
92 #ifdef SO_ERROR
93 int err = 0;
94 socklen_t len = sizeof(err);
95
96 if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
97 {
98 if (err)
99 errtmp = err;
100 }
101 errno = errtmp;
102 #endif
103 return errtmp;
104 }
105
106 /*
107 * report_error - report an error from an errno.
108 * Record error to log and also send a copy to all *LOCAL* opers online.
109 *
110 * text is a *format* string for outputing error. It must
111 * contain only two '%s', the first will be replaced
112 * by the sockhost from the client_p, and the latter will
113 * be taken from sys_errlist[errno].
114 *
115 * client_p if not NULL, is the *LOCAL* client associated with
116 * the error.
117 *
118 * Cannot use perror() within daemon. stderr is closed in
119 * ircd and cannot be used. And, worse yet, it might have
120 * been reassigned to a normal connection...
121 *
122 * Actually stderr is still there IFF ircd was run with -s --Rodder
123 */
124
125 void
126 report_error(int level, const char* text, const char* who, int error)
127 {
128 who = (who) ? who : "";
129
130 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
131 text, who, strerror(error));
132 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
133 }
134
135 /*
136 * setup_socket()
137 *
138 * Set the socket non-blocking, and other wonderful bits.
139 */
140 static void
141 setup_socket(int fd)
142 {
143 int opt = 1;
144
145 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
146
147 #ifdef IPTOS_LOWDELAY
148 opt = IPTOS_LOWDELAY;
149 setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
150 #endif
151
152 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
153 }
154
155 /*
156 * close_connection
157 * Close the physical connection. This function must make
158 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
159 */
160 void
161 close_connection(struct Client *client_p)
162 {
163 dlink_node *ptr = NULL;
164
165 assert(client_p);
166
167 if (!IsDead(client_p))
168 {
169 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
170 /* there is still a chance that we might send data to this socket
171 * even if it is marked as blocked (COMM_SELECT_READ handler is called
172 * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
173 */
174 ClearSendqBlocked(client_p);
175 send_queued_write(client_p);
176 }
177
178 if (IsClient(client_p))
179 {
180 ++ServerStats.is_cl;
181 ServerStats.is_cbs += client_p->localClient->send.bytes;
182 ServerStats.is_cbr += client_p->localClient->recv.bytes;
183 ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
184 }
185 else if (IsServer(client_p))
186 {
187 ++ServerStats.is_sv;
188 ServerStats.is_sbs += client_p->localClient->send.bytes;
189 ServerStats.is_sbr += client_p->localClient->recv.bytes;
190 ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
191
192 DLINK_FOREACH(ptr, server_items.head)
193 {
194 struct MaskItem *conf = ptr->data;
195
196 if (irccmp(conf->name, client_p->name))
197 continue;
198
199 /*
200 * Reset next-connect cycle of all connect{} blocks that match
201 * this servername.
202 */
203 conf->until = CurrentTime + conf->class->con_freq;
204 }
205 }
206 else
207 ++ServerStats.is_ni;
208
209 #ifdef HAVE_LIBCRYPTO
210 if (client_p->localClient->fd.ssl)
211 {
212 SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
213
214 if (!SSL_shutdown(client_p->localClient->fd.ssl))
215 SSL_shutdown(client_p->localClient->fd.ssl);
216 }
217 #endif
218 if (client_p->localClient->fd.flags.open)
219 fd_close(&client_p->localClient->fd);
220
221 dbuf_clear(&client_p->localClient->buf_sendq);
222 dbuf_clear(&client_p->localClient->buf_recvq);
223
224 MyFree(client_p->localClient->passwd);
225 detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
226 client_p->from = NULL; /* ...this should catch them! >:) --msa */
227 }
228
229 #ifdef HAVE_LIBCRYPTO
230 /*
231 * ssl_handshake - let OpenSSL initialize the protocol. Register for
232 * read/write events if necessary.
233 */
234 static void
235 ssl_handshake(int fd, struct Client *client_p)
236 {
237 X509 *cert = NULL;
238 int ret = 0;
239
240 if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0)
241 {
242 if ((CurrentTime - client_p->localClient->firsttime) > 30)
243 {
244 exit_client(client_p, client_p, "Timeout during SSL handshake");
245 return;
246 }
247
248 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
249 {
250 case SSL_ERROR_WANT_WRITE:
251 comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
252 (PF *) ssl_handshake, client_p, 30);
253 return;
254
255 case SSL_ERROR_WANT_READ:
256 comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
257 (PF *) ssl_handshake, client_p, 30);
258 return;
259
260 default:
261 exit_client(client_p, client_p, "Error during SSL handshake");
262 return;
263 }
264 }
265
266 if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
267 {
268 int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
269 char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
270 unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
271
272 if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
273 res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
274 res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
275 {
276 unsigned int i = 0, n = 0;
277
278 if (X509_digest(cert, EVP_sha256(), md, &n))
279 {
280 for (; i < n; ++i)
281 snprintf(buf + 2 * i, 3, "%02X", md[i]);
282 client_p->certfp = xstrdup(buf);
283 }
284 }
285 else
286 ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
287 client_p->name, client_p->username, client_p->host, res);
288 X509_free(cert);
289 }
290
291 start_auth(client_p);
292 }
293 #endif
294
295 /*
296 * add_connection - creates a client which has just connected to us on
297 * the given fd. The sockhost field is initialized with the ip# of the host.
298 * An unique id is calculated now, in case it is needed for auth.
299 * The client is sent to the auth module for verification, and not put in
300 * any client list yet.
301 */
302 void
303 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
304 {
305 struct Client *new_client = make_client(NULL);
306
307 fd_open(&new_client->localClient->fd, fd, 1,
308 (listener->flags & LISTENER_SSL) ?
309 "Incoming SSL connection" : "Incoming connection");
310
311 /*
312 * copy address to 'sockhost' as a string, copy it to host too
313 * so we have something valid to put into error messages...
314 */
315 memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
316
317 getnameinfo((struct sockaddr *)&new_client->localClient->ip,
318 new_client->localClient->ip.ss_len, new_client->sockhost,
319 sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
320 new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
321
322 #ifdef HAVE_LIBGEOIP
323 /* XXX IPV6 SUPPORT XXX */
324 if (irn->ss.ss_family == AF_INET && geoip_ctx)
325 {
326 const struct sockaddr_in *v4 = (const struct sockaddr_in *)&new_client->localClient->ip;
327 new_client->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
328 }
329 #endif
330
331 if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
332 {
333 strlcpy(new_client->host, "0", sizeof(new_client->host));
334 strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
335 memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
336 new_client->sockhost[0] = '0';
337 }
338 else
339 strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
340
341 new_client->localClient->listener = listener;
342 ++listener->ref_count;
343
344 #ifdef HAVE_LIBCRYPTO
345 if (listener->flags & LISTENER_SSL)
346 {
347 if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
348 {
349 ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
350 ERR_error_string(ERR_get_error(), NULL));
351
352 SetDead(new_client);
353 exit_client(new_client, new_client, "SSL_new failed");
354 return;
355 }
356
357 AddFlag(new_client, FLAGS_SSL);
358 SSL_set_fd(new_client->localClient->fd.ssl, fd);
359 ssl_handshake(0, new_client);
360 }
361 else
362 #endif
363 start_auth(new_client);
364 }
365
366 /*
367 * stolen from squid - its a neat (but overused! :) routine which we
368 * can use to see whether we can ignore this errno or not. It is
369 * generally useful for non-blocking network IO related errnos.
370 * -- adrian
371 */
372 int
373 ignoreErrno(int ierrno)
374 {
375 switch (ierrno)
376 {
377 case EINPROGRESS:
378 case EWOULDBLOCK:
379 #if EAGAIN != EWOULDBLOCK
380 case EAGAIN:
381 #endif
382 case EALREADY:
383 case EINTR:
384 #ifdef ERESTART
385 case ERESTART:
386 #endif
387 return 1;
388 default:
389 return 0;
390 }
391 }
392
393 /*
394 * comm_settimeout() - set the socket timeout
395 *
396 * Set the timeout for the fd
397 */
398 void
399 comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
400 {
401 assert(fd->flags.open);
402
403 fd->timeout = CurrentTime + (timeout / 1000);
404 fd->timeout_handler = callback;
405 fd->timeout_data = cbdata;
406 }
407
408 /*
409 * comm_setflush() - set a flush function
410 *
411 * A flush function is simply a function called if found during
412 * comm_timeouts(). Its basically a second timeout, except in this case
413 * I'm too lazy to implement multiple timeout functions! :-)
414 * its kinda nice to have it separate, since this is designed for
415 * flush functions, and when comm_close() is implemented correctly
416 * with close functions, we _actually_ don't call comm_close() here ..
417 * -- originally Adrian's notes
418 * comm_close() is replaced with fd_close() in fdlist.c
419 */
420 void
421 comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
422 {
423 assert(fd->flags.open);
424
425 fd->flush_timeout = CurrentTime + (timeout / 1000);
426 fd->flush_handler = callback;
427 fd->flush_data = cbdata;
428 }
429
430 /*
431 * comm_checktimeouts() - check the socket timeouts
432 *
433 * All this routine does is call the given callback/cbdata, without closing
434 * down the file descriptor. When close handlers have been implemented,
435 * this will happen.
436 */
437 void
438 comm_checktimeouts(void *notused)
439 {
440 int i;
441 fde_t *F;
442 PF *hdl;
443 void *data;
444
445 for (i = 0; i < FD_HASH_SIZE; i++)
446 for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop)
447 {
448 assert(F->flags.open);
449 fd_next_in_loop = F->hnext;
450
451 /* check flush functions */
452 if (F->flush_handler && F->flush_timeout > 0 &&
453 F->flush_timeout < CurrentTime)
454 {
455 hdl = F->flush_handler;
456 data = F->flush_data;
457 comm_setflush(F, 0, NULL, NULL);
458 hdl(F, data);
459 }
460
461 /* check timeouts */
462 if (F->timeout_handler && F->timeout > 0 &&
463 F->timeout < CurrentTime)
464 {
465 /* Call timeout handler */
466 hdl = F->timeout_handler;
467 data = F->timeout_data;
468 comm_settimeout(F, 0, NULL, NULL);
469 hdl(F, data);
470 }
471 }
472 }
473
474 /*
475 * void comm_connect_tcp(int fd, const char *host, unsigned short port,
476 * struct sockaddr *clocal, int socklen,
477 * CNCB *callback, void *data, int aftype, int timeout)
478 * Input: An fd to connect with, a host and port to connect to,
479 * a local sockaddr to connect from + length(or NULL to use the
480 * default), a callback, the data to pass into the callback, the
481 * address family.
482 * Output: None.
483 * Side-effects: A non-blocking connection to the host is started, and
484 * if necessary, set up for selection. The callback given
485 * may be called now, or it may be called later.
486 */
487 void
488 comm_connect_tcp(fde_t *fd, const char *host, unsigned short port,
489 struct sockaddr *clocal, int socklen, CNCB *callback,
490 void *data, int aftype, int timeout)
491 {
492 struct addrinfo hints, *res;
493 char portname[PORTNAMELEN + 1];
494
495 assert(callback);
496 fd->connect.callback = callback;
497 fd->connect.data = data;
498
499 fd->connect.hostaddr.ss.ss_family = aftype;
500 fd->connect.hostaddr.ss_port = htons(port);
501
502 /* Note that we're using a passed sockaddr here. This is because
503 * generally you'll be bind()ing to a sockaddr grabbed from
504 * getsockname(), so this makes things easier.
505 * XXX If NULL is passed as local, we should later on bind() to the
506 * virtual host IP, for completeness.
507 * -- adrian
508 */
509 if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
510 {
511 /* Failure, call the callback with COMM_ERR_BIND */
512 comm_connect_callback(fd, COMM_ERR_BIND);
513 /* ... and quit */
514 return;
515 }
516
517 /* Next, if we have been given an IP, get the addr and skip the
518 * DNS check (and head direct to comm_connect_tryconnect().
519 */
520 memset(&hints, 0, sizeof(hints));
521 hints.ai_family = AF_UNSPEC;
522 hints.ai_socktype = SOCK_STREAM;
523 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
524
525 snprintf(portname, sizeof(portname), "%d", port);
526
527 if (getaddrinfo(host, portname, &hints, &res))
528 {
529 /* Send the DNS request, for the next level */
530 if (aftype == AF_INET6)
531 gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
532 else
533 gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
534 }
535 else
536 {
537 /* We have a valid IP, so we just call tryconnect */
538 /* Make sure we actually set the timeout here .. */
539 assert(res != NULL);
540 memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
541 fd->connect.hostaddr.ss_len = res->ai_addrlen;
542 fd->connect.hostaddr.ss.ss_family = res->ai_family;
543 freeaddrinfo(res);
544 comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
545 comm_connect_tryconnect(fd, NULL);
546 }
547 }
548
549 /*
550 * comm_connect_callback() - call the callback, and continue with life
551 */
552 static void
553 comm_connect_callback(fde_t *fd, int status)
554 {
555 CNCB *hdl;
556
557 /* This check is gross..but probably necessary */
558 if (fd->connect.callback == NULL)
559 return;
560
561 /* Clear the connect flag + handler */
562 hdl = fd->connect.callback;
563 fd->connect.callback = NULL;
564
565 /* Clear the timeout handler */
566 comm_settimeout(fd, 0, NULL, NULL);
567
568 /* Call the handler */
569 hdl(fd, status, fd->connect.data);
570 }
571
572 /*
573 * comm_connect_timeout() - this gets called when the socket connection
574 * times out. This *only* can be called once connect() is initially
575 * called ..
576 */
577 static void
578 comm_connect_timeout(fde_t *fd, void *notused)
579 {
580 /* error! */
581 comm_connect_callback(fd, COMM_ERR_TIMEOUT);
582 }
583
584 /*
585 * comm_connect_dns_callback() - called at the completion of the DNS request
586 *
587 * The DNS request has completed, so if we've got an error, return it,
588 * otherwise we initiate the connect()
589 */
590 static void
591 comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
592 {
593 fde_t *F = vptr;
594
595 if (name == NULL)
596 {
597 comm_connect_callback(F, COMM_ERR_DNS);
598 return;
599 }
600
601 /* No error, set a 10 second timeout */
602 comm_settimeout(F, 30*1000, comm_connect_timeout, NULL);
603
604 /* Copy over the DNS reply info so we can use it in the connect() */
605 /*
606 * Note we don't fudge the refcount here, because we aren't keeping
607 * the DNS record around, and the DNS cache is gone anyway..
608 * -- adrian
609 */
610 memcpy(&F->connect.hostaddr, addr, addr->ss_len);
611 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
612 ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
613 F->connect.hostaddr.ss_port;
614 F->connect.hostaddr.ss_len = addr->ss_len;
615
616 /* Now, call the tryconnect() routine to try a connect() */
617 comm_connect_tryconnect(F, NULL);
618 }
619
620 /* static void comm_connect_tryconnect(int fd, void *notused)
621 * Input: The fd, the handler data(unused).
622 * Output: None.
623 * Side-effects: Try and connect with pending connect data for the FD. If
624 * we succeed or get a fatal error, call the callback.
625 * Otherwise, it is still blocking or something, so register
626 * to select for a write event on this FD.
627 */
628 static void
629 comm_connect_tryconnect(fde_t *fd, void *notused)
630 {
631 int retval;
632
633 /* This check is needed or re-entrant s_bsd_* like sigio break it. */
634 if (fd->connect.callback == NULL)
635 return;
636
637 /* Try the connect() */
638 retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
639 fd->connect.hostaddr.ss_len);
640
641 /* Error? */
642 if (retval < 0)
643 {
644 /*
645 * If we get EISCONN, then we've already connect()ed the socket,
646 * which is a good thing.
647 * -- adrian
648 */
649 if (errno == EISCONN)
650 comm_connect_callback(fd, COMM_OK);
651 else if (ignoreErrno(errno))
652 /* Ignore error? Reschedule */
653 comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect,
654 NULL, 0);
655 else
656 /* Error? Fail with COMM_ERR_CONNECT */
657 comm_connect_callback(fd, COMM_ERR_CONNECT);
658 return;
659 }
660
661 /* If we get here, we've suceeded, so call with COMM_OK */
662 comm_connect_callback(fd, COMM_OK);
663 }
664
665 /*
666 * comm_errorstr() - return an error string for the given error condition
667 */
668 const char *
669 comm_errstr(int error)
670 {
671 if (error < 0 || error >= COMM_ERR_MAX)
672 return "Invalid error number!";
673 return comm_err_str[error];
674 }
675
676 /*
677 * comm_open() - open a socket
678 *
679 * This is a highly highly cut down version of squid's comm_open() which
680 * for the most part emulates socket(), *EXCEPT* it fails if we're about
681 * to run out of file descriptors.
682 */
683 int
684 comm_open(fde_t *F, int family, int sock_type, int proto, const char *note)
685 {
686 int fd;
687
688 /* First, make sure we aren't going to run out of file descriptors */
689 if (number_fd >= hard_fdlimit)
690 {
691 errno = ENFILE;
692 return -1;
693 }
694
695 /*
696 * Next, we try to open the socket. We *should* drop the reserved FD
697 * limit if/when we get an error, but we can deal with that later.
698 * XXX !!! -- adrian
699 */
700 fd = socket(family, sock_type, proto);
701 if (fd < 0)
702 return -1; /* errno will be passed through, yay.. */
703
704 setup_socket(fd);
705
706 /* update things in our fd tracking */
707 fd_open(F, fd, 1, note);
708 return 0;
709 }
710
711 /*
712 * comm_accept() - accept an incoming connection
713 *
714 * This is a simple wrapper for accept() which enforces FD limits like
715 * comm_open() does. Returned fd must be either closed or tagged with
716 * fd_open (this function no longer does it).
717 */
718 int
719 comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
720 {
721 int newfd;
722 socklen_t addrlen = sizeof(struct irc_ssaddr);
723
724 if (number_fd >= hard_fdlimit)
725 {
726 errno = ENFILE;
727 return -1;
728 }
729
730 /*
731 * Next, do the accept(). if we get an error, we should drop the
732 * reserved fd limit, but we can deal with that when comm_open()
733 * also does it. XXX -- adrian
734 */
735 newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
736 if (newfd < 0)
737 return -1;
738
739 #ifdef IPV6
740 remove_ipv6_mapping(pn);
741 #else
742 pn->ss_len = addrlen;
743 #endif
744
745 setup_socket(newfd);
746
747 /* .. and return */
748 return newfd;
749 }
750
751 /*
752 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
753 * OSes with IPv6 mapping listening on both
754 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
755 *
756 */
757 #ifdef IPV6
758 void
759 remove_ipv6_mapping(struct irc_ssaddr *addr)
760 {
761 if (addr->ss.ss_family == AF_INET6)
762 {
763 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
764 {
765 struct sockaddr_in6 v6;
766 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
767
768 memcpy(&v6, addr, sizeof(v6));
769 memset(v4, 0, sizeof(struct sockaddr_in));
770 memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
771
772 addr->ss.ss_family = AF_INET;
773 addr->ss_len = sizeof(struct sockaddr_in);
774 }
775 else
776 addr->ss_len = sizeof(struct sockaddr_in6);
777 }
778 else
779 addr->ss_len = sizeof(struct sockaddr_in);
780 }
781 #endif

Properties

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