]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: native msg add array of strings support
authorChristian Hopps <chopps@labn.net>
Tue, 4 Jun 2024 14:28:48 +0000 (10:28 -0400)
committerChristian Hopps <chopps@labn.net>
Fri, 7 Jun 2024 02:39:47 +0000 (22:39 -0400)
Signed-off-by: Christian Hopps <chopps@labn.net>
lib/mgmt_msg_native.c
lib/mgmt_msg_native.h

index 39ce9abae64966fb94aafa80fff004864b969558..d0a0b72189bab8b2927c678af1ffb78100c43614 100644 (file)
@@ -6,6 +6,7 @@
  *
  */
 #include <zebra.h>
+#include "darr.h"
 #include "mgmt_msg_native.h"
 
 DEFINE_MGROUP(MSG_NATIVE, "Native message allocations");
@@ -50,3 +51,20 @@ int vmgmt_msg_native_send_error(struct msg_conn *conn, uint64_t sess_or_txn_id,
        mgmt_msg_native_free_msg(msg);
        return ret;
 }
+
+const char **_mgmt_msg_native_strings_decode(const void *_sdata, int sdlen)
+{
+       const char *sdata = _sdata;
+       const char **strings = NULL;
+       int len;
+
+       if (sdata[sdlen - 1] != 0)
+               return NULL;
+
+       for (; sdlen; sdata += len, sdlen -= len) {
+               *darr_append(strings) = darr_strdup(sdata);
+               len = 1 + darr_strlen(strings[darr_lasti(strings)]);
+       }
+
+       return strings;
+}
index 21f702cc61341aa94725cefec6b0a97acf417f3f..cb1101d24f1c512f16234314d088490c4c919c87 100644 (file)
@@ -524,6 +524,25 @@ extern int vmgmt_msg_native_send_error(struct msg_conn *conn,
                p;                                                             \
        })
 
+/**
+ * mgmt_msg_native_add_str() - Append [another] string to the msg.
+ * @msg: (IN/OUT) Pointer to the native message, variable may be updated.
+ * @s: string to append.
+ *
+ * Append string @s to the native message @msg. @msg is assumed to have a
+ * sequence of NUL-terminated strings at the end of it. This function appends
+ * the string @s and it's NUL terminating octet to the message.
+ *
+ * NOTE: Be aware @msg pointer may change as a result of reallocating the
+ * message to fit the new data. Any other pointers into the old message should
+ * be discarded.
+ */
+#define mgmt_msg_native_add_str(msg, s)                                        \
+       do {                                                                   \
+               int __len = strlen(s) + 1;                                     \
+               mgmt_msg_native_append(msg, s, __len);                         \
+       } while (0)
+
 /**
  * mgmt_msg_native_send_msg(msg, short_circuit_ok) - Send a native msg.
  * @conn: the mgmt_msg connection.
@@ -689,6 +708,27 @@ extern int vmgmt_msg_native_send_error(struct msg_conn *conn,
 #define mgmt_msg_native_data_len_decode(msg, msglen)                           \
        ((msglen) - sizeof(*msg) - msg->vsplit)
 
+/**
+ * mgmt_msg_native_strings_decode() - Get dynamic array of str ptrs from the msg.
+ * @msg: Pointer to the native message.
+ * @msglen: Length of the message.
+ * @sdata: pointer to the variable length string data at end of @msg.
+ *
+ * Given a pointer to a sequence of NUL-terminated strings allocate
+ * and return a dynamic array of dynamic array strings. This function
+ * can be used to decode a message that was built using
+ * mgmt_msg_native_add_str().
+ *
+ * Return: a dynamic array (darr) of string pointers, or NULL if the message
+ * is corrupt.
+ */
+#define mgmt_msg_native_strings_decode(msg, msg_len, sdata)                    \
+       _mgmt_msg_native_strings_decode(sdata,                                 \
+                                       (msg_len) - ((sdata) - (char *)(msg)))
+
+extern const char **_mgmt_msg_native_strings_decode(const void *sdata,
+                                                   int sdlen);
+
 #ifdef __cplusplus
 }
 #endif