From b91d29a28e170c16d65d956db79f2cd3a82372d2 Mon Sep 17 00:00:00 2001
From: Gokhan Sengun <gokhansengun@gmail.com>
Date: Mon, 25 Jun 2012 01:46:01 +0300
Subject: [PATCH 1/2] ftp: active conn, allow application to set sockopt after
 accept() call

For active FTP connections, applications may need setting the sockopt after accept() call returns successful. This fix gives a call to the callback registered with CURL_SOCKOPTFUNCTION option. Also a new sock type - CURLSOCKTYPE_ACCEPT - is added. This type is to be passed to application callbacks with - purpose - parameter. Applications may use this parameter to distinguish between socket types.
---
 include/curl/curl.h |    5 +++--
 lib/ftp.c           |   14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/include/curl/curl.h b/include/curl/curl.h
index 2cad282..4031e0f 100644
--- a/include/curl/curl.h
+++ b/include/curl/curl.h
@@ -309,8 +309,9 @@ typedef size_t (*curl_read_callback)(char *buffer,
                                       void *instream);
 
 typedef enum  {
-  CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */
-  CURLSOCKTYPE_LAST   /* never use */
+  CURLSOCKTYPE_IPCXN,  /* socket created for a specific IP connection */
+  CURLSOCKTYPE_ACCEPT, /* socket created by accept() call */
+  CURLSOCKTYPE_LAST    /* never use */
 } curlsocktype;
 
 /* The return code from the sockopt_callback can signal information back
diff --git a/lib/ftp.c b/lib/ftp.c
index 3a49453..d0eee7c 100644
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -348,6 +348,20 @@ static CURLcode AcceptServerConnect(struct connectdata *conn)
   }
   infof(data, "Connection accepted from server\n");
 
+  if(data->set.fsockopt) {
+    int error = 0;
+
+    /* activate callback for setting socket options */
+    error = data->set.fsockopt(data->set.sockopt_client,
+                               s,
+                               CURLSOCKTYPE_ACCEPT);
+
+    if(error) {
+      Curl_closesocket(conn, s); /* close the socket and bail out */
+      return CURLE_ABORTED_BY_CALLBACK;
+    }
+  }
+
   conn->sock[SECONDARYSOCKET] = s;
   curlx_nonblock(s, TRUE); /* enable non-blocking */
   conn->sock_accepted[SECONDARYSOCKET] = TRUE;
-- 
1.7.9.5

