mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2025-01-12 21:41:28 +00:00
Reformat tftp_server.c using astylerc
This commit is contained in:
parent
2ab73ad572
commit
6e7fe4520a
@ -12,7 +12,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification,are permitted provided that the following conditions are met:
|
* modification,are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
@ -92,7 +92,7 @@ struct tftp_state {
|
|||||||
|
|
||||||
static struct tftp_state tftp_state;
|
static struct tftp_state tftp_state;
|
||||||
|
|
||||||
static void tftp_tmr(void* arg);
|
static void tftp_tmr(void *arg);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
close_handle(void)
|
close_handle(void)
|
||||||
@ -100,13 +100,13 @@ close_handle(void)
|
|||||||
tftp_state.port = 0;
|
tftp_state.port = 0;
|
||||||
ip_addr_set_any(0, &tftp_state.addr);
|
ip_addr_set_any(0, &tftp_state.addr);
|
||||||
|
|
||||||
if(tftp_state.last_data != NULL) {
|
if (tftp_state.last_data != NULL) {
|
||||||
pbuf_free(tftp_state.last_data);
|
pbuf_free(tftp_state.last_data);
|
||||||
tftp_state.last_data = NULL;
|
tftp_state.last_data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_untimeout(tftp_tmr, NULL);
|
sys_untimeout(tftp_tmr, NULL);
|
||||||
|
|
||||||
if (tftp_state.handle) {
|
if (tftp_state.handle) {
|
||||||
tftp_state.ctx->close(tftp_state.handle);
|
tftp_state.ctx->close(tftp_state.handle);
|
||||||
tftp_state.handle = NULL;
|
tftp_state.handle = NULL;
|
||||||
@ -118,15 +118,15 @@ static void
|
|||||||
send_error(const ip_addr_t *addr, u16_t port, enum tftp_error code, const char *str)
|
send_error(const ip_addr_t *addr, u16_t port, enum tftp_error code, const char *str)
|
||||||
{
|
{
|
||||||
int str_length = strlen(str);
|
int str_length = strlen(str);
|
||||||
struct pbuf* p;
|
struct pbuf *p;
|
||||||
u16_t* payload;
|
u16_t *payload;
|
||||||
|
|
||||||
p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(TFTP_HEADER_LENGTH + str_length + 1), PBUF_RAM);
|
p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(TFTP_HEADER_LENGTH + str_length + 1), PBUF_RAM);
|
||||||
if(p == NULL) {
|
if (p == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
payload = (u16_t*) p->payload;
|
payload = (u16_t *) p->payload;
|
||||||
payload[0] = PP_HTONS(TFTP_ERROR);
|
payload[0] = PP_HTONS(TFTP_ERROR);
|
||||||
payload[1] = lwip_htons(code);
|
payload[1] = lwip_htons(code);
|
||||||
MEMCPY(&payload[2], str, str_length + 1);
|
MEMCPY(&payload[2], str, str_length + 1);
|
||||||
@ -138,15 +138,15 @@ send_error(const ip_addr_t *addr, u16_t port, enum tftp_error code, const char *
|
|||||||
static void
|
static void
|
||||||
send_ack(u16_t blknum)
|
send_ack(u16_t blknum)
|
||||||
{
|
{
|
||||||
struct pbuf* p;
|
struct pbuf *p;
|
||||||
u16_t* payload;
|
u16_t *payload;
|
||||||
|
|
||||||
p = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH, PBUF_RAM);
|
p = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH, PBUF_RAM);
|
||||||
if(p == NULL) {
|
if (p == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
payload = (u16_t*) p->payload;
|
payload = (u16_t *) p->payload;
|
||||||
|
|
||||||
payload[0] = PP_HTONS(TFTP_ACK);
|
payload[0] = PP_HTONS(TFTP_ACK);
|
||||||
payload[1] = lwip_htons(blknum);
|
payload[1] = lwip_htons(blknum);
|
||||||
udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
|
udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
|
||||||
@ -157,15 +157,15 @@ static void
|
|||||||
resend_data(void)
|
resend_data(void)
|
||||||
{
|
{
|
||||||
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, tftp_state.last_data->len, PBUF_RAM);
|
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, tftp_state.last_data->len, PBUF_RAM);
|
||||||
if(p == NULL) {
|
if (p == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(pbuf_copy(p, tftp_state.last_data) != ERR_OK) {
|
if (pbuf_copy(p, tftp_state.last_data) != ERR_OK) {
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
|
udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
}
|
}
|
||||||
@ -176,12 +176,12 @@ send_data(void)
|
|||||||
u16_t *payload;
|
u16_t *payload;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if(tftp_state.last_data != NULL) {
|
if (tftp_state.last_data != NULL) {
|
||||||
pbuf_free(tftp_state.last_data);
|
pbuf_free(tftp_state.last_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
tftp_state.last_data = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH + TFTP_MAX_PAYLOAD_SIZE, PBUF_RAM);
|
tftp_state.last_data = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH + TFTP_MAX_PAYLOAD_SIZE, PBUF_RAM);
|
||||||
if(tftp_state.last_data == NULL) {
|
if (tftp_state.last_data == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
|
|
||||||
LWIP_UNUSED_ARG(arg);
|
LWIP_UNUSED_ARG(arg);
|
||||||
LWIP_UNUSED_ARG(upcb);
|
LWIP_UNUSED_ARG(upcb);
|
||||||
|
|
||||||
if (((tftp_state.port != 0) && (port != tftp_state.port)) ||
|
if (((tftp_state.port != 0) && (port != tftp_state.port)) ||
|
||||||
(!ip_addr_isany_val(tftp_state.addr) && !ip_addr_cmp(&tftp_state.addr, addr))) {
|
(!ip_addr_isany_val(tftp_state.addr) && !ip_addr_cmp(&tftp_state.addr, addr))) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
|
||||||
@ -223,37 +223,36 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
|
|
||||||
switch (opcode) {
|
switch (opcode) {
|
||||||
case PP_HTONS(TFTP_RRQ): /* fall through */
|
case PP_HTONS(TFTP_RRQ): /* fall through */
|
||||||
case PP_HTONS(TFTP_WRQ):
|
case PP_HTONS(TFTP_WRQ): {
|
||||||
{
|
|
||||||
const char tftp_null = 0;
|
const char tftp_null = 0;
|
||||||
char filename[TFTP_MAX_FILENAME_LEN+1];
|
char filename[TFTP_MAX_FILENAME_LEN + 1];
|
||||||
char mode[TFTP_MAX_MODE_LEN+1];
|
char mode[TFTP_MAX_MODE_LEN + 1];
|
||||||
u16_t filename_end_offset;
|
u16_t filename_end_offset;
|
||||||
u16_t mode_end_offset;
|
u16_t mode_end_offset;
|
||||||
|
|
||||||
if(tftp_state.handle != NULL) {
|
if (tftp_state.handle != NULL) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Only one connection at a time is supported");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
|
sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
|
||||||
|
|
||||||
/* find \0 in pbuf -> end of filename string */
|
/* find \0 in pbuf -> end of filename string */
|
||||||
filename_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), 2);
|
filename_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), 2);
|
||||||
if((u16_t)(filename_end_offset-1) > sizeof(filename)) {
|
if ((u16_t)(filename_end_offset - 1) > sizeof(filename)) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Filename too long/not NULL terminated");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Filename too long/not NULL terminated");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pbuf_copy_partial(p, filename, filename_end_offset-1, 2);
|
pbuf_copy_partial(p, filename, filename_end_offset - 1, 2);
|
||||||
|
|
||||||
/* find \0 in pbuf -> end of mode string */
|
/* find \0 in pbuf -> end of mode string */
|
||||||
mode_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), filename_end_offset+1);
|
mode_end_offset = pbuf_memfind(p, &tftp_null, sizeof(tftp_null), filename_end_offset + 1);
|
||||||
if((u16_t)(mode_end_offset-filename_end_offset) > sizeof(mode)) {
|
if ((u16_t)(mode_end_offset - filename_end_offset) > sizeof(mode)) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Mode too long/not NULL terminated");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Mode too long/not NULL terminated");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pbuf_copy_partial(p, mode, mode_end_offset-filename_end_offset, filename_end_offset+1);
|
pbuf_copy_partial(p, mode, mode_end_offset - filename_end_offset, filename_end_offset + 1);
|
||||||
|
|
||||||
tftp_state.handle = tftp_state.ctx->open(filename, mode, opcode == PP_HTONS(TFTP_WRQ));
|
tftp_state.handle = tftp_state.ctx->open(filename, mode, opcode == PP_HTONS(TFTP_WRQ));
|
||||||
tftp_state.blknum = 1;
|
tftp_state.blknum = 1;
|
||||||
|
|
||||||
@ -279,12 +278,11 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case PP_HTONS(TFTP_DATA):
|
case PP_HTONS(TFTP_DATA): {
|
||||||
{
|
|
||||||
int ret;
|
int ret;
|
||||||
u16_t blknum;
|
u16_t blknum;
|
||||||
|
|
||||||
if (tftp_state.handle == NULL) {
|
if (tftp_state.handle == NULL) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "No connection");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "No connection");
|
||||||
break;
|
break;
|
||||||
@ -312,8 +310,7 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case PP_HTONS(TFTP_ACK):
|
case PP_HTONS(TFTP_ACK): {
|
||||||
{
|
|
||||||
u16_t blknum;
|
u16_t blknum;
|
||||||
int lastpkt;
|
int lastpkt;
|
||||||
|
|
||||||
@ -348,7 +345,7 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
send_error(addr, port, TFTP_ERROR_ILLEGAL_OPERATION, "Unknown operation");
|
send_error(addr, port, TFTP_ERROR_ILLEGAL_OPERATION, "Unknown operation");
|
||||||
break;
|
break;
|
||||||
@ -358,10 +355,10 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
tftp_tmr(void* arg)
|
tftp_tmr(void *arg)
|
||||||
{
|
{
|
||||||
LWIP_UNUSED_ARG(arg);
|
LWIP_UNUSED_ARG(arg);
|
||||||
|
|
||||||
tftp_state.timer++;
|
tftp_state.timer++;
|
||||||
|
|
||||||
if (tftp_state.handle == NULL) {
|
if (tftp_state.handle == NULL) {
|
||||||
@ -386,7 +383,7 @@ tftp_tmr(void* arg)
|
|||||||
* Initialize TFTP server.
|
* Initialize TFTP server.
|
||||||
* @param ctx TFTP callback struct
|
* @param ctx TFTP callback struct
|
||||||
*/
|
*/
|
||||||
err_t
|
err_t
|
||||||
tftp_init(const struct tftp_context *ctx)
|
tftp_init(const struct tftp_context *ctx)
|
||||||
{
|
{
|
||||||
err_t ret;
|
err_t ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user