From 54e4724b9aa619dfae9be482d8d48722c7fcf850 Mon Sep 17 00:00:00 2001 From: "Joseph C. Lehner" Date: Fri, 12 Feb 2016 14:37:26 +0100 Subject: [PATCH] Add and use file_remote --- main.c | 1 + nmrp.c | 33 ++++++++++++++++++++++++++------- nmrpd.h | 3 +++ tftp.c | 9 +++++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 45b406f..a290134 100644 --- 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 e412a4f..0bd9b19 100644 --- 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 1e0997f..049b647 100644 --- 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 e18c0aa..a151792 100644 --- 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) { -- 2.25.1