]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
ftp: use easy handle and connectin meta data for protocol structs
authorStefan Eissing <stefan@eissing.org>
Mon, 5 May 2025 11:31:14 +0000 (13:31 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 6 May 2025 07:07:43 +0000 (09:07 +0200)
- remove data->req.p.ftp and store `struct FTP` as easy meta data
- place `struct ftp_conn` instance in connection meta data

Closes #17249

lib/ftp.c
lib/ftp.h
lib/krb5.c
lib/request.c
lib/request.h
lib/url.c
lib/urldata.h

index f7d756a2a97be6e1cf8584a4cb9bdc97c91079a7..ff57a57b3e6ad517fb824200c4b41e34014e1603 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -95,8 +95,7 @@
 
 #ifdef CURL_DISABLE_VERBOSE_STRINGS
 #define ftp_pasv_verbose(a,b,c,d)  Curl_nop_stmt
-#define FTP_CSTATE(c)   ""
-#define FTP_DSTATE(d)   ""
+#define FTP_CSTATE(c)   ((void)(c), "")
 #else /* CURL_DISABLE_VERBOSE_STRINGS */
   /* for tracing purposes */
 static const char * const ftp_state_names[]={
@@ -136,23 +135,19 @@ static const char * const ftp_state_names[]={
   "STOR",
   "QUIT"
 };
-#define FTP_CSTATE(c)   ((c)? ftp_state_names[(c)->proto.ftpc.state] : "???")
-#define FTP_DSTATE(d)   (((d) && (d)->conn)? \
-                         ftp_state_names[(d)->conn->proto.ftpc.state] : "???")
+#define FTP_CSTATE(ftpc)   ((ftpc)? ftp_state_names[(ftpc)->state] : "???")
 
 #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
 
 /* This is the ONLY way to change FTP state! */
 static void _ftp_state(struct Curl_easy *data,
+                       struct ftp_conn *ftpc,
                        ftpstate newstate
 #ifdef DEBUGBUILD
                        , int lineno
 #endif
   )
 {
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-
 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
 #ifdef DEBUGBUILD
   (void)lineno;
@@ -160,10 +155,10 @@ static void _ftp_state(struct Curl_easy *data,
 #else /* CURL_DISABLE_VERBOSE_STRINGS */
   if(ftpc->state != newstate)
 #ifdef DEBUGBUILD
-    CURL_TRC_FTP(data, "[%s] -> [%s] (line %d)", FTP_DSTATE(data),
+    CURL_TRC_FTP(data, "[%s] -> [%s] (line %d)", FTP_CSTATE(ftpc),
                  ftp_state_names[newstate], lineno);
 #else
-    CURL_TRC_FTP(data, "[%s] -> [%s]", FTP_DSTATE(data),
+    CURL_TRC_FTP(data, "[%s] -> [%s]", FTP_CSTATE(ftpc),
                  ftp_state_names[newstate]);
 #endif
 #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
@@ -174,32 +169,40 @@ static void _ftp_state(struct Curl_easy *data,
 
 /* Local API functions */
 #ifndef DEBUGBUILD
-#define ftp_state(x,y) _ftp_state(x,y)
+#define ftp_state(x,y,z) _ftp_state(x,y,z)
 #else /* !DEBUGBUILD */
-#define ftp_state(x,y) _ftp_state(x,y,__LINE__)
+#define ftp_state(x,y,z) _ftp_state(x,y,z,__LINE__)
 #endif /* DEBUGBUILD */
 
 static CURLcode ftp_sendquote(struct Curl_easy *data,
-                              struct connectdata *conn,
+                              struct ftp_conn *ftpc,
                               struct curl_slist *quote);
-static CURLcode ftp_quit(struct Curl_easy *data, struct connectdata *conn);
-static CURLcode ftp_parse_url_path(struct Curl_easy *data);
-static CURLcode ftp_regular_transfer(struct Curl_easy *data, bool *done);
+static CURLcode ftp_quit(struct Curl_easy *data, struct ftp_conn *ftpc);
+static CURLcode ftp_parse_url_path(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc,
+                                   struct FTP *ftp);
+static CURLcode ftp_regular_transfer(struct Curl_easy *data,
+                                     struct ftp_conn *ftpc,
+                                     struct FTP *ftp,
+                                     bool *done);
 #ifndef CURL_DISABLE_VERBOSE_STRINGS
 static void ftp_pasv_verbose(struct Curl_easy *data,
                              struct Curl_addrinfo *ai,
                              char *newhost, /* ASCII version */
                              int port);
 #endif
-static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data);
-static CURLcode ftp_state_mdtm(struct Curl_easy *data);
+static CURLcode ftp_state_mdtm(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp);
 static CURLcode ftp_state_quote(struct Curl_easy *data,
+                                struct ftp_conn *ftpc,
+                                struct FTP *ftp,
                                 bool init, ftpstate instate);
 static CURLcode ftp_nb_type(struct Curl_easy *data,
-                            struct connectdata *conn,
+                            struct ftp_conn *ftpc,
+                            struct FTP *ftp,
                             bool ascii, ftpstate newstate);
-static int ftp_need_type(struct connectdata *conn,
-                         bool ascii);
+static int ftp_need_type(struct ftp_conn *ftpc, bool ascii);
 static CURLcode ftp_do(struct Curl_easy *data, bool *done);
 static CURLcode ftp_done(struct Curl_easy *data,
                          CURLcode, bool premature);
@@ -216,16 +219,26 @@ static CURLcode ftp_doing(struct Curl_easy *data,
                           bool *dophase_done);
 static CURLcode ftp_setup_connection(struct Curl_easy *data,
                                      struct connectdata *conn);
-static CURLcode init_wc_data(struct Curl_easy *data);
-static CURLcode wc_statemach(struct Curl_easy *data);
+static CURLcode init_wc_data(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
+                             struct FTP *ftp);
+static CURLcode wc_statemach(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
+                             struct FTP *ftp);
 static void wc_data_dtor(void *ptr);
-static CURLcode ftp_state_retr(struct Curl_easy *data, curl_off_t filesize);
+static CURLcode ftp_state_retr(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp,
+                               curl_off_t filesize);
 static CURLcode ftp_readresp(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
                              int sockindex,
                              struct pingpong *pp,
                              int *ftpcode,
                              size_t *size);
 static CURLcode ftp_dophase_done(struct Curl_easy *data,
+                                 struct ftp_conn *ftpc,
+                                 struct FTP *ftp,
                                  bool connected);
 
 /*
@@ -292,9 +305,11 @@ const struct Curl_handler Curl_handler_ftps = {
 };
 #endif
 
-static void close_secondarysocket(struct Curl_easy *data)
+static void close_secondarysocket(struct Curl_easy *data,
+                                  struct ftp_conn *ftpc)
 {
-  CURL_TRC_FTP(data, "[%s] closing DATA connection", FTP_DSTATE(data));
+  (void)ftpc;
+  CURL_TRC_FTP(data, "[%s] closing DATA connection", FTP_CSTATE(ftpc));
   Curl_conn_close(data, SECONDARYSOCKET);
   Curl_conn_cf_discard_all(data, data->conn, SECONDARYSOCKET);
 }
@@ -347,9 +362,12 @@ static CURLcode ftp_cw_lc_write(struct Curl_easy *data,
 {
   static const char nl = '\n';
   struct ftp_cw_lc_ctx *ctx = writer->ctx;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
 
-  if(!(type & CLIENTWRITE_BODY) ||
-     data->conn->proto.ftpc.transfertype != 'A')
+  if(!ftpc)
+    return CURLE_FAILED_INIT;
+
+  if(!(type & CLIENTWRITE_BODY) || ftpc->transfertype != 'A')
     return Curl_cwriter_write(data, writer->next, type, buf, blen);
 
   /* ASCII mode BODY data, convert lineends */
@@ -423,11 +441,11 @@ static const struct Curl_cwtype ftp_cw_lc = {
  * ftp_check_ctrl_on_data_wait()
  *
  */
-static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data)
+static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data,
+                                            struct ftp_conn *ftpc)
 {
   struct connectdata *conn = data->conn;
   curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   struct pingpong *pp = &ftpc->pp;
   ssize_t nread;
   int ftpcode;
@@ -498,30 +516,30 @@ static CURLcode ftp_check_ctrl_on_data_wait(struct Curl_easy *data)
 
 /***********************************************************************
  *
- * InitiateTransfer()
+ * ftp_initiate_transfer()
  *
  * After connection from server is accepted this function is called to
  * setup transfer parameters and initiate the data transfer.
  *
  */
-static CURLcode InitiateTransfer(struct Curl_easy *data)
+static CURLcode ftp_initiate_transfer(struct Curl_easy *data,
+                                      struct ftp_conn *ftpc)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
   bool connected;
 
-  CURL_TRC_FTP(data, "InitiateTransfer()");
+  CURL_TRC_FTP(data, "ftp_initiate_transfer()");
   result = Curl_conn_connect(data, SECONDARYSOCKET, TRUE, &connected);
   if(result || !connected)
     return result;
 
-  if(conn->proto.ftpc.state_saved == FTP_STOR) {
+  if(ftpc->state_saved == FTP_STOR) {
     /* When we know we are uploading a specified file, we can get the file
        size prior to the actual upload. */
     Curl_pgrsSetUploadSize(data, data->state.infilesize);
 
     /* set the SO_SNDBUF for the secondary socket for those who need it */
-    Curl_sndbuf_init(conn->sock[SECONDARYSOCKET]);
+    Curl_sndbuf_init(data->conn->sock[SECONDARYSOCKET]);
 
     /* FTP upload, shutdown DATA, ignore shutdown errors, as we rely
      * on the server response on the CONTROL connection. */
@@ -530,11 +548,11 @@ static CURLcode InitiateTransfer(struct Curl_easy *data)
   else {
     /* FTP download, shutdown, do not ignore errors */
     Curl_xfer_setup2(data, CURL_XFER_RECV,
-                     conn->proto.ftpc.retr_size_saved, TRUE, FALSE);
+                     ftpc->retr_size_saved, TRUE, FALSE);
   }
 
-  conn->proto.ftpc.pp.pending_resp = TRUE; /* expect server response */
-  ftp_state(data, FTP_STOP);
+  ftpc->pp.pending_resp = TRUE; /* expect server response */
+  ftp_state(data, ftpc, FTP_STOP);
 
   return CURLE_OK;
 }
@@ -555,6 +573,7 @@ static bool ftp_endofresp(struct Curl_easy *data, struct connectdata *conn,
 }
 
 static CURLcode ftp_readresp(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
                              int sockindex,
                              struct pingpong *pp,
                              int *ftpcode, /* return the ftp-code if done */
@@ -566,7 +585,7 @@ static CURLcode ftp_readresp(struct Curl_easy *data,
 #ifdef HAVE_GSSAPI
   {
     struct connectdata *conn = data->conn;
-    char * const buf = Curl_dyn_ptr(&data->conn->proto.ftpc.pp.recvbuf);
+    char * const buf = Curl_dyn_ptr(&ftpc->pp.recvbuf);
 
     /* handle the security-oriented responses 6xx ***/
     switch(code) {
@@ -587,7 +606,7 @@ static CURLcode ftp_readresp(struct Curl_easy *data,
 #endif
 
   /* store the latest code for later retrieval, except during shutdown */
-  if(!data->conn->proto.ftpc.shutdown)
+  if(!ftpc->shutdown)
     data->info.httpcode = code;
 
   if(ftpcode)
@@ -602,7 +621,7 @@ static CURLcode ftp_readresp(struct Curl_easy *data,
      * generically is a good idea.
      */
     infof(data, "We got a 421 - timeout");
-    ftp_state(data, FTP_STOP);
+    ftp_state(data, ftpc, FTP_STOP);
     return CURLE_OPERATION_TIMEDOUT;
   }
 
@@ -631,21 +650,22 @@ CURLcode Curl_GetFTPResponse(struct Curl_easy *data,
   struct connectdata *conn = data->conn;
   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
   CURLcode result = CURLE_OK;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
   struct pingpong *pp = &ftpc->pp;
   size_t nread;
   int cache_skip = 0;
   int value_to_be_ignored = 0;
 
   CURL_TRC_FTP(data, "getFTPResponse start");
-
+  *nreadp = 0;
   if(ftpcode)
     *ftpcode = 0; /* 0 for errors */
   else
     /* make the pointer point to something for the rest of this function */
     ftpcode = &value_to_be_ignored;
 
-  *nreadp = 0;
+  if(!ftpc)
+    return CURLE_FAILED_INIT;
 
   while(!*ftpcode && !result) {
     /* check and reset timeout value every lap */
@@ -705,7 +725,7 @@ CURLcode Curl_GetFTPResponse(struct Curl_easy *data,
         break;
     }
 
-    result = ftp_readresp(data, FIRSTSOCKET, pp, ftpcode, &nread);
+    result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, ftpcode, &nread);
     if(result)
       break;
 
@@ -730,25 +750,24 @@ CURLcode Curl_GetFTPResponse(struct Curl_easy *data,
 }
 
 static CURLcode ftp_state_user(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
                                struct connectdata *conn)
 {
-  CURLcode result = Curl_pp_sendf(data,
-                                  &conn->proto.ftpc.pp, "USER %s",
+  CURLcode result = Curl_pp_sendf(data, &ftpc->pp, "USER %s",
                                   conn->user ? conn->user : "");
   if(!result) {
-    struct ftp_conn *ftpc = &conn->proto.ftpc;
     ftpc->ftp_trying_alternative = FALSE;
-    ftp_state(data, FTP_USER);
+    ftp_state(data, ftpc, FTP_USER);
   }
   return result;
 }
 
 static CURLcode ftp_state_pwd(struct Curl_easy *data,
-                              struct connectdata *conn)
+                              struct ftp_conn *ftpc)
 {
-  CURLcode result = Curl_pp_sendf(data, &conn->proto.ftpc.pp, "%s", "PWD");
+  CURLcode result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PWD");
   if(!result)
-    ftp_state(data, FTP_PWD);
+    ftp_state(data, ftpc, FTP_PWD);
 
   return result;
 }
@@ -758,21 +777,25 @@ static int ftp_getsock(struct Curl_easy *data,
                        struct connectdata *conn,
                        curl_socket_t *socks)
 {
-  return Curl_pp_getsock(data, &conn->proto.ftpc.pp, socks);
+  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
+  return ftpc ? Curl_pp_getsock(data, &ftpc->pp, socks) : GETSOCK_BLANK;
 }
 
 /* For the FTP "DO_MORE" phase only */
 static int ftp_domore_getsock(struct Curl_easy *data,
                               struct connectdata *conn, curl_socket_t *socks)
 {
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
   (void)data;
 
+  if(!ftpc)
+    return GETSOCK_BLANK;
+
   /* When in DO_MORE state, we could be either waiting for us to connect to a
    * remote site, or we could wait for that site to connect to us. Or just
    * handle ordinary commands.
    */
-  CURL_TRC_FTP(data, "[%s] ftp_domore_getsock()", FTP_DSTATE(data));
+  CURL_TRC_FTP(data, "[%s] ftp_domore_getsock()", FTP_CSTATE(ftpc));
 
   if(FTP_STOP == ftpc->state) {
     /* if stopped and still in this state, then we are also waiting for a
@@ -785,7 +808,7 @@ static int ftp_domore_getsock(struct Curl_easy *data,
      * via its adjust_pollset() */
     return GETSOCK_READSOCK(0);
   }
-  return Curl_pp_getsock(data, &conn->proto.ftpc.pp, socks);
+  return Curl_pp_getsock(data, &ftpc->pp, socks);
 }
 
 /* This is called after the FTP_QUOTE state is passed.
@@ -795,14 +818,14 @@ static int ftp_domore_getsock(struct Curl_easy *data,
    missing ones, if that option is enabled.
 */
 static CURLcode ftp_state_cwd(struct Curl_easy *data,
-                              struct connectdata *conn)
+                              struct ftp_conn *ftpc,
+                              struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   if(ftpc->cwddone)
     /* already done and fine */
-    result = ftp_state_mdtm(data);
+    result = ftp_state_mdtm(data, ftpc, ftp);
   else {
     /* FTPFILE_NOCWD with full path: expect ftpc->cwddone! */
     DEBUGASSERT((data->set.ftp_filemethod != FTPFILE_NOCWD) ||
@@ -810,7 +833,7 @@ static CURLcode ftp_state_cwd(struct Curl_easy *data,
 
     ftpc->count2 = 0; /* count2 counts failed CWDs */
 
-    if(conn->bits.reuse && ftpc->entrypath &&
+    if(data->conn->bits.reuse && ftpc->entrypath &&
        /* no need to go to entrypath when we have an absolute path */
        !(ftpc->dirdepth && ftpc->dirs[0][0] == '/')) {
       /* This is a reused connection. Since we change directory to where the
@@ -820,7 +843,7 @@ static CURLcode ftp_state_cwd(struct Curl_easy *data,
                              for all upcoming ones in the ftp->dirs[] array */
       result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s", ftpc->entrypath);
       if(!result)
-        ftp_state(data, FTP_CWD);
+        ftp_state(data, ftpc, FTP_CWD);
     }
     else {
       if(ftpc->dirdepth) {
@@ -830,11 +853,11 @@ static CURLcode ftp_state_cwd(struct Curl_easy *data,
         result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s",
                                ftpc->dirs[ftpc->cwdcount -1]);
         if(!result)
-          ftp_state(data, FTP_CWD);
+          ftp_state(data, ftpc, FTP_CWD);
       }
       else {
         /* No CWD necessary */
-        result = ftp_state_mdtm(data);
+        result = ftp_state_mdtm(data, ftpc, ftp);
       }
     }
   }
@@ -848,11 +871,11 @@ typedef enum {
 } ftpport;
 
 static CURLcode ftp_state_use_port(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc,
                                    ftpport fcmd) /* start with this */
 {
   CURLcode result = CURLE_FTP_PORT_FAILED;
   struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   curl_socket_t portsock = CURL_SOCKET_BAD;
   char myhost[MAX_IPADR_LEN + 1] = "";
 
@@ -1041,7 +1064,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
     goto out;
   }
   CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), opened socket",
-               FTP_DSTATE(data));
+               FTP_CSTATE(ftpc));
 
   /* step 3, bind to a suitable local address */
 
@@ -1103,7 +1126,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
     goto out;
   }
   CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), socket bound to port %d",
-               FTP_DSTATE(data), port);
+               FTP_CSTATE(ftpc), port);
 
   /* step 4, listen on the socket */
 
@@ -1113,7 +1136,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
     goto out;
   }
   CURL_TRC_FTP(data, "[%s] ftp_state_use_port(), listening on %d",
-               FTP_DSTATE(data), port);
+               FTP_CSTATE(ftpc), port);
 
   /* step 5, send the proper FTP command */
 
@@ -1200,7 +1223,7 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
 
   /* store which command was sent */
   ftpc->count1 = fcmd;
-  ftp_state(data, FTP_PORT);
+  ftp_state(data, ftpc, FTP_PORT);
 
   /* Replace any filter on SECONDARY with one listening on this socket */
   result = Curl_conn_tcp_listen_set(data, conn, SECONDARYSOCKET, &portsock);
@@ -1212,7 +1235,7 @@ out:
   if(dns_entry)
     Curl_resolv_unlink(data, &dns_entry);
   if(result) {
-    ftp_state(data, FTP_STOP);
+    ftp_state(data, ftpc, FTP_STOP);
   }
   else {
     /* successfully setup the list socket filter. Do we need more? */
@@ -1232,9 +1255,9 @@ out:
 }
 
 static CURLcode ftp_state_use_pasv(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc,
                                    struct connectdata *conn)
 {
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   CURLcode result = CURLE_OK;
   /*
     Here's the executive summary on what to do:
@@ -1265,7 +1288,7 @@ static CURLcode ftp_state_use_pasv(struct Curl_easy *data,
   result = Curl_pp_sendf(data, &ftpc->pp, "%s", mode[modeoff]);
   if(!result) {
     ftpc->count1 = modeoff;
-    ftp_state(data, FTP_PASV);
+    ftp_state(data, ftpc, FTP_PASV);
     infof(data, "Connect data stream passively");
   }
   return result;
@@ -1278,55 +1301,52 @@ static CURLcode ftp_state_use_pasv(struct Curl_easy *data,
  * request is made. Thus, if an actual transfer is to be made this is where we
  * take off for real.
  */
-static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data)
+static CURLcode ftp_state_prepare_transfer(struct Curl_easy *data,
+                                           struct ftp_conn *ftpc,
+                                           struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
   struct connectdata *conn = data->conn;
 
   if(ftp->transfer != PPTRANSFER_BODY) {
     /* does not transfer any data */
 
     /* still possibly do PRE QUOTE jobs */
-    ftp_state(data, FTP_RETR_PREQUOTE);
-    result = ftp_state_quote(data, TRUE, FTP_RETR_PREQUOTE);
+    ftp_state(data, ftpc, FTP_RETR_PREQUOTE);
+    result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
   }
   else if(data->set.ftp_use_port) {
     /* We have chosen to use the PORT (or similar) command */
-    result = ftp_state_use_port(data, EPRT);
+    result = ftp_state_use_port(data, ftpc, EPRT);
   }
   else {
     /* We have chosen (this is default) to use the PASV (or similar) command */
     if(data->set.ftp_use_pret) {
       /* The user has requested that we send a PRET command
          to prepare the server for the upcoming PASV */
-      struct ftp_conn *ftpc = &conn->proto.ftpc;
-      if(!conn->proto.ftpc.file)
+      if(!ftpc->file)
         result = Curl_pp_sendf(data, &ftpc->pp, "PRET %s",
                                data->set.str[STRING_CUSTOMREQUEST] ?
                                data->set.str[STRING_CUSTOMREQUEST] :
                                (data->state.list_only ? "NLST" : "LIST"));
       else if(data->state.upload)
-        result = Curl_pp_sendf(data, &ftpc->pp, "PRET STOR %s",
-                               conn->proto.ftpc.file);
+        result = Curl_pp_sendf(data, &ftpc->pp, "PRET STOR %s", ftpc->file);
       else
-        result = Curl_pp_sendf(data, &ftpc->pp, "PRET RETR %s",
-                               conn->proto.ftpc.file);
+        result = Curl_pp_sendf(data, &ftpc->pp, "PRET RETR %s", ftpc->file);
       if(!result)
-        ftp_state(data, FTP_PRET);
+        ftp_state(data, ftpc, FTP_PRET);
     }
     else
-      result = ftp_state_use_pasv(data, conn);
+      result = ftp_state_use_pasv(data, ftpc, conn);
   }
   return result;
 }
 
 static CURLcode ftp_state_rest(struct Curl_easy *data,
-                               struct connectdata *conn)
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   if((ftp->transfer != PPTRANSFER_BODY) && ftpc->file) {
     /* if a "head"-like request is being made (on a file) */
@@ -1335,20 +1355,19 @@ static CURLcode ftp_state_rest(struct Curl_easy *data,
        whether it supports range */
     result = Curl_pp_sendf(data, &ftpc->pp, "REST %d", 0);
     if(!result)
-      ftp_state(data, FTP_REST);
+      ftp_state(data, ftpc, FTP_REST);
   }
   else
-    result = ftp_state_prepare_transfer(data);
+    result = ftp_state_prepare_transfer(data, ftpc, ftp);
 
   return result;
 }
 
 static CURLcode ftp_state_size(struct Curl_easy *data,
-                               struct connectdata *conn)
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   if((ftp->transfer == PPTRANSFER_INFO) && ftpc->file) {
     /* if a "head"-like request is being made (on a file) */
@@ -1356,19 +1375,19 @@ static CURLcode ftp_state_size(struct Curl_easy *data,
     /* we know ftpc->file is a valid pointer to a filename */
     result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
     if(!result)
-      ftp_state(data, FTP_SIZE);
+      ftp_state(data, ftpc, FTP_SIZE);
   }
   else
-    result = ftp_state_rest(data, conn);
+    result = ftp_state_rest(data, ftpc, ftp);
 
   return result;
 }
 
-static CURLcode ftp_state_list(struct Curl_easy *data)
+static CURLcode ftp_state_list(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
 
   /* If this output is to be machine-parsed, the NLST command might be better
      to use, since the LIST command output is not specified or standard in any
@@ -1420,39 +1439,42 @@ static CURLcode ftp_state_list(struct Curl_easy *data)
   if(!cmd)
     return CURLE_OUT_OF_MEMORY;
 
-  result = Curl_pp_sendf(data, &conn->proto.ftpc.pp, "%s", cmd);
+  result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
   free(cmd);
 
   if(!result)
-    ftp_state(data, FTP_LIST);
+    ftp_state(data, ftpc, FTP_LIST);
 
   return result;
 }
 
-static CURLcode ftp_state_retr_prequote(struct Curl_easy *data)
+static CURLcode ftp_state_retr_prequote(struct Curl_easy *data,
+                                        struct ftp_conn *ftpc,
+                                        struct FTP *ftp)
 {
   /* We have sent the TYPE, now we must send the list of prequote strings */
-  return ftp_state_quote(data, TRUE, FTP_RETR_PREQUOTE);
+  return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_RETR_PREQUOTE);
 }
 
-static CURLcode ftp_state_stor_prequote(struct Curl_easy *data)
+static CURLcode ftp_state_stor_prequote(struct Curl_easy *data,
+                                        struct ftp_conn *ftpc,
+                                        struct FTP *ftp)
 {
   /* We have sent the TYPE, now we must send the list of prequote strings */
-  return ftp_state_quote(data, TRUE, FTP_STOR_PREQUOTE);
+  return ftp_state_quote(data, ftpc, ftp, TRUE, FTP_STOR_PREQUOTE);
 }
 
-static CURLcode ftp_state_type(struct Curl_easy *data)
+static CURLcode ftp_state_type(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   /* If we have selected NOBODY and HEADER, it means that we only want file
      information. Which in FTP cannot be much more than the file size and
      date. */
   if(data->req.no_body && ftpc->file &&
-     ftp_need_type(conn, data->state.prefer_ascii)) {
+     ftp_need_type(ftpc, data->state.prefer_ascii)) {
     /* The SIZE command is _not_ RFC 959 specified, and therefore many servers
        may not support it! It is however the only way we have to get a file's
        size! */
@@ -1462,23 +1484,23 @@ static CURLcode ftp_state_type(struct Curl_easy *data)
 
     /* Some servers return different sizes for different modes, and thus we
        must set the proper type before we check the size */
-    result = ftp_nb_type(data, conn, data->state.prefer_ascii, FTP_TYPE);
+    result = ftp_nb_type(data, ftpc, ftp, data->state.prefer_ascii, FTP_TYPE);
     if(result)
       return result;
   }
   else
-    result = ftp_state_size(data, conn);
+    result = ftp_state_size(data, ftpc, ftp);
 
   return result;
 }
 
 /* This is called after the CWD commands have been done in the beginning of
    the DO phase */
-static CURLcode ftp_state_mdtm(struct Curl_easy *data)
+static CURLcode ftp_state_mdtm(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   /* Requested time of file or time-depended transfer? */
   if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
@@ -1488,10 +1510,10 @@ static CURLcode ftp_state_mdtm(struct Curl_easy *data)
     result = Curl_pp_sendf(data, &ftpc->pp, "MDTM %s", ftpc->file);
 
     if(!result)
-      ftp_state(data, FTP_MDTM);
+      ftp_state(data, ftpc, FTP_MDTM);
   }
   else
-    result = ftp_state_type(data);
+    result = ftp_state_type(data, ftpc, ftp);
 
   return result;
 }
@@ -1499,12 +1521,11 @@ static CURLcode ftp_state_mdtm(struct Curl_easy *data)
 
 /* This is called after the TYPE and possible quote commands have been sent */
 static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc,
+                                   struct FTP *ftp,
                                    bool sizechecked)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
-  struct FTP *ftp = data->req.p.ftp;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   bool append = data->set.remote_append;
 
   if((data->state.resume_from && !sizechecked) ||
@@ -1527,7 +1548,7 @@ static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
       /* Got no given size to start from, figure it out */
       result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
       if(!result)
-        ftp_state(data, FTP_STOR_SIZE);
+        ftp_state(data, ftpc, FTP_STOR_SIZE);
       return result;
     }
 
@@ -1583,7 +1604,7 @@ static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
          * ftp_done() because we did not transfer anything! */
         ftp->transfer = PPTRANSFER_NONE;
 
-        ftp_state(data, FTP_STOP);
+        ftp_state(data, ftpc, FTP_STOP);
         return CURLE_OK;
       }
     }
