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

Contents of /ircd-hybrid-7.2/src/s_bsd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations)
Sun Oct 2 21:05:51 2005 UTC (14 years, 9 months ago) by lusky
File MIME type: text/x-chdr
File size: 23108 byte(s)
create 7.2 branch, we can move/rename it as needed.


1 /*
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 * $Id$
23 */
24
25 #include "stdinc.h"
26 #ifndef _WIN32
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <netinet/tcp.h>
30 #endif
31 #include "fdlist.h"
32 #include "s_bsd.h"
33 #include "client.h"
34 #include "common.h"
35 #include "dbuf.h"
36 #include "event.h"
37 #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
40 #include "ircd.h"
41 #include "list.h"
42 #include "listener.h"
43 #include "numeric.h"
44 #include "packet.h"
45 #include "irc_res.h"
46 #include "inet_misc.h"
47 #include "restart.h"
48 #include "s_auth.h"
49 #include "s_conf.h"
50 #include "s_log.h"
51 #include "s_serv.h"
52 #include "s_stats.h"
53 #include "send.h"
54 #include "memory.h"
55 #include "s_user.h"
56 #include "hook.h"
57
58 static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
59 "Error during DNS lookup", "connect timeout", "Error during connect()",
60 "Comm Error" };
61
62 struct Callback *setup_socket_cb = NULL;
63
64 static void comm_connect_callback(fde_t *fd, int status);
65 static PF comm_connect_timeout;
66 static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
67 static PF comm_connect_tryconnect;
68
69 extern void init_netio(void);
70
71 /* check_can_use_v6()
72 * Check if the system can open AF_INET6 sockets
73 */
74 void
75 check_can_use_v6(void)
76 {
77 #ifdef IPV6
78 int v6;
79
80 if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
81 ServerInfo.can_use_v6 = 0;
82 else
83 {
84 ServerInfo.can_use_v6 = 1;
85 #ifdef _WIN32
86 closesocket(v6);
87 #else
88 close(v6);
89 #endif
90 }
91 #else
92 ServerInfo.can_use_v6 = 0;
93 #endif
94 }
95
96 /* get_sockerr - get the error value from the socket or the current errno
97 *
98 * Get the *real* error from the socket (well try to anyway..).
99 * This may only work when SO_DEBUG is enabled but its worth the
100 * gamble anyway.
101 */
102 int
103 get_sockerr(int fd)
104 {
105 #ifndef _WIN32
106 int errtmp = errno;
107 #else
108 int errtmp = WSAGetLastError();
109 #endif
110 #ifdef SO_ERROR
111 int err = 0;
112 socklen_t len = sizeof(err);
113
114 if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
115 {
116 if (err)
117 errtmp = err;
118 }
119 errno = errtmp;
120 #endif
121 return errtmp;
122 }
123
124 /*
125 * report_error - report an error from an errno.
126 * Record error to log and also send a copy to all *LOCAL* opers online.
127 *
128 * text is a *format* string for outputing error. It must
129 * contain only two '%s', the first will be replaced
130 * by the sockhost from the client_p, and the latter will
131 * be taken from sys_errlist[errno].
132 *
133 * client_p if not NULL, is the *LOCAL* client associated with
134 * the error.
135 *
136 * Cannot use perror() within daemon. stderr is closed in
137 * ircd and cannot be used. And, worse yet, it might have
138 * been reassigned to a normal connection...
139 *
140 * Actually stderr is still there IFF ircd was run with -s --Rodder
141 */
142
143 void
144 report_error(int level, const char* text, const char* who, int error)
145 {
146 who = (who) ? who : "";
147
148 sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
149 log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
150 ilog(L_ERROR, text, who, strerror(error));
151 }
152
153 /*
154 * setup_socket()
155 *
156 * Set the socket non-blocking, and other wonderful bits.
157 */
158 static void *
159 setup_socket(va_list args)
160 {
161 int fd = va_arg(args, int);
162 int opt = 1;
163
164 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
165
166 #ifdef IPTOS_LOWDELAY
167 opt = IPTOS_LOWDELAY;
168 setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
169 #endif
170
171 #ifndef _WIN32
172 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
173 #endif
174
175 return NULL;
176 }
177
178 /*
179 * init_comm()
180 *
181 * Initializes comm subsystem.
182 */
183 void
184 init_comm(void)
185 {
186 setup_socket_cb = register_callback("setup_socket", setup_socket);
187 init_netio();
188 }
189
190 /*
191 * close_connection
192 * Close the physical connection. This function must make
193 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
194 */
195 void
196 close_connection(struct Client *client_p)
197 {
198 struct ConfItem *conf;
199 struct AccessItem *aconf;
200 struct ClassItem *aclass;
201
202 assert(NULL != client_p);
203
204 if (IsServer(client_p))
205 {
206 ServerStats->is_sv++;
207 ServerStats->is_sbs += client_p->localClient->send.bytes;
208 ServerStats->is_sbr += client_p->localClient->recv.bytes;
209 ServerStats->is_sti += CurrentTime - client_p->firsttime;
210
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 if ((conf = find_conf_exact(SERVER_TYPE,
220 client_p->name, client_p->username,
221 client_p->host)))
222 {
223 /*
224 * Reschedule a faster reconnect, if this was a automatically
225 * connected configuration entry. (Note that if we have had
226 * a rehash in between, the status has been changed to
227 * CONF_ILLEGAL). But only do this if it was a "good" link.
228 */
229 aconf = (struct AccessItem *)map_to_conf(conf);
230 aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
231 aconf->hold = time(NULL);
232 aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
233 HANGONRETRYDELAY : ConFreq(aclass);
234 if (nextconnect > aconf->hold)
235 nextconnect = aconf->hold;
236 }
237 }
238 else if (IsClient(client_p))
239 {
240 ServerStats->is_cl++;
241 ServerStats->is_cbs += client_p->localClient->send.bytes;
242 ServerStats->is_cbr += client_p->localClient->recv.bytes;
243 ServerStats->is_cti += CurrentTime - client_p->firsttime;
244 }
245 else
246 ServerStats->is_ni++;
247
248 if (!IsDead(client_p))
249 {
250 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
251 /* there is still a chance that we might send data to this socket
252 * even if it is marked as blocked (COMM_SELECT_READ handler is called
253 * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
254 */
255 ClearSendqBlocked(client_p);
256 send_queued_write(client_p);
257 }
258
259 #ifdef HAVE_LIBCRYPTO
260 if (client_p->localClient->fd.ssl)
261 SSL_shutdown(client_p->localClient->fd.ssl);
262 #endif
263 if (client_p->localClient->fd.flags.open)
264 fd_close(&client_p->localClient->fd);
265
266 if (HasServlink(client_p))
267 {
268 if (client_p->localClient->ctrlfd.flags.open)
269 fd_close(&client_p->localClient->ctrlfd);
270 }
271
272 dbuf_clear(&client_p->localClient->buf_sendq);
273 dbuf_clear(&client_p->localClient->buf_recvq);
274
275 MyFree(client_p->localClient->passwd);
276 detach_conf(client_p, CONF_TYPE);
277 client_p->from = NULL; /* ...this should catch them! >:) --msa */
278 }
279
280 #ifdef HAVE_LIBCRYPTO
281 /*
282 * ssl_handshake - let OpenSSL initialize the protocol. Register for
283 * read/write events if necessary.
284 */
285 static void
286 ssl_handshake(int fd, struct Client *client_p)
287 {
288 int ret = SSL_accept(client_p->localClient->fd.ssl);
289
290 if (ret <= 0)
291 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
292 {
293 case SSL_ERROR_WANT_WRITE:
294 comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
295 (PF *) ssl_handshake, client_p, 0);
296 return;
297
298 case SSL_ERROR_WANT_READ:
299 comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
300 (PF *) ssl_handshake, client_p, 0);
301 return;
302
303 default:
304 exit_client(client_p, client_p, "Error during SSL handshake");
305 return;
306 }
307
308 execute_callback(auth_cb, client_p);
309 }
310 #endif
311
312 /*
313 * add_connection - creates a client which has just connected to us on
314 * the given fd. The sockhost field is initialized with the ip# of the host.
315 * An unique id is calculated now, in case it is needed for auth.
316 * The client is sent to the auth module for verification, and not put in
317 * any client list yet.
318 */
319 void
320 add_connection(struct Listener* listener, int fd)
321 {
322 struct Client *new_client;
323 socklen_t len = sizeof(struct irc_ssaddr);
324 struct irc_ssaddr irn;
325 assert(NULL != listener);
326
327 /*
328 * get the client socket name from the socket
329 * the client has already been checked out in accept_connection
330 */
331
332 memset(&irn, 0, sizeof(irn));
333 if (getpeername(fd, (struct sockaddr *)&irn, (socklen_t *)&len))
334 {
335 #ifdef _WIN32
336 errno = WSAGetLastError();
337 #endif
338 report_error(L_ALL, "Failed in adding new connection %s :%s",
339 get_listener_name(listener), errno);
340 ServerStats->is_ref++;
341 #ifdef _WIN32
342 closesocket(fd);
343 #else
344 close(fd);
345 #endif
346 return;
347 }
348
349 #ifdef IPV6
350 remove_ipv6_mapping(&irn);
351 #else
352 irn.ss_len = len;
353 #endif
354 new_client = make_client(NULL);
355 fd_open(&new_client->localClient->fd, fd, 1,
356 (listener->flags & LISTENER_SSL) ?
357 "Incoming SSL connection" : "Incoming connection");
358 memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
359
360 /*
361 * copy address to 'sockhost' as a string, copy it to host too
362 * so we have something valid to put into error messages...
363 */
364 new_client->localClient->port = ntohs(irn.ss_port);
365 memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr));
366
367 irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
368 new_client->localClient->ip.ss_len, new_client->sockhost,
369 HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
370 new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
371
372 *new_client->host = '\0';
373 #ifdef IPV6
374 if (*new_client->sockhost == ':')
375 strlcat(new_client->host, "0", HOSTLEN+1);
376
377 if (new_client->localClient->aftype == AF_INET6 &&
378 ConfigFileEntry.dot_in_ip6_addr == 1)
379 {
380 strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
381 strlcat(new_client->host, ".", HOSTLEN+1);
382 } else
383 #endif
384 strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
385
386 new_client->localClient->listener = listener;
387 ++listener->ref_count;
388
389 connect_id++;
390 new_client->connect_id = connect_id;
391
392 #ifdef HAVE_LIBCRYPTO
393 if ((listener->flags & LISTENER_SSL))
394 {
395 if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
396 {
397 ilog(L_CRIT, "SSL_new() ERROR! -- %s",
398 ERR_error_string(ERR_get_error(), NULL));
399
400 SetDead(new_client);
401 exit_client(new_client, new_client, "SSL_new failed");
402 return;
403 }
404
405 SSL_set_fd(new_client->localClient->fd.ssl, fd);
406 ssl_handshake(0, new_client);
407 }
408 else
409 #endif
410 execute_callback(auth_cb, new_client);
411 }
412
413 /*
414 * stolen from squid - its a neat (but overused! :) routine which we
415 * can use to see whether we can ignore this errno or not. It is
416 * generally useful for non-blocking network IO related errnos.
417 * -- adrian
418 */
419 int
420 ignoreErrno(int ierrno)
421 {
422 switch (ierrno)
423 {
424 case EINPROGRESS:
425 case EWOULDBLOCK:
426 #if EAGAIN != EWOULDBLOCK
427 case EAGAIN:
428 #endif
429 case EALREADY:
430 case EINTR:
431 #ifdef ERESTART
432 case ERESTART:
433 #endif
434 return 1;
435 default:
436 return 0;
437 }
438 }
439
440 /*
441 * comm_settimeout() - set the socket timeout
442 *
443 * Set the timeout for the fd
444 */
445 void
446 comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
447 {
448 assert(fd->flags.open);
449
450 fd->timeout = CurrentTime + (timeout / 1000);
451 fd->timeout_handler = callback;
452 fd->timeout_data = cbdata;
453 }
454
455 /*
456 * comm_setflush() - set a flush function
457 *
458 * A flush function is simply a function called if found during
459 * comm_timeouts(). Its basically a second timeout, except in this case
460 * I'm too lazy to implement multiple timeout functions! :-)
461 * its kinda nice to have it separate, since this is designed for
462 * flush functions, and when comm_close() is implemented correctly
463 * with close functions, we _actually_ don't call comm_close() here ..
464 * -- originally Adrian's notes
465 * comm_close() is replaced with fd_close() in fdlist.c
466 */
467 void
468 comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
469 {
470 assert(fd->flags.open);
471
472 fd->flush_timeout = CurrentTime + (timeout / 1000);
473 fd->flush_handler = callback;
474 fd->flush_data = cbdata;
475 }
476
477 /*
478 * comm_checktimeouts() - check the socket timeouts
479 *
480 * All this routine does is call the given callback/cbdata, without closing
481 * down the file descriptor. When close handlers have been implemented,
482 * this will happen.
483 */
484 void
485 comm_checktimeouts(void *notused)
486 {
487 int i;
488 fde_t *F;
489 PF *hdl;
490 void *data;
491
492 for (i = 0; i < FD_HASH_SIZE; i++)
493 for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop)
494 {
495 assert(F->flags.open);
496 fd_next_in_loop = F->hnext;
497
498 /* check flush functions */
499 if (F->flush_handler && F->flush_timeout > 0 &&
500 F->flush_timeout < CurrentTime)
501 {
502 hdl = F->flush_handler;
503 data = F->flush_data;
504 comm_setflush(F, 0, NULL, NULL);
505 hdl(F, data);
506 }
507
508 /* check timeouts */
509 if (F->timeout_handler && F->timeout > 0 &&
510 F->timeout < CurrentTime)
511 {
512 /* Call timeout handler */
513 hdl = F->timeout_handler;
514 data = F->timeout_data;
515 comm_settimeout(F, 0, NULL, NULL);
516 hdl(F, data);
517 }
518 }
519 }
520
521 /*
522 * void comm_connect_tcp(int fd, const char *host, unsigned short port,
523 * struct sockaddr *clocal, int socklen,
524 * CNCB *callback, void *data, int aftype, int timeout)
525 * Input: An fd to connect with, a host and port to connect to,
526 * a local sockaddr to connect from + length(or NULL to use the
527 * default), a callback, the data to pass into the callback, the
528 * address family.
529 * Output: None.
530 * Side-effects: A non-blocking connection to the host is started, and
531 * if necessary, set up for selection. The callback given
532 * may be called now, or it may be called later.
533 */
534 void
535 comm_connect_tcp(fde_t *fd, const char *host, unsigned short port,
536 struct sockaddr *clocal, int socklen, CNCB *callback,
537 void *data, int aftype, int timeout)
538 {
539 struct addrinfo hints, *res;
540 char portname[PORTNAMELEN+1];
541
542 assert(callback);
543 fd->connect.callback = callback;
544 fd->connect.data = data;
545
546 fd->connect.hostaddr.ss.ss_family = aftype;
547 fd->connect.hostaddr.ss_port = htons(port);
548
549 /* Note that we're using a passed sockaddr here. This is because
550 * generally you'll be bind()ing to a sockaddr grabbed from
551 * getsockname(), so this makes things easier.
552 * XXX If NULL is passed as local, we should later on bind() to the
553 * virtual host IP, for completeness.
554 * -- adrian
555 */
556 if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
557 {
558 /* Failure, call the callback with COMM_ERR_BIND */
559 comm_connect_callback(fd, COMM_ERR_BIND);
560 /* ... and quit */
561 return;
562 }
563
564 /* Next, if we have been given an IP, get the addr and skip the
565 * DNS check (and head direct to comm_connect_tryconnect().
566 */
567 memset(&hints, 0, sizeof(hints));
568 hints.ai_family = AF_UNSPEC;
569 hints.ai_socktype = SOCK_STREAM;
570 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
571
572 snprintf(portname, PORTNAMELEN, "%d", port);
573
574 if (irc_getaddrinfo(host, portname, &hints, &res))
575 {
576 /* Send the DNS request, for the next level */
577 fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
578 fd->dns_query->ptr = fd;
579 fd->dns_query->callback = comm_connect_dns_callback;
580 gethost_byname(host, fd->dns_query);
581 }
582 else
583 {
584 /* We have a valid IP, so we just call tryconnect */
585 /* Make sure we actually set the timeout here .. */
586 assert(res != NULL);
587 memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
588 fd->connect.hostaddr.ss_len = res->ai_addrlen;
589 fd->connect.hostaddr.ss.ss_family = res->ai_family;
590 irc_freeaddrinfo(res);
591 comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
592 comm_connect_tryconnect(fd, NULL);
593 }
594 }
595
596 /*
597 * comm_connect_callback() - call the callback, and continue with life
598 */
599 static void
600 comm_connect_callback(fde_t *fd, int status)
601 {
602 CNCB *hdl;
603
604 /* This check is gross..but probably necessary */
605 if (fd->connect.callback == NULL)
606 return;
607
608 /* Clear the connect flag + handler */
609 hdl = fd->connect.callback;
610 fd->connect.callback = NULL;
611
612 /* Clear the timeout handler */
613 comm_settimeout(fd, 0, NULL, NULL);
614
615 /* Call the handler */
616 hdl(fd, status, fd->connect.data);
617 }
618
619 /*
620 * comm_connect_timeout() - this gets called when the socket connection
621 * times out. This *only* can be called once connect() is initially
622 * called ..
623 */
624 static void
625 comm_connect_timeout(fde_t *fd, void *notused)
626 {
627 /* error! */
628 comm_connect_callback(fd, COMM_ERR_TIMEOUT);
629 }
630
631 /*
632 * comm_connect_dns_callback() - called at the completion of the DNS request
633 *
634 * The DNS request has completed, so if we've got an error, return it,
635 * otherwise we initiate the connect()
636 */
637 static void
638 comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
639 {
640 fde_t *F = vptr;
641
642 if (reply == NULL)
643 {
644 MyFree(F->dns_query);
645 F->dns_query = NULL;
646 comm_connect_callback(F, COMM_ERR_DNS);
647 return;
648 }
649
650 /* No error, set a 10 second timeout */
651 comm_settimeout(F, 30*1000, comm_connect_timeout, NULL);
652
653 /* Copy over the DNS reply info so we can use it in the connect() */
654 /*
655 * Note we don't fudge the refcount here, because we aren't keeping
656 * the DNS record around, and the DNS cache is gone anyway..
657 * -- adrian
658 */
659 memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
660 /* The cast is hacky, but safe - port offset is same on v4 and v6 */
661 ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
662 F->connect.hostaddr.ss_port;
663 F->connect.hostaddr.ss_len = reply->addr.ss_len;
664
665 /* Now, call the tryconnect() routine to try a connect() */
666 MyFree(F->dns_query);
667 F->dns_query = NULL;
668 comm_connect_tryconnect(F, NULL);
669 }
670
671 /* static void comm_connect_tryconnect(int fd, void *notused)
672 * Input: The fd, the handler data(unused).
673 * Output: None.
674 * Side-effects: Try and connect with pending connect data for the FD. If
675 * we succeed or get a fatal error, call the callback.
676 * Otherwise, it is still blocking or something, so register
677 * to select for a write event on this FD.
678 */
679 static void
680 comm_connect_tryconnect(fde_t *fd, void *notused)
681 {
682 int retval;
683
684 /* This check is needed or re-entrant s_bsd_* like sigio break it. */
685 if (fd->connect.callback == NULL)
686 return;
687
688 /* Try the connect() */
689 retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
690 fd->connect.hostaddr.ss_len);
691
692 /* Error? */
693 if (retval < 0)
694 {
695 #ifdef _WIN32
696 errno = WSAGetLastError();
697 #endif
698 /*
699 * If we get EISCONN, then we've already connect()ed the socket,
700 * which is a good thing.
701 * -- adrian
702 */
703 if (errno == EISCONN)
704 comm_connect_callback(fd, COMM_OK);
705 else if (ignoreErrno(errno))
706 /* Ignore error? Reschedule */
707 comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect,
708 NULL, 0);
709 else
710 /* Error? Fail with COMM_ERR_CONNECT */
711 comm_connect_callback(fd, COMM_ERR_CONNECT);
712 return;
713 }
714
715 /* If we get here, we've suceeded, so call with COMM_OK */
716 comm_connect_callback(fd, COMM_OK);
717 }
718
719 /*
720 * comm_errorstr() - return an error string for the given error condition
721 */
722 const char *
723 comm_errstr(int error)
724 {
725 if (error < 0 || error >= COMM_ERR_MAX)
726 return "Invalid error number!";
727 return comm_err_str[error];
728 }
729
730 /*
731 * comm_open() - open a socket
732 *
733 * This is a highly highly cut down version of squid's comm_open() which
734 * for the most part emulates socket(), *EXCEPT* it fails if we're about
735 * to run out of file descriptors.
736 */
737 int
738 comm_open(fde_t *F, int family, int sock_type, int proto, const char *note)
739 {
740 int fd;
741
742 /* First, make sure we aren't going to run out of file descriptors */
743 if (number_fd >= hard_fdlimit)
744 {
745 errno = ENFILE;
746 return -1;
747 }
748
749 /*
750 * Next, we try to open the socket. We *should* drop the reserved FD
751 * limit if/when we get an error, but we can deal with that later.
752 * XXX !!! -- adrian
753 */
754 fd = socket(family, sock_type, proto);
755 if (fd < 0)
756 {
757 #ifdef _WIN32
758 errno = WSAGetLastError();
759 #endif
760 return -1; /* errno will be passed through, yay.. */
761 }
762
763 execute_callback(setup_socket_cb, fd);
764
765 /* update things in our fd tracking */
766 fd_open(F, fd, 1, note);
767 return 0;
768 }
769
770 /*
771 * comm_accept() - accept an incoming connection
772 *
773 * This is a simple wrapper for accept() which enforces FD limits like
774 * comm_open() does. Returned fd must be either closed or tagged with
775 * fd_open (this function no longer does it).
776 */
777 int
778 comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
779 {
780 int newfd;
781 socklen_t addrlen = sizeof(struct irc_ssaddr);
782
783 if (number_fd >= hard_fdlimit)
784 {
785 errno = ENFILE;
786 return -1;
787 }
788
789 /*
790 * Next, do the accept(). if we get an error, we should drop the
791 * reserved fd limit, but we can deal with that when comm_open()
792 * also does it. XXX -- adrian
793 */
794 newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
795 if (newfd < 0)
796 {
797 #ifdef _WIN32
798 errno = WSAGetLastError();
799 #endif
800 return -1;
801 }
802
803 #ifdef IPV6
804 remove_ipv6_mapping(pn);
805 #else
806 pn->ss_len = addrlen;
807 #endif
808
809 execute_callback(setup_socket_cb, newfd);
810
811 /* .. and return */
812 return newfd;
813 }
814
815 /*
816 * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
817 * This function should really inspect the struct itself rather than relying
818 * on inet_pton and inet_ntop. OSes with IPv6 mapping listening on both
819 * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
820 *
821 */
822 #ifdef IPV6
823 void
824 remove_ipv6_mapping(struct irc_ssaddr *addr)
825 {
826 if (addr->ss.ss_family == AF_INET6)
827 {
828 struct sockaddr_in6 *v6;
829
830 v6 = (struct sockaddr_in6*)addr;
831 if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
832 {
833 char v4ip[HOSTIPLEN];
834 struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
835 inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
836 inet_pton(AF_INET, v4ip, &v4->sin_addr);
837 addr->ss.ss_family = AF_INET;
838 addr->ss_len = sizeof(struct sockaddr_in);
839 }
840 else
841 addr->ss_len = sizeof(struct sockaddr_in6);
842 }
843 else
844 addr->ss_len = sizeof(struct sockaddr_in);
845 }
846 #endif

Properties

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

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