This patch introduces packet_add_new_data_source() which effectively deprecates add_n...
[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         CHAR_ASCII       = 0,   /* ASCII */
83         CHAR_EBCDIC      = 1    /* EBCDIC */
84 } 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()".
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 port numbers and dissectors; this is opaque
95    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 /* Opaque structure - provides type checking but no access to components */
136 typedef struct dtbl_entry dtbl_entry_t;
137
138 extern dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
139 extern dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
140 extern void dissector_table_foreach_changed (const char *name, DATFunc func,
141     gpointer user_data);
142 extern void dissector_table_foreach (const char *name, DATFunc func,
143     gpointer user_data);
144 extern void dissector_all_tables_foreach_changed (DATFunc func,
145     gpointer user_data);
146 extern void dissector_table_foreach_handle(const char *name, DATFunc_handle func,
147     gpointer user_data);
148 extern void dissector_all_tables_foreach_table (DATFunc_table func,
149     gpointer user_data);
150
151 /* a protocol uses the function to register a sub-dissector table */
152 extern dissector_table_t register_dissector_table(const char *name,
153     const char *ui_name, ftenum_t type, int base);
154
155 /* Find a dissector table by table name. */
156 extern dissector_table_t find_dissector_table(const char *name);
157
158 /* Get the UI name for a sub-dissector table, given its internal name */
159 extern const char *get_dissector_table_ui_name(const char *name);
160
161 /* Get the field type for values of the selector for a dissector table,
162    given the table's internal name */
163 extern ftenum_t get_dissector_table_selector_type(const char *name);
164
165 /* Get the base to use when displaying values of the selector for a
166    sub-dissector table, given the table's internal name */
167 extern int get_dissector_table_base(const char *name);
168
169 /* Add an entry to a uint dissector table. */
170 extern void dissector_add(const char *abbrev, guint32 pattern,
171     dissector_handle_t handle);
172
173 /* Delete the entry for a dissector in a uint dissector table
174    with a particular pattern. */
175 extern void dissector_delete(const char *name, guint32 pattern,
176     dissector_handle_t handle);
177
178 /* Change the entry for a dissector in a uint dissector table
179    with a particular pattern to use a new dissector handle. */
180 extern void dissector_change(const char *abbrev, guint32 pattern,
181     dissector_handle_t handle);
182
183 /* Reset an entry in a uint dissector table to its initial value. */
184 extern void dissector_reset(const char *name, guint32 pattern);
185
186 /* Look for a given value in a given uint dissector table and, if found,
187    call the dissector with the arguments supplied, and return TRUE,
188    otherwise return FALSE. */
189 extern gboolean dissector_try_port(dissector_table_t sub_dissectors,
190     guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
191
192 /* Look for a given value in a given uint dissector table and, if found,
193    call the dissector with the arguments supplied, and return TRUE,
194    otherwise return FALSE. */
195 extern gboolean dissector_try_port_new(dissector_table_t sub_dissectors, 
196         guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean add_proto_name);
197
198 /* Look for a given value in a given uint dissector table and, if found,
199    return the dissector handle for that value. */
200 extern dissector_handle_t dissector_get_port_handle(
201     dissector_table_t sub_dissectors, guint32 port);
202
203 /* Add an entry to a string dissector table. */
204 extern void dissector_add_string(const char *name, const gchar *pattern,
205     dissector_handle_t handle);
206
207 /* Delete the entry for a dissector in a string dissector table
208    with a particular pattern. */
209 extern void dissector_delete_string(const char *name, const gchar *pattern,
210         dissector_handle_t handle);
211
212 /* Change the entry for a dissector in a string dissector table
213    with a particular pattern to use a new dissector handle. */
214 extern void dissector_change_string(const char *name, gchar *pattern,
215     dissector_handle_t handle);
216
217 /* Reset an entry in a string sub-dissector table to its initial value. */
218 extern void dissector_reset_string(const char *name, const gchar *pattern);
219
220 /* Look for a given string in a given dissector table and, if found, call
221    the dissector with the arguments supplied, and return TRUE, otherwise
222    return FALSE. */
223 extern gboolean dissector_try_string(dissector_table_t sub_dissectors,
224     const gchar *string, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
225
226 /* Look for a given value in a given string dissector table and, if found,
227    return the dissector handle for that value. */
228 extern dissector_handle_t dissector_get_string_handle(
229     dissector_table_t sub_dissectors, const gchar *string);
230
231 /* Add a handle to the list of handles that *could* be used with this
232    table.  That list is used by code in the UI. */
233 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
234
235 /* List of "heuristic" dissectors (which get handed a packet, look at it,
236    and either recognize it as being for their protocol, dissect it, and
237    return TRUE, or don't recognize it and return FALSE) to be called
238    by another dissector. */
239 typedef GSList *heur_dissector_list_t;
240
241 /** A protocol uses this function to register a heuristic sub-dissector list.
242  *  Call this in the parent dissectors proto_register function.
243  *
244  * @param name the name of this protocol
245  * @param list the list of heuristic sub-dissectors to be registered
246  */
247 extern void register_heur_dissector_list(const char *name,
248     heur_dissector_list_t *list);
249
250 /** Try all the dissectors in a given heuristic dissector list. This is done,
251  *  until we find one that recognizes the protocol.
252  *  Call this while the parent dissector running.
253  *
254  * @param sub_dissectors the sub-dissector list
255  * @param tvb the tv_buff with the (remaining) packet data
256  * @param pinfo the packet info of this packet (additional info)
257  * @param tree the protocol tree to be build or NULL
258  * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
259  */
260 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
261     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
262
263 /** Add a sub-dissector to a heuristic dissector list.
264  *  Call this in the proto_handoff function of the sub-dissector.
265  *
266  * @param name the name of the "parent" protocol, e.g. "tcp"
267  * @param dissector the sub-dissector to be registered
268  * @param proto the protocol id of the sub-dissector
269  */
270 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
271     int proto);
272
273 /** Remove a sub-dissector from a heuristic dissector list.
274  *  Call this in the prefs_reinit function of the sub-dissector.
275  *
276  * @param name the name of the "parent" protocol, e.g. "tcp"
277  * @param dissector the sub-dissector to be unregistered
278  * @param proto the protocol id of the sub-dissector
279  */
280 extern void heur_dissector_delete(const char *name, heur_dissector_t dissector, int proto);
281
282 /* Register a dissector. */
283 extern void register_dissector(const char *name, dissector_t dissector,
284     int proto);
285 extern void new_register_dissector(const char *name, new_dissector_t dissector,
286     int proto);
287
288 /* Get the short name of the protocol for a dissector handle. */
289 extern const char *dissector_handle_get_short_name(dissector_handle_t handle);
290
291 /* Get the index of the protocol for a dissector handle. */
292 extern int dissector_handle_get_protocol_index(dissector_handle_t handle);
293
294 /* Find a dissector by name. */
295 extern dissector_handle_t find_dissector(const char *name);
296
297 /* Create an anonymous handle for a dissector. */
298 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
299     int proto);
300 extern dissector_handle_t new_create_dissector_handle(new_dissector_t dissector,
301     int proto);
302
303 /* Call a dissector through a handle and if no dissector was found
304  * pass if over to the "data" dissector instead.
305  *
306  *   @param handle The dissector to call.
307  *   @param  tvb The buffer to dissect.
308  *   @param  pinfo Packet Info.
309  *   @param  tree The protocol tree.
310  *   @return  If the protocol for that handle isn't enabled call the data 
311  *   dissector. Otherwise, if the handle refers to a new-style 
312  *   dissector, call the dissector and return its return value, otherwise call 
313  *   it and return the length of the tvbuff pointed to by the argument.
314  */
315 extern int call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
316     packet_info *pinfo, proto_tree *tree);
317     
318 /* Call a dissector through a handle but if no dissector was found
319  * just return 0 and do not call the "data" dissector instead.
320  *
321  *   @param handle The dissector to call.
322  *   @param  tvb The buffer to dissect.
323  *   @param  pinfo Packet Info.
324  *   @param  tree The protocol tree.
325  *   @return  If the protocol for that handle isn't enabled, return 0 without
326  *   calling the dissector. Otherwise, if the handle refers to a new-style 
327  *   dissector, call the dissector and return its return value, otherwise call 
328  *   it and return the length of the tvbuff pointed to by the argument.
329  */
330 extern int call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
331     packet_info *pinfo, proto_tree *tree);
332
333 /* Do all one-time initialization. */
334 extern void dissect_init(void);
335
336 extern void dissect_cleanup(void);
337
338 /*
339  * Given a tvbuff, and a length from a packet header, adjust the length
340  * of the tvbuff to reflect the specified length.
341  */
342 extern void set_actual_length(tvbuff_t *tvb, guint specified_len);
343
344 /* Allow protocols to register "init" routines, which are called before
345    we make a pass through a capture file and dissect all its packets
346    (e.g., when we read in a new capture file, or run a "filter packets"
347    or "colorize packets" pass over the current capture file). */
348 extern void register_init_routine(void (*func)(void));
349
350 /* Initialize all data structures used for dissection. */
351 extern void init_dissection(void);
352
353 /* Free data structures allocated for dissection. */
354 extern void cleanup_dissection(void);
355
356 /* Allow protocols to register a "cleanup" routine to be
357  * run after the initial sequential run through the packets.
358  * Note that the file can still be open after this; this is not
359  * the final cleanup. */
360 extern void register_postseq_cleanup_routine(void (*func)(void));
361
362 /* Call all the registered "postseq_cleanup" routines. */
363 extern void postseq_cleanup_all_protocols(void);
364
365 /* Allow dissectors to register a "final_registration" routine
366  * that is run like the proto_register_XXX() routine, but the end
367  * end of the epan_init() function; that is, *after* all other
368  * subsystems, liked dfilters, have finished initializing. This is
369  * useful for dissector registration routines which need to compile
370  * display filters. dfilters can't initialize itself until all protocols
371  * have registereed themselvs. */
372 extern void
373 register_final_registration_routine(void (*func)(void));
374
375 /* Call all the registered "final_registration" routines. */
376 extern void
377 final_registration_all_protocols(void);
378
379 /*
380  * DEPRECATED: Use packet_add_new_data_source() instead.
381  */
382 extern void
383 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name);
384
385 /*
386  * Add a new data source to the list of data sources for a frame, given
387  * the tvbuff for the data source and its name.
388  *
389  *   @param  tree The protocol tree.
390  *   @param  pinfo Packet Info.
391  *   @param  tvb The buffer to store.
392  *   @param  name The name of the data source
393  */
394 extern void
395 packet_add_new_data_source(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, const char *name);
396
397 /*
398  * Return the data source name.
399  */
400 extern const char* get_data_source_name(data_source *src);
401
402 /*
403  * Free up a frame's list of data sources.
404  */
405 extern void free_data_sources(packet_info *pinfo);
406
407 /*
408  * Dissectors should never modify the packet data.
409  */
410 extern void dissect_packet(epan_dissect_t *edt,
411     union wtap_pseudo_header *pseudo_header, const guchar *pd,
412     frame_data *fd, column_info *cinfo);
413
414 /* These functions are in packet-ethertype.c */
415 extern void capture_ethertype(guint16 etype, const guchar *pd, int offset,
416                 int len, packet_counts *ld);
417 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
418                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
419                 int etype_id, int trailer_id, int fcs_len);
420
421 /*
422  * Dump layer/selector/dissector records in a fashion similar to the
423  * proto_registrar_dump_* routines.
424  */
425 extern void dissector_dump_decodes(void);
426
427 /*
428  * post dissectors are to be called by packet-frame.c after every other
429  * dissector has been called.
430  */
431 extern void register_postdissector(dissector_handle_t);
432 extern gboolean have_postdissector(void);
433 extern void call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
434
435 #ifdef __cplusplus
436 }
437 #endif /* __cplusplus */
438
439 #endif /* packet.h */