Blame SOURCES/squid-5.0.6-active-ftp.patch

805743
diff --git a/src/clients/FtpClient.cc b/src/clients/FtpClient.cc
805743
index 747ed35..f2b7126 100644
805743
--- a/src/clients/FtpClient.cc
805743
+++ b/src/clients/FtpClient.cc
805743
@@ -795,7 +795,8 @@ Ftp::Client::connectDataChannel()
805743
 bool
805743
 Ftp::Client::openListenSocket()
805743
 {
805743
-    return false;
805743
+    debugs(9, 3, HERE);
805743
+	  return false;
805743
 }
805743
 
805743
 /// creates a data channel Comm close callback
805743
diff --git a/src/clients/FtpClient.h b/src/clients/FtpClient.h
805743
index eb5ea1b..e92c007 100644
805743
--- a/src/clients/FtpClient.h
805743
+++ b/src/clients/FtpClient.h
805743
@@ -137,7 +137,7 @@ public:
805743
     bool sendPort();
805743
     bool sendPassive();
805743
     void connectDataChannel();
805743
-    bool openListenSocket();
805743
+    virtual bool openListenSocket();
805743
     void switchTimeoutToDataChannel();
805743
 
805743
     CtrlChannel ctrl; ///< FTP control channel state
805743
diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc
805743
index 05db817..2989cd2 100644
805743
--- a/src/clients/FtpGateway.cc
805743
+++ b/src/clients/FtpGateway.cc
805743
@@ -86,6 +86,13 @@ struct GatewayFlags {
805743
 class Gateway;
805743
 typedef void (StateMethod)(Ftp::Gateway *);
805743
 
805743
+} // namespace FTP
805743
+
805743
+static void ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback);
805743
+
805743
+namespace Ftp
805743
+{
805743
+
805743
 /// FTP Gateway: An FTP client that takes an HTTP request with an ftp:// URI,
805743
 /// converts it into one or more FTP commands, and then
805743
 /// converts one or more FTP responses into the final HTTP response.
805743
@@ -136,7 +143,11 @@ public:
805743
 
805743
     /// create a data channel acceptor and start listening.
805743
     void listenForDataChannel(const Comm::ConnectionPointer &conn;;
805743
-
805743
+    virtual bool openListenSocket() {
805743
+    		debugs(9, 3, HERE);
805743
+				ftpOpenListenSocket(this, 0);
805743
+        return Comm::IsConnOpen(data.conn);
805743
+		}
805743
     int checkAuth(const HttpHeader * req_hdr);
805743
     void checkUrlpath();
805743
     void buildTitleUrl();
805743
@@ -1786,6 +1797,7 @@ ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback)
805743
     }
805743
 
805743
     ftpState->listenForDataChannel(temp);
805743
+    ftpState->data.listenConn = temp;
805743
 }
805743
 
805743
 static void
805743
@@ -1821,13 +1833,19 @@ ftpSendPORT(Ftp::Gateway * ftpState)
805743
     // pull out the internal IP address bytes to send in PORT command...
805743
     // source them from the listen_conn->local
805743
 
805743
+    struct sockaddr_in addr;
805743
+    socklen_t addrlen = sizeof(addr);
805743
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
805743
+    unsigned char port_high = ntohs(addr.sin_port) >> 8;
805743
+    unsigned char port_low  = ntohs(addr.sin_port) & 0xff;
805743
+
805743
     struct addrinfo *AI = NULL;
805743
     ftpState->data.listenConn->local.getAddrInfo(AI, AF_INET);
805743
     unsigned char *addrptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_addr;
805743
-    unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
805743
+    // unsigned char *portptr = (unsigned char *) &((struct sockaddr_in*)AI->ai_addr)->sin_port;
805743
     snprintf(cbuf, CTRL_BUFLEN, "PORT %d,%d,%d,%d,%d,%d\r\n",
805743
              addrptr[0], addrptr[1], addrptr[2], addrptr[3],
805743
-             portptr[0], portptr[1]);
805743
+             port_high, port_low);
805743
     ftpState->writeCommand(cbuf);
805743
     ftpState->state = Ftp::Client::SENT_PORT;
805743
 
805743
@@ -1880,14 +1898,27 @@ ftpSendEPRT(Ftp::Gateway * ftpState)
805743
         return;
805743
     }
805743
 
805743
+
805743
+    unsigned int port;
805743
+    struct sockaddr_storage addr;
805743
+    socklen_t addrlen = sizeof(addr);
805743
+    getsockname(ftpState->data.listenConn->fd, (struct sockaddr *) &addr, &addrlen);
805743
+    if (addr.ss_family == AF_INET) {
805743
+        struct sockaddr_in *addr4 = (struct sockaddr_in*) &addr;
805743
+        port = ntohs( addr4->sin_port );
805743
+    } else {
805743
+        struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr;
805743
+        port = ntohs( addr6->sin6_port );
805743
+    }
805743
+
805743
     char buf[MAX_IPSTRLEN];
805743
 
805743
     /* RFC 2428 defines EPRT as IPv6 equivalent to IPv4 PORT command. */
805743
     /* Which can be used by EITHER protocol. */
805743
-    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%d|\r\n",
805743
+    snprintf(cbuf, CTRL_BUFLEN, "EPRT |%d|%s|%u|\r\n",
805743
              ( ftpState->data.listenConn->local.isIPv6() ? 2 : 1 ),
805743
              ftpState->data.listenConn->local.toStr(buf,MAX_IPSTRLEN),
805743
-             ftpState->data.listenConn->local.port() );
805743
+             port);
805743
 
805743
     ftpState->writeCommand(cbuf);
805743
     ftpState->state = Ftp::Client::SENT_EPRT;
805743
@@ -1906,7 +1937,7 @@ ftpReadEPRT(Ftp::Gateway * ftpState)
805743
         ftpSendPORT(ftpState);
805743
         return;
805743
     }
805743
-
805743
+    ftpState->ctrl.message = NULL;
805743
     ftpRestOrList(ftpState);
805743
 }
805743