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 451 by michael, Sun Feb 12 16:19:54 2006 UTC vs.
ircd-hybrid/branches/8.2.x/src/s_bsd.c (file contents), Revision 4406 by michael, Wed Aug 6 18:40:24 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"
52 < #include "s_stats.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;
63 <
64 < 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  
69 extern void init_netio(void);
65  
66   /* check_can_use_v6()
67   *  Check if the system can open AF_INET6 sockets
# Line 78 | Line 73 | check_can_use_v6(void)
73    int v6;
74  
75    if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
76 <    ServerInfo.can_use_v6 = 0;
76 >    ConfigServerInfo.can_use_v6 = 0;
77    else
78    {
79 <    ServerInfo.can_use_v6 = 1;
85 < #ifdef _WIN32
86 <    closesocket(v6);
87 < #else
79 >    ConfigServerInfo.can_use_v6 = 1;
80      close(v6);
89 #endif
81    }
82   #else
83 <  ServerInfo.can_use_v6 = 0;
83 >  ConfigServerInfo.can_use_v6 = 0;
84   #endif
85   }
86  
# Line 102 | Line 93 | check_can_use_v6(void)
93   int
94   get_sockerr(int fd)
95   {
105 #ifndef _WIN32
96    int errtmp = errno;
107 #else
108  int errtmp = WSAGetLastError();
109 #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 122 | 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 136 | 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 155 | 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   {
161  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  
171 #ifndef _WIN32
157    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();
158   }
159  
160   /*
# Line 195 | Line 165 | init_comm(void)
165   void
166   close_connection(struct Client *client_p)
167   {
168 <  struct ConfItem *conf;
169 <  struct AccessItem *aconf;
170 <  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++;
168 >  dlink_node *ptr = NULL;
169 >
170 >  assert(client_p);
171  
172    if (!IsDead(client_p))
173    {
# Line 252 | 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 (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->localClient->firsttime;
196 +
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 +       * Reset next-connect cycle of all connect{} blocks that match
206 +       * this servername.
207 +       */
208 +      conf->until = CurrentTime + conf->class->con_freq;
209 +    }
210 +  }
211 +  else
212 +    ++ServerStats.is_ni;
213 +
214   #ifdef HAVE_LIBCRYPTO
215    if (client_p->localClient->fd.ssl)
216    {
# Line 268 | Line 223 | close_connection(struct Client *client_p
223    if (client_p->localClient->fd.flags.open)
224      fd_close(&client_p->localClient->fd);
225  
271  if (HasServlink(client_p))
272  {
273    if (client_p->localClient->ctrlfd.flags.open)
274      fd_close(&client_p->localClient->ctrlfd);
275  }
276
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);
282 <  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 290 | 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  
295  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 <  execute_callback(auth_cb, client_p);
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, ConfigServerInfo.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 >  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
305   * any client list yet.
306   */
307   void
308 < add_connection(struct Listener* listener, int fd)
308 > add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
309   {
310 <  struct Client *new_client;
328 <  socklen_t len = sizeof(struct irc_ssaddr);
329 <  struct irc_ssaddr irn;
330 <  assert(NULL != listener);
331 <
332 <  /*
333 <   * get the client socket name from the socket
334 <   * the client has already been checked out in accept_connection
335 <   */
310 >  struct Client *client_p = make_client(NULL);
311  
312 <  memset(&irn, 0, sizeof(irn));
338 <  if (getpeername(fd, (struct sockaddr *)&irn, (socklen_t *)&len))
339 <  {
340 < #ifdef _WIN32
341 <    errno = WSAGetLastError();
342 < #endif
343 <    report_error(L_ALL, "Failed in adding new connection %s :%s",
344 <            get_listener_name(listener), errno);
345 <    ServerStats->is_ref++;
346 < #ifdef _WIN32
347 <    closesocket(fd);
348 < #else
349 <    close(fd);
350 < #endif
351 <    return;
352 <  }
353 <
354 < #ifdef IPV6
355 <  remove_ipv6_mapping(&irn);
356 < #else
357 <  irn.ss_len = len;
358 < #endif
359 <  new_client = make_client(NULL);
360 <  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");
363 <  memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr));
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;
375 <
376 <  *new_client->host = '\0';
377 < #ifdef IPV6
378 <  if (*new_client->sockhost == ':')
379 <    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);
333 <  } else
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 >  }
334   #endif
388    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
335  
336 <  new_client->localClient->listener = listener;
337 <  ++listener->ref_count;
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 <  connect_id++;
347 <  new_client->connect_id = connect_id;
346 >  client_p->localClient->listener = listener;
347 >  ++listener->ref_count;
348  
349   #ifdef HAVE_LIBCRYPTO
350 <  if ((listener->flags & LISTENER_SSL))
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(ConfigServerInfo.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 466 | 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 541 | 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 558 | 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 573 | 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 */
535 <    fd->dns_query = MyMalloc(sizeof(struct DNSQuery));
536 <    fd->dns_query->ptr = fd;
537 <    fd->dns_query->callback = comm_connect_dns_callback;
538 <    gethost_byname(host, fd->dns_query);
535 >    if (aftype == AF_INET6)
536 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
537 >    else
538 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
539    }
540    else
541    {
# Line 591 | 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 639 | 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    {
648    MyFree(F->dns_query);
649    F->dns_query = NULL;
602      comm_connect_callback(F, COMM_ERR_DNS);
603      return;
604    }
# Line 657 | 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() */
670  MyFree(F->dns_query);
671  F->dns_query = NULL;
622    comm_connect_tryconnect(F, NULL);
623   }
624  
# Line 690 | 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    {
699 #ifdef _WIN32
700    errno = WSAGetLastError();
701 #endif
649      /*
650       * If we get EISCONN, then we've already connect()ed the socket,
651       * which is a good thing.
# Line 757 | Line 704 | comm_open(fde_t *F, int family, int sock
704     */
705    fd = socket(family, sock_type, proto);
706    if (fd < 0)
760  {
761 #ifdef _WIN32
762    errno = WSAGetLastError();
763 #endif
707      return -1; /* errno will be passed through, yay.. */
765  }
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 779 | Line 721 | comm_open(fde_t *F, int family, int sock
721   * fd_open (this function no longer does it).
722   */
723   int
724 < comm_accept(struct Listener *lptr, struct irc_ssaddr *pn)
724 > comm_accept(struct Listener *lptr, struct irc_ssaddr *addr)
725   {
726    int newfd;
727    socklen_t addrlen = sizeof(struct irc_ssaddr);
# Line 790 | Line 732 | comm_accept(struct Listener *lptr, struc
732      return -1;
733    }
734  
735 +  memset(&addr, 0, sizeof(struct irc_ssaddr));
736 +
737    /*
738     * Next, do the accept(). if we get an error, we should drop the
739     * reserved fd limit, but we can deal with that when comm_open()
740     * also does it. XXX -- adrian
741     */
742 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
742 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)addr, &addrlen);
743    if (newfd < 0)
800  {
801 #ifdef _WIN32
802    errno = WSAGetLastError();
803 #endif
744      return -1;
805  }
745  
746   #ifdef IPV6
747 <  remove_ipv6_mapping(pn);
747 >  remove_ipv6_mapping(addr);
748   #else
749 <  pn->ss_len = addrlen;
749 >  addr->ss_len = addrlen;
750   #endif
751  
752 <  execute_callback(setup_socket_cb, newfd);
752 >  setup_socket(newfd);
753  
754    /* .. and return */
755    return newfd;
756   }
757  
758 < /*
758 > /*
759   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
760 < * This function should really inspect the struct itself rather than relying
822 < * on inet_pton and inet_ntop.  OSes with IPv6 mapping listening on both
760 > * OSes with IPv6 mapping listening on both
761   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
762 < *
762 > *
763   */
764   #ifdef IPV6
765   void
# Line 829 | Line 767 | remove_ipv6_mapping(struct irc_ssaddr *a
767   {
768    if (addr->ss.ss_family == AF_INET6)
769    {
770 <    struct sockaddr_in6 *v6;
833 <
834 <    v6 = (struct sockaddr_in6*)addr;
835 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
770 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
771      {
772 <      char v4ip[HOSTIPLEN];
773 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
774 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
775 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
772 >      struct sockaddr_in6 v6;
773 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
774 >
775 >      memcpy(&v6, addr, sizeof(v6));
776 >      memset(v4, 0, sizeof(struct sockaddr_in));
777 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
778 >
779        addr->ss.ss_family = AF_INET;
780        addr->ss_len = sizeof(struct sockaddr_in);
781      }
782 <    else
782 >    else
783        addr->ss_len = sizeof(struct sockaddr_in6);
784    }
785    else
786      addr->ss_len = sizeof(struct sockaddr_in);
787 < }
787 > }
788   #endif

Diff Legend

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