Ethereal-users: Re: [Ethereal-users] Problem with val_to_str

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

From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Wed, 2 Jul 2003 13:19:36 -0700

On Wednesday, July 2, 2003, at 2:47 AM, Chua Wee Leng wrote:

I encounter a proble with val_to_str function while doing my plugin.

That's a developer question, so it should have been sent to ethereal-dev@xxxxxxxxxxxx.

The following is part of my code of protocol dis in packet-dis.c

#define ENTITY_STATE_PDU        	1
#define FIRE_PDU	        	2
#define DETONATION_PDU	3
#define START_RESUME_PDU      13
#define STOP_FREEZE_PDU         	14
#define ACKNOWLEDGE_PDU       15
#define SET_DATA_PDU        	19
#define DATA_PDU        	20

	const value_string pdu[] = {
                                {ENTITY_STATE_PDU,
	"ENTITY_STATE_PDU"},
                                {FIRE_PDU,	        	"FIRE_PDU"},
                                {DETONATION_PDU,		"DETONATION_PDU"},
                                {START_RESUME_PDU,
	"START_RESUME_PDU"},
{STOP_FREEZE_PDU, "STOP_FREEZE_PDU"}, {ACKNOWLEDGE_PDU, "ACKNOWLEDGE_PDU"},
	        };

        static const value_string vs_Entity_Kind[] = {
                                {1,   "Platform"},
                                {2,   "Munition"},
                                {4,   "Environment"},
                                {9,   "Sensor/Emitter"},
	      		};

A value_string table *MUST* end with a

	{0, NULL }

item. Otherwise, the code that converts values to strings will, if handed a value not in the table, go past the end of the table.

This is documented in "doc/README.developer":

	strings
	-------
Some integer fields, of type FT_UINT*, need labels to represent the true
	value of a field.  You could think of those fields as having an
	enumerated data type, rather than an integral data type.

	A 'value_string' structure is a way to map values to strings.

	        typedef struct _value_string {
	                guint32  value;
	                gchar   *strptr;
	        } value_string;

	For fields of that type, you would declare an array of "value_string"s:

 	       static const value_string valstringname[] = {
 	               { INTVAL1, "Descriptive String 1" },
 	               { INTVAL2, "Descriptive String 2" },
    	           { 0,       NULL },
        	};

	(the last entry in the array must have a NULL 'strptr' value, to
	indicate the end of the array).  The 'strings' field would be set to
	'VALS(valstringname)'.

Your tables must therefore be

	const value_string pdu[] = {
		{ENTITY_STATE_PDU,		"ENTITY_STATE_PDU"},
		{FIRE_PDU,	        	"FIRE_PDU"},
		{DETONATION_PDU,		"DETONATION_PDU"},
		{START_RESUME_PDU,		"START_RESUME_PDU"},
		{STOP_FREEZE_PDU,		"STOP_FREEZE_PDU"},
		{ACKNOWLEDGE_PDU,		"ACKNOWLEDGE_PDU"},
		{0,						NULL }
	};

	static const value_string vs_Entity_Kind[] = {
		{1,   "Platform"},
		{2,   "Munition"},
		{4,   "Environment"},
		{9,   "Sensor/Emitter"},
		{0,   NULL}
	};