#!/u3/thesis/clipper/pl/perl
# a subroutine to resolve a Internet host name to IP address
# Copyright 1990 by Khun Yee Fung (clipper@csd.uwo.ca)
# See the end of the program for warranty information
sub resolver {
  local($sockaddr) = 'S n a4 x8';
  local($hostname) = $_[0];
  local($server) = 'ria';
  local($port) = 53;

  (local($name, $aliases, $type, $len, $saddr) = gethostbyname($server))
    || ((print "hostname\n"), return 0);
  local($sin) = pack($sockaddr, 2, $port, $saddr);

  socket(NSERVER, 2, 1, 0) || ((print "socket: $!\n"), return 0);
  connect(NSERVER, $sin) || ((print "connect: $!\n"), return 0);

  select(NSERVER); $| = 1; select(STDOUT); $| = 1;

  local($len) = 18 + length($hostname);
  local(@names) = split('\.', $hostname);
  local($head) = pack('S6', 319, 256, 1, 0, 0, 0);
  print NSERVER pack('S', $len), $head;
  local($arg, $response);
  foreach $arg (@names) {
    print NSERVER pack('C', length($arg)), $arg;
  }
  print NSERVER pack('CS2', 0, 1, 1);

  read(NSERVER, $len, 2);
  read(NSERVER, $response, unpack('S', $len));
  close NSERVER;

  local(@RCODE) = ("No error", "Format error", "Server Failure", "Name error",
    "Not implemented", "Refused");
  local(@shead) = unpack('S6', $response);
  ($shead[1] & 0x0F) == 0 || return 0;

  local($in) = 12;
  local($ans) = $shead[2];
  local($c);
  while ($ans > 0) {
    while (($c = ord(substr($response, $in++, 1))) != 0) {
      $in += $c;
    }
    $in += 4;
    $ans--;
  }

  $ans = $shead[3];
  local($type, $rdlength, $rdata);
  while ($ans > 0) {
    while (($c = ord(substr($response, $in++, 1))) != 0) {
      ($c & 0xc0) != 0xc0 || $in++, last;
      $in += $c;
    }
    $type = substr($response, $in, 2);
    $in += 8;
    $rdlength = unpack('n', substr($response, $in, 2));
    $in += 2;
    $rdata = substr($response, $in, $rdlength);
    if (unpack('S', $type) == 1) {
      return $rdata;
    }
    $in += $rdlength;
    $ans--;
  }
  return 0;
}
1;

# Copyright 1990 Khun Yee Fung <clipper@csd.uwo.ca>
#
# Permission to use, copy, modify, and distribute, this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of the copyright holders be used in
# advertising or publicity pertaining to distribution of the software with
# specific, written prior permission, and that no fee is charged for further
# distribution of this software, or any modifications thereof.  The copyright
# holder make no representations about the suitability of this software for
# any purpose.  It is provided "as is" without express or implied warranty.
#
# THE COPYRIGHT HOLDER DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
