add appropriate sorting function for struct leases_t

The HASH_SORT in analyze needs this.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2014-08-09 19:26:29 +01:00
parent 965875d20b
commit e09f655a7b
6 changed files with 38 additions and 1 deletions

1
src/.indent.pro vendored
View file

@ -1,6 +1,7 @@
-linux -linux
-TFILE -TFILE
-Tipaddr_t -Tipaddr_t
-Tleases_t
-Toff_t -Toff_t
-Trange_t -Trange_t
-Tsize_t -Tsize_t

View file

@ -51,7 +51,7 @@
int prepare_data(void) int prepare_data(void)
{ {
/* Sort leases */ /* Sort leases */
HASH_SORT(leases, ipcomp); HASH_SORT(leases, leasecomp);
/* Sort ranges */ /* Sort ranges */
qsort(ranges, (size_t)num_ranges, sizeof(struct range_t), &rangecomp); qsort(ranges, (size_t)num_ranges, sizeof(struct range_t), &rangecomp);
return 0; return 0;

View file

@ -71,6 +71,7 @@ const char *(*ntop_ipaddr) (const union ipaddr_t *ip);
double (*get_range_size) (const struct range_t *r); double (*get_range_size) (const struct range_t *r);
int (*xstrstr) (const char *__restrict str); int (*xstrstr) (const char *__restrict str);
int (*ipcomp) (const union ipaddr_t *restrict a, const union ipaddr_t *restrict b); 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); int (*output_analysis) (void);
void (*add_lease) (union ipaddr_t *ip, enum ltype type); void (*add_lease) (union ipaddr_t *ip, enum ltype type);
struct leases_t *(*find_lease) (union ipaddr_t *ip); struct leases_t *(*find_lease) (union ipaddr_t *ip);

View file

@ -269,6 +269,13 @@ extern int ipcomp_v4(const union ipaddr_t *restrict a,
extern int ipcomp_v6(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; 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_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_double(double f1, double f2) _DP_ATTRIBUTE_CONST;
extern int comp_ip(struct range_t *r1, struct range_t *r2); extern int comp_ip(struct range_t *r1, struct range_t *r2);

View file

@ -69,6 +69,7 @@ void set_ipv_functions(int version)
find_lease = find_lease_v4; find_lease = find_lease_v4;
get_range_size = get_range_size_v4; get_range_size = get_range_size_v4;
ipcomp = ipcomp_v4; ipcomp = ipcomp_v4;
leasecomp = leasecomp_v4;
ntop_ipaddr = ntop_ipaddr_v4; ntop_ipaddr = ntop_ipaddr_v4;
parse_ipaddr = parse_ipaddr_v4; parse_ipaddr = parse_ipaddr_v4;
xstrstr = xstrstr_v4; xstrstr = xstrstr_v4;
@ -81,6 +82,7 @@ void set_ipv_functions(int version)
find_lease = find_lease_v6; find_lease = find_lease_v6;
get_range_size = get_range_size_v6; get_range_size = get_range_size_v6;
ipcomp = ipcomp_v6; ipcomp = ipcomp_v6;
leasecomp = leasecomp_v6;
ntop_ipaddr = ntop_ipaddr_v6; ntop_ipaddr = ntop_ipaddr_v6;
parse_ipaddr = parse_ipaddr_v6; parse_ipaddr = parse_ipaddr_v6;
xstrstr = xstrstr_v6; xstrstr = xstrstr_v6;
@ -93,6 +95,7 @@ void set_ipv_functions(int version)
find_lease = find_lease_init; find_lease = find_lease_init;
get_range_size = get_range_size_init; get_range_size = get_range_size_init;
ipcomp = ipcomp_init; ipcomp = ipcomp_init;
leasecomp = leasecomp_init;
ntop_ipaddr = ntop_ipaddr_init; ntop_ipaddr = ntop_ipaddr_init;
parse_ipaddr = parse_ipaddr_init; parse_ipaddr = parse_ipaddr_init;
xstrstr = xstrstr_init; xstrstr = xstrstr_init;

View file

@ -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)); 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. /*! \brief Compare IP address in leases. Suitable for sorting range table.
* \param r1 A range structure. * \param r1 A range structure.
* \param r2 A range structure. * \param r2 A range structure.