As suggested in https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9033
[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-int.h"
40 #include "epan.h"
41 #include "epan_dissect.h"
42 #include "wsutil/report_err.h"
43
44 #include "conversation.h"
45 #include "circuit.h"
46 #include "except.h"
47 #include "packet.h"
48 #include "prefs.h"
49 #include "column-utils.h"
50 #include "tap.h"
51 #include "addr_resolv.h"
52 #include "oids.h"
53 #include "emem.h"
54 #include "wmem/wmem.h"
55 #include "expert.h"
56
57 #ifdef HAVE_LUA
58 #include <lua.h>
59 #include <wslua/wslua.h>
60 #endif
61
62 #ifdef HAVE_LIBSMI
63 #include <smi.h>
64 #endif
65
66 #ifdef HAVE_C_ARES
67 #include <ares_version.h>
68 #endif
69
70 const gchar*
71 epan_get_version(void) {
72         return VERSION;
73 }
74
75 void
76 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
77           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
78           register_cb cb,
79           gpointer client_data,
80           void (*report_failure_fcn_p)(const char *, va_list),
81           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
82           void (*report_read_failure_fcn_p)(const char *, int),
83           void (*report_write_failure_fcn_p)(const char *, int))
84 {
85         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
86             report_read_failure_fcn_p, report_write_failure_fcn_p);
87
88         /* initialize memory allocation subsystems */
89         emem_init();
90         wmem_init();
91
92         /* initialize the GUID to name mapping table */
93         guids_init();
94
95         /* initialize name resolution (addr_resolv.c) */
96         addr_resolv_init();
97
98         except_init();
99 #ifdef HAVE_LIBGCRYPT
100         /* initialize libgcrypt (beware, it won't be thread-safe) */
101         gcry_check_version(NULL);
102         gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
103         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
104 #endif
105 #ifdef HAVE_LIBGNUTLS
106         gnutls_global_init();
107 #endif
108         tap_init();
109         prefs_init();
110         expert_init();
111         packet_init();
112         proto_init(register_all_protocols_func, register_all_handoffs_func,
113             cb, client_data);
114         packet_cache_proto_handles();
115         dfilter_init();
116         final_registration_all_protocols();
117         expert_packet_init();
118 #ifdef HAVE_LUA
119         wslua_init(cb, client_data);
120 #endif
121 }
122
123 void
124 epan_cleanup(void)
125 {
126         dfilter_cleanup();
127         proto_cleanup();
128         prefs_cleanup();
129         packet_cleanup();
130         expert_cleanup();
131         oid_resolv_cleanup();
132 #ifdef HAVE_LIBGNUTLS
133         gnutls_global_deinit();
134 #endif
135         except_deinit();
136         addr_resolv_cleanup();
137         wmem_cleanup();
138 }
139
140 epan_t *
141 epan_new(void)
142 {
143         epan_t *session = g_slice_new(epan_t);
144
145         /* XXX, it should take session as param */
146         init_dissection();
147
148         return session;
149 }
150
151 const char *
152 epan_get_user_comment(const epan_t *session, const frame_data *fd)
153 {
154         if (session->get_user_comment)
155                 return session->get_user_comment(session->data, fd);
156
157         return NULL;
158 }
159
160 const char *
161 epan_get_interface_name(const epan_t *session, guint32 interface_id)
162 {
163         if (session->get_interface_name)
164                 return session->get_interface_name(session->data, interface_id);
165
166         return NULL;
167 }
168
169 const nstime_t *
170 epan_get_frame_ts(const epan_t *session, guint32 frame_num)
171 {
172         const nstime_t *abs_ts = NULL;
173
174         if (session->get_frame_ts)
175                 abs_ts = session->get_frame_ts(session->data, frame_num);
176
177         if (!abs_ts)
178                 g_warning("!!! couldn't get frame ts for %u !!!\n", frame_num);
179
180         return abs_ts;
181 }
182
183 void
184 epan_free(epan_t *session)
185 {
186         if (session) {
187                 /* XXX, it should take session as param */
188                 cleanup_dissection();
189
190                 g_slice_free(epan_t, session);
191         }
192 }
193
194 void
195 epan_conversation_init(void)
196 {
197         conversation_init();
198 }
199
200 void
201 epan_conversation_cleanup(void)
202 {
203         conversation_cleanup();
204 }
205
206 void
207 epan_circuit_init(void)
208 {
209         circuit_init();
210 }
211
212 void
213 epan_circuit_cleanup(void)
214 {
215         circuit_cleanup();
216 }
217
218 epan_dissect_t*
219 epan_dissect_init(epan_dissect_t *edt, epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
220 {
221         g_assert(edt);
222
223         edt->session = session;
224         edt->pi.pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
225
226         if (create_proto_tree) {
227                 edt->tree = proto_tree_create_root(&edt->pi);
228                 proto_tree_set_visible(edt->tree, proto_tree_visible);
229         }
230         else {
231                 edt->tree = NULL;
232         }
233
234         edt->pi.dependent_frames = NULL;
235
236         return edt;
237 }
238
239 epan_dissect_t*
240 epan_dissect_new(epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
241 {
242         epan_dissect_t *edt;
243
244         edt = g_new0(epan_dissect_t, 1);
245
246         return epan_dissect_init(edt, session, create_proto_tree, proto_tree_visible);
247 }
248
249 void
250 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
251 {
252         if (edt)
253                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
254 }
255
256 void
257 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
258         tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
259 {
260 #ifdef HAVE_LUA
261         wslua_prime_dfilter(edt); /* done before entering wmem scope */
262 #endif
263         wmem_enter_packet_scope();
264         dissect_packet(edt, phdr, tvb, fd, cinfo);
265
266         /* free all memory allocated */
267         ep_free_all();
268         wmem_leave_packet_scope();
269 }
270
271 void
272 epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
273         tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
274 {
275         wmem_enter_packet_scope();
276         tap_queue_init(edt);
277         dissect_packet(edt, phdr, tvb, fd, cinfo);
278         tap_push_tapped_queue(edt);
279
280         /* free all memory allocated */
281         ep_free_all();
282         wmem_leave_packet_scope();
283 }
284
285 void
286 epan_dissect_cleanup(epan_dissect_t* edt)
287 {
288         g_assert(edt);
289
290         g_slist_free(edt->pi.dependent_frames);
291
292         /* Free the data sources list. */
293         free_data_sources(&edt->pi);
294
295         /* Free all tvb's chained from this tvb */
296         tvb_free_chain(edt->tvb);
297
298         if (edt->tree) {
299                 proto_tree_free(edt->tree);
300         }
301
302         wmem_destroy_allocator(edt->pi.pool);
303 }
304
305 void
306 epan_dissect_free(epan_dissect_t* edt)
307 {
308         epan_dissect_cleanup(edt);
309         g_free(edt);
310 }
311
312 void
313 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
314 {
315     dfilter_prime_proto_tree(dfcode, edt->tree);
316 }
317
318 /* ----------------------- */
319 const gchar *
320 epan_custom_set(epan_dissect_t *edt, int field_id,
321                              gint occurrence,
322                              gchar *result,
323                              gchar *expr, const int size )
324 {
325     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
326 }
327
328 void
329 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
330 {
331     col_custom_set_edt(edt, edt->pi.cinfo);
332     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
333 }
334
335 gboolean
336 epan_dissect_packet_contains_field(epan_dissect_t* edt,
337                                    const char *field_name)
338 {
339     GPtrArray* array;
340     int        field_id;
341     gboolean   contains_field;
342
343     if (!edt || !edt->tree)
344         return FALSE;
345     field_id = proto_get_id_by_filter_name(field_name);
346     if (field_id < 0)
347         return FALSE;
348     array = proto_find_finfo(edt->tree, field_id);
349     contains_field = (array->len > 0) ? TRUE : FALSE;
350     g_ptr_array_free(array, TRUE);
351     return contains_field;
352 }
353
354 /*
355  * Get compile-time information for libraries used by libwireshark.
356  */
357 void
358 epan_get_compiled_version_info(GString *str)
359 {
360         /* SNMP */
361         g_string_append(str, ", ");
362 #ifdef HAVE_LIBSMI
363         g_string_append(str, "with SMI " SMI_VERSION_STRING);
364 #else /* no SNMP library */
365         g_string_append(str, "without SMI");
366 #endif /* _SMI_H */
367
368         /* c-ares */
369         g_string_append(str, ", ");
370 #ifdef HAVE_C_ARES
371         g_string_append(str, "with c-ares " ARES_VERSION_STR);
372 #else
373         g_string_append(str, "without c-ares");
374
375         /* ADNS - only add if no c-ares */
376         g_string_append(str, ", ");
377 #ifdef HAVE_GNU_ADNS
378         g_string_append(str, "with ADNS");
379 #else
380         g_string_append(str, "without ADNS");
381 #endif /* HAVE_GNU_ADNS */
382 #endif /* HAVE_C_ARES */
383
384         /* LUA */
385         g_string_append(str, ", ");
386 #ifdef HAVE_LUA
387         g_string_append(str, "with ");
388         g_string_append(str, LUA_VERSION);
389 #else
390         g_string_append(str, "without Lua");
391 #endif /* HAVE_LUA */
392
393         g_string_append(str, ", ");
394 #ifdef HAVE_PYTHON
395         g_string_append(str, "with Python");
396 #ifdef PY_VERSION
397         g_string_append(str, " " PY_VERSION);
398 #endif /* PY_VERSION */
399 #else
400         g_string_append(str, "without Python");
401 #endif /* HAVE_PYTHON */
402
403         /* GnuTLS */
404         g_string_append(str, ", ");
405 #ifdef HAVE_LIBGNUTLS
406         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
407 #else
408         g_string_append(str, "without GnuTLS");
409 #endif /* HAVE_LIBGNUTLS */
410
411         /* Gcrypt */
412         g_string_append(str, ", ");
413 #ifdef HAVE_LIBGCRYPT
414         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
415 #else
416         g_string_append(str, "without Gcrypt");
417 #endif /* HAVE_LIBGCRYPT */
418
419         /* Kerberos */
420         /* XXX - I don't see how to get the version number, at least for KfW */
421         g_string_append(str, ", ");
422 #ifdef HAVE_KERBEROS
423 #ifdef HAVE_MIT_KERBEROS
424         g_string_append(str, "with MIT Kerberos");
425 #else
426         /* HAVE_HEIMDAL_KERBEROS */
427         g_string_append(str, "with Heimdal Kerberos");
428 #endif
429 #else
430         g_string_append(str, "without Kerberos");
431 #endif /* HAVE_KERBEROS */
432
433         /* GeoIP */
434         g_string_append(str, ", ");
435 #ifdef HAVE_GEOIP
436         g_string_append(str, "with GeoIP");
437 #else
438         g_string_append(str, "without GeoIP");
439 #endif /* HAVE_GEOIP */
440
441 }
442
443 /*
444  * Get runtime information for libraries used by libwireshark.
445  */
446 void
447 epan_get_runtime_version_info(GString *str
448 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
449 _U_
450 #endif
451 )
452 {
453         /* GnuTLS */
454 #ifdef HAVE_LIBGNUTLS
455         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
456 #endif /* HAVE_LIBGNUTLS */
457
458         /* Gcrypt */
459 #ifdef HAVE_LIBGCRYPT
460         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
461 #endif /* HAVE_LIBGCRYPT */
462 }
463
464 /*
465  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
466  *
467  * Local variables:
468  * c-basic-offset: 8
469  * tab-width: 8
470  * indent-tabs-mode: t
471  * End:
472  *
473  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
474  * :indentSize=8:tabSize=8:noTabs=false:
475  */