@@ -1593,19 +1614,18 @@ static CURLcode ftp_state_ul_setup(struct Curl_easy *data,
   result = Curl_pp_sendf(data, &ftpc->pp, append ? "APPE %s" : "STOR %s",
                          ftpc->file);
   if(!result)
-    ftp_state(data, FTP_STOR);
+    ftp_state(data, ftpc, FTP_STOR);
 
   return result;
 }
 
 static CURLcode ftp_state_quote(struct Curl_easy *data,
+                                struct ftp_conn *ftpc,
+                                struct FTP *ftp,
                                 bool init,
                                 ftpstate instate)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   bool quote = FALSE;
   struct curl_slist *item;
 
@@ -1654,7 +1674,7 @@ static CURLcode ftp_state_quote(struct Curl_easy *data,
       result = Curl_pp_sendf(data, &ftpc->pp, "%s", cmd);
       if(result)
         return result;
-      ftp_state(data, instate);
+      ftp_state(data, ftpc, instate);
       quote = TRUE;
     }
   }
@@ -1664,15 +1684,15 @@ static CURLcode ftp_state_quote(struct Curl_easy *data,
     switch(instate) {
     case FTP_QUOTE:
     default:
-      result = ftp_state_cwd(data, conn);
+      result = ftp_state_cwd(data, ftpc, ftp);
       break;
     case FTP_RETR_PREQUOTE:
       if(ftp->transfer != PPTRANSFER_BODY)
-        ftp_state(data, FTP_STOP);
+        ftp_state(data, ftpc, FTP_STOP);
       else {
         if(ftpc->known_filesize != -1) {
           Curl_pgrsSetDownloadSize(data, ftpc->known_filesize);
-          result = ftp_state_retr(data, ftpc->known_filesize);
+          result = ftp_state_retr(data, ftpc, ftp, ftpc->known_filesize);
         }
         else {
           if(data->set.ignorecl || data->state.prefer_ascii) {
@@ -1690,18 +1710,18 @@ static CURLcode ftp_state_quote(struct Curl_easy *data,
             */
             result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
             if(!result)
-              ftp_state(data, FTP_RETR);
+              ftp_state(data, ftpc, FTP_RETR);
           }
           else {
             result = Curl_pp_sendf(data, &ftpc->pp, "SIZE %s", ftpc->file);
             if(!result)
-              ftp_state(data, FTP_RETR_SIZE);
+              ftp_state(data, ftpc, FTP_RETR_SIZE);
           }
         }
       }
       break;
     case FTP_STOR_PREQUOTE:
-      result = ftp_state_ul_setup(data, FALSE);
+      result = ftp_state_ul_setup(data, ftpc, ftp, FALSE);
       break;
     case FTP_POSTQUOTE:
       break;
@@ -1714,6 +1734,7 @@ static CURLcode ftp_state_quote(struct Curl_easy *data,
 /* called from ftp_state_pasv_resp to switch to PASV in case of EPSV
    problems */
 static CURLcode ftp_epsv_disable(struct Curl_easy *data,
+                                 struct ftp_conn *ftpc,
                                  struct connectdata *conn)
 {
   CURLcode result = CURLE_OK;
@@ -1735,11 +1756,11 @@ static CURLcode ftp_epsv_disable(struct Curl_easy *data,
   Curl_conn_cf_discard_all(data, conn, SECONDARYSOCKET);
   data->state.errorbuf = FALSE; /* allow error message to get
                                          rewritten */
-  result = Curl_pp_sendf(data, &conn->proto.ftpc.pp, "%s", "PASV");
+  result = Curl_pp_sendf(data, &ftpc->pp, "%s", "PASV");
   if(!result) {
-    conn->proto.ftpc.count1++;
+    ftpc->count1++;
     /* remain in/go to the FTP_PASV state */
-    ftp_state(data, FTP_PASV);
+    ftp_state(data, ftpc, FTP_PASV);
   }
   return result;
 }
@@ -1777,10 +1798,10 @@ static bool match_pasv_6nums(const char *p,
 }
 
 static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
                                     int ftpcode)
 {
   struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   CURLcode result;
   struct Curl_dns_entry *dns = NULL;
   unsigned short connectport; /* the local port connect() should use! */
@@ -1864,7 +1885,7 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
   }
   else if(ftpc->count1 == 0) {
     /* EPSV failed, move on to PASV */
-    return ftp_epsv_disable(data, conn);
+    return ftp_epsv_disable(data, ftpc, conn);
   }
   else {
     failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
@@ -1922,7 +1943,7 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
   if(result) {
     Curl_resolv_unlink(data, &dns); /* we are done using this dns entry */
     if(ftpc->count1 == 0 && ftpcode == 229)
-      return ftp_epsv_disable(data, conn);
+      return ftp_epsv_disable(data, ftpc, conn);
 
     return result;
   }
@@ -1947,16 +1968,17 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
     return CURLE_OUT_OF_MEMORY;
 
   conn->bits.do_more = TRUE;
-  ftp_state(data, FTP_STOP); /* this phase is completed */
+  ftp_state(data, ftpc, FTP_STOP); /* this phase is completed */
 
   return result;
 }
 
 static CURLcode ftp_state_port_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
+                                    struct FTP *ftp,
                                     int ftpcode)
 {
   struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   ftpport fcmd = (ftpport)ftpc->count1;
   CURLcode result = CURLE_OK;
 
@@ -1977,12 +1999,12 @@ static CURLcode ftp_state_port_resp(struct Curl_easy *data,
     }
     else
       /* try next */
-      result = ftp_state_use_port(data, fcmd);
+      result = ftp_state_use_port(data, ftpc, fcmd);
   }
   else {
     infof(data, "Connect data stream actively");
-    ftp_state(data, FTP_STOP); /* end of DO phase */
-    result = ftp_dophase_done(data, FALSE);
+    ftp_state(data, ftpc, FTP_STOP); /* end of DO phase */
+    result = ftp_dophase_done(data, ftpc, ftp, FALSE);
   }
 
   return result;
@@ -2038,12 +2060,11 @@ static CURLcode client_write_header(struct Curl_easy *data,
 }
 
 static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
+                                    struct FTP *ftp,
                                     int ftpcode)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   switch(ftpcode) {
   case 213:
@@ -2128,7 +2149,7 @@ static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
           infof(data, "The requested document is not new enough");
           ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
           data->info.timecond = TRUE;
-          ftp_state(data, FTP_STOP);
+          ftp_state(data, ftpc, FTP_STOP);
           return CURLE_OK;
         }
         break;
@@ -2137,7 +2158,7 @@ static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
           infof(data, "The requested document is not old enough");
           ftp->transfer = PPTRANSFER_NONE; /* mark to not transfer data */
           data->info.timecond = TRUE;
-          ftp_state(data, FTP_STOP);
+          ftp_state(data, ftpc, FTP_STOP);
           return CURLE_OK;
         }
         break;
@@ -2149,17 +2170,18 @@ static CURLcode ftp_state_mdtm_resp(struct Curl_easy *data,
   }
 
   if(!result)
-    result = ftp_state_type(data);
+    result = ftp_state_type(data, ftpc, ftp);
 
   return result;
 }
 
 static CURLcode ftp_state_type_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
