Rename "register_ethereal_tap()" to "register_tap_listener_cmd_arg()" as
[obnox/wireshark/wip.git] / epan / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id$
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 /*
29  * If <inttypes.h> defines formats to be used to print 64-bit integers,
30  * include it.
31  */
32 #ifdef INTTYPES_H_DEFINES_FORMATS
33 #include <inttypes.h>
34 #endif
35
36 #include "wiretap/wtap.h"
37 #include "proto.h"
38 #include "tvbuff.h"
39 #include "pint.h"
40 #include "to_str.h"
41 #include "value_string.h"
42 #include "column_info.h"
43 #include "frame_data.h"
44 #include "packet_info.h"
45 #include "column-utils.h"
46 #include "epan.h"
47
48 #define hi_nibble(b) (((b) & 0xf0) >> 4)
49 #define lo_nibble(b) ((b) & 0x0f)
50
51 /* Useful when you have an array whose size you can tell at compile-time */
52 #define array_length(x) (sizeof x / sizeof x[0])
53
54 /* Check whether the "len" bytes of data starting at "offset" is
55  * entirely inside the captured data for this packet. */
56 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
57         ((guint)(offset) + (guint)(len) > (guint)(offset) && \
58          (guint)(offset) + (guint)(len) <= (guint)(captured_len))
59
60 /* To pass one of two strings, singular or plural */
61 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
62
63 typedef struct _packet_counts {
64   gint           sctp;
65   gint           tcp;
66   gint           udp;
67   gint           icmp;
68   gint           ospf;
69   gint           gre;
70   gint           netbios;
71   gint           ipx;
72   gint           vines;
73   gint           other;
74   gint           total;
75   gint           arp;
76 } packet_counts;
77
78 /** Number of packet counts. */
79 #define PACKET_COUNTS_SIZE sizeof(packet_counts) / sizeof (gint)
80
81 /* Types of character encodings */
82 typedef enum {
83         CHAR_ASCII       = 0,   /* ASCII */
84         CHAR_EBCDIC      = 1    /* EBCDIC */
85 } char_enc;
86
87 /* Struct for boolean enumerations */
88 typedef struct true_false_string {
89         char    *true_string;
90         char    *false_string;
91 } true_false_string;
92
93 /**
94 * A default set of true/false strings that dissectors can use for 
95 * FT_BOOLEAN header fields.
96 **/
97 extern const true_false_string flags_set_truth;
98
99 extern void packet_init(void);
100 extern void packet_cleanup(void);
101
102 /* Handle for dissectors you call directly or register with "dissector_add()".
103    This handle is opaque outside of "packet.c". */
104 struct dissector_handle;
105 typedef struct dissector_handle *dissector_handle_t;
106
107 /* Hash table for matching port numbers and dissectors; this is opaque
108    outside of "packet.c". */
109 struct dissector_table;
110 typedef struct dissector_table *dissector_table_t;
111
112 /*
113  * Dissector that returns nothing.
114  */
115 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
116
117 /*
118  * Dissector that returns:
119  *
120  *      the amount of data in the protocol's PDU, if it was able to
121  *      dissect all the data;
122  *
123  *      0, if the tvbuff doesn't contain a PDU for that protocol;
124  *
125  *      the negative of the amount of additional data needed, if
126  *      we need more data (e.g., from subsequent TCP segments) to
127  *      dissect the entire PDU.
128  */
129 typedef int (*new_dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
130
131 typedef void (*DATFunc) (gchar *table_name, ftenum_t selector_type,
132     gpointer key, gpointer value, gpointer user_data);
133 typedef void (*DATFunc_handle) (gchar *table_name, gpointer value,
134     gpointer user_data);
135 typedef void (*DATFunc_table) (gchar *table_name, gchar *ui_name,
136     gpointer user_data);
137
138 /* Opaque structure - provides type checking but no access to components */
139 typedef struct dtbl_entry dtbl_entry_t;
140
141 extern dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
142 extern dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
143 extern void dissector_table_foreach_changed (char *name, DATFunc func,
144     gpointer user_data);
145 extern void dissector_table_foreach (char *name, DATFunc func,
146     gpointer user_data);
147 extern void dissector_all_tables_foreach_changed (DATFunc func,
148     gpointer user_data);
149 extern void dissector_table_foreach_handle(char *name, DATFunc_handle func,
150     gpointer user_data);
151 extern void dissector_all_tables_foreach_table (DATFunc_table func,
152     gpointer user_data);
153
154 /* a protocol uses the function to register a sub-dissector table */
155 extern dissector_table_t register_dissector_table(const char *name,
156     char *ui_name, ftenum_t type, int base);
157
158 /* Find a dissector table by table name. */
159 extern dissector_table_t find_dissector_table(const char *name);
160
161 /* Get the UI name for a sub-dissector table, given its internal name */
162 extern char *get_dissector_table_ui_name(const char *name);
163
164 /* Get the field type for values of the selector for a dissector table,
165    given the table's internal name */
166 extern ftenum_t get_dissector_table_selector_type(const char *name);
167
168 /* Get the base to use when displaying values of the selector for a
169    sub-dissector table, given the table's internal name */
170 extern int get_dissector_table_base(const char *name);
171
172 /* Add an entry to a uint dissector table. */
173 extern void dissector_add(const char *abbrev, guint32 pattern,
174     dissector_handle_t handle);
175
176 /* Delete the entry for a dissector in a uint dissector table
177    with a particular pattern. */
178 extern void dissector_delete(const char *name, guint32 pattern,
179     dissector_handle_t handle);
180
181 /* Change the entry for a dissector in a uint dissector table
182    with a particular pattern to use a new dissector handle. */
183 extern void dissector_change(const char *abbrev, guint32 pattern,
184     dissector_handle_t handle);
185
186 /* Reset an entry in a uint dissector table to its initial value. */
187 extern void dissector_reset(const char *name, guint32 pattern);
188
189 /* Look for a given value in a given uint dissector table and, if found,
190    call the dissector with the arguments supplied, and return TRUE,
191    otherwise return FALSE. */
192 extern gboolean dissector_try_port(dissector_table_t sub_dissectors,
193     guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
194
195 /* Look for a given value in a given uint dissector table and, if found,
196    return the dissector handle for that value. */
197 extern dissector_handle_t dissector_get_port_handle(
198     dissector_table_t sub_dissectors, guint32 port);
199
200 /* Add an entry to a string dissector table. */
201 extern void dissector_add_string(const char *name, gchar *pattern,
202     dissector_handle_t handle);
203
204 /* Delete the entry for a dissector in a string dissector table
205    with a particular pattern. */
206 extern void dissector_delete_string(const char *name, const gchar *pattern,
207         dissector_handle_t handle);
208
209 /* Change the entry for a dissector in a string dissector table
210    with a particular pattern to use a new dissector handle. */
211 extern void dissector_change_string(const char *name, gchar *pattern,
212     dissector_handle_t handle);
213
214 /* Reset an entry in a string sub-dissector table to its initial value. */
215 extern void dissector_reset_string(const char *name, const gchar *pattern);
216
217 /* Look for a given string in a given dissector table and, if found, call
218    the dissector with the arguments supplied, and return TRUE, otherwise
219    return FALSE. */
220 extern gboolean dissector_try_string(dissector_table_t sub_dissectors,
221     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
222
223 /* Look for a given value in a given string dissector table and, if found,
224    return the dissector handle for that value. */
225 extern dissector_handle_t dissector_get_string_handle(
226     dissector_table_t sub_dissectors, const gchar *string);
227
228 /* Add a handle to the list of handles that *could* be used with this
229    table.  That list is used by code in the UI. */
230 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
231
232 /* List of "heuristic" dissectors (which get handed a packet, look at it,
233    and either recognize it as being for their protocol, dissect it, and
234    return TRUE, or don't recognize it and return FALSE) to be called
235    by another dissector. */
236 typedef GSList *heur_dissector_list_t;
237
238 /** A protocol uses this function to register a heuristic sub-dissector list.
239  *  Call this in the parent dissectors proto_register function.
240  *
241  * @param name the name of this protocol
242  * @param list the list of heuristic sub-dissectors to be registered
243  */
244 extern void register_heur_dissector_list(const char *name,
245     heur_dissector_list_t *list);
246
247 /** Try all the dissectors in a given heuristic dissector list. This is done,
248  *  until we find one that recognizes the protocol.
249  *  Call this while the parent dissector running.
250  *
251  * @param sub_dissectors the sub-dissector list
252  * @param tvb the tv_buff with the (remaining) packet data
253  * @param pinfo the packet info of this packet (additional info)
254  * @param tree the protocol tree to be build or NULL
255  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
256  */
257 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
258     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
259
260 /** Type of a heuristic dissector, used in heur_dissector_add().
261  *
262  * @param tvb the tv_buff with the (remaining) packet data
263  * @param pinfo the packet info of this packet (additional info)
264  * @param tree the protocol tree to be build or NULL
265  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
266  */
267 typedef gboolean (*heur_dissector_t)(tvbuff_t *tvb, packet_info *pinfo,
268         proto_tree *tree);
269
270 /** Add a sub-dissector to a heuristic dissector list.
271  *  Call this in the proto_handoff function of the sub-dissector.
272  *
273  * @param name the name of the "parent" protocol, e.g. "tcp"
274  * @param dissector the sub-dissector to be registered
275  * @param proto the protocol id of the sub-dissector
276  */
277 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
278     int proto);
279
280 /* Register a dissector. */
281 extern void register_dissector(const char *name, dissector_t dissector,
282     int proto);
283 extern void new_register_dissector(const char *name, new_dissector_t dissector,
284     int proto);
285
286 /* Get the short name of the protocol for a dissector handle. */
287 extern char *dissector_handle_get_short_name(dissector_handle_t handle);
288
289 /* Get the index of the protocol for a dissector handle. */
290 extern int dissector_handle_get_protocol_index(dissector_handle_t handle);
291
292 /* Find a dissector by name. */
293 extern dissector_handle_t find_dissector(const char *name);
294
295 /* Create an anonymous handle for a dissector. */
296 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
297     int proto);
298 extern dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
299     int proto);
300
301 /* Call a dissector through a handle. */
302 extern int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
303     packet_info *pinfo, proto_tree *tree);
304
305 /* Do all one-time initialization. */
306 extern void dissect_init(void);
307
308 extern void dissect_cleanup(void);
309
310 /*
311  * Given a tvbuff, and a length from a packet header, adjust the length
312  * of the tvbuff to reflect the specified length.
313  */
314 extern void set_actual_length(tvbuff_t *tvb, guint specified_len);
315
316 /* Allow protocols to register "init" routines, which are called before
317    we make a pass through a capture file and dissect all its packets
318    (e.g., when we read in a new capture file, or run a "filter packets"
319    or "colorize packets" pass over the current capture file). */
320 extern void register_init_routine(void (*func)(void));
321
322 /* Initialize all data structures used for dissection. */
323 extern void init_dissection(void);
324
325 /* Free data structures allocated for dissection. */
326 extern void cleanup_dissection(void);
327
328 /* Allow protocols to register a "cleanup" routine to be
329  * run after the initial sequential run through the packets.
330  * Note that the file can still be open after this; this is not
331  * the final cleanup. */
332 extern void register_postseq_cleanup_routine(void (*func)(void));
333
334 /* Call all the registered "postseq_cleanup" routines. */
335 extern void postseq_cleanup_all_protocols(void);
336
337 /* Allow dissectors to register a "final_registration" routine
338  * that is run like the proto_register_XXX() routine, but the end
339  * end of the epan_init() function; that is, *after* all other
340  * subsystems, liked dfilters, have finished initializing. This is
341  * useful for dissector registration routines which need to compile
342  * display filters. dfilters can't initialize itself until all protocols
343  * have registereed themselvs. */
344 extern void
345 register_final_registration_routine(void (*func)(void));
346
347 /* Call all the registered "final_registration" routines. */
348 extern void
349 final_registration_all_protocols(void);
350
351 /*
352  * Add a new data source to the list of data sources for a frame, given
353  * the tvbuff for the data source and its name.
354  */
355 extern void add_new_data_source(packet_info *pinfo, tvbuff_t *tvb,
356     char *name);
357
358 /*
359  * Free up a frame's list of data sources.
360  */
361 extern void free_data_sources(packet_info *pinfo);
362
363 /*
364  * Dissectors should never modify the packet data.
365  */
366 extern void dissect_packet(epan_dissect_t *edt,
367     union wtap_pseudo_header *pseudo_header, const guchar *pd,
368     frame_data *fd, column_info *cinfo);
369
370 /* These functions are in packet-ethertype.c */
371 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
372                 int len, packet_counts *ld);
373 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
374                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
375                 int etype_id, int trailer_id, int fcs_len);
376
377 /*
378  * Dump layer/selector/dissector records in a fashion similar to the
379  * proto_registrar_dump_* routines.
380  */
381 extern void dissector_dump_decodes(void);
382
383 #endif /* packet.h */