+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) {
+ 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;
+ opt = (struct nmrp_opt*)((char*)opt) + opt->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;
+}
+