Update README.md
[oweals/nmrpflash.git] / tftp.c
diff --git a/tftp.c b/tftp.c
index f5ad35a75c996c7bf8c6d61e7bd69e614fae9d22..735f992e36e635379c180087657e489b88c8a9c7 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -30,7 +30,7 @@
 #define O_BINARY 0
 #endif
 
-#define TFTP_PKT_SIZE 516
+#define TFTP_BLKSIZE 1456
 
 static const char *opcode_names[] = {
        "RRQ", "WRQ", "DATA", "ACK", "ERR", "OACK"
@@ -72,7 +72,7 @@ static inline uint16_t pkt_num(char *pkt)
 static char *pkt_mkopt(char *pkt, const char *opt, const char* val)
 {
        strcpy(pkt, opt);
-       pkt += strlen(pkt) + 1;
+       pkt += strlen(opt) + 1;
        strcpy(pkt, val);
        pkt += strlen(val) + 1;
        return pkt;
@@ -111,7 +111,7 @@ static char *pkt_optval(char* pkt, const char* name)
        pkt += 2;
 
        while (pkt_nextopt(&pkt, &opt, &val, &rem)) {
-               if (!strcmp(name, opt)) {
+               if (!strcasecmp(name, opt)) {
                        return val;
                }
        }
@@ -133,8 +133,6 @@ static size_t pkt_xrqlen(char *pkt)
 
 static void pkt_mkwrq(char *pkt, const char *filename, unsigned blksize)
 {
-       size_t len = 2;
-
        filename = leafname(filename);
        if (!tftp_is_valid_filename(filename)) {
                fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
@@ -275,6 +273,10 @@ static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
 
 const char *leafname(const char *path)
 {
+       if (!path) {
+               return NULL;
+       }
+
        const char *slash, *bslash;
 
        slash = strrchr(path, '/');
@@ -300,18 +302,23 @@ void sock_perror(const char *msg)
 
 inline bool tftp_is_valid_filename(const char *filename)
 {
-       return strlen(filename) <= 500 && is_netascii(filename);
+       return strlen(filename) <= 255 && is_netascii(filename);
 }
 
+static const char *spinner = "\\|/-";
+
 int tftp_put(struct nmrpd_args *args)
 {
        struct sockaddr_in addr;
        uint16_t block, port, op, blksize;
        ssize_t len, last_len;
-       int fd, sock, ret, timeout, errors, ackblock;
+       int fd, sock, ret, timeouts, errors, ackblock;
        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;
@@ -364,6 +371,7 @@ int tftp_put(struct nmrpd_args *args)
                xperror("inet_addr");
                goto cleanup;
        }
+
        addr.sin_port = htons(args->port);
 
        blksize = 512;
@@ -371,34 +379,49 @@ 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 */
-       timeout = 1;
+       timeouts = 1;
 
-       pkt_mkwrq(tx, file_remote, 1456);
+       pkt_mkwrq(tx, file_remote, TFTP_BLKSIZE);
 
        while (!g_interrupted) {
                ackblock = -1;
                op = pkt_num(rx);
 
-               if (!timeout) {
+               if (!timeouts) {
                        if (op == ACK) {
                                ackblock = pkt_num(rx + 2);
                        } else if (op == OACK) {
                                ackblock = 0;
                                if ((val = pkt_optval(rx, "blksize"))) {
                                        blksize = strtol(val, &end, 10);
-                                       if (!blksize || (*end != '\0')) {
+                                       if (*end != '\0' || blksize < 8 || blksize > TFTP_BLKSIZE) {
                                                fprintf(stderr, "Error: invalid blksize in OACK: %s\n", val);
                                                ret = -1;
                                                goto cleanup;
                                        }
+
+                                       if (verbosity) {
+                                               printf("Remote accepted blksize option: %d b\n", blksize);
+                                       }
                                }
                        }
                }
 
-               if (timeout || ackblock == block) {
-                       if (!timeout) {
-                               ++block;
+               if (timeouts || ackblock == block) {
+                       if (!timeouts) {
+                               if (++block == 0) {
+                                       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);
@@ -433,21 +456,27 @@ int tftp_put(struct nmrpd_args *args)
                        }
                }
 
-               ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 2);
+               ret = tftp_recvfrom(sock, rx, &port, rx_timeout, blksize + 4);
                if (ret < 0) {
                        goto cleanup;
                } else if (!ret) {
-                       if (++timeout < 5 || (!block && timeout < 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);
                        } else {
-                               fprintf(stderr, "Timeout while waiting for initial reply.\n");
+                               fprintf(stderr, "Timeout while waiting for ACK(0)/OACK.\n");
                        }
                        ret = -1;
                        goto cleanup;
                } else {
-                       timeout = 0;
+                       timeouts = 0;
                        ret = 0;
 
                        if (!block && port != args->port) {