in reply to Re^3: Need help with (un)pack templates
in thread Need help with (un)pack templates

I have that same book and just see the IP header format. How do you know what letters to use in the (un)pack template just by looking at the header format?

  • Comment on Re^4: Need help with (un)pack templates

Replies are listed 'Best First'.
Re^5: Need help with (un)pack templates
by gmargo (Hermit) on Dec 20, 2009 at 14:39 UTC

    It's pretty well spelled out in the pack documentation. C for unsigned char (byte), n for unsigned 16-bit short in network order, N for 32-bit long in network order, a for "arbitrary binary data".

    Don't be confused by the 32-bit CCCC's, since an IP address is 4 8-bit numbers, not a 32-bit number.

      Don't be confused by the 32-bit CCCC's, since an IP address is 4 8-bit numbers, not a 32-bit number.

      It's actually 32 opaque bits. The dotted form is just a representation. The separation into bytes in completely arbitrary. But if you're going to display in that form, it might be useful to extract the IP as 4 bytes.

      an IP address is 4 8-bit numbers, not a 32-bit number
      s/is/is normally represented as/;

      $ host perlmonks.org perlmonks.org has address 66.39.54.27 $ perl -le 'print ((66 * 256**3)+(39 * 256**2)+(54 * 256)+27);' 1109866011 $ perl -le 'print unpack('N',(gethostbyname("perlmonks.org"))[4]);' 1109866011
      Many web browsers accept the decimal value of the IP address: http://1109866011/

        s/is normally represented as/is encoded in an IP header as/; # :-)

      Now it's making more sense... Thanks for the help, gmargo!