2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-06-11 12:06:42 +02:00
|
|
|
* apps/netutils/tftpc/tftpc_packets.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/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
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"
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the recvfrom timeout */
|
|
|
|
|
|
|
|
timeo.tv_sec = CONFIG_NETUTILS_TFTP_TIMEOUT / 10;
|
|
|
|
timeo.tv_usec = (CONFIG_NETUTILS_TFTP_TIMEOUT % 10) * 100000;
|
2021-01-19 14:59:12 +01:00
|
|
|
ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &timeo,
|
|
|
|
sizeof(struct timeval));
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
int tftp_mkreqpacket(uint8_t *buffer, int opcode, const char *path,
|
|
|
|
bool binary)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
buffer[0] = opcode >> 8;
|
|
|
|
buffer[1] = opcode & 0xff;
|
2021-01-19 14:59:12 +01:00
|
|
|
return sprintf((char *)&buffer[2], "%s%c%s", path, 0,
|
|
|
|
tftp_mode(binary)) + 3;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
int tftp_mkerrpacket(uint8_t *buffer, uint16_t errorcode,
|
|
|
|
const char *errormsg)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
buffer[0] = TFTP_ERR >> 8;
|
|
|
|
buffer[1] = TFTP_ERR & 0xff;
|
|
|
|
buffer[2] = errorcode >> 8;
|
|
|
|
buffer[3] = errorcode & 0xff;
|
2021-01-19 14:59:12 +01:00
|
|
|
strcpy((char *)&buffer[4], errormsg);
|
2011-03-19 22:04:13 +01:00
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-13 01:33:49 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
2011-03-19 22:04:13 +01:00
|
|
|
int tftp_parseerrpacket(const uint8_t *buffer)
|
|
|
|
{
|
2016-06-13 01:33:49 +02:00
|
|
|
uint16_t opcode = (uint16_t)buffer[0] << 8 | (uint16_t)buffer[1];
|
|
|
|
uint16_t errcode = (uint16_t)buffer[2] << 8 | (uint16_t)buffer[3];
|
|
|
|
FAR const char *errmsg = (const char *)&buffer[4];
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (opcode == TFTP_ERR)
|
|
|
|
{
|
2019-11-04 02:29:20 +01:00
|
|
|
nwarn("WARNING: ERR message: %s (%d)\n", errmsg, errcode);
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2016-06-13 01:33:49 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftp_recvfrom
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* recvfrom helper
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
ssize_t tftp_recvfrom(int sd, void *buf, size_t len,
|
|
|
|
struct sockaddr_in *from)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-01-19 14:59:12 +01:00
|
|
|
socklen_t addrlen;
|
2011-03-19 22:04:13 +01:00
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
/* Loop handles the case where the recvfrom is interrupted by a signal and
|
|
|
|
* we should unconditionally try again.
|
|
|
|
*/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* For debugging, it is helpful to start with a clean buffer */
|
|
|
|
|
2016-06-11 19:50:38 +02:00
|
|
|
#if defined(CONFIG_DEBUG_INFO) && defined(CONFIG_DEBUG_NET)
|
2011-03-19 22:04:13 +01:00
|
|
|
memset(buf, 0, len);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Receive the packet */
|
|
|
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2021-01-19 14:59:12 +01:00
|
|
|
nbytes = recvfrom(sd, buf, len, 0, (struct sockaddr *)from, &addrlen);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* Check for a timeout */
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: recvfrom timed out\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If EINTR, then loop and try again. Other errors are fatal */
|
|
|
|
|
|
|
|
else if (errno != EINTR)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: recvfrom failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No errors? Return the number of bytes received */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftp_sendto
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* sendto helper
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
ssize_t tftp_sendto(int sd, const void *buf, size_t len,
|
|
|
|
struct sockaddr_in *to)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
/* Loop handles the case where the sendto is interrupted by a signal and
|
|
|
|
* we should unconditionally try again.
|
|
|
|
*/
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Send the packet */
|
|
|
|
|
2021-01-19 14:59:12 +01:00
|
|
|
nbytes = sendto(sd, buf, len, 0, (struct sockaddr *)to,
|
|
|
|
sizeof(struct sockaddr_in));
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* If EINTR, then loop and try again. Other errors are fatal */
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: sendto failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No errors? Return the number of bytes received */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|