2 * Definitions for packet disassembly structures and routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
28 #include "wiretap/wtap.h"
31 #include "value_string.h"
32 #include "column-info.h"
33 #include "frame_data.h"
34 #include "packet_info.h"
35 #include "column-utils.h"
38 #include "ws_symbol_export.h"
43 #endif /* __cplusplus */
45 /** @defgroup packet General Packet Dissection
50 #define hi_nibble(b) (((b) & 0xf0) >> 4)
51 #define lo_nibble(b) ((b) & 0x0f)
53 /* Useful when you have an array whose size you can tell at compile-time */
54 #define array_length(x) (sizeof x / sizeof x[0])
56 /* Check whether the "len" bytes of data starting at "offset" is
57 * entirely inside the captured data for this packet. */
58 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
59 ((guint)(offset) + (guint)(len) > (guint)(offset) && \
60 (guint)(offset) + (guint)(len) <= (guint)(captured_len))
62 /* To pass one of two strings, singular or plural */
63 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
65 typedef struct _packet_counts
{
82 /** Number of packet counts. */
83 #define PACKET_COUNTS_SIZE sizeof(packet_counts) / sizeof (gint)
85 extern void packet_init(void);
86 extern void packet_cache_proto_handles(void);
87 extern void packet_cleanup(void);
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
;
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
;
100 * Dissector that returns nothing.
102 typedef void (*dissector_t
)(tvbuff_t
*, packet_info
*, proto_tree
*);
105 * Dissector that returns:
107 * The amount of data in the protocol's PDU, if it was able to
108 * dissect all the data;
110 * 0, if the tvbuff doesn't contain a PDU for that protocol;
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.
116 typedef int (*new_dissector_t
)(tvbuff_t
*, packet_info
*, proto_tree
*, void *);
118 /** Type of a heuristic dissector, used in heur_dissector_add().
120 * @param tvb the tvbuff 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)
125 typedef gboolean (*heur_dissector_t
)(tvbuff_t
*tvb
, packet_info
*pinfo
,
126 proto_tree
*tree
, void *);
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
,
132 typedef void (*DATFunc_table
) (const gchar
*table_name
, const gchar
*ui_name
,
135 typedef void (*DATFunc_heur_table
) (const gchar
*table_name
,gpointer table
,
138 /* Opaque structure - provides type checking but no access to components */
139 typedef struct dtbl_entry dtbl_entry_t
;
141 WS_DLL_PUBLIC dissector_handle_t
dtbl_entry_get_handle (dtbl_entry_t
*dtbl_entry
);
142 WS_DLL_PUBLIC dissector_handle_t
dtbl_entry_get_initial_handle (dtbl_entry_t
* entry
);
143 void dissector_table_foreach_changed (const char *name
, DATFunc func
,
145 WS_DLL_PUBLIC
void dissector_table_foreach (const char *name
, DATFunc func
,
147 WS_DLL_PUBLIC
void dissector_all_tables_foreach_changed (DATFunc func
,
149 WS_DLL_PUBLIC
void dissector_table_foreach_handle(const char *name
, DATFunc_handle func
,
151 WS_DLL_PUBLIC
void dissector_all_tables_foreach_table (DATFunc_table func
,
152 gpointer user_data
, GCompareFunc compare_key_func
);
154 /* a protocol uses the function to register a sub-dissector table */
155 WS_DLL_PUBLIC dissector_table_t
register_dissector_table(const char *name
,
156 const char *ui_name
, const ftenum_t type
, const int base
);
158 /* Find a dissector table by table name. */
159 WS_DLL_PUBLIC dissector_table_t
find_dissector_table(const char *name
);
161 /* Get the UI name for a sub-dissector table, given its internal name */
162 WS_DLL_PUBLIC
const char *get_dissector_table_ui_name(const char *name
);
164 /* Get the field type for values of the selector for a dissector table,
165 given the table's internal name */
166 WS_DLL_PUBLIC ftenum_t
get_dissector_table_selector_type(const char *name
);
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 WS_DLL_PUBLIC
int get_dissector_table_base(const char *name
);
172 /* Add an entry to a uint dissector table. */
173 WS_DLL_PUBLIC
void dissector_add_uint(const char *abbrev
, const guint32 pattern
,
174 dissector_handle_t handle
);
176 /* Add an range of entries to a uint dissector table. */
177 WS_DLL_PUBLIC
void dissector_add_uint_range(const char *abbrev
, range_t
*range
,
178 dissector_handle_t handle
);
180 /* Delete the entry for a dissector in a uint dissector table
181 with a particular pattern. */
182 WS_DLL_PUBLIC
void dissector_delete_uint(const char *name
, const guint32 pattern
,
183 dissector_handle_t handle
);
185 /* Delete an range of entries from a uint dissector table. */
186 WS_DLL_PUBLIC
void dissector_delete_uint_range(const char *abbrev
, range_t
*range
,
187 dissector_handle_t handle
);
189 /* Delete all entries from a dissector table. */
190 WS_DLL_PUBLIC
void dissector_delete_all(const char *name
, dissector_handle_t handle
);
192 /* Change the entry for a dissector in a uint dissector table
193 with a particular pattern to use a new dissector handle. */
194 WS_DLL_PUBLIC
void dissector_change_uint(const char *abbrev
, const guint32 pattern
,
195 dissector_handle_t handle
);
197 /* Reset an entry in a uint dissector table to its initial value. */
198 WS_DLL_PUBLIC
void dissector_reset_uint(const char *name
, const guint32 pattern
);
200 /* Look for a given value in a given uint dissector table and, if found,
201 call the dissector with the arguments supplied, and return TRUE,
202 otherwise return FALSE. */
203 WS_DLL_PUBLIC gboolean
dissector_try_uint(dissector_table_t sub_dissectors
,
204 const guint32 uint_val
, tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
);
206 /* Look for a given value in a given uint dissector table and, if found,
207 call the dissector with the arguments supplied, and return TRUE,
208 otherwise return FALSE. */
209 WS_DLL_PUBLIC gboolean
dissector_try_uint_new(dissector_table_t sub_dissectors
,
210 const guint32 uint_val
, tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, const gboolean add_proto_name
, void *data
);
212 /* Look for a given value in a given uint dissector table and, if found,
213 return the dissector handle for that value. */
214 WS_DLL_PUBLIC dissector_handle_t
dissector_get_uint_handle(
215 dissector_table_t
const sub_dissectors
, const guint32 uint_val
);
217 /* Add an entry to a string dissector table. */
218 WS_DLL_PUBLIC
void dissector_add_string(const char *name
, const gchar
*pattern
,
219 dissector_handle_t handle
);
221 /* Delete the entry for a dissector in a string dissector table
222 with a particular pattern. */
223 WS_DLL_PUBLIC
void dissector_delete_string(const char *name
, const gchar
*pattern
,
224 dissector_handle_t handle
);
226 /* Change the entry for a dissector in a string dissector table
227 with a particular pattern to use a new dissector handle. */
228 WS_DLL_PUBLIC
void dissector_change_string(const char *name
, const gchar
*pattern
,
229 dissector_handle_t handle
);
231 /* Reset an entry in a string sub-dissector table to its initial value. */
232 WS_DLL_PUBLIC
void dissector_reset_string(const char *name
, const gchar
*pattern
);
234 /* Look for a given string in a given dissector table and, if found, call
235 the dissector with the arguments supplied, and return TRUE, otherwise
237 WS_DLL_PUBLIC gboolean
dissector_try_string(dissector_table_t sub_dissectors
,
238 const gchar
*string
, tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
);
240 /* Look for a given value in a given string dissector table and, if found,
241 return the dissector handle for that value. */
242 WS_DLL_PUBLIC dissector_handle_t
dissector_get_string_handle(
243 dissector_table_t sub_dissectors
, const gchar
*string
);
245 /* Add a handle to the list of handles that *could* be used with this
246 table. That list is used by code in the UI. */
247 WS_DLL_PUBLIC
void dissector_add_handle(const char *name
, dissector_handle_t handle
);
249 /* List of "heuristic" dissectors (which get handed a packet, look at it,
250 and either recognize it as being for their protocol, dissect it, and
251 return TRUE, or don't recognize it and return FALSE) to be called
252 by another dissector. */
253 typedef GSList
*heur_dissector_list_t
;
257 heur_dissector_t dissector
;
258 protocol_t
*protocol
;
262 /** A protocol uses this function to register a heuristic sub-dissector list.
263 * Call this in the parent dissectors proto_register function.
265 * @param name the name of this protocol
266 * @param list the list of heuristic sub-dissectors to be registered
268 WS_DLL_PUBLIC
void register_heur_dissector_list(const char *name
,
269 heur_dissector_list_t
*list
);
271 WS_DLL_PUBLIC
void dissector_all_heur_tables_foreach_table (DATFunc_heur_table func
,
274 /** Try all the dissectors in a given heuristic dissector list. This is done,
275 * until we find one that recognizes the protocol.
276 * Call this while the parent dissector running.
278 * @param sub_dissectors the sub-dissector list
279 * @param tvb the tvbuff with the (remaining) packet data
280 * @param pinfo the packet info of this packet (additional info)
281 * @param tree the protocol tree to be build or NULL
282 * @param data parameter to pass to subdissector
283 * @return TRUE if the packet was recognized by the sub-dissector (stop dissection here)
285 WS_DLL_PUBLIC gboolean
dissector_try_heuristic(heur_dissector_list_t sub_dissectors
,
286 tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
);
288 /** Add a sub-dissector to a heuristic dissector list.
289 * Call this in the proto_handoff function of the sub-dissector.
291 * @param name the name of the "parent" protocol, e.g. "tcp"
292 * @param dissector the sub-dissector to be registered
293 * @param proto the protocol id of the sub-dissector
295 WS_DLL_PUBLIC
void heur_dissector_add(const char *name
, heur_dissector_t dissector
,
298 /** Remove a sub-dissector from a heuristic dissector list.
299 * Call this in the prefs_reinit function of the sub-dissector.
301 * @param name the name of the "parent" protocol, e.g. "tcp"
302 * @param dissector the sub-dissector to be unregistered
303 * @param proto the protocol id of the sub-dissector
305 WS_DLL_PUBLIC
void heur_dissector_delete(const char *name
, heur_dissector_t dissector
, const int proto
);
307 /** Enable/Disable a sub-dissector in a heuristic dissector list
308 * Call this in the prefs_reinit function of the sub-dissector.
310 * @param name the name of the "parent" protocol, e.g. "tcp"
311 * @param dissector the sub-dissector to be disabled/enabled
312 * @param proto the protocol id of the sub-dissector
313 * @param enabled TRUE/FALSE to enable/disable the sub-dissector
315 extern void heur_dissector_set_enabled(const char *name
, heur_dissector_t dissector
, const int proto
, const gboolean enabled
);
317 /* Register a dissector. */
318 WS_DLL_PUBLIC dissector_handle_t
register_dissector(const char *name
, dissector_t dissector
,
320 WS_DLL_PUBLIC dissector_handle_t
new_register_dissector(const char *name
, new_dissector_t dissector
,
323 /* Get the long name of the protocol for a dissector handle. */
324 extern const char *dissector_handle_get_long_name(const dissector_handle_t handle
);
326 /* Get the short name of the protocol for a dissector handle. */
327 WS_DLL_PUBLIC
const char *dissector_handle_get_short_name(const dissector_handle_t handle
);
329 /* Get the index of the protocol for a dissector handle. */
330 WS_DLL_PUBLIC
int dissector_handle_get_protocol_index(const dissector_handle_t handle
);
332 /* Find a dissector by name. */
333 WS_DLL_PUBLIC dissector_handle_t
find_dissector(const char *name
);
335 /* Get a dissector name from handle. */
336 WS_DLL_PUBLIC
const char *dissector_handle_get_dissector_name(const dissector_handle_t handle
);
338 /* Create an anonymous handle for a dissector. */
339 WS_DLL_PUBLIC dissector_handle_t
create_dissector_handle(dissector_t dissector
,
341 WS_DLL_PUBLIC dissector_handle_t
new_create_dissector_handle(new_dissector_t dissector
,
344 /* Call a dissector through a handle and if no dissector was found
345 * pass it over to the "data" dissector instead.
347 * @param handle The dissector to call.
348 * @param tvb The buffer to dissect.
349 * @param pinfo Packet Info.
350 * @param tree The protocol tree.
351 * @param data parameter to pass to dissector
352 * @return If the protocol for that handle isn't enabled call the data
353 * dissector. Otherwise, if the handle refers to a new-style
354 * dissector, call the dissector and return its return value, otherwise call
355 * it and return the length of the tvbuff pointed to by the argument.
357 WS_DLL_PUBLIC
int call_dissector_with_data(dissector_handle_t handle
, tvbuff_t
*tvb
,
358 packet_info
*pinfo
, proto_tree
*tree
, void *data
);
359 WS_DLL_PUBLIC
int call_dissector(dissector_handle_t handle
, tvbuff_t
*tvb
,
360 packet_info
*pinfo
, proto_tree
*tree
);
362 /* Call a dissector through a handle but if no dissector was found
363 * just return 0 and do not call the "data" dissector instead.
365 * @param handle The dissector to call.
366 * @param tvb The buffer to dissect.
367 * @param pinfo Packet Info.
368 * @param tree The protocol tree.
369 * @param data parameter to pass to dissector
370 * @return If the protocol for that handle isn't enabled, return 0 without
371 * calling the dissector. Otherwise, if the handle refers to a new-style
372 * dissector, call the dissector and return its return value, otherwise call
373 * it and return the length of the tvbuff pointed to by the argument.
375 WS_DLL_PUBLIC
int call_dissector_only(dissector_handle_t handle
, tvbuff_t
*tvb
,
376 packet_info
*pinfo
, proto_tree
*tree
, void *data
);
378 /* Do all one-time initialization. */
379 extern void dissect_init(void);
381 extern void dissect_cleanup(void);
384 * Given a tvbuff, and a length from a packet header, adjust the length
385 * of the tvbuff to reflect the specified length.
387 WS_DLL_PUBLIC
void set_actual_length(tvbuff_t
*tvb
, const guint specified_len
);
389 /* Allow protocols to register "init" routines, which are called before
390 we make a pass through a capture file and dissect all its packets
391 (e.g., when we read in a new capture file, or run a "filter packets"
392 or "colorize packets" pass over the current capture file). */
393 WS_DLL_PUBLIC
void register_init_routine(void (*func
)(void));
395 /* Initialize all data structures used for dissection. */
396 void init_dissection(void);
398 /* Free data structures allocated for dissection. */
399 void cleanup_dissection(void);
401 /* Allow protocols to register a "cleanup" routine to be
402 * run after the initial sequential run through the packets.
403 * Note that the file can still be open after this; this is not
404 * the final cleanup. */
405 WS_DLL_PUBLIC
void register_postseq_cleanup_routine(void (*func
)(void));
407 /* Call all the registered "postseq_cleanup" routines. */
408 WS_DLL_PUBLIC
void postseq_cleanup_all_protocols(void);
410 /* Allow dissectors to register a "final_registration" routine
411 * that is run like the proto_register_XXX() routine, but the end
412 * end of the epan_init() function; that is, *after* all other
413 * subsystems, liked dfilters, have finished initializing. This is
414 * useful for dissector registration routines which need to compile
415 * display filters. dfilters can't initialize itself until all protocols
416 * have registereed themselvs. */
418 register_final_registration_routine(void (*func
)(void));
420 /* Call all the registered "final_registration" routines. */
422 final_registration_all_protocols(void);
425 * Add a new data source to the list of data sources for a frame, given
426 * the tvbuff for the data source and its name.
428 WS_DLL_PUBLIC
void add_new_data_source(packet_info
*pinfo
, tvbuff_t
*tvb
,
433 * Return the data source name, tvb.
436 WS_DLL_PUBLIC
const char *get_data_source_name(const struct data_source
*src
);
437 WS_DLL_PUBLIC tvbuff_t
*get_data_source_tvb(const struct data_source
*src
);
440 * Free up a frame's list of data sources.
442 extern void free_data_sources(packet_info
*pinfo
);
444 /* Mark another frame as depended upon by the current frame.
446 * This information is used to ensure that the dependend-upon frame is saved
447 * if the user does a File->Save-As of only the Displayed packets and the
448 * current frame passed the display filter.
450 WS_DLL_PUBLIC
void mark_frame_as_depended_upon(packet_info
*pinfo
, guint32 frame_num
);
453 * Dissectors should never modify the packet data.
455 extern void dissect_packet(epan_dissect_t
*edt
,
456 struct wtap_pkthdr
*phdr
, tvbuff_t
*tvb
,
457 frame_data
*fd
, column_info
*cinfo
);
459 /* These functions are in packet-ethertype.c */
460 extern void capture_ethertype(guint16 etype
, const guchar
*pd
, int offset
,
461 int len
, packet_counts
*ld
);
462 WS_DLL_PUBLIC
void ethertype(guint16 etype
, tvbuff_t
*tvb
, int offset_after_ethertype
,
463 packet_info
*pinfo
, proto_tree
*tree
, proto_tree
*fh_tree
,
464 int etype_id
, int trailer_id
, int fcs_len
);
467 * Dump layer/selector/dissector records in a fashion similar to the
468 * proto_registrar_dump_* routines.
470 WS_DLL_PUBLIC
void dissector_dump_decodes(void);
473 * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
475 WS_DLL_PUBLIC
void dissector_dump_heur_decodes(void);
478 * post dissectors are to be called by packet-frame.c after every other
479 * dissector has been called.
481 WS_DLL_PUBLIC
void register_postdissector(dissector_handle_t
);
482 extern gboolean
have_postdissector(void);
483 extern void call_all_postdissectors(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
);
489 #endif /* __cplusplus */
491 #endif /* packet.h */