]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add cf_data_add function for dealing with non-talloced data
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 13 Jun 2017 19:54:10 +0000 (15:54 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 13 Jun 2017 19:54:16 +0000 (15:54 -0400)
src/include/cf_util.h
src/main/cf_priv.h
src/main/cf_util.c

index 9f2ecf229e639b6ab451e3e949186a4edaf2e296..cf346f0a2e05a93c3177b96b55821cc0b7901354 100644 (file)
@@ -166,6 +166,9 @@ void                *cf_data_value(CONF_DATA const *cd);
 #define                cf_data_add(_cf, _data, _name, _free) _cf_data_add(CF_TO_ITEM(_cf), _data, _name, _free)
 CONF_DATA const *_cf_data_add(CONF_ITEM *ci, void const *data, char const *name, bool free);
 
+#define                cf_data_add_static(_cf, _data, _type, _name) _cf_data_add(CF_TO_ITEM(_cf), _data, #_type, _name)
+CONF_DATA const *_cf_data_add_static(CONF_ITEM *ci, void const *data, char const *type, char const *name);
+
 #define                cf_data_remove(_cf, _cd) _cf_data_remove(CF_TO_ITEM(_cf), _cd);
 void           *_cf_data_remove(CONF_ITEM *ci, CONF_DATA const *_cd);
 
index 610713088023641afd6c8956727403fe200c8484..fe43b050e279ba76708f99279889b621d4c265a6 100644 (file)
@@ -115,6 +115,7 @@ struct cf_data {
        char const              *name;          //!< Additional qualification of type.
 
        void const              *data;          //!< User data.
+       bool                    is_talloced;    //!< If true we can do extra checks.
        bool                    free;           //!< If true, free data with talloc if parent node is freed.
 };
 
index b79770c7663024d389911ffd916ae76a9ea4e112..81144b6c9491ebd6c4e33d33985367d04afb90e9 100644 (file)
@@ -1253,14 +1253,15 @@ static int _cd_free(CONF_DATA *cd)
 /** Allocate a new user data container
  *
  * @param[in] parent   #CONF_PAIR, or #CONF_SECTION to hang CONF_DATA off of.
- * @param[in] name     String identifier of the user data.
  * @param[in] data     being added.
+ * @param[in] type     of data being added.
+ * @param[in] name     String identifier of the user data.
  * @param[in] do_free  function, called when the parent #CONF_SECTION is being freed.
  * @return
  *     - CONF_DATA on success.
  *     - NULL on error.
  */
-static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const *name, bool do_free)
+static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const *type, char const *name, bool do_free)
 {
        CONF_DATA *cd;
 
@@ -1277,7 +1278,7 @@ static CONF_DATA *cf_data_alloc(CONF_ITEM *parent, void const *data, char const
         *      explosions.
         */
        if (data) {
-               cd->type = talloc_typed_strdup(cd, talloc_get_name(data));
+               cd->type = talloc_typed_strdup(cd, type);
                cd->data = data;
        }
        if (name) cd->name = talloc_typed_strdup(cd, name);
@@ -1340,7 +1341,7 @@ void *cf_data_value(CONF_DATA const *cd)
        return to_return;
 }
 
-/** Add user data to a config section
+/** Add talloced user data to a config section
  *
  * @param[in] ci       to add data to.
  * @param[in] data     to add.
@@ -1367,13 +1368,47 @@ CONF_DATA const *_cf_data_add(CONF_ITEM *ci, void const *data, char const *name,
                return NULL;
        }
 
-       cd = cf_data_alloc(ci, data, name, do_free);
+       cd = cf_data_alloc(ci, data, type, name, do_free);
+       if (!cd) {
+               cf_log_err(ci, "Failed allocating data");
+               return NULL;
+       }
+       cd->is_talloced = true;
+
+       cf_item_add(ci, cd);
+
+       return cd;
+}
+
+/** Add non-talloced user data to a config section
+ *
+ * @param[in] ci       to add data to.
+ * @param[in] data     to add.
+ * @param[in] type     identifier of the user data.
+ * @param[in] name     String identifier of the user data.
+ *     - #CONF_DATA  - opaque handle to the stored data - on success.
+ *     - NULL error.
+ */
+CONF_DATA const *_cf_data_add_static(CONF_ITEM *ci, void const *data, char const *type, char const *name)
+{
+       CONF_DATA *cd;
+
+       /*
+        *      Already exists.  Can't add it.
+        */
+       if (_cf_data_find(ci, type, name)) {
+               cf_log_err(ci, "Data of type %s with name %s already exists", type, name);
+               return NULL;
+       }
+
+       cd = cf_data_alloc(ci, data, type, name, false);
        if (!cd) {
                cf_log_err(ci, "Failed allocating data");
                return NULL;
        }
+       cd->is_talloced = false;
 
-       cf_item_add(ci, cf_data_to_item(cd));
+       cf_item_add(ci, cd);
 
        return cd;
 }