ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (12 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 20886 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

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

Properties

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