ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd.c
Revision: 7431
Committed: Tue Mar 8 18:20:10 2016 UTC (10 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 21131 byte(s)
Log Message:
- Minor style cleanups & constification

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

Properties

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