|
|
| version 1.2, 2010/01/06 13:33:19 | version 1.4, 2010/12/10 23:28:09 |
|---|---|
| Line 35 use threads::shared; | Line 35 use threads::shared; |
| my %opts = &fetchOptions( ); | my %opts = &fetchOptions( ); |
| my $pidFile = "/var/run/telnetbbs.pid"; | my $pidFile = "/var/run/telnetbbs.pid"; |
| my $EOL = "\015\012"; | 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_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' | ## Check that we are 'root' |
| Line 60 $SIG{CHLD} = 'IGNORE'; | Line 72 $SIG{CHLD} = 'IGNORE'; |
| local $SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&shutdown; | local $SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&shutdown; |
| ## | ## |
| ## Open the Log | |
| ## | |
| #open LOG, ">>$LOG"; | |
| &logmsg( "Starting telnetbbs server" ); | |
| ## | |
| ## Start the network server | ## Start the network server |
| ## | ## |
| my $netThread = threads->create( \&startNetServer ); | my $netThread = threads->create( \&startNetServer( ) ); |
| while( 1 ) { sleep 1; } | while( 1 ) { sleep 1; } |
| Line 79 while( 1 ) { sleep 1; } | Line 98 while( 1 ) { sleep 1; } |
| ############################################################################### | ############################################################################### |
| ############################################################################### | ############################################################################### |
| sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } | sub logmsg { print "$0 $$ ", scalar localtime, ":@_\n" } |
| ############################################################################### | ############################################################################### |
| ## startNetServer( ); | ## startNetServer( ); |
| Line 92 sub startNetServer | Line 111 sub startNetServer |
| { | { |
| my $hostConnection; | my $hostConnection; |
| my $childPID; | my $childPID; |
| my $port = $opts{'port'} || 23; | my $port = $opts{'port'} || 23; |
| my $node = 1; | my @nodes = ( ); |
| my $server = IO::Socket::INET->new( | my $server = IO::Socket::INET->new( |
| LocalPort => $port, | LocalPort => $port, |
| Line 111 sub startNetServer | Line 130 sub startNetServer |
| ## | ## |
| REQUEST: while( $hostConnection = $server->accept( ) ) | 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 = <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 $kidpid; |
| my $line; | my $line; |
| Line 126 sub startNetServer | Line 181 sub startNetServer |
| $hostConnection->autoflush( 1 ); | $hostConnection->autoflush( 1 ); |
| print $hostConnection "Welcome to $BBS_NAME!" . $EOL; | print $hostConnection "Welcome to $BBS_NAME!" . $EOL; |
| print $hostConnection "Starting BBS on node $node...$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 | ## Launch BBS via dosbox |
| Line 134 sub startNetServer | Line 197 sub startNetServer |
| my $bbsPID = fork( ); | my $bbsPID = fork( ); |
| if ( $bbsPID ) | if ( $bbsPID ) |
| { | { |
| exec( "dosbox" ); | my $cmd = $BBS_CMD . $DBCONF; |
| exec( $cmd ); | |
| exit; | 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 | ## Create connection to BBS |
| ## | ## |
| my $bbs = IO::Socket::INET->new ( | my $bbs = IO::Socket::INET->new ( |
| PeerAddr => 'localhost', | PeerAddr => 'localhost', |
| Type => SOCK_STREAM, | Type => SOCK_STREAM, |
| PeerPort => 5000, | PeerPort => $bpn, |
| Proto => 'tcp', | Proto => 'tcp', |
| ) || die "Could not open BBS socket: $!\n"; | ) || die "Could not open BBS socket: $!\n"; |
| $bbs->autoflush( 1 ); | $bbs->autoflush( 1 ); |
| Line 159 sub startNetServer | Line 228 sub startNetServer |
| { | { |
| print $hostConnection $byte; | print $hostConnection $byte; |
| } | } |
| kill( "TERM" => $childPID ); | $nodes[$BBS_NODE] = 0; |
| print "Post-Disconnect Node Status: \n" . Dumper( @nodes ); | |
| kill( "TERM" => $kidpid ); | |
| } | } |
| else | else |
| { | { |
| Line 169 sub startNetServer | Line 240 sub startNetServer |
| print $bbs $byte; | print $bbs $byte; |
| } | } |
| } | } |
| unlink( $DBCONF ); | |
| } | } |
| close( $hostConnection ); | close( $hostConnection ); |
| exit; | exit; |
| close( $server ); | # close( $server ); |
| } | } |
| ############################################################################### | ############################################################################### |
| Line 189 sub shutdown | Line 262 sub shutdown |
| { | { |
| my $signame = shift || 0; | my $signame = shift || 0; |
| print "$0: Shutdown (SIG$signame) received.\n" | &logmsg( "$0: Shutdown (SIG$signame) received.\n" ) |
| if( $signame ); | if( $signame ); |
| ## | |
| ## Close Log | |
| ## | |
| close( LOG ); | |
| ## | ## |
| ## Remove the PID | ## Remove the PID |
| ## | ## |