]> git.proxmox.com Git - mirror_frr.git/blob - lib/frrlua.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / frrlua.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016-2019 Cumulus Networks, Inc.
4 * Donald Sharp, Quentin Young
5 */
6 #ifndef __FRRLUA_H__
7 #define __FRRLUA_H__
8
9 #include <zebra.h>
10
11 #ifdef HAVE_SCRIPTING
12
13 #include <lua.h>
14 #include <lualib.h>
15 #include <lauxlib.h>
16
17 #include "prefix.h"
18 #include "frrscript.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 DECLARE_MTYPE(SCRIPT_RES);
25
26 /*
27 * gcc-10 is complaining about the wrapper function
28 * not being compatible with lua_pushstring returning
29 * a char *. Let's wrapper it here to make our life
30 * easier
31 */
32 static inline void lua_pushstring_wrapper(lua_State *L, const char *str)
33 {
34 (void)lua_pushstring(L, str);
35 }
36
37 /*
38 * Converts a prefix to a Lua value and pushes it on the stack.
39 */
40 void lua_pushprefix(lua_State *L, const struct prefix *prefix);
41
42 void lua_decode_prefix(lua_State *L, int idx, struct prefix *prefix);
43
44 /*
45 * Converts the Lua value at idx to a prefix.
46 *
47 * Returns:
48 * struct prefix allocated with MTYPE_TMP
49 */
50 void *lua_toprefix(lua_State *L, int idx);
51
52 /*
53 * Converts an interface to a Lua value and pushes it on the stack.
54 */
55 void lua_pushinterface(lua_State *L, const struct interface *ifp);
56
57 void lua_decode_interface(lua_State *L, int idx, struct interface *ifp);
58
59 /*
60 * Converts the Lua value at idx to an interface.
61 *
62 * Returns:
63 * struct interface allocated with MTYPE_TMP. This interface is not hooked
64 * to anything, nor is it inserted in the global interface tree.
65 */
66 void *lua_tointerface(lua_State *L, int idx);
67
68 /*
69 * Converts an in_addr to a Lua value and pushes it on the stack.
70 */
71 void lua_pushinaddr(lua_State *L, const struct in_addr *addr);
72
73 void lua_decode_inaddr(lua_State *L, int idx, struct in_addr *addr);
74
75 /*
76 * Converts the Lua value at idx to an in_addr.
77 *
78 * Returns:
79 * struct in_addr allocated with MTYPE_TMP.
80 */
81 void *lua_toinaddr(lua_State *L, int idx);
82
83 /*
84 * Converts an in6_addr to a Lua value and pushes it on the stack.
85 */
86 void lua_pushin6addr(lua_State *L, const struct in6_addr *addr);
87
88 void lua_decode_in6addr(lua_State *L, int idx, struct in6_addr *addr);
89
90 void lua_pushipaddr(lua_State *L, const struct ipaddr *addr);
91
92 void lua_pushethaddr(lua_State *L, const struct ethaddr *addr);
93
94 /*
95 * Converts the Lua value at idx to an in6_addr.
96 *
97 * Returns:
98 * struct in6_addr allocated with MTYPE_TMP.
99 */
100 void *lua_toin6addr(lua_State *L, int idx);
101
102 /*
103 * Converts a time_t to a Lua value and pushes it on the stack.
104 */
105 void lua_pushtimet(lua_State *L, const time_t *time);
106
107 void lua_decode_timet(lua_State *L, int idx, time_t *time);
108
109 /*
110 * Converts the Lua value at idx to a time_t.
111 *
112 * Returns:
113 * time_t allocated with MTYPE_TMP.
114 */
115 void *lua_totimet(lua_State *L, int idx);
116
117 /*
118 * Converts a sockunion to a Lua value and pushes it on the stack.
119 */
120 void lua_pushsockunion(lua_State *L, const union sockunion *su);
121
122 void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su);
123
124 /*
125 * Converts the Lua value at idx to a sockunion.
126 *
127 * Returns:
128 * sockunion allocated with MTYPE_TMP.
129 */
130 void *lua_tosockunion(lua_State *L, int idx);
131
132 void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng);
133
134 void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop);
135
136 /*
137 * Converts an int to a Lua value and pushes it on the stack.
138 */
139 void lua_pushintegerp(lua_State *L, const long long *num);
140
141 void lua_decode_integerp(lua_State *L, int idx, long long *num);
142
143 /*
144 * Converts the Lua value at idx to an int.
145 *
146 * Returns:
147 * int allocated with MTYPE_TMP.
148 */
149 void *lua_tointegerp(lua_State *L, int idx);
150
151 void lua_decode_stringp(lua_State *L, int idx, char *str);
152
153 /*
154 * Pop string.
155 *
156 * Sets *string to a copy of the string at the top of the stack. The copy is
157 * allocated with MTYPE_TMP and the caller is responsible for freeing it.
158 */
159 void *lua_tostringp(lua_State *L, int idx);
160
161 /*
162 * No-op decoders
163 */
164 void lua_decode_noop(lua_State *L, int idx, const void *ptr);
165
166 void lua_decode_integer_noop(lua_State *L, int idx, int i);
167
168 /*
169 * Retrieve an integer from table on the top of the stack.
170 *
171 * key
172 * Key of string value in table
173 */
174 int frrlua_table_get_integer(lua_State *L, const char *key);
175
176 /*
177 * Exports a new table containing bindings to FRR zlog functions into the
178 * global namespace.
179 *
180 * From Lua, these functions may be accessed as:
181 *
182 * - log.debug()
183 * - log.info()
184 * - log.warn()
185 * - log.error()
186 *
187 * They take a single string argument.
188 */
189 void frrlua_export_logging(lua_State *L);
190
191 /*
192 * Dump Lua stack to a string.
193 *
194 * Return value must be freed with XFREE(MTYPE_TMP, ...);
195 */
196 char *frrlua_stackdump(lua_State *L);
197
198 #ifdef __cplusplus
199 }
200 #endif
201
202 #endif /* HAVE_SCRIPTING */
203
204 #endif /* __FRRLUA_H__ */