Add "install" target
[oweals/nmrpflash.git] / tftp.c
diff --git a/tftp.c b/tftp.c
index 4342bc85936566a4b827a0a68d483545282571ad..4470d925bd748ae83c1ddb08f4da1f78947c7af1 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -1,3 +1,22 @@
+/**
+ * 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>
@@ -7,6 +26,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <fcntl.h>
+#include "nmrpd.h"
 
 #define TFTP_PKT_SIZE 516
 
@@ -142,7 +162,7 @@ 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 addr;
        uint16_t block;
@@ -150,7 +170,7 @@ int tftp_put(const char *filename, const char *ipaddr, uint16_t port)
        int fd, sock, err, timeout, last_len;
        char rx[TFTP_PKT_SIZE], tx[TFTP_PKT_SIZE];
 
-       fd = open(filename, O_RDONLY);
+       fd = open(args->filename, O_RDONLY);
        if (fd < 0) {
                perror("open");
                err = fd;
@@ -164,21 +184,21 @@ 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, &addr.sin_addr);
+       err = !inet_aton(args->ipaddr, &addr.sin_addr);
        if (err) {
                perror("inet_aton");
                goto cleanup;
        }
 
        addr.sin_family = AF_INET;
-       addr.sin_port = htons(port);
+       addr.sin_port = htons(args->port);
 
-       pkt_mkwrq(tx, filename, "octet");
+       pkt_mkwrq(tx, args->filename, "octet");
 
        len = tftp_sendto(sock, tx, 0, &addr);
        if (len < 0) {
@@ -237,6 +257,7 @@ int tftp_put(const char *filename, const char *ipaddr, uint16_t port)
                        goto cleanup;
                } else {
                        timeout = 0;
+                       err = 0;
                }
        } while(1);