+                                    struct FTP *ftp,
                                     int ftpcode,
                                     ftpstate instate)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
 
   if(ftpcode/100 != 2) {
     /* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a
@@ -2173,26 +2195,25 @@ static CURLcode ftp_state_type_resp(struct Curl_easy *data,
           ftpcode);
 
   if(instate == FTP_TYPE)
-    result = ftp_state_size(data, conn);
+    result = ftp_state_size(data, ftpc, ftp);
   else if(instate == FTP_LIST_TYPE)
-    result = ftp_state_list(data);
+    result = ftp_state_list(data, ftpc, ftp);
   else if(instate == FTP_RETR_TYPE)
-    result = ftp_state_retr_prequote(data);
+    result = ftp_state_retr_prequote(data, ftpc, ftp);
   else if(instate == FTP_STOR_TYPE)
-    result = ftp_state_stor_prequote(data);
+    result = ftp_state_stor_prequote(data, ftpc, ftp);
 
   return result;
 }
 
 static CURLcode ftp_state_retr(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               struct FTP *ftp,
                                curl_off_t filesize)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
-  CURL_TRC_FTP(data, "[%s] ftp_state_retr()", FTP_DSTATE(data));
+  CURL_TRC_FTP(data, "[%s] ftp_state_retr()", FTP_CSTATE(ftpc));
   if(data->set.max_filesize && (filesize > data->set.max_filesize)) {
     failf(data, "Maximum file size exceeded");
     return CURLE_FILESIZE_EXCEEDED;
@@ -2245,7 +2266,7 @@ static CURLcode ftp_state_retr(struct Curl_easy *data,
       /* Set ->transfer so that we will not get any error in ftp_done()
        * because we did not transfer the any file */
       ftp->transfer = PPTRANSFER_NONE;
-      ftp_state(data, FTP_STOP);
+      ftp_state(data, ftpc, FTP_STOP);
       return CURLE_OK;
     }
 
