]>
Commit | Line | Data |
---|---|---|
634949ae DS |
1 | /* |
2 | * This file defines the lua interface into | |
3 | * FRRouting. | |
4 | * | |
5 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
6 | * Donald Sharp | |
7 | * | |
8678d638 | 8 | * This file is part of FRRouting (FRR). |
634949ae DS |
9 | * |
10 | * FRR is free software; you can redistribute it and/or modify it under the | |
11 | * terms of the GNU General Public License as published by the Free Software | |
12 | * Foundation; either version 2, or (at your option) any later version. | |
13 | * | |
14 | * FRR is distributed in the hope that it will be useful, but WITHOUT ANY | |
15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
16 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
17 | * details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with FRR; see the file COPYING. If not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | */ | |
634949ae DS |
23 | |
24 | #include <zebra.h> | |
25 | ||
26 | #if defined(HAVE_LUA) | |
27 | #include "prefix.h" | |
5dbb0a7b | 28 | #include "frrlua.h" |
634949ae DS |
29 | #include "log.h" |
30 | ||
31 | static int lua_zlog_debug(lua_State *L) | |
32 | { | |
33 | int debug_lua = 1; | |
34 | const char *string = lua_tostring(L, 1); | |
35 | ||
36 | if (debug_lua) | |
37 | zlog_debug("%s", string); | |
38 | ||
39 | return 0; | |
40 | } | |
41 | ||
42 | const char *get_string(lua_State *L, const char *key) | |
43 | { | |
44 | const char *str; | |
45 | ||
46 | lua_pushstring(L, key); | |
47 | lua_gettable(L, -2); | |
48 | ||
49 | str = (const char *)lua_tostring(L, -1); | |
50 | lua_pop(L, 1); | |
51 | ||
52 | return str; | |
53 | } | |
54 | ||
55 | int get_integer(lua_State *L, const char *key) | |
56 | { | |
57 | int result; | |
58 | ||
59 | lua_pushstring(L, key); | |
60 | lua_gettable(L, -2); | |
61 | ||
62 | result = lua_tointeger(L, -1); | |
63 | lua_pop(L, 1); | |
64 | ||
65 | return result; | |
66 | } | |
67 | ||
68 | static void *lua_alloc(void *ud, void *ptr, size_t osize, | |
69 | size_t nsize) | |
70 | { | |
71 | (void)ud; (void)osize; /* not used */ | |
72 | if (nsize == 0) { | |
73 | free(ptr); | |
74 | return NULL; | |
75 | } else | |
76 | return realloc(ptr, nsize); | |
77 | } | |
78 | ||
79 | lua_State *lua_initialize(const char *file) | |
80 | { | |
81 | int status; | |
82 | lua_State *L = lua_newstate(lua_alloc, NULL); | |
83 | ||
84 | zlog_debug("Newstate: %p", L); | |
85 | luaL_openlibs(L); | |
86 | zlog_debug("Opened lib"); | |
87 | status = luaL_loadfile(L, file); | |
88 | if (status) { | |
89 | zlog_debug("Failure to open %s %d", file, status); | |
90 | lua_close(L); | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | lua_pcall(L, 0, LUA_MULTRET, 0); | |
95 | zlog_debug("Setting global function"); | |
96 | lua_pushcfunction(L, lua_zlog_debug); | |
97 | lua_setglobal(L, "zlog_debug"); | |
98 | ||
99 | return L; | |
100 | } | |
101 | ||
102 | void lua_setup_prefix_table(lua_State *L, const struct prefix *prefix) | |
103 | { | |
104 | char buffer[100]; | |
105 | ||
106 | lua_newtable(L); | |
107 | lua_pushstring(L, prefix2str(prefix, buffer, 100)); | |
108 | lua_setfield(L, -2, "route"); | |
109 | lua_pushinteger(L, prefix->family); | |
110 | lua_setfield(L, -2, "family"); | |
111 | lua_setglobal(L, "prefix"); | |
112 | } | |
113 | ||
114 | enum lua_rm_status lua_run_rm_rule(lua_State *L, const char *rule) | |
115 | { | |
116 | int status; | |
117 | ||
118 | lua_getglobal(L, rule); | |
119 | status = lua_pcall(L, 0, 1, 0); | |
120 | if (status) { | |
121 | zlog_debug("Executing Failure with function: %s: %d", | |
122 | rule, status); | |
123 | return LUA_RM_FAILURE; | |
124 | } | |
125 | ||
126 | status = lua_tonumber(L, -1); | |
127 | return status; | |
128 | } | |
129 | #endif |