For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-fractalgeneratorprotocol.c
1 /* packet-fractalgeneratorprotocol.c
2  * Routines for the Fractal Generator Protocol, a test application of the
3  * rsplib RSerPool implementation
4  * http://tdrwww.exp-math.uni-essen.de/dreibholz/rserpool/
5  *
6  * Copyright 2006 by Thomas Dreibholz <dreibh [AT] exp-math.uni-essen.de>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied from README.developer
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <epan/packet.h>
36 #include <epan/sctpppids.h>
37
38
39 #define FRACTALGENERATORPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY 0x29097601
40
41
42 /* Initialize the protocol and registered fields */
43 static int proto_fractalgeneratorprotocol = -1;
44 static int hf_message_type                = -1;
45 static int hf_message_flags               = -1;
46 static int hf_message_length              = -1;
47 static int hf_data_start_x                = -1;
48 static int hf_data_start_y                = -1;
49 static int hf_data_points                 = -1;
50 static int hf_parameter_width             = -1;
51 static int hf_parameter_height            = -1;
52 static int hf_parameter_maxiterations     = -1;
53 static int hf_parameter_algorithmid       = -1;
54 static int hf_parameter_c1real            = -1;
55 static int hf_parameter_c1imag            = -1;
56 static int hf_parameter_c2real            = -1;
57 static int hf_parameter_c2imag            = -1;
58 static int hf_parameter_n                 = -1;
59 static int hf_buffer                      = -1;
60
61 /* Initialize the subtree pointers */
62 static gint ett_fractalgeneratorprotocol = -1;
63
64 static void
65 dissect_fractalgeneratorprotocol_message(tvbuff_t *, packet_info *, proto_tree *);
66
67
68 /* Dissectors for messages. This is specific to FractalGeneratorProtocol */
69 #define MESSAGE_TYPE_LENGTH    1
70 #define MESSAGE_FLAGS_LENGTH   1
71 #define MESSAGE_LENGTH_LENGTH  2
72
73 #define MESSAGE_TYPE_OFFSET    0
74 #define MESSAGE_FLAGS_OFFSET   (MESSAGE_TYPE_OFFSET    + MESSAGE_TYPE_LENGTH)
75 #define MESSAGE_LENGTH_OFFSET  (MESSAGE_FLAGS_OFFSET   + MESSAGE_FLAGS_OFFSET)
76 #define MESSAGE_VALUE_OFFSET   (MESSAGE_LENGTH_OFFSET  + MESSAGE_LENGTH_LENGTH)
77
78
79 #define DATA_STARTX_LENGTH 4
80 #define DATA_STARTY_LENGTH 4
81 #define DATA_POINTS_LENGTH 4
82 #define DATA_BUFFER_LENGTH 4
83
84 #define DATA_STARTX_OFFSET MESSAGE_VALUE_OFFSET
85 #define DATA_STARTY_OFFSET (DATA_STARTX_OFFSET + DATA_STARTX_LENGTH)
86 #define DATA_POINTS_OFFSET (DATA_STARTY_OFFSET + DATA_STARTY_LENGTH)
87 #define DATA_BUFFER_OFFSET (DATA_POINTS_OFFSET + DATA_POINTS_LENGTH)
88
89
90 #define PARAMETER_WIDTH_LENGTH         4
91 #define PARAMETER_HEIGHT_LENGTH        4
92 #define PARAMETER_MAXITERATIONS_LENGTH 4
93 #define PARAMETER_ALGORITHMID_LENGTH   4
94 #define PARAMETER_C1REAL_LENGTH        8
95 #define PARAMETER_C1IMAG_LENGTH        8
96 #define PARAMETER_C2REAL_LENGTH        8
97 #define PARAMETER_C2IMAG_LENGTH        8
98 #define PARAMETER_N_LENGTH             8
99
100 #define PARAMETER_WIDTH_OFFSET         MESSAGE_VALUE_OFFSET
101 #define PARAMETER_HEIGHT_OFFSET        (PARAMETER_WIDTH_OFFSET + PARAMETER_WIDTH_LENGTH)
102 #define PARAMETER_MAXITERATIONS_OFFSET (PARAMETER_HEIGHT_OFFSET + PARAMETER_HEIGHT_LENGTH)
103 #define PARAMETER_ALGORITHMID_OFFSET   (PARAMETER_MAXITERATIONS_OFFSET + PARAMETER_MAXITERATIONS_LENGTH)
104 #define PARAMETER_C1REAL_OFFSET        (PARAMETER_ALGORITHMID_OFFSET + PARAMETER_ALGORITHMID_LENGTH)
105 #define PARAMETER_C1IMAG_OFFSET        (PARAMETER_C1REAL_OFFSET + PARAMETER_C1REAL_LENGTH)
106 #define PARAMETER_C2REAL_OFFSET        (PARAMETER_C1IMAG_OFFSET + PARAMETER_C1IMAG_LENGTH)
107 #define PARAMETER_C2IMAG_OFFSET        (PARAMETER_C2REAL_OFFSET + PARAMETER_C2REAL_LENGTH)
108 #define PARAMETER_N_OFFSET             (PARAMETER_C2IMAG_OFFSET + PARAMETER_C2IMAG_LENGTH)
109
110 #define FRACTALGENERATOR_PARAMETER_MESSAGE_TYPE 0x01
111 #define FRACTALGENERATOR_DATA_MESSAGE_TYPE      0x02
112
113
114 static const value_string message_type_values[] = {
115   { FRACTALGENERATOR_PARAMETER_MESSAGE_TYPE,        "FractalGenerator Parameter" },
116   { FRACTALGENERATOR_DATA_MESSAGE_TYPE,             "FractalGenerator Data" },
117   { 0, NULL }
118 };
119
120
121 static void
122 dissect_fractalgeneratorprotocol_parameter_message(tvbuff_t *message_tvb, proto_tree *message_tree)
123 {
124   proto_tree_add_item(message_tree, hf_parameter_width,         message_tvb, PARAMETER_WIDTH_OFFSET,         PARAMETER_WIDTH_LENGTH,         ENC_BIG_ENDIAN);
125   proto_tree_add_item(message_tree, hf_parameter_height,        message_tvb, PARAMETER_HEIGHT_OFFSET,        PARAMETER_HEIGHT_LENGTH,        ENC_BIG_ENDIAN);
126   proto_tree_add_item(message_tree, hf_parameter_maxiterations, message_tvb, PARAMETER_MAXITERATIONS_OFFSET, PARAMETER_MAXITERATIONS_LENGTH, ENC_BIG_ENDIAN);
127   proto_tree_add_item(message_tree, hf_parameter_algorithmid,   message_tvb, PARAMETER_ALGORITHMID_OFFSET,   PARAMETER_ALGORITHMID_LENGTH,   ENC_BIG_ENDIAN);
128   proto_tree_add_item(message_tree, hf_parameter_c1real,        message_tvb, PARAMETER_C1REAL_OFFSET,        PARAMETER_C1REAL_LENGTH,        ENC_BIG_ENDIAN);
129   proto_tree_add_item(message_tree, hf_parameter_c1imag,        message_tvb, PARAMETER_C1IMAG_OFFSET,        PARAMETER_C1IMAG_LENGTH,        ENC_BIG_ENDIAN);
130   proto_tree_add_item(message_tree, hf_parameter_c2real,        message_tvb, PARAMETER_C2REAL_OFFSET,        PARAMETER_C2REAL_LENGTH,        ENC_BIG_ENDIAN);
131   proto_tree_add_item(message_tree, hf_parameter_c2imag,        message_tvb, PARAMETER_C2IMAG_OFFSET,        PARAMETER_C2IMAG_LENGTH,        ENC_BIG_ENDIAN);
132   proto_tree_add_item(message_tree, hf_parameter_n,             message_tvb, PARAMETER_N_OFFSET,             PARAMETER_N_LENGTH,             ENC_BIG_ENDIAN);
133 }
134
135
136 static void
137 dissect_fractalgeneratorprotocol_data_message(tvbuff_t *message_tvb, proto_tree *message_tree)
138 {
139   guint16 buffer_length;
140
141   proto_tree_add_item(message_tree, hf_data_start_x, message_tvb, DATA_STARTX_OFFSET, DATA_STARTX_LENGTH, ENC_BIG_ENDIAN);
142   proto_tree_add_item(message_tree, hf_data_start_y, message_tvb, DATA_STARTY_OFFSET, DATA_STARTY_LENGTH, ENC_BIG_ENDIAN);
143   proto_tree_add_item(message_tree, hf_data_points,  message_tvb, DATA_POINTS_OFFSET, DATA_POINTS_LENGTH, ENC_BIG_ENDIAN);
144
145   buffer_length = tvb_get_ntohl(message_tvb, DATA_POINTS_OFFSET)*4;
146   if (buffer_length > 0) {
147     proto_tree_add_item(message_tree, hf_buffer, message_tvb, DATA_BUFFER_OFFSET, buffer_length, ENC_NA);
148   }
149 }
150
151
152 static void
153 dissect_fractalgeneratorprotocol_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *fractalgeneratorprotocol_tree)
154 {
155   guint8 type;
156
157   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
158   if (pinfo && (check_col(pinfo->cinfo, COL_INFO))) {
159     col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(type, message_type_values, "Unknown FractalGeneratorProtocol type"));
160   }
161   proto_tree_add_item(fractalgeneratorprotocol_tree, hf_message_type,   message_tvb, MESSAGE_TYPE_OFFSET,   MESSAGE_TYPE_LENGTH,   ENC_BIG_ENDIAN);
162   proto_tree_add_item(fractalgeneratorprotocol_tree, hf_message_flags,  message_tvb, MESSAGE_FLAGS_OFFSET,  MESSAGE_FLAGS_LENGTH,  ENC_BIG_ENDIAN);
163   proto_tree_add_item(fractalgeneratorprotocol_tree, hf_message_length, message_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH, ENC_BIG_ENDIAN);
164   switch (type) {
165     case FRACTALGENERATOR_PARAMETER_MESSAGE_TYPE:
166       dissect_fractalgeneratorprotocol_parameter_message(message_tvb, fractalgeneratorprotocol_tree);
167      break;
168     case FRACTALGENERATOR_DATA_MESSAGE_TYPE:
169       dissect_fractalgeneratorprotocol_data_message(message_tvb, fractalgeneratorprotocol_tree);
170      break;
171   }
172 }
173
174 static int
175 dissect_fractalgeneratorprotocol(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
176 {
177   proto_item *fractalgeneratorprotocol_item;
178   proto_tree *fractalgeneratorprotocol_tree;
179
180   /* pinfo is NULL only if dissect_fractalgeneratorprotocol_message is called from dissect_error cause */
181   if (pinfo)
182     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FractalGeneratorProtocol");
183
184   /* In the interest of speed, if "tree" is NULL, don't do any work not
185      necessary to generate protocol tree items. */
186   if (tree) {
187     /* create the fractalgeneratorprotocol protocol tree */
188     fractalgeneratorprotocol_item = proto_tree_add_item(tree, proto_fractalgeneratorprotocol, message_tvb, 0, -1, ENC_NA);
189     fractalgeneratorprotocol_tree = proto_item_add_subtree(fractalgeneratorprotocol_item, ett_fractalgeneratorprotocol);
190   } else {
191     fractalgeneratorprotocol_tree = NULL;
192   };
193   /* dissect the message */
194   dissect_fractalgeneratorprotocol_message(message_tvb, pinfo, fractalgeneratorprotocol_tree);
195   return(TRUE);
196 }
197
198 /* Register the protocol with Wireshark */
199 void
200 proto_register_fractalgeneratorprotocol(void)
201 {
202
203   /* Setup list of header fields */
204   static hf_register_info hf[] = {
205     { &hf_message_type,            { "Type",          "fractalgeneratorprotocol.message_type",            FT_UINT8,  BASE_DEC, VALS(message_type_values), 0x0, NULL, HFILL } },
206     { &hf_message_flags,           { "Flags",         "fractalgeneratorprotocol.message_flags",           FT_UINT8,  BASE_DEC, NULL,                      0x0, NULL, HFILL } },
207     { &hf_message_length,          { "Length",        "fractalgeneratorprotocol.message_length",          FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
208     { &hf_data_start_x,            { "StartX",        "fractalgeneratorprotocol.data_start_x",            FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
209     { &hf_data_start_y,            { "StartY",        "fractalgeneratorprotocol.data_start_y",            FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
210     { &hf_data_points,             { "Points",        "fractalgeneratorprotocol.data_points",             FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
211     { &hf_parameter_width,         { "Width",         "fractalgeneratorprotocol.parameter_width",         FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
212     { &hf_parameter_height,        { "Height",        "fractalgeneratorprotocol.parameter_height",        FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
213     { &hf_parameter_maxiterations, { "MaxIterations", "fractalgeneratorprotocol.parameter_maxiterations", FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
214     { &hf_parameter_algorithmid,   { "AlgorithmID",   "fractalgeneratorprotocol.parameter_algorithmid",   FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
215     { &hf_parameter_c1real,        { "C1Real",        "fractalgeneratorprotocol.parameter_c1real",        FT_DOUBLE, BASE_NONE, NULL,                      0x0, NULL, HFILL } },
216     { &hf_parameter_c1imag,        { "C1Imag",        "fractalgeneratorprotocol.parameter_c1imag",        FT_DOUBLE, BASE_NONE, NULL,                      0x0, NULL, HFILL } },
217     { &hf_parameter_c2real,        { "C2Real",        "fractalgeneratorprotocol.parameter_c2real",        FT_DOUBLE, BASE_NONE, NULL,                      0x0, NULL, HFILL } },
218     { &hf_parameter_c2imag,        { "C2Imag",        "fractalgeneratorprotocol.parameter_c2imag",        FT_DOUBLE, BASE_NONE, NULL,                      0x0, NULL, HFILL } },
219     { &hf_parameter_n,             { "N",             "fractalgeneratorprotocol.parameter_n",             FT_DOUBLE, BASE_NONE, NULL,                      0x0, NULL, HFILL } },
220     { &hf_buffer,                  { "Buffer",        "fractalgeneratorprotocol.buffer",                  FT_BYTES,  BASE_NONE, NULL,                      0x0, NULL, HFILL } },
221   };
222
223   /* Setup protocol subtree array */
224   static gint *ett[] = {
225     &ett_fractalgeneratorprotocol
226   };
227
228   /* Register the protocol name and description */
229   proto_fractalgeneratorprotocol = proto_register_protocol("Fractal Generator Protocol", "FractalGeneratorProtocol",  "fractalgeneratorprotocol");
230
231   /* Required function calls to register the header fields and subtrees used */
232   proto_register_field_array(proto_fractalgeneratorprotocol, hf, array_length(hf));
233   proto_register_subtree_array(ett, array_length(ett));
234
235 }
236
237 void
238 proto_reg_handoff_fractalgeneratorprotocol(void)
239 {
240   dissector_handle_t fractalgeneratorprotocol_handle;
241
242   fractalgeneratorprotocol_handle = new_create_dissector_handle(dissect_fractalgeneratorprotocol, proto_fractalgeneratorprotocol);
243   dissector_add_uint("sctp.ppi", FRACTALGENERATORPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY, fractalgeneratorprotocol_handle);
244   dissector_add_uint("sctp.ppi", FGP_PAYLOAD_PROTOCOL_ID, fractalgeneratorprotocol_handle);
245 }