ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 4114
Committed: Tue Jul 1 16:47:15 2014 UTC (11 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 21807 byte(s)
Log Message:
- Added ssl_message_digest_algorithm configuration option to serverinfo{} block.
  See doc/reference.conf for more information.

File Contents

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

Properties

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