Bug Summary

File:builds/wireshark/wireshark/ui/io_graph_item.h
Warning:line 229, column 35
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sharkd_session.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-21-100318-3661-1 -x c /builds/wireshark/wireshark/sharkd_session.c

/builds/wireshark/wireshark/sharkd_session.c

→
1/* sharkd_session.c
2 *
3 * Copyright (C) 2016 Jakub Zawadzki
4 *
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12#include <config.h>
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <stdarg.h>
17#include <string.h>
18#include <errno(*__errno_location ()).h>
19#include <inttypes.h>
20
21#include <glib.h>
22
23#include <wsutil/wsjson.h>
24#include <wsutil/json_dumper.h>
25#include <wsutil/ws_assert.h>
26#include <wsutil/wsgcrypt.h>
27#include <wsutil/pint.h>
28
29#include <file.h>
30#include <epan/epan_dissect.h>
31#include <epan/exceptions.h>
32#include <epan/color_filters.h>
33#include <epan/prefs.h>
34#include <epan/prefs-int.h>
35#include <epan/uat-int.h>
36#include <wiretap/wtap.h>
37
38#include <epan/column.h>
39#include <epan/column-info.h>
40
41#include <ui/io_graph_item.h>
42#include <epan/stats_tree_priv.h>
43#include <epan/stat_tap_ui.h>
44#include <epan/conversation_table.h>
45#include <epan/sequence_analysis.h>
46#include <epan/expert.h>
47#include <epan/export_object.h>
48#include <epan/follow.h>
49#include <epan/rtd_table.h>
50#include <epan/srt_table.h>
51#include <epan/to_str.h>
52#include <epan/secrets.h>
53
54#include <epan/dissectors/packet-h225.h>
55#include <ui/voip_calls.h>
56#include <ui/rtp_stream.h>
57#include <ui/tap-rtp-common.h>
58#include <ui/tap-rtp-analysis.h>
59#include <ui/cli/tap-protohierstat.h>
60#include <ui/cli/tap-voip.h>
61#include <app/application_flavor.h>
62#include <epan/to_str.h>
63
64#include <epan/addr_resolv.h>
65#include <epan/dissectors/packet-rtp.h>
66#include <epan/dissectors/packet-rtp_pt.h>
67#include <ui/rtp_media.h>
68#include <ui/mcast_stream.h>
69#include <speex/speex_resampler.h>
70
71#include <epan/maxmind_db.h>
72
73#include <wsutil/pint.h>
74#include <wsutil/strnatcmp.h>
75#include <wsutil/strtoi.h>
76
77#include "globals.h"
78
79#include "sharkd.h"
80
81struct sharkd_filter_item
82{
83 uint8_t *filtered; /* can be NULL if all frames are matching for given filter. */
84};
85
86static GHashTable *filter_table;
87
88static int mode;
89static uint32_t rpcid;
90
91static json_dumper dumper;
92
93
94static const char *
95json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
96{
97 int i;
98
99 for (i = 0; i < count; i += 2)
100 {
101 const char *tok_attr = &buf[tokens[i + 0].start];
102 const char *tok_value = &buf[tokens[i + 1].start];
103
104 if (!strcmp(tok_attr, attr))
105 return tok_value;
106 }
107
108 return NULL((void*)0);
109}
110
111static void
112json_print_base64(const uint8_t *data, size_t len)
113{
114 json_dumper_begin_base64(&dumper);
115 json_dumper_write_base64(&dumper, data, len);
116 json_dumper_end_base64(&dumper);
117}
118
119static void G_GNUC_PRINTF(2, 3)__attribute__((__format__ (__printf__, 2, 3)))
120sharkd_json_value_anyf(const char *key, const char *format, ...)
121{
122 if (key)
123 json_dumper_set_member_name(&dumper, key);
124
125 va_list ap;
126 va_start(ap, format)__builtin_va_start(ap, format);
127 json_dumper_value_va_list(&dumper, format, ap);
128 va_end(ap)__builtin_va_end(ap);
129}
130
131static void
132sharkd_json_value_string(const char *key, const char *str)
133{
134 if (key)
135 json_dumper_set_member_name(&dumper, key);
136 json_dumper_value_string(&dumper, str);
137}
138
139static void
140sharkd_json_value_base64(const char *key, const uint8_t *data, size_t len)
141{
142 if (key)
143 json_dumper_set_member_name(&dumper, key);
144 json_print_base64(data, len);
145}
146
147static void G_GNUC_PRINTF(2, 3)__attribute__((__format__ (__printf__, 2, 3)))
148sharkd_json_value_stringf(const char *key, const char *format, ...)
149{
150 if (key)
151 json_dumper_set_member_name(&dumper, key);
152
153 va_list ap;
154 va_start(ap, format)__builtin_va_start(ap, format);
155 char* sformat = ws_strdup_printf("\"%s\"", format)wmem_strdup_printf(((void*)0), "\"%s\"", format);
156 json_dumper_value_va_list(&dumper, sformat, ap);
157 g_free(sformat)(__builtin_object_size ((sformat), 0) != ((size_t) - 1)) ? g_free_sized
(sformat, __builtin_object_size ((sformat), 0)) : (g_free) (
sformat)
;
158 va_end(ap)__builtin_va_end(ap);
159}
160
161static void
162sharkd_json_array_open(const char *key)
163{
164 if (key)
165 json_dumper_set_member_name(&dumper, key);
166 json_dumper_begin_array(&dumper);
167}
168
169static void
170sharkd_json_array_close(void)
171{
172 json_dumper_end_array(&dumper);
173}
174
175static void
176sharkd_json_object_open(const char *key)
177{
178 if (key)
179 json_dumper_set_member_name(&dumper, key);
180 json_dumper_begin_object(&dumper);
181}
182
183static void
184sharkd_json_object_close(void)
185{
186 json_dumper_end_object(&dumper);
187}
188
189static void
190sharkd_json_response_open(uint32_t id)
191{
192 json_dumper_begin_object(&dumper); // start the message
193 sharkd_json_value_string("jsonrpc", "2.0");
194 sharkd_json_value_anyf("id", "%d", id);
195}
196
197static void
198sharkd_json_response_close(void)
199{
200 json_dumper_end_object(&dumper); // end the message
201
202 json_dumper_finish(&dumper);
203
204 /*
205 * We do an explicit fflush after every line, because
206 * we want output to be written to the socket as soon
207 * as the line is complete.
208 *
209 * The stream is fully-buffered by default, so it's
210 * only flushed when the buffer fills or the FILE *
211 * is closed. On UN*X, we could set it to be line
212 * buffered, but the MSVC standard I/O routines don't
213 * support line buffering - they only support *byte*
214 * buffering, doing a write for every byte written,
215 * which is too inefficient, and full buffering,
216 * which is what you get if you request line buffering.
217 */
218 fflush(stdoutstdout);
219}
220
221static void
222sharkd_json_result_prologue(uint32_t id)
223{
224 sharkd_json_response_open(id);
225 sharkd_json_object_open("result"); // start the result object
226}
227
228static void
229sharkd_json_result_epilogue(void)
230{
231 json_dumper_end_object(&dumper); // end the result object
232 sharkd_json_response_close();
233}
234
235static void
236sharkd_json_result_array_prologue(uint32_t id)
237{
238 sharkd_json_response_open(id);
239 sharkd_json_array_open("result"); // start the result array
240}
241
242static void
243sharkd_json_result_array_epilogue(void)
244{
245 sharkd_json_array_close(); // end of result array
246 sharkd_json_response_close();
247}
248
249static void
250sharkd_json_simple_ok(uint32_t id)
251{
252 sharkd_json_result_prologue(id);
253 sharkd_json_value_string("status", "OK");
254 sharkd_json_result_epilogue();
255}
256
257static void
258sharkd_json_warning(uint32_t id, char *warning)
259{
260 sharkd_json_result_prologue(id);
261 sharkd_json_value_string("status", "Warning");
262 sharkd_json_value_string("warning", warning);
263 sharkd_json_result_epilogue();
264}
265
266static void G_GNUC_PRINTF(4, 5)__attribute__((__format__ (__printf__, 4, 5)))
267sharkd_json_error(uint32_t id, int code, char* data, char* format, ...)
268{
269 sharkd_json_response_open(id);
270 sharkd_json_object_open("error");
271 sharkd_json_value_anyf("code", "%d", code);
272
273 if (format)
274 {
275 // format the text message
276 va_list args;
277
278 va_start(args, format)__builtin_va_start(args, format);
279 char *error_msg = ws_strdup_vprintf(format, args)wmem_strdup_vprintf(((void*)0), format, args);
280 va_end(args)__builtin_va_end(args);
281
282 sharkd_json_value_string("message", error_msg);
283
284 g_free(error_msg)(__builtin_object_size ((error_msg), 0) != ((size_t) - 1)) ? g_free_sized
(error_msg, __builtin_object_size ((error_msg), 0)) : (g_free
) (error_msg)
;
285 }
286
287 sharkd_json_object_close();
288
289 if (data)
290 sharkd_json_value_string("data", data);
291
292 sharkd_json_response_close();
293}
294
295static bool_Bool
296is_param_match(const char *param_in, const char *valid_param)
297{
298 char* ptr;
299
300 if ((ptr = g_strrstr(valid_param, "*")))
301 {
302 size_t prefix_len = ptr - valid_param;
303 return !strncmp(param_in, valid_param, prefix_len);
304 }
305 else
306 return !strcmp(param_in, valid_param);
307}
308
309/*
310 * json_prep does four things:
311 *
312 * 1. check the syntax of the root and parameter members
313 * 2. tokenize the names and values by zero terminating them
314 * 3. unescape the names and values
315 * 4. extracts and saves the rpcid
316 * - we have to do it here as it's needed for the error messages
317 *
318 * The objective is to minimise the validation work in the functions
319 * that process each called method.
320 *
321 * This gets a little messy as the JSON parser creates a flat list
322 * of all members rather than create a tree.
323 */
324static bool_Bool
325json_prep(char* buf, const jsmntok_t* tokens, int count)
326{
327 int i;
328 const char* method = NULL((void*)0);
329 char* attr_name = NULL((void*)0);
330 char* attr_value = NULL((void*)0);
331
332#define SHARKD_JSON_ANY0 0
333#define SHARKD_JSON_STRING1 1
334#define SHARKD_JSON_INTEGER2 2
335#define SHARKD_JSON_UINTEGER3 3
336#define SHARKD_JSON_FLOAT4 4
337#define SHARKD_JSON_OBJECT5 5
338#define SHARKD_JSON_ARRAY6 6
339#define SHARKD_JSON_BOOLEAN7 7
340#define SHARKD_ARRAY_END99 99
341
342 struct member_attribute {
343 const char* parent_ctx;
344 const char* name;
345 int level;
346 jsmntype_t type;
347 int value_type;
348 bool_Bool is_mandatory;
349 };
350
351#define SHARKD_MANDATORY1 true1
352#define SHARKD_OPTIONAL0 false0
353
354 /*
355 * The member attribute structure is key to the syntax checking. The
356 * array contains all of the root level (1) member names, the data
357 * types permissible for the value and a boolean that indicates whether
358 * or not the member is mandatory.
359 *
360 * Once we get into the next layer (2) of the json tree, we need to check
361 * params member names and data types dependent in the context of the method
362 * (parent_ctx).
363 */
364
365 struct member_attribute name_array[] = {
366 // Root members
367 {NULL((void*)0), "jsonrpc", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
368 {NULL((void*)0), "userid", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
369 {NULL((void*)0), "id", 1, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
370 {NULL((void*)0), "method", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
371 {NULL((void*)0), "params", 1, JSMN_OBJECT, SHARKD_JSON_OBJECT5, SHARKD_OPTIONAL0},
372
373 // Valid methods
374 {"method", "analyse", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
375 {"method", "bye", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
376 {"method", "check", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
377 {"method", "complete", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
378 {"method", "download", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
379 {"method", "dumpconf", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
380 {"method", "follow", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
381 {"method", "field", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
382 {"method", "fields", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
383 {"method", "frame", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
384 {"method", "frames", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
385 {"method", "info", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
386 {"method", "intervals", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
387 {"method", "iograph", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
388 {"method", "load", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
389 {"method", "setcomment", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
390 {"method", "setconf", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
391 {"method", "status", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
392 {"method", "tap", 1, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
393
394 // Parameters and their method context
395 {"check", "field", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
396 {"check", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
397 {"complete", "field", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
398 {"complete", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
399 {"download", "token", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
400 {"dumpconf", "pref", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
401 {"follow", "follow", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
402 {"follow", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
403 {"follow", "sub_stream", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
404 {"field", "name", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
405 {"frame", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
406 {"frame", "proto", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
407 {"frame", "ref_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
408 {"frame", "prev_frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
409 {"frame", "columns", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
410 {"frame", "color", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
411 {"frame", "bytes", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
412 {"frame", "hidden", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
413 {"frames", "column*", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY0, SHARKD_OPTIONAL0},
414 {"frames", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
415 {"frames", "skip", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
416 {"frames", "limit", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
417 {"frames", "refs", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
418 {"intervals", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
419 {"intervals", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
420 {"iograph", "interval", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
421 {"iograph", "interval_units", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
422 {"iograph", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
423 {"iograph", "graph0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
424 {"iograph", "graph1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
425 {"iograph", "graph2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
426 {"iograph", "graph3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
427 {"iograph", "graph4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
428 {"iograph", "graph5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
429 {"iograph", "graph6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
430 {"iograph", "graph7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
431 {"iograph", "graph8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
432 {"iograph", "graph9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
433 {"iograph", "filter0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
434 {"iograph", "filter1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
435 {"iograph", "filter2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
436 {"iograph", "filter3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
437 {"iograph", "filter4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
438 {"iograph", "filter5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
439 {"iograph", "filter6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
440 {"iograph", "filter7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
441 {"iograph", "filter8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
442 {"iograph", "filter9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
443 {"iograph", "aot0", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
444 {"iograph", "aot1", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
445 {"iograph", "aot2", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
446 {"iograph", "aot3", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
447 {"iograph", "aot4", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
448 {"iograph", "aot5", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
449 {"iograph", "aot6", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
450 {"iograph", "aot7", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
451 {"iograph", "aot8", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
452 {"iograph", "aot9", 2, JSMN_PRIMITIVE, SHARKD_JSON_BOOLEAN7, SHARKD_OPTIONAL0},
453 {"load", "file", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
454 {"load", "max_packets", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
455 {"load", "max_bytes", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_OPTIONAL0},
456 {"setcomment", "frame", 2, JSMN_PRIMITIVE, SHARKD_JSON_UINTEGER3, SHARKD_MANDATORY1},
457 {"setcomment", "comment", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
458 {"setconf", "name", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
459 {"setconf", "value", 2, JSMN_UNDEFINED, SHARKD_JSON_ANY0, SHARKD_MANDATORY1},
460 {"tap", "tap0", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_MANDATORY1},
461 {"tap", "tap1", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
462 {"tap", "tap2", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
463 {"tap", "tap3", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
464 {"tap", "tap4", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
465 {"tap", "tap5", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
466 {"tap", "tap6", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
467 {"tap", "tap7", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
468 {"tap", "tap8", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
469 {"tap", "tap9", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
470 {"tap", "tap10", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
471 {"tap", "tap11", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
472 {"tap", "tap12", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
473 {"tap", "tap13", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
474 {"tap", "tap14", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
475 {"tap", "tap15", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
476 {"tap", "filter", 2, JSMN_STRING, SHARKD_JSON_STRING1, SHARKD_OPTIONAL0},
477
478 // End of the name_array
479 {NULL((void*)0), NULL((void*)0), 0, JSMN_STRING, SHARKD_ARRAY_END99, SHARKD_OPTIONAL0},
480 };
481
482 rpcid = 0;
483
484 /* sanity check, and split strings */
485 if (count < 1 || tokens[0].type != JSMN_OBJECT)
486 {
487 sharkd_json_error(
488 rpcid, -32600, NULL((void*)0),
489 "The request must an object"
490 );
491 return false0;
492 }
493
494 /* don't need [0] token */
495 tokens++;
496 count--;
497
498 if (count & 1)
499 {
500 sharkd_json_error(
501 rpcid, -32600, NULL((void*)0),
502 "The request must contain name/value pairs"
503 );
504 return false0;
505 }
506
507 for (i = 0; i < count; i += 2)
508 {
509 buf[tokens[i + 0].end] = '\0';
510 buf[tokens[i + 1].end] = '\0';
511 }
512
513 // we must get the id as soon as possible so that it's available in all future error messages
514 attr_value = (char*)json_find_attr(buf, tokens, count, "id");
515 if (attr_value)
516 {
517 if (!ws_strtou32(attr_value, NULL((void*)0), &rpcid))
518 {
519 sharkd_json_error(
520 rpcid, -32600, NULL((void*)0),
521 "The id value must be a positive integer"
522 );
523 return false0;
524 }
525 }
526
527 method = json_find_attr(buf, tokens, count, "method");
528
529 if (method)
530 {
531 bool_Bool is_supported = false0;
532 i = 0; // name array index
533
534 // check that the request method is good
535 while (name_array[i].value_type != SHARKD_ARRAY_END99)
536 {
537 if (name_array[i].parent_ctx)
538 {
539 if (!strcmp(method, name_array[i].name) && !strcmp(name_array[i].parent_ctx, "method"))
540 is_supported = true1; // the method is valid
541 }
542
543 i++;
544 }
545
546 if (!is_supported)
547 {
548 sharkd_json_error(
549 rpcid, -32601, NULL((void*)0),
550 "The method %s is not supported", method
551 );
552 return false0;
553 }
554 }
555
556 for (i = 0; i < count; i += 2)
557 {
558 if (tokens[i].type != JSMN_STRING)
559 {
560 sharkd_json_error(
561 rpcid, -32600, NULL((void*)0),
562 "Member names must be a string - member %d is not string", (i / 2) + 1
563 );
564 return false0;
565 }
566
567 attr_name = &buf[tokens[i + 0].start];
568 attr_value = &buf[tokens[i + 1].start];
569
570 if (!strcmp(attr_name, "jsonrpc"))
571 {
572 if (strcmp(&buf[tokens[i + 1].start], "2.0"))
573 {
574 sharkd_json_error(
575 rpcid, -32600, NULL((void*)0),
576 "Only JSON %s is supported", "2.0"
577 );
578 return false0;
579 }
580 }
581
582 /* unescape only value, as keys are simple strings */
583 if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(attr_value))
584 {
585 sharkd_json_error(
586 rpcid, -32600, NULL((void*)0),
587 "Cannot unescape the value string of member %d", (i / 2) + 1
588 );
589 return false0;
590 }
591
592 /* Confirm that the member is valid */
593 bool_Bool match = false0;
594
595 // We need to check root members (level 1) and parameters (level 2), hence the for loop.
596
597 for (int level = 1; level < 3; level++)
598 {
599 size_t j = 0;
600
601 while (name_array[j].value_type != SHARKD_ARRAY_END99) // iterate through the array until we hit the end
602 {
603 if (is_param_match(attr_name, name_array[j].name) && name_array[j].level == level)
604 {
605 // We need to be sure the match is in the correct context
606 // i.e. is this a match for a root member (level 1) or for a parameter (level 2).
607
608 if (level == 1)
609 {
610 // need to guard against a parameter name matching a method name
611 if (method)
612 {
613 if (name_array[j].parent_ctx)
614 {
615 j++;
616 continue;
617 }
618
619 if (!strcmp(method, &buf[tokens[i + 0].start]))
620 {
621 j++;
622 continue;
623 }
624 }
625
626 match = true1;
627 }
628 else if (method)
629 {
630 if (level == 2 && !strcmp(name_array[j].parent_ctx, method))
631 match = true1;
632 else
633 {
634 j++;
635 continue;
636 }
637 }
638 else
639 {
640 j++;
641 continue;
642 }
643
644 // The match looks good, let's now check the data types
645
646 if (tokens[i + 1].type != name_array[j].type && name_array[j].type != SHARKD_JSON_ANY0)
647 {
648 sharkd_json_error(
649 rpcid, -32600, NULL((void*)0),
650 "The data type for member %s is not valid", attr_name
651 );
652 return false0;
653 }
654 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_UINTEGER3)
655 {
656 uint32_t temp;
657 if (!ws_strtou32(attr_value, NULL((void*)0), &temp) || temp <= 0)
658 {
659 sharkd_json_error(
660 rpcid, -32600, NULL((void*)0),
661 "The value for %s must be a positive integer", name_array[j].name
662 );
663 return false0;
664 }
665 }
666 else if (name_array[j].type == JSMN_PRIMITIVE && name_array[j].value_type == SHARKD_JSON_BOOLEAN7)
667 {
668 if (strcmp(attr_value, "true") && strcmp(attr_value, "false"))
669 {
670 sharkd_json_error(
671 rpcid, -32600, NULL((void*)0),
672 "The value for %s must be a boolean (true or false)", name_array[j].name
673 );
674 return false0;
675 }
676
677 }
678 break; // looks like a valid match
679 }
680 j++;
681 }
682 }
683
684 if (!match)
685 {
686 sharkd_json_error(
687 rpcid, -32600, NULL((void*)0),
688 "%s is not a valid member name", attr_name
689 );
690 return false0;
691 }
692 }
693
694 /* check for mandatory members */
695 size_t j = 0;
696
697 while (name_array[j].value_type != SHARKD_ARRAY_END99)
698 {
699 if (name_array[j].is_mandatory && name_array[j].level == 1)
700 {
701 if (!json_find_attr(buf, tokens, count, name_array[j].name))
702 {
703 sharkd_json_error(
704 rpcid, -32600, NULL((void*)0),
705 "Mandatory member %s is missing", name_array[j].name
706 );
707 return false0;
708 }
709 }
710 j++;
711 }
712
713 // check that the current request contains the mandatory parameters
714 j = 0;
715
716 while (name_array[j].value_type != SHARKD_ARRAY_END99)
717 {
718 if (name_array[j].is_mandatory && name_array[j].level == 2 && !strcmp(method, name_array[j].parent_ctx))
719 {
720 if (!json_find_attr(buf, tokens, count, name_array[j].name))
721 {
722 sharkd_json_error(
723 rpcid, -32600, NULL((void*)0),
724 "Mandatory parameter %s is missing", name_array[j].name
725 );
726 return false0;
727 }
728 }
729 j++;
730 }
731
732
733 // check that the parameters for the current request are valid for the method and that the data type for the value is valid
734
735 return true1;
736}
737
738static void
739sharkd_session_filter_free(void *data)
740{
741 struct sharkd_filter_item *l = (struct sharkd_filter_item *) data;
742
743 g_free(l->filtered)(__builtin_object_size ((l->filtered), 0) != ((size_t) - 1
)) ? g_free_sized (l->filtered, __builtin_object_size ((l->
filtered), 0)) : (g_free) (l->filtered)
;
744 g_free(l)(__builtin_object_size ((l), 0) != ((size_t) - 1)) ? g_free_sized
(l, __builtin_object_size ((l), 0)) : (g_free) (l)
;
745}
746
747static const struct sharkd_filter_item *
748sharkd_session_filter_data(const char *filter)
749{
750 struct sharkd_filter_item *l;
751
752 l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter);
753 if (!l)
754 {
755 uint8_t *filtered = NULL((void*)0);
756
757 int ret = sharkd_filter(filter, &filtered);
758
759 if (ret == -1)
760 return NULL((void*)0);
761
762 l = g_new(struct sharkd_filter_item, 1)((struct sharkd_filter_item *) g_malloc_n ((1), sizeof (struct
sharkd_filter_item)))
;
763 l->filtered = filtered;
764
765 g_hash_table_insert(filter_table, g_strdup(filter)g_strdup_inline (filter), l);
766 }
767
768 return l;
769}
770
771static bool_Bool
772sharkd_rtp_match_init(rtpstream_id_t *id, const char *init_str)
773{
774 bool_Bool ret = false0;
775 char **arr;
776 uint32_t tmp_addr_src, tmp_addr_dst;
777 address tmp_src_addr, tmp_dst_addr;
778
779 memset(id, 0, sizeof(*id));
780
781 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
782 if (g_strv_length(arr) != 5)
783 goto fail;
784
785 /* TODO, for now only IPv4 */
786 if (!get_host_ipaddr(arr[0], &tmp_addr_src))
787 goto fail;
788
789 if (!ws_strtou16(arr[1], NULL((void*)0), &id->src_port))
790 goto fail;
791
792 if (!get_host_ipaddr(arr[2], &tmp_addr_dst))
793 goto fail;
794
795 if (!ws_strtou16(arr[3], NULL((void*)0), &id->dst_port))
796 goto fail;
797
798 if (!ws_hexstrtou32(arr[4], NULL((void*)0), &id->ssrc))
799 goto fail;
800
801 set_address(&tmp_src_addr, AT_IPv4, 4, &tmp_addr_src);
802 copy_address(&id->src_addr, &tmp_src_addr);
803 set_address(&tmp_dst_addr, AT_IPv4, 4, &tmp_addr_dst);
804 copy_address(&id->dst_addr, &tmp_dst_addr);
805
806 ret = true1;
807
808fail:
809 g_strfreev(arr);
810 return ret;
811}
812
813static bool_Bool
814sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata _U___attribute__((unused)))
815{
816 stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
817
818 json_dumper_begin_object(&dumper);
819 sharkd_json_value_string("name", stat_tap->title);
820 sharkd_json_value_stringf("tap", "nstat:%s", (const char *) key);
821 json_dumper_end_object(&dumper);
822
823 return false0;
824}
825
826static bool_Bool
827sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata _U___attribute__((unused)))
828{
829 struct register_ct *table = (struct register_ct *) value;
830
831 const char *label = (const char *) key;
832
833 if (get_conversation_packet_func(table))
834 {
835 json_dumper_begin_object(&dumper);
836 sharkd_json_value_stringf("name", "Conversation List/%s", label);
837 sharkd_json_value_stringf("tap", "conv:%s", label);
838 json_dumper_end_object(&dumper);
839 }
840
841 if (get_endpoint_packet_func(table))
842 {
843 json_dumper_begin_object(&dumper);
844 sharkd_json_value_stringf("name", "Endpoint/%s", label);
845 sharkd_json_value_stringf("tap", "endpt:%s", label);
846 json_dumper_end_object(&dumper);
847 }
848 return false0;
849}
850
851static bool_Bool
852sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata _U___attribute__((unused)))
853{
854 register_analysis_t *analysis = (register_analysis_t *) value;
855
856 json_dumper_begin_object(&dumper);
857 sharkd_json_value_string("name", sequence_analysis_get_ui_name(analysis));
858 sharkd_json_value_stringf("tap", "seqa:%s", (const char *) key);
859 json_dumper_end_object(&dumper);
860
861 return false0;
862}
863
864static bool_Bool
865sharkd_export_object_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
866{
867 register_eo_t *eo = (register_eo_t *) value;
868
869 const int proto_id = get_eo_proto_id(eo);
870 const char *filter = proto_get_protocol_filter_name(proto_id);
871 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
872
873 json_dumper_begin_object(&dumper);
874 sharkd_json_value_stringf("name", "Export Object/%s", label);
875 sharkd_json_value_stringf("tap", "eo:%s", filter);
876 json_dumper_end_object(&dumper);
877
878 return false0;
879}
880
881static bool_Bool
882sharkd_srt_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
883{
884 register_srt_t *srt = (register_srt_t *) value;
885
886 const int proto_id = get_srt_proto_id(srt);
887 const char *filter = proto_get_protocol_filter_name(proto_id);
888 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
889
890 json_dumper_begin_object(&dumper);
891 sharkd_json_value_stringf("name", "Service Response Time/%s", label);
892 sharkd_json_value_stringf("tap", "srt:%s", filter);
893 json_dumper_end_object(&dumper);
894
895 return false0;
896}
897
898static bool_Bool
899sharkd_rtd_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
900{
901 register_rtd_t *rtd = (register_rtd_t *) value;
902
903 const int proto_id = get_rtd_proto_id(rtd);
904 const char *filter = proto_get_protocol_filter_name(proto_id);
905 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
906
907 json_dumper_begin_object(&dumper);
908 sharkd_json_value_stringf("name", "Response Time Delay/%s", label);
909 sharkd_json_value_stringf("tap", "rtd:%s", filter);
910 json_dumper_end_object(&dumper);
911
912 return false0;
913}
914
915static bool_Bool
916sharkd_follower_visit_cb(const void *key _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
917{
918 register_follow_t *follower = (register_follow_t *) value;
919
920 const int proto_id = get_follow_proto_id(follower);
921 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
922 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
923
924 json_dumper_begin_object(&dumper);
925 sharkd_json_value_stringf("name", "Follow/%s", label);
926 sharkd_json_value_stringf("tap", "follow:%s", filter);
927 json_dumper_end_object(&dumper);
928
929 return false0;
930}
931
932static void
933sharkd_session_print_capture_types(void)
934{
935 unsigned i;
936 GArray *writable_type_subtypes;
937 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
938 for (i = 0; i < writable_type_subtypes->len; i++) {
939 int ft = g_array_index(writable_type_subtypes, int, i)(((int*) (void *) (writable_type_subtypes)->data) [(i)]);
940 sharkd_json_object_open(NULL((void*)0));
941 sharkd_json_value_string("name", wtap_file_type_subtype_name(ft));
942 sharkd_json_value_string("description", wtap_file_type_subtype_description(ft));
943 sharkd_json_object_close();
944 }
945 g_array_free(writable_type_subtypes, TRUE(!(0)));
946}
947
948struct encap_type_info
949{
950 const char *name;
951 const char *description;
952};
953
954static int
955encap_type_info_nat_compare(const void *a, const void *b)
956{
957 return ws_ascii_strnatcmp(((const struct encap_type_info *)a)->name,
958 ((const struct encap_type_info *)b)->name);
959}
960
961static void
962encap_type_info_visit(void *data, void *user_data _U___attribute__((unused)))
963{
964 sharkd_json_object_open(NULL((void*)0));
965 sharkd_json_value_string("name", ((struct encap_type_info *)data)->name);
966 sharkd_json_value_string("description", ((struct encap_type_info *)data)->description);
967 sharkd_json_object_close();
968}
969
970static void
971sharkd_session_print_encap_types(void)
972{
973 int i;
974 struct encap_type_info *encaps;
975 GSList *list = NULL((void*)0);
976 encaps = g_new(struct encap_type_info, WTAP_NUM_ENCAP_TYPES)((struct encap_type_info *) g_malloc_n ((wtap_get_num_encap_types
()), sizeof (struct encap_type_info)))
;
977 for (i = 0; i < WTAP_NUM_ENCAP_TYPESwtap_get_num_encap_types(); i++) {
978 encaps[i].name = wtap_encap_name(i);
979 if (encaps[i].name != NULL((void*)0)) {
980 encaps[i].description = wtap_encap_description(i);
981 list = g_slist_insert_sorted(list, &encaps[i], encap_type_info_nat_compare);
982 }
983 }
984 g_slist_foreach(list, encap_type_info_visit, NULL((void*)0));
985 g_slist_free(list);
986 g_free(encaps)(__builtin_object_size ((encaps), 0) != ((size_t) - 1)) ? g_free_sized
(encaps, __builtin_object_size ((encaps), 0)) : (g_free) (encaps
)
;
987}
988
989/**
990 * sharkd_session_process_info()
991 *
992 * Process info request
993 *
994 * Output object with attributes:
995 * (m) version - version number
996 *
997 * (m) columns - available column formats, array of object with attributes:
998 * 'name' - column name
999 * 'format' - column format-name
1000 *
1001 * (m) stats - available statistics, array of object with attributes:
1002 * 'name' - statistic name
1003 * 'tap' - sharkd tap-name for statistic
1004 *
1005 * (m) convs - available conversation list, array of object with attributes:
1006 * 'name' - conversation name
1007 * 'tap' - sharkd tap-name for conversation
1008 *
1009 * (m) eo - available export object list, array of object with attributes:
1010 * 'name' - export object name
1011 * 'tap' - sharkd tap-name for eo
1012 *
1013 * (m) srt - available service response time list, array of object with attributes:
1014 * 'name' - service response time name
1015 * 'tap' - sharkd tap-name for srt
1016 *
1017 * (m) rtd - available response time delay list, array of object with attributes:
1018 * 'name' - response time delay name
1019 * 'tap' - sharkd tap-name for rtd
1020 *
1021 * (m) seqa - available sequence analysis (flow) list, array of object with attributes:
1022 * 'name' - sequence analysis name
1023 * 'tap' - sharkd tap-name
1024 *
1025 * (m) taps - available taps, array of object with attributes:
1026 * 'name' - tap name
1027 * 'tap' - sharkd tap-name
1028 *
1029 * (m) follow - available followers, array of object with attributes:
1030 * 'name' - tap name
1031 * 'tap' - sharkd tap-name
1032 *
1033 * (m) ftypes - conversation table for FT_ number to string, array of FT_xxx strings.
1034 *
1035 * (m) nstat - available table-based taps, array of object with attributes:
1036 * 'name' - tap name
1037 * 'tap' - sharkd tap-name
1038 *
1039 * (m) capture_types - available capture types, array of object with attributes:
1040 * 'name' - capture type name
1041 * 'description' - capture type description
1042 *
1043 * (m) encap_types - available encapsulation types, array of object with attributes:
1044 * 'name' - encapsulation type name
1045 * 'description' - encapsulation type description
1046 */
1047static void
1048sharkd_session_process_info(void)
1049{
1050 int i;
1051
1052 sharkd_json_result_prologue(rpcid);
1053
1054 sharkd_json_array_open("columns");
1055 for (i = 0; i < NUM_COL_FMTS; i++)
1056 {
1057 const char *col_format = col_format_to_string(i);
1058 const char *col_descr = col_format_desc(i);
1059
1060 json_dumper_begin_object(&dumper);
1061 sharkd_json_value_string("name", col_descr);
1062 sharkd_json_value_string("format", col_format);
1063 json_dumper_end_object(&dumper);
1064 }
1065 sharkd_json_array_close();
1066
1067 sharkd_json_array_open("stats");
1068 {
1069 GList *cfg_list = stats_tree_get_cfg_list();
1070 GList *l;
1071
1072 for (l = cfg_list; l; l = l->next)
1073 {
1074 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
1075
1076 json_dumper_begin_object(&dumper);
1077 sharkd_json_value_string("name", cfg->title);
1078 sharkd_json_value_stringf("tap", "stat:%s", cfg->abbr);
1079 json_dumper_end_object(&dumper);
1080 }
1081
1082 g_list_free(cfg_list);
1083 }
1084 sharkd_json_array_close();
1085
1086 sharkd_json_array_open("ftypes");
1087 for (i = 0; i < FT_NUM_TYPES; i++)
1088 sharkd_json_value_string(NULL((void*)0), ftype_name((ftenum_t) i));
1089 sharkd_json_array_close();
1090
1091 sharkd_json_array_open("capture_types");
1092 sharkd_session_print_capture_types();
1093 sharkd_json_array_close();
1094
1095 sharkd_json_array_open("encap_types");
1096 sharkd_session_print_encap_types();
1097 sharkd_json_array_close();
1098
1099 sharkd_json_value_string("version", application_get_vcs_version_info_short());
1100
1101 sharkd_json_array_open("nstat");
1102 i = 0;
1103 stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
1104 sharkd_json_array_close();
1105
1106 sharkd_json_array_open("convs");
1107 i = 0;
1108 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
1109 sharkd_json_array_close();
1110
1111 sharkd_json_array_open("seqa");
1112 i = 0;
1113 sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
1114 sharkd_json_array_close();
1115
1116 sharkd_json_array_open("taps");
1117 {
1118 json_dumper_begin_object(&dumper);
1119 sharkd_json_value_string("name", "UDP Multicast Streams");
1120 sharkd_json_value_string("tap", "multicast");
1121 json_dumper_end_object(&dumper);
1122
1123 json_dumper_begin_object(&dumper);
1124 sharkd_json_value_string("name", "RTP streams");
1125 sharkd_json_value_string("tap", "rtp-streams");
1126 json_dumper_end_object(&dumper);
1127
1128 json_dumper_begin_object(&dumper);
1129 sharkd_json_value_string("name", "Protocol Hierarchy Statistics");
1130 sharkd_json_value_string("tap", "phs");
1131 json_dumper_end_object(&dumper);
1132
1133 json_dumper_begin_object(&dumper);
1134 sharkd_json_value_string("name", "VoIP Calls");
1135 sharkd_json_value_string("tap", "voip-calls");
1136 json_dumper_end_object(&dumper);
1137
1138 json_dumper_begin_object(&dumper);
1139 sharkd_json_value_string("name", "VoIP Conversations");
1140 sharkd_json_value_string("tap", "voip-convs");
1141 json_dumper_end_object(&dumper);
1142
1143 json_dumper_begin_object(&dumper);
1144 sharkd_json_value_string("name", "Expert Information");
1145 sharkd_json_value_string("tap", "expert");
1146 json_dumper_end_object(&dumper);
1147 }
1148 sharkd_json_array_close();
1149
1150 sharkd_json_array_open("eo");
1151 i = 0;
1152 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
1153 sharkd_json_array_close();
1154
1155 sharkd_json_array_open("srt");
1156 i = 0;
1157 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
1158 sharkd_json_array_close();
1159
1160 sharkd_json_array_open("rtd");
1161 i = 0;
1162 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
1163 sharkd_json_array_close();
1164
1165 sharkd_json_array_open("follow");
1166 i = 0;
1167 follow_iterate_followers(sharkd_follower_visit_cb, &i);
1168 sharkd_json_array_close();
1169
1170 sharkd_json_result_epilogue();
1171}
1172
1173static void sharkd_session_print_field(header_field_info* current_header_field_info, bool_Bool as_object)
1174{
1175 if (as_object)
1176 {
1177 sharkd_json_object_open(NULL((void*)0));
1178 }
1179
1180 sharkd_json_value_stringf("id", "%i", current_header_field_info->id);
1181 sharkd_json_value_stringf("parent_id", "%i", current_header_field_info->parent);
1182 sharkd_json_value_string("name", current_header_field_info->abbrev);
1183 sharkd_json_value_string("display_name", current_header_field_info->name);
1184 sharkd_json_value_string("type", ftype_name(current_header_field_info->type));
1185
1186 if (as_object)
1187 {
1188 sharkd_json_object_close();
1189 }
1190}
1191
1192static void
1193sharkd_session_print_fields(void)
1194{
1195 void* proto_cookie = NULL((void*)0);
1196 void* field_cookie = NULL((void*)0);
1197 int protocol_id = -1;
1198
1199 for (protocol_id = proto_get_first_protocol(&proto_cookie); protocol_id != -1; protocol_id = proto_get_next_protocol(&proto_cookie))
1200 {
1201 protocol_t* protocol = find_protocol_by_id(protocol_id);
1202 if (!proto_is_protocol_enabled(protocol))
1203 {
1204 continue;
1205 }
1206
1207 header_field_info* current_header_field_info = proto_registrar_get_nth(proto_get_id(protocol));
1208
1209 sharkd_session_print_field(current_header_field_info, true1);
1210
1211 for (current_header_field_info = proto_get_first_protocol_field(protocol_id, &field_cookie); current_header_field_info != NULL((void*)0); current_header_field_info = proto_get_next_protocol_field(protocol_id, &field_cookie))
1212 {
1213 if (current_header_field_info->same_name_prev_id != -1)
1214 {
1215 continue;
1216 }
1217
1218 sharkd_session_print_field(current_header_field_info, true1);
1219 }
1220 }
1221}
1222
1223/**
1224 * sharkd_session_process_fields()
1225 *
1226 * Process fields request
1227 *
1228 * Output object with attributes:
1229 * (m) fields - all fields with their id, name, display_name, type and id of their parent
1230 */
1231static void
1232sharkd_session_process_fields(void)
1233{
1234 sharkd_json_result_prologue(rpcid);
1235
1236 sharkd_json_array_open("fields");
1237 sharkd_session_print_fields();
1238 sharkd_json_array_close();
1239
1240 sharkd_json_result_epilogue();
1241}
1242
1243/**
1244 * sharkd_session_process_field()
1245 *
1246 * Process field request
1247 *
1248 * Input:
1249 * (m) name - (filter) name of the field
1250 *
1251 * Output object with attributes:
1252 * (m) id - id of the field
1253 * (m) parent_id - id of the field's parent
1254 * (m) name - name of the field
1255 * (m) display_name - display_name of the field
1256 * (m) type - type of the field
1257 */
1258static void
1259sharkd_session_process_field(const char* buf, const jsmntok_t* tokens, int count)
1260{
1261 const char* tok_name = json_find_attr(buf, tokens, count, "name");
1262
1263 sharkd_json_result_prologue(rpcid);
1264
1265 if (tok_name)
1266 {
1267 header_field_info* current_header_field_info = proto_registrar_get_byname(tok_name);
1268 if (current_header_field_info == NULL((void*)0))
1269 {
1270 sharkd_json_error(
1271 rpcid, -32602, NULL((void*)0),
1272 "Unknown field name"
1273 );
1274 return;
1275 }
1276
1277 sharkd_session_print_field(current_header_field_info, false0);
1278 }
1279
1280 sharkd_json_result_epilogue();
1281}
1282
1283/**
1284 * sharkd_session_process_load()
1285 *
1286 * Process load request
1287 *
1288 * Input:
1289 * (m) file - file to be loaded
1290 *
1291 * Output object with attributes:
1292 * (m) err - error code
1293 */
1294static void
1295sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
1296{
1297 const char *tok_file = json_find_attr(buf, tokens, count, "file");
1298 const char *tok_max_packets = json_find_attr(buf, tokens, count, "max_packets");
1299 const char *tok_max_bytes = json_find_attr(buf, tokens, count, "max_bytes");
1300 int err = 0;
1301
1302 uint32_t max_packets = 0; /* 0 means unlimited */
1303 uint64_t max_bytes = 0; /* 0 means unlimited */
1304
1305 if (!tok_file)
1306 return;
1307
1308 /* Parse optional max_packets parameter */
1309 if (tok_max_packets)
1310 {
1311 if (!ws_strtou32(tok_max_packets, NULL((void*)0), &max_packets))
1312 {
1313 sharkd_json_error(
1314 rpcid, -32602, NULL((void*)0),
1315 "Invalid max_packets parameter"
1316 );
1317 return;
1318 }
1319 }
1320
1321 /* Parse optional max_bytes parameter */
1322 if (tok_max_bytes)
1323 {
1324 if (!ws_strtou64(tok_max_bytes, NULL((void*)0), &max_bytes))
1325 {
1326 sharkd_json_error(
1327 rpcid, -32602, NULL((void*)0),
1328 "Invalid max_bytes parameter"
1329 );
1330 return;
1331 }
1332 }
1333
1334 fprintf(stderrstderr, "load: filename=%s, max_packets=%u, max_bytes=%" PRIu64"l" "u" "\n",
1335 tok_file, max_packets, max_bytes);
1336
1337 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO0, false0, &err) != CF_OK)
1338 {
1339 sharkd_json_error(
1340 rpcid, -2001, NULL((void*)0),
1341 "Unable to open the file"
1342 );
1343 return;
1344 }
1345
1346 /* The open succeeded, and any previous file was closed. Remove any filter
1347 * results that refer to the previous file. */
1348 g_hash_table_remove_all(filter_table);
1349
1350 TRY{ except_t *volatile exc; volatile int except_state = 0; static
const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode
except_sn; struct except_catch except_ch; except_setup_try(&
except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch
.except_jmp)) *(&exc) = &except_ch.except_obj; else *
(&exc) = 0; if(except_state & 1) except_state |= 2; except_state
&= ~1; if (except_state == 0 && exc == 0)
1351 {
1352 if (max_packets > 0 || max_bytes > 0)
1353 {
1354 err = sharkd_load_cap_file_with_limits((int)max_packets, (int64_t)max_bytes);
1355 }
1356 else
1357 {
1358 err = sharkd_load_cap_file();
1359 }
1360 }
1361 CATCH(OutOfMemoryError)if (except_state == 0 && exc != 0 && exc->
except_id.except_code == (8) && (except_state |= 1))
1362 {
1363 sharkd_json_error(
1364 rpcid, -32603, NULL((void*)0),
1365 "Load failed, out of memory"
1366 );
1367 fprintf(stderrstderr, "load: OutOfMemoryError\n");
1368 err = ENOMEM12;
1369 }
1370 ENDTRYif(!(except_state&1) && exc != 0) except_rethrow(
exc); except_free(except_ch.except_obj.except_dyndata); except_pop
(); };}
;
1371
1372 if (err == 0)
1373 {
1374 sharkd_json_simple_ok(rpcid);
1375 }
1376 else
1377 {
1378 sharkd_json_result_prologue(rpcid);
1379 sharkd_json_value_string("status", wtap_strerror(err));
1380 sharkd_json_value_anyf("err", "%d", err);
1381 sharkd_json_result_epilogue();
1382 }
1383
1384}
1385
1386/**
1387 * sharkd_session_process_status()
1388 *
1389 * Process status request
1390 *
1391 * Output object with attributes:
1392 * (m) frames - count of currently loaded frames
1393 * (m) duration - time difference between time of first frame, and last loaded frame
1394 * (o) filename - capture filename
1395 * (o) filesize - capture filesize
1396 * (o) columns - array of column titles
1397 * (o) column_info - array of column infos, array of object with attributes:
1398 * 'title' - column title
1399 * 'format' - column format (%x or %Cus:<expr>:<occurrence> if COL_CUSTOM)
1400 * 'visible' - true if column is visible
1401 * 'display' - column display format; 'U', 'R' or 'D'
1402 */
1403static void
1404sharkd_session_process_status(void)
1405{
1406 sharkd_json_result_prologue(rpcid);
1407
1408 sharkd_json_value_anyf("frames", "%u", cfile.count);
1409 sharkd_json_value_anyf("duration", "%.9f", nstime_to_sec(&cfile.elapsed_time));
1410
1411 if (cfile.filename)
1412 {
1413 char *name = g_path_get_basename(cfile.filename);
1414
1415 sharkd_json_value_string("filename", name);
1416 g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized
(name, __builtin_object_size ((name), 0)) : (g_free) (name)
;
1417 }
1418
1419 if (cfile.provider.wth)
1420 {
1421 int64_t file_size = wtap_file_size(cfile.provider.wth, NULL((void*)0));
1422
1423 if (file_size > 0)
1424 sharkd_json_value_anyf("filesize", "%" PRId64"l" "d", file_size);
1425 }
1426
1427 if (cfile.cinfo.num_cols > 0)
1428 {
1429 sharkd_json_array_open("columns");
1430 for (unsigned i = 0; i < cfile.cinfo.num_cols; ++i)
1431 {
1432 sharkd_json_value_string(NULL((void*)0), get_column_title(i));
1433 }
1434 sharkd_json_array_close();
1435
1436 sharkd_json_array_open("column_info");
1437 for (unsigned i = 0; i < cfile.cinfo.num_cols; ++i)
1438 {
1439 int fmt = get_column_format(i);
1440 sharkd_json_object_open(NULL((void*)0));
1441 sharkd_json_value_string("title", get_column_title(i));
1442 if (fmt != COL_CUSTOM)
1443 {
1444 sharkd_json_value_string("format", col_format_to_string(fmt));
1445 } else {
1446 sharkd_json_value_stringf("format", "%s:%s:%d", col_format_to_string(fmt), get_column_custom_fields(i), get_column_custom_occurrence(i));
1447 }
1448 sharkd_json_value_anyf("visible", get_column_visible(i) ? "true" : "false");
1449 sharkd_json_value_stringf("display", "%c", get_column_display_format(i));
1450 sharkd_json_object_close();
1451 }
1452 sharkd_json_array_close();
1453 }
1454
1455 sharkd_json_result_epilogue();
1456}
1457
1458struct sharkd_analyse_data
1459{
1460 GHashTable *protocols_set;
1461 nstime_t *first_time;
1462 nstime_t *last_time;
1463};
1464
1465static void
1466sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree _U___attribute__((unused)),
1467 struct epan_column_info *cinfo _U___attribute__((unused)), const GSList *data_src _U___attribute__((unused)), void *data)
1468{
1469 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
1470 packet_info *pi = &edt->pi;
1471 frame_data *fdata = pi->fd;
1472
1473 if (analyser->first_time == NULL((void*)0) || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
1474 analyser->first_time = &fdata->abs_ts;
1475
1476 if (analyser->last_time == NULL((void*)0) || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
1477 analyser->last_time = &fdata->abs_ts;
1478
1479 if (pi->layers)
1480 {
1481 wmem_list_frame_t *frame;
1482
1483 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
1484 {
1485 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame))((guint) (gulong) (wmem_list_frame_data(frame)));
1486
1487 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)), NULL((void*)0), NULL((void*)0)))
1488 {
1489 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)), GUINT_TO_POINTER(proto_id)((gpointer) (gulong) (proto_id)));
1490 sharkd_json_value_string(NULL((void*)0), proto_get_protocol_filter_name(proto_id));
1491 }
1492 }
1493 }
1494
1495}
1496
1497/**
1498 * sharkd_session_process_status()
1499 *
1500 * Process analyse request
1501 *
1502 * Output object with attributes:
1503 * (m) frames - count of currently loaded frames
1504 * (m) protocols - protocol list
1505 * (m) first - earliest frame time
1506 * (m) last - latest frame time
1507 */
1508static void
1509sharkd_session_process_analyse(void)
1510{
1511 struct sharkd_analyse_data analyser;
1512 wtap_rec rec; /* Record information */
1513
1514 analyser.first_time = NULL((void*)0);
1515 analyser.last_time = NULL((void*)0);
1516 analyser.protocols_set = g_hash_table_new(NULL((void*)0) /* g_direct_hash() */, NULL((void*)0) /* g_direct_equal */);
1517
1518 sharkd_json_result_prologue(rpcid);
1519
1520 sharkd_json_value_anyf("frames", "%u", cfile.count);
1521
1522 sharkd_json_array_open("protocols");
1523
1524 wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048(2 * 1024));
1525
1526 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1527 {
1528 enum dissect_request_status status;
1529 int err;
1530 char *err_info;
1531
1532 status = sharkd_dissect_request(framenum,
1533 (framenum != 1) ? 1 : 0, framenum - 1,
1534 &rec, NULL((void*)0), SHARKD_DISSECT_FLAG_NULL0x00u,
1535 &sharkd_session_process_analyse_cb, &analyser,
1536 &err, &err_info);
1537 switch (status) {
1538
1539 case DISSECT_REQUEST_SUCCESS:
1540 break;
1541
1542 case DISSECT_REQUEST_NO_SUCH_FRAME:
1543 /* XXX - report the error. */
1544 break;
1545
1546 case DISSECT_REQUEST_READ_ERROR:
1547 /*
1548 * Free up the error string.
1549 * XXX - report the error.
1550 */
1551 g_free(err_info)(__builtin_object_size ((err_info), 0) != ((size_t) - 1)) ? g_free_sized
(err_info, __builtin_object_size ((err_info), 0)) : (g_free)
(err_info)
;
1552 break;
1553 }
1554 }
1555
1556 sharkd_json_array_close();
1557
1558 if (analyser.first_time)
1559 sharkd_json_value_anyf("first", "%.9f", nstime_to_sec(analyser.first_time));
1560
1561 if (analyser.last_time)
1562 sharkd_json_value_anyf("last", "%.9f", nstime_to_sec(analyser.last_time));
1563
1564 sharkd_json_result_epilogue();
1565
1566 wtap_rec_cleanup(&rec);
1567
1568 g_hash_table_destroy(analyser.protocols_set);
1569}
1570
1571static column_info *
1572sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
1573{
1574 const char *columns_custom[32];
1575 uint16_t columns_fmt[32];
1576 int16_t columns_occur[32];
1577
1578 int i, cols;
1579
1580 for (i = 0; i < 32; i++)
1581 {
1582 const char *tok_column;
1583 char tok_column_name[64];
1584 char *custom_sepa;
1585
1586 snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
1587 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
1588 if (tok_column == NULL((void*)0))
1589 break;
1590
1591 columns_custom[i] = NULL((void*)0);
1592 columns_occur[i] = 0;
1593
1594 if ((custom_sepa = strchr(tok_column, ':')))
1595 {
1596 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
1597
1598 columns_fmt[i] = COL_CUSTOM;
1599 columns_custom[i] = tok_column;
1600
1601 if (!ws_strtoi16(custom_sepa + 1, NULL((void*)0), &columns_occur[i]))
1602 return NULL((void*)0);
1603 }
1604 else
1605 {
1606 if (!ws_strtou16(tok_column, NULL((void*)0), &columns_fmt[i]))
1607 return NULL((void*)0);
1608
1609 if (columns_fmt[i] >= NUM_COL_FMTS)
1610 return NULL((void*)0);
1611
1612 /* if custom, that it shouldn't be just custom number -> error */
1613 if (columns_fmt[i] == COL_CUSTOM)
1614 return NULL((void*)0);
1615 }
1616 }
1617
1618 cols = i;
1619
1620 col_setup(cinfo, cols);
1621
1622 for (i = 0; i < cols; i++)
1623 {
1624 col_item_t *col_item = &cinfo->columns[i];
1625
1626 col_item->col_fmt = columns_fmt[i];
1627 col_item->col_title = NULL((void*)0); /* no need for title */
1628
1629 if (col_item->col_fmt == COL_CUSTOM)
1630 {
1631 col_item->col_custom_fields = g_strdup(columns_custom[i])g_strdup_inline (columns_custom[i]);
1632 col_item->col_custom_occurrence = columns_occur[i];
1633 }
1634
1635 col_item->col_fence = 0;
1636 }
1637
1638 col_finalize(cinfo);
1639
1640 return cinfo;
1641}
1642
1643static void
1644sharkd_session_process_frames_cb(epan_dissect_t *edt, proto_tree *tree _U___attribute__((unused)),
1645 struct epan_column_info *cinfo, const GSList *data_src _U___attribute__((unused)), void *data _U___attribute__((unused)))
1646{
1647 packet_info *pi = &edt->pi;
1648 frame_data *fdata = pi->fd;
1649 wtap_block_t pkt_block = NULL((void*)0);
1650 unsigned int i;
1651 char *comment = NULL((void*)0);
1652
1653 json_dumper_begin_object(&dumper);
1654
1655 sharkd_json_array_open("c");
1656 for (unsigned col = 0; col < cinfo->num_cols; ++col)
1657 {
1658 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
1659 }
1660 sharkd_json_array_close();
1661
1662 sharkd_json_value_anyf("num", "%u", pi->num);
1663
1664 /*
1665 * Get the block for this record, if it has one.
1666 */
1667 pkt_block = sharkd_get_packet_block(fdata);
1668
1669 /*
1670 * Does this record have any comments?
1671 */
1672 if (pkt_block != NULL((void*)0) &&
1673 WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, 0, &comment))
1674 {
1675 sharkd_json_value_anyf("ct", "true");
1676
1677 sharkd_json_array_open("comments");
1678 for (i = 0; wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment) == WTAP_OPTTYPE_SUCCESS; i++) {
1679 sharkd_json_value_string(NULL((void*)0), comment);
1680 }
1681 sharkd_json_array_close();
1682 }
1683
1684 if (fdata->ignored)
1685 sharkd_json_value_anyf("i", "true");
1686
1687 if (fdata->marked)
1688 sharkd_json_value_anyf("m", "true");
1689
1690 if (fdata->color_filter)
1691 {
1692 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
1693 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
1694 }
1695
1696 wtap_block_unref(pkt_block);
1697 json_dumper_end_object(&dumper);
1698}
1699
1700/**
1701 * sharkd_session_process_frames()
1702 *
1703 * Process frames request
1704 *
1705 * Input:
1706 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurrence>).
1707 * If column0 is not specified default column set will be used.
1708 * (o) filter - filter to be used
1709 * (o) skip=N - skip N frames
1710 * (o) limit=N - show only N frames
1711 * (o) refs - list (comma separated) with sorted time reference frame numbers.
1712 *
1713 * Output array of frames with attributes:
1714 * (m) c - array of column data
1715 * (m) num - frame number
1716 * (o) i - if frame is ignored
1717 * (o) m - if frame is marked
1718 * (o) ct - if frame is commented
1719 * (o) comments - array of comment strings
1720 * (o) bg - color filter - background color in hex
1721 * (o) fg - color filter - foreground color in hex
1722 */
1723static void
1724sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
1725{
1726 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1727 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
1728 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
1729 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
1730 const char *tok_refs = json_find_attr(buf, tokens, count, "refs");
1731
1732 const uint8_t *filter_data = NULL((void*)0);
1733
1734 uint32_t prev_dis_num = 0;
1735 uint32_t current_ref_frame = 0, next_ref_frame = UINT32_MAX(4294967295U);
1736 uint32_t skip;
1737 uint32_t limit;
1738
1739 wtap_rec rec; /* Record information */
1740 column_info *cinfo = &cfile.cinfo;
1741 column_info user_cinfo;
1742
1743 if (tok_column)
1744 {
1745 memset(&user_cinfo, 0, sizeof(user_cinfo));
1746 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
1747 if (!cinfo)
1748 {
1749 sharkd_json_error(
1750 rpcid, -13001, NULL((void*)0),
1751 "Column definition invalid - note column 6 requires a custom definition"
1752 );
1753 return;
1754 }
1755 }
1756
1757 if (tok_filter)
1758 {
1759 const struct sharkd_filter_item *filter_item;
1760
1761 filter_item = sharkd_session_filter_data(tok_filter);
1762 if (!filter_item)
1763 {
1764 sharkd_json_error(
1765 rpcid, -13002, NULL((void*)0),
1766 "Filter expression invalid"
1767 );
1768 return;
1769 }
1770
1771 filter_data = filter_item->filtered;
1772 }
1773
1774 skip = 0;
1775 if (tok_skip)
1776 {
1777 if (!ws_strtou32(tok_skip, NULL((void*)0), &skip))
1778 return;
1779 }
1780
1781 limit = 0;
1782 if (tok_limit)
1783 {
1784 if (!ws_strtou32(tok_limit, NULL((void*)0), &limit))
1785 return;
1786 }
1787
1788 if (tok_refs)
1789 {
1790 if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame))
1791 return;
1792 }
1793
1794 sharkd_json_result_array_prologue(rpcid);
1795
1796 wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048(2 * 1024));
1797
1798 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
1799 {
1800 frame_data *fdata;
1801 uint32_t ref_frame = (framenum != 1) ? 1 : 0;
1802 enum dissect_request_status status;
1803 int err;
1804 char *err_info;
1805
1806 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
1807 continue;
1808
1809 if (skip)
1810 {
1811 skip--;
1812 prev_dis_num = framenum;
1813 continue;
1814 }
1815
1816 if (tok_refs)
1817 {
1818 if (framenum >= next_ref_frame)
1819 {
1820 current_ref_frame = next_ref_frame;
1821
1822 if (*tok_refs != ',')
1823 next_ref_frame = UINT32_MAX(4294967295U);
1824
1825 while (*tok_refs == ',' && framenum >= next_ref_frame)
1826 {
1827 current_ref_frame = next_ref_frame;
1828
1829 if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame))
1830 {
1831 fprintf(stderrstderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs);
1832 break;
1833 }
1834 }
1835
1836 if (*tok_refs == '\0' && framenum >= next_ref_frame)
1837 {
1838 current_ref_frame = next_ref_frame;
1839 next_ref_frame = UINT32_MAX(4294967295U);
1840 }
1841 }
1842
1843 if (current_ref_frame)
1844 ref_frame = current_ref_frame;
1845 }
1846
1847 fdata = sharkd_get_frame(framenum);
1848 status = sharkd_dissect_request(framenum,
1849 ref_frame, prev_dis_num,
1850 &rec, cinfo,
1851 (fdata->color_filter == NULL((void*)0)) ? SHARKD_DISSECT_FLAG_COLOR0x08u : SHARKD_DISSECT_FLAG_NULL0x00u,
1852 &sharkd_session_process_frames_cb, NULL((void*)0),
1853 &err, &err_info);
1854 switch (status) {
1855
1856 case DISSECT_REQUEST_SUCCESS:
1857 break;
1858
1859 case DISSECT_REQUEST_NO_SUCH_FRAME:
1860 /* XXX - report the error. */
1861 break;
1862
1863 case DISSECT_REQUEST_READ_ERROR:
1864 /*
1865 * Free up the error string.
1866 * XXX - report the error.
1867 */
1868 g_free(err_info)(__builtin_object_size ((err_info), 0) != ((size_t) - 1)) ? g_free_sized
(err_info, __builtin_object_size ((err_info), 0)) : (g_free)
(err_info)
;
1869 break;
1870 }
1871
1872 prev_dis_num = framenum;
1873
1874 if (limit && --limit == 0)
1875 break;
1876 }
1877 sharkd_json_result_array_epilogue();
1878
1879 if (cinfo != &cfile.cinfo)
1880 col_cleanup(cinfo);
1881
1882 wtap_rec_cleanup(&rec);
1883}
1884
1885static void
1886// NOLINTNEXTLINE(misc-no-recursion)
1887sharkd_session_process_tap_stats_node_cb(const char *key, const stat_node *n)
1888{
1889 stat_node *node;
1890
1891 sharkd_json_array_open(key);
1892 for (node = n->children; node; node = node->next)
1893 {
1894 json_dumper_begin_object(&dumper);
1895
1896 /* code based on stats_tree_get_values_from_node() */
1897 sharkd_json_value_string("name", node->name);
1898 sharkd_json_value_anyf("count", "%d", node->counter);
1899 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE0x10000000) || node->rng))
1900 {
1901 switch(node->datatype)
1902 {
1903 case STAT_DT_INT:
1904 sharkd_json_value_anyf("avg", "%.2f", ((float)node->total.int_total) / node->counter);
1905 sharkd_json_value_anyf("min", "%d", node->minvalue.int_min);
1906 sharkd_json_value_anyf("max", "%d", node->maxvalue.int_max);
1907 break;
1908 case STAT_DT_FLOAT:
1909 sharkd_json_value_anyf("avg", "%.2f", node->total.float_total / node->counter);
1910 sharkd_json_value_anyf("min", "%f", node->minvalue.float_min);
1911 sharkd_json_value_anyf("max", "%f", node->maxvalue.float_max);
1912 break;
1913 }
1914 }
1915
1916 if (node->st->elapsed)
1917 sharkd_json_value_anyf("rate", "%.4f", ((float)node->counter) / node->st->elapsed);
1918
1919 if (node->parent && node->parent->counter)
1920 sharkd_json_value_anyf("perc", "%.2f", (node->counter * 100.0) / node->parent->counter);
1921 else if (node->parent == &(node->st->root))
1922 sharkd_json_value_anyf("perc", "100");
1923
1924 if (prefs.st_enable_burstinfo && node->max_burst)
1925 {
1926 if (prefs.st_burst_showcount)
1927 sharkd_json_value_anyf("burstcount", "%d", node->max_burst);
1928 else
1929 sharkd_json_value_anyf("burstrate", "%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
1930
1931 sharkd_json_value_anyf("bursttime", "%.3f", (node->burst_time / 1000.0));
1932 }
1933
1934 if (node->children)
1935 {
1936 // We recurse here but our depth is limited
1937 sharkd_session_process_tap_stats_node_cb("sub", node);
1938 }
1939 json_dumper_end_object(&dumper);
1940 }
1941 sharkd_json_array_close();
1942}
1943
1944/**
1945 * sharkd_session_process_tap_stats_cb()
1946 *
1947 * Output stats tap:
1948 *
1949 * (m) tap - tap name
1950 * (m) type:stats - tap output type
1951 * (m) name - stat name
1952 * (m) stats - array of object with attributes:
1953 * (m) name - stat item name
1954 * (m) count - stat item counter
1955 * (o) avg - stat item averange value
1956 * (o) min - stat item min value
1957 * (o) max - stat item max value
1958 * (o) rate - stat item rate value (ms)
1959 * (o) perc - stat item percentage
1960 * (o) burstrate - stat item burst rate
1961 * (o) burstcount - stat item burst count
1962 * (o) burstttme - stat item burst start
1963 * (o) sub - array of object with attributes like in stats node.
1964 */
1965static void
1966sharkd_session_process_tap_stats_cb(void *psp)
1967{
1968 stats_tree *st = (stats_tree *) psp;
1969
1970 json_dumper_begin_object(&dumper);
1971
1972 sharkd_json_value_stringf("tap", "stats:%s", st->cfg->abbr);
1973 sharkd_json_value_string("type", "stats");
1974 sharkd_json_value_string("name", st->cfg->path);
1975
1976 sharkd_session_process_tap_stats_node_cb("stats", &st->root);
1977
1978 json_dumper_end_object(&dumper);
1979}
1980
1981static void
1982sharkd_session_free_tap_stats_cb(void *psp)
1983{
1984 stats_tree *st = (stats_tree *) psp;
1985
1986 stats_tree_free(st);
1987}
1988
1989struct sharkd_expert_tap
1990{
1991 GSList *details;
1992 GStringChunk *text;
1993};
1994
1995/**
1996 * sharkd_session_process_tap_expert_cb()
1997 *
1998 * Output expert tap:
1999 *
2000 * (m) tap - tap name
2001 * (m) type:expert - tap output type
2002 * (m) details - array of object with attributes:
2003 * (m) f - frame number, which generated expert information
2004 * (o) s - severity
2005 * (o) g - group
2006 * (m) m - expert message
2007 * (o) p - protocol
2008 */
2009static void
2010sharkd_session_process_tap_expert_cb(void *tapdata)
2011{
2012 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
2013 GSList *list;
2014
2015 json_dumper_begin_object(&dumper);
2016
2017 sharkd_json_value_string("tap", "expert");
2018 sharkd_json_value_string("type", "expert");
2019
2020 sharkd_json_array_open("details");
2021 for (list = etd->details; list; list = list->next)
2022 {
2023 expert_info_t *ei = (expert_info_t *) list->data;
2024 const char *tmp;
2025
2026 json_dumper_begin_object(&dumper);
2027
2028 sharkd_json_value_anyf("f", "%u", ei->packet_num);
2029
2030 tmp = try_val_to_str(ei->severity, expert_severity_vals);
2031 if (tmp)
2032 sharkd_json_value_string("s", tmp);
2033
2034 tmp = try_val_to_str(ei->group, expert_group_vals);
2035 if (tmp)
2036 sharkd_json_value_string("g", tmp);
2037
2038 sharkd_json_value_string("m", ei->summary);
2039
2040 if (ei->protocol)
2041 sharkd_json_value_string("p", ei->protocol);
2042
2043 json_dumper_end_object(&dumper);
2044 }
2045 sharkd_json_array_close();
2046
2047 json_dumper_end_object(&dumper);
2048}
2049
2050static tap_packet_status
2051sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U___attribute__((unused)), epan_dissect_t *edt _U___attribute__((unused)), const void *pointer, tap_flags_t flags _U___attribute__((unused)))
2052{
2053 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
2054 const expert_info_t *ei = (const expert_info_t *) pointer;
2055 expert_info_t *ei_copy;
2056
2057 if (ei == NULL((void*)0))
2058 return TAP_PACKET_DONT_REDRAW;
2059
2060 ei_copy = g_new(expert_info_t, 1)((expert_info_t *) g_malloc_n ((1), sizeof (expert_info_t)));
2061 /* Note: this is a shallow copy */
2062 *ei_copy = *ei;
2063
2064 /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */
2065 ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
2066 ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary);
2067
2068 etd->details = g_slist_prepend(etd->details, ei_copy);
2069
2070 return TAP_PACKET_REDRAW;
2071}
2072
2073static void
2074sharkd_session_free_tap_expert_cb(void *tapdata)
2075{
2076 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
2077
2078 g_slist_free_full(etd->details, g_free);
2079 g_string_chunk_free(etd->text);
2080 g_free(etd)(__builtin_object_size ((etd), 0) != ((size_t) - 1)) ? g_free_sized
(etd, __builtin_object_size ((etd), 0)) : (g_free) (etd)
;
2081}
2082
2083/**
2084 * sharkd_session_process_tap_flow_cb()
2085 *
2086 * Output flow tap:
2087 * (m) tap - tap name
2088 * (m) type:flow - tap output type
2089 * (m) nodes - array of strings with node address
2090 * (m) flows - array of object with attributes:
2091 * (m) t - frame time string
2092 * (m) n - array of two numbers with source node index and destination node index
2093 * (m) pn - array of two numbers with source and destination port
2094 * (o) c - comment
2095 */
2096static void
2097sharkd_session_process_tap_flow_cb(void *tapdata)
2098{
2099 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
2100 GList *flow_list;
2101 unsigned i;
2102
2103 sequence_analysis_get_nodes(graph_analysis);
2104
2105 json_dumper_begin_object(&dumper);
2106 sharkd_json_value_stringf("tap", "seqa:%s", graph_analysis->name);
2107 sharkd_json_value_string("type", "flow");
2108
2109 sharkd_json_array_open("nodes");
2110 for (i = 0; i < graph_analysis->num_nodes; i++)
2111 {
2112 char *addr_str;
2113
2114 addr_str = address_to_display(NULL((void*)0), &(graph_analysis->nodes[i]));
2115 sharkd_json_value_string(NULL((void*)0), addr_str);
2116 wmem_free(NULL((void*)0), addr_str);
2117 }
2118 sharkd_json_array_close();
2119
2120 sharkd_json_array_open("flows");
2121 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
2122 while (flow_list)
2123 {
2124 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
2125
2126 flow_list = g_list_next(flow_list)((flow_list) ? (((GList *)(flow_list))->next) : ((void*)0)
)
;
2127
2128 if (!sai->display)
2129 continue;
2130
2131 json_dumper_begin_object(&dumper);
2132
2133 sharkd_json_value_string("t", sai->time_str);
2134 sharkd_json_value_anyf("n", "[%u,%u]", sai->src_node, sai->dst_node);
2135 sharkd_json_value_anyf("pn", "[%u,%u]", sai->port_src, sai->port_dst);
2136
2137 if (sai->comment)
2138 sharkd_json_value_string("c", sai->comment);
2139
2140 json_dumper_end_object(&dumper);
2141 }
2142 sharkd_json_array_close();
2143
2144 json_dumper_end_object(&dumper);
2145}
2146
2147static void
2148sharkd_session_free_tap_flow_cb(void *tapdata)
2149{
2150 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
2151
2152 sequence_analysis_info_free(graph_analysis);
2153}
2154
2155struct sharkd_conv_tap_data
2156{
2157 const char *type;
2158 conv_hash_t hash;
2159 bool_Bool resolve_name;
2160 bool_Bool resolve_port;
2161};
2162
2163static bool_Bool
2164sharkd_session_geoip_addr(address *addr, const char *suffix)
2165{
2166 const mmdb_lookup_t *lookup = NULL((void*)0);
2167 bool_Bool with_geoip = false0;
2168 char json_key[64];
2169
2170 if (addr->type == AT_IPv4)
2171 {
2172 const ws_in4_addr *ip4 = (const ws_in4_addr *) addr->data;
2173
2174 lookup = maxmind_db_lookup_ipv4(ip4);
2175 }
2176 else if (addr->type == AT_IPv6)
2177 {
2178 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
2179
2180 lookup = maxmind_db_lookup_ipv6(ip6);
2181 }
2182
2183 if (!lookup || !lookup->found)
2184 return false0;
2185
2186 if (lookup->country)
2187 {
2188 snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix);
2189 sharkd_json_value_string(json_key, lookup->country);
2190 with_geoip = true1;
2191 }
2192
2193 if (lookup->country_iso)
2194 {
2195 snprintf(json_key, sizeof(json_key), "geoip_country_iso%s", suffix);
2196 sharkd_json_value_string(json_key, lookup->country_iso);
2197 with_geoip = true1;
2198 }
2199
2200 if (lookup->city)
2201 {
2202 snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix);
2203 sharkd_json_value_string(json_key, lookup->city);
2204 with_geoip = true1;
2205 }
2206
2207 if (lookup->as_org)
2208 {
2209 snprintf(json_key, sizeof(json_key), "geoip_as_org%s", suffix);
2210 sharkd_json_value_string(json_key, lookup->as_org);
2211 with_geoip = true1;
2212 }
2213
2214 if (lookup->as_number > 0)
2215 {
2216 snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix);
2217 sharkd_json_value_anyf(json_key, "%u", lookup->as_number);
2218 with_geoip = true1;
2219 }
2220
2221 if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
2222 {
2223 snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix);
2224 sharkd_json_value_anyf(json_key, "%f", lookup->latitude);
2225 with_geoip = true1;
2226 }
2227
2228 if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
2229 {
2230 snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix);
2231 sharkd_json_value_anyf(json_key, "%f", lookup->longitude);
2232 with_geoip = true1;
2233 }
2234
2235 return with_geoip;
2236}
2237
2238struct sharkd_analyse_rtp_items
2239{
2240 uint32_t frame_num;
2241 uint32_t sequence_num;
2242
2243 double delta;
2244 double jitter;
2245 double skew;
2246 double bandwidth;
2247 bool_Bool marker;
2248
2249 double arrive_offset;
2250
2251 /* from tap_rtp_stat_t */
2252 uint32_t flags;
2253 uint16_t pt;
2254};
2255
2256struct sharkd_analyse_rtp
2257{
2258 const char *tap_name;
2259 rtpstream_id_t id;
2260
2261 GSList *packets;
2262 double start_time;
2263 tap_rtp_stat_t statinfo;
2264};
2265
2266static void
2267sharkd_session_process_tap_rtp_free_cb(void *tapdata)
2268{
2269 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2270
2271 g_slist_free_full(rtp_req->packets, g_free);
2272 rtpstream_id_free(&rtp_req->id);
2273 g_free(rtp_req)(__builtin_object_size ((rtp_req), 0) != ((size_t) - 1)) ? g_free_sized
(rtp_req, __builtin_object_size ((rtp_req), 0)) : (g_free) (
rtp_req)
;
2274}
2275
2276static tap_packet_status
2277sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U___attribute__((unused)), const void *pointer, tap_flags_t flags _U___attribute__((unused)))
2278{
2279 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2280 const struct _rtp_info *rtp_info = (const struct _rtp_info *) pointer;
2281
2282 if (rtpstream_id_equal_pinfo_rtp_info(&rtp_req->id, pinfo, rtp_info))
2283 {
2284 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
2285 struct sharkd_analyse_rtp_items *item;
2286
2287 rtppacket_analyse(statinfo, pinfo, rtp_info);
2288
2289 item = g_new(struct sharkd_analyse_rtp_items, 1)((struct sharkd_analyse_rtp_items *) g_malloc_n ((1), sizeof (
struct sharkd_analyse_rtp_items)))
;
2290
2291 if (!rtp_req->packets)
2292 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
2293
2294 item->frame_num = pinfo->num;
2295 item->sequence_num = rtp_info->info_seq_num;
2296 item->delta = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->delta;
2297 item->jitter = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->jitter;
2298 item->skew = (statinfo->flags & STAT_FLAG_FIRST0x001) ? 0.0 : statinfo->skew;
2299 item->bandwidth = statinfo->bandwidth;
2300 item->marker = rtp_info->info_marker_set ? true1 : false0;
2301 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
2302
2303 item->flags = statinfo->flags;
2304 item->pt = statinfo->pt;
2305
2306 /* XXX, O(n) optimize */
2307 rtp_req->packets = g_slist_append(rtp_req->packets, item);
2308 }
2309
2310 return TAP_PACKET_REDRAW;
2311}
2312
2313/**
2314 * sharkd_session_process_tap_rtp_analyse_cb()
2315 *
2316 * Output rtp analyse tap:
2317 * (m) tap - tap name
2318 * (m) type - tap output type
2319 * (m) ssrc - RTP SSRC
2320 * (m) max_delta - Max delta (ms)
2321 * (m) max_delta_nr - Max delta packet #
2322 * (m) max_jitter - Max jitter (ms)
2323 * (m) mean_jitter - Mean jitter (ms)
2324 * (m) max_skew - Max skew (ms)
2325 * (m) total_nr - Total number of RTP packets
2326 * (m) seq_err - Number of sequence errors
2327 * (m) duration - Duration (ms)
2328 * (m) items - array of object with attributes:
2329 * (m) f - frame number
2330 * (m) o - arrive offset
2331 * (m) sn - sequence number
2332 * (m) d - delta
2333 * (m) j - jitter
2334 * (m) sk - skew
2335 * (m) bw - bandwidth
2336 * (o) s - status string
2337 * (o) t - status type
2338 * (o) mark - rtp mark
2339 */
2340static void
2341sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
2342{
2343 const int RTP_TYPE_CN = 1;
2344 const int RTP_TYPE_ERROR = 2;
2345 const int RTP_TYPE_WARN = 3;
2346 const int RTP_TYPE_PT_EVENT = 4;
2347
2348 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
2349 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
2350
2351 GSList *l;
2352
2353 json_dumper_begin_object(&dumper);
2354
2355 sharkd_json_value_string("tap", rtp_req->tap_name);
2356 sharkd_json_value_string("type", "rtp-analyse");
2357 sharkd_json_value_stringf("ssrc", "0x%x", rtp_req->id.ssrc);
2358
2359 sharkd_json_value_anyf("max_delta", "%f", statinfo->max_delta);
2360 sharkd_json_value_anyf("max_delta_nr", "%u", statinfo->max_nr);
2361 sharkd_json_value_anyf("max_jitter", "%f", statinfo->max_jitter);
2362 sharkd_json_value_anyf("mean_jitter", "%f", statinfo->mean_jitter);
2363 sharkd_json_value_anyf("max_skew", "%f", statinfo->max_skew);
2364 sharkd_json_value_anyf("total_nr", "%u", statinfo->total_nr);
2365 sharkd_json_value_anyf("seq_err", "%u", statinfo->sequence);
2366 sharkd_json_value_anyf("duration", "%f", statinfo->time - statinfo->start_time);
2367
2368 sharkd_json_array_open("items");
2369 for (l = rtp_req->packets; l; l = l->next)
2370 {
2371 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
2372
2373 json_dumper_begin_object(&dumper);
2374
2375 sharkd_json_value_anyf("f", "%u", item->frame_num);
2376 sharkd_json_value_anyf("o", "%.9f", item->arrive_offset);
2377 sharkd_json_value_anyf("sn", "%u", item->sequence_num);
2378 sharkd_json_value_anyf("d", "%.2f", item->delta);
2379 sharkd_json_value_anyf("j", "%.2f", item->jitter);
2380 sharkd_json_value_anyf("sk", "%.2f", item->skew);
2381 sharkd_json_value_anyf("bw", "%.2f", item->bandwidth);
2382
2383 if (item->pt == PT_CN13)
2384 {
2385 sharkd_json_value_string("s", "Comfort noise (PT=13, RFC 3389)");
2386 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2387 }
2388 else if (item->pt == PT_CN_OLD19)
2389 {
2390 sharkd_json_value_string("s", "Comfort noise (PT=19, reserved)");
2391 sharkd_json_value_anyf("t", "%d", RTP_TYPE_CN);
2392 }
2393 else if (item->flags & STAT_FLAG_WRONG_SEQ0x004)
2394 {
2395 sharkd_json_value_string("s", "Wrong sequence number");
2396 sharkd_json_value_anyf("t", "%d", RTP_TYPE_ERROR);
2397 }
2398 else if (item->flags & STAT_FLAG_DUP_PKT0x200)
2399 {
2400 sharkd_json_value_string("s", "Suspected duplicate (MAC address) only delta time calculated");
2401 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2402 }
2403 else if (item->flags & STAT_FLAG_REG_PT_CHANGE0x040)
2404 {
2405 sharkd_json_value_stringf("s", "Payload changed to PT=%u%s",
2406 item->pt,
2407 (item->flags & STAT_FLAG_PT_T_EVENT0x100) ? " telephone/event" : "");
2408 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2409 }
2410 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP0x080)
2411 {
2412 sharkd_json_value_string("s", "Incorrect timestamp");
2413 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2414 }
2415 else if ((item->flags & STAT_FLAG_PT_CHANGE0x008)
2416 && !(item->flags & STAT_FLAG_FIRST0x001)
2417 && !(item->flags & STAT_FLAG_PT_CN0x010)
2418 && (item->flags & STAT_FLAG_FOLLOW_PT_CN0x020)
2419 && !(item->flags & STAT_FLAG_MARKER0x002))
2420 {
2421 sharkd_json_value_string("s", "Marker missing?");
2422 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2423 }
2424 else if (item->flags & STAT_FLAG_PT_T_EVENT0x100)
2425 {
2426 sharkd_json_value_stringf("s", "PT=%u telephone/event", item->pt);
2427 sharkd_json_value_anyf("t", "%d", RTP_TYPE_PT_EVENT);
2428 }
2429 else if (item->flags & STAT_FLAG_MARKER0x002)
2430 {
2431 sharkd_json_value_anyf("t", "%d", RTP_TYPE_WARN);
2432 }
2433
2434 if (item->marker)
2435 sharkd_json_value_anyf("mark", "1");
2436
2437 json_dumper_end_object(&dumper);
2438 }
2439 sharkd_json_array_close();
2440
2441 json_dumper_end_object(&dumper);
2442}
2443
2444/**
2445 * sharkd_session_process_tap_conv_cb()
2446 *
2447 * Output conv tap:
2448 * (m) tap - tap name
2449 * (m) type - tap output type
2450 * (m) proto - protocol short name
2451 * (o) filter - filter string
2452 * (o) geoip - whether GeoIP information is available, boolean
2453 *
2454 * (o) convs - array of object with attributes:
2455 * (m) saddr - source address
2456 * (m) daddr - destination address
2457 * (o) sport - source port
2458 * (o) dport - destination port
2459 * (m) txf - TX frame count
2460 * (m) txb - TX bytes
2461 * (m) rxf - RX frame count
2462 * (m) rxb - RX bytes
2463 * (m) start - (relative) first packet time
2464 * (m) stop - (relative) last packet time
2465 * (o) filter - conversation filter
2466 *
2467 * (o) hosts - array of object with attributes:
2468 * (m) host - host address
2469 * (o) port - host port
2470 * (m) txf - TX frame count
2471 * (m) txb - TX bytes
2472 * (m) rxf - RX frame count
2473 * (m) rxb - RX bytes
2474 */
2475static void
2476sharkd_session_process_tap_conv_cb(void *arg)
2477{
2478 conv_hash_t *hash = (conv_hash_t *) arg;
2479 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2480 const char *proto;
2481 int proto_with_port;
2482 unsigned i;
2483
2484 int with_geoip = 0;
2485
2486 json_dumper_begin_object(&dumper);
2487 sharkd_json_value_string("tap", iu->type);
2488
2489 if (!strncmp(iu->type, "conv:", 5))
2490 {
2491 sharkd_json_value_string("type", "conv");
2492 sharkd_json_array_open("convs");
2493 proto = iu->type + 5;
2494 }
2495 else if (!strncmp(iu->type, "endpt:", 6))
2496 {
2497 sharkd_json_value_string("type", "host");
2498 sharkd_json_array_open("hosts");
2499 proto = iu->type + 6;
2500 }
2501 else
2502 {
2503 sharkd_json_value_string("type", "err");
2504 proto = "";
2505 }
2506
2507 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
2508
2509 if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "conv:", 5))
2510 {
2511 for (i = 0; i < iu->hash.conv_array->len; i++)
2512 {
2513 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i)(((conv_item_t*) (void *) (iu->hash.conv_array)->data) [
(i)])
;
2514 char *src_addr, *dst_addr;
2515 char *src_port, *dst_port;
2516 char *filter_str;
2517
2518 json_dumper_begin_object(&dumper);
2519
2520 sharkd_json_value_string("saddr", (src_addr = get_conversation_address(NULL((void*)0), &iui->src_address, iu->resolve_name)));
2521 sharkd_json_value_string("daddr", (dst_addr = get_conversation_address(NULL((void*)0), &iui->dst_address, iu->resolve_name)));
2522
2523 if (proto_with_port)
2524 {
2525 sharkd_json_value_string("sport", (src_port = get_conversation_port(NULL((void*)0), iui->src_port, iui->ctype, iu->resolve_port)));
2526 sharkd_json_value_string("dport", (dst_port = get_conversation_port(NULL((void*)0), iui->dst_port, iui->ctype, iu->resolve_port)));
2527
2528 wmem_free(NULL((void*)0), src_port);
2529 wmem_free(NULL((void*)0), dst_port);
2530 }
2531
2532 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", iui->rx_frames);
2533 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", iui->rx_bytes);
2534
2535 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", iui->tx_frames);
2536 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", iui->tx_bytes);
2537
2538 sharkd_json_value_anyf("start", "%.9f", nstime_to_sec(&iui->start_time));
2539 sharkd_json_value_anyf("stop", "%.9f", nstime_to_sec(&iui->stop_time));
2540
2541 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
2542 if (filter_str)
2543 {
2544 sharkd_json_value_string("filter", filter_str);
2545 g_free(filter_str)(__builtin_object_size ((filter_str), 0) != ((size_t) - 1)) ?
g_free_sized (filter_str, __builtin_object_size ((filter_str
), 0)) : (g_free) (filter_str)
;
2546 }
2547
2548 wmem_free(NULL((void*)0), src_addr);
2549 wmem_free(NULL((void*)0), dst_addr);
2550
2551 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
2552 with_geoip = 1;
2553 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
2554 with_geoip = 1;
2555
2556 json_dumper_end_object(&dumper);
2557 }
2558 }
2559 else if (iu->hash.conv_array != NULL((void*)0) && !strncmp(iu->type, "endpt:", 6))
2560 {
2561 for (i = 0; i < iu->hash.conv_array->len; i++)
2562 {
2563 endpoint_item_t *endpoint = &g_array_index(iu->hash.conv_array, endpoint_item_t, i)(((endpoint_item_t*) (void *) (iu->hash.conv_array)->data
) [(i)])
;
2564 char *host_str, *port_str;
2565 char *filter_str;
2566
2567 json_dumper_begin_object(&dumper);
2568
2569 sharkd_json_value_string("host", (host_str = get_conversation_address(NULL((void*)0), &endpoint->myaddress, iu->resolve_name)));
2570
2571 if (proto_with_port)
2572 {
2573 sharkd_json_value_string("port", (port_str = get_endpoint_port(NULL((void*)0), endpoint, iu->resolve_port)));
2574
2575 wmem_free(NULL((void*)0), port_str);
2576 }
2577
2578 sharkd_json_value_anyf("rxf", "%" PRIu64"l" "u", endpoint->rx_frames);
2579 sharkd_json_value_anyf("rxb", "%" PRIu64"l" "u", endpoint->rx_bytes);
2580
2581 sharkd_json_value_anyf("txf", "%" PRIu64"l" "u", endpoint->tx_frames);
2582 sharkd_json_value_anyf("txb", "%" PRIu64"l" "u", endpoint->tx_bytes);
2583
2584 filter_str = get_endpoint_filter(endpoint);
2585 if (filter_str)
2586 {
2587 sharkd_json_value_string("filter", filter_str);
2588 g_free(filter_str)(__builtin_object_size ((filter_str), 0) != ((size_t) - 1)) ?
g_free_sized (filter_str, __builtin_object_size ((filter_str
), 0)) : (g_free) (filter_str)
;
2589 }
2590
2591 wmem_free(NULL((void*)0), host_str);
2592
2593 if (sharkd_session_geoip_addr(&(endpoint->myaddress), ""))
2594 with_geoip = 1;
2595 json_dumper_end_object(&dumper);
2596 }
2597 }
2598 sharkd_json_array_close();
2599
2600 sharkd_json_value_string("proto", proto);
2601 sharkd_json_value_anyf("geoip", with_geoip ? "true" : "false");
2602
2603 json_dumper_end_object(&dumper);
2604}
2605
2606static void
2607sharkd_session_free_tap_conv_cb(void *arg)
2608{
2609 conv_hash_t *hash = (conv_hash_t *) arg;
2610 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
2611
2612 if (!strncmp(iu->type, "conv:", 5))
2613 {
2614 reset_conversation_table_data(hash);
2615 }
2616 else if (!strncmp(iu->type, "endpt:", 6))
2617 {
2618 reset_endpoint_table_data(hash);
2619 }
2620
2621 g_free(iu)(__builtin_object_size ((iu), 0) != ((size_t) - 1)) ? g_free_sized
(iu, __builtin_object_size ((iu), 0)) : (g_free) (iu)
;
2622}
2623
2624/**
2625 * sharkd_session_process_tap_nstat_cb()
2626 *
2627 * Output nstat tap:
2628 * (m) tap - tap name
2629 * (m) type - tap output type
2630 * (m) fields: array of objects with attributes:
2631 * (m) c - name
2632 *
2633 * (m) tables: array of object with attributes:
2634 * (m) t - table title
2635 * (m) i - array of items
2636 */
2637static void
2638sharkd_session_process_tap_nstat_cb(void *arg)
2639{
2640 stat_data_t *stat_data = (stat_data_t *) arg;
2641 unsigned i, j, k;
2642
2643 json_dumper_begin_object(&dumper);
2644 sharkd_json_value_stringf("tap", "nstat:%s", stat_data->stat_tap_data->cli_string);
2645 sharkd_json_value_string("type", "nstat");
2646
2647 sharkd_json_array_open("fields");
2648 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
2649 {
2650 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
2651
2652 json_dumper_begin_object(&dumper);
2653 sharkd_json_value_string("c", field->column_name);
2654 json_dumper_end_object(&dumper);
2655 }
2656 sharkd_json_array_close();
2657
2658 sharkd_json_array_open("tables");
2659 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
2660 {
2661 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i)(((stat_tap_table **) (void *) (stat_data->stat_tap_data->
tables)->data) [(i)])
;
2662
2663 json_dumper_begin_object(&dumper);
2664
2665 sharkd_json_value_string("t", table->title);
2666
2667 sharkd_json_array_open("i");
2668 for (j = 0; j < table->num_elements; j++)
2669 {
2670 stat_tap_table_item_type *field_data;
2671
2672 field_data = stat_tap_get_field_data(table, j, 0);
2673 if (field_data == NULL((void*)0) || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
2674 continue;
2675
2676 sharkd_json_array_open(NULL((void*)0));
2677 for (k = 0; k < table->num_fields; k++)
2678 {
2679 field_data = stat_tap_get_field_data(table, j, k);
2680
2681 switch (field_data->type)
2682 {
2683 case TABLE_ITEM_UINT:
2684 sharkd_json_value_anyf(NULL((void*)0), "%u", field_data->value.uint_value);
2685 break;
2686
2687 case TABLE_ITEM_INT:
2688 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.int_value);
2689 break;
2690
2691 case TABLE_ITEM_STRING:
2692 sharkd_json_value_string(NULL((void*)0), field_data->value.string_value);
2693 break;
2694
2695 case TABLE_ITEM_FLOAT:
2696 sharkd_json_value_anyf(NULL((void*)0), "%f", field_data->value.float_value);
2697 break;
2698
2699 case TABLE_ITEM_ENUM:
2700 sharkd_json_value_anyf(NULL((void*)0), "%d", field_data->value.enum_value);
2701 break;
2702
2703 case TABLE_ITEM_NONE:
2704 sharkd_json_value_anyf(NULL((void*)0), "null");
2705 break;
2706 }
2707 }
2708
2709 sharkd_json_array_close();
2710 }
2711 sharkd_json_array_close();
2712 json_dumper_end_object(&dumper);
2713 }
2714 sharkd_json_array_close();
2715
2716 json_dumper_end_object(&dumper);
2717}
2718
2719static void
2720sharkd_session_free_tap_nstat_cb(void *arg)
2721{
2722 stat_data_t *stat_data = (stat_data_t *) arg;
2723
2724 free_stat_tables(stat_data->stat_tap_data);
2725}
2726
2727/**
2728 * sharkd_session_process_tap_rtd_cb()
2729 *
2730 * Output rtd tap:
2731 * (m) tap - tap name
2732 * (m) type - tap output type
2733 * (m) stats - statistics rows - array object with attributes:
2734 * (m) type - statistic name
2735 * (m) num - number of messages
2736 * (m) min - minimum SRT time
2737 * (m) max - maximum SRT time
2738 * (m) tot - total SRT time
2739 * (m) min_frame - minimal SRT
2740 * (m) max_frame - maximum SRT
2741 * (o) open_req - Open Requests
2742 * (o) disc_rsp - Discarded Responses
2743 * (o) req_dup - Duplicated Requests
2744 * (o) rsp_dup - Duplicated Responses
2745 * (o) open_req - Open Requests
2746 * (o) disc_rsp - Discarded Responses
2747 * (o) req_dup - Duplicated Requests
2748 * (o) rsp_dup - Duplicated Responses
2749 */
2750static void
2751sharkd_session_process_tap_rtd_cb(void *arg)
2752{
2753 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2754 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
2755
2756 unsigned i, j;
2757
2758 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
2759
2760 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
2761 * some multiple table and single timestat (radius, h225)
2762 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
2763 * I wonder how it will gonna work with multiple timestats and multiple tables...
2764 * (for usage grep for: register_rtd_table)
2765 */
2766 const value_string *vs = get_rtd_value_string(rtd);
2767
2768 json_dumper_begin_object(&dumper);
2769 sharkd_json_value_stringf("tap", "rtd:%s", filter);
2770 sharkd_json_value_string("type", "rtd");
2771
2772 if (rtd_data->stat_table.num_rtds == 1)
2773 {
2774 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
2775
2776 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2777 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2778 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2779 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2780 }
2781
2782 sharkd_json_array_open("stats");
2783 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
2784 {
2785 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
2786
2787 for (j = 0; j < ms->num_timestat; j++)
2788 {
2789 const char *type_str;
2790
2791 if (ms->rtd[j].num == 0)
2792 continue;
2793
2794 json_dumper_begin_object(&dumper);
2795
2796 if (rtd_data->stat_table.num_rtds == 1)
2797 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
2798 else
2799 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
2800 sharkd_json_value_string("type", type_str);
2801
2802 sharkd_json_value_anyf("num", "%u", ms->rtd[j].num);
2803 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&(ms->rtd[j].min)));
2804 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&(ms->rtd[j].max)));
2805 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
2806 sharkd_json_value_anyf("min_frame", "%u", ms->rtd[j].min_num);
2807 sharkd_json_value_anyf("max_frame", "%u", ms->rtd[j].max_num);
2808
2809 if (rtd_data->stat_table.num_rtds != 1)
2810 {
2811 /* like in tshark, display it on every row */
2812 sharkd_json_value_anyf("open_req", "%u", ms->open_req_num);
2813 sharkd_json_value_anyf("disc_rsp", "%u", ms->disc_rsp_num);
2814 sharkd_json_value_anyf("req_dup", "%u", ms->req_dup_num);
2815 sharkd_json_value_anyf("rsp_dup", "%u", ms->rsp_dup_num);
2816 }
2817
2818 json_dumper_end_object(&dumper);
2819 }
2820 }
2821 sharkd_json_array_close();
2822
2823 json_dumper_end_object(&dumper);
2824}
2825
2826static void
2827sharkd_session_free_tap_rtd_cb(void *arg)
2828{
2829 rtd_data_t *rtd_data = (rtd_data_t *) arg;
2830
2831 free_rtd_table(&rtd_data->stat_table);
2832 g_free(rtd_data)(__builtin_object_size ((rtd_data), 0) != ((size_t) - 1)) ? g_free_sized
(rtd_data, __builtin_object_size ((rtd_data), 0)) : (g_free)
(rtd_data)
;
2833}
2834
2835/**
2836 * sharkd_session_process_tap_srt_cb()
2837 *
2838 * Output srt tap:
2839 * (m) tap - tap name
2840 * (m) type - tap output type
2841 *
2842 * (m) tables - array of object with attributes:
2843 * (m) n - table name
2844 * (m) f - table filter
2845 * (o) c - table column name
2846 * (m) r - table rows - array object with attributes:
2847 * (m) n - row name
2848 * (m) idx - procedure index
2849 * (m) num - number of events
2850 * (m) min - minimum SRT time
2851 * (m) max - maximum SRT time
2852 * (m) tot - total SRT time
2853 */
2854static void
2855sharkd_session_process_tap_srt_cb(void *arg)
2856{
2857 srt_data_t *srt_data = (srt_data_t *) arg;
2858 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2859
2860 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
2861
2862 unsigned i;
2863
2864 json_dumper_begin_object(&dumper);
2865 sharkd_json_value_stringf("tap", "srt:%s", filter);
2866 sharkd_json_value_string("type", "srt");
2867
2868 sharkd_json_array_open("tables");
2869 for (i = 0; i < srt_data->srt_array->len; i++)
2870 {
2871 /* SRT table */
2872 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i)(((srt_stat_table **) (void *) (srt_data->srt_array)->data
) [(i)])
;
2873
2874 int j;
2875
2876 json_dumper_begin_object(&dumper);
2877
2878 if (rst->name)
2879 sharkd_json_value_string("n", rst->name);
2880 else if (rst->short_name)
2881 sharkd_json_value_string("n", rst->short_name);
2882 else
2883 sharkd_json_value_stringf("n", "table%u", i);
2884
2885 if (rst->filter_string)
2886 sharkd_json_value_string("f", rst->filter_string);
2887
2888 if (rst->proc_column_name)
2889 sharkd_json_value_string("c", rst->proc_column_name);
2890
2891 sharkd_json_array_open("r");
2892 for (j = 0; j < rst->num_procs; j++)
2893 {
2894 /* SRT row */
2895 srt_procedure_t *proc = &rst->procedures[j];
2896
2897 if (proc->stats.num == 0)
2898 continue;
2899
2900 json_dumper_begin_object(&dumper);
2901
2902 sharkd_json_value_string("n", proc->procedure);
2903
2904 if (rst->filter_string)
2905 sharkd_json_value_anyf("idx", "%d", proc->proc_index);
2906
2907 sharkd_json_value_anyf("num", "%u", proc->stats.num);
2908
2909 sharkd_json_value_anyf("min", "%.9f", nstime_to_sec(&proc->stats.min));
2910 sharkd_json_value_anyf("max", "%.9f", nstime_to_sec(&proc->stats.max));
2911 sharkd_json_value_anyf("tot", "%.9f", nstime_to_sec(&proc->stats.tot));
2912
2913 json_dumper_end_object(&dumper);
2914 }
2915 sharkd_json_array_close();
2916
2917 json_dumper_end_object(&dumper);
2918 }
2919 sharkd_json_array_close();
2920
2921 json_dumper_end_object(&dumper);
2922}
2923
2924static void
2925sharkd_session_free_tap_srt_cb(void *arg)
2926{
2927 srt_data_t *srt_data = (srt_data_t *) arg;
2928 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2929
2930 free_srt_table(srt, srt_data->srt_array);
2931 g_array_free(srt_data->srt_array, TRUE(!(0)));
2932 g_free(srt_data)(__builtin_object_size ((srt_data), 0) != ((size_t) - 1)) ? g_free_sized
(srt_data, __builtin_object_size ((srt_data), 0)) : (g_free)
(srt_data)
;
2933}
2934
2935static void
2936// NOLINTNEXTLINE(misc-no-recursion)
2937sharkd_session_process_tap_phs_cb_aux(phs_t *rs)
2938{
2939 for (; rs; rs = rs->sibling) {
2940 if (rs->protocol == -1) {
2941 return;
2942 }
2943 sharkd_json_object_open(NULL((void*)0));
2944 sharkd_json_value_string("proto", rs->proto_name);
2945 sharkd_json_value_anyf("frames", "%"PRIu32"u", rs->frames);
2946 sharkd_json_value_anyf("bytes", "%"PRIu64"l" "u", rs->bytes);
2947 if (rs->child != NULL((void*)0) && rs->child->protocol != -1) {
2948 sharkd_json_array_open("protos");
2949 // We recurse here but our depth is limited
2950 sharkd_session_process_tap_phs_cb_aux(rs->child);
2951 sharkd_json_array_close();
2952 }
2953 sharkd_json_object_close();
2954 }
2955}
2956
2957/**
2958 * sharkd_session_process_tap_phs_cb()
2959 *
2960 * Output phs tap:
2961 * (m) tap - tap name
2962 * (m) type - tap output type
2963 * (m) filter - tap filter argument
2964 * (m) protos - array of proto objects
2965 *
2966 * proto object:
2967 * (m) proto - protocol name
2968 * (m) frames - frame count
2969 * (m) bytes - bytes count
2970 * (o) protos - array of proto objects
2971 */
2972static void
2973sharkd_session_process_tap_phs_cb(void *arg)
2974{
2975 phs_t *rs = (phs_t *)arg;
2976 sharkd_json_object_open(NULL((void*)0));
2977 sharkd_json_value_string("tap", "phs");
2978 sharkd_json_value_string("type", "phs");
2979 sharkd_json_value_string("filter", rs->filter ? rs->filter : "");
2980 sharkd_json_array_open("protos");
2981 sharkd_session_process_tap_phs_cb_aux(rs);
2982 sharkd_json_array_close();
2983 sharkd_json_object_close();
2984}
2985
2986static void
2987sharkd_session_free_tap_phs_cb(void *arg)
2988{
2989 phs_t *rs = (phs_t *)arg;
2990 free_phs(rs);
2991}
2992
2993struct sharkd_export_object_list
2994{
2995 struct sharkd_export_object_list *next;
2996
2997 char *type;
2998 const char *proto;
2999 GSList *entries;
3000};
3001
3002static struct sharkd_export_object_list *sharkd_eo_list;
3003
3004/**
3005 * sharkd_session_process_tap_eo_cb()
3006 *
3007 * Output eo tap:
3008 * (m) tap - tap name
3009 * (m) type - tap output type
3010 * (m) proto - protocol short name
3011 * (m) objects - array of object with attributes:
3012 * (m) pkt - packet number
3013 * (o) hostname - hostname
3014 * (o) type - content type
3015 * (o) filename - filename
3016 * (m) len - object length
3017 * (m) sha1 - object's sha1 sum
3018 */
3019static void
3020sharkd_session_process_tap_eo_cb(void *tapdata)
3021{
3022 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
3023 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data;
3024 GSList *slist;
3025 int i = 0;
3026 char *sha1sum_str;
3027 uint8_t sha1sum_bytes[HASH_SHA1_LENGTH20];
3028
3029 json_dumper_begin_object(&dumper);
3030 sharkd_json_value_string("tap", object_list->type);
3031 sharkd_json_value_string("type", "eo");
3032
3033 sharkd_json_value_string("proto", object_list->proto);
3034
3035 sharkd_json_array_open("objects");
3036 for (slist = object_list->entries; slist; slist = slist->next)
3037 {
3038 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
3039
3040 json_dumper_begin_object(&dumper);
3041
3042 sharkd_json_value_anyf("pkt", "%u", eo_entry->pkt_num);
3043
3044 if (eo_entry->hostname)
3045 sharkd_json_value_string("hostname", eo_entry->hostname);
3046
3047 if (eo_entry->content_type)
3048 sharkd_json_value_string("type", eo_entry->content_type);
3049
3050 if (eo_entry->filename)
3051 sharkd_json_value_string("filename", eo_entry->filename);
3052
3053 sharkd_json_value_stringf("_download", "%s_%d", object_list->type, i);
3054
3055 sharkd_json_value_anyf("len", "%zu", eo_entry->payload_len);
3056
3057 gcry_md_hash_buffer(GCRY_MD_SHA1, sha1sum_bytes, eo_entry->payload_data, eo_entry->payload_len);
3058 sha1sum_str = bytes_to_str(NULL, sha1sum_bytes, HASH_SHA1_LENGTH)bytes_to_str_maxlen(((void*)0), sha1sum_bytes, 20, 36);
3059 sharkd_json_value_string("sha1", sha1sum_str);
3060 g_free(sha1sum_str)(__builtin_object_size ((sha1sum_str), 0) != ((size_t) - 1)) ?
g_free_sized (sha1sum_str, __builtin_object_size ((sha1sum_str
), 0)) : (g_free) (sha1sum_str)
;
3061
3062 json_dumper_end_object(&dumper);
3063
3064 i++;
3065 }
3066 sharkd_json_array_close();
3067
3068 json_dumper_end_object(&dumper);
3069}
3070
3071static void
3072sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
3073{
3074 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
3075
3076 object_list->entries = g_slist_append(object_list->entries, entry);
3077}
3078
3079static export_object_entry_t *
3080sharkd_eo_object_list_get_entry(void *gui_data, int row)
3081{
3082 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
3083
3084 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
3085}
3086
3087static struct sharkd_export_object_list *
3088sharkd_eo_object_list_get_entry_by_type(void *gui_data, const char *tap_type)
3089{
3090 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
3091 for (; object_list; object_list = object_list->next)
3092 {
3093 if (!strcmp(object_list->type, tap_type))
3094 return object_list;
3095 }
3096 return NULL((void*)0);
3097}
3098
3099
3100/**
3101 * sharkd_session_process_tap_rtp_cb()
3102 *
3103 * Output RTP streams tap:
3104 * (m) tap - tap name
3105 * (m) type - tap output type
3106 * (m) streams - array of object with attributes:
3107 * (m) ssrc - RTP synchronization source identifier
3108 * (m) payload - stream payload
3109 * (m) saddr - source address
3110 * (m) sport - source port
3111 * (m) daddr - destination address
3112 * (m) dport - destination port
3113 * (m) pkts - packets count
3114 * (m) max_delta - max delta (ms)
3115 * (m) max_jitter - max jitter (ms)
3116 * (m) mean_jitter - mean jitter (ms)
3117 * (m) expectednr -
3118 * (m) totalnr -
3119 * (m) problem - if analyser found the problem
3120 * (m) ipver - address IP version (4 or 6)
3121 */
3122static void
3123sharkd_session_process_tap_rtp_cb(void *arg)
3124{
3125 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
3126
3127 GList *listx;
3128
3129 json_dumper_begin_object(&dumper);
3130 sharkd_json_value_string("tap", "rtp-streams");
3131 sharkd_json_value_string("type", "rtp-streams");
3132
3133 sharkd_json_array_open("streams");
3134 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
3135 {
3136 rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data;
3137 rtpstream_info_calc_t calc;
3138
3139 rtpstream_info_calculate(streaminfo, &calc);
3140
3141 json_dumper_begin_object(&dumper);
3142
3143 sharkd_json_value_stringf("ssrc", "0x%x", calc.ssrc);
3144 sharkd_json_value_string("payload", calc.all_payload_type_names);
3145
3146 sharkd_json_value_string("saddr", calc.src_addr_str);
3147 sharkd_json_value_anyf("sport", "%u", calc.src_port);
3148 sharkd_json_value_string("daddr", calc.dst_addr_str);
3149 sharkd_json_value_anyf("dport", "%u", calc.dst_port);
3150
3151 sharkd_json_value_anyf("start_time", "%f", calc.start_time_ms);
3152 sharkd_json_value_anyf("duration", "%f", calc.duration_ms);
3153
3154 sharkd_json_value_anyf("pkts", "%u", calc.packet_count);
3155 sharkd_json_value_anyf("lost", "%u", calc.lost_num);
3156 sharkd_json_value_anyf("lost_percent", "%f", calc.lost_perc);
3157
3158 sharkd_json_value_anyf("max_delta", "%f",calc.max_delta);
3159 sharkd_json_value_anyf("min_delta", "%f",calc.min_delta);
3160 sharkd_json_value_anyf("mean_delta", "%f",calc.mean_delta);
3161 sharkd_json_value_anyf("min_jitter", "%f", calc.min_jitter);
3162 sharkd_json_value_anyf("max_jitter", "%f", calc.max_jitter);
3163 sharkd_json_value_anyf("mean_jitter", "%f", calc.mean_jitter);
3164
3165 sharkd_json_value_anyf("expectednr", "%u", calc.packet_expected);
3166 sharkd_json_value_anyf("totalnr", "%u", calc.total_nr);
3167
3168 sharkd_json_value_anyf("problem", calc.problem ? "true" : "false");
3169
3170 /* for filter */
3171 sharkd_json_value_anyf("ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
3172
3173 rtpstream_info_calc_free(&calc);
3174
3175 json_dumper_end_object(&dumper);
3176 }
3177 sharkd_json_array_close();
3178
3179 json_dumper_end_object(&dumper);
3180}
3181
3182/**
3183* sharkd_session_process_tap_multicast_cb()
3184*
3185* Output UDP Multicast streams tap:
3186* (m) tap - tap name
3187* (m) type - tap output type
3188* (m) bufferThresholdBytes - byte count for a stream where a buffer alarm should be reported
3189* (m) burstIntervalMs - analysis interval in milliseconds
3190* (m) burstThresholdPackets - count of packets in an interval that should trigger an alarm
3191* (m) streams - array of streams with metrics:
3192* (m) saddr - source address
3193* (m) sport - source port
3194* (m) daddr - destination address
3195* (m) dport - destination port
3196* (m) packets - object group for packet metrics with attributes:
3197* (m) number - count of packets in the stream
3198* (m) perSecond - average number of packets per seconds in the stream
3199* (m) bandwidth - object group for bandwidth metrics with attributes:
3200* (m) average - average measured bitrate in the stream
3201* (m) max - max measured bitrate in the stream
3202* (m) buffer - object group for buffer metrics with attributes:
3203* (m) alarms - number of times the stream exceeded the buffer threshold
3204* (m) max - highest stream buffer utilization
3205* (m) burst - object group for burst metrics with attributes:
3206* (m) alarms - number of times the stream exceeded the burst threshold
3207* (m) max - most stream packets measured in a burst interval
3208*/
3209static void
3210sharkd_session_process_tap_multicast_cb(void *arg)
3211{
3212 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)arg;
3213 GList *list_item;
3214 char *addr_str;
3215
3216 json_dumper_begin_object(&dumper);
3217
3218 sharkd_json_value_string("tap", "multicast");
3219 sharkd_json_value_string("type", "multicast");
3220
3221 sharkd_json_value_anyf("bufferThresholdBytes", "%u", mcast_stream_bufferalarm);
3222 sharkd_json_value_anyf("burstIntervalMs", "%u", mcast_stream_burstint);
3223 sharkd_json_value_anyf("burstThresholdPackets", "%u", mcast_stream_trigger);
3224
3225 sharkd_json_array_open("streams");
3226 for (list_item = g_list_first(tapinfo->strinfo_list); list_item; list_item = list_item->next) {
3227 mcast_stream_info_t *stream_info = (mcast_stream_info_t *) list_item->data;
3228 sharkd_json_object_open(NULL((void*)0));
3229 {
3230 addr_str = address_to_display(NULL((void*)0), &stream_info->src_addr);
3231 sharkd_json_value_string("saddr", addr_str);
3232 wmem_free(NULL((void*)0), addr_str);
3233 sharkd_json_value_anyf("sport", "%u", stream_info->src_port);
3234 addr_str = address_to_display(NULL((void*)0), &stream_info->dest_addr);
3235 sharkd_json_value_string("daddr", addr_str);
3236 wmem_free(NULL((void*)0), addr_str);
3237 sharkd_json_value_anyf("dport", "%u", stream_info->dest_port);
3238 sharkd_json_object_open("packets");
3239 {
3240 sharkd_json_value_anyf("number", "%u", stream_info->npackets);
3241 sharkd_json_value_anyf("perSecond", "%f", stream_info->apackets);
3242 }
3243 sharkd_json_object_close();
3244 sharkd_json_object_open("bandwidth");
3245 {
3246 sharkd_json_value_anyf("average", "%f", stream_info->average_bw);
3247 sharkd_json_value_anyf("max", "%f", stream_info->element.maxbw);
3248 }
3249 sharkd_json_object_close();
3250 sharkd_json_object_open("buffer");
3251 {
3252 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbuffalarms);
3253 sharkd_json_value_anyf("max", "%u", stream_info->element.topbuffusage);
3254 }
3255 sharkd_json_object_close();
3256 sharkd_json_object_open("burst");
3257 {
3258 sharkd_json_value_anyf("alarms", "%u", stream_info->element.numbursts);
3259 sharkd_json_value_anyf("max", "%u", stream_info->element.topburstsize);
3260 }
3261 sharkd_json_object_close();
3262 }
3263 sharkd_json_object_close();
3264 }
3265 sharkd_json_array_close();
3266
3267 json_dumper_end_object(&dumper);
3268}
3269
3270static void
3271sharkd_session_process_free_tap_multicast_cb(void *tapdata)
3272{
3273 mcaststream_tapinfo_t *tapinfo = (mcaststream_tapinfo_t *)tapdata;
3274
3275 mcaststream_reset(tapinfo);
3276
3277 g_free(tapinfo)(__builtin_object_size ((tapinfo), 0) != ((size_t) - 1)) ? g_free_sized
(tapinfo, __builtin_object_size ((tapinfo), 0)) : (g_free) (
tapinfo)
;
3278}
3279
3280/**
3281 * sharkd_session_process_tap_voip_calls_cb()
3282 *
3283 * Output VoIP Calls tap:
3284 * (m) tap - tap name
3285 * (m) type - tap output type
3286 * (m) calls - array of objects with attributes:
3287 * (m) call - call number
3288 * (m) start_time - start timestamp
3289 * (m) stop_time - stop timestamp
3290 * (m) initial_speaker - address of initial speaker
3291 * (m) from - from address
3292 * (m) to - to address
3293 * (m) protocol - protocol name
3294 * (m) packets - packet count
3295 * (m) state - state string
3296 * (m) comment - comment string
3297 */
3298static void
3299sharkd_session_process_tap_voip_calls_cb(void *arg)
3300{
3301 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)arg;
3302 char *addr_str;
3303 GList *cur_call = g_queue_peek_nth_link(tapinfo->callsinfos, 0);
3304 sharkd_json_object_open(NULL((void*)0));
3305 sharkd_json_value_string("tap", "voip-calls");
3306 sharkd_json_value_string("type", "voip-calls");
3307 sharkd_json_array_open("calls");
3308 while (cur_call && cur_call->data) {
3309 voip_calls_info_t *call_info_ = (voip_calls_info_t*) cur_call->data;
3310 sharkd_json_object_open(NULL((void*)0));
3311 sharkd_json_value_anyf("call", "%hu", call_info_->call_num);
3312 sharkd_json_value_anyf("start_time", "%.6f", nstime_to_sec(&(call_info_->start_rel_ts)));
3313 sharkd_json_value_anyf("stop_time", "%.6f", nstime_to_sec(&(call_info_->stop_rel_ts)));
3314 addr_str = address_to_display(NULL((void*)0), &(call_info_->initial_speaker));
3315 sharkd_json_value_string("initial_speaker", addr_str);
3316 wmem_free(NULL((void*)0), addr_str);
3317 sharkd_json_value_string("from", call_info_->from_identity);
3318 sharkd_json_value_string("to", call_info_->to_identity);
3319 sharkd_json_value_string("protocol", ((call_info_->protocol == VOIP_COMMON) && call_info_->protocol_name) ?
3320 call_info_->protocol_name : voip_protocol_name[call_info_->protocol]);
3321 sharkd_json_value_anyf("packets", "%u", call_info_->npackets);
3322 sharkd_json_value_string("state", voip_call_state_name[call_info_->call_state]);
3323 sharkd_json_value_string("comment", call_info_->call_comment);
3324 sharkd_json_object_close();
3325 cur_call = g_list_next(cur_call)((cur_call) ? (((GList *)(cur_call))->next) : ((void*)0));
3326 }
3327 sharkd_json_array_close();
3328 sharkd_json_object_close();
3329}
3330
3331static void
3332sharkd_session_free_tap_voip_calls_cb(void *tapdata)
3333{
3334 voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *)tapdata;
3335 voip_calls_remove_all_tap_listeners(tapinfo);
3336 if (tapinfo->callsinfos != NULL((void*)0)) {
3337 g_queue_free(tapinfo->callsinfos);
3338 }
3339 if (tapinfo->graph_analysis != NULL((void*)0)) {
3340 sequence_analysis_info_free(tapinfo->graph_analysis);
3341 }
3342 memset(tapinfo, 0, sizeof(*tapinfo));
3343}
3344
3345
3346struct sharkd_voip_convs_req {
3347 voip_calls_tapinfo_t *tapinfo;
3348 const char *tap_name;
3349};
3350
3351/**
3352 * sharkd_session_process_tap_voip_convs_cb()
3353 *
3354 * Output VoIP Conversations tap:
3355 * (m) tap - tap name
3356 * (m) type - tap output type
3357 * (m) convs - array of objects with attributes:
3358 * (m) frame - frame number
3359 * (m) call - call number
3360 * (m) time - timestamp
3361 * (m) dst_addr - destination address
3362 * (m) dst_port - destination port
3363 * (m) src_addr - source address
3364 * (m) src_port - source port
3365 * (m) label - label string
3366 * (m) comment - comment string
3367 */
3368static void
3369sharkd_session_process_tap_voip_convs_cb(void *arg)
3370{
3371 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)arg;
3372 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3373 seq_analysis_info_t *sainfo = tapinfo->graph_analysis;
3374 char *addr_str;
3375 sequence_analysis_list_sort(sainfo);
3376 sharkd_json_object_open(NULL((void*)0));
3377 sharkd_json_value_string("tap", voip_convs_req->tap_name);
3378 sharkd_json_value_string("type", "voip-convs");
3379 sharkd_json_array_open("convs");
3380 for (GList *cur = g_queue_peek_nth_link(sainfo->items, 0); cur; cur = g_list_next(cur)((cur) ? (((GList *)(cur))->next) : ((void*)0))) {
3381 seq_analysis_item_t *sai = (seq_analysis_item_t *) cur->data;
3382 if ((voip_conv_sel[sai->conv_num / VOIP_CONV_BITS(sizeof(int) * 8)] & (1 << (sai->conv_num % VOIP_CONV_BITS(sizeof(int) * 8)))) == 0)
3383 continue;
3384 sharkd_json_object_open(NULL((void*)0));
3385 sharkd_json_value_anyf("frame", "%d", sai->frame_number);
3386 sharkd_json_value_anyf("call", "%d", sai->conv_num);
3387 sharkd_json_value_string("time", sai->time_str);
3388 addr_str = address_to_display(NULL((void*)0), &(sai->dst_addr));
3389 sharkd_json_value_string("dst_addr", addr_str);
3390 wmem_free(NULL((void*)0), addr_str);
3391 sharkd_json_value_anyf("dst_port", "%d", sai->port_dst);
3392 addr_str = address_to_display(NULL((void*)0), &(sai->src_addr));
3393 sharkd_json_value_string("src_addr", addr_str);
3394 wmem_free(NULL((void*)0), addr_str);
3395 sharkd_json_value_anyf("src_port", "%d", sai->port_src);
3396 sharkd_json_value_string("label", sai->frame_label);
3397 sharkd_json_value_string("comment", sai->comment);
3398 sharkd_json_object_close();
3399 }
3400 sharkd_json_array_close();
3401 sharkd_json_object_close();
3402}
3403
3404static void
3405sharkd_session_free_tap_voip_convs_cb(void *tapdata)
3406{
3407 struct sharkd_voip_convs_req *voip_convs_req = (struct sharkd_voip_convs_req *)tapdata;
3408 voip_calls_tapinfo_t *tapinfo = voip_convs_req->tapinfo;
3409 voip_calls_remove_all_tap_listeners(tapinfo);
3410 if (tapinfo->callsinfos != NULL((void*)0)) {
3411 g_queue_free(tapinfo->callsinfos);
3412 }
3413 if (tapinfo->graph_analysis != NULL((void*)0)) {
3414 sequence_analysis_info_free(tapinfo->graph_analysis);
3415 }
3416 memset(tapinfo, 0, sizeof(*tapinfo));
3417 g_free(voip_convs_req)(__builtin_object_size ((voip_convs_req), 0) != ((size_t) - 1
)) ? g_free_sized (voip_convs_req, __builtin_object_size ((voip_convs_req
), 0)) : (g_free) (voip_convs_req)
;
3418}
3419
3420struct sharkd_hosts_req {
3421 const char *tap_name;
3422 bool_Bool dump_v4;
3423 bool_Bool dump_v6;
3424};
3425
3426static int
3427sharkd_session_tap_ipv4_host_compare(const void *a, const void *b)
3428{
3429 return ws_ascii_strnatcmp(((const hashipv4_t *)a)->name,
3430 ((const hashipv4_t *)b)->name);
3431}
3432
3433static int
3434sharkd_session_tap_ipv6_host_compare(const void *a, const void *b)
3435{
3436 return ws_ascii_strnatcmp(((const hashipv6_t *)a)->name,
3437 ((const hashipv6_t *)b)->name);
3438}
3439
3440static void
3441sharkd_session_tap_ipv4_host_print(void *data, void *user_data _U___attribute__((unused)))
3442{
3443 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)data;
3444 sharkd_json_object_open(NULL((void*)0));
3445 sharkd_json_value_string("name", ipv4_hash_table_entry->name);
3446 sharkd_json_value_string("addr", ipv4_hash_table_entry->ip);
3447 sharkd_json_object_close();
3448}
3449
3450static void
3451sharkd_session_tap_ipv6_host_print(void *data, void *user_data _U___attribute__((unused)))
3452{
3453 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)data;
3454 sharkd_json_object_open(NULL((void*)0));
3455 sharkd_json_value_string("name", ipv6_hash_table_entry->name);
3456 sharkd_json_value_string("addr", ipv6_hash_table_entry->ip6);
3457 sharkd_json_object_close();
3458}
3459
3460static void
3461sharkd_session_tap_ipv4_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3462{
3463 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
3464 GSList **list = (GSList **)user_data;
3465 if ((ipv4_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3466 *list = g_slist_insert_sorted(*list, ipv4_hash_table_entry, sharkd_session_tap_ipv4_host_compare);
3467 }
3468}
3469
3470static void
3471sharkd_session_tap_ipv6_host_insert_sorted(void *key _U___attribute__((unused)), void *value, void *user_data)
3472{
3473 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
3474 GSList **list = (GSList **) user_data;
3475 if ((ipv6_hash_table_entry->flags & NAME_RESOLVED(1U<<1))) {
3476 *list = g_slist_insert_sorted(*list, ipv6_hash_table_entry, sharkd_session_tap_ipv6_host_compare);
3477 }
3478}
3479
3480static void
3481sharkd_session_tap_ipv4_hosts_print(void)
3482{
3483 wmem_map_t *ipv4_hash_table = get_ipv4_hash_table();
3484 if (!ipv4_hash_table)
3485 return;
3486 GSList *list = NULL((void*)0);
3487 wmem_map_foreach(ipv4_hash_table, sharkd_session_tap_ipv4_host_insert_sorted, &list);
3488 g_slist_foreach(list, sharkd_session_tap_ipv4_host_print, NULL((void*)0));
3489 g_slist_free(list);
3490}
3491
3492static void
3493sharkd_session_tap_ipv6_hosts_print(void)
3494{
3495 wmem_map_t *ipv6_hash_table = get_ipv6_hash_table();
3496 if (!ipv6_hash_table)
3497 return;
3498 GSList *list = NULL((void*)0);
3499 wmem_map_foreach(ipv6_hash_table, sharkd_session_tap_ipv6_host_insert_sorted, &list);
3500 g_slist_foreach(list, sharkd_session_tap_ipv6_host_print, NULL((void*)0));
3501 g_slist_free(list);
3502}
3503
3504/**
3505 * sharkd_session_process_tap_hosts_cb()
3506 *
3507 * Output Hosts tap:
3508 * (m) tap - tap name
3509 * (m) type - tap output type
3510 * (o) ipv4_hosts - array of objects with attributes:
3511 * (m) addr - ipv4 address
3512 * (m) name - resolved name of address
3513 * (o) ipv6_hosts - array of objects with attributes:
3514 * (m) addr - ipv6 address
3515 * (m) name - resolved name of address
3516 */
3517static void
3518sharkd_session_process_tap_hosts_cb(void *arg)
3519{
3520 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)arg;
3521 sharkd_json_object_open(NULL((void*)0));
3522 sharkd_json_value_string("tap", hosts_req->tap_name);
3523 sharkd_json_value_string("type", "hosts");
3524 if (hosts_req->dump_v4) {
3525 sharkd_json_array_open("ipv4_hosts");
3526 sharkd_session_tap_ipv4_hosts_print();
3527 sharkd_json_array_close();
3528 }
3529 if (hosts_req->dump_v6) {
3530 sharkd_json_array_open("ipv6_hosts");
3531 sharkd_session_tap_ipv6_hosts_print();
3532 sharkd_json_array_close();
3533 }
3534 sharkd_json_object_close();
3535}
3536
3537static void
3538sharkd_session_free_tap_hosts_cb(void *tapdata)
3539{
3540 struct sharkd_hosts_req *hosts_req = (struct sharkd_hosts_req *)tapdata;
3541 g_free(hosts_req)(__builtin_object_size ((hosts_req), 0) != ((size_t) - 1)) ? g_free_sized
(hosts_req, __builtin_object_size ((hosts_req), 0)) : (g_free
) (hosts_req)
;
3542}
3543
3544static GString*
3545sharkd_session_eo_register_tap_listener(register_eo_t *eo, const char *tap_type, const char *tap_filter, tap_draw_cb tap_draw, void **ptap_data, GFreeFunc* ptap_free)
3546{
3547 export_object_list_t *eo_object;
3548 struct sharkd_export_object_list *object_list;
3549
3550 object_list = sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type);
3551 if (object_list)
3552 {
3553 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
3554 object_list->entries = NULL((void*)0);
3555 }
3556 else
3557 {
3558 object_list = g_new(struct sharkd_export_object_list, 1)((struct sharkd_export_object_list *) g_malloc_n ((1), sizeof
(struct sharkd_export_object_list)))
;
3559 object_list->type = g_strdup(tap_type)g_strdup_inline (tap_type);
3560 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
3561 object_list->entries = NULL((void*)0);
3562 object_list->next = sharkd_eo_list;
3563 sharkd_eo_list = object_list;
3564 }
3565
3566 eo_object = g_new0(export_object_list_t, 1)((export_object_list_t *) g_malloc0_n ((1), sizeof (export_object_list_t
)))
;
3567 eo_object->add_entry = sharkd_eo_object_list_add_entry;
3568 eo_object->get_entry = sharkd_eo_object_list_get_entry;
3569 eo_object->gui_data = (void *) object_list;
3570
3571 *ptap_data = eo_object;
3572 *ptap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
3573
3574 return register_tap_listener(get_eo_tap_listener_name(eo), eo_object, tap_filter, 0, NULL((void*)0), get_eo_packet_func(eo), tap_draw, NULL((void*)0));
3575}
3576
3577static void
3578rtpstream_free_cb(void *data)
3579{
3580 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t*)data;
3581
3582 rtpstream_reset(rtp_tapinfo);
3583 g_free(rtp_tapinfo)(__builtin_object_size ((rtp_tapinfo), 0) != ((size_t) - 1)) ?
g_free_sized (rtp_tapinfo, __builtin_object_size ((rtp_tapinfo
), 0)) : (g_free) (rtp_tapinfo)
;
3584}
3585
3586static bool_Bool
3587sharkd_session_register_tap(const char *tok_tap, const char *tap_filter, void **tap_datap, GFreeFunc *tap_freep)
3588{
3589 void *tap_data = NULL((void*)0);
3590 GFreeFunc tap_free = NULL((void*)0);
3591 GString *tap_error = NULL((void*)0);
3592
3593 if (!strncmp(tok_tap, "stat:", 5))
3594 {
3595 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
3596 stats_tree *st;
3597
3598 if (!cfg)
3599 {
3600 sharkd_json_error(
3601 rpcid, -11001, NULL((void*)0),
3602 "sharkd_session_process_tap() stat %s not found", tok_tap + 5
3603 );
3604 return false0;
3605 }
3606
3607 st = stats_tree_new(cfg, NULL((void*)0), tap_filter);
3608
3609 tap_error = register_tap_listener(st->cfg->tapname, st, st->filter, st->cfg->flags, stats_tree_reset, stats_tree_packet, sharkd_session_process_tap_stats_cb, NULL((void*)0));
3610
3611 if (!tap_error && cfg->init)
3612 cfg->init(st);
3613
3614 tap_data = st;
3615 tap_free = sharkd_session_free_tap_stats_cb;
3616 }
3617 else if (!strcmp(tok_tap, "expert"))
3618 {
3619 struct sharkd_expert_tap *expert_tap;
3620
3621 expert_tap = g_new0(struct sharkd_expert_tap, 1)((struct sharkd_expert_tap *) g_malloc0_n ((1), sizeof (struct
sharkd_expert_tap)))
;
3622 expert_tap->text = g_string_chunk_new(100);
3623
3624 tap_error = register_tap_listener("expert", expert_tap, tap_filter, 0, NULL((void*)0), sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb, NULL((void*)0));
3625
3626 tap_data = expert_tap;
3627 tap_free = sharkd_session_free_tap_expert_cb;
3628 }
3629 else if (!strncmp(tok_tap, "seqa:", 5))
3630 {
3631 seq_analysis_info_t *graph_analysis;
3632 register_analysis_t *analysis;
3633 const char *tap_name;
3634 tap_packet_cb tap_func;
3635 unsigned tap_flags;
3636
3637 analysis = sequence_analysis_find_by_name(tok_tap + 5);
3638 if (!analysis)
3639 {
3640 sharkd_json_error(
3641 rpcid, -11002, NULL((void*)0),
3642 "sharkd_session_process_tap() seq analysis %s not found", tok_tap + 5
3643 );
3644 return false0;
3645 }
3646
3647 graph_analysis = sequence_analysis_info_new();
3648 graph_analysis->name = tok_tap + 5;
3649 /* TODO, make configurable */
3650 graph_analysis->any_addr = false0;
3651
3652 tap_name = sequence_analysis_get_tap_listener_name(analysis);
3653 tap_flags = sequence_analysis_get_tap_flags(analysis);
3654 tap_func = sequence_analysis_get_packet_func(analysis);
3655
3656 tap_error = register_tap_listener(tap_name, graph_analysis, tap_filter, tap_flags, NULL((void*)0), tap_func, sharkd_session_process_tap_flow_cb, NULL((void*)0));
3657
3658 tap_data = graph_analysis;
3659 tap_free = sharkd_session_free_tap_flow_cb;
3660 }
3661 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
3662 {
3663 struct register_ct *ct = NULL((void*)0);
3664 const char *ct_tapname;
3665 struct sharkd_conv_tap_data *ct_data;
3666 tap_packet_cb tap_func = NULL((void*)0);
3667
3668 if (!strncmp(tok_tap, "conv:", 5))
3669 {
3670 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
3671
3672 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
3673 {
3674 sharkd_json_error(
3675 rpcid, -11003, NULL((void*)0),
3676 "sharkd_session_process_tap() conv %s not found", tok_tap + 5
3677 );
3678 return false0;
3679 }
3680 }
3681 else if (!strncmp(tok_tap, "endpt:", 6))
3682 {
3683 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
3684
3685 if (!ct || !(tap_func = get_endpoint_packet_func(ct)))
3686 {
3687 sharkd_json_error(
3688 rpcid, -11004, NULL((void*)0),
3689 "sharkd_session_process_tap() endpt %s not found", tok_tap + 6
3690 );
3691 return false0;
3692 }
3693 }
3694 else
3695 {
3696 sharkd_json_error(
3697 rpcid, -11005, NULL((void*)0),
3698 "sharkd_session_process_tap() conv/endpt(?): %s not found", tok_tap
3699 );
3700 return false0;
3701 }
3702
3703 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
3704
3705 ct_data = g_new0(struct sharkd_conv_tap_data, 1)((struct sharkd_conv_tap_data *) g_malloc0_n ((1), sizeof (struct
sharkd_conv_tap_data)))
;
3706 ct_data->type = tok_tap;
3707 ct_data->hash.user_data = ct_data;
3708
3709 /* XXX: make configurable */
3710 ct_data->resolve_name = true1;
3711 ct_data->resolve_port = true1;
3712
3713 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL((void*)0), tap_func, sharkd_session_process_tap_conv_cb, NULL((void*)0));
3714
3715 tap_data = &ct_data->hash;
3716 tap_free = sharkd_session_free_tap_conv_cb;
3717 }
3718 else if (!strncmp(tok_tap, "nstat:", 6))
3719 {
3720 stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6);
3721 stat_data_t *stat_data;
3722
3723 if (!stat_tap)
3724 {
3725 sharkd_json_error(
3726 rpcid, -11006, NULL((void*)0),
3727 "sharkd_session_process_tap() nstat=%s not found", tok_tap + 6
3728 );
3729 return false0;
3730 }
3731
3732 stat_tap->stat_tap_init_cb(stat_tap);
3733
3734 stat_data = g_new0(stat_data_t, 1)((stat_data_t *) g_malloc0_n ((1), sizeof (stat_data_t)));
3735 stat_data->stat_tap_data = stat_tap;
3736 stat_data->user_data = NULL((void*)0);
3737
3738 tap_error = register_tap_listener(stat_tap->tap_name, stat_data, tap_filter, 0, NULL((void*)0), stat_tap->packet_func, sharkd_session_process_tap_nstat_cb, NULL((void*)0));
3739
3740 tap_data = stat_data;
3741 tap_free = sharkd_session_free_tap_nstat_cb;
3742 }
3743 else if (!strncmp(tok_tap, "rtd:", 4))
3744 {
3745 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
3746 rtd_data_t *rtd_data;
3747 char *err;
3748
3749 if (!rtd)
3750 {
3751 sharkd_json_error(
3752 rpcid, -11007, NULL((void*)0),
3753 "sharkd_session_process_tap() rtd=%s not found", tok_tap + 4
3754 );
3755 return false0;
3756 }
3757
3758 rtd_table_get_filter(rtd, "", &tap_filter, &err);
3759 if (err != NULL((void*)0))
3760 {
3761 sharkd_json_error(
3762 rpcid, -11008, NULL((void*)0),
3763 "sharkd_session_process_tap() rtd=%s err=%s", tok_tap + 4, err
3764 );
3765 g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized
(err, __builtin_object_size ((err), 0)) : (g_free) (err)
;
3766 return false0;
3767 }
3768
3769 rtd_data = g_new0(rtd_data_t, 1)((rtd_data_t *) g_malloc0_n ((1), sizeof (rtd_data_t)));
3770 rtd_data->user_data = rtd;
3771 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL((void*)0), NULL((void*)0));
3772
3773 tap_error = register_tap_listener(get_rtd_tap_listener_name(rtd), rtd_data, tap_filter, 0, NULL((void*)0), get_rtd_packet_func(rtd), sharkd_session_process_tap_rtd_cb, NULL((void*)0));
3774
3775 tap_data = rtd_data;
3776 tap_free = sharkd_session_free_tap_rtd_cb;
3777 }
3778 else if (!strncmp(tok_tap, "srt:", 4))
3779 {
3780 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
3781 srt_data_t *srt_data;
3782 char *err;
3783
3784 if (!srt)
3785 {
3786 sharkd_json_error(
3787 rpcid, -11009, NULL((void*)0),
3788 "sharkd_session_process_tap() srt=%s not found", tok_tap + 4
3789 );
3790 return false0;
3791 }
3792
3793 srt_table_get_filter(srt, "", &tap_filter, &err);
3794 if (err != NULL((void*)0))
3795 {
3796 sharkd_json_error(
3797 rpcid, -11010, NULL((void*)0),
3798 "sharkd_session_process_tap() srt=%s err=%s", tok_tap + 4, err
3799 );
3800 g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized
(err, __builtin_object_size ((err), 0)) : (g_free) (err)
;
3801 return false0;
3802 }
3803
3804 srt_data = g_new0(srt_data_t, 1)((srt_data_t *) g_malloc0_n ((1), sizeof (srt_data_t)));
3805 srt_data->srt_array = g_array_new(FALSE(0), TRUE(!(0)), sizeof(srt_stat_table *));
3806 srt_data->user_data = srt;
3807 srt_table_dissector_init(srt, srt_data->srt_array);
3808
3809 tap_error = register_tap_listener(get_srt_tap_listener_name(srt), srt_data, tap_filter, 0, NULL((void*)0), get_srt_packet_func(srt), sharkd_session_process_tap_srt_cb, NULL((void*)0));
3810
3811 tap_data = srt_data;
3812 tap_free = sharkd_session_free_tap_srt_cb;
3813 }
3814 else if (!strncmp(tok_tap, "eo:", 3))
3815 {
3816 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
3817
3818 if (!eo)
3819 {
3820 sharkd_json_error(
3821 rpcid, -11011, NULL((void*)0),
3822 "sharkd_session_process_tap() eo=%s not found", tok_tap + 3
3823 );
3824 return false0;
3825 }
3826
3827 tap_error = sharkd_session_eo_register_tap_listener(eo, tok_tap, tap_filter, sharkd_session_process_tap_eo_cb, &tap_data, &tap_free);
3828
3829 /* tap_data & tap_free assigned by sharkd_session_eo_register_tap_listener */
3830 }
3831 else if (!strcmp(tok_tap, "rtp-streams"))
3832 {
3833 rtpstream_tapinfo_t *rtp_tapinfo = g_new0(rtpstream_tapinfo_t, 1)((rtpstream_tapinfo_t *) g_malloc0_n ((1), sizeof (rtpstream_tapinfo_t
)))
;
3834 rtp_tapinfo->mode = TAP_ANALYSE; // This is 0 anyway
3835
3836 tap_error = register_tap_listener("rtp", rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet_cb, sharkd_session_process_tap_rtp_cb, NULL((void*)0));
3837
3838 tap_data = rtp_tapinfo;
3839 tap_free = rtpstream_free_cb;
3840 }
3841 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
3842 {
3843 struct sharkd_analyse_rtp *rtp_req;
3844
3845 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
3846 if (!sharkd_rtp_match_init(&rtp_req->id, tok_tap + 12))
3847 {
3848 sharkd_json_error(
3849 rpcid, -10001, NULL((void*)0),
3850 "sharkd_session_process_tap() rtp tokenizing error %s", tok_tap
3851 );
3852 rtpstream_id_free(&rtp_req->id);
3853 g_free(rtp_req)(__builtin_object_size ((rtp_req), 0) != ((size_t) - 1)) ? g_free_sized
(rtp_req, __builtin_object_size ((rtp_req), 0)) : (g_free) (
rtp_req)
;
3854 return false0;
3855 }
3856
3857 rtp_req->tap_name = tok_tap;
3858 rtp_req->statinfo.first_packet = true1;
3859 rtp_req->statinfo.reg_pt = PT_UNDEFINED-1;
3860
3861 tap_error = register_tap_listener("rtp", rtp_req, tap_filter, 0, NULL((void*)0), sharkd_session_packet_tap_rtp_analyse_cb, sharkd_session_process_tap_rtp_analyse_cb, NULL((void*)0));
3862
3863 tap_data = rtp_req;
3864 tap_free = sharkd_session_process_tap_rtp_free_cb;
3865 }
3866 else if (!strcmp(tok_tap, "multicast"))
3867 {
3868 mcaststream_tapinfo_t *mcaststream_tapinfo;
3869 mcaststream_tapinfo = (mcaststream_tapinfo_t *) g_malloc0(sizeof(*mcaststream_tapinfo));
3870
3871 tap_error = register_tap_listener("udp", mcaststream_tapinfo, tap_filter, 0, NULL((void*)0), mcaststream_packet, sharkd_session_process_tap_multicast_cb, NULL((void*)0));
3872 tap_data = mcaststream_tapinfo;
3873 tap_free = sharkd_session_process_free_tap_multicast_cb;
3874 }
3875 else if (!strcmp(tok_tap, "phs"))
3876 {
3877 phs_t *rs;
3878
3879 pc_proto_id = proto_registrar_get_id_byname("pkt_comment");
3880
3881 rs = new_phs_t(NULL((void*)0), tap_filter);
3882
3883 tap_error = register_tap_listener("frame", rs, tap_filter,
3884 TL_REQUIRES_PROTO_TREE0x00000001|TL_REQUIRES_PROTOCOLS0x00000020,
3885 NULL((void*)0), protohierstat_packet,
3886 sharkd_session_process_tap_phs_cb, NULL((void*)0));
3887
3888 tap_data = rs;
3889 tap_free = sharkd_session_free_tap_phs_cb;
3890 }
3891 else if (!strcmp(tok_tap, "voip-calls"))
3892 {
3893 voip_stat_init_tapinfo();
3894
3895 tap_error = register_tap_listener("frame", &tapinfo_, tap_filter, 0, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_voip_calls_cb, NULL((void*)0));
3896
3897 tapinfo_.session = cfile.epan;
3898 voip_calls_init_all_taps(&tapinfo_);
3899
3900 tap_data = &tapinfo_;
3901 tap_free = sharkd_session_free_tap_voip_calls_cb;
3902 }
3903 else if (!strncmp(tok_tap, "voip-convs:", 11))
3904 {
3905 int len;
3906 unsigned int min, max;
3907 struct sharkd_voip_convs_req *voip_convs_req;
3908 const char *conv_arg = tok_tap + 11;
3909
3910 // parse tok_tap to get which call we are asking for
3911 if (*conv_arg == 0) {
3912 // set all bits of voip_conv_sel (-1 in binary is all 1's)
3913 memset(voip_conv_sel, -1, sizeof(voip_conv_sel));
3914 } else {
3915 memset(voip_conv_sel, 0, sizeof(voip_conv_sel));
3916
3917 while (*conv_arg != 0) {
3918 if (*conv_arg == ',') {
3919 conv_arg++;
3920 }
3921 if (sscanf(conv_arg, "%u-%u%n", &min, &max, &len) == 2) {
3922 conv_arg += len;
3923 } else if (sscanf(conv_arg, "%u%n", &min, &len) == 1) {
3924 max = min;
3925 conv_arg += len;
3926 } else {
3927 sharkd_json_error(
3928 rpcid, -11014, NULL((void*)0),
3929 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' parameter", tok_tap
3930 );
3931 return false0;
3932 }
3933 if (min > max || min >= VOIP_CONV_MAX((sizeof(int) * 8) * ((1<<(sizeof(uint16_t) * 8))/(sizeof
(int) * 8)))
|| max >= VOIP_CONV_MAX((sizeof(int) * 8) * ((1<<(sizeof(uint16_t) * 8))/(sizeof
(int) * 8)))
) {
3934 sharkd_json_error(
3935 rpcid, -11012, NULL((void*)0),
3936 "sharkd_session_process_tap() voip-convs=%s invalid 'convs' number range", tok_tap
3937 );
3938 return false0;
3939 }
3940 for(; min <= max; min++) {
3941 voip_conv_sel[min / VOIP_CONV_BITS(sizeof(int) * 8)] |= 1 << (min % VOIP_CONV_BITS(sizeof(int) * 8));
3942 }
3943 }
3944 }
3945
3946 voip_stat_init_tapinfo();
3947
3948 voip_convs_req = (struct sharkd_voip_convs_req *) g_malloc0(sizeof(*voip_convs_req));
3949 voip_convs_req->tapinfo = &tapinfo_;
3950 voip_convs_req->tap_name = tok_tap;
3951
3952 tap_error = register_tap_listener("frame", voip_convs_req, tap_filter, 0, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_voip_convs_cb, NULL((void*)0));
3953
3954 tapinfo_.session = cfile.epan;
3955 voip_calls_init_all_taps(&tapinfo_);
3956
3957 tap_data = voip_convs_req;
3958 tap_free = sharkd_session_free_tap_voip_convs_cb;
3959 }
3960 else if (!strncmp(tok_tap, "hosts:", 6))
3961 {
3962 bool_Bool dump_v4;
3963 bool_Bool dump_v6;
3964 struct sharkd_hosts_req *hosts_req;
3965 const char *proto_arg;
3966 char **proto_tokens;
3967 int proto_count;
3968
3969 proto_arg = tok_tap + 6;
3970
3971 if (strlen(proto_arg) == 0) {
3972 dump_v4 = true1;
3973 dump_v6 = true1;
3974 } else {
3975 dump_v4 = false0;
3976 dump_v6 = false0;
3977
3978 proto_tokens = g_strsplit(proto_arg, ",", 0);
3979 proto_count = 0;
3980 while (proto_tokens[proto_count]) {
3981 if (!strcmp("ip", proto_tokens[proto_count]) ||
3982 !strcmp("ipv4", proto_tokens[proto_count])) {
3983 dump_v4 = true1;
3984 } else if (!strcmp("ipv6", proto_tokens[proto_count])) {
3985 dump_v6 = true1;
3986 } else {
3987 g_strfreev(proto_tokens);
3988 sharkd_json_error(
3989 rpcid, -11015, NULL((void*)0),
3990 "sharkd_session_process_tap() hosts=%s invalid 'protos' parameter", tok_tap
3991 );
3992 return false0;
3993 }
3994 proto_count++;
3995 }
3996 g_strfreev(proto_tokens);
3997 }
3998
3999 hosts_req = (struct sharkd_hosts_req *)g_malloc0(sizeof(*hosts_req));
4000 hosts_req->dump_v4 = dump_v4;
4001 hosts_req->dump_v6 = dump_v6;
4002 hosts_req->tap_name = tok_tap;
4003
4004 tap_error = register_tap_listener("frame", hosts_req, tap_filter, TL_REQUIRES_PROTO_TREE0x00000001, NULL((void*)0), NULL((void*)0), sharkd_session_process_tap_hosts_cb, NULL((void*)0));
4005
4006 tap_data = hosts_req;
4007 tap_free = sharkd_session_free_tap_hosts_cb;
4008 }
4009 else
4010 {
4011 sharkd_json_error(
4012 rpcid, -11012, NULL((void*)0),
4013 "sharkd_session_process_tap() %s not recognized", tok_tap
4014 );
4015 return false0;
4016 }
4017
4018 if (tap_error)
4019 {
4020 sharkd_json_error(
4021 rpcid, -11013, NULL((void*)0),
4022 "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str
4023 );
4024 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
4025 if (tap_free)
4026 tap_free(tap_data);
4027 return false0;
4028 }
4029
4030 *tap_freep = tap_free;
4031 *tap_datap = tap_data;
4032 return true1;
4033}
4034
4035/**
4036 * sharkd_session_process_tap()
4037 *
4038 * Process tap request
4039 *
4040 * Input:
4041 * (m) tap0 - First tap request
4042 * (o) tap1...tap15 - Other tap requests
4043 *
4044 * Output object with attributes:
4045 * (m) taps - array of object with attributes:
4046 * (m) tap - tap name
4047 * (m) type - tap output type
4048 * ...
4049 * for type:stats see sharkd_session_process_tap_stats_cb()
4050 * for type:nstat see sharkd_session_process_tap_nstat_cb()
4051 * for type:conv see sharkd_session_process_tap_conv_cb()
4052 * for type:host see sharkd_session_process_tap_conv_cb()
4053 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
4054 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
4055 * for type:eo see sharkd_session_process_tap_eo_cb()
4056 * for type:expert see sharkd_session_process_tap_expert_cb()
4057 * for type:rtd see sharkd_session_process_tap_rtd_cb()
4058 * for type:srt see sharkd_session_process_tap_srt_cb()
4059 * for type:flow see sharkd_session_process_tap_flow_cb()
4060 *
4061 * (m) err - error code
4062 */
4063static void
4064sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
4065{
4066 void *taps_data[16];
4067 GFreeFunc taps_free[16];
4068 int taps_count = 0;
4069 int i;
4070 const char *tap_filter = json_find_attr(buf, tokens, count, "filter");
4071
4072 for (i = 0; i < 16; i++)
4073 {
4074 char tapbuf[32];
4075 const char *tok_tap;
4076
4077 void *tap_data = NULL((void*)0);
4078 GFreeFunc tap_free = NULL((void*)0);
4079
4080 snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
4081 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
4082 if (!tok_tap)
4083 break;
4084
4085 if (!sharkd_session_register_tap(tok_tap, tap_filter, &tap_data, &tap_free)) {
4086 goto cleanup;
4087 }
4088
4089 taps_data[taps_count] = tap_data;
4090 taps_free[taps_count] = tap_free;
4091 taps_count++;
4092 }
4093
4094 fprintf(stderrstderr, "sharkd_session_process_tap() count=%d\n", taps_count);
4095 if (taps_count == 0)
4096 {
4097 sharkd_json_result_prologue(rpcid);
4098 sharkd_json_array_open("taps");
4099 sharkd_json_array_close();
4100 sharkd_json_result_epilogue();
4101 return;
4102 }
4103
4104 sharkd_json_result_prologue(rpcid);
4105 sharkd_json_array_open("taps");
4106 sharkd_retap();
4107 sharkd_json_array_close();
4108 sharkd_json_result_epilogue();
4109
4110cleanup:
4111 for (i = 0; i < taps_count; i++)
4112 {
4113 if (taps_data[i])
4114 remove_tap_listener(taps_data[i]);
4115
4116 if (taps_free[i])
4117 taps_free[i](taps_data[i]);
4118 }
4119}
4120
4121/**
4122 * sharkd_session_process_follow()
4123 *
4124 * Process follow request
4125 *
4126 * Input:
4127 * (m) follow - follow protocol request (e.g. HTTP)
4128 * (m) filter - filter request (e.g. tcp.stream == 1)
4129 * (m) stream - stream index number
4130 * (o) sub_stream - follow sub-stream index number (e.g. for HTTP/2 and QUIC streams)
4131 *
4132 * Output object with attributes:
4133 *
4134 * (m) err - error code
4135 * (m) shost - server host
4136 * (m) sport - server port
4137 * (m) sbytes - server send bytes count
4138 * (m) chost - client host
4139 * (m) cport - client port
4140 * (m) cbytes - client send bytes count
4141 * (o) payloads - array of object with attributes:
4142 * (o) s - set if server sent, else client
4143 * (m) n - packet number
4144 * (m) d - data base64 encoded
4145 */
4146static void
4147sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
4148{
4149 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
4150 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4151 const char *tok_sub_stream = json_find_attr(buf, tokens, count, "sub_stream");
4152
4153 register_follow_t *follower;
4154 GString *tap_error;
4155
4156 follow_info_t *follow_info;
4157 const char *host;
4158 char *port;
4159
4160 follower = get_follow_by_name(tok_follow);
4161 if (!follower)
4162 {
4163 sharkd_json_error(
4164 rpcid, -12001, NULL((void*)0),
4165 "sharkd_session_process_follow() follower=%s not found", tok_follow
4166 );
4167 return;
4168 }
4169
4170 uint64_t substream_id = SUBSTREAM_UNUSED0xFFFFFFFFFFFFFFFFUL;
4171 if (tok_sub_stream)
4172 {
4173 ws_strtou64(tok_sub_stream, NULL((void*)0), &substream_id);
4174 }
4175
4176 /* follow_reset_stream ? */
4177 follow_info = g_new0(follow_info_t, 1)((follow_info_t *) g_malloc0_n ((1), sizeof (follow_info_t)));
4178 follow_info->substream_id = substream_id;
4179 /* gui_data, filter_out_filter not set, but not used by dissector */
4180
4181 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL((void*)0), get_follow_tap_handler(follower), NULL((void*)0), NULL((void*)0));
4182 if (tap_error)
4183 {
4184 sharkd_json_error(
4185 rpcid, -12002, NULL((void*)0),
4186 "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str
4187 );
4188 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
4189 g_free(follow_info)(__builtin_object_size ((follow_info), 0) != ((size_t) - 1)) ?
g_free_sized (follow_info, __builtin_object_size ((follow_info
), 0)) : (g_free) (follow_info)
;
4190 return;
4191 }
4192
4193 sharkd_retap();
4194
4195 sharkd_json_result_prologue(rpcid);
4196
4197 /* Server information: hostname, port, bytes sent */
4198 host = address_to_name(&follow_info->server_ip);
4199 sharkd_json_value_string("shost", host);
4200
4201 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->server_port);
4202 sharkd_json_value_string("sport", port);
4203 wmem_free(NULL((void*)0), port);
4204
4205 sharkd_json_value_anyf("sbytes", "%u", follow_info->bytes_written[0]);
4206
4207 /* Client information: hostname, port, bytes sent */
4208 host = address_to_name(&follow_info->client_ip);
4209 sharkd_json_value_string("chost", host);
4210
4211 port = get_follow_port_to_display(follower)(NULL((void*)0), follow_info->client_port);
4212 sharkd_json_value_string("cport", port);
4213 wmem_free(NULL((void*)0), port);
4214
4215 sharkd_json_value_anyf("cbytes", "%u", follow_info->bytes_written[1]);
4216
4217 if (follow_info->payload)
4218 {
4219 follow_record_t *follow_record;
4220 GList *cur;
4221
4222 sharkd_json_array_open("payloads");
4223 for (cur = g_list_last(follow_info->payload); cur; cur = g_list_previous(cur)((cur) ? (((GList *)(cur))->prev) : ((void*)0)))
4224 {
4225 follow_record = (follow_record_t *) cur->data;
4226
4227 json_dumper_begin_object(&dumper);
4228
4229 sharkd_json_value_anyf("n", "%u", follow_record->packet_num);
4230 sharkd_json_value_base64("d", follow_record->data->data, follow_record->data->len);
4231
4232 if (follow_record->is_server)
4233 sharkd_json_value_anyf("s", "%d", 1);
4234
4235 json_dumper_end_object(&dumper);
4236 }
4237 sharkd_json_array_close();
4238 }
4239
4240 sharkd_json_result_epilogue();
4241
4242 remove_tap_listener(follow_info);
4243 follow_info_free(follow_info);
4244}
4245
4246static void
4247// NOLINTNEXTLINE(misc-no-recursion)
4248sharkd_session_process_frame_cb_tree(const char *key, epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs, bool_Bool display_hidden)
4249{
4250 proto_node *node;
4251
4252 sharkd_json_array_open(key);
4253 for (node = tree->first_child; node; node = node->next)
4254 {
4255 field_info *finfo = PNODE_FINFO(node)((node)->finfo);
4256
4257 if (!finfo)
4258 continue;
4259
4260 if (!display_hidden && FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4261 continue;
4262
4263 json_dumper_begin_object(&dumper);
4264
4265 if (!finfo->rep)
4266 {
4267 char label_str[ITEM_LABEL_LENGTH240];
4268
4269 label_str[0] = '\0';
4270 proto_item_fill_label(finfo, label_str, NULL((void*)0));
4271 sharkd_json_value_string("l", label_str);
4272 }
4273 else
4274 {
4275 sharkd_json_value_string("l", finfo->rep->representation);
4276 }
4277
4278 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
4279 {
4280 int idx;
4281
4282 for (idx = 1; tvbs[idx]; idx++)
4283 {
4284 if (tvbs[idx] == finfo->ds_tvb)
4285 {
4286 sharkd_json_value_anyf("ds", "%d", idx);
4287 break;
4288 }
4289 }
4290 }
4291
4292 if (finfo->length > 0)
4293 sharkd_json_value_anyf("h", "[%d,%d]", finfo->start, finfo->length);
4294
4295 if (finfo->appendix_length > 0)
4296 sharkd_json_value_anyf("i", "[%d,%d]", finfo->appendix_start, finfo->appendix_length);
4297
4298
4299 if (finfo->hfinfo)
4300 {
4301 char *filter;
4302
4303 if (finfo->hfinfo->type == FT_PROTOCOL)
4304 {
4305 sharkd_json_value_string("t", "proto");
4306 }
4307 else if (finfo->hfinfo->type == FT_FRAMENUM)
4308 {
4309 sharkd_json_value_string("t", "framenum");
4310 sharkd_json_value_anyf("fnum", "%u", fvalue_get_uinteger(finfo->value));
4311 }
4312 else if (FI_GET_FLAG(finfo, FI_URL)((finfo) ? ((finfo)->flags & (0x00000004)) : 0) && FT_IS_STRING(finfo->hfinfo->type)((finfo->hfinfo->type) == FT_STRING || (finfo->hfinfo
->type) == FT_STRINGZ || (finfo->hfinfo->type) == FT_STRINGZPAD
|| (finfo->hfinfo->type) == FT_STRINGZTRUNC || (finfo->
hfinfo->type) == FT_UINT_STRING || (finfo->hfinfo->type
) == FT_AX25)
)
4313 {
4314 char *url = fvalue_to_string_repr(NULL((void*)0), finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
4315
4316 sharkd_json_value_string("t", "url");
4317 sharkd_json_value_string("url", url);
4318 wmem_free(NULL((void*)0), url);
4319 }
4320
4321 filter = proto_construct_match_selected_string(finfo, edt);
4322 if (filter)
4323 {
4324 sharkd_json_value_string("f", filter);
4325 wmem_free(NULL((void*)0), filter);
4326 }
4327
4328 if (finfo->hfinfo->abbrev)
4329 sharkd_json_value_string("fn", finfo->hfinfo->abbrev);
4330 }
4331
4332 if (FI_GET_FLAG(finfo, FI_GENERATED)((finfo) ? ((finfo)->flags & (0x00000002)) : 0))
4333 sharkd_json_value_anyf("g", "true");
4334
4335 if (FI_GET_FLAG(finfo, FI_HIDDEN)((finfo) ? ((finfo)->flags & (0x00000001)) : 0))
4336 sharkd_json_value_anyf("v", "true");
4337
4338 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0))
4339 {
4340 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK)((finfo) ? ((finfo)->flags & (0x00F00000)) : 0), expert_severity_vals);
4341
4342 ws_assert(severity != NULL)do { if ((1) && !(severity != ((void*)0))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "sharkd_session.c", 4342, __func__, "assertion failed: %s"
, "severity != ((void*)0)"); } while (0)
;
4343
4344 sharkd_json_value_string("s", severity);
4345 }
4346
4347 if (((proto_tree *) node)->first_child)
4348 {
4349 if (finfo->tree_type != -1)
4350 sharkd_json_value_anyf("e", "%d", finfo->tree_type);
4351
4352 // We recurse here but our depth is limited
4353 sharkd_session_process_frame_cb_tree("n", edt, (proto_tree *) node, tvbs, display_hidden);
4354 }
4355
4356 json_dumper_end_object(&dumper);
4357 }
4358 sharkd_json_array_close();
4359}
4360
4361static bool_Bool
4362sharkd_follower_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4363{
4364 register_follow_t *follower = (register_follow_t *) value;
4365 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4366 packet_info *pi = &edt->pi;
4367
4368 const int proto_id = get_follow_proto_id(follower);
4369
4370 uint32_t ignore_stream;
4371 uint32_t ignore_sub_stream;
4372
4373 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4374 {
4375 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4376 char *follow_filter;
4377
4378 follow_filter = get_follow_conv_func(follower)(edt, pi, &ignore_stream, &ignore_sub_stream);
4379
4380 json_dumper_begin_array(&dumper);
4381 json_dumper_value_string(&dumper, layer_proto);
4382 json_dumper_value_string(&dumper, follow_filter);
4383 json_dumper_end_array(&dumper);
4384
4385 g_free(follow_filter)(__builtin_object_size ((follow_filter), 0) != ((size_t) - 1)
) ? g_free_sized (follow_filter, __builtin_object_size ((follow_filter
), 0)) : (g_free) (follow_filter)
;
4386 }
4387
4388 return false0;
4389}
4390
4391static bool_Bool
4392sharkd_followers_visit_layers_cb(const void *key _U___attribute__((unused)), void *value, void *user_data)
4393{
4394 register_follow_t *follower = (register_follow_t *) value;
4395 epan_dissect_t *edt = (epan_dissect_t *) user_data;
4396 packet_info *pi = &edt->pi;
4397
4398 const int proto_id = get_follow_proto_id(follower);
4399
4400 unsigned stream;
4401 unsigned sub_stream;
4402
4403 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
4404 {
4405 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
4406 char *follow_filter;
4407
4408 follow_filter = get_follow_conv_func(follower)(edt, pi, &stream, &sub_stream);
4409
4410 sharkd_json_object_open(NULL((void*)0));
4411 sharkd_json_value_string("protocol", layer_proto);
4412 sharkd_json_value_string("filter", follow_filter);
4413 if (get_follow_stream_count_func(follower) != NULL((void*)0))
4414 {
4415 sharkd_json_value_anyf("stream", "%u", stream);
4416 }
4417 if (get_follow_sub_stream_id_func(follower) != NULL((void*)0))
4418 {
4419 sharkd_json_value_anyf("sub_stream", "%u", sub_stream);
4420 }
4421 sharkd_json_object_close();
4422
4423 g_free(follow_filter)(__builtin_object_size ((follow_filter), 0) != ((size_t) - 1)
) ? g_free_sized (follow_filter, __builtin_object_size ((follow_filter
), 0)) : (g_free) (follow_filter)
;
4424 }
4425
4426 return false0;
4427}
4428
4429struct sharkd_frame_request_data
4430{
4431 bool_Bool display_hidden;
4432};
4433
4434static void
4435sharkd_session_process_add_data_source(struct data_source *src, bool_Bool add_description)
4436{
4437 tvbuff_t *tvb;
4438 unsigned length;
4439
4440 if (add_description) {
4441 char *src_description = get_data_source_description(src);
4442
4443 sharkd_json_value_string("name", src_description);
4444 wmem_free(NULL((void*)0), src_description);
4445 }
4446
4447 tvb = get_data_source_tvb(src);
4448
4449 TRY{ except_t *volatile exc; volatile int except_state = 0; static
const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode
except_sn; struct except_catch except_ch; except_setup_try(&
except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch
.except_jmp)) *(&exc) = &except_ch.except_obj; else *
(&exc) = 0; if(except_state & 1) except_state |= 2; except_state
&= ~1; if (except_state == 0 && exc == 0)
{
4450 length = tvb_captured_length(tvb);
4451
4452 if (length != 0)
4453 {
4454 const unsigned char *cp = tvb_get_ptr(tvb, 0, length);
4455
4456 /* XXX pi.fd->encoding */
4457 sharkd_json_value_base64("bytes", cp, length);
4458 }
4459 else
4460 {
4461 sharkd_json_value_base64("bytes", (const uint8_t*)"", 0);
4462 }
4463 } CATCH_BOUNDS_AND_DISSECTOR_ERRORSif (except_state == 0 && exc != 0 && (exc->
except_id.except_code == (1) || exc->except_id.except_code
== (4) || exc->except_id.except_code == (2) || exc->except_id
.except_code == (3) || exc->except_id.except_code == (7) ||
exc->except_id.except_code == (6) || exc->except_id.except_code
== (9)) && (except_state|=1))
{
4464 /* tvb_captured_length can throw DissectorError. With an
4465 * offset of 0 and a length of the captured length, tvb_get_ptr
4466 * can throw DissectorError or various bounds errors. We have
4467 * to catch it so that the JSON is valid. */
4468 sharkd_json_value_base64("bytes", (const uint8_t*)"", 0);
4469 } CATCH_ALLif (except_state == 0 && exc != 0 && (except_state
|=1))
{
4470 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "sharkd_session.c", 4470
, __func__, "assertion \"not reached\" failed")
;
4471 }
4472 ENDTRYif(!(except_state&1) && exc != 0) except_rethrow(
exc); except_free(except_ch.except_obj.except_dyndata); except_pop
(); };}
;
4473}
4474
4475static void
4476sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
4477{
4478 packet_info *pi = &edt->pi;
4479 frame_data *fdata = pi->fd;
4480 wtap_block_t pkt_block = NULL((void*)0);
4481
4482 const struct sharkd_frame_request_data * const req_data = (const struct sharkd_frame_request_data * const) data;
4483 const bool_Bool display_hidden = (req_data) ? req_data->display_hidden : false0;
4484
4485 sharkd_json_result_prologue(rpcid);
4486
4487 if (fdata->has_modified_block)
4488 pkt_block = sharkd_get_modified_block(fdata);
4489 else
4490 pkt_block = pi->rec->block;
4491
4492 if (pkt_block)
4493 {
4494 unsigned i;
4495 unsigned n;
4496 char *comment;
4497
4498 n = wtap_block_count_option(pkt_block, OPT_COMMENT1);
4499
4500 sharkd_json_array_open("comment");
4501 for (i = 0; i < n; i++) {
4502 if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment)) {
4503 sharkd_json_value_string(NULL((void*)0), comment);
4504 }
4505 }
4506 sharkd_json_array_close();
4507 }
4508
4509 if (tree)
4510 {
4511 tvbuff_t **tvbs = NULL((void*)0);
4512
4513 /* arrayize data src, to speedup searching for ds_tvb index */
4514 if (data_src && data_src->next /* only needed if there are more than one data source */)
4515 {
4516 unsigned count = g_slist_length((GSList *) data_src);
4517 unsigned i;
4518
4519 tvbs = (tvbuff_t **) g_malloc0((count + 1) * sizeof(*tvbs));
4520
4521 for (i = 0; i < count; i++)
4522 {
4523 const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i);
4524
4525 tvbs[i] = get_data_source_tvb(src);
4526 }
4527
4528 tvbs[count] = NULL((void*)0);
4529 }
4530
4531 sharkd_session_process_frame_cb_tree("tree", edt, tree, tvbs, display_hidden);
4532
4533 g_free(tvbs)(__builtin_object_size ((tvbs), 0) != ((size_t) - 1)) ? g_free_sized
(tvbs, __builtin_object_size ((tvbs), 0)) : (g_free) (tvbs)
;
4534 }
4535
4536 if (cinfo)
4537 {
4538 unsigned col;
4539
4540 sharkd_json_array_open("col");
4541 for (col = 0; col < cinfo->num_cols; ++col)
4542 {
4543 sharkd_json_value_string(NULL((void*)0), get_column_text(cinfo, col));
4544 }
4545 sharkd_json_array_close();
4546 }
4547
4548 if (fdata->ignored)
4549 sharkd_json_value_anyf("i", "true");
4550
4551 if (fdata->marked)
4552 sharkd_json_value_anyf("m", "true");
4553
4554 if (fdata->color_filter)
4555 {
4556 sharkd_json_value_stringf("bg", "%06x", color_t_to_rgb(&fdata->color_filter->bg_color));
4557 sharkd_json_value_stringf("fg", "%06x", color_t_to_rgb(&fdata->color_filter->fg_color));
4558 }
4559
4560 if (data_src)
4561 {
4562 struct data_source *src = (struct data_source *) data_src->data;
4563 bool_Bool ds_open = false0;
4564
4565 sharkd_session_process_add_data_source(src, ds_open);
4566
4567 data_src = data_src->next;
4568 if (data_src)
4569 {
4570 sharkd_json_array_open("ds");
4571 ds_open = true1;
4572 }
4573
4574 while (data_src)
4575 {
4576 src = (struct data_source *) data_src->data;
4577
4578 json_dumper_begin_object(&dumper);
4579
4580 sharkd_session_process_add_data_source(src, ds_open);
4581
4582 json_dumper_end_object(&dumper);
4583
4584 data_src = data_src->next;
4585 }
4586
4587 /* close ds, only if was opened */
4588 if (ds_open)
4589 sharkd_json_array_close();
4590 }
4591
4592 sharkd_json_array_open("fol");
4593 follow_iterate_followers(sharkd_follower_visit_layers_cb, edt);
4594 sharkd_json_array_close();
4595
4596 sharkd_json_array_open("followers");
4597 follow_iterate_followers(sharkd_followers_visit_layers_cb, edt);
4598 sharkd_json_array_close();
4599
4600 sharkd_json_result_epilogue();
4601}
4602
4603#define SHARKD_IOGRAPH_MAX_ITEMS1 << 25 1 << 25 /* 33,554,432 limit of items, same as max_io_items_ in ui/qt/io_graph_dialog.h */
4604
4605struct sharkd_iograph
4606{
4607 /* config */
4608 int hf_index;
4609 io_graph_item_unit_t calc_type;
4610 uint32_t interval;
4611 bool_Bool aot;
4612
4613 /* result */
4614 int space_items;
4615 int num_items;
4616 io_graph_item_t *items;
4617 GString *error;
4618};
4619
4620static tap_packet_status
4621sharkd_iograph_packet(void *g, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U___attribute__((unused)), tap_flags_t flags _U___attribute__((unused)))
4622{
4623 struct sharkd_iograph *graph = (struct sharkd_iograph *) g;
4624 int idx;
4625 bool_Bool update_succeeded;
4626
4627 int64_t tmp_idx = get_io_graph_index(pinfo, graph->interval);
4628 if (tmp_idx < 0 || tmp_idx >= SHARKD_IOGRAPH_MAX_ITEMS1 << 25)
1
Assuming 'tmp_idx' is >= 0
2
Assuming the condition is false
3
Taking false branch
4629 return TAP_PACKET_DONT_REDRAW;
4630
4631 idx = (int)tmp_idx;
4632
4633 if (idx + 1 > graph->num_items)
4
Assuming the condition is true
5
Taking true branch
4634 {
4635 if (idx + 1 > graph->space_items)
6
Assuming the condition is false
7
Taking false branch
4636 {
4637 int new_size = idx + 1024;
4638
4639 graph->items = (io_graph_item_t *) g_realloc(graph->items, sizeof(io_graph_item_t) * new_size);
4640 reset_io_graph_items(&graph->items[graph->space_items], new_size - graph->space_items, graph->hf_index);
4641
4642 graph->space_items = new_size;
4643 }
4644 else if (graph->items == NULL((void*)0))
8
Assuming field 'items' is equal to NULL
9
Taking true branch
4645 {
4646 graph->items = g_new(io_graph_item_t, graph->space_items)((io_graph_item_t *) g_malloc_n ((graph->space_items), sizeof
(io_graph_item_t)))
;
10
Uninitialized value stored to field 'first_frame_in_invl'
4647 reset_io_graph_items(graph->items, graph->space_items, graph->hf_index);
11
Calling 'reset_io_graph_items'
14
Returning from 'reset_io_graph_items'
4648 }
4649
4650 graph->num_items = idx + 1;
4651 }
4652
4653 update_succeeded = update_io_graph_item(graph->items, idx, pinfo, edt, graph->hf_index, graph->calc_type, graph->interval);
15
Calling 'update_io_graph_item'
4654 /* XXX - TAP_PACKET_FAILED if the item couldn't be updated, with an error message? */
4655 return update_succeeded ? TAP_PACKET_REDRAW : TAP_PACKET_DONT_REDRAW;
4656}
4657
4658/**
4659 * sharkd_session_process_iograph()
4660 *
4661 * Process iograph request
4662 *
4663 * Input:
4664 * (o) interval - interval time, if not specified: 1000
4665 * (o) interval_units - units for interval time, must be 's', 'ms' or 'us', if not specified: ms
4666 * (m) graph0 - First graph request
4667 * (o) graph1...graph9 - Other graph requests
4668 * (o) filter0 - First graph filter
4669 * (o) filter1...filter9 - Other graph filters
4670 *
4671 * Graph requests can be one of: "packets", "bytes", "bits", "sum:<field>", "frames:<field>", "max:<field>", "min:<field>", "avg:<field>", "load:<field>",
4672 * if you use variant with <field>, you need to pass field name in filter request.
4673 *
4674 * Output object with attributes:
4675 * (m) iograph - array of graph results with attributes:
4676 * errmsg - graph cannot be constructed
4677 * items - graph values, zeros are skipped, if value is not a number it's next index encoded as hex string
4678 */
4679static void
4680sharkd_session_process_iograph(char *buf, const jsmntok_t *tokens, int count)
4681{
4682 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4683 const char *tok_interval_units = json_find_attr(buf, tokens, count, "interval_units");
4684 struct sharkd_iograph graphs[10];
4685 unsigned graph_count;
4686
4687 unsigned i;
4688
4689 /* default: 1000ms = one per second */
4690 uint32_t interval = 1000;
4691 const char *interval_units = "ms";
4692
4693 if (tok_interval)
4694 ws_strtou32(tok_interval, NULL((void*)0), &interval);
4695
4696 if (tok_interval_units)
4697 {
4698 if (strcmp(tok_interval_units, "us") != 0 &&
4699 strcmp(tok_interval_units, "ms") != 0 &&
4700 strcmp(tok_interval_units, "s") != 0)
4701 {
4702 sharkd_json_error(
4703 rpcid, -7003, NULL((void*)0),
4704 "Invalid interval_units parameter: '%s', must be 's', 'ms' or 'us'", tok_interval_units
4705 );
4706 return;
4707 }
4708 interval_units = tok_interval_units;
4709 }
4710
4711 uint32_t interval_us = 0;
4712 if (strcmp(interval_units, "us") == 0)
4713 {
4714 interval_us = interval;
4715 }
4716 else if (strcmp(interval_units, "ms") == 0)
4717 {
4718 interval_us = 1000 * interval;
4719 }
4720 else if (strcmp(interval_units, "s") == 0)
4721 {
4722 interval_us = 1000000 * interval;
4723 }
4724
4725 for (i = graph_count = 0; i < G_N_ELEMENTS(graphs)(sizeof (graphs) / sizeof ((graphs)[0])); i++)
4726 {
4727 struct sharkd_iograph *graph = &graphs[graph_count];
4728
4729 const char *tok_graph;
4730 const char *tok_filter;
4731 char tok_format_buf[32];
4732 const char *field_name;
4733 const char *tok_aot;
4734
4735 snprintf(tok_format_buf, sizeof(tok_format_buf), "graph%d", i);
4736 tok_graph = json_find_attr(buf, tokens, count, tok_format_buf);
4737 if (!tok_graph)
4738 break;
4739
4740 snprintf(tok_format_buf, sizeof(tok_format_buf), "filter%d", i);
4741 tok_filter = json_find_attr(buf, tokens, count, tok_format_buf);
4742
4743 if (!strcmp(tok_graph, "packets"))
4744 graph->calc_type = IOG_ITEM_UNIT_PACKETS;
4745 else if (!strcmp(tok_graph, "bytes"))
4746 graph->calc_type = IOG_ITEM_UNIT_BYTES;
4747 else if (!strcmp(tok_graph, "bits"))
4748 graph->calc_type = IOG_ITEM_UNIT_BITS;
4749 else if (g_str_has_prefix(tok_graph, "sum:")(__builtin_constant_p ("sum:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("sum:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "sum:") )
)
4750 graph->calc_type = IOG_ITEM_UNIT_CALC_SUM;
4751 else if (g_str_has_prefix(tok_graph, "frames:")(__builtin_constant_p ("frames:")? __extension__ ({ const char
* const __str = (tok_graph); const char * const __prefix = (
"frames:"); gboolean __result = (0); if (__str == ((void*)0) ||
__prefix == ((void*)0)) __result = (g_str_has_prefix) (__str
, __prefix); else { const size_t __str_len = strlen (((__str)
+ !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "frames:"
) )
)
4752 graph->calc_type = IOG_ITEM_UNIT_CALC_FRAMES;
4753 else if (g_str_has_prefix(tok_graph, "fields:")(__builtin_constant_p ("fields:")? __extension__ ({ const char
* const __str = (tok_graph); const char * const __prefix = (
"fields:"); gboolean __result = (0); if (__str == ((void*)0) ||
__prefix == ((void*)0)) __result = (g_str_has_prefix) (__str
, __prefix); else { const size_t __str_len = strlen (((__str)
+ !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "fields:"
) )
)
4754 graph->calc_type = IOG_ITEM_UNIT_CALC_FIELDS;
4755 else if (g_str_has_prefix(tok_graph, "max:")(__builtin_constant_p ("max:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("max:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "max:") )
)
4756 graph->calc_type = IOG_ITEM_UNIT_CALC_MAX;
4757 else if (g_str_has_prefix(tok_graph, "min:")(__builtin_constant_p ("min:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("min:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "min:") )
)
4758 graph->calc_type = IOG_ITEM_UNIT_CALC_MIN;
4759 else if (g_str_has_prefix(tok_graph, "avg:")(__builtin_constant_p ("avg:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("avg:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "avg:") )
)
4760 graph->calc_type = IOG_ITEM_UNIT_CALC_AVERAGE;
4761 else if (g_str_has_prefix(tok_graph, "load:")(__builtin_constant_p ("load:")? __extension__ ({ const char *
const __str = (tok_graph); const char * const __prefix = ("load:"
); gboolean __result = (0); if (__str == ((void*)0) || __prefix
== ((void*)0)) __result = (g_str_has_prefix) (__str, __prefix
); else { const size_t __str_len = strlen (((__str) + !(__str
))); const size_t __prefix_len = strlen (((__prefix) + !(__prefix
))); if (__str_len >= __prefix_len) __result = memcmp (((__str
) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len) == 0
; } __result; }) : (g_str_has_prefix) (tok_graph, "load:") )
)
4762 graph->calc_type = IOG_ITEM_UNIT_CALC_LOAD;
4763 else if (g_str_has_prefix(tok_graph, "throughput:")(__builtin_constant_p ("throughput:")? __extension__ ({ const
char * const __str = (tok_graph); const char * const __prefix
= ("throughput:"); gboolean __result = (0); if (__str == ((void
*)0) || __prefix == ((void*)0)) __result = (g_str_has_prefix)
(__str, __prefix); else { const size_t __str_len = strlen ((
(__str) + !(__str))); const size_t __prefix_len = strlen (((__prefix
) + !(__prefix))); if (__str_len >= __prefix_len) __result
= memcmp (((__str) + !(__str)), ((__prefix) + !(__prefix)), __prefix_len
) == 0; } __result; }) : (g_str_has_prefix) (tok_graph, "throughput:"
) )
)
4764 graph->calc_type = IOG_ITEM_UNIT_CALC_THROUGHPUT;
4765 else
4766 break;
4767
4768 field_name = strchr(tok_graph, ':');
4769 if (field_name)
4770 field_name = field_name + 1;
4771
4772 /* io_graph_item now supports microseconds (and this parameter
4773 * is expected to be in microseconds.) */
4774 graph->interval = interval_us;
4775
4776 graph->hf_index = -1;
4777 graph->error = check_field_unit(field_name, &graph->hf_index, graph->calc_type, "Packets");
4778
4779 graph->space_items = 0; /* TODO, can avoid realloc()s in sharkd_iograph_packet() by calculating: capture_time / interval */
4780 graph->num_items = 0;
4781 graph->items = NULL((void*)0);
4782
4783 snprintf(tok_format_buf, sizeof(tok_format_buf), "aot%d", i);
4784 tok_aot = json_find_attr(buf, tokens, count, tok_format_buf);
4785 if (tok_aot!=NULL((void*)0)) {
4786 graph->aot = (!strcmp(tok_aot, "true")) ? true1 : false0;
4787 }
4788 else {
4789 graph->aot = false0;
4790 }
4791
4792 if (!graph->error)
4793 graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE0x00000001, NULL((void*)0), sharkd_iograph_packet, NULL((void*)0), NULL((void*)0));
4794
4795 if (graph->error)
4796 {
4797 sharkd_json_error(
4798 rpcid, -6001, NULL((void*)0),
4799 "%s", graph->error->str
4800 );
4801 g_string_free(graph->error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(graph->error), ((!(0)))) : g_string_free_and_steal (graph
->error)) : (g_string_free) ((graph->error), ((!(0)))))
;
4802 goto cleanup;
4803 }
4804
4805 graph_count++;
4806 }
4807
4808 /* retap only if we have at least one ok */
4809 if (graph_count)
4810 sharkd_retap();
4811
4812 sharkd_json_result_prologue(rpcid);
4813
4814 sharkd_json_array_open("iograph");
4815 for (i = 0; i < graph_count; i++)
4816 {
4817 struct sharkd_iograph *graph = &graphs[i];
4818
4819 json_dumper_begin_object(&dumper);
4820
4821 if (graph->error)
4822 {
4823 fprintf(stderrstderr, "SNAP 6002 - we should never get to here.\n");
4824 g_string_free(graph->error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(graph->error), ((!(0)))) : g_string_free_and_steal (graph
->error)) : (g_string_free) ((graph->error), ((!(0)))))
;
4825 exit(-1);
4826 }
4827 else
4828 {
4829 int idx;
4830 int next_idx = 0;
4831
4832 sharkd_json_array_open("items");
4833 for (idx = 0; idx < graph->num_items; idx++)
4834 {
4835 double val;
4836
4837 val = get_io_graph_item(graph->items, graph->calc_type, idx, graph->hf_index, &cfile, graph->interval, graph->num_items, graph->aot);
4838
4839 /* if it's zero, don't display */
4840 if (val == 0.0)
4841 continue;
4842
4843 /* cause zeros are not printed, need to output index */
4844 if (next_idx != idx)
4845 sharkd_json_value_stringf(NULL((void*)0), "%x", idx);
4846
4847 sharkd_json_value_anyf(NULL((void*)0), "%f", val);
4848 next_idx = idx + 1;
4849 }
4850 sharkd_json_array_close();
4851 }
4852 json_dumper_end_object(&dumper);
4853
4854 }
4855 sharkd_json_array_close();
4856
4857 sharkd_json_result_epilogue();
4858
4859cleanup:
4860 for (i = 0; i < graph_count; i++)
4861 {
4862 struct sharkd_iograph *graph = &graphs[i];
4863
4864 remove_tap_listener(graph);
4865 g_free(graph->items)(__builtin_object_size ((graph->items), 0) != ((size_t) - 1
)) ? g_free_sized (graph->items, __builtin_object_size ((graph
->items), 0)) : (g_free) (graph->items)
;
4866 }
4867}
4868
4869/**
4870 * sharkd_session_process_intervals()
4871 *
4872 * Process intervals request - generate basic capture file statistics per requested interval.
4873 *
4874 * Input:
4875 * (o) interval - interval time in ms, if not specified: 1000ms
4876 * (o) filter - filter for generating interval request
4877 *
4878 * Output object with attributes:
4879 * (m) intervals - array of intervals, with indexes:
4880 * [0] - index of interval,
4881 * [1] - number of frames during interval,
4882 * [2] - number of bytes during interval.
4883 *
4884 * (m) last - last interval number.
4885 * (m) frames - total number of frames
4886 * (m) bytes - total number of bytes
4887 *
4888 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
4889 */
4890static void
4891sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
4892{
4893 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
4894 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
4895
4896 const uint8_t *filter_data = NULL((void*)0);
4897
4898 struct
4899 {
4900 unsigned int frames;
4901 uint64_t bytes;
4902 } st, st_total;
4903
4904 nstime_t *start_ts;
4905
4906 uint32_t interval_ms = 1000; /* default: one per second */
4907
4908 int64_t idx;
4909 int64_t max_idx = 0;
4910
4911 if (tok_interval)
4912 ws_strtou32(tok_interval, NULL((void*)0), &interval_ms); // already validated
4913
4914 if (tok_filter)
4915 {
4916 const struct sharkd_filter_item *filter_item;
4917
4918 filter_item = sharkd_session_filter_data(tok_filter);
4919 if (!filter_item)
4920 {
4921 sharkd_json_error(
4922 rpcid, -7001, NULL((void*)0),
4923 "Invalid filter parameter: %s", tok_filter
4924 );
4925 return;
4926 }
4927 filter_data = filter_item->filtered;
4928 }
4929
4930 st_total.frames = 0;
4931 st_total.bytes = 0;
4932
4933 st.frames = 0;
4934 st.bytes = 0;
4935
4936 idx = 0;
4937
4938 sharkd_json_result_prologue(rpcid);
4939 sharkd_json_array_open("intervals");
4940
4941 start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL((void*)0);
4942
4943 for (uint32_t framenum = 1; framenum <= cfile.count; framenum++)
4944 {
4945 frame_data *fdata;
4946 int64_t msec_rel;
4947 int64_t new_idx;
4948
4949 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
4950 continue;
4951
4952 fdata = sharkd_get_frame(framenum);
4953
4954 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (int64_t) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
4955 new_idx = msec_rel / interval_ms;
4956
4957 if (idx != new_idx)
4958 {
4959 if (st.frames != 0)
4960 {
4961 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4962 }
4963
4964 idx = new_idx;
4965 if (idx > max_idx)
4966 max_idx = idx;
4967
4968 st.frames = 0;
4969 st.bytes = 0;
4970 }
4971
4972 st.frames += 1;
4973 st.bytes += fdata->pkt_len;
4974
4975 st_total.frames += 1;
4976 st_total.bytes += fdata->pkt_len;
4977 }
4978
4979 if (st.frames != 0)
4980 {
4981 sharkd_json_value_anyf(NULL((void*)0), "[%" PRId64"l" "d" ",%u,%" PRIu64"l" "u" "]", idx, st.frames, st.bytes);
4982 }
4983 sharkd_json_array_close();
4984
4985 sharkd_json_value_anyf("last", "%" PRId64"l" "d", max_idx);
4986 sharkd_json_value_anyf("frames", "%u", st_total.frames);
4987 sharkd_json_value_anyf("bytes", "%" PRIu64"l" "u", st_total.bytes);
4988
4989 sharkd_json_result_epilogue();
4990}
4991
4992/**
4993 * sharkd_session_process_frame()
4994 *
4995 * Process frame request
4996 *
4997 * Input:
4998 * (m) frame - requested frame number
4999 * (o) ref_frame - time reference frame number
5000 * (o) prev_frame - previously displayed frame number
5001 * (o) proto - set if output frame tree
5002 * (o) columns - set if output frame columns
5003 * (o) color - set if output color-filter bg/fg
5004 * (o) bytes - set if output frame bytes
5005 * (o) hidden - set if output hidden tree fields
5006 *
5007 * Output object with attributes:
5008 * (m) err - 0 if succeed
5009 * (o) tree - array of frame nodes with attributes:
5010 * l - label
5011 * t: 'proto', 'framenum', 'url' - type of node
5012 * f - filter string
5013 * fn - field name
5014 * s - severity
5015 * e - subtree ett index
5016 * n - array of subtree nodes
5017 * h - two item array: (item start, item length)
5018 * i - two item array: (appendix start, appendix length)
5019 * p - [RESERVED] two item array: (protocol start, protocol length)
5020 * ds- data src index
5021 * url - only for t:'url', url
5022 * fnum - only for t:'framenum', frame number
5023 * g - if field is generated by Wireshark
5024 * v - if field is hidden
5025 *
5026 * (o) col - array of column data
5027 * (o) bytes - base64 of frame bytes
5028 * (o) ds - array of other data srcs
5029 * (o) comment - frame comment
5030 * (o) fol - array of follow filters:
5031 * [0] - protocol
5032 * [1] - filter string
5033 * (o) followers - array of followers with attributes:
5034 * protocol - protocol string
5035 * filter - filter string
5036 * stream - stream index number
5037 * sub_stream - sub-stream index number (optional, e.g. for HTTP/2 and QUIC streams)
5038 * (o) i - if frame is ignored
5039 * (o) m - if frame is marked
5040 * (o) bg - color filter - background color in hex
5041 * (o) fg - color filter - foreground color in hex
5042 */
5043static void
5044sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
5045{
5046 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
5047 const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
5048 const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
5049 column_info *cinfo = NULL((void*)0);
5050
5051 uint32_t framenum, ref_frame_num, prev_dis_num;
5052 uint32_t dissect_flags = SHARKD_DISSECT_FLAG_NULL0x00u;
5053 struct sharkd_frame_request_data req_data;
5054 wtap_rec rec; /* Record information */
5055 enum dissect_request_status status;
5056 int err;
5057 char *err_info;
5058
5059 ws_strtou32(tok_frame, NULL((void*)0), &framenum); // we have already validated this
5060
5061 ref_frame_num = (framenum != 1) ? 1 : 0;
5062 if (tok_ref_frame)
5063 {
5064 ws_strtou32(tok_ref_frame, NULL((void*)0), &ref_frame_num);
5065 if (ref_frame_num > framenum)
5066 {
5067 sharkd_json_error(
5068 rpcid, -8001, NULL((void*)0),
5069 "Invalid ref_frame - The ref_frame occurs after the frame specified"
5070 );
5071 return;
5072 }
5073 }
5074
5075 prev_dis_num = framenum - 1;
5076 if (tok_prev_frame)
5077 {
5078 ws_strtou32(tok_prev_frame, NULL((void*)0), &prev_dis_num);
5079 if (prev_dis_num >= framenum)
5080 {
5081 sharkd_json_error(
5082 rpcid, -8002, NULL((void*)0),
5083 "Invalid prev_frame - The prev_frame occurs on or after the frame specified"
5084 );
5085 return;
5086 }
5087 }
5088
5089 if (json_find_attr(buf, tokens, count, "proto") != NULL((void*)0))
5090 dissect_flags |= SHARKD_DISSECT_FLAG_PROTO_TREE0x04u;
5091 if (json_find_attr(buf, tokens, count, "bytes") != NULL((void*)0))
5092 dissect_flags |= SHARKD_DISSECT_FLAG_BYTES0x01u;
5093 if (json_find_attr(buf, tokens, count, "columns") != NULL((void*)0)) {
5094 dissect_flags |= SHARKD_DISSECT_FLAG_COLUMNS0x02u;
5095 cinfo = &cfile.cinfo;
5096 }
5097 if (json_find_attr(buf, tokens, count, "color") != NULL((void*)0))
5098 dissect_flags |= SHARKD_DISSECT_FLAG_COLOR0x08u;
5099
5100 req_data.display_hidden = (json_find_attr(buf, tokens, count, "v") != NULL((void*)0));
5101
5102 wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048(2 * 1024));
5103
5104 status = sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num,
5105 &rec, cinfo, dissect_flags,
5106 &sharkd_session_process_frame_cb, &req_data, &err, &err_info);
5107 switch (status) {
5108
5109 case DISSECT_REQUEST_SUCCESS:
5110 /* success */
5111 break;
5112
5113 case DISSECT_REQUEST_NO_SUCH_FRAME:
5114 sharkd_json_error(
5115 rpcid, -8003, NULL((void*)0),
5116 "Invalid frame - The frame number requested is out of range"
5117 );
5118 break;
5119
5120 case DISSECT_REQUEST_READ_ERROR:
5121 sharkd_json_error(
5122 rpcid, -8003, NULL((void*)0),
5123 /* XXX - show the error details */
5124 "Read error - The frame could not be read from the file"
5125 );
5126 g_free(err_info)(__builtin_object_size ((err_info), 0) != ((size_t) - 1)) ? g_free_sized
(err_info, __builtin_object_size ((err_info), 0)) : (g_free)
(err_info)
;
5127 break;
5128 }
5129
5130 wtap_rec_cleanup(&rec);
5131}
5132
5133/**
5134 * sharkd_session_process_check()
5135 *
5136 * Process check request.
5137 *
5138 * Input:
5139 * (o) filter - filter to be checked
5140 * (o) field - field to be checked
5141 *
5142 * Output object with attributes:
5143 * (m) err - always 0
5144 * (o) filter - 'ok', 'warn' or error message
5145 * (o) field - 'ok', or 'notfound'
5146 */
5147static int
5148sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
5149{
5150 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
5151 const char *tok_field = json_find_attr(buf, tokens, count, "field");
5152
5153 if (tok_filter != NULL((void*)0))
5154 {
5155 dfilter_t *dfp;
5156 df_error_t *df_err = NULL((void*)0);
5157
5158 if (dfilter_compile(tok_filter, &dfp, &df_err)dfilter_compile_full(tok_filter, &dfp, &df_err, (1U <<
1)|(1U << 2), __func__)
)
5159 {
5160 if (dfp && dfilter_deprecated_tokens(dfp))
5161 sharkd_json_warning(rpcid, "Filter contains deprecated tokens");
5162 else
5163 sharkd_json_simple_ok(rpcid);
5164
5165 dfilter_free(dfp);
5166 df_error_free(&df_err);
5167 return 0;
5168 }
5169 else
5170 {
5171 sharkd_json_error(
5172 rpcid, -5001, NULL((void*)0),
5173 "Filter invalid - %s", df_err->msg
5174 );
5175 df_error_free(&df_err);
5176 return -5001;
5177 }
5178 }
5179
5180 if (tok_field != NULL((void*)0))
5181 {
5182 header_field_info *hfi = proto_registrar_get_byname(tok_field);
5183
5184 if (!hfi)
5185 {
5186 sharkd_json_error(
5187 rpcid, -5002, NULL((void*)0),
5188 "Field %s not found", tok_field
5189 );
5190 return -5002;
5191 }
5192 else
5193 {
5194 sharkd_json_simple_ok(rpcid);
5195 return 0;
5196 }
5197 }
5198
5199 sharkd_json_simple_ok(rpcid);
5200 return 0;
5201}
5202
5203struct sharkd_session_process_complete_pref_data
5204{
5205 const char *module;
5206 const char *pref;
5207};
5208
5209static unsigned
5210sharkd_session_process_complete_pref_cb(module_t *module, void *d)
5211{
5212 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5213
5214 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
5215 return 0;
5216
5217 json_dumper_begin_object(&dumper);
5218 sharkd_json_value_string("f", module->name);
5219 sharkd_json_value_string("d", module->title);
5220 json_dumper_end_object(&dumper);
5221
5222 return 0;
5223}
5224
5225static unsigned
5226sharkd_session_process_complete_pref_option_cb(pref_t *pref, void *d)
5227{
5228 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
5229 const char *pref_name = prefs_get_name(pref);
5230 const char *pref_title = prefs_get_title(pref);
5231
5232 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
5233 return 0;
5234
5235 json_dumper_begin_object(&dumper);
5236 sharkd_json_value_stringf("f", "%s.%s", data->module, pref_name);
5237 sharkd_json_value_string("d", pref_title);
5238 json_dumper_end_object(&dumper);
5239
5240 return 0; /* continue */
5241}
5242
5243/**
5244 * sharkd_session_process_complete()
5245 *
5246 * Process complete request
5247 *
5248 * Input:
5249 * (o) field - field to be completed
5250 * (o) pref - preference to be completed
5251 *
5252 * Output object with attributes:
5253 * (m) err - always 0
5254 * (o) field - array of object with attributes:
5255 * (m) f - field text
5256 * (o) t - field type (FT_ number)
5257 * (o) n - field name
5258 * (o) pref - array of object with attributes:
5259 * (m) f - pref name
5260 * (o) d - pref description
5261 */
5262static int
5263sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
5264{
5265 const char *tok_field = json_find_attr(buf, tokens, count, "field");
5266 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5267
5268 sharkd_json_result_prologue(rpcid);
5269
5270 if (tok_field != NULL((void*)0) && tok_field[0])
5271 {
5272 const size_t filter_length = strlen(tok_field);
5273 const int filter_with_dot = !!strchr(tok_field, '.');
5274
5275 void *proto_cookie;
5276 void *field_cookie;
5277 int proto_id;
5278
5279 sharkd_json_array_open("field");
5280
5281 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
5282 {
5283 protocol_t *protocol = find_protocol_by_id(proto_id);
5284 const char *protocol_filter;
5285 const char *protocol_name;
5286 header_field_info *hfinfo;
5287
5288 if (!proto_is_protocol_enabled(protocol))
5289 continue;
5290
5291 protocol_name = proto_get_protocol_long_name(protocol);
5292 protocol_filter = proto_get_protocol_filter_name(proto_id);
5293
5294 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
5295 {
5296 json_dumper_begin_object(&dumper);
5297 {
5298 sharkd_json_value_string("f", protocol_filter);
5299 sharkd_json_value_anyf("t", "%d", FT_PROTOCOL);
5300 sharkd_json_value_string("n", protocol_name);
5301 }
5302 json_dumper_end_object(&dumper);
5303 }
5304
5305 if (!filter_with_dot)
5306 continue;
5307
5308 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL((void*)0); hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
5309 {
5310 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
5311 continue;
5312
5313 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
5314 {
5315 json_dumper_begin_object(&dumper);
5316 {
5317 sharkd_json_value_string("f", hfinfo->abbrev);
5318
5319 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
5320 if (hfinfo->same_name_next == NULL((void*)0))
5321 {
5322 sharkd_json_value_anyf("t", "%d", hfinfo->type);
5323 sharkd_json_value_string("n", hfinfo->name);
5324 }
5325 }
5326 json_dumper_end_object(&dumper);
5327 }
5328 }
5329 }
5330
5331 sharkd_json_array_close();
5332 }
5333
5334 if (tok_pref != NULL((void*)0) && tok_pref[0])
5335 {
5336 struct sharkd_session_process_complete_pref_data data;
5337 char *dot_sepa;
5338
5339 data.module = tok_pref;
5340 data.pref = tok_pref;
5341
5342 sharkd_json_array_open("pref");
5343 if ((dot_sepa = strchr(tok_pref, '.')))
5344 {
5345 module_t *pref_mod;
5346
5347 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5348 data.pref = dot_sepa + 1;
5349
5350 pref_mod = prefs_find_module(data.module);
5351 if (pref_mod)
5352 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
5353
5354 *dot_sepa = '.';
5355 }
5356 else
5357 {
5358 prefs_modules_foreach(prefs_get_module_tree(), sharkd_session_process_complete_pref_cb, &data);
5359 }
5360 sharkd_json_array_close();
5361 }
5362
5363 sharkd_json_result_epilogue();
5364
5365 return 0;
5366}
5367
5368/**
5369 * sharkd_session_process_setcomment()
5370 *
5371 * Process setcomment request
5372 *
5373 * Input:
5374 * (m) frame - frame number
5375 * (m) comment - user comment
5376 *
5377 * Output object with attributes:
5378 * (m) err - error code: 0 succeed
5379 *
5380 * Note:
5381 * For now, adds comments, doesn't remove or replace them.
5382 */
5383static void
5384sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count)
5385{
5386 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
5387 const char *tok_comment = json_find_attr(buf, tokens, count, "comment");
5388
5389 uint32_t framenum;
5390 frame_data *fdata;
5391 wtap_opttype_return_val ret;
5392 wtap_block_t pkt_block = NULL((void*)0);
5393
5394 if (!tok_frame || !ws_strtou32(tok_frame, NULL((void*)0), &framenum) || framenum == 0)
5395 {
5396 sharkd_json_error(
5397 rpcid, -3001, NULL((void*)0),
5398 "Frame number must be a positive integer"
5399 );
5400 return;
5401 }
5402
5403 fdata = sharkd_get_frame(framenum); // BUG HERE - If no file loaded you get a crash
5404 if (!fdata)
5405 {
5406 sharkd_json_error(
5407 rpcid, -3002, NULL((void*)0),
5408 "Frame number is out of range"
5409 );
5410 return;
5411 }
5412
5413 pkt_block = sharkd_get_packet_block(fdata);
5414
5415 ret = wtap_block_add_string_option(pkt_block, OPT_COMMENT1, tok_comment, strlen(tok_comment));
5416
5417 if (ret != WTAP_OPTTYPE_SUCCESS)
5418 {
5419 sharkd_json_error(
5420 rpcid, -3003, NULL((void*)0),
5421 "Unable to set the comment"
5422 );
5423 }
5424 else
5425 {
5426 sharkd_set_modified_block(fdata, pkt_block);
5427 sharkd_json_simple_ok(rpcid);
5428 }
5429}
5430
5431/**
5432 * sharkd_session_process_setconf()
5433 *
5434 * Process setconf request
5435 *
5436 * Input:
5437 * (m) name - preference name
5438 * (m) value - preference value
5439 *
5440 * Output object with attributes:
5441 * (m) err - error code: 0 succeed
5442 */
5443static void
5444sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
5445{
5446 const char *tok_name = json_find_attr(buf, tokens, count, "name");
5447 const char *tok_value = json_find_attr(buf, tokens, count, "value");
5448 char pref[4096];
5449 char *errmsg = NULL((void*)0);
5450
5451 prefs_set_pref_e ret;
5452
5453 if (!tok_name || tok_name[0] == '\0')
5454 {
5455 sharkd_json_error(
5456 rpcid, -4001, NULL((void*)0),
5457 "Preference name missing"
5458 );
5459 return;
5460 }
5461
5462 if (!tok_value)
5463 {
5464 sharkd_json_error(
5465 rpcid, -4002, NULL((void*)0),
5466 "Preference value missing"
5467 );
5468 return;
5469 }
5470
5471 snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
5472
5473 ret = prefs_set_pref(pref, &errmsg);
5474
5475 switch (ret)
5476 {
5477 case PREFS_SET_OK:
5478 sharkd_json_simple_ok(rpcid);
5479 break;
5480
5481 case PREFS_SET_OBSOLETE:
5482 sharkd_json_error(
5483 rpcid, -4003, NULL((void*)0),
5484 "The preference specified is obsolete"
5485 );
5486 break;
5487
5488 case PREFS_SET_NO_SUCH_PREF:
5489 sharkd_json_error(
5490 rpcid, -4004, NULL((void*)0),
5491 "No such preference exists"
5492 );
5493 break;
5494
5495 default:
5496 sharkd_json_error(
5497 rpcid, -4005, NULL((void*)0),
5498 "Unable to set the preference%s%s",
5499 errmsg ? ": " : "", errmsg ? errmsg : ""
5500 );
5501 }
5502
5503 g_free(errmsg)(__builtin_object_size ((errmsg), 0) != ((size_t) - 1)) ? g_free_sized
(errmsg, __builtin_object_size ((errmsg), 0)) : (g_free) (errmsg
)
;
5504}
5505
5506struct sharkd_session_process_dumpconf_data
5507{
5508 module_t *module;
5509};
5510
5511static unsigned
5512sharkd_session_process_dumpconf_cb(pref_t *pref, void *d)
5513{
5514 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5515 const char *pref_name = prefs_get_name(pref);
5516
5517 char json_pref_key[512];
5518
5519 snprintf(json_pref_key, sizeof(json_pref_key), "%s.%s", data->module->name, pref_name);
5520 sharkd_json_object_open(json_pref_key);
5521
5522 if (!prefs_is_preference_obsolete(pref)) {
5523 switch (prefs_get_type(pref))
5524 {
5525 case PREF_UINT:
5526 sharkd_json_value_anyf("u", "%u", prefs_get_uint_value(pref, pref_current));
5527 if (prefs_get_uint_base(pref) != 10)
5528 sharkd_json_value_anyf("ub", "%u", prefs_get_uint_base(pref));
5529 break;
5530
5531 case PREF_INT:
5532 sharkd_json_value_anyf("d", "%d", prefs_get_int_value(pref, pref_current));
5533 break;
5534
5535 case PREF_FLOAT:
5536 sharkd_json_value_anyf("f", "%.*f", prefs_get_uint_base(pref), prefs_get_float_value(pref, pref_current));
5537 break;
5538
5539 case PREF_BOOL:
5540 sharkd_json_value_anyf("b", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
5541 break;
5542
5543 case PREF_STRING:
5544 case PREF_SAVE_FILENAME:
5545 case PREF_OPEN_FILENAME:
5546 case PREF_DIRNAME:
5547 case PREF_PASSWORD:
5548 case PREF_DISSECTOR:
5549 sharkd_json_value_string("s", prefs_get_string_value(pref, pref_current));
5550 break;
5551
5552 case PREF_ENUM:
5553 {
5554 const enum_val_t *enums;
5555
5556 sharkd_json_array_open("e");
5557 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
5558 {
5559 json_dumper_begin_object(&dumper);
5560
5561 sharkd_json_value_anyf("v", "%d", enums->value);
5562
5563 if (enums->value == prefs_get_enum_value(pref, pref_current))
5564 sharkd_json_value_anyf("s", "1");
5565
5566 sharkd_json_value_string("d", enums->description);
5567
5568 json_dumper_end_object(&dumper);
5569 }
5570 sharkd_json_array_close();
5571 break;
5572 }
5573
5574 case PREF_RANGE:
5575 case PREF_DECODE_AS_RANGE:
5576 {
5577 char *range_str = range_convert_range(NULL((void*)0), prefs_get_range_value_real(pref, pref_current));
5578 sharkd_json_value_string("r", range_str);
5579 wmem_free(NULL((void*)0), range_str);
5580 break;
5581 }
5582
5583 case PREF_UAT:
5584 {
5585 uat_t *uat = prefs_get_uat_value(pref);
5586 unsigned idx;
5587
5588 sharkd_json_array_open("t");
5589 for (idx = 0; idx < uat->raw_data->len; idx++)
5590 {
5591 void *rec = UAT_INDEX_PTR(uat, idx)(uat->raw_data->data + (uat->record_size * (idx)));
5592 unsigned colnum;
5593
5594 sharkd_json_array_open(NULL((void*)0));
5595 for (colnum = 0; colnum < uat->ncols; colnum++)
5596 {
5597 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
5598
5599 sharkd_json_value_string(NULL((void*)0), str);
5600 g_free(str)(__builtin_object_size ((str), 0) != ((size_t) - 1)) ? g_free_sized
(str, __builtin_object_size ((str), 0)) : (g_free) (str)
;
5601 }
5602
5603 sharkd_json_array_close();
5604 }
5605
5606 sharkd_json_array_close();
5607 break;
5608 }
5609
5610 case PREF_COLOR:
5611 case PREF_CUSTOM:
5612 case PREF_STATIC_TEXT:
5613 /* TODO */
5614 break;
5615 }
5616 }
5617
5618#if 0
5619 sharkd_json_value_string("t", prefs_get_title(pref));
5620#endif
5621
5622 sharkd_json_object_close();
5623
5624 return 0; /* continue */
5625}
5626
5627static unsigned
5628sharkd_session_process_dumpconf_mod_cb(module_t *module, void *d)
5629{
5630 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
5631
5632 data->module = module;
5633 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
5634
5635 return 0;
5636}
5637
5638/**
5639 * sharkd_session_process_dumpconf()
5640 *
5641 * Process dumpconf request
5642 *
5643 * Input:
5644 * (o) pref - module, or preference, NULL for all
5645 *
5646 * Output object with attributes:
5647 * (o) prefs - object with module preferences
5648 * (m) [KEY] - preference name
5649 * (o) u - preference value (for PREF_UINT)
5650 * (o) ub - preference value suggested base for display (for PREF_UINT) and if different than 10
5651 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
5652 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME, PREF_PASSWORD, PREF_DISSECTOR)
5653 * (o) e - preference possible values (only for PREF_ENUM)
5654 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
5655 * (o) t - preference value (only for PREF_UAT)
5656 */
5657static void
5658sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
5659{
5660 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
5661 module_t *pref_mod;
5662 char *dot_sepa;
5663
5664 if (!tok_pref)
5665 {
5666 struct sharkd_session_process_dumpconf_data data;
5667
5668 data.module = NULL((void*)0);
5669
5670 sharkd_json_result_prologue(rpcid);
5671
5672 sharkd_json_object_open("prefs");
5673 prefs_modules_foreach(prefs_get_module_tree(), sharkd_session_process_dumpconf_mod_cb, &data);
5674 sharkd_json_object_close();
5675
5676 sharkd_json_result_epilogue();
5677 return;
5678 }
5679
5680 if ((dot_sepa = strchr(tok_pref, '.')))
5681 {
5682 pref_t *pref = NULL((void*)0);
5683
5684 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
5685 pref_mod = prefs_find_module(tok_pref);
5686 if (pref_mod)
5687 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
5688 *dot_sepa = '.';
5689
5690 if (pref)
5691 {
5692 struct sharkd_session_process_dumpconf_data data;
5693
5694 data.module = pref_mod;
5695
5696 sharkd_json_result_prologue(rpcid);
5697
5698 sharkd_json_object_open("prefs");
5699 sharkd_session_process_dumpconf_cb(pref, &data);
5700 sharkd_json_object_close();
5701
5702 sharkd_json_result_epilogue();
5703 return;
5704 }
5705 else
5706 {
5707 sharkd_json_error(
5708 rpcid, -9001, NULL((void*)0),
5709 "Invalid pref %s.", tok_pref
5710 );
5711 return;
5712 }
5713
5714 }
5715
5716 pref_mod = prefs_find_module(tok_pref);
5717 if (pref_mod)
5718 {
5719 struct sharkd_session_process_dumpconf_data data;
5720
5721 data.module = pref_mod;
5722
5723 sharkd_json_result_prologue(rpcid);
5724
5725 sharkd_json_object_open("prefs");
5726 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
5727 sharkd_json_object_close();
5728
5729 sharkd_json_result_epilogue();
5730 }
5731 else
5732 {
5733 sharkd_json_error(
5734 rpcid, -9002, NULL((void*)0),
5735 "Invalid pref %s.", tok_pref
5736 );
5737 }
5738}
5739
5740struct sharkd_download_rtp
5741{
5742 rtpstream_id_t id;
5743 GSList *packets;
5744 double start_time;
5745};
5746
5747static void
5748sharkd_rtp_download_free_items(void *ptr)
5749{
5750 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
5751
5752 g_free(rtp_packet->info)(__builtin_object_size ((rtp_packet->info), 0) != ((size_t
) - 1)) ? g_free_sized (rtp_packet->info, __builtin_object_size
((rtp_packet->info), 0)) : (g_free) (rtp_packet->info)
;
5753 g_free(rtp_packet->payload_data)(__builtin_object_size ((rtp_packet->payload_data), 0) != (
(size_t) - 1)) ? g_free_sized (rtp_packet->payload_data, __builtin_object_size
((rtp_packet->payload_data), 0)) : (g_free) (rtp_packet->
payload_data)
;
5754 g_free(rtp_packet)(__builtin_object_size ((rtp_packet), 0) != ((size_t) - 1)) ?
g_free_sized (rtp_packet, __builtin_object_size ((rtp_packet
), 0)) : (g_free) (rtp_packet)
;
5755}
5756
5757/* WAV files are little-endian; the decoders produce samples in host byte order. */
5758static void
5759sharkd_rtp_samples_to_le(SAMPLE *buf, size_t nbytes)
5760{
5761#if G_BYTE_ORDER1234 == G_BIG_ENDIAN4321
5762 for (size_t i = 0; i < nbytes / SAMPLE_BYTES(sizeof(SAMPLE) / sizeof(char)); i++)
5763 {
5764 phtoleu16((uint8_t *)&buf[i], (uint16_t)buf[i]);
5765 }
5766#else
5767 (void)buf;
5768 (void)nbytes;
5769#endif
5770}
5771
5772static void
5773sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
5774{
5775 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
5776 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
5777
5778 static const int sample_bytes_ = SAMPLE_BYTES(sizeof(SAMPLE) / sizeof(char));
5779
5780 uint32_t audio_out_rate_ = 0;
5781 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
5782 struct SpeexResamplerState_ *audio_resampler_ = NULL((void*)0);
5783
5784 size_t resample_buff_len = 0x1000;
5785 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
5786 spx_uint32_t cur_in_rate = 0;
5787 char *write_buff = NULL((void*)0);
5788 size_t write_bytes = 0;
5789 unsigned channels = 0;
5790 unsigned sample_rate = 0;
5791
5792 GSList *l;
5793
5794 for (l = req->packets; l; l = l->next)
5795 {
5796 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
5797
5798 SAMPLE *decode_buff = NULL((void*)0);
5799 size_t decoded_bytes;
5800
5801 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
5802 if (decoded_bytes == 0 || sample_rate == 0)
5803 {
5804 /* We didn't decode anything. Clean up and prep for the next packet. */
5805 g_free(decode_buff)(__builtin_object_size ((decode_buff), 0) != ((size_t) - 1)) ?
g_free_sized (decode_buff, __builtin_object_size ((decode_buff
), 0)) : (g_free) (decode_buff)
;
5806 continue;
5807 }
5808
5809 if (audio_out_rate_ == 0)
5810 {
5811 uint8_t wav_hdr[44];
5812
5813 /* First non-zero wins */
5814 audio_out_rate_ = sample_rate;
5815
5816 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
5817
5818 /* write WAVE header */
5819 memset(&wav_hdr, 0, sizeof(wav_hdr));
5820 memcpy(&wav_hdr[0], "RIFF", 4);
5821 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5822 memcpy(&wav_hdr[8], "WAVE", 4);
5823
5824 memcpy(&wav_hdr[12], "fmt ", 4);
5825 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
5826 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
5827 /* # channels */
5828 phtoleu16(&wav_hdr[22], channels);
5829 /* sample rate */
5830 phtoleu32(&wav_hdr[24], sample_rate);
5831 /* byte rate */
5832 phtoleu32(&wav_hdr[28], sample_rate * channels * sample_bytes_);
5833 /* block align */
5834 phtoleu16(&wav_hdr[32], channels * sample_bytes_);
5835 /* bits per sample */
5836 phtoleu16(&wav_hdr[34], 8 * sample_bytes_);
5837
5838 memcpy(&wav_hdr[36], "data", 4);
5839 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
5840
5841 json_dumper_write_base64(&dumper, wav_hdr, sizeof(wav_hdr));
5842 }
5843
5844 // Write samples to our file.
5845 write_buff = (char *) decode_buff;
5846 write_bytes = decoded_bytes;
5847
5848 if (audio_out_rate_ != sample_rate)
5849 {
5850 spx_uint32_t in_len, out_len;
5851
5852 /* Resample the audio to match our previous output rate. */
5853 if (!audio_resampler_)
5854 {
5855 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL((void*)0));
5856 speex_resampler_skip_zeros(audio_resampler_);
5857 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
5858 }
5859 else
5860 {
5861 spx_uint32_t audio_out_rate;
5862 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
5863
5864 if (sample_rate != cur_in_rate)
5865 {
5866 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
5867 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
5868 }
5869 }
5870 in_len = (spx_uint32_t)(decoded_bytes / sample_bytes_);
5871 out_len = (spx_uint32_t)(((uint64_t)in_len * audio_out_rate_ / sample_rate) + (audio_out_rate_ % sample_rate != 0));
5872 if (out_len * sample_bytes_ > resample_buff_len)
5873 {
5874 while ((out_len * sample_bytes_ > resample_buff_len))
5875 resample_buff_len *= 2;
5876 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
5877 }
5878
5879 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
5880 write_buff = (char *) resample_buff;
5881 write_bytes = out_len * sample_bytes_;
5882 }
5883
5884 /* Write the decoded, possibly-resampled audio */
5885 sharkd_rtp_samples_to_le((SAMPLE *)write_buff, write_bytes);
5886 json_dumper_write_base64(&dumper, (const uint8_t*)write_buff, write_bytes);
5887
5888 g_free(decode_buff)(__builtin_object_size ((decode_buff), 0) != ((size_t) - 1)) ?
g_free_sized (decode_buff, __builtin_object_size ((decode_buff
), 0)) : (g_free) (decode_buff)
;
5889 }
5890
5891 g_free(resample_buff)(__builtin_object_size ((resample_buff), 0) != ((size_t) - 1)
) ? g_free_sized (resample_buff, __builtin_object_size ((resample_buff
), 0)) : (g_free) (resample_buff)
;
5892 if (audio_resampler_)
5893 speex_resampler_destroy(audio_resampler_);
5894 g_hash_table_destroy(decoders_hash_);
5895}
5896
5897static tap_packet_status
5898sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U___attribute__((unused)), const void *data, tap_flags_t flags _U___attribute__((unused)))
5899{
5900 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
5901 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
5902
5903 /* do not consider RTP packets without a setup frame */
5904 if (rtp_info->info_setup_frame_num == 0)
5905 return TAP_PACKET_DONT_REDRAW;
5906
5907 if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info))
5908 {
5909 rtp_packet_t *rtp_packet;
5910
5911 rtp_packet = g_new0(rtp_packet_t, 1)((rtp_packet_t *) g_malloc0_n ((1), sizeof (rtp_packet_t)));
5912 rtp_packet->info = (struct _rtp_info *) g_memdup2(rtp_info, sizeof(struct _rtp_info));
5913
5914 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
5915 rtp_packet->payload_data = (uint8_t *) g_memdup2(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
5916
5917 if (!req_rtp->packets)
5918 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
5919
5920 rtp_packet->frame_num = pinfo->num;
5921 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
5922
5923 /* XXX, O(n) optimize */
5924 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
5925 }
5926
5927 return TAP_PACKET_DONT_REDRAW;
5928}
5929
5930static bool_Bool
5931sharkd_session_eo_retap_listener(const char *tap_type) {
5932 bool_Bool ok = true1;
5933 register_eo_t *eo = NULL((void*)0);
5934 GString *tap_error = NULL((void*)0);
5935 void *tap_data = NULL((void*)0);
5936 GFreeFunc tap_free = NULL((void*)0);
5937
5938 // get <name> from eo:<name>, get_eo_by_name only needs the name (http etc.)
5939 eo = get_eo_by_name(tap_type + 3);
5940 if (!eo)
5941 {
5942 ok = false0;
5943 sharkd_json_error(
5944 rpcid, -11011, NULL((void*)0),
5945 "sharkd_session_eo_retap_listener() eo=%s not found", tap_type + 3
5946 );
5947 }
5948
5949 if (ok)
5950 {
5951 tap_error = sharkd_session_eo_register_tap_listener(eo, tap_type, NULL((void*)0), NULL((void*)0), &tap_data, &tap_free);
5952 if (tap_error)
5953 {
5954 ok = false0;
5955 sharkd_json_error(
5956 rpcid, -10002, NULL((void*)0),
5957 "sharkd_session_eo_retap_listener() sharkd_session_eo_register_tap_listener error %s",
5958 tap_error->str);
5959 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
5960 }
5961 }
5962
5963 if (ok)
5964 sharkd_retap();
5965
5966 if (!tap_error)
5967 remove_tap_listener(tap_data);
5968
5969 if (tap_free)
5970 tap_free(tap_data);
5971
5972 return ok;
5973}
5974
5975/**
5976 * sharkd_session_process_download()
5977 *
5978 * Process download request
5979 *
5980 * Input:
5981 * (m) token - token to download
5982 *
5983 * Output object with attributes:
5984 * (o) file - suggested name of file
5985 * (o) mime - suggested content type
5986 * (o) data - payload base64 encoded
5987 */
5988static void
5989sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
5990{
5991 const char *tok_token = json_find_attr(buf, tokens, count, "token");
5992
5993 if (!tok_token)
5994 {
5995 sharkd_json_error(
5996 rpcid, -10005, NULL((void*)0),
5997 "missing token"
5998 );
5999 return;
6000 }
6001
6002 if (!strncmp(tok_token, "eo:", 3))
6003 {
6004 // get eo:<name> from eo:<name>_<row>
6005 char *tap_type = g_strdup(tok_token)g_strdup_inline (tok_token);
6006 char *tmp = strrchr(tap_type, '_');
6007 if (tmp)
6008 *tmp = '\0';
6009
6010 // if eo:<name> not in sharkd_eo_list, retap
6011 if (!sharkd_eo_object_list_get_entry_by_type(sharkd_eo_list, tap_type) &&
6012 !sharkd_session_eo_retap_listener(tap_type))
6013 {
6014 g_free(tap_type)(__builtin_object_size ((tap_type), 0) != ((size_t) - 1)) ? g_free_sized
(tap_type, __builtin_object_size ((tap_type), 0)) : (g_free)
(tap_type)
;
6015 // sharkd_json_error called in sharkd_session_eo_retap_listener
6016 return;
6017 }
6018
6019 g_free(tap_type)(__builtin_object_size ((tap_type), 0) != ((size_t) - 1)) ? g_free_sized
(tap_type, __builtin_object_size ((tap_type), 0)) : (g_free)
(tap_type)
;
6020
6021 struct sharkd_export_object_list *object_list;
6022 const export_object_entry_t *eo_entry = NULL((void*)0);
6023
6024 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
6025 {
6026 size_t eo_type_len = strlen(object_list->type);
6027
6028 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
6029 {
6030 int row;
6031
6032 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
6033 break;
6034
6035 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
6036 break;
6037 }
6038 }
6039
6040 if (eo_entry)
6041 {
6042 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
6043 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
6044
6045 sharkd_json_result_prologue(rpcid);
6046 sharkd_json_value_string("file", filename);
6047 sharkd_json_value_string("mime", mime);
6048 sharkd_json_value_base64("data", eo_entry->payload_data, eo_entry->payload_len);
6049 sharkd_json_result_epilogue();
6050 }
6051 else
6052 {
6053 sharkd_json_result_prologue(rpcid);
6054 sharkd_json_result_epilogue();
6055 }
6056 }
6057 else if (!strcmp(tok_token, "ssl-secrets"))
6058 {
6059 size_t str_len = 0;
6060 unsigned num_keys = 0;
6061 char* str = NULL((void*)0);
6062 secrets_export_values ret = secrets_export("TLS", &str, &str_len, &num_keys);
6063
6064 if ((ret == SECRETS_EXPORT_SUCCESS) && (str_len > 0))
6065 {
6066 const char *mime = "text/plain";
6067 const char *filename = "keylog.txt";
6068
6069 sharkd_json_result_prologue(rpcid);
6070 sharkd_json_value_string("file", filename);
6071 sharkd_json_value_string("mime", mime);
6072 sharkd_json_value_base64("data", (const uint8_t*)str, str_len);
6073 sharkd_json_result_epilogue();
6074 }
6075 g_free(str)(__builtin_object_size ((str), 0) != ((size_t) - 1)) ? g_free_sized
(str, __builtin_object_size ((str), 0)) : (g_free) (str)
;
6076 }
6077 else if (!strncmp(tok_token, "rtp:", 4))
6078 {
6079 struct sharkd_download_rtp rtp_req;
6080 GString *tap_error;
6081
6082 memset(&rtp_req, 0, sizeof(rtp_req));
6083 if (!sharkd_rtp_match_init(&rtp_req.id, tok_token + 4))
6084 {
6085 sharkd_json_error(
6086 rpcid, -10001, NULL((void*)0),
6087 "sharkd_session_process_download() rtp tokenizing error %s", tok_token
6088 );
6089 return;
6090 }
6091
6092 tap_error = register_tap_listener("rtp", &rtp_req, NULL((void*)0), 0, NULL((void*)0), sharkd_session_packet_download_tap_rtp_cb, NULL((void*)0), NULL((void*)0));
6093 if (tap_error)
6094 {
6095 sharkd_json_error(
6096 rpcid, -10002, NULL((void*)0),
6097 "sharkd_session_process_download() rtp error %s", tap_error->str
6098 );
6099 g_string_free(tap_error, TRUE)(__builtin_constant_p ((!(0))) ? (((!(0))) ? (g_string_free) (
(tap_error), ((!(0)))) : g_string_free_and_steal (tap_error))
: (g_string_free) ((tap_error), ((!(0)))))
;
6100 rtpstream_id_free(&rtp_req.id);
6101 return;
6102 }
6103
6104 sharkd_retap();
6105 remove_tap_listener(&rtp_req);
6106 rtpstream_id_free(&rtp_req.id);
6107
6108 if (rtp_req.packets)
6109 {
6110 const char *mime = "audio/x-wav";
6111 const char *filename = tok_token;
6112
6113 sharkd_json_result_prologue(rpcid);
6114 sharkd_json_value_string("file", filename);
6115 sharkd_json_value_string("mime", mime);
6116
6117 json_dumper_set_member_name(&dumper, "data");
6118 json_dumper_begin_base64(&dumper);
6119 sharkd_rtp_download_decode(&rtp_req);
6120 json_dumper_end_base64(&dumper);
6121
6122 sharkd_json_result_epilogue();
6123
6124 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
6125 }
6126 else
6127 {
6128 sharkd_json_error(
6129 rpcid, -10003, NULL((void*)0),
6130 "no rtp data available"
6131 );
6132 }
6133 }
6134 else
6135 {
6136 sharkd_json_error(
6137 rpcid, -10004, NULL((void*)0),
6138 "unrecognized token"
6139 );
6140 }
6141}
6142
6143static void
6144sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
6145{
6146 if (json_prep(buf, tokens, count))
6147 {
6148 /* don't need [0] token */
6149 tokens++;
6150 count--;
6151
6152 const char* tok_method = json_find_attr(buf, tokens, count, "method");
6153
6154 if (!tok_method) {
6155 sharkd_json_error(
6156 rpcid, -32601, NULL((void*)0),
6157 "No method found");
6158 return;
6159 }
6160 if (!strcmp(tok_method, "load"))
6161 sharkd_session_process_load(buf, tokens, count);
6162 else if (!strcmp(tok_method, "status"))
6163 sharkd_session_process_status();
6164 else if (!strcmp(tok_method, "analyse"))
6165 sharkd_session_process_analyse();
6166 else if (!strcmp(tok_method, "info"))
6167 sharkd_session_process_info();
6168 else if (!strcmp(tok_method, "fields"))
6169 sharkd_session_process_fields();
6170 else if (!strcmp(tok_method, "field"))
6171 sharkd_session_process_field(buf, tokens, count);
6172 else if (!strcmp(tok_method, "check"))
6173 sharkd_session_process_check(buf, tokens, count);
6174 else if (!strcmp(tok_method, "complete"))
6175 sharkd_session_process_complete(buf, tokens, count);
6176 else if (!strcmp(tok_method, "frames"))
6177 sharkd_session_process_frames(buf, tokens, count);
6178 else if (!strcmp(tok_method, "tap"))
6179 sharkd_session_process_tap(buf, tokens, count);
6180 else if (!strcmp(tok_method, "follow"))
6181 sharkd_session_process_follow(buf, tokens, count);
6182 else if (!strcmp(tok_method, "iograph"))
6183 sharkd_session_process_iograph(buf, tokens, count);
6184 else if (!strcmp(tok_method, "intervals"))
6185 sharkd_session_process_intervals(buf, tokens, count);
6186 else if (!strcmp(tok_method, "frame"))
6187 sharkd_session_process_frame(buf, tokens, count);
6188 else if (!strcmp(tok_method, "setcomment"))
6189 sharkd_session_process_setcomment(buf, tokens, count);
6190 else if (!strcmp(tok_method, "setconf"))
6191 sharkd_session_process_setconf(buf, tokens, count);
6192 else if (!strcmp(tok_method, "dumpconf"))
6193 sharkd_session_process_dumpconf(buf, tokens, count);
6194 else if (!strcmp(tok_method, "download"))
6195 sharkd_session_process_download(buf, tokens, count);
6196 else if (!strcmp(tok_method, "bye"))
6197 {
6198 sharkd_json_simple_ok(rpcid);
6199 exit(0);
6200 }
6201 else
6202 {
6203 sharkd_json_error(
6204 rpcid, -32601, NULL((void*)0),
6205 "The method \"%s\" is unknown", tok_method
6206 );
6207 }
6208 }
6209}
6210
6211int
6212sharkd_session_main(int mode_setting)
6213{
6214 char buf[8 * 1024];
6215 jsmntok_t *tokens = NULL((void*)0);
6216 int tokens_max = -1;
6217
6218 mode = mode_setting;
6219
6220 fprintf(stderrstderr, "Hello in child.\n");
6221
6222 dumper.output_file = stdoutstdout;
6223
6224 /* XXX - This could be a wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(),...) */
6225 filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free);
6226
6227#ifdef HAVE_MAXMINDDB1
6228 /* mmdbresolve was stopped before fork(), force starting it */
6229 uat_get_table_by_name("MaxMind Database Paths")->post_update_cb();
6230#endif
6231
6232 while (fgets(buf, sizeof(buf), stdinstdin))
6233 {
6234 /* every command is line separated JSON */
6235 int ret;
6236
6237 ret = json_parse(buf, NULL((void*)0), 0);
6238 if (ret <= 0)
6239 {
6240 sharkd_json_error(
6241 rpcid, -32600, NULL((void*)0),
6242 "Invalid JSON(1)"
6243 );
6244 continue;
6245 }
6246
6247 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
6248 ret += 1;
6249
6250 if (tokens == NULL((void*)0) || tokens_max < ret)
6251 {
6252 tokens_max = ret;
6253 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
6254 }
6255
6256 memset(tokens, 0, ret * sizeof(jsmntok_t));
6257
6258 ret = json_parse(buf, tokens, ret);
6259 if (ret <= 0)
6260 {
6261 sharkd_json_error(
6262 rpcid, -32600, NULL((void*)0),
6263 "Invalid JSON(2)"
6264 );
6265 continue;
6266 }
6267
6268 host_name_lookup_process();
6269
6270 sharkd_session_process(buf, tokens, ret);
6271 }
6272
6273 g_hash_table_destroy(filter_table);
6274 g_free(tokens)(__builtin_object_size ((tokens), 0) != ((size_t) - 1)) ? g_free_sized
(tokens, __builtin_object_size ((tokens), 0)) : (g_free) (tokens
)
;
6275
6276 return 0;
6277}

←

/builds/wireshark/wireshark/ui/io_graph_item.h

1/** @file
2 *
3 * Definitions and functions for I/O graph items
4 *
5 * Copied from gtk/io_stat.c, (c) 2002 Ronnie Sahlberg
6 *
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
10 *
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 */
13
14#ifndef __IO_GRAPH_ITEM_H__
15#define __IO_GRAPH_ITEM_H__
16
17#include <epan/cfile.h>
18#include <wsutil/ws_assert.h>
19
20#include <epan/epan_dissect.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
25
26/**
27 * @brief Selects the Y-axis value unit or aggregate calculation mode for an I/O graph plot.
28 */
29typedef enum {
30 IOG_ITEM_UNIT_FIRST, /**< Sentinel: first valid unit value */
31 IOG_ITEM_UNIT_PACKETS = IOG_ITEM_UNIT_FIRST, /**< Plot packet count per interval */
32 IOG_ITEM_UNIT_BYTES, /**< Plot total byte count per interval */
33 IOG_ITEM_UNIT_BITS, /**< Plot total bit count per interval */
34 IOG_ITEM_UNIT_CALC_SUM, /**< Plot sum of a field's values per interval */
35 IOG_ITEM_UNIT_CALC_FRAMES, /**< Plot frame count matching the display filter per interval */
36 IOG_ITEM_UNIT_CALC_FIELDS, /**< Plot count of field occurrences per interval */
37 IOG_ITEM_UNIT_CALC_MAX, /**< Plot maximum field value per interval */
38 IOG_ITEM_UNIT_CALC_MIN, /**< Plot minimum field value per interval */
39 IOG_ITEM_UNIT_CALC_AVERAGE, /**< Plot average field value per interval */
40 IOG_ITEM_UNIT_CALC_THROUGHPUT, /**< Plot throughput (bits or bytes per second) per interval */
41 IOG_ITEM_UNIT_CALC_LOAD, /**< Plot load average (e.g. for response-time fields) per interval */
42 IOG_ITEM_UNIT_LAST = IOG_ITEM_UNIT_CALC_LOAD, /**< Sentinel: last valid unit value */
43 NUM_IOG_ITEM_UNITS /**< Sentinel: total number of unit values */
44} io_graph_item_unit_t;
45
46
47/**
48 * @brief Accumulated statistics for all frames falling within a single I/O graph time interval.
49 *
50 * @note @p frames and @p bytes are always computed regardless of the active
51 * ::io_graph_item_unit_t. The min/max union members use 64-bit integers
52 * rather than doubles so that the originating frame number can be tracked
53 * exactly; values are only converted to double at plot time. Totals use
54 * double to avoid overflow across large intervals.
55 */
56typedef struct _io_graph_item_t {
57 uint32_t frames; /**< Number of frames in this interval (always computed) */
58 uint64_t bytes; /**< Total bytes across all frames in this interval (always computed) */
59 uint64_t fields; /**< Number of field occurrences accumulated in this interval */
60
61 /** Maximum field value observed in this interval. */
62 union {
63 nstime_t time_max; /**< Maximum value for relative/absolute time fields */
64 double double_max; /**< Maximum value for floating-point fields */
65 int64_t int_max; /**< Maximum value for signed integer fields */
66 uint64_t uint_max; /**< Maximum value for unsigned integer fields */
67 };
68
69 /** Minimum field value observed in this interval. */
70 union {
71 nstime_t time_min; /**< Minimum value for relative/absolute time fields */
72 double double_min; /**< Minimum value for floating-point fields */
73 int64_t int_min; /**< Minimum value for signed integer fields */
74 uint64_t uint_min; /**< Minimum value for unsigned integer fields */
75 };
76
77 /** Running total of field values accumulated in this interval (used for average and sum). */
78 union {
79 nstime_t time_tot; /**< Cumulative total for relative/absolute time fields */
80 double double_tot; /**< Cumulative total for numeric fields */
81 };
82
83 uint32_t first_frame_in_invl; /**< Frame number of the first frame in this interval */
84 uint32_t min_frame_in_invl; /**< Frame number of the frame that produced the minimum value */
85 uint32_t max_frame_in_invl; /**< Frame number of the frame that produced the maximum value */
86 uint32_t last_frame_in_invl; /**< Frame number of the last frame in this interval */
87} io_graph_item_t;
88
89/** Reset (zero) an io_graph_item_t.
90 *
91 * @param items [in,out] Array containing the items to reset.
92 * @param count [in] The number of items in the array.
93 */
94static inline void
95reset_io_graph_items(io_graph_item_t *items, size_t count, int hf_index _U___attribute__((unused))) {
96 io_graph_item_t *item;
97 size_t i;
98
99 for (i = 0; i < count; i++) {
12
Assuming 'i' is >= 'count'
13
Loop condition is false. Execution continues on line 4647
100 item = &items[i];
101
102 item->frames = 0;
103 item->bytes = 0;
104 item->fields = 0;
105 item->first_frame_in_invl = 0;
106 item->min_frame_in_invl = 0;
107 item->max_frame_in_invl = 0;
108 item->last_frame_in_invl = 0;
109
110 nstime_set_zero(&item->time_max);
111 nstime_set_zero(&item->time_min);
112 nstime_set_zero(&item->time_tot);
113
114#if 0
115 /* XXX - On C, type punning is explicitly allowed since C99 so
116 * setting the nstime_t values to 0 is always sufficient.
117 * On C++ that appears technically to be undefined behavior (though
118 * I don't know of any compilers for which it doesn't work and I
119 * can't get UBSAN to complain about it) and this would be safer.
120 */
121 if (hf_index > 0) {
122
123 switch (proto_registrar_get_ftype(hf_index)) {
124
125 case FT_INT8:
126 case FT_INT16:
127 case FT_INT24:
128 case FT_INT32:
129 case FT_INT40:
130 case FT_INT48:
131 case FT_INT56:
132 case FT_INT64:
133 item->int_max = 0;
134 item->int_min = 0;
135 item->double_tot = 0;
136 break;
137
138 case FT_UINT8:
139 case FT_UINT16:
140 case FT_UINT24:
141 case FT_UINT32:
142 case FT_UINT40:
143 case FT_UINT48:
144 case FT_UINT56:
145 case FT_UINT64:
146 item->uint_max = 0;
147 item->uint_min = 0;
148 item->double_tot = 0;
149 break;
150
151 case FT_DOUBLE:
152 case FT_FLOAT:
153 item->double_max = 0;
154 item->double_min = 0;
155 item->double_tot = 0;
156 break;
157
158 case FT_RELATIVE_TIME:
159 nstime_set_zero(&item->time_max);
160 nstime_set_zero(&item->time_min);
161 nstime_set_zero(&item->time_tot);
162 break;
163
164 default:
165 break;
166 }
167 }
168#endif
169 }
170}
171
172/** Get the interval (array index) for a packet
173 *
174 * It is up to the caller to determine if the return value is valid.
175 *
176 * @param [in] pinfo Packet of interest.
177 * @param [in] interval Time interval in microseconds
178 * @return Array index on success, -1 on failure.
179 *
180 * @note pinfo->rel_ts, and hence the index, is not affected by ignoring
181 * frames.
182 */
183int64_t get_io_graph_index(packet_info *pinfo, int interval);
184
185/** Check field and item unit compatibility
186 *
187 * @param field_name [in] Header field name to check
188 * @param hf_index [out] Assigned the header field index corresponding to field_name if valid.
189 * Can be NULL.
190 * @param item_unit [in] The type of unit to calculate. From IOG_ITEM_UNITS.
191 * @param type_unit_name [in] The name of the first unit type (i.e. Packets or Events)
192 * @return NULL if compatible, otherwise an error string. The string must
193 * be freed by the caller.
194 */
195GString *check_field_unit(const char *field_name, int *hf_index, io_graph_item_unit_t item_unit, const char* type_unit_name);
196
197/** Get the value at the given interval (idx) for the current value unit.
198 *
199 * @param items [in] Array containing the item to get.
200 * @param val_units [in] The type of unit to calculate. From IOG_ITEM_UNITS.
201 * @param idx [in] Index of the item to get.
202 * @param hf_index [in] Header field index for advanced statistics.
203 * @param cap_file [in] Capture file.
204 * @param interval [in] Timing interval in ms.
205 * @param cur_idx [in] Current index.
206 * @param asAOT [in] Interpret when possible the value as an Average Over Time.
207 */
208double get_io_graph_item(const io_graph_item_t *items, io_graph_item_unit_t val_units, int idx, int hf_index, const capture_file *cap_file, int interval, int cur_idx, bool_Bool asAOT);
209
210/** Update the values of an io_graph_item_t.
211 *
212 * Frame and byte counts are always calculated. If edt is non-NULL advanced
213 * statistics are calculated using hfindex.
214 *
215 * @param items [in,out] Array containing the item to update.
216 * @param idx [in] Index of the item to update.
217 * @param pinfo [in] Packet containing update information.
218 * @param edt [in] Dissection information for advanced statistics. May be NULL.
219 * @param hf_index [in] Header field index for advanced statistics.
220 * @param item_unit [in] The type of unit to calculate. From IOG_ITEM_UNITS.
221 * @param interval [in] Timing interval in μs.
222 * @return true if the update was successful, otherwise false.
223 */
224static inline bool_Bool
225update_io_graph_item(io_graph_item_t *items, int idx, packet_info *pinfo, epan_dissect_t *edt, int hf_index, int item_unit, uint32_t interval) {
226 io_graph_item_t *item = &items[idx];
227
228 /* Set the first and last frame num in current interval matching the target field+filter */
229 if (item->first_frame_in_invl == 0) {
16
The left operand of '==' is a garbage value
230 item->first_frame_in_invl = pinfo->num;
231 }
232 item->last_frame_in_invl = pinfo->num;
233
234 if (edt && hf_index >= 0) {
235 GPtrArray *gp;
236 unsigned i;
237
238 gp = proto_get_finfo_ptr_array(edt->tree, hf_index);
239 if (!gp) {
240 return false0;
241 }
242
243 /* Update the appropriate counters. If fields == 0, this is the first seen
244 * value so set any min/max values accordingly. */
245 for (i=0; i < gp->len; i++) {
246 int64_t new_int64;
247 uint64_t new_uint64;
248 float new_float;
249 double new_double;
250 const nstime_t *new_time;
251
252 switch (proto_registrar_get_ftype(hf_index)) {
253 case FT_UINT8:
254 case FT_UINT16:
255 case FT_UINT24:
256 case FT_UINT32:
257 new_uint64 = fvalue_get_uinteger(((field_info *)gp->pdata[i])->value);
258
259 if ((new_uint64 > item->uint_max) || (item->fields == 0)) {
260 item->uint_max = new_uint64;
261 item->max_frame_in_invl = pinfo->num;
262 }
263 if ((new_uint64 < item->uint_min) || (item->fields == 0)) {
264 item->uint_min = new_uint64;
265 item->min_frame_in_invl = pinfo->num;
266 }
267 item->double_tot += (double)new_uint64;
268 item->fields++;
269 break;
270 case FT_INT8:
271 case FT_INT16:
272 case FT_INT24:
273 case FT_INT32:
274 new_int64 = fvalue_get_sinteger(((field_info *)gp->pdata[i])->value);
275 if ((new_int64 > item->int_max) || (item->fields == 0)) {
276 item->int_max = new_int64;
277 item->max_frame_in_invl = pinfo->num;
278 }
279 if ((new_int64 < item->int_min) || (item->fields == 0)) {
280 item->int_min = new_int64;
281 item->min_frame_in_invl = pinfo->num;
282 }
283 item->double_tot += (double)new_int64;
284 item->fields++;
285 break;
286 case FT_UINT40:
287 case FT_UINT48:
288 case FT_UINT56:
289 case FT_UINT64:
290 new_uint64 = fvalue_get_uinteger64(((field_info *)gp->pdata[i])->value);
291 if ((new_uint64 > item->uint_max) || (item->fields == 0)) {
292 item->uint_max = new_uint64;
293 item->max_frame_in_invl = pinfo->num;
294 }
295 if ((new_uint64 < item->uint_min) || (item->fields == 0)) {
296 item->uint_min = new_uint64;
297 item->min_frame_in_invl = pinfo->num;
298 }
299 item->double_tot += (double)new_uint64;
300 item->fields++;
301 break;
302 case FT_INT40:
303 case FT_INT48:
304 case FT_INT56:
305 case FT_INT64:
306 new_int64 = fvalue_get_sinteger64(((field_info *)gp->pdata[i])->value);
307 if ((new_int64 > item->int_max) || (item->fields == 0)) {
308 item->int_max = new_int64;
309 item->max_frame_in_invl = pinfo->num;
310 }
311 if ((new_int64 < item->int_min) || (item->fields == 0)) {
312 item->int_min = new_int64;
313 item->min_frame_in_invl = pinfo->num;
314 }
315 item->double_tot += (double)new_int64;
316 item->fields++;
317 break;
318 case FT_FLOAT:
319 new_float = (float)fvalue_get_floating(((field_info *)gp->pdata[i])->value);
320 if ((new_float > item->double_max) || (item->fields == 0)) {
321 item->double_max = new_float;
322 item->max_frame_in_invl = pinfo->num;
323 }
324 if ((new_float < item->double_min) || (item->fields == 0)) {
325 item->double_min = new_float;
326 item->min_frame_in_invl = pinfo->num;
327 }
328 item->double_tot += new_float;
329 item->fields++;
330 break;
331 case FT_DOUBLE:
332 new_double = fvalue_get_floating(((field_info *)gp->pdata[i])->value);
333 if ((new_double > item->double_max) || (item->fields == 0)) {
334 item->double_max = new_double;
335 item->max_frame_in_invl = pinfo->num;
336 }
337 if ((new_double < item->double_min) || (item->fields == 0)) {
338 item->double_min = new_double;
339 item->min_frame_in_invl = pinfo->num;
340 }
341 item->double_tot += new_double;
342 item->fields++;
343 break;
344 case FT_RELATIVE_TIME:
345 new_time = fvalue_get_time(((field_info *)gp->pdata[i])->value);
346
347 switch (item_unit) {
348 case IOG_ITEM_UNIT_CALC_LOAD:
349 {
350 uint64_t t, pt; /* time in us */
351 int j;
352 /*
353 * Add the time this call spanned each interval according to
354 * its contribution to that interval.
355 * If the call time is negative (unlikely, requires both an
356 * out of order capture file plus retransmission), ignore.
357 */
358 const nstime_t time_zero = NSTIME_INIT_ZERO{0, 0};
359 if (nstime_cmp(new_time, &time_zero) < 0) {
360 break;
361 }
362 t = new_time->secs;
363 t = t * 1000000 + new_time->nsecs / 1000;
364 j = idx;
365 /*
366 * Handle current interval
367 * This cannot be negative, because get_io_graph_index
368 * returns an invalid interval if so.
369 */
370 pt = pinfo->rel_ts.secs * 1000000 + pinfo->rel_ts.nsecs / 1000;
371 pt = pt % interval;
372 if (pt > t) {
373 pt = t;
374 }
375 while (t) {
376 io_graph_item_t *load_item;
377
378 load_item = &items[j];
379 load_item->time_tot.nsecs += (int) (pt * 1000);
380 if (load_item->time_tot.nsecs > 1000000000) {
381 load_item->time_tot.secs++;
382 load_item->time_tot.nsecs -= 1000000000;
383 }
384 load_item->fields++;
385
386 if (j == 0) {
387 break;
388 }
389 j--;
390 t -= pt;
391 if (t > (uint64_t) interval) {
392 pt = (uint64_t) interval;
393 } else {
394 pt = t;
395 }
396 }
397 break;
398 }
399 default:
400 if ( (nstime_cmp(new_time, &item->time_max) > 0)
401 || (item->fields == 0)) {
402 item->time_max = *new_time;
403 item->max_frame_in_invl = pinfo->num;
404 }
405 if ( (nstime_cmp(new_time, &item->time_min) < 0)
406 || (item->fields == 0)) {
407 item->time_min = *new_time;
408 item->min_frame_in_invl = pinfo->num;
409 }
410 nstime_add(&item->time_tot, new_time)nstime_sum(&item->time_tot, &item->time_tot, new_time
)
;
411 item->fields++;
412 }
413 break;
414 default:
415 if ((item_unit == IOG_ITEM_UNIT_CALC_FRAMES) ||
416 (item_unit == IOG_ITEM_UNIT_CALC_FIELDS)) {
417 /*
418 * It's not an integeresque type, but
419 * all we want to do is count it, so
420 * that's all right.
421 */
422 item->fields++;
423 }
424 else {
425 /*
426 * "Can't happen"; see the "check that the
427 * type is compatible" check in
428 * filter_callback().
429 */
430 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "ui/io_graph_item.h", 430
, __func__, "assertion \"not reached\" failed")
;
431 }
432 break;
433 }
434 }
435 }
436
437 item->frames++;
438 item->bytes += pinfo->fd->pkt_len;
439
440 return true1;
441}
442
443
444#ifdef __cplusplus
445}
446#endif /* __cplusplus */
447
448#endif /* __IO_GRAPH_ITEM_H__ */