summaryrefslogtreecommitdiffstats
path: root/epan/stat_tap_ui.c
blob: 2d8f9a20dbc11eb434640b7116ab080aea63f3ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* stat_tap_ui.c
 * Routines to register UI information for stats
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdio.h>

#include <string.h>

#include <glib.h>

#include <epan/stat_tap_ui.h>
#include <wsutil/ws_assert.h>

/* structure to keep track of what stats have registered command-line
   arguments.
 */
typedef struct _stat_cmd_arg {
    stat_tap_ui *ui;
    const char *cmd;
    void (*func)(const char *arg, void* userdata);
    void* userdata;
} stat_cmd_arg;

static wmem_list_t *stat_cmd_arg_list;

/* structure to keep track of what stats have been specified on the
   command line.
 */
typedef struct {
    stat_cmd_arg *sca;
    char *arg;
} stat_requested;
static GSList *stats_requested;

/* **********************************************************************
 * Function called from stat to register the stat's command-line argument
 * and initialization routine
 * ********************************************************************** */
static int
search_duplicate(const void *a, const void *b)
{
    return strcmp(((const stat_cmd_arg *)a)->cmd, (const char *)b);
}

static int
sort_by_name(const void *a, const void *b)
{
    return strcmp(((const stat_cmd_arg *)a)->cmd, ((const stat_cmd_arg *)b)->cmd);
}

void
register_stat_tap_ui(stat_tap_ui *ui, void *userdata)
{
    stat_cmd_arg *newsca;

    if (stat_cmd_arg_list == NULL)
        stat_cmd_arg_list = wmem_list_new(wmem_epan_scope());

    /* Key is already present */
    if (wmem_list_find_custom(stat_cmd_arg_list, ui->cli_string, search_duplicate))
        return;

    newsca = wmem_new(wmem_epan_scope(), stat_cmd_arg);
    newsca->cmd= wmem_strdup(wmem_epan_scope(), ui->cli_string);
    newsca->func=ui->tap_init_cb;
    newsca->userdata=userdata;

    wmem_list_insert_sorted(stat_cmd_arg_list, newsca, sort_by_name);
}

/* **********************************************************************
 * Function called for a stat command-line argument
 * ********************************************************************** */
bool
process_stat_cmd_arg(const char *optstr)
{
    wmem_list_frame_t *entry;
    stat_cmd_arg *sca;
    stat_requested *tr;
    char *stat_command = g_strdup(optstr);

    /* Renamed in Wireshark 3.0, backwards compatibility. */
    if (!strncmp(stat_command, "follow,ssl", strlen("follow,ssl"))) {
        memcpy(stat_command + 7, "tls", 3);
    }

    /* The strings "ipx" or "ipv6" must be tested before "ip" to select the
      right tap so the sorting does matter.  And it's also why the list is
      walked backwards */
    for (entry = wmem_list_tail(stat_cmd_arg_list); entry; entry = wmem_list_frame_prev(entry)) {
        sca = (stat_cmd_arg*)wmem_list_frame_data(entry);
        if (!strncmp(sca->cmd, stat_command, strlen(sca->cmd))) {
            tr=g_new(stat_requested, 1);
            tr->sca = sca;
            tr->arg = stat_command;
            stats_requested = g_slist_append(stats_requested, tr);
            return true;
        }
    }
    g_free(stat_command);
    return false;
}

/* **********************************************************************
 * Function to list all possible tap command-line arguments
 * ********************************************************************** */
static void
list_stat_cmd_args_func(void *data, void *userdata _U_)
{
    fprintf(stderr,"     %s\n", ((stat_cmd_arg*)data)->cmd);
}

void
list_stat_cmd_args(void)
{
    wmem_list_foreach(stat_cmd_arg_list, list_stat_cmd_args_func, NULL);
}

/* **********************************************************************
 * Function to process stats requested with command-line arguments
 * ********************************************************************** */
void
start_requested_stats(void)
{
    stat_requested *sr;

    while(stats_requested){
        sr=(stat_requested *)stats_requested->data;
        (*sr->sca->func)(sr->arg,sr->sca->userdata);
        stats_requested=g_slist_remove(stats_requested, sr);
        g_free(sr->arg);
        g_free(sr);
    }
}

static wmem_tree_t *registered_stat_tables;

void register_stat_tap_table_ui(stat_tap_table_ui *ui)
{
    if (registered_stat_tables == NULL)
        registered_stat_tables = wmem_tree_new(wmem_epan_scope());

    wmem_tree_insert_string(registered_stat_tables, ui->cli_string, ui, 0);
}

stat_tap_table_ui *stat_tap_by_name(const char *name)
{
    return (stat_tap_table_ui *) wmem_tree_lookup_string(registered_stat_tables, name, 0);
}

void stat_tap_iterate_tables(wmem_foreach_func func, void *user_data)
{
    wmem_tree_foreach(registered_stat_tables, func, user_data);
}

