ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 992
Committed: Mon Aug 17 19:19:16 2009 UTC (17 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/s_bsd.c
File size: 22248 byte(s)
Log Message:
- fix possible auth/dns related memleaks

File Contents

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

Properties

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