summaryrefslogtreecommitdiff
path: root/accel-pppd/lua
diff options
context:
space:
mode:
authorDmitry Kozlov <xeb@mail.ru>2017-10-16 15:59:20 +0300
committerDmitry Kozlov <xeb@mail.ru>2017-10-16 16:53:52 +0300
commit55694075e77a4c70f99b97175d839bd556f9f1b4 (patch)
tree87f1965475611a9e0848c64d0e1162d82308aa87 /accel-pppd/lua
parent4bbecf70cc3ad33da75f0dd01d6b683f271f9bd0 (diff)
downloadaccel-ppp-55694075e77a4c70f99b97175d839bd556f9f1b4.tar.gz
accel-ppp-55694075e77a4c70f99b97175d839bd556f9f1b4.zip
lua: implemented "session" module that provides session object to be passed to lua scripts
session object consists of following functions: ifname() - interface name ifindex() - interface ifindex sid() - Acct-Session-ID uptime() - session uptime in seconds username() ctrl_type() - type of session (pppt/pppoe/l2tp/ipoe) calling_sid() - Calling-Station-ID called_sid() - Called-Station-ID ipv4() - retuns pair (peer address, local address) ipv6() - ipv6 address or nil rx_bytes() tx_bytes()
Diffstat (limited to 'accel-pppd/lua')
-rw-r--r--accel-pppd/lua/CMakeLists.txt1
-rw-r--r--accel-pppd/lua/luasupp.h5
-rw-r--r--accel-pppd/lua/session.c234
3 files changed, 239 insertions, 1 deletions
diff --git a/accel-pppd/lua/CMakeLists.txt b/accel-pppd/lua/CMakeLists.txt
index 699cfb7b..adf5ed25 100644
--- a/accel-pppd/lua/CMakeLists.txt
+++ b/accel-pppd/lua/CMakeLists.txt
@@ -4,6 +4,7 @@ INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR})
SET(sources
lua_lpack.c
lua_bit.c
+ session.c
)
ADD_LIBRARY(luasupp SHARED ${sources})
diff --git a/accel-pppd/lua/luasupp.h b/accel-pppd/lua/luasupp.h
index fa7a7c30..ea5a646b 100644
--- a/accel-pppd/lua/luasupp.h
+++ b/accel-pppd/lua/luasupp.h
@@ -1,9 +1,12 @@
#ifndef __LUASUPP_H
#define __LUASUPP_H
-#include "lua.h"
+#include <lua.h>
int luaopen_lpack(lua_State *L);
int luaopen_bit(lua_State *L);
+#define LUA_AP_SESSION "ap_session"
+int luaopen_ap_session(lua_State *L);
+
#endif
diff --git a/accel-pppd/lua/session.c b/accel-pppd/lua/session.c
new file mode 100644
index 00000000..d2e05fff
--- /dev/null
+++ b/accel-pppd/lua/session.c
@@ -0,0 +1,234 @@
+#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"
+
+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 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},
+ {NULL, NULL}
+};
+
+int __export luaopen_ap_session(lua_State *L)
+{
+ 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
+
+ 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) {
+ 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 gword_sz = (uint64_t)UINT32_MAX + 1;
+ uint64_t bytes = gword_sz*ses->acct_input_gigawords + ses->acct_rx_bytes;
+
+ if (!ses)
+ return 0;
+
+ 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 gword_sz = (uint64_t)UINT32_MAX + 1;
+ uint64_t bytes = gword_sz*ses->acct_output_gigawords + ses->acct_tx_bytes;
+
+ if (!ses)
+ return 0;
+
+ lua_pushnumber(L, bytes);
+
+ return 1;
+}
+
+