2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-01-19 14:59:12 +01:00
|
|
|
* netutils/tftp/tftpc_put.c
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2018-03-09 14:08:06 +01:00
|
|
|
* Copyright (C) 2008-2009, 2011, 2018 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2018-03-09 14:08:06 +01:00
|
|
|
* Copyright (C) 2018 Sebastien Lorquet. All rights reserved.
|
|
|
|
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/tftp.h"
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#include "tftpc_internal.h"
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define TFTP_RETRIES 3
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2020-05-02 15:30:54 +02:00
|
|
|
* Number of bytes read into the packet. <TFTP_PACKETSIZE means end of
|
|
|
|
* file; <1 if an error occurs.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
int tftp_mkdatapacket(off_t offset, FAR uint8_t *packet, uint16_t blockno,
|
|
|
|
tftp_callback_t tftp_cb, FAR void *ctx)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
nbytesread = tftp_cb(ctx, offset, &packet[TFTP_DATAHEADERSIZE],
|
|
|
|
TFTP_DATASIZE);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (nbytesread < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
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
|
2020-02-23 05:51:44 +01:00
|
|
|
* packet - buffer to use for the transfers
|
2011-03-19 22:04:13 +01:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
static int tftp_rcvack(int sd, FAR uint8_t *packet,
|
|
|
|
FAR struct sockaddr_in *server, FAR uint16_t *port,
|
|
|
|
FAR uint16_t *blockno)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2018-03-09 14:08:06 +01:00
|
|
|
struct sockaddr_in from; /* The address the last UDP msg recv'd from */
|
2011-03-19 22:04:13 +01:00
|
|
|
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 */
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
2012-02-18 19:13:30 +01:00
|
|
|
if (nbytes == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-11-24 23:36:46 +01:00
|
|
|
nerr("ERROR: Connection lost: %zd bytes\n", nbytes);
|
2012-02-18 19:13:30 +01:00
|
|
|
}
|
|
|
|
else if (nbytes > 0)
|
|
|
|
{
|
2020-11-24 23:36:46 +01:00
|
|
|
nerr("ERROR: Short packet: %zd bytes\n", nbytes);
|
2012-02-18 19:13:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Recvfrom failure\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Break out to bump up the retry count */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
ninfo("Invalid address in DATA\n");
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (*port != server->sin_port)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Invalid port in DATA\n");
|
2018-03-09 14:08:06 +01:00
|
|
|
packetlen = tftp_mkerrpacket(packet, TFTP_ERR_UNKID,
|
|
|
|
TFTP_ERRST_UNKID);
|
2020-01-02 13:09:50 +01:00
|
|
|
tftp_sendto(sd, packet, packetlen, server);
|
2011-03-19 22:04:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the error message */
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
opcode = (uint16_t)packet[0] << 8 | (uint16_t)packet[1];
|
|
|
|
rblockno = (uint16_t)packet[2] << 8 | (uint16_t)packet[3];
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Verify that the message that we received is an ACK for the
|
|
|
|
* expected block number.
|
|
|
|
*/
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
if (opcode != TFTP_ACK)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Bad opcode\n");
|
2016-06-13 01:33:49 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
2011-03-19 22:04:13 +01:00
|
|
|
if (opcode == TFTP_ERR)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
tftp_parseerrpacket(packet);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (opcode > TFTP_MAXRFC1350)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
packetlen = tftp_mkerrpacket(packet,
|
|
|
|
TFTP_ERR_ILLEGALOP,
|
2018-03-09 14:08:06 +01:00
|
|
|
TFTP_ERRST_ILLEGALOP);
|
2020-01-02 13:09:50 +01:00
|
|
|
tftp_sendto(sd, packet, packetlen, server);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Break out an bump up the retry count */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Received ACK for block %d\n", rblockno);
|
2011-03-19 22:04:13 +01:00
|
|
|
*blockno = rblockno;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have tried TFTP_RETRIES times */
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Timeout, Waiting for ACK\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR; /* Will never get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-09 14:08:06 +01:00
|
|
|
* Name: tftpput_cb
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* 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
|
2018-03-09 14:08:06 +01:00
|
|
|
* cb - callback that will be called with data packets
|
|
|
|
* ctx - pointer passed to the previous callback
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
int tftpput_cb(FAR const char *remote, in_addr_t addr, bool binary,
|
|
|
|
tftp_callback_t cb, FAR void *ctx)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in server; /* The address of the TFTP server */
|
2018-03-09 14:08:06 +01:00
|
|
|
FAR uint8_t *packet; /* Allocated memory to hold one packet */
|
2011-03-19 22:04:13 +01:00
|
|
|
off_t offset; /* Offset into source file */
|
|
|
|
uint16_t blockno; /* The current transfer block number */
|
|
|
|
uint16_t rblockno; /* The ACK'ed block number */
|
2018-03-09 14:08:06 +01:00
|
|
|
uint16_t port = 0; /* This is the port nbr for the transfer */
|
2011-03-19 22:04:13 +01:00
|
|
|
int packetlen; /* The length of the data packet */
|
|
|
|
int sd; /* Socket descriptor for socket 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 */
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
packet = (FAR uint8_t *)zalloc(TFTP_IOBUFSIZE);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (!packet)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: packet memory allocation failure\n");
|
2020-05-07 22:07:08 +02:00
|
|
|
errno = ENOMEM;
|
2011-03-19 22:04:13 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Initialize a UDP socket and setup the server address */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
sd = tftp_sockinit(&server, addr);
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
2018-03-09 14:08:06 +01:00
|
|
|
goto errout_with_packet;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2020-05-02 15:30:54 +02:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: Re-sending request\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* We are going to loop and re-send the request packet. Check the
|
|
|
|
* retry count so that we do not loop forever.
|
2020-05-02 15:30:54 +02:00
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (++retry > TFTP_RETRIES)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Retry count exceeded\n");
|
2020-05-07 22:07:08 +02:00
|
|
|
errno = ETIMEDOUT;
|
2011-03-19 22:04:13 +01:00
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then loop sending the entire file to the server in chunks */
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
retry = 0;
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Construct the next data packet */
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
packetlen = tftp_mkdatapacket(offset, packet, blockno, cb, ctx);
|
2011-03-19 22:04:13 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
/* Yes.. If we are at the end of the file and if all of the
|
|
|
|
* packets have been ACKed, then we are done.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (packetlen < TFTP_PACKETSIZE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
/* Not the last block.. set up for the next block */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
blockno += 1;
|
|
|
|
offset += TFTP_DATASIZE;
|
|
|
|
retry = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
/* Skip the retry test */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
continue;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Retry count exceeded\n");
|
2020-05-07 22:07:08 +02:00
|
|
|
errno = ETIMEDOUT;
|
2011-03-19 22:04:13 +01:00
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
result = OK;
|
|
|
|
|
|
|
|
errout_with_sd:
|
|
|
|
close(sd);
|
|
|
|
errout_with_packet:
|
|
|
|
free(packet);
|
|
|
|
errout:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftp_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tftp_read(FAR void *ctx, uint32_t offset, FAR uint8_t *buf,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2019-12-05 14:14:29 +01:00
|
|
|
int fd = (intptr_t)ctx;
|
2018-03-09 14:08:06 +01:00
|
|
|
off_t tmp;
|
|
|
|
ssize_t nbytesread;
|
|
|
|
ssize_t totalread = 0;
|
|
|
|
|
|
|
|
/* Seek to the correct offset in the file */
|
|
|
|
|
|
|
|
tmp = lseek(fd, offset, SEEK_SET);
|
|
|
|
if (tmp == (off_t)-1)
|
|
|
|
{
|
|
|
|
nerr("ERROR: lseek failed: %d\n", errno);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file data into the packet buffer */
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
nerr("ERROR: 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;
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
return totalread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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(FAR const char *local, FAR const char *remote, in_addr_t addr,
|
|
|
|
bool binary)
|
|
|
|
{
|
|
|
|
int fd; /* File descriptor for file I/O */
|
|
|
|
int result = ERROR; /* Assume failure */
|
|
|
|
|
|
|
|
/* Open the file for reading */
|
|
|
|
|
|
|
|
fd = open(local, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: open failed: %d\n", errno);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:14:29 +01:00
|
|
|
result = tftpput_cb(remote, addr, binary, tftp_read,
|
|
|
|
(FAR void *)(intptr_t)fd);
|
2018-03-09 14:08:06 +01:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|