mirror of
https://github.com/apache/nuttx.git
synced 2026-09-21 05:14:27 +08:00
TFTP Client
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@878 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
############################################################################
|
||||
# netutils/tftpc/Make.defs
|
||||
#
|
||||
# Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
ifeq ($(CONFIG_NET_UDP),y)
|
||||
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
|
||||
TFTPC_ASRCS =
|
||||
TFTPC_CSRCS = tftpc_get.c tftpc_put.c tftpc_packets.c
|
||||
endif
|
||||
endif
|
||||
@@ -0,0 +1,352 @@
|
||||
/****************************************************************************
|
||||
* netuils/tftp/tftpc_get.c
|
||||
*
|
||||
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <net/uip/uipopt.h>
|
||||
#include <net/uip/uip.h>
|
||||
#include <net/uip/tftp.h>
|
||||
|
||||
#include "tftpc_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define TFTP_RETRIES 3
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_write
|
||||
****************************************************************************/
|
||||
|
||||
static inline ssize_t tftp_write(int fd, const ubyte *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(g_tftpcallfailed, "write", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Handle partial writes */
|
||||
|
||||
left -= nbyteswritten;
|
||||
buf += nbyteswritten;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_parsedatapacket
|
||||
****************************************************************************/
|
||||
|
||||
static inline int tftp_parsedatapacket(const ubyte *packet,
|
||||
uint16 *opcode, uint16 *blockno)
|
||||
{
|
||||
*opcode = (uint16)packet[0] << 8 | (uint16)packet[1];
|
||||
if (*opcode == TFTP_DATA)
|
||||
{
|
||||
*blockno = (uint16)packet[2] << 8 | (uint16)packet[3];
|
||||
return OK;
|
||||
}
|
||||
#if CONFIG_DEBUG
|
||||
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, boolean binary)
|
||||
{
|
||||
struct sockaddr_in server; /* The address of the TFTP server */
|
||||
struct sockaddr_in from; /* The address the last UDP message recv'd from */
|
||||
ubyte *packet; /* Allocated memory to hold one packet */
|
||||
uint16 blockno = 0; /* The current transfer block number */
|
||||
uint16 port = 0; /* This is the port number for the transfer */
|
||||
uint16 opcode; /* Received opcode */
|
||||
uint16 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; /* 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 */
|
||||
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
|
||||
uint16 lastacked = 0; /* The last block number that was ACK'ed */
|
||||
int ablockno; /* Number of un-ACKed packets */
|
||||
#endif
|
||||
|
||||
/* Allocate the buffer to used for socket/disk I/O */
|
||||
|
||||
packet = (ubyte*)zalloc(TFTP_IOBUFSIZE);
|
||||
if (!packet)
|
||||
{
|
||||
ndbg(g_tftpnomemory, "packet");
|
||||
errno = ENOMEM;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Open the file for writing */
|
||||
|
||||
fd = open(local, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||
if (fd < 0)
|
||||
{
|
||||
ndbg(g_tftpcallfailed, "open", 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 read request */
|
||||
|
||||
len = tftp_mkreqpacket(packet, TFTP_RRQ, remote, binary);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
if (ret != len)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Then enter the transfer loop. Loop until the entire file has
|
||||
* been received or until an error occurs.
|
||||
*/
|
||||
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
|
||||
ablockno = CONFIG_NETUTILS_TFTP_ACKPACKETS-1;
|
||||
#endif
|
||||
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++)
|
||||
{
|
||||
/* Get the next packet from the server */
|
||||
|
||||
nbytesrecvd = tftp_recvfrom(sd, packet, TFTP_IOBUFSIZE, &from);
|
||||
|
||||
/* Check if anything valid was received */
|
||||
|
||||
if (nbytesrecvd >= 0)
|
||||
{
|
||||
/* Replace the server port to the one in the response */
|
||||
|
||||
if (!port)
|
||||
{
|
||||
port = from.sin_port;
|
||||
server.sin_port = port;
|
||||
}
|
||||
|
||||
/* Verify the sender address and port number */
|
||||
|
||||
if (server.sin_addr.s_addr != from.sin_addr.s_addr)
|
||||
{
|
||||
nvdbg(g_tftpaddress, "recvfrom");
|
||||
retry--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (port != from.sin_port)
|
||||
{
|
||||
nvdbg(g_tftpport, "recvfrom");
|
||||
len = tftp_mkerrpacket(packet, TFTP_ERR_UNKID, TFTP_ERRST_UNKID);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
retry--;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Parse the incoming DATA packet */
|
||||
|
||||
if (nbytesrecvd < TFTP_DATAHEADERSIZE ||
|
||||
tftp_parsedatapacket(packet, &opcode, &blockno) != OK ||
|
||||
blockno != rblockno)
|
||||
{
|
||||
nvdbg("Parse failure\n");
|
||||
if (opcode > TFTP_MAXRFC1350)
|
||||
{
|
||||
len = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP, TFTP_ERRST_ILLEGALOP);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Break out of the loop when we receive a good data packet */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Did we exhaust all of the retries? */
|
||||
|
||||
if (retry == TFTP_RETRIES)
|
||||
{
|
||||
nvdbg(g_tftptoomanyretries);
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Write the received data chunk to the file */
|
||||
|
||||
ndatabytes = nbytesrecvd - TFTP_DATAHEADERSIZE;
|
||||
if (tftp_write(fd, packet + TFTP_DATAHEADERSIZE, ndatabytes) < 0)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
|
||||
/* Send the acknowledgment if we have reach the configured block count */
|
||||
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
|
||||
ablockno++;
|
||||
if (ablockno == CONFIG_NETUTILS_TFTP_ACKPACKETS)
|
||||
#endif
|
||||
{
|
||||
len = tftp_mkackpacket(packet, blockno);
|
||||
ret = tftp_sendto(sd, packet, len, &server);
|
||||
if (ret != len)
|
||||
{
|
||||
goto errout_with_sd;
|
||||
}
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
|
||||
lastacked = blockno;
|
||||
#endif
|
||||
nvdbg("ACK blockno %d\n", blockno);
|
||||
}
|
||||
}
|
||||
while (ndatabytes >= TFTP_DATASIZE);
|
||||
|
||||
/* The final packet of the transfer will be a partial packet
|
||||
*
|
||||
* If the final packet(s) were not ACK'ed, then we will ACK them here
|
||||
*/
|
||||
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > 1
|
||||
if (ndatabytes < blockno != lastacked)
|
||||
{
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 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,171 @@
|
||||
/****************************************************************************
|
||||
* netutils/tftoc/tftpc_internal.h
|
||||
*
|
||||
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
*
|
||||
* 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 <sys/types.h>
|
||||
#include <net/uip/uipopt.h>
|
||||
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* Number of packets before ACK is returned */
|
||||
|
||||
#ifndef CONFIG_NETUTILS_TFTP_ACKPACKETS
|
||||
# define CONFIG_NETUTILS_TFTP_ACKPACKETS 1
|
||||
#endif
|
||||
|
||||
#define TFTP_MAXACKPACKETS 16
|
||||
#if CONFIG_NETUTILS_TFTP_ACKPACKETS > TFTP_MAXACKPACKETS
|
||||
# error "CONFIG_NETUTILS_TFTP_ACKPACKETS exceeds maximum"
|
||||
#endif
|
||||
|
||||
/* The TFTP port number (usually 69) */
|
||||
|
||||
#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
|
||||
|
||||
#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
|
||||
# warning "uIP MSS is too small for TFTP"
|
||||
#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
|
||||
****************************************************************************/
|
||||
|
||||
#if CONFIG_DEBUG
|
||||
extern const char g_tftpcallfailed[];
|
||||
extern const char g_tftpcalltimedout[];
|
||||
extern const char g_tftpnomemory[];
|
||||
extern const char g_tftptoomanyretries[];
|
||||
extern const char g_tftpaddress[];
|
||||
extern const char g_tftpport[];
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/* Defined in tftp_packet.c *************************************************/
|
||||
|
||||
extern int tftp_sockinit(struct sockaddr_in *server, in_addr_t addr);
|
||||
extern int tftp_mkreqpacket(ubyte *buffer, int opcode, const char *path, boolean binary);
|
||||
extern int tftp_mkackpacket(ubyte *buffer, uint16 blockno);
|
||||
extern int tftp_mkerrpacket(ubyte *buffer, uint16 errorcode, const char *errormsg);
|
||||
#if CONFIG_DEBUG
|
||||
extern int tftp_parseerrpacket(const ubyte *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);
|
||||
|
||||
#endif /* __NETUTILS_TFTP_TFTPC_INTERNAL_H */
|
||||
@@ -0,0 +1,334 @@
|
||||
/****************************************************************************
|
||||
* netuils/tftp/tftpc_packets.c
|
||||
*
|
||||
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <net/uip/uipopt.h>
|
||||
#include <net/uip/uip.h>
|
||||
#include <net/uip/tftp.h>
|
||||
|
||||
#include "tftpc_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
#if CONFIG_DEBUG
|
||||
const char g_tftpcallfailed[] = "%s failed: %d\n";
|
||||
const char g_tftpcalltimedout[] = "%s timed out\n";
|
||||
const char g_tftpnomemory[] = "%s memory allocation failure\n";
|
||||
const char g_tftptoomanyretries[] = "Retry limit exceeded\n";
|
||||
const char g_tftpaddress[] = "%s invalid address\n";
|
||||
const char g_tftpport[] = "%s invalid port\n";
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: tftp_mode
|
||||
****************************************************************************/
|
||||
|
||||
static inline const char *tftp_mode(boolean 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(g_tftpcallfailed, "socket", errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 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(g_tftpcallfailed, "setsockopt", 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
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int tftp_mkreqpacket(ubyte *buffer, int opcode, const char *path, boolean 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(ubyte *buffer, uint16 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(ubyte *buffer, uint16 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
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
int tftp_parseerrpacket(const ubyte *buffer)
|
||||
{
|
||||
uint16 opcode = (uint16)buffer[0] << 8 | (uint16)buffer[1];
|
||||
uint16 errcode = (uint16)buffer[2] << 8 | (uint16)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 (;;)
|
||||
{
|
||||
/* 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(g_tftpcalltimedout, "recvfrom");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* If EINTR, then loop and try again. Other errors are fatal */
|
||||
|
||||
else if (errno != EINTR)
|
||||
{
|
||||
ndbg(g_tftpcallfailed, "recvfrom", 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(g_tftpcallfailed, "sendto", errno);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/* No errors? Return the number of bytes received */
|
||||
|
||||
else
|
||||
{
|
||||
return nbytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user