2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-06-11 12:06:42 +02:00
|
|
|
* apps/netutils/tftpc/tftpc_get.c
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.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 Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftp_parsedatapacket
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
static inline int tftp_parsedatapacket(FAR const uint8_t *packet,
|
|
|
|
FAR uint16_t *opcode,
|
|
|
|
FAR uint16_t *blockno)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
*opcode = (uint16_t)packet[0] << 8 | (uint16_t)packet[1];
|
|
|
|
if (*opcode == TFTP_DATA)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
*blockno = (uint16_t)packet[2] << 8 | (uint16_t)packet[3];
|
|
|
|
return OK;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2016-06-13 01:33:49 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
2011-03-19 22:04:13 +01:00
|
|
|
else if (*opcode == TFTP_ERR)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
tftp_parseerrpacket(packet);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
#endif
|
2016-06-13 01:33:49 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-09 14:08:06 +01:00
|
|
|
* Name: tftpget_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
|
2022-01-05 16:34:05 +01:00
|
|
|
* binary - TRUE: Perform binary ('octet') transfer
|
2011-03-19 22:04:13 +01:00
|
|
|
* 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 tftpget_cb(FAR const char *remote, in_addr_t addr, bool binary,
|
|
|
|
tftp_callback_t tftp_cb, FAR void *ctx)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in server; /* The address of the TFTP server */
|
|
|
|
struct sockaddr_in from; /* The address the last UDP message recv'd from */
|
2014-12-11 01:37:43 +01:00
|
|
|
FAR uint8_t *packet; /* Allocated memory to hold one packet */
|
2011-03-19 22:04:13 +01:00
|
|
|
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 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 */
|
|
|
|
|
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;
|
2018-03-09 14:08:06 +01:00
|
|
|
return result;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2023-03-05 16:55:15 +01:00
|
|
|
len = tftp_mkreqpacket(packet, TFTP_IOBUFSIZE,
|
|
|
|
TFTP_RRQ, remote, binary);
|
2011-03-19 22:04:13 +01:00
|
|
|
server.sin_port = HTONS(CONFIG_NETUTILS_TFTP_PORT);
|
|
|
|
ret = tftp_sendto(sd, packet, len, &server);
|
|
|
|
if (ret != len)
|
|
|
|
{
|
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
/* 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.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2012-02-18 19:13:30 +01:00
|
|
|
if (nbytesrecvd > 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Verify the sender address and port number */
|
|
|
|
|
|
|
|
if (server.sin_addr.s_addr != from.sin_addr.s_addr)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Invalid address in DATA\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
retry--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server.sin_port && server.sin_port != from.sin_port)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Invalid port in DATA\n");
|
2018-03-09 14:08:06 +01:00
|
|
|
len = tftp_mkerrpacket(packet, TFTP_ERR_UNKID,
|
|
|
|
TFTP_ERRST_UNKID);
|
2011-03-19 22:04:13 +01:00
|
|
|
ret = tftp_sendto(sd, packet, len, &from);
|
|
|
|
retry--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the incoming DATA packet */
|
|
|
|
|
2011-12-12 16:59:33 +01:00
|
|
|
if (nbytesrecvd < TFTP_DATAHEADERSIZE)
|
|
|
|
{
|
|
|
|
/* Packet is not big enough to be parsed */
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Tiny data packet ignored\n");
|
2011-12-12 16:59:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tftp_parsedatapacket(packet, &opcode, &rblockno) != OK ||
|
2011-03-19 22:04:13 +01:00
|
|
|
blockno != rblockno)
|
|
|
|
{
|
2018-03-09 14:08:06 +01:00
|
|
|
/* Opcode is not TFTP_DATA or the block number is
|
|
|
|
* unexpected.
|
|
|
|
*/
|
2011-12-12 16:59:33 +01:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Parse failure\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
if (opcode > TFTP_MAXRFC1350)
|
|
|
|
{
|
2018-03-09 14:08:06 +01:00
|
|
|
len = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP,
|
|
|
|
TFTP_ERRST_ILLEGALOP);
|
2011-03-19 22:04:13 +01:00
|
|
|
ret = tftp_sendto(sd, packet, len, &from);
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
/* Replace the server port to the one in the good response */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Retry limit exceeded\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the received data chunk to the file */
|
|
|
|
|
|
|
|
ndatabytes = nbytesrecvd - TFTP_DATAHEADERSIZE;
|
2020-05-02 15:30:54 +02:00
|
|
|
tftp_dumpbuffer("Recvd DATA",
|
|
|
|
packet + TFTP_DATAHEADERSIZE, ndatabytes);
|
2018-03-09 14:08:06 +01:00
|
|
|
if (tftp_cb(ctx, 0, packet + TFTP_DATAHEADERSIZE, ndatabytes) < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("ACK blockno %d\n", blockno);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
while (ndatabytes >= TFTP_DATASIZE);
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
result = OK;
|
|
|
|
|
|
|
|
errout_with_sd:
|
|
|
|
close(sd);
|
2018-03-09 14:08:06 +01:00
|
|
|
|
|
|
|
errout:
|
2011-03-19 22:04:13 +01:00
|
|
|
free(packet);
|
2018-03-09 14:08:06 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftp_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tftp_write(FAR void *ctx, uint32_t offset, FAR uint8_t *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
2019-12-05 14:14:29 +01:00
|
|
|
int fd = (intptr_t)ctx;
|
2018-03-09 14:08:06 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
nerr("ERROR: write failed: %d\n", errno);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle partial writes */
|
|
|
|
|
2020-11-24 23:35:46 +01:00
|
|
|
ninfo("Wrote %zd bytes to file\n", nbyteswritten);
|
2018-03-09 14:08:06 +01:00
|
|
|
left -= nbyteswritten;
|
|
|
|
buf += nbyteswritten;
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2018-03-09 14:08:06 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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(FAR const char *remote, FAR const char *local, in_addr_t addr,
|
|
|
|
bool binary)
|
|
|
|
{
|
|
|
|
int fd; /* File descriptor for file I/O */
|
|
|
|
int result = ERROR; /* Generic return status */
|
|
|
|
|
|
|
|
/* Open the file for writing */
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
fd = open(local, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
2018-03-09 14:08:06 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: open failed: %d\n", errno);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:14:29 +01:00
|
|
|
result = tftpget_cb(remote, addr, binary, tftp_write,
|
|
|
|
(FAR void *)(intptr_t)fd);
|
2018-03-09 14:08:06 +01:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
errout:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|