When registering a dissector table, choose what type of hash and
[obnox/wireshark/wip.git] / epan / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.69 2003/09/06 23:37:02 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __PACKET_H__
26 #define __PACKET_H__
27
28 #include "wiretap/wtap.h"
29 #include "proto.h"
30 #include "tvbuff.h"
31 #include "pint.h"
32 #include "to_str.h"
33 #include "value_string.h"
34 #include "column_info.h"
35 #include "frame_data.h"
36 #include "packet_info.h"
37 #include "column-utils.h"
38 #include "epan.h"
39
40 #define hi_nibble(b) (((b) & 0xf0) >> 4)
41 #define lo_nibble(b) ((b) & 0x0f)
42
43 /* Useful when you have an array whose size you can tell at compile-time */
44 #define array_length(x) (sizeof x / sizeof x[0])
45
46 /* Check whether the "len" bytes of data starting at "offset" is
47  * entirely inside the captured data for this packet. */
48 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
49         ((guint)(offset) + (guint)(len) > (guint)(offset) && \
50          (guint)(offset) + (guint)(len) <= (guint)(captured_len))
51
52 /* To pass one of two strings, singular or plural */
53 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
54
55 typedef struct _packet_counts {
56   gint           sctp;
57   gint           tcp;
58   gint           udp;
59   gint           icmp;
60   gint           ospf;
61   gint           gre;
62   gint           netbios;
63   gint           ipx;
64   gint           vines;
65   gint           other;
66   gint           total;
67   gint           arp;
68 } packet_counts;
69
70 /* Types of character encodings */
71 typedef enum {
72         CHAR_ASCII       = 0,   /* ASCII */
73         CHAR_EBCDIC      = 1    /* EBCDIC */
74 } char_enc;
75
76 /* Struct for boolean enumerations */
77 typedef struct true_false_string {
78         char    *true_string;
79         char    *false_string;
80 } true_false_string;
81
82 extern const true_false_string flags_set_truth;
83
84 extern void packet_init(void);
85 extern void packet_cleanup(void);
86
87 /* Handle for dissectors you call directly or register with "dissector_add()".
88    This handle is opaque outside of "packet.c". */
89 struct dissector_handle;
90 typedef struct dissector_handle *dissector_handle_t;
91
92 /* Hash table for matching port numbers and dissectors; this is opaque
93    outside of "packet.c". */
94 struct dissector_table;
95 typedef struct dissector_table *dissector_table_t;
96
97 /*
98  * Dissector that returns nothing.
99  */
100 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
101
102 /*
103  * Dissector that returns:
104  *
105  *      the amount of data in the protocol's PDU, if it was able to
106  *      dissect all the data;
107  *
108  *      0, if the tvbuff doesn't contain a PDU for that protocol;
109  *
110  *      the negative of the amount of additional data needed, if
111  *      we need more data (e.g., from subsequent TCP segments) to
112  *      dissect the entire PDU.
113  */
114 typedef int (*new_dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
115
116 typedef void (*DATFunc) (gchar *table_name, gpointer key, gpointer value,
117     gpointer user_data);
118 typedef void (*DATFunc_handle) (gchar *table_name, gpointer value,
119     gpointer user_data);
120 typedef void (*DATFunc_table) (gchar *table_name, gchar *ui_name,
121     gpointer user_data);
122
123 /* Opaque structure - provides type checking but no access to components */
124 typedef struct dtbl_entry dtbl_entry_t;
125
126 extern dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
127 extern dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
128 extern void dissector_table_foreach_changed (char *name, DATFunc func,
129     gpointer user_data);
130 extern void dissector_table_foreach (char *name, DATFunc func,
131     gpointer user_data);
132 extern void dissector_all_tables_foreach_changed (DATFunc func,
133     gpointer user_data);
134 extern void dissector_table_foreach_handle(char *name, DATFunc_handle func,
135     gpointer user_data);
136 extern void dissector_all_tables_foreach_table (DATFunc_table func,
137     gpointer user_data);
138
139 /* a protocol uses the function to register a sub-dissector table */
140 extern dissector_table_t register_dissector_table(const char *name,
141     char *ui_name, ftenum_t type, int base);
142
143 /* Find a dissector table by table name. */
144 extern dissector_table_t find_dissector_table(const char *name);
145
146 /* Get the UI name for a sub-dissector table, given its internal name */
147 extern char *get_dissector_table_ui_name(const char *name);
148
149 /* Get the field type to use when displaying values of the selector for a
150    sub-dissector table, given the table's internal name */
151 extern ftenum_t get_dissector_table_type(const char *name);
152
153 /* Get the base to use when displaying values of the selector for a
154    sub-dissector table, given the table's internal name */
155 extern int get_dissector_table_base(const char *name);
156
157 /* Add an entry to a uint dissector table. */
158 extern void dissector_add(const char *abbrev, guint32 pattern,
159     dissector_handle_t handle);
160
161 /* Delete the entry for a dissector in a uint dissector table
162    with a particular pattern. */
163 extern void dissector_delete(const char *name, guint32 pattern,
164     dissector_handle_t handle);
165
166 /* Change the entry for a dissector in a uint dissector table
167    with a particular pattern to use a new dissector handle. */
168 extern void dissector_change(const char *abbrev, guint32 pattern,
169     dissector_handle_t handle);
170
171 /* Reset an entry in a uint dissector table to its initial value. */
172 extern void dissector_reset(const char *name, guint32 pattern);
173
174 /* Look for a given value in a given uint dissector table and, if found,
175    call the dissector with the arguments supplied, and return TRUE,
176    otherwise return FALSE. */
177 extern gboolean dissector_try_port(dissector_table_t sub_dissectors,
178     guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
179
180 /* Look for a given value in a given uint dissector table and, if found,
181    return the dissector handle for that value. */
182 extern dissector_handle_t dissector_get_port_handle(
183     dissector_table_t sub_dissectors, guint32 port);
184
185 /* Add an entry to a string dissector table. */
186 extern void dissector_add_string(const char *name, gchar *pattern,
187     dissector_handle_t handle);
188
189 /* Delete the entry for a dissector in a string dissector table
190    with a particular pattern. */
191 extern void dissector_delete_string(const char *name, const gchar *pattern,
192         dissector_handle_t handle);
193
194 /* Change the entry for a dissector in a string dissector table
195    with a particular pattern to use a new dissector handle. */
196 extern void dissector_change_string(const char *name, gchar *pattern,
197     dissector_handle_t handle);
198
199 /* Reset an entry in a string sub-dissector table to its initial value. */
200 extern void dissector_reset_string(const char *name, const gchar *pattern);
201
202 /* Look for a given string in a given dissector table and, if found, call
203    the dissector with the arguments supplied, and return TRUE, otherwise
204    return FALSE. */
205 extern gboolean dissector_try_string(dissector_table_t sub_dissectors,
206     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
207
208 /* Add a handle to the list of handles that *could* be used with this
209    table.  That list is used by code in the UI. */
210 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
211
212 /* List of "heuristic" dissectors (which get handed a packet, look at it,
213    and either recognize it as being for their protocol, dissect it, and
214    return TRUE, or don't recognize it and return FALSE) to be called
215    by another dissector. */
216 typedef GSList *heur_dissector_list_t;
217
218 /* Type of a heuristic dissector */
219 typedef gboolean (*heur_dissector_t)(tvbuff_t *, packet_info *,
220         proto_tree *);
221
222 /* A protocol uses this function to register a heuristic dissector list */
223 extern void register_heur_dissector_list(const char *name,
224     heur_dissector_list_t *list);
225
226 /* Add a sub-dissector to a heuristic dissector list.  Called by the
227    protocol routine that wants to register a sub-dissector.  */
228 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
229     int proto);
230
231 /* Try all the dissectors in a given heuristic dissector list until
232    we find one that recognizes the protocol, in which case we return
233    TRUE, or we run out of dissectors, in which case we return FALSE. */
234 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
235     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
236
237 /* Register a dissector. */
238 extern void register_dissector(const char *name, dissector_t dissector,
239     int proto);
240 extern void new_register_dissector(const char *name, new_dissector_t dissector,
241     int proto);
242
243 /* Get the short name of the protocol for a dissector handle. */
244 extern char *dissector_handle_get_short_name(dissector_handle_t handle);
245
246 /* Get the index of the protocol for a dissector handle. */
247 extern int dissector_handle_get_protocol_index(dissector_handle_t handle);
248
249 /* Find a dissector by name. */
250 extern dissector_handle_t find_dissector(const char *name);
251
252 /* Create an anonymous handle for a dissector. */
253 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
254     int proto);
255 extern dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
256     int proto);
257
258 /* Call a dissector through a handle. */
259 extern int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
260     packet_info *pinfo, proto_tree *tree);
261
262 /* Do all one-time initialization. */
263 extern void dissect_init(void);
264
265 extern void dissect_cleanup(void);
266
267 /*
268  * Given a tvbuff, and a length from a packet header, adjust the length
269  * of the tvbuff to reflect the specified length.
270  */
271 extern void set_actual_length(tvbuff_t *tvb, guint specified_len);
272
273 /* Allow protocols to register "init" routines, which are called before
274    we make a pass through a capture file and dissect all its packets
275    (e.g., when we read in a new capture file, or run a "filter packets"
276    or "colorize packets" pass over the current capture file). */
277 extern void register_init_routine(void (*func)(void));
278
279 /* Initialize all data structures used for dissection. */
280 extern void init_dissection(void);
281
282 /* Allow protocols to register a "cleanup" routine to be
283  * run after the initial sequential run through the packets.
284  * Note that the file can still be open after this; this is not
285  * the final cleanup. */
286 extern void register_postseq_cleanup_routine(void (*func)(void));
287
288 /* Call all the registered "postseq_cleanup" routines. */
289 extern void postseq_cleanup_all_protocols(void);
290
291 /* Allow dissectors to register a "final_registration" routine
292  * that is run like the proto_register_XXX() routine, but the end
293  * end of the epan_init() function; that is, *after* all other
294  * subsystems, liked dfilters, have finished initializing. This is
295  * useful for dissector registration routines which need to compile
296  * display filters. dfilters can't initialize itself until all protocols
297  * have registereed themselvs. */
298 extern void
299 register_final_registration_routine(void (*func)(void));
300
301 /* Call all the registered "final_registration" routines. */
302 extern void
303 final_registration_all_protocols(void);
304
305 /*
306  * Add a new data source to the list of data sources for a frame, given
307  * the tvbuff for the data source and its name.
308  */
309 extern void add_new_data_source(packet_info *pinfo, tvbuff_t *tvb,
310     char *name);
311
312 /*
313  * Free up a frame's list of data sources.
314  */
315 extern void free_data_sources(packet_info *pinfo);
316
317 /*
318  * Dissectors should never modify the packet data.
319  */
320 extern void dissect_packet(epan_dissect_t *edt,
321     union wtap_pseudo_header *pseudo_header, const guchar *pd,
322     frame_data *fd, column_info *cinfo);
323
324 /* These functions are in packet-ethertype.c */
325 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
326                 int len, packet_counts *ld);
327 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
328                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
329                 int etype_id, int trailer_id);
330
331 #endif /* packet.h */