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