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 = NULL;
27
28 int
register_export_object(const int proto_id,tap_packet_cb export_packet_func,export_object_gui_reset_cb reset_cb)29 register_export_object(const int proto_id, tap_packet_cb export_packet_func, export_object_gui_reset_cb reset_cb)
30 {
31 register_eo_t *table;
32 DISSECTOR_ASSERT(export_packet_func);
33
34 table = wmem_new(wmem_epan_scope(), register_eo_t);
35
36 table->proto_id = proto_id;
37 table->tap_listen_str = wmem_strdup_printf(wmem_epan_scope(), "%s_eo", proto_get_protocol_filter_name(proto_id));
38 table->eo_func = export_packet_func;
39 table->reset_cb = reset_cb;
40
41 if (registered_eo_tables == NULL)
42 registered_eo_tables = wmem_tree_new(wmem_epan_scope());
43
44 wmem_tree_insert_string(registered_eo_tables, proto_get_protocol_filter_name(proto_id), table, 0);
45 return register_tap(table->tap_listen_str);
46 }
47
get_eo_proto_id(register_eo_t * eo)48 int get_eo_proto_id(register_eo_t* eo)
49 {
50 if (!eo) {
51 return -1;
52 }
53 return eo->proto_id;
54 }
55
get_eo_tap_listener_name(register_eo_t * eo)56 const char* get_eo_tap_listener_name(register_eo_t* eo)
57 {
58 return eo->tap_listen_str;
59 }
60
get_eo_packet_func(register_eo_t * eo)61 tap_packet_cb get_eo_packet_func(register_eo_t* eo)
62 {
63 return eo->eo_func;
64 }
65
get_eo_reset_func(register_eo_t * eo)66 export_object_gui_reset_cb get_eo_reset_func(register_eo_t* eo)
67 {
68 return eo->reset_cb;
69 }
70
get_eo_by_name(const char * name)71 register_eo_t* get_eo_by_name(const char* name)
72 {
73 return (register_eo_t*)wmem_tree_lookup_string(registered_eo_tables, name, 0);
74 }
75
eo_iterate_tables(wmem_foreach_func func,gpointer user_data)76 void eo_iterate_tables(wmem_foreach_func func, gpointer user_data)
77 {
78 wmem_tree_foreach(registered_eo_tables, func, user_data);
79 }
80
eo_rename(GString * gstr,gsize maxlen,int dupn)81 static GString *eo_rename(GString *gstr, gsize maxlen, int dupn)
82 {
83 GString *gstr_tmp;
84 gchar *tmp_ptr;
85 GString *ext_str = NULL;
86
87 gstr_tmp = g_string_new("");
88 if (dupn != 0) {
89 g_string_append_printf (gstr_tmp, "(%d)", dupn);
90 }
91 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 gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
94 if ( gstr->len >= (maxlen - (strlen(gstr_tmp->str) + ext_str->len)) )
95 gstr = g_string_truncate(gstr, maxlen - (strlen(gstr_tmp->str) + ext_str->len));
96 gstr = g_string_append(gstr, gstr_tmp->str);
97 gstr = g_string_append(gstr, ext_str->str);
98 }
99 else {
100 if ( gstr->len >= (maxlen - strlen(gstr_tmp->str)) )
101 gstr = g_string_truncate(gstr, maxlen - strlen(gstr_tmp->str));
102 gstr = g_string_append(gstr, gstr_tmp->str);
103 }
104
105 if (ext_str) {
106 g_string_free(ext_str, TRUE);
107 }
108
109 g_string_free(gstr_tmp, TRUE);
110 return gstr;
111 }
112
113 GString *
eo_massage_str(const gchar * in_str,gsize maxlen,int dupn)114 eo_massage_str(const gchar *in_str, gsize maxlen, int dupn)
115 {
116 gchar *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 const gchar *reject = "<>:\"/\\|?*"
122 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
123 "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
124 "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
125 GString *out_str;
126
127 out_str = g_string_new("");
128
129 /* Find all disallowed characters/bytes and replace them with %xx */
130 while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
131 out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
132 g_string_append_printf(out_str, "%%%02x", *tmp_ptr);
133 in_str = tmp_ptr + 1;
134 }
135 out_str = g_string_append(out_str, in_str);
136 if ( dupn != 0 || out_str->len > maxlen )
137 out_str = eo_rename(out_str, maxlen, dupn);
138 return out_str;
139 }
140
141 const char *
eo_ct2ext(const char * content_type)142 eo_ct2ext(const char *content_type)
143 {
144 /* TODO: Map the content type string to an extension string. If no match,
145 * return NULL. */
146 return content_type;
147 }
148
eo_free_entry(export_object_entry_t * entry)149 void eo_free_entry(export_object_entry_t *entry)
150 {
151 g_free(entry->hostname);
152 g_free(entry->content_type);
153 g_free(entry->filename);
154 g_free(entry->payload_data);
155
156 g_free(entry);
157 }
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 */
171