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