tokhi has asked for the wisdom of the Perl Monks concerning the following question:

I am sending a request to DNS server and I think I am getting the packed reply back from the server,but I don't know how to unpack the reply and print it on the screen, pleae help , thanks
#!/usr/bin/perl use IO::Socket; &dnsQueryPacket($ARGV[0],$ARGV[1]); sub dnsQueryPacket{ my $hostname = $_[0]; # my @labels = split(/\./, $hostname) ; my $n = scalar(@labels) ; +my $question = pack("(C/a*)$n C n2", @labels, 0, 1, 1) ; my @labels; my $nlabels = 0; my @labels = split(/\./, $hostname) ; my $n = scalar(@labels) ; my $question = pack("(C/a*)$n C n2", @labels, 0, 1, 1) ; $header1 = &dnsheader; $server = '130.149.2.12'; $sock = new IO::Socket::INET (PeerAddr=>$server,PeerPort=>'53',Proto=>'udp') or die "Can't Connect" +; #sending request $sock->send($header1.$question); #receiveing request $sock->recv($buf,512); close($sock); } sub dnsheader{ $id = 1000; $qr_Opcode_aa_tc_rd = '00000000'; $ra_Z_Rcode = '00000000'; $Qdcount = 1; $Ancount = 0; $Nscount = 0; $Arcount = 0; $header = pack("n,b8,b8,n4",$id++, "$qr_Opcode_aa_tc_rd",#qr,opcode,aa,tc,rd fields "$ra_Z_Rcode",#ra,Z,Rcode fields "$Qdcount",#one question(Qdcount) "$Ancount",#no answers (ancount) "$Nscount", "$Arcount");#no addtl rr's return $header; }

Replies are listed 'Best First'.
Re: how to get reply from dns server
by Corion (Patriarch) on Nov 19, 2008 at 16:51 UTC
Re: how to get reply from dns server
by mr_mischief (Monsignor) on Nov 19, 2008 at 17:03 UTC
    You probably want to check both the relevant RFCs and books on how to implement network software. There are also several DNS modules on the CPAN. Net::DNS::ToolKit even provides tools for working directly with DNS packets. If there's a particular motive for reinventing this particular wheel, that's cool. If not, the modules are probably a better use of your time.

    ISBN 9780201615715, ISBN 9780131411555 (I actually have the older version of this ISBN 9780134900124 that still has XTI information as well), ISBN 9780201633467, and ISBN 9781565925724 are the books to which I refer most for networking topics.

Re: how to get reply from dns server
by gone2015 (Deacon) on Nov 20, 2008 at 00:24 UTC

    Is this coursework ? Recent thread 724155 appears to have a similar question, towards the end.

      And to think.... you so helpfully did their homework for them.

        It's tricky to judge, isn't it :-(

        I tried to help only with Perl features that would contribute to a solution... The sort of thing not obvious if you're, say, a 'C' programmer. The first poster nearly had the packing piece.

        ...if you think the fragments of code I posted are a complete solution, then you don't know the scale of the problem :-)