1 /* packet-pingpongprotocol.c
2 * Routines for the Ping Pong Protocol, a test application of the
3 * RSPLIB RSerPool implementation
4 * https://www.uni-due.de/~be0001/rserpool/
6 * Copyright 2006-2021 by Thomas Dreibholz <dreibh [AT] iem.uni-due.de>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * Copied from README.developer
14 * SPDX-License-Identifier: GPL-2.0-or-later
19 #include <epan/packet.h>
20 #include <epan/sctpppids.h>
21 #include <epan/stat_tap_ui.h>
23 #include <wsutil/array.h>
25 #define PINGPONGPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY 0x29097602
27 void proto_register_pingpongprotocol(void);
28 void proto_reg_handoff_pingpongprotocol(void);
30 static dissector_handle_t pingpongprotocol_handle
;
32 /* Initialize the protocol and registered fields */
33 static int proto_pingpongprotocol
;
34 static int tap_pingpongprotocol
= -1;
35 static int ett_pingpongprotocol
;
36 static int hf_message_type
;
37 static int hf_message_flags
;
38 static int hf_message_length
;
39 static int hf_ping_messageno
;
40 static int hf_ping_data
;
41 static int hf_pong_messageno
;
42 static int hf_pong_replyno
;
43 static int hf_pong_data
;
45 static uint64_t pingpongprotocol_total_msgs
;
46 static uint64_t pingpongprotocol_total_bytes
;
48 /* Dissectors for messages. This is specific to PingPongProtocol */
49 #define MESSAGE_TYPE_LENGTH 1
50 #define MESSAGE_FLAGS_LENGTH 1
51 #define MESSAGE_LENGTH_LENGTH 2
53 #define MESSAGE_TYPE_OFFSET 0
54 #define MESSAGE_FLAGS_OFFSET (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)
55 #define MESSAGE_LENGTH_OFFSET (MESSAGE_FLAGS_OFFSET + MESSAGE_FLAGS_LENGTH)
56 #define MESSAGE_VALUE_OFFSET (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
59 #define PING_MESSAGENO_LENGTH 8
61 #define PING_MESSAGENO_OFFSET MESSAGE_VALUE_OFFSET
62 #define PING_DATA_OFFSET (PING_MESSAGENO_OFFSET + PING_MESSAGENO_LENGTH)
64 #define PONG_MESSAGENO_LENGTH 8
65 #define PONG_REPLYNO_LENGTH 8
67 #define PONG_MESSAGENO_OFFSET MESSAGE_VALUE_OFFSET
68 #define PONG_REPLYNO_OFFSET (PONG_MESSAGENO_OFFSET + PONG_MESSAGENO_LENGTH)
69 #define PONG_DATA_OFFSET (PONG_REPLYNO_OFFSET + PONG_REPLYNO_LENGTH)
72 #define PINGPONG_PING_MESSAGE_TYPE 0x01
73 #define PINGPONG_PONG_MESSAGE_TYPE 0x02
77 static const value_string message_type_values
[] = {
78 { PINGPONG_PONG_MESSAGE_TYPE
, "PingPongProtocol Pong" },
79 { PINGPONG_PING_MESSAGE_TYPE
, "PingPongProtocol Ping" },
84 typedef struct _tap_pingpongprotocol_rec_t
{
87 const char* type_string
;
88 } tap_pingpongprotocol_rec_t
;
92 dissect_pingpongprotocol_ping_message(tvbuff_t
*message_tvb
, proto_tree
*message_tree
)
94 uint16_t ping_data_length
;
96 proto_tree_add_item(message_tree
, hf_ping_messageno
, message_tvb
, PING_MESSAGENO_OFFSET
, PING_MESSAGENO_LENGTH
, ENC_BIG_ENDIAN
);
98 ping_data_length
= tvb_get_ntohs(message_tvb
, MESSAGE_LENGTH_OFFSET
) - PING_DATA_OFFSET
;
99 if (ping_data_length
> 0)
100 proto_tree_add_item(message_tree
, hf_ping_data
, message_tvb
, PING_DATA_OFFSET
, ping_data_length
, ENC_NA
);
104 dissect_pingpongprotocol_pong_message(tvbuff_t
*message_tvb
, proto_tree
*message_tree
)
106 uint16_t pong_data_length
;
108 proto_tree_add_item(message_tree
, hf_pong_messageno
, message_tvb
, PONG_MESSAGENO_OFFSET
, PONG_MESSAGENO_LENGTH
, ENC_BIG_ENDIAN
);
109 proto_tree_add_item(message_tree
, hf_pong_replyno
, message_tvb
, PONG_REPLYNO_OFFSET
, PONG_REPLYNO_LENGTH
, ENC_BIG_ENDIAN
);
111 pong_data_length
= tvb_get_ntohs(message_tvb
, MESSAGE_LENGTH_OFFSET
) - PONG_DATA_OFFSET
;
112 if (pong_data_length
> 0) {
113 proto_tree_add_item(message_tree
, hf_pong_data
, message_tvb
, PONG_DATA_OFFSET
, pong_data_length
, ENC_NA
);
119 dissect_pingpongprotocol_message(tvbuff_t
*message_tvb
, packet_info
*pinfo
, proto_tree
*pingpongprotocol_tree
)
121 tap_pingpongprotocol_rec_t
* tap_rec
= wmem_new0(pinfo
->pool
, tap_pingpongprotocol_rec_t
);
122 tap_rec
->type
= tvb_get_uint8(message_tvb
, MESSAGE_TYPE_OFFSET
);
123 tap_rec
->size
= tvb_get_ntohs(message_tvb
, MESSAGE_LENGTH_OFFSET
);
124 tap_rec
->type_string
= val_to_str_const(tap_rec
->type
, message_type_values
, "Unknown PingPongProtocol message type");
125 tap_queue_packet(tap_pingpongprotocol
, pinfo
, tap_rec
);
127 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "%s ", tap_rec
->type_string
);
129 proto_tree_add_item(pingpongprotocol_tree
, hf_message_type
, message_tvb
, MESSAGE_TYPE_OFFSET
, MESSAGE_TYPE_LENGTH
, ENC_BIG_ENDIAN
);
130 proto_tree_add_item(pingpongprotocol_tree
, hf_message_flags
, message_tvb
, MESSAGE_FLAGS_OFFSET
, MESSAGE_FLAGS_LENGTH
, ENC_BIG_ENDIAN
);
131 proto_tree_add_item(pingpongprotocol_tree
, hf_message_length
, message_tvb
, MESSAGE_LENGTH_OFFSET
, MESSAGE_LENGTH_LENGTH
, ENC_BIG_ENDIAN
);
132 switch (tap_rec
->type
) {
133 case PINGPONG_PING_MESSAGE_TYPE
:
134 dissect_pingpongprotocol_ping_message(message_tvb
, pingpongprotocol_tree
);
136 case PINGPONG_PONG_MESSAGE_TYPE
:
137 dissect_pingpongprotocol_pong_message(message_tvb
, pingpongprotocol_tree
);
143 dissect_pingpongprotocol(tvbuff_t
*message_tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
145 proto_item
*pingpongprotocol_item
;
146 proto_tree
*pingpongprotocol_tree
;
148 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "PingPongProtocol");
150 /* In the interest of speed, if "tree" is NULL, don't do any work not
151 necessary to generate protocol tree items. */
153 /* create the pingpongprotocol protocol tree */
154 pingpongprotocol_item
= proto_tree_add_item(tree
, proto_pingpongprotocol
, message_tvb
, 0, -1, ENC_NA
);
155 pingpongprotocol_tree
= proto_item_add_subtree(pingpongprotocol_item
, ett_pingpongprotocol
);
157 pingpongprotocol_tree
= NULL
;
159 /* dissect the message */
160 dissect_pingpongprotocol_message(message_tvb
, pinfo
, pingpongprotocol_tree
);
168 MESSAGE_TYPE_COLUMN
= 0,
170 MESSAGES_SHARE_COLUMN
,
178 } pingpongprotocol_stat_columns
;
180 static stat_tap_table_item pingpongprotocol_stat_fields
[] = {
181 { TABLE_ITEM_STRING
, TAP_ALIGN_LEFT
, "PingPongProtocol Message Type", "%-25s" },
182 { TABLE_ITEM_UINT
, TAP_ALIGN_RIGHT
, "Messages ", "%u" },
183 { TABLE_ITEM_UINT
, TAP_ALIGN_RIGHT
, "Messages Share (%)" , "%1.3f %%" },
184 { TABLE_ITEM_UINT
, TAP_ALIGN_RIGHT
, "Bytes (B)", "%u" },
185 { TABLE_ITEM_UINT
, TAP_ALIGN_RIGHT
, "Bytes Share (%) ", "%1.3f %%" },
186 { TABLE_ITEM_FLOAT
, TAP_ALIGN_LEFT
, "First Seen (s)", "%1.6f" },
187 { TABLE_ITEM_FLOAT
, TAP_ALIGN_LEFT
, "Last Seen (s)", "%1.6f" },
188 { TABLE_ITEM_FLOAT
, TAP_ALIGN_LEFT
, "Interval (s)", "%1.6f" },
189 { TABLE_ITEM_FLOAT
, TAP_ALIGN_LEFT
, "Message Rate (Msg/s)", "%1.2f" },
190 { TABLE_ITEM_FLOAT
, TAP_ALIGN_LEFT
, "Byte Rate (B/s)", "%1.2f" }
193 static void pingpongprotocol_stat_init(stat_tap_table_ui
* new_stat
)
195 const char *table_name
= "PingPongProtocol Statistics";
196 int num_fields
= array_length(pingpongprotocol_stat_fields
);
197 stat_tap_table
*table
;
199 stat_tap_table_item_type items
[array_length(pingpongprotocol_stat_fields
)];
201 table
= stat_tap_find_table(new_stat
, table_name
);
203 if (new_stat
->stat_tap_reset_table_cb
) {
204 new_stat
->stat_tap_reset_table_cb(table
);
209 table
= stat_tap_init_table(table_name
, num_fields
, 0, NULL
);
210 stat_tap_add_table(new_stat
, table
);
212 memset(items
, 0x0, sizeof(items
));
213 /* Add a row for each value type */
214 while (message_type_values
[i
].strptr
) {
215 items
[MESSAGE_TYPE_COLUMN
].type
= TABLE_ITEM_STRING
;
216 items
[MESSAGE_TYPE_COLUMN
].value
.string_value
= message_type_values
[i
].strptr
;
217 items
[MESSAGES_COLUMN
].type
= TABLE_ITEM_UINT
;
218 items
[MESSAGES_COLUMN
].value
.uint_value
= 0;
219 items
[MESSAGES_SHARE_COLUMN
].type
= TABLE_ITEM_NONE
;
220 items
[MESSAGES_SHARE_COLUMN
].value
.float_value
= -1.0;
221 items
[BYTES_COLUMN
].type
= TABLE_ITEM_UINT
;
222 items
[BYTES_COLUMN
].value
.uint_value
= 0;
223 items
[BYTES_SHARE_COLUMN
].type
= TABLE_ITEM_NONE
;
224 items
[BYTES_SHARE_COLUMN
].value
.float_value
= -1.0;
225 items
[FIRST_SEEN_COLUMN
].type
= TABLE_ITEM_NONE
;
226 items
[FIRST_SEEN_COLUMN
].value
.float_value
= DBL_MAX
;
227 items
[LAST_SEEN_COLUMN
].type
= TABLE_ITEM_NONE
;
228 items
[LAST_SEEN_COLUMN
].value
.float_value
= DBL_MIN
;
229 items
[INTERVAL_COLUMN
].type
= TABLE_ITEM_NONE
;
230 items
[INTERVAL_COLUMN
].value
.float_value
= -1.0;
231 items
[MESSAGE_RATE_COLUMN
].type
= TABLE_ITEM_NONE
;
232 items
[MESSAGE_RATE_COLUMN
].value
.float_value
= -1.0;
233 items
[BYTE_RATE_COLUMN
].type
= TABLE_ITEM_NONE
;
234 items
[BYTE_RATE_COLUMN
].value
.float_value
= -1.0;
235 stat_tap_init_table_row(table
, i
, num_fields
, items
);
240 static tap_packet_status
241 pingpongprotocol_stat_packet(void* tapdata
, packet_info
* pinfo _U_
, epan_dissect_t
* edt _U_
, const void* data
, tap_flags_t flags _U_
)
243 stat_data_t
* stat_data
= (stat_data_t
*)tapdata
;
244 const tap_pingpongprotocol_rec_t
* tap_rec
= (const tap_pingpongprotocol_rec_t
*)data
;
245 stat_tap_table
* table
;
246 stat_tap_table_item_type
* msg_data
;
251 double firstSeen
= -1.0;
252 double lastSeen
= -1.0;
254 idx
= str_to_val_idx(tap_rec
->type_string
, message_type_values
);
256 return TAP_PACKET_DONT_REDRAW
;
258 table
= g_array_index(stat_data
->stat_tap_data
->tables
, stat_tap_table
*, 0);
260 /* Update packets counter */
261 pingpongprotocol_total_msgs
++;
262 msg_data
= stat_tap_get_field_data(table
, idx
, MESSAGES_COLUMN
);
263 msg_data
->value
.uint_value
++;
264 messages
= msg_data
->value
.uint_value
;
265 stat_tap_set_field_data(table
, idx
, MESSAGES_COLUMN
, msg_data
);
267 /* Update bytes counter */
268 pingpongprotocol_total_bytes
+= tap_rec
->size
;
269 msg_data
= stat_tap_get_field_data(table
, idx
, BYTES_COLUMN
);
270 msg_data
->value
.uint_value
+= tap_rec
->size
;
271 bytes
= msg_data
->value
.uint_value
;
272 stat_tap_set_field_data(table
, idx
, BYTES_COLUMN
, msg_data
);
274 /* Update messages and bytes share */
275 while (message_type_values
[i
].strptr
) {
276 msg_data
= stat_tap_get_field_data(table
, i
, MESSAGES_COLUMN
);
277 const unsigned m
= msg_data
->value
.uint_value
;
278 msg_data
= stat_tap_get_field_data(table
, i
, BYTES_COLUMN
);
279 const unsigned b
= msg_data
->value
.uint_value
;
281 msg_data
= stat_tap_get_field_data(table
, i
, MESSAGES_SHARE_COLUMN
);
282 msg_data
->type
= TABLE_ITEM_FLOAT
;
283 msg_data
->value
.float_value
= 100.0 * m
/ (double)pingpongprotocol_total_msgs
;
284 stat_tap_set_field_data(table
, i
, MESSAGES_SHARE_COLUMN
, msg_data
);
286 msg_data
= stat_tap_get_field_data(table
, i
, BYTES_SHARE_COLUMN
);
287 msg_data
->type
= TABLE_ITEM_FLOAT
;
288 msg_data
->value
.float_value
= 100.0 * b
/ (double)pingpongprotocol_total_bytes
;
289 stat_tap_set_field_data(table
, i
, BYTES_SHARE_COLUMN
, msg_data
);
293 /* Update first seen time */
294 if (pinfo
->presence_flags
& PINFO_HAS_TS
) {
295 msg_data
= stat_tap_get_field_data(table
, idx
, FIRST_SEEN_COLUMN
);
296 msg_data
->type
= TABLE_ITEM_FLOAT
;
297 msg_data
->value
.float_value
= MIN(msg_data
->value
.float_value
, nstime_to_sec(&pinfo
->rel_ts
));
298 firstSeen
= msg_data
->value
.float_value
;
299 stat_tap_set_field_data(table
, idx
, FIRST_SEEN_COLUMN
, msg_data
);
302 /* Update last seen time */
303 if (pinfo
->presence_flags
& PINFO_HAS_TS
) {
304 msg_data
= stat_tap_get_field_data(table
, idx
, LAST_SEEN_COLUMN
);
305 msg_data
->type
= TABLE_ITEM_FLOAT
;
306 msg_data
->value
.float_value
= MAX(msg_data
->value
.float_value
, nstime_to_sec(&pinfo
->rel_ts
));
307 lastSeen
= msg_data
->value
.float_value
;
308 stat_tap_set_field_data(table
, idx
, LAST_SEEN_COLUMN
, msg_data
);
311 if ((lastSeen
- firstSeen
) > 0.0) {
312 /* Update interval */
313 msg_data
= stat_tap_get_field_data(table
, idx
, INTERVAL_COLUMN
);
314 msg_data
->type
= TABLE_ITEM_FLOAT
;
315 msg_data
->value
.float_value
= lastSeen
- firstSeen
;
316 stat_tap_set_field_data(table
, idx
, INTERVAL_COLUMN
, msg_data
);
318 /* Update message rate */
319 msg_data
= stat_tap_get_field_data(table
, idx
, MESSAGE_RATE_COLUMN
);
320 msg_data
->type
= TABLE_ITEM_FLOAT
;
321 msg_data
->value
.float_value
= messages
/ (lastSeen
- firstSeen
);
322 stat_tap_set_field_data(table
, idx
, MESSAGE_RATE_COLUMN
, msg_data
);
324 /* Update byte rate */
325 msg_data
= stat_tap_get_field_data(table
, idx
, BYTE_RATE_COLUMN
);
326 msg_data
->type
= TABLE_ITEM_FLOAT
;
327 msg_data
->value
.float_value
= bytes
/ (lastSeen
- firstSeen
);
328 stat_tap_set_field_data(table
, idx
, BYTE_RATE_COLUMN
, msg_data
);
331 return TAP_PACKET_REDRAW
;
335 pingpongprotocol_stat_reset(stat_tap_table
* table
)
338 stat_tap_table_item_type
* item_data
;
340 for (element
= 0; element
< table
->num_elements
; element
++) {
341 item_data
= stat_tap_get_field_data(table
, element
, MESSAGES_COLUMN
);
342 item_data
->value
.uint_value
= 0;
343 stat_tap_set_field_data(table
, element
, MESSAGES_COLUMN
, item_data
);
345 item_data
= stat_tap_get_field_data(table
, element
, MESSAGES_SHARE_COLUMN
);
346 item_data
->type
= TABLE_ITEM_NONE
;
347 item_data
->value
.float_value
= -1.0;
348 stat_tap_set_field_data(table
, element
, MESSAGES_SHARE_COLUMN
, item_data
);
350 item_data
= stat_tap_get_field_data(table
, element
, BYTES_COLUMN
);
351 item_data
->value
.uint_value
= 0;
352 stat_tap_set_field_data(table
, element
, BYTES_COLUMN
, item_data
);
354 item_data
= stat_tap_get_field_data(table
, element
, BYTES_SHARE_COLUMN
);
355 item_data
->type
= TABLE_ITEM_NONE
;
356 item_data
->value
.float_value
= -1.0;
357 stat_tap_set_field_data(table
, element
, BYTES_SHARE_COLUMN
, item_data
);
359 item_data
= stat_tap_get_field_data(table
, element
, FIRST_SEEN_COLUMN
);
360 item_data
->type
= TABLE_ITEM_NONE
;
361 item_data
->value
.float_value
= DBL_MAX
;
362 stat_tap_set_field_data(table
, element
, FIRST_SEEN_COLUMN
, item_data
);
364 item_data
= stat_tap_get_field_data(table
, element
, LAST_SEEN_COLUMN
);
365 item_data
->type
= TABLE_ITEM_NONE
;
366 item_data
->value
.float_value
= DBL_MIN
;
367 stat_tap_set_field_data(table
, element
, LAST_SEEN_COLUMN
, item_data
);
369 item_data
= stat_tap_get_field_data(table
, element
, INTERVAL_COLUMN
);
370 item_data
->type
= TABLE_ITEM_NONE
;
371 item_data
->value
.float_value
= -1.0;
372 stat_tap_set_field_data(table
, element
, INTERVAL_COLUMN
, item_data
);
374 item_data
= stat_tap_get_field_data(table
, element
, MESSAGE_RATE_COLUMN
);
375 item_data
->type
= TABLE_ITEM_NONE
;
376 item_data
->value
.float_value
= -1.0;
377 stat_tap_set_field_data(table
, element
, MESSAGE_RATE_COLUMN
, item_data
);
379 item_data
= stat_tap_get_field_data(table
, element
, BYTE_RATE_COLUMN
);
380 item_data
->type
= TABLE_ITEM_NONE
;
381 item_data
->value
.float_value
= -1.0;
382 stat_tap_set_field_data(table
, element
, BYTE_RATE_COLUMN
, item_data
);
384 pingpongprotocol_total_msgs
= 0;
385 pingpongprotocol_total_bytes
= 0;
389 /* Register the protocol with Wireshark */
391 proto_register_pingpongprotocol(void)
394 /* Setup list of header fields */
395 static hf_register_info hf
[] = {
396 { &hf_message_type
, { "Type", "pingpongprotocol.message_type", FT_UINT8
, BASE_DEC
, VALS(message_type_values
), 0x0, NULL
, HFILL
} },
397 { &hf_message_flags
, { "Flags", "pingpongprotocol.message_flags", FT_UINT8
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
398 { &hf_message_length
, { "Length", "pingpongprotocol.message_length", FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
399 { &hf_ping_messageno
, { "MessageNo", "pingpongprotocol.ping_messageno", FT_UINT64
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
400 { &hf_ping_data
, { "Ping_Data", "pingpongprotocol.ping_data", FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
} },
401 { &hf_pong_messageno
, { "MessageNo", "pingpongprotocol.pong_messageno", FT_UINT64
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
402 { &hf_pong_replyno
, { "ReplyNo", "pingpongprotocol.pong_replyno", FT_UINT64
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
403 { &hf_pong_data
, { "Pong_Data", "pingpongprotocol.pong_data", FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
} },
406 /* Setup protocol subtree array */
407 static int *ett
[] = {
408 &ett_pingpongprotocol
411 static tap_param pingpongprotocol_stat_params
[] = {
412 { PARAM_FILTER
, "filter", "Filter", NULL
, true }
415 static stat_tap_table_ui pingpongprotocol_stat_table
= {
416 REGISTER_STAT_GROUP_RSERPOOL
,
417 "PingPongProtocol Statistics",
419 "pingpongprotocol,stat",
420 pingpongprotocol_stat_init
,
421 pingpongprotocol_stat_packet
,
422 pingpongprotocol_stat_reset
,
425 array_length(pingpongprotocol_stat_fields
), pingpongprotocol_stat_fields
,
426 array_length(pingpongprotocol_stat_params
), pingpongprotocol_stat_params
,
431 /* Register the protocol name and description */
432 proto_pingpongprotocol
= proto_register_protocol("Ping Pong Protocol", "PingPongProtocol", "pingpongprotocol");
434 /* Required function calls to register the header fields and subtrees used */
435 proto_register_field_array(proto_pingpongprotocol
, hf
, array_length(hf
));
436 proto_register_subtree_array(ett
, array_length(ett
));
437 tap_pingpongprotocol
= register_tap("pingpongprotocol");
439 /* Register the dissector handle */
440 pingpongprotocol_handle
= register_dissector("pingpongprotocol", dissect_pingpongprotocol
, proto_pingpongprotocol
);
442 register_stat_tap_table_ui(&pingpongprotocol_stat_table
);
446 proto_reg_handoff_pingpongprotocol(void)
448 dissector_add_uint("sctp.ppi", PINGPONGPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY
, pingpongprotocol_handle
);
449 dissector_add_uint("sctp.ppi", PPP_PAYLOAD_PROTOCOL_ID
, pingpongprotocol_handle
);
453 * Editor modelines - https://www.wireshark.org/tools/modelines.html
458 * indent-tabs-mode: nil
461 * ex: set shiftwidth=2 tabstop=8 expandtab:
462 * :indentSize=2:tabSize=8:noTabs=true: