Bug Summary

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

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