Move the code to get version information for libraries used by
[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_LIBGCRYPT
29 #include <gcrypt.h>
30 #endif /* HAVE_LIBGCRYPT */
31
32 #ifdef HAVE_LIBGNUTLS
33 #include <gnutls/gnutls.h>
34 #endif /* HAVE_LIBGNUTLS */
35
36
37 #include <glib.h>
38 #include "epan.h"
39 #include "epan_dissect.h"
40 #include "report_err.h"
41
42 #include "conversation.h"
43 #include "circuit.h"
44 #include "except.h"
45 #include "packet.h"
46 #include "prefs.h"
47 #include "column-utils.h"
48 #include "tap.h"
49 #include "addr_resolv.h"
50 #include "oids.h"
51 #include "emem.h"
52 #include "expert.h"
53
54 #ifdef HAVE_LUA_5_1
55 #include <lua.h>
56 #include <wslua/wslua.h>
57 #endif
58
59 #ifdef HAVE_LIBSMI
60 #include <smi.h>
61 #endif
62
63 #ifdef HAVE_C_ARES
64 #include <ares_version.h>
65 #endif
66
67 #ifdef HAVE_GEOIP
68 #include "geoip_db.h"
69 #endif
70
71 gchar*
72 epan_get_version(void) {
73   return VERSION;
74 }
75
76 void
77 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
78           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
79           register_cb cb,
80           gpointer client_data,
81           void (*report_failure_fcn_p)(const char *, va_list),
82           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
83           void (*report_read_failure_fcn_p)(const char *, int),
84           void (*report_write_failure_fcn_p)(const char *, int))
85 {
86         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
87             report_read_failure_fcn_p, report_write_failure_fcn_p);
88
89         /* initialize memory allocation subsystem */
90         emem_init();
91
92         /* initialize the GUID to name mapping table */
93         guids_init();
94
95         except_init();
96 #ifdef HAVE_LIBGNUTLS
97         gnutls_global_init();
98 #elif defined(HAVE_LIBGCRYPT)
99         gcry_check_version(NULL);
100 #endif
101         tvbuff_init();
102         tap_init();
103         prefs_init();
104         proto_init(register_all_protocols_func, register_all_handoffs_func,
105             cb, client_data);
106         packet_init();
107         dfilter_init();
108         final_registration_all_protocols();
109         host_name_lookup_init();
110         expert_init();
111 #ifdef HAVE_LUA_5_1
112         wslua_init(NULL);
113 #endif
114 #ifdef HAVE_GEOIP
115         geoip_db_init();
116 #endif
117
118 }
119
120 void
121 epan_cleanup(void)
122 {
123         cleanup_dissection();
124         dfilter_cleanup();
125         proto_cleanup();
126         prefs_cleanup();
127         packet_cleanup();
128         oid_resolv_cleanup();
129         tvbuff_cleanup();
130 #ifdef HAVE_LIBGNUTLS
131         gnutls_global_deinit();
132 #endif
133         except_deinit();
134         host_name_lookup_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         if (create_proto_tree) {
167                 edt->tree = proto_tree_create_root();
168                 proto_tree_set_visible(edt->tree, proto_tree_visible);
169         }
170         else {
171                 edt->tree = NULL;
172         }
173
174         return edt;
175 }
176
177 epan_dissect_t*
178 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
179 {
180         epan_dissect_t  *edt;
181
182         edt = g_new0(epan_dissect_t, 1);
183
184         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
185 }
186
187 void
188 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
189 {
190         if (edt)
191                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
192 }
193
194 void
195 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
196         const guint8* data, frame_data *fd, column_info *cinfo)
197 {
198         /* free all memory allocated during previous packet */
199         ep_free_all();
200
201         dissect_packet(edt, pseudo_header, data, fd, cinfo);
202 }
203
204 void
205 epan_dissect_cleanup(epan_dissect_t* edt)
206 {
207         g_assert(edt);
208
209         /* Free the data sources list. */
210         free_data_sources(&edt->pi);
211
212         /* Free all tvb's created from this tvb, unless dissector
213          * wanted to store the pointer (in which case, the dissector
214          * would have incremented the usage count on that tvbuff_t*) */
215         tvb_free_chain(edt->tvb);
216
217         if (edt->tree) {
218                 proto_tree_free(edt->tree);
219         }
220 }
221
222 void
223 epan_dissect_free(epan_dissect_t* edt)
224 {
225         epan_dissect_cleanup(edt);
226         g_free(edt);
227 }
228
229 void
230 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
231 {
232     dfilter_prime_proto_tree(dfcode, edt->tree);
233 }
234
235 /* ----------------------- */
236 const gchar *
237 epan_custom_set(epan_dissect_t *edt, int field_id,
238                              gchar *result,
239                              gchar *expr, const int size )
240 {
241     return proto_custom_set(edt->tree, field_id, result, expr, size);
242 }
243
244 void
245 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
246 {
247     col_custom_set_edt(edt, edt->pi.cinfo);
248     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
249 }
250
251 /*
252  * Get compile-time information for libraries used by libwireshark.
253  */
254 void
255 epan_get_compiled_version_info(GString *str)
256 {
257         /* PCRE */
258         g_string_append(str, ", ");
259 #ifdef HAVE_LIBPCRE
260         g_string_append(str, "with libpcre ");
261 #ifdef PCRE_MAJOR
262 #ifdef PCRE_MINOR
263         g_string_append_printf(str, "%u.%u", PCRE_MAJOR, PCRE_MINOR);
264 #else                   /* PCRE_MINOR */
265         g_string_append_printf(str, "%u", PCRE_MAJOR);
266 #endif                  /* PCRE_MINOR */
267 #else           /* PCRE_MAJOR */
268         g_string_append(str, "(version unknown)");
269 #endif          /* PCRE_MAJOR */
270 #else   /* HAVE_LIBPCRE */
271         g_string_append(str, "without libpcre");
272 #endif  /* HAVE_LIBPCRE */
273
274         /* SNMP */
275         g_string_append(str, ", ");
276 #ifdef HAVE_LIBSMI
277         g_string_append(str, "with SMI " SMI_VERSION_STRING);
278 #else /* no SNMP library */
279         g_string_append(str, "without SMI");
280 #endif /* _SMI_H */
281
282         /* c-ares */
283         g_string_append(str, ", ");
284 #ifdef HAVE_C_ARES
285         g_string_append(str, "with c-ares " ARES_VERSION_STR);
286 #else
287         g_string_append(str, "without c-ares");
288
289         /* ADNS - only add if no c-ares */
290         g_string_append(str, ", ");
291 #ifdef HAVE_GNU_ADNS
292         g_string_append(str, "with ADNS");
293 #else
294         g_string_append(str, "without ADNS");
295 #endif /* HAVE_GNU_ADNS */
296 #endif /* HAVE_C_ARES */
297
298         /* LUA */
299         g_string_append(str, ", ");
300 #ifdef HAVE_LUA_5_1
301         g_string_append(str, "with ");
302         g_string_append(str, LUA_VERSION);
303 #else
304         g_string_append(str, "without Lua");
305 #endif /* HAVE_LUA_5_1 */
306
307         g_string_append(str, ", ");
308 #ifdef HAVE_PYTHON
309         g_string_append(str, "with Python");
310 #ifdef PY_VERSION
311         g_string_append(str, " " PY_VERSION);
312 #endif /* PY_VERSION */
313 #else
314         g_string_append(str, "without Python");
315 #endif /* HAVE_PYTHON */
316
317         /* GnuTLS */
318         g_string_append(str, ", ");
319 #ifdef HAVE_LIBGNUTLS
320         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
321 #else
322         g_string_append(str, "without GnuTLS");
323 #endif /* HAVE_LIBGNUTLS */
324
325         /* Gcrypt */
326         g_string_append(str, ", ");
327 #ifdef HAVE_LIBGCRYPT
328         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
329 #else
330         g_string_append(str, "without Gcrypt");
331 #endif /* HAVE_LIBGCRYPT */
332
333         /* Kerberos */
334         /* XXX - I don't see how to get the version number, at least for KfW */
335         g_string_append(str, ", ");
336 #ifdef HAVE_KERBEROS
337 #ifdef HAVE_MIT_KERBEROS
338         g_string_append(str, "with MIT Kerberos");
339 #else
340         /* HAVE_HEIMDAL_KERBEROS */
341         g_string_append(str, "with Heimdal Kerberos");
342 #endif
343 #else
344         g_string_append(str, "without Kerberos");
345 #endif /* HAVE_KERBEROS */
346
347         /* GeoIP */
348         g_string_append(str, ", ");
349 #ifdef HAVE_GEOIP
350         g_string_append(str, "with GeoIP");
351 #else
352         g_string_append(str, "without GeoIP");
353 #endif /* HAVE_GEOIP */
354
355 }
356
357 /*
358  * Get runtime information for libraries used by libwireshark.
359  */
360 void
361 epan_get_runtime_version_info(GString *str)
362 {
363         /* GnuTLS */
364 #ifdef HAVE_LIBGNUTLS
365         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
366 #endif /* HAVE_LIBGNUTLS */
367
368         /* Gcrypt */
369 #ifdef HAVE_LIBGCRYPT
370         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
371 #endif /* HAVE_LIBGCRYPT */
372 }