aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-08-22 12:58:28 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-08-22 12:58:28 +0100
commit28193d7f3fc7377388055ba9d6311747659f4303 (patch)
treec52439c872a492670de145cb28ed8ee10fe1c0ce /src/libserver
parentdc917551811ef6850831f0770016e65f3d2e51dd (diff)
downloadrspamd-28193d7f3fc7377388055ba9d6311747659f4303.tar.gz
rspamd-28193d7f3fc7377388055ba9d6311747659f4303.zip
[Minor] Always try to check IPv4 address first when checking HTTP maps
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/maps/map.c53
1 files changed, 48 insertions, 5 deletions
diff --git a/src/libserver/maps/map.c b/src/libserver/maps/map.c
index b0801cbf3..712bdeaa8 100644
--- a/src/libserver/maps/map.c
+++ b/src/libserver/maps/map.c
@@ -1127,6 +1127,40 @@ rspamd_map_schedule_periodic (struct rspamd_map *map, int how)
cbd, jittered_sec, map->name, reason);
}
+static gint
+rspamd_map_af_to_weight (const rspamd_inet_addr_t *addr)
+{
+ int ret;
+
+ switch (rspamd_inet_address_get_af (addr)) {
+ case AF_UNIX:
+ ret = 2;
+ break;
+ case AF_INET:
+ ret = 1;
+ break;
+ default:
+ ret = 0;
+ break;
+ }
+
+ return ret;
+}
+
+static gint
+rspamd_map_dns_address_sort_func (gconstpointer a, gconstpointer b)
+{
+ const rspamd_inet_addr_t *ip1 = *(const rspamd_inet_addr_t **)a,
+ *ip2 = *(const rspamd_inet_addr_t **)b;
+ gint w1, w2;
+
+ w1 = rspamd_map_af_to_weight (ip1);
+ w2 = rspamd_map_af_to_weight (ip2);
+
+ /* Inverse order */
+ return w2 - w1;
+}
+
static void
rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
{
@@ -1188,11 +1222,20 @@ rspamd_map_dns_callback (struct rdns_reply *reply, void *arg)
}
if (cbd->stage == http_map_http_conn && cbd->addrs->len > 0) {
- guint selected_addr_idx;
-
- selected_addr_idx = rspamd_random_uint64_fast () % cbd->addrs->len;
- cbd->addr = (rspamd_inet_addr_t *)g_ptr_array_index (cbd->addrs,
- selected_addr_idx);
+ rspamd_ptr_array_shuffle (cbd->addrs);
+ /*
+ * For the existing addr we can just select any address as we have
+ * data available
+ */
+ if (cbd->map->nelts > 0 && rspamd_random_double_fast () > 0.5) {
+ /* Already shuffled, use whatever is the first */
+ cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, 0);
+ }
+ else {
+ /* Always prefer IPv4 as IPv6 is almost all the time broken */
+ g_ptr_array_sort (cbd->addrs, rspamd_map_dns_address_sort_func);
+ cbd->addr = (rspamd_inet_addr_t *) g_ptr_array_index (cbd->addrs, 0);
+ }
msg_debug_map ("open http connection to %s",
rspamd_inet_address_to_string_pretty (cbd->addr));