GithubHelp home page GithubHelp logo

Passive Mode about lwip-ftpd HOT 4 CLOSED

toelke avatar toelke commented on August 15, 2024
Passive Mode

from lwip-ftpd.

Comments (4)

toelke avatar toelke commented on August 15, 2024

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.

mwalters1024 avatar mwalters1024 commented on August 15, 2024

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.

toelke avatar toelke commented on August 15, 2024

That is great. Could you please submit a pull request?

from lwip-ftpd.

mwalters1024 avatar mwalters1024 commented on August 15, 2024

from lwip-ftpd.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.