some compilers dont like unnamed unions and structs
[obnox/wireshark/wip.git] / epan / epan.c
1 /* epan.h
2  *
3  * $Id$
4  *
5  * Wireshark Protocol Analyzer Library
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #if (defined(HAVE_LIBGCRYPT) || defined(HAVE_LIBGNUTLS)) && defined(_WIN32)
13 #include <winposixtype.h>
14 #endif
15
16 #ifdef HAVE_LIBGCRYPT
17 #include <gcrypt.h>
18 #endif /* HAVE_LIBGCRYPT */
19
20 #ifdef HAVE_LIBGNUTLS
21 #include <gnutls/gnutls.h>
22 #endif /* HAVE_LIBGNUTLS */
23
24
25 #include <glib.h>
26 #include "epan.h"
27 #include "epan_dissect.h"
28 #include "report_err.h"
29
30 #include "conversation.h"
31 #include "circuit.h"
32 #include "except.h"
33 #include "packet.h"
34 #include "column-utils.h"
35 #include "tap.h"
36 #include "addr_resolv.h"
37 #include "oid_resolv.h"
38 #include "emem.h"
39 #include "expert.h"
40
41 #ifdef HAVE_LUA_5_1
42         int wslua_init(void*);
43 #endif
44
45 static void (*report_failure_func)(const char *, va_list);
46 static void (*report_open_failure_func)(const char *, int, gboolean);
47 static void (*report_read_failure_func)(const char *, int);
48
49 gchar*
50 epan_get_version(void) {
51   return VERSION;
52 }
53
54 /*
55  * XXX - this takes the plugin directory as an argument, because
56  * libwireshark now has its own configure script and "config.h" file,
57  * which is what code in the "epan" directory includes, but we need
58  * to define PLUGIN_DIR in the top-level directory, as it's used by,
59  * for example, the Makefile for the Gryphon plugin, so it knows
60  * where to install the plugin.
61  *
62  * Eventually, we should probably have an "epan-configure" script
63  * (or "libwireshark-configure", or whatever), along the lines of what
64  * GTK+ and GLib have, that can print, among other things, the directory
65  * into which plugins should be installed.  That way, only libwireshark
66  * need know what directory that is; programs using it won't, *and*
67  * Makefiles for plugins can just use "epan-configure" to figure out
68  * where to install the plugins.
69  *
70  * (Would that *more* libraries had configure scripts like that, so
71  * that configure scripts didn't have to go through various contortions
72  * to figure out where the header files and libraries for various
73  * libraries are located.)
74  */
75 void
76 epan_init(const char *plugin_dir, void (*register_all_protocols)(void),
77           void (*register_all_handoffs)(void),
78           void (*report_failure)(const char *, va_list),
79           void (*report_open_failure)(const char *, int, gboolean),
80           void (*report_read_failure)(const char *, int))
81 {
82         report_failure_func = report_failure;
83         report_open_failure_func = report_open_failure;
84         report_read_failure_func = report_read_failure;
85         except_init();
86 #ifdef HAVE_LIBGNUTLS
87         gnutls_global_init();
88 #elif defined(HAVE_LIBGCRYPT)
89         gcry_check_version(NULL);
90 #endif
91         tvbuff_init();
92         oid_resolv_init();
93         tap_init();
94         proto_init(plugin_dir,register_all_protocols,register_all_handoffs);
95         packet_init();
96         dfilter_init();
97         final_registration_all_protocols();
98         host_name_lookup_init();
99         expert_init();
100 #ifdef HAVE_LUA_5_1
101         wslua_init(NULL);
102 #endif
103
104 }
105
106 void
107 epan_cleanup(void)
108 {
109         expert_cleanup();
110         dfilter_cleanup();
111         proto_cleanup();
112         packet_cleanup();
113         oid_resolv_cleanup();
114         tvbuff_cleanup();
115 #ifdef HAVE_LIBGNUTLS
116         gnutls_global_deinit();
117 #endif
118         except_deinit();
119         host_name_lookup_cleanup();
120 }
121
122 void
123 epan_conversation_init(void)
124 {
125         conversation_init();
126 }
127
128 void
129 epan_circuit_init(void)
130 {
131         circuit_init();
132 }
133
134 /*
135  * Report a general error.
136  */
137 void
138 report_failure(const char *msg_format, ...)
139 {
140         va_list ap;
141
142         va_start(ap, msg_format);
143         (*report_failure_func)(msg_format, ap);
144         va_end(ap);
145 }
146
147 /*
148  * Report an error when trying to open or create a file.
149  * "err" is assumed to be an error code from Wiretap; positive values are
150  * UNIX-style errnos, so this can be used for open failures not from
151  * Wiretap as long as the failue code is just an errno.
152  */
153 void
154 report_open_failure(const char *filename, int err,
155     gboolean for_writing)
156 {
157         (*report_open_failure_func)(filename, err, for_writing);
158 }
159
160 /*
161  * Report an error when trying to read a file.
162  * "err" is assumed to be a UNIX-style errno.
163  */
164 void
165 report_read_failure(const char *filename, int err)
166 {
167         (*report_read_failure_func)(filename, err);
168 }
169
170 epan_dissect_t*
171 epan_dissect_new(gboolean create_proto_tree, gboolean proto_tree_visible)
172 {
173         epan_dissect_t  *edt;
174
175         edt = g_new(epan_dissect_t, 1);
176
177         if (create_proto_tree) {
178                 edt->tree = proto_tree_create_root();
179                 proto_tree_set_visible(edt->tree, proto_tree_visible);
180         }
181         else {
182                 edt->tree = NULL;
183         }
184
185         return edt;
186 }
187
188 void
189 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
190         const guint8* data, frame_data *fd, column_info *cinfo)
191 {
192         /* free all memory allocated during previous packet */
193         ep_free_all();
194
195         dissect_packet(edt, pseudo_header, data, fd, cinfo);
196 }
197
198
199 void
200 epan_dissect_free(epan_dissect_t* edt)
201 {
202         /* Free the data sources list. */
203         free_data_sources(&edt->pi);
204
205         /* Free all tvb's created from this tvb, unless dissector
206          * wanted to store the pointer (in which case, the dissector
207          * would have incremented the usage count on that tvbuff_t*) */
208         tvb_free_chain(edt->tvb);
209
210         if (edt->tree) {
211                 proto_tree_free(edt->tree);
212         }
213
214         g_free(edt);
215 }
216
217 void
218 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
219 {
220         dfilter_prime_proto_tree(dfcode, edt->tree);
221 }
222
223 void
224 epan_dissect_fill_in_columns(epan_dissect_t *edt)
225 {
226     col_fill_in(&edt->pi);
227 }