* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
* ------------------------------------------------------------------------- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-#include <fcntl.h>
-
#include "busybox.h"
#endif
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
static int tftp_blocksize_check(int blocksize, int bufsize)
{
/* Can't use RESERVE_CONFIG_BUFFER here since the allocation
* size varies meaning BUFFERS_GO_ON_STACK would fail */
- char *buf=xmalloc(tftp_bufsize += 4);
+ /* We must keep the transmit and receive buffers seperate */
+ /* In case we rcv a garbage pkt and we need to rexmit the last pkt */
+ char *xbuf = xmalloc(tftp_bufsize += 4);
+ char *rbuf = xmalloc(tftp_bufsize);
if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
- /* need to unlink the localfile, so don't use bb_xsocket here. */
+ /* need to unlink the localfile, so don't use xsocket here. */
bb_perror_msg("socket");
return EXIT_FAILURE;
}
len = sizeof(sa);
memset(&sa, 0, len);
- bb_xbind(socketfd, (struct sockaddr *)&sa, len);
+ xbind(socketfd, (struct sockaddr *)&sa, len);
sa.sin_family = host->h_addrtype;
sa.sin_port = port;
while (1) {
- cp = buf;
+ cp = xbuf;
/* first create the opcode part */
*((unsigned short *) cp) = htons(opcode);
{
int too_long = 0;
- /* see if the filename fits into buf
+ /* see if the filename fits into xbuf
* and fill in packet. */
len = strlen(remotefile) + 1;
- if ((cp + len) >= &buf[tftp_bufsize - 1]) {
+ if ((cp + len) >= &xbuf[tftp_bufsize - 1]) {
too_long = 1;
} else {
safe_strncpy(cp, remotefile, len);
cp += len;
}
- if (too_long || ((&buf[tftp_bufsize - 1] - cp) < MODE_OCTET_LEN)) {
+ if (too_long || ((&xbuf[tftp_bufsize - 1] - cp) < MODE_OCTET_LEN)) {
bb_error_msg("remote filename too long");
break;
}
memcpy(cp, MODE_OCTET, MODE_OCTET_LEN);
cp += MODE_OCTET_LEN;
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
len = tftp_bufsize - 4; /* data block size */
if (len != TFTP_BLOCKSIZE_DEFAULT) {
- if ((&buf[tftp_bufsize - 1] - cp) < 15) {
+ if ((&xbuf[tftp_bufsize - 1] - cp) < 15) {
bb_error_msg("remote filename too long");
break;
}
block_nr++;
if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
- len = bb_full_read(localfd, cp, tftp_bufsize - 4);
+ len = full_read(localfd, cp, tftp_bufsize - 4);
if (len < 0) {
bb_perror_msg(bb_msg_read_error);
timeout = TFTP_NUM_RETRIES; /* re-initialize */
do {
- len = cp - buf;
+ len = cp - xbuf;
-#ifdef CONFIG_DEBUG_TFTP
+#if ENABLE_DEBUG_TFTP
fprintf(stderr, "sending %u bytes\n", len);
- for (cp = buf; cp < &buf[len]; cp++)
+ for (cp = xbuf; cp < &xbuf[len]; cp++)
fprintf(stderr, "%02x ", (unsigned char) *cp);
fprintf(stderr, "\n");
#endif
- if (sendto(socketfd, buf, len, 0,
+ if (sendto(socketfd, xbuf, len, 0,
(struct sockaddr *) &sa, sizeof(sa)) < 0) {
bb_perror_msg("send");
len = -1;
switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
case 1:
- len = recvfrom(socketfd, buf, tftp_bufsize, 0,
+ len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
(struct sockaddr *) &from, &fromlen);
if (len < 0) {
/* process received packet */
- opcode = ntohs(*((unsigned short *) buf));
- tmp = ntohs(*((unsigned short *) &buf[2]));
+ opcode = ntohs(*((unsigned short *) rbuf));
+ tmp = ntohs(*((unsigned short *) &rbuf[2]));
-#ifdef CONFIG_DEBUG_TFTP
+#if ENABLE_DEBUG_TFTP
fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
#endif
if (opcode == TFTP_ERROR) {
const char *msg = NULL;
- if (buf[4] != '\0') {
- msg = &buf[4];
- buf[tftp_bufsize - 1] = '\0';
+ if (rbuf[4] != '\0') {
+ msg = &rbuf[4];
+ rbuf[tftp_bufsize - 1] = '\0';
} else if (tmp < (sizeof(tftp_bb_error_msg)
/ sizeof(char *))) {
break;
}
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
if (want_option_ack) {
want_option_ack = 0;
char *res;
- res = tftp_option_get(&buf[2], len - 2, OPTION_BLOCKSIZE);
+ res = tftp_option_get(&rbuf[2], len - 2, OPTION_BLOCKSIZE);
if (res) {
- int blksize = atoi(res);
+ int blksize = xatoi_u(res);
if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
} else {
opcode = TFTP_ACK;
}
-#ifdef CONFIG_DEBUG_TFTP
+#if ENABLE_DEBUG_TFTP
fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE,
blksize);
#endif
if (tmp == block_nr) {
- len = bb_full_write(localfd, &buf[4], len - 4);
+ len = full_write(localfd, &rbuf[4], len - 4);
if (len < 0) {
bb_perror_msg(bb_msg_write_error);
}
}
-#ifdef CONFIG_FEATURE_CLEAN_UP
+#if ENABLE_FEATURE_CLEAN_UP
close(socketfd);
- free(buf);
+ free(xbuf);
+ free(rbuf);
#endif
return finished ? EXIT_SUCCESS : EXIT_FAILURE;
/* figure out what to pass to getopt */
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
char *sblocksize = NULL;
#define BS "b:"
#define BS_ARG
#endif
-#ifdef CONFIG_FEATURE_TFTP_GET
+#if ENABLE_FEATURE_TFTP_GET
#define GET "g"
#define GET_COMPL ":g"
#else
#define GET_COMPL
#endif
-#ifdef CONFIG_FEATURE_TFTP_PUT
+#if ENABLE_FEATURE_TFTP_PUT
#define PUT "p"
#define PUT_COMPL ":p"
#else
#endif
#if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT)
- bb_opt_complementally = GET_COMPL PUT_COMPL ":?g--p:p--g";
+ opt_complementary = GET_COMPL PUT_COMPL ":?g--p:p--g";
#elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT)
- bb_opt_complementally = GET_COMPL PUT_COMPL;
+ opt_complementary = GET_COMPL PUT_COMPL;
#endif
-
- cmd = bb_getopt_ulflags(argc, argv, GET PUT "l:r:" BS,
- &localfile, &remotefile BS_ARG);
+ cmd = getopt32(argc, argv, GET PUT "l:r:" BS, &localfile, &remotefile BS_ARG);
cmd &= (tftp_cmd_get | tftp_cmd_put);
-#ifdef CONFIG_FEATURE_TFTP_GET
+#if ENABLE_FEATURE_TFTP_GET
if (cmd == tftp_cmd_get)
flags = O_WRONLY | O_CREAT | O_TRUNC;
#endif
-#ifdef CONFIG_FEATURE_TFTP_PUT
+#if ENABLE_FEATURE_TFTP_PUT
if (cmd == tftp_cmd_put)
flags = O_RDONLY;
#endif
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
if (sblocksize) {
- blocksize = atoi(sblocksize);
+ blocksize = xatoi_u(sblocksize);
if (!tftp_blocksize_check(blocksize, 0)) {
return EXIT_FAILURE;
}
if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL))
bb_show_usage();
- if (localfile == NULL || strcmp(localfile, "-") == 0) {
+ if (localfile == NULL || LONE_DASH(localfile)) {
fd = (cmd == tftp_cmd_get) ? STDOUT_FILENO : STDIN_FILENO;
} else {
fd = open(localfile, flags, 0644); /* fail below */
host = xgethostbyname(argv[optind]);
port = bb_lookup_port(argv[optind + 1], "udp", 69);
-#ifdef CONFIG_DEBUG_TFTP
+#if ENABLE_DEBUG_TFTP
fprintf(stderr, "using server \"%s\", remotefile \"%s\", "
"localfile \"%s\".\n",
inet_ntoa(*((struct in_addr *) host->h_addr)),
if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)
unlink(localfile);
}
- return (result);
+ return result;
}