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