diff options
Diffstat (limited to 'accel-pppd')
-rw-r--r-- | accel-pppd/cli/show_sessions.c | 10 | ||||
-rw-r--r-- | accel-pppd/include/ap_session.h | 3 | ||||
-rw-r--r-- | accel-pppd/ipdb.c | 11 | ||||
-rw-r--r-- | accel-pppd/ipdb.h | 2 | ||||
-rw-r--r-- | accel-pppd/lua/CMakeLists.txt | 1 | ||||
-rw-r--r-- | accel-pppd/lua/luasupp.h | 5 | ||||
-rw-r--r-- | accel-pppd/lua/session.c | 234 |
7 files changed, 255 insertions, 11 deletions
diff --git a/accel-pppd/cli/show_sessions.c b/accel-pppd/cli/show_sessions.c index eabf95bc..47adb3a3 100644 --- a/accel-pppd/cli/show_sessions.c +++ b/accel-pppd/cli/show_sessions.c @@ -408,16 +408,6 @@ static void print_ip6_dp(struct ap_session *ses, char *buf) sprintf(ptr, "/%i", a->prefix_len); } -static void build_ip6_addr(struct ipv6db_addr_t *a, uint64_t intf_id, struct in6_addr *addr) -{ - memcpy(addr, &a->addr, sizeof(*addr)); - - if (a->prefix_len <= 64) - *(uint64_t *)(addr->s6_addr + 8) = intf_id; - else - *(uint64_t *)(addr->s6_addr + 8) |= intf_id & ((1 << (128 - a->prefix_len)) - 1); -} - static void print_ip6(struct ap_session *ses, char *buf) { struct ipv6db_addr_t *a; diff --git a/accel-pppd/include/ap_session.h b/accel-pppd/include/ap_session.h index 421360db..b2d05327 100644 --- a/accel-pppd/include/ap_session.h +++ b/accel-pppd/include/ap_session.h @@ -1,6 +1,9 @@ #ifndef __AP_SESSION_H__ #define __AP_SESSION_H__ +#include <sys/socket.h> + +#include "triton.h" #include "ap_net.h" //#define AP_SESSIONID_LEN 16 diff --git a/accel-pppd/ipdb.c b/accel-pppd/ipdb.c index 7c3ebb82..a7ef0a44 100644 --- a/accel-pppd/ipdb.c +++ b/accel-pppd/ipdb.c @@ -1,3 +1,5 @@ +#include <string.h> + #include "triton.h" #include "ipdb.h" @@ -71,6 +73,15 @@ void __export ipdb_put_ipv6_prefix(struct ap_session *ses, struct ipv6db_prefix_ it->owner->put_ipv6_prefix(ses, it); } +void __export build_ip6_addr(struct ipv6db_addr_t *a, uint64_t intf_id, struct in6_addr *addr) +{ + memcpy(addr, &a->addr, sizeof(*addr)); + + if (a->prefix_len <= 64) + *(uint64_t *)(addr->s6_addr + 8) = intf_id; + else + *(uint64_t *)(addr->s6_addr + 8) |= intf_id & ((1 << (128 - a->prefix_len)) - 1); +} void __export ipdb_register(struct ipdb_t *ipdb) { diff --git a/accel-pppd/ipdb.h b/accel-pppd/ipdb.h index d461c88d..f2a6cee7 100644 --- a/accel-pppd/ipdb.h +++ b/accel-pppd/ipdb.h @@ -61,5 +61,7 @@ void ipdb_put_ipv6_prefix(struct ap_session *ses, struct ipv6db_prefix_t *it); void ipdb_register(struct ipdb_t *); +void build_ip6_addr(struct ipv6db_addr_t *a, uint64_t intf_id, struct in6_addr *addr); + #endif 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; +} + + |