]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file defines the lua interface into | |
3 | * FRRouting. | |
4 | * | |
5 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
6 | * Donald Sharp | |
7 | * | |
8 | * This file is part of FreeRangeRouting (FRR). | |
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 | */ | |
23 | #include <stdio.h> | |
24 | ||
25 | #include <zebra.h> | |
26 | ||
27 | #if defined(HAVE_LUA) | |
28 | #include "prefix.h" | |
29 | #include "lua.h" | |
30 | #include "log.h" | |
31 | ||
32 | static int lua_zlog_debug(lua_State *L) | |
33 | { | |
34 | int debug_lua = 1; | |
35 | const char *string = lua_tostring(L, 1); | |
36 | ||
37 | if (debug_lua) | |
38 | zlog_debug("%s", string); | |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
43 | const char *get_string(lua_State *L, const char *key) | |
44 | { | |
45 | const char *str; | |
46 | ||
47 | lua_pushstring(L, key); | |
48 | lua_gettable(L, -2); | |
49 | ||
50 | str = (const char *)lua_tostring(L, -1); | |
51 | lua_pop(L, 1); | |
52 | ||
53 | return str; | |
54 | } | |
55 | ||
56 | int get_integer(lua_State *L, const char *key) | |
57 | { | |
58 | int result; | |
59 | ||
60 | lua_pushstring(L, key); | |
61 | lua_gettable(L, -2); | |
62 | ||
63 | result = lua_tointeger(L, -1); | |
64 | lua_pop(L, 1); | |
65 | ||
66 | return result; | |
67 | } | |
68 | ||
69 | static void *lua_alloc(void *ud, void *ptr, size_t osize, | |
70 | size_t nsize) | |
71 | { | |
72 | (void)ud; (void)osize; /* not used */ | |
73 | if (nsize == 0) { | |
74 | free(ptr); | |
75 | return NULL; | |
76 | } else | |
77 | return realloc(ptr, nsize); | |
78 | } | |
79 | ||
80 | lua_State *lua_initialize(const char *file) | |
81 | { | |
82 | int status; | |
83 | lua_State *L = lua_newstate(lua_alloc, NULL); | |
84 | ||
85 | zlog_debug("Newstate: %p", L); | |
86 | luaL_openlibs(L); | |
87 | zlog_debug("Opened lib"); | |
88 | status = luaL_loadfile(L, file); | |
89 | if (status) { | |
90 | zlog_debug("Failure to open %s %d", file, status); | |
91 | lua_close(L); | |
92 | return NULL; | |
93 | } | |
94 | ||
95 | lua_pcall(L, 0, LUA_MULTRET, 0); | |
96 | zlog_debug("Setting global function"); | |
97 | lua_pushcfunction(L, lua_zlog_debug); | |
98 | lua_setglobal(L, "zlog_debug"); | |
99 | ||
100 | return L; | |
101 | } | |
102 | ||
103 | void lua_setup_prefix_table(lua_State *L, const struct prefix *prefix) | |
104 | { | |
105 | char buffer[100]; | |
106 | ||
107 | lua_newtable(L); | |
108 | lua_pushstring(L, prefix2str(prefix, buffer, 100)); | |
109 | lua_setfield(L, -2, "route"); | |
110 | lua_pushinteger(L, prefix->family); | |
111 | lua_setfield(L, -2, "family"); | |
112 | lua_setglobal(L, "prefix"); | |
113 | } | |
114 | ||
115 | enum lua_rm_status lua_run_rm_rule(lua_State *L, const char *rule) | |
116 | { | |
117 | int status; | |
118 | ||
119 | lua_getglobal(L, rule); | |
120 | status = lua_pcall(L, 0, 1, 0); | |
121 | if (status) { | |
122 | zlog_debug("Executing Failure with function: %s: %d", | |
123 | rule, status); | |
124 | return LUA_RM_FAILURE; | |
125 | } | |
126 | ||
127 | status = lua_tonumber(L, -1); | |
128 | return status; | |
129 | } | |
130 | #endif |