@@ -2256,26 +2277,28 @@ static CURLcode ftp_state_retr(struct Curl_easy *data,
     result = Curl_pp_sendf(data, &ftpc->pp, "REST %" FMT_OFF_T,
                            data->state.resume_from);
     if(!result)
-      ftp_state(data, FTP_RETR_REST);
+      ftp_state(data, ftpc, FTP_RETR_REST);
   }
   else {
     /* no resume */
     result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
     if(!result)
-      ftp_state(data, FTP_RETR);
+      ftp_state(data, ftpc, FTP_RETR);
   }
 
   return result;
 }
 
 static CURLcode ftp_state_size_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
+                                    struct FTP *ftp,
                                     int ftpcode,
                                     ftpstate instate)
 {
   CURLcode result = CURLE_OK;
   curl_off_t filesize = -1;
-  char *buf = Curl_dyn_ptr(&data->conn->proto.ftpc.pp.recvbuf);
-  size_t len = data->conn->proto.ftpc.pp.nfinal;
+  char *buf = Curl_dyn_ptr(&ftpc->pp.recvbuf);
+  size_t len = ftpc->pp.nfinal;
 
   /* get the size from the ascii string: */
   if(ftpcode == 213) {
@@ -2317,27 +2340,27 @@ static CURLcode ftp_state_size_resp(struct Curl_easy *data,
     }
 #endif
     Curl_pgrsSetDownloadSize(data, filesize);
-    result = ftp_state_rest(data, data->conn);
+    result = ftp_state_rest(data, ftpc, ftp);
   }
   else if(instate == FTP_RETR_SIZE) {
     Curl_pgrsSetDownloadSize(data, filesize);
-    result = ftp_state_retr(data, filesize);
+    result = ftp_state_retr(data, ftpc, ftp, filesize);
   }
   else if(instate == FTP_STOR_SIZE) {
     data->state.resume_from = filesize;
-    result = ftp_state_ul_setup(data, TRUE);
+    result = ftp_state_ul_setup(data, ftpc, ftp, TRUE);
   }
 
   return result;
 }
 
 static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
-                                    struct connectdata *conn,
+                                    struct ftp_conn *ftpc,
+                                    struct FTP *ftp,
                                     int ftpcode,
                                     ftpstate instate)
 {
   CURLcode result = CURLE_OK;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   switch(instate) {
   case FTP_REST:
@@ -2350,7 +2373,7 @@ static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
         return result;
     }
 #endif
-    result = ftp_state_prepare_transfer(data);
+    result = ftp_state_prepare_transfer(data, ftpc, ftp);
     break;
 
   case FTP_RETR_REST:
@@ -2361,7 +2384,7 @@ static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
     else {
       result = Curl_pp_sendf(data, &ftpc->pp, "RETR %s", ftpc->file);
       if(!result)
-        ftp_state(data, FTP_RETR);
+        ftp_state(data, ftpc, FTP_RETR);
     }
     break;
   }
@@ -2370,26 +2393,25 @@ static CURLcode ftp_state_rest_resp(struct Curl_easy *data,
 }
 
 static CURLcode ftp_state_stor_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
                                     int ftpcode, ftpstate instate)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
 
   if(ftpcode >= 400) {
     failf(data, "Failed FTP upload: %0d", ftpcode);
-    ftp_state(data, FTP_STOP);
+    ftp_state(data, ftpc, FTP_STOP);
     /* oops, we never close the sockets! */
     return CURLE_UPLOAD_FAILED;
   }
 
