in reply to How to create a DNS message in perl?

How can can i convert it into the hexa value of 2 octets which is '0001'

pack. Specifically, pack('n', 1).

The $QNAME = "www.google.com". How can i convert it into the hexa value '0377777706676F6F676C6503636F6D00'.

Each segment of the domain is passed as a byte indicating the length of the string followed by the string itself. The whole is ended with a NUL byte (pack('C', 0) or "\x00").

0377777706676F6F676C6503636F6D00 03 777777 3 www 06 676F6F676C65 6 google 03 636F6D 3 com 00 0

Update: Fixed with better understanding.

Replies are listed 'Best First'.
Re^2: How to create a DNS message in perl?
by sanjay nayak (Sexton) on Jul 30, 2008 at 07:42 UTC

    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

      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