Require GLib 2.4 or later.
[metze/wireshark/wip.git] / epan / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #include "tfs.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif /* __cplusplus */
44
45 #define hi_nibble(b) (((b) & 0xf0) >> 4)
46 #define lo_nibble(b) ((b) & 0x0f)
47
48 /* Useful when you have an array whose size you can tell at compile-time */
49 #define array_length(x) (sizeof x / sizeof x[0])
50
51 /* Check whether the "len" bytes of data starting at "offset" is
52  * entirely inside the captured data for this packet. */
53 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
54         ((guint)(offset) + (guint)(len) > (guint)(offset) && \
55          (guint)(offset) + (guint)(len) <= (guint)(captured_len))
56
57 /* To pass one of two strings, singular or plural */
58 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
59
60 typedef struct _packet_counts {
61   gint           sctp;
62   gint           tcp;
63   gint           udp;
64   gint           icmp;
65   gint           ospf;
66   gint           gre;
67   gint           netbios;
68   gint           ipx;
69   gint           vines;
70   gint           other;
71   gint           total;
72   gint           arp;
73 } packet_counts;
74
75 /** Number of packet counts. */
76 #define PACKET_COUNTS_SIZE sizeof(packet_counts) / sizeof (gint)
77
78 /* Types of character encodings */
79 typedef enum {
80         CHAR_ASCII       = 0,   /* ASCII */
81         CHAR_EBCDIC      = 1    /* EBCDIC */
82 } char_enc;
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 /** Type of a heuristic dissector, used in heur_dissector_add().
117  *
118  * @param tvb the tv_buff with the (remaining) packet data
119  * @param pinfo the packet info of this packet (additional info)
120  * @param tree the protocol tree to be build or NULL
121  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
122  */
123 typedef gboolean (*heur_dissector_t)(tvbuff_t *tvb, packet_info *pinfo,
124         proto_tree *tree);
125
126 typedef void (*DATFunc) (const gchar *table_name, ftenum_t selector_type,
127     gpointer key, gpointer value, gpointer user_data);
128 typedef void (*DATFunc_handle) (const gchar *table_name, gpointer value,
129     gpointer user_data);
130 typedef void (*DATFunc_table) (const gchar *table_name, const gchar *ui_name,
131     gpointer user_data);
132
133 /* Opaque structure - provides type checking but no access to components */
134 typedef struct dtbl_entry dtbl_entry_t;
135
136 extern dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
137 extern dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
138 extern void dissector_table_foreach_changed (const char *name, DATFunc func,
139     gpointer user_data);
140 extern void dissector_table_foreach (const char *name, DATFunc func,
141     gpointer user_data);
142 extern void dissector_all_tables_foreach_changed (DATFunc func,
143     gpointer user_data);
144 extern void dissector_table_foreach_handle(const char *name, DATFunc_handle func,
145     gpointer user_data);
146 extern void dissector_all_tables_foreach_table (DATFunc_table func,
147     gpointer user_data);
148
149 /* a protocol uses the function to register a sub-dissector table */
150 extern dissector_table_t register_dissector_table(const char *name,
151     const char *ui_name, ftenum_t type, int base);
152
153 /* Find a dissector table by table name. */
154 extern dissector_table_t find_dissector_table(const char *name);
155
156 /* Get the UI name for a sub-dissector table, given its internal name */
157 extern const char *get_dissector_table_ui_name(const char *name);
158
159 /* Get the field type for values of the selector for a dissector table,
160    given the table's internal name */
161 extern ftenum_t get_dissector_table_selector_type(const char *name);
162
163 /* Get the base to use when displaying values of the selector for a
164    sub-dissector table, given the table's internal name */
165 extern int get_dissector_table_base(const char *name);
166
167 /* Add an entry to a uint dissector table. */
168 extern void dissector_add(const char *abbrev, guint32 pattern,
169     dissector_handle_t handle);
170
171 /* Delete the entry for a dissector in a uint dissector table
172    with a particular pattern. */
173 extern void dissector_delete(const char *name, guint32 pattern,
174     dissector_handle_t handle);
175
176 /* Change the entry for a dissector in a uint dissector table
177    with a particular pattern to use a new dissector handle. */
178 extern void dissector_change(const char *abbrev, guint32 pattern,
179     dissector_handle_t handle);
180
181 /* Reset an entry in a uint dissector table to its initial value. */
182 extern void dissector_reset(const char *name, guint32 pattern);
183
184 /* Look for a given value in a given uint dissector table and, if found,
185    call the dissector with the arguments supplied, and return TRUE,
186    otherwise return FALSE. */
187 extern gboolean dissector_try_port(dissector_table_t sub_dissectors,
188     guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
189
190 /* Look for a given value in a given uint dissector table and, if found,
191    return the dissector handle for that value. */
192 extern dissector_handle_t dissector_get_port_handle(
193     dissector_table_t sub_dissectors, guint32 port);
194
195 /* Add an entry to a string dissector table. */
196 extern void dissector_add_string(const char *name, const gchar *pattern,
197     dissector_handle_t handle);
198
199 /* Delete the entry for a dissector in a string dissector table
200    with a particular pattern. */
201 extern void dissector_delete_string(const char *name, const gchar *pattern,
202         dissector_handle_t handle);
203
204 /* Change the entry for a dissector in a string dissector table
205    with a particular pattern to use a new dissector handle. */
206 extern void dissector_change_string(const char *name, gchar *pattern,
207     dissector_handle_t handle);
208
209 /* Reset an entry in a string sub-dissector table to its initial value. */
210 extern void dissector_reset_string(const char *name, const gchar *pattern);
211
212 /* Look for a given string in a given dissector table and, if found, call
213    the dissector with the arguments supplied, and return TRUE, otherwise
214    return FALSE. */
215 extern gboolean dissector_try_string(dissector_table_t sub_dissectors,
216     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
217
218 /* Look for a given value in a given string dissector table and, if found,
219    return the dissector handle for that value. */
220 extern dissector_handle_t dissector_get_string_handle(
221     dissector_table_t sub_dissectors, const gchar *string);
222
223 /* Add a handle to the list of handles that *could* be used with this
224    table.  That list is used by code in the UI. */
225 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
226
227 /* List of "heuristic" dissectors (which get handed a packet, look at it,
228    and either recognize it as being for their protocol, dissect it, and
229    return TRUE, or don't recognize it and return FALSE) to be called
230    by another dissector. */
231 typedef GSList *heur_dissector_list_t;
232
233 /** A protocol uses this function to register a heuristic sub-dissector list.
234  *  Call this in the parent dissectors proto_register function.
235  *
236  * @param name the name of this protocol
237  * @param list the list of heuristic sub-dissectors to be registered
238  */
239 extern void register_heur_dissector_list(const char *name,
240     heur_dissector_list_t *list);
241
242 /** Try all the dissectors in a given heuristic dissector list. This is done,
243  *  until we find one that recognizes the protocol.
244  *  Call this while the parent dissector running.
245  *
246  * @param sub_dissectors the sub-dissector list
247  * @param tvb the tv_buff with the (remaining) packet data
248  * @param pinfo the packet info of this packet (additional info)
249  * @param tree the protocol tree to be build or NULL
250  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
251  */
252 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
253     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
254
255 /** Add a sub-dissector to a heuristic dissector list.
256  *  Call this in the proto_handoff function of the sub-dissector.
257  *
258  * @param name the name of the "parent" protocol, e.g. "tcp"
259  * @param dissector the sub-dissector to be registered
260  * @param proto the protocol id of the sub-dissector
261  */
262 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
263     int proto);
264
265 /** Remove a sub-dissector from a heuristic dissector list.
266  *  Call this in the prefs_reinit function of the sub-dissector.
267  *
268  * @param name the name of the "parent" protocol, e.g. "tcp"
269  * @param dissector the sub-dissector to be unregistered
270  * @param proto the protocol id of the sub-dissector
271  */
272 extern void heur_dissector_delete(const char *name, heur_dissector_t dissector, int proto);
273
274 /* Register a dissector. */
275 extern void register_dissector(const char *name, dissector_t dissector,
276     int proto);
277 extern void new_register_dissector(const char *name, new_dissector_t dissector,
278     int proto);
279
280 /* Get the short name of the protocol for a dissector handle. */
281 extern const char *dissector_handle_get_short_name(dissector_handle_t handle);
282
283 /* Get the index of the protocol for a dissector handle. */
284 extern int dissector_handle_get_protocol_index(dissector_handle_t handle);
285
286 /* Find a dissector by name. */
287 extern dissector_handle_t find_dissector(const char *name);
288
289 /* Create an anonymous handle for a dissector. */
290 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
291     int proto);
292 extern dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
293     int proto);
294
295 /* Call a dissector through a handle and if no dissector was found
296  * pass if over to the "data" dissector instead.
297  *
298  *   @param handle The dissector to call.
299  *   @param  tvb The buffer to dissect.
300  *   @param  pinfo Packet Info.
301  *   @param  tree The protocol tree.
302  *   @return  If the protocol for that handle isn't enabled call the data 
303  *   dissector. Otherwise, if the handle refers to a new-style 
304  *   dissector, call the dissector and return its return value, otherwise call 
305  *   it and return the length of the tvbuff pointed to by the argument.
306  */
307 extern int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
308     packet_info *pinfo, proto_tree *tree);
309     
310 /* Call a dissector through a handle but if no dissector was found
311  * just return 0 and do not call the "data" dissector instead.
312  *
313  *   @param handle The dissector to call.
314  *   @param  tvb The buffer to dissect.
315  *   @param  pinfo Packet Info.
316  *   @param  tree The protocol tree.
317  *   @return  If the protocol for that handle isn't enabled, return 0 without
318  *   calling the dissector. Otherwise, if the handle refers to a new-style 
319  *   dissector, call the dissector and return its return value, otherwise call 
320  *   it and return the length of the tvbuff pointed to by the argument.
321  */
322 extern int call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
323     packet_info *pinfo, proto_tree *tree);
324
325 /* Do all one-time initialization. */
326 extern void dissect_init(void);
327
328 extern void dissect_cleanup(void);
329
330 /*
331  * Given a tvbuff, and a length from a packet header, adjust the length
332  * of the tvbuff to reflect the specified length.
333  */
334 extern void set_actual_length(tvbuff_t *tvb, guint specified_len);
335
336 /* Allow protocols to register "init" routines, which are called before
337    we make a pass through a capture file and dissect all its packets
338    (e.g., when we read in a new capture file, or run a "filter packets"
339    or "colorize packets" pass over the current capture file). */
340 extern void register_init_routine(void (*func)(void));
341
342 /* Initialize all data structures used for dissection. */
343 extern void init_dissection(void);
344
345 /* Free data structures allocated for dissection. */
346 extern void cleanup_dissection(void);
347
348 /* Allow protocols to register a "cleanup" routine to be
349  * run after the initial sequential run through the packets.
350  * Note that the file can still be open after this; this is not
351  * the final cleanup. */
352 extern void register_postseq_cleanup_routine(void (*func)(void));
353
354 /* Call all the registered "postseq_cleanup" routines. */
355 extern void postseq_cleanup_all_protocols(void);
356
357 /* Allow dissectors to register a "final_registration" routine
358  * that is run like the proto_register_XXX() routine, but the end
359  * end of the epan_init() function; that is, *after* all other
360  * subsystems, liked dfilters, have finished initializing. This is
361  * useful for dissector registration routines which need to compile
362  * display filters. dfilters can't initialize itself until all protocols
363  * have registereed themselvs. */
364 extern void
365 register_final_registration_routine(void (*func)(void));
366
367 /* Call all the registered "final_registration" routines. */
368 extern void
369 final_registration_all_protocols(void);
370
371 /*
372  * Add a new data source to the list of data sources for a frame, given
373  * the tvbuff for the data source and its name.
374  */
375 extern void add_new_data_source(packet_info *pinfo, tvbuff_t *tvb,
376     const char *name);
377
378 /*
379  * Free up a frame's list of data sources.
380  */
381 extern void free_data_sources(packet_info *pinfo);
382
383 /*
384  * Dissectors should never modify the packet data.
385  */
386 extern void dissect_packet(epan_dissect_t *edt,
387     union wtap_pseudo_header *pseudo_header, const guchar *pd,
388     frame_data *fd, column_info *cinfo);
389
390 /* These functions are in packet-ethertype.c */
391 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
392                 int len, packet_counts *ld);
393 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
394                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
395                 int etype_id, int trailer_id, int fcs_len);
396
397 /*
398  * Dump layer/selector/dissector records in a fashion similar to the
399  * proto_registrar_dump_* routines.
400  */
401 extern void dissector_dump_decodes(void);
402
403 /*
404  * post dissectors are to be called by packet-frame.c after every other
405  * dissector has been called.
406  */
407 extern void register_postdissector(dissector_handle_t);
408 extern void call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
409
410 #ifdef __cplusplus
411 }
412 #endif /* __cplusplus */
413
414 #endif /* packet.h */