Code: Select all
static err_t ftp_data_open(ftp_session_t *s, struct pbuf *p)
{
err_t error;
char *ptr;
ip4_addr_t data_server;
u16_t data_port;
// Find server connection parameter
ptr = strchr(p->payload, '(');
if (!ptr) return ERR_BUF;
do {
unsigned int a = strtoul(ptr+1,&ptr,10);
unsigned int b = strtoul(ptr+1,&ptr,10);
unsigned int c = strtoul(ptr+1,&ptr,10);
unsigned int d = strtoul(ptr+1,&ptr,10);
IP4_ADDR(&data_server,a,b,c,d);
} while(0);
data_port = strtoul(ptr+1,&ptr,10) << 8;
data_port |= strtoul(ptr+1,&ptr,10) & 255;
if (*ptr!=')') return ERR_BUF;
// Open data session
tcp_arg(s->data_pcb, s);
tcp_err(s->data_pcb, ftp_data_err);
tcp_recv(s->data_pcb, ftp_data_recv);
tcp_sent(s->data_pcb, ftp_data_sent);
error = tcp_connect(s->data_pcb, (const ip_addr_t *)&data_server, data_port, ftp_data_connected);
return error;
}
Code: Select all
if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
return ERR_VAL;
}
I use ESP32 to build an ftpclient for OTA upgrade, and use PASV anonymous mode. In my code, I can connect the control link, but there is a mistake on the connection of the data link. By the way, my control link is also applicable to this function. There is no problem. It is very strange.
This is the code about controls link
Code: Select all
err_t ftp_connect(ftp_session_t *s)
{
err_t error;
enum ftp_results retval = FTP_RESULT_ERR_UNKNOWN;
// Check user supplied data
if ( (s->control_state!=FTP_CLOSED) ||
s->control_pcb ||
s->data_pcb ||
!s->user ||
!s->pass )
{
ESP_LOGI(TAG, "ftp:invalid control session\n");
retval = FTP_RESULT_ERR_ARGUMENT;
goto exit;
}
// Get sessions pcb
s->control_pcb = tcp_new();
if (!s->control_pcb) {
ESP_LOGI(TAG, "ftp:cannot alloc control_pcb (low memory?)\n");
retval = FTP_RESULT_ERR_MEMORY;
goto exit;
}
// Open control session
tcp_arg(s->control_pcb, s);
tcp_err(s->control_pcb, ftp_control_err);
tcp_recv(s->control_pcb, ftp_control_recv);
tcp_sent(s->control_pcb, ftp_control_sent);
error = tcp_connect(s->control_pcb, (const ip_addr_t *)&s->server_ip, s->server_port, ftp_control_connected);
if ( error == ERR_OK ) {
retval = FTP_RESULT_INPROGRESS;
return retval;
}
// Release pcbs in case of failure
ESP_LOGI(TAG, "ftp:cannot connect control_pcb (%s)\n", lwip_strerr(error));
ftp_control_close(s, -1);
exit:
if (s->done_fn) s->done_fn(s->handle, retval);
return retval;
}