in reply to Re: How to create a DNS message in perl?
in thread How to create a DNS message in perl?


Hi Ikegami

Thanks for reply.

pack('n',1) encodes the data as '0001'. But how can can i convert $QTYPE=23 to the hexa value of 2 octets which is '0023'. If i use pack('n', 23) then it enocdes it as '0017'. Plz suggest.

The second thing is that I am extracting the domain name from the Req URI of a SIP message received. By using which i am sending the DNS request.Suppose sometimes i am getting 'www.google.com' ,

sometimes i am getting 'www.yahoo.com' or anything else in the SIP message which is not fixed. So is there any function in perl which directly convert the domain name into the required hexa format so that wireshark will decode it properly without doing any manual countings of characters in each segment of the domain name.

Actually what i am wanting is as follows. I am getting the domain as 'www.google.com'. After putting it in the argument in any perl function it converts it into the '0377777706676F6F676C6503636F6D00' without doing any further things. So that i can use that variable along with other variables while sending the DNS request.Plz suggest

Regd's
Sanjay
  • Comment on Re^2: How to create a DNS message in perl?

Replies are listed 'Best First'.
Re^3: How to create a DNS message in perl?
by ikegami (Patriarch) on Jul 30, 2008 at 09:11 UTC

    But how can can i convert $QTYPE=23 to the hexa value of 2 octets which is '0023'

    Wrong question.

    You need to learn how to store 2316 into $QTYPE. Right now, you are storing 2310, a completely different number.

    $QTYPE = 0x23; # 35

    If you're starting with a string, you can use hex or oct.

    $QTYPE = hex('23'); # 35 $QTYPE = oct('0x23'); # 35

    So is there any function in perl which directly convert the domain name into the required hexa format

    There's no builtin function. I've specified the format (from which a solution is easily derived using split, length, and pack 'C') in an earlier post. Furthermore, someone gave you the name of a module that supposedly does the work for you.


      Hi
      Thanks for the reply.Acording to your suggestion i have written the code as follows.
      my $QNAME= "www.google.com"; my ($QNAME1, $QNAME2,$QNAME3)= split(/\./,$QNAME); my ($len1,$len2, $len3)=(length($QNAME1),length($QNAME2),length($QNAME +3));

      Then how to use the pack(('C',0)or "\x00") for in this case.So that it deocded the value as '0377777706676F6F676C6503636F6D00'.

      Plz suggest.

      Regd's
      Sanjay

        That won't work for www.some.domain.com. Use a loop.

        my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ) ) { $QNAME_packed .= pack( 'C', length($part) ) . $part; } $QNAME_packed .= pack('C', 0)

        Or

        my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ) ) { $QNAME_packed .= pack( 'C/a*', $part ); } $QNAME_packed .= pack('C', 0)

        Or

        my $QNAME= "www.google.com"; my $QNAME_packed = ''; for my $part ( split( /\./, $QNAME ), '' ) { $QNAME_packed .= pack( 'C/a*', $part ); }

        Or if requiring Perl 5.8 is acceptable

        my $QNAME= "www.google.com"; my $QNAME_packed = pack( '(C/a*)*', split( /\./, $QNAME ), '' ) );