ftp: use easy handle and connectin meta data for protocol structs

- remove data->req.p.ftp and store `struct FTP` as easy meta data
- place `struct ftp_conn` instance in connection meta data

Closes #17249
This commit is contained in:
Stefan Eissing 2025-05-05 13:31:14 +02:00 committed by Daniel Stenberg
parent 378aa011e6
commit a2d90d4ba5
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
7 changed files with 456 additions and 377 deletions

789
lib/ftp.c

File diff suppressed because it is too large Load diff

View file

@ -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 */

View 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 */

View 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);

View 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;

View file

@ -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

View 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