diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index 9b90807..aa401e0 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -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"); diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index b15eb30..6dd493a 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -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); diff --git a/src/getdata.c b/src/getdata.c index d5fe77e..1a57909 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -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; } diff --git a/src/hash.c b/src/hash.c index a9e41b8..d374860 100644 --- a/src/hash.c +++ b/src/hash.c @@ -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); } diff --git a/src/output.c b/src/output.c index c3e6243..ae16421 100644 --- a/src/output.c +++ b/src/output.c @@ -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, "\n"); - if (macaddr != NULL) { - for (macaddr_p = macaddr; macaddr_p->next != NULL; - macaddr_p = macaddr_p->next) { - fprintf(outfile, - "\n\t%s\n\t%s\n\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("\n\t", outfile); + fputs(ntop_ipaddr(&l->ip), outfile); + fputs("\n\t", outfile); + fputs(l->ethernet, outfile); + fputs("\n\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++;