From e09f655a7be0daffade2f33129577e034c3c84d9 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 9 Aug 2014 19:26:29 +0100 Subject: [PATCH] add appropriate sorting function for struct leases_t The HASH_SORT in analyze needs this. Signed-off-by: Sami Kerola --- src/.indent.pro | 1 + src/analyze.c | 2 +- src/dhcpd-pools.c | 1 + src/dhcpd-pools.h | 7 +++++++ src/other.c | 3 +++ src/sort.c | 25 +++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/.indent.pro b/src/.indent.pro index fb2aa48..16702c4 100644 --- a/src/.indent.pro +++ b/src/.indent.pro @@ -1,6 +1,7 @@ -linux -TFILE -Tipaddr_t +-Tleases_t -Toff_t -Trange_t -Tsize_t diff --git a/src/analyze.c b/src/analyze.c index 2ec8626..9c05a43 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -51,7 +51,7 @@ int prepare_data(void) { /* Sort leases */ - HASH_SORT(leases, ipcomp); + HASH_SORT(leases, leasecomp); /* Sort ranges */ qsort(ranges, (size_t)num_ranges, sizeof(struct range_t), &rangecomp); return 0; diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index 279db9a..974eaf6 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -71,6 +71,7 @@ const char *(*ntop_ipaddr) (const union ipaddr_t *ip); double (*get_range_size) (const struct range_t *r); int (*xstrstr) (const char *__restrict str); int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b); +int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b); int (*output_analysis) (void); void (*add_lease) (union ipaddr_t *ip, enum ltype type); struct leases_t *(*find_lease) (union ipaddr_t *ip); diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index f6dfd95..4c61273 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -269,6 +269,13 @@ extern int ipcomp_v4(const union ipaddr_t *restrict a, extern int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b) _DP_ATTRIBUTE_PURE; +extern int (*leasecomp) (const struct leases_t *restrict a, const struct leases_t *restrict b); +extern int leasecomp_init(const struct leases_t *restrict a + __attribute__ ((unused)), + const struct leases_t *restrict b __attribute__ ((unused))); +extern int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b); +extern int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b); + extern int comp_cur(struct range_t *r1, struct range_t *r2) _DP_ATTRIBUTE_PURE; extern int comp_double(double f1, double f2) _DP_ATTRIBUTE_CONST; extern int comp_ip(struct range_t *r1, struct range_t *r2); diff --git a/src/other.c b/src/other.c index fabb6d8..a983a5c 100644 --- a/src/other.c +++ b/src/other.c @@ -69,6 +69,7 @@ void set_ipv_functions(int version) find_lease = find_lease_v4; get_range_size = get_range_size_v4; ipcomp = ipcomp_v4; + leasecomp = leasecomp_v4; ntop_ipaddr = ntop_ipaddr_v4; parse_ipaddr = parse_ipaddr_v4; xstrstr = xstrstr_v4; @@ -81,6 +82,7 @@ void set_ipv_functions(int version) find_lease = find_lease_v6; get_range_size = get_range_size_v6; ipcomp = ipcomp_v6; + leasecomp = leasecomp_v6; ntop_ipaddr = ntop_ipaddr_v6; parse_ipaddr = parse_ipaddr_v6; xstrstr = xstrstr_v6; @@ -93,6 +95,7 @@ void set_ipv_functions(int version) find_lease = find_lease_init; get_range_size = get_range_size_init; ipcomp = ipcomp_init; + leasecomp = leasecomp_init; ntop_ipaddr = ntop_ipaddr_init; parse_ipaddr = parse_ipaddr_init; xstrstr = xstrstr_init; diff --git a/src/sort.c b/src/sort.c index bafc290..666b11e 100644 --- a/src/sort.c +++ b/src/sort.c @@ -74,6 +74,31 @@ int ipcomp_v6(const union ipaddr_t *restrict a, const union ipaddr_t *restrict b return memcmp(&a->v6, &b->v6, sizeof(a->v6)); } +/*! \brief Compare IP address in leases_t structure, with IPv4/v6 determination. + * \param a Binary IP address. + * \param b Binary IP address. + * \return If a < b return -1, if a < b return 1, when they are equal return 0. + */ +int leasecomp_init(const struct leases_t *restrict a __attribute__ ((unused)), + const struct leases_t *restrict b __attribute__ ((unused))) +{ + return 0; +} + +int leasecomp_v4(const struct leases_t *restrict a, const struct leases_t *restrict b) +{ + if (a->ip.v4 < b->ip.v4) + return -1; + if (a->ip.v4 > b->ip.v4) + return 1; + return 0; +} + +int leasecomp_v6(const struct leases_t *restrict a, const struct leases_t *restrict b) +{ + return memcmp(&a->ip.v6, &b->ip.v6, sizeof(a->ip.v6)); +} + /*! \brief Compare IP address in leases. Suitable for sorting range table. * \param r1 A range structure. * \param r2 A range structure.