-  conn->proto.ftpc.state_saved = instate;
+  ftpc->state_saved = instate;
 
   /* PORT means we are now awaiting the server to connect to us. */
   if(data->set.ftp_use_port) {
-    struct ftp_conn *ftpc = &conn->proto.ftpc;
     bool connected;
 
-    ftp_state(data, FTP_STOP); /* no longer in STOR state */
+    ftp_state(data, ftpc, FTP_STOP); /* no longer in STOR state */
 
     result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
     if(result)
@@ -2398,21 +2420,21 @@ static CURLcode ftp_state_stor_resp(struct Curl_easy *data,
     if(!connected) {
       infof(data, "Data conn was not available immediately");
       ftpc->wait_data_conn = TRUE;
-      return ftp_check_ctrl_on_data_wait(data);
+      return ftp_check_ctrl_on_data_wait(data, ftpc);
     }
     ftpc->wait_data_conn = FALSE;
   }
-  return InitiateTransfer(data);
+  return ftp_initiate_transfer(data, ftpc);
 }
 
 /* for LIST and RETR responses */
 static CURLcode ftp_state_get_resp(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc,
+                                   struct FTP *ftp,
                                    int ftpcode,
                                    ftpstate instate)
 {
   CURLcode result = CURLE_OK;
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
 
   if((ftpcode == 150) || (ftpcode == 125)) {
 
@@ -2457,7 +2479,7 @@ static CURLcode ftp_state_get_resp(struct Curl_easy *data,
        *
        * Example D above makes this parsing a little tricky */
       const char *bytes;
-      char *buf = Curl_dyn_ptr(&conn->proto.ftpc.pp.recvbuf);
+      char *buf = Curl_dyn_ptr(&ftpc->pp.recvbuf);
       bytes = strstr(buf, " bytes");
       if(bytes) {
         long in = (long)(--bytes-buf);
@@ -2497,11 +2519,10 @@ static CURLcode ftp_state_get_resp(struct Curl_easy *data,
       infof(data, "Getting file with size: %" FMT_OFF_T, size);
 
     /* FTP download: */
-    conn->proto.ftpc.state_saved = instate;
-    conn->proto.ftpc.retr_size_saved = size;
+    ftpc->state_saved = instate;
+    ftpc->retr_size_saved = size;
 
     if(data->set.ftp_use_port) {
-      struct ftp_conn *ftpc = &conn->proto.ftpc;
       bool connected;
 
       result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &connected);
@@ -2510,19 +2531,19 @@ static CURLcode ftp_state_get_resp(struct Curl_easy *data,
 
       if(!connected) {
         infof(data, "Data conn was not available immediately");
-        ftp_state(data, FTP_STOP);
+        ftp_state(data, ftpc, FTP_STOP);
         ftpc->wait_data_conn = TRUE;
-        return ftp_check_ctrl_on_data_wait(data);
+        return ftp_check_ctrl_on_data_wait(data, ftpc);
       }
       ftpc->wait_data_conn = FALSE;
     }
-    return InitiateTransfer(data);
+    return ftp_initiate_transfer(data, ftpc);
   }
   else {
     if((instate == FTP_LIST) && (ftpcode == 450)) {
       /* simply no matching files in the dir listing */
       ftp->transfer = PPTRANSFER_NONE; /* do not download anything */
-      ftp_state(data, FTP_STOP); /* this phase is over */
+      ftp_state(data, ftpc, FTP_STOP); /* this phase is over */
     }
     else {
       failf(data, "RETR response: %03d", ftpcode);
@@ -2536,12 +2557,12 @@ static CURLcode ftp_state_get_resp(struct Curl_easy *data,
 }
 
 /* after USER, PASS and ACCT */
-static CURLcode ftp_state_loggedin(struct Curl_easy *data)
+static CURLcode ftp_state_loggedin(struct Curl_easy *data,
+                                   struct ftp_conn *ftpc)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
 
-  if(conn->bits.ftp_use_control_ssl) {
+  if(data->conn->bits.ftp_use_control_ssl) {
     /* PBSZ = PROTECTION BUFFER SIZE.
 
     The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
@@ -2556,44 +2577,43 @@ static CURLcode ftp_state_loggedin(struct Curl_easy *data)
     parameter of '0' to indicate that no buffering is taking place
     and the data connection should not be encapsulated.
     */
-    result = Curl_pp_sendf(data, &conn->proto.ftpc.pp, "PBSZ %d", 0);
+    result = Curl_pp_sendf(data, &ftpc->pp, "PBSZ %d", 0);
     if(!result)
-      ftp_state(data, FTP_PBSZ);
+      ftp_state(data, ftpc, FTP_PBSZ);
   }
   else {
-    result = ftp_state_pwd(data, conn);
+    result = ftp_state_pwd(data, ftpc);
   }
   return result;
 }
 
 /* for USER and PASS responses */
 static CURLcode ftp_state_user_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
                                     int ftpcode)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
 
   /* some need password anyway, and others just return 2xx ignored */
   if((ftpcode == 331) && (ftpc->state == FTP_USER)) {
     /* 331 Password required for ...
        (the server requires to send the user's password too) */
     result = Curl_pp_sendf(data, &ftpc->pp, "PASS %s",
-                           conn->passwd ? conn->passwd : "");
+                           data->conn->passwd ? data->conn->passwd : "");
     if(!result)
-      ftp_state(data, FTP_PASS);
+      ftp_state(data, ftpc, FTP_PASS);
   }
   else if(ftpcode/100 == 2) {
     /* 230 User ... logged in.
        (the user logged in with or without password) */
-    result = ftp_state_loggedin(data);
+    result = ftp_state_loggedin(data, ftpc);
   }
   else if(ftpcode == 332) {
     if(data->set.str[STRING_FTP_ACCOUNT]) {
       result = Curl_pp_sendf(data, &ftpc->pp, "ACCT %s",
                              data->set.str[STRING_FTP_ACCOUNT]);
       if(!result)
-        ftp_state(data, FTP_ACCT);
+        ftp_state(data, ftpc, FTP_ACCT);
     }
     else {
       failf(data, "ACCT requested but none available");
@@ -2614,7 +2634,7 @@ static CURLcode ftp_state_user_resp(struct Curl_easy *data,
                       data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
       if(!result) {
         ftpc->ftp_trying_alternative = TRUE;
-        ftp_state(data, FTP_USER);
+        ftp_state(data, ftpc, FTP_USER);
       }
     }
     else {
@@ -2627,6 +2647,7 @@ static CURLcode ftp_state_user_resp(struct Curl_easy *data,
 
 /* for ACCT response */
 static CURLcode ftp_state_acct_resp(struct Curl_easy *data,
+                                    struct ftp_conn *ftpc,
                                     int ftpcode)
 {
   CURLcode result = CURLE_OK;
@@ -2635,26 +2656,30 @@ static CURLcode ftp_state_acct_resp(struct Curl_easy *data,
     result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */
   }
   else
-    result = ftp_state_loggedin(data);
+    result = ftp_state_loggedin(data, ftpc);
 
   return result;
 }
 
 
-static CURLcode ftp_statemachine(struct Curl_easy *data,
-                                 struct connectdata *conn)
+static CURLcode ftp_pp_statemachine(struct Curl_easy *data,
+                                    struct connectdata *conn)
 {
   CURLcode result;
   int ftpcode;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-  struct pingpong *pp = &ftpc->pp;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
+  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
+  struct pingpong *pp;
   static const char * const ftpauth[] = { "SSL", "TLS" };
   size_t nread = 0;
 
+  if(!ftpc || !ftp)
+    return CURLE_FAILED_INIT;
+  pp = &ftpc->pp;
   if(pp->sendleft)
     return Curl_pp_flushsend(data, pp);
 
-  result = ftp_readresp(data, FIRSTSOCKET, pp, &ftpcode, &nread);
+  result = ftp_readresp(data, ftpc, FIRSTSOCKET, pp, &ftpcode, &nread);
   if(result)
     return result;
 
@@ -2666,7 +2691,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         /* 230 User logged in - already! Take as 220 if TLS required. */
         if(data->set.use_ssl <= CURLUSESSL_TRY ||
            conn->bits.ftp_use_control_ssl)
-          return ftp_state_user_resp(data, ftpcode);
+          return ftp_state_user_resp(data, ftpc, ftpcode);
       }
       else if(ftpcode != 220) {
         failf(data, "Got a %03d ftp-server response when 220 was expected",
@@ -2716,10 +2741,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         result = Curl_pp_sendf(data, &ftpc->pp, "AUTH %s",
                                ftpauth[ftpc->count1]);
         if(!result)
-          ftp_state(data, FTP_AUTH);
+          ftp_state(data, ftpc, FTP_AUTH);
       }
       else
-        result = ftp_state_user(data, conn);
+        result = ftp_state_user(data, ftpc, conn);
       break;
 
     case FTP_AUTH:
@@ -2749,7 +2774,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         if(!result) {
           conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */
           conn->bits.ftp_use_control_ssl = TRUE; /* SSL on control */
-          result = ftp_state_user(data, conn);
+          result = ftp_state_user(data, ftpc, conn);
         }
       }
       else if(ftpc->count3 < 1) {
@@ -2765,17 +2790,17 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
           result = CURLE_USE_SSL_FAILED;
         else
           /* ignore the failure and continue */
-          result = ftp_state_user(data, conn);
+          result = ftp_state_user(data, ftpc, conn);
       }
       break;
 
     case FTP_USER:
     case FTP_PASS:
-      result = ftp_state_user_resp(data, ftpcode);
+      result = ftp_state_user_resp(data, ftpc, ftpcode);
       break;
 
     case FTP_ACCT:
-      result = ftp_state_acct_resp(data, ftpcode);
+      result = ftp_state_acct_resp(data, ftpc, ftpcode);
       break;
 
     case FTP_PBSZ:
@@ -2783,7 +2808,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         Curl_pp_sendf(data, &ftpc->pp, "PROT %c",
                       data->set.use_ssl == CURLUSESSL_CONTROL ? 'C' : 'P');
       if(!result)
-        ftp_state(data, FTP_PROT);
+        ftp_state(data, ftpc, FTP_PROT);
       break;
 
     case FTP_PROT:
@@ -2802,10 +2827,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
          */
         result = Curl_pp_sendf(data, &ftpc->pp, "%s", "CCC");
         if(!result)
-          ftp_state(data, FTP_CCC);
+          ftp_state(data, ftpc, FTP_CCC);
       }
       else
-        result = ftp_state_pwd(data, conn);
+        result = ftp_state_pwd(data, ftpc);
       break;
 
     case FTP_CCC:
@@ -2824,7 +2849,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
       }
       if(!result)
         /* Then continue as normal */
-        result = ftp_state_pwd(data, conn);
+        result = ftp_state_pwd(data, ftpc);
       break;
 
     case FTP_PWD:
@@ -2898,7 +2923,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
             data->state.most_recent_ftp_entrypath = strdup(ftpc->entrypath);
             if(!data->state.most_recent_ftp_entrypath)
               return CURLE_OUT_OF_MEMORY;
-            ftp_state(data, FTP_SYST);
+            ftp_state(data, ftpc, FTP_SYST);
             break;
           }
 
@@ -2917,8 +2942,8 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
           infof(data, "Failed to figure out path");
         }
       }
-      ftp_state(data, FTP_STOP); /* we are done with the CONNECT phase! */
-      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_DSTATE(data));
+      ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
+      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
       break;
 
     case FTP_SYST:
@@ -2950,7 +2975,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
           /* remember target server OS */
           free(ftpc->server_os);
           ftpc->server_os = os;
-          ftp_state(data, FTP_NAMEFMT);
+          ftp_state(data, ftpc, FTP_NAMEFMT);
           break;
         }
         /* Nothing special for the target server. */
@@ -2962,19 +2987,19 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         /* Cannot identify server OS. Continue anyway and cross fingers. */
       }
 
-      ftp_state(data, FTP_STOP); /* we are done with the CONNECT phase! */
-      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_DSTATE(data));
+      ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
+      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
       break;
 
     case FTP_NAMEFMT:
       if(ftpcode == 250) {
         /* Name format change successful: reload initial path. */
-        ftp_state_pwd(data, conn);
+        ftp_state_pwd(data, ftpc);
         break;
       }
 
-      ftp_state(data, FTP_STOP); /* we are done with the CONNECT phase! */
-      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_DSTATE(data));
+      ftp_state(data, ftpc, FTP_STOP); /* we are done with CONNECT phase! */
+      CURL_TRC_FTP(data, "[%s] protocol connect phase DONE", FTP_CSTATE(ftpc));
       break;
 
     case FTP_QUOTE:
@@ -2987,7 +3012,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         result = CURLE_QUOTE_ERROR;
       }
       else
-        result = ftp_state_quote(data, FALSE, ftpc->state);
+        result = ftp_state_quote(data, ftpc, ftp, FALSE, ftpc->state);
       break;
 
     case FTP_CWD:
@@ -3006,7 +3031,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
           result = Curl_pp_sendf(data, &ftpc->pp, "MKD %s",
                                  ftpc->dirs[ftpc->cwdcount - 1]);
           if(!result)
-            ftp_state(data, FTP_MKD);
+            ftp_state(data, ftpc, FTP_MKD);
         }
         else {
           /* return failure */
@@ -3024,7 +3049,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
           result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s",
                                  ftpc->dirs[ftpc->cwdcount - 1]);
         else
-          result = ftp_state_mdtm(data);
+          result = ftp_state_mdtm(data, ftpc, ftp);
       }
       break;
 
@@ -3035,7 +3060,7 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         result = CURLE_REMOTE_ACCESS_DENIED;
       }
       else {
-        ftp_state(data, FTP_CWD);
+        ftp_state(data, ftpc, FTP_CWD);
         /* send CWD */
         result = Curl_pp_sendf(data, &ftpc->pp, "CWD %s",
                                ftpc->dirs[ftpc->cwdcount - 1]);
@@ -3043,25 +3068,25 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
       break;
 
     case FTP_MDTM:
-      result = ftp_state_mdtm_resp(data, ftpcode);
+      result = ftp_state_mdtm_resp(data, ftpc, ftp, ftpcode);
       break;
 
     case FTP_TYPE:
     case FTP_LIST_TYPE:
     case FTP_RETR_TYPE:
     case FTP_STOR_TYPE:
-      result = ftp_state_type_resp(data, ftpcode, ftpc->state);
+      result = ftp_state_type_resp(data, ftpc, ftp, ftpcode, ftpc->state);
       break;
 
     case FTP_SIZE:
     case FTP_RETR_SIZE:
     case FTP_STOR_SIZE:
-      result = ftp_state_size_resp(data, ftpcode, ftpc->state);
+      result = ftp_state_size_resp(data, ftpc, ftp, ftpcode, ftpc->state);
       break;
 
     case FTP_REST:
     case FTP_RETR_REST:
-      result = ftp_state_rest_resp(data, conn, ftpcode, ftpc->state);
+      result = ftp_state_rest_resp(data, ftpc, ftp, ftpcode, ftpc->state);
       break;
 
     case FTP_PRET:
@@ -3070,30 +3095,30 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
         failf(data, "PRET command not accepted: %03d", ftpcode);
         return CURLE_FTP_PRET_FAILED;
       }
-      result = ftp_state_use_pasv(data, conn);
+      result = ftp_state_use_pasv(data, ftpc, conn);
       break;
 
     case FTP_PASV:
-      result = ftp_state_pasv_resp(data, ftpcode);
+      result = ftp_state_pasv_resp(data, ftpc, ftpcode);
       break;
 
     case FTP_PORT:
-      result = ftp_state_port_resp(data, ftpcode);
+      result = ftp_state_port_resp(data, ftpc, ftp, ftpcode);
       break;
 
     case FTP_LIST:
     case FTP_RETR:
-      result = ftp_state_get_resp(data, ftpcode, ftpc->state);
+      result = ftp_state_get_resp(data, ftpc, ftp, ftpcode, ftpc->state);
       break;
 
     case FTP_STOR:
-      result = ftp_state_stor_resp(data, ftpcode, ftpc->state);
+      result = ftp_state_stor_resp(data, ftpc, ftpcode, ftpc->state);
       break;
 
     case FTP_QUIT:
     default:
       /* internal error */
-      ftp_state(data, FTP_STOP);
+      ftp_state(data, ftpc, FTP_STOP);
       break;
     }
   } /* if(ftpcode) */
@@ -3103,11 +3128,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
 
 
 /* called repeatedly until done from multi.c */
