]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: Add frrscript names hash
authorDonald Lee <dlqs@gmx.com>
Mon, 18 Oct 2021 00:23:19 +0000 (08:23 +0800)
committerDonald Lee <dlqs@gmx.com>
Tue, 19 Oct 2021 16:56:00 +0000 (00:56 +0800)
Signed-off-by: Donald Lee <dlqs@gmx.com>
lib/frrscript.c
lib/frrscript.h

index 0e0d3c030c8237842936773aa0f8768d51b98b45..cc56d031cceb6a70332a999b32a1e19c841f0671 100644 (file)
 
 DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting");
 
+/*
+ * Script name hash utilities
+ */
+
+struct frrscript_names_head frrscript_names_hash;
+
+/*
+ * Wrapper for frrscript_names_add
+ * Use this to register hook calls when a daemon starts up
+ */
+int frrscript_names_add_function_name(const char *function_name)
+{
+       struct frrscript_names_entry *insert =
+               XCALLOC(MTYPE_SCRIPT, sizeof(*insert));
+       strlcpy(insert->function_name, function_name,
+               sizeof(insert->function_name));
+
+       if (frrscript_names_add(&frrscript_names_hash, insert)) {
+               zlog_warn(
+                       "Failed to add hook call function name to script_names");
+               return 1;
+       }
+       return 0;
+}
+
+void frrscript_names_destroy(void)
+{
+       struct frrscript_names_entry *ne;
+
+       while ((ne = frrscript_names_pop(&frrscript_names_hash)))
+               XFREE(MTYPE_SCRIPT, ne);
+}
+
+/*
+ * Given a function_name, set its script_name. function_names and script_names
+ * are one-to-one. Each set will wipe the previous script_name.
+ * Return 0 if set was successful, else 1.
+ *
+ * script_name is the base name of the file, without .lua.
+ */
+int frrscript_names_set_script_name(const char *function_name,
+                                   const char *script_name)
+{
+       struct frrscript_names_entry lookup;
+
+       strlcpy(lookup.function_name, function_name,
+               sizeof(lookup.function_name));
+       struct frrscript_names_entry *snhe =
+               frrscript_names_find(&frrscript_names_hash, &lookup);
+       if (!snhe)
+               return 1;
+       strlcpy(snhe->script_name, script_name, sizeof(snhe->script_name));
+       return 0;
+}
+
+/*
+ * Given a function_name, get its script_name.
+ * Return NULL if function_name not found.
+ *
+ * script_name is the base name of the file, without .lua.
+ */
+char *frrscript_names_get_script_name(const char *function_name)
+{
+       struct frrscript_names_entry lookup;
+
+       strlcpy(lookup.function_name, function_name,
+               sizeof(lookup.function_name));
+       struct frrscript_names_entry *snhe =
+               frrscript_names_find(&frrscript_names_hash, &lookup);
+       if (!snhe)
+               return NULL;
+       return snhe->script_name;
+}
+
+uint32_t frrscript_names_hash_key(const struct frrscript_names_entry *snhe)
+{
+       return string_hash_make(snhe->function_name);
+}
+
+int frrscript_names_hash_cmp(const struct frrscript_names_entry *snhe1,
+                            const struct frrscript_names_entry *snhe2)
+{
+       return strncmp(snhe1->function_name, snhe2->function_name,
+                      sizeof(snhe1->function_name));
+}
+
 /* Codecs */
 
 struct frrscript_codec frrscript_codecs_lib[] = {
index cb96b6832360a53df9adec6c53b27a461f2ff494..4db3e6f1b2260907a001196ee6877d4546df9d9c 100644 (file)
@@ -40,6 +40,36 @@ extern void lua_pushzebra_dplane_ctx(lua_State *L,
 extern void lua_decode_zebra_dplane_ctx(lua_State *L, int idx,
                                        struct zebra_dplane_ctx *ctx);
 
+/*
+ * Script name hash
+ */
+PREDECL_HASH(frrscript_names);
+
+struct frrscript_names_entry {
+       /* Name of a Lua hook call */
+       char function_name[MAXPATHLEN];
+
+       /* Lua script in which to look for it */
+       char script_name[MAXPATHLEN];
+
+       struct frrscript_names_item item;
+};
+
+extern struct frrscript_names_head frrscript_names_hash;
+
+int frrscript_names_hash_cmp(const struct frrscript_names_entry *snhe1,
+                            const struct frrscript_names_entry *snhe2);
+uint32_t frrscript_names_hash_key(const struct frrscript_names_entry *snhe);
+
+DECLARE_HASH(frrscript_names, struct frrscript_names_entry, item,
+            frrscript_names_hash_cmp, frrscript_names_hash_key);
+
+int frrscript_names_add_function_name(const char *function_name);
+void frrscript_names_destroy(void);
+int frrscript_names_set_script_name(const char *function_name,
+                                   const char *script_name);
+char *frrscript_names_get_script_name(const char *function_name);
+
 typedef void (*encoder_func)(lua_State *, const void *);
 typedef void *(*decoder_func)(lua_State *, int);