Hi all,
I started to drive into haproxy's lua interface. I produced a few code
that allows dnsbl lookup and it seems to work.
First I have a C wrapper against the libc resolver..
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <lua53/lua.h>
#include <lua53/lauxlib.h>
static int gethostbyname_wrapper(lua_State *L)
{
const char* query = luaL_checkstring(L, 1);
struct hostent *he;
if ((he = gethostbyname(query)) != NULL) {
const char *first_addr =
inet_ntoa(*(struct in_addr*)he->h_addr_list[0]);
lua_pushstring(L, first_addr);
return 1;
}
return 0;
}
static const luaL_Reg sysdb_methods[] = {
{"gethostbyname", gethostbyname_wrapper},
{NULL, NULL}
};
LUALIB_API int luaopen_sysdb(lua_State *L) {
luaL_newlib(L, sysdb_methods);
return 1;
}
I have some doubts on the asyncness of libc operations but in other
side I don't want to reinvent the wheel. Should I prefer a resolver
implementation that uses lua socket ? As far as I tested libc seems to
do the job.
Then the lua code
local sysdb = require("sysdb")
core.register_fetches("rbl", function(txn, rbl, ip)
if (not ip) then
ip = txn.sf:src()
end
if (not rbl) then
rbl = "zen.spamhaus.org"
end
local query = rbl
for x in string.gmatch(ip, "[^%.]+") do
query = x .. '.' .. query
end
if(sysdb.gethostbyname(query)) then
return 1
else
return 0
end
end)
I want to use a sticky table as a local cache so my second question :
is there a way to set a gpt0 value from lua ?
Thanks for any comments on this.
Joris