ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/s_bsd.c
Revision: 3241
Committed: Sun Mar 30 16:45:31 2014 UTC (12 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_bsd.c
File size: 21758 byte(s)
Log Message:
- Incorporate Adam's writev() patch

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file s_bsd.c
23     * \brief Network functions.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28     #include <netinet/in_systm.h>
29     #include <netinet/ip.h>
30     #include <netinet/tcp.h>
31 michael 1011 #include "list.h"
32 adx 30 #include "fdlist.h"
33     #include "s_bsd.h"
34     #include "client.h"
35     #include "dbuf.h"
36     #include "event.h"
37     #include "irc_string.h"
38     #include "ircd.h"
39     #include "listener.h"
40     #include "numeric.h"
41     #include "packet.h"
42     #include "irc_res.h"
43     #include "restart.h"
44     #include "s_auth.h"
45 michael 1309 #include "conf.h"
46     #include "log.h"
47 adx 30 #include "s_serv.h"
48     #include "send.h"
49     #include "memory.h"
50     #include "s_user.h"
51     #include "hook.h"
52    
53     static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
54     "Error during DNS lookup", "connect timeout", "Error during connect()",
55     "Comm Error" };
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 michael 2916 * report_error - report an error from an errno.
110 adx 30 * 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 michael 2916 *
124 adx 30 * Actually stderr is still there IFF ircd was run with -s --Rodder
125     */
126    
127     void
128 michael 2916 report_error(int level, const char* text, const char* who, int error)
129 adx 30 {
130     who = (who) ? who : "";
131    
132 michael 1618 sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
133     text, who, strerror(error));
134 michael 1247 ilog(LOG_TYPE_IRCD, text, who, strerror(error));
135 adx 30 }
136    
137     /*
138     * setup_socket()
139     *
140     * Set the socket non-blocking, and other wonderful bits.
141     */
142 michael 2632 static void
143     setup_socket(int fd)
144 adx 30 {
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    
157     /*
158     * close_connection
159     * Close the physical connection. This function must make
160     * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
161     */
162     void
163     close_connection(struct Client *client_p)
164     {
165 michael 1391 dlink_node *ptr = NULL;
166 adx 30
167 michael 1391 assert(client_p);
168 adx 30
169 michael 683 if (!IsDead(client_p))
170     {
171 michael 3241 /*
172     * Flush pending write buffer, if any, but first clear the
173     * cork as it no longer matters, this connection is being
174     * closed now
175 michael 683 */
176 michael 3241 DelFlag(client_p, FLAGS_CORK);
177 michael 683 send_queued_write(client_p);
178     }
179    
180 michael 1143 if (IsClient(client_p))
181 adx 30 {
182 michael 1143 ++ServerStats.is_cl;
183     ServerStats.is_cbs += client_p->localClient->send.bytes;
184     ServerStats.is_cbr += client_p->localClient->recv.bytes;
185 michael 1241 ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
186 michael 1143 }
187     else if (IsServer(client_p))
188     {
189 michael 896 ++ServerStats.is_sv;
190     ServerStats.is_sbs += client_p->localClient->send.bytes;
191     ServerStats.is_sbr += client_p->localClient->recv.bytes;
192 michael 1241 ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
193 adx 30
194 michael 1391 DLINK_FOREACH(ptr, server_items.head)
195 adx 30 {
196 michael 1632 struct MaskItem *conf = ptr->data;
197 michael 1391
198     if (irccmp(conf->name, client_p->name))
199     continue;
200    
201 adx 30 /*
202 michael 1391 * Reset next-connect cycle of all connect{} blocks that match
203     * this servername.
204 adx 30 */
205 michael 1649 conf->until = CurrentTime + conf->class->con_freq;
206 adx 30 }
207     }
208     else
209 michael 896 ++ServerStats.is_ni;
210 adx 30
211     #ifdef HAVE_LIBCRYPTO
212     if (client_p->localClient->fd.ssl)
213 michael 451 {
214     SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
215    
216     if (!SSL_shutdown(client_p->localClient->fd.ssl))
217     SSL_shutdown(client_p->localClient->fd.ssl);
218     }
219 adx 30 #endif
220     if (client_p->localClient->fd.flags.open)
221     fd_close(&client_p->localClient->fd);
222    
223     dbuf_clear(&client_p->localClient->buf_sendq);
224     dbuf_clear(&client_p->localClient->buf_recvq);
225 michael 2916
226 adx 30 MyFree(client_p->localClient->passwd);
227 michael 1632 detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
228 adx 30 client_p->from = NULL; /* ...this should catch them! >:) --msa */
229     }
230    
231     #ifdef HAVE_LIBCRYPTO
232     /*
233     * ssl_handshake - let OpenSSL initialize the protocol. Register for
234     * read/write events if necessary.
235     */
236     static void
237     ssl_handshake(int fd, struct Client *client_p)
238     {
239 michael 2228 X509 *cert = NULL;
240 michael 2463 int ret = 0;
241 adx 30
242 michael 2463 if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0)
243     {
244 michael 2725 if ((CurrentTime - client_p->localClient->firsttime) > 30)
245     {
246 michael 3171 exit_client(client_p, "Timeout during SSL handshake");
247 michael 2725 return;
248     }
249    
250 michael 2463 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
251     {
252     case SSL_ERROR_WANT_WRITE:
253     comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
254 michael 2916 (PF *)ssl_handshake, client_p, 30);
255 michael 2463 return;
256    
257     case SSL_ERROR_WANT_READ:
258     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
259 michael 2916 (PF *)ssl_handshake, client_p, 30);
260 michael 2463 return;
261    
262     default:
263 michael 3171 exit_client(client_p, "Error during SSL handshake");
264 michael 2916 return;
265 michael 2463 }
266     }
267    
268 michael 2733 comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL);
269    
270 michael 2228 if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
271     {
272     int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
273 michael 2235 char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
274 michael 2287 unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
275 michael 2228
276     if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
277     res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
278     res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
279 michael 2229 {
280 michael 2235 unsigned int i = 0, n = 0;
281    
282     if (X509_digest(cert, EVP_sha256(), md, &n))
283     {
284     for (; i < n; ++i)
285     snprintf(buf + 2 * i, 3, "%02X", md[i]);
286     client_p->certfp = xstrdup(buf);
287     }
288 michael 2229 }
289 michael 2228 else
290     ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
291     client_p->name, client_p->username, client_p->host, res);
292     X509_free(cert);
293     }
294    
295 michael 2181 start_auth(client_p);
296 adx 30 }
297     #endif
298    
299     /*
300 michael 2916 * add_connection - creates a client which has just connected to us on
301 adx 30 * the given fd. The sockhost field is initialized with the ip# of the host.
302     * An unique id is calculated now, in case it is needed for auth.
303     * The client is sent to the auth module for verification, and not put in
304     * any client list yet.
305     */
306     void
307 michael 549 add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
308 adx 30 {
309 michael 3171 struct Client *client_p = make_client(NULL);
310 michael 549
311 michael 3171 fd_open(&client_p->localClient->fd, fd, 1,
312 adx 30 (listener->flags & LISTENER_SSL) ?
313 michael 2916 "Incoming SSL connection" : "Incoming connection");
314 adx 30
315 michael 1123 /*
316 adx 30 * copy address to 'sockhost' as a string, copy it to host too
317     * so we have something valid to put into error messages...
318     */
319 michael 3171 memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr));
320 adx 30
321 michael 3171 getnameinfo((struct sockaddr *)&client_p->localClient->ip,
322     client_p->localClient->ip.ss_len, client_p->sockhost,
323     sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST);
324     client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family;
325 michael 1072
326 michael 1858 #ifdef HAVE_LIBGEOIP
327     /* XXX IPV6 SUPPORT XXX */
328     if (irn->ss.ss_family == AF_INET && geoip_ctx)
329     {
330 michael 3171 const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip;
331     client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
332 michael 1858 }
333     #endif
334    
335 michael 3171 if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':')
336 adx 30 {
337 michael 3171 strlcpy(client_p->host, "0", sizeof(client_p->host));
338     strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1);
339     memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1);
340     client_p->sockhost[0] = '0';
341 michael 549 }
342     else
343 michael 3171 strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host));
344 adx 30
345 michael 3171 client_p->localClient->listener = listener;
346 adx 30 ++listener->ref_count;
347    
348     #ifdef HAVE_LIBCRYPTO
349 michael 549 if (listener->flags & LISTENER_SSL)
350 adx 30 {
351 michael 3171 if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
352 adx 30 {
353 michael 1247 ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
354 adx 30 ERR_error_string(ERR_get_error(), NULL));
355    
356 michael 3171 SetDead(client_p);
357     exit_client(client_p, "SSL_new failed");
358 adx 30 return;
359     }
360    
361 michael 3171 AddFlag(client_p, FLAGS_SSL);
362     SSL_set_fd(client_p->localClient->fd.ssl, fd);
363     ssl_handshake(0, client_p);
364 adx 30 }
365     else
366     #endif
367 michael 3171 start_auth(client_p);
368 adx 30 }
369    
370     /*
371     * stolen from squid - its a neat (but overused! :) routine which we
372     * can use to see whether we can ignore this errno or not. It is
373     * generally useful for non-blocking network IO related errnos.
374     * -- adrian
375     */
376     int
377     ignoreErrno(int ierrno)
378     {
379     switch (ierrno)
380     {
381     case EINPROGRESS:
382     case EWOULDBLOCK:
383     #if EAGAIN != EWOULDBLOCK
384     case EAGAIN:
385     #endif
386     case EALREADY:
387     case EINTR:
388     #ifdef ERESTART
389     case ERESTART:
390     #endif
391     return 1;
392     default:
393     return 0;
394     }
395     }
396    
397     /*
398     * comm_settimeout() - set the socket timeout
399     *
400     * Set the timeout for the fd
401     */
402     void
403     comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
404     {
405     assert(fd->flags.open);
406    
407     fd->timeout = CurrentTime + (timeout / 1000);
408     fd->timeout_handler = callback;
409     fd->timeout_data = cbdata;
410     }
411    
412     /*
413     * comm_setflush() - set a flush function
414     *
415     * A flush function is simply a function called if found during
416     * comm_timeouts(). Its basically a second timeout, except in this case
417     * I'm too lazy to implement multiple timeout functions! :-)
418     * its kinda nice to have it separate, since this is designed for
419     * flush functions, and when comm_close() is implemented correctly
420     * with close functions, we _actually_ don't call comm_close() here ..
421     * -- originally Adrian's notes
422 michael 2916 * comm_close() is replaced with fd_close() in fdlist.c
423 adx 30 */
424     void
425     comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
426     {
427     assert(fd->flags.open);
428    
429     fd->flush_timeout = CurrentTime + (timeout / 1000);
430     fd->flush_handler = callback;
431     fd->flush_data = cbdata;
432     }
433    
434     /*
435     * comm_checktimeouts() - check the socket timeouts
436     *
437     * All this routine does is call the given callback/cbdata, without closing
438     * down the file descriptor. When close handlers have been implemented,
439     * this will happen.
440     */
441     void
442     comm_checktimeouts(void *notused)
443     {
444     int i;
445     fde_t *F;
446     PF *hdl;
447     void *data;
448    
449     for (i = 0; i < FD_HASH_SIZE; i++)
450     for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop)
451     {
452     assert(F->flags.open);
453     fd_next_in_loop = F->hnext;
454    
455     /* check flush functions */
456     if (F->flush_handler && F->flush_timeout > 0 &&
457     F->flush_timeout < CurrentTime)
458     {
459     hdl = F->flush_handler;
460     data = F->flush_data;
461     comm_setflush(F, 0, NULL, NULL);
462     hdl(F, data);
463     }
464    
465     /* check timeouts */
466     if (F->timeout_handler && F->timeout > 0 &&
467     F->timeout < CurrentTime)
468     {
469     /* Call timeout handler */
470     hdl = F->timeout_handler;
471     data = F->timeout_data;
472     comm_settimeout(F, 0, NULL, NULL);
473     hdl(F, data);
474     }
475     }
476     }
477    
478     /*
479     * void comm_connect_tcp(int fd, const char *host, unsigned short port,
480     * struct sockaddr *clocal, int socklen,
481     * CNCB *callback, void *data, int aftype, int timeout)
482     * Input: An fd to connect with, a host and port to connect to,
483     * a local sockaddr to connect from + length(or NULL to use the
484     * default), a callback, the data to pass into the callback, the
485     * address family.
486     * Output: None.
487     * Side-effects: A non-blocking connection to the host is started, and
488     * if necessary, set up for selection. The callback given
489     * may be called now, or it may be called later.
490     */
491     void
492     comm_connect_tcp(fde_t *fd, const char *host, unsigned short port,
493     struct sockaddr *clocal, int socklen, CNCB *callback,
494     void *data, int aftype, int timeout)
495     {
496     struct addrinfo hints, *res;
497 michael 992 char portname[PORTNAMELEN + 1];
498 adx 30
499     assert(callback);
500     fd->connect.callback = callback;
501     fd->connect.data = data;
502    
503     fd->connect.hostaddr.ss.ss_family = aftype;
504     fd->connect.hostaddr.ss_port = htons(port);
505    
506     /* Note that we're using a passed sockaddr here. This is because
507     * generally you'll be bind()ing to a sockaddr grabbed from
508     * getsockname(), so this makes things easier.
509     * XXX If NULL is passed as local, we should later on bind() to the
510     * virtual host IP, for completeness.
511     * -- adrian
512     */
513     if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
514 michael 2916 {
515 adx 30 /* Failure, call the callback with COMM_ERR_BIND */
516     comm_connect_callback(fd, COMM_ERR_BIND);
517     /* ... and quit */
518     return;
519     }
520    
521     /* Next, if we have been given an IP, get the addr and skip the
522     * DNS check (and head direct to comm_connect_tryconnect().
523     */
524     memset(&hints, 0, sizeof(hints));
525     hints.ai_family = AF_UNSPEC;
526     hints.ai_socktype = SOCK_STREAM;
527     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
528    
529 michael 992 snprintf(portname, sizeof(portname), "%d", port);
530 adx 30
531 michael 1123 if (getaddrinfo(host, portname, &hints, &res))
532 adx 30 {
533     /* Send the DNS request, for the next level */
534 db 871 if (aftype == AF_INET6)
535 michael 992 gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
536 db 871 else
537 michael 992 gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
538 adx 30 }
539     else
540     {
541     /* We have a valid IP, so we just call tryconnect */
542     /* Make sure we actually set the timeout here .. */
543     assert(res != NULL);
544     memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
545     fd->connect.hostaddr.ss_len = res->ai_addrlen;
546     fd->connect.hostaddr.ss.ss_family = res->ai_family;
547 michael 1123 freeaddrinfo(res);
548 adx 30 comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
549     comm_connect_tryconnect(fd, NULL);
550     }
551     }
552    
553     /*
554     * comm_connect_callback() - call the callback, and continue with life
555     */
556     static void
557     comm_connect_callback(fde_t *fd, int status)
558     {
559     CNCB *hdl;
560    
561     /* This check is gross..but probably necessary */
562     if (fd->connect.callback == NULL)
563     return;
564    
565     /* Clear the connect flag + handler */
566     hdl = fd->connect.callback;
567     fd->connect.callback = NULL;
568    
569     /* Clear the timeout handler */
570     comm_settimeout(fd, 0, NULL, NULL);
571    
572     /* Call the handler */
573     hdl(fd, status, fd->connect.data);
574     }
575    
576     /*
577     * comm_connect_timeout() - this gets called when the socket connection
578     * times out. This *only* can be called once connect() is initially
579     * called ..
580     */
581     static void
582     comm_connect_timeout(fde_t *fd, void *notused)
583     {
584     /* error! */
585     comm_connect_callback(fd, COMM_ERR_TIMEOUT);
586     }
587    
588     /*
589     * comm_connect_dns_callback() - called at the completion of the DNS request
590     *
591     * The DNS request has completed, so if we've got an error, return it,
592     * otherwise we initiate the connect()
593     */
594     static void
595 michael 992 comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
596 adx 30 {
597     fde_t *F = vptr;
598    
599 michael 992 if (name == NULL)
600 adx 30 {
601     comm_connect_callback(F, COMM_ERR_DNS);
602     return;
603     }
604    
605     /* No error, set a 10 second timeout */
606     comm_settimeout(F, 30*1000, comm_connect_timeout, NULL);
607    
608     /* Copy over the DNS reply info so we can use it in the connect() */
609     /*
610     * Note we don't fudge the refcount here, because we aren't keeping
611 michael 2916 * the DNS record around, and the DNS cache is gone anyway..
612 adx 30 * -- adrian
613     */
614 michael 992 memcpy(&F->connect.hostaddr, addr, addr->ss_len);
615 adx 30 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
616     ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
617     F->connect.hostaddr.ss_port;
618 michael 992 F->connect.hostaddr.ss_len = addr->ss_len;
619 adx 30
620     /* Now, call the tryconnect() routine to try a connect() */
621     comm_connect_tryconnect(F, NULL);
622     }
623    
624     /* static void comm_connect_tryconnect(int fd, void *notused)
625     * Input: The fd, the handler data(unused).
626     * Output: None.
627     * Side-effects: Try and connect with pending connect data for the FD. If
628     * we succeed or get a fatal error, call the callback.
629     * Otherwise, it is still blocking or something, so register
630     * to select for a write event on this FD.
631     */
632     static void
633     comm_connect_tryconnect(fde_t *fd, void *notused)
634     {
635     int retval;
636    
637     /* This check is needed or re-entrant s_bsd_* like sigio break it. */
638     if (fd->connect.callback == NULL)
639     return;
640    
641     /* Try the connect() */
642 michael 2916 retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
643 adx 30 fd->connect.hostaddr.ss_len);
644    
645     /* Error? */
646     if (retval < 0)
647     {
648     /*
649     * If we get EISCONN, then we've already connect()ed the socket,
650     * which is a good thing.
651     * -- adrian
652     */
653     if (errno == EISCONN)
654     comm_connect_callback(fd, COMM_OK);
655     else if (ignoreErrno(errno))
656     /* Ignore error? Reschedule */
657     comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect,
658     NULL, 0);
659     else
660     /* Error? Fail with COMM_ERR_CONNECT */
661     comm_connect_callback(fd, COMM_ERR_CONNECT);
662     return;
663     }
664    
665     /* If we get here, we've suceeded, so call with COMM_OK */
666     comm_connect_callback(fd, COMM_OK);
667     }
668    
669     /*
670     * comm_errorstr() - return an error string for the given error condition
671     */
672     const char *
673     comm_errstr(int error)
674     {
675     if (error < 0 || error >= COMM_ERR_MAX)
676     return "Invalid error number!";
677     return comm_err_str[error];
678     }
679    
680     /*
681     * comm_open() - open a socket
682     *
683     * This is a highly highly cut down version of squid's comm_open() which
684     * for the most part emulates socket(), *EXCEPT* it fails if we're about
685     * to run out of file descriptors.
686     */
687     int
688     comm_open(fde_t *F, int family, int sock_type, int proto, const char *note)
689     {
690     int fd;
691    
692     /* First, make sure we aren't going to run out of file descriptors */
693     if (number_fd >= hard_fdlimit)
694     {
695     errno = ENFILE;
696     return -1;
697     }
698    
699     /*
700     * Next, we try to open the socket. We *should* drop the reserved FD
701     * limit if/when we get an error, but we can deal with that later.
702     * XXX !!! -- adrian
703     */
704     fd = socket(family, sock_type, proto);
705     if (fd < 0)
706     return -1; /* errno will be passed through, yay.. */
707    
708 michael 2632 setup_socket(fd);
709 adx 30
710     /* update things in our fd tracking */
711     fd_open(F, fd, 1, note);
712     return 0;
713     }
714    
715     /*
716     * comm_accept() - accept an incoming connection
717     *
718     * This is a simple wrapper for accept() which enforces FD limits like
719     * comm_open() does. Returned fd must be either closed or tagged with
720     * fd_open (this function no longer does it).
721     */
722     int
723     comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
724     {
725     int newfd;
726     socklen_t addrlen = sizeof(struct irc_ssaddr);
727    
728     if (number_fd >= hard_fdlimit)
729     {
730     errno = ENFILE;
731     return -1;
732     }
733    
734     /*
735     * Next, do the accept(). if we get an error, we should drop the
736     * reserved fd limit, but we can deal with that when comm_open()
737     * also does it. XXX -- adrian
738     */
739 michael 1013 newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
740 adx 30 if (newfd < 0)
741     return -1;
742    
743     #ifdef IPV6
744     remove_ipv6_mapping(pn);
745     #else
746     pn->ss_len = addrlen;
747     #endif
748    
749 michael 2632 setup_socket(newfd);
750 adx 30
751     /* .. and return */
752     return newfd;
753     }
754    
755 michael 2916 /*
756 adx 30 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
757 michael 1122 * OSes with IPv6 mapping listening on both
758 adx 30 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
759 michael 2916 *
760 adx 30 */
761     #ifdef IPV6
762     void
763     remove_ipv6_mapping(struct irc_ssaddr *addr)
764     {
765     if (addr->ss.ss_family == AF_INET6)
766     {
767 michael 1122 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
768     {
769 michael 2916 struct sockaddr_in6 v6;
770 michael 1122 struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
771 adx 30
772 michael 1122 memcpy(&v6, addr, sizeof(v6));
773     memset(v4, 0, sizeof(struct sockaddr_in));
774     memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
775    
776 adx 30 addr->ss.ss_family = AF_INET;
777     addr->ss_len = sizeof(struct sockaddr_in);
778     }
779 michael 2916 else
780 adx 30 addr->ss_len = sizeof(struct sockaddr_in6);
781     }
782     else
783     addr->ss_len = sizeof(struct sockaddr_in);
784 michael 2916 }
785 adx 30 #endif

Properties

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