mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-19 03:33:34 +08:00
Resync new repository with old repo r5166
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5153 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see misc/tools/kconfig-language.txt.
|
||||
#
|
||||
|
||||
config NETUTILS_TFTPC
|
||||
bool "TFTP client"
|
||||
default n
|
||||
---help---
|
||||
Enable support for the TFTP client.
|
||||
|
||||
if NETUTILS_TFTPC
|
||||
endif
|
||||
@@ -0,0 +1,99 @@
|
||||
############################################################################
|
||||
# apps/netutils/tftpc/Makefile
|
||||
#
|
||||
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# 3. Neither the name NuttX nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# TFTP Client Library
|
||||
|
||||
ASRCS =
|
||||
CSRCS =
|
||||
|
||||
ifeq ($(CONFIG_NET_UDP),y)
|
||||
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
||||
CSRCS += tftpc_get.c tftpc_put.c tftpc_packets.c
|
||||
endif
|
||||
endif
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
|
||||
else
|
||||
BIN = "$(APPDIR)/libapps$(LIBEXT)"
|
||||
endif
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: context depend clean distclean
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
@( for obj in $(OBJS) ; do \
|
||||
$(call ARCHIVE, $(BIN), $${obj}); \
|
||||
done ; )
|
||||
@touch .built
|
||||
|
||||
context:
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
@rm -f *.o *~ .*.swp .built
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
@rm -f Make.dep .depend
|
||||
|
||||
-include Make.dep
|
||||
@@ -0,0 +1,337 @@
|
||||
/****************************************************************************
|
||||
* netuils/tftp/tftpc_get.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, TFTP_DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <apps/netutils/tftp.h>
|
||||
|
||||
#include "tftpc_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define TFTP_RETRIES 3
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_write
|
||||
****************************************************************************/
|
||||
|
||||
static inline ssize_t tftp_write(int fd, const uint8_t *buf, size_t len)
|
||||
{
|
||||
size_t left = len;
|
||||
ssize_t nbyteswritten;
|
||||
|
||||
while (left > 0)
|
||||
{
|
||||
/* Write the data... repeating the write in the event that it was
|
||||
* interrupted by a signal.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
nbyteswritten = write(fd, buf, left);
|
||||
}
|
||||
while (nbyteswritten < 0 && errno == EINTR);
|
||||
|
||||
/* Check for non-EINTR errors */
|
||||
|
||||
if (nbyteswritten < 0)
|
||||
{
|
||||
ndbg("write failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Handle partial writes */
|
||||
|
||||
nvdbg("Wrote %d bytes to file\n", nbyteswritten);
|
||||
left -= nbyteswritten;
|
||||
buf += nbyteswritten;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_parsedatapacket
|
||||
****************************************************************************/
|
||||
|
||||
static inline int tftp_parsedatapacket(const uint8_t *packet,
|
||||
uint16_t *opcode, uint16_t *blockno)
|
||||
{
|
||||
*opcode = (uint16_t)packet[0] << 8 | (uint16_t)packet[1];
|
||||
if (*opcode == TFTP_DATA)
|
||||
{
|
||||
*blockno = (uint16_t)packet[2] << 8 | (uint16_t)packet[3];
|
||||
return OK;
|
||||
}
|
||||
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
||||
else if (*opcode == TFTP_ERR)
|
||||
{
|
||||
(void)tftp_parseerrpacket(packet);
|
||||
}
|
||||
#endif
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftpget
|
||||
*
|
||||
* Input Parameters:
|
||||
* remote - The name of the file on the TFTP server.
|
||||
* local - Path to the location on a mounted filesystem where the file
|
||||
* will be stored.
|
||||
* addr - The IP address of the server in network order
|
||||
* binary - TRUE: Perform binary ('octect') transfer
|
||||
* FALSE: Perform text ('netascii') transfer
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftpget(const char *remote, const char *local, in_addr_t addr, bool binary)
|
||||
{
|
||||
struct sockaddr_in server; /* The address of the TFTP server */
|
||||
struct sockaddr_in from; /* The address the last UDP message recv'd from */
|
||||
uint8_t *packet; /* Allocated memory to hold one packet */
|
||||
uint16_t blockno = 0; /* The current transfer block number */
|
||||
uint16_t opcode; /* Received opcode */
|
||||
uint16_t rblockno; /* Received block number */
|
||||
int len; /* Generic length */
|
||||
int sd; /* Socket descriptor for socket I/O */
|
||||
int fd; /* File descriptor for file I/O */
|
||||
int retry; /* Retry counter */
|
||||
int nbytesrecvd = 0; /* The number of bytes received in the packet */
|
||||
int ndatabytes; /* The number of data bytes received */
|
||||
int result = ERROR; /* Assume failure */
|
||||
int ret; /* Generic return status */
|
||||
|
||||
/* Allocate the buffer to used for socket/disk I/O */
|
||||
|
||||
packet = (uint8_t*)zalloc(TFTP_IOBUFSIZE);
|
||||
if (!packet)
|
||||
{
|
||||
ndbg("packet memory allocation failure\n");
|
||||
set_errno(ENOMEM);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Open the file for writing */
|
||||
|
||||
fd = open(local, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||
if (fd < 0)
|
||||
{
|
||||
ndbg("open failed: %d\n", errno);
|
||||
goto errout_with_packet;
|
||||
}
|
||||
|
||||
/* Initialize a UDP socket and setup the server addresss */
|
||||
|
||||
sd = tftp_sockinit(&server, addr);
|
||||
if (sd < 0)
|
||||
{
|
||||
goto errout_with_fd;
|
||||
}
|
||||
|
||||
/* Then enter the transfer loop. Loop until the entire file has
|
||||
* been received or until an error occurs.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
/* Increment the TFTP block number for the next transfer */
|
||||
|
||||
blockno++;
|
||||
|
||||
/* Send the next block if the file within a loop. We will
|
||||
* retry up to TFTP_RETRIES times before giving up on the
|
||||
* transfer.
|
||||
*/
|
||||
|
||||
for (retry = 0; retry < TFTP_RETRIES; retry++)
|
||||
{
|
||||
/* Send the read request using the well-known port number before
|
||||
* receiving the first block. Each retry of the first block will
|
||||
* re-send the request.
|
||||
*/
|
||||
|
||||
if (blockno == 1)
|
||||
{
|
||||
len = tftp_mkreqpacket(packet, TFTP_RRQ, remote, binary);
|
||||
server.sin_port = HTONS(CONFIG_NETUTILS_TFTP_PORT);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
if (ret != len)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Subsequent sendto will use the port number selected by the TFTP
|
||||
* server in the DATA packet. Setting the server port to zero
|
||||
* here indicates that we have not yet received the server port number.
|
||||
*/
|
||||
|
||||
server.sin_port = 0;
|
||||
}
|
||||
|
||||
/* Get the next packet from the server */
|
||||
|
||||
nbytesrecvd = tftp_recvfrom(sd, packet, TFTP_IOBUFSIZE, &from);
|
||||
|
||||
/* Check if anything valid was received */
|
||||
|
||||
if (nbytesrecvd > 0)
|
||||
{
|
||||
/* Verify the sender address and port number */
|
||||
|
||||
if (server.sin_addr.s_addr != from.sin_addr.s_addr)
|
||||
{
|
||||
nvdbg("Invalid address in DATA\n");
|
||||
retry--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (server.sin_port && server.sin_port != from.sin_port)
|
||||
{
|
||||
nvdbg("Invalid port in DATA\n");
|
||||
len = tftp_mkerrpacket(packet, TFTP_ERR_UNKID, TFTP_ERRST_UNKID);
|
||||
ret = tftp_sendto(sd, packet, len, &from);
|
||||
retry--;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Parse the incoming DATA packet */
|
||||
|
||||
if (nbytesrecvd < TFTP_DATAHEADERSIZE)
|
||||
{
|
||||
/* Packet is not big enough to be parsed */
|
||||
|
||||
nvdbg("Tiny data packet ignored\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tftp_parsedatapacket(packet, &opcode, &rblockno) != OK ||
|
||||
blockno != rblockno)
|
||||
{
|
||||
/* Opcode is not TFTP_DATA or the block number is unexpected */
|
||||
|
||||
nvdbg("Parse failure\n");
|
||||
if (opcode > TFTP_MAXRFC1350)
|
||||
{
|
||||
len = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP, TFTP_ERRST_ILLEGALOP);
|
||||
ret = tftp_sendto(sd, packet, len, &from);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Replace the server port to the one in the good data response */
|
||||
|
||||
if (!server.sin_port)
|
||||
{
|
||||
server.sin_port = from.sin_port;
|
||||
}
|
||||
|
||||
/* Then break out of the loop */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Did we exhaust all of the retries? */
|
||||
|
||||
if (retry == TFTP_RETRIES)
|
||||
{
|
||||
nvdbg("Retry limit exceeded\n");
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Write the received data chunk to the file */
|
||||
|
||||
ndatabytes = nbytesrecvd - TFTP_DATAHEADERSIZE;
|
||||
tftp_dumpbuffer("Recvd DATA", packet + TFTP_DATAHEADERSIZE, ndatabytes);
|
||||
if (tftp_write(fd, packet + TFTP_DATAHEADERSIZE, ndatabytes) < 0)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Send the acknowledgment */
|
||||
|
||||
len = tftp_mkackpacket(packet, blockno);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
if (ret != len)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
nvdbg("ACK blockno %d\n", blockno);
|
||||
}
|
||||
while (ndatabytes >= TFTP_DATASIZE);
|
||||
|
||||
/* Return success */
|
||||
|
||||
result = OK;
|
||||
|
||||
errout_with_sd:
|
||||
close(sd);
|
||||
errout_with_fd:
|
||||
close(fd);
|
||||
errout_with_packet:
|
||||
free(packet);
|
||||
errout:
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 */
|
||||
@@ -0,0 +1,173 @@
|
||||
/****************************************************************************
|
||||
* netutils/tftoc/tftpc_internal.h
|
||||
*
|
||||
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __NETUTILS_TFTP_TFTPC_INTERNAL_H
|
||||
#define __NETUTILS_TFTP_TFTPC_INTERNAL_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/compiler.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* Verify TFTP configuration settings ***************************************/
|
||||
/* The settings beginning with CONFIG_NETUTILS_TFTP_* can all be set in the
|
||||
* NuttX configuration file. If they are are defined in the configuration
|
||||
* then default values are assigned here.
|
||||
*/
|
||||
|
||||
/* The "well-known" server TFTP port number (usually 69). This port number
|
||||
* is only used for the initial server contact. The server will negotiate
|
||||
* a new transfer port number after the initial client request.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_NETUTILS_TFTP_PORT
|
||||
# define CONFIG_NETUTILS_TFTP_PORT 69
|
||||
#endif
|
||||
|
||||
/* recvfrom timeout in deci-seconds */
|
||||
|
||||
#ifndef CONFIG_NETUTILS_TFTP_TIMEOUT
|
||||
# define CONFIG_NETUTILS_TFTP_TIMEOUT 10 /* One second */
|
||||
#endif
|
||||
|
||||
/* Dump received buffers */
|
||||
|
||||
#undef CONFIG_NETUTILS_TFTP_DUMPBUFFERS
|
||||
|
||||
/* Sizes of TFTP messsage headers */
|
||||
|
||||
#define TFTP_ACKHEADERSIZE 4
|
||||
#define TFTP_ERRHEADERSIZE 4
|
||||
#define TFTP_DATAHEADERSIZE 4
|
||||
|
||||
/* The maximum size for TFTP data is determined by the configured uIP packet
|
||||
* size (but cannot exceed 512 + sizeof(TFTP_DATA header).
|
||||
*/
|
||||
|
||||
#define TFTP_DATAHEADERSIZE 4
|
||||
#define TFTP_MAXPACKETSIZE (TFTP_DATAHEADERSIZE+512)
|
||||
|
||||
#if UIP_UDP_MSS < TFTP_MAXPACKETSIZE
|
||||
# define TFTP_PACKETSIZE UIP_UDP_MSS
|
||||
# ifdef CONFIG_CPP_HAVE_WARNING
|
||||
# warning "uIP MSS is too small for TFTP"
|
||||
# endif
|
||||
#else
|
||||
# define TFTP_PACKETSIZE TFTP_MAXPACKETSIZE
|
||||
#endif
|
||||
|
||||
#define TFTP_DATASIZE (TFTP_PACKETSIZE-TFTP_DATAHEADERSIZE)
|
||||
#define TFTP_IOBUFSIZE (TFTP_PACKETSIZE+8)
|
||||
|
||||
/* TFTP Opcodes *************************************************************/
|
||||
|
||||
#define TFTP_RRQ 1 /* Read Request RFC 1350, RFC 2090 */
|
||||
#define TFTP_WRQ 2 /* Write Request RFC 1350 */
|
||||
#define TFTP_DATA 3 /* Data chunk RFC 1350 */
|
||||
#define TFTP_ACK 4 /* Acknowledgement RFC 1350 */
|
||||
#define TFTP_ERR 5 /* Error Message RFC 1350 */
|
||||
#define TFTP_OACK 6 /* Option acknowledgment RFC 2347 */
|
||||
|
||||
#define TFTP_MAXRFC1350 5
|
||||
|
||||
/* TFTP Error Codes *********************************************************/
|
||||
|
||||
/* Error codes */
|
||||
|
||||
#define TFTP_ERR_NONE 0 /* No error */
|
||||
#define TFTP_ERR_NOSUCHFILE 1 /* File not found */
|
||||
#define TFTP_ERR_ACCESS 2 /* Access violation */
|
||||
#define TFTP_ERR_FULL 3 /* Disk full or allocation exceeded */
|
||||
#define TFTP_ERR_ILLEGALOP 4 /* Illegal TFTP operation */
|
||||
#define TFTP_ERR_UNKID 5 /* Unknown transfer ID */
|
||||
#define TFTP_ERR_EXISTS 6 /* File already exists */
|
||||
#define TFTP_ERR_UNKUSER 7 /* No such user */
|
||||
#define TFTP_ERR_NEGOTIATE 8 /* Terminate transfer due to option negotiation */
|
||||
|
||||
/* Error strings */
|
||||
|
||||
#define TFTP_ERR_STNOSUCHFILE "File not found"
|
||||
#define TFTP_ERRST_ACCESS "Access violation"
|
||||
#define TFTP_ERRST_FULL "Disk full or allocation exceeded"
|
||||
#define TFTP_ERRST_ILLEGALOP "Illegal TFTP operation"
|
||||
#define TFTP_ERRST_UNKID "Unknown transfer ID"
|
||||
#define TFTP_ERRST_EXISTS "File already exists"
|
||||
#define TFTP_ERRST_UNKUSER "No such user"
|
||||
#define TFTP_ERRST_NEGOTIATE "Terminate transfer due to option negotiation"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Type Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/* Defined in tftp_packet.c *************************************************/
|
||||
|
||||
extern int tftp_sockinit(struct sockaddr_in *server, in_addr_t addr);
|
||||
extern int tftp_mkreqpacket(uint8_t *buffer, int opcode, const char *path, bool binary);
|
||||
extern int tftp_mkackpacket(uint8_t *buffer, uint16_t blockno);
|
||||
extern int tftp_mkerrpacket(uint8_t *buffer, uint16_t errorcode, const char *errormsg);
|
||||
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
||||
extern int tftp_parseerrpacket(const uint8_t *packet);
|
||||
#endif
|
||||
|
||||
extern ssize_t tftp_recvfrom(int sd, void *buf, size_t len, struct sockaddr_in *from);
|
||||
extern ssize_t tftp_sendto(int sd, const void *buf, size_t len, struct sockaddr_in *to);
|
||||
|
||||
#ifdef CONFIG_NETUTILS_TFTP_DUMPBUFFERS
|
||||
# define tftp_dumpbuffer(msg, buffer, nbytes) nvdbgdumpbuffer(msg, buffer, nbytes)
|
||||
#else
|
||||
# define tftp_dumpbuffer(msg, buffer, nbytes)
|
||||
#endif
|
||||
|
||||
#endif /* __NETUTILS_TFTP_TFTPC_INTERNAL_H */
|
||||
@@ -0,0 +1,330 @@
|
||||
/****************************************************************************
|
||||
* netuils/tftp/tftpc_packets.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, TFTP_DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <apps/netutils/tftp.h>
|
||||
|
||||
#include "tftpc_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mode
|
||||
****************************************************************************/
|
||||
|
||||
static inline const char *tftp_mode(bool binary)
|
||||
{
|
||||
return binary ? "octet" : "netascii";
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_sockinit
|
||||
*
|
||||
* Description:
|
||||
* Common initialization logic: Create the socket and initialize the
|
||||
* server address structure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_sockinit(struct sockaddr_in *server, in_addr_t addr)
|
||||
{
|
||||
struct timeval timeo;
|
||||
int sd;
|
||||
int ret;
|
||||
|
||||
/* Create the UDP socket */
|
||||
|
||||
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (sd < 0)
|
||||
{
|
||||
ndbg("socket failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Set the recvfrom timeout */
|
||||
|
||||
timeo.tv_sec = CONFIG_NETUTILS_TFTP_TIMEOUT / 10;
|
||||
timeo.tv_usec = (CONFIG_NETUTILS_TFTP_TIMEOUT % 10) * 100000;
|
||||
ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(struct timeval));
|
||||
if (ret < 0)
|
||||
{
|
||||
ndbg("setsockopt failed: %d\n", errno);
|
||||
}
|
||||
|
||||
/* Initialize the server address structure */
|
||||
|
||||
memset(server, 0, sizeof(struct sockaddr_in));
|
||||
server->sin_family = AF_INET;
|
||||
server->sin_addr.s_addr = addr;
|
||||
server->sin_port = HTONS(CONFIG_NETUTILS_TFTP_PORT);
|
||||
return sd;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mkreqpacket
|
||||
*
|
||||
* Description:
|
||||
* RRQ or WRQ message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* N bytes: Filename
|
||||
* 1 byte: 0
|
||||
* N bytes: mode
|
||||
* 1 byte: 0
|
||||
*
|
||||
* Return
|
||||
* Then number of bytes in the request packet (never fails)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_mkreqpacket(uint8_t *buffer, int opcode, const char *path, bool binary)
|
||||
{
|
||||
buffer[0] = opcode >> 8;
|
||||
buffer[1] = opcode & 0xff;
|
||||
return sprintf((char*)&buffer[2], "%s%c%s", path, 0, tftp_mode(binary)) + 3;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mkackpacket
|
||||
*
|
||||
* Description:
|
||||
* ACK message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* 2 bytes: Block number (network order == big-endian)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_mkackpacket(uint8_t *buffer, uint16_t blockno)
|
||||
{
|
||||
buffer[0] = TFTP_ACK >> 8;
|
||||
buffer[1] = TFTP_ACK & 0xff;
|
||||
buffer[2] = blockno >> 8;
|
||||
buffer[3] = blockno & 0xff;
|
||||
return 4;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mkerrpacket
|
||||
*
|
||||
* Description:
|
||||
* ERROR message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* 2 bytes: Error number (network order == big-endian)
|
||||
* N bytes: Error string
|
||||
* 1 byte: 0
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_mkerrpacket(uint8_t *buffer, uint16_t errorcode, const char *errormsg)
|
||||
{
|
||||
buffer[0] = TFTP_ERR >> 8;
|
||||
buffer[1] = TFTP_ERR & 0xff;
|
||||
buffer[2] = errorcode >> 8;
|
||||
buffer[3] = errorcode & 0xff;
|
||||
strcpy((char*)&buffer[4], errormsg);
|
||||
return strlen(errormsg) + 5;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_parseerrpacket
|
||||
*
|
||||
* Description:
|
||||
* ERROR message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* 2 bytes: Error number (network order == big-endian)
|
||||
* N bytes: Error string
|
||||
* 1 byte: 0
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
||||
int tftp_parseerrpacket(const uint8_t *buffer)
|
||||
{
|
||||
uint16_t opcode = (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1];
|
||||
uint16_t errcode = (uint16_t)buffer[2] << 8 | (uint16_t)buffer[3];
|
||||
const char *errmsg = (const char *)&buffer[4];
|
||||
|
||||
if (opcode == TFTP_ERR)
|
||||
{
|
||||
ndbg("ERR message: %s (%d)\n", errmsg, errcode);
|
||||
return OK;
|
||||
}
|
||||
return ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_recvfrom
|
||||
*
|
||||
* Description:
|
||||
* recvfrom helper
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t tftp_recvfrom(int sd, void *buf, size_t len, struct sockaddr_in *from)
|
||||
{
|
||||
int addrlen;
|
||||
ssize_t nbytes;
|
||||
|
||||
/* Loop handles the case where the recvfrom is interrupted by a signal and
|
||||
* we should unconditionally try again.
|
||||
*/
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* For debugging, it is helpful to start with a clean buffer */
|
||||
|
||||
#if defined(CONFIG_DEBUG_VERBOSE) && defined(CONFIG_DEBUG_NET)
|
||||
memset(buf, 0, len);
|
||||
#endif
|
||||
|
||||
/* Receive the packet */
|
||||
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
nbytes = recvfrom(sd, buf, len, 0, (struct sockaddr*)from, (socklen_t*)&addrlen);
|
||||
|
||||
/* Check for errors */
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
/* Check for a timeout */
|
||||
|
||||
if (errno == EAGAIN)
|
||||
{
|
||||
ndbg("recvfrom timed out\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* If EINTR, then loop and try again. Other errors are fatal */
|
||||
|
||||
else if (errno != EINTR)
|
||||
{
|
||||
ndbg("recvfrom failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* No errors? Return the number of bytes received */
|
||||
|
||||
else
|
||||
{
|
||||
return nbytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_sendto
|
||||
*
|
||||
* Description:
|
||||
* sendto helper
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t tftp_sendto(int sd, const void *buf, size_t len, struct sockaddr_in *to)
|
||||
{
|
||||
ssize_t nbytes;
|
||||
|
||||
/* Loop handles the case where the sendto is interrupted by a signal and
|
||||
* we should unconditionally try again.
|
||||
*/
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Send the packet */
|
||||
|
||||
nbytes = sendto(sd, buf, len, 0, (struct sockaddr*)to, sizeof(struct sockaddr_in));
|
||||
|
||||
/* Check for errors */
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
/* If EINTR, then loop and try again. Other errors are fatal */
|
||||
|
||||
if (errno != EINTR)
|
||||
{
|
||||
ndbg("sendto failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* No errors? Return the number of bytes received */
|
||||
|
||||
else
|
||||
{
|
||||
return nbytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS */
|
||||
@@ -0,0 +1,485 @@
|
||||
/****************************************************************************
|
||||
* netuils/tftp/tftpc_put.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, TFTP_DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <apps/netutils/tftp.h>
|
||||
|
||||
#include "tftpc_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define TFTP_RETRIES 3
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_read
|
||||
****************************************************************************/
|
||||
|
||||
static inline ssize_t tftp_read(int fd, uint8_t *buf, size_t buflen)
|
||||
{
|
||||
ssize_t nbytesread;
|
||||
ssize_t totalread = 0;
|
||||
|
||||
while (totalread < buflen)
|
||||
{
|
||||
/* Read the data... repeating the read in the event that it was
|
||||
* interrupted by a signal.
|
||||
*/
|
||||
|
||||
do
|
||||
{
|
||||
nbytesread = read(fd, buf, buflen - totalread);
|
||||
}
|
||||
while (nbytesread < 0 && errno == EINTR);
|
||||
|
||||
/* Check for non-EINTR errors */
|
||||
|
||||
if (nbytesread < 0)
|
||||
{
|
||||
ndbg("read failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Check for end of file */
|
||||
|
||||
else if (nbytesread == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* Handle partial reads. Partial reads can happen normally
|
||||
* when the source is some device driver that returns data
|
||||
* in bits and pieces as received (such as a pipe)
|
||||
*/
|
||||
|
||||
totalread += nbytesread;
|
||||
buf += nbytesread;
|
||||
}
|
||||
return totalread;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mkdatapacket
|
||||
*
|
||||
* Description:
|
||||
* DATA message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* 2 bytes: Block number (network order == big-endian)
|
||||
* N bytes: Data (where N <= 512)
|
||||
*
|
||||
* Input Parameters:
|
||||
* fd - File descriptor used to read from the file
|
||||
* offset - File offset to read from
|
||||
* packet - Buffer to write the data packet into
|
||||
* blockno - The block number of the packet
|
||||
*
|
||||
* Return Value:
|
||||
* Number of bytes read into the packet. <TFTP_PACKETSIZE means end of file;
|
||||
* <1 if an error occurs.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_mkdatapacket(int fd, off_t offset, uint8_t *packet, uint16_t blockno)
|
||||
{
|
||||
off_t tmp;
|
||||
int nbytesread;
|
||||
|
||||
/* Format the DATA message header */
|
||||
|
||||
packet[0] = TFTP_DATA >> 8;
|
||||
packet[1] = TFTP_DATA & 0xff;
|
||||
packet[2] = blockno >> 8;
|
||||
packet[3] = blockno & 0xff;
|
||||
|
||||
/* Seek to the correct offset in the file */
|
||||
|
||||
tmp = lseek(fd, offset, SEEK_SET);
|
||||
if (tmp == (off_t)-1)
|
||||
{
|
||||
ndbg("lseek failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Read the file data into the packet buffer */
|
||||
|
||||
nbytesread = tftp_read(fd, &packet[TFTP_DATAHEADERSIZE], TFTP_DATASIZE);
|
||||
if (nbytesread < 0)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
return nbytesread + TFTP_DATAHEADERSIZE;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_rcvack
|
||||
*
|
||||
* Description:
|
||||
* ACK message format:
|
||||
*
|
||||
* 2 bytes: Opcode (network order == big-endian)
|
||||
* 2 bytes: Block number (network order == big-endian)
|
||||
*
|
||||
* Input Parameters:
|
||||
* sd - Socket descriptor to use in in the transfer
|
||||
* packet - buffer to use for the tranfers
|
||||
* server - The address of the server
|
||||
* port - The port number of the server (0 if not yet known)
|
||||
* blockno - Location to return block number in the received ACK
|
||||
*
|
||||
* Returned Value:
|
||||
* OK:success and blockno valid, ERROR:failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int tftp_rcvack(int sd, uint8_t *packet, struct sockaddr_in *server,
|
||||
uint16_t *port, uint16_t *blockno)
|
||||
{
|
||||
struct sockaddr_in from; /* The address the last UDP message recv'd from */
|
||||
ssize_t nbytes; /* The number of bytes received. */
|
||||
uint16_t opcode; /* The received opcode */
|
||||
uint16_t rblockno; /* The received block number */
|
||||
int packetlen; /* Packet length */
|
||||
int retry; /* Retry counter */
|
||||
|
||||
/* Try up to TFTP_RETRIES times */
|
||||
|
||||
for (retry = 0; retry < TFTP_RETRIES; retry++)
|
||||
{
|
||||
/* Try for until a valid ACK is received or some error occurs */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Receive the next UDP packet from the server */
|
||||
|
||||
nbytes = tftp_recvfrom(sd, packet, TFTP_IOBUFSIZE, &from);
|
||||
if (nbytes < TFTP_ACKHEADERSIZE)
|
||||
{
|
||||
/* Failed to receive a good packet */
|
||||
|
||||
if (nbytes == 0)
|
||||
{
|
||||
ndbg("Connection lost: %d bytes\n", nbytes);
|
||||
}
|
||||
else if (nbytes > 0)
|
||||
{
|
||||
ndbg("Short packet: %d bytes\n", nbytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
ndbg("Recveid failure\n");
|
||||
}
|
||||
|
||||
/* Break out to bump up the retry count */
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Get the port being used by the server if that has not yet been established */
|
||||
|
||||
if (!*port)
|
||||
{
|
||||
*port = from.sin_port;
|
||||
server->sin_port = from.sin_port;
|
||||
}
|
||||
|
||||
/* Verify that the packet was received from the correct host and port */
|
||||
|
||||
if (server->sin_addr.s_addr != from.sin_addr.s_addr)
|
||||
{
|
||||
nvdbg("Invalid address in DATA\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*port != server->sin_port)
|
||||
{
|
||||
nvdbg("Invalid port in DATA\n");
|
||||
packetlen = tftp_mkerrpacket(packet, TFTP_ERR_UNKID, TFTP_ERRST_UNKID);
|
||||
(void)tftp_sendto(sd, packet, packetlen, server);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Parse the error message */
|
||||
|
||||
opcode = (uint16_t)packet[0] << 8 | (uint16_t)packet[1];
|
||||
rblockno = (uint16_t)packet[2] << 8 | (uint16_t)packet[3];
|
||||
|
||||
/* Verify that the message that we received is an ACK for the
|
||||
* expected block number.
|
||||
*/
|
||||
|
||||
if (opcode != TFTP_ACK)
|
||||
{
|
||||
nvdbg("Bad opcode\n");
|
||||
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
||||
if (opcode == TFTP_ERR)
|
||||
{
|
||||
(void)tftp_parseerrpacket(packet);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (opcode > TFTP_MAXRFC1350)
|
||||
{
|
||||
packetlen = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP, TFTP_ERRST_ILLEGALOP);
|
||||
(void)tftp_sendto(sd, packet, packetlen, server);
|
||||
}
|
||||
|
||||
/* Break out an bump up the retry count */
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Success! */
|
||||
|
||||
nvdbg("Received ACK for block %d\n", rblockno);
|
||||
*blockno = rblockno;
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* We have tried TFTP_RETRIES times */
|
||||
|
||||
ndbg("Timeout, Waiting for ACK\n");
|
||||
return ERROR; /* Will never get here */
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftpput
|
||||
*
|
||||
* Input Parameters:
|
||||
* local - Path to the file system object to be sent.
|
||||
* remote - The name of the file on the TFTP server.
|
||||
* addr - The IP address of the server in network order
|
||||
* binary - TRUE: Perform binary ('octect') transfer
|
||||
* FALSE: Perform text ('netascii') transfer
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftpput(const char *local, const char *remote, in_addr_t addr, bool binary)
|
||||
{
|
||||
struct sockaddr_in server; /* The address of the TFTP server */
|
||||
uint8_t *packet; /* Allocated memory to hold one packet */
|
||||
off_t offset; /* Offset into source file */
|
||||
uint16_t blockno; /* The current transfer block number */
|
||||
uint16_t rblockno; /* The ACK'ed block number */
|
||||
uint16_t port = 0; /* This is the port number for the transfer */
|
||||
int packetlen; /* The length of the data packet */
|
||||
int sd; /* Socket descriptor for socket I/O */
|
||||
int fd; /* File descriptor for file I/O */
|
||||
int retry; /* Retry counter */
|
||||
int result = ERROR; /* Assume failure */
|
||||
int ret; /* Generic return status */
|
||||
|
||||
/* Allocate the buffer to used for socket/disk I/O */
|
||||
|
||||
packet = (uint8_t*)zalloc(TFTP_IOBUFSIZE);
|
||||
if (!packet)
|
||||
{
|
||||
ndbg("packet memory allocation failure\n");
|
||||
set_errno(ENOMEM);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Open the file for reading */
|
||||
|
||||
fd = open(local, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
ndbg("open failed: %d\n", errno);
|
||||
goto errout_with_packet;
|
||||
}
|
||||
|
||||
/* Initialize a UDP socket and setup the server addresss */
|
||||
|
||||
sd = tftp_sockinit(&server, addr);
|
||||
if (sd < 0)
|
||||
{
|
||||
goto errout_with_fd;
|
||||
}
|
||||
|
||||
/* Send the write request using the well known port. This may need
|
||||
* to be done several times because (1) UDP is inherenly unreliable
|
||||
* and packets may be lost normally, and (2) uIP has a nasty habit
|
||||
* of droppying packets if there is nothing hit in the ARP table.
|
||||
*/
|
||||
|
||||
blockno = 1;
|
||||
retry = 0;
|
||||
for (;;)
|
||||
{
|
||||
packetlen = tftp_mkreqpacket(packet, TFTP_WRQ, remote, binary);
|
||||
ret = tftp_sendto(sd, packet, packetlen, &server);
|
||||
if (ret != packetlen)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Receive the ACK for the write request */
|
||||
|
||||
if (tftp_rcvack(sd, packet, &server, &port, NULL) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ndbg("Re-sending request\n");
|
||||
|
||||
/* We are going to loop and re-send the request packet. Check the
|
||||
* retry count so that we do not loop forever.
|
||||
*/
|
||||
|
||||
if (++retry > TFTP_RETRIES)
|
||||
{
|
||||
ndbg("Retry count exceeded\n");
|
||||
set_errno(ETIMEDOUT);
|
||||
goto errout_with_sd;
|
||||
}
|
||||
}
|
||||
|
||||
/* Then loop sending the entire file to the server in chunks */
|
||||
|
||||
offset = 0;
|
||||
retry = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Construct the next data packet */
|
||||
|
||||
packetlen = tftp_mkdatapacket(fd, offset, packet, blockno);
|
||||
if (packetlen < 0)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Send the next data chunk */
|
||||
|
||||
ret = tftp_sendto(sd, packet, packetlen, &server);
|
||||
if (ret != packetlen)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Check for an ACK for the data chunk */
|
||||
|
||||
if (tftp_rcvack(sd, packet, &server, &port, &rblockno) == OK)
|
||||
{
|
||||
/* Check if the packet that we just sent was ACK'ed. If not,
|
||||
* we just loop to resend the same packet (same blockno, same
|
||||
* file offset).
|
||||
*/
|
||||
|
||||
if (rblockno == blockno)
|
||||
{
|
||||
/* Yes.. If we are at the end of the file and if all of the packets
|
||||
* have been ACKed, then we are done.
|
||||
*/
|
||||
|
||||
if (packetlen < TFTP_PACKETSIZE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* Not the last block.. set up for the next block */
|
||||
|
||||
blockno++;
|
||||
offset += TFTP_DATASIZE;
|
||||
retry = 0;
|
||||
|
||||
/* Skip the retry test */
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* We are going to loop and re-send the data packet. Check the retry
|
||||
* count so that we do not loop forever.
|
||||
*/
|
||||
|
||||
if (++retry > TFTP_RETRIES)
|
||||
{
|
||||
ndbg("Retry count exceeded\n");
|
||||
set_errno(ETIMEDOUT);
|
||||
goto errout_with_sd;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return success */
|
||||
|
||||
result = OK;
|
||||
|
||||
errout_with_sd:
|
||||
close(sd);
|
||||
errout_with_fd:
|
||||
close(fd);
|
||||
errout_with_packet:
|
||||
free(packet);
|
||||
errout:
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 */
|
||||
Reference in New Issue
Block a user