]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add fr_dict_protocol_alloc() for use with local dictionaries
authorAlan T. DeKok <aland@freeradius.org>
Thu, 24 Nov 2022 14:39:02 +0000 (09:39 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 24 Nov 2022 20:06:16 +0000 (15:06 -0500)
src/lib/util/dict.h
src/lib/util/dict_util.c

index eda4c353b213035830d7ffde9e79a90d80fa357b..d30255498926a7c05505392f58d3d80d46d00586 100644 (file)
@@ -587,6 +587,8 @@ int                 fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name,
 int                    fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir,
                                                    char const *dependent);
 
+fr_dict_t              *fr_dict_protocol_alloc(TALLOC_CTX *ctx);
+
 int                    fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename);
 /** @} */
 
index 4bfea70df3f499e1ee096ccb35f6df288eecd539..f72f9397d0719875069226a6bd2e1d362817538b 100644 (file)
@@ -3374,6 +3374,57 @@ fr_dict_t *dict_alloc(TALLOC_CTX *ctx)
        return dict;
 }
 
+/** Allocate a new local dictionary
+ *
+ * @param[in] ctx to allocate dictionary in.
+ * @return
+ *     - NULL on memory allocation error.
+ *
+ *  This dictionary cannot define vendors, or inter-dictionary
+ *  dependencies.  However, we initialize the relevant fields just in
+ *  case.  We should arguably just skip initializing those fields, and
+ *  just allow the server to crash if programmers do something stupid with it.
+ */
+fr_dict_t *fr_dict_protocol_alloc(TALLOC_CTX *ctx)
+{
+       fr_dict_t *dict;
+       fr_dict_attr_t *da;
+
+       fr_dict_attr_flags_t flags = {
+               .is_root = 1,
+               .type_size = 2,
+               .length = 2
+       };
+
+
+       dict = dict_alloc(ctx);
+       if (!dict) return NULL;
+
+       /*
+        *      Allow for 64k local attributes.
+        */
+       dict->default_type_size = 2;
+       dict->default_type_length = 2;
+       dict->self_allocated = (1 << 24);
+
+       /*
+        *      Allocate the root attribute.  This dictionary is
+        *      always protocol "local", and number "0".
+        */
+       da = dict_attr_alloc(dict->pool, NULL, "local", 0, FR_TYPE_TLV,
+                            &(dict_attr_args_t){ .flags = &flags });
+       if (unlikely(!da)) {
+               talloc_free(dict);
+               return NULL;
+       }
+
+       dict->root = da;
+       dict->root->dict = dict;
+       DA_VERIFY(dict->root);
+
+       return dict;
+}
+
 /** Decrement the reference count on a previously loaded dictionary
  *
  * @param[in] dict     to free.