add start, end and hostname printing support for xml and json

In short it gets these parameters and prints them in json and xml formats.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Belkacem Daheb 2023-07-26 09:20:32 +01:00 committed by Sami Kerola
parent 102d017ed5
commit 3d37ac0a2d
No known key found for this signature in database
GPG key ID: 0D46FEF7E61DBB46
8 changed files with 92 additions and 4 deletions

1
THANKS
View file

@ -49,3 +49,4 @@ Mark Sangster
Brent Swingle
Mathieu Morier
Jean Benoit
Belkacem Daheb

View file

@ -102,6 +102,9 @@ enum prefix_t {
PREFIX_BINDING_STATE_ACTIVE,
PREFIX_BINDING_STATE_BACKUP,
PREFIX_HARDWARE_ETHERNET,
PREFIX_STARTS,
PREFIX_ENDS,
PREFIX_HOSTNAME,
NUM_OF_PREFIX
};
@ -180,6 +183,9 @@ struct leases_t {
char *ethernet;
UT_hash_handle hh;
enum ltype type;
char *ends;
char *starts;
char *hostname;
};
/*! \enum limbits

View file

@ -83,7 +83,7 @@ enum isc_conf_parser {
int parse_leases(struct conf_t *state)
{
FILE *dhcpd_leases;
char *line, *ipstring, macstring[20], *stop;
char *line, *ipstring, macstring[20], *stop, endsstr[30], startsstr[30], hostnamestr[MAXLEN];
union ipaddr_t addr;
struct leases_t *lease;
@ -100,6 +100,9 @@ int parse_leases(struct conf_t *state)
line[0] = '\0';
ipstring = xmalloc(sizeof(char) * MAXLEN);
ipstring[0] = '\0';
endsstr[0] = '\0';
startsstr[0] = '\0';
hostnamestr[0] = '\0';
while (!feof(dhcpd_leases)) {
if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases))
error(EXIT_FAILURE, errno, "parse_leases: %s", state->dhcpdlease_file);
@ -142,8 +145,32 @@ int parse_leases(struct conf_t *state)
break;
memcpy(macstring, line + 20, 17);
macstring[17] = '\0';
if ((lease = find_lease(state, &addr)) != NULL)
if ((lease = find_lease(state, &addr)) != NULL) {
lease->ethernet = xstrdup(macstring);
lease->starts = xstrdup(startsstr);
lease->ends = xstrdup(endsstr);
}
break;
case PREFIX_ENDS:
if (state->print_mac_addreses == 0)
break;
strncpy(endsstr, line + 7, sizeof(endsstr)-1);
endsstr[strlen(endsstr)-2] = '\0';
break;
case PREFIX_STARTS:
if (state->print_mac_addreses == 0)
break;
strncpy(startsstr, line + 9, sizeof(startsstr)-1);
startsstr[strlen(startsstr)-2] = '\0';
break;
case PREFIX_HOSTNAME:
if (state->print_mac_addreses == 0)
break;
strncpy(hostnamestr, line + 19, sizeof(hostnamestr)-1);
hostnamestr[strlen(hostnamestr)-3] = '\0';
if ((lease = find_lease(state, &addr)) != NULL) {
lease->hostname = xstrdup(hostnamestr);
}
break;
default:
/* do nothing */ ;

View file

