# $Id: hesiod.pl,v 1.1 2008/01/04 00:52:04 root Exp root $

package hes;

sub make_domain {
    $d = $_[0].$_[1];
    $d .= "." if $d !~ /\.$/;
}

sub get_domain {
    return($domain) if $domain;

    if (open(CONF,"/etc/hesiod.conf")) {
	while(<CONF>) {
	    chop;
	    $lhs = $' if /^lhs=/;
	    $rhs = $' if /^rhs=/;
	}
	close(CONF);
    } else {
	($lhs,$rhs) = (".ns",".athena.mit.edu");
    }

    $domain = &make_domain($lhs,$rhs);
}

sub get_lhs {
    return($lhs) if $lhs;

    &get_domain();

    die "\$lhs is false\n" if !$lhs;

    return($lhs);
}

sub hes_resolve {
  my($name,$type,$dom) = @_;
    local(@ans,@fields);

    if (! $fh) { $fh = &main'res_open(); }
    ($name,$dom) = ($`,$') if $name =~ /@/;
    $dom = $dom?&make_domain(&get_lhs(),".".$dom):&get_domain();

    $query = &main'res_mkquery($name.".".$type.$dom,"TXT","HS");
    @repl = &main'res_send($fh, $query);
    for (0..$repl[7]-1) {
	@fields = split(/ /,$repl[$repl[6]+$_],5);
	push(@ans,$fields[4]) if ($fields[1] eq "TXT" && $fields[2] eq "HS");
    }
    return(@ans);
}
