httpd: trivial simplification
[oweals/busybox.git] / networking / tftp.c
index 1f1dfff714135a5588fe539c0040401935a283d4..59f53ae4a735e026566667ac1e7fed221970268d 100644 (file)
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  * ------------------------------------------------------------------------- */
 
-#include "busybox.h"
-
+#include "libbb.h"
 
 #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
 
-#define TFTP_BLOCKSIZE_DEFAULT 512     /* according to RFC 1350, don't change */
-#define TFTP_TIMEOUT 5 /* seconds */
-#define TFTP_NUM_RETRIES 5 /* number of retries */
+#define TFTP_BLOCKSIZE_DEFAULT 512      /* according to RFC 1350, don't change */
+#define TFTP_TIMEOUT_MS         50
+#define TFTP_MAXTIMEOUT_MS    2000
+#define TFTP_NUM_RETRIES        12      /* number of backed-off retries */
 
 /* opcodes we support */
 #define TFTP_RRQ   1
 #define TFTP_OACK  6
 
 #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
-#define USE_GETPUT(a)
+#define USE_GETPUT(...)
 #define CMD_GET(cmd) 1
 #define CMD_PUT(cmd) 0
 #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
-#define USE_GETPUT(a)
+#define USE_GETPUT(...)
 #define CMD_GET(cmd) 0
 #define CMD_PUT(cmd) 1
 #else
-#define USE_GETPUT(a) a
+#define USE_GETPUT(...) __VA_ARGS__
 /* masks coming from getpot32 */
 #define CMD_GET(cmd) ((cmd) & 1)
 #define CMD_PUT(cmd) ((cmd) & 2)
@@ -109,17 +109,13 @@ static char *tftp_option_get(char *buf, int len, const char *option)
 
 #endif
 
