<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/local/bin/perl

# DNS resolver library.  See RFC1035 for more details.
# by Marc Horowitz &lt;marc@mit.edu&gt;

# $Id: resolv.pl,v 1.2 1997/12/05 10:10:25 root Exp root $

# The interface works like this (all functions are in the main package)
#
# res_init()
#	One-time initialization
#
# res_open($nameserver)
#	Sets up a connection to $nameserver.
#	If $nameserver is undefined or empty, then the server is
#	looked up in /etc/resolv.conf.  Returns the filehandle.
#
# res_mkquery($name, $type, $class, $id)
#	builds and returns a query for the given name, type, class,
#	and id.  If no id is given, a random ID is chosen.
#
# res_send($fh, $query)
#	sends $query to the server at $fh.  Returns the response from the
#	server.
#
# res_search($name, $type, $class);
#	attempts to resolve &lt;$name,$type,$class&gt;.  Returns all appropriate
#	answers, join'd by $;.  if the first response is "", the next will
#	be the error code.
#
# response format:
# [0] = id
# [1] = authoritative
# [2] = recursion available
# [3] = response code
# [4] = start of questions
# [5] = number of questions
# [6] = start of answers
# [7] = number of answers
# [8] = start of authority records
# [9] = number of authority records
# [10] = start of add'l records
# [11] = number of add'l records
# [12] ... query and resource records
#
# The variable res'options can be set to any of the values
# which _res.options can be set to.  The constants will be in the 
# main package.
#
# Not implemented yet:
# Only one query is sent out.  That is, RES_DNSRCH is ignored, and
#	only the primary nameserver is used.
#	

## Everything after this is code.  If you look at it, you're
## violating an abstraction barrier.  Shame on you.  :-)

# hack! hack!  This is to confuse the byte order stuff in arpa/nameser.h
# nothing here depends on it anyway.

sub vax {1;}
sub NeXT {0;}

use Socket;
#require 'arpa/nameser.ph' || die "can\'t do arpa/nameser.ph: $@";
#require 'resolv.ph' || die "can\'t do resolv.ph: $@";

# who? me? kludge?

undef &amp;vax;

package res;

# Create conversion arrays. unfortunately, I need to hardcode lists
# of the types and classes.

@qtypes = ("A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL",
	    "WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "UINFO", "UID", "GID",
	    "UNSPEC", "UNSPECA", "AXFR", "MAILB", "MAILA", "ANY");

@qclasses = ("IN", "CHAOS", "HS", "ANY");

@rcodes = ("No Error","Format Error","Server Failure","Name Error",
	   "Not Implemented","Refused");

for (@qtypes) {
    eval "\$qtype[&amp;main'T_$_] = \"$_\";\$qtype{\$_} = &amp;main'T_$_;";
}

for (@qclasses) {
    eval "\$qclass[&amp;main'C_$_] = \"$_\";\$qclass{\$_} = &amp;main'C_$_;";
}

sub qtype_strtonum {
    $qtype{$_[0]} || -1;
}

sub qclass_strtonum {
    $qclass{$_[0]} || -1;
}

sub qtype_numtostr {
    $qtype[$_[0]] || $_[0];
}

sub qclass_numtostr {
    $qclass[$_[0]] || $_[0];
}

sub rcode_text {
    $_[0] ? ($rcodes[$_[0]] || "Unknown rcode") : "";
}

# option bits and defaults

$options = &amp;main'RES_DEFAULT;
$domain = "";

