c61260a65fe5d0cf558def0f1526b1c159b0ad27
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifdef HAVE_PYTHON
29 #include <Python.h> /* to get the Python version number (PY_VERSION) */
30 #endif
31
32 #ifdef HAVE_LIBGCRYPT
33 #include <gcrypt.h>
34 #endif /* HAVE_LIBGCRYPT */
35
36 #ifdef HAVE_LIBGNUTLS
37 #include <gnutls/gnutls.h>
38 #endif /* HAVE_LIBGNUTLS */
39
40 #include <glib.h>
41 #include "epan.h"
42 #include "epan_dissect.h"
43 #include "report_err.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 "expert.h"
56
57 #ifdef HAVE_LUA_5_1
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 #ifdef HAVE_GEOIP
71 #include "geoip_db.h"
72 #endif
73
74 const gchar*
75 epan_get_version(void) {
76         return VERSION;
77 }
78
79 void
80 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
81           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
82           register_cb cb,
83           gpointer client_data,
84           void (*report_failure_fcn_p)(const char *, va_list),
85           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
86           void (*report_read_failure_fcn_p)(const char *, int),
87           void (*report_write_failure_fcn_p)(const char *, int))
88 {
89         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
90             report_read_failure_fcn_p, report_write_failure_fcn_p);
91
92         /* initialize memory allocation subsystem */
93         emem_init();
94
95         /* initialize the GUID to name mapping table */
96         guids_init();
97
98         except_init();
99 #ifdef HAVE_LIBGNUTLS
100         gnutls_global_init();
101 #elif defined(HAVE_LIBGCRYPT)
102         gcry_check_version(NULL);
103 #endif
104         tap_init();
105         prefs_init();
106         proto_init(register_all_protocols_func, register_all_handoffs_func,
107             cb, client_data);
108         packet_init();
109         dfilter_init();
110         final_registration_all_protocols();
111         host_name_lookup_init();
112         expert_init();
113 #ifdef HAVE_LUA_5_1
114         wslua_init(cb, client_data);
115 #endif
116 #ifdef HAVE_GEOIP
117         geoip_db_init();
118 #endif
119
120 }
121
122 void
123 epan_cleanup(void)
124 {
125         cleanup_dissection();
126         dfilter_cleanup();
127         proto_cleanup();
128         prefs_cleanup();
129         packet_cleanup();
130         oid_resolv_cleanup();
131 #ifdef HAVE_LIBGNUTLS
132         gnutls_global_deinit();
133 #endif
134         except_deinit();
135         host_name_lookup_cleanup();
136 }
137
138 void
139 epan_conversation_init(void)
140 {
141         conversation_init();
142 }
143
144 void
145 epan_conversation_cleanup(void)
146 {
147         conversation_cleanup();
148 }
149
150 void
151 epan_circuit_init(void)
152 {
153         circuit_init();
154 }
155
156 void
157 epan_circuit_cleanup(void)
158 {
159         circuit_cleanup();
160 }
161
162 epan_dissect_t*
163 epan_dissect_init(epan_dissect_t *edt, const gboolean create_proto_tree, const gboolean proto_tree_visible)
164 {
165         g_assert(edt);
166
167         if (create_proto_tree) {
168                 edt->tree = proto_tree_create_root();
169                 proto_tree_set_visible(edt->tree, proto_tree_visible);
170         }
171         else {
172                 edt->tree = NULL;
173         }
174
175         edt->pi.dependent_frames = NULL;
176
177         return edt;
178 }
179
180 epan_dissect_t*
181 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
182 {
183         epan_dissect_t *edt;
184
185         edt = g_new0(epan_dissect_t, 1);
186
187         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
188 }
189
190 void
191 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
192 {
193         if (edt)
194                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
195 }
196
197 void
198 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
199         const guint8* data, frame_data *fd, column_info *cinfo)
200 {
201         /* free all memory allocated during previous packet */
202         ep_free_all();
203
204         dissect_packet(edt, pseudo_header, data, fd, cinfo);
205 }
206
207 void
208 epan_dissect_cleanup(epan_dissect_t* edt)
209 {
210         g_assert(edt);
211
212         /* Free the data sources list. */
213         free_data_sources(&edt->pi);
214
215         /* Free all tvb's chained from this tvb */
216         tvb_free_chain(edt->tvb);
217
218         if (edt->tree) {
219                 proto_tree_free(edt->tree);
220         }
221 }
222
223 void
224 epan_dissect_free(epan_dissect_t* edt)
225 {
226         epan_dissect_cleanup(edt);
227         g_free(edt);
228 }
229
230 void
231 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
232 {
233     dfilter_prime_proto_tree(dfcode, edt->tree);
234 }
235
236 /* ----------------------- */
237 const gchar *
238 epan_custom_set(epan_dissect_t *edt, int field_id,
239                              gint occurrence,
240                              gchar *result,
241                              gchar *expr, const int size )
242 {
243     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
244 }
245
246 void
247 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
248 {
249     col_custom_set_edt(edt, edt->pi.cinfo);
250     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
251 }
252
253 /*
254  * Get compile-time information for libraries used by libwireshark.
255  */
256 void
257 epan_get_compiled_version_info(GString *str)
258 {
259         /* SNMP */
260         g_string_append(str, ", ");
261 #ifdef HAVE_LIBSMI
262         g_string_append(str, "with SMI " SMI_VERSION_STRING);
263 #else /* no SNMP library */
264         g_string_append(str, "without SMI");
265 #endif /* _SMI_H */
266
267         /* c-ares */
268         g_string_append(str, ", ");
269 #ifdef HAVE_C_ARES
270         g_string_append(str, "with c-ares " ARES_VERSION_STR);
271 #else
272         g_string_append(str, "without c-ares");
273
274         /* ADNS - only add if no c-ares */
275         g_string_append(str, ", ");
276 #ifdef HAVE_GNU_ADNS
277         g_string_append(str, "with ADNS");
278 #else
279         g_string_append(str, "without ADNS");
280 #endif /* HAVE_GNU_ADNS */
281 #endif /* HAVE_C_ARES */
282
283         /* LUA */
284         g_string_append(str, ", ");
285 #ifdef HAVE_LUA_5_1
286         g_string_append(str, "with ");
287         g_string_append(str, LUA_VERSION);
288 #else
289         g_string_append(str, "without Lua");
290 #endif /* HAVE_LUA_5_1 */
291
292         g_string_append(str, ", ");
293 #ifdef HAVE_PYTHON
294         g_string_append(str, "with Python");
295 #ifdef PY_VERSION
296         g_string_append(str, " " PY_VERSION);
297 #endif /* PY_VERSION */
298 #else
299         g_string_append(str, "without Python");
300 #endif /* HAVE_PYTHON */
301
302         /* GnuTLS */
303         g_string_append(str, ", ");
304 #ifdef HAVE_LIBGNUTLS
305         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
306 #else
307         g_string_append(str, "without GnuTLS");
308 #endif /* HAVE_LIBGNUTLS */
309
310         /* Gcrypt */
311         g_string_append(str, ", ");
312 #ifdef HAVE_LIBGCRYPT
313         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
314 #else
315         g_string_append(str, "without Gcrypt");
316 #endif /* HAVE_LIBGCRYPT */
317
318         /* Kerberos */
319         /* XXX - I don't see how to get the version number, at least for KfW */
320         g_string_append(str, ", ");
321 #ifdef HAVE_KERBEROS
322 #ifdef HAVE_MIT_KERBEROS
323         g_string_append(str, "with MIT Kerberos");
324 #else
325         /* HAVE_HEIMDAL_KERBEROS */
326         g_string_append(str, "with Heimdal Kerberos");
327 #endif
328 #else
329         g_string_append(str, "without Kerberos");
330 #endif /* HAVE_KERBEROS */
331
332         /* GeoIP */
333         g_string_append(str, ", ");
334 #ifdef HAVE_GEOIP
335         g_string_append(str, "with GeoIP");
336 #else
337         g_string_append(str, "without GeoIP");
338 #endif /* HAVE_GEOIP */
339
340 }
341
342 /*
343  * Get runtime information for libraries used by libwireshark.
344  */
345 void
346 epan_get_runtime_version_info(GString *str
347 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
348 _U_
349 #endif
350 )
351 {
352         /* GnuTLS */
353 #ifdef HAVE_LIBGNUTLS
354         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
355 #endif /* HAVE_LIBGNUTLS */
356
357         /* Gcrypt */
358 #ifdef HAVE_LIBGCRYPT
359         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
360 #endif /* HAVE_LIBGCRYPT */
361 }