gint i = 0;
struct rspamd_dns_resolver **presolver;
struct rdns_reply_entry *elt;
+ rspamd_inet_addr_t addr;
lua_rawgeti (cd->L, LUA_REGISTRYINDEX, cd->cbref);
presolver = lua_newuserdata (cd->L, sizeof (gpointer));
LL_FOREACH (reply->entries, elt) {
switch (elt->type) {
case RDNS_REQUEST_A:
- lua_ip_push (cd->L, AF_INET, &elt->content.a.addr);
+ addr.af = AF_INET;
+ addr.slen = sizeof (addr.addr.s4);
+ memcpy (&addr.addr.s4.sin_addr, &elt->content.a.addr,
+ sizeof (addr.addr.s4.sin_addr));
+ lua_ip_push (cd->L, &addr);
lua_rawseti (cd->L, -2, ++i);
break;
case RDNS_REQUEST_AAAA:
- lua_ip_push (cd->L, AF_INET6, &elt->content.aaa.addr);
+ addr.af = AF_INET6;
+ addr.slen = sizeof (addr.addr.s6);
+ memcpy (&addr.addr.s6.sin6_addr, &elt->content.aaa.addr,
+ sizeof (addr.addr.s6.sin6_addr));
+ lua_ip_push (cd->L, &addr);
lua_rawseti (cd->L, -2, ++i);
break;
case RDNS_REQUEST_PTR:
if (ip != NULL && ip->is_valid) {
lua_newtable (L);
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
max = 32 / 8;
}
else {
max = 128 / 8;
}
- ptr = (guint8 *)&ip->data;
+ ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr;
for (i = 1; i <= max; i ++, ptr ++) {
lua_pushnumber (L, *ptr);
lua_rawseti (L, -2, i);
if (ip != NULL && ip->is_valid) {
lua_newtable (L);
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
max = 32 / 8;
}
else {
max = 128 / 8;
}
- ptr = (guint8 *)&ip->data;
+ ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr;
for (i = 1; i <= max; i ++, ptr ++) {
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
lua_pushstring (L, numbuf);
lua_rawseti (L, -2, i);
if (ip != NULL && ip->is_valid) {
lua_newtable (L);
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
max = 32 / 8;
}
else {
max = 128 / 8;
}
- ptr = (guint8 *)&ip->data;
+ ptr = (guint8 *)&ip->addr.addr.s6.sin6_addr;
ptr += max - 1;
for (i = 1; i <= max; i ++, ptr --) {
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
rspamd_snprintf (numbuf, sizeof (numbuf), "%d", *ptr);
lua_pushstring (L, numbuf);
lua_rawseti (L, -2, i);
lua_ip_to_string (lua_State *L)
{
struct rspamd_lua_ip *ip = lua_check_ip (L, 1);
- gchar dst[INET6_ADDRSTRLEN + 1];
if (ip != NULL && ip->is_valid) {
- lua_pushstring (L, inet_ntop (ip->af, &ip->data, dst, sizeof (dst)));
+ lua_pushstring (L, rspamd_inet_address_to_string (&ip->addr));
}
else {
lua_pushnil (L);
ip_str = luaL_checkstring (L, 1);
if (ip_str) {
ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
- if (inet_pton (AF_INET, ip_str, &ip->data.ip4) == 1) {
- ip->af = AF_INET;
- ip->is_valid = TRUE;
- }
- else if (inet_pton (AF_INET6, ip_str, &ip->data.ip6) == 1) {
- ip->af = AF_INET6;
- ip->is_valid = TRUE;
- }
- else {
- ip->is_valid = FALSE;
- }
+ ip->is_valid = rspamd_parse_inet_address (&ip->addr, ip_str);
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
lua_setclass (L, "rspamd{ip}", -1);
*pip = ip;
guint32 dst[4], i;
if (ip != NULL && ip->is_valid) {
- if (ip->af == AF_INET) {
+ if (ip->addr.af == AF_INET) {
/* One integer in host byte order */
- lua_pushinteger (L, ntohl (ip->data.ip4.s_addr));
+ lua_pushinteger (L, ntohl (ip->addr.addr.s4.sin_addr.s_addr));
}
else {
/* 4 integers in host byte order */
- G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (dst));
- memcpy (dst, &ip->data, sizeof (dst));
+ G_STATIC_ASSERT (sizeof (ip->addr.addr.s6.sin6_addr) >= sizeof (dst));
+ memcpy (dst, &ip->addr.addr.s6.sin6_addr, sizeof (dst));
for (i = 0; i < G_N_ELEMENTS (dst); i ++) {
lua_pushinteger (L, ntohl (dst[i]));
}
/* Ipv4 version */
ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
src[0] = lua_tointeger (L, 1);
- ip->af = AF_INET;
+ ip->addr.af = AF_INET;
ip->is_valid = TRUE;
- ip->data.ip4.s_addr = htonl (src[0]);
+ ip->addr.addr.s4.sin_addr.s_addr = htonl (src[0]);
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
lua_setclass (L, "rspamd{ip}", -1);
*pip = ip;
for (i = 0; i < 4; i ++) {
src[i] = htonl (lua_tonumber (L, i + 1));
}
- G_STATIC_ASSERT (sizeof (ip->data) >= sizeof (src));
+ G_STATIC_ASSERT (sizeof (ip->addr.addr.s6.sin6_addr) >= sizeof (src));
ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
- ip->af = AF_INET6;
+ ip->addr.af = AF_INET6;
ip->is_valid = TRUE;
- memcpy (&ip->data, src, sizeof (src));
+ memcpy (&ip->addr.addr.s6.sin6_addr, src, sizeof (src));
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
lua_setclass (L, "rspamd{ip}", -1);
*pip = ip;
struct rspamd_lua_ip *ip = lua_check_ip (L, 1);
if (ip && ip->is_valid) {
- lua_pushnumber (L, ip->af == AF_INET6 ? 6 : 4);
+ lua_pushnumber (L, ip->addr.af == AF_INET6 ? 6 : 4);
}
else {
lua_pushnil (L);
}
void
-lua_ip_push (lua_State *L, int af, gpointer data)
+lua_ip_push (lua_State *L, rspamd_inet_addr_t *addr)
{
struct rspamd_lua_ip *ip, **pip;
ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
- if (!rspamd_ip_is_valid (data, af)) {
+ if (!rspamd_ip_is_valid (addr)) {
ip->is_valid = FALSE;
}
else {
ip->is_valid = TRUE;
- ip->af = af;
- if (af == AF_INET6) {
- memcpy (&ip->data, data, sizeof (struct in6_addr));
- }
- else {
- memcpy (&ip->data, data, sizeof (struct in_addr));
- }
-
+ memcpy (&ip->addr, addr, sizeof (ip->addr));
}
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
lua_setclass (L, "rspamd{ip}", -1);
}
else {
ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
- ip->is_valid = TRUE;
- if (inet_pton (AF_INET, ip_str, &ip->data.ip4) == 1) {
- ip->af = AF_INET;
- }
- else if (inet_pton (AF_INET6, ip_str, &ip->data.ip6) == 1) {
- ip->af = AF_INET6;
- }
- else {
- ip->is_valid = FALSE;
- }
+ ip->is_valid = rspamd_parse_inet_address (&ip->addr, ip_str);
pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
lua_setclass (L, "rspamd{ip}", -1);