From 1a662da82fa19f8ab3b3c5a6feccfd6abc864f73 Mon Sep 17 00:00:00 2001
From: Anjan Chanda <anjan.chanda@iopsys.eu>
Date: Sat, 10 Apr 2021 16:24:17 +0200
Subject: [PATCH] clone unfragmented cmdu with cmdu_clone()

---
 src/cmdu.c | 63 ++++++++++++++++++++++++++++--------------------------
 src/cmdu.h |  1 +
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/src/cmdu.c b/src/cmdu.c
index 708549d7..5cb9a023 100644
--- a/src/cmdu.c
+++ b/src/cmdu.c
@@ -195,8 +195,8 @@ struct cmdu_buff *cmdu_alloc_simple(uint16_t type, uint16_t *mid)
 	}
 
 	f->cdata = (struct cmdu_linear *)(f->head + 18);
-	if (type <= CMDU_TYPE_MAX)
-		cmdu_set_type(f, type);
+	//if (type <= CMDU_TYPE_MAX)
+	cmdu_set_type(f, type);
 
 	if (*mid == 0)
 		*mid = cmdu_get_next_mid();
@@ -241,34 +241,6 @@ int cmdu_reserve(struct cmdu_buff *c, size_t s)
 
 int cmdu_copy_tlvs_linear(struct cmdu_buff *c, uint8_t *tlvs, uint32_t tlvslen)
 {
-#if 0
-	struct cmdu *n;
-
-
-	if (!is_cmdu_type_valid(type))
-		return NULL;
-
-	//if (!is_tlv_stream_valid(type, tlvs, tlvslen))
-	//	return NULL;
-
-
-	n = calloc(1, sizeof(*n) + tlvslen * sizeof(uint8_t));
-	if (!n)
-		return NULL;
-
-	n->hdr.version = 0;
-	n->hdr.type = type;
-
-	if (*mid == 0)
-		*mid = cmdu_get_next_mid();
-
-	n->hdr.mid = *mid;
-
-	if (cmdu_should_relay(type))
-		CMDU_SET_RELAY_MCAST(n);
-
-#endif
-
 	if (c->end - c->tail < tlvslen)
 		return -1;
 
@@ -307,6 +279,37 @@ struct cmdu_buff *cmdu_alloc_custom(uint16_t type, uint16_t *mid, char *ifname,
 	return f;
 }
 
+struct cmdu_buff *cmdu_clone(struct cmdu_buff *frm)
+{
+	struct cmdu_buff *f;
+	int len;
+
+
+	if (!frm || !frm->cdata) {
+		fprintf(stderr, "%s: cmdu for cloning is invalid!\n", __func__);
+		return NULL;
+	}
+
+	len = cmdu_size(frm);
+	f = cmdu_alloc(len);
+	if (!f) {
+		fprintf(stderr, "%s: -ENOMEM\n", __func__);
+		return NULL;
+	}
+
+	f->cdata = (struct cmdu_linear *)(f->head + 18);
+	f->data = (uint8_t *)(f->cdata + 1);
+	f->datalen = frm->datalen;
+	f->tail = f->data + f->datalen;
+	memcpy(f->cdata, frm->cdata, len);
+
+	memcpy(f->dev_macaddr, frm->dev_macaddr, 6);
+	strncpy(f->dev_ifname, frm->dev_ifname, 15);
+	memcpy(f->origin, frm->origin, 6);
+
+	return f;
+}
+
 int cmdu_copy_tlvs(struct cmdu_buff *c, struct tlv *tv[], int tv_arrsize)
 {
 	uint16_t tlvslen = 0;
diff --git a/src/cmdu.h b/src/cmdu.h
index 9a410f0f..5a6a0e8d 100644
--- a/src/cmdu.h
+++ b/src/cmdu.h
@@ -174,6 +174,7 @@ int cmdu_parse_tlvs(struct cmdu_buff *c, struct tlv *tv[][16],
 
 int cmdu_copy_tlvs_linear(struct cmdu_buff *c, uint8_t *tlvs, uint32_t tlvslen);
 int cmdu_copy_tlvs(struct cmdu_buff *c, struct tlv *tv[], int tv_arrsize);
+struct cmdu_buff *cmdu_clone(struct cmdu_buff *frm);
 
 struct tlv *cmdu_reserve_tlv(struct cmdu_buff *c, uint16_t tlv_datalen);
 int cmdu_put_tlv(struct cmdu_buff *c, struct tlv *t);
-- 
GitLab