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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "ap_session.h"
#include "ipdb.h"
#include "utils.h"
#include "luasupp.h"
#include "log.h"
static int mod_cnt;
static const struct lua_session_module **mods;
static int session_ifname(lua_State *L);
static int session_ifindex(lua_State *L);
static int session_sid(lua_State *L);
static int session_uptime(lua_State *L);
static int session_username(lua_State *L);
static int session_type(lua_State *L);
static int session_calling_sid(lua_State *L);
static int session_called_sid(lua_State *L);
static int session_ipv4(lua_State *L);
static int session_ipv6(lua_State *L);
static int session_rx_bytes(lua_State *L);
static int session_tx_bytes(lua_State *L);
static int session_module(lua_State *L);
static const struct luaL_Reg session_lib [] = {
{"ifname", session_ifname},
{"ifindex", session_ifindex},
{"sid", session_sid},
{"uptime", session_uptime},
{"username", session_username},
{"ctrl_type", session_type},
{"calling_sid", session_calling_sid},
{"called_sid", session_called_sid},
{"ipv4", session_ipv4},
{"ipv6", session_ipv6},
{"rx_bytes", session_rx_bytes},
{"tx_bytes", session_tx_bytes},
{"module", session_module},
{NULL, NULL}
};
int __export luaopen_ap_session(lua_State *L)
{
int i;
luaL_newmetatable(L, LUA_AP_SESSION);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
#if LUA_VERSION_NUM < 502
luaL_register(L, NULL, session_lib);
#else
luaL_setfuncs(L, session_lib, 0);
#endif
for (i = 0; i < mod_cnt; i++)
mods[i]->init(L);
return 1;
}
static int session_ifname(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->ifname);
return 1;
}
static int session_ifindex(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushinteger(L, ses->ifindex);
return 1;
}
static int session_sid(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->sessionid);
return 1;
}
static int session_uptime(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
time_t t;
if (!ses)
return 0;
t = ses->stop_time ?: _time();
lua_pushinteger(L, t - ses->start_time);
return 1;
}
static int session_username(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->username);
return 1;
}
static int session_type(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->ctrl->name);
return 1;
}
static int session_calling_sid(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->ctrl->calling_station_id);
return 1;
}
static int session_called_sid(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
if (!ses)
return 0;
lua_pushstring(L, ses->ctrl->called_station_id);
return 1;
}
static int session_ipv4(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
char addr1[17], addr2[17];
if (!ses)
return 0;
if (ses->ipv4) {
u_inet_ntoa(ses->ipv4->peer_addr, addr1);
u_inet_ntoa(ses->ipv4->addr, addr2);
lua_pushstring(L, addr1);
lua_pushstring(L, addr2);
return 2;
}
lua_pushnil(L);
return 1;
}
static int session_ipv6(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
struct ipv6db_addr_t *a;
struct in6_addr addr;
char str[64];
if (!ses)
return 0;
if (ses->ipv6 && !list_empty(&ses->ipv6->addr_list)) {
a = list_entry(ses->ipv6->addr_list.next, typeof(*a), entry);
if (a->prefix_len) {
build_ip6_addr(a, ses->ipv6->peer_intf_id, &addr);
inet_ntop(AF_INET6, &addr, str, 64);
sprintf(strchr(str, 0), "/%i", a->prefix_len);
lua_pushstring(L, str);
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int session_rx_bytes(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
uint64_t bytes;
if (!ses)
return 0;
bytes = ses->acct_rx_bytes;
lua_pushnumber(L, bytes);
return 1;
}
static int session_tx_bytes(lua_State *L)
{
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
uint64_t bytes;
if (!ses)
return 0;
bytes = ses->acct_tx_bytes;
lua_pushnumber(L, bytes);
return 1;
}
static int session_module(lua_State *L)
{
int i;
struct ap_session *ses = luaL_checkudata(L, 1, LUA_AP_SESSION);
const char *name;
if (!ses)
return 0;
name = luaL_checkstring(L, 2);
if (!name)
return 0;
for (i = 0; i < mod_cnt; i++) {
if (strcmp(name, mods[i]->name) == 0)
return mods[i]->get_instance(L, ses);
}
lua_pushnil(L);
return 1;
}
void __export lua_session_module_register(const struct lua_session_module *mod)
{
void *mods_new;
if (!mods)
mods_new = malloc(sizeof(void *));
else
mods_new = realloc(mods, (mod_cnt + 1) * sizeof(void *));
if (mods_new) {
mods = mods_new;
mods[mod_cnt++] = mod;
} else {
log_emerg("lua: out of memory\n");
}
}
|