From 3046eb0ed3fc2ce283bc2cca26a9c7882da2fba4 Mon Sep 17 00:00:00 2001 From: Tomas Krizek Date: Fri, 8 Mar 2019 16:44:24 +0100 Subject: [PATCH] daemon/bindings/net: change output format of net.list() --- NEWS | 1 + daemon/bindings/net.c | 57 +++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/NEWS b/NEWS index 7c2a17c41..a82954d00 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,7 @@ Incompatible changes - DNSSEC is enabled by default - upstream packages for Debian now require systemd - libknot >= 2.8 is required +- net.list() output format changed Improvements ------------ diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c index 9139b17bf..7bd5fc585 100644 --- a/daemon/bindings/net.c +++ b/daemon/bindings/net.c @@ -27,20 +27,49 @@ static int net_list_add(const char *key, void *val, void *ext) { lua_State *L = (lua_State *)ext; + lua_Integer i = lua_tointeger(L, -1); endpoint_array_t *ep_array = val; - lua_newtable(L); - for (size_t i = ep_array->len; i--;) { - struct endpoint *ep = ep_array->at[i]; - lua_pushinteger(L, ep->port); - lua_setfield(L, -2, "port"); - lua_pushboolean(L, ep->flags & NET_UDP); - lua_setfield(L, -2, "udp"); - lua_pushboolean(L, ep->flags & NET_TCP); - lua_setfield(L, -2, "tcp"); - lua_pushboolean(L, ep->flags & NET_TLS); - lua_setfield(L, -2, "tls"); - } - lua_setfield(L, -2, key); + for (size_t j = ep_array->len; j--;) { + struct endpoint *ep = ep_array->at[j]; + lua_newtable(L); // connection tuple + + lua_pushstring(L, key); + lua_setfield(L, -2, "ip"); + + lua_newtable(L); // "transport" table + if (ep->flags & NET_UDP) { + lua_pushliteral(L, "udp"); + lua_setfield(L, -2, "protocol"); + lua_pushinteger(L, ep->port); + lua_setfield(L, -2, "port"); + lua_pushliteral(L, "none"); + lua_setfield(L, -2, "security"); + } else if (ep->flags & NET_TCP) { + lua_pushliteral(L, "tcp"); + lua_setfield(L, -2, "protocol"); + lua_pushinteger(L, ep->port); + lua_setfield(L, -2, "port"); + if (ep->flags & NET_TLS) { + lua_pushliteral(L, "tls"); + } else { + lua_pushliteral(L, "none"); + } + lua_setfield(L, -2, "security"); + } else { + lua_pushliteral(L, "unknown"); + lua_setfield(L, -2, "protocol"); + } + lua_setfield(L, -2, "transport"); + + lua_newtable(L); // "application" table + lua_pushliteral(L, "dns"); + lua_setfield(L, -2, "protocol"); + lua_setfield(L, -2, "application"); + + lua_settable(L, -3); + i++; + lua_pushinteger(L, i); + } return kr_ok(); } @@ -49,7 +78,9 @@ static int net_list(lua_State *L) { struct engine *engine = engine_luaget(L); lua_newtable(L); + lua_pushinteger(L, 1); map_walk(&engine->net.endpoints, net_list_add, L); + lua_pop(L, 1); return 1; } -- 2.47.2