2 * Routines for packet disassembly
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.
41 #include "timestamp.h"
43 #include "atalk-utils.h"
44 #include "sna-utils.h"
45 #include "osi-utils.h"
48 #include "addr_resolv.h"
51 #include "epan_dissect.h"
55 #include <epan/reassemble.h>
56 #include <epan/stream.h>
57 #include <epan/expert.h>
59 static gint proto_malformed = -1;
60 static dissector_handle_t frame_handle = NULL;
61 static dissector_handle_t data_handle = NULL;
66 frame_handle = find_dissector("frame");
67 data_handle = find_dissector("data");
68 proto_malformed = proto_get_id_by_filter_name("malformed");
78 * Given a tvbuff, and a length from a packet header, adjust the length
79 * of the tvbuff to reflect the specified length.
82 set_actual_length(tvbuff_t *tvb, const guint specified_len)
84 if (specified_len < tvb_reported_length(tvb)) {
85 /* Adjust the length of this tvbuff to include only the specified
88 The dissector above the one calling us (the dissector above is
89 probably us) may use that to determine how much of its packet
91 tvb_set_reported_length(tvb, specified_len);
95 /* Allow protocols to register "init" routines, which are called before
96 we make a pass through a capture file and dissect all its packets
97 (e.g., when we read in a new capture file, or run a "filter packets"
98 or "colorize packets" pass over the current capture file). */
99 static GSList *init_routines;
102 register_init_routine(void (*func)(void))
104 init_routines = g_slist_append(init_routines, (gpointer)func);
107 typedef void (*void_func_t)(void);
109 /* Initialize all data structures used for dissection. */
111 call_init_routine(gpointer routine, gpointer dummy _U_)
113 void_func_t func = (void_func_t)routine;
118 * XXX - for now, these are the same; the "init" routines free whatever
119 * stuff is left over from any previous dissection, and then initialize
122 * We should probably split that into "init" and "cleanup" routines, for
126 init_dissection(void)
128 /* Reclaim and reinitialize all memory of seasonal scope */
132 * Reinitialize resolution information. We do initialization here in
133 * case we need to resolve between captures.
135 host_name_lookup_cleanup();
136 host_name_lookup_init();
138 /* Initialize the table of conversations. */
139 epan_conversation_init();
141 /* Initialize the table of circuits. */
144 /* Initialize protocol-specific variables. */
145 g_slist_foreach(init_routines, &call_init_routine, NULL);
147 /* Initialize the stream-handling tables */
150 /* Initialize the expert infos */
155 cleanup_dissection(void)
157 /* Cleanup the table of conversations. Do this before freeing seasonal
158 * memory (at least until conversation's use of g_slist is changed).
160 epan_conversation_cleanup();
162 /* Reclaim all memory of seasonal scope */
165 /* Cleanup the table of circuits. */
166 epan_circuit_cleanup();
168 /* TODO: Introduce cleanup_routines */
169 /* Cleanup protocol-specific variables. */
170 g_slist_foreach(init_routines, &call_init_routine, NULL);
172 /* Cleanup the stream-handling tables */
175 /* Initialize the expert infos */
179 * Reinitialize resolution information. We do initialization here in
180 * case we need to resolve between captures.
182 host_name_lookup_cleanup();
183 host_name_lookup_init();
186 /* Allow protocols to register a "cleanup" routine to be
187 * run after the initial sequential run through the packets.
188 * Note that the file can still be open after this; this is not
189 * the final cleanup. */
190 static GSList *postseq_cleanup_routines;
193 register_postseq_cleanup_routine(void_func_t func)
195 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
199 /* Call all the registered "postseq_cleanup" routines. */
201 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
203 void_func_t func = (void_func_t)routine;
208 postseq_cleanup_all_protocols(void)
210 g_slist_foreach(postseq_cleanup_routines,
211 &call_postseq_cleanup_routine, NULL);
215 * Add a new data source to the list of data sources for a frame, given
216 * the tvbuff for the data source and its name.
219 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
223 src = ep_alloc(sizeof (data_source));
225 src->name_initialized = FALSE;
227 pinfo->data_src = g_slist_append(pinfo->data_src, src);
231 get_data_source_name(data_source *src)
233 if (!src->name_initialized) {
234 src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
235 src->name_initialized = TRUE;
242 * Free up a frame's list of data sources.
245 free_data_sources(packet_info *pinfo)
247 if (pinfo->data_src) {
248 g_slist_free(pinfo->data_src);
249 pinfo->data_src = NULL;
254 mark_frame_as_depended_upon(packet_info *pinfo, guint32 frame_num)
256 /* Don't mark a frame as dependent on itself */
257 if (frame_num != PINFO_FD_NUM(pinfo)) {
258 pinfo->dependent_frames = g_slist_prepend(pinfo->dependent_frames, GUINT_TO_POINTER(frame_num));
262 /* Allow dissectors to register a "final_registration" routine
263 * that is run like the proto_register_XXX() routine, but at the
264 * end of the epan_init() function; that is, *after* all other
265 * subsystems, like dfilters, have finished initializing. This is
266 * useful for dissector registration routines which need to compile
267 * display filters. dfilters can't initialize itself until all protocols
268 * have registered themselves. */
269 static GSList *final_registration_routines;
272 register_final_registration_routine(void (*func)(void))
274 final_registration_routines = g_slist_append(final_registration_routines,
278 /* Call all the registered "final_registration" routines. */
280 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
282 void_func_t func = (void_func_t)routine;
288 final_registration_all_protocols(void)
290 g_slist_foreach(final_registration_routines,
291 &call_final_registration_routine, NULL);
295 /* Creates the top-most tvbuff and calls dissect_frame() */
297 dissect_packet(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
298 const guchar *pd, frame_data *fd, column_info *cinfo)
302 memset(&edt->pi, 0, sizeof(edt->pi));
303 edt->pi.current_proto = "<Missing Protocol Name>";
304 edt->pi.cinfo = cinfo;
307 edt->pi.pseudo_header = &phdr->pseudo_header;
308 edt->pi.dl_src.type = AT_NONE;
309 edt->pi.dl_dst.type = AT_NONE;
310 edt->pi.net_src.type = AT_NONE;
311 edt->pi.net_dst.type = AT_NONE;
312 edt->pi.src.type = AT_NONE;
313 edt->pi.dst.type = AT_NONE;
314 edt->pi.ctype = CT_NONE;
315 edt->pi.noreassembly_reason = "";
316 edt->pi.ptype = PT_NONE;
317 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
318 edt->pi.dcetransporttype = -1;
319 edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
320 edt->pi.dcerpc_procedure_name="";
321 edt->pi.link_dir = LINK_DIR_UNKNOWN;
324 /* to enable decode as for ethertype=0x0000 (fix for bug 4721) */
325 edt->pi.ethertype = G_MAXINT;
327 EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
331 * XXX - currently, the length arguments to
332 * tvb_new_real_data() are signed, but the captured
333 * and reported length values are unsigned; this means
334 * that length values > 2^31 - 1 will appear as
335 * negative lengths in tvb_new_real_data().
337 * Captured length values that large will already
338 * have been filtered out by the Wiretap modules
339 * (the file will be reported as corrupted), to
340 * avoid trying to allocate large chunks of data.
342 * Reported length values will not have been
343 * filtered out, and should not be filtered out,
344 * as those lengths are not necessarily invalid.
346 * For now, we clip the reported length at G_MAXINT,
347 * so that tvb_new_real_data() doesn't fail. It
348 * would throw an exception, which we'd catch, but
349 * that would mean we would have no tvbuffs
350 * associated with edt, which would upset much of
351 * the rest of the application.
353 edt->tvb = tvb_new_real_data(pd, fd->cap_len,
354 fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len);
355 /* Add this tvbuffer into the data_src list */
356 add_new_data_source(&edt->pi, edt->tvb, "Frame");
358 /* Even though dissect_frame() catches all the exceptions a
359 * sub-dissector can throw, dissect_frame() itself may throw
360 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
361 * in this function. */
362 if(frame_handle != NULL)
363 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
367 g_assert_not_reached();
369 CATCH(ReportedBoundsError) {
370 if(proto_malformed != -1){
371 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
372 "[Malformed Frame: Packet Length]" );
374 g_assert_not_reached();
377 CATCH(OutOfMemoryError) {
382 EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
384 fd->flags.visited = 1;
387 /*********************** code added for sub-dissector lookup *********************/
390 * A dissector handle.
392 struct dissector_handle {
393 const char *name; /* dissector name */
394 gboolean is_new; /* TRUE if new-style dissector */
399 protocol_t *protocol;
402 /* This function will return
403 * old style dissector :
404 * length of the payload or 1 of the payload is empty
406 * >0 this protocol was successfully dissected and this was this protocol.
407 * 0 this packet did not match this protocol.
409 * The only time this function will return 0 is if it is a new style dissector
410 * and if the dissector rejected the packet.
413 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
414 packet_info *pinfo, proto_tree *tree, void *data)
416 const char *saved_proto;
419 saved_proto = pinfo->current_proto;
421 if (handle->protocol != NULL) {
422 pinfo->current_proto =
423 proto_get_protocol_short_name(handle->protocol);
426 if (handle->is_new) {
427 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
428 ret = (*handle->dissector.new)(tvb, pinfo, tree, data);
429 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
431 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
432 (*handle->dissector.old)(tvb, pinfo, tree);
433 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
434 ret = tvb_length(tvb);
437 * XXX - a tvbuff can have 0 bytes of data in
438 * it, so we have to make sure we don't return
445 pinfo->current_proto = saved_proto;
451 * Call a dissector through a handle.
452 * If the protocol for that handle isn't enabled, return 0 without
453 * calling the dissector.
454 * Otherwise, if the handle refers to a new-style dissector, call the
455 * dissector and return its return value, otherwise call it and return
456 * the length of the tvbuff pointed to by the argument.
460 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
461 packet_info *pinfo_arg, proto_tree *tree, void *);
464 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb, packet_info *pinfo_arg,
465 proto_tree *tree, gboolean add_proto_name, void *data)
467 packet_info *pinfo = pinfo_arg;
468 const char *saved_proto;
469 guint16 saved_can_desegment;
471 gint saved_layer_names_len = 0;
473 if (handle->protocol != NULL &&
474 !proto_is_protocol_enabled(handle->protocol)) {
476 * The protocol isn't enabled.
481 saved_proto = pinfo->current_proto;
482 saved_can_desegment = pinfo->can_desegment;
484 if (pinfo->layer_names != NULL)
485 saved_layer_names_len = (gint) pinfo->layer_names->len;
488 * can_desegment is set to 2 by anyone which offers the
489 * desegmentation api/service.
490 * Then everytime a subdissector is called it is decremented
492 * Thus only the subdissector immediately on top of whoever
493 * offers this service can use it.
494 * We save the current value of "can_desegment" for the
495 * benefit of TCP proxying dissectors such as SOCKS, so they
496 * can restore it and allow the dissectors they call to use
497 * the desegmentation service.
499 pinfo->saved_can_desegment = saved_can_desegment;
500 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
501 if (handle->protocol != NULL) {
502 pinfo->current_proto =
503 proto_get_protocol_short_name(handle->protocol);
506 * Add the protocol name to the layers
507 * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
509 if ((pinfo->layer_names)&&(add_proto_name)) {
510 if (pinfo->layer_names->len > 0)
511 g_string_append(pinfo->layer_names, ":");
512 g_string_append(pinfo->layer_names,
513 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
517 if (pinfo->flags.in_error_pkt) {
518 ret = call_dissector_work_error(handle, tvb, pinfo, tree, data);
521 * Just call the subdissector.
523 ret = call_dissector_through_handle(handle, tvb, pinfo, tree, data);
527 * That dissector didn't accept the packet, so
528 * remove its protocol's name from the list
531 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
532 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
535 pinfo->current_proto = saved_proto;
536 pinfo->can_desegment = saved_can_desegment;
542 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
543 packet_info *pinfo_arg, proto_tree *tree, void *data)
545 packet_info *pinfo = pinfo_arg;
546 const char *saved_proto;
547 guint16 saved_can_desegment;
548 volatile int ret = 0;
549 gboolean save_writable;
552 address save_net_src;
553 address save_net_dst;
558 * This isn't a packet being transported inside
559 * the protocol whose dissector is calling us,
560 * it's a copy of a packet that caused an error
561 * in some protocol included in a packet that
562 * reports the error (e.g., an ICMP Unreachable
567 * Save the current state of the writability of
568 * the columns, and restore them after the
569 * dissector returns, so that the columns
570 * don't reflect the packet that got the error,
571 * they reflect the packet that reported the
574 saved_proto = pinfo->current_proto;
575 saved_can_desegment = pinfo->can_desegment;
577 save_writable = col_get_writable(pinfo->cinfo);
578 col_set_writable(pinfo->cinfo, FALSE);
579 save_dl_src = pinfo->dl_src;
580 save_dl_dst = pinfo->dl_dst;
581 save_net_src = pinfo->net_src;
582 save_net_dst = pinfo->net_dst;
583 save_src = pinfo->src;
584 save_dst = pinfo->dst;
586 /* Dissect the contained packet. */
588 ret = call_dissector_through_handle(handle, tvb,pinfo, tree, data);
592 * Restore the column writability and addresses.
594 col_set_writable(pinfo->cinfo, save_writable);
595 pinfo->dl_src = save_dl_src;
596 pinfo->dl_dst = save_dl_dst;
597 pinfo->net_src = save_net_src;
598 pinfo->net_dst = save_net_dst;
599 pinfo->src = save_src;
600 pinfo->dst = save_dst;
603 * Restore the current protocol, so any
604 * "Short Frame" indication reflects that
605 * protocol, not the protocol for the
606 * packet that got the error.
608 pinfo->current_proto = saved_proto;
611 * Restore the desegmentability state.
613 pinfo->can_desegment = saved_can_desegment;
616 * Rethrow the exception, so this will be
617 * reported as a short frame.
621 CATCH(ReportedBoundsError) {
623 * "ret" wasn't set because an exception was thrown
624 * before "call_dissector_through_handle()" returned.
625 * As it called something, at least one dissector
626 * accepted the packet, and, as an exception was
627 * thrown, not only was all the tvbuff dissected,
628 * a dissector tried dissecting past the end of
629 * the data in some tvbuff, so we'll assume that
630 * the entire tvbuff was dissected.
632 ret = tvb_length(tvb);
634 CATCH(OutOfMemoryError) {
639 col_set_writable(pinfo->cinfo, save_writable);
640 pinfo->dl_src = save_dl_src;
641 pinfo->dl_dst = save_dl_dst;
642 pinfo->net_src = save_net_src;
643 pinfo->net_dst = save_net_dst;
644 pinfo->src = save_src;
645 pinfo->dst = save_dst;
646 pinfo->want_pdu_tracking = 0;
651 * An entry in the hash table portion of a dissector table.
654 dissector_handle_t initial;
655 dissector_handle_t current;
661 * "hash_table" is a hash table, indexed by port number, supplying
662 * a "struct dtbl_entry"; it records what dissector is assigned to
663 * that uint or string value in that table.
665 * "dissector_handles" is a list of all dissectors that *could* be
666 * used in that table; not all of them are necessarily in the table,
667 * as they may be for protocols that don't have a fixed uint value,
668 * e.g. for TCP or UDP port number tables and protocols with no fixed
671 * "ui_name" is the name the dissector table has in the user interface.
673 * "type" is a field type giving the width of the uint value for that
674 * dissector table, if it's a uint dissector table.
676 * "base" is the base in which to display the uint value for that
677 * dissector table, if it's a uint dissector table.
679 struct dissector_table {
680 GHashTable *hash_table;
681 GSList *dissector_handles;
687 static GHashTable *dissector_tables = NULL;
689 /* Finds a dissector table by table name. */
691 find_dissector_table(const char *name)
693 g_assert(dissector_tables);
694 return g_hash_table_lookup( dissector_tables, name );
697 /* Find an entry in a uint dissector table. */
698 static dtbl_entry_t *
699 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
701 switch (sub_dissectors->type) {
708 * You can do a uint lookup in these tables.
714 * But you can't do a uint lookup in any other types
717 g_assert_not_reached();
723 return g_hash_table_lookup(sub_dissectors->hash_table,
724 GUINT_TO_POINTER(pattern));
729 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
731 dtbl_entry_t *dtbl_entry;
734 g_warning("%s: %s registering using a pattern of 0",
735 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
738 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
739 if (dtbl_entry != NULL) {
740 g_warning("%s: %s registering using pattern %d already registered by %s",
741 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
742 pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
747 /* Add an entry to a uint dissector table. */
749 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
751 dissector_table_t sub_dissectors;
752 dtbl_entry_t *dtbl_entry;
754 sub_dissectors = find_dissector_table(name);
757 * Make sure the dissector table exists.
759 if (sub_dissectors == NULL) {
760 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
762 fprintf(stderr, "Protocol being registered is \"%s\"\n",
763 proto_get_protocol_long_name(handle->protocol));
764 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
770 g_assert(handle!=NULL);
771 switch (sub_dissectors->type) {
778 * You can do a uint lookup in these tables.
784 * But you can't do a uint lookup in any other types
787 g_assert_not_reached();
791 dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
794 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
795 dtbl_entry->current = handle;
796 dtbl_entry->initial = dtbl_entry->current;
798 /* do the table insertion */
799 g_hash_table_insert( sub_dissectors->hash_table,
800 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
803 * Now add it to the list of handles that could be used with this
804 * table, because it *is* being used with this table.
806 dissector_add_handle(name, handle);
809 /* Delete the entry for a dissector in a uint dissector table
810 with a particular pattern. */
812 /* NOTE: this doesn't use the dissector call variable. It is included to */
813 /* be consistant with the dissector_add_uint and more importantly to be used */
814 /* if the technique of adding a temporary dissector is implemented. */
815 /* If temporary dissectors are deleted, then the original dissector must */
818 dissector_delete_uint(const char *name, const guint32 pattern,
819 dissector_handle_t handle _U_)
821 dissector_table_t sub_dissectors = find_dissector_table( name);
822 dtbl_entry_t *dtbl_entry;
825 g_assert( sub_dissectors);
830 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
832 if (dtbl_entry != NULL) {
836 g_hash_table_remove(sub_dissectors->hash_table,
837 GUINT_TO_POINTER(pattern));
841 /* Change the entry for a dissector in a uint dissector table
842 with a particular pattern to use a new dissector handle. */
844 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
846 dissector_table_t sub_dissectors = find_dissector_table( name);
847 dtbl_entry_t *dtbl_entry;
850 g_assert( sub_dissectors);
853 * See if the entry already exists. If so, reuse it.
855 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
856 if (dtbl_entry != NULL) {
857 dtbl_entry->current = handle;
862 * Don't create an entry if there is no dissector handle - I.E. the
863 * user said not to decode something that wasn't being decoded
864 * in the first place.
869 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
870 dtbl_entry->initial = NULL;
871 dtbl_entry->current = handle;
873 /* do the table insertion */
874 g_hash_table_insert( sub_dissectors->hash_table,
875 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
878 /* Reset an entry in a uint dissector table to its initial value. */
880 dissector_reset_uint(const char *name, const guint32 pattern)
882 dissector_table_t sub_dissectors = find_dissector_table( name);
883 dtbl_entry_t *dtbl_entry;
886 g_assert( sub_dissectors);
891 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
893 if (dtbl_entry == NULL)
897 * Found - is there an initial value?
899 if (dtbl_entry->initial != NULL) {
900 dtbl_entry->current = dtbl_entry->initial;
902 g_hash_table_remove(sub_dissectors->hash_table,
903 GUINT_TO_POINTER(pattern));
907 /* Look for a given value in a given uint dissector table and, if found,
908 call the dissector with the arguments supplied, and return TRUE,
909 otherwise return FALSE. */
912 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
913 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
914 const gboolean add_proto_name, void *data)
916 dtbl_entry_t *dtbl_entry;
917 struct dissector_handle *handle;
918 guint32 saved_match_uint;
921 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
922 if (dtbl_entry != NULL) {
924 * Is there currently a dissector handle for this entry?
926 handle = dtbl_entry->current;
927 if (handle == NULL) {
929 * No - pretend this dissector didn't exist,
930 * so that other dissectors might have a chance
931 * to dissect this packet.
937 * Save the current value of "pinfo->match_uint",
938 * set it to the uint_val that matched, call the
939 * dissector, and restore "pinfo->match_uint".
941 saved_match_uint = pinfo->match_uint;
942 pinfo->match_uint = uint_val;
943 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, data);
944 pinfo->match_uint = saved_match_uint;
947 * If a new-style dissector returned 0, it means that
948 * it didn't think this tvbuff represented a packet for
949 * its protocol, and didn't dissect anything.
951 * Old-style dissectors can't reject the packet.
953 * 0 is also returned if the protocol wasn't enabled.
955 * If the packet was rejected, we return FALSE, so that
956 * other dissectors might have a chance to dissect this
957 * packet, otherwise we return TRUE.
965 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
966 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
969 return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE, NULL);
972 /* Look for a given value in a given uint dissector table and, if found,
973 return the dissector handle for that value. */
975 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
977 dtbl_entry_t *dtbl_entry;
979 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
980 if (dtbl_entry != NULL)
981 return dtbl_entry->current;
986 /* Find an entry in a string dissector table. */
987 static dtbl_entry_t *
988 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
990 switch (sub_dissectors->type) {
995 * You can do a string lookup in these tables.
1001 * But you can't do a string lookup in any other types
1004 g_assert_not_reached();
1010 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
1013 /* Add an entry to a string dissector table. */
1015 dissector_add_string(const char *name, const gchar *pattern,
1016 dissector_handle_t handle)
1018 dissector_table_t sub_dissectors = find_dissector_table( name);
1019 dtbl_entry_t *dtbl_entry;
1022 * Make sure the dissector table exists.
1024 if (sub_dissectors == NULL) {
1025 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1027 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1028 proto_get_protocol_long_name(handle->protocol));
1029 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1035 g_assert(handle!=NULL);
1036 switch (sub_dissectors->type) {
1041 * You can do a string lookup in these tables.
1047 * But you can't do a string lookup in any other types
1050 g_assert_not_reached();
1053 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1054 dtbl_entry->current = handle;
1055 dtbl_entry->initial = dtbl_entry->current;
1057 /* do the table insertion */
1058 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1059 (gpointer)dtbl_entry);
1062 * Now add it to the list of handles that could be used with this
1063 * table, because it *is* being used with this table.
1065 dissector_add_handle(name, handle);
1068 /* Delete the entry for a dissector in a string dissector table
1069 with a particular pattern. */
1071 /* NOTE: this doesn't use the dissector call variable. It is included to */
1072 /* be consistant with the dissector_add_string and more importantly to */
1073 /* be used if the technique of adding a temporary dissector is */
1075 /* If temporary dissectors are deleted, then the original dissector must */
1078 dissector_delete_string(const char *name, const gchar *pattern,
1079 dissector_handle_t handle _U_)
1081 dissector_table_t sub_dissectors = find_dissector_table( name);
1082 dtbl_entry_t *dtbl_entry;
1085 g_assert( sub_dissectors);
1090 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1092 if (dtbl_entry != NULL) {
1094 * Found - remove it.
1096 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1100 /* Change the entry for a dissector in a string dissector table
1101 with a particular pattern to use a new dissector handle. */
1103 dissector_change_string(const char *name, const gchar *pattern,
1104 dissector_handle_t handle)
1106 dissector_table_t sub_dissectors = find_dissector_table( name);
1107 dtbl_entry_t *dtbl_entry;
1110 g_assert( sub_dissectors);
1113 * See if the entry already exists. If so, reuse it.
1115 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1116 if (dtbl_entry != NULL) {
1117 dtbl_entry->current = handle;
1122 * Don't create an entry if there is no dissector handle - I.E. the
1123 * user said not to decode something that wasn't being decoded
1124 * in the first place.
1129 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1130 dtbl_entry->initial = NULL;
1131 dtbl_entry->current = handle;
1133 /* do the table insertion */
1134 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1135 (gpointer)dtbl_entry);
1138 /* Reset an entry in a string sub-dissector table to its initial value. */
1140 dissector_reset_string(const char *name, const gchar *pattern)
1142 dissector_table_t sub_dissectors = find_dissector_table( name);
1143 dtbl_entry_t *dtbl_entry;
1146 g_assert( sub_dissectors);
1151 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1153 if (dtbl_entry == NULL)
1157 * Found - is there an initial value?
1159 if (dtbl_entry->initial != NULL) {
1160 dtbl_entry->current = dtbl_entry->initial;
1162 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1166 /* Look for a given string in a given dissector table and, if found, call
1167 the dissector with the arguments supplied, and return TRUE, otherwise
1170 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1171 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1173 dtbl_entry_t *dtbl_entry;
1174 struct dissector_handle *handle;
1176 const gchar *saved_match_string;
1178 /* XXX ASSERT instead ? */
1179 if (!string) return FALSE;
1180 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1181 if (dtbl_entry != NULL) {
1183 * Is there currently a dissector handle for this entry?
1185 handle = dtbl_entry->current;
1186 if (handle == NULL) {
1188 * No - pretend this dissector didn't exist,
1189 * so that other dissectors might have a chance
1190 * to dissect this packet.
1196 * Save the current value of "pinfo->match_string",
1197 * set it to the string that matched, call the
1198 * dissector, and restore "pinfo->match_string".
1200 saved_match_string = pinfo->match_string;
1201 pinfo->match_string = string;
1202 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, NULL);
1203 pinfo->match_string = saved_match_string;
1206 * If a new-style dissector returned 0, it means that
1207 * it didn't think this tvbuff represented a packet for
1208 * its protocol, and didn't dissect anything.
1210 * Old-style dissectors can't reject the packet.
1212 * 0 is also returned if the protocol wasn't enabled.
1214 * If the packet was rejected, we return FALSE, so that
1215 * other dissectors might have a chance to dissect this
1216 * packet, otherwise we return TRUE.
1223 /* Look for a given value in a given string dissector table and, if found,
1224 return the dissector handle for that value. */
1226 dissector_get_string_handle(dissector_table_t sub_dissectors,
1227 const gchar *string)
1229 dtbl_entry_t *dtbl_entry;
1231 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1232 if (dtbl_entry != NULL)
1233 return dtbl_entry->current;
1239 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1241 return dtbl_entry->current;
1245 dissector_compare_filter_name(gconstpointer dissector_a, gconstpointer dissector_b)
1247 dissector_handle_t a = (dissector_handle_t)dissector_a;
1248 dissector_handle_t b = (dissector_handle_t)dissector_b;
1249 const char *a_name, *b_name;
1252 if (a->protocol == NULL)
1255 a_name = proto_get_protocol_filter_name(proto_get_id(a->protocol));
1257 if (b->protocol == NULL)
1260 b_name = proto_get_protocol_filter_name(proto_get_id(b->protocol));
1262 ret = strcmp(a_name, b_name);
1266 /* Add a handle to the list of handles that *could* be used with this
1267 table. That list is used by code in the UI. */
1269 dissector_add_handle(const char *name, dissector_handle_t handle)
1271 dissector_table_t sub_dissectors = find_dissector_table( name);
1275 * Make sure the dissector table exists.
1277 if (sub_dissectors == NULL) {
1278 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1280 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1281 proto_get_protocol_long_name(handle->protocol));
1282 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1287 /* Is it already in this list? */
1288 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1289 if (entry != NULL) {
1291 * Yes - don't insert it again.
1296 /* Add it to the list. */
1297 sub_dissectors->dissector_handles =
1298 g_slist_insert_sorted(sub_dissectors->dissector_handles, (gpointer)handle, (GCompareFunc)dissector_compare_filter_name);
1302 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1304 return dtbl_entry->initial;
1307 /**************************************************/
1309 /* Routines to walk dissector tables */
1311 /**************************************************/
1313 typedef struct dissector_foreach_info {
1314 gpointer caller_data;
1315 DATFunc caller_func;
1317 const gchar *table_name;
1318 ftenum_t selector_type;
1319 } dissector_foreach_info_t;
1322 * Called for each entry in a dissector table.
1325 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1327 dissector_foreach_info_t *info;
1328 dtbl_entry_t *dtbl_entry;
1331 g_assert(user_data);
1334 if (dtbl_entry->current == NULL ||
1335 dtbl_entry->current->protocol == NULL) {
1337 * Either there is no dissector for this entry, or
1338 * the dissector doesn't have a protocol associated
1341 * XXX - should the latter check be done?
1347 info->caller_func(info->table_name, info->selector_type, key, value,
1352 * Called for each entry in the table of all dissector tables.
1355 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1357 dissector_table_t sub_dissectors;
1358 dissector_foreach_info_t *info;
1361 g_assert(user_data);
1363 sub_dissectors = value;
1365 info->table_name = (gchar*) key;
1366 info->selector_type = get_dissector_table_selector_type(info->table_name);
1367 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1371 * Walk all dissector tables calling a user supplied function on each
1375 dissector_all_tables_foreach (DATFunc func,
1378 dissector_foreach_info_t info;
1380 info.caller_data = user_data;
1381 info.caller_func = func;
1382 info.next_func = dissector_table_foreach_func;
1383 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1387 * Walk one dissector table's hash table calling a user supplied function
1391 dissector_table_foreach (const char *name,
1395 dissector_foreach_info_t info;
1396 dissector_table_t sub_dissectors = find_dissector_table( name);
1398 info.table_name = name;
1399 info.selector_type = sub_dissectors->type;
1400 info.caller_func = func;
1401 info.caller_data = user_data;
1402 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1406 * Walk one dissector table's list of handles calling a user supplied
1407 * function on each entry.
1410 dissector_table_foreach_handle(const char *name,
1411 DATFunc_handle func,
1414 dissector_table_t sub_dissectors = find_dissector_table( name);
1417 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1418 tmp = g_slist_next(tmp))
1419 func(name, tmp->data, user_data);
1423 * Called for each entry in a dissector table.
1426 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1428 dtbl_entry_t *dtbl_entry;
1429 dissector_foreach_info_t *info;
1432 g_assert(user_data);
1435 if (dtbl_entry->initial == dtbl_entry->current) {
1437 * Entry hasn't changed - don't call the function.
1443 info->caller_func(info->table_name, info->selector_type, key, value,
1448 * Walk all dissector tables calling a user supplied function only on
1449 * any entry that has been changed from its original state.
1452 dissector_all_tables_foreach_changed (DATFunc func,
1455 dissector_foreach_info_t info;
1457 info.caller_data = user_data;
1458 info.caller_func = func;
1459 info.next_func = dissector_table_foreach_changed_func;
1460 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1464 * Walk one dissector table calling a user supplied function only on
1465 * any entry that has been changed from its original state.
1468 dissector_table_foreach_changed (const char *name,
1472 dissector_foreach_info_t info;
1473 dissector_table_t sub_dissectors = find_dissector_table( name);
1475 info.table_name = name;
1476 info.selector_type = sub_dissectors->type;
1477 info.caller_func = func;
1478 info.caller_data = user_data;
1479 g_hash_table_foreach(sub_dissectors->hash_table,
1480 dissector_table_foreach_changed_func, &info);
1483 typedef struct dissector_foreach_table_info {
1484 gpointer caller_data;
1485 DATFunc_table caller_func;
1486 } dissector_foreach_table_info_t;
1489 * Called for each entry in the table of all dissector tables.
1492 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1494 dissector_table_t table;
1495 dissector_foreach_table_info_t *info;
1499 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1503 * Called for each key in the table of all dissector tables.
1506 dissector_all_tables_foreach_list_func (gpointer key, gpointer user_data)
1508 dissector_table_t table;
1509 dissector_foreach_table_info_t *info;
1511 table = g_hash_table_lookup( dissector_tables, key );
1513 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1517 * Walk all dissector tables calling a user supplied function on each
1521 dissector_all_tables_foreach_table (DATFunc_table func,
1523 GCompareFunc compare_key_func)
1525 dissector_foreach_table_info_t info;
1528 info.caller_data = user_data;
1529 info.caller_func = func;
1530 if (compare_key_func != NULL)
1532 list = g_hash_table_get_keys(dissector_tables);
1533 list = g_list_sort(list, compare_key_func);
1534 g_list_foreach(list, dissector_all_tables_foreach_list_func, &info);
1539 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1544 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1547 dissector_table_t sub_dissectors;
1549 /* Create our hash-of-hashes if it doesn't already exist */
1550 if (!dissector_tables) {
1551 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1552 g_assert(dissector_tables);
1555 /* Make sure the registration is unique */
1556 if(g_hash_table_lookup( dissector_tables, name )) {
1557 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1560 /* Create and register the dissector table for this name; returns */
1561 /* a pointer to the dissector table. */
1562 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1570 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1571 * so we use "g_direct_hash()" and "g_direct_equal()".
1573 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash,
1581 sub_dissectors->hash_table = g_hash_table_new_full( g_str_hash,
1588 g_assert_not_reached();
1590 sub_dissectors->dissector_handles = NULL;
1591 sub_dissectors->ui_name = ui_name;
1592 sub_dissectors->type = type;
1593 sub_dissectors->base = base;
1594 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1595 return sub_dissectors;
1599 get_dissector_table_ui_name(const char *name)
1601 dissector_table_t sub_dissectors = find_dissector_table( name);
1603 return sub_dissectors->ui_name;
1607 get_dissector_table_selector_type(const char *name)
1609 dissector_table_t sub_dissectors = find_dissector_table( name);
1611 return sub_dissectors->type;
1615 get_dissector_table_base(const char *name)
1617 dissector_table_t sub_dissectors = find_dissector_table( name);
1619 return sub_dissectors->base;
1622 static GHashTable *heur_dissector_lists = NULL;
1625 /* Finds a heuristic dissector table by table name. */
1626 static heur_dissector_list_t *
1627 find_heur_dissector_list(const char *name)
1629 g_assert(heur_dissector_lists != NULL);
1630 return g_hash_table_lookup(heur_dissector_lists, name);
1634 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1636 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1637 const char *proto_name;
1638 heur_dtbl_entry_t *hdtbl_entry;
1641 * Make sure the dissector table exists.
1643 if (sub_dissectors == NULL) {
1644 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1646 proto_name = proto_get_protocol_name(proto);
1647 if (proto_name != NULL) {
1648 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1651 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1656 /* XXX: Should verify that sub-dissector is not already in the list ? */
1658 hdtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1659 hdtbl_entry->dissector = dissector;
1660 hdtbl_entry->protocol = find_protocol_by_id(proto);
1661 hdtbl_entry->enabled = TRUE;
1663 /* do the table insertion */
1664 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)hdtbl_entry);
1670 find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1671 const heur_dtbl_entry_t *hdtbl_entry_a = (const heur_dtbl_entry_t *) a;
1672 const heur_dtbl_entry_t *hdtbl_entry_b = (const heur_dtbl_entry_t *) b;
1673 return (hdtbl_entry_a->dissector == hdtbl_entry_b->dissector) &&
1674 (hdtbl_entry_a->protocol == hdtbl_entry_b->protocol) ? 0 : 1;
1678 heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1679 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1680 heur_dtbl_entry_t hdtbl_entry;
1681 GSList *found_entry;
1684 g_assert(sub_dissectors != NULL);
1686 hdtbl_entry.dissector = dissector;
1688 hdtbl_entry.protocol = find_protocol_by_id(proto);
1690 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1693 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1694 g_free(g_slist_nth_data(found_entry, 1));
1695 g_slist_free_1(found_entry);
1700 heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled) {
1701 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1702 GSList *found_entry;
1703 heur_dtbl_entry_t hdtbl_entry;
1706 g_assert(sub_dissectors != NULL);
1708 hdtbl_entry.dissector = dissector;
1710 hdtbl_entry.protocol = find_protocol_by_id(proto);
1712 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1715 heur_dtbl_entry_t *hdtbl_entry_p;
1716 hdtbl_entry_p = (heur_dtbl_entry_t *)found_entry->data;
1717 hdtbl_entry_p->enabled = enabled;
1722 dissector_try_heuristic(heur_dissector_list_t sub_dissectors, tvbuff_t *tvb,
1723 packet_info *pinfo, proto_tree *tree, void *data)
1726 const char *saved_proto;
1728 heur_dtbl_entry_t *hdtbl_entry;
1729 guint16 saved_can_desegment;
1730 gint saved_layer_names_len = 0;
1732 /* can_desegment is set to 2 by anyone which offers this api/service.
1733 then everytime a subdissector is called it is decremented by one.
1734 thus only the subdissector immediately ontop of whoever offers this
1736 We save the current value of "can_desegment" for the
1737 benefit of TCP proxying dissectors such as SOCKS, so they
1738 can restore it and allow the dissectors they call to use
1739 the desegmentation service.
1741 saved_can_desegment = pinfo->can_desegment;
1742 pinfo->saved_can_desegment = saved_can_desegment;
1743 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1746 saved_proto = pinfo->current_proto;
1748 if (pinfo->layer_names != NULL)
1749 saved_layer_names_len = (gint) pinfo->layer_names->len;
1751 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1752 /* XXX - why set this now and above? */
1753 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1754 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1756 if (hdtbl_entry->protocol != NULL &&
1757 (!proto_is_protocol_enabled(hdtbl_entry->protocol)||(hdtbl_entry->enabled==FALSE))) {
1759 * No - don't try this dissector.
1764 if (hdtbl_entry->protocol != NULL) {
1765 pinfo->current_proto =
1766 proto_get_protocol_short_name(hdtbl_entry->protocol);
1769 * Add the protocol name to the layers; we'll remove it
1770 * if the dissector fails.
1772 if (pinfo->layer_names) {
1773 if (pinfo->layer_names->len > 0)
1774 g_string_append(pinfo->layer_names, ":");
1775 g_string_append(pinfo->layer_names,
1776 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)));
1779 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1780 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1781 if ((*hdtbl_entry->dissector)(tvb, pinfo, tree, data)) {
1782 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1783 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1787 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1788 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1791 * That dissector didn't accept the packet, so
1792 * remove its protocol's name from the list
1795 if (pinfo->layer_names != NULL) {
1796 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1800 pinfo->current_proto = saved_proto;
1801 pinfo->can_desegment=saved_can_desegment;
1806 * Called for each entry in the table of all heuristic dissector tables.
1808 typedef struct heur_dissector_foreach_table_info {
1809 gpointer caller_data;
1810 DATFunc_heur_table caller_func;
1811 } heur_dissector_foreach_table_info_t;
1815 dissector_dump_heur_decodes_display(const gchar *table_name, const gpointer value, const gpointer user_data _U_)
1817 heur_dissector_list_t sub_dissectors = *(heur_dissector_list_t *)value;
1819 heur_dtbl_entry_t *hdtbl_entry;
1821 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1822 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1823 if (hdtbl_entry->protocol != NULL) {
1824 printf("%s\t%s\t%c\n",
1826 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)),
1827 (proto_is_protocol_enabled(hdtbl_entry->protocol) && hdtbl_entry->enabled) ? 'T' : 'F');
1834 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1836 heur_dissector_foreach_table_info_t *info;
1839 (*info->caller_func)((gchar*)key, value, info->caller_data);
1843 * Walk all heuristic dissector tables calling a user supplied function on each
1847 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1850 heur_dissector_foreach_table_info_t info;
1852 info.caller_data = user_data;
1853 info.caller_func = func;
1854 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1858 * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
1861 dissector_dump_heur_decodes(void)
1863 dissector_all_heur_tables_foreach_table(dissector_dump_heur_decodes_display, NULL);
1868 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1870 /* Create our hash-of-lists if it doesn't already exist */
1871 if (heur_dissector_lists == NULL) {
1872 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1873 g_assert(heur_dissector_lists != NULL);
1876 /* Make sure the registration is unique */
1877 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1879 *sub_dissectors = NULL; /* initially empty */
1880 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1881 (gpointer) sub_dissectors);
1885 * Register dissectors by name; used if one dissector always calls a
1886 * particular dissector, or if it bases the decision of which dissector
1887 * to call on something other than a numerical value or on "try a bunch
1888 * of dissectors until one likes the packet".
1892 * List of registered dissectors.
1894 static GHashTable *registered_dissectors = NULL;
1896 /* Get the long name of the protocol for a dissector handle, if it has
1899 dissector_handle_get_long_name(const dissector_handle_t handle)
1901 if (handle == NULL || handle->protocol == NULL) {
1904 return proto_get_protocol_long_name(handle->protocol);
1907 /* Get the short name of the protocol for a dissector handle, if it has
1910 dissector_handle_get_short_name(const dissector_handle_t handle)
1912 if (handle->protocol == NULL) {
1914 * No protocol (see, for example, the handle for
1915 * dissecting the set of protocols where the first
1916 * octet of the payload is an OSI network layer protocol
1921 return proto_get_protocol_short_name(handle->protocol);
1924 /* Get the index of the protocol for a dissector handle, if it has
1927 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1929 if (handle->protocol == NULL) {
1931 * No protocol (see, for example, the handle for
1932 * dissecting the set of protocols where the first
1933 * octet of the payload is an OSI network layer protocol
1938 return proto_get_id(handle->protocol);
1941 /* Find a registered dissector by name. */
1943 find_dissector(const char *name)
1945 g_assert(registered_dissectors != NULL);
1946 return g_hash_table_lookup(registered_dissectors, name);
1949 /* Create an anonymous handle for a dissector. */
1951 create_dissector_handle(dissector_t dissector, const int proto)
1953 struct dissector_handle *handle;
1955 handle = g_malloc(sizeof (struct dissector_handle));
1956 handle->name = NULL;
1957 handle->is_new = FALSE;
1958 handle->dissector.old = dissector;
1959 handle->protocol = find_protocol_by_id(proto);
1965 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1967 struct dissector_handle *handle;
1969 handle = g_malloc(sizeof (struct dissector_handle));
1970 handle->name = NULL;
1971 handle->is_new = TRUE;
1972 handle->dissector.new = dissector;
1973 handle->protocol = find_protocol_by_id(proto);
1978 /* Register a dissector by name. */
1980 register_dissector(const char *name, dissector_t dissector, const int proto)
1982 struct dissector_handle *handle;
1984 /* Create our hash table if it doesn't already exist */
1985 if (registered_dissectors == NULL) {
1986 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1987 g_assert(registered_dissectors != NULL);
1990 /* Make sure the registration is unique */
1991 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1993 handle = g_malloc(sizeof (struct dissector_handle));
1994 handle->name = name;
1995 handle->is_new = FALSE;
1996 handle->dissector.old = dissector;
1997 handle->protocol = find_protocol_by_id(proto);
1999 g_hash_table_insert(registered_dissectors, (gpointer)name,
2004 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
2006 struct dissector_handle *handle;
2008 /* Create our hash table if it doesn't already exist */
2009 if (registered_dissectors == NULL) {
2010 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
2011 g_assert(registered_dissectors != NULL);
2014 /* Make sure the registration is unique */
2015 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
2017 handle = g_malloc(sizeof (struct dissector_handle));
2018 handle->name = name;
2019 handle->is_new = TRUE;
2020 handle->dissector.new = dissector;
2021 handle->protocol = find_protocol_by_id(proto);
2023 g_hash_table_insert(registered_dissectors, (gpointer)name,
2027 /* Call a dissector through a handle but if the dissector rejected it
2031 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
2032 packet_info *pinfo, proto_tree *tree, void *data)
2036 g_assert(handle != NULL);
2037 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
2041 /* Call a dissector through a handle and if this fails call the "data"
2045 call_dissector_with_data(dissector_handle_t handle, tvbuff_t *tvb,
2046 packet_info *pinfo, proto_tree *tree, void *data)
2050 ret = call_dissector_only(handle, tvb, pinfo, tree, data);
2053 * The protocol was disabled, or the dissector rejected
2054 * it. Just dissect this packet as data.
2056 g_assert(data_handle != NULL);
2057 g_assert(data_handle->protocol != NULL);
2058 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2059 return tvb_length(tvb);
2065 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
2066 packet_info *pinfo, proto_tree *tree)
2068 return call_dissector_with_data(handle, tvb, pinfo, tree, NULL);
2072 * Dumps the "layer type"/"decode as" associations to stdout, similar
2073 * to the proto_registrar_dump_*() routines.
2075 * There is one record per line. The fields are tab-delimited.
2077 * Field 1 = layer type, e.g. "tcp.port"
2078 * Field 2 = selector in decimal
2079 * Field 3 = "decode as" name, e.g. "http"
2084 dissector_dump_decodes_display(const gchar *table_name,
2085 ftenum_t selector_type _U_, const gpointer key, const gpointer value,
2086 gpointer user_data _U_)
2088 guint32 selector = (guint32)(unsigned long) key;
2089 dissector_table_t sub_dissectors = find_dissector_table(table_name);
2090 dtbl_entry_t *dtbl_entry;
2091 dissector_handle_t handle;
2093 const gchar *decode_as;
2095 g_assert(sub_dissectors);
2096 switch (sub_dissectors->type) {
2103 g_assert(dtbl_entry);
2105 handle = dtbl_entry->current;
2108 proto_id = dissector_handle_get_protocol_index(handle);
2110 if (proto_id != -1) {
2111 decode_as = proto_get_protocol_filter_name(proto_id);
2112 g_assert(decode_as != NULL);
2113 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
2123 dissector_dump_decodes(void)
2125 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2128 static GPtrArray* post_dissectors = NULL;
2129 static guint num_of_postdissectors = 0;
2132 register_postdissector(dissector_handle_t handle)
2134 if (!post_dissectors)
2135 post_dissectors = g_ptr_array_new();
2137 g_ptr_array_add(post_dissectors, handle);
2138 num_of_postdissectors++;
2142 have_postdissector(void)
2145 dissector_handle_t handle;
2147 for(i = 0; i < num_of_postdissectors; i++) {
2148 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
2150 if (handle->protocol != NULL
2151 && proto_is_protocol_enabled(handle->protocol)) {
2152 /* We have at least one enabled postdissector */
2160 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2164 for(i = 0; i < num_of_postdissectors; i++) {
2165 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
2166 tvb,pinfo,tree, NULL);
2171 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2176 * indent-tabs-mode: t
2179 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2180 * :indentSize=8:tabSize=8:noTabs=false: