Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-moldudp64.c
1 /* packet-moldudp64.c
2  * Routines for MoldUDP64 dissection
3  * Copyright 2012, Evan Huus <eapache@gmail.com>
4  *
5  * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/moldudp64.pdf
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include <epan/packet.h>
31 #include <epan/prefs.h>
32 #include <epan/expert.h>
33
34 void proto_register_moldudp64(void);
35 void proto_reg_handoff_moldudp64(void);
36
37 /* Initialize the protocol and registered fields */
38 static int proto_moldudp64       = -1;
39 static int hf_moldudp64_session  = -1;
40 static int hf_moldudp64_sequence = -1;
41 static int hf_moldudp64_count    = -1;
42 static int hf_moldudp64_msgblk   = -1;
43 static int hf_moldudp64_msglen   = -1;
44 static int hf_moldudp64_msgseq   = -1;
45 static int hf_moldudp64_msgdata  = -1;
46
47 #define MOLDUDP64_SESSION_LEN  10
48 #define MOLDUDP64_SEQUENCE_LEN  8
49 #define MOLDUDP64_COUNT_LEN     2
50 #define MOLDUDP64_MSGLEN_LEN    2
51
52 #define MOLDUDP64_HEARTBEAT 0x0000
53 #define MOLDUDP64_ENDOFSESS 0xFFFF
54
55 /* Global port pref */
56 static guint pf_moldudp64_port = 0;
57
58 /* Initialize the subtree pointers */
59 static gint ett_moldudp64        = -1;
60 static gint ett_moldudp64_msgblk = -1;
61
62 static expert_field ei_moldudp64_msglen_invalid = EI_INIT;
63 static expert_field ei_moldudp64_end_of_session_extra = EI_INIT;
64 static expert_field ei_moldudp64_count_invalid = EI_INIT;
65 static expert_field ei_moldudp64_request = EI_INIT;
66
67 /* Code to dissect a message block */
68 static guint
69 dissect_moldudp64_msgblk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
70         guint offset, guint64 sequence)
71 {
72     proto_item *ti;
73     proto_tree *blk_tree;
74     guint16     msglen, real_msglen, whole_len;
75     gint        remaining;
76
77     if (tvb_length_remaining(tvb, offset) < MOLDUDP64_MSGLEN_LEN)
78         return 0;
79
80     msglen = tvb_get_ntohs(tvb, offset);
81     remaining = tvb_reported_length(tvb) - offset - MOLDUDP64_MSGLEN_LEN;
82
83     if (remaining < 0)
84         real_msglen = 0;
85     else if (msglen <= remaining)
86         real_msglen = msglen;
87     else
88         real_msglen = remaining;
89
90     /* msglen and real_msglen only count the data section, and don't
91      * include the two bytes for the length field itself. */
92     whole_len = real_msglen + MOLDUDP64_MSGLEN_LEN;
93
94     ti = proto_tree_add_item(tree, hf_moldudp64_msgblk,
95             tvb, offset, whole_len, ENC_NA);
96
97     blk_tree = proto_item_add_subtree(ti, ett_moldudp64_msgblk);
98
99     ti = proto_tree_add_uint64(blk_tree, hf_moldudp64_msgseq,
100             tvb, offset, 0, sequence);
101
102     PROTO_ITEM_SET_GENERATED(ti);
103
104     ti = proto_tree_add_item(blk_tree, hf_moldudp64_msglen,
105             tvb, offset, MOLDUDP64_MSGLEN_LEN, ENC_BIG_ENDIAN);
106
107     if (msglen != real_msglen)
108         expert_add_info_format(pinfo, ti, &ei_moldudp64_msglen_invalid,
109                 "Invalid Message Length (claimed %u, found %u)",
110                 msglen, real_msglen);
111
112     offset += MOLDUDP64_MSGLEN_LEN;
113
114     proto_tree_add_item(blk_tree, hf_moldudp64_msgdata,
115             tvb, offset, real_msglen, ENC_NA);
116
117     return whole_len;
118 }
119
120 /* Code to actually dissect the packets */
121 static int
122 dissect_moldudp64(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
123 {
124     proto_item *ti;
125     proto_tree *moldudp64_tree;
126     guint       offset            = 0;
127     guint16     count, real_count = 0;
128     guint64     sequence;
129
130     /* Check that there's enough data */
131     if (tvb_reported_length(tvb) < (MOLDUDP64_SESSION_LEN  +
132                                     MOLDUDP64_SEQUENCE_LEN +
133                                     MOLDUDP64_COUNT_LEN))
134         return 0;
135
136     /* Make entries in Protocol column and Info column on summary display */
137     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MoldUDP64");
138
139     /* Clear the info column so it's sane if we crash. We fill it in later when
140      * we've dissected more of the packet. */
141     col_clear(pinfo->cinfo, COL_INFO);
142
143     sequence = tvb_get_ntoh64(tvb, MOLDUDP64_SESSION_LEN);
144     count = tvb_get_ntohs(tvb, MOLDUDP64_SESSION_LEN + MOLDUDP64_SEQUENCE_LEN);
145
146     if (count == MOLDUDP64_HEARTBEAT)
147         col_set_str(pinfo->cinfo, COL_INFO, "MoldUDP64 Heartbeat");
148     else if (count == MOLDUDP64_ENDOFSESS)
149         col_set_str(pinfo->cinfo, COL_INFO, "MoldUDP64 End Of Session");
150     else if (count > 0 && tvb_reported_length(tvb) == (MOLDUDP64_SESSION_LEN  +
151                                                        MOLDUDP64_SEQUENCE_LEN +
152                                                        MOLDUDP64_COUNT_LEN))
153         col_set_str(pinfo->cinfo, COL_INFO, "MoldUDP64 Request");
154     else
155         col_set_str(pinfo->cinfo, COL_INFO, "MoldUDP64 Messages");
156
157     /* create display subtree for the protocol */
158     ti = proto_tree_add_item(tree, proto_moldudp64,
159                              tvb, offset, -1, ENC_NA);
160
161     moldudp64_tree = proto_item_add_subtree(ti, ett_moldudp64);
162
163     proto_tree_add_item(moldudp64_tree, hf_moldudp64_session,
164                         tvb, offset, MOLDUDP64_SESSION_LEN, ENC_ASCII|ENC_NA);
165     offset += MOLDUDP64_SESSION_LEN;
166
167     proto_tree_add_item(moldudp64_tree, hf_moldudp64_sequence,
168                         tvb, offset, MOLDUDP64_SEQUENCE_LEN, ENC_BIG_ENDIAN);
169     offset += MOLDUDP64_SEQUENCE_LEN;
170
171     ti = proto_tree_add_item(moldudp64_tree, hf_moldudp64_count,
172                              tvb, offset, MOLDUDP64_COUNT_LEN, ENC_BIG_ENDIAN);
173     offset += MOLDUDP64_COUNT_LEN;
174
175     while (tvb_reported_length(tvb) >= offset + MOLDUDP64_MSGLEN_LEN)
176     {
177         offset += dissect_moldudp64_msgblk(tvb, pinfo, moldudp64_tree,
178                                            offset, sequence++);
179         real_count++;
180     }
181
182     if (count == MOLDUDP64_ENDOFSESS && real_count != 0)
183     {
184         expert_add_info(pinfo, ti, &ei_moldudp64_end_of_session_extra);
185     }
186     else if (count > 0 && real_count == 0)
187     {
188         expert_add_info(pinfo, ti, &ei_moldudp64_request);
189     }
190     else if (real_count != count)
191     {
192         expert_add_info_format(pinfo, ti, &ei_moldudp64_count_invalid,
193                                "Invalid Message Count (claimed %u, found %u)",
194                                count, real_count);
195     }
196
197     /* Return the amount of data this dissector was able to dissect */
198     return tvb_length(tvb);
199 }
200
201
202 /* Register the protocol with Wireshark */
203 void
204 proto_register_moldudp64(void)
205 {
206     module_t *moldudp64_module;
207
208     /* Setup list of header fields */
209     static hf_register_info hf[] = {
210
211         { &hf_moldudp64_session,
212         { "Session",       "moldudp64.session",  FT_STRING, BASE_NONE, NULL, 0,
213           "The session to which this packet belongs.", HFILL }},
214
215         { &hf_moldudp64_sequence,
216         { "Sequence",      "moldudp64.sequence", FT_UINT64, BASE_DEC,  NULL, 0,
217           "The sequence number of the first message in this packet.", HFILL }},
218
219         { &hf_moldudp64_count,
220         { "Count",         "moldudp64.count",    FT_UINT16, BASE_DEC,  NULL, 0,
221           "The number of messages contained in this packet.", HFILL }},
222
223         { &hf_moldudp64_msgblk,
224         { "Message Block", "moldudp64.msgblock", FT_NONE,   BASE_NONE, NULL, 0,
225           "A message.", HFILL }},
226
227         { &hf_moldudp64_msglen,
228         { "Length",        "moldudp64.msglen",   FT_UINT16, BASE_DEC,  NULL, 0,
229           "The length of this message.", HFILL }},
230
231         { &hf_moldudp64_msgseq,
232         { "Sequence",      "moldudp64.msgseq",   FT_UINT64, BASE_DEC,  NULL, 0,
233           "The sequence number of this message.", HFILL }},
234
235         { &hf_moldudp64_msgdata,
236         { "Payload",       "moldudp64.msgdata",  FT_BYTES,  BASE_NONE, NULL, 0,
237           "The payload data of this message.", HFILL }}
238     };
239
240     /* Setup protocol subtree array */
241     static gint *ett[] = {
242         &ett_moldudp64,
243         &ett_moldudp64_msgblk
244     };
245
246     static ei_register_info ei[] = {
247         { &ei_moldudp64_msglen_invalid, { "moldudp64.msglen.invalid", PI_MALFORMED, PI_ERROR, "Invalid Message Length", EXPFILL }},
248         { &ei_moldudp64_end_of_session_extra, { "moldudp64.end_of_session_extra", PI_MALFORMED, PI_ERROR, "End Of Session packet with extra data.", EXPFILL }},
249         { &ei_moldudp64_count_invalid, { "moldudp64.count.invalid", PI_MALFORMED, PI_ERROR, "Invalid Message Count", EXPFILL }},
250         { &ei_moldudp64_request, { "moldudp64.request", PI_COMMENTS_GROUP, PI_COMMENT, "Number of Requested Messages", EXPFILL }},
251     };
252
253     expert_module_t* expert_moldudp64;
254
255     /* Register the protocol name and description */
256     proto_moldudp64 = proto_register_protocol("MoldUDP64",
257             "MoldUDP64", "moldudp64");
258
259     /* Required function calls to register the header fields and subtrees used */
260     proto_register_field_array(proto_moldudp64, hf, array_length(hf));
261     proto_register_subtree_array(ett, array_length(ett));
262     expert_moldudp64 = expert_register_protocol(proto_moldudp64);
263     expert_register_field_array(expert_moldudp64, ei, array_length(ei));
264
265     /* Register preferences module */
266     moldudp64_module = prefs_register_protocol(proto_moldudp64,
267             proto_reg_handoff_moldudp64);
268
269     /* Register a sample port preference   */
270     prefs_register_uint_preference(moldudp64_module, "udp.port", "MoldUDP64 UDP Port",
271             "MoldUDP64 UDP port to dissect on.",
272             10, &pf_moldudp64_port);
273 }
274
275
276 void
277 proto_reg_handoff_moldudp64(void)
278 {
279     static gboolean           initialized = FALSE;
280     static dissector_handle_t moldudp64_handle;
281     static int                currentPort;
282
283     if (!initialized) {
284
285         moldudp64_handle = new_create_dissector_handle(dissect_moldudp64,
286                 proto_moldudp64);
287         initialized = TRUE;
288     } else {
289
290         dissector_delete_uint("udp.port", currentPort, moldudp64_handle);
291     }
292
293     currentPort = pf_moldudp64_port;
294
295     dissector_add_uint("udp.port", currentPort, moldudp64_handle);
296
297 }
298
299 /*
300  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
301  *
302  * Local variables:
303  * c-basic-offset: 4
304  * tab-width: 8
305  * indent-tabs-mode: nil
306  * End:
307  *
308  * vi: set shiftwidth=4 tabstop=8 expandtab:
309  * :indentSize=4:tabSize=8:noTabs=true:
310  */