Thanks. Follow up question, on performance:
Which method is preferable:
(1)
struct some_hdr hdr;
tvb_memcpy(tvb, (guint8 *)&hdr, offset, sizeof(hdr));
proto_tree_add_X(...,hdr.a);
proto_tree_add_Y(...,hdr.b);
-or-
(2)
struct some_hdr hdr;
hdr.a = tvb_get_X();
hdr.b = tvb_get_Y();
proto_tree_add_X(...,hdr.a);
proto_tree_add_Y(...,hdr.b);
Or, to put it in other words, should I copy the header with one function,
then work on it 'locally', or extract each part of it individually? How
expensive are multiple tvb_get's, against one tvb_memcpy() (in general, I
can only assume it depends on the header size and possibly complexity). I
tend to see the second method preferable (for example, when dealing with a
cut packet) ,but I'm not sure performance-wise which is better.
Thanks in advance,
Y.
> -----Original Message-----
> From: ethereal-dev-admin@xxxxxxxxxxxx
> [mailto:ethereal-dev-admin@xxxxxxxxxxxx]On Behalf Of Guy Harris
> Sent: Wed, August 14, 2002 7:58 PM
> To: Yaniv Kaul
> Cc: Ethereal-Dev@xxxxxxxxxxxx
> Subject: Re: [Ethereal-dev] ntohl(), pntohl(), g_ntohl() ?
>
>
> On Wed, Aug 14, 2002 at 06:59:44PM +0200, Yaniv Kaul wrote:
> > What should I be using?
>
> You left one out - "tvb_get_ntohl()".
>
> That's the best one to use. If you use "tvb_get_ptr()" and then extract
> a bunch of stuff by hand, that throws an exception unless the *entire*
> structure on which you're doing the "tvb_get_ptr()" is available in the
> frame, but if you get each individual field with, for example,
> "tvb_get_ntohl()", the exception won't be thrown until you fetch a field
> that's not available.
>
> That means that if you have, for example, a short frame (captured with a
> snapshot length less than the length of the frame), you will get more
> stuff in the protocol tree.
>
> Of the alternatives:
>
> ntohl():
> requires that <netinet/in.h> (on UNIX) or <winsock2.h>
> (on Windows) be included, and Joerg Mayer is trying to
> get rid of those
>
> is passed a 32-bit integral value, but there's no
> guarantee that a structure pointer set from
> "tvb_get_ptr()" is properly aligned, so if you do
>
> structp = tvb_get_ptr(tvb, offset, sizeof (struct));
>
> foo = ntohl(structp->field);
>
> you run the risk of getting an alignment fault on many
> platforms (including the one I use at work, namely
> SPARC/Solaris)
>
> g_ntohl():
> doesn't require that <netinet/in.h> (on UNIX) or
> <winsock2.h> (on Windows) be included
>
> is passed a 32-bit integral value, but there's no
> guarantee that a structure pointer set from
> "tvb_get_ptr()" is properly aligned, so if you do
>
> structp = tvb_get_ptr(tvb, offset, sizeof (struct));
>
> foo = ntohl(structp->field);
>
> you run the risk of getting an alignment fault on many
> platforms (including the one I use at work, namely
> SPARC/Solaris)
>
> pntohl()
> doesn't require that <netinet/in.h> (on UNIX) or
> <winsock2.h> (on Windows) be included
>
> is passed a pointer, and dereferences it a byte at a
> time, so it's not subject to alignment restrictions
>
> so I'd vote for "pntohl()" unless you know *for certain* that the value
> you're passing is aligned on the right boundary, in which case I'd vote
> for "g_ntohl()".
> _______________________________________________
> Ethereal-dev mailing list
> Ethereal-dev@xxxxxxxxxxxx
> http://www.ethereal.com/mailman/listinfo/ethereal-dev
>