-static int tftp(
-#if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
-               const int cmd,
-#endif
+static int tftp( USE_GETPUT(const int cmd,)
                len_and_sockaddr *peer_lsa,
                const char *remotefile, const int localfd,
                unsigned port, int tftp_bufsize)
 {
-       struct timeval tv;
-       fd_set rfds;
-       int socketfd;
+       struct pollfd pfd[1];
+#define socketfd (pfd[0].fd)
        int len;
        int send_len;
        USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
@@ -127,7 +123,7 @@ static int tftp(
        uint16_t opcode;
        uint16_t block_nr = 1;
        uint16_t recv_blk;
-       int timeout = TFTP_NUM_RETRIES;
+       int retries, waittime_ms;
        char *cp;
 
        unsigned org_port;
@@ -181,6 +177,9 @@ static int tftp(
        /* First packet is built, so skip packet generation */
        goto send_pkt;
 
+       /* Using mostly goto's - continue/break will be less clear
+        * in where we actually jump to */
+
        while (1) {
                /* Build ACK or DATA */
                cp = xbuf + 2;
@@ -203,67 +202,71 @@ static int tftp(
  send_pkt:
                /* Send packet */
                *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */
-               timeout = TFTP_NUM_RETRIES;     /* re-initialize */
-               while (1) {
-                       send_len = cp - xbuf;
-                       /* nb: need to preserve send_len value in code below
-                        * for potential resend! */
+               send_len = cp - xbuf;
+               /* NB: send_len value is preserved in code below
+                * for potential resend */
+
+               retries = TFTP_NUM_RETRIES;     /* re-initialize */
+               waittime_ms = TFTP_TIMEOUT_MS;
+
  send_again:
 #if ENABLE_DEBUG_TFTP
-                       fprintf(stderr, "sending %u bytes\n", send_len);
-                       for (cp = xbuf; cp < &xbuf[send_len]; cp++)
-                               fprintf(stderr, "%02x ", (unsigned char) *cp);
-                       fprintf(stderr, "\n");
+               fprintf(stderr, "sending %u bytes\n", send_len);
+               for (cp = xbuf; cp < &xbuf[send_len]; cp++)
+                       fprintf(stderr, "%02x ", (unsigned char) *cp);
+               fprintf(stderr, "\n");
 #endif
-                       xsendto(socketfd, xbuf, send_len, &peer_lsa->sa, peer_lsa->len);
-                       /* Was it final ACK? then exit */
-                       if (finished && (opcode == TFTP_ACK))
-                               goto ret;
+               xsendto(socketfd, xbuf, send_len, &peer_lsa->sa, peer_lsa->len);
+               /* Was it final ACK? then exit */
+               if (finished && (opcode == TFTP_ACK))
+                       goto ret;
 
-                       /* Receive packet */
  recv_again:
-                       tv.tv_sec = TFTP_TIMEOUT;
-                       tv.tv_usec = 0;
-                       FD_ZERO(&rfds);
-                       FD_SET(socketfd, &rfds);
-                       switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
-                               unsigned from_port;
-                       case 1:
-                               from->len = peer_lsa->len;
-                               memset(&from->sa, 0, peer_lsa->len);
-                               len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
-                                                       &from->sa, &from->len);
-                               if (len < 0) {
-                                       bb_perror_msg("recvfrom");
-                                       goto ret;
-                               }
-                               from_port = get_nport(&from->sa);
-                               if (port == org_port) {
-                                       /* Our first query went to port 69
-                                        * but reply will come from different one.
-                                        * Remember and use this new port */
-                                       port = from_port;
-                                       set_nport(peer_lsa, from_port);
-                               }
-                               if (port != from_port)
-                                       goto recv_again;
-                               goto recvd_good;
-                       case 0:
-                               timeout--;
-                               if (timeout == 0) {
-                                       bb_error_msg("last timeout");
-                                       goto ret;
-                               }
-                               bb_error_msg("last timeout" + 5);
-                               goto send_again; /* resend last sent pkt */
-                       default:
-                               bb_perror_msg("select");
+               /* Receive packet */
+               /*pfd[0].fd = socketfd;*/
+               pfd[0].events = POLLIN;
+               switch (safe_poll(pfd, 1, waittime_ms)) {
+                       unsigned from_port;
+               case 1:
+                       from->len = peer_lsa->len;
+                       memset(&from->sa, 0, peer_lsa->len);
+                       len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
+                                               &from->sa, &from->len);
+                       if (len < 0) {
+                               bb_perror_msg("recvfrom");
+                               goto ret;
+                       }
+                       from_port = get_nport(&from->sa);
+                       if (port == org_port) {
+                               /* Our first query went to port 69
+                                * but reply will come from different one.
+                                * Remember and use this new port */
+                               port = from_port;
+                               set_nport(peer_lsa, from_port);
+                       }
+                       if (port != from_port)
+                               goto recv_again;
+                       goto process_pkt;
+               case 0:
+                       retries--;
+                       if (retries == 0) {
+                               bb_error_msg("timeout");
                                goto ret;
                        }
-               } /* while we don't see recv packet with correct port# */
 
+                       /* exponential backoff with limit */
+                       waittime_ms += waittime_ms/2;
+                       if (waittime_ms > TFTP_MAXTIMEOUT_MS) {
+                               waittime_ms = TFTP_MAXTIMEOUT_MS;
+                       }
+
+                       goto send_again; /* resend last sent pkt */
+               default:
+                       /*bb_perror_msg("poll"); - done in safe_poll */
+                       goto ret;
+               }
+ process_pkt:
                /* Process recv'ed packet */
- recvd_good:
                opcode = ntohs( ((uint16_t*)rbuf)[0] );
                recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
 
@@ -281,15 +284,15 @@ static int tftp(
                                "unknown transfer id",
                                "file already exists",
                                "no such user",
+                               "bad option"
                        };
-                       enum { NUM_ERRCODE = sizeof(errcode_str) / sizeof(errcode_str[0]) };
 
                        const char *msg = "";
 
                        if (rbuf[4] != '\0') {
                                msg = &rbuf[4];
                                rbuf[tftp_bufsize - 1] = '\0';
-                       } else if (recv_blk < NUM_ERRCODE) {
+                       } else if (recv_blk < ARRAY_SIZE(errcode_str)) {
                                msg = errcode_str[recv_blk];
                        }
                        bb_error_msg("server error: (%u) %s", recv_blk, msg);
@@ -308,8 +311,13 @@ static int tftp(
                                if (res) {
                                        int blksize = xatoi_u(res);
                                        if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
+                                               /* send ERROR 8 to server... */
+                                               /* htons can be impossible to use in const initializer: */
+                                               /*static const uint16_t error_8[2] = { htons(TFTP_ERROR), htons(8) };*/
+                                               /* thus we open-code big-endian layout */
+                                               static const uint8_t error_8[4] = { 0,TFTP_ERROR, 0,8 };
+                                               xsendto(socketfd, error_8, 4, &peer_lsa->sa, peer_lsa->len);
                                                bb_error_msg("server proposes bad blksize %d, exiting", blksize);
-                                               // FIXME: must also send ERROR 8 to server...
                                                goto ret;
                                        }
 #if ENABLE_DEBUG_TFTP
@@ -317,7 +325,8 @@ static int tftp(
                                                        blksize);
 #endif
                                        tftp_bufsize = blksize + 4;
-                                       block_nr = 0; // TODO: explain why???
+                                       /* Send ACK for OACK ("block" no: 0) */
+                                       block_nr = 0;
                                        continue;
                                }
                                /* rfc2347:
@@ -350,7 +359,7 @@ static int tftp(
                                /* Server lost our TFTP_ACK.  Resend it */
                                block_nr = recv_blk;
                                continue;
-                       } 
+                       }
                }
 
                if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
@@ -402,7 +411,7 @@ int tftp_main(int argc, char **argv)
        opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
                        USE_GETPUT("?g--p:p--g");
 
-       USE_GETPUT(cmd =) getopt32(argc, argv,
+       USE_GETPUT(cmd =) getopt32(argv,
                        USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
                                "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
                        &localfile, &remotefile
@@ -430,9 +439,8 @@ int tftp_main(int argc, char **argv)
        if (!remotefile || !argv[0])
                bb_show_usage();
 
-       if (LONE_DASH(localfile)) {
-               fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
-       } else {
+       fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
+       if (!LONE_DASH(localfile)) {
                fd = xopen(localfile, flags);
        }
 
@@ -440,17 +448,12 @@ int tftp_main(int argc, char **argv)
        peer_lsa = xhost2sockaddr(argv[0], port);
 
 #if ENABLE_DEBUG_TFTP
-       fprintf(stderr, "using server \"%s\", "
-                       "remotefile \"%s\", localfile \"%s\"\n",
-                       xmalloc_sockaddr2dotted(&peer_lsa->sa, peer_lsa->len),
+       fprintf(stderr, "using server '%s', remotefile '%s', localfile '%s'\n",
+                       xmalloc_sockaddr2dotted(&peer_lsa->sa),
                        remotefile, localfile);
 #endif
 
-       result = tftp(
-#if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
-               cmd,
-#endif
-               peer_lsa, remotefile, fd, port, blocksize);
+       result = tftp( USE_GETPUT(cmd,) peer_lsa, remotefile, fd, port, blocksize);
 
        if (ENABLE_FEATURE_CLEAN_UP)
                close(fd);