[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Is there a trick to gethostbyname()?



At 13:34 -0800 12/28/98, Michael Brian Bentley wrote:
> Thanks. In w3mir, the following code:
>
> my $thishost = hostname();
> ( my $name, undef ) = gethostbyname( $thishost );

w3http.pm does
  use Sys::Hostname;

that's where hostname() is defined (hostname is not a standard Perl
function).

>From The Camel (2d ed. p. 503)

  Sys::Hostname - Try Every Conceivable Way to Get Hostname
  use Sys::Hostname
  $host = hostname();

  Attempts several methods of getting the system hostname ... It tries
  syscall(SYS_gethostname), `hostname`, `uname -n`, and the file /com/host.

Thus I would deduce that it tries `hostname`, given that the other choices
don't work on a Mac (however, my experients indicate that the results from
`hostname` are not quite the same as the results from hostname() so there
is a little more going on here...)

On a Unix machine, `hostname` runs the hostname program in the shell;
In MacPerl (as stated ion macperl.pod) "`hostname` will return the name of
the current Macintosh if TCP/IP is running."

But I don't know where it gets this. When I reset TCP/IP to not have
nameservers, the hostname still comes back (host148.deltagen.com). When it
has nameservers, the data is more useful (catnip.deltagen.com). One thing
is certain; as Chris points out the information does not come from the
FileSharing control panel.

So hostname() will return the name of the Mac you've got, if TCP/IP is
running. Then gethostbyname translates that hostname into its
"corresponding addresses and other names" by asking the nearest nameserver
or the hosts file (/etc/hosts on Unix; System Folder:Preferences:hosts on
the Mac) for the data.

  #!perl

  use Sys::Hostname;
  my $thishost = hostname();

  print "\$thishost $thishost\n";

  (my $name, undef) = gethostbyname($thishost);

  print "\$name $name\n";

  print "hostname is ", `hostname`;

prints:

  $thishost catnip
  $name catnip.deltagen.com
  hostname is catnip.deltagen.com

$name is defined because we have a local nameserver that hands back the
information. When there is no nameserver, that variable is undefined.

> I'm trying now to make sense of hostname() and why it selects that name,

hostname() returns the name of the Mac but I'm not sure where it gets that.
I can't prove it gets this from the File Sharing Control panel; on 8.5.1 I
am pretty much certain it does not get the info from there.  Perhaps
something "makes it up" from the IP number?  _someone_ knows.

gethostbyname() asks a name server for information.  Alternatively, you can
put this information into a host file and tell TCP/IP 9in Advanced User
mode) to use that file. Entries in the hosts file are of the form

	charlie			    CNAME	myhost.mydomain.edu
	myhost.mydomain.edu	 A	    128.1.1.1

> and what the original author intended.

Ah, now, there you have me :-)

By the way, the version of w3mir on the MacPerl CD is 0.99; w3mir is up to
1.05 at http://www.math.uio.no/~janl/w3mir/.
---
Vicki Brown <vlb@cfcl.com>           |\      _,,,---,,_
Journeyman Sourceror           ZZZzz /,`.-'`'    -.  ;-;;,_
Scripts & Philtres                  |,4-  ) )-,_. ,\ (  `'-'
P.O. Box 1269, San Bruno CA 94066  '---''(_/--'  `-'\_)
http://www.cfcl.com/~vlb      http://www.ptf.com/macperl

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch