Add msg_add_opt
[oweals/nmrpflash.git] / nmrp.c
diff --git a/nmrp.c b/nmrp.c
index 7554929e27897bbcf8df7edf86988fbb60422c4e..20662fccc1bf15bd0854d1bd202f02f156332426 100644 (file)
--- a/nmrp.c
+++ b/nmrp.c
 #include "nmrpd.h"
 
 #define NMRP_HDR_LEN 6
-#define NMRP_OPT_LEN 4
+#define NMRP_OPT_HDR_LEN 4
 #define NMRP_MIN_PKT_LEN (sizeof(struct eth_hdr) +  NMRP_HDR_LEN)
 
 #define NMRP_MAX_OPT_SIZE 12
-#define NMRP_MAX_OPT_NUM 2
+#define NMRP_MAX_OPT_NUM 3
 
 #define ETH_P_NMRP 0x0912
 #define IP_LEN 4
@@ -80,7 +80,7 @@ struct nmrp_msg {
        uint8_t code;
        uint8_t id;
        uint16_t len;
-       struct nmrp_opt opts[2];
+       struct nmrp_opt opts[NMRP_MAX_OPT_NUM];
        uint32_t num_opts;
 } PACKED;
 
@@ -95,6 +95,27 @@ struct nmrp_pkt {
        struct nmrp_msg msg;
 } PACKED;
 
