ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/branches/newio/src/s_bsd.c
Revision: 871
Committed: Fri Sep 14 03:33:44 2007 UTC (16 years, 6 months ago) by db
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/s_bsd.c
File size: 22564 byte(s)
Log Message:
- When a connect block uses a hostname instead of an IP, ircd will connect
to the hostname's IPv6 if available, independent of the aftype setting.
This causes the ircd to fail to connect to a IPv4 host that also has an IPv6 IP.

Submitted By:	evilmoon (hgchew) (via Sourceforge bts)

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

Properties

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