Some changes related to "-f -"
[oweals/nmrpflash.git] / nmrp.c
diff --git a/nmrp.c b/nmrp.c
index 77ed6470ee14c6547295b2448ede5ba6b3af4ef9..66c166713a988646f0cc3d44dee56fb16ab26903 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
@@ -1,19 +1,19 @@
 /**
- * nmrp-flash - Netgear Unbrick Utility
+ * nmrpflash - 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
+ * nmrpflash 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,
+ * nmrpflash 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/>.
+ * along with nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
 
@@ -213,8 +213,9 @@ static int pkt_recv(struct ethsock *sock, struct nmrp_pkt *pkt)
        msg_hdr_ntoh(&pkt->msg);
        len = pkt->msg.len + sizeof(pkt->eh);
 
-       if (bytes != len) {
-               fprintf(stderr, "Unexpected message length (%d bytes).\n", (int)len);
+       if (bytes < len) {
+               fprintf(stderr, "Short packet (expected %d, got %d).\n",
+                               (int)len, (int)bytes);
                return 1;
        }
 
@@ -289,7 +290,7 @@ int nmrp_do(struct nmrpd_args *args)
                return 1;
        }
 
-       if (access(args->filename, R_OK) == -1) {
+       if (strcmp(args->filename, "-") && access(args->filename, R_OK) == -1) {
                fprintf(stderr, "Error accessing file '%s'.\n", args->filename);
                return 1;
        }
@@ -380,11 +381,8 @@ int nmrp_do(struct nmrpd_args *args)
 
                switch (rx.msg.code) {
                        case NMRP_C_ADVERTISE:
-                               printf("Received NMRP advertisement from "
-                                               "%02x:%02x:%02x:%02x:%02x:%02x.\n",
-                                               rx.eh.ether_shost[0], rx.eh.ether_shost[1],
-                                               rx.eh.ether_shost[2], rx.eh.ether_shost[3],
-                                               rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
+                               printf("Received NMRP advertisement from %s.\n",
+                                               mac_to_str(rx.eh.ether_shost));
                                err = 1;
                                goto out;
                        case NMRP_C_CONF_REQ:
@@ -402,11 +400,8 @@ int nmrp_do(struct nmrpd_args *args)
 
                                expect = NMRP_C_TFTP_UL_REQ;
 
-                               printf("Received configuration request from "
-                                               "%02x:%02x:%02x:%02x:%02x:%02x.\n",
-                                               rx.eh.ether_shost[0], rx.eh.ether_shost[1],
-                                               rx.eh.ether_shost[2], rx.eh.ether_shost[3],
-                                               rx.eh.ether_shost[4], rx.eh.ether_shost[5]);
+                               printf("Received configuration request from %s.\n",
+                                               mac_to_str(rx.eh.ether_shost));
 
                                memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6);
 
@@ -422,14 +417,27 @@ int nmrp_do(struct nmrpd_args *args)
                                        break;
                                }
 
-                               if (!args->tftpcmd) {
-                                       printf("Uploading %s ... ", args->filename);
-                                       fflush(stdout);
-                                       err = tftp_put(args);
-                               } else {
-                                       printf("Running %s ... ", args->tftpcmd);
+                               err = 0;
+
+                               if (args->tftpcmd) {
+                                       printf("Executing '%s' ... ", args->tftpcmd);
                                        fflush(stdout);
                                        err = system(args->tftpcmd);
+                                       if (!err) {
+                                               printf("OK\n");
+                                       } else {
+                                               printf("ERR\n");
+                                       }
+                               }
+
+                               if (!err && args->filename) {
+                                       if (!strcmp(args->filename, "-")) {
+                                               printf("Uploading from stdin ... ");
+                                       } else {
+                                               printf("Uploading %s ... ", args->filename);
+                                       }
+                                       fflush(stdout);
+                                       err = tftp_put(args);
                                }
 
                                if (!err) {
@@ -487,6 +495,8 @@ int nmrp_do(struct nmrpd_args *args)
 
        err = 0;
 
+       printf("Reboot your device now.\n");
+
 out:
        signal(SIGINT, sigh_orig);
        gsock = NULL;