ctrladdr.in 7.31 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# Copyright (c) 2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
24 25
#

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#
# 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.

46 47 48 49 50 51 52 53 54 55 56
use English;
use Getopt::Std;
use strict;
use Socket;

#
# Configure variables
#
my $TB			= "@prefix@";
my $control_network     = "@CONTROL_NETWORK@";
my $control_netmask     = "@CONTROL_NETMASK@";
57
my $dhcpd_dynrange      = "@DHCPD_DYNRANGE@";
58 59
my $DIG                 = "@DIG@";
my $ourdomain           = "@OURDOMAIN@";
60
my $DHCPD_TEMPLATE      = "/usr/local/etc/dhcpd.conf.template";
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

#
# 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;

116 117 118 119

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
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 ) ) . " (" . 
145
		( $i - $range_start ) . " addresses)\n";
146 147 148 149 150 151
	}

	$range_start = undef;
    }
}

152 153 154
#
# Populate the set of known addresses.
#
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

# configure-time DHCP addresses.
if( $dhcpd_dynrange =~ /([0-9.]+) ([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";
	}
    }
}

# DHCP config file.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
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";
}

210
# DNS records retrieved from name server.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
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 );
}

229
# Anything marked with a control role in the interfaces table.
230 231 232 233 234 235 236 237 238 239 240
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;
}

241
# Anything with a record in the virt_node_public_addr table.
242 243 244 245 246 247 248 249 250 251 252 253
$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";
}

254
# Network and broadcast addresses (assume all-zeroes and all-ones).
255 256 257
$used{ inet_ntoa( pack( "N", $net ) ) } = "the network";
$used{ inet_ntoa( pack( "N", $net | ~$mask ) ) } = "broadcast";

258 259 260
#
# The list is complete... ready to compute the output.
#
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
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 );