Coverage Report

Created: 2025-02-15 06:25

/src/wireshark/epan/export_object.c
Line
Count
Source (jump to first uncovered line)
1
/* export_object.c
2
 * GUI independent helper routines common to all export object taps.
3
 *
4
 * Wireshark - Network traffic analyzer
5
 * By Gerald Combs <gerald@wireshark.org>
6
 * Copyright 1998 Gerald Combs
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 */
10
11
#include "config.h"
12
13
#include <string.h>
14
15
#include "proto.h"
16
#include "packet_info.h"
17
#include "export_object.h"
18
19
struct register_eo {
20
    int proto_id;                        /* protocol id (0-indexed) */
21
    const char* tap_listen_str;          /* string used in register_tap_listener (NULL to use protocol name) */
22
    tap_packet_cb eo_func;               /* function to be called for new incoming packets for SRT */
23
    export_object_gui_reset_cb reset_cb; /* function to parse parameters of optional arguments of tap string */
24
};
25
26
static wmem_tree_t *registered_eo_tables;
27
28
int
29
register_export_object(const int proto_id, tap_packet_cb export_packet_func, export_object_gui_reset_cb reset_cb)
30
98
{
31
98
    register_eo_t *table;
32
98
    DISSECTOR_ASSERT(export_packet_func);
33
34
98
    table = wmem_new(wmem_epan_scope(), register_eo_t);
35
36
98
    table->proto_id      = proto_id;
37
98
    table->tap_listen_str = wmem_strdup_printf(wmem_epan_scope(), "%s_eo", proto_get_protocol_filter_name(proto_id));
38
98
    table->eo_func = export_packet_func;
39
98
    table->reset_cb = reset_cb;
40
41
98
    if (registered_eo_tables == NULL)
42
14
        registered_eo_tables = wmem_tree_new(wmem_epan_scope());
43
44
98
    wmem_tree_insert_string(registered_eo_tables, proto_get_protocol_filter_name(proto_id), table, 0);
45
98
    return register_tap(table->tap_listen_str);
46
98
}
47
48
int get_eo_proto_id(register_eo_t* eo)
49
0
{
50
0
    if (!eo) {
51
0
        return -1;
52
0
    }
53
0
    return eo->proto_id;
54
0
}
55
56
const char* get_eo_tap_listener_name(register_eo_t* eo)
57
0
{
58
0
    return eo->tap_listen_str;
59
0
}
60
61
tap_packet_cb get_eo_packet_func(register_eo_t* eo)
62
0
{
63
0
    return eo->eo_func;
64
0
}
65
66
export_object_gui_reset_cb get_eo_reset_func(register_eo_t* eo)
67
0
{
68
0
    return eo->reset_cb;
69
0
}
70
71
register_eo_t* get_eo_by_name(const char* name)
72
0
{
73
0
    return (register_eo_t*)wmem_tree_lookup_string(registered_eo_tables, name, 0);
74
0
}
75
76
void eo_iterate_tables(wmem_foreach_func func, void *user_data)
77
0
{
78
0
    wmem_tree_foreach(registered_eo_tables, func, user_data);
79
0
}
80
81
static GString *eo_rename(GString *gstr, size_t maxlen, int dupn)
82
0
{
83
0
    GString *gstr_tmp;
84
0
    char *tmp_ptr;
85
0
    GString *ext_str = NULL;
86
87
0
    gstr_tmp = g_string_new("");
88
0
    if (dupn != 0) {
89
0
        g_string_append_printf (gstr_tmp, "(%d)", dupn);
90
0
    }
91
0
    if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL && ((ext_str = g_string_new(tmp_ptr))->len + strlen(gstr_tmp->str) < maxlen) ) {
92
        /* Retain the extension */
93
0
        gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
94
0
        if ( gstr->len >= (maxlen - (strlen(gstr_tmp->str) + ext_str->len)) )
95
0
            gstr = g_string_truncate(gstr, maxlen - (strlen(gstr_tmp->str) + ext_str->len));
96
0
        gstr = g_string_append(gstr, gstr_tmp->str);
97
0
        gstr = g_string_append(gstr, ext_str->str);
98
0
    }
99
0
    else {
100
0
        if ( gstr->len >= (maxlen - strlen(gstr_tmp->str)) )
101
0
            gstr = g_string_truncate(gstr, maxlen - strlen(gstr_tmp->str));
102
0
        gstr = g_string_append(gstr, gstr_tmp->str);
103
0
    }
104
105
0
    if (ext_str) {
106
0
        g_string_free(ext_str, TRUE);
107
0
    }
108
109
0
    g_string_free(gstr_tmp, TRUE);
110
0
    return gstr;
111
0
}
112
113
GString *
114
eo_massage_str(const char *in_str, size_t maxlen, int dupn)
115
0
{
116
0
    char *tmp_ptr;
117
    /* The characters in "reject" come from:
118
     * https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions.
119
     * Add to the list as necessary for other OS's.
120
     */
121
0
    const char *reject = "<>:\"/\\|?*"
122
0
        "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
123
0
    "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
124
0
    "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
125
0
    GString *out_str;
126
127
0
    out_str = g_string_new("");
128
129
    /* Find all disallowed characters/bytes and replace them with %xx */
130
0
    while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
131
0
        out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
132
0
        g_string_append_printf(out_str, "%%%02x", *tmp_ptr);
133
0
        in_str = tmp_ptr + 1;
134
0
    }
135
0
    out_str = g_string_append(out_str, in_str);
136
0
    if ( dupn != 0 || out_str->len > maxlen )
137
0
        out_str = eo_rename(out_str, maxlen, dupn);
138
0
    return out_str;
139
0
}
140
141
const char *
142
eo_ct2ext(const char *content_type)
143
0
{
144
    /* TODO: Map the content type string to an extension string.  If no match,
145
     * return NULL. */
146
0
    return content_type;
147
0
}
148
149
void eo_free_entry(export_object_entry_t *entry)
150
0
{
151
0
    g_free(entry->hostname);
152
0
    g_free(entry->content_type);
153
0
    g_free(entry->filename);
154
0
    g_free(entry->payload_data);
155
156
0
    g_free(entry);
157
0
}
158
159
/*
160
 * Editor modelines
161
 *
162
 * Local Variables:
163
 * c-basic-offset: 4
164
 * tab-width: 8
165
 * indent-tabs-mode: nil
166
 * End:
167
 *
168
 * ex: set shiftwidth=4 tabstop=8 expandtab:
169
 * :indentSize=4:tabSize=8:noTabs=true:
170
 */