sub debug { $options &amp; &amp;main'RES_DEBUG; }

sub ipaddr {
    if ($_[0] =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
	$saddr = pack("CCCC", $1, $2, $3, $4);
    } else {
	($saddr = ((gethostbyname($_[0]))[4] || undef)) ||
	    return(undef);
    }
}

sub read_conf {
    return if ($domain);

    local($addr);

    if (!$ENV{"LOCALRESOLVERS"} || !$ENV{"LOCALDOMAIN"}) {
	print "Reading /etc/resolv.conf\n" if &amp;debug;

	if (open(CONF,"/etc/resolv.conf")) {
	    local($") = ",";
	    while(&lt;CONF&gt;) {
		chop;
		if (/^domain\s+/) { $domain = $'; }
		elsif (/^nameserver\s+/) {
		    if ($addr = &amp;ipaddr($')) { push(@servers,$addr); }
		}
	    }
	    close(CONF);
	} else {
	    $domain = '';
	    @servers = ("\0\0\0\0");
	}
    }

    (@servers = grep($_=&amp;ipaddr($_), split(/:/,$ENV{"LOCALRESOLVERS"})))
	if ($ENV{"LOCALRESOLVERS"});
    ($domain = $ENV{"LOCALDOMAIN"})
	if ($ENV{"LOCALDOMAIN"});

    if (&amp;debug) {
	print "domain is ",$domain,"\nresolvers are";
	foreach $machine (@servers) { 
	    printf " %s.%s.%s.%s",unpack("CCCC",$machine);
	}
	print "\n";
    }
}

# one-time initialization

$inited = 0;

sub main'res_init {
    return if ($options &amp; &amp;main'RES_INIT);

    local($fh);

    socket(NSUDP, &amp;main'AF_INET, &amp;main'SOCK_DGRAM, &amp;main'PF_UNSPEC) ||
	die "socket: $!";

    $options |= &amp;main'RES_INIT;
    &amp;read_conf;

    $fh = select(NSUDP); $| = 1;   # set nonbufferred
    select($fh);
}

# begin a conversation with a nameserver

sub main'res_open {			# @_ = ($nameserver)
    local($saddr,$port);

    &amp;main'res_init if (!($options &amp; &amp;main'RES_INIT));

    if ($_[0]) {
	if ($_[0] =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
	    $saddr = pack("CCCC", $1, $2, $3, $4);
	} else {
	    ($saddr = ((gethostbyname($_[0]))[4] || undef)) ||
		return(undef);
	}
    } else {
	$saddr = $servers[0];
    }

    # lossage in /etc/services.  hardcode for now.
    #$port = (getservbyname("nameserver","tcp"))[2];
    $port = 53;

    pack("S n a4 x8",&amp;main'AF_INET,$port,$saddr);
}

sub main'res_mkquery { # @_ = ($name,$type,$class,$id)
    local($tnum, $cnum);

    print "building query for &lt;$_[0],$_[1],$_[2]&gt;\n" if &amp;debug;

    $tnum = &amp;qtype_strtonum($_[1]);
    $cnum = &amp;qclass_strtonum($_[2]);

    warn "Unknown query type $_[1] while building query\n" if $tnum == -1;
    warn "Unknown query class $_[2] while building query\n" if $cnum == -1;

    pack("n6",$_[3],
	 ($options &amp; &amp;main'RES_RECURSE)?0x0100:0x0000,
	 1,0,0,0)
	.
	    &amp;unparse_name($_[0])
		.
		    pack("n n",&amp;qtype_strtonum($_[1]),
			 &amp;qclass_strtonum($_[2]));
}

sub unparse_name {
	local($label,$labellen,$str);
	$str = "";
	foreach $label (split(/\./,$_[0])) {
		$labellen = length($label);
		$str .= pack("Ca$labellen",$labellen,$label);
	}
	$str .= pack("C",0);		# root octet
}

%nstcp = ();			# assoc array of open VC's
$sockcnt = "nstcp000";

sub sysread_nchars { # @_ = ($fh,$len)
    local($fh,$len) = @_;
    local($tresp, $resp);

    $tresp = "";
    while(length($resp) &lt; $len) {
	sysread($fh,$tresp,$len - length($resp)) || die "recv: $!";
	print "Got a partial response (size=",length($tresp),")\n"
	    if (&amp;debug &amp;&amp; length($tresp) &lt; $len);
	$resp.=$tresp;
    }
    return($resp);
}

sub main'res_send { # @_ = ($socket,$query)
    local($sin,$len,$packet) = ($_[0],pack("n",length($_[1])),$_[1]);
    local($fh,$tresp,$resp);

    if ($options &amp; &amp;main'RES_USEVC) { # 
	$fh = $nstcp{$sin};
	if (!$fh) {
	    $fh = $sockcnt++;

	    if (&amp;debug) {
		local($,) = (".");
		print "Connecting to ",(unpack("S n C4",$sin))[2..5,1],"\n";
	    }

	    socket($fh,&amp;main'AF_INET,&amp;main'SOCK_STREAM,&amp;main'PF_UNSPEC) ||
		die "socket: $!";
	    connect($fh, $sin) || die "connect: $!";

	    $nstcp{$sin} = $fh;
	}

	if ($packet) {
	    if (&amp;debug) {
		print "Sending to ",join(".",(unpack("S n C4",$sin))[2..5,1])
		    ," via TCP\n"; # 
	    }

	    send($fh,$len,0);
	    send($fh,$packet,0);
	}

	if (&amp;debug) {
	    print "Receiving from ",join(".",(unpack("S n C4",$sin))[2..5,1])
		," via TCP\n";
	}


	$len = &amp;sysread_nchars($fh,2);
	if ($len = unpack("n",$len)) {
	    print "Got a size (size=$len)\n" if (&amp;debug);
	    $resp = &amp;sysread_nchars($fh,$len);
	}

	print "Got a response (size=",length($resp),")\n" if (&amp;debug);

	if (! ($options &amp; &amp;main'RES_STAYOPEN)) {
	    foreach (keys(%nstcp)) {
		if (&amp;debug) {
		    local($,) = (".");
		    print "Disconnecting from ",(unpack("S n C4",$_))[2..5,1]
			,"\n";
		}

		close($_);
	    }
	    %nstcp = ();
	}
    } else {
	print "Sending to ",join(".",(unpack("S n C4",$sin))[2..5,1])
	    ," via UDP\n" if &amp;debug;

	send(NSUDP,$packet,0,$sin) || die "send: $!";

	print "Receiving from ",join(".",(unpack("S n C4",$sin))[2..5,1])
	    ," via UDP\n" if &amp;debug;

	$len = 512;
	recv(NSUDP,$resp,$len,0) || die "recv: $!";

	print "Got a response (size=",length($resp),")\n" if (&amp;debug);
    }

    &amp;parse_response($resp);
}

$localns = "";

sub main'res_search { # @_ = ($name, $type, $class)
    local($name, $type, $class) = @_;
    local($q, @ans, $dom);

    $localns = &amp;main'res_open() if !$localns;

    $q = &amp;main'res_mkquery(@_);
    @ans = &amp;main'res_send($localns, $q);

    if ($ans[3] &amp;&amp;
	(substr($name,-1,1) ne ".") &amp;&amp;
	(($options &amp; &amp;main'RES_DEFNAMES) ||
	 ($options &amp; &amp;main'RES_DNSRCH))) {
	
	$q = &amp;main'res_mkquery($name.".".$domain,$type,$class);
	@ans = &amp;main'res_send($localns, $q);
    }
    if ($ans[3] &amp;&amp;
	($options &amp; &amp;main'RES_DNSRCH)) {
	$dom = $domain;

	while($ans[3]) {
	    $dom =~ s/^[^\.]+\.//;
	    last if ((&amp;main'LOCALDOMAINPARTS-1 &gt; ($dom =~ tr/\./\./)) ||
		     !$dom);
	
	    $q = &amp;main'res_mkquery($name.".".$domain,$type,$class);
	    @ans = &amp;main'res_send($localns, $q);
	}
    }

    @ans;
}

sub main'res_search_simple {
    local(@ans) = &amp;main'res_search;

    if ($ans[3]) {
	return($;.$ans[3]);
    }

    local(@s, $ret);

    for (@ans[$ans[6]..($ans[6]+$ans[7]-1)]) {
	@s = split(' ',$_,5);
	$ret .= $s[4].$; if ($s[1] eq $_[1]) &amp;&amp; ($s[2] eq $_[2]);
    }
    chop $ret;

    return($ret);
}

sub parse_response { # @_ = ($response)
	local(@resp);

	push(@_,0);

#	open(CV,"|cat -v");print CV @_;close(CV);print"\n";

	($id,$bits,$qdcount,$ancount,$nscount,$adcount) =
	    unpack("n6",&amp;next_chars(12,@_));

	$auth = ($bits &gt;&gt; 10) &amp; 0x01;
	$recurse = ($bits &gt;&gt; 8) &amp; 0x01;
	$rcode = $bits &amp; 0x0f;
	if ((!$auth) &amp;&amp; ($options &amp; &amp;main'RES_AAONLY)) {
	    ($ancount,$nscount,$adcount) = (0,0,0);
	}

	$rrs = $ancount+$nscount+$adcount;
	@resp = ($id,$auth,$recurse);			#	[0..2]
	push(@resp,&amp;rcode_text($rcode));		#	[3]
#	push(@resp,$rcode);
	push(@resp,12,$qdcount);			#	[4,5]
	push(@resp,$resp[$#resp-1]+$resp[$#resp],$ancount);   # [6,7]
	push(@resp,$resp[$#resp-1]+$resp[$#resp],$nscount);   # [8,9]
	push(@resp,$resp[$#resp-1]+$resp[$#resp],$adcount);   # [10,11]
	
	while($qdcount--) {
	    push(@resp,join(" ",&amp;parse_name,
			    &amp;qtype_numtostr(&amp;next_netshort),
			    &amp;qclass_numtostr(&amp;next_netshort)));
	}
	while($rrs--) {
	    @new = &amp;parse_rrbits;
	    if (@new) {
		push(@resp,@new);
	    } else {
		warn "Malformed packet: missing resource records.\n";
		last;
	    }
	}
	return(@resp);
}

sub parse_name {
	local($ch,$name) = (substr($_[0],$_[1],1));
	while (ord($ch = substr($_[0],$_[1],1)) != 0) {
		# Message compression (RFC1035 4.1.4)
		if (ord($ch) &gt;= 0xc0) {
			return($name.
			       &amp;parse_name($_[0],&amp;next_netshort &amp; 0x3fff));
		}
		$name .= &amp;next_str.".";
	}
	&amp;skip_char;		# move past \0
	$name = ".." if ($name eq "");
	chop($name);  # remove trailing "."
	return($name);
}

sub parse_rrbits {
	local($rrec,$type,$tstr,$rdlen,$pfct);

#	print STDERR "--\n",substr($_[0],0,$_[1]),"\n--\n",substr($_[0],$_[1]),"\n--\n";

	if (length($_[0]) &lt;= $_[1]) {
	    return();
	}

	$rrec = &amp;parse_name;					# NAME
	$tstr = &amp;qtype_numtostr($type=&amp;next_netshort);
	$rrec .= " ".$tstr;					# TYPE
	$rrec .= " ".&amp;qclass_numtostr(&amp;next_netshort);		# CLASS
	$rrec .= " ".&amp;next_netlong;				# TTL (integer)

	$rdlen = &amp;next_netshort;
	$pfct = "rrparse_".$tstr;
	if ($pfct =~ /^rrparse_\d*$/) {
	    warn "Unknown query type \"$type\" in parse_rrbits, using NULL\n";
	    $pfct = "rrparse_NULL";
	}

	$rrec .= " ".&amp;$pfct(@_,$rdlen);			# RDATA
}

# strips the first character-string from the argument, and returns it as a
# perl string
sub next_str {
    local($cslen);
    $cslen = unpack("C",&amp;next_chars(1,@_));
    &amp;next_chars($cslen,@_);
}

# returns the first $_[0] chars at position $_[2] in string $_[1]
# and increments $_[2]
sub next_chars {
    $_[2] += $_[0];
    substr($_[1],$_[2]-$_[0],$_[0]);
}

# get the next network byte order short/long from the input

sub next_netshort {
    $_[1] += 2;
    unpack("n",substr($_[0],$_[1]-2));
}

sub next_netlong {
    $_[1] += 4;
    unpack("N",substr($_[0],$_[1]-4));
}

# skip a character
sub skip_char {
    $_[1]++;
}

# routines to parse apart the rrdata

sub rrparse_A {
    join('.',unpack("C4",&amp;next_chars(4,@_)));
}

sub rrparse_NS {
    &amp;parse_name;
}

sub rrparse_MD {
    &amp;parse_name;
}

sub rrparse_MF {
    &amp;parse_name;
}

sub rrparse_CNAME {
    &amp;parse_name;
}

sub rrparse_SOA {
    join(' ',
	 &amp;parse_name,
	 &amp;parse_name,
	 &amp;next_netlong,
	 &amp;next_netlong,
	 &amp;next_netlong,
	 &amp;next_netlong,
	 &amp;next_netlong);
}

sub rrparse_MB {
    &amp;parse_name;
}

sub rrparse_MG {
    &amp;parse_name;
}

sub rrparse_MR {
    &amp;parse_name;
}

sub rrparse_NULL {
    &amp;next_chars(@_[2,0,1]);
}

sub rrparse_WKS {
    local(@resp,$bitmap,$len,%bit);

    push(@resp,
	 &amp;rrparse_A,
	 ord(&amp;next_chars(1,@_)));
    $bitmap = &amp;next_chars($_[2]-5,@_);
    @bit{0..(length($bitmap)*8)} = split(//,unpack("B*", $bitmap));
    push(@resp, grep($bit{$_},keys(%bit)));

    join(' ',@resp);
}

sub rrparse_PTR {
    &amp;parse_name;
}

sub rrparse_HINFO {
    &amp;next_str.",".&amp;next_str;
}

sub rrparse_MINFO {
    &amp;parse_name.",".&amp;parse_name;
}

sub rrparse_MX {
    &amp;next_netshort." ".&amp;parse_name;
}

sub rrparse_TXT {
    local($str,$end);
    $end = $_[1]+$_[2];
    while ($_[1] &lt; $end) {
	$str.=&amp;next_str;
    }
    if ($_[1] &gt; $end) { print STDERR "Something weird in rrparse_TXT\n"; }
    return($str);
}

sub rrparse_UINFO {
    &amp;next_chars(@_[2,0,1]);
}

sub rrparse_UNSPEC {
    &amp;next_chars(@_[2,0,1]);
}

sub rrparse_UNSPECA {
    &amp;next_chars(@_[2,0,1]);
}

1;
</pre></body></html>