#!/usr/bin/env perl # WWW interface for amplifier, ipa_control_server.pl ## Start user config # # port to bind to $port = 2600; # file to log connections, writable by whatever uid this is running as $log = "/var/log/ipa_control_server.log"; # path to ipa_control $ipac = "/usr/local/bin/ipa_control"; # null all messages $null = ">/dev/null 2>&1"; # # End user config use Socket; use FileHandle; sub spawn; sub Do_It_Disco_Fluid { print LOG "($_)\n"; if ((0xffff & system $args) != 0) { print "error: $!\n"; exit 1; } exit 0; } $proto = getprotobyname('tcp'); socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die "setsockopt: $!\n"; bind(Server, sockaddr_in ($port, INADDR_ANY)) or die "bind: $!\n"; listen(Server, SOMAXCONN) or die "listen $!\n"; $paddr = 0; $iaddr = 0; $name = 0; $_ = 0; @_ = (0); $coderef = 0; $pid = 0; $waitedpid = 0; $port=0; sub ABDUCTOR { $waitedpid = wait; $SIG{CHLD} = \&ABDUCTOR; } $SIG{CHLD} = \&ABDUCTOR; for (; $paddr = accept(Client, Server); close Client) { ($port, $iaddr) = sockaddr_in($paddr); $name = gethostbyaddr($iaddr, AF_INET); spawn sub { open LOG,">>$log" or warn "Can't open $log: $!\n"; print LOG "\n", scalar gmtime, "\n". "Connection from $name [", inet_ntoa($iaddr); print LOG "] at port $port "; while ($_ = ) { s/[\r\n]//g; tr/a-zA-Z //csd; if ($_ eq "HIGH") { &Do_It_Disco_Fluid($args = "$ipac -B $null"); } elsif ($_ eq "MIDDLE HIGH") { &Do_It_Disco_Fluid($args = "$ipac -b $null"); } elsif ($_ eq "MIDDLE") { &Do_It_Disco_Fluid($args = "$ipac -m $null"); } elsif ($_ eq "MIDDLE LOW") { &Do_It_Disco_Fluid($args = "$ipac -l $null"); } elsif ($_ eq "LOW") { &Do_It_Disco_Fluid($args = "$ipac -L $null"); } else { print LOG "(unknown: $_)\n"; exit 1; } } }; } sub spawn { local ($coderef, $pid); $coderef = shift; @_ = (); $_ = ""; if (!defined ($pid = fork)) { return; } elsif ($pid) { return; } open STDIN, "<&Client" or die "Can't dup client to stdin\n"; open STDOUT, ">&Client" or die "Can't dup client to stdout\n"; STDOUT->autoflush(); exit &$coderef(); }