3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
20 #include <wsutil/file_util.h>
21 #include <ui/cmdarg_err.h>
23 #include <epan/packet_info.h>
24 #include <epan/packet.h>
25 #include <epan/export_object.h>
26 #include <ui/export_object_ui.h>
27 #include "tap-exportobject.h"
29 typedef struct _export_object_list_gui_t {
32 } export_object_list_gui_t;
34 static GHashTable* eo_opts = NULL;
37 list_exportobject_protocol(const void *key, void *value _U_, void *userdata _U_)
39 fprintf(stderr, " %s\n", (const gchar*)key);
43 void eo_list_object_types(void)
45 eo_iterate_tables(list_exportobject_protocol, NULL);
48 gboolean eo_tap_opt_add(const char *option_string)
53 eo_opts = g_hash_table_new(g_str_hash,g_str_equal);
55 splitted = g_strsplit(option_string, ",", 2);
57 if ((splitted[0] == NULL) || (splitted[1] == NULL) || (get_eo_by_name(splitted[0]) == NULL))
59 fprintf(stderr, "tshark: \"--export-objects\" are specified as: <protocol>,<destdir>\n");
60 fprintf(stderr, "tshark: The available export object types for the \"--export-objects\" option are:\n");
61 eo_list_object_types();
65 gchar* dir = (gchar*)g_hash_table_lookup(eo_opts, splitted[0]);
67 /* Since we're saving all objects from a protocol,
68 it can only be listed once */
70 g_hash_table_insert(eo_opts, splitted[0], splitted[1]);
77 cmdarg_err("\"--export-objects\" already specified protocol '%s'", splitted[0]);
86 object_list_add_entry(void *gui_data, export_object_entry_t *entry)
88 export_object_list_gui_t *object_list = (export_object_list_gui_t*)gui_data;
90 object_list->entries = g_slist_append(object_list->entries, entry);
93 static export_object_entry_t*
94 object_list_get_entry(void *gui_data, int row) {
95 export_object_list_gui_t *object_list = (export_object_list_gui_t*)gui_data;
97 return (export_object_entry_t *)g_slist_nth_data(object_list->entries, row);
100 /* This is just for writing Exported Objects to a file */
102 eo_draw(void *tapdata)
104 export_object_list_t *tap_object = (export_object_list_t *)tapdata;
105 export_object_list_gui_t *object_list = (export_object_list_gui_t*)tap_object->gui_data;
106 GSList *slist = object_list->entries;
107 export_object_entry_t *entry;
108 gboolean all_saved = TRUE;
109 gchar* save_in_path = (gchar*)g_hash_table_lookup(eo_opts, proto_get_protocol_filter_name(get_eo_proto_id(object_list->eo)));
110 GString *safe_filename = NULL;
111 gchar *save_as_fullpath = NULL;
114 if (!g_file_test(save_in_path, G_FILE_TEST_IS_DIR)) {
115 /* If the destination directory (or its parents) do not exist, create them. */
116 if (g_mkdir_with_parents(save_in_path, 0755) == -1) {
117 fprintf(stderr, "Failed to create export objects output directory \"%s\": %s\n",
118 save_in_path, g_strerror(errno));
124 entry = (export_object_entry_t *)slist->data;
126 g_free(save_as_fullpath);
127 if (entry->filename) {
128 safe_filename = eo_massage_str(entry->filename,
129 EXPORT_OBJECT_MAXFILELEN, count);
131 char generic_name[EXPORT_OBJECT_MAXFILELEN+1];
133 ext = eo_ct2ext(entry->content_type);
134 g_snprintf(generic_name, sizeof(generic_name),
135 "object%u%s%s", entry->pkt_num, ext ? "." : "", ext ? ext : "");
136 safe_filename = eo_massage_str(generic_name,
137 EXPORT_OBJECT_MAXFILELEN, count);
139 save_as_fullpath = g_build_filename(save_in_path, safe_filename->str, NULL);
140 g_string_free(safe_filename, TRUE);
141 } while (g_file_test(save_as_fullpath, G_FILE_TEST_EXISTS) && ++count < 1000);
143 if (!eo_save_entry(save_as_fullpath, entry, TRUE))
145 g_free(save_as_fullpath);
146 save_as_fullpath = NULL;
151 fprintf(stderr, "Export objects (%s): Some files could not be saved.\n",
152 proto_get_protocol_filter_name(get_eo_proto_id(object_list->eo)));
156 exportobject_handler(gpointer key, gpointer value _U_, gpointer user_data _U_)
159 export_object_list_t *tap_data;
160 export_object_list_gui_t *object_list;
163 eo = get_eo_by_name((const char*)key);
166 cmdarg_err("\"--export-objects\" INTERNAL ERROR '%s' protocol not found", (const char*)key);
170 tap_data = g_new0(export_object_list_t,1);
171 object_list = g_new0(export_object_list_gui_t,1);
173 tap_data->add_entry = object_list_add_entry;
174 tap_data->get_entry = object_list_get_entry;
175 tap_data->gui_data = (void*)object_list;
177 object_list->eo = eo;
179 /* Data will be gathered via a tap callback */
180 error_msg = register_tap_listener(get_eo_tap_listener_name(eo), tap_data, NULL, 0,
181 NULL, get_eo_packet_func(eo), eo_draw, NULL);
184 cmdarg_err("Can't register %s tap: %s", (const char*)key, error_msg->str);
185 g_string_free(error_msg, TRUE);
192 void start_exportobjects(void)
195 g_hash_table_foreach(eo_opts, exportobject_handler, NULL);
199 * Editor modelines - https://www.wireshark.org/tools/modelines.html
204 * indent-tabs-mode: nil
207 * vi: set shiftwidth=4 tabstop=8 expandtab:
208 * :indentSize=4:tabSize=8:noTabs=true: