/[svn]/ircd-hybrid-8/src/s_bsd.c
ViewVC logotype

Annotation of /ircd-hybrid-8/src/s_bsd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1377 - (hide annotations)
Mon Apr 30 19:35:16 2012 UTC (8 years, 4 months ago) by michael
File MIME type: text/x-chdr
File size: 21476 byte(s)
- Remove all Class related #defines like MaxTotal, MaxSendq, etc.
  All they do is lead to confusion.

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 ConfItem *conf;
180     struct AccessItem *aconf;
181     struct ClassItem *aclass;
182    
183     assert(NULL != client_p);
184    
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     /* XXX Does this even make any sense at all anymore?
211     * scheduling a 'quick' reconnect could cause a pile of
212     * nick collides under TSora protocol... -db
213     */
214     /*
215     * If the connection has been up for a long amount of time, schedule
216     * a 'quick' reconnect, else reset the next-connect cycle.
217     */
218 michael 1013 if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
219     client_p->username, client_p->host)))
220 adx 30 {
221     /*
222     * Reschedule a faster reconnect, if this was a automatically
223     * connected configuration entry. (Note that if we have had
224     * a rehash in between, the status has been changed to
225     * CONF_ILLEGAL). But only do this if it was a "good" link.
226     */
227 michael 1013 aconf = map_to_conf(conf);
228     aclass = map_to_conf(aconf->class_ptr);
229 adx 30 aconf->hold = time(NULL);
230 michael 1241 aconf->hold += (aconf->hold - client_p->localClient->since > HANGONGOODLINK) ?
231 michael 1377 HANGONRETRYDELAY : aclass->con_freq;
232 adx 30 }
233     }
234     else
235 michael 896 ++ServerStats.is_ni;
236 adx 30
237     #ifdef HAVE_LIBCRYPTO
238     if (client_p->localClient->fd.ssl)
239 michael 451 {
240     SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
241    
242     if (!SSL_shutdown(client_p->localClient->fd.ssl))
243     SSL_shutdown(client_p->localClient->fd.ssl);
244     }
245 adx 30 #endif
246     if (client_p->localClient->fd.flags.open)
247     fd_close(&client_p->localClient->fd);
248    
249     dbuf_clear(&client_p->localClient->buf_sendq);
250     dbuf_clear(&client_p->localClient->buf_recvq);
251    
252     MyFree(client_p->localClient->passwd);
253     detach_conf(client_p, CONF_TYPE);
254     client_p->from = NULL; /* ...this should catch them! >:) --msa */
255     }
256    
257     #ifdef HAVE_LIBCRYPTO
258     /*
259     * ssl_handshake - let OpenSSL initialize the protocol. Register for
260     * read/write events if necessary.
261     */
262     static void
263     ssl_handshake(int fd, struct Client *client_p)
264     {
265     int ret = SSL_accept(client_p->localClient->fd.ssl);
266    
267     if (ret <= 0)
268     switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
269     {
270     case SSL_ERROR_WANT_WRITE:
271     comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
272     (PF *) ssl_handshake, client_p, 0);
273     return;
274    
275     case SSL_ERROR_WANT_READ:
276     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
277     (PF *) ssl_handshake, client_p, 0);
278     return;
279    
280     default:
281     exit_client(client_p, client_p, "Error during SSL handshake");
282     return;
283     }
284    
285     execute_callback(auth_cb, client_p);
286     }
287     #endif
288    
289     /*
290     * add_connection - creates a client which has just connected to us on
291     * the given fd. The sockhost field is initialized with the ip# of the host.
292     * An unique id is calculated now, in case it is needed for auth.
293     * The client is sent to the auth module for verification, and not put in
294     * any client list yet.
295     */
296     void
297 michael 549 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
298 adx 30 {
299 michael 1123 struct Client *new_client = make_client(NULL);
300 michael 549
301 adx 30 fd_open(&new_client->localClient->fd, fd, 1,
302     (listener->flags & LISTENER_SSL) ?
303     "Incoming SSL connection" : "Incoming connection");
304    
305 michael 1123 /*
306 adx 30 * copy address to 'sockhost' as a string, copy it to host too
307     * so we have something valid to put into error messages...
308     */
309 michael 549 memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
310 adx 30
311 michael 1123 getnameinfo((struct sockaddr *)&new_client->localClient->ip,
312     new_client->localClient->ip.ss_len, new_client->sockhost,
313     sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
314 adx 30 new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
315 michael 1072
316 michael 1123 if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
317 adx 30 {
318 michael 1072 strlcpy(new_client->host, "0", sizeof(new_client->host));
319     strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
320 michael 1123 memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
321     new_client->sockhost[0] = '0';
322 michael 549 }
323     else
324 michael 1072 strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
325 adx 30
326     new_client->localClient->listener = listener;
327     ++listener->ref_count;
328    
329     #ifdef HAVE_LIBCRYPTO
330 michael 549 if (listener->flags & LISTENER_SSL)
331 adx 30 {
332 michael 967 if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
333 adx 30 {
334 michael 1247 ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
335 adx 30 ERR_error_string(ERR_get_error(), NULL));
336    
337     SetDead(new_client);
338     exit_client(new_client, new_client, "SSL_new failed");
339     return;
340     }
341    
342     SSL_set_fd(new_client->localClient->fd.ssl, fd);
343     ssl_handshake(0, new_client);
344     }
345     else
346     #endif
347     execute_callback(auth_cb, new_client);
348     }
349    
350     /*
351     * stolen from squid - its a neat (but overused! :) routine which we
352     * can use to see whether we can ignore this errno or not. It is
353     * generally useful for non-blocking network IO related errnos.
354     * -- adrian
355     */
356     int
357     ignoreErrno(int ierrno)
358     {
359     switch (ierrno)
360     {
361     case EINPROGRESS:
362     case EWOULDBLOCK:
363     #if EAGAIN != EWOULDBLOCK
364     case EAGAIN:
365     #endif
366     case EALREADY:
367     case EINTR:
368     #ifdef ERESTART
369     case ERESTART:
370     #endif
371     return 1;
372     default:
373     return 0;
374     }
375     }
376    
377     /*
378     * comm_settimeout() - set the socket timeout
379     *
380     * Set the timeout for the fd
381     */
382     void
383     comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
384     {
385     assert(fd->flags.open);
386    
387     fd->timeout = CurrentTime + (timeout / 1000);
388     fd->timeout_handler = callback;
389     fd->timeout_data = cbdata;
390     }
391    
392     /*
393     * comm_setflush() - set a flush function
394     *
395     * A flush function is simply a function called if found during
396     * comm_timeouts(). Its basically a second timeout, except in this case
397     * I'm too lazy to implement multiple timeout functions! :-)
398     * its kinda nice to have it separate, since this is designed for
399     * flush functions, and when comm_close() is implemented correctly
400     * with close functions, we _actually_ don't call comm_close() here ..
401     * -- originally Adrian's notes
402     * comm_close() is replaced with fd_close() in fdlist.c
403     */
404     void
405     comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
406     {
407     assert(fd->flags.open);
408    
409     fd->flush_timeout = CurrentTime + (timeout / 1000);
410     fd->flush_handler = callback;
411     fd->flush_data = cbdata;
412     }
413    
414     /*
415     * comm_checktimeouts() - check the socket timeouts
416     *
417     * All this routine does is call the given callback/cbdata, without closing
418     * down the file descriptor. When close handlers have been implemented,
419     * this will happen.
420     */
421     void
422     comm_checktimeouts(void *notused)
423     {
424     int i;
425     fde_t *F;
426     PF *hdl;
427     void *data;
428    
429     for (i = 0; i < FD_HASH_SIZE; i++)
430     for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop)
431     {
432     assert(F->flags.open);
433     fd_next_in_loop = F->hnext;
434    
435     /* check flush functions */
436     if (F->flush_handler && F->flush_timeout > 0 &&
437     F->flush_timeout < CurrentTime)
438     {
439     hdl = F->flush_handler;
440     data = F->flush_data;
441     comm_setflush(F, 0, NULL, NULL);
442     hdl(F, data);
443     }
444    
445     /* check timeouts */
446     if (F->timeout_handler && F->timeout > 0 &&
447     F->timeout < CurrentTime)
448     {
449     /* Call timeout handler */
450     hdl = F->timeout_handler;
451     data = F->timeout_data;
452     comm_settimeout(F, 0, NULL, NULL);
453     hdl(F, data);
454     }
455     }
456     }
457    
458     /*
459     * void comm_connect_tcp(int fd, const char *host, unsigned short port,
460     * struct sockaddr *clocal, int socklen,
461     * CNCB *callback, void *data, int aftype, int timeout)
462     * Input: An fd to connect with, a host and port to connect to,
463     * a local sockaddr to connect from + length(or NULL to use the
464     * default), a callback, the data to pass into the callback, the
465     * address family.
466     * Output: None.
467     * Side-effects: A non-blocking connection to the host is started, and
468     * if necessary, set up for selection. The callback given
469     * may be called now, or it may be called later.
470     */
471     void
472     comm_connect_tcp(fde_t *fd, const char *host, unsigned short port,
473     struct sockaddr *clocal, int socklen, CNCB *callback,
474     void *data, int aftype, int timeout)
475     {
476     struct addrinfo hints, *res;
477 michael 992 char portname[PORTNAMELEN + 1];
478 adx 30
479     assert(callback);
480     fd->connect.callback = callback;
481     fd->connect.data = data;
482    
483     fd->connect.hostaddr.ss.ss_family = aftype;
484     fd->connect.hostaddr.ss_port = htons(port);
485    
486     /* Note that we're using a passed sockaddr here. This is because
487     * generally you'll be bind()ing to a sockaddr grabbed from
488     * getsockname(), so this makes things easier.
489     * XXX If NULL is passed as local, we should later on bind() to the
490     * virtual host IP, for completeness.
491     * -- adrian
492     */
493     if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
494     {
495     /* Failure, call the callback with COMM_ERR_BIND */
496     comm_connect_callback(fd, COMM_ERR_BIND);
497     /* ... and quit */
498     return;
499     }
500    
501     /* Next, if we have been given an IP, get the addr and skip the
502     * DNS check (and head direct to comm_connect_tryconnect().
503     */
504     memset(&hints, 0, sizeof(hints));
505     hints.ai_family = AF_UNSPEC;
506     hints.ai_socktype = SOCK_STREAM;
507     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
508    
509 michael 992 snprintf(portname, sizeof(portname), "%d", port);
510 adx 30
511 michael 1123 if (getaddrinfo(host, portname, &hints, &res))
512 adx 30 {
513     /* Send the DNS request, for the next level */
514 db 871 if (aftype == AF_INET6)
515 michael 992 gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
516 db 871 else
517 michael 992 gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
518 adx 30 }
519     else
520     {
521     /* We have a valid IP, so we just call tryconnect */
522     /* Make sure we actually set the timeout here .. */
523     assert(res != NULL);
524     memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
525     fd->connect.hostaddr.ss_len = res->ai_addrlen;
526     fd->connect.hostaddr.ss.ss_family = res->ai_family;
527 michael 1123 freeaddrinfo(res);
528 adx 30 comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
529     comm_connect_tryconnect(fd, NULL);
530     }
531     }
532    
533     /*
534     * comm_connect_callback() - call the callback, and continue with life
535     */
536     static void
537     comm_connect_callback(fde_t *fd, int status)
538     {
539     CNCB *hdl;
540    
541     /* This check is gross..but probably necessary */
542     if (fd->connect.callback == NULL)
543     return;
544    
545     /* Clear the connect flag + handler */
546     hdl = fd->connect.callback;
547     fd->connect.callback = NULL;
548    
549     /* Clear the timeout handler */
550     comm_settimeout(fd, 0, NULL, NULL);
551    
552     /* Call the handler */
553     hdl(fd, status, fd->connect.data);
554     }
555    
556     /*
557     * comm_connect_timeout() - this gets called when the socket connection
558     * times out. This *only* can be called once connect() is initially
559     * called ..
560     */
561     static void
562     comm_connect_timeout(fde_t *fd, void *notused)
563     {
564     /* error! */
565     comm_connect_callback(fd, COMM_ERR_TIMEOUT);
566     }
567    
568     /*
569     * comm_connect_dns_callback() - called at the completion of the DNS request
570     *
571     * The DNS request has completed, so if we've got an error, return it,
572     * otherwise we initiate the connect()
573     */
574     static void
575 michael 992 comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
576 adx 30 {
577     fde_t *F = vptr;
578    
579 michael 992 if (name == NULL)
580 adx 30 {
581     comm_connect_callback(F, COMM_ERR_DNS);
582     return;
583     }
584    
585     /* No error, set a 10 second timeout */
586     comm_settimeout(F, 30*1000, comm_connect_timeout, NULL);
587    
588     /* Copy over the DNS reply info so we can use it in the connect() */
589     /*
590     * Note we don't fudge the refcount here, because we aren't keeping
591     * the DNS record around, and the DNS cache is gone anyway..
592     * -- adrian
593     */
594 michael 992 memcpy(&F->connect.hostaddr, addr, addr->ss_len);
595 adx 30 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
596     ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
597     F->connect.hostaddr.ss_port;
598 michael 992 F->connect.hostaddr.ss_len = addr->ss_len;
599 adx 30
600     /* Now, call the tryconnect() routine to try a connect() */
601     comm_connect_tryconnect(F, NULL);
602     }
603    
604     /* static void comm_connect_tryconnect(int fd, void *notused)
605     * Input: The fd, the handler data(unused).
606     * Output: None.
607     * Side-effects: Try and connect with pending connect data for the FD. If
608     * we succeed or get a fatal error, call the callback.
609     * Otherwise, it is still blocking or something, so register
610     * to select for a write event on this FD.
611     */
612     static void
613     comm_connect_tryconnect(fde_t *fd, void *notused)
614     {
615     int retval;
616    
617     /* This check is needed or re-entrant s_bsd_* like sigio break it. */
618     if (fd->connect.callback == NULL)
619     return;
620    
621     /* Try the connect() */
622     retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
623     fd->connect.hostaddr.ss_len);
624    
625     /* Error? */
626     if (retval < 0)
627     {
628     /*
629     * If we get EISCONN, then we've already connect()ed the socket,
630     * which is a good thing.
631     * -- adrian
632     */
633     if (errno == EISCONN)
634     comm_connect_callback(fd, COMM_OK);
635     else if (ignoreErrno(errno))
636     /* Ignore error? Reschedule */
637     comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect,
638     NULL, 0);
639     else
640     /* Error? Fail with COMM_ERR_CONNECT */
641     comm_connect_callback(fd, COMM_ERR_CONNECT);
642     return;
643     }
644    
645     /* If we get here, we've suceeded, so call with COMM_OK */
646     comm_connect_callback(fd, COMM_OK);
647     }
648    
649     /*
650     * comm_errorstr() - return an error string for the given error condition
651     */
652     const char *
653     comm_errstr(int error)
654     {
655     if (error < 0 || error >= COMM_ERR_MAX)
656     return "Invalid error number!";
657     return comm_err_str[error];
658     }
659    
660     /*
661     * comm_open() - open a socket
662     *
663     * This is a highly highly cut down version of squid's comm_open() which
664     * for the most part emulates socket(), *EXCEPT* it fails if we're about
665     * to run out of file descriptors.
666     */
667     int
668     comm_open(fde_t *F, int family, int sock_type, int proto, const char *note)
669     {
670     int fd;
671    
672     /* First, make sure we aren't going to run out of file descriptors */
673     if (number_fd >= hard_fdlimit)
674     {
675     errno = ENFILE;
676     return -1;
677     }
678    
679     /*
680     * Next, we try to open the socket. We *should* drop the reserved FD
681     * limit if/when we get an error, but we can deal with that later.
682     * XXX !!! -- adrian
683     */
684     fd = socket(family, sock_type, proto);
685     if (fd < 0)
686     return -1; /* errno will be passed through, yay.. */
687    
688     execute_callback(setup_socket_cb, fd);
689    
690     /* update things in our fd tracking */
691     fd_open(F, fd, 1, note);
692     return 0;
693     }
694    
695     /*
696     * comm_accept() - accept an incoming connection
697     *
698     * This is a simple wrapper for accept() which enforces FD limits like
699     * comm_open() does. Returned fd must be either closed or tagged with
700     * fd_open (this function no longer does it).
701     */
702     int
703     comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
704     {
705     int newfd;
706     socklen_t addrlen = sizeof(struct irc_ssaddr);
707    
708     if (number_fd >= hard_fdlimit)
709     {
710     errno = ENFILE;
711     return -1;
712     }
713    
714     /*
715     * Next, do the accept(). if we get an error, we should drop the
716     * reserved fd limit, but we can deal with that when comm_open()
717     * also does it. XXX -- adrian
718     */
719 michael 1013 newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
720 adx 30 if (newfd < 0)
721     return -1;
722    
723     #ifdef IPV6
724     remove_ipv6_mapping(pn);
725     #else
726     pn->ss_len = addrlen;
727     #endif
728    
729     execute_callback(setup_socket_cb, newfd);
730    
731     /* .. and return */
732     return newfd;
733     }
734    
735     /*
736     * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
737 michael 1122 * OSes with IPv6 mapping listening on both
738 adx 30 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
739     *
740     */
741     #ifdef IPV6
742     void
743     remove_ipv6_mapping(struct irc_ssaddr *addr)
744     {
745     if (addr->ss.ss_family == AF_INET6)
746     {
747 michael 1122 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
748     {
749     struct sockaddr_in6 v6;
750     struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
751 adx 30
752 michael 1122 memcpy(&v6, addr, sizeof(v6));
753     memset(v4, 0, sizeof(struct sockaddr_in));
754     memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
755    
756 adx 30 addr->ss.ss_family = AF_INET;
757     addr->ss_len = sizeof(struct sockaddr_in);
758     }
759     else
760     addr->ss_len = sizeof(struct sockaddr_in6);
761     }
762     else
763     addr->ss_len = sizeof(struct sockaddr_in);
764     }
765     #endif

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28