]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
add clone functionality to the edns struct and list, as well as doc strings
authorTCY16 <tom@nlnetlabs.nl>
Tue, 12 Apr 2022 10:57:49 +0000 (12:57 +0200)
committerTCY16 <tom@nlnetlabs.nl>
Tue, 12 Apr 2022 10:57:49 +0000 (12:57 +0200)
edns.c
ldns/edns.h

diff --git a/edns.c b/edns.c
index f6a8a797be594f135a77d66b98895ea350c53757..b1db5f03ee62739d22fe7f4c95d08bff6e0acc7d 100644 (file)
--- a/edns.c
+++ b/edns.c
@@ -131,6 +131,20 @@ ldns_edns_new_from_data(ldns_edns_option_code code, size_t size, const void *dat
        return edns;
 }
 
+ldns_edns_option *
+ldns_edns_clone(ldns_edns_option *edns)
+{
+       ldns_edns_option *new_option;
+
+       assert(edns != NULL);
+
+       new_option = ldns_edns_new_from_data(ldns_edns_get_code(edns),
+               ldns_edns_get_size(edns),
+               ldns_edns_get_data(edns));
+
+       return new_option;
+}
+
 void
 ldns_edns_deep_free(ldns_edns_option *edns)
 {
@@ -164,6 +178,32 @@ ldns_edns_option_list_new()
        return option_list;
 }
 
+ldns_edns_option_list *
+ldns_edns_option_list_clone(ldns_edns_option_list *old_list)
+{
+       size_t i;
+       ldns_edns_option_list *new_list;
+
+       if (!old_list) {
+               return NULL;
+       }
+
+       new_list = ldns_edns_option_list_new();
+       if (!new_list) {
+               return NULL;
+       }
+
+       new_list->_option_count = old_list->_option_count;
+
+       /* adding options also updates the total options size */
+       for (i = 0; i < old_list->_option_count; i++) {
+               ldns_edns_option_list_push(new_list,
+                       ldns_edns_clone(ldns_edns_option_list_get_option(old_list, i)));
+       }
+
+       return new_list;
+}
+
 void
 ldns_edns_option_list_free(ldns_edns_option_list *option_list)
 {
@@ -186,7 +226,6 @@ ldns_edns_option_list_deep_free(ldns_edns_option_list *option_list)
        }
 }
 
-
 size_t
 ldns_edns_option_list_get_count(const ldns_edns_option_list *option_list)
 {
index 9d821ee9f7ebf185f30780ecaf36c6a26899e3bf..a1bfa91f89fe2798d6de5173148e362016cd1386 100644 (file)
@@ -107,7 +107,7 @@ typedef struct ldns_struct_edns_option ldns_edns_option;
  */
 struct ldns_struct_edns_option_list
 {
-       size_t _option_count;
+       size_t _option_count; /* the number of EDNS options in the list */
        size_t _options_size; /* the total size of the options serialized */
        ldns_edns_option **_options;
 };
@@ -150,8 +150,8 @@ ldns_buffer *ldns_edns_get_wireformat_buffer(const ldns_edns_option *edns);
 /* Constructors and destructors*/
 
 /**
- * allocates a new EDNS structure and fills it.
- * This function DOES NOT copy the contents from the buffer
+ * allocates a new EDNS structure and fills it. This function *DOES NOT* copy
+ * the contents from the data parameter.
  * \param[in] code the EDNS code
  * \param[in] size size of the buffer
  * \param[in] data pointer to the buffer to be assigned
@@ -159,18 +159,48 @@ ldns_buffer *ldns_edns_get_wireformat_buffer(const ldns_edns_option *edns);
  */
 ldns_edns_option *ldns_edns_new(ldns_edns_option_code code, size_t size, void *data);
 
-// @TODO write this/determine if we need it
+/**
+ * allocates a new EDNS structure and fills it. This function *DOES* copy
+ * the contents from the data parameter.
+ * \param[in] code the EDNS code
+ * \param[in] size size of the buffer
+ * \param[in] data pointer to the buffer to be assigned
+ * \return the new EDNS structure or NULL on failure
+ */
 ldns_edns_option *ldns_edns_new_from_data(ldns_edns_option_code code, size_t size, const void *data);
 
+/**
+ * clone an EDNS option
+ * \param[in] the EDNS option
+ * \return the new EDNS structure
+ */
+ldns_edns_option *ldns_edns_clone(ldns_edns_option *edns);
+
+/**
+ * free the EDNS option. Use deep_free if the _data member is allocated.
+ * \param[in] the EDNS option to free
+ */
 void ldns_edns_deep_free(ldns_edns_option *edns);
 void ldns_edns_free(ldns_edns_option *edns);
 
-
 /**
  * allocates space for a new list of EDNS options
  * \return the new EDNS option list or NULL on failure
  */
 ldns_edns_option_list* ldns_edns_option_list_new(void);
+
+/**
+ * clone the EDNS options list and it's contents
+ * \param[in] options_list  the EDNS options_list to read from
+ * \return the new EDNS option list
+ */
+ldns_edns_option_list *ldns_edns_option_list_clone(ldns_edns_option_list *options_list);
+
+/**
+ * free the EDNS option list. Use deep_free to free the options options
+ * in the list as well.
+ * \param[in] the EDNS option to free
+ */
 void ldns_edns_option_list_free(ldns_edns_option_list *options_list);
 void ldns_edns_option_list_deep_free(ldns_edns_option_list *options_list);