Attachment 'patch-PayloadTCPSocket.cpp.diff'

Download

   1 --- PayloadTCPSocket.cpp.orig	2008-02-13 15:34:06.000000000 +0100
   2 +++ PayloadTCPSocket.cpp	2008-02-13 18:44:18.000000000 +0100
   3 @@ -7,6 +7,7 @@
   4  typedef int socklen_t;
   5  #define ErrNo WSAGetLastError()
   6  #else // UNIX
   7 +#include <sys/types.h>
   8  #include <sys/socket.h>
   9  #include <netdb.h>
  10  #include <netinet/in.h>
  11 @@ -18,45 +19,43 @@
  12  namespace Arc {
  13  
  14  int PayloadTCPSocket::connect_socket(const char* hostname,int port) {
  15 -  struct hostent* host = NULL;
  16 -  struct hostent  hostbuf;
  17 -  int    errcode = 0;
  18 -#ifndef HAVE_GETHOSTBYNAME_R
  19 -  /* According to the developer manual the Darwin's version of gethostbyname
  20 -     is thread-safe */
  21 -  host = gethostbyname(hostname);
  22 -  if (host == NULL) {
  23 -#else
  24 -  #if defined(_AIX)
  25 -  struct hostent_data buf[BUFSIZ];
  26 -  if((errcode=gethostbyname_r(hostname,
  27 -                                  (host=&hostbuf),buf)) != 0) {
  28 -  #else
  29 -  char   buf[BUFSIZ];
  30 -  if((gethostbyname_r(hostname,&hostbuf,buf,sizeof(buf),
  31 -                                        &host,&errcode) != 0) ||
  32 -     (host == NULL)) {
  33 -  #endif
  34 -#endif
  35 -    logger.msg(WARNING, "Failed to resolve %s", hostname);
  36 -    return -1;
  37 -  };
  38 -  if( (host->h_length < (int)sizeof(struct in_addr)) ||
  39 -      (host->h_addr_list[0] == NULL) ) {
  40 -    logger.msg(WARNING, "Failed to resolve %s", hostname);
  41 +  struct addrinfo gaihints, *gaires, *gaires0;
  42 +  ssize_t l;
  43 +  int s;
  44 +  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  45 +  int    gaierror = 0;
  46 +
  47 +  memset(&gaihints, 0, sizeof(gaihints));
  48 +  gaihints.ai_socktype = SOCK_STREAM;
  49 +  snprintf(sbuf, NI_MAXSERV, "%d",port);
  50 +  gaierror = getaddrinfo(hostname, sbuf, &gaihints, &gaires0);
  51 +  if (gaierror) {
  52 +    logger.msg(WARNING, "Failed to resolve %s: %s", hostname, gai_strerror(gaierror));
  53      return -1;
  54 +  }
  55 +  /* try all the sockaddrs until connection goes successful */
  56 +  for (gaires = gaires0; gaires; gaires = gaires->ai_next) {
  57 +     gaierror = getnameinfo(gaires->ai_addr, gaires->ai_addrlen, hbuf,
  58 +                    sizeof(hbuf), sbuf, sizeof(sbuf),
  59 +                    NI_NUMERICHOST | NI_NUMERICSERV);
  60 +     if (gaierror) {
  61 +        logger.msg(WARNING, "Failed to getnameinfo() %s: %s", hostname, gai_strerror(gaierror));
  62 +                        continue;
  63 +      }
  64 +      logger.msg(INFO, "trying %s port %s\n", hbuf, sbuf);
  65 +
  66 +      s = socket(gaires->ai_family, gaires->ai_socktype, gaires->ai_protocol);
  67 +      if (s < 0)
  68 +         continue;
  69 +      if (connect(s, gaires->ai_addr, gaires->ai_addrlen) < 0) {
  70 +         close(s);
  71 +         s = -1;
  72 +         logger.msg(WARNING, "Failed to connect to %s:%i", hostname, port);
  73 +         continue;
  74 +      }
  75 +
  76    };
  77 -  struct sockaddr_in addr;
  78 -  memset(&addr,0,sizeof(addr));
  79 -  addr.sin_family=AF_INET;
  80 -  addr.sin_port=htons(port);
  81 -  memcpy(&addr.sin_addr,host->h_addr_list[0],sizeof(struct in_addr));
  82 -  int s = ::socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  83 -  if(s==-1) return -1;
  84 -  if(::connect(s,(struct sockaddr *)&addr,sizeof(addr))==-1) {
  85 -    logger.msg(WARNING, "Failed to connect to %s:%i", hostname, port);
  86 -    close(s); return -1;
  87 -  };
  88 +  freeaddrinfo(gaires0);
  89    return s;
  90  }
  91  

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-02-13 18:46:41, 3.2 KB) [[attachment:patch-MCCTCP.cpp.diff]]
  • [get | view] (2008-02-13 18:47:03, 3.0 KB) [[attachment:patch-PayloadTCPSocket.cpp.diff]]
  • [get | view] (2008-02-13 18:47:38, 1.4 KB) [[attachment:service.xml]]
  • [get | view] (2008-02-13 18:59:44, 9.1 KB) [[attachment:service_script.txt]]
  • [get | view] (2008-02-13 19:08:19, 3.3 KB) [[attachment:test_client_ipv4.cpp]]
  • [get | view] (2008-02-13 18:54:28, 3.3 KB) [[attachment:test_client_ipv6.cpp]]
  • [get | view] (2008-02-13 18:53:03, 25.6 KB) [[attachment:test_service_netstat.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.