-static CURLcode ftp_multi_statemach(struct Curl_easy *data,
-                                    bool *done)
+static CURLcode ftp_statemach(struct Curl_easy *data,
+                               struct ftp_conn *ftpc,
+                               bool *done)
 {
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   CURLcode result = Curl_pp_statemach(data, &ftpc->pp, FALSE, FALSE);
 
   /* Check for the state outside of the Curl_socket_check() return code checks
@@ -3118,10 +3142,17 @@ static CURLcode ftp_multi_statemach(struct Curl_easy *data,
   return result;
 }
 
+/* called repeatedly until done from multi.c */
+static CURLcode ftp_multi_statemach(struct Curl_easy *data,
+                                    bool *done)
+{
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  return ftpc ? ftp_statemach(data, ftpc, done) : CURLE_FAILED_INIT;
+}
+
 static CURLcode ftp_block_statemach(struct Curl_easy *data,
-                                    struct connectdata *conn)
+                                    struct ftp_conn *ftpc)
 {
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   struct pingpong *pp = &ftpc->pp;
   CURLcode result = CURLE_OK;
 
@@ -3149,15 +3180,17 @@ static CURLcode ftp_connect(struct Curl_easy *data,
 {
   CURLcode result;
   struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-  struct pingpong *pp = &ftpc->pp;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  struct pingpong *pp;
 
   *done = FALSE; /* default to not done yet */
-
+  if(!ftpc)
+    return CURLE_FAILED_INIT;
+  pp = &ftpc->pp;
   /* We always support persistent connections on ftp */
   connkeep(conn, "FTP default");
 
-  PINGPONG_SETUP(pp, ftp_statemachine, ftp_endofresp);
+  PINGPONG_SETUP(pp, ftp_pp_statemachine, ftp_endofresp);
 
   if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
     /* BLOCKING */
@@ -3171,9 +3204,9 @@ static CURLcode ftp_connect(struct Curl_easy *data,
 
   /* When we connect, we start in the state where we await the 220
      response */
-  ftp_state(data, FTP_WAIT220);
+  ftp_state(data, ftpc, FTP_WAIT220);
 
-  result = ftp_multi_statemach(data, done);
+  result = ftp_statemach(data, ftpc, done);
 
   return result;
 }
@@ -3191,18 +3224,19 @@ static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
                          bool premature)
 {
   struct connectdata *conn = data->conn;
-  struct FTP *ftp = data->req.p.ftp;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-  struct pingpong *pp = &ftpc->pp;
+  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  struct pingpong *pp;
   ssize_t nread;
   int ftpcode;
   CURLcode result = CURLE_OK;
   char *rawPath = NULL;
   size_t pathLen = 0;
 
-  if(!ftp)
+  if(!ftp || !ftpc)
     return CURLE_OK;
 
+  pp = &ftpc->pp;
   switch(status) {
   case CURLE_BAD_DOWNLOAD_RESUME:
   case CURLE_FTP_WEIRD_PASV_REPLY:
@@ -3303,7 +3337,7 @@ static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
       }
     }
 
-    close_secondarysocket(data);
+    close_secondarysocket(data, ftpc);
   }
 
   if(!result && (ftp->transfer == PPTRANSFER_BODY) && ftpc->ctl_valid &&
@@ -3329,10 +3363,8 @@ static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
       connclose(conn, "Timeout or similar in FTP DONE operation"); /* close */
     }
 
-    if(result) {
-      Curl_safefree(ftp->pathalloc);
+    if(result)
       return result;
-    }
 
     if(ftpc->dont_check && data->req.maxdownload > 0) {
       /* we have just sent ABOR and there is no reliable way to check if it was
@@ -3400,9 +3432,8 @@ static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
 
   /* Send any post-transfer QUOTE strings? */
   if(!status && !result && !premature && data->set.postquote)
-    result = ftp_sendquote(data, conn, data->set.postquote);
-  CURL_TRC_FTP(data, "[%s] done, result=%d", FTP_DSTATE(data), result);
-  Curl_safefree(ftp->pathalloc);
+    result = ftp_sendquote(data, ftpc, data->set.postquote);
+  CURL_TRC_FTP(data, "[%s] done, result=%d", FTP_CSTATE(ftpc), result);
   return result;
 }
 
@@ -3418,10 +3449,10 @@ static CURLcode ftp_done(struct Curl_easy *data, CURLcode status,
 
 static
 CURLcode ftp_sendquote(struct Curl_easy *data,
-                       struct connectdata *conn, struct curl_slist *quote)
+                       struct ftp_conn *ftpc,
+                       struct curl_slist *quote)
 {
   struct curl_slist *item;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   struct pingpong *pp = &ftpc->pp;
 
   item = quote;
@@ -3469,10 +3500,10 @@ CURLcode ftp_sendquote(struct Curl_easy *data,
  *
  * Returns TRUE if we in the current situation should send TYPE
  */
-static int ftp_need_type(struct connectdata *conn,
+static int ftp_need_type(struct ftp_conn *ftpc,
                          bool ascii_wanted)
 {
-  return conn->proto.ftpc.transfertype != (ascii_wanted ? 'A' : 'I');
+  return ftpc->transfertype != (ascii_wanted ? 'A' : 'I');
 }
 
 /***********************************************************************
@@ -3484,21 +3515,21 @@ static int ftp_need_type(struct connectdata *conn,
  * If the transfer type is not sent, simulate on OK response in newstate
  */
 static CURLcode ftp_nb_type(struct Curl_easy *data,
-                            struct connectdata *conn,
+                            struct ftp_conn *ftpc,
+                            struct FTP *ftp,
                             bool ascii, ftpstate newstate)
 {
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   CURLcode result;
   char want = (char)(ascii ? 'A' : 'I');
 
   if(ftpc->transfertype == want) {
-    ftp_state(data, newstate);
-    return ftp_state_type_resp(data, 200, newstate);
+    ftp_state(data, ftpc, newstate);
+    return ftp_state_type_resp(data, ftpc, ftp, 200, newstate);
   }
 
   result = Curl_pp_sendf(data, &ftpc->pp, "TYPE %c", want);
   if(!result) {
-    ftp_state(data, newstate);
+    ftp_state(data, ftpc, newstate);
 
     /* keep track of our current transfer type */
     ftpc->transfertype = want;
@@ -3542,16 +3573,17 @@ ftp_pasv_verbose(struct Curl_easy *data,
 static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
 {
   struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
   CURLcode result = CURLE_OK;
   bool connected = FALSE;
   bool complete = FALSE;
-
   /* the ftp struct is inited in ftp_connect(). If we are connecting to an HTTP
    * proxy then the state will not be valid until after that connection is
    * complete */
-  struct FTP *ftp = NULL;
 
+  if(!ftpc || !ftp)
+    return CURLE_FAILED_INIT;
   /* if the second connection has been set up, try to connect it fully
    * to the remote host. This may not complete at this time, for several
    * reasons:
@@ -3568,20 +3600,17 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
       if(result && !is_eptr && (ftpc->count1 == 0)) {
         *completep = -1; /* go back to DOING please */
         /* this is a EPSV connect failing, try PASV instead */
-        return ftp_epsv_disable(data, conn);
+        return ftp_epsv_disable(data, ftpc, conn);
       }
       *completep = (int)complete;
       return result;
     }
   }
 
-  /* Curl_proxy_connect might have moved the protocol state */
-  ftp = data->req.p.ftp;
-
   if(ftpc->state) {
     /* already in a state so skip the initial commands.
        They are only done to kickstart the do_more state */
-    result = ftp_multi_statemach(data, &complete);
+    result = ftp_statemach(data, ftpc, &complete);
 
     *completep = (int)complete;
 
@@ -3610,7 +3639,7 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
       if(serv_conned) {
         /* It looks data connection is established */
         ftpc->wait_data_conn = FALSE;
-        result = InitiateTransfer(data);
+        result = ftp_initiate_transfer(data, ftpc);
 
         if(result)
           return result;
@@ -3619,18 +3648,18 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
                            connected back to us */
       }
       else {
-        result = ftp_check_ctrl_on_data_wait(data);
+        result = ftp_check_ctrl_on_data_wait(data, ftpc);
         if(result)
           return result;
       }
     }
     else if(data->state.upload) {
-      result = ftp_nb_type(data, conn, data->state.prefer_ascii,
+      result = ftp_nb_type(data, ftpc, ftp, data->state.prefer_ascii,
                            FTP_STOR_TYPE);
       if(result)
         return result;
 
-      result = ftp_multi_statemach(data, &complete);
+      result = ftp_statemach(data, ftpc, &complete);
       *completep = (int)complete;
     }
     else {
@@ -3653,20 +3682,20 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
 
         /* But only if a body transfer was requested. */
         if(ftp->transfer == PPTRANSFER_BODY) {
-          result = ftp_nb_type(data, conn, TRUE, FTP_LIST_TYPE);
+          result = ftp_nb_type(data, ftpc, ftp, TRUE, FTP_LIST_TYPE);
           if(result)
             return result;
         }
         /* otherwise just fall through */
       }
       else {
-        result = ftp_nb_type(data, conn, data->state.prefer_ascii,
+        result = ftp_nb_type(data, ftpc, ftp, data->state.prefer_ascii,
                              FTP_RETR_TYPE);
         if(result)
           return result;
       }
 
-      result = ftp_multi_statemach(data, &complete);
+      result = ftp_statemach(data, ftpc, &complete);
       *completep = (int)complete;
     }
     return result;
@@ -3678,7 +3707,7 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
   if(!ftpc->wait_data_conn) {
     /* no waiting for the data connection so this is now complete */
     *completep = 1;
-    CURL_TRC_FTP(data, "[%s] DO-MORE phase ends with %d", FTP_DSTATE(data),
+    CURL_TRC_FTP(data, "[%s] DO-MORE phase ends with %d", FTP_CSTATE(ftpc),
                  (int)result);
   }
 
@@ -3697,41 +3726,42 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
 
 static
 CURLcode ftp_perform(struct Curl_easy *data,
+                     struct ftp_conn *ftpc,
+                     struct FTP *ftp,
                      bool *connected,  /* connect status after PASV / PORT */
                      bool *dophase_done)
 {
   /* this is FTP and no proxy */
   CURLcode result = CURLE_OK;
 
-  CURL_TRC_FTP(data, "[%s] DO phase starts", FTP_DSTATE(data));
+  CURL_TRC_FTP(data, "[%s] DO phase starts", FTP_CSTATE(ftpc));
 
   if(data->req.no_body) {
     /* requested no body means no transfer... */
-    struct FTP *ftp = data->req.p.ftp;
     ftp->transfer = PPTRANSFER_INFO;
   }
 
   *dophase_done = FALSE; /* not done yet */
 
   /* start the first command in the DO phase */
-  result = ftp_state_quote(data, TRUE, FTP_QUOTE);
+  result = ftp_state_quote(data, ftpc, ftp, TRUE, FTP_QUOTE);
   if(result)
     return result;
 
   /* run the state-machine */
-  result = ftp_multi_statemach(data, dophase_done);
+  result = ftp_statemach(data, ftpc, dophase_done);
 
   *connected = Curl_conn_is_connected(data->conn, SECONDARYSOCKET);
 
   if(*connected)
     infof(data, "[FTP] [%s] perform, DATA connection established",
-          FTP_DSTATE(data));
+          FTP_CSTATE(ftpc));
   else
     CURL_TRC_FTP(data, "[%s] perform, awaiting DATA connect",
-                 FTP_DSTATE(data));
+                 FTP_CSTATE(ftpc));
 
   if(*dophase_done)
-    CURL_TRC_FTP(data, "[%s] DO phase is complete1", FTP_DSTATE(data));
+    CURL_TRC_FTP(data, "[%s] DO phase is complete1", FTP_CSTATE(ftpc));
 
   return result;
 }
@@ -3744,10 +3774,11 @@ static void wc_data_dtor(void *ptr)
   free(ftpwc);
 }
 
-static CURLcode init_wc_data(struct Curl_easy *data)
+static CURLcode init_wc_data(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
+                             struct FTP *ftp)
 {
   char *last_slash;
-  struct FTP *ftp = data->req.p.ftp;
   char *path = ftp->path;
   struct WildcardData *wildcard = data->wildcard;
   CURLcode result = CURLE_OK;
@@ -3758,7 +3789,7 @@ static CURLcode init_wc_data(struct Curl_easy *data)
     last_slash++;
     if(last_slash[0] == '\0') {
       wildcard->state = CURLWC_CLEAN;
-      return ftp_parse_url_path(data);
+      return ftp_parse_url_path(data, ftpc, ftp);
     }
     wildcard->pattern = strdup(last_slash);
     if(!wildcard->pattern)
@@ -3774,7 +3805,7 @@ static CURLcode init_wc_data(struct Curl_easy *data)
     }
     else { /* only list */
       wildcard->state = CURLWC_CLEAN;
-      return ftp_parse_url_path(data);
+      return ftp_parse_url_path(data, ftpc, ftp);
     }
   }
 
@@ -3803,7 +3834,7 @@ static CURLcode init_wc_data(struct Curl_easy *data)
     data->set.ftp_filemethod = FTPFILE_MULTICWD;
 
   /* try to parse ftp URL */
-  result = ftp_parse_url_path(data);
+  result = ftp_parse_url_path(data, ftpc, ftp);
   if(result) {
     goto fail;
   }
@@ -3837,16 +3868,17 @@ fail:
   return result;
 }
 
-static CURLcode wc_statemach(struct Curl_easy *data)
+static CURLcode wc_statemach(struct Curl_easy *data,
+                             struct ftp_conn *ftpc,
+                             struct FTP *ftp)
 {
   struct WildcardData * const wildcard = data->wildcard;
-  struct connectdata *conn = data->conn;
   CURLcode result = CURLE_OK;
 
   for(;;) {
     switch(wildcard->state) {
     case CURLWC_INIT:
-      result = init_wc_data(data);
+      result = init_wc_data(data, ftpc, ftp);
       if(wildcard->state == CURLWC_CLEAN)
         /* only listing! */
         return result;
@@ -3878,10 +3910,8 @@ static CURLcode wc_statemach(struct Curl_easy *data)
 
     case CURLWC_DOWNLOADING: {
       /* filelist has at least one file, lets get first one */
-      struct ftp_conn *ftpc = &conn->proto.ftpc;
       struct Curl_llist_node *head = Curl_llist_head(&wildcard->filelist);
       struct curl_fileinfo *finfo = Curl_node_elem(head);
-      struct FTP *ftp = data->req.p.ftp;
 
       char *tmp_path = aprintf("%s%s", wildcard->path, finfo->filename);
       if(!tmp_path)
@@ -3918,7 +3948,7 @@ static CURLcode wc_statemach(struct Curl_easy *data)
       if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
         ftpc->known_filesize = finfo->size;
 
-      result = ftp_parse_url_path(data);
+      result = ftp_parse_url_path(data, ftpc, ftp);
       if(result)
         return result;
 
@@ -3982,10 +4012,12 @@ static CURLcode wc_statemach(struct Curl_easy *data)
 static CURLcode ftp_do(struct Curl_easy *data, bool *done)
 {
   CURLcode result = CURLE_OK;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
 
   *done = FALSE; /* default to false */
+  if(!ftpc || !ftp)
+    return CURLE_FAILED_INIT;
   ftpc->wait_data_conn = FALSE; /* default to no such wait */
 
 #ifdef CURL_PREFER_LF_LINEENDS
@@ -4007,7 +4039,7 @@ static CURLcode ftp_do(struct Curl_easy *data, bool *done)
 #endif /* CURL_PREFER_LF_LINEENDS */
 
   if(data->state.wildcardmatch) {
-    result = wc_statemach(data);
+    result = wc_statemach(data, ftpc, ftp);
     if(data->wildcard->state == CURLWC_SKIP ||
        data->wildcard->state == CURLWC_DONE) {
       /* do not call ftp_regular_transfer */
@@ -4017,12 +4049,12 @@ static CURLcode ftp_do(struct Curl_easy *data, bool *done)
       return result;
   }
   else { /* no wildcard FSM needed */
-    result = ftp_parse_url_path(data);
+    result = ftp_parse_url_path(data, ftpc, ftp);
     if(result)
       return result;
   }
 
-  result = ftp_regular_transfer(data, done);
+  result = ftp_regular_transfer(data, ftpc, ftp, done);
 
   return result;
 }
@@ -4037,25 +4069,26 @@ static CURLcode ftp_do(struct Curl_easy *data, bool *done)
  * connection.
  *
  */
-static CURLcode ftp_quit(struct Curl_easy *data, struct connectdata *conn)
+static CURLcode ftp_quit(struct Curl_easy *data,
+                         struct ftp_conn *ftpc)
 {
   CURLcode result = CURLE_OK;
 
-  if(conn->proto.ftpc.ctl_valid) {
+  if(ftpc->ctl_valid) {
     CURL_TRC_FTP(data, "sending QUIT to close session");
-    result = Curl_pp_sendf(data, &conn->proto.ftpc.pp, "%s", "QUIT");
+    result = Curl_pp_sendf(data, &ftpc->pp, "%s", "QUIT");
     if(result) {
       failf(data, "Failure sending QUIT command: %s",
             curl_easy_strerror(result));
-      conn->proto.ftpc.ctl_valid = FALSE; /* mark control connection as bad */
-      connclose(conn, "QUIT command failed"); /* mark for connection closure */
-      ftp_state(data, FTP_STOP);
+      ftpc->ctl_valid = FALSE; /* mark control connection as bad */
+      connclose(data->conn, "QUIT command failed"); /* mark for closure */
+      ftp_state(data, ftpc, FTP_STOP);
       return result;
     }
 
-    ftp_state(data, FTP_QUIT);
+    ftp_state(data, ftpc, FTP_QUIT);
 
-    result = ftp_block_statemach(data, conn);
+    result = ftp_block_statemach(data, ftpc);
   }
 
   return result;
@@ -4072,9 +4105,10 @@ static CURLcode ftp_disconnect(struct Curl_easy *data,
                                struct connectdata *conn,
                                bool dead_connection)
 {
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-  struct pingpong *pp = &ftpc->pp;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
 
+  if(!ftpc)
+    return CURLE_FAILED_INIT;
   /* We cannot send quit unconditionally. If this connection is stale or
      bad in any way, sending quit and waiting around here will make the
      disconnect wait in vain and cause more problems than we need to.
@@ -4087,15 +4121,7 @@ static CURLcode ftp_disconnect(struct Curl_easy *data,
     ftpc->ctl_valid = FALSE;
 
   /* The FTP session may or may not have been allocated/setup at this point! */
-  (void)ftp_quit(data, conn); /* ignore errors on the QUIT */
-
-  freedirs(ftpc);
-  Curl_safefree(ftpc->account);
-  Curl_safefree(ftpc->alternative_to_user);
-  Curl_safefree(ftpc->entrypath);
-  Curl_safefree(ftpc->prevpath);
-  Curl_safefree(ftpc->server_os);
-  Curl_pp_disconnect(pp);
+  (void)ftp_quit(data, ftpc); /* ignore errors on the QUIT */
   return CURLE_OK;
 }
 
@@ -4107,12 +4133,10 @@ static CURLcode ftp_disconnect(struct Curl_easy *data,
  *
  */
 static
-CURLcode ftp_parse_url_path(struct Curl_easy *data)
+CURLcode ftp_parse_url_path(struct Curl_easy *data,
+                            struct ftp_conn *ftpc,
+                            struct FTP *ftp)
 {
-  /* the ftp struct is already inited in ftp_connect() */
-  struct FTP *ftp = data->req.p.ftp;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   const char *slashPos = NULL;
   const char *fileName = NULL;
   CURLcode result = CURLE_OK;
@@ -4235,7 +4259,7 @@ CURLcode ftp_parse_url_path(struct Curl_easy *data)
   if((data->set.ftp_filemethod == FTPFILE_NOCWD) && (rawPath[0] == '/'))
     ftpc->cwddone = TRUE; /* skip CWD for absolute paths */
   else { /* newly created FTP connections are already in entry path */
-    const char *oldPath = conn->bits.reuse ? ftpc->prevpath : "";
+    const char *oldPath = data->conn->bits.reuse ? ftpc->prevpath : "";
     if(oldPath) {
       size_t n = pathLen;
       if(data->set.ftp_filemethod == FTPFILE_NOCWD)
@@ -4255,18 +4279,17 @@ CURLcode ftp_parse_url_path(struct Curl_easy *data)
 }
 
 /* call this when the DO phase has completed */
-static CURLcode ftp_dophase_done(struct Curl_easy *data, bool connected)
+static CURLcode ftp_dophase_done(struct Curl_easy *data,
+                                 struct ftp_conn *ftpc,
+                                 struct FTP *ftp,
+                                 bool connected)
 {
-  struct connectdata *conn = data->conn;
-  struct FTP *ftp = data->req.p.ftp;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
-
   if(connected) {
     int completed;
     CURLcode result = ftp_do_more(data, &completed);
 
     if(result) {
-      close_secondarysocket(data);
+      close_secondarysocket(data, ftpc);
       return result;
     }
   }
@@ -4276,7 +4299,7 @@ static CURLcode ftp_dophase_done(struct Curl_easy *data, bool connected)
     Curl_xfer_setup_nop(data);
   else if(!connected)
     /* since we did not connect now, we want do_more to get called */
-    conn->bits.do_more = TRUE;
+    data->conn->bits.do_more = TRUE;
 
   ftpc->ctl_valid = TRUE; /* seems good */
 
@@ -4287,14 +4310,20 @@ static CURLcode ftp_dophase_done(struct Curl_easy *data, bool connected)
 static CURLcode ftp_doing(struct Curl_easy *data,
                           bool *dophase_done)
 {
-  CURLcode result = ftp_multi_statemach(data, dophase_done);
+  struct ftp_conn *ftpc = Curl_conn_meta_get(data->conn, CURL_META_FTP_CONN);
+  struct FTP *ftp = Curl_meta_get(data, CURL_META_FTP_EASY);
+  CURLcode result;
+
+  if(!ftpc || !ftp)
+    return CURLE_FAILED_INIT;
+  result = ftp_statemach(data, ftpc, dophase_done);
 
   if(result)
-    CURL_TRC_FTP(data, "[%s] DO phase failed", FTP_DSTATE(data));
+    CURL_TRC_FTP(data, "[%s] DO phase failed", FTP_CSTATE(ftpc));
   else if(*dophase_done) {
-    result = ftp_dophase_done(data, FALSE /* not connected */);
+    result = ftp_dophase_done(data, ftpc, ftp, FALSE /* not connected */);
 
-    CURL_TRC_FTP(data, "[%s] DO phase is complete2", FTP_DSTATE(data));
+    CURL_TRC_FTP(data, "[%s] DO phase is complete2", FTP_CSTATE(ftpc));
   }
   return result;
 }
@@ -4313,12 +4342,12 @@ static CURLcode ftp_doing(struct Curl_easy *data,
  */
 static
 CURLcode ftp_regular_transfer(struct Curl_easy *data,
+                              struct ftp_conn *ftpc,
+                              struct FTP *ftp,
                               bool *dophase_done)
 {
   CURLcode result = CURLE_OK;
   bool connected = FALSE;
-  struct connectdata *conn = data->conn;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
   data->req.size = -1; /* make sure this is unknown at this point */
 
   Curl_pgrsSetUploadCounter(data, 0);
@@ -4328,7 +4357,7 @@ CURLcode ftp_regular_transfer(struct Curl_easy *data,
 
   ftpc->ctl_valid = TRUE; /* starts good */
 
-  result = ftp_perform(data,
+  result = ftp_perform(data, ftpc, ftp,
                        &connected, /* have we connected after PASV/PORT */
                        dophase_done); /* all commands in the DO-phase done? */
 
@@ -4338,7 +4367,7 @@ CURLcode ftp_regular_transfer(struct Curl_easy *data,
       /* the DO phase has not completed yet */
       return CURLE_OK;
 
-    result = ftp_dophase_done(data, connected);
+    result = ftp_dophase_done(data, ftpc, ftp, connected);
 
     if(result)
       return result;
@@ -4349,16 +4378,46 @@ CURLcode ftp_regular_transfer(struct Curl_easy *data,
   return result;
 }
 
+static void ftp_easy_dtor(void *key, size_t klen, void *entry)
+{
+  struct FTP *ftp = entry;
+  (void)key;
+  (void)klen;
+  Curl_safefree(ftp->pathalloc);
+  free(ftp);
+}
+
+static void ftp_conn_dtor(void *key, size_t klen, void *entry)
+{
+  struct ftp_conn *ftpc = entry;
+  (void)key;
+  (void)klen;
+  freedirs(ftpc);
+  Curl_safefree(ftpc->account);
+  Curl_safefree(ftpc->alternative_to_user);
+  Curl_safefree(ftpc->entrypath);
+  Curl_safefree(ftpc->prevpath);
+  Curl_safefree(ftpc->server_os);
+  Curl_pp_disconnect(&ftpc->pp);
+  free(ftpc);
+}
+
 static CURLcode ftp_setup_connection(struct Curl_easy *data,
                                      struct connectdata *conn)
 {
   char *type;
   struct FTP *ftp;
   CURLcode result = CURLE_OK;
-  struct ftp_conn *ftpc = &conn->proto.ftpc;
+  struct ftp_conn *ftpc;
+
+  ftp = calloc(1, sizeof(*ftp));
+  if(!ftp ||
+    Curl_meta_set(data, CURL_META_FTP_EASY, ftp, ftp_easy_dtor))
+    return CURLE_OUT_OF_MEMORY;
 
-  ftp = calloc(1, sizeof(struct FTP));
-  if(!ftp)
+  ftpc = calloc(1, sizeof(*ftpc));
+  if(!ftpc ||
+     Curl_conn_meta_set(conn, CURL_META_FTP_CONN, ftpc, ftp_conn_dtor))
     return CURLE_OUT_OF_MEMORY;
 
   /* clone connection related data that is FTP specific */
@@ -4366,6 +4425,7 @@ static CURLcode ftp_setup_connection(struct Curl_easy *data,
     ftpc->account = strdup(data->set.str[STRING_FTP_ACCOUNT]);
     if(!ftpc->account) {
       free(ftp);
+      Curl_conn_meta_remove(conn, CURL_META_FTP_CONN);
       return CURLE_OUT_OF_MEMORY;
     }
   }
@@ -4375,10 +4435,10 @@ static CURLcode ftp_setup_connection(struct Curl_easy *data,
     if(!ftpc->alternative_to_user) {
       Curl_safefree(ftpc->account);
       free(ftp);
+      Curl_conn_meta_remove(conn, CURL_META_FTP_CONN);
       return CURLE_OUT_OF_MEMORY;
     }
   }
-  data->req.p.ftp = ftp;
 
   ftp->path = &data->state.up.path[1]; /* do not include the initial slash */
 
@@ -4418,8 +4478,23 @@ static CURLcode ftp_setup_connection(struct Curl_easy *data,
   ftpc->use_ssl = data->set.use_ssl;
   ftpc->ccc = data->set.ftp_ccc;
 
-  CURL_TRC_FTP(data, "[%s] setup connection -> %d", FTP_CSTATE(conn), result);
+  CURL_TRC_FTP(data, "[%s] setup connection -> %d", FTP_CSTATE(ftpc), result);
   return result;
 }
 
+bool ftp_conns_match(struct connectdata *needle, struct connectdata *conn)
+{
+  struct ftp_conn *nftpc = Curl_conn_meta_get(needle, CURL_META_FTP_CONN);
+  struct ftp_conn *cftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
+  /* Also match ACCOUNT, ALTERNATIVE-TO-USER, USE_SSL and CCC options */
+  if(!nftpc || !cftpc ||
+     Curl_timestrcmp(nftpc->account, cftpc->account) ||
+     Curl_timestrcmp(nftpc->alternative_to_user,
+                     cftpc->alternative_to_user) ||
+     (nftpc->use_ssl != cftpc->use_ssl) ||
+     (nftpc->ccc != cftpc->ccc))
+    return FALSE;
+  return TRUE;
+}
+
 #endif /* CURL_DISABLE_FTP */
index bf6542a5bd5174da9fe2fe4a6486ee471df3bee9..c31aa9328617b93e0ce899e83f4142666fadc2e6 100644 (file)
--- a/lib/ftp.h
+++ b/lib/ftp.h
@@ -37,6 +37,9 @@ extern const struct Curl_handler Curl_handler_ftps;
 
 CURLcode Curl_GetFTPResponse(struct Curl_easy *data, ssize_t *nread,
                              int *ftpcode);
+
+bool ftp_conns_match(struct connectdata *needle, struct connectdata *conn);
+
 #endif /* CURL_DISABLE_FTP */
 
 /****************************************************************************
@@ -163,6 +166,11 @@ struct ftp_conn {
   BIT(shutdown);    /* connection is being shutdown, e.g. QUIT */
 };
 
+/* meta key for storing `struct FTP` as easy meta data */
+#define CURL_META_FTP_EASY   "meta:proto:ftp:easy"
+/* meta key for storing `struct ftp_conn` as connection meta data */
+#define CURL_META_FTP_CONN   "meta:proto:ftp:conn"
+
 #define DEFAULT_ACCEPT_TIMEOUT   60000 /* milliseconds == one minute */
 
 #endif /* HEADER_CURL_FTP_H */
index 99fd91f9102febe32f29dfb43eeb7f807aae5693..5c74d9f033b2872c1774ea94a14749ecfa2bc88d 100644 (file)
@@ -46,6 +46,7 @@
 #endif
 
 #include "urldata.h"
+#include "url.h"
 #include "cfilters.h"
 #include "cf-socket.h"
 #include "curl_base64.h"
@@ -217,6 +218,10 @@ krb5_auth(void *app_data, struct Curl_easy *data, struct connectdata *conn)
   struct sockaddr_in *remote_addr =
     (struct sockaddr_in *)CURL_UNCONST(&conn->remote_addr->curl_sa_addr);
   char *stringp;
+  struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
+
+  if(!ftpc)
+    return -2;
 
   if(getsockname(conn->sock[FIRSTSOCKET],
                  (struct sockaddr *)&conn->local_addr, &l) < 0)
@@ -242,8 +247,7 @@ krb5_auth(void *app_data, struct Curl_easy *data, struct connectdata *conn)
       if(Curl_GetFTPResponse(data, &nread, NULL))
         return -1;
       else {
-        struct pingpong *pp = &conn->proto.ftpc.pp;
-        char *line = Curl_dyn_ptr(&pp->recvbuf);
+        char *line = Curl_dyn_ptr(&ftpc->pp.recvbuf);
         if(line[0] != '3')
           return -1;
       }
@@ -331,9 +335,8 @@ krb5_auth(void *app_data, struct Curl_easy *data, struct connectdata *conn)
           break;
         }
         else {
-          struct pingpong *pp = &conn->proto.ftpc.pp;
-          size_t len = Curl_dyn_len(&pp->recvbuf);
-          p = Curl_dyn_ptr(&pp->recvbuf);
+          size_t len = Curl_dyn_len(&ftpc->pp.recvbuf);
+          p = Curl_dyn_ptr(&ftpc->pp.recvbuf);
           if((len < 4) || (p[0] != '2' && p[0] != '3')) {
             infof(data, "Server did not accept auth data");
             ret = AUTH_ERROR;
@@ -781,9 +784,12 @@ static int sec_set_protection_level(struct Curl_easy *data)
   if(level) {
     char *pbsz;
     unsigned int buffer_size = 1 << 20; /* 1048576 */
-    struct pingpong *pp = &conn->proto.ftpc.pp;
+    struct ftp_conn *ftpc = Curl_conn_meta_get(conn, CURL_META_FTP_CONN);
     char *line;
 
+    if(!ftpc)
+      return -2;
+
     code = ftp_send_command(data, "PBSZ %u", buffer_size);
     if(code < 0)
       return -1;
@@ -794,7 +800,7 @@ static int sec_set_protection_level(struct Curl_easy *data)
     }
     conn->buffer_size = buffer_size;
 
-    line = Curl_dyn_ptr(&pp->recvbuf);
+    line = Curl_dyn_ptr(&ftpc->pp.recvbuf);
     pbsz = strstr(line, "PBSZ=");
     if(pbsz) {
       /* stick to default value if the check fails */
index 3b8f212123cab251e51bf7a3485e44cd1b3dbef6..79d1544c270251190e84e35ed31342f4281dab16 100644 (file)
@@ -120,7 +120,7 @@ void Curl_req_hard_reset(struct SingleRequest *req, struct Curl_easy *data)
 
   /* This is a bit ugly. `req->p` is a union and we assume we can
    * free this safely without leaks. */
-  Curl_safefree(req->p.ftp);
+  Curl_safefree(req->p.file);
   Curl_safefree(req->newurl);
   Curl_client_reset(data);
   if(req->sendbuf_init)
@@ -173,7 +173,7 @@ void Curl_req_free(struct SingleRequest *req, struct Curl_easy *data)
 {
   /* This is a bit ugly. `req->p` is a union and we assume we can
    * free this safely without leaks. */
-  Curl_safefree(req->p.ftp);
+  Curl_safefree(req->p.file);
   Curl_safefree(req->newurl);
   if(req->sendbuf_init)
     Curl_bufq_free(&req->sendbuf);
index c6380a24d312dc4ae0d5267874b37430edf9b7b9..d332ed014307a924a566647f01efb737d1426d48 100644 (file)
@@ -103,7 +103,6 @@ struct SingleRequest {
      points to data it needs. */
   union {
     struct FILEPROTO *file;
-    struct FTP *ftp;
     struct IMAP *imap;
     struct ldapreqinfo *ldap;
     struct POP3 *pop3;
index 41c714ae53e23cd275657661e6d2aa33a181da9a..fe885b29f56496ffbc3b762f05d3e1d1453c4a4e 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -1046,13 +1046,7 @@ static bool url_match_conn(struct connectdata *conn, void *userdata)
 #endif
 #ifndef CURL_DISABLE_FTP
   else if(get_protocol_family(needle->handler) & PROTO_FAMILY_FTP) {
-    /* Also match ACCOUNT, ALTERNATIVE-TO-USER, USE_SSL and CCC options */
-    if(Curl_timestrcmp(needle->proto.ftpc.account,
-                       conn->proto.ftpc.account) ||
-       Curl_timestrcmp(needle->proto.ftpc.alternative_to_user,
-                       conn->proto.ftpc.alternative_to_user) ||
-       (needle->proto.ftpc.use_ssl != conn->proto.ftpc.use_ssl) ||
-       (needle->proto.ftpc.ccc != conn->proto.ftpc.ccc))
+    if(!ftp_conns_match(needle, conn))
       return FALSE;
   }
 #endif
index 39f57695fe6c47f03a098e06258f05e95b608a9e..fe502a7b2a2fb9aa212896bca8ecc71863b653fb 100644 (file)
@@ -864,9 +864,6 @@ struct connectdata {
 #endif
 
   union {
-#ifndef CURL_DISABLE_FTP
-    struct ftp_conn ftpc;
-#endif
 #ifdef USE_SSH
     struct ssh_conn sshc;
 #endif