Ethereal-dev: Re: [Ethereal-dev] Buffers and pointers in a dissector

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

From: "Ronnie Sahlberg" <ronnie_sahlberg@xxxxxxxxxxxxxx>
Date: Sat, 23 Nov 2002 03:42:23 +1100
From: "Marco Molteni"
Sent: Saturday, November 23, 2002 12:08 AM
Subject: Re: [Ethereal-dev] Buffers and pointers in a dissector


> On Thu, 21 Nov 2002, Gerald Combs <gerald@xxxxxxxxxxxx> wrote:
>
> > On Thu, 21 Nov 2002, Paul Smith wrote:
>
> [..]
>
> > > struct udp
> > > {
> > >     unsigned short source_port;
> > >     unsigned short destination_port
> > >     unsigned short length;
> > >     unsigned short checksum;
> > > }
> > >
> > > struct udp_header *udp_hdr;
> > >
> > > udp_hdr=(struct udp_header*)data_buffer
> >
> > This will fail if:
> >
> >   - One of the structure members isn't properly aligned on a 32-bit
> >     boundary, and your processor doesn't like that, e.g. SPARC or
> >     MIPS.  Alphas and PowerPCs might be subject to this as well.
>
> Gerald,
>
> I have this same question since I read README.developer, so I'll jump in
> to ask.
>
> what's wrong with this:
>
> struct foo {
> ....
> } __attribute__ packed
>
> I think the only problem is that __attribute__ is a ggcism and is not
> portable, right?

Even if __attribute__ could be used by all compilers supported be ethereal
it would still
not help.
The attribute will only really control the internal padding of a structure
layout.
I will not by magic convert
> > > udp_hdr=(struct udp_header*)data_buffer
into a 4 byte aligned address.

Some CPUs just can not handle non-aligned accesses.

I.e.  since the strucure in the example contains uint16s
data_buffer had better be aligned on a 16bit boundary or there will be a
coredump.

It is not possible to guarantee that data_buffer is aligned in any special
way, so
that is why one can not just cast it.

>
> >   - Your processor's host byte order differs from network byte order,
> >     e.g. IA32.
>
> Should be enough to always use ntohs() and ntohl(), right?

Yes,   fill in the structure manually, element by element using
tvb_get_ntoh[sl]()
Only way to do it.