1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "lua.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
static int luaB_err(lua_State *L) {
int n = lua_gettop(L);
if (n != 1)
log(L_WARN "bird.err() accepts exactly 1 argument");
if (n < 1)
return 0;
log(L_ERR "%s", lua_tostring(L, 1));
return 0;
}
static int luaB_warn(lua_State *L) {
int n = lua_gettop(L);
if (n != 1)
log(L_WARN "bird.warn() accepts exactly 1 argument");
if (n < 1)
return 0;
log(L_WARN "%s", lua_tostring(L, 1));
return 0;
}
static int luaB_info(lua_State *L) {
int n = lua_gettop(L);
if (n != 1)
log(L_WARN "bird.info() accepts exactly 1 argument");
if (n < 1)
return 0;
log(L_INFO "%s", lua_tostring(L, 1));
return 0;
}
static int luaB_trace(lua_State *L) {
int n = lua_gettop(L);
if (n != 1)
log(L_WARN "bird.trace() accepts exactly 1 argument");
if (n < 1)
return 0;
log(L_TRACE "%s", lua_tostring(L, 1));
return 0;
}
#define lua_sett(L, idx, val, what) do { \
lua_pushstring(L, idx); \
lua_push##what(L, val); \
lua_settable(L, -3); \
} while (0)
#define lua_settablecfunction(L, idx, val) lua_sett(L, idx, val, cfunction)
#define lua_settableinteger(L, idx, val) lua_sett(L, idx, val, integer)
#define lua_settableip4(L, idx, val) lua_sett(L, idx, val, ip4)
#define lua_settablelightuserdata(L, idx, val) lua_sett(L, idx, val, lightuserdata)
#define lua_setglobalcfunction(L, n, val) do { \
lua_pushcfunction(L, val); \
lua_setglobal(L, n); \
} while (0)
static int luaB_generic_concat(lua_State *L) {
int n = lua_gettop(L);
if (n != 2) {
log(L_WARN "__concat needs exactly 2 arguments");
return 0;
}
const char *a, *b;
size_t la, lb;
a = luaL_tolstring(L, 1, &la);
b = luaL_tolstring(L, 2, &lb);
if (a == NULL) {
a = "";
la = 0;
}
if (b == NULL) {
b = "";
lb = 0;
}
char *c = alloca(la + lb + 1);
memcpy(c, a, la);
memcpy(c + la, b, lb);
c[la + lb] = 0;
lua_pushlstring(L, c, la + lb);
return 1;
}
static int luaB_ip4_tostring(lua_State *L) {
int n = lua_gettop(L);
if (n != 1) {
log(L_WARN "__tostring needs exactly 1 argument");
return 0;
}
lua_pushliteral(L, "addr");
lua_gettable(L, 1);
lua_Integer a = lua_tointeger(L, -1);
char c[IP4_MAX_TEXT_LENGTH];
bsnprintf(c, IP4_MAX_TEXT_LENGTH, "%I4", a);
lua_pushstring(L, c);
return 1;
}
static void lua_puship4(lua_State *L, ip4_addr a) {
lua_newtable(L);
lua_settableinteger(L, "addr", ip4_to_u32(a));
lua_newtable(L);
lua_settablecfunction(L, "__tostring", luaB_ip4_tostring);
lua_settablecfunction(L, "__concat", luaB_generic_concat);
lua_setmetatable(L, -2);
}
static lua_bird_state *luaB_getinternalstate(lua_State *L) {
lua_getglobal(L, "bird");
lua_pushstring(L, "_internal_state");
lua_gettable(L, -2);
if (!lua_isuserdata(L, -1))
luaL_error(L, "fatal: bird internal state not found, type %d", lua_type(L, -1));
lua_bird_state *lbs = lua_touserdata(L, -1);
lua_pop(L, 2); /* Pop the user data and then the table. The string is consumed by gettable(). */
return lbs;
}
static int luaB_global_exception(lua_State *L, int value) {
int n = lua_gettop(L);
if (n > 1)
log(L_WARN "Called exception with too many arguments.");
lua_bird_state *lbs = luaB_getinternalstate(L);
lbs->exception = value;
lua_error(L);
return 0;
}
static inline int luaB_accept(lua_State *L) { return luaB_global_exception(L, F_ACCEPT); }
static inline int luaB_reject(lua_State *L) { return luaB_global_exception(L, F_REJECT); }
lua_bird_state *luaB_init(lua_State *L, struct linpool *lp) {
lua_newtable(L);
lua_settablecfunction(L, "err", luaB_err);
lua_settablecfunction(L, "warn", luaB_warn);
lua_settablecfunction(L, "info", luaB_info);
lua_settablecfunction(L, "trace", luaB_trace);
lua_bird_state *lbs = lp_allocz(lp, sizeof(lua_bird_state));
lua_settablelightuserdata(L, "_internal_state", lbs);
lua_settableip4(L, "router_id", ip4_from_u32(config->router_id));
lua_setglobal(L, "bird");
lua_pushcfunction(L, luaB_accept);
lua_setglobal(L, "accept");
lua_pushcfunction(L, luaB_reject);
lua_setglobal(L, "reject");
return lbs;
}
void luaB_push_route(lua_State *L, struct rte *e) {
}
|