AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])
-# Checks for programs.
+AC_GNU_SOURCE
+
+# Checks for programs
AC_PROG_CC
# Checks for libraries.
range_p->shared_net->available += block_size;
}
- /* Reverse so that not even a one IP will be missed. */
+ /* Go backwards one step so that not even a one IP will be
+ * missed. This is possibly always unnecessary. */
if (i) {
i--;
}
range_p++;
}
- /* During count of other shared networks default network and
- * all networks got mixed to gether semantically. This fixes
- * the problem, but is not elegant. TODO: fix semantics of all
- * and default share_network. */
+ /* FIXME: During count of other shared networks default network and
+ * all networks got mixed to gether semantically. This fixes the
+ * problem, but is not elegant. */
shared_networks->available = 0;
shared_networks->used = 0;
shared_networks->touched = 0;
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else /* Not STDC_HEADERS */
-extern void exit();
extern char *malloc();
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
#endif
#include <getopt.h>
#include <errno.h>
+#include <err.h>
#include "dhcpd-pools.h"
#include "defaults.h"
int main(int argc, char **argv)
{
- int c, sorts = 0;
+ int i, c, sorts = 0;
int option_index = 0;
char *tmp;
struct range_t *tmp_ranges;
{0, 0, 0, 0}
};
- program_name = argv[0];
- atexit(clean_up);
-
- /* TODO: make either dynamic or find out max path lenght that auto config
- * provides */
+ /* FIXME: make these allocations dynamic up on need. */
config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
config.output_file = safe_malloc(sizeof(char) * MAXLEN);
- /* Make sure string has zero lenght if there is no command line
- * option */
+ /* Make sure string has zero lenght if there is no
+ * command line option */
config.output_file[0] = '\0';
/* File location defaults */
break;
case 'c':
/* config file */
- if (optarg != NULL) {
- strncpy(config.dhcpdconf_file, optarg,
- (size_t) MAXLEN - 1);
- } else {
- eprintf
- ("main: for argument configuration file parameter not set");
- usage(EXIT_FAILURE);
- }
+ strncpy(config.dhcpdconf_file, optarg,
+ (size_t) MAXLEN - 1);
break;
case 'l':
/* lease file */
- if (optarg != NULL) {
- strncpy(config.dhcpdlease_file, optarg,
- (size_t) MAXLEN - 1);
- } else {
- eprintf
- ("main: for argument lease file parameter not set");
- usage(EXIT_FAILURE);
- }
+ strncpy(config.dhcpdlease_file, optarg,
+ (size_t) MAXLEN - 1);
break;
case 'f':
/* Output format */
- if (optarg != NULL) {
- strncpy(config.output_format, optarg,
- (size_t) 1);
- } else {
- eprintf
- ("main: for argument output format parameter not set");
- usage(EXIT_FAILURE);
- }
+ strncpy(config.output_format, optarg, (size_t) 1);
break;
case 's':
/* Output sorting option */
- if (optarg != NULL) {
- sorts = strlen(optarg);
- if (5 < sorts) {
- eprintf
- ("main: only 5 first sort orders will be used");
- strncpy(config.sort, optarg,
- (size_t) 5);
- sorts = 5;
- } else {
- strncpy(config.sort, optarg,
- (size_t) sorts);
- }
+ sorts = strlen(optarg);
+ if (5 < sorts) {
+ warn("main: only first 5 sort orders will be used");
+ strncpy(config.sort, optarg, (size_t) 5);
+ sorts = 5;
} else {
- eprintf
- ("main: for argument sort order parameter not set");
- usage(EXIT_FAILURE);
+ strncpy(config.sort, optarg,
+ (size_t) sorts);
}
break;
case 'r':
break;
case 'o':
/* Output file */
- if (optarg != NULL) {
- strncpy(config.output_file, optarg,
- (size_t) MAXLEN - 1);
- } else {
- eprintf
- ("main: for argument output file parameter not set");
- usage(EXIT_FAILURE);
- }
+ strncpy(config.output_file, optarg,
+ (size_t) MAXLEN - 1);
break;
case 'L':
/* Specification what will be printed */
- if (optarg != NULL) {
- if (optarg[0] >= '0' && optarg[0] < '8') {
- config.output_limit[0] =
- (int) optarg[0] - '0';
+ for (i = 0; i < 2; i++) {
+ if (optarg[i] >= '0' && optarg[i] < '8') {
+ config.output_limit[i] =
+ (int) optarg[i] - '0';
} else {
- eprintf
- ("main: output mask %s illegal",
- argv[optind]);
- usage(EXIT_FAILURE);
- }
- if (optarg[1] >= '0' && optarg[1] < '8') {
- config.output_limit[1] =
- (int) optarg[1] - '0';
- } else {
- eprintf
- ("main: output mask %s illegal",
+ errx(EXIT_FAILURE,
+ "main: output mask `%s' is illegal",
optarg);
- usage(EXIT_FAILURE);
}
- } else {
- eprintf
- ("main: for argument output mask parameter not set");
- usage(EXIT_FAILURE);
}
break;
case 'v':
/* Print version */
print_version();
- exit(EXIT_SUCCESS);
+ return (EXIT_SUCCESS);
case 'h':
/* Print help */
usage(EXIT_SUCCESS);
default:
- usage(EXIT_FAILURE);
+ errx(EXIT_FAILURE,
+ "Try `%s --help' for more information.",
+ program_invocation_short_name);
}
}
output_analysis = output_txt;
break;
default:
- eprintf("main: unknown ouput format");
- usage(EXIT_FAILURE);
+ errx(EXIT_FAILURE, "main: unknown ouput format `%c'",
+ config.output_format[0]);
}
/* Do the job */
printf("</dhcpstatus>\n");
};
- exit(EXIT_SUCCESS);
+ clean_up();
+ return (EXIT_SUCCESS);
}
/* Global allocations, counter resets etc */
unsigned long int backups;
};
/* Global variables */
-static int true = 1;
-static int false = 0;
+static int const true = 1;
+static int const false = 0;
-char *program_name;
struct configuration_t config;
-static int output_limit_bit_1 = 1;
-static int output_limit_bit_2 = 2;
-static int output_limit_bit_3 = 4;
+static int const output_limit_bit_1 = 1;
+static int const output_limit_bit_2 = 2;
+static int const output_limit_bit_3 = 4;
unsigned int fullhtml;
struct shared_network_t *shared_networks;
int do_counting (void);
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges);
/* General support functions */
-void *safe_malloc (size_t size);
-void eprintf (char *, ...);
+void *safe_malloc (const size_t size);
void print_version (void);
void usage (int status);
/* qsort required functions... */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else /* Not STDC_HEADERS */
-extern void exit();
extern char *malloc();
#define EXIT_FAILURE 1 /* Failing exit status. */
#define EXIT_SUCCESS 0 /* Successful exit status. */
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
+#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
+#endif
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include <ctype.h>
+#include <assert.h>
#include "dhcpd-pools.h"
#include "defaults.h"
-/* Parse dhcpd.leases file. All performance boosts for this
- * function are wellcome */
+/* Parse dhcpd.leases file. All performance boosts for this function are
+ * wellcome */
int parse_leases(void)
{
FILE *dhcpd_leases;
dhcpd_leases = fopen(config.dhcpdlease_file, "r");
if (dhcpd_leases == NULL) {
- eprintf("parse_leases: %s:", config.dhcpdlease_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_leases: %s",
+ config.dhcpdlease_file);
}
#ifdef POSIX_FADV_NOREUSE
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_NOREUSE);
if (errno) {
- eprintf("parse_leases: fadvise:");
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_leases: fadvise noreuse");
}
#endif /* POSIX_FADV_NOREUSE */
#ifdef POSIX_FADV_SEQUENTIAL
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
if (errno) {
- eprintf("parse_leases: fadvise:");
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_leases: fadvise sequential");
}
#endif /* POSIX_FADV_SEQUENTIAL */
+
/* I found out that there's one lease address per 300 bytes in
- * dhcpd.leases file. Malloc is little bit pessimistic and uses
- * 250. If someone has higher density in lease file I'm
- * interested to hear about that. */
+ * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
+ * If someone has higher density in lease file I'm interested to
+ * hear about that. */
if (stat(config.dhcpdlease_file, &lease_file_stats)) {
- eprintf("parse_leases: %s:", config.dhcpdlease_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_leases: %s",
+ config.dhcpdlease_file);
}
leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
else if (strstr(line, "binding state active")) {
leases[num_leases] = htonl(inp.s_addr);
num_leases++;
- if (leasesmallocsize < num_leases) {
- errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug");
- }
+ assert(!(leasesmallocsize < num_leases));
sw_active_lease = 1;
} else if (strstr(line, " binding state free")) {
touches[num_touches] = htonl(inp.s_addr);
num_touches++;
- if (touchesmallocsize < num_touches) {
- errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug");
- }
+ assert(!(touchesmallocsize < num_touches));
} else if (strstr(line, " binding state backup")) {
if (num_backups == 0) {
backups =
}
backups[num_backups] = htonl(inp.s_addr);
num_backups++;
- if (backupsmallocsize < num_backups) {
- errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug");
- }
+ assert(!(backupsmallocsize < num_backups));
}
- /* FIXME: move to output.c and use FILE *outfile */
+ /* FIXME: move to output.c and use the FILE
+ * *outfile */
if ((config.output_format[0] == 'X')
&& (sw_active_lease == 1)
&& (strstr(line, "hardware ethernet"))) {
nth_field(3, macstring, line);
macstring[strlen(macstring) - 1] = '\0';
- printf
+ printf
("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
ipstring, macstring);
}
return 0;
}
-/* Like strcpy but for field which is separated by white spaces.
- * Number of first field is 1 and not 0 like C programs should
- * have. Question of semantics, send mail to author if this
- * annoys. All performance boosts for this function are well
- * come. */
+/* Like strcpy but for field which is separated by white spaces. Number of
+ * first field is 1 and not 0 like C programs should have. Question of
+ * semantics, send mail to author if this annoys. All performance boosts for
+ * this function are well come. */
int nth_field(int n, char *dest, const char *src)
{
int i, j = 0, wordn = 0, len;
}
}
-/* TODO: This spagetti monster function need to be rewrote at
- * least ones. */
+/* FIXME: This spagetti monster function need to be rewrote at least ones. */
char *parse_config(int is_include, char *config_file,
char *current_shared_name,
char *next_free_shared_name,
/* Open configuration file */
dhcpd_config = fopen(config_file, "r");
if (dhcpd_config == NULL) {
- eprintf("parse_config: %s:", config_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_config: %s", config_file);
}
#ifdef POSIX_FADV_NOREUSE
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_NOREUSE);
if (errno) {
- eprintf("parse_config: fadvise:");
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_config: fadvise noreuse");
}
#endif /* POSIX_FADV_NOREUSE */
#ifdef POSIX_FADV_SEQUENTIAL
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
if (errno) {
- eprintf("parse_config: fadvise:");
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "parse_config: fadvise sequential");
}
#endif /* POSIX_FADV_SEQUENTIAL */
+
/* Very hairy stuff begins. */
while (!feof(dhcpd_config)) {
c = fgetc(dhcpd_config);
}
continue;
case '\n':
- /* New line resets comment section, but not if quoted */
+ /* New line resets comment section, but
+ * not if quoted */
if (quote == false) {
comment = false;
}
newclause = true;
i = 0;
} else if (argument == 2) {
- /* Range ends to ; and this hair in code make two
- * ranges wrote to gether like...
+ /* Range ends to ; and this hair in code
+ * make two ranges wrote to gether like...
*
* range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
- *
+ *
* ...to be interpreted correctly. */
c = ' ';
}
if (braces_shared == braces) {
current_shared_name =
shared_net_names;
- /* TODO: Using 1000 is lame, but works. */
+ /* FIXME: Using 1000 is lame, but
+ * works. */
braces_shared = 1000;
shared_p = shared_networks;
}
- /* Not literally true, but works for this program */
+ /* Not literally true, but works for this
+ * program */
newclause = true;
}
continue;
&& (!isspace(c) || quote == true)) {
word[i] = c;
i++;
- /* Long word which is almost causing overflow. Not any of words
- * this program is looking for are this long. */
+ /* Long word which is almost causing overflow. None
+ * of words are this long which the program is
+ * searching. */
if (MAXLEN < i) {
newclause = false;
i = 0;
range_p->shared_net = shared_p;
num_ranges++;
if (RANGES < num_ranges) {
- eprintf
- ("parse_config: Range space full! Increase RANGES and recompile.");
- exit(EXIT_FAILURE);
+ errx(EXIT_FAILURE,
+ "parse_config: Range space full! Increase RANGES and recompile.");
}
newclause = true;
break;
/* printf ("range 1nd ip: %s\n", word); */
range_p = ranges + num_ranges;
if (!(inet_aton(word, &inp))) {
- /* word was not ip, try again */
+ /* word was not ip, try
+ * again */
break;
}
range_p->first_ip = htonl(inp.s_addr) - 1;
shared_p->used = 0;
shared_p->touched = 0;
shared_p->backups = 0;
- /* Temporary abuse of argument variable */
+ /* Temporary abuse of argument
+ * variable */
argument =
strlen(next_free_shared_name) + 1;
- if (last_shared_name >
- next_free_shared_name + argument) {
+ if (next_free_shared_name + argument <
+ last_shared_name) {
next_free_shared_name += argument;
} else {
- /* TODO: make this go away by reallocationg more space. */
- eprintf
- ("parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
- exit(EXIT_FAILURE);
+ /* FIXME: make this go
+ * away by reallocationg
+ * more space. */
+ errx(EXIT_FAILURE,
+ "parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
}
argument = 0;
braces_shared = braces;
argument = 0;
break;
default:
- eprintf
- ("parse_config: This cannot happen, report a bug!");
- exit(EXIT_FAILURE);
+ warnx("impossible occurred, report a bug");
+ assert(0);
}
}
}
extern char *malloc();
#endif /* STDC_HEADERS */
#include <errno.h>
+#include <err.h>
#include <stdarg.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
/* Simple memory allocation wrapper */
-void *safe_malloc(size_t size)
+void *safe_malloc(const size_t size)
{
void *ret = malloc(size);
+
if (ret == NULL) {
- eprintf("safe_malloc: malloc: ");
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE,
+ "safe_malloc: cannot allocate %lu bytes: ", size);
}
- return ret;
-}
-
-/* Copyright (C) 1999 Lucent Technologies
- * Excerpted from 'The Practice of Programming'
- * by Brian W. Kernighan and Rob Pike
- * slight modifications by Sami Kerola.
- * eprintf: print error message and exit */
-void eprintf(char *fmt, ...)
-{
- va_list args;
- fflush(stdout);
- fprintf(stderr, "%s: ", program_name);
-
- va_start(args, fmt);
- vfprintf(stderr, fmt, args);
- va_end(args);
-
- if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
- fprintf(stderr, " %s", strerror(errno));
- /* Should be safe, after all dhcpd-pools has only one
- * thread. */
- errno = 0;
-
- fprintf(stderr, "\n");
- fflush(stderr);
+ return ret;
}
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
}
-
/* Free memory, flush buffers etc */
void clean_up(void)
{
int ret;
if (errno) {
- eprintf
- ("clean_up: errno (%d) set but not checked in correct place; if this is repeatable send strace output as a bug report:",
- errno);
+ warn("clean_up: errno (%d) set but not checked in correct place.\nif this is repeatable send strace output as a bug report", errno);
}
/* Just in case there something in buffers */
ret = fflush(stdout);
if (errno || ret) {
- eprintf("clean_up: stdout:");
+ warn("clean_up: stdout");
}
ret = fflush(stderr);
if (errno || ret) {
- eprintf("clean_up: stderr:");
+ warn("clean_up: stderr");
}
free(config.dhcpdconf_file);
free(config.dhcpdlease_file);
out = status != 0 ? stderr : stdout;
fprintf(out, "\
-Usage: %s [OPTIONS]\n", program_name);
+Usage: %s [OPTIONS]\n", program_invocation_short_name);
fprintf(out, "\
This is ISC dhcpd pools usage analyzer.\n\
\n");
x for xml\n\
X for xml with active lease details\n\
c for comma separated values\n");
-/* TODO
- s for snmp\n");
- */
fprintf(out, "\
-s --sort [nimcptTe] sort ranges by\n\
n name\n\
Report bugs to <%s>\n\
Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
- exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <err.h>
#include "dhcpd-pools.h"
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
- eprintf("output_txt: %s:", config.output_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "output_txt: %s",
+ config.output_file);
}
} else {
outfile = stdout;
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
- eprintf("output_txt: fflush:");
+ warn("output_txt: fflush");
}
} else {
ret = fclose(outfile);
if (ret) {
- eprintf("output_txt: fclose:");
+ warn("output_txt: fclose");
}
}
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
- eprintf("output_xml: %s:", config.output_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "output_xml: %s",
+ config.output_file);
}
} else {
outfile = stdout;
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
- eprintf("output_xml: fflush:");
+ warn("output_xml: fflush");
}
} else {
ret = fclose(outfile);
if (ret) {
- eprintf("output_xml: fclose:");
+ warn("output_xml: fclose");
}
}
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
- eprintf("output_html: %s:", config.output_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "output_html: %s",
+ config.output_file);
}
} else {
outfile = stdout;
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
- eprintf("output_html: fflush:");
+ warn("output_html: fflush");
}
} else {
ret = fclose(outfile);
if (ret) {
- eprintf("output_html: fclose:");
+ warn("output_html: fclose");
}
}
return 0;
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
- eprintf("output_csv: %s:", config.output_file);
- exit(EXIT_FAILURE);
+ err(EXIT_FAILURE, "output_csv: %s",
+ config.output_file);
}
} else {
outfile = stdout;
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
- eprintf("output_cvs: fflush:");
+ warn("output_cvs: fflush");
}
} else {
ret = fclose(outfile);
if (ret) {
- eprintf("output_cvs: fclose:");
+ warn("output_cvs: fclose");
}
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <err.h>
#include "dhcpd-pools.h"
#include "defaults.h"
returner = ret_tcperc;
break;
default:
- eprintf("field_selector: unknown sort order: %c",
- config.sort[0]);
- usage(EXIT_FAILURE);
+ errx(EXIT_FAILURE,
+ "field_selector: unknown sort order `%c'",
+ config.sort[0]);
}
}