]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrlua.c
2 * This file defines the lua interface into
5 * Copyright (C) 2016-2019 Cumulus Networks, Inc.
6 * Donald Sharp, Quentin Young
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 * FRR convenience functions.
37 * This section has convenience functions used to make interacting with the Lua
41 int frrlua_table_get_integer(lua_State
*L
, const char *key
)
45 lua_pushstring(L
, key
);
48 result
= lua_tointeger(L
, -1);
55 * This section has functions that convert internal FRR datatypes into Lua
56 * datatypes: one encoder function and two decoder functions for each type.
60 void lua_pushprefix(lua_State
*L
, const struct prefix
*prefix
)
62 char buffer
[PREFIX_STRLEN
];
65 lua_pushstring(L
, prefix2str(prefix
, buffer
, PREFIX_STRLEN
));
66 lua_setfield(L
, -2, "network");
67 lua_pushinteger(L
, prefix
->prefixlen
);
68 lua_setfield(L
, -2, "length");
69 lua_pushinteger(L
, prefix
->family
);
70 lua_setfield(L
, -2, "family");
73 void lua_decode_prefix(lua_State
*L
, int idx
, struct prefix
*prefix
)
75 lua_getfield(L
, idx
, "network");
76 (void)str2prefix(lua_tostring(L
, -1), prefix
);
82 void *lua_toprefix(lua_State
*L
, int idx
)
84 struct prefix
*p
= XCALLOC(MTYPE_TMP
, sizeof(struct prefix
));
85 lua_decode_prefix(L
, idx
, p
);
89 void lua_pushinterface(lua_State
*L
, const struct interface
*ifp
)
92 lua_pushstring(L
, ifp
->name
);
93 lua_setfield(L
, -2, "name");
94 lua_pushinteger(L
, ifp
->ifindex
);
95 lua_setfield(L
, -2, "ifindex");
96 lua_pushinteger(L
, ifp
->status
);
97 lua_setfield(L
, -2, "status");
98 lua_pushinteger(L
, ifp
->flags
);
99 lua_setfield(L
, -2, "flags");
100 lua_pushinteger(L
, ifp
->metric
);
101 lua_setfield(L
, -2, "metric");
102 lua_pushinteger(L
, ifp
->speed
);
103 lua_setfield(L
, -2, "speed");
104 lua_pushinteger(L
, ifp
->mtu
);
105 lua_setfield(L
, -2, "mtu");
106 lua_pushinteger(L
, ifp
->mtu6
);
107 lua_setfield(L
, -2, "mtu6");
108 lua_pushinteger(L
, ifp
->bandwidth
);
109 lua_setfield(L
, -2, "bandwidth");
110 lua_pushinteger(L
, ifp
->link_ifindex
);
111 lua_setfield(L
, -2, "link_ifindex");
112 lua_pushinteger(L
, ifp
->ll_type
);
113 lua_setfield(L
, -2, "linklayer_type");
116 void lua_decode_interface(lua_State
*L
, int idx
, struct interface
*ifp
)
118 lua_getfield(L
, idx
, "name");
119 strlcpy(ifp
->name
, lua_tostring(L
, -1), sizeof(ifp
->name
));
121 lua_getfield(L
, idx
, "ifindex");
122 ifp
->ifindex
= lua_tointeger(L
, -1);
124 lua_getfield(L
, idx
, "status");
125 ifp
->status
= lua_tointeger(L
, -1);
127 lua_getfield(L
, idx
, "flags");
128 ifp
->flags
= lua_tointeger(L
, -1);
130 lua_getfield(L
, idx
, "metric");
131 ifp
->metric
= lua_tointeger(L
, -1);
133 lua_getfield(L
, idx
, "speed");
134 ifp
->speed
= lua_tointeger(L
, -1);
136 lua_getfield(L
, idx
, "mtu");
137 ifp
->mtu
= lua_tointeger(L
, -1);
139 lua_getfield(L
, idx
, "mtu6");
140 ifp
->mtu6
= lua_tointeger(L
, -1);
142 lua_getfield(L
, idx
, "bandwidth");
143 ifp
->bandwidth
= lua_tointeger(L
, -1);
145 lua_getfield(L
, idx
, "link_ifindex");
146 ifp
->link_ifindex
= lua_tointeger(L
, -1);
148 lua_getfield(L
, idx
, "linklayer_type");
149 ifp
->ll_type
= lua_tointeger(L
, -1);
154 void *lua_tointerface(lua_State
*L
, int idx
)
156 struct interface
*ifp
= XCALLOC(MTYPE_TMP
, sizeof(struct interface
));
158 lua_decode_interface(L
, idx
, ifp
);
162 void lua_pushinaddr(lua_State
*L
, const struct in_addr
*addr
)
164 char buf
[INET_ADDRSTRLEN
];
166 inet_ntop(AF_INET
, addr
, buf
, sizeof(buf
));
169 lua_pushinteger(L
, addr
->s_addr
);
170 lua_setfield(L
, -2, "value");
171 lua_pushstring(L
, buf
);
172 lua_setfield(L
, -2, "string");
175 void lua_decode_inaddr(lua_State
*L
, int idx
, struct in_addr
*inaddr
)
177 lua_getfield(L
, idx
, "value");
178 inaddr
->s_addr
= lua_tointeger(L
, -1);
184 void *lua_toinaddr(lua_State
*L
, int idx
)
186 struct in_addr
*inaddr
= XCALLOC(MTYPE_TMP
, sizeof(struct in_addr
));
187 lua_decode_inaddr(L
, idx
, inaddr
);
192 void lua_pushin6addr(lua_State
*L
, const struct in6_addr
*addr
)
194 char buf
[INET6_ADDRSTRLEN
];
196 inet_ntop(AF_INET6
, addr
, buf
, sizeof(buf
));
199 lua_pushlstring(L
, (const char *)addr
->s6_addr
, 16);
200 lua_setfield(L
, -2, "value");
201 lua_pushstring(L
, buf
);
202 lua_setfield(L
, -2, "string");
205 void lua_decode_in6addr(lua_State
*L
, int idx
, struct in6_addr
*in6addr
)
207 lua_getfield(L
, idx
, "string");
208 inet_pton(AF_INET6
, lua_tostring(L
, -1), in6addr
);
214 void *lua_toin6addr(lua_State
*L
, int idx
)
216 struct in6_addr
*in6addr
= XCALLOC(MTYPE_TMP
, sizeof(struct in6_addr
));
217 lua_decode_in6addr(L
, idx
, in6addr
);
221 void lua_pushsockunion(lua_State
*L
, const union sockunion
*su
)
223 char buf
[SU_ADDRSTRLEN
];
225 sockunion2str(su
, buf
, sizeof(buf
));
228 lua_pushlstring(L
, (const char *)sockunion_get_addr(su
),
229 sockunion_get_addrlen(su
));
230 lua_setfield(L
, -2, "value");
231 lua_pushstring(L
, buf
);
232 lua_setfield(L
, -2, "string");
235 void lua_decode_sockunion(lua_State
*L
, int idx
, union sockunion
*su
)
237 lua_getfield(L
, idx
, "string");
238 str2sockunion(lua_tostring(L
, -1), su
);
244 void *lua_tosockunion(lua_State
*L
, int idx
)
246 union sockunion
*su
= XCALLOC(MTYPE_TMP
, sizeof(union sockunion
));
248 lua_decode_sockunion(L
, idx
, su
);
252 void lua_pushtimet(lua_State
*L
, const time_t *time
)
254 lua_pushinteger(L
, *time
);
257 void lua_decode_timet(lua_State
*L
, int idx
, time_t *t
)
259 *t
= lua_tointeger(L
, idx
);
263 void *lua_totimet(lua_State
*L
, int idx
)
265 time_t *t
= XCALLOC(MTYPE_TMP
, sizeof(time_t));
267 lua_decode_timet(L
, idx
, t
);
271 void lua_pushintegerp(lua_State
*L
, const long long *num
)
273 lua_pushinteger(L
, *num
);
276 void lua_decode_integerp(lua_State
*L
, int idx
, long long *num
)
279 *num
= lua_tonumberx(L
, idx
, &isnum
);
284 void *lua_tointegerp(lua_State
*L
, int idx
)
286 long long *num
= XCALLOC(MTYPE_TMP
, sizeof(long long));
288 lua_decode_integerp(L
, idx
, num
);
292 void lua_decode_stringp(lua_State
*L
, int idx
, char *str
)
294 strlcpy(str
, lua_tostring(L
, idx
), strlen(str
) + 1);
298 void *lua_tostringp(lua_State
*L
, int idx
)
300 char *string
= XSTRDUP(MTYPE_TMP
, lua_tostring(L
, idx
));
306 * Decoder for const values, since we cannot modify them.
308 void lua_decode_noop(lua_State
*L
, int idx
, const void *ptr
)
314 * Noop decoder for int.
316 void lua_decode_int_noop(lua_State
*L
, int idx
, int i
)
323 * Lua-compatible wrappers for FRR logging functions.
325 static const char *frrlua_log_thunk(lua_State
*L
)
329 nargs
= lua_gettop(L
);
332 return lua_tostring(L
, 1);
335 static int frrlua_log_debug(lua_State
*L
)
337 zlog_debug("%s", frrlua_log_thunk(L
));
341 static int frrlua_log_info(lua_State
*L
)
343 zlog_info("%s", frrlua_log_thunk(L
));
347 static int frrlua_log_notice(lua_State
*L
)
349 zlog_notice("%s", frrlua_log_thunk(L
));
353 static int frrlua_log_warn(lua_State
*L
)
355 zlog_warn("%s", frrlua_log_thunk(L
));
359 static int frrlua_log_error(lua_State
*L
)
361 zlog_err("%s", frrlua_log_thunk(L
));
365 static const luaL_Reg log_funcs
[] = {
366 {"debug", frrlua_log_debug
},
367 {"info", frrlua_log_info
},
368 {"notice", frrlua_log_notice
},
369 {"warn", frrlua_log_warn
},
370 {"error", frrlua_log_error
},
374 void frrlua_export_logging(lua_State
*L
)
377 luaL_setfuncs(L
, log_funcs
, 0);
378 lua_setglobal(L
, "log");
385 char *frrlua_stackdump(lua_State
*L
)
387 int top
= lua_gettop(L
);
390 struct buffer
*buf
= buffer_new(4098);
392 for (int i
= 1; i
<= top
; i
++) {
393 int t
= lua_type(L
, i
);
396 case LUA_TSTRING
: /* strings */
397 snprintf(tmpbuf
, sizeof(tmpbuf
), "\"%s\"\n",
399 buffer_putstr(buf
, tmpbuf
);
401 case LUA_TBOOLEAN
: /* booleans */
402 snprintf(tmpbuf
, sizeof(tmpbuf
), "%s\n",
403 lua_toboolean(L
, i
) ? "true" : "false");
404 buffer_putstr(buf
, tmpbuf
);
406 case LUA_TNUMBER
: /* numbers */
407 snprintf(tmpbuf
, sizeof(tmpbuf
), "%g\n",
409 buffer_putstr(buf
, tmpbuf
);
411 default: /* other values */
412 snprintf(tmpbuf
, sizeof(tmpbuf
), "%s\n",
414 buffer_putstr(buf
, tmpbuf
);
419 char *result
= XSTRDUP(MTYPE_TMP
, buffer_getstr(buf
));
426 #endif /* HAVE_SCRIPTING */