mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2025-03-28 19:21:20 +00:00
Try to fix line endings of tftp_server.c in git rep
This commit is contained in:
parent
754e49643f
commit
1f1f2e1c46
@ -1,417 +1,417 @@
|
|||||||
/*******************************************************************
|
/*******************************************************************
|
||||||
*
|
*
|
||||||
* @file tftp_server.c
|
* @file tftp_server.c
|
||||||
*
|
*
|
||||||
* @author Logan Gunthorpe <logang@deltatee.com>
|
* @author Logan Gunthorpe <logang@deltatee.com>
|
||||||
* Dirk Ziegelmeier <dziegel@gmx.de>
|
* Dirk Ziegelmeier <dziegel@gmx.de>
|
||||||
*
|
*
|
||||||
* @brief Trivial File Transfer Protocol (RFC 1350)
|
* @brief Trivial File Transfer Protocol (RFC 1350)
|
||||||
*
|
*
|
||||||
* Copyright (c) Deltatee Enterprises Ltd. 2013
|
* Copyright (c) Deltatee Enterprises Ltd. 2013
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
********************************************************************/
|
********************************************************************/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
* 3. The name of the author may not be used to endorse or promote products
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
* derived from this software without specific prior written permission.
|
* derived from this software without specific prior written permission.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||||
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* Author: Logan Gunthorpe <logang@deltatee.com>
|
* Author: Logan Gunthorpe <logang@deltatee.com>
|
||||||
* Dirk Ziegelmeier <dziegel@gmx.de>
|
* Dirk Ziegelmeier <dziegel@gmx.de>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup tftp TFTP server
|
* @defgroup tftp TFTP server
|
||||||
* @ingroup apps
|
* @ingroup apps
|
||||||
*
|
*
|
||||||
* This is simple TFTP server for the lwIP raw API.
|
* This is simple TFTP server for the lwIP raw API.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "lwip/apps/tftp_server.h"
|
#include "lwip/apps/tftp_server.h"
|
||||||
|
|
||||||
#if LWIP_UDP
|
#if LWIP_UDP
|
||||||
|
|
||||||
#include "lwip/udp.h"
|
#include "lwip/udp.h"
|
||||||
#include "lwip/timeouts.h"
|
#include "lwip/timeouts.h"
|
||||||
#include "lwip/debug.h"
|
#include "lwip/debug.h"
|
||||||
|
|
||||||
#define TFTP_MAX_PAYLOAD_SIZE 512
|
#define TFTP_MAX_PAYLOAD_SIZE 512
|
||||||
#define TFTP_HEADER_LENGTH 4
|
#define TFTP_HEADER_LENGTH 4
|
||||||
|
|
||||||
#define TFTP_RRQ 1
|
#define TFTP_RRQ 1
|
||||||
#define TFTP_WRQ 2
|
#define TFTP_WRQ 2
|
||||||
#define TFTP_DATA 3
|
#define TFTP_DATA 3
|
||||||
#define TFTP_ACK 4
|
#define TFTP_ACK 4
|
||||||
#define TFTP_ERROR 5
|
#define TFTP_ERROR 5
|
||||||
|
|
||||||
enum tftp_error {
|
enum tftp_error {
|
||||||
TFTP_ERROR_FILE_NOT_FOUND = 1,
|
TFTP_ERROR_FILE_NOT_FOUND = 1,
|
||||||
TFTP_ERROR_ACCESS_VIOLATION = 2,
|
TFTP_ERROR_ACCESS_VIOLATION = 2,
|
||||||
TFTP_ERROR_DISK_FULL = 3,
|
TFTP_ERROR_DISK_FULL = 3,
|
||||||
TFTP_ERROR_ILLEGAL_OPERATION = 4,
|
TFTP_ERROR_ILLEGAL_OPERATION = 4,
|
||||||
TFTP_ERROR_UNKNOWN_TRFR_ID = 5,
|
TFTP_ERROR_UNKNOWN_TRFR_ID = 5,
|
||||||
TFTP_ERROR_FILE_EXISTS = 6,
|
TFTP_ERROR_FILE_EXISTS = 6,
|
||||||
TFTP_ERROR_NO_SUCH_USER = 7
|
TFTP_ERROR_NO_SUCH_USER = 7
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct tftp_state {
|
struct tftp_state {
|
||||||
const struct tftp_context *ctx;
|
const struct tftp_context *ctx;
|
||||||
void *handle;
|
void *handle;
|
||||||
struct pbuf *last_data;
|
struct pbuf *last_data;
|
||||||
struct udp_pcb *upcb;
|
struct udp_pcb *upcb;
|
||||||
ip_addr_t addr;
|
ip_addr_t addr;
|
||||||
u16_t port;
|
u16_t port;
|
||||||
int timer;
|
int timer;
|
||||||
int last_pkt;
|
int last_pkt;
|
||||||
u16_t blknum;
|
u16_t blknum;
|
||||||
u8_t retries;
|
u8_t retries;
|
||||||
u8_t mode_write;
|
u8_t mode_write;
|
||||||
};
|
};
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
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;
|
||||||
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: closing\n"));
|
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: closing\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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);
|
||||||
|
|
||||||
udp_sendto(tftp_state.upcb, p, addr, port);
|
udp_sendto(tftp_state.upcb, p, addr, port);
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
send_data(void)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
payload = (u16_t *) tftp_state.last_data->payload;
|
payload = (u16_t *) tftp_state.last_data->payload;
|
||||||
payload[0] = PP_HTONS(TFTP_DATA);
|
payload[0] = PP_HTONS(TFTP_DATA);
|
||||||
payload[1] = lwip_htons(tftp_state.blknum);
|
payload[1] = lwip_htons(tftp_state.blknum);
|
||||||
|
|
||||||
ret = tftp_state.ctx->read(tftp_state.handle, &payload[2], TFTP_MAX_PAYLOAD_SIZE);
|
ret = tftp_state.ctx->read(tftp_state.handle, &payload[2], TFTP_MAX_PAYLOAD_SIZE);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
send_error(&tftp_state.addr, tftp_state.port, TFTP_ERROR_ACCESS_VIOLATION, "Error occured while reading the file.");
|
send_error(&tftp_state.addr, tftp_state.port, TFTP_ERROR_ACCESS_VIOLATION, "Error occured while reading the file.");
|
||||||
close_handle();
|
close_handle();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pbuf_realloc(tftp_state.last_data, (u16_t)(TFTP_HEADER_LENGTH + ret));
|
pbuf_realloc(tftp_state.last_data, (u16_t)(TFTP_HEADER_LENGTH + ret));
|
||||||
resend_data();
|
resend_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
|
||||||
{
|
{
|
||||||
u16_t *sbuf = (u16_t *) p->payload;
|
u16_t *sbuf = (u16_t *) p->payload;
|
||||||
int opcode;
|
int opcode;
|
||||||
|
|
||||||
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");
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
opcode = sbuf[0];
|
opcode = sbuf[0];
|
||||||
|
|
||||||
tftp_state.last_pkt = tftp_state.timer;
|
tftp_state.last_pkt = tftp_state.timer;
|
||||||
tftp_state.retries = 0;
|
tftp_state.retries = 0;
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (!tftp_state.handle) {
|
if (!tftp_state.handle) {
|
||||||
send_error(addr, port, TFTP_ERROR_FILE_NOT_FOUND, "Unable to open requested file.");
|
send_error(addr, port, TFTP_ERROR_FILE_NOT_FOUND, "Unable to open requested file.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: %s request from ", (opcode == PP_HTONS(TFTP_WRQ)) ? "write" : "read"));
|
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: %s request from ", (opcode == PP_HTONS(TFTP_WRQ)) ? "write" : "read"));
|
||||||
ip_addr_debug_print(TFTP_DEBUG | LWIP_DBG_STATE, addr);
|
ip_addr_debug_print(TFTP_DEBUG | LWIP_DBG_STATE, addr);
|
||||||
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, (" for '%s' mode '%s'\n", filename, mode));
|
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, (" for '%s' mode '%s'\n", filename, mode));
|
||||||
|
|
||||||
ip_addr_copy(tftp_state.addr, *addr);
|
ip_addr_copy(tftp_state.addr, *addr);
|
||||||
tftp_state.port = port;
|
tftp_state.port = port;
|
||||||
|
|
||||||
if (opcode == PP_HTONS(TFTP_WRQ)) {
|
if (opcode == PP_HTONS(TFTP_WRQ)) {
|
||||||
tftp_state.mode_write = 1;
|
tftp_state.mode_write = 1;
|
||||||
send_ack(0);
|
send_ack(0);
|
||||||
} else {
|
} else {
|
||||||
tftp_state.mode_write = 0;
|
tftp_state.mode_write = 0;
|
||||||
send_data();
|
send_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tftp_state.mode_write != 1) {
|
if (tftp_state.mode_write != 1) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a write connection");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a write connection");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
blknum = lwip_ntohs(sbuf[1]);
|
blknum = lwip_ntohs(sbuf[1]);
|
||||||
pbuf_header(p, -TFTP_HEADER_LENGTH);
|
pbuf_header(p, -TFTP_HEADER_LENGTH);
|
||||||
|
|
||||||
ret = tftp_state.ctx->write(tftp_state.handle, p);
|
ret = tftp_state.ctx->write(tftp_state.handle, p);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "error writing file");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "error writing file");
|
||||||
close_handle();
|
close_handle();
|
||||||
} else {
|
} else {
|
||||||
send_ack(blknum);
|
send_ack(blknum);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p->tot_len < TFTP_MAX_PAYLOAD_SIZE) {
|
if (p->tot_len < TFTP_MAX_PAYLOAD_SIZE) {
|
||||||
close_handle();
|
close_handle();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case PP_HTONS(TFTP_ACK):
|
case PP_HTONS(TFTP_ACK):
|
||||||
{
|
{
|
||||||
u16_t blknum;
|
u16_t blknum;
|
||||||
int lastpkt;
|
int lastpkt;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tftp_state.mode_write != 0) {
|
if (tftp_state.mode_write != 0) {
|
||||||
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a read connection");
|
send_error(addr, port, TFTP_ERROR_ACCESS_VIOLATION, "Not a read connection");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
blknum = lwip_ntohs(sbuf[1]);
|
blknum = lwip_ntohs(sbuf[1]);
|
||||||
if (blknum != tftp_state.blknum) {
|
if (blknum != tftp_state.blknum) {
|
||||||
send_error(addr, port, TFTP_ERROR_UNKNOWN_TRFR_ID, "Wrong block number");
|
send_error(addr, port, TFTP_ERROR_UNKNOWN_TRFR_ID, "Wrong block number");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
lastpkt = 0;
|
lastpkt = 0;
|
||||||
|
|
||||||
if (tftp_state.last_data != NULL) {
|
if (tftp_state.last_data != NULL) {
|
||||||
lastpkt = tftp_state.last_data->tot_len != (TFTP_MAX_PAYLOAD_SIZE + TFTP_HEADER_LENGTH);
|
lastpkt = tftp_state.last_data->tot_len != (TFTP_MAX_PAYLOAD_SIZE + TFTP_HEADER_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lastpkt) {
|
if (!lastpkt) {
|
||||||
tftp_state.blknum++;
|
tftp_state.blknum++;
|
||||||
send_data();
|
send_data();
|
||||||
} else {
|
} else {
|
||||||
close_handle();
|
close_handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
|
sys_timeout(TFTP_TIMER_MSECS, tftp_tmr, NULL);
|
||||||
|
|
||||||
if ((tftp_state.timer - tftp_state.last_pkt) > (TFTP_TIMEOUT_MSECS / TFTP_TIMER_MSECS)) {
|
if ((tftp_state.timer - tftp_state.last_pkt) > (TFTP_TIMEOUT_MSECS / TFTP_TIMER_MSECS)) {
|
||||||
if ((tftp_state.last_data != NULL) && (tftp_state.retries < TFTP_MAX_RETRIES)) {
|
if ((tftp_state.last_data != NULL) && (tftp_state.retries < TFTP_MAX_RETRIES)) {
|
||||||
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout, retrying\n"));
|
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout, retrying\n"));
|
||||||
resend_data();
|
resend_data();
|
||||||
tftp_state.retries++;
|
tftp_state.retries++;
|
||||||
} else {
|
} else {
|
||||||
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout\n"));
|
LWIP_DEBUGF(TFTP_DEBUG | LWIP_DBG_STATE, ("tftp: timeout\n"));
|
||||||
close_handle();
|
close_handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @ingroup tftp
|
/** @ingroup tftp
|
||||||
* 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;
|
||||||
|
|
||||||
struct udp_pcb *pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
struct udp_pcb *pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||||
if (pcb == NULL) {
|
if (pcb == NULL) {
|
||||||
return ERR_MEM;
|
return ERR_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT);
|
ret = udp_bind(pcb, IP_ANY_TYPE, TFTP_PORT);
|
||||||
if (ret != ERR_OK) {
|
if (ret != ERR_OK) {
|
||||||
udp_remove(pcb);
|
udp_remove(pcb);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
tftp_state.handle = NULL;
|
tftp_state.handle = NULL;
|
||||||
tftp_state.port = 0;
|
tftp_state.port = 0;
|
||||||
tftp_state.ctx = ctx;
|
tftp_state.ctx = ctx;
|
||||||
tftp_state.timer = 0;
|
tftp_state.timer = 0;
|
||||||
tftp_state.last_data = NULL;
|
tftp_state.last_data = NULL;
|
||||||
tftp_state.upcb = pcb;
|
tftp_state.upcb = pcb;
|
||||||
|
|
||||||
udp_recv(pcb, recv, NULL);
|
udp_recv(pcb, recv, NULL);
|
||||||
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* LWIP_UDP */
|
#endif /* LWIP_UDP */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user