ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/s_bsd.c
(Generate patch)

Comparing:
ircd-hybrid-7.2/src/s_bsd.c (file contents), Revision 950 by michael, Tue Jul 21 23:07:52 2009 UTC vs.
ircd-hybrid/branches/8.2.x/src/s_bsd.c (file contents), Revision 4115 by michael, Tue Jul 1 16:49:54 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  s_bsd.c: Network functions.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2014 ircd-hybrid development team
5   *
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
# Line 18 | Line 17
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 < *  $Id$
20 > */
21 >
22 > /*! \file s_bsd.c
23 > * \brief Network functions.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
28 < #ifndef _WIN32
28 > #ifdef HAVE_LIBCRYPTO
29 > #include "rsa.h"
30 > #endif
31   #include <netinet/in_systm.h>
32   #include <netinet/ip.h>
33   #include <netinet/tcp.h>
34 < #endif
34 > #include "list.h"
35   #include "fdlist.h"
36   #include "s_bsd.h"
37   #include "client.h"
34 #include "common.h"
38   #include "dbuf.h"
39   #include "event.h"
40   #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
41   #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"
45 > #include "res.h"
46   #include "restart.h"
47 < #include "s_auth.h"
48 < #include "s_conf.h"
49 < #include "s_log.h"
50 < #include "s_serv.h"
47 > #include "auth.h"
48 > #include "conf.h"
49 > #include "log.h"
50 > #include "server.h"
51   #include "send.h"
52   #include "memory.h"
53 < #include "s_user.h"
54 < #include "hook.h"
53 > #include "user.h"
54 >
55  
56   static const char *comm_err_str[] = { "Comm OK", "Error during bind()",
57    "Error during DNS lookup", "connect timeout", "Error during connect()",
58    "Comm Error" };
59  
60 < struct Callback *setup_socket_cb = NULL;
62 <
63 < static void comm_connect_callback(fde_t *fd, int status);
60 > static void comm_connect_callback(fde_t *, int);
61   static PF comm_connect_timeout;
62 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
62 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
63   static PF comm_connect_tryconnect;
64  
65  
# Line 80 | Line 77 | check_can_use_v6(void)
77    else
78    {
79      ServerInfo.can_use_v6 = 1;
83 #ifdef _WIN32
84    closesocket(v6);
85 #else
80      close(v6);
87 #endif
81    }
82   #else
83    ServerInfo.can_use_v6 = 0;
# Line 100 | Line 93 | check_can_use_v6(void)
93   int
94   get_sockerr(int fd)
95   {
103 #ifndef _WIN32
96    int errtmp = errno;
105 #else
106  int errtmp = WSAGetLastError();
107 #endif
97   #ifdef SO_ERROR
98    int err = 0;
99    socklen_t len = sizeof(err);
100  
101 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
101 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
102    {
103      if (err)
104        errtmp = err;
# Line 120 | Line 109 | get_sockerr(int fd)
109   }
110  
111   /*
112 < * report_error - report an error from an errno.
112 > * report_error - report an error from an errno.
113   * Record error to log and also send a copy to all *LOCAL* opers online.
114   *
115   *        text        is a *format* string for outputing error. It must
# Line 134 | Line 123 | get_sockerr(int fd)
123   * Cannot use perror() within daemon. stderr is closed in
124   * ircd and cannot be used. And, worse yet, it might have
125   * been reassigned to a normal connection...
126 < *
126 > *
127   * Actually stderr is still there IFF ircd was run with -s --Rodder
128   */
129  
130   void
131 < report_error(int level, const char* text, const char* who, int error)
131 > report_error(int level, const char* text, const char* who, int error)
132   {
133    who = (who) ? who : "";
134  
135 <  sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
136 <  log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
137 <  ilog(L_ERROR, text, who, strerror(error));
135 >  sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE,
136 >                       text, who, strerror(error));
137 >  ilog(LOG_TYPE_IRCD, text, who, strerror(error));
138   }
139  
140   /*
# Line 153 | Line 142 | report_error(int level, const char* text
142   *
143   * Set the socket non-blocking, and other wonderful bits.
144   */
145 < static void *
146 < setup_socket(va_list args)
145 > static void
146 > setup_socket(int fd)
147   {
159  int fd = va_arg(args, int);
148    int opt = 1;
149  
150 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
150 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
151  
152   #ifdef IPTOS_LOWDELAY
153    opt = IPTOS_LOWDELAY;
154 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
154 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
155   #endif
156  
169 #ifndef _WIN32
157    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
171 #endif
172
173  return NULL;
174 }
175
176 /*
177 * init_comm()
178 *
179 * Initializes comm subsystem.
180 */
181 void
182 init_comm(void)
183 {
184  setup_socket_cb = register_callback("setup_socket", setup_socket);
185  init_netio();
158   }
159  
160   /*
# Line 193 | Line 165 | init_comm(void)
165   void
166   close_connection(struct Client *client_p)
167   {
168 <  struct ConfItem *conf;
197 <  struct AccessItem *aconf;
198 <  struct ClassItem *aclass;
168 >  dlink_node *ptr = NULL;
169  
170 <  assert(NULL != client_p);
170 >  assert(client_p);
171  
172    if (!IsDead(client_p))
173    {
# Line 206 | Line 176 | close_connection(struct Client *client_p
176       * even if it is marked as blocked (COMM_SELECT_READ handler is called
177       * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
178       */
179 <    ClearSendqBlocked(client_p);
179 >    DelFlag(client_p, FLAGS_BLOCKED);
180      send_queued_write(client_p);
181    }
182  
183 <  if (IsServer(client_p))
183 >  if (IsClient(client_p))
184 >  {
185 >    ++ServerStats.is_cl;
186 >    ServerStats.is_cbs += client_p->localClient->send.bytes;
187 >    ServerStats.is_cbr += client_p->localClient->recv.bytes;
188 >    ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime;
189 >  }
190 >  else if (IsServer(client_p))
191    {
192      ++ServerStats.is_sv;
193      ServerStats.is_sbs += client_p->localClient->send.bytes;
194      ServerStats.is_sbr += client_p->localClient->recv.bytes;
195 <    ServerStats.is_sti += CurrentTime - client_p->firsttime;
195 >    ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime;
196  
197 <    /* XXX Does this even make any sense at all anymore?
221 <     * scheduling a 'quick' reconnect could cause a pile of
222 <     * nick collides under TSora protocol... -db
223 <     */
224 <    /*
225 <     * If the connection has been up for a long amount of time, schedule
226 <     * a 'quick' reconnect, else reset the next-connect cycle.
227 <     */
228 <    if ((conf = find_conf_exact(SERVER_TYPE,
229 <                                  client_p->name, client_p->username,
230 <                                  client_p->host)))
197 >    DLINK_FOREACH(ptr, server_items.head)
198      {
199 +      struct MaskItem *conf = ptr->data;
200 +
201 +      if (irccmp(conf->name, client_p->name))
202 +        continue;
203 +
204        /*
205 <       * Reschedule a faster reconnect, if this was a automatically
206 <       * connected configuration entry. (Note that if we have had
235 <       * a rehash in between, the status has been changed to
236 <       * CONF_ILLEGAL). But only do this if it was a "good" link.
205 >       * Reset next-connect cycle of all connect{} blocks that match
206 >       * this servername.
207         */
208 <      aconf = (struct AccessItem *)map_to_conf(conf);
239 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
240 <      aconf->hold = time(NULL);
241 <      aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
242 <        HANGONRETRYDELAY : ConFreq(aclass);
243 <      if (nextconnect > aconf->hold)
244 <        nextconnect = aconf->hold;
208 >      conf->until = CurrentTime + conf->class->con_freq;
209      }
210    }
247  else if (IsClient(client_p))
248  {
249    ++ServerStats.is_cl;
250    ServerStats.is_cbs += client_p->localClient->send.bytes;
251    ServerStats.is_cbr += client_p->localClient->recv.bytes;
252    ServerStats.is_cti += CurrentTime - client_p->firsttime;
253  }
211    else
212      ++ServerStats.is_ni;
213  
# Line 266 | Line 223 | close_connection(struct Client *client_p
223    if (client_p->localClient->fd.flags.open)
224      fd_close(&client_p->localClient->fd);
225  
269  if (HasServlink(client_p))
270  {
271    if (client_p->localClient->ctrlfd.flags.open)
272      fd_close(&client_p->localClient->ctrlfd);
273  }
274
226    dbuf_clear(&client_p->localClient->buf_sendq);
227    dbuf_clear(&client_p->localClient->buf_recvq);
228 <  
228 >
229    MyFree(client_p->localClient->passwd);
230 <  detach_conf(client_p, CONF_TYPE);
280 <  client_p->from = NULL; /* ...this should catch them! >:) --msa */
230 >  detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER);
231   }
232  
233   #ifdef HAVE_LIBCRYPTO
# Line 288 | Line 238 | close_connection(struct Client *client_p
238   static void
239   ssl_handshake(int fd, struct Client *client_p)
240   {
241 <  int ret = SSL_accept(client_p->localClient->fd.ssl);
241 >  X509 *cert = NULL;
242 >  int ret = 0;
243 >
244 >  if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0)
245 >  {
246 >    if ((CurrentTime - client_p->localClient->firsttime) > 30)
247 >    {
248 >      exit_client(client_p, "Timeout during SSL handshake");
249 >      return;
250 >    }
251  
293  if (ret <= 0)
252      switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
253      {
254        case SSL_ERROR_WANT_WRITE:
255          comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
256 <                       (PF *) ssl_handshake, client_p, 0);
256 >                       (PF *)ssl_handshake, client_p, 30);
257          return;
258  
259        case SSL_ERROR_WANT_READ:
260          comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
261 <                       (PF *) ssl_handshake, client_p, 0);
261 >                       (PF *)ssl_handshake, client_p, 30);
262          return;
263  
264        default:
265 <        exit_client(client_p, client_p, "Error during SSL handshake");
266 <        return;
265 >        exit_client(client_p, "Error during SSL handshake");
266 >        return;
267 >    }
268 >  }
269 >
270 >  comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL);
271 >
272 >  if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
273 >  {
274 >    int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
275 >    char buf[EVP_MAX_MD_SIZE * 2 + 1] = "";
276 >    unsigned char md[EVP_MAX_MD_SIZE] = "";
277 >
278 >    if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
279 >        res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
280 >        res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
281 >    {
282 >      unsigned int n = 0;
283 >
284 >      if (X509_digest(cert, ServerInfo.message_digest_algorithm, md, &n))
285 >      {
286 >        binary_to_hex(md, buf, n);
287 >        client_p->certfp = xstrdup(buf);
288 >      }
289      }
290 +    else
291 +      ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d",
292 +           client_p->name, client_p->username, client_p->host, res);
293 +    X509_free(cert);
294 +  }
295  
296 <  execute_callback(auth_cb, client_p);
296 >  start_auth(client_p);
297   }
298   #endif
299  
300   /*
301 < * add_connection - creates a client which has just connected to us on
301 > * add_connection - creates a client which has just connected to us on
302   * the given fd. The sockhost field is initialized with the ip# of the host.
303   * An unique id is calculated now, in case it is needed for auth.
304   * The client is sent to the auth module for verification, and not put in
# Line 322 | Line 307 | ssl_handshake(int fd, struct Client *cli
307   void
308   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
309   {
310 <  struct Client *new_client;
326 <
327 <  assert(NULL != listener);
310 >  struct Client *client_p = make_client(NULL);
311  
312 <  new_client = make_client(NULL);
330 <
331 <  fd_open(&new_client->localClient->fd, fd, 1,
312 >  fd_open(&client_p->localClient->fd, fd, 1,
313            (listener->flags & LISTENER_SSL) ?
314 <          "Incoming SSL connection" : "Incoming connection");
314 >          "Incoming SSL connection" : "Incoming connection");
315  
316 <  /*
316 >  /*
317     * copy address to 'sockhost' as a string, copy it to host too
318     * so we have something valid to put into error messages...
319     */
320 <  memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
320 >  memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr));
321  
322 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
323 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
324 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
325 <  new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family;
345 < #ifdef IPV6
346 <  if (new_client->sockhost[0] == ':')
347 <    strlcat(new_client->host, "0", HOSTLEN+1);
322 >  getnameinfo((struct sockaddr *)&client_p->localClient->ip,
323 >              client_p->localClient->ip.ss_len, client_p->sockhost,
324 >              sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST);
325 >  client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family;
326  
327 <  if (new_client->localClient->aftype == AF_INET6 &&
328 <      ConfigFileEntry.dot_in_ip6_addr == 1)
327 > #ifdef HAVE_LIBGEOIP
328 >  /* XXX IPV6 SUPPORT XXX */
329 >  if (irn->ss.ss_family == AF_INET && geoip_ctx)
330    {
331 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
332 <    strlcat(new_client->host, ".", HOSTLEN+1);
331 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip;
332 >    client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr));
333    }
355  else
334   #endif
357    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
335  
336 <  new_client->localClient->listener = listener;
336 >  if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':')
337 >  {
338 >    strlcpy(client_p->host, "0", sizeof(client_p->host));
339 >    strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1);
340 >    memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1);
341 >    client_p->sockhost[0] = '0';
342 >  }
343 >  else
344 >    strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host));
345 >
346 >  client_p->localClient->listener = listener;
347    ++listener->ref_count;
348  
349   #ifdef HAVE_LIBCRYPTO
350    if (listener->flags & LISTENER_SSL)
351    {
352 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
352 >    if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
353      {
354 <      ilog(L_CRIT, "SSL_new() ERROR! -- %s",
354 >      ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
355             ERR_error_string(ERR_get_error(), NULL));
356  
357 <      SetDead(new_client);
358 <      exit_client(new_client, new_client, "SSL_new failed");
357 >      SetDead(client_p);
358 >      exit_client(client_p, "SSL_new failed");
359        return;
360      }
361  
362 <    SSL_set_fd(new_client->localClient->fd.ssl, fd);
363 <    ssl_handshake(0, new_client);
362 >    AddFlag(client_p, FLAGS_SSL);
363 >    SSL_set_fd(client_p->localClient->fd.ssl, fd);
364 >    ssl_handshake(0, client_p);
365    }
366    else
367   #endif
368 <    execute_callback(auth_cb, new_client);
368 >    start_auth(client_p);
369   }
370  
371   /*
# Line 432 | Line 420 | comm_settimeout(fde_t *fd, time_t timeou
420   * flush functions, and when comm_close() is implemented correctly
421   * with close functions, we _actually_ don't call comm_close() here ..
422   * -- originally Adrian's notes
423 < * comm_close() is replaced with fd_close() in fdlist.c
423 > * comm_close() is replaced with fd_close() in fdlist.c
424   */
425   void
426   comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata)
# Line 507 | Line 495 | comm_connect_tcp(fde_t *fd, const char *
495                   void *data, int aftype, int timeout)
496   {
497    struct addrinfo hints, *res;
498 <  char portname[PORTNAMELEN+1];
498 >  char portname[PORTNAMELEN + 1];
499  
500    assert(callback);
501    fd->connect.callback = callback;
# Line 524 | Line 512 | comm_connect_tcp(fde_t *fd, const char *
512     *   -- adrian
513     */
514    if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0))
515 <  {
515 >  {
516      /* Failure, call the callback with COMM_ERR_BIND */
517      comm_connect_callback(fd, COMM_ERR_BIND);
518      /* ... and quit */
# Line 539 | Line 527 | comm_connect_tcp(fde_t *fd, const char *
527    hints.ai_socktype = SOCK_STREAM;
528    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
529  
530 <  snprintf(portname, PORTNAMELEN, "%d", port);
530 >  snprintf(portname, sizeof(portname), "%d", port);
531  
532 <  if (irc_getaddrinfo(host, portname, &hints, &res))
532 >  if (getaddrinfo(host, portname, &hints, &res))
533    {
534      /* Send the DNS request, for the next level */
547    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
548    fd->dns_query->ptr = fd;
549    fd->dns_query->callback = comm_connect_dns_callback;
535      if (aftype == AF_INET6)
536 <      gethost_byname_type(host, fd->dns_query, T_AAAA);
536 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
537      else
538 <      gethost_byname_type(host, fd->dns_query, T_A);
538 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
539    }
540    else
541    {
# Line 560 | Line 545 | comm_connect_tcp(fde_t *fd, const char *
545      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
546      fd->connect.hostaddr.ss_len = res->ai_addrlen;
547      fd->connect.hostaddr.ss.ss_family = res->ai_family;
548 <    irc_freeaddrinfo(res);
548 >    freeaddrinfo(res);
549      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
550      comm_connect_tryconnect(fd, NULL);
551    }
# Line 608 | Line 593 | comm_connect_timeout(fde_t *fd, void *no
593   * otherwise we initiate the connect()
594   */
595   static void
596 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
596 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
597   {
598    fde_t *F = vptr;
599  
600 <  if (reply == NULL)
600 >  if (name == NULL)
601    {
617    MyFree(F->dns_query);
618    F->dns_query = NULL;
602      comm_connect_callback(F, COMM_ERR_DNS);
603      return;
604    }
# Line 626 | Line 609 | comm_connect_dns_callback(void *vptr, st
609    /* Copy over the DNS reply info so we can use it in the connect() */
610    /*
611     * Note we don't fudge the refcount here, because we aren't keeping
612 <   * the DNS record around, and the DNS cache is gone anyway..
612 >   * the DNS record around, and the DNS cache is gone anyway..
613     *     -- adrian
614     */
615 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
615 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
616    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
617    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
618      F->connect.hostaddr.ss_port;
619 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
619 >  F->connect.hostaddr.ss_len = addr->ss_len;
620  
621    /* Now, call the tryconnect() routine to try a connect() */
639  MyFree(F->dns_query);
640  F->dns_query = NULL;
622    comm_connect_tryconnect(F, NULL);
623   }
624  
# Line 659 | Line 640 | comm_connect_tryconnect(fde_t *fd, void
640      return;
641  
642    /* Try the connect() */
643 <  retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
643 >  retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr,
644      fd->connect.hostaddr.ss_len);
645  
646    /* Error? */
647    if (retval < 0)
648    {
668 #ifdef _WIN32
669    errno = WSAGetLastError();
670 #endif
649      /*
650       * If we get EISCONN, then we've already connect()ed the socket,
651       * which is a good thing.
# Line 726 | Line 704 | comm_open(fde_t *F, int family, int sock
704     */
705    fd = socket(family, sock_type, proto);
706    if (fd < 0)
729  {
730 #ifdef _WIN32
731    errno = WSAGetLastError();
732 #endif
707      return -1; /* errno will be passed through, yay.. */
734  }
708  
709 <  execute_callback(setup_socket_cb, fd);
709 >  setup_socket(fd);
710  
711    /* update things in our fd tracking */
712    fd_open(F, fd, 1, note);
# Line 764 | Line 737 | comm_accept(struct Listener *lptr, struc
737     * reserved fd limit, but we can deal with that when comm_open()
738     * also does it. XXX -- adrian
739     */
740 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
740 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
741    if (newfd < 0)
769  {
770 #ifdef _WIN32
771    errno = WSAGetLastError();
772 #endif
742      return -1;
774  }
743  
744   #ifdef IPV6
745    remove_ipv6_mapping(pn);
# Line 779 | Line 747 | comm_accept(struct Listener *lptr, struc
747    pn->ss_len = addrlen;
748   #endif
749  
750 <  execute_callback(setup_socket_cb, newfd);
750 >  setup_socket(newfd);
751  
752    /* .. and return */
753    return newfd;
754   }
755  
756 < /*
756 > /*
757   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
758 < * This function should really inspect the struct itself rather than relying
791 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
758 > * OSes with IPv6 mapping listening on both
759   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
760 < *
760 > *
761   */
762   #ifdef IPV6
763   void
# Line 798 | Line 765 | remove_ipv6_mapping(struct irc_ssaddr *a
765   {
766    if (addr->ss.ss_family == AF_INET6)
767    {
768 <    struct sockaddr_in6 *v6;
802 <
803 <    v6 = (struct sockaddr_in6*)addr;
804 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
768 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
769      {
770 <      char v4ip[HOSTIPLEN];
771 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
772 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
773 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
770 >      struct sockaddr_in6 v6;
771 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
772 >
773 >      memcpy(&v6, addr, sizeof(v6));
774 >      memset(v4, 0, sizeof(struct sockaddr_in));
775 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
776 >
777        addr->ss.ss_family = AF_INET;
778        addr->ss_len = sizeof(struct sockaddr_in);
779      }
780 <    else
780 >    else
781        addr->ss_len = sizeof(struct sockaddr_in6);
782    }
783    else
784      addr->ss_len = sizeof(struct sockaddr_in);
785 < }
785 > }
786   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)