--- telnetbbs/telnetbbs.pl 2010/01/06 05:19:47 1.1 +++ telnetbbs/telnetbbs.pl 2010/12/13 05:54:50 1.5 @@ -6,7 +6,7 @@ ## ## Nicholas DeClario ## October 2009 -## $Id: telnetbbs.pl,v 1.1 2010/01/06 05:19:47 nick Exp $ +## $Id: telnetbbs.pl,v 1.5 2010/12/13 05:54:50 nick Exp $ ## ################################################################################ BEGIN { @@ -36,6 +36,20 @@ my %opts = &fetchOptions( ); my $pidFile = "/var/run/telnetbbs.pid"; my $EOL = "\015\012"; +## +## These will be moved in to a config file +## +my $DISPLAY = ":0.0"; +my $BBS_NAME = "Hell's Dominion BBS"; +my $BBS_NODE = 0; +my $DBCONF = "/tmp/dosbox-__NODE__.conf"; +my $BBS_CMD = "DISPLAY=$DISPLAY /usr/bin/dosbox -conf "; +my $LOG = "/var/log/bbs.log"; +my $MAX_NODE = 1; +my $DOSBOXT = "dosbox.conf.template"; +my $BASE_PORT = 7000; +my $LOCK_PATH = "/tmp"; + ## ## Check that we are 'root' ## @@ -59,9 +73,16 @@ $SIG{CHLD} = 'IGNORE'; local $SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&shutdown; ## +## Open the Log +## +#open LOG, ">>$LOG"; +&logmsg( "Starting telnetbbs server" ); + +## ## Start the network server ## -my $netThread = threads->create( \&startNetServer ); +my $netThread = threads->create( \&startNetServer( ) ); + while( 1 ) { sleep 1; } @@ -78,7 +99,7 @@ while( 1 ) { sleep 1; } ############################################################################### ############################################################################### -sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } +sub logmsg { print STDOUT "$0 $$ ", scalar localtime, ":@_\n" } ############################################################################### ## startNetServer( ); @@ -91,8 +112,8 @@ sub startNetServer { my $hostConnection; my $childPID; - my $port = $opts{'port'}; - my $node = 1; + my $port = $opts{'port'} || 23; + my @nodes = ( ); my $server = IO::Socket::INET->new( LocalPort => $port, @@ -110,6 +131,46 @@ sub startNetServer ## REQUEST: while( $hostConnection = $server->accept( ) ) { + ## + ## Find the next available node + ## + my $node = 0; + my $lock_file = ""; + foreach (1 .. $MAX_NODE) + { +print "Searching for lock: " . $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock\n"; + next if ( -f $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock" ); + + ## + ## Create node lock file + ## + $lock_file = $LOCK_PATH."/".$BBS_NAME."_node".$_.".lock"; + open LOCK, ">$lock_file"; + close( LOCK ); + $node = $BBS_NODE = $_; + } +print "Using lock: " . $LOCK_PATH."/".$BBS_NAME."_node".$node.".lock\n"; + + ## + ## Create our dosbox config + ## + open( DBT, "<$DOSBOXT" ); + my @dbt = ; + close( DBT ); + + my $bpn = $BASE_PORT + $BBS_NODE; + $DBCONF =~ s/__NODE__/$BBS_NODE/g; + open( DBC, ">$DBCONF" ); + foreach( @dbt ) + { + $_ =~ s/__NODE__/$BBS_NODE/g; + $_ =~ s/__LISTEN_PORT__/$bpn/g; + print DBC $_; + } + close( DBC ); + + &logmsg( "Connecting on node $BBS_NODE\n" ); + my $kidpid; my $line; @@ -119,13 +180,23 @@ sub startNetServer } defined( $childPID ) || die( "Cannot fork: $!\n" ); + select $hostConnection; + ## ## Default file descriptor to the client and turn on autoflush ## $hostConnection->autoflush( 1 ); - print $hostConnection "Welcome to Hell's Dominion BBS!" . $EOL; - print $hostConnection "Starting BBS on node $node...$EOL"; + print "Welcome to $BBS_NAME!" . $EOL; + + ## + if ( ! $lock_file ) + { + print "No available nodes. Try again later.".$EOL; + exit; + } + + print "Starting BBS on node $BBS_NODE...$EOL"; ## ## Launch BBS via dosbox @@ -133,18 +204,31 @@ sub startNetServer my $bbsPID = fork( ); if ( $bbsPID ) { - exec( "dosbox" ); + select STDOUT; + my $cmd = $BBS_CMD . $DBCONF; + system( $cmd ); + print "Shutting down node $BBS_NODE\n"; + ## + ## Remove Lock + ## + unlink( $lock_file ); + unlink( $DBCONF ); exit; } - sleep 10; + + ## + ## We wait for dosbox to start and the BBS to start + ## There really should be a better way to determine this + ## + sleep 5; ## ## Create connection to BBS ## my $bbs = IO::Socket::INET->new ( PeerAddr => 'localhost', - Type => SOCK_STREAM, - PeerPort => 5000, + Type => SOCK_STREAM, + PeerPort => $bpn, Proto => 'tcp', ) || die "Could not open BBS socket: $!\n"; $bbs->autoflush( 1 ); @@ -158,7 +242,8 @@ sub startNetServer { print $hostConnection $byte; } - kill( "TERM" => $childPID ); + $nodes[$BBS_NODE] = 0; + kill( "TERM" => $kidpid ); } else { @@ -168,11 +253,13 @@ sub startNetServer print $bbs $byte; } } + + unlink( $DBCONF ); } close( $hostConnection ); exit; - close( $server ); +# close( $server ); } ############################################################################### @@ -188,9 +275,16 @@ sub shutdown { my $signame = shift || 0; - print "$0: Shutdown (SIG$signame) received.\n" + select STDOUT; + + &logmsg( "$0: Shutdown (SIG$signame) received.\n" ) if( $signame ); + ## + ## Close Log + ## + close( LOG ); + ## ## Remove the PID ## @@ -319,7 +413,7 @@ sub fetchOptions { &GetOptions( "help|?" => \$opts{'help'}, "man" => \$opts{'man'}, - "port:i" => \$opts{'port'}, + "port:i" => \$opts{'port'}, ) || &pod2usage( ); &pod2usage( ) if defined $opts{'help'}; &pod2usage( { -verbose => 2, -input => \*DATA } ) if defined $opts{'man'}; @@ -331,15 +425,16 @@ __END__ =head1 NAME -masterbuild.pl - blurb +telnetbbs.pl - A telnet server designed to launch a multi-node BBS. =head1 SYNOPSIS -masterbuild.pl [options] +telnetbbs.pl [options] Options: --help,? Display the basic help menu --man,m Display the detailed man page + --port,p Port to listen on, default 23. =head1 DESCRIPTION