lib: Add frrscript names hash

Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
Donald Lee 2021-10-18 08:23:19 +08:00
parent 263951c1be
commit a3ef7ae835
2 changed files with 116 additions and 0 deletions

View File

@ -32,6 +32,92 @@
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[] = {

View 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);