Ethereal-dev: Re: [Ethereal-dev] Clarification needed

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

From: Guy Harris <gharris@xxxxxxxxx>
Date: Tue, 9 Jul 2002 21:22:21 -0700
On Tue, Jul 09, 2002 at 05:27:25PM +0200, Michael Tuexen wrote:
> First of all I have the impression that i should use
> 
> proto_item*
> proto_tree_add_item(tree, id, tvb, start, length, little_endian);
> 
> whenever possible compared to the more specific ones like
> 
> proto_item *
> proto_tree_add_uint(tree, id, tvb, start, length, value);
> 
> Is that correct?

Well, "proto_tree_add_item()" does more of the work for you, so it's a
bit more convenient.

Also, you don't have to write the starting offset twice (once in the
"proto_tree_add_" call and once in the call to fetch the data to add),
and don't have to write the length twice if it's not implicit in the
call to fetch the data, so it might be less error-prone (although one
could also forget that the last argument to "proto_tree_add_item()" is a
Boolean rather than a value; several of us have done that at times).

> However, I decided to use proto_tree_add_item. So the text says that
> the last argument is a flag describing little/big endian. I'm assuming
> that describes the format of the item in the tvb.

Yes.

> This would mean for
> IETF protocol it is always big endian. If this is correct I would suggest
> to introduce two constants, one for big one for little endian.

Yes, we could define the big-endian one as TRUE and the little-endian
one as FALSE.

> Assuming that this is correct I do not understand the example given in 
> the file:
> 
>          guint8 th_0 = tvb_get_guint8(tvb, offset);
>          proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, 
> th_0);
> 
> I would have expected
>          proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, TRUE 
> or FALSE);

As I said above, "one could also forget that the last argument to
"proto_tree_add_item()" is a Boolean rather than a value; several of us
have done that at times".   I guess this is one of those times. :-)

I'll update the documentation.

> So why do I need to extract the value of th_0 and how is that related to 
> to the byte order? Is the byte order coded in the th_0?

No, the example is just wrong.

The correct examples would be

         guint8 th_0 = tvb_get_guint8(tvb, offset);
         proto_tree_add_uint(bf_tree, hf_sna_th_fid, tvb, offset, 1, th_0);

or

         guint8 th_0 = tvb_get_guint8(tvb, offset);
         proto_tree_add_item(bf_tree, hf_sna_th_fid, tvb, offset, 1, FALSE);

(although in that case either FALSE or TRUE would work - but that's SNA
stuff, so it's probably big-endian, as Amdahl, Blaauw, Brooks, and
company intended).