ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 6951
Committed: Tue Dec 15 19:11:07 2015 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 21863 byte(s)
Log Message:
- s_bsd.h, s_bsd.c:comm_connect_tcp(): change the 'timeout' parameter to a time_t to match up with the other comm_* functions

File Contents

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

Properties

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