ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/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-7.3/src/s_bsd.c (file contents), Revision 1123 by michael, Sun Feb 6 21:57:50 2011 UTC

# Line 23 | Line 23
23   */
24  
25   #include "stdinc.h"
26 #ifndef _WIN32
26   #include <netinet/in_systm.h>
27   #include <netinet/ip.h>
28   #include <netinet/tcp.h>
29 < #endif
29 > #include "list.h"
30   #include "fdlist.h"
31   #include "s_bsd.h"
32   #include "client.h"
# Line 35 | Line 34
34   #include "dbuf.h"
35   #include "event.h"
36   #include "irc_string.h"
38 #include "irc_getnameinfo.h"
39 #include "irc_getaddrinfo.h"
37   #include "ircd.h"
41 #include "list.h"
38   #include "listener.h"
39   #include "numeric.h"
40   #include "packet.h"
41   #include "irc_res.h"
46 #include "inet_misc.h"
42   #include "restart.h"
43   #include "s_auth.h"
44   #include "s_conf.h"
# Line 60 | Line 55 | static const char *comm_err_str[] = { "C
55  
56   struct Callback *setup_socket_cb = NULL;
57  
58 < static void comm_connect_callback(fde_t *fd, int status);
58 > static void comm_connect_callback(fde_t *, int);
59   static PF comm_connect_timeout;
60 < static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply);
60 > static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *);
61   static PF comm_connect_tryconnect;
62  
63  
# Line 80 | Line 75 | check_can_use_v6(void)
75    else
76    {
77      ServerInfo.can_use_v6 = 1;
83 #ifdef _WIN32
84    closesocket(v6);
85 #else
78      close(v6);
87 #endif
79    }
80   #else
81    ServerInfo.can_use_v6 = 0;
# Line 100 | Line 91 | check_can_use_v6(void)
91   int
92   get_sockerr(int fd)
93   {
103 #ifndef _WIN32
94    int errtmp = errno;
105 #else
106  int errtmp = WSAGetLastError();
107 #endif
95   #ifdef SO_ERROR
96    int err = 0;
97    socklen_t len = sizeof(err);
98  
99 <  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len))
99 >  if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len))
100    {
101      if (err)
102        errtmp = err;
# Line 159 | Line 146 | setup_socket(va_list args)
146    int fd = va_arg(args, int);
147    int opt = 1;
148  
149 <  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));
149 >  setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
150  
151   #ifdef IPTOS_LOWDELAY
152    opt = IPTOS_LOWDELAY;
153 <  setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt));
153 >  setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt));
154   #endif
155  
169 #ifndef _WIN32
156    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
171 #endif
157  
158    return NULL;
159   }
# Line 225 | Line 210 | close_connection(struct Client *client_p
210       * If the connection has been up for a long amount of time, schedule
211       * a 'quick' reconnect, else reset the next-connect cycle.
212       */
213 <    if ((conf = find_conf_exact(SERVER_TYPE,
214 <                                  client_p->name, client_p->username,
230 <                                  client_p->host)))
213 >    if ((conf = find_conf_exact(SERVER_TYPE, client_p->name,
214 >                                client_p->username, client_p->host)))
215      {
216        /*
217         * Reschedule a faster reconnect, if this was a automatically
# Line 235 | Line 219 | close_connection(struct Client *client_p
219         * a rehash in between, the status has been changed to
220         * CONF_ILLEGAL). But only do this if it was a "good" link.
221         */
222 <      aconf = (struct AccessItem *)map_to_conf(conf);
223 <      aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr);
222 >      aconf  = map_to_conf(conf);
223 >      aclass = map_to_conf(aconf->class_ptr);
224        aconf->hold = time(NULL);
225        aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
226          HANGONRETRYDELAY : ConFreq(aclass);
243      if (nextconnect > aconf->hold)
244        nextconnect = aconf->hold;
227      }
228    }
229    else if (IsClient(client_p))
# Line 322 | Line 304 | ssl_handshake(int fd, struct Client *cli
304   void
305   add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd)
306   {
307 <  struct Client *new_client;
326 <
327 <  assert(NULL != listener);
328 <
329 <  new_client = make_client(NULL);
307 >  struct Client *new_client = make_client(NULL);
308  
309    fd_open(&new_client->localClient->fd, fd, 1,
310            (listener->flags & LISTENER_SSL) ?
311            "Incoming SSL connection" : "Incoming connection");
312  
313 <  /*
313 >  /*
314     * copy address to 'sockhost' as a string, copy it to host too
315     * so we have something valid to put into error messages...
316     */
317    memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr));
318  
319 <  irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip,
320 <        new_client->localClient->ip.ss_len,  new_client->sockhost,
321 <        HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
319 >  getnameinfo((struct sockaddr *)&new_client->localClient->ip,
320 >              new_client->localClient->ip.ss_len, new_client->sockhost,
321 >              sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST);
322    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);
323  
324 <  if (new_client->localClient->aftype == AF_INET6 &&
350 <      ConfigFileEntry.dot_in_ip6_addr == 1)
324 >  if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':')
325    {
326 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
327 <    strlcat(new_client->host, ".", HOSTLEN+1);
326 >    strlcpy(new_client->host, "0", sizeof(new_client->host));
327 >    strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1);
328 >    memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1);
329 >    new_client->sockhost[0] = '0';
330    }
331    else
332 < #endif
357 <    strlcat(new_client->host, new_client->sockhost,HOSTLEN+1);
332 >    strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
333  
334    new_client->localClient->listener = listener;
335    ++listener->ref_count;
# Line 362 | Line 337 | add_connection(struct Listener *listener
337   #ifdef HAVE_LIBCRYPTO
338    if (listener->flags & LISTENER_SSL)
339    {
340 <    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL)
340 >    if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL)
341      {
342        ilog(L_CRIT, "SSL_new() ERROR! -- %s",
343             ERR_error_string(ERR_get_error(), NULL));
# Line 507 | Line 482 | comm_connect_tcp(fde_t *fd, const char *
482                   void *data, int aftype, int timeout)
483   {
484    struct addrinfo hints, *res;
485 <  char portname[PORTNAMELEN+1];
485 >  char portname[PORTNAMELEN + 1];
486  
487    assert(callback);
488    fd->connect.callback = callback;
# Line 539 | Line 514 | comm_connect_tcp(fde_t *fd, const char *
514    hints.ai_socktype = SOCK_STREAM;
515    hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
516  
517 <  snprintf(portname, PORTNAMELEN, "%d", port);
517 >  snprintf(portname, sizeof(portname), "%d", port);
518  
519 <  if (irc_getaddrinfo(host, portname, &hints, &res))
519 >  if (getaddrinfo(host, portname, &hints, &res))
520    {
521      /* 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;
522      if (aftype == AF_INET6)
523 <      gethost_byname_type(host, fd->dns_query, T_AAAA);
523 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA);
524      else
525 <      gethost_byname_type(host, fd->dns_query, T_A);
525 >      gethost_byname_type(comm_connect_dns_callback, fd, host, T_A);
526    }
527    else
528    {
# Line 560 | Line 532 | comm_connect_tcp(fde_t *fd, const char *
532      memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen);
533      fd->connect.hostaddr.ss_len = res->ai_addrlen;
534      fd->connect.hostaddr.ss.ss_family = res->ai_family;
535 <    irc_freeaddrinfo(res);
535 >    freeaddrinfo(res);
536      comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL);
537      comm_connect_tryconnect(fd, NULL);
538    }
# Line 608 | Line 580 | comm_connect_timeout(fde_t *fd, void *no
580   * otherwise we initiate the connect()
581   */
582   static void
583 < comm_connect_dns_callback(void *vptr, struct DNSReply *reply)
583 > comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
584   {
585    fde_t *F = vptr;
586  
587 <  if (reply == NULL)
587 >  if (name == NULL)
588    {
617    MyFree(F->dns_query);
618    F->dns_query = NULL;
589      comm_connect_callback(F, COMM_ERR_DNS);
590      return;
591    }
# Line 629 | Line 599 | comm_connect_dns_callback(void *vptr, st
599     * the DNS record around, and the DNS cache is gone anyway..
600     *     -- adrian
601     */
602 <  memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len);
602 >  memcpy(&F->connect.hostaddr, addr, addr->ss_len);
603    /* The cast is hacky, but safe - port offset is same on v4 and v6 */
604    ((struct sockaddr_in *) &F->connect.hostaddr)->sin_port =
605      F->connect.hostaddr.ss_port;
606 <  F->connect.hostaddr.ss_len = reply->addr.ss_len;
606 >  F->connect.hostaddr.ss_len = addr->ss_len;
607  
608    /* Now, call the tryconnect() routine to try a connect() */
639  MyFree(F->dns_query);
640  F->dns_query = NULL;
609    comm_connect_tryconnect(F, NULL);
610   }
611  
# Line 665 | Line 633 | comm_connect_tryconnect(fde_t *fd, void
633    /* Error? */
634    if (retval < 0)
635    {
668 #ifdef _WIN32
669    errno = WSAGetLastError();
670 #endif
636      /*
637       * If we get EISCONN, then we've already connect()ed the socket,
638       * which is a good thing.
# Line 726 | Line 691 | comm_open(fde_t *F, int family, int sock
691     */
692    fd = socket(family, sock_type, proto);
693    if (fd < 0)
729  {
730 #ifdef _WIN32
731    errno = WSAGetLastError();
732 #endif
694      return -1; /* errno will be passed through, yay.. */
734  }
695  
696    execute_callback(setup_socket_cb, fd);
697  
# Line 764 | Line 724 | comm_accept(struct Listener *lptr, struc
724     * reserved fd limit, but we can deal with that when comm_open()
725     * also does it. XXX -- adrian
726     */
727 <  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen);
727 >  newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen);
728    if (newfd < 0)
769  {
770 #ifdef _WIN32
771    errno = WSAGetLastError();
772 #endif
729      return -1;
774  }
730  
731   #ifdef IPV6
732    remove_ipv6_mapping(pn);
# Line 787 | Line 742 | comm_accept(struct Listener *lptr, struc
742  
743   /*
744   * remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address
745 < * 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
745 > * OSes with IPv6 mapping listening on both
746   * AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures
747   *
748   */
# Line 798 | Line 752 | remove_ipv6_mapping(struct irc_ssaddr *a
752   {
753    if (addr->ss.ss_family == AF_INET6)
754    {
755 <    struct sockaddr_in6 *v6;
802 <
803 <    v6 = (struct sockaddr_in6*)addr;
804 <    if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))
755 >    if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
756      {
757 <      char v4ip[HOSTIPLEN];
758 <      struct sockaddr_in *v4 = (struct sockaddr_in*)addr;
759 <      inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN);
760 <      inet_pton(AF_INET, v4ip, &v4->sin_addr);
757 >      struct sockaddr_in6 v6;
758 >      struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
759 >
760 >      memcpy(&v6, addr, sizeof(v6));
761 >      memset(v4, 0, sizeof(struct sockaddr_in));
762 >      memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr));
763 >
764        addr->ss.ss_family = AF_INET;
765        addr->ss_len = sizeof(struct sockaddr_in);
766      }

Diff Legend

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