mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2025-04-15 23:42:33 +00:00
Improve TFTP implementation
- implement server/client mode (API is nicer to use) - Increase TFTP_MAX_MODE_LEN to be able to contain "netascii" transfer mode - Adapt tftp_example to changes
This commit is contained in:
parent
de81e8bf19
commit
6c2fd2d25d
@ -32,6 +32,8 @@
|
||||
#include "lwip/apps/tftp_server.h"
|
||||
#include "tftp_example.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if LWIP_UDP
|
||||
|
||||
static void*
|
||||
@ -75,17 +77,31 @@ tftp_write(void* handle, struct pbuf* p)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tftp_error(void* handle, int err, const char* msg, int size)
|
||||
{
|
||||
char message[100];
|
||||
|
||||
memset(message, 0, sizeof(message));
|
||||
MEMCPY(message, msg, LWIP_MIN(sizeof(message)-1, size));
|
||||
|
||||
printf("TFTP error: %d (%s)", err, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct tftp_context tftp = {
|
||||
tftp_open,
|
||||
tftp_close,
|
||||
tftp_read,
|
||||
tftp_write
|
||||
tftp_write,
|
||||
tftp_error
|
||||
};
|
||||
|
||||
void
|
||||
tftp_example_init(void)
|
||||
{
|
||||
tftp_init(&tftp);
|
||||
tftp_init_server(&tftp);
|
||||
}
|
||||
|
||||
#endif /* LWIP_UDP */
|
||||
|
@ -91,6 +91,7 @@ struct tftp_state {
|
||||
};
|
||||
|
||||
static struct tftp_state tftp_state;
|
||||
static u8_t tftp_mode;
|
||||
|
||||
static void tftp_tmr(void *arg);
|
||||
|
||||
@ -261,6 +262,11 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
||||
break;
|
||||
}
|
||||
|
||||
if ((tftp_mode & LWIP_TFTP_MODE_SERVER) == 0) {
|
||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "TFTP server not enabled");
|
||||
break;
|
||||
}
|
||||
|
||||
sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
|
||||
|
||||
/* find \0 in pbuf -> end of filename string */
|
||||
@ -420,12 +426,13 @@ tftp_tmr(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
/** @ingroup tftp
|
||||
/**
|
||||
* Initialize TFTP client/server.
|
||||
* @param mode TFTP mode (client/server)
|
||||
* @param ctx TFTP callback struct
|
||||
*/
|
||||
err_t
|
||||
tftp_init(const struct tftp_context *ctx)
|
||||
tftp_init_common(u8_t mode, const struct tftp_context *ctx)
|
||||
{
|
||||
err_t ret;
|
||||
|
||||
@ -435,6 +442,8 @@ tftp_init(const struct tftp_context *ctx)
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
tftp_mode = mode;
|
||||
|
||||
ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT);
|
||||
if (ret != ERR_OK) {
|
||||
udp_remove(pcb);
|
||||
@ -453,6 +462,26 @@ tftp_init(const struct tftp_context *ctx)
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
/** @ingroup tftp
|
||||
* Initialize TFTP server.
|
||||
* @param ctx TFTP callback struct
|
||||
*/
|
||||
err_t
|
||||
tftp_init_server(const struct tftp_context *ctx)
|
||||
{
|
||||
return tftp_init_common(LWIP_TFTP_MODE_SERVER, ctx);
|
||||
}
|
||||
|
||||
/** @ingroup tftp
|
||||
* Initialize TFTP client.
|
||||
* @param ctx TFTP callback struct
|
||||
*/
|
||||
err_t
|
||||
tftp_init_client(u8_t mode, const struct tftp_context *ctx)
|
||||
{
|
||||
return tftp_init_common(LWIP_TFTP_MODE_CLIENT, ctx);
|
||||
}
|
||||
|
||||
/** @ingroup tftp
|
||||
* Deinitialize ("turn off") TFTP client/server.
|
||||
*/
|
||||
@ -465,7 +494,10 @@ void tftp_cleanup(void)
|
||||
}
|
||||
|
||||
err_t
|
||||
tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode) {
|
||||
tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode)
|
||||
{
|
||||
LWIP_ERROR("TFTP client is not enabled (tftp_init)", (tftp_mode & LWIP_TFTP_MODE_CLIENT) == 0, return ERR_VAL);
|
||||
|
||||
tftp_state.handle = handle;
|
||||
tftp_state.blknum = 1;
|
||||
tftp_state.mode_write = 1; // We want to receive data
|
||||
@ -474,7 +506,10 @@ tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, con
|
||||
}
|
||||
|
||||
err_t
|
||||
tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode) {
|
||||
tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode)
|
||||
{
|
||||
LWIP_ERROR("TFTP client is not enabled (tftp_init)", (tftp_mode & LWIP_TFTP_MODE_CLIENT) == 0, return ERR_VAL);
|
||||
|
||||
tftp_state.handle = handle;
|
||||
tftp_state.blknum = 1;
|
||||
tftp_state.mode_write = 0; // We want to send data
|
||||
|
@ -37,6 +37,7 @@
|
||||
|
||||
#include "lwip/apps/tftp_common.h"
|
||||
|
||||
err_t tftp_init_client(const struct tftp_context* ctx);
|
||||
err_t tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode);
|
||||
err_t tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode);
|
||||
|
||||
|
@ -94,7 +94,11 @@ struct tftp_context {
|
||||
void (*error)(void* handle, int err, const char* msg, int size);
|
||||
};
|
||||
|
||||
err_t tftp_init(const struct tftp_context* ctx);
|
||||
#define LWIP_TFTP_MODE_SERVER 0x01
|
||||
#define LWIP_TFTP_MODE_CLIENT 0x02
|
||||
#define LWIP_TFTP_MODE_CLIENTSERVER (LWIP_TFTP_MODE_SERVER | LWIP_TFTP_MODE_CLIENT)
|
||||
|
||||
err_t tftp_init_common(u8_t mode, const struct tftp_context* ctx);
|
||||
void tftp_cleanup(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -96,7 +96,7 @@
|
||||
* Max. length of TFTP mode
|
||||
*/
|
||||
#if !defined TFTP_MAX_MODE_LEN || defined __DOXYGEN__
|
||||
#define TFTP_MAX_MODE_LEN 7
|
||||
#define TFTP_MAX_MODE_LEN 10
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -37,4 +37,6 @@
|
||||
|
||||
#include "lwip/apps/tftp_common.h"
|
||||
|
||||
err_t tftp_init_server(const struct tftp_context* ctx);
|
||||
|
||||
#endif /* LWIP_HDR_APPS_TFTP_SERVER_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user