Comments (4)
It seems you are correct; one would have to close the listening socket in addition to the accepted one. I would think about doing that in the dataaccept-call as there should always be only one connection to a PASV-port, if I remember correctly.
from lwip-ftpd.
Thanks for confirming.
I have made the following modifications, and found the problem is resolved:
struct ftpd_msgstate {
enum ftpd_state_e state;
sfifo_t fifo;
vfs_t *vfs;
struct ip4_addr dataip;
u16_t dataport;
struct tcp_pcb *datalistenpcb; // added by Mark Walters to fix a memory leak in passive FTP sessions
struct tcp_pcb *datapcb;
struct ftpd_datastate *datafs;
int passive;
char *renamefrom;
};
static void ftpd_dataclose(struct tcp_pcb *pcb, struct ftpd_datastate *fsd)
{
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
// Added by Mark Walters to a fix memory leak in passive FTP sessions
if (fsd->msgfs->datalistenpcb) {
tcp_arg(fsd->msgfs->datalistenpcb, NULL);
tcp_accept(fsd->msgfs->datalistenpcb, NULL);
tcp_close(fsd->msgfs->datalistenpcb);
fsd->msgfs->datalistenpcb = NULL;
}
fsd->msgfs->datafs = NULL;
sfifo_close(&fsd->fifo);
free(fsd);
tcp_arg(pcb, NULL);
tcp_close(pcb);
}
// Amended by Mark Walters to a fix memory leak in passive FTP sessions
static void cmd_pasv(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate *fsm)
{
static u16_t port = 4096;
static u16_t start_port = 4096;
struct tcp_pcb *temppcb;
/* Allocate memory for the structure that holds the state of the connection. */
fsm->datafs = malloc(sizeof(struct ftpd_datastate));
if (fsm->datafs == NULL) {
send_msg(pcb, fsm, msg451);
return;
}
memset(fsm->datafs, 0, sizeof(struct ftpd_datastate));
if (sfifo_init(&fsm->datafs->fifo, 2000) != 0) {
free(fsm->datafs);
send_msg(pcb, fsm, msg451);
return;
}
fsm->datalistenpcb = tcp_new();
if (fsm->datalistenpcb == NULL) {
free(fsm->datafs);
sfifo_close(&fsm->datafs->fifo);
send_msg(pcb, fsm, msg451);
return;
}
start_port = port;
while (1) {
err_t err;
if(++port > 0x7fff)
port = 4096;
fsm->dataport = port;
err = tcp_bind(fsm->datalistenpcb, (ip_addr_t*)&pcb->local_ip, fsm->dataport);
if (err == ERR_OK)
break;
if (start_port == port)
err = ERR_CLSD;
if (err == ERR_USE) {
continue;
} else {
ftpd_dataclose(fsm->datalistenpcb, fsm->datafs);
fsm->datalistenpcb = NULL;
fsm->datafs = NULL;
return;
}
}
temppcb = tcp_listen(fsm->datalistenpcb);
if (!temppcb) {
ftpd_dataclose(fsm->datalistenpcb, fsm->datafs);
fsm->datalistenpcb = NULL;
fsm->datafs = NULL;
return;
}
fsm->datalistenpcb = temppcb;
fsm->passive = 1;
fsm->datafs->connected = 0;
fsm->datafs->msgfs = fsm;
fsm->datafs->msgpcb = pcb;
/* Tell TCP that this is the structure we wish to be passed for our callbacks. */
tcp_arg(fsm->datalistenpcb, fsm->datafs);
tcp_accept(fsm->datalistenpcb, ftpd_dataaccept);
send_msg(pcb, fsm, msg227, ip4_addr1(ip_2_ip4(&pcb->local_ip)), ip4_addr2(ip_2_ip4(&pcb->local_ip)), ip4_addr3(ip_2_ip4(&pcb->local_ip)), ip4_addr4(ip_2_ip4(&pcb->local_ip)), (fsm->dataport >> 8) & 0xff, (fsm->dataport) & 0xff);
}
from lwip-ftpd.
That is great. Could you please submit a pull request?
from lwip-ftpd.
from lwip-ftpd.
Related Issues (16)
- ftpd hangs after LIST command HOT 4
- Memory leak after client quits session HOT 3
- Having problems with '0:\' drive/LUN designator HOT 10
- len wrapped rcv_wnd HOT 1
- re-use of free'd memory causes hard crash
- malloc size of pointer rather than size of struct HOT 2
- missing call to f_closedir() HOT 1
- Does not work with FileZilla on windows HOT 3
- Memory leak?
- ftpd hangs on cmd_pasv
- Trouble getting ftpd to run HOT 5
- Directory listing extremely slow HOT 5
- send_next_directory limitation HOT 4
- send_next_directory hangs the client HOT 3
- vfs_close deletes guard_for_the_whole_fs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lwip-ftpd.