Bump version
[oweals/nmrpflash.git] / tftp.c
diff --git a/tftp.c b/tftp.c
index f5ad35a75c996c7bf8c6d61e7bd69e614fae9d22..bf74650b42340093dcc83339b0e98f3387447f23 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"
@@ -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");
@@ -300,7 +298,7 @@ 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);
 }
 
 int tftp_put(struct nmrpd_args *args)
@@ -308,7 +306,7 @@ 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;
@@ -372,22 +370,22 @@ int tftp_put(struct nmrpd_args *args)
        len = 0;
        errors = 0;
        /* 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;
@@ -396,9 +394,11 @@ int tftp_put(struct nmrpd_args *args)
                        }
                }
 
-               if (timeout || ackblock == block) {
-                       if (!timeout) {
+               if (timeouts || ackblock == block) {
+                       if (!timeouts) {
+                               // TODO: set block to 1 if ++block == 0 ?
                                ++block;
+
                                pkt_mknum(tx, DATA);
                                pkt_mknum(tx + 2, block);
                                len = read(fd, tx + 4, blksize);
@@ -433,21 +433,21 @@ int tftp_put(struct nmrpd_args *args)
                        }
                }
 
-               ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 2);
+               ret = tftp_recvfrom(sock, rx, &port, args->rx_timeout, blksize + 4);
                if (ret < 0) {
                        goto cleanup;
                } else if (!ret) {
-                       if (++timeout < 5 || (!block && timeout < 10)) {
+                       if (++timeouts < 5 || (!block && timeouts < 10)) {
                                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) {