ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/s_bsd.c
Revision: 948
Committed: Tue Jul 21 17:34:06 2009 UTC (14 years, 8 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/s_bsd.c
File size: 22493 byte(s)
Log Message:
- lp64\llp64\ilp32 portability fixes

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

Properties

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