Commit cd0cc74b authored by Gary Wong's avatar Gary Wong

Added DHCP template parsing to ctrladdr.

Walk through /usr/local/etc/dhcpd.conf.template, and try to detect
"fixed-address" and "range" directives.
parent 8f21a75f
...@@ -39,6 +39,7 @@ my $control_network = "@CONTROL_NETWORK@"; ...@@ -39,6 +39,7 @@ my $control_network = "@CONTROL_NETWORK@";
my $control_netmask = "@CONTROL_NETMASK@"; my $control_netmask = "@CONTROL_NETMASK@";
my $DIG = "@DIG@"; my $DIG = "@DIG@";
my $ourdomain = "@OURDOMAIN@"; my $ourdomain = "@OURDOMAIN@";
my $DHCPD_TEMPLATE = "/usr/local/etc/dhcpd.conf.template";
# #
# Turn off line buffering on output # Turn off line buffering on output
...@@ -133,6 +134,46 @@ sub range_done($) { ...@@ -133,6 +134,46 @@ sub range_done($) {
# #
# Populate the set of known addresses. # Populate the set of known addresses.
# #
if( open( DHCP, $DHCPD_TEMPLATE ) ) {
# This parser is hopelessly fragile, but hopefully it will be good enough.
my $host;
my $name;
my $address;
while( <DHCP> ) {
if( /^\s*host\s+(\S+)\s*\{/ ) {
$host = $1;
} elsif( /^\s*option\s+host-name\s+"(.+)"/ ) {
$name = $1;
} elsif( /^\s*fixed-address\s*([0-9.]+)/ ) {
$address = $1;
} elsif( /^[^#]*\}/ ) {
if( defined( $host ) && defined( $address ) ) {
my $addr_num = unpack( "N", inet_aton( $address ) );
if( ( $addr_num & $mask ) == $net ) {
$used{ $address } = defined( $name ) ? $name : "DHCP";
}
}
$host = $name = $address = undef;
} elsif( /^\s*range\s*([0-9.]+)\s+([0-9.]+)/ ) {
my $first = unpack( "N", inet_aton( $1 ) );
my $last = unpack( "N", inet_aton( $2 ) );
my $i;
for( $i = $first; $i <= $last; $i++ ) {
if( ( $i & $mask ) == $net ) {
$used{ inet_ntoa( pack( "N", $i ) ) } = "dynamic DHCP";
}
}
}
}
close( DHCP );
} else {
print STDERR "warning: could not open DHCP template, " .
"ignoring DHCP hosts\n";
}
if( open( DNS, "$DIG -t axfr $ourdomain |" ) ) { if( open( DNS, "$DIG -t axfr $ourdomain |" ) ) {
while( <DNS> ) { while( <DNS> ) {
if( /^([-a-zA-Z0-9.]+)\.\s+[0-9]+\s+IN\s+A\s+([0-9.]+)/ ) { if( /^([-a-zA-Z0-9.]+)\.\s+[0-9]+\s+IN\s+A\s+([0-9.]+)/ ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment