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: 2881
Committed: Mon Jan 20 17:15:39 2014 UTC (12 years, 6 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_bsd.c
File size: 21920 byte(s)
Log Message:
- Use the i/o subsystem to execute scheduled writes. Patch provided by Adam.

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

Properties

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