--- telnetbbs/telnetbbs.pl 2010/01/06 05:19:47 1.1 +++ telnetbbs/telnetbbs.pl 2010/12/10 23:28:09 1.4 @@ -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.4 2010/12/10 23:28:09 nick Exp $ ## ################################################################################ BEGIN { @@ -36,6 +36,19 @@ 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 = 5000; + ## ## Check that we are 'root' ## @@ -59,9 +72,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 +98,7 @@ while( 1 ) { sleep 1; } ############################################################################### ############################################################################### -sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } +sub logmsg { print "$0 $$ ", scalar localtime, ":@_\n" } ############################################################################### ## startNetServer( ); @@ -91,8 +111,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 +130,42 @@ sub startNetServer ## REQUEST: while( $hostConnection = $server->accept( ) ) { + ## + ## Find the next available node + ## + my $node = 0; + foreach (1 .. $MAX_NODE) + { + next if ( $node ); + if ( ! $nodes[$_] ) + { + $node = $BBS_NODE = $_; + $nodes[$node]++; + } + } + +print STDOUT "Node Status: \n" . Dumper( @nodes ); + + ## + ## 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; @@ -124,8 +180,16 @@ sub startNetServer ## $hostConnection->autoflush( 1 ); - print $hostConnection "Welcome to Hell's Dominion BBS!" . $EOL; - print $hostConnection "Starting BBS on node $node...$EOL"; + print $hostConnection "Welcome to $BBS_NAME!" . $EOL; + + ## + if ( ! $BBS_NODE ) + { + print $hostConnection "No available nodes. Try again later.".$EOL; + exit; + } + + print $hostConnection "Starting BBS on node $BBS_NODE...$EOL"; ## ## Launch BBS via dosbox @@ -133,18 +197,24 @@ sub startNetServer my $bbsPID = fork( ); if ( $bbsPID ) { - exec( "dosbox" ); + my $cmd = $BBS_CMD . $DBCONF; + exec( $cmd ); 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 +228,9 @@ sub startNetServer { print $hostConnection $byte; } - kill( "TERM" => $childPID ); + $nodes[$BBS_NODE] = 0; +print "Post-Disconnect Node Status: \n" . Dumper( @nodes ); + kill( "TERM" => $kidpid ); } else { @@ -168,11 +240,13 @@ sub startNetServer print $bbs $byte; } } + + unlink( $DBCONF ); } close( $hostConnection ); exit; - close( $server ); +# close( $server ); } ############################################################################### @@ -188,9 +262,14 @@ sub shutdown { my $signame = shift || 0; - print "$0: Shutdown (SIG$signame) received.\n" + &logmsg( "$0: Shutdown (SIG$signame) received.\n" ) if( $signame ); + ## + ## Close Log + ## + close( LOG ); + ## ## Remove the PID ## @@ -319,7 +398,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 +410,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