mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-16 15:57:00 +00:00
Use what libc will provide
The eprintf removed and replaced with err & warn. Option parsing no longer tries to find missing optargs, which are getopts should notice. Few complier warnings got to be removed as well. Finally the commments will no longer exceed standard terminal width. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
775157b1a5
commit
849c8f5e73
8 changed files with 136 additions and 214 deletions
|
|
@ -8,7 +8,9 @@ AM_INIT_AUTOMAKE(dhcpd-pools, 2.13)
|
||||||
AC_CONFIG_SRCDIR([config.h.in])
|
AC_CONFIG_SRCDIR([config.h.in])
|
||||||
AC_CONFIG_HEADERS([config.h])
|
AC_CONFIG_HEADERS([config.h])
|
||||||
|
|
||||||
# Checks for programs.
|
AC_GNU_SOURCE
|
||||||
|
|
||||||
|
# Checks for programs
|
||||||
AC_PROG_CC
|
AC_PROG_CC
|
||||||
|
|
||||||
# Checks for libraries.
|
# Checks for libraries.
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,8 @@ int do_counting(void)
|
||||||
range_p->shared_net->available += block_size;
|
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) {
|
if (i) {
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
|
@ -140,10 +141,9 @@ int do_counting(void)
|
||||||
range_p++;
|
range_p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* During count of other shared networks default network and
|
/* FIXME: During count of other shared networks default network and
|
||||||
* all networks got mixed to gether semantically. This fixes
|
* all networks got mixed to gether semantically. This fixes the
|
||||||
* the problem, but is not elegant. TODO: fix semantics of all
|
* problem, but is not elegant. */
|
||||||
* and default share_network. */
|
|
||||||
shared_networks->available = 0;
|
shared_networks->available = 0;
|
||||||
shared_networks->used = 0;
|
shared_networks->used = 0;
|
||||||
shared_networks->touched = 0;
|
shared_networks->touched = 0;
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@
|
||||||
#ifdef HAVE_STDLIB_H
|
#ifdef HAVE_STDLIB_H
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#else /* Not STDC_HEADERS */
|
#else /* Not STDC_HEADERS */
|
||||||
extern void exit();
|
|
||||||
extern char *malloc();
|
extern char *malloc();
|
||||||
#endif /* STDC_HEADERS */
|
#endif /* STDC_HEADERS */
|
||||||
#ifdef HAVE_STRING_H
|
#ifdef HAVE_STRING_H
|
||||||
|
|
@ -37,13 +36,14 @@ extern char *malloc();
|
||||||
#endif
|
#endif
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
#include "dhcpd-pools.h"
|
#include "dhcpd-pools.h"
|
||||||
#include "defaults.h"
|
#include "defaults.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c, sorts = 0;
|
int i, c, sorts = 0;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
struct range_t *tmp_ranges;
|
struct range_t *tmp_ranges;
|
||||||
|
|
@ -62,17 +62,13 @@ int main(int argc, char **argv)
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
program_name = argv[0];
|
/* FIXME: make these allocations dynamic up on need. */
|
||||||
atexit(clean_up);
|
|
||||||
|
|
||||||
/* TODO: make either dynamic or find out max path lenght that auto config
|
|
||||||
* provides */
|
|
||||||
config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
|
config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
|
||||||
config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
|
config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
|
||||||
config.output_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
|
/* Make sure string has zero lenght if there is no
|
||||||
* option */
|
* command line option */
|
||||||
config.output_file[0] = '\0';
|
config.output_file[0] = '\0';
|
||||||
|
|
||||||
/* File location defaults */
|
/* File location defaults */
|
||||||
|
|
@ -105,55 +101,28 @@ int main(int argc, char **argv)
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
/* config file */
|
/* config file */
|
||||||
if (optarg != NULL) {
|
strncpy(config.dhcpdconf_file, optarg,
|
||||||
strncpy(config.dhcpdconf_file, optarg,
|
(size_t) MAXLEN - 1);
|
||||||
(size_t) MAXLEN - 1);
|
|
||||||
} else {
|
|
||||||
eprintf
|
|
||||||
("main: for argument configuration file parameter not set");
|
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
/* lease file */
|
/* lease file */
|
||||||
if (optarg != NULL) {
|
strncpy(config.dhcpdlease_file, optarg,
|
||||||
strncpy(config.dhcpdlease_file, optarg,
|
(size_t) MAXLEN - 1);
|
||||||
(size_t) MAXLEN - 1);
|
|
||||||
} else {
|
|
||||||
eprintf
|
|
||||||
("main: for argument lease file parameter not set");
|
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
/* Output format */
|
/* Output format */
|
||||||
if (optarg != NULL) {
|
strncpy(config.output_format, optarg, (size_t) 1);
|
||||||
strncpy(config.output_format, optarg,
|
|
||||||
(size_t) 1);
|
|
||||||
} else {
|
|
||||||
eprintf
|
|
||||||
("main: for argument output format parameter not set");
|
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
/* Output sorting option */
|
/* Output sorting option */
|
||||||
if (optarg != NULL) {
|
sorts = strlen(optarg);
|
||||||
sorts = strlen(optarg);
|
if (5 < sorts) {
|
||||||
if (5 < sorts) {
|
warn("main: only first 5 sort orders will be used");
|
||||||
eprintf
|
strncpy(config.sort, optarg, (size_t) 5);
|
||||||
("main: only 5 first sort orders will be used");
|
sorts = 5;
|
||||||
strncpy(config.sort, optarg,
|
|
||||||
(size_t) 5);
|
|
||||||
sorts = 5;
|
|
||||||
} else {
|
|
||||||
strncpy(config.sort, optarg,
|
|
||||||
(size_t) sorts);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
eprintf
|
strncpy(config.sort, optarg,
|
||||||
("main: for argument sort order parameter not set");
|
(size_t) sorts);
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
|
|
@ -162,51 +131,33 @@ int main(int argc, char **argv)
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
/* Output file */
|
/* Output file */
|
||||||
if (optarg != NULL) {
|
strncpy(config.output_file, optarg,
|
||||||
strncpy(config.output_file, optarg,
|
(size_t) MAXLEN - 1);
|
||||||
(size_t) MAXLEN - 1);
|
|
||||||
} else {
|
|
||||||
eprintf
|
|
||||||
("main: for argument output file parameter not set");
|
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'L':
|
||||||
/* Specification what will be printed */
|
/* Specification what will be printed */
|
||||||
if (optarg != NULL) {
|
for (i = 0; i < 2; i++) {
|
||||||
if (optarg[0] >= '0' && optarg[0] < '8') {
|
if (optarg[i] >= '0' && optarg[i] < '8') {
|
||||||
config.output_limit[0] =
|
config.output_limit[i] =
|
||||||
(int) optarg[0] - '0';
|
(int) optarg[i] - '0';
|
||||||
} else {
|
} else {
|
||||||
eprintf
|
errx(EXIT_FAILURE,
|
||||||
("main: output mask %s illegal",
|
"main: output mask `%s' is 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",
|
|
||||||
optarg);
|
optarg);
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
eprintf
|
|
||||||
("main: for argument output mask parameter not set");
|
|
||||||
usage(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
/* Print version */
|
/* Print version */
|
||||||
print_version();
|
print_version();
|
||||||
exit(EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
case 'h':
|
case 'h':
|
||||||
/* Print help */
|
/* Print help */
|
||||||
usage(EXIT_SUCCESS);
|
usage(EXIT_SUCCESS);
|
||||||
default:
|
default:
|
||||||
usage(EXIT_FAILURE);
|
errx(EXIT_FAILURE,
|
||||||
|
"Try `%s --help' for more information.",
|
||||||
|
program_invocation_short_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,8 +187,8 @@ int main(int argc, char **argv)
|
||||||
output_analysis = output_txt;
|
output_analysis = output_txt;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
eprintf("main: unknown ouput format");
|
errx(EXIT_FAILURE, "main: unknown ouput format `%c'",
|
||||||
usage(EXIT_FAILURE);
|
config.output_format[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do the job */
|
/* Do the job */
|
||||||
|
|
@ -278,7 +229,8 @@ int main(int argc, char **argv)
|
||||||
printf("</dhcpstatus>\n");
|
printf("</dhcpstatus>\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
clean_up();
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Global allocations, counter resets etc */
|
/* Global allocations, counter resets etc */
|
||||||
|
|
|
||||||
|
|
@ -61,15 +61,14 @@ struct range_t
|
||||||
unsigned long int backups;
|
unsigned long int backups;
|
||||||
};
|
};
|
||||||
/* Global variables */
|
/* Global variables */
|
||||||
static int true = 1;
|
static int const true = 1;
|
||||||
static int false = 0;
|
static int const false = 0;
|
||||||
|
|
||||||
char *program_name;
|
|
||||||
struct configuration_t config;
|
struct configuration_t config;
|
||||||
|
|
||||||
static int output_limit_bit_1 = 1;
|
static int const output_limit_bit_1 = 1;
|
||||||
static int output_limit_bit_2 = 2;
|
static int const output_limit_bit_2 = 2;
|
||||||
static int output_limit_bit_3 = 4;
|
static int const output_limit_bit_3 = 4;
|
||||||
unsigned int fullhtml;
|
unsigned int fullhtml;
|
||||||
|
|
||||||
struct shared_network_t *shared_networks;
|
struct shared_network_t *shared_networks;
|
||||||
|
|
@ -97,8 +96,7 @@ int prepare_data (void);
|
||||||
int do_counting (void);
|
int do_counting (void);
|
||||||
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges);
|
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges);
|
||||||
/* General support functions */
|
/* General support functions */
|
||||||
void *safe_malloc (size_t size);
|
void *safe_malloc (const size_t size);
|
||||||
void eprintf (char *, ...);
|
|
||||||
void print_version (void);
|
void print_version (void);
|
||||||
void usage (int status);
|
void usage (int status);
|
||||||
/* qsort required functions... */
|
/* qsort required functions... */
|
||||||
|
|
|
||||||
117
src/getdata.c
117
src/getdata.c
|
|
@ -27,7 +27,6 @@
|
||||||
#ifdef HAVE_STDLIB_H
|
#ifdef HAVE_STDLIB_H
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#else /* Not STDC_HEADERS */
|
#else /* Not STDC_HEADERS */
|
||||||
extern void exit();
|
|
||||||
extern char *malloc();
|
extern char *malloc();
|
||||||
#define EXIT_FAILURE 1 /* Failing exit status. */
|
#define EXIT_FAILURE 1 /* Failing exit status. */
|
||||||
#define EXIT_SUCCESS 0 /* Successful exit status. */
|
#define EXIT_SUCCESS 0 /* Successful exit status. */
|
||||||
|
|
@ -45,17 +44,20 @@ extern char *malloc();
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#ifndef _XOPEN_SOURCE
|
||||||
#define _XOPEN_SOURCE 600
|
#define _XOPEN_SOURCE 600
|
||||||
|
#endif
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "dhcpd-pools.h"
|
#include "dhcpd-pools.h"
|
||||||
#include "defaults.h"
|
#include "defaults.h"
|
||||||
|
|
||||||
/* Parse dhcpd.leases file. All performance boosts for this
|
/* Parse dhcpd.leases file. All performance boosts for this function are
|
||||||
* function are wellcome */
|
* wellcome */
|
||||||
int parse_leases(void)
|
int parse_leases(void)
|
||||||
{
|
{
|
||||||
FILE *dhcpd_leases;
|
FILE *dhcpd_leases;
|
||||||
|
|
@ -71,30 +73,29 @@ int parse_leases(void)
|
||||||
|
|
||||||
dhcpd_leases = fopen(config.dhcpdlease_file, "r");
|
dhcpd_leases = fopen(config.dhcpdlease_file, "r");
|
||||||
if (dhcpd_leases == NULL) {
|
if (dhcpd_leases == NULL) {
|
||||||
eprintf("parse_leases: %s:", config.dhcpdlease_file);
|
err(EXIT_FAILURE, "parse_leases: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.dhcpdlease_file);
|
||||||
}
|
}
|
||||||
#ifdef POSIX_FADV_NOREUSE
|
#ifdef POSIX_FADV_NOREUSE
|
||||||
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_NOREUSE);
|
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_NOREUSE);
|
||||||
if (errno) {
|
if (errno) {
|
||||||
eprintf("parse_leases: fadvise:");
|
err(EXIT_FAILURE, "parse_leases: fadvise noreuse");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
#endif /* POSIX_FADV_NOREUSE */
|
#endif /* POSIX_FADV_NOREUSE */
|
||||||
#ifdef POSIX_FADV_SEQUENTIAL
|
#ifdef POSIX_FADV_SEQUENTIAL
|
||||||
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
|
posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
|
||||||
if (errno) {
|
if (errno) {
|
||||||
eprintf("parse_leases: fadvise:");
|
err(EXIT_FAILURE, "parse_leases: fadvise sequential");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
#endif /* POSIX_FADV_SEQUENTIAL */
|
#endif /* POSIX_FADV_SEQUENTIAL */
|
||||||
|
|
||||||
/* I found out that there's one lease address per 300 bytes in
|
/* I found out that there's one lease address per 300 bytes in
|
||||||
* dhcpd.leases file. Malloc is little bit pessimistic and uses
|
* dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
|
||||||
* 250. If someone has higher density in lease file I'm
|
* If someone has higher density in lease file I'm interested to
|
||||||
* interested to hear about that. */
|
* hear about that. */
|
||||||
if (stat(config.dhcpdlease_file, &lease_file_stats)) {
|
if (stat(config.dhcpdlease_file, &lease_file_stats)) {
|
||||||
eprintf("parse_leases: %s:", config.dhcpdlease_file);
|
err(EXIT_FAILURE, "parse_leases: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.dhcpdlease_file);
|
||||||
}
|
}
|
||||||
leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
|
leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
|
||||||
touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
|
touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
|
||||||
|
|
@ -121,16 +122,12 @@ int parse_leases(void)
|
||||||
else if (strstr(line, "binding state active")) {
|
else if (strstr(line, "binding state active")) {
|
||||||
leases[num_leases] = htonl(inp.s_addr);
|
leases[num_leases] = htonl(inp.s_addr);
|
||||||
num_leases++;
|
num_leases++;
|
||||||
if (leasesmallocsize < num_leases) {
|
assert(!(leasesmallocsize < num_leases));
|
||||||
errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug");
|
|
||||||
}
|
|
||||||
sw_active_lease = 1;
|
sw_active_lease = 1;
|
||||||
} else if (strstr(line, " binding state free")) {
|
} else if (strstr(line, " binding state free")) {
|
||||||
touches[num_touches] = htonl(inp.s_addr);
|
touches[num_touches] = htonl(inp.s_addr);
|
||||||
num_touches++;
|
num_touches++;
|
||||||
if (touchesmallocsize < num_touches) {
|
assert(!(touchesmallocsize < num_touches));
|
||||||
errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug");
|
|
||||||
}
|
|
||||||
} else if (strstr(line, " binding state backup")) {
|
} else if (strstr(line, " binding state backup")) {
|
||||||
if (num_backups == 0) {
|
if (num_backups == 0) {
|
||||||
backups =
|
backups =
|
||||||
|
|
@ -139,19 +136,18 @@ int parse_leases(void)
|
||||||
}
|
}
|
||||||
backups[num_backups] = htonl(inp.s_addr);
|
backups[num_backups] = htonl(inp.s_addr);
|
||||||
num_backups++;
|
num_backups++;
|
||||||
if (backupsmallocsize < num_backups) {
|
assert(!(backupsmallocsize < num_backups));
|
||||||
errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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')
|
if ((config.output_format[0] == 'X')
|
||||||
&& (sw_active_lease == 1)
|
&& (sw_active_lease == 1)
|
||||||
&& (strstr(line, "hardware ethernet"))) {
|
&& (strstr(line, "hardware ethernet"))) {
|
||||||
nth_field(3, macstring, line);
|
nth_field(3, macstring, line);
|
||||||
macstring[strlen(macstring) - 1] = '\0';
|
macstring[strlen(macstring) - 1] = '\0';
|
||||||
|
|
||||||
printf
|
printf
|
||||||
("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
|
("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
|
||||||
ipstring, macstring);
|
ipstring, macstring);
|
||||||
}
|
}
|
||||||
|
|
@ -159,11 +155,10 @@ int parse_leases(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Like strcpy but for field which is separated by white spaces.
|
/* Like strcpy but for field which is separated by white spaces. Number of
|
||||||
* Number of first field is 1 and not 0 like C programs should
|
* first field is 1 and not 0 like C programs should have. Question of
|
||||||
* have. Question of semantics, send mail to author if this
|
* semantics, send mail to author if this annoys. All performance boosts for
|
||||||
* annoys. All performance boosts for this function are well
|
* this function are well come. */
|
||||||
* come. */
|
|
||||||
int nth_field(int n, char *dest, const char *src)
|
int nth_field(int n, char *dest, const char *src)
|
||||||
{
|
{
|
||||||
int i, j = 0, wordn = 0, len;
|
int i, j = 0, wordn = 0, len;
|
||||||
|
|
@ -204,8 +199,7 @@ int is_interesting_config_clause(char *s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: This spagetti monster function need to be rewrote at
|
/* FIXME: This spagetti monster function need to be rewrote at least ones. */
|
||||||
* least ones. */
|
|
||||||
char *parse_config(int is_include, char *config_file,
|
char *parse_config(int is_include, char *config_file,
|
||||||
char *current_shared_name,
|
char *current_shared_name,
|
||||||
char *next_free_shared_name,
|
char *next_free_shared_name,
|
||||||
|
|
@ -232,23 +226,21 @@ char *parse_config(int is_include, char *config_file,
|
||||||
/* Open configuration file */
|
/* Open configuration file */
|
||||||
dhcpd_config = fopen(config_file, "r");
|
dhcpd_config = fopen(config_file, "r");
|
||||||
if (dhcpd_config == NULL) {
|
if (dhcpd_config == NULL) {
|
||||||
eprintf("parse_config: %s:", config_file);
|
err(EXIT_FAILURE, "parse_config: %s", config_file);
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
#ifdef POSIX_FADV_NOREUSE
|
#ifdef POSIX_FADV_NOREUSE
|
||||||
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_NOREUSE);
|
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_NOREUSE);
|
||||||
if (errno) {
|
if (errno) {
|
||||||
eprintf("parse_config: fadvise:");
|
err(EXIT_FAILURE, "parse_config: fadvise noreuse");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
#endif /* POSIX_FADV_NOREUSE */
|
#endif /* POSIX_FADV_NOREUSE */
|
||||||
#ifdef POSIX_FADV_SEQUENTIAL
|
#ifdef POSIX_FADV_SEQUENTIAL
|
||||||
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
|
posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
|
||||||
if (errno) {
|
if (errno) {
|
||||||
eprintf("parse_config: fadvise:");
|
err(EXIT_FAILURE, "parse_config: fadvise sequential");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
#endif /* POSIX_FADV_SEQUENTIAL */
|
#endif /* POSIX_FADV_SEQUENTIAL */
|
||||||
|
|
||||||
/* Very hairy stuff begins. */
|
/* Very hairy stuff begins. */
|
||||||
while (!feof(dhcpd_config)) {
|
while (!feof(dhcpd_config)) {
|
||||||
c = fgetc(dhcpd_config);
|
c = fgetc(dhcpd_config);
|
||||||
|
|
@ -268,7 +260,8 @@ char *parse_config(int is_include, char *config_file,
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
case '\n':
|
case '\n':
|
||||||
/* New line resets comment section, but not if quoted */
|
/* New line resets comment section, but
|
||||||
|
* not if quoted */
|
||||||
if (quote == false) {
|
if (quote == false) {
|
||||||
comment = false;
|
comment = false;
|
||||||
}
|
}
|
||||||
|
|
@ -283,11 +276,11 @@ char *parse_config(int is_include, char *config_file,
|
||||||
newclause = true;
|
newclause = true;
|
||||||
i = 0;
|
i = 0;
|
||||||
} else if (argument == 2) {
|
} else if (argument == 2) {
|
||||||
/* Range ends to ; and this hair in code make two
|
/* Range ends to ; and this hair in code
|
||||||
* ranges wrote to gether like...
|
* 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;
|
* range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
|
||||||
*
|
*
|
||||||
* ...to be interpreted correctly. */
|
* ...to be interpreted correctly. */
|
||||||
c = ' ';
|
c = ' ';
|
||||||
}
|
}
|
||||||
|
|
@ -316,11 +309,13 @@ char *parse_config(int is_include, char *config_file,
|
||||||
if (braces_shared == braces) {
|
if (braces_shared == braces) {
|
||||||
current_shared_name =
|
current_shared_name =
|
||||||
shared_net_names;
|
shared_net_names;
|
||||||
/* TODO: Using 1000 is lame, but works. */
|
/* FIXME: Using 1000 is lame, but
|
||||||
|
* works. */
|
||||||
braces_shared = 1000;
|
braces_shared = 1000;
|
||||||
shared_p = shared_networks;
|
shared_p = shared_networks;
|
||||||
}
|
}
|
||||||
/* Not literally true, but works for this program */
|
/* Not literally true, but works for this
|
||||||
|
* program */
|
||||||
newclause = true;
|
newclause = true;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -343,8 +338,9 @@ char *parse_config(int is_include, char *config_file,
|
||||||
&& (!isspace(c) || quote == true)) {
|
&& (!isspace(c) || quote == true)) {
|
||||||
word[i] = c;
|
word[i] = c;
|
||||||
i++;
|
i++;
|
||||||
/* Long word which is almost causing overflow. Not any of words
|
/* Long word which is almost causing overflow. None
|
||||||
* this program is looking for are this long. */
|
* of words are this long which the program is
|
||||||
|
* searching. */
|
||||||
if (MAXLEN < i) {
|
if (MAXLEN < i) {
|
||||||
newclause = false;
|
newclause = false;
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
@ -379,9 +375,8 @@ char *parse_config(int is_include, char *config_file,
|
||||||
range_p->shared_net = shared_p;
|
range_p->shared_net = shared_p;
|
||||||
num_ranges++;
|
num_ranges++;
|
||||||
if (RANGES < num_ranges) {
|
if (RANGES < num_ranges) {
|
||||||
eprintf
|
errx(EXIT_FAILURE,
|
||||||
("parse_config: Range space full! Increase RANGES and recompile.");
|
"parse_config: Range space full! Increase RANGES and recompile.");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
newclause = true;
|
newclause = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -389,7 +384,8 @@ char *parse_config(int is_include, char *config_file,
|
||||||
/* printf ("range 1nd ip: %s\n", word); */
|
/* printf ("range 1nd ip: %s\n", word); */
|
||||||
range_p = ranges + num_ranges;
|
range_p = ranges + num_ranges;
|
||||||
if (!(inet_aton(word, &inp))) {
|
if (!(inet_aton(word, &inp))) {
|
||||||
/* word was not ip, try again */
|
/* word was not ip, try
|
||||||
|
* again */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
range_p->first_ip = htonl(inp.s_addr) - 1;
|
range_p->first_ip = htonl(inp.s_addr) - 1;
|
||||||
|
|
@ -407,17 +403,19 @@ char *parse_config(int is_include, char *config_file,
|
||||||
shared_p->used = 0;
|
shared_p->used = 0;
|
||||||
shared_p->touched = 0;
|
shared_p->touched = 0;
|
||||||
shared_p->backups = 0;
|
shared_p->backups = 0;
|
||||||
/* Temporary abuse of argument variable */
|
/* Temporary abuse of argument
|
||||||
|
* variable */
|
||||||
argument =
|
argument =
|
||||||
strlen(next_free_shared_name) + 1;
|
strlen(next_free_shared_name) + 1;
|
||||||
if (last_shared_name >
|
if (next_free_shared_name + argument <
|
||||||
next_free_shared_name + argument) {
|
last_shared_name) {
|
||||||
next_free_shared_name += argument;
|
next_free_shared_name += argument;
|
||||||
} else {
|
} else {
|
||||||
/* TODO: make this go away by reallocationg more space. */
|
/* FIXME: make this go
|
||||||
eprintf
|
* away by reallocationg
|
||||||
("parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
|
* more space. */
|
||||||
exit(EXIT_FAILURE);
|
errx(EXIT_FAILURE,
|
||||||
|
"parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
|
||||||
}
|
}
|
||||||
argument = 0;
|
argument = 0;
|
||||||
braces_shared = braces;
|
braces_shared = braces;
|
||||||
|
|
@ -437,9 +435,8 @@ char *parse_config(int is_include, char *config_file,
|
||||||
argument = 0;
|
argument = 0;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
eprintf
|
warnx("impossible occurred, report a bug");
|
||||||
("parse_config: This cannot happen, report a bug!");
|
assert(0);
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
51
src/other.c
51
src/other.c
|
|
@ -30,6 +30,7 @@ extern void exit();
|
||||||
extern char *malloc();
|
extern char *malloc();
|
||||||
#endif /* STDC_HEADERS */
|
#endif /* STDC_HEADERS */
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <err.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#ifdef HAVE_STRING_H
|
#ifdef HAVE_STRING_H
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -38,42 +39,18 @@ extern char *malloc();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Simple memory allocation wrapper */
|
/* Simple memory allocation wrapper */
|
||||||
void *safe_malloc(size_t size)
|
void *safe_malloc(const size_t size)
|
||||||
{
|
{
|
||||||
void *ret = malloc(size);
|
void *ret = malloc(size);
|
||||||
|
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
eprintf("safe_malloc: malloc: ");
|
err(EXIT_FAILURE,
|
||||||
exit(EXIT_FAILURE);
|
"safe_malloc: cannot allocate %lu bytes: ", size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
|
void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
|
||||||
{
|
{
|
||||||
unsigned int i = num_ranges - 1, j;
|
unsigned int i = num_ranges - 1, j;
|
||||||
|
|
@ -86,24 +63,21 @@ void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
|
||||||
memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
|
memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Free memory, flush buffers etc */
|
/* Free memory, flush buffers etc */
|
||||||
void clean_up(void)
|
void clean_up(void)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
if (errno) {
|
if (errno) {
|
||||||
eprintf
|
warn("clean_up: errno (%d) set but not checked in correct place.\nif this is repeatable send strace output as a bug report", errno);
|
||||||
("clean_up: errno (%d) set but not checked in correct place; if this is repeatable send strace output as a bug report:",
|
|
||||||
errno);
|
|
||||||
}
|
}
|
||||||
/* Just in case there something in buffers */
|
/* Just in case there something in buffers */
|
||||||
ret = fflush(stdout);
|
ret = fflush(stdout);
|
||||||
if (errno || ret) {
|
if (errno || ret) {
|
||||||
eprintf("clean_up: stdout:");
|
warn("clean_up: stdout");
|
||||||
}
|
}
|
||||||
ret = fflush(stderr);
|
ret = fflush(stderr);
|
||||||
if (errno || ret) {
|
if (errno || ret) {
|
||||||
eprintf("clean_up: stderr:");
|
warn("clean_up: stderr");
|
||||||
}
|
}
|
||||||
free(config.dhcpdconf_file);
|
free(config.dhcpdconf_file);
|
||||||
free(config.dhcpdlease_file);
|
free(config.dhcpdlease_file);
|
||||||
|
|
@ -132,7 +106,7 @@ void usage(int status)
|
||||||
out = status != 0 ? stderr : stdout;
|
out = status != 0 ? stderr : stdout;
|
||||||
|
|
||||||
fprintf(out, "\
|
fprintf(out, "\
|
||||||
Usage: %s [OPTIONS]\n", program_name);
|
Usage: %s [OPTIONS]\n", program_invocation_short_name);
|
||||||
fprintf(out, "\
|
fprintf(out, "\
|
||||||
This is ISC dhcpd pools usage analyzer.\n\
|
This is ISC dhcpd pools usage analyzer.\n\
|
||||||
\n");
|
\n");
|
||||||
|
|
@ -147,9 +121,6 @@ This is ISC dhcpd pools usage analyzer.\n\
|
||||||
x for xml\n\
|
x for xml\n\
|
||||||
X for xml with active lease details\n\
|
X for xml with active lease details\n\
|
||||||
c for comma separated values\n");
|
c for comma separated values\n");
|
||||||
/* TODO
|
|
||||||
s for snmp\n");
|
|
||||||
*/
|
|
||||||
fprintf(out, "\
|
fprintf(out, "\
|
||||||
-s --sort [nimcptTe] sort ranges by\n\
|
-s --sort [nimcptTe] sort ranges by\n\
|
||||||
n name\n\
|
n name\n\
|
||||||
|
|
@ -170,5 +141,5 @@ This is ISC dhcpd pools usage analyzer.\n\
|
||||||
Report bugs to <%s>\n\
|
Report bugs to <%s>\n\
|
||||||
Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
|
Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
|
||||||
|
|
||||||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
33
src/output.c
33
src/output.c
|
|
@ -24,6 +24,7 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
#include "dhcpd-pools.h"
|
#include "dhcpd-pools.h"
|
||||||
|
|
||||||
|
|
@ -39,8 +40,8 @@ int output_txt(void)
|
||||||
if (config.output_file[0]) {
|
if (config.output_file[0]) {
|
||||||
outfile = fopen(config.output_file, "w+");
|
outfile = fopen(config.output_file, "w+");
|
||||||
if (outfile == NULL) {
|
if (outfile == NULL) {
|
||||||
eprintf("output_txt: %s:", config.output_file);
|
err(EXIT_FAILURE, "output_txt: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.output_file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
outfile = stdout;
|
outfile = stdout;
|
||||||
|
|
@ -172,12 +173,12 @@ int output_txt(void)
|
||||||
if (outfile == stdout) {
|
if (outfile == stdout) {
|
||||||
ret = fflush(stdout);
|
ret = fflush(stdout);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_txt: fflush:");
|
warn("output_txt: fflush");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret = fclose(outfile);
|
ret = fclose(outfile);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_txt: fclose:");
|
warn("output_txt: fclose");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,8 +197,8 @@ int output_xml(void)
|
||||||
if (config.output_file[0]) {
|
if (config.output_file[0]) {
|
||||||
outfile = fopen(config.output_file, "w+");
|
outfile = fopen(config.output_file, "w+");
|
||||||
if (outfile == NULL) {
|
if (outfile == NULL) {
|
||||||
eprintf("output_xml: %s:", config.output_file);
|
err(EXIT_FAILURE, "output_xml: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.output_file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
outfile = stdout;
|
outfile = stdout;
|
||||||
|
|
@ -276,12 +277,12 @@ int output_xml(void)
|
||||||
if (outfile == stdout) {
|
if (outfile == stdout) {
|
||||||
ret = fflush(stdout);
|
ret = fflush(stdout);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_xml: fflush:");
|
warn("output_xml: fflush");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret = fclose(outfile);
|
ret = fclose(outfile);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_xml: fclose:");
|
warn("output_xml: fclose");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -425,8 +426,8 @@ int output_html(void)
|
||||||
if (config.output_file[0]) {
|
if (config.output_file[0]) {
|
||||||
outfile = fopen(config.output_file, "w+");
|
outfile = fopen(config.output_file, "w+");
|
||||||
if (outfile == NULL) {
|
if (outfile == NULL) {
|
||||||
eprintf("output_html: %s:", config.output_file);
|
err(EXIT_FAILURE, "output_html: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.output_file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
outfile = stdout;
|
outfile = stdout;
|
||||||
|
|
@ -607,12 +608,12 @@ int output_html(void)
|
||||||
if (outfile == stdout) {
|
if (outfile == stdout) {
|
||||||
ret = fflush(stdout);
|
ret = fflush(stdout);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_html: fflush:");
|
warn("output_html: fflush");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret = fclose(outfile);
|
ret = fclose(outfile);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_html: fclose:");
|
warn("output_html: fclose");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -630,8 +631,8 @@ int output_csv(void)
|
||||||
if (config.output_file[0]) {
|
if (config.output_file[0]) {
|
||||||
outfile = fopen(config.output_file, "w+");
|
outfile = fopen(config.output_file, "w+");
|
||||||
if (outfile == NULL) {
|
if (outfile == NULL) {
|
||||||
eprintf("output_csv: %s:", config.output_file);
|
err(EXIT_FAILURE, "output_csv: %s",
|
||||||
exit(EXIT_FAILURE);
|
config.output_file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
outfile = stdout;
|
outfile = stdout;
|
||||||
|
|
@ -763,13 +764,13 @@ int output_csv(void)
|
||||||
if (outfile == stdout) {
|
if (outfile == stdout) {
|
||||||
ret = fflush(stdout);
|
ret = fflush(stdout);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_cvs: fflush:");
|
warn("output_cvs: fflush");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
ret = fclose(outfile);
|
ret = fclose(outfile);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
eprintf("output_cvs: fclose:");
|
warn("output_cvs: fclose");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
#include "dhcpd-pools.h"
|
#include "dhcpd-pools.h"
|
||||||
#include "defaults.h"
|
#include "defaults.h"
|
||||||
|
|
@ -116,9 +117,9 @@ void field_selector(char c)
|
||||||
returner = ret_tcperc;
|
returner = ret_tcperc;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
eprintf("field_selector: unknown sort order: %c",
|
errx(EXIT_FAILURE,
|
||||||
config.sort[0]);
|
"field_selector: unknown sort order `%c'",
|
||||||
usage(EXIT_FAILURE);
|
config.sort[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue