From 074d28ebc2c4b5ab952e74b9a815e0a55c26110a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 12 Mar 2015 19:08:42 +0000 Subject: [PATCH] Fix sorting order and unit test for upstreams. --- src/libutil/addr.c | 25 +++++++++++++++++-------- src/libutil/addr.h | 8 ++++++++ src/libutil/upstream.c | 9 ++++----- src/main.c | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/libutil/addr.c b/src/libutil/addr.c index 19eda5a13..c2bb3db9c 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -349,6 +349,12 @@ rspamd_parse_inet_address (rspamd_inet_addr_t **target, const char *src) if (src[0] == '/' || src[0] == '.') { return rspamd_parse_unix_path (target, src); } + else if (inet_pton (AF_INET, src, &su.s4.sin_addr) == 1) { + addr = rspamd_inet_addr_create (AF_INET); + memcpy (&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, + sizeof (struct in_addr)); + ret = TRUE; + } else if (ipv6_status == RSPAMD_IPV6_SUPPORTED && inet_pton (AF_INET6, src, &su.s6.sin6_addr) == 1) { addr = rspamd_inet_addr_create (AF_INET6); @@ -356,12 +362,6 @@ rspamd_parse_inet_address (rspamd_inet_addr_t **target, const char *src) sizeof (struct in6_addr)); ret = TRUE; } - else if (inet_pton (AF_INET, src, &su.s4.sin_addr) == 1) { - addr = rspamd_inet_addr_create (AF_INET6); - memcpy (&addr->u.in.addr.s4.sin_addr, &su.s4.sin_addr, - sizeof (struct in_addr)); - ret = TRUE; - } if (ret && target) { *target = addr; @@ -918,8 +918,8 @@ rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, g_assert (a2 != NULL); if (a1->af != a2->af) { - return (rspamd_inet_address_af_order (a1) - - rspamd_inet_address_af_order (a2)); + return (rspamd_inet_address_af_order (a2) - + rspamd_inet_address_af_order (a1)); } else { switch (a1->af) { @@ -940,6 +940,15 @@ rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, return 0; } +gint +rspamd_inet_address_compare_ptr (const gpointer a1, + const gpointer a2) +{ + const rspamd_inet_addr_t **i1 = a1, **i2 = a2; + + return rspamd_inet_address_compare (*i1, *i2); +} + rspamd_inet_addr_t * rspamd_inet_address_copy (const rspamd_inet_addr_t *addr) { diff --git a/src/libutil/addr.h b/src/libutil/addr.h index dd5b6adb6..1ea0ab8b5 100644 --- a/src/libutil/addr.h +++ b/src/libutil/addr.h @@ -196,6 +196,14 @@ void rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask); gint rspamd_inet_address_compare (const rspamd_inet_addr_t *a1, const rspamd_inet_addr_t *a2); +/** + * Utility function to compare addresses by in g_ptr_array + * @param a1 + * @param a2 + * @return + */ +gint rspamd_inet_address_compare_ptr (const gpointer a1, + const gpointer a2); /** * Performs deep copy of rspamd inet addr * @param addr diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 6c962c357..6aa447042 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -130,12 +130,12 @@ rspamd_upstream_af_to_weight (const rspamd_inet_addr_t *addr) static gint rspamd_upstream_addr_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; + const rspamd_inet_addr_t **ip1 = (const rspamd_inet_addr_t **)a, + **ip2 = (const rspamd_inet_addr_t **)b; gint w1, w2; - w1 = rspamd_upstream_af_to_weight (ip1); - w2 = rspamd_upstream_af_to_weight (ip2); + w1 = rspamd_upstream_af_to_weight (*ip1); + w2 = rspamd_upstream_af_to_weight (*ip2); return w2 - w1; } @@ -381,7 +381,6 @@ rspamd_upstream_dtor (struct upstream *up) rspamd_mutex_free (up->lock); g_free (up->name); - g_free (up->addrs.addr); g_slice_free1 (sizeof (*up), up); } diff --git a/src/main.c b/src/main.c index 0864ab395..373c01695 100644 --- a/src/main.c +++ b/src/main.c @@ -576,7 +576,7 @@ create_listen_socket (GPtrArray *addrs, guint cnt, gint listen_type) gint fd; guint i; - g_ptr_array_sort (addrs, (GCompareFunc)rspamd_inet_address_compare); + g_ptr_array_sort (addrs, rspamd_inet_address_compare_ptr); for (i = 0; i < cnt; i ++) { fd = rspamd_inet_address_listen (g_ptr_array_index (addrs, i), listen_type, TRUE); -- 2.39.5