Update README.md
[oweals/nmrpflash.git] / tftp.c
diff --git a/tftp.c b/tftp.c
index 06258dc4259e6967e6cf0fae71000b2a44e729cf..735f992e36e635379c180087657e489b88c8a9c7 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -305,6 +305,8 @@ inline bool tftp_is_valid_filename(const char *filename)
        return strlen(filename) <= 255 && is_netascii(filename);
 }
 
+static const char *spinner = "\\|/-";
+
 int tftp_put(struct nmrpd_args *args)
 {
        struct sockaddr_in addr;
@@ -314,6 +316,9 @@ int tftp_put(struct nmrpd_args *args)
        char rx[2048], tx[2048];
        const char *file_remote = args->file_remote;
        char *val, *end;
+       bool rollover;
+       const unsigned rx_timeout = MAX(args->rx_timeout / (args->blind ? 50 : 5), 2000);
+       const unsigned max_timeouts = args->blind ? 3 : 5;
 
        sock = -1;
        ret = -1;
@@ -366,6 +371,7 @@ int tftp_put(struct nmrpd_args *args)
                xperror("inet_addr");
                goto cleanup;
        }
+
        addr.sin_port = htons(args->port);
 
        blksize = 512;
@@ -373,6 +379,7 @@ int tftp_put(struct nmrpd_args *args)
        last_len = -1;
        len = 0;
        errors = 0;
+       rollover = false;
        /* Not really, but this way the loop sends our WRQ before receiving */
        timeouts = 1;
 
@@ -394,6 +401,10 @@ int tftp_put(struct nmrpd_args *args)
                                                ret = -1;
                                                goto cleanup;
                                        }
+
+                                       if (verbosity) {
+                                               printf("Remote accepted blksize option: %d b\n", blksize);
+                                       }
                                }
                        }
                }
@@ -401,10 +412,16 @@ int tftp_put(struct nmrpd_args *args)
                if (timeouts || ackblock == block) {
                        if (!timeouts) {
                                if (++block == 0) {
-                                       // rollover; skip to block 1
-                                       block = 1;
+                                       if (!rollover) {
+                                               printf("Warning: TFTP block rollover. Upload might fail!\n");
+                                               rollover = true;
+                                       }
                                }
 
+                               printf("%c ", spinner[block & 3]);
+                               fflush(stdout);
+                               printf("\b\b");
+
                                pkt_mknum(tx, DATA);
                                pkt_mknum(tx + 2, block);
                                len = read(fd, tx + 4, blksize);
@@ -439,11 +456,17 @@ int tftp_put(struct nmrpd_args *args)
                        }
                }
 
-               ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 4);
+               ret = tftp_recvfrom(sock, rx, &port, rx_timeout, blksize + 4);
                if (ret < 0) {
                        goto cleanup;
                } else if (!ret) {
-                       if (++timeouts < 5 || (!block && timeouts < 10)) {
+                       if (++timeouts < max_timeouts || (!block && timeouts < (max_timeouts * 4))) {
+                               continue;
+                       } else if (args->blind) {
+                               timeouts = 0;
+                               // fake an ACK packet
+                               pkt_mknum(rx, ACK);
+                               pkt_mknum(rx + 2, block);
                                continue;
                        } else if (block) {
                                fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);