Gracefully close the socket
[oweals/nmrpflash.git] / tftp.c
diff --git a/tftp.c b/tftp.c
index f70874b0459d4fe969aa5ed7cb73a468389e194a..80efff0c090839d532c23f35071bce467709c7e8 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -1,16 +1,34 @@
+/**
+ * nmrp-flash - Netgear Unbrick Utility
+ * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
+ *
+ * nmrp-flash is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * nmrp-flash is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with nmrp-flash.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 #define _BSD_SOURCE
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <net/if.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
 #include <fcntl.h>
+#include "nmrpd.h"
 
 #define TFTP_PKT_SIZE 516
 
-static const char *opcode_names[] = { 
+static const char *opcode_names[] = {
        "RRQ", "WRQ", "DATA", "ACK", "ERR"
 };
 
@@ -19,10 +37,40 @@ enum tftp_opcode {
        WRQ  = 2,
        DATA = 3,
        ACK  = 4,
-       ERR  = 5,
-       NETGEAR_ERR = 0x4669
+       ERR  = 5
 };
 
+static char *x_basename(const char *path)
+{
+       char *slash, *bslash;
+
+       slash = rindex(path, '/');
+       bslash = rindex(path, '\\');
+
+       if (slash && bslash) {
+               path = 1 + (slash > bslash ? slash : bslash);
+       } else if (slash) {
+               path = 1 + slash;
+       } else if (bslash) {
+               path = 1 + bslash;
+       }
+
+       return strdup(path);
+}
+
+static const char *sanitize_netascii(char *str)
+{
+       char *p = str;
+
+       for (; *p; ++p) {
+               if (*p < 0x20 || *p > 0x7f) {
+                       *p = '_';
+               }
+       }
+
+       return str;
+}
+
 static inline void pkt_mknum(char *pkt, uint16_t n)
 {
        *(uint16_t*)pkt = htons(n);
@@ -62,8 +110,6 @@ static inline void pkt_print(char *pkt, FILE *fp)
 
 static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
 {
-       static int fail = 0;
-       socklen_t socklen;
        ssize_t len;
 
        len = recvfrom(sock, pkt, TFTP_PKT_SIZE, 0, NULL, NULL);
@@ -92,7 +138,7 @@ static ssize_t tftp_recvfrom(int sock, char *pkt, struct sockaddr_in *src)
        return len;
 }
 
-static ssize_t tftp_sendto(int sock, char *pkt, size_t len, 
+static ssize_t tftp_sendto(int sock, char *pkt, size_t len,
                struct sockaddr_in *dst)
 {
        ssize_t sent;
@@ -133,7 +179,7 @@ int sock_set_rx_timeout(int fd, unsigned msec)
        if (msec) {
                tv.tv_usec = (msec % 1000) * 1000;
                tv.tv_sec = msec / 1000;
-               if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
+               if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)) < 0) {
                        perror("setsockopt(SO_RCVTIMEO)");
                        return 1;
                }
@@ -142,17 +188,19 @@ int sock_set_rx_timeout(int fd, unsigned msec)
        return 0;
 }
 
-int tftp_put(const char *filename, const char *ipaddr, uint16_t port)
+int tftp_put(struct nmrpd_args *args)
 {
-       struct sockaddr_in dst, src;
-       enum tftp_opcode opcode;
-       struct timeval tv;
+       struct sockaddr_in addr;
+       char *filename;
        uint16_t block;
        ssize_t len;
-       int fd, sock, err, done, i, last_len;
-       char pkt[TFTP_PKT_SIZE];
+       int fd, sock, err, timeout, last_len;
+       char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
 
-       fd = open(filename, O_RDONLY);
+       sock = -1;
+       filename = NULL;
+
+       fd = open(args->filename, O_RDONLY);
        if (fd < 0) {
                perror("open");
                err = fd;
@@ -166,88 +214,112 @@ int tftp_put(const char *filename, const char *ipaddr, uint16_t port)
                goto cleanup;
        }
 
-       err = sock_set_rx_timeout(sock, 999);
+       err = sock_set_rx_timeout(sock, args->rx_timeout);
        if (err) {
                goto cleanup;
        }
 
-       err = !inet_aton(ipaddr, &dst.sin_addr);
-       if (err) {
-               perror("inet_aton");
+       if ((addr.sin_addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) {
+               perror("inet_addr");
+               goto cleanup;
+       }
+
+       addr.sin_family = AF_INET;
+       addr.sin_port = htons(args->port);
+
+       filename = x_basename(args->filename);
+       if (!filename) {
+               perror("x_basename");
+               goto cleanup;
+       } else if (strlen(filename) > 256) {
+               fprintf(stderr, "Filename exceeds maximum of 256 characters.\n");
                goto cleanup;
        }
 
-       dst.sin_family = AF_INET;
-       dst.sin_port = htons(port);
+       sanitize_netascii(filename);
+       if (verbosity > 1) {
+               printf("%s -> %s\n", args->filename, filename);
+       }
 
-       pkt_mkwrq(pkt, filename, "octet");
+       pkt_mkwrq(tx, filename, "octet");
 
-       len = tftp_sendto(sock, pkt, 0, &dst);
+       len = tftp_sendto(sock, tx, 0, &addr);
        if (len < 0) {
                err = len;
                goto cleanup;
        }
 
-       len = tftp_recvfrom(sock, pkt, &dst);
+       len = tftp_recvfrom(sock, rx, &addr);
        if (len < 0) {
                err = len;
                goto cleanup;
        }
 
-       //dst.sin_port = src.sin_port;
-
+       timeout = 0;
        block = 0;
-       done = 0;
        last_len = -1;
 
        do {
-               if (pkt_num(pkt) == ACK && pkt_num(pkt + 2) == block) {
-                       ++block;
-                       pkt_mknum(pkt, DATA);
-                       pkt_mknum(pkt + 2, block);
-                       len = read(fd, pkt + 4, 512);
-                       if (len < 0) {
-                               perror("read");
-                               err = len;
-                               goto cleanup;
-                       } else if (!len) {
-                               done = last_len != 512;
+               if (timeout || (pkt_num(rx) == ACK && pkt_num(rx + 2) == block)) {
+                       if (!timeout) {
+                               ++block;
+                               pkt_mknum(tx, DATA);
+                               pkt_mknum(tx + 2, block);
+                               len = read(fd, tx + 4, 512);
+                               if (len < 0) {
+                                       perror("read");
+                                       err = len;
+                                       goto cleanup;
+                               } else if (!len) {
+                                       if (last_len != 512) {
+                                               break;
+                                       }
+                               }
+
+                               last_len = len;
                        }
 
-                       last_len = len;
-
-                       len = tftp_sendto(sock, pkt, len, &dst);
-                       if (len < 0) {
-                               err = len;
+                       err = tftp_sendto(sock, tx, len, &addr);
+                       if (err < 0) {
                                goto cleanup;
                        }
-               } else {
+               } else if (pkt_num(rx) != ACK) {
                        fprintf(stderr, "Expected ACK(%d), got ", block);
-                       pkt_print(pkt, stderr);
+                       pkt_print(rx, stderr);
                        fprintf(stderr, "!\n");
-                       err = 1;
-                       goto cleanup;
                }
 
-               len = tftp_recvfrom(sock, pkt, &dst);
-               if (len < 0) {
-                       if (len == -2) {
-                               fprintf(stderr, "Timeout while waiting for ACK(%d).\n", block);
+               err = tftp_recvfrom(sock, rx, &addr);
+               if (err < 0) {
+                       if (err == -2) {
+                               if (++timeout < 5) {
+                                       continue;
+                               }
+                               fprintf(stderr, "Timeout while waiting for ACK(%d)\n.", block);
                        }
-                       err = len;
                        goto cleanup;
+               } else {
+                       timeout = 0;
+                       err = 0;
                }
-       } while(!done);
+       } while(1);
 
        err = 0;
 
 cleanup:
+       free(filename);
+
        if (fd >= 0) {
                close(fd);
        }
 
        if (sock >= 0) {
+               shutdown(sock, SHUT_RDWR);
+#ifndef NMRPFLASH_WINDOWS
                close(sock);
+#else
+               closesocket(sock);
+#endif
        }
 
        return err;