2 * Routines for MoldUDP64 dissection
3 * Copyright 2012, Evan Huus <eapache@gmail.com>
5 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/moldudp64.pdf
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
30 #include <epan/packet.h>
31 #include <epan/prefs.h>
32 #include <epan/expert.h>
34 void proto_register_moldudp64(void);
35 void proto_reg_handoff_moldudp64(void);
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;
47 #define MOLDUDP64_SESSION_LEN 10
48 #define MOLDUDP64_SEQUENCE_LEN 8
49 #define MOLDUDP64_COUNT_LEN 2
50 #define MOLDUDP64_MSGLEN_LEN 2
52 #define MOLDUDP64_HEARTBEAT 0x0000
53 #define MOLDUDP64_ENDOFSESS 0xFFFF
55 /* Global port pref */
56 static guint pf_moldudp64_port = 0;
58 /* Initialize the subtree pointers */
59 static gint ett_moldudp64 = -1;
60 static gint ett_moldudp64_msgblk = -1;
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;
67 /* Code to dissect a message block */
69 dissect_moldudp64_msgblk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
70 guint offset, guint64 sequence)
74 guint16 msglen, real_msglen, whole_len;
77 if (tvb_length_remaining(tvb, offset) < MOLDUDP64_MSGLEN_LEN)
80 msglen = tvb_get_ntohs(tvb, offset);
81 remaining = tvb_reported_length(tvb) - offset - MOLDUDP64_MSGLEN_LEN;
85 else if (msglen <= remaining)
88 real_msglen = remaining;
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;
94 ti = proto_tree_add_item(tree, hf_moldudp64_msgblk,
95 tvb, offset, whole_len, ENC_NA);
97 blk_tree = proto_item_add_subtree(ti, ett_moldudp64_msgblk);
99 ti = proto_tree_add_uint64(blk_tree, hf_moldudp64_msgseq,
100 tvb, offset, 0, sequence);
102 PROTO_ITEM_SET_GENERATED(ti);
104 ti = proto_tree_add_item(blk_tree, hf_moldudp64_msglen,
105 tvb, offset, MOLDUDP64_MSGLEN_LEN, ENC_BIG_ENDIAN);
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);
112 offset += MOLDUDP64_MSGLEN_LEN;
114 proto_tree_add_item(blk_tree, hf_moldudp64_msgdata,
115 tvb, offset, real_msglen, ENC_NA);
120 /* Code to actually dissect the packets */
122 dissect_moldudp64(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
125 proto_tree *moldudp64_tree;
127 guint16 count, real_count = 0;
130 /* Check that there's enough data */
131 if (tvb_reported_length(tvb) < (MOLDUDP64_SESSION_LEN +
132 MOLDUDP64_SEQUENCE_LEN +
133 MOLDUDP64_COUNT_LEN))
136 /* Make entries in Protocol column and Info column on summary display */
137 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MoldUDP64");
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);
143 sequence = tvb_get_ntoh64(tvb, MOLDUDP64_SESSION_LEN);
144 count = tvb_get_ntohs(tvb, MOLDUDP64_SESSION_LEN + MOLDUDP64_SEQUENCE_LEN);
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");
155 col_set_str(pinfo->cinfo, COL_INFO, "MoldUDP64 Messages");
157 /* create display subtree for the protocol */
158 ti = proto_tree_add_item(tree, proto_moldudp64,
159 tvb, offset, -1, ENC_NA);
161 moldudp64_tree = proto_item_add_subtree(ti, ett_moldudp64);
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;
167 proto_tree_add_item(moldudp64_tree, hf_moldudp64_sequence,
168 tvb, offset, MOLDUDP64_SEQUENCE_LEN, ENC_BIG_ENDIAN);
169 offset += MOLDUDP64_SEQUENCE_LEN;
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;
175 while (tvb_reported_length(tvb) >= offset + MOLDUDP64_MSGLEN_LEN)
177 offset += dissect_moldudp64_msgblk(tvb, pinfo, moldudp64_tree,
182 if (count == MOLDUDP64_ENDOFSESS && real_count != 0)
184 expert_add_info(pinfo, ti, &ei_moldudp64_end_of_session_extra);
186 else if (count > 0 && real_count == 0)
188 expert_add_info(pinfo, ti, &ei_moldudp64_request);
190 else if (real_count != count)
192 expert_add_info_format(pinfo, ti, &ei_moldudp64_count_invalid,
193 "Invalid Message Count (claimed %u, found %u)",
197 /* Return the amount of data this dissector was able to dissect */
198 return tvb_length(tvb);
202 /* Register the protocol with Wireshark */
204 proto_register_moldudp64(void)
206 module_t *moldudp64_module;
208 /* Setup list of header fields */
209 static hf_register_info hf[] = {
211 { &hf_moldudp64_session,
212 { "Session", "moldudp64.session", FT_STRING, BASE_NONE, NULL, 0,
213 "The session to which this packet belongs.", HFILL }},
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 }},
219 { &hf_moldudp64_count,
220 { "Count", "moldudp64.count", FT_UINT16, BASE_DEC, NULL, 0,
221 "The number of messages contained in this packet.", HFILL }},
223 { &hf_moldudp64_msgblk,
224 { "Message Block", "moldudp64.msgblock", FT_NONE, BASE_NONE, NULL, 0,
225 "A message.", HFILL }},
227 { &hf_moldudp64_msglen,
228 { "Length", "moldudp64.msglen", FT_UINT16, BASE_DEC, NULL, 0,
229 "The length of this message.", HFILL }},
231 { &hf_moldudp64_msgseq,
232 { "Sequence", "moldudp64.msgseq", FT_UINT64, BASE_DEC, NULL, 0,
233 "The sequence number of this message.", HFILL }},
235 { &hf_moldudp64_msgdata,
236 { "Payload", "moldudp64.msgdata", FT_BYTES, BASE_NONE, NULL, 0,
237 "The payload data of this message.", HFILL }}
240 /* Setup protocol subtree array */
241 static gint *ett[] = {
243 &ett_moldudp64_msgblk
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 }},
253 expert_module_t* expert_moldudp64;
255 /* Register the protocol name and description */
256 proto_moldudp64 = proto_register_protocol("MoldUDP64",
257 "MoldUDP64", "moldudp64");
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));
265 /* Register preferences module */
266 moldudp64_module = prefs_register_protocol(proto_moldudp64,
267 proto_reg_handoff_moldudp64);
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);
277 proto_reg_handoff_moldudp64(void)
279 static gboolean initialized = FALSE;
280 static dissector_handle_t moldudp64_handle;
281 static int currentPort;
285 moldudp64_handle = new_create_dissector_handle(dissect_moldudp64,
290 dissector_delete_uint("udp.port", currentPort, moldudp64_handle);
293 currentPort = pf_moldudp64_port;
295 dissector_add_uint("udp.port", currentPort, moldudp64_handle);
300 * Editor modelines - http://www.wireshark.org/tools/modelines.html
305 * indent-tabs-mode: nil
308 * vi: set shiftwidth=4 tabstop=8 expandtab:
309 * :indentSize=4:tabSize=8:noTabs=true: