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 <xiaqqaix@gmail.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2012-11-21 21:29:58 +00:00
parent 83e12457bc
commit 822ca33804
3 changed files with 29 additions and 27 deletions

View file

@ -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;

View file

@ -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:

View file

@ -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, "<subnet>\n");
if (range_p->shared_net) {
fprintf(outfile,
@ -259,11 +259,11 @@ int output_xml(void)
fprintf(outfile, "- %s</range>\n", inet_ntoa(last));
fprintf(outfile, "\t<gateway></gateway>\n");
fprintf(outfile, "\t<defined>%" PRIu32 "</defined>\n",
range_p->last_ip - range_p->first_ip - 1);
range_p->last_ip - range_p->first_ip + 1);
fprintf(outfile, "\t<used>%lu</used>\n",
range_p->count);
fprintf(outfile, "\t<free>%lu</free>\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, "</subnet>\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)