From 30ba1e75feaa3d8e0a30845e338a47eade56fe53 Mon Sep 17 00:00:00 2001
From: Anjan Chanda <anjan.chanda@iopsys.eu>
Date: Sun, 10 Jan 2021 21:03:29 +0100
Subject: [PATCH] reserve space for tlv with cmdu_reserve_tlv()

---
 src/cmdu.c | 17 +++++++++++++++++
 src/cmdu.h |  1 +
 2 files changed, 18 insertions(+)

diff --git a/src/cmdu.c b/src/cmdu.c
index ff02d052..825c743d 100644
--- a/src/cmdu.c
+++ b/src/cmdu.c
@@ -348,6 +348,23 @@ int cmdu_put_eom(struct cmdu_buff *c)
 	return cmdu_put(c, eom, sizeof(eom));
 }
 
+struct tlv *cmdu_reserve_tlv(struct cmdu_buff *c, uint16_t tlv_datalen)
+{
+	uint16_t len = tlv_datalen + TLV_HLEN;
+
+	if (!c)
+		return NULL;
+
+	if (c->end - c->tail < len) {
+		fprintf(stderr, "%s: Failed to reserve %hu! Allocate new cmdu fragment\n",
+			__func__, tlv_datalen);
+
+		return NULL;
+	}
+
+	return (struct tlv *)c->tail;
+}
+
 int cmdu_parse_tlvs(struct cmdu_buff *c, struct tlv *tv[][16],
 		    struct tlv_policy *policy, int policy_len)
 {
diff --git a/src/cmdu.h b/src/cmdu.h
index 90fcd0d9..9a78e8d5 100644
--- a/src/cmdu.h
+++ b/src/cmdu.h
@@ -159,6 +159,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 tlv *cmdu_reserve_tlv(struct cmdu_buff *c, uint16_t tlv_datalen);
 int cmdu_put_tlv(struct cmdu_buff *c, struct tlv *t);
 int cmdu_put(struct cmdu_buff *c, uint8_t *bytes, int len);
 int cmdu_put_eom(struct cmdu_buff *c);
-- 
GitLab