File:  [Local Repository] / vmodem / vmodem
Revision 1.5: download - view: text, annotated - select for diffs
Mon May 18 14:45:45 2009 UTC (14 years, 11 months ago) by nick
Branches: MAIN
CVS tags: vmodem-2_branch, vmodem-2, HEAD
Project is going on hold for a while.  Code entered in may not work, but is
not broken.

#!/usr/bin/perl -wT
################################################################################
##
## See end of script for comments and 'pod2man vmodem | nroff -man' to
## view man page or pod2text $NAME for plain text.
##
##   Nicholas DeClario <nick@declario.com>
##   May 200
##	$Id: vmodem,v 1.5 2009/05/18 14:45:45 nick Exp $
##
################################################################################
use strict;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use IO::Handle;
use IO::Socket::INET;
use IO::Socket::UNIX;

my $ver  = q$Id: vmodem,v 1.5 2009/05/18 14:45:45 nick Exp $;
my %opts = &fetchOptions( );
my %conf = &getConfig( );
my $log  = $conf{'log'} || "/var/log/vmodem.log";
my $node = &findAvailableNode( );
my %modem_strings = ( 'ATZ'	=> 'OK',
		      'ATA'	=> 'CONNECT 57600',
		      'ATH0'	=> 'DISCONNECT',
		      'ATH1'	=> 'OFFHOOK',
		      'RING'	=> 'RING',
		    );

$node = 1; ## Forced for testing

&openLog( $log );
print "vmodem $ver\nStarted " . &getDate( 1 ) . "\n";
&connectToSerialSocket( $node );
&createTCPTelnetSocket( );

print MODEM "\nOK\n\rRING\n\r";
sleep( 3 );
print MODEM "CONNECT 57600\n\r";
sleep( 5 );

&closeLog( $log );

###############################################################################
##
## &connectToSerialSocket( );
##
##   This will create a UNIX-CONNECT type of connection to the specified 
##   socket in the configuration file.  
##  
##   Generally these sockets should already exist, as the "server" program 
##   (vmware/dosemu) should have created them; however, if this is not the 
##   and the option to create them is enabled, they will be created if they
##   can not be found.
##
###############################################################################
sub connectToSerialSocket {
	my $node = shift || return 0;
	print "Connecting to node sockets\n";
	print "Opening socket node $node: $conf{'nodesocket'}{$node}";
	if ( -S $conf{'nodesocket'}{$node} ) {
		socket( MODEM, PF_UNIX, SOCK_STREAM, 0 ) || die $!;
		connect( MODEM, sockaddr_un( $conf{'nodesocket'}{$node} ) ) || die $!;
		print " Connected!\n";
		autoflush MODEM;
	}
	else {
		print "Failed\n";
		if ( $conf{'createsock'} ) {
			print "Creating socket $conf{'nodesocket'}{$node}";
#			$MODEM = IO::Socket::UNIX->new( 
#				LocalAddr  =>  $conf{'nodesocket'}{$node},
#				Type	   =>  SOCK_DGRAM,
#				Timeout	   =>  10
 #            	        ) or die( $@ );
		}
	}
}

###############################################################################
##
## &createTCPTelnetSocket( );
###
###############################################################################
sub createTCPTelnetSocket {

}

###############################################################################
##
## my $node = &findAvailableNode( );
## 
##   Based on the configuration file options, we search for a the first
##   node that doesn't have a lock file assigned to it and return that.
##   This is the node that this session will be using.
##
##   We return 0 on failure.  Valid nodes are 1..X
##
###############################################################################
sub findAvailableNode {

	return 0;
}
###############################################################################
##
## &getDate( $format );
## 
##    Return the date in a full format by passing '1' to it, or retrieve 
##    the date/time in the form of a time stamp YearMonthDayHourMinute
###
###############################################################################
sub getDate {
	my $format    = shift || 0;
	my @timeData  = localtime( time );
	my @months    = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /;
	my $formatted = "";

	$timeData[5] += 1900;
	$timeData[4]++;

	if ( $format ) {
		$formatted = $months[$timeData[4]] . " " . $timeData[3] . ", " .
			     $timeData[5] . " " . $timeData[2] . ":" .
			     $timeData[1] . ":" . $timeData[0];	
	}
	else {
		for ( my $i = 5; $i >= 1; $i-- ) { $formatted .= $timeData[$i]; }
	}
	
	return $formatted;
}

