summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/frrscript.c5
-rw-r--r--lib/frrscript.h16
-rw-r--r--lib/frrstr.c22
-rw-r--r--lib/frrstr.h20
4 files changed, 57 insertions, 6 deletions
diff --git a/lib/frrscript.c b/lib/frrscript.c
index c9fc938997..0e0d3c030c 100644
--- a/lib/frrscript.c
+++ b/lib/frrscript.c
@@ -288,13 +288,16 @@ int frrscript_load(struct frrscript *fs, const char *function_name,
goto fail;
}
- /* Push the Lua function we want */
+ /* To check the Lua function, we get it from the global table */
lua_getglobal(L, function_name);
if (lua_isfunction(L, lua_gettop(L)) == 0) {
zlog_err("frrscript: loaded script '%s.lua' but %s not found",
script_name, function_name);
goto fail;
}
+ /* Then pop the function (frrscript_call will push it when it needs it)
+ */
+ lua_pop(L, 1);
if (load_cb && (*load_cb)(fs) != 0) {
zlog_err(
diff --git a/lib/frrscript.h b/lib/frrscript.h
index 540676c099..c089df61fc 100644
--- a/lib/frrscript.h
+++ b/lib/frrscript.h
@@ -203,8 +203,18 @@ const struct prefix * : lua_decode_noop \
int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
/*
- * Wrapper for calling Lua function state. Maps values passed in to their
- * encoder and decoder types.
+ * Wrapper for calling Lua function state.
+ *
+ * The Lua function name (f) to run should have already been checked by
+ * frrscript_load. So this wrapper will:
+ * 1) Find the Lua function state, which contains the Lua state
+ * 2) Clear the Lua state (there may be leftovers items from previous call)
+ * 3) Push the Lua function (f)
+ * 4) Map frrscript_call arguments onto their encoder and decoders, push those
+ * 5) Call _frrscript_call_lua (Lua execution takes place)
+ * 6) Write back to frrscript_call arguments using their decoders
+ *
+ * This wrapper can be called multiple times (after one frrscript_load).
*
* fs
* The struct frrscript in which the Lua fuction was loaded into
@@ -226,6 +236,8 @@ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
1; \
}) \
: ({ \
+ lua_settop(lfs->L, 0); \
+ lua_getglobal(lfs->L, f); \
MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \
_frrscript_call_lua( \
lfs, PP_NARG(__VA_ARGS__)); \
diff --git a/lib/frrstr.c b/lib/frrstr.c
index 7ef5fffd12..1b98b224cc 100644
--- a/lib/frrstr.c
+++ b/lib/frrstr.c
@@ -18,9 +18,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include "zebra.h"
#include <string.h>
#include <ctype.h>
@@ -217,3 +215,21 @@ int all_digit(const char *str)
return 0;
return 1;
}
+
+
+char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num)
+{
+ if (bufsiz == 0)
+ return buff;
+
+ char tmp[3];
+
+ buff[0] = '\0';
+
+ for (size_t i = 0; i < num; i++) {
+ snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]);
+ strlcat(buff, tmp, bufsiz);
+ }
+
+ return buff;
+}
diff --git a/lib/frrstr.h b/lib/frrstr.h
index 441d7b8670..d52d6a4482 100644
--- a/lib/frrstr.h
+++ b/lib/frrstr.h
@@ -154,6 +154,26 @@ bool frrstr_endswith(const char *str, const char *suffix);
*/
int all_digit(const char *str);
+/*
+ * Copy the hexadecimal representation of the string to a buffer.
+ *
+ * buff
+ * Buffer to copy result into with size of at least (2 * num) + 1.
+ *
+ * bufsiz
+ * Size of destination buffer.
+ *
+ * str
+ * String to represent as hexadecimal.
+ *
+ * num
+ * Number of characters to copy.
+ *
+ * Returns:
+ * Pointer to buffer containing resulting hexadecimal representation.
+ */
+char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num);
+
#ifdef __cplusplus
}
#endif