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