From 822ca338045d00eaab7533670680e86ee524dcf7 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Wed, 21 Nov 2012 21:29:58 +0000 Subject: [PATCH] getdata & analyze: fix first_ip and last_ip arithmetics There were too clever tricks done with first and last. Earlier the aim was to minimize '<=' and '>=' by fiddling with the numbers at the time when they were saved. While the program logic seemed to work there were some off by one mistakes, resulting to a count error. Even worse there is not even that many of such comparisons, so that one cannot really even justify added complexity. I really hope this patch is last this kind fix ever needed. Reported-by: Cheer Xiao Signed-off-by: Sami Kerola --- src/analyze.c | 6 ++++-- src/getdata.c | 4 ++-- src/output.c | 46 +++++++++++++++++++++++----------------------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/analyze.c b/src/analyze.c index b7f6baa..cd738ff 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -76,6 +76,8 @@ int do_counting(void) /* rewind */ ; if (l == NULL) l = leases; + /* last_ip + 1 make comparison to small bit quicker as it results to + * be 'smaller than' not 'smaller or equal to' */ r_end = range_p->last_ip + 1; for (; l != NULL && l->ip < r_end; l = l->hh.next) { if (l->ip < range_p->first_ip) { @@ -112,7 +114,7 @@ int do_counting(void) /* Size of range, shared net & all networks */ block_size = - (unsigned int)(range_p->last_ip - range_p->first_ip - 1); + (unsigned int)(range_p->last_ip - range_p->first_ip + 1); if (range_p->shared_net) { range_p->shared_net->available += block_size; } @@ -129,7 +131,7 @@ int do_counting(void) range_p = ranges; for (k = 0; k < num_ranges; k++) { shared_networks->available += - range_p->last_ip - range_p->first_ip - 1; + range_p->last_ip - range_p->first_ip + 1; shared_networks->used += range_p->count; shared_networks->touched += range_p->touched; shared_networks->backups += range_p->backups; diff --git a/src/getdata.c b/src/getdata.c index e6c7746..e10798f 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -358,7 +358,7 @@ void parse_config(int is_include, const char *restrict config_file, range_p = ranges + num_ranges; inet_aton(word, &inp); argument = 0; - range_p->last_ip = ntohl(inp.s_addr) + 1; + range_p->last_ip = ntohl(inp.s_addr); range_p->count = 0; range_p->touched = 0; range_p->backups = 0; @@ -383,7 +383,7 @@ void parse_config(int is_include, const char *restrict config_file, * again */ break; } - range_p->first_ip = ntohl(inp.s_addr) - 1; + range_p->first_ip = ntohl(inp.s_addr); argument = 2; break; case 1: diff --git a/src/output.c b/src/output.c index ecc8428..1eb3a65 100644 --- a/src/output.c +++ b/src/output.c @@ -83,8 +83,8 @@ int output_txt(void) } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { - first.s_addr = ntohl(range_p->first_ip + 1); - last.s_addr = ntohl(range_p->last_ip - 1); + first.s_addr = ntohl(range_p->first_ip); + last.s_addr = ntohl(range_p->last_ip); if (range_p->shared_net) { fprintf(outfile, "%-20s", @@ -97,23 +97,23 @@ int output_txt(void) " - %-16s %5" PRIu32 " %5lu %10.3f %5lu %5lu %9.3f", inet_ntoa(last), - range_p->last_ip - range_p->first_ip - 1, + range_p->last_ip - range_p->first_ip + 1, range_p->count, (float)(100 * range_p->count) / - (range_p->last_ip - range_p->first_ip - 1), + (range_p->last_ip - range_p->first_ip + 1), range_p->touched, range_p->touched + range_p->count, (float)(100 * (range_p->touched + range_p->count)) / (range_p->last_ip - - range_p->first_ip - + range_p->first_ip + 1)); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", range_p->backups, (float)(100 * range_p->backups) / (range_p->last_ip - - range_p->first_ip - 1)); + range_p->first_ip + 1)); } fprintf(outfile, "\n"); range_p++; @@ -242,8 +242,8 @@ int output_xml(void) if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { - first.s_addr = ntohl(range_p->first_ip + 1); - last.s_addr = ntohl(range_p->last_ip - 1); + first.s_addr = ntohl(range_p->first_ip); + last.s_addr = ntohl(range_p->last_ip); fprintf(outfile, "\n"); if (range_p->shared_net) { fprintf(outfile, @@ -259,11 +259,11 @@ int output_xml(void) fprintf(outfile, "- %s\n", inet_ntoa(last)); fprintf(outfile, "\t\n"); fprintf(outfile, "\t%" PRIu32 "\n", - range_p->last_ip - range_p->first_ip - 1); + range_p->last_ip - range_p->first_ip + 1); fprintf(outfile, "\t%lu\n", range_p->count); fprintf(outfile, "\t%lu\n", - range_p->last_ip - range_p->first_ip - 1 - + range_p->last_ip - range_p->first_ip + 1 - range_p->count); range_p++; fprintf(outfile, "\n"); @@ -497,8 +497,8 @@ int output_html(void) } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { - first.s_addr = ntohl(range_p->first_ip + 1); - last.s_addr = ntohl(range_p->last_ip - 1); + first.s_addr = ntohl(range_p->first_ip); + last.s_addr = ntohl(range_p->last_ip); newrow(outfile); if (range_p->shared_net) { output_line(outfile, "td", "calign", @@ -510,12 +510,12 @@ int output_html(void) output_line(outfile, "td", "calign", inet_ntoa(first)); output_line(outfile, "td", "calign", inet_ntoa(last)); output_long(outfile, "td", - range_p->last_ip - range_p->first_ip - 1); + range_p->last_ip - range_p->first_ip + 1); output_long(outfile, "td", range_p->count); output_float(outfile, "td", (float)(100 * range_p->count) / (range_p->last_ip - - range_p->first_ip - 1)); + range_p->first_ip + 1)); output_long(outfile, "td", range_p->touched); output_long(outfile, "td", range_p->touched + range_p->count); @@ -524,14 +524,14 @@ int output_html(void) (range_p->touched + range_p->count)) / (range_p->last_ip - - range_p->first_ip - 1)); + range_p->first_ip + 1)); if (0 < num_backups) { output_long(outfile, "td", range_p->backups); output_float(outfile, "td", (float)(100 * range_p->backups) / (range_p->last_ip - - range_p->first_ip - 1)); + range_p->first_ip + 1)); } endrow(outfile); range_p++; @@ -675,8 +675,8 @@ int output_csv(void) } if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { - first.s_addr = ntohl(range_p->first_ip + 1); - last.s_addr = ntohl(range_p->last_ip - 1); + first.s_addr = ntohl(range_p->first_ip); + last.s_addr = ntohl(range_p->last_ip); if (range_p->shared_net) { fprintf(outfile, "\"%s\",", range_p->shared_net->name); @@ -688,23 +688,23 @@ int output_csv(void) "\"%s\",\"%" PRIu32 "\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", inet_ntoa(last), - range_p->last_ip - range_p->first_ip - 1, + range_p->last_ip - range_p->first_ip + 1, range_p->count, (float)(100 * range_p->count) / - (range_p->last_ip - range_p->first_ip - 1), + (range_p->last_ip - range_p->first_ip + 1), range_p->touched, range_p->touched + range_p->count, (float)(100 * (range_p->touched + range_p->count)) / (range_p->last_ip - - range_p->first_ip - + range_p->first_ip + 1)); if (0 < num_backups) { fprintf(outfile, ",\"%lu\",\"%.3f\"", range_p->backups, (float)(100 * range_p->backups) / (range_p->last_ip - - range_p->first_ip - 1)); + range_p->first_ip + 1)); } fprintf(outfile, "\n"); @@ -818,7 +818,7 @@ int output_alarming(void) if (config.output_limit[1] & output_limit_bit_1) { for (i = 0; i < num_ranges; i++) { perc = (float)(100 * range_p->count) / - (range_p->last_ip - range_p->first_ip - 1); + (range_p->last_ip - range_p->first_ip + 1); if (config.critical < perc) rc++; else if (config.warning < perc)