+static const char *msg_code_str(uint16_t code)
+{
+#define CASE_CODE(x) case NMRP_C_ ## x: return #x
+       static char buf[16];
+
+       switch (code) {
+               CASE_CODE(ADVERTISE);
+               CASE_CODE(CONF_REQ);
+               CASE_CODE(CONF_ACK);
+               CASE_CODE(CLOSE_REQ);
+               CASE_CODE(CLOSE_ACK);
+               CASE_CODE(KEEP_ALIVE_REQ);
+               CASE_CODE(KEEP_ALIVE_ACK);
+               CASE_CODE(TFTP_UL_REQ);
+               default:
+                       snprintf(buf, sizeof(buf), "%04x", code);
+                       return buf;
+       }
+#undef CASE_CODE
+}
+
 static void msg_update_len(struct nmrp_msg *msg)
 {
        uint32_t i = 0;
@@ -121,7 +142,7 @@ static void msg_dump(struct nmrp_msg *msg, int dump_opts)
                while (remain_len > 0) {
                        len = opt->len;
                        fprintf(stderr, "  opt type=%u, len=%u", opt->type, len);
-                       for (i = 0; i != len - NMRP_OPT_LEN; ++i) {
+                       for (i = 0; i != len - NMRP_OPT_HDR_LEN; ++i) {
                                if (!(i % 16)) {
                                        fprintf(stderr, "\n  ");
                                }
@@ -165,7 +186,7 @@ static int msg_ntoh(struct nmrp_msg *msg)
        // size is 12
        if (remaining < NMRP_MAX_OPT_NUM * NMRP_MAX_OPT_SIZE) {
                while (remaining > 0) {
-                       if (remaining < NMRP_OPT_LEN) {
+                       if (remaining < NMRP_OPT_HDR_LEN) {
                                break;
                        }
 
@@ -190,15 +211,22 @@ static int msg_ntoh(struct nmrp_msg *msg)
        return 1;
 }
 
-static void *msg_opt_data(struct nmrp_msg *msg, int type, uint16_t *len)
+static void *msg_opt_data(struct nmrp_msg *msg, uint16_t type, uint16_t *len)
 {
+       static char buf[128];
        struct nmrp_opt *opt = msg->opts;
        int remaining = msg->len - NMRP_HDR_LEN;
 
+       memset(buf, 0, sizeof(buf));
+
        while (remaining > 0) {
                if (opt->type == type) {
-                       *len = opt->len - NMRP_OPT_LEN;
-                       return (char*)&opt->val;
+                       if (opt->len == NMRP_OPT_HDR_LEN) {
+                               return NULL;
+                       }
+                       *len = opt->len - NMRP_OPT_HDR_LEN;
+                       memcpy(buf, &opt->val, MIN(*len, sizeof(buf)-1));
+                       return buf;
                }
 
                remaining -= opt->len;
@@ -208,6 +236,33 @@ static void *msg_opt_data(struct nmrp_msg *msg, int type, uint16_t *len)
        return NULL;
 }
 
+static void msg_opt_add(struct nmrp_msg *msg, uint16_t type, void *data,
+               uint16_t len)
+{
+       uint32_t i = 0;
+       struct nmrp_opt *opt = msg->opts;
+
+       if (len + NMRP_OPT_HDR_LEN > NMRP_MAX_OPT_SIZE
+                       || msg->num_opts == NMRP_MAX_OPT_NUM) {
+               fprintf(stderr, "Invalid option - this is a bug.\n");
+       }
+
+       for (; i != msg->num_opts; ++i) {
+               opt = (struct nmrp_opt*)(((char*)opt) + msg->len);
+       }
+
+       opt->len = NMRP_OPT_HDR_LEN + len;
+       opt->type = type;
+
+       if (len) {
+               memcpy(&opt->val, data, len);
+       }
+
+       ++msg->num_opts;
+
+       return true;
+}
+
 static int pkt_send(struct ethsock *sock, struct nmrp_pkt *pkt)
 {
        size_t len = ntohs(pkt->msg.len) + sizeof(pkt->eh);
@@ -321,7 +376,7 @@ int nmrp_do(struct nmrpd_args *args)
        char *filename;
        struct in_addr ipaddr, ipmask;
        time_t beg;
-       int i, status, ulreqs, expect;
+       int i, status, ulreqs, expect, upload_ok;
        struct ethsock *sock;
        void (*sigh_orig)(int);
 
@@ -393,9 +448,10 @@ int nmrp_do(struct nmrpd_args *args)
        tx.msg.reserved = 0;
        tx.msg.code = NMRP_C_ADVERTISE;
        tx.msg.id = 0;
-       tx.msg.num_opts = 1;
+       tx.msg.num_opts = 0;
+
        tx.msg.opts[0].type = NMRP_O_MAGIC_NO;
-       tx.msg.opts[0].len = NMRP_OPT_LEN + 4;
+       tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 4;
        tx.msg.opts[0].val.magic[0] = 'N';
        tx.msg.opts[0].val.magic[1] = 'T';
        tx.msg.opts[0].val.magic[2] = 'G';
@@ -405,6 +461,7 @@ int nmrp_do(struct nmrpd_args *args)
        msg_hton(&tx.msg);
 
        i = 0;
+       upload_ok = 0;
        beg = time(NULL);
 
        while (1) {
@@ -439,8 +496,8 @@ int nmrp_do(struct nmrpd_args *args)
 
        do {
                if (expect != NMRP_C_NONE && rx.msg.code != expect) {
-                       fprintf(stderr, "Received code 0x%02x while waiting for 0x%02x!\n",
-                                       rx.msg.code, expect);
+                       fprintf(stderr, "Received %s while waiting for %s!\n",
+                                       msg_code_str(rx.msg.code), msg_code_str(expect));
                }
 
                tx.msg.code = NMRP_C_NONE;
@@ -462,19 +519,19 @@ int nmrp_do(struct nmrpd_args *args)
                                tx.msg.num_opts = 2;
 
                                tx.msg.opts[0].type = NMRP_O_DEV_IP;
-                               tx.msg.opts[0].len = NMRP_OPT_LEN + 2 * 4;
+                               tx.msg.opts[0].len = NMRP_OPT_HDR_LEN + 2 * 4;
 
                                memcpy(tx.msg.opts[0].val.ip.addr, &ipaddr, 4);
                                memcpy(tx.msg.opts[0].val.ip.mask, &ipmask, 4);
 
                                tx.msg.opts[1].type = NMRP_O_FW_UP;
-                               tx.msg.opts[1].len = NMRP_OPT_LEN;
+                               tx.msg.opts[1].len = NMRP_OPT_HDR_LEN;
 
 #ifdef NMRPFLASH_SET_REGION
                                tx.msg.num_opts = 3;
 
                                tx.msg.opts[2].type = NMRP_O_DEV_REGION;
-                               tx.msg.opts[2].len = NMRP_OPT_LEN + 2;
+                               tx.msg.opts[2].len = NMRP_OPT_HDR_LEN + 2;
                                tx.msg.opts[2].val.region = args->region;
 #endif
 
@@ -490,10 +547,19 @@ int nmrp_do(struct nmrpd_args *args)
 
                                break;
                        case NMRP_C_TFTP_UL_REQ:
-                               if (++ulreqs > 5) {
-                                       fprintf(stderr, "Device re-requested file upload %d "
-                                                       "times; aborting.\n", ulreqs);
-                                       tx.msg.code = NMRP_C_CLOSE_REQ;
+                               if (!upload_ok) {
+                                       if (++ulreqs > 5) {
+                                               printf("Bailing out after %d upload requests.\n",
+                                                               ulreqs);
+                                               tx.msg.code = NMRP_C_CLOSE_REQ;
+                                               break;
+                                       }
+                               } else {
+                                       if (verbosity) {
+                                               printf("Ignoring extra upload request.\n");
+                                       }
+                                       ethsock_set_timeout(sock, args->ul_timeout);
+                                       tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
                                        break;
                                }
 
@@ -506,11 +572,7 @@ int nmrp_do(struct nmrpd_args *args)
                                        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 {
-                                               args->file_remote = "firmware";
-                                       }
+                                       args->file_remote = args->file_local;
                                        printf("Received upload request with empty filename.");
                                }
 
@@ -546,7 +608,7 @@ int nmrp_do(struct nmrpd_args *args)
                                        if (!strcmp(args->file_local, "-")) {
                                                printf("Uploading from stdin ... ");
                                        } else {
-                                               printf("Uploading %s ... ", args->file_local);
+                                               printf("Uploading %s ... ", leafname(args->file_local));
                                        }
                                        fflush(stdout);
                                        status = tftp_put(args);
@@ -554,8 +616,10 @@ int nmrp_do(struct nmrpd_args *args)
 
                                if (!status) {
                                        printf("OK\nWaiting for remote to respond.\n");
+                                       upload_ok = 1;
                                        ethsock_set_timeout(sock, args->ul_timeout);
-                                       expect = NMRP_C_CLOSE_REQ;
+                                       tx.msg.code = NMRP_C_KEEP_ALIVE_REQ;
+                                       expect = NMRP_C_NONE;
                                } else if (status == -2) {
                                        expect = NMRP_C_TFTP_UL_REQ;
                                } else {
@@ -565,6 +629,8 @@ int nmrp_do(struct nmrpd_args *args)
                                break;
                        case NMRP_C_KEEP_ALIVE_REQ:
                                tx.msg.code = NMRP_C_KEEP_ALIVE_ACK;
+                               ethsock_set_timeout(sock, 15000);
+                               printf("Received keep-alive request.\n");
                                break;
                        case NMRP_C_CLOSE_REQ:
                                tx.msg.code = NMRP_C_CLOSE_ACK;
@@ -596,7 +662,8 @@ int nmrp_do(struct nmrpd_args *args)
                status = pkt_recv(sock, &rx);
                if (status) {
                        if (status == 2) {
-                               fprintf(stderr, "Timeout while waiting for 0x%02x.\n", expect);
+                               fprintf(stderr, "Timeout while waiting for %s.\n",
+                                               msg_code_str(expect));
                        }
                        goto out;
                }
@@ -607,7 +674,11 @@ int nmrp_do(struct nmrpd_args *args)
 
        status = 0;
 
-       printf("Reboot your device now.\n");
+       if (ulreqs) {
+               printf("Reboot your device now.\n");
+       } else {
+               printf("No upload request received.\n");
+       }
 
 out:
        signal(SIGINT, sigh_orig);