in reply to How to create DNS packet

Hello Everyone! Another Question, when we send the DNS packet, how does its query format look like, i mean does it look like e.g "3www6google3com011" or like "wwwgooglecom011"? Please help! when i send a packet, in wireshark, it shows the Query name like "3.www.6.google.3.com"! any one know y?? i packed it like this (as suggested by others):
my $hostname = $ARGV[0]; my @labels; my $nlabels = 0; for (split /\./, $hostname) { push @labels, length, $_; $nlabels++; } my $n = scalar(@labels); my $question = pack("(C/a*)$n C n2", @labels, 0, 1, 1) ;
or used this one:
my @labels = split(/\./, $hostname) ; my $n = scalar(@labels) ; my $question = pack("(C/a*)$n C n2", @labels, 0, 1, 1) ;

Replies are listed 'Best First'.
Re^2: How to create DNS packet
by broomduster (Priest) on Nov 18, 2008 at 20:19 UTC
Re^2: How to create DNS packet
by almut (Canon) on Nov 18, 2008 at 19:35 UTC

    Use "C a*" when supplying length/label pairs in @labels (as in the first snippet), or "C/a*" when supplying labels only (as in the second snippet). In the latter case, the length bytes will automatically be generated.  (Note that the length bytes (when < 32) might map to non-printable control characters if you try to print them as ASCII... so you probably want to visualize the data in hex representation.)

    I'm no expert in DNS packet format nitty gritties, but the latter snippet looks okay to me...  What exactly doesn't work; any error messages?

Re^2: How to create DNS packet
by gone2015 (Deacon) on Nov 18, 2008 at 19:59 UTC
    in wireshark, it shows the Query name like "3.www.6.google.3.com"

    I think wireshark is being helpful, and showing the label lengths in human readable form, using '.' as a separator (it won't, after all, appear in any label !)

    This is a little crude:

    sub show { return join('', map { ord($_) > 0x3F ? $_ : sprintf('\\x%02X', ord +($_)) } split(//, $_[0])) ; } ; my $hostname = 'much.ado.about.not.alot.org' ; my @labels = split(/\./, $hostname) ; my $n = scalar(@labels) ; my $question = pack("(C/a*)$n C n2", @labels, 0, 1, 1) ; print "\"", show($question), "\"\n" ;
    but gives:
      "\x04much\x03ado\x05about\x03not\x04alot\x03org\x00\x00\x01\x00\x01"
    
    which looks right to me.