Commit d6bf400e authored by Marc Hoersken's avatar Marc Hoersken
Browse files

socks.c: use Curl_printable_address in SOCKS5 connection sequence

Replace custom string formatting with Curl_printable_address.
Add additional debug and error output in case of failures.
parent 213c27e4
Loading
Loading
Loading
Loading
+17 −24
Original line number Diff line number Diff line
@@ -431,6 +431,8 @@ CURLcode Curl_SOCKS5(const char *proxy_name,

  (void)curlx_nonblock(sock, FALSE);

  infof(data, "SOCKS5 communication to %s:%d\n", hostname, remote_port);

  code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
                          &written);
  if(code || (written != (2 + (int)socksreq[1]))) {
@@ -596,33 +598,40 @@ CURLcode Curl_SOCKS5(const char *proxy_name,
    if(dns)
      hp=dns->addr;
    if(hp) {
      struct sockaddr_in *saddr_in;
#ifdef ENABLE_IPV6
      struct sockaddr_in6 *saddr_in6;
#endif
      int i;
      char buf[64];
      Curl_printable_address(hp, buf, sizeof(buf));

      if(hp->ai_family == AF_INET) {
        struct sockaddr_in *saddr_in;
        socksreq[len++] = 1; /* ATYP: IPv4 = 1 */

        saddr_in = (struct sockaddr_in*)(void*)hp->ai_addr;
        for(i = 0; i < 4; i++) {
          socksreq[len++] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[i];
        }

        infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)\n", buf);
      }
#ifdef ENABLE_IPV6
      else if(hp->ai_family == AF_INET6) {
        struct sockaddr_in6 *saddr_in6;
        socksreq[len++] = 4; /* ATYP: IPv6 = 4 */

        saddr_in6 = (struct sockaddr_in6*)(void*)hp->ai_addr;
        for(i = 0; i < 16; i++) {
          socksreq[len++] = ((unsigned char*)&saddr_in6->sin6_addr.s6_addr)[i];
        }

        infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)\n", buf);
      }
#endif
      else
      else {
        hp = NULL; /* fail! */

        failf(data, "SOCKS5 connection to %s not supported\n", buf);
      }

      Curl_resolv_unlock(data, dns); /* not used anymore from now on */
    }
    if(!hp) {
@@ -630,25 +639,6 @@ CURLcode Curl_SOCKS5(const char *proxy_name,
            hostname);
      return CURLE_COULDNT_RESOLVE_HOST;
    }
    else {
      if(socksreq[3] == 1) {
        infof(data, "SOCKS5 connect to %d.%d.%d.%d (locally resolved)\n",
              (unsigned char)socksreq[4], (unsigned char)socksreq[5],
              (unsigned char)socksreq[6], (unsigned char)socksreq[7]);
      }
      else if(socksreq[3] == 4) {
        infof(data, "SOCKS5 connect to %02x%02x:%02x%02x:%02x%02x:%02x%02x:"
                   "%02x%02x:%02x%02x:%02x%02x:%02x%02x (locally resolved)\n",
              (unsigned char)socksreq[4], (unsigned char)socksreq[5],
              (unsigned char)socksreq[6], (unsigned char)socksreq[7],
              (unsigned char)socksreq[8], (unsigned char)socksreq[9],
              (unsigned char)socksreq[10], (unsigned char)socksreq[11],
              (unsigned char)socksreq[12], (unsigned char)socksreq[13],
              (unsigned char)socksreq[14], (unsigned char)socksreq[15],
              (unsigned char)socksreq[16], (unsigned char)socksreq[17],
              (unsigned char)socksreq[18], (unsigned char)socksreq[19]);
      }
    }
  }

  socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
@@ -769,6 +759,9 @@ CURLcode Curl_SOCKS5(const char *proxy_name,
    }
    return CURLE_COULDNT_CONNECT;
  }
  else {
    infof(data, "SOCKS5 request granted.\n");
  }

  (void)curlx_nonblock(sock, TRUE);
  return CURLE_OK; /* Proxy was successful! */