Slightly different fix for https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9117 :
[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 "wsutil/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 #include "ws_symbol_export.h"
41 #include "range.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif /* __cplusplus */
46
47 /** @defgroup packet General Packet Dissection
48  *
49  * @{
50  */
51
52 #define hi_nibble(b) (((b) & 0xf0) >> 4)
53 #define lo_nibble(b) ((b) & 0x0f)
54
55 /* Useful when you have an array whose size you can tell at compile-time */
56 #define array_length(x) (sizeof x / sizeof x[0])
57
58 /* Check whether the "len" bytes of data starting at "offset" is
59  * entirely inside the captured data for this packet. */
60 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
61         ((guint)(offset) + (guint)(len) > (guint)(offset) && \
62          (guint)(offset) + (guint)(len) <= (guint)(captured_len))
63
64 /* To pass one of two strings, singular or plural */
65 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
66
67 typedef struct _packet_counts {
68   gint           sctp;
69   gint           tcp;
70   gint           udp;
71   gint           icmp;
72   gint           ospf;
73   gint           gre;
74   gint           netbios;
75   gint           ipx;
76   gint           vines;
77   gint           other;
78   gint           total;
79   gint           arp;
80   gint           i2c_event;
81   gint           i2c_data;
82 } packet_counts;
83
84 /** Number of packet counts. */
85 #define PACKET_COUNTS_SIZE sizeof(packet_counts) / sizeof (gint)
86
87 extern void packet_init(void);
88 extern void packet_cache_proto_handles(void);
89 extern void packet_cleanup(void);
90
91 /* Handle for dissectors you call directly or register with "dissector_add_uint()".
92    This handle is opaque outside of "packet.c". */
93 struct dissector_handle;
94 typedef struct dissector_handle *dissector_handle_t;
95
96 /* Hash table for matching unsigned integers, or strings, and dissectors;
97    this is opaque outside of "packet.c". */
98 struct dissector_table;
99 typedef struct dissector_table *dissector_table_t;
100
101 /*
102  * Dissector that returns nothing.
103  */
104 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
105
106 /*
107  * Dissector that returns:
108  *
109  *      The amount of data in the protocol's PDU, if it was able to
110  *      dissect all the data;
111  *
112  *      0, if the tvbuff doesn't contain a PDU for that protocol;
113  *
114  *      The negative of the amount of additional data needed, if
115  *      we need more data (e.g., from subsequent TCP segments) to
116  *      dissect the entire PDU.
117  */
118 typedef int (*new_dissector_t)(tvbuff_t *, packet_info *, proto_tree *, void *);
119
120 /** Type of a heuristic dissector, used in heur_dissector_add().
121  *
122  * @param tvb the tvbuff with the (remaining) packet data
123  * @param pinfo the packet info of this packet (additional info)
124  * @param tree the protocol tree to be build or NULL
125  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
126  */
127 typedef gboolean (*heur_dissector_t)(tvbuff_t *tvb, packet_info *pinfo,
128         proto_tree *tree, void *);
129
130 typedef void (*DATFunc) (const gchar *table_name, ftenum_t selector_type,
131     gpointer key, gpointer value, gpointer user_data);
132 typedef void (*DATFunc_handle) (const gchar *table_name, gpointer value,
133     gpointer user_data);
134 typedef void (*DATFunc_table) (const gchar *table_name, const gchar *ui_name,
135     gpointer user_data);
136
137 typedef void (*DATFunc_heur_table) (const gchar *table_name,gpointer table,
138     gpointer user_data);
139
140 /* Opaque structure - provides type checking but no access to components */
141 typedef struct dtbl_entry dtbl_entry_t;
142
143 WS_DLL_PUBLIC dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
144 WS_DLL_PUBLIC dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
145 void dissector_table_foreach_changed (const char *name, DATFunc func,
146     gpointer user_data);
147 WS_DLL_PUBLIC void dissector_table_foreach (const char *name, DATFunc func,
148     gpointer user_data);
149 WS_DLL_PUBLIC void dissector_all_tables_foreach_changed (DATFunc func,
150     gpointer user_data);
151 WS_DLL_PUBLIC void dissector_table_foreach_handle(const char *name, DATFunc_handle func,
152     gpointer user_data);
153 WS_DLL_PUBLIC void dissector_all_tables_foreach_table (DATFunc_table func,
154     gpointer user_data, GCompareFunc compare_key_func);
155
156 /* a protocol uses the function to register a sub-dissector table */
157 WS_DLL_PUBLIC dissector_table_t register_dissector_table(const char *name,
158     const char *ui_name, const ftenum_t type, const int base);
159
160 /* Find a dissector table by table name. */
161 WS_DLL_PUBLIC dissector_table_t find_dissector_table(const char *name);
162
163 /* Get the UI name for a sub-dissector table, given its internal name */
164 WS_DLL_PUBLIC const char *get_dissector_table_ui_name(const char *name);
165
166 /* Get the field type for values of the selector for a dissector table,
167    given the table's internal name */
168 WS_DLL_PUBLIC ftenum_t get_dissector_table_selector_type(const char *name);
169
170 /* Get the base to use when displaying values of the selector for a
171    sub-dissector table, given the table's internal name */
172 WS_DLL_PUBLIC int get_dissector_table_base(const char *name);
173
174 /* Add an entry to a uint dissector table. */
175 WS_DLL_PUBLIC void dissector_add_uint(const char *abbrev, const guint32 pattern,
176     dissector_handle_t handle);
177
178 /* Add an range of entries to a uint dissector table. */
179 WS_DLL_PUBLIC void dissector_add_uint_range(const char *abbrev, range_t *range,
180     dissector_handle_t handle);
181
182 /* Delete the entry for a dissector in a uint dissector table
183    with a particular pattern. */
184 WS_DLL_PUBLIC void dissector_delete_uint(const char *name, const guint32 pattern,
185     dissector_handle_t handle);
186
187 /* Delete an range of entries from a uint dissector table. */
188 WS_DLL_PUBLIC void dissector_delete_uint_range(const char *abbrev, range_t *range,
189     dissector_handle_t handle);
190
191 /* Change the entry for a dissector in a uint dissector table
192    with a particular pattern to use a new dissector handle. */
193 WS_DLL_PUBLIC void dissector_change_uint(const char *abbrev, const guint32 pattern,
194     dissector_handle_t handle);
195
196 /* Reset an entry in a uint dissector table to its initial value. */
197 WS_DLL_PUBLIC void dissector_reset_uint(const char *name, const guint32 pattern);
198
199 /* Look for a given value in a given uint dissector table and, if found,
200    call the dissector with the arguments supplied, and return TRUE,
201    otherwise return FALSE. */
202 WS_DLL_PUBLIC gboolean dissector_try_uint(dissector_table_t sub_dissectors,
203     const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
204
205 /* Look for a given value in a given uint dissector table and, if found,
206    call the dissector with the arguments supplied, and return TRUE,
207    otherwise return FALSE. */
208 gboolean dissector_try_uint_new(dissector_table_t sub_dissectors,
209         const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const gboolean add_proto_name, void *data);
210
211 /* Look for a given value in a given uint dissector table and, if found,
212    return the dissector handle for that value. */
213 WS_DLL_PUBLIC dissector_handle_t dissector_get_uint_handle(
214     dissector_table_t const sub_dissectors, const guint32 uint_val);
215
216 /* Add an entry to a string dissector table. */
217 WS_DLL_PUBLIC void dissector_add_string(const char *name, const gchar *pattern,
218     dissector_handle_t handle);
219
220 /* Delete the entry for a dissector in a string dissector table
221    with a particular pattern. */
222 WS_DLL_PUBLIC void dissector_delete_string(const char *name, const gchar *pattern,
223         dissector_handle_t handle);
224
225 /* Change the entry for a dissector in a string dissector table
226    with a particular pattern to use a new dissector handle. */
227 WS_DLL_PUBLIC void dissector_change_string(const char *name, const gchar *pattern,
228     dissector_handle_t handle);
229
230 /* Reset an entry in a string sub-dissector table to its initial value. */
231 WS_DLL_PUBLIC void dissector_reset_string(const char *name, const gchar *pattern);
232
233 /* Look for a given string in a given dissector table and, if found, call
234    the dissector with the arguments supplied, and return TRUE, otherwise
235    return FALSE. */
236 WS_DLL_PUBLIC gboolean dissector_try_string(dissector_table_t sub_dissectors,
237     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
238
239 /* Look for a given value in a given string dissector table and, if found,
240    return the dissector handle for that value. */
241 WS_DLL_PUBLIC dissector_handle_t dissector_get_string_handle(
242     dissector_table_t sub_dissectors, const gchar *string);
243
244 /* Add a handle to the list of handles that *could* be used with this
245    table.  That list is used by code in the UI. */
246 WS_DLL_PUBLIC void dissector_add_handle(const char *name, dissector_handle_t handle);
247
248 /* List of "heuristic" dissectors (which get handed a packet, look at it,
249    and either recognize it as being for their protocol, dissect it, and
250    return TRUE, or don't recognize it and return FALSE) to be called
251    by another dissector. */
252 typedef GSList *heur_dissector_list_t;
253
254
255 typedef struct {
256         heur_dissector_t dissector;
257         protocol_t *protocol;
258         gboolean enabled;
259 } heur_dtbl_entry_t;
260
261 /** A protocol uses this function to register a heuristic sub-dissector list.
262  *  Call this in the parent dissectors proto_register function.
263  *
264  * @param name the name of this protocol
265  * @param list the list of heuristic sub-dissectors to be registered
266  */
267 WS_DLL_PUBLIC void register_heur_dissector_list(const char *name,
268     heur_dissector_list_t *list);
269
270 WS_DLL_PUBLIC void dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
271     gpointer user_data);
272
273 /** Try all the dissectors in a given heuristic dissector list. This is done,
274  *  until we find one that recognizes the protocol.
275  *  Call this while the parent dissector running.
276  *
277  * @param sub_dissectors the sub-dissector list
278  * @param tvb the tvbuff with the (remaining) packet data
279  * @param pinfo the packet info of this packet (additional info)
280  * @param tree the protocol tree to be build or NULL
281  * @param data parameter to pass to subdissector
282  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
283  */
284 WS_DLL_PUBLIC gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
285     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data);
286
287 /** Add a sub-dissector to a heuristic dissector list.
288  *  Call this in the proto_handoff function of the sub-dissector.
289  *
290  * @param name the name of the "parent" protocol, e.g. "tcp"
291  * @param dissector the sub-dissector to be registered
292  * @param proto the protocol id of the sub-dissector
293  */
294 WS_DLL_PUBLIC void heur_dissector_add(const char *name, heur_dissector_t dissector,
295     const int proto);
296
297 /** Remove a sub-dissector from a heuristic dissector list.
298  *  Call this in the prefs_reinit function of the sub-dissector.
299  *
300  * @param name the name of the "parent" protocol, e.g. "tcp"
301  * @param dissector the sub-dissector to be unregistered
302  * @param proto the protocol id of the sub-dissector
303  */
304 WS_DLL_PUBLIC void heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto);
305
306 /** Enable/Disable a sub-dissector in a heuristic dissector list
307  *  Call this in the prefs_reinit function of the sub-dissector.
308  *
309  * @param name the name of the "parent" protocol, e.g. "tcp"
310  * @param dissector the sub-dissector to be disabled/enabled
311  * @param proto the protocol id of the sub-dissector
312  * @param enabled TRUE/FALSE to enable/disable the sub-dissector
313  */
314 extern void heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled);
315
316 /* Register a dissector. */
317 WS_DLL_PUBLIC dissector_handle_t register_dissector(const char *name, dissector_t dissector,
318     const int proto);
319 WS_DLL_PUBLIC dissector_handle_t new_register_dissector(const char *name, new_dissector_t dissector,
320     const int proto);
321
322 /* Get the long name of the protocol for a dissector handle. */
323 extern const char *dissector_handle_get_long_name(const dissector_handle_t handle);
324
325 /* Get the short name of the protocol for a dissector handle. */
326 WS_DLL_PUBLIC const char *dissector_handle_get_short_name(const dissector_handle_t handle);
327
328 /* Get the index of the protocol for a dissector handle. */
329 WS_DLL_PUBLIC int dissector_handle_get_protocol_index(const dissector_handle_t handle);
330
331 /* Find a dissector by name. */
332 WS_DLL_PUBLIC dissector_handle_t find_dissector(const char *name);
333
334 /* Get a dissector name from handle. */
335 WS_DLL_PUBLIC const char *dissector_handle_get_dissector_name(const dissector_handle_t handle);
336
337 /* Create an anonymous handle for a dissector. */
338 WS_DLL_PUBLIC dissector_handle_t create_dissector_handle(dissector_t dissector,
339     const int proto);
340 WS_DLL_PUBLIC dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
341     const int proto);
342
343 /* Call a dissector through a handle and if no dissector was found
344  * pass it over to the "data" dissector instead.
345  *
346  *   @param handle The dissector to call.
347  *   @param  tvb The buffer to dissect.
348  *   @param  pinfo Packet Info.
349  *   @param  tree The protocol tree.
350  *   @param  data parameter to pass to dissector
351  *   @return  If the protocol for that handle isn't enabled call the data
352  *   dissector. Otherwise, if the handle refers to a new-style
353  *   dissector, call the dissector and return its return value, otherwise call
354  *   it and return the length of the tvbuff pointed to by the argument.
355  */
356 WS_DLL_PUBLIC int call_dissector_with_data(dissector_handle_t handle, tvbuff_t *tvb,
357     packet_info *pinfo, proto_tree *tree, void *data);
358 WS_DLL_PUBLIC int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
359     packet_info *pinfo, proto_tree *tree);
360
361 /* Call a dissector through a handle but if no dissector was found
362  * just return 0 and do not call the "data" dissector instead.
363  *
364  *   @param handle The dissector to call.
365  *   @param  tvb The buffer to dissect.
366  *   @param  pinfo Packet Info.
367  *   @param  tree The protocol tree.
368  *   @param  data parameter to pass to dissector
369  *   @return  If the protocol for that handle isn't enabled, return 0 without
370  *   calling the dissector. Otherwise, if the handle refers to a new-style
371  *   dissector, call the dissector and return its return value, otherwise call
372  *   it and return the length of the tvbuff pointed to by the argument.
373  */
374 WS_DLL_PUBLIC int call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
375     packet_info *pinfo, proto_tree *tree, void *data);
376
377 /* Do all one-time initialization. */
378 extern void dissect_init(void);
379
380 extern void dissect_cleanup(void);
381
382 /*
383  * Given a tvbuff, and a length from a packet header, adjust the length
384  * of the tvbuff to reflect the specified length.
385  */
386 WS_DLL_PUBLIC void set_actual_length(tvbuff_t *tvb, const guint specified_len);
387
388 /* Allow protocols to register "init" routines, which are called before
389    we make a pass through a capture file and dissect all its packets
390    (e.g., when we read in a new capture file, or run a "filter packets"
391    or "colorize packets" pass over the current capture file). */
392 WS_DLL_PUBLIC void register_init_routine(void (*func)(void));
393
394 /* Initialize all data structures used for dissection. */
395 void init_dissection(void);
396
397 /* Free data structures allocated for dissection. */
398 void cleanup_dissection(void);
399
400 /* Allow protocols to register a "cleanup" routine to be
401  * run after the initial sequential run through the packets.
402  * Note that the file can still be open after this; this is not
403  * the final cleanup. */
404 WS_DLL_PUBLIC void register_postseq_cleanup_routine(void (*func)(void));
405
406 /* Call all the registered "postseq_cleanup" routines. */
407 WS_DLL_PUBLIC void postseq_cleanup_all_protocols(void);
408
409 /* Allow dissectors to register a "final_registration" routine
410  * that is run like the proto_register_XXX() routine, but the end
411  * end of the epan_init() function; that is, *after* all other
412  * subsystems, liked dfilters, have finished initializing. This is
413  * useful for dissector registration routines which need to compile
414  * display filters. dfilters can't initialize itself until all protocols
415  * have registereed themselvs. */
416 WS_DLL_PUBLIC void
417 register_final_registration_routine(void (*func)(void));
418
419 /* Call all the registered "final_registration" routines. */
420 extern void
421 final_registration_all_protocols(void);
422
423 /*
424  * Add a new data source to the list of data sources for a frame, given
425  * the tvbuff for the data source and its name.
426  */
427 WS_DLL_PUBLIC void add_new_data_source(packet_info *pinfo, tvbuff_t *tvb,
428     const char *name);
429
430
431 /*
432  * Return the data source name, tvb.
433  */
434 struct data_source;
435 WS_DLL_PUBLIC const char *get_data_source_name(const struct data_source *src);
436 WS_DLL_PUBLIC tvbuff_t *get_data_source_tvb(const struct data_source *src);
437
438 /*
439  * Free up a frame's list of data sources.
440  */
441 extern void free_data_sources(packet_info *pinfo);
442
443 /* Mark another frame as depended upon by the current frame.
444  *
445  * This information is used to ensure that the dependend-upon frame is saved
446  * if the user does a File->Save-As of only the Displayed packets and the
447  * current frame passed the display filter.
448  */
449 WS_DLL_PUBLIC void mark_frame_as_depended_upon(packet_info *pinfo, guint32 frame_num);
450
451 /*
452  * Dissectors should never modify the packet data.
453  */
454 extern void dissect_packet(epan_dissect_t *edt,
455     struct wtap_pkthdr *phdr, tvbuff_t *tvb,
456     frame_data *fd, column_info *cinfo);
457
458 /* These functions are in packet-ethertype.c */
459 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
460                 int len, packet_counts *ld);
461 WS_DLL_PUBLIC void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
462                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
463                 int etype_id, int trailer_id, int fcs_len);
464
465 /*
466  * Dump layer/selector/dissector records in a fashion similar to the
467  * proto_registrar_dump_* routines.
468  */
469 WS_DLL_PUBLIC void dissector_dump_decodes(void);
470
471 /*
472  * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
473  */
474 WS_DLL_PUBLIC void dissector_dump_heur_decodes(void);
475
476 /*
477  * post dissectors are to be called by packet-frame.c after every other
478  * dissector has been called.
479  */
480 WS_DLL_PUBLIC void register_postdissector(dissector_handle_t);
481 extern gboolean have_postdissector(void);
482 extern void call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
483
484 /** @} */
485
486 #ifdef __cplusplus
487 }
488 #endif /* __cplusplus */
489
490 #endif /* packet.h */