>From 24365637b73517a659e8d94305cf513bd27bfb12 Mon Sep 17 00:00:00 2001
From: Markus Klinik <markus.klinik () gmx de>
Date: Sat, 27 Feb 2010 12:18:31 +0100
Subject: [PATCH 7/8] ncat_connect: wrap proxy connection in fdinfo

Just an adaption to the socket_buffer interface change.

With some additional work it would be possible to SSL-encrypt the
connection to a proxy (I don't know of any other http clients that have
that feature). But as our http server supports SSL encryption now, we
could use it.

diff --git a/ncat_connect.c b/ncat_connect.c
index 047f986..823bfe8 100644
--- a/ncat_connect.c
+++ b/ncat_connect.c
@@ -329,11 +329,14 @@ static int do_proxy_http(void)
     char *status_line, *header;
     char *remainder;
     size_t len;
-    int sd, code;
+    int code;
+    struct fdinfo sfdi;
     int n;
 
-    sd = do_connect(SOCK_STREAM);
-    if (sd == -1) {
+    fdinfo_init(&sfdi);
+
+    sfdi.fd = do_connect(SOCK_STREAM);
+    if (sfdi.fd == -1) {
         loguser("Proxy connection failed: %s.\n", socket_strerror(socket_errno()));
         return -1;
     }
@@ -343,14 +346,14 @@ static int do_proxy_http(void)
 
     /* First try a request with no authentication. */
     request = http_connect_request(&httpconnect, &n);
-    if (send(sd, request, n, 0) < 0) {
+    if (ncat_send(&sfdi, request, n) < 0) {
         loguser("Error sending proxy request: %s.\n", socket_strerror(socket_errno()));
         free(request);
         return -1;
     }
     free(request);
 
-    socket_buffer_init(&sockbuf, sd);
+    socket_buffer_init(&sockbuf, &sfdi);
 
     if (http_read_status_line(&sockbuf, &status_line) != 0) {
         loguser("Error reading proxy response Status-Line.\n");
@@ -369,8 +372,8 @@ static int do_proxy_http(void)
         struct http_header *h;
         struct http_challenge challenge;
 
-        close(sd);
-        sd = -1;
+        fdinfo_close(&sfdi);
+        sfdi.fd = -1;
 
         if (http_parse_header(&h, header) != 0) {
             loguser("Error parsing proxy response header.\n");
@@ -387,8 +390,8 @@ static int do_proxy_http(void)
         }
         http_header_free(h);
 
-        sd = do_connect(SOCK_STREAM);
-        if (sd == -1) {
+        sfdi.fd = do_connect(SOCK_STREAM);
+        if (sfdi.fd == -1) {
             loguser("Proxy reconnection failed: %s.\n", socket_strerror(socket_errno()));
             goto bail;
         }
@@ -400,7 +403,7 @@ static int do_proxy_http(void)
             goto bail;
         }
         logdebug("Reconnection header:\n%s", request);
-        if (send(sd, request, n, 0) < 0) {
+        if (ncat_send(&sfdi, request, n) < 0) {
             loguser("Error sending proxy request: %s.\n", socket_strerror(socket_errno()));
             free(request);
             http_challenge_free(&challenge);
@@ -409,7 +412,7 @@ static int do_proxy_http(void)
         free(request);
         http_challenge_free(&challenge);
 
-        socket_buffer_init(&sockbuf, sd);
+        socket_buffer_init(&sockbuf, &sfdi);
 
         if (http_read_status_line(&sockbuf, &status_line) != 0) {
             loguser("Error reading proxy response Status-Line.\n");
@@ -436,11 +439,11 @@ static int do_proxy_http(void)
     remainder = socket_buffer_remainder(&sockbuf, &len);
     Write(STDOUT_FILENO, remainder, len);
 
-    return sd;
+    return sfdi.fd;
 
 bail:
-    if (sd != -1)
-        close(sd);
+    if (sfdi.fd != -1)
+        fdinfo_close(&sfdi);
     if (status_line != NULL)
         free(status_line);
     if (header != NULL)
@@ -529,6 +532,9 @@ int ncat_connect(void) {
         int len;
         char* line;
         size_t n;
+        struct fdinfo connect_fdi;
+
+        fdinfo_init(&connect_fdi);
 
         if (httpconnect.storage.ss_family != AF_UNSPEC) {
             connect_socket = do_proxy_http();
@@ -545,7 +551,8 @@ int ncat_connect(void) {
                 return 1;
             }
 
-            socket_buffer_init(&stateful_buf, connect_socket);
+            connect_fdi.fd = connect_socket;
+            socket_buffer_init(&stateful_buf, &connect_fdi);
 
             if (o.verbose) {
                 loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetss),
-- 
1.6.6.1

