attend.in 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/perl -w

#
# Copyright (c) 2015 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#

use English;
use strict;
use IO::Select;
use IO::Socket::INET;
use POSIX;
31
use Getopt::Std;
32 33 34 35 36 37 38 39 40 41

#
# Configure variables
#
my $TB			= "@prefix@";
my $RTPORT              = 0x10db; # 10 dB hex = 4315 decimal
my $SETUPPORT           = 0x11db; # 11 dB hex = 4571 decimal
my $ATTEN_ADDR		= "@ATTENUATOR@";
my $ATTEN_PORT		= 3001;
my $VERSION		= 1;
42
my $LOGFILE      	= "$TB/log/attend.log";
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

#
# Socket state constants
#
my $TYPE_MASK = 0x8000;
my $STATE_MASK = 0x7FFF;

my $REAL_TIME = 0x8000;
my $SETUP = 0;

my $LISTENER = 0;
my $WAITING_VERSION = 1;
my $IDLE = 2;
my $ATTEN_LINK = 0x7FFF;

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

76 77 78 79 80 81
sub usage()
{
    print "Usage: mondbd [-d] [-i]\n";
    exit(1);
}

82 83
$ATTEN_ADDR or die "No attenuator defined!\n";

84 85 86 87 88 89 90 91 92 93 94
my $optlist   = "d";
my $debug     = 0;

my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
$SIG{PIPE} = 'IGNORE';

# Listen ONLY on loopback interface -- don't accept control connections
# from remote hosts.
my $rt = new IO::Socket::INET( LocalAddr => '127.0.0.1',
			       LocalPort => "$RTPORT", Proto => 'tcp',
			       Listen => 10, ReuseAddr => 1,
			       Blocking => 0 )
    or die "socket: $!\n";

my $setup = new IO::Socket::INET( LocalAddr => '127.0.0.1',
				  LocalPort => "$SETUPPORT", Proto => 'tcp',
				  Listen => 10, ReuseAddr => 1,
				  Blocking => 0 )
    or die "socket: $!\n";

my $atten;

113 114 115 116 117 118 119 120 121 122
if (CheckDaemonRunning("attend")) {
    fatal("Not starting another attenuator daemon!");
}

if( !$debug) {
    # Go to background.
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
123

124 125 126
if (MarkDaemonRunning("attend")) {
    fatal("Could not mark daemon as running!");
}
127 128 129 130 131 132 133 134 135 136 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 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

my $selectlist = IO::Select->new( $rt, $setup )
    or die "select: $!\n";

my @rtqueue = ();
my @setupqueue = ();

my %states = ( $rt->fileno => $REAL_TIME | $LISTENER,
	       $setup->fileno => $SETUP | $LISTENER );

sub atten_connect() {

    do {
	$atten = IO::Socket::INET->new( PeerAddr => $ATTEN_ADDR,
					PeerPort => $ATTEN_PORT,
					Proto => 'tcp' )
    } until( defined( $atten ) );
    
    $states{ $atten->fileno } = $ATTEN_LINK;

    $selectlist->add( $atten );
}

sub cleanup($) {
    
    my ($fh) = @_;

    $selectlist->remove( $fh );
    
    delete( $states{ $fh->fileno } );
    
    $fh->close();
}

sub handle($) {

    my ($fh) = @_;

    my $state = $states{ $fh->fileno };

    if( ( $state & $STATE_MASK ) == $LISTENER ) {
	my $new = $fh->accept();
	$new->blocking( 0 );
	$states{ $new->fileno } = ( $state & $TYPE_MASK ) | $WAITING_VERSION;
	$selectlist->add( $new );
    } elsif( ( $state & $STATE_MASK ) == $WAITING_VERSION ) {
	my $buf;
	
	if( sysread( $fh, $buf, 1 ) ) {
	    # you can have whatever version you want, as long as it's 1
	    $fh->write( pack( "C", $VERSION ), 1 );
	    $states{ $fh->fileno }++;
	} else {
	    cleanup( $fh );
	}
    } elsif( ( $state & $STATE_MASK ) == $IDLE ) {
	my $buf;
	my $len;

	$len = sysread( $fh, $buf, 2 );
	
	if( defined( $len ) && $len == 2 ) {
	    my $link;
	    my $val;

	    ($link,$val) = unpack( "CC", $buf );

	    if( ( $state & $TYPE_MASK ) == $REAL_TIME ) {
		$rtqueue[ ++$#rtqueue ] = "$link $val";
	    } else {
		$setupqueue[ ++$#setupqueue ] = "$link $val";
	    }
	} else {
	    cleanup( $fh );
	}
    } elsif( ( $state & $STATE_MASK ) == $ATTEN_LINK ) {
	my $buf;
	my $len;

	do {
	    $len = sysread( $fh, $buf, 0xFFFF );
	} while( defined( $len ) && $len == 0xFFFF );

	if( !$len ) {
	    $selectlist->remove( $atten );
	    delete( $states{ $atten->fileno } );
	    $atten->close();
	    atten_connect();
	}
    }
}

sub update($) {

    my ($pair) = @_;

    print $atten "sa $pair\n";
}

atten_connect();

while( 1 ) {
    while( my $pair = shift( @rtqueue ) ) {
	# Handle ALL outstanding real-time updates, since they're our
	# highest priority.
	update( $pair );
    }
    
    if( my $pair = shift( @setupqueue ) ) {
	# Handle ONE outstanding setup update, and then check for higher
	# priority tasks.
	update( $pair );
    }
    
    if( my @ready = $selectlist->can_read ) {
	# Handle ONE ready setup socket, and then check for higher
	# priority tasks.
	handle( $ready[ 0 ] );
    }
}

exit( 0 );