ouput format: fix active lease printing

Earlier ethernet address collection did not remove duplicated.  Basically
the problem was the same as in problem fixed in commit
ae7747db87, but this time affecting only
the ethernet addresses.  The fix is, as one can see, to make ethernet
collection to share structure with the lease, so that if one is wrong
both are wrong the same way.

Reported-by: Gilles Bouthenot <gilles.bouthenot@univ-fcomte.fr>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2012-12-07 21:23:01 +00:00
parent d8aae53968
commit fdc560bab9
5 changed files with 38 additions and 50 deletions

View file

@ -259,7 +259,6 @@ int prepare_memory(void)
xmalloc(sizeof(struct shared_network_t) * SHARED_NETWORKS);
ranges = xmalloc(sizeof(struct range_t) * RANGES);
macaddr = NULL;
/* First shared network entry is all networks */
shared_networks->name = xstrdup("All networks");

View file

@ -97,11 +97,6 @@ struct range_t {
unsigned long int touched;
unsigned long int backups;
};
struct macaddr_t {
char *ethernet;
char *ip;
struct macaddr_t *next;
};
enum ltype {
ACTIVE,
FREE,
@ -110,6 +105,7 @@ enum ltype {
struct leases_t {
union ipaddr_t ip; /* ip as key */
enum ltype type;
char *ethernet;
UT_hash_handle hh;
};
@ -130,7 +126,6 @@ struct leases_t *leases;
unsigned long int num_leases;
unsigned long int num_touches;
unsigned long int num_backups;
struct macaddr_t *macaddr;
/* Function prototypes */
int prepare_memory(void);

View file

@ -79,11 +79,10 @@ int prefix_length[2][NUM_OF_PREFIX] = { };
int parse_leases(void)
{
FILE *dhcpd_leases;
char *line, *ipstring, *macstring = NULL;
char *line, *ipstring, macstring[20];
union ipaddr_t addr;
struct stat lease_file_stats;
struct macaddr_t *macaddr_p = NULL;
int sw_active_lease = 0;
int ethernets = false;
struct leases_t *lease;
num_touches = num_leases = num_backups = 0;
@ -120,10 +119,7 @@ int parse_leases(void)
line = xmalloc(sizeof(char) * MAXLEN);
ipstring = xmalloc(sizeof(char) * MAXLEN);
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
macstring = xmalloc(sizeof(char) * 18);
macaddr = xmalloc(sizeof(struct macaddr_t));
macaddr_p = macaddr;
macaddr_p->next = NULL;
ethernets = true;
}
const char **p = prefixes[dhcp_version];
@ -139,7 +135,6 @@ int parse_leases(void)
if (HAS_PREFIX(line, PREFIX_LEASE)) {
nth_field(ipstring, line + l[PREFIX_LEASE]);
parse_ipaddr(ipstring, &addr);
sw_active_lease = 0;
continue;
}
if (HAS_PREFIX(line, PREFIX_BINDING_STATE_FREE)) {
@ -157,7 +152,6 @@ int parse_leases(void)
delete_lease(lease);
}
add_lease(&addr, ACTIVE);
sw_active_lease = 1;
continue;
}
if (HAS_PREFIX(line, PREFIX_BINDING_STATE_BACKUP)) {
@ -168,27 +162,17 @@ int parse_leases(void)
add_lease(&addr, BACKUP);
continue;
}
if ((macaddr != NULL)
&& (sw_active_lease == 1)
&& (xstrstr(line, " hardware ethernet", 19))) {
if (ethernets && (xstrstr(line, " hardware ethernet", 19))) {
nth_field(macstring, line + 20);
if (macstring) {
macstring[17] = '\0';
macaddr_p->ethernet = xstrdup(macstring);
macaddr_p->ip = xstrdup(ipstring);
macaddr_p->next =
xmalloc(sizeof(struct macaddr_t));
macaddr_p = macaddr_p->next;
macaddr_p->next = NULL;
macstring[17] = '\0';
if ((lease = find_lease(&addr)) != NULL) {
lease->ethernet = xstrdup(macstring);
}
}
}
#undef HAS_PREFIX
free(line);
free(ipstring);
if (macaddr != NULL) {
free(macstring);
}
fclose(dhcpd_leases);
return 0;
}

View file

@ -50,6 +50,7 @@ void add_lease(union ipaddr_t *addr, enum ltype type)
} else {
HASH_ADD_INT(leases, ip.v4, l);
}
l->ethernet = NULL;
}
struct leases_t *find_lease(union ipaddr_t *addr)
@ -66,6 +67,8 @@ struct leases_t *find_lease(union ipaddr_t *addr)
void delete_lease(struct leases_t *lease)
{
if (lease->ethernet != NULL)
free(lease->ethernet);
HASH_DEL(leases, lease);
free(lease);
}
@ -86,6 +89,7 @@ void delete_all_leases()
struct leases_t *l;
while (leases) {
l = leases;
free(l->ethernet);
HASH_DEL(leases, l); /* leases advances to next on delete */
free(l);
}

View file

@ -220,7 +220,6 @@ int output_xml(void)
struct range_t *range_p;
unsigned long range_size;
struct shared_network_t *shared_p;
struct macaddr_t *macaddr_p;
int ret;
FILE *outfile;
@ -239,12 +238,17 @@ int output_xml(void)
fprintf(outfile, "<dhcpstatus>\n");
if (macaddr != NULL) {
for (macaddr_p = macaddr; macaddr_p->next != NULL;
macaddr_p = macaddr_p->next) {
fprintf(outfile,
"<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
macaddr_p->ip, macaddr_p->ethernet);
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
struct leases_t *l;
for (l = leases; l != NULL; l = l->hh.next) {
if (l->type == ACTIVE) {
fputs("<active_lease>\n\t<ip>", outfile);
fputs(ntop_ipaddr(&l->ip), outfile);
fputs("</ip>\n\t<macaddress>", outfile);
fputs(l->ethernet, outfile);
fputs("</macaddress>\n</active_lease>\n",
outfile);
}
}
}
@ -324,11 +328,10 @@ int output_xml(void)
int output_json(void)
{
unsigned int i;
unsigned int i = 0;
struct range_t *range_p;
unsigned long range_size;
struct shared_network_t *shared_p;
struct macaddr_t *macaddr_p;
int ret;
FILE *outfile;
unsigned int sep;
@ -350,19 +353,22 @@ int output_json(void)
fprintf(outfile, "{\n");
if (macaddr != NULL) {
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
struct leases_t *l;
fprintf(outfile, " \"active_leases\": [");
for (i = 0, macaddr_p = macaddr; macaddr_p->next != NULL;
macaddr_p = macaddr_p->next) {
if (0 != i) {
fprintf(outfile, ",\n ");
} else {
fprintf(outfile, "\n ");
for (l = leases; l != NULL; l = l->hh.next) {
if (l->type == ACTIVE) {
if (i == 0) {
i = 1;
} else {
fputc(',', outfile);
}
fputs("\n { \"ip\":\"", outfile);
fputs(ntop_ipaddr(&l->ip), outfile);
fputs("\", \"macaddress\":\"", outfile);
fputs(l->ethernet, outfile);
fputs("\" }", outfile);
}
fprintf(outfile,
"{ \"ip\":\"%s\", \"macaddress\":\"%s\" }",
macaddr_p->ip, macaddr_p->ethernet);
i++;
}
fprintf(outfile, "\n ]"); /* end of active_leases */
sep++;