ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
July 17th, 2024 | 10:00am-11:55am SGT (UTC+8) | Online

Ethereal-dev: Re: [ethereal-dev] How to handle compiler padding?

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Ashok Narayanan <ashokn@xxxxxxxxx>
Date: Mon, 6 Dec 1999 17:43:39 -0500 (EST)
The only guaranteed way to avoid compiler padding (that I know of,
anyway) is to avoid using non-byte values in structures. Make the
holding_timer and pdu_length structures into guint8[2] instead of
guint16. 

The functions pntohs|l are useful in this context, because you can do
something like this

	...
	guint8 holding_timer_array[2]

... and then

	i = pntohs(&struct->holding_timer_array[0]);

If you're lucky enough to have a structure where the multibyte
primitives start on 32-bit boundaries (and you're running on a 32-bit
machine) then you may be in luck. Doesn't seem true in your case,
however - I count 7 bytes in your structure before holding_timer.

-Ashok

Stuart Stanley writes:
 > 
 > Hello,
 > 
 > I am working on adding IS-IS protocol unpacking.  Given that OSI loves to
 > use lots of packed bytes, I ran into a packing problem pretty much right
 > off the bat.  The question is, what is the "official" way for doing
 > structure overlays onto the PDU without having the compiler play with
 > the alignment.  For example:
 > 
 > typedef struct {
 >         guint8  isis_hello_circuit_reserved;    /* circuit type & reserved */
 >         guint8  isis_hello_source_id[6];        /* source id */
 >         guint16 isis_hello_holding_timer;       /* holding timer */
 >         guint16 isis_hello_pdu_length;          /* full length, includ. hdr */
 >         guint8  isis_hello_priority_reserved;   /* priority & reserved */
 >         guint8  isis_hello_lan_id[7];           /* LAN id */
 > } isis_hello_t;
 > 
 > Later, I was planning on doing a (in a dissector)
 > 
 > 	isis_hello_t	*ihd;
 > 
 > 	ihd = (isis_hello_t) *pd;
 > 
 > 
 > Now, when I access ihd->isis_hello_holding_timer, I am missing an octet.
 > After playing around, I found isis_hello_t has an extra octet stuck
 > between the source_id and the holding timer.  ooops.
 > 
 > I can see a couple of routes to do this, but what is the best "style"
 > for this.  I guess on a similar note, which dissector would make a good
 > example of current good dissector coding style?  
 > 
 > Thanks for the great framework on this, BTW!
 > 
 > 
 > 
 >  Life is something set to music; I can hear it     | Stuart Stanley
 >  when I'm sad. There's a chord in every muscle,    | stuarts@xxxxxxxxxx
 >  every kiss you've ever had -- Heather Nova        | Lucent Technologies,
 >                                                    | Eden Prairie, MN
 >                                                    | 612.996.6829
 > 

-- 
--- Asok the Intern ----------------------------------------
IOS Network Protocols, Cisco Systems
250 Apollo Drive, Chelmsford, MA 01824
Ph: 978-244-8387