@ -66,6 +66,9 @@ void add_lease_v4(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
l->type = type;
HASH_ADD_INT(state->leases, ip.v4, l);
l->ethernet = NULL;
l->ends = NULL;
l->starts = NULL;
l->hostname = NULL;
}
void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
@ -77,6 +80,9 @@ void add_lease_v6(struct conf_t *state, union ipaddr_t *addr, enum ltype type)
l->type = type;
HASH_ADD_V6(state->leases, ip.v6, l);
l->ethernet = NULL;
l->ends = NULL;
l->starts = NULL;
l->hostname = NULL;
}
/*! \brief Find pointer to lease from hash array.
@ -110,6 +116,9 @@ struct leases_t *find_lease_v6(struct conf_t *state, union ipaddr_t *addr)
void delete_lease(struct conf_t *state, struct leases_t *lease)
{
free(lease->ethernet);
free(lease->ends);
free(lease->starts);
free(lease->hostname);
HASH_DEL(state->leases, lease);
free(lease);
}
@ -122,6 +131,9 @@ void delete_all_leases(struct conf_t *state)
HASH_ITER(hh, state->leases, l, tmp) {
free(l->ethernet);
free(l->ends);
free(l->starts);
free(l->hostname);
HASH_DEL(state->leases, l);
free(l);
}
@ -134,6 +146,9 @@ void delete_all_leases(struct conf_t *state)
l = state->leases;
free(l->ethernet);
free(l->ends);
free(l->starts);
free(l->hostname);
HASH_DEL(state->leases, l); /* leases advances to next on delete */
free(l);
}

View file

@ -417,6 +417,12 @@ int
}
if (!memcmp("lease ", str, 6))
return PREFIX_LEASE;
else if (!memcmp(" starts ", str, 9))
return PREFIX_STARTS;
else if (!memcmp(" ends ", str, 7))
return PREFIX_ENDS;
else if (!memcmp(" client-hostname ", str, 18))
return PREFIX_HOSTNAME;
return NUM_OF_PREFIX;
}
@ -472,6 +478,12 @@ int
}
if (!memcmp(" iaaddr ", str, 9))
return PREFIX_LEASE;
else if (!memcmp(" starts ", str, 9))
return PREFIX_STARTS;
else if (!memcmp(" ends ", str, 7))
return PREFIX_ENDS;
else if (!memcmp(" client-hostname ", str, 18))
return PREFIX_HOSTNAME;
return NUM_OF_PREFIX;
}

View file

@ -387,7 +387,19 @@ static int output_xml(struct conf_t *state)
if (l->ethernet != NULL) {
fputs(l->ethernet, outfile);
}
fputs("</macaddress>\n</active_lease>\n", outfile);
fputs("</macaddress>\n\t<starts>", outfile);
if (l->starts != NULL) {
fputs(l->starts, outfile);
}
fputs("</starts>\n\t<ends>", outfile);
if (l->ends != NULL) {
fputs(l->ends, outfile);
}
fputs("</ends>\n\t<hostname>", outfile);
if (l->hostname != NULL) {
fputs(l->hostname, outfile);
}
fputs("</hostname>\n</active_lease>\n", outfile);
}
}
}
@ -481,6 +493,18 @@ static int output_json(struct conf_t *state)
if (l->ethernet != NULL) {
fputs(l->ethernet, outfile);
}
fputs("\", \"starts\":\"", outfile);
if (l->starts != NULL) {
fputs(l->starts, outfile);
}
fputs("\", \"ends\":\"", outfile);
if (l->ends != NULL) {
fputs(l->ends, outfile);
}
fputs("\", \"hostname\":\"", outfile);
if (l->hostname != NULL) {
fputs(l->hostname, outfile);
}
fputs("\" }", outfile);
}
}

View file

@ -1,6 +1,6 @@
{
"active_leases": [
{ "ip":"10.0.0.5", "macaddress":"00:00:00:00:00:00" }
{ "ip":"10.0.0.5", "macaddress":"00:00:00:00:00:00", "starts":"", "ends":"", "hostname":"" }
],
"subnets": [
{ "location":"All networks", "range":"10.0.0.1 - 10.0.0.10", "first_ip":"10.0.0.1", "last_ip":"10.0.0.10", "defined":10, "used":1, "touched":0, "free":9, "percent":10, "touch_count":1, "touch_percent":10, "status":0 }

View file

@ -2,6 +2,9 @@
<active_lease>
<ip>10.0.0.5</ip>
<macaddress>00:00:00:00:00:00</macaddress>
<starts></starts>
<ends></ends>
<hostname></hostname>
</active_lease>
<subnet>
<location>All networks</location>