--- loncom/lonnet/perl/lonnet.pm 2010/07/17 20:02:13 1.1073 +++ loncom/lonnet/perl/lonnet.pm 2010/07/20 02:42:47 1.1074 @@ -1,7 +1,7 @@ # The LearningOnline Network # TCP networking package # -# $Id: lonnet.pm,v 1.1073 2010/07/17 20:02:13 raeburn Exp $ +# $Id: lonnet.pm,v 1.1074 2010/07/20 02:42:47 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -76,7 +76,7 @@ use HTTP::Date; use Image::Magick; use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir - $_64bit %env %protocol %loncaparevs); + $_64bit %env %protocol %loncaparevs %serverhomeIDs); my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash, %userrolehash, $processmarker, $dumpcount, %coursedombuf, @@ -253,6 +253,34 @@ sub get_server_loncaparev { } } +sub get_server_homeID { + my ($hostname,$ignore_cache,$caller) = @_; + unless ($ignore_cache) { + my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname); + if (defined($cached)) { + return $serverhomeID; + } + } + my $cachetime = 12*3600; + my $serverhomeID; + if ($caller eq 'loncron') { + my @machine_ids = &machine_ids($hostname); + foreach my $id (@machine_ids) { + my $response = &reply('serverhomeID',$id); + unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) { + $serverhomeID = $response; + last; + } + } + if ($serverhomeID eq '') { + $serverhomeID = $machine_ids[-1]; + } + } else { + $serverhomeID = $serverhomeIDs{$hostname}; + } + return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime); +} + # -------------------------------------------------- Non-critical communication sub subreply { my ($cmd,$server)=@_; @@ -884,18 +912,19 @@ sub authenticate { } sub can_host_session { - my ($udom,$machinedom,$remoterev,$remotesessions,$hostedsessions) = @_; + my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_; my $canhost = 1; + my $host_idn = &Apache::lonnet::internet_dom($lonhost); if (ref($remotesessions) eq 'HASH') { if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') { - if (grep(/^\Q$machinedom\E$/,@{$remotesessions->{'excludedomain'}})) { + if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) { $canhost = 0; } else { $canhost = 1; } } if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') { - if (grep(/^\Q$machinedom\E$/,@{$remotesessions->{'includedomain'}})) { + if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) { $canhost = 1; } else { $canhost = 0; @@ -9773,6 +9802,7 @@ sub get_dns { my %libserv; my $loaded; my %name_to_host; + my %internetdom; sub parse_hosts_tab { my ($file) = @_; @@ -9780,7 +9810,7 @@ sub get_dns { next if ($configline =~ /^(\#|\s*$ )/x); next if ($configline =~ /^\^/); chomp($configline); - my ($id,$domain,$role,$name,$protocol)=split(/:/,$configline); + my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline); $name=~s/\s//g; if ($id && $domain && $role && $name) { $hostname{$id}=$name; @@ -9796,6 +9826,9 @@ sub get_dns { } else { $protocol{$id} = 'http'; } + if (defined($intdom)) { + $internetdom{$id} = $intdom; + } } } } @@ -9905,6 +9938,13 @@ sub get_dns { my @uniq = grep(!$seen{$_}++, values(%hostdom)); return @uniq; } + + sub internet_dom { + &load_hosts_tab() if (!$loaded); + + my ($lonid) = @_; + return $internetdom{$lonid}; + } } { @@ -10022,6 +10062,36 @@ sub get_dns { return undef; } + sub get_internet_names { + my ($lonid) = @_; + return if ($lonid eq ''); + my ($idnref,$cached)= + &Apache::lonnet::is_cached_new('internetnames',$lonid); + if ($cached) { + return $idnref; + } + my $ip = &get_host_ip($lonid); + my @hosts = &get_hosts_from_ip($ip); + my %iphost = &get_iphost(); + my (@idns,%seen); + foreach my $id (@hosts) { + my $dom = &host_domain($id); + my $prim_id = &domain($dom,'primary'); + my $prim_ip = &get_host_ip($prim_id); + next if ($seen{$prim_ip}); + if (ref($iphost{$prim_ip}) eq 'ARRAY') { + foreach my $id (@{$iphost{$prim_ip}}) { + my $intdom = &internet_dom($id); + unless (grep(/^\Q$intdom\E$/,@idns)) { + push(@idns,$intdom); + } + } + } + $seen{$prim_ip} = 1; + } + return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60); + } + } BEGIN { @@ -10110,6 +10180,20 @@ BEGIN { } close($config); } + } +} + +# ---------------------------------------------------------- Read serverhostID table +{ + if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") { + if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) { + while (my $configline=<$config>) { + chomp($configline); + my ($name,$id)=split(/:/,$configline); + $serverhomeIDs{$name}=$id; + } + close($config); + } } }