|
|
@ -47,16 +47,6 @@ |
|
|
|
|
|
|
|
#include "vfs.h" |
|
|
|
|
|
|
|
#ifdef FTPD_DEBUG |
|
|
|
int dbg_printf(const char *fmt, ...); |
|
|
|
#else |
|
|
|
#ifdef _MSC_VER |
|
|
|
#define dbg_printf(x) /* x */ |
|
|
|
#else |
|
|
|
#define dbg_printf(f, ...) /* */ |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
#define EINVAL 1 |
|
|
|
#define ENOMEM 2 |
|
|
|
#define ENODEV 3 |
|
|
@ -251,8 +241,6 @@ typedef struct sfifo_t |
|
|
|
#define sfifo_used(x) (((x)->writepos - (x)->readpos) & SFIFO_SIZEMASK(x)) |
|
|
|
#define sfifo_space(x) ((x)->size - 1 - sfifo_used(x)) |
|
|
|
|
|
|
|
#define DBG(x) |
|
|
|
|
|
|
|
/*
|
|
|
|
* Alloc buffer, init FIFO etc... |
|
|
|
*/ |
|
|
@ -308,7 +296,7 @@ static int sfifo_write(sfifo_t *f, const void *_buf, int len) |
|
|
|
|
|
|
|
/* total = len = min(space, len) */ |
|
|
|
total = sfifo_space(f); |
|
|
|
DBG(dbg_printf("sfifo_space() = %d\n",total)); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("sfifo_space() = %d\n",total)); |
|
|
|
if(len > total) |
|
|
|
len = total; |
|
|
|
else |
|
|
@ -357,7 +345,7 @@ static void ftpd_dataerr(void *arg, err_t err) |
|
|
|
{ |
|
|
|
struct ftpd_datastate *fsd = arg; |
|
|
|
|
|
|
|
dbg_printf("ftpd_dataerr: %s (%i)\n", lwip_strerr(err), err); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_dataerr: %s (%i)\n", lwip_strerr(err), err)); |
|
|
|
if (fsd == NULL) |
|
|
|
return; |
|
|
|
fsd->msgfs->datafs = NULL; |
|
|
@ -405,7 +393,7 @@ static void send_data(struct tcp_pcb *pcb, struct ftpd_datastate *fsd) |
|
|
|
if ((i + len) > fsd->fifo.size) { |
|
|
|
err = tcp_write(pcb, fsd->fifo.buffer + i, (u16_t)(fsd->fifo.size - i), 1); |
|
|
|
if (err != ERR_OK) { |
|
|
|
dbg_printf("send_data: error writing!\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("send_data: error writing!\n")); |
|
|
|
return; |
|
|
|
} |
|
|
|
len -= fsd->fifo.size - i; |
|
|
@ -415,7 +403,7 @@ static void send_data(struct tcp_pcb *pcb, struct ftpd_datastate *fsd) |
|
|
|
|
|
|
|
err = tcp_write(pcb, fsd->fifo.buffer + i, len, 1); |
|
|
|
if (err != ERR_OK) { |
|
|
|
dbg_printf("send_data: error writing!\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("send_data: error writing!\n")); |
|
|
|
return; |
|
|
|
} |
|
|
|
fsd->fifo.readpos += len; |
|
|
@ -674,7 +662,7 @@ static int open_dataconnection(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) |
|
|
|
fsm->datafs = malloc(sizeof(struct ftpd_datastate)); |
|
|
|
|
|
|
|
if (fsm->datafs == NULL) { |
|
|
|
dbg_printf("open_dataconnection: Out of memory\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("open_dataconnection: Out of memory\n")); |
|
|
|
send_msg(pcb, fsm, msg451); |
|
|
|
return 1; |
|
|
|
} |
|
|
@ -889,7 +877,7 @@ static void cmd_pasv(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate |
|
|
|
fsm->datafs = malloc(sizeof(struct ftpd_datastate)); |
|
|
|
|
|
|
|
if (fsm->datafs == NULL) { |
|
|
|
dbg_printf("cmd_pasv: Out of memory\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_pasv: Out of memory\n")); |
|
|
|
send_msg(pcb, fsm, msg451); |
|
|
|
return; |
|
|
|
} |
|
|
@ -936,7 +924,7 @@ static void cmd_pasv(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate |
|
|
|
|
|
|
|
temppcb = tcp_listen(fsm->datalistenpcb); |
|
|
|
if (!temppcb) { |
|
|
|
dbg_printf("cmd_pasv: tcp_listen failed\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_pasv: tcp_listen failed\n")); |
|
|
|
ftpd_dataclose(fsm->datalistenpcb, fsm->datafs); |
|
|
|
fsm->datalistenpcb = NULL; |
|
|
|
fsm->datafs = NULL; |
|
|
@ -972,7 +960,7 @@ static void cmd_abrt(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate |
|
|
|
|
|
|
|
static void cmd_type(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) |
|
|
|
{ |
|
|
|
dbg_printf("Got TYPE -%s-\n", arg); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("Got TYPE -%s-\n", arg)); |
|
|
|
|
|
|
|
if(strcmp(arg, "I") != 0) { |
|
|
|
send_msg(pcb, fsm, msg502); |
|
|
@ -984,7 +972,7 @@ static void cmd_type(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate |
|
|
|
|
|
|
|
static void cmd_mode(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) |
|
|
|
{ |
|
|
|
dbg_printf("Got MODE -%s-\n", arg); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("Got MODE -%s-\n", arg)); |
|
|
|
send_msg(pcb, fsm, msg502); |
|
|
|
} |
|
|
|
|
|
|
@ -1002,7 +990,7 @@ static void cmd_rnfr(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate |
|
|
|
free(fsm->renamefrom); |
|
|
|
fsm->renamefrom = malloc(strlen(arg) + 1); |
|
|
|
if (fsm->renamefrom == NULL) { |
|
|
|
dbg_printf("cmd_rnfr: Out of memory\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("cmd_rnfr: Out of memory\n")); |
|
|
|
send_msg(pcb, fsm, msg451); |
|
|
|
return; |
|
|
|
} |
|
|
@ -1158,7 +1146,7 @@ static void send_msgdata(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) |
|
|
|
if ((i + len) > fsm->fifo.size) { |
|
|
|
err = tcp_write(pcb, fsm->fifo.buffer + i, (u16_t)(fsm->fifo.size - i), 1); |
|
|
|
if (err != ERR_OK) { |
|
|
|
dbg_printf("send_msgdata: error writing!\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("send_msgdata: error writing!\n")); |
|
|
|
return; |
|
|
|
} |
|
|
|
len -= fsm->fifo.size - i; |
|
|
@ -1168,7 +1156,7 @@ static void send_msgdata(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) |
|
|
|
|
|
|
|
err = tcp_write(pcb, fsm->fifo.buffer + i, len, 1); |
|
|
|
if (err != ERR_OK) { |
|
|
|
dbg_printf("send_msgdata: error writing!\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("send_msgdata: error writing!\n")); |
|
|
|
return; |
|
|
|
} |
|
|
|
fsm->fifo.readpos += len; |
|
|
@ -1189,7 +1177,7 @@ static void send_msg(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm, char *msg, |
|
|
|
if (sfifo_space(&fsm->fifo) < len) |
|
|
|
return; |
|
|
|
sfifo_write(&fsm->fifo, buffer, len); |
|
|
|
dbg_printf("response: %s", buffer); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("response: %s", buffer)); |
|
|
|
send_msgdata(pcb, fsm); |
|
|
|
} |
|
|
|
|
|
|
@ -1197,7 +1185,7 @@ static void ftpd_msgerr(void *arg, err_t err) |
|
|
|
{ |
|
|
|
struct ftpd_msgstate *fsm = arg; |
|
|
|
|
|
|
|
dbg_printf("ftpd_msgerr: %s (%i)\n", lwip_strerr(err), err); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_msgerr: %s (%i)\n", lwip_strerr(err), err)); |
|
|
|
if (fsm == NULL) |
|
|
|
return; |
|
|
|
if (fsm->datafs) |
|
|
@ -1269,7 +1257,7 @@ static err_t ftpd_msgrecv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t |
|
|
|
while (((*pt == '\r') || (*pt == '\n')) && pt >= text) |
|
|
|
*pt-- = '\0'; |
|
|
|
|
|
|
|
dbg_printf("query: %s\n", text); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("query: %s\n", text)); |
|
|
|
|
|
|
|
strncpy(cmd, text, 4); |
|
|
|
for (pt = cmd; isalpha(*pt) && pt < &cmd[4]; pt++) |
|
|
@ -1331,6 +1319,7 @@ static err_t ftpd_msgpoll(void *arg, struct tcp_pcb *pcb) |
|
|
|
|
|
|
|
static err_t ftpd_msgaccept(void *arg, struct tcp_pcb *pcb, err_t err) |
|
|
|
{ |
|
|
|
LWIP_PLATFORM_DIAG(("ftpd_msgaccept called")); |
|
|
|
struct ftpd_msgstate *fsm; |
|
|
|
|
|
|
|
/* Allocate memory for the structure that holds the state of the
|
|
|
@ -1338,7 +1327,7 @@ static err_t ftpd_msgaccept(void *arg, struct tcp_pcb *pcb, err_t err) |
|
|
|
fsm = malloc(sizeof(struct ftpd_msgstate)); |
|
|
|
|
|
|
|
if (fsm == NULL) { |
|
|
|
dbg_printf("ftpd_msgaccept: Out of memory\n"); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_msgaccept: Out of memory\n")); |
|
|
|
return ERR_MEM; |
|
|
|
} |
|
|
|
memset(fsm, 0, sizeof(struct ftpd_msgstate)); |
|
|
@ -1384,7 +1373,10 @@ void ftpd_init(void) |
|
|
|
vfs_load_plugin(vfs_default_fs); |
|
|
|
|
|
|
|
pcb = tcp_new(); |
|
|
|
tcp_bind(pcb, IP_ADDR_ANY, 21); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_init: pcb: %x\n", pcb)); |
|
|
|
int r = tcp_bind(pcb, IP_ADDR_ANY, 21); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_init: tcp_bind: %d\n", r)); |
|
|
|
pcb = tcp_listen(pcb); |
|
|
|
LWIP_DEBUGF(FTPD_DEBUG, ("ftpd_init: listen-pcb: %x\n", pcb)); |
|
|
|
tcp_accept(pcb, ftpd_msgaccept); |
|
|
|
} |
|
|
|