Add expert info configuration framework. Bug 2412 (https://bugs.wireshark.org/bugzil...
[metze/wireshark/wip.git] / epan / epan.c
1 /* epan.c
2  *
3  * $Id$
4  *
5  * Wireshark Protocol Analyzer Library
6  *
7  * Copyright (c) 2001 by Gerald Combs <gerald@wireshark.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_PYTHON
27 #include <Python.h> /* to get the Python version number (PY_VERSION) */
28 #endif
29
30 #ifdef HAVE_LIBGCRYPT
31 #include <wsutil/wsgcrypt.h>
32 #endif /* HAVE_LIBGCRYPT */
33
34 #ifdef HAVE_LIBGNUTLS
35 #include <gnutls/gnutls.h>
36 #endif /* HAVE_LIBGNUTLS */
37
38 #include <glib.h>
39 #include "epan.h"
40 #include "epan_dissect.h"
41 #include "report_err.h"
42
43 #include "conversation.h"
44 #include "circuit.h"
45 #include "except.h"
46 #include "packet.h"
47 #include "prefs.h"
48 #include "column-utils.h"
49 #include "tap.h"
50 #include "addr_resolv.h"
51 #include "oids.h"
52 #include "emem.h"
53 #include "wmem/wmem.h"
54 #include "expert.h"
55
56 #ifdef HAVE_LUA
57 #include <lua.h>
58 #include <wslua/wslua.h>
59 #endif
60
61 #ifdef HAVE_LIBSMI
62 #include <smi.h>
63 #endif
64
65 #ifdef HAVE_C_ARES
66 #include <ares_version.h>
67 #endif
68
69 const gchar*
70 epan_get_version(void) {
71         return VERSION;
72 }
73
74 void
75 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
76           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
77           register_cb cb,
78           gpointer client_data,
79           void (*report_failure_fcn_p)(const char *, va_list),
80           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
81           void (*report_read_failure_fcn_p)(const char *, int),
82           void (*report_write_failure_fcn_p)(const char *, int))
83 {
84         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
85             report_read_failure_fcn_p, report_write_failure_fcn_p);
86
87         /* initialize memory allocation subsystems */
88         emem_init();
89         wmem_init();
90
91         /* initialize the GUID to name mapping table */
92         guids_init();
93
94         except_init();
95 #ifdef HAVE_LIBGCRYPT
96         /* initialize libgcrypt (beware, it won't be thread-safe) */
97         gcry_check_version(NULL);
98         gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
99         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
100 #endif
101 #ifdef HAVE_LIBGNUTLS
102         gnutls_global_init();
103 #endif
104         tap_init();
105         prefs_init();
106         expert_init();
107         proto_init(register_all_protocols_func, register_all_handoffs_func,
108             cb, client_data);
109         packet_init();
110         dfilter_init();
111         final_registration_all_protocols();
112         /*host_name_lookup_init();*//* We load the hostname file in cf_open, no need to do it here? */
113         expert_packet_init();
114 #ifdef HAVE_LUA
115         wslua_init(cb, client_data);
116 #endif
117 }
118
119 void
120 epan_cleanup(void)
121 {
122         cleanup_dissection();
123         dfilter_cleanup();
124         proto_cleanup();
125         prefs_cleanup();
126         packet_cleanup();
127         expert_cleanup();
128         oid_resolv_cleanup();
129 #ifdef HAVE_LIBGNUTLS
130         gnutls_global_deinit();
131 #endif
132         except_deinit();
133         host_name_lookup_cleanup();
134         wmem_cleanup();
135 }
136
137 void
138 epan_conversation_init(void)
139 {
140         conversation_init();
141 }
142
143 void
144 epan_conversation_cleanup(void)
145 {
146         conversation_cleanup();
147 }
148
149 void
150 epan_circuit_init(void)
151 {
152         circuit_init();
153 }
154
155 void
156 epan_circuit_cleanup(void)
157 {
158         circuit_cleanup();
159 }
160
161 epan_dissect_t*
162 epan_dissect_init(epan_dissect_t *edt, const gboolean create_proto_tree, const gboolean proto_tree_visible)
163 {
164         g_assert(edt);
165
166         edt->pi.pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
167
168         if (create_proto_tree) {
169                 edt->tree = proto_tree_create_root(&edt->pi);
170                 proto_tree_set_visible(edt->tree, proto_tree_visible);
171         }
172         else {
173                 edt->tree = NULL;
174         }
175
176         edt->pi.dependent_frames = NULL;
177
178         return edt;
179 }
180
181 epan_dissect_t*
182 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
183 {
184         epan_dissect_t *edt;
185
186         edt = g_new0(epan_dissect_t, 1);
187
188         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
189 }
190
191 void
192 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
193 {
194         if (edt)
195                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
196 }
197
198 void
199 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
200         const guint8* data, frame_data *fd, column_info *cinfo)
201 {
202 #ifdef HAVE_LUA
203         wslua_prime_dfilter(edt); /* done before entering wmem scope */
204 #endif
205         wmem_enter_packet_scope();
206         dissect_packet(edt, phdr, data, fd, cinfo);
207
208         /* free all memory allocated */
209         ep_free_all();
210         wmem_leave_packet_scope();
211 }
212
213 void
214 epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
215         const guint8* data, frame_data *fd, column_info *cinfo)
216 {
217         wmem_enter_packet_scope();
218         tap_queue_init(edt);
219         dissect_packet(edt, phdr, data, fd, cinfo);
220         tap_push_tapped_queue(edt);
221
222         /* free all memory allocated */
223         ep_free_all();
224         wmem_leave_packet_scope();
225 }
226
227 void
228 epan_dissect_cleanup(epan_dissect_t* edt)
229 {
230         g_assert(edt);
231
232         g_slist_free(edt->pi.dependent_frames);
233
234         /* Free the data sources list. */
235         free_data_sources(&edt->pi);
236
237         /* Free all tvb's chained from this tvb */
238         tvb_free_chain(edt->tvb);
239
240         if (edt->tree) {
241                 proto_tree_free(edt->tree);
242         }
243
244         wmem_destroy_allocator(edt->pi.pool);
245 }
246
247 void
248 epan_dissect_free(epan_dissect_t* edt)
249 {
250         epan_dissect_cleanup(edt);
251         g_free(edt);
252 }
253
254 void
255 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
256 {
257     dfilter_prime_proto_tree(dfcode, edt->tree);
258 }
259
260 /* ----------------------- */
261 const gchar *
262 epan_custom_set(epan_dissect_t *edt, int field_id,
263                              gint occurrence,
264                              gchar *result,
265                              gchar *expr, const int size )
266 {
267     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
268 }
269
270 void
271 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
272 {
273     col_custom_set_edt(edt, edt->pi.cinfo);
274     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
275 }
276
277 gboolean
278 epan_dissect_packet_contains_field(epan_dissect_t* edt,
279                                    const char *field_name)
280 {
281     GPtrArray* array;
282     int        field_id;
283     gboolean   contains_field;
284
285     if (!edt || !edt->tree)
286         return FALSE;
287     field_id = proto_get_id_by_filter_name(field_name);
288     if (field_id < 0)
289         return FALSE;
290     array = proto_find_finfo(edt->tree, field_id);
291     contains_field = (array->len > 0) ? TRUE : FALSE;
292     g_ptr_array_free(array, TRUE);
293     return contains_field;
294 }
295
296 /*
297  * Get compile-time information for libraries used by libwireshark.
298  */
299 void
300 epan_get_compiled_version_info(GString *str)
301 {
302         /* SNMP */
303         g_string_append(str, ", ");
304 #ifdef HAVE_LIBSMI
305         g_string_append(str, "with SMI " SMI_VERSION_STRING);
306 #else /* no SNMP library */
307         g_string_append(str, "without SMI");
308 #endif /* _SMI_H */
309
310         /* c-ares */
311         g_string_append(str, ", ");
312 #ifdef HAVE_C_ARES
313         g_string_append(str, "with c-ares " ARES_VERSION_STR);
314 #else
315         g_string_append(str, "without c-ares");
316
317         /* ADNS - only add if no c-ares */
318         g_string_append(str, ", ");
319 #ifdef HAVE_GNU_ADNS
320         g_string_append(str, "with ADNS");
321 #else
322         g_string_append(str, "without ADNS");
323 #endif /* HAVE_GNU_ADNS */
324 #endif /* HAVE_C_ARES */
325
326         /* LUA */
327         g_string_append(str, ", ");
328 #ifdef HAVE_LUA
329         g_string_append(str, "with ");
330         g_string_append(str, LUA_VERSION);
331 #else
332         g_string_append(str, "without Lua");
333 #endif /* HAVE_LUA */
334
335         g_string_append(str, ", ");
336 #ifdef HAVE_PYTHON
337         g_string_append(str, "with Python");
338 #ifdef PY_VERSION
339         g_string_append(str, " " PY_VERSION);
340 #endif /* PY_VERSION */
341 #else
342         g_string_append(str, "without Python");
343 #endif /* HAVE_PYTHON */
344
345         /* GnuTLS */
346         g_string_append(str, ", ");
347 #ifdef HAVE_LIBGNUTLS
348         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
349 #else
350         g_string_append(str, "without GnuTLS");
351 #endif /* HAVE_LIBGNUTLS */
352
353         /* Gcrypt */
354         g_string_append(str, ", ");
355 #ifdef HAVE_LIBGCRYPT
356         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
357 #else
358         g_string_append(str, "without Gcrypt");
359 #endif /* HAVE_LIBGCRYPT */
360
361         /* Kerberos */
362         /* XXX - I don't see how to get the version number, at least for KfW */
363         g_string_append(str, ", ");
364 #ifdef HAVE_KERBEROS
365 #ifdef HAVE_MIT_KERBEROS
366         g_string_append(str, "with MIT Kerberos");
367 #else
368         /* HAVE_HEIMDAL_KERBEROS */
369         g_string_append(str, "with Heimdal Kerberos");
370 #endif
371 #else
372         g_string_append(str, "without Kerberos");
373 #endif /* HAVE_KERBEROS */
374
375         /* GeoIP */
376         g_string_append(str, ", ");
377 #ifdef HAVE_GEOIP
378         g_string_append(str, "with GeoIP");
379 #else
380         g_string_append(str, "without GeoIP");
381 #endif /* HAVE_GEOIP */
382
383 }
384
385 /*
386  * Get runtime information for libraries used by libwireshark.
387  */
388 void
389 epan_get_runtime_version_info(GString *str
390 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
391 _U_
392 #endif
393 )
394 {
395         /* GnuTLS */
396 #ifdef HAVE_LIBGNUTLS
397         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
398 #endif /* HAVE_LIBGNUTLS */
399
400         /* Gcrypt */
401 #ifdef HAVE_LIBGCRYPT
402         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
403 #endif /* HAVE_LIBGCRYPT */
404 }
405
406 /*
407  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
408  *
409  * Local variables:
410  * c-basic-offset: 8
411  * tab-width: 8
412  * indent-tabs-mode: t
413  * End:
414  *
415  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
416  * :indentSize=8:tabSize=8:noTabs=false:
417  */