###############################################################################
##
## &getConfig( );
##
##   Read the designated configuration file and store it in a hash.  The 
##   format of this is a basic key=value set up and we strip leading and/or
##   trailing whitespace.  A '.' delimter can be used in a key to specify
##   a hash of a hash. 
##
###############################################################################
sub getConfig {
	my $file = &determineConfigFile( );
	my %c    = ( );

	if ( ! $file ) {
		print "ERROR: No config file found.\n";
		exit 255;
	}

	open( CONF, "<$file" ) || die ( "Error: Can't read config file $file: $!\n" );
		while( <CONF> ) {
			if ( $_ =~ m/(.*)=(.*)/ ) {
				my $k1 = $1;
				my $k2 = 0;
				my $v  = $2;
				
				$k1 =~ s/^\s+|\s+$//;
				$v  =~ s/^\s+|\s+$//;

				if ( $k1 =~ m/(.*)\.(.*)/ ) {
					$c{$1}{$2} = $v;
				}
				else {
					$c{$k1} = $v;
				}
			}
		}
	close( CONF );

	return %c;
}

###############################################################################
##
## &openLog( $logFile );
##
###############################################################################
sub openLog {
	my $log = shift || return 0;

	open( LOG, ">>$log" ) || die( "Can't open log: $log: $!\n" );
	select( LOG );
}

###############################################################################
##
## &closeLog( $logFile );
##
###############################################################################
sub closeLog {
	my $log = shift || return 0;

	close( LOG );
	select( STDOUT );
}

###############################################################################
##
## &determineConfigFile( );
## 
##    Determine which configuration file to use.  Try the one specified on the
##    command line first, then check in the order of @cfgs below.  If none
##    exist, we return 0;
##
###############################################################################
sub determineConfigFile {
	my @cfgs = qw| ./vmodem.cfg /etc/vmodem.cfg /etc/defaults/vmodem.cfg 
		       /etc/sysconfig/vmodem.cfg |;

	return $opts{'config'} if ( defined $opts{'config'} && -f $opts{'config'} );
	foreach( @cfgs ) { return $_ if ( -f $_ ); }

	return 0;
}

###############################################################################
##
## &displayVersion( );
##
##    Display the current CVS version defined by the CVS ID tag
##
###############################################################################
sub displayVersion {
	print "vModem Version $ver\n\n";
	exit( 0 );
}

###############################################################################
##
## &fetchOptions( );
##
##      Grab our command line arguments and toss them in to a hash
##
###############################################################################
sub fetchOptions {
        my %opts;

        &GetOptions(
			"config=s"	=> \$opts{'config'},
                        "help|?"        => \$opts{'help'},
                        "man"           => \$opts{'man'},
			"version"	=> \$opts{'version'},
                   ) || &pod2usage( );
        &pod2usage( ) if defined $opts{'help'};
        &pod2usage( -verbose => 2 ) if defined $opts{'man'};
	&displayVersion( ) if defined $opts{'version'};

        return %opts;
}

__END__

=head1 NAME

vmodem - blurb

=head1 SYNOPSIS

masterbuild.pl [options]

 Options:
	--config,c	Choose the config file
        --help,?        Display the basic help menu
        --man,m         Display the detailed man page
	--version,v	Display the version and exit

=head1 DESCRIPTION

=head1 HISTORY

=head1 AUTHOR

Nicholas DeClario <nick@declario.com>

=head1 BUGS

This is a work in progress.  Please report all bugs to the author.

=head1 SEE ALSO

=head1 COPYRIGHT

    vModem Copyright (C) 2009 Nicholas DeClario
    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

=cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>