small ipv6 doc changes; nslookup a tiny bit smaller
[oweals/busybox.git] / networking / tftp.c
index e1b6f403c74575cbc3a96388f395a8fb3376bd5b..6213d662271a2d6698312ecb347a5c78b44daa46 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"
 
 
 #define TFTP_TIMEOUT 5 /* seconds */
 #define TFTP_NUM_RETRIES 5 /* number of retries */
 
-/* RFC2348 says between 8 and 65464 */
-#define TFTP_OCTECTS_MIN 8
-#define TFTP_OCTECTS_MAX 65464
-
 static const char * const MODE_OCTET = "octet";
 #define MODE_OCTET_LEN 6 /* sizeof(MODE_OCTET)*/
 
@@ -87,7 +71,7 @@ static int tftp_blocksize_check(int blocksize, int bufsize)
         */
 
        if ((bufsize && (blocksize > bufsize)) ||
-               (blocksize < TFTP_OCTECTS_MIN) || (blocksize > TFTP_OCTECTS_MAX)) {
+               (blocksize < 8) || (blocksize > 65564)) {
                bb_error_msg("bad blocksize");
                return 0;
        }
@@ -95,7 +79,7 @@ static int tftp_blocksize_check(int blocksize, int bufsize)
        return blocksize;
 }
 
