ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/releases/8.1.0beta2/src/s_bsd.c
Revision: 1243
Committed: Fri Sep 30 10:47:53 2011 UTC (14 years, 9 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/s_bsd.c
File size: 21685 byte(s)
Log Message:
- move content of msg.h, ircd_handler.h and handlers.h into parse.h and
  remove headers accordingly
- killed common.h
- remove m_killhost.c and m_flags.c from contrib/
- sort out unused header includes here and there

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

Properties

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