--- loncom/LondConnection.pm 2006/09/15 20:49:24 1.43 +++ loncom/LondConnection.pm 2010/12/21 11:17:33 1.50 @@ -1,7 +1,7 @@ # This module defines and implements a class that represents # a connection to a lond daemon. # -# $Id: LondConnection.pm,v 1.43 2006/09/15 20:49:24 raeburn Exp $ +# $Id: LondConnection.pm,v 1.50 2010/12/21 11:17:33 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -43,9 +43,7 @@ use LONCAPA::lonssl; my $DebugLevel=0; -my %hostshash; my %perlvar; -my $LocalDns = ""; # Need not be defined for managers. my $InsecureOk; # @@ -71,67 +69,9 @@ sub ReadConfig { my $perlvarref = read_conf('loncapa.conf'); %perlvar = %{$perlvarref}; - my $hoststab = read_hosts( - "$perlvar{lonTabDir}/hosts.tab") || - die "Can't read host table!!"; - %hostshash = %{$hoststab}; $ConfigRead = 1; - my $myLonCapaName = $perlvar{lonHostID}; - Debug(8, "My loncapa name is $myLonCapaName"); - - if(defined $hostshash{$myLonCapaName}) { - Debug(8, "My loncapa name is in hosthash"); - my @ConfigLine = @{$hostshash{$myLonCapaName}}; - $LocalDns = $ConfigLine[3]; - Debug(8, "Got local name $LocalDns"); - } - $InsecureOk = $perlvar{loncAllowInsecure}; - - Debug(3, "ReadConfig - LocalDNS = $LocalDns"); -} - -# -# Read a foreign configuration. -# This sub is intended for the cases where the package -# will be read from outside the LonCAPA environment, in that case -# the client will need to explicitly provide: -# - A file in hosts.tab format. -# - Some idea of the 'lonCAPA' name of the local host (for building -# the encryption key). -# -# Parameters: -# MyHost - Name of this host as far as LonCAPA is concerned. -# Filename - Name of a hosts.tab formatted file that will be used -# to build up the hosts table. -# -sub ReadForeignConfig { - - my ($MyHost, $Filename) = @_; - - &Debug(4, "ReadForeignConfig $MyHost $Filename\n"); - - $perlvar{lonHostID} = $MyHost; # Rmember my host. - my $hosttab = read_hosts($Filename) || - die "Can't read hosts table!!"; - %hostshash = %{$hosttab}; - if($DebugLevel > 3) { - foreach my $host (keys %hostshash) { - print STDERR "host $host => $hostshash{$host}\n"; - } - } - $ConfigRead = 1; - - my $myLonCapaName = $perlvar{lonHostID}; - - if(defined $hostshash{$myLonCapaName}) { - my @ConfigLine = @{$hostshash{$myLonCapaName}}; - $LocalDns = $ConfigLine[3]; - } $InsecureOk = $perlvar{loncAllowInsecure}; - - Debug(3, "ReadForeignConfig - LocalDNS = $LocalDns"); - } sub Debug { @@ -213,13 +153,13 @@ host the remote lond is on. This host is =cut sub new { - my ($class, $DnsName, $Port) = @_; + my ($class, $DnsName, $Port, $lonid) = @_; if (!$ConfigRead) { ReadConfig(); $ConfigRead = 1; } - &Debug(4,$class."::new( ".$DnsName.",".$Port.")\n"); + &Debug(4,$class."::new( ".$DnsName.",".$Port.",".$lonid.")\n"); # The host must map to an entry in the hosts table: # We connect to the dns host that corresponds to that @@ -227,19 +167,12 @@ sub new { # negotion. In the objec these become the Host and # LoncapaHim fields of the object respectively. # - if (!exists $hostshash{$DnsName}) { - &Debug(8, "No Such host $DnsName"); - return undef; # No such host!!! - } - my @ConfigLine = @{$hostshash{$DnsName}}; - my $Hostname = $ConfigLine[0]; # 0'th item is the msu id of host. - Debug(5, "Connecting to ".$DnsName); # if it is me use loopback for connection - if ($DnsName eq $LocalDns) { $DnsName="127.0.0.1"; } - Debug(8, "Connecting to $DnsName I am $LocalDns"); + if ($DnsName eq &main::my_hostname()) { $DnsName="127.0.0.1"; } + Debug(9, "Connecting to $DnsName"); # Now create the object... my $self = { Host => $DnsName, - LoncapaHim => $Hostname, + LoncapaHim => $lonid, Port => $Port, State => "Initialized", AuthenticationMode => "", @@ -308,11 +241,11 @@ sub new { if((defined $ca) && (defined $cert) && (defined $sslkeyfile)) { $self->{AuthenticationMode} = "ssl"; - $self->{TransactionRequest} = "init:ssl\n"; + $self->{TransactionRequest} = "init:ssl:$perlvar{'lonVersion'}\n"; } else { if($InsecureOk) { # Allowed to do insecure: $self->{AuthenticationMode} = "insecure"; - $self->{TransactionRequest} = "init\n"; + $self->{TransactionRequest} = "init::$perlvar{'lonVersion'}\n"; } else { # Not allowed to do insecure... $socket->close; @@ -409,6 +342,8 @@ sub Readable { } # Append the data to the buffer. And figure out if the read is done: + $self->{TimeoutRemaining} = $self->{TimeoutValue}; # getting data resets the timeout period. + &Debug(9,"Received from host: ".$data); $self->{TransactionReply} .= $data; if($self->{TransactionReply} =~ m/\n$/) { @@ -568,7 +503,6 @@ sub Readable { $self->{InformWritable} = 1; $self->{InformReadable} = 0; $self->{Timeoutable} = 1; - $self->{TimeoutRemaining} = $self->{TimeoutValue}; $self->Transition("SendingRequest"); return 0; } else { @@ -630,41 +564,42 @@ sub Writable { ($errno == POSIX::EAGAIN) || ($errno == POSIX::EINTR) || ($errno == 0)) { + $self->{TimeoutRemaining} = $self->{TimeoutValue}; substr($self->{TransactionRequest}, 0, $nwritten) = ""; # rmv written part - if(length $self->{TransactionRequest} == 0) { - $self->{InformWritable} = 0; - $self->{InformReadable} = 1; - $self->{TransactionReply} = ''; - # - # Figure out the next state: - # - if($self->{State} eq "Connected") { - $self->Transition("Initialized"); - } elsif($self->{State} eq "ChallengeReceived") { - $self->Transition("ChallengeReplied"); - } elsif($self->{State} eq "RequestingVersion") { - $self->Transition("ReadingVersionString"); - } elsif ($self->{State} eq "SetHost") { - $self->Transition("HostSet"); - } elsif($self->{State} eq "RequestingKey") { - $self->Transition("ReceivingKey"); + if(length $self->{TransactionRequest} == 0) { + $self->{InformWritable} = 0; + $self->{InformReadable} = 1; + $self->{TransactionReply} = ''; + # + # Figure out the next state: + # + if($self->{State} eq "Connected") { + $self->Transition("Initialized"); + } elsif($self->{State} eq "ChallengeReceived") { + $self->Transition("ChallengeReplied"); + } elsif($self->{State} eq "RequestingVersion") { + $self->Transition("ReadingVersionString"); + } elsif ($self->{State} eq "SetHost") { + $self->Transition("HostSet"); + } elsif($self->{State} eq "RequestingKey") { + $self->Transition("ReceivingKey"); # $self->{InformWritable} = 0; # $self->{InformReadable} = 1; # $self->{TransactionReply} = ''; - } elsif ($self->{State} eq "SendingRequest") { - $self->Transition("ReceivingReply"); - $self->{TimeoutRemaining} = $self->{TimeoutValue}; - } elsif ($self->{State} eq "Disconnected") { - return -1; - } - return 0; - } - } else { # The write failed (e.g. partner disconnected). - $self->Transition("Disconnected"); - $socket->close(); - return -1; - } - + } elsif ($self->{State} eq "SendingRequest") { + $self->Transition("ReceivingReply"); + $self->{TimeoutRemaining} = $self->{TimeoutValue}; + } elsif ($self->{State} eq "Disconnected") { + return -1; + } + return 0; + } + } else { # The write failed (e.g. partner disconnected). + $self->Transition("Disconnected"); + $socket->close(); + return -1; + } + } =pod @@ -811,6 +746,7 @@ sub Shutdown { $socket->shutdown(2); } } + $self->{Timeoutable} = 0; # Shutdown sockets can't timeout. } =pod @@ -1136,31 +1072,6 @@ sub CompleteInsecure { } } -=pod - -=head2 GetHostIterator - -Returns a hash iterator to the host information. Each get from -this iterator returns a reference to an array that contains -information read from the hosts configuration file. Array elements -are used as follows: - - [0] - LonCapa host id. - [1] - LonCapa domain name. - [2] - Loncapa role (e.g. library or access). - [3] - DNS name server hostname. - [4] - IP address (result of e.g. nslookup [3]). - [5] - Maximum connection count. - [6] - Idle timeout for reducing connection count. - [7] - Minimum connection count. - -=cut - -sub GetHostIterator { - - return HashIterator->new(\%hostshash); -} - ########################################################### # # The following is an unashamed kludge that is here to @@ -1217,45 +1128,6 @@ sub read_conf return $perlvarref; } -#---------------------- Subroutine read_hosts: Read a LON-CAPA hosts.tab -# formatted configuration file. -# -my $RequiredCount = 4; # Required item count in hosts.tab. -my $DefaultMaxCon = 5; # Default value for maximum connections. -my $DefaultIdle = 1000; # Default connection idle time in seconds. -my $DefaultMinCon = 0; # Default value for minimum connections. - -sub read_hosts { - my $Filename = shift; - my %HostsTab; - - open(CONFIG,'<'.$Filename) or die("Can't read $Filename"); - while (my $line = ) { - if ($line !~ /^\s*\#/) { - $line=~s/\s*$//; - my @items = split(/:/, $line); - if(scalar @items >= $RequiredCount) { - if (scalar @items == $RequiredCount) { # Only required items: - $items[$RequiredCount] = $DefaultMaxCon; - } - if(scalar @items == $RequiredCount + 1) { # up through maxcon. - $items[$RequiredCount+1] = $DefaultIdle; - } - if(scalar @items == $RequiredCount + 2) { # up through idle. - $items[$RequiredCount+2] = $DefaultMinCon; - } - { - my @list = @items; # probably not needed but I'm unsure of - # about the scope of item so... - $HostsTab{$list[3]} = \@list; - } - } - } - } - close(CONFIG); - my $hostref = \%HostsTab; - return ($hostref); -} # # Get the version of our peer. Note that this is only well # defined if the state machine has hit the idle state at least @@ -1461,8 +1333,4 @@ true if the current state requires a wri true if the current state requires timeout support. -=item GetHostIterator: - -Returns an iterator into the host file hash. - =cut