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: Guy Harris <guy@xxxxxxxxxx>
Date: Mon, 6 Dec 1999 14:39:39 -0800 (PST)
> 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.

I'm not sure there's an official way.

Some alternatives:

	1) don't overlay a structure; instead, have a pile of #defines,
	   say, for the offsets into the packet, and do, say

		xxx = pntohs(&pd[OFFSET_OF_2_OCTET_FIELD_IN_QUESTION]);

	   to extract the value;

	2) do something such as

typedef struct {
        guint8  isis_hello_circuit_reserved;    /* circuit type & reserved */
        guint8  isis_hello_source_id[6];        /* source id */
        guint8  isis_hello_holding_timer[2];    /* holding timer */
        guint8  isis_hello_pdu_length[2];       /* full length, includ. hdr */
        guint8  isis_hello_priority_reserved;   /* priority & reserved */
        guint8  isis_hello_lan_id[7];           /* LAN id */
} isis_hello_t;

and do something such as

		xxx = pntohs(ihd->isis_hello_holding_timer);

	   to extract the value.

Note that, in *any* case, unless IS-IS specifies that the byte order of
multi-byte integral quantities is always the byte order of the machines
sniffing the network (which is a bit unlikely - unless there are
machines of different byte orders sniffing the network, in which case
it's a bit impossible...), you need to use "pntoh[sl]()" or
"pletoh[sl]()" *anyway* to extract multi-byte integral quantities, so
that

	1) you treat them as being in the appropriate byte order;

	2) you don't blow up if the field doesn't happen to be aligned
	   on the right boundary (even if the packet format aligns
	   stuff, there's no guarantee that the headers, etc. *before*
	   it are so aligned).