exported_object: add cleanup function.
[metze/wireshark/wip.git] / epan / export_object.c
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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "proto.h"
28 #include "packet_info.h"
29 #include "export_object.h"
30
31 struct register_eo {
32     int proto_id;                        /* protocol id (0-indexed) */
33     const char* tap_listen_str;          /* string used in register_tap_listener (NULL to use protocol name) */
34     tap_packet_cb eo_func;               /* function to be called for new incoming packets for SRT */
35     export_object_gui_reset_cb reset_cb; /* function to parse parameters of optional arguments of tap string */
36 };
37
38 static GSList *registered_eo_tables = NULL;
39
40 static gint
41 insert_sorted_by_table_name(gconstpointer aparam, gconstpointer bparam)
42 {
43     const register_eo_t *a = (const register_eo_t *)aparam;
44     const register_eo_t *b = (const register_eo_t *)bparam;
45
46     return g_ascii_strcasecmp(proto_get_protocol_filter_name(a->proto_id), proto_get_protocol_filter_name(b->proto_id));
47 }
48
49 int
50 register_export_object(const int proto_id, tap_packet_cb export_packet_func, export_object_gui_reset_cb reset_cb)
51 {
52     register_eo_t *table;
53     DISSECTOR_ASSERT(export_packet_func);
54
55     table = g_new(register_eo_t,1);
56
57     table->proto_id      = proto_id;
58     table->tap_listen_str = g_strdup_printf("%s_eo", proto_get_protocol_filter_name(proto_id));
59     table->eo_func = export_packet_func;
60     table->reset_cb = reset_cb;
61
62     registered_eo_tables = g_slist_insert_sorted(registered_eo_tables, table, insert_sorted_by_table_name);
63     return register_tap(table->tap_listen_str);
64 }
65
66 int get_eo_proto_id(register_eo_t* eo)
67 {
68     if (!eo) {
69         return -1;
70     }
71     return eo->proto_id;
72 }
73
74 const char* get_eo_tap_listener_name(register_eo_t* eo)
75 {
76     return eo->tap_listen_str;
77 }
78
79 tap_packet_cb get_eo_packet_func(register_eo_t* eo)
80 {
81     return eo->eo_func;
82 }
83
84 export_object_gui_reset_cb get_eo_reset_func(register_eo_t* eo)
85 {
86     return eo->reset_cb;
87 }
88
89 static gint
90 find_matching_eo(gconstpointer arg1, gconstpointer arg2)
91 {
92     register_eo_t *eo = (register_eo_t*)arg1;
93     const gchar   *name   = (const gchar *)arg2;
94
95     return strcmp(proto_get_protocol_filter_name(eo->proto_id), name);
96 }
97
98 register_eo_t* get_eo_by_name(const char* name)
99 {
100     GSList *found_eo;
101
102     found_eo = g_slist_find_custom(registered_eo_tables,
103                     (gpointer)name, find_matching_eo);
104
105     if (found_eo)
106         return (register_eo_t*)found_eo->data;
107
108     return NULL;
109 }
110
111 void eo_iterate_tables(GFunc func, gpointer user_data)
112 {
113     g_slist_foreach(registered_eo_tables, func, user_data);
114 }
115
116 static GString *eo_rename(GString *gstr, int dupn)
117 {
118     GString *gstr_tmp;
119     gchar *tmp_ptr;
120     GString *ext_str;
121
122     gstr_tmp = g_string_new("(");
123     g_string_append_printf (gstr_tmp, "%d)", dupn);
124     if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL ) {
125         /* Retain the extension */
126         ext_str = g_string_new(tmp_ptr);
127         gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
128         if ( gstr->len >= (EXPORT_OBJECT_MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len)) )
129             gstr = g_string_truncate(gstr, EXPORT_OBJECT_MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len));
130         gstr = g_string_append(gstr, gstr_tmp->str);
131         gstr = g_string_append(gstr, ext_str->str);
132         g_string_free(ext_str, TRUE);
133     }
134     else {
135         if ( gstr->len >= (EXPORT_OBJECT_MAXFILELEN - strlen(gstr_tmp->str)) )
136             gstr = g_string_truncate(gstr, EXPORT_OBJECT_MAXFILELEN - strlen(gstr_tmp->str));
137         gstr = g_string_append(gstr, gstr_tmp->str);
138     }
139     g_string_free(gstr_tmp, TRUE);
140     return gstr;
141 }
142
143 GString *
144 eo_massage_str(const gchar *in_str, gsize maxlen, int dupn)
145 {
146     gchar *tmp_ptr;
147     /* The characters in "reject" come from:
148      * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
149      * Add to the list as necessary for other OS's.
150      */
151     const gchar *reject = "<>:\"/\\|?*"
152         "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
153     "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
154     "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
155     GString *out_str;
156     GString *ext_str;
157
158     out_str = g_string_new("");
159
160     /* Find all disallowed characters/bytes and replace them with %xx */
161     while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
162         out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
163         g_string_append_printf(out_str, "%%%02x", *tmp_ptr);
164         in_str = tmp_ptr + 1;
165     }
166     out_str = g_string_append(out_str, in_str);
167     if ( out_str->len > maxlen ) {
168         if ( (tmp_ptr = strrchr(out_str->str, '.')) != NULL ) {
169             /* Retain the extension */
170             ext_str = g_string_new(tmp_ptr);
171             out_str = g_string_truncate(out_str, maxlen - ext_str->len);
172             out_str = g_string_append(out_str, ext_str->str);
173             g_string_free(ext_str, TRUE);
174         }
175         else
176             out_str = g_string_truncate(out_str, maxlen);
177     }
178     if ( dupn != 0 )
179         out_str = eo_rename(out_str, dupn);
180     return out_str;
181 }
182
183 const char *
184 eo_ct2ext(const char *content_type)
185 {
186     /* TODO: Map the content type string to an extension string.  If no match,
187      * return NULL. */
188     return content_type;
189 }
190
191 void eo_free_entry(export_object_entry_t *entry)
192 {
193     g_free(entry->hostname);
194     g_free(entry->content_type);
195     g_free(entry->filename);
196     g_free(entry->payload_data);
197
198     g_free(entry);
199 }
200
201 static void
202 free_eo_table(gpointer p, gpointer user_data _U_)
203 {
204     register_eo_t *table = (register_eo_t*)p;
205     g_free((gpointer)table->tap_listen_str);
206     g_free(table);
207 }
208
209 void export_object_cleanup(void)
210 {
211     g_slist_foreach(registered_eo_tables, free_eo_table, NULL);
212     g_slist_free(registered_eo_tables);
213 }
214
215 /*
216  * Editor modelines
217  *
218  * Local Variables:
219  * c-basic-offset: 4
220  * tab-width: 8
221  * indent-tabs-mode: nil
222  * End:
223  *
224  * ex: set shiftwidth=4 tabstop=8 expandtab:
225  * :indentSize=4:tabSize=8:noTabs=true:
226  */