pack and unpack problems

Justin Johnson justinjohnson at fastmail.fm
Wed Jun 18 19:51:59 CEST 2003


Thanks a bunch.  That is very helpful.

On Wed, 18 Jun 2003 09:31:33 -0600, "Steven Taschuk"
<staschuk at telusplanet.net> said:
> Quoth Justin Johnson:
> > Would you mind explaining this for me?  What do you mean by check
> > alignment?  Sorry, this is kinda new for me.  In the meantime I'll go
> > read my orielly tcp book.  :-)
> 
> Some architectures constrain (some) values to occur "aligned" at
> certain boundaries in memory.  Structs on such platforms might
> need padding between their members to ensure proper alignment;
> e.g., instances of
>     struct foo {
>         char a;
>         int b;
>     }
> might be organized in memory by the C compiler as
>     0     1     2     3     4     5     6     7     8
>     |  a  |     padding     |           b           |
> where the padding makes sure b is aligned properly, but is not
> otherwise used.  (Obviously here the whole struct must also be
> aligned.)
> 
> But if you're parsing UDP packets, you know the exact layout and
> don't care what the platform's C compiler would do with a struct
> declaration.  Thus you want '!' for "standard alignment", which is
> to say, no alignment required at all.  ('!' also implies network
> byte order; see the docs.)
> 
> (Header layouts are, I suspect, designed with this kind of thing
> in mind, so values are aligned in sensible ways anyway.  Thus
> "parsing" a UDP packet in C could be as simple as casting a void *
> to a struct udpheader *.)
> 
> -- 
> Steven Taschuk                                    
> staschuk at telusplanet.net
> Receive them ignorant; dispatch them confused.  (Weschler's Teaching
> Motto)
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list