Import from release candidate 2.13.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-01-21 18:15:50 +01:00
commit 74aef1c34e
38 changed files with 15998 additions and 0 deletions

288
src/dhcpd-pools.c Normal file
View file

@ -0,0 +1,288 @@
/*
** Copyright (C) 2006- Sami Kerola <kerolasa@iki.fi>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else /* Not STDC_HEADERS */
extern void exit();
extern char *malloc();
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <getopt.h>
#include "dhcpd-pools.h"
#include "defaults.h"
int main(int argc, char **argv)
{
int c, sorts = 0;
int option_index = 0;
char *tmp;
struct range_t *tmp_ranges;
/* Options for getopt_long */
static struct option const long_options[] = {
{"config", required_argument, 0, (int) 'c'},
{"leases", required_argument, 0, (int) 'l'},
{"format", required_argument, 0, (int) 'f'},
{"sort", required_argument, 0, (int) 's'},
{"reverse", no_argument, 0, (int) 'r'},
{"output", required_argument, 0, (int) 'o'},
{"limit", required_argument, 0, (int) 'L'},
{"version", no_argument, 0, (int) 'v'},
{"help", no_argument, 0, (int) 'h'},
{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 */
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 */
config.output_file[0] = '\0';
/* File location defaults */
strncpy(config.dhcpdconf_file, DHCPDCONF_FILE,
(size_t) MAXLEN - 1);
strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE,
(size_t) MAXLEN - 1);
tmp = OUTPUT_LIMIT;
config.output_limit[0] = (int) (*tmp - '0');
tmp++;
config.output_limit[1] = (int) (*tmp - '0');
fullhtml = false;
/* Make sure some output format is selected by default */
strncpy(config.output_format, OUTPUT_FORMAT, (size_t) 1);
/* Default sort order is by IPs small to big */
config.reverse_order = false;
/* Parse command line options */
while (1) {
c = getopt_long(argc, argv, "c:l:f:o:s:rL:vh",
long_options, &option_index);
if (c == EOF)
break;
switch (c) {
case 0:
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);
}
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);
}
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);
}
break;
case 's':
/* Output sorting option */
if (optarg != NULL) {
sorts = strlen(optarg);
if (sorts > 5) {
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);
}
} else {
eprintf
("main: for argument sort order parameter not set");
usage(EXIT_FAILURE);
}
break;
case 'r':
/* What ever sort in reverse order */
config.reverse_order = true;
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);
}
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';
} 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",
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);
case 'h':
/* Print help */
usage(EXIT_SUCCESS);
default:
usage(EXIT_FAILURE);
}
}
/* Output function selection */
switch (config.output_format[0]) {
case 't':
output_analysis = output_txt;
break;
case 'h':
output_analysis = output_html;
break;
case 'H':
output_analysis = output_html;
fullhtml = true;
break;
case 'x':
output_analysis = output_xml;
break;
case 'X':
output_analysis = output_xml;
break;
case 'c':
output_analysis = output_csv;
break;
case 's':
/* output_analysis = output_snmp; */
output_analysis = output_txt;
break;
default:
eprintf("main: unknown ouput format");
usage(EXIT_FAILURE);
}
/* Do the job */
prepare_memory();
parse_config(true, config.dhcpdconf_file, shared_net_names,
shared_net_names + strlen(shared_net_names) + 1,
shared_networks);
if ((config.output_format[0] == 'x')
|| (config.output_format[0] == 'X')) {
printf("<dhcpstatus>\n");
};
parse_leases();
prepare_data();
do_counting();
tmp_ranges = safe_malloc(sizeof(struct range_t) * num_ranges);
if (sorts != 0) {
mergesort_ranges(ranges, num_ranges, tmp_ranges);
}
if (config.reverse_order == true) {
flip_ranges(ranges, tmp_ranges);
}
free(tmp_ranges);
output_analysis();
if ((config.output_format[0] == 'x')
|| (config.output_format[0] == 'X')) {
printf("</dhcpstatus>\n");
};
exit(EXIT_SUCCESS);
}
/* Global allocations, counter resets etc */
int prepare_memory()
{
num_ranges = num_shared_networks = 0;
shared_networks =
safe_malloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
shared_net_names =
safe_malloc(sizeof(char) * SHARED_NETWORKS_NAMES);
ranges = safe_malloc(sizeof(struct range_t) * RANGES);
/* First shared network entry is all networks */
strcpy(shared_net_names, "All networks");
return 0;
}