From Yaniv Kaul: constify parameters
[obnox/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         int wslua_init(void*);
56 #endif
57
58 #ifdef HAVE_GEOIP
59 #include "geoip_db.h"
60 #endif
61
62 gchar*
63 epan_get_version(void) {
64   return VERSION;
65 }
66
67 void
68 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
69           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
70           register_cb cb,
71           gpointer client_data,
72           void (*report_failure_fcn_p)(const char *, va_list),
73           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
74           void (*report_read_failure_fcn_p)(const char *, int),
75           void (*report_write_failure_fcn_p)(const char *, int))
76 {
77         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
78             report_read_failure_fcn_p, report_write_failure_fcn_p);
79
80         /* initialize memory allocation subsystem */
81         emem_init();
82
83         /* initialize the GUID to name mapping table */
84         guids_init();
85
86         except_init();
87 #ifdef HAVE_LIBGNUTLS
88         gnutls_global_init();
89 #elif defined(HAVE_LIBGCRYPT)
90         gcry_check_version(NULL);
91 #endif
92         tvbuff_init();
93         tap_init();
94         prefs_init();
95         proto_init(register_all_protocols_func, register_all_handoffs_func,
96             cb, client_data);
97         packet_init();
98         dfilter_init();
99         final_registration_all_protocols();
100         host_name_lookup_init();
101         expert_init();
102 #ifdef HAVE_LUA_5_1
103         wslua_init(NULL);
104 #endif
105 #ifdef HAVE_GEOIP
106         geoip_db_init();
107 #endif
108
109 }
110
111 void
112 epan_cleanup(void)
113 {
114         cleanup_dissection();
115         dfilter_cleanup();
116         proto_cleanup();
117         prefs_cleanup();
118         packet_cleanup();
119         oid_resolv_cleanup();
120         tvbuff_cleanup();
121 #ifdef HAVE_LIBGNUTLS
122         gnutls_global_deinit();
123 #endif
124         except_deinit();
125         host_name_lookup_cleanup();
126 }
127
128 void
129 epan_conversation_init(void)
130 {
131         conversation_init();
132 }
133
134 void
135 epan_conversation_cleanup(void)
136 {
137         conversation_cleanup();
138 }
139
140 void
141 epan_circuit_init(void)
142 {
143         circuit_init();
144 }
145
146 void
147 epan_circuit_cleanup(void)
148 {
149         circuit_cleanup();
150 }
151
152 epan_dissect_t*
153 epan_dissect_init(epan_dissect_t        *edt, const gboolean create_proto_tree, const gboolean proto_tree_visible)
154 {
155         g_assert(edt);
156
157         if (create_proto_tree) {
158                 edt->tree = proto_tree_create_root();
159                 proto_tree_set_visible(edt->tree, proto_tree_visible);
160         }
161         else {
162                 edt->tree = NULL;
163         }
164
165         return edt;
166 }
167
168 epan_dissect_t*
169 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
170 {
171         epan_dissect_t  *edt;
172
173         edt = g_new(epan_dissect_t, 1);
174
175         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
176 }
177
178 void
179 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
180 {
181         if (edt)
182                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
183 }
184
185 void
186 epan_dissect_run(epan_dissect_t *edt, void* pseudo_header,
187         const guint8* data, frame_data *fd, column_info *cinfo)
188 {
189         /* free all memory allocated during previous packet */
190         ep_free_all();
191
192         dissect_packet(edt, pseudo_header, data, fd, cinfo);
193 }
194
195 void
196 epan_dissect_cleanup(epan_dissect_t* edt)
197 {
198         g_assert(edt);
199
200         /* Free the data sources list. */
201         free_data_sources(&edt->pi);
202
203         /* Free all tvb's created from this tvb, unless dissector
204          * wanted to store the pointer (in which case, the dissector
205          * would have incremented the usage count on that tvbuff_t*) */
206         tvb_free_chain(edt->tvb);
207
208         if (edt->tree) {
209                 proto_tree_free(edt->tree);
210         }
211 }
212
213 void
214 epan_dissect_free(epan_dissect_t* edt)
215 {
216         epan_dissect_cleanup(edt);
217         g_free(edt);
218 }
219
220 void
221 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
222 {
223     dfilter_prime_proto_tree(dfcode, edt->tree);
224 }
225
226 /* ----------------------- */
227 const gchar *
228 epan_custom_set(epan_dissect_t *edt, int field_id,
229                              gchar *result,
230                              gchar *expr, const int size )
231 {
232     return proto_custom_set(edt->tree, field_id, result, expr, size);
233 }
234
235 void
236 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
237 {
238     col_custom_set_edt(edt, edt->pi.cinfo);
239     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
240 }
241