u_short, ulong exterminated
[oweals/busybox.git] / networking / tftp.c
index b0572c89009c676088b4db32733a3f315eefa5da..2d28973dc6ed52419bc7bd997f8d36aff8ad6ef4 100644 (file)
  * 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"
 
 
@@ -72,7 +60,7 @@ static const char *const tftp_bb_error_msg[] = {
 #endif
 
 
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
 
 static int tftp_blocksize_check(int blocksize, int bufsize)
 {
@@ -156,10 +144,13 @@ static int tftp(const int cmd, const struct hostent *host,
 
        /* 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;
        }
@@ -167,7 +158,7 @@ static int tftp(const int cmd, const struct hostent *host,
        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;
@@ -184,7 +175,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
        while (1) {
 
-               cp = buf;
+               cp = xbuf;
 
                /* first create the opcode part */
                *((unsigned short *) cp) = htons(opcode);
@@ -196,18 +187,18 @@ static int tftp(const int cmd, const struct hostent *host,
                {
                        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;
                        }
@@ -216,13 +207,13 @@ static int tftp(const int cmd, const struct hostent *host,
                        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;
                                }
@@ -249,7 +240,7 @@ static int tftp(const int cmd, const struct hostent *host,
                        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);
@@ -271,15 +262,15 @@ static int tftp(const int cmd, const struct hostent *host,
                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;
@@ -304,7 +295,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
                        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) {
@@ -346,19 +337,19 @@ static int tftp(const int cmd, const struct hostent *host,
 
                /* 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 *))) {
 
@@ -371,7 +362,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
                        break;
                }
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
                if (want_option_ack) {
 
                        want_option_ack = 0;
@@ -382,10 +373,10 @@ static int tftp(const int cmd, const struct hostent *host,
 
                                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)) {
 
@@ -394,7 +385,7 @@ static int tftp(const int cmd, const struct hostent *host,
                                                } else {
                                                        opcode = TFTP_ACK;
                                                }
-#ifdef CONFIG_DEBUG_TFTP
+#if ENABLE_DEBUG_TFTP
                                                fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE,
                                                                blksize);
 #endif
@@ -420,7 +411,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
                        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);
@@ -460,9 +451,10 @@ static int tftp(const int cmd, const struct hostent *host,
                }
        }
 
-#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;
@@ -482,7 +474,7 @@ int tftp_main(int argc, char **argv)
 
        /* figure out what to pass to getopt */
 
-#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
+#if ENABLE_FEATURE_TFTP_BLOCKSIZE
        char *sblocksize = NULL;
 
 #define BS "b:"
@@ -492,7 +484,7 @@ int tftp_main(int argc, char **argv)
 #define BS_ARG
 #endif
 
-#ifdef CONFIG_FEATURE_TFTP_GET
+#if ENABLE_FEATURE_TFTP_GET
 #define GET "g"
 #define GET_COMPL ":g"
 #else
@@ -500,7 +492,7 @@ int tftp_main(int argc, char **argv)
 #define GET_COMPL
 #endif
 
-#ifdef CONFIG_FEATURE_TFTP_PUT
+#if ENABLE_FEATURE_TFTP_PUT
 #define PUT "p"
 #define PUT_COMPL ":p"
 #else
@@ -509,28 +501,26 @@ int tftp_main(int argc, char **argv)
 #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;
                }
@@ -544,7 +534,7 @@ int tftp_main(int argc, char **argv)
        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 */
@@ -556,7 +546,7 @@ int tftp_main(int argc, char **argv)
        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)),
@@ -571,5 +561,5 @@ int tftp_main(int argc, char **argv)
                if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)
                        unlink(localfile);
        }
-       return (result);
+       return result;
 }