Add and use file_remote
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 12 Feb 2016 13:37:26 +0000 (14:37 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 12 Feb 2016 13:37:26 +0000 (14:37 +0100)
main.c
nmrp.c
nmrpd.h
tftp.c

diff --git a/main.c b/main.c
index 45b406fbcdb668ce608ee308a15eb79e1b6542e7..a2901345165c7c3c591ec65ab79f9ba0dd87bff8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -72,6 +72,7 @@ int main(int argc, char **argv)
                .ul_timeout = 120000,
                .tftpcmd = NULL,
                .file_local = NULL,
+               .file_remote = NULL,
                .ipaddr = NULL,
                .ipmask = "255.255.255.0",
                .intf = NULL,
diff --git a/nmrp.c b/nmrp.c
index e412a4f6f9222f9e1eb7f24e4c881d176524fa18..0bd9b19382e264246221a3c7e937ad54f84bb246 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -315,6 +315,14 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
+       if (args->file_remote) {
+               if (!tftp_is_valid_filename(args->file_remote)) {
+                       fprintf(stderr, "Invalid remote filename '%s'.\n",
+                                       args->file_remote);
+                       return 1;
+               }
+       }
+
        err = 1;
 
        sock = ethsock_create(args->intf, ETH_P_NMRP);
@@ -437,15 +445,21 @@ int nmrp_do(struct nmrpd_args *args)
                                        break;
                                }
 
-                               if (verbosity) {
-                                       len = 0;
-                                       filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
-                                       if (filename) {
-                                               printf("Received upload request for '%.*s'.\n", len,
-                                                               filename);
+                               len = 0;
+                               filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len);
+                               if (filename) {
+                                       if (!args->file_remote) {
+                                               args->file_remote = filename;
+                                       }
+                                       printf("Received upload request: filename '%.*s'.\n",
+                                                       len, filename);
+                               } else if (!args->file_remote) {
+                                       if (tftp_is_valid_filename(args->file_local)) {
+                                               args->file_remote = args->file_local;
                                        } else {
-                                               printf("No filename specified in upload request.");
+                                               args->file_remote = "firmware";
                                        }
+                                       printf("Received upload request with empty filename.");
                                }
 
                                err = 0;
@@ -462,6 +476,11 @@ int nmrp_do(struct nmrpd_args *args)
                                }
 
                                if (!err && args->file_local) {
+                                       if (verbosity) {
+                                               printf("Using remote filename '%s'.\n",
+                                                               args->file_remote);
+                                       }
+
                                        if (!strcmp(args->file_local, "-")) {
                                                printf("Uploading from stdin ... ");
                                        } else {
diff --git a/nmrpd.h b/nmrpd.h
index 1e0997f9781bde83e3eb9f79c4b16d6f8820b8cf..049b6479d15a720b61cc47bf0151d7a2ebb42cee 100644 (file)
--- a/nmrpd.h
+++ b/nmrpd.h
@@ -60,6 +60,7 @@ struct nmrpd_args {
        unsigned ul_timeout;
        const char *tftpcmd;
        const char *file_local;
+       const char *file_remote;
        const char *ipaddr;
        const char *ipmask;
        const char *intf;
@@ -70,6 +71,8 @@ struct nmrpd_args {
 };
 
 int tftp_put(struct nmrpd_args *args);
+bool tftp_is_valid_filename(const char *filename);
+
 int nmrp_do(struct nmrpd_args *args);
 
 int select_fd(int fd, unsigned timeout);
diff --git a/tftp.c b/tftp.c
index e18c0aab665c22aaa618c4e2cb9eee372b25bb57..a15179259166dea89f218194ff2dff1d67b04ee6 100644 (file)
--- a/tftp.c
+++ b/tftp.c
@@ -86,7 +86,7 @@ static void pkt_mkwrq(char *pkt, const char *filename)
        size_t len = 2;
 
        filename = leafname(filename);
-       if (!is_netascii(filename) || strlen(filename) > 500) {
+       if (!tftp_is_valid_filename(filename)) {
                fprintf(stderr, "Overlong/illegal filename; using 'firmware'.\n");
                filename = "firmware";
        } else if (!strcmp(filename, "-")) {
@@ -223,6 +223,11 @@ inline void sock_perror(const char *msg)
 }
 #endif
 
+inline bool tftp_is_valid_filename(const char *filename)
+{
+       return strlen(filename) <= 500 && is_netascii(filename);
+}
+
 int tftp_put(struct nmrpd_args *args)
 {
        struct sockaddr_in addr;
@@ -267,7 +272,7 @@ int tftp_put(struct nmrpd_args *args)
        /* Not really, but this way the loop sends our WRQ before receiving */
        timeout = 1;
 
-       pkt_mkwrq(tx, args->file_local);
+       pkt_mkwrq(tx, args->file_remote);
 
        do {
                if (!timeout && pkt_num(rx) == ACK) {