{
struct rspamd_lua_ip *ip, **pip;
- ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
-
- ip->af = af;
- if (af == AF_INET6) {
- memcpy (&ip->data, data, sizeof (struct in6_addr));
+ if (!rspamd_ip_is_valid (data, af)) {
+ lua_pushnil (L);
}
else {
- memcpy (&ip->data, data, sizeof (struct in_addr));
+ ip = g_slice_alloc (sizeof (struct rspamd_lua_ip));
+
+ ip->af = af;
+ if (af == AF_INET6) {
+ memcpy (&ip->data, data, sizeof (struct in6_addr));
+ }
+ else {
+ memcpy (&ip->data, data, sizeof (struct in_addr));
+ }
+ pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
+ lua_setclass (L, "rspamd{ip}", -1);
+ *pip = ip;
}
- pip = lua_newuserdata (L, sizeof (struct rspamd_lua_ip *));
- lua_setclass (L, "rspamd{ip}", -1);
- *pip = ip;
}
void
g_random_set_seed (rand_seed);
}
+gboolean
+rspamd_ip_is_valid (void *ptr, int af)
+{
+ const struct in_addr ip4_any = { INADDR_ANY }, ip4_none = { INADDR_NONE };
+ const struct in6_addr ip6_any = IN6ADDR_ANY_INIT;
+
+ gboolean ret = FALSE;
+
+ if (G_LIKELY (af == AF_INET)) {
+ if (memcmp (ptr, &ip4_any, sizeof (struct in_addr)) != 0 &&
+ memcmp (ptr, &ip4_none, sizeof (struct in_addr)) != 0) {
+ ret = TRUE;
+ }
+ }
+ else if (G_UNLIKELY (af == AF_INET6)) {
+ if (memcmp (ptr, &ip6_any, sizeof (struct in6_addr)) != 0) {
+ ret = TRUE;
+ }
+ }
+
+ return ret;
+}
+
/*
* vi:ts=4
*/
*/
void rspamd_random_bytes (gchar *buf, gsize buflen);
+/**
+ * Check whether specified ip is valid (not INADDR_ANY or INADDR_NONE) for ipv4 or ipv6
+ * @param ptr pointer to struct in_addr or struct in6_addr
+ * @param af address family (AF_INET or AF_INET6)
+ * @return TRUE if the address is valid
+ */
+gboolean rspamd_ip_is_valid (void *ptr, int af);
+
#endif