-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Sep 30, 2005 at 11:32:36PM -0700, Angus Lees wrote:
> 
> Since you dared me, here's some (untested) perl5 code that will parse
> a TCP header, including network byte order conversions and bitfields:
> 
>  my ($source, $dest, $seq, $ack_seq,
>      $off, undef, undef, $urg, $ack, $psh, $rst, $syn, $fin,
>      $window, $check, $urg_ptr) = unpack 'n2 N2 C B8 n3', $tcpdata;
> 
>  $off &= 0x0F;

Pack is fine for a fixed structure but TCP inside an IP packet does
not have a fixed position (IP options) so you get:

   unpack( '.....', substr( $packet, $offset ))

Then you are tracking partial unpack() templates plus offsets and
copying chunks of data back and forth. Consider IP fragments.
Of course it can be done, but making good use of structs and pointers
actually makes it easier (as well as faster).

Keeping track of structured data is just something you have to do
and high level languages only partially shield you from it.
The same problem comes up in binary database records, graphics file
formats and similar places. 

        - Tel
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQIVAwUBQz/HQsfOVl0KFTApAQIawQ/7Bv/3lB6etPaCi+W2HkJeMGCdGANVZhKy
qQVLQoNdY9z5z2KbYCrZ5JzYHQok5fEFbV4u4goi/G+UpyVJSDFxP5KBZjBPmM1y
ehu+u5aRW3K3eT4CBxMNw/9hnecgq9tO2XCjqdm0bBOiANLDSDwIo4oIMQnCztok
wfOBMntU5CHAg5u0llMYS692lbTCmYRyvER1SeNZklv1181G7OG2EVNMftoihei9
NnmS4w5dvxaAbAeHGJLfVxnjWVSU1eOBGgyiBksGKSTobS46FCiM/+qOJVk/rQEw
Ok/Pyp/nBdJM9vtMShB9Fb4ENbAVldaso+4QScjzJbSDS3cVbtfPk6VZM/VEFraL
Gjtrc3b6+8nsqmKCGWR+wJUMGxWNwEkFa1GVaBctaLsuLGRiCMIpMNDUbhsRtG5O
gTkz0taWBRfY1Bjqm5RxSSrCzLr+n4DYCiouT4m9YQDttORYmxpBW4AglvFDdnMy
Bqb1K+ebUEmXX+MHbPcVugA2PfvGkf95G/CD0lRBWyi0V5OS7CFGgBpj5FxaQoJw
BEd1ISN2s7IVzrxB+rjVrfDVZ00/dz7MzOo2gPUZs4rz8NBAZEuuJDBxcJh5Lsye
bmimBNY3pv69LOSlxuJtvZXgZFh3kDvvYBEdOEraowIpSh/GSvsGwec+elpBlq3G
WJKWTVxQeF4=
=ovmz
-----END PGP SIGNATURE-----
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to