void stat_tap_get_filter(stat_tap_table_ui* new_stat, const char *opt_arg, const char **filter, char** err)
{
    unsigned len = (unsigned) strlen(new_stat->cli_string);
    *filter=NULL;
    *err=NULL;

    if (!strncmp(opt_arg, new_stat->cli_string, len))
    {
        if (opt_arg[len] == ',')
        {
           *filter = opt_arg + len+1;
        }
    }

    if (new_stat->stat_filter_check_cb)
        new_stat->stat_filter_check_cb(opt_arg, filter, err);
}

stat_tap_table* stat_tap_init_table(const char *name, int num_fields, int num_elements,
                const char *filter_string)
{
    stat_tap_table* new_table = g_new0(stat_tap_table, 1);

    new_table->title = name;
    new_table->num_elements = num_elements;
    new_table->num_fields = num_fields;
    new_table->filter_string = filter_string;
    new_table->elements = g_new0(stat_tap_table_item_type*, num_elements);

    return new_table;
}

stat_tap_table *stat_tap_find_table(stat_tap_table_ui *ui, const char *name)
{
    unsigned i = 0;
    stat_tap_table *stat_table;

    if (ui->tables == NULL)
        return NULL;

    for (i = 0; i < ui->tables->len; i++) {
        stat_table = g_array_index(ui->tables, stat_tap_table *, i);
        if (!g_strcmp0(stat_table->title, name)) {
            return stat_table;
        }
    }

    return NULL;
}

void stat_tap_add_table(stat_tap_table_ui* new_stat, stat_tap_table* table)
{
    if (new_stat->tables == NULL)
        new_stat->tables = g_array_new(false, true, sizeof(stat_tap_table*));

    g_array_insert_val(new_stat->tables, new_stat->tables->len, table);
}

void stat_tap_init_table_row(stat_tap_table *stat_table, unsigned table_index, unsigned num_fields, const stat_tap_table_item_type* fields)
{
    /* we have discovered a new procedure. Extend the table accordingly */
    if(table_index>=stat_table->num_elements){
        unsigned old_num_elements=stat_table->num_elements;
        unsigned i;

        stat_table->num_elements=table_index+1;
        stat_table->elements = (stat_tap_table_item_type**)g_realloc(stat_table->elements, sizeof(stat_tap_table_item_type*)*(stat_table->num_elements));
        for(i=old_num_elements;i<stat_table->num_elements;i++){
            stat_table->elements[i] = g_new0(stat_tap_table_item_type, stat_table->num_fields);
        }
    }
    memcpy(stat_table->elements[table_index], fields, num_fields*sizeof(stat_tap_table_item_type));

}

stat_tap_table_item_type* stat_tap_get_field_data(const stat_tap_table *stat_table, unsigned table_index, unsigned field_index)
{
    stat_tap_table_item_type* field_value;
    ws_assert(table_index < stat_table->num_elements);

    field_value = stat_table->elements[table_index];

    ws_assert(field_index < stat_table->num_fields);

    return &field_value[field_index];
}

void stat_tap_set_field_data(stat_tap_table *stat_table, unsigned table_index, unsigned field_index, stat_tap_table_item_type* field_data)
{
    stat_tap_table_item_type* field_value;
    ws_assert(table_index < stat_table->num_elements);

    field_value = stat_table->elements[table_index];

    ws_assert(field_index < stat_table->num_fields);

    field_value[field_index] = *field_data;
}

void reset_stat_table(stat_tap_table_ui* new_stat)
{
    unsigned i = 0;
    stat_tap_table *stat_table;

    for (i = 0; i < new_stat->tables->len; i++)
    {
        stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);

        if (new_stat->stat_tap_reset_table_cb)
            new_stat->stat_tap_reset_table_cb(stat_table);
    }
}

void free_stat_tables(stat_tap_table_ui* new_stat)
{
    unsigned i = 0, element, field_index;
    stat_tap_table *stat_table;
    stat_tap_table_item_type* field_data;

    for (i = 0; i < new_stat->tables->len; i++)
    {
        stat_table = g_array_index(new_stat->tables, stat_tap_table*, i);

        for (element = 0; element < stat_table->num_elements; element++)
        {
            for (field_index = 0; field_index < stat_table->num_fields; field_index++)
            {
                field_data = stat_tap_get_field_data(stat_table, element, field_index);
                /* Give dissector a crack at it */
                /* XXX Should this be per-row instead? */
                if (new_stat->stat_tap_free_table_item_cb)
                    new_stat->stat_tap_free_table_item_cb(stat_table, element, field_index, field_data);
            }
            g_free(stat_table->elements[element]);
        }
        g_free(stat_table->elements);
        g_free(stat_table);
    }
    g_array_set_size(new_stat->tables, 0);
}


/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */