Bug Summary

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

/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 <[email protected]>
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 4607
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__ */