ctrladdr.in 6.07 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2012 University of Utah and the Flux Group.
# All rights reserved.
#

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#
# Show (un)allocated addresses on the control net.
#
# By default, the output is supposed to be easy to parse and simply
# displays the first available address.  More than one available address
# can be requested with the "-n" option (e.g. "-n 10" will show the
# first ten unallocated addresses).  "-n 0" will show every free
# address on the subnet.
# 
# The "-a" option (meant more for human consumption) also describes
# allocated addresses.  For instance, "ctrladdr -a -n 0" will show
# every address on the control net, and what it's used for (if
# anything).  "-r" will compress ranges of consecutive free addresses
# onto a single line.
# 
# To test whether a particular address is in use, invoke it as (e.g.)
# "ctrladdr -t 155.98.36.1".  This will give an exit code of 0 if the
# address is available, and 1 if used.  Any other options are ignored
# if "-t" is specified.

29 30 31 32 33 34 35 36 37 38 39
use English;
use Getopt::Std;
use strict;
use Socket;

#
# Configure variables
#
my $TB			= "@prefix@";
my $control_network     = "@CONTROL_NETWORK@";
my $control_netmask     = "@CONTROL_NETMASK@";
40 41
my $DIG                 = "@DIG@";
my $ourdomain           = "@OURDOMAIN@";
42
my $DHCPD_TEMPLATE      = "/usr/local/etc/dhcpd.conf.template";
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# Handle command-line options.
#
sub usage() {
    print STDERR "Usage: $0 [-t aaa.bbb.ccc.ddd] [-n num] [-a] [-r]\n" .
        "    -t aaa.bbb.ccc.ddd : exit 0 if address available, 1 if used\n" .
	"    -n num : show num addresses (0 = unlimited, defaults to 1)\n" .
	"    -a : show allocated addresses too (defaults to free only)\n" .
	"    -r : compress ranges of consecutive free addresses\n";
    exit( 1 );
}

my $test = undef;
my $num = 1;
my $all = undef;
my $range = undef;
my %options = ();

if( !getopts( "t:n:ar", \%options ) ) {
    usage();
}
if( $options{ 't' } ) {
    $test = $options{ 't' };
}
if( exists( $options{ 'n' } ) ) {
    $num = $options{ 'n' };
}
if( $options{ 'a' } ) {
    $all = 1;
}
if( $options{ 'r' } ) {
    $range = 1;
}

usage() unless @ARGV == 0;

98 99 100 101

my $net = unpack( "N", inet_aton( $control_network ) );
my $mask = unpack( "N", inet_aton( $control_netmask ) );

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
my %used = ();

sub show($) {

    my ($addr) = @_;

    if( $used{ $addr } ) {
	print $addr . " is assigned to " . $used{ $addr } . "\n";
    } else {
	print $addr . "\n";
    }
}

my $range_start = undef;

sub range_done($) {
    
    my ($i) = @_;

    if( $range_start ) {
	if( $range_start == $i - 1 ) {
	    print inet_ntoa( pack( "N", $range_start ) ) . "\n";
	} else {
	    print inet_ntoa( pack( "N", $range_start ) ) . "-" .
		inet_ntoa( pack( "N", $i - 1 ) ) . " (" . 
127
		( $i - $range_start ) . " addresses)\n";
128 129 130 131 132 133
	}

	$range_start = undef;
    }
}

134 135 136
#
# Populate the set of known addresses.
#
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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";
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
if( open( DNS, "$DIG -t axfr $ourdomain |" ) ) {
    while( <DNS> ) {
	if( /^([-a-zA-Z0-9.]+)\.\s+[0-9]+\s+IN\s+A\s+([0-9.]+)/ ) {
	    my $dns_name = $1;
	    my $dns_addr = $2;
	    my $dns_addr_num = unpack( "N", inet_aton( $2 ) );

	    if( ( $dns_addr_num & $mask ) == $net ) {
		$used{ $dns_addr } = $dns_name;
	    }
	}
    }
    close( DNS );
} else {
    print STDERR "Could not read zone transfer\n";
    exit( 1 );
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
my $result = DBQueryWarn( "SELECT IP, node_id FROM interfaces WHERE " .
			  "role='ctrl';" );
if( !$result ) {
    print STDERR "Could not retrieve addresses from interfaces table\n";
    exit( 1 );
}

while( my( $IP, $node_id ) = $result->fetchrow_array() ) {
    $used{ $IP } = $node_id;
}

$result = DBQueryWarn( "SELECT IP FROM virt_node_public_addr;" );

if( !$result ) {
    print STDERR "Could not retrieve addresses from " .
	"virt_node_public_addr table\n";
    exit( 1 );
}

while( my( $IP ) = $result->fetchrow_array() ) {
    $used{ $IP } = "dynamic virtual nodes";
}

# XXX Find any other addresses which are allocated, and add them to %used

$used{ inet_ntoa( pack( "N", $net ) ) } = "the network";
$used{ inet_ntoa( pack( "N", $net | ~$mask ) ) } = "broadcast";

223 224 225
#
# The list is complete... ready to compute the output.
#
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
if( $test ) {
    my $t = unpack( "N", inet_aton( $test ) );

    if( ( $t & $mask ) != $net ) {
	print STDERR "warning: address $test is not on control network\n";
    }

    show( $test );

    exit( exists( $used{ $test } ) ? 1 : 0 );
} else {
    my $free = 0;

    my $i;

    for( $i = $net; ( ( $i & $mask ) == $net ) &&
	 ( !$num || $free < $num ); $i++ ) {
	my $addr = inet_ntoa( pack( "N", $i ) );
	if( $range ) {
	    if( $used{ $addr } ) {
		range_done( $i );
	    } elsif( !defined( $range_start ) ) {
		$range_start = $i;
	    }

	    show( $addr ) if( $all && $used{ $addr } );
	} else {
	    show( $addr ) if( $all || !$used{ $addr } );
	}
	$free++ if( !$used{ $addr } );
    }

    range_done( $i );
}

exit( 0 );