-static char *tftp_option_get(char *buf, int len, const char const *option)
+static char *tftp_option_get(char *buf, int len, const char * const option)
 {
        int opt_val = 0;
        int opt_found = 0;
@@ -142,8 +126,6 @@ static int tftp(const int cmd, const struct hostent *host,
                   const char *remotefile, const int localfd,
                   const unsigned short port, int tftp_bufsize)
 {
-#define cmd_get  cmd & tftp_cmd_get
-#define cmd_put  cmd & tftp_cmd_put
        struct sockaddr_in sa;
        struct sockaddr_in from;
        struct timeval tv;
@@ -165,7 +147,7 @@ static int tftp(const int cmd, const struct hostent *host,
        char *buf=xmalloc(tftp_bufsize += 4);
 
        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;
        }
@@ -173,7 +155,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;
@@ -181,10 +163,10 @@ static int tftp(const int cmd, const struct hostent *host,
                   sizeof(sa.sin_addr));
 
        /* build opcode */
-       if (cmd_get) {
+       if (cmd & tftp_cmd_get) {
                opcode = TFTP_RRQ;
        }
-       if (cmd_put) {
+       if (cmd & tftp_cmd_put) {
                opcode = TFTP_WRQ;
        }
 
@@ -197,8 +179,8 @@ static int tftp(const int cmd, const struct hostent *host,
                cp += 2;
 
                /* add filename and mode */
-               if (((cmd_get) && (opcode == TFTP_RRQ)) ||
-                       ((cmd_put) && (opcode == TFTP_WRQ)))
+               if (((cmd & tftp_cmd_get) && (opcode == TFTP_RRQ)) ||
+                       ((cmd & tftp_cmd_put) && (opcode == TFTP_WRQ)))
                {
                        int too_long = 0;
 
@@ -245,8 +227,8 @@ static int tftp(const int cmd, const struct hostent *host,
 
                /* add ack and data */
 
-               if (((cmd_get) && (opcode == TFTP_ACK)) ||
-                       ((cmd_put) && (opcode == TFTP_DATA))) {
+               if (((cmd & tftp_cmd_get) && (opcode == TFTP_ACK)) ||
+                       ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA))) {
 
                        *((unsigned short *) cp) = htons(block_nr);
 
@@ -254,8 +236,8 @@ static int tftp(const int cmd, const struct hostent *host,
 
                        block_nr++;
 
-                       if ((cmd_put) && (opcode == TFTP_DATA)) {
-                               len = bb_full_read(localfd, cp, tftp_bufsize - 4);
+                       if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
+                               len = full_read(localfd, cp, tftp_bufsize - 4);
 
                                if (len < 0) {
                                        bb_perror_msg(bb_msg_read_error);
@@ -279,7 +261,7 @@ static int tftp(const int cmd, const struct hostent *host,
 
                        len = cp - buf;
 
-#ifdef CONFIG_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_DEBUG_TFTP
                        fprintf(stderr, "sending %u bytes\n", len);
                        for (cp = buf; cp < &buf[len]; cp++)
                                fprintf(stderr, "%02x ", (unsigned char) *cp);
@@ -355,7 +337,7 @@ static int tftp(const int cmd, const struct hostent *host,
                opcode = ntohs(*((unsigned short *) buf));
                tmp = ntohs(*((unsigned short *) &buf[2]));
 
-#ifdef CONFIG_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_DEBUG_TFTP
                fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
 #endif
 
@@ -391,16 +373,16 @@ static int tftp(const int cmd, const struct hostent *host,
                                res = tftp_option_get(&buf[2], len - 2, OPTION_BLOCKSIZE);
 
                                if (res) {
-                                       int blksize = atoi(res);
+                                       int blksize = xatoi_u(res);
 
                                        if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
 
-                                               if (cmd_put) {
+                                               if (cmd & tftp_cmd_put) {
                                                        opcode = TFTP_DATA;
                                                } else {
                                                        opcode = TFTP_ACK;
                                                }
-#ifdef CONFIG_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_DEBUG_TFTP
                                                fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE,
                                                                blksize);
 #endif
@@ -422,11 +404,11 @@ static int tftp(const int cmd, const struct hostent *host,
                }
 #endif
 
-               if ((cmd_get) && (opcode == TFTP_DATA)) {
+               if ((cmd & tftp_cmd_get) && (opcode == TFTP_DATA)) {
 
                        if (tmp == block_nr) {
 
-                               len = bb_full_write(localfd, &buf[4], len - 4);
+                               len = full_write(localfd, &buf[4], len - 4);
 
                                if (len < 0) {
                                        bb_perror_msg(bb_msg_write_error);
@@ -453,7 +435,7 @@ static int tftp(const int cmd, const struct hostent *host,
                        }
                }
 
-               if ((cmd_put) && (opcode == TFTP_ACK)) {
+               if ((cmd & tftp_cmd_put) && (opcode == TFTP_ACK)) {
 
                        if (tmp == (unsigned short) (block_nr - 1)) {
                                if (finished) {
@@ -515,16 +497,12 @@ 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;
-#else
-#error "Either CONFIG_FEATURE_TFTP_GET or CONFIG_FEATURE_TFTP_PUT must be defined"
+       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
@@ -538,7 +516,7 @@ int tftp_main(int argc, char **argv)
 
 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
        if (sblocksize) {
-               blocksize = atoi(sblocksize);
+               blocksize = xatoi_u(sblocksize);
                if (!tftp_blocksize_check(blocksize, 0)) {
                        return EXIT_FAILURE;
                }
@@ -564,7 +542,7 @@ int tftp_main(int argc, char **argv)
        host = xgethostbyname(argv[optind]);
        port = bb_lookup_port(argv[optind + 1], "udp", 69);
 
-#ifdef CONFIG_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_DEBUG_TFTP
        fprintf(stderr, "using server \"%s\", remotefile \"%s\", "
                        "localfile \"%s\".\n",
                        inet_ntoa(*((struct in_addr *) host->h_addr)),
@@ -573,12 +551,11 @@ int tftp_main(int argc, char **argv)
 
        result = tftp(cmd, host, remotefile, fd, port, blocksize);
 
-#ifdef CONFIG_FEATURE_CLEAN_UP
        if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) {
-               close(fd);
+               if (ENABLE_FEATURE_CLEAN_UP)
+                       close(fd);
+               if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)
+                       unlink(localfile);
        }
-#endif
-       if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)
-               unlink(localfile);
-       return (result);
+       return result;
 }