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

I typed it in from the front inside cover of my Stevens book (TCP/IP Illustrated Volume 1).

Wikipedia has it too: http://en.wikipedia.org/wiki/IPv4, but the real gospel is RFC791: http://www.ietf.org/rfc/rfc791.txt.

Update: Other protocol standards you might like:
RFC 768 User Datagram Protocol
RFC 792 Internet Control Message Protocol
RFC 793 Transmission Control Protocol

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

Replies are listed 'Best First'.
Re^4: Need help with (un)pack templates
by Subop (Acolyte) on Dec 20, 2009 at 14:23 UTC

    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?

      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/
        Now it's making more sense... Thanks for the help, gmargo!