From Tom Brezinski:
[obnox/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   gint           i2c_event;
74   gint           i2c_data;
75 } packet_counts;
76
77 /** Number of packet counts. */
78 #define PACKET_COUNTS_SIZE sizeof(packet_counts) / sizeof (gint)
79
80 /* Types of character encodings */
81 typedef enum {
82         PACKET_CHAR_ENC_CHAR_ASCII       = 0,   /* ASCII */
83         PACKET_CHAR_ENC_CHAR_EBCDIC      = 1    /* EBCDIC */
84 } packet_char_enc;
85
86 extern void packet_init(void);
87 extern void packet_cleanup(void);
88
89 /* Handle for dissectors you call directly or register with "dissector_add_uint()".
90    This handle is opaque outside of "packet.c". */
91 struct dissector_handle;
92 typedef struct dissector_handle *dissector_handle_t;
93
94 /* Hash table for matching unsigned integers, or strings, and dissectors;
95    this is opaque outside of "packet.c". */
96 struct dissector_table;
97 typedef struct dissector_table *dissector_table_t;
98
99 /*
100  * Dissector that returns nothing.
101  */
102 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
103
104 /*
105  * Dissector that returns:
106  *
107  *      The amount of data in the protocol's PDU, if it was able to
108  *      dissect all the data;
109  *
110  *      0, if the tvbuff doesn't contain a PDU for that protocol;
111  *
112  *      The negative of the amount of additional data needed, if
113  *      we need more data (e.g., from subsequent TCP segments) to
114  *      dissect the entire PDU.
115  */
116 typedef int (*new_dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
117
118 /** Type of a heuristic dissector, used in heur_dissector_add().
119  *
120  * @param tvb the tv_buff with the (remaining) packet data
121  * @param pinfo the packet info of this packet (additional info)
122  * @param tree the protocol tree to be build or NULL
123  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
124  */
125 typedef gboolean (*heur_dissector_t)(tvbuff_t *tvb, packet_info *pinfo,
126         proto_tree *tree);
127
128 typedef void (*DATFunc) (const gchar *table_name, ftenum_t selector_type,
129     gpointer key, gpointer value, gpointer user_data);
130 typedef void (*DATFunc_handle) (const gchar *table_name, gpointer value,
131     gpointer user_data);
132 typedef void (*DATFunc_table) (const gchar *table_name, const gchar *ui_name,
133     gpointer user_data);
134
135 typedef void (*DATFunc_heur_table) (const gchar *table_name,gpointer table,
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 (const char *name, DATFunc func,
144     gpointer user_data);
145 extern void dissector_table_foreach (const 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(const 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     const char *ui_name, const ftenum_t type, const 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 const 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_uint(const char *abbrev, const guint32 pattern,
174     dissector_handle_t handle);
175
176 /* For old code that hasn't yet been changed. */
177 #define dissector_add(abbrev, pattern, handle) \
178         dissector_add_uint(abbrev, pattern, handle)
179
180 /* Delete the entry for a dissector in a uint dissector table
181    with a particular pattern. */
182 extern void dissector_delete_uint(const char *name, const guint32 pattern,
183     dissector_handle_t handle);
184
185 /* For old code that hasn't yet been changed. */
186 #define dissector_delete(name, pattern, handle) \
187         dissector_delete_uint(name, pattern, handle)
188
189 /* Change the entry for a dissector in a uint dissector table
190    with a particular pattern to use a new dissector handle. */
191 extern void dissector_change_uint(const char *abbrev, const guint32 pattern,
192     dissector_handle_t handle);
193
194 /* For old code that hasn't yet been changed. */
195 #define dissector_change(abbrev, pattern, handle) \
196         dissector_change_uint(abbrev, pattern, handle)
197
198 /* Reset an entry in a uint dissector table to its initial value. */
199 extern void dissector_reset_uint(const char *name, const guint32 pattern);
200
201 /* For old code that hasn't yet been changed. */
202 #define dissector_reset(name, pattern) \
203         dissector_reset_uint(name, pattern)
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 extern gboolean dissector_try_uint(dissector_table_t sub_dissectors,
209     const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
210
211 /* For old code that hasn't yet been changed. */
212 #define dissector_try_port(sub_dissectors, uint_val, tvb, pinfo, tree) \
213         dissector_try_uint(sub_dissectors, uint_val, tvb, pinfo, tree)
214
215 /* Look for a given value in a given uint dissector table and, if found,
216    call the dissector with the arguments supplied, and return TRUE,
217    otherwise return FALSE. */
218 extern gboolean dissector_try_uint_new(dissector_table_t sub_dissectors,
219         const guint32 uint_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const gboolean add_proto_name);
220
221 /* For old code that hasn't yet been changed. */
222 #define dissector_try_port_new(sub_dissectors, uint_val, tvb, pinfo, tree, add_proto_name) \
223         dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, add_proto_name)
224
225 /* Look for a given value in a given uint dissector table and, if found,
226    return the dissector handle for that value. */
227 extern dissector_handle_t dissector_get_uint_handle(
228     dissector_table_t const sub_dissectors, const guint32 uint_val);
229
230 /* For old code that hasn't yet been changed. */
231 #define dissector_get_port_handle(sub_dissectors, uint_val) \
232         dissector_get_uint_handle(sub_dissectors, uint_val)
233
234 /* Add an entry to a string dissector table. */
235 extern void dissector_add_string(const char *name, const gchar *pattern,
236     dissector_handle_t handle);
237
238 /* Delete the entry for a dissector in a string dissector table
239    with a particular pattern. */
240 extern void dissector_delete_string(const char *name, const gchar *pattern,
241         dissector_handle_t handle);
242
243 /* Change the entry for a dissector in a string dissector table
244    with a particular pattern to use a new dissector handle. */
245 extern void dissector_change_string(const char *name, const gchar *pattern,
246     dissector_handle_t handle);
247
248 /* Reset an entry in a string sub-dissector table to its initial value. */
249 extern void dissector_reset_string(const char *name, const gchar *pattern);
250
251 /* Look for a given string in a given dissector table and, if found, call
252    the dissector with the arguments supplied, and return TRUE, otherwise
253    return FALSE. */
254 extern gboolean dissector_try_string(dissector_table_t sub_dissectors,
255     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
256
257 /* Look for a given value in a given string dissector table and, if found,
258    return the dissector handle for that value. */
259 extern dissector_handle_t dissector_get_string_handle(
260     dissector_table_t sub_dissectors, const gchar *string);
261
262 /* Add a handle to the list of handles that *could* be used with this
263    table.  That list is used by code in the UI. */
264 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
265
266 /* List of "heuristic" dissectors (which get handed a packet, look at it,
267    and either recognize it as being for their protocol, dissect it, and
268    return TRUE, or don't recognize it and return FALSE) to be called
269    by another dissector. */
270 typedef GSList *heur_dissector_list_t;
271
272
273 typedef struct {
274         heur_dissector_t dissector;
275         protocol_t *protocol;
276         gboolean enabled;
277 } heur_dtbl_entry_t;
278
279 /** A protocol uses this function to register a heuristic sub-dissector list.
280  *  Call this in the parent dissectors proto_register function.
281  *
282  * @param name the name of this protocol
283  * @param list the list of heuristic sub-dissectors to be registered
284  */
285 extern void register_heur_dissector_list(const char *name,
286     heur_dissector_list_t *list);
287
288 extern void dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
289     gpointer user_data);
290
291 /** Try all the dissectors in a given heuristic dissector list. This is done,
292  *  until we find one that recognizes the protocol.
293  *  Call this while the parent dissector running.
294  *
295  * @param sub_dissectors the sub-dissector list
296  * @param tvb the tv_buff with the (remaining) packet data
297  * @param pinfo the packet info of this packet (additional info)
298  * @param tree the protocol tree to be build or NULL
299  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
300  */
301 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
302     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
303
304 /** Add a sub-dissector to a heuristic dissector list.
305  *  Call this in the proto_handoff function of the sub-dissector.
306  *
307  * @param name the name of the "parent" protocol, e.g. "tcp"
308  * @param dissector the sub-dissector to be registered
309  * @param proto the protocol id of the sub-dissector
310  */
311 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
312     const int proto);
313
314 /** Remove a sub-dissector from a heuristic dissector list.
315  *  Call this in the prefs_reinit function of the sub-dissector.
316  *
317  * @param name the name of the "parent" protocol, e.g. "tcp"
318  * @param dissector the sub-dissector to be unregistered
319  * @param proto the protocol id of the sub-dissector
320  */
321 extern void heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto);
322
323 /** Enable/Disable a sub-dissector in a heuristic dissector list
324  *  Call this in the prefs_reinit function of the sub-dissector.
325  *
326  * @param name the name of the "parent" protocol, e.g. "tcp"
327  * @param dissector the sub-dissector to be disabled/enabled
328  * @param proto the protocol id of the sub-dissector
329  * @param TRUE/FALSE to enable/disable the sub-dissector
330  */
331 extern void heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled);
332
333 /* Register a dissector. */
334 extern void register_dissector(const char *name, dissector_t dissector,
335     const int proto);
336 extern void new_register_dissector(const char *name, new_dissector_t dissector,
337     const int proto);
338
339 /* Get the long name of the protocol for a dissector handle. */
340 extern const char *dissector_handle_get_long_name(const dissector_handle_t handle);
341
342 /* Get the short name of the protocol for a dissector handle. */
343 extern const char *dissector_handle_get_short_name(const dissector_handle_t handle);
344
345 /* Get the index of the protocol for a dissector handle. */
346 extern int dissector_handle_get_protocol_index(const dissector_handle_t handle);
347
348 /* Find a dissector by name. */
349 extern dissector_handle_t find_dissector(const char *name);
350
351 /* Create an anonymous handle for a dissector. */
352 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
353     const int proto);
354 extern dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
355     const int proto);
356
357 /* Call a dissector through a handle and if no dissector was found
358  * pass if over to the "data" dissector instead.
359  *
360  *   @param handle The dissector to call.
361  *   @param  tvb The buffer to dissect.
362  *   @param  pinfo Packet Info.
363  *   @param  tree The protocol tree.
364  *   @return  If the protocol for that handle isn't enabled call the data
365  *   dissector. Otherwise, if the handle refers to a new-style
366  *   dissector, call the dissector and return its return value, otherwise call
367  *   it and return the length of the tvbuff pointed to by the argument.
368  */
369 extern int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
370     packet_info *pinfo, proto_tree *tree);
371
372 /* Call a dissector through a handle but if no dissector was found
373  * just return 0 and do not call the "data" dissector instead.
374  *
375  *   @param handle The dissector to call.
376  *   @param  tvb The buffer to dissect.
377  *   @param  pinfo Packet Info.
378  *   @param  tree The protocol tree.
379  *   @return  If the protocol for that handle isn't enabled, return 0 without
380  *   calling the dissector. Otherwise, if the handle refers to a new-style
381  *   dissector, call the dissector and return its return value, otherwise call
382  *   it and return the length of the tvbuff pointed to by the argument.
383  */
384 extern int call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
385     packet_info *pinfo, proto_tree *tree);
386
387 /* Do all one-time initialization. */
388 extern void dissect_init(void);
389
390 extern void dissect_cleanup(void);
391
392 /*
393  * Given a tvbuff, and a length from a packet header, adjust the length
394  * of the tvbuff to reflect the specified length.
395  */
396 extern void set_actual_length(tvbuff_t *tvb, const guint specified_len);
397
398 /* Allow protocols to register "init" routines, which are called before
399    we make a pass through a capture file and dissect all its packets
400    (e.g., when we read in a new capture file, or run a "filter packets"
401    or "colorize packets" pass over the current capture file). */
402 extern void register_init_routine(void (*func)(void));
403
404 /* Initialize all data structures used for dissection. */
405 extern void init_dissection(void);
406
407 /* Free data structures allocated for dissection. */
408 extern void cleanup_dissection(void);
409
410 /* Allow protocols to register a "cleanup" routine to be
411  * run after the initial sequential run through the packets.
412  * Note that the file can still be open after this; this is not
413  * the final cleanup. */
414 extern void register_postseq_cleanup_routine(void (*func)(void));
415
416 /* Call all the registered "postseq_cleanup" routines. */
417 extern void postseq_cleanup_all_protocols(void);
418
419 /* Allow dissectors to register a "final_registration" routine
420  * that is run like the proto_register_XXX() routine, but the end
421  * end of the epan_init() function; that is, *after* all other
422  * subsystems, liked dfilters, have finished initializing. This is
423  * useful for dissector registration routines which need to compile
424  * display filters. dfilters can't initialize itself until all protocols
425  * have registereed themselvs. */
426 extern void
427 register_final_registration_routine(void (*func)(void));
428
429 /* Call all the registered "final_registration" routines. */
430 extern void
431 final_registration_all_protocols(void);
432
433 /*
434  * Add a new data source to the list of data sources for a frame, given
435  * the tvbuff for the data source and its name.
436  */
437 extern void add_new_data_source(packet_info *pinfo, tvbuff_t *tvb,
438     const char *name);
439
440 /*
441  * Return the data source name.
442  */
443 extern const char* get_data_source_name(data_source *src);
444
445 /*
446  * Free up a frame's list of data sources.
447  */
448 extern void free_data_sources(packet_info *pinfo);
449
450 /*
451  * Dissectors should never modify the packet data.
452  */
453 extern void dissect_packet(epan_dissect_t *edt,
454     union wtap_pseudo_header *pseudo_header, const guchar *pd,
455     frame_data *fd, column_info *cinfo);
456
457 /* These functions are in packet-ethertype.c */
458 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
459                 int len, packet_counts *ld);
460 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
461                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
462                 int etype_id, int trailer_id, int fcs_len);
463
464 /*
465  * Dump layer/selector/dissector records in a fashion similar to the
466  * proto_registrar_dump_* routines.
467  */
468 extern void dissector_dump_decodes(void);
469
470 /*
471  * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
472  */
473 extern void dissector_dump_heur_decodes(void);
474
475 /*
476  * post dissectors are to be called by packet-frame.c after every other
477  * dissector has been called.
478  */
479 extern void register_postdissector(dissector_handle_t);
480 extern gboolean have_postdissector(void);
481 extern void call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
482
483 #ifdef __cplusplus
484 }
485 #endif /* __cplusplus */
486
487 #endif /* packet.h */