[LON-CAPA-cvs] cvs: loncom /lonnet/perl lonnet.pm

foxr foxr@source.lon-capa.org
Tue, 10 Feb 2009 11:15:17 -0000


foxr		Tue Feb 10 11:15:17 2009 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Added host_from_dns to translate dns hostnames -> loncapa hostnames.
  
  
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.985 loncom/lonnet/perl/lonnet.pm:1.986
--- loncom/lonnet/perl/lonnet.pm:1.985	Sun Feb  8 20:28:52 2009
+++ loncom/lonnet/perl/lonnet.pm	Tue Feb 10 11:15:16 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.985 2009/02/08 20:28:52 raeburn Exp $
+# $Id: lonnet.pm,v 1.986 2009/02/10 11:15:16 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -74,6 +74,7 @@
 use LWP::UserAgent();
 use HTTP::Date;
 use Image::Magick;
+use IO::Socket;
 
 # use Date::Parse;
 use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
@@ -150,7 +151,8 @@
     my $now=time;
     my $local=localtime($now);
     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
-	print $fh "$local ($$): $message\n";
+	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
+	print $fh $logstring;
 	close($fh);
     }
     return 1;
@@ -9010,6 +9012,23 @@
     }
 }
 
+#
+#  Given a DNS returns the loncapa host name for that DNS 
+# 
+sub host_from_dns {
+    my ($dns) = @_;
+    my @hosts;
+    my $ip;
+
+    $ip = gethostbyname($dns);	# Initial translation to IP is in net order.
+    if (length($ip) == 4) { 
+	$ip   = &IO::Socket::inet_ntoa($ip);
+	@hosts = get_hosts_from_ip($ip);
+	return $hosts[0];
+    }
+    return undef;
+}
+
 BEGIN {
 
 # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
@@ -9899,8 +9918,15 @@
 
 spareserver() : find server with least workload from spare.tab
 
+
+=item *
+
+host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
+if there is no corresponding loncapa host.
+
 =back
 
+
 =head2 Apache Request
 
 =over 4