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