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.
43 #include "timestamp.h"
45 #include "atalk-utils.h"
46 #include "sna-utils.h"
47 #include "osi-utils.h"
50 #include "addr_resolv.h"
53 #include "epan_dissect.h"
57 #include <epan/reassemble.h>
58 #include <epan/stream.h>
59 #include <epan/expert.h>
61 static gint proto_malformed = -1;
62 static dissector_handle_t frame_handle = NULL;
63 static dissector_handle_t data_handle = NULL;
68 frame_handle = find_dissector("frame");
69 data_handle = find_dissector("data");
70 proto_malformed = proto_get_id_by_filter_name("malformed");
80 * Given a tvbuff, and a length from a packet header, adjust the length
81 * of the tvbuff to reflect the specified length.
84 set_actual_length(tvbuff_t *tvb, const guint specified_len)
86 if (specified_len < tvb_reported_length(tvb)) {
87 /* Adjust the length of this tvbuff to include only the specified
90 The dissector above the one calling us (the dissector above is
91 probably us) may use that to determine how much of its packet
93 tvb_set_reported_length(tvb, specified_len);
97 /* Allow protocols to register "init" routines, which are called before
98 we make a pass through a capture file and dissect all its packets
99 (e.g., when we read in a new capture file, or run a "filter packets"
100 or "colorize packets" pass over the current capture file). */
101 static GSList *init_routines;
104 register_init_routine(void (*func)(void))
106 init_routines = g_slist_append(init_routines, (gpointer)func);
109 typedef void (*void_func_t)(void);
111 /* Initialize all data structures used for dissection. */
113 call_init_routine(gpointer routine, gpointer dummy _U_)
115 void_func_t func = (void_func_t)routine;
120 * XXX - for now, these are the same; the "init" routines free whatever
121 * stuff is left over from any previous dissection, and then initialize
124 * We should probably split that into "init" and "cleanup" routines, for
128 init_dissection(void)
130 /* Reclaim and reinitialize all memory of seasonal scope */
133 /* Initialize the table of conversations. */
134 epan_conversation_init();
136 /* Initialize the table of circuits. */
139 /* Initialize protocol-specific variables. */
140 g_slist_foreach(init_routines, &call_init_routine, NULL);
142 /* Initialize the stream-handling tables */
145 /* Initialize the expert infos */
150 cleanup_dissection(void)
152 /* Reclaim all memory of seasonal scope */
155 /* Cleanup the table of conversations. */
156 epan_conversation_cleanup();
158 /* Cleanup the table of circuits. */
159 epan_circuit_cleanup();
161 /* TODO: Introduce cleanup_routines */
162 /* Cleanup protocol-specific variables. */
163 g_slist_foreach(init_routines, &call_init_routine, NULL);
165 /* Cleanup the stream-handling tables */
168 /* Initialize the expert infos */
172 /* Allow protocols to register a "cleanup" routine to be
173 * run after the initial sequential run through the packets.
174 * Note that the file can still be open after this; this is not
175 * the final cleanup. */
176 static GSList *postseq_cleanup_routines;
179 register_postseq_cleanup_routine(void_func_t func)
181 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
185 /* Call all the registered "postseq_cleanup" routines. */
187 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
189 void_func_t func = (void_func_t)routine;
194 postseq_cleanup_all_protocols(void)
196 g_slist_foreach(postseq_cleanup_routines,
197 &call_postseq_cleanup_routine, NULL);
201 * Add a new data source to the list of data sources for a frame, given
202 * the tvbuff for the data source and its name.
205 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
209 src = ep_alloc(sizeof (data_source));
211 src->name_initialized = FALSE;
213 pinfo->data_src = g_slist_append(pinfo->data_src, src);
217 get_data_source_name(data_source *src)
219 if (!src->name_initialized) {
220 src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
221 src->name_initialized = TRUE;
228 * Free up a frame's list of data sources.
231 free_data_sources(packet_info *pinfo)
233 if (pinfo->data_src) {
234 g_slist_free(pinfo->data_src);
235 pinfo->data_src = NULL;
239 /* Allow dissectors to register a "final_registration" routine
240 * that is run like the proto_register_XXX() routine, but at the
241 * end of the epan_init() function; that is, *after* all other
242 * subsystems, like dfilters, have finished initializing. This is
243 * useful for dissector registration routines which need to compile
244 * display filters. dfilters can't initialize itself until all protocols
245 * have registered themselves. */
246 static GSList *final_registration_routines;
249 register_final_registration_routine(void (*func)(void))
251 final_registration_routines = g_slist_append(final_registration_routines,
255 /* Call all the registered "final_registration" routines. */
257 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
259 void_func_t func = (void_func_t)routine;
265 final_registration_all_protocols(void)
267 g_slist_foreach(final_registration_routines,
268 &call_final_registration_routine, NULL);
272 /* Creates the top-most tvbuff and calls dissect_frame() */
274 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
275 const guchar *pd, frame_data *fd, column_info *cinfo)
279 memset(&edt->pi, 0, sizeof(edt->pi));
280 edt->pi.current_proto = "<Missing Protocol Name>";
281 edt->pi.cinfo = cinfo;
283 edt->pi.pseudo_header = pseudo_header;
284 edt->pi.dl_src.type = AT_NONE;
285 edt->pi.dl_dst.type = AT_NONE;
286 edt->pi.net_src.type = AT_NONE;
287 edt->pi.net_dst.type = AT_NONE;
288 edt->pi.src.type = AT_NONE;
289 edt->pi.dst.type = AT_NONE;
290 edt->pi.ctype = CT_NONE;
291 edt->pi.noreassembly_reason = "";
292 edt->pi.ptype = PT_NONE;
293 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
294 edt->pi.dcetransporttype = -1;
295 edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
296 edt->pi.dcerpc_procedure_name="";
297 edt->pi.link_dir = LINK_DIR_UNKNOWN;
300 /* to enable decode as for ethertype=0x0000 (fix for bug 4721) */
301 edt->pi.ethertype = G_MAXINT;
303 EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
306 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
307 /* Add this tvbuffer into the data_src list */
308 add_new_data_source(&edt->pi, edt->tvb, "Frame");
310 /* Even though dissect_frame() catches all the exceptions a
311 * sub-dissector can throw, dissect_frame() itself may throw
312 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
313 * in this function. */
314 if(frame_handle != NULL)
315 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
319 g_assert_not_reached();
321 CATCH(ReportedBoundsError) {
322 if(proto_malformed != -1){
323 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
324 "[Malformed Frame: Packet Length]" );
326 g_assert_not_reached();
329 CATCH(OutOfMemoryError) {
334 EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
336 fd->flags.visited = 1;
339 /*********************** code added for sub-dissector lookup *********************/
342 * A dissector handle.
344 struct dissector_handle {
345 const char *name; /* dissector name */
346 gboolean is_new; /* TRUE if new-style dissector */
351 protocol_t *protocol;
354 /* This function will return
355 * old style dissector :
356 * length of the payload or 1 of the payload is empty
358 * >0 this protocol was successfully dissected and this was this protocol.
359 * 0 this packet did not match this protocol.
361 * The only time this function will return 0 is if it is a new style dissector
362 * and if the dissector rejected the packet.
365 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
366 packet_info *pinfo, proto_tree *tree)
368 const char *saved_proto;
371 saved_proto = pinfo->current_proto;
373 if (handle->protocol != NULL) {
374 pinfo->current_proto =
375 proto_get_protocol_short_name(handle->protocol);
378 if (handle->is_new) {
379 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
380 ret = (*handle->dissector.new)(tvb, pinfo, tree);
381 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
383 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
384 (*handle->dissector.old)(tvb, pinfo, tree);
385 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
386 ret = tvb_length(tvb);
389 * XXX - a tvbuff can have 0 bytes of data in
390 * it, so we have to make sure we don't return
397 pinfo->current_proto = saved_proto;
403 * Call a dissector through a handle.
404 * If the protocol for that handle isn't enabled, return 0 without
405 * calling the dissector.
406 * Otherwise, if the handle refers to a new-style dissector, call the
407 * dissector and return its return value, otherwise call it and return
408 * the length of the tvbuff pointed to by the argument.
412 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
413 packet_info *pinfo_arg, proto_tree *tree);
416 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
417 packet_info *pinfo_arg, proto_tree *tree, gboolean add_proto_name)
419 packet_info *pinfo = pinfo_arg;
420 const char *saved_proto;
421 guint16 saved_can_desegment;
423 gint saved_layer_names_len = 0;
425 if (handle->protocol != NULL &&
426 !proto_is_protocol_enabled(handle->protocol)) {
428 * The protocol isn't enabled.
433 saved_proto = pinfo->current_proto;
434 saved_can_desegment = pinfo->can_desegment;
436 if (pinfo->layer_names != NULL)
437 saved_layer_names_len = (gint) pinfo->layer_names->len;
440 * can_desegment is set to 2 by anyone which offers the
441 * desegmentation api/service.
442 * Then everytime a subdissector is called it is decremented
444 * Thus only the subdissector immediately on top of whoever
445 * offers this service can use it.
446 * We save the current value of "can_desegment" for the
447 * benefit of TCP proxying dissectors such as SOCKS, so they
448 * can restore it and allow the dissectors they call to use
449 * the desegmentation service.
451 pinfo->saved_can_desegment = saved_can_desegment;
452 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
453 if (handle->protocol != NULL) {
454 pinfo->current_proto =
455 proto_get_protocol_short_name(handle->protocol);
458 * Add the protocol name to the layers
459 * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
461 if ((pinfo->layer_names)&&(add_proto_name)) {
462 if (pinfo->layer_names->len > 0)
463 g_string_append(pinfo->layer_names, ":");
464 g_string_append(pinfo->layer_names,
465 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
469 if (pinfo->in_error_pkt) {
470 ret = call_dissector_work_error(handle, tvb, pinfo, tree);
473 * Just call the subdissector.
475 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
479 * That dissector didn't accept the packet, so
480 * remove its protocol's name from the list
483 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
484 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
487 pinfo->current_proto = saved_proto;
488 pinfo->can_desegment = saved_can_desegment;
494 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
495 packet_info *pinfo_arg, proto_tree *tree)
497 packet_info *pinfo = pinfo_arg;
498 const char *saved_proto;
499 guint16 saved_can_desegment;
500 volatile int ret = 0;
501 gboolean save_writable;
504 address save_net_src;
505 address save_net_dst;
510 * This isn't a packet being transported inside
511 * the protocol whose dissector is calling us,
512 * it's a copy of a packet that caused an error
513 * in some protocol included in a packet that
514 * reports the error (e.g., an ICMP Unreachable
519 * Save the current state of the writability of
520 * the columns, and restore them after the
521 * dissector returns, so that the columns
522 * don't reflect the packet that got the error,
523 * they reflect the packet that reported the
526 saved_proto = pinfo->current_proto;
527 saved_can_desegment = pinfo->can_desegment;
529 save_writable = col_get_writable(pinfo->cinfo);
530 col_set_writable(pinfo->cinfo, FALSE);
531 save_dl_src = pinfo->dl_src;
532 save_dl_dst = pinfo->dl_dst;
533 save_net_src = pinfo->net_src;
534 save_net_dst = pinfo->net_dst;
535 save_src = pinfo->src;
536 save_dst = pinfo->dst;
538 /* Dissect the contained packet. */
540 ret = call_dissector_through_handle(handle, tvb,pinfo, tree);
544 * Restore the column writability and addresses.
546 col_set_writable(pinfo->cinfo, save_writable);
547 pinfo->dl_src = save_dl_src;
548 pinfo->dl_dst = save_dl_dst;
549 pinfo->net_src = save_net_src;
550 pinfo->net_dst = save_net_dst;
551 pinfo->src = save_src;
552 pinfo->dst = save_dst;
555 * Restore the current protocol, so any
556 * "Short Frame" indication reflects that
557 * protocol, not the protocol for the
558 * packet that got the error.
560 pinfo->current_proto = saved_proto;
563 * Restore the desegmentability state.
565 pinfo->can_desegment = saved_can_desegment;
568 * Rethrow the exception, so this will be
569 * reported as a short frame.
573 CATCH(ReportedBoundsError) {
575 * "ret" wasn't set because an exception was thrown
576 * before "call_dissector_through_handle()" returned.
577 * As it called something, at least one dissector
578 * accepted the packet, and, as an exception was
579 * thrown, not only was all the tvbuff dissected,
580 * a dissector tried dissecting past the end of
581 * the data in some tvbuff, so we'll assume that
582 * the entire tvbuff was dissected.
584 ret = tvb_length(tvb);
586 CATCH(OutOfMemoryError) {
591 col_set_writable(pinfo->cinfo, save_writable);
592 pinfo->dl_src = save_dl_src;
593 pinfo->dl_dst = save_dl_dst;
594 pinfo->net_src = save_net_src;
595 pinfo->net_dst = save_net_dst;
596 pinfo->src = save_src;
597 pinfo->dst = save_dst;
598 pinfo->want_pdu_tracking = 0;
603 * An entry in the hash table portion of a dissector table.
606 dissector_handle_t initial;
607 dissector_handle_t current;
613 * "hash_table" is a hash table, indexed by port number, supplying
614 * a "struct dtbl_entry"; it records what dissector is assigned to
615 * that uint or string value in that table.
617 * "dissector_handles" is a list of all dissectors that *could* be
618 * used in that table; not all of them are necessarily in the table,
619 * as they may be for protocols that don't have a fixed uint value,
620 * e.g. for TCP or UDP port number tables and protocols with no fixed
623 * "ui_name" is the name the dissector table has in the user interface.
625 * "type" is a field type giving the width of the uint value for that
626 * dissector table, if it's a uint dissector table.
628 * "base" is the base in which to display the uint value for that
629 * dissector table, if it's a uint dissector table.
631 struct dissector_table {
632 GHashTable *hash_table;
633 GSList *dissector_handles;
639 static GHashTable *dissector_tables = NULL;
641 /* Finds a dissector table by table name. */
643 find_dissector_table(const char *name)
645 g_assert(dissector_tables);
646 return g_hash_table_lookup( dissector_tables, name );
649 /* Find an entry in a uint dissector table. */
650 static dtbl_entry_t *
651 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
653 switch (sub_dissectors->type) {
660 * You can do a uint lookup in these tables.
666 * But you can't do a uint lookup in any other types
669 g_assert_not_reached();
675 return g_hash_table_lookup(sub_dissectors->hash_table,
676 GUINT_TO_POINTER(pattern));
681 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
683 dtbl_entry_t *dtbl_entry;
686 g_warning("%s: %s registering using a pattern of 0",
687 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
690 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
691 if (dtbl_entry != NULL) {
692 g_warning("%s: %s registering using pattern %d already registered by %s",
693 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
694 pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
699 /* Add an entry to a uint dissector table. */
701 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
703 dissector_table_t sub_dissectors;
704 dtbl_entry_t *dtbl_entry;
706 sub_dissectors = find_dissector_table(name);
709 * Make sure the dissector table exists.
711 if (sub_dissectors == NULL) {
712 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
714 fprintf(stderr, "Protocol being registered is \"%s\"\n",
715 proto_get_protocol_long_name(handle->protocol));
716 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
722 g_assert(handle!=NULL);
723 switch (sub_dissectors->type) {
730 * You can do a uint lookup in these tables.
736 * But you can't do a uint lookup in any other types
739 g_assert_not_reached();
743 dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
746 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
747 dtbl_entry->current = handle;
748 dtbl_entry->initial = dtbl_entry->current;
750 /* do the table insertion */
751 g_hash_table_insert( sub_dissectors->hash_table,
752 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
755 * Now add it to the list of handles that could be used with this
756 * table, because it *is* being used with this table.
758 dissector_add_handle(name, handle);
761 /* Delete the entry for a dissector in a uint dissector table
762 with a particular pattern. */
764 /* NOTE: this doesn't use the dissector call variable. It is included to */
765 /* be consistant with the dissector_add_uint and more importantly to be used */
766 /* if the technique of adding a temporary dissector is implemented. */
767 /* If temporary dissectors are deleted, then the original dissector must */
770 dissector_delete_uint(const char *name, const guint32 pattern,
771 dissector_handle_t handle _U_)
773 dissector_table_t sub_dissectors = find_dissector_table( name);
774 dtbl_entry_t *dtbl_entry;
777 g_assert( sub_dissectors);
782 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
784 if (dtbl_entry != NULL) {
788 g_hash_table_remove(sub_dissectors->hash_table,
789 GUINT_TO_POINTER(pattern));
792 * Now free up the entry.
798 /* Change the entry for a dissector in a uint dissector table
799 with a particular pattern to use a new dissector handle. */
801 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
803 dissector_table_t sub_dissectors = find_dissector_table( name);
804 dtbl_entry_t *dtbl_entry;
807 g_assert( sub_dissectors);
810 * See if the entry already exists. If so, reuse it.
812 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
813 if (dtbl_entry != NULL) {
814 dtbl_entry->current = handle;
819 * Don't create an entry if there is no dissector handle - I.E. the
820 * user said not to decode something that wasn't being decoded
821 * in the first place.
826 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
827 dtbl_entry->initial = NULL;
828 dtbl_entry->current = handle;
830 /* do the table insertion */
831 g_hash_table_insert( sub_dissectors->hash_table,
832 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
835 /* Reset an entry in a uint dissector table to its initial value. */
837 dissector_reset_uint(const char *name, const guint32 pattern)
839 dissector_table_t sub_dissectors = find_dissector_table( name);
840 dtbl_entry_t *dtbl_entry;
843 g_assert( sub_dissectors);
848 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
850 if (dtbl_entry == NULL)
854 * Found - is there an initial value?
856 if (dtbl_entry->initial != NULL) {
857 dtbl_entry->current = dtbl_entry->initial;
859 g_hash_table_remove(sub_dissectors->hash_table,
860 GUINT_TO_POINTER(pattern));
865 /* Look for a given value in a given uint dissector table and, if found,
866 call the dissector with the arguments supplied, and return TRUE,
867 otherwise return FALSE. */
870 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
871 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const gboolean add_proto_name)
873 dtbl_entry_t *dtbl_entry;
874 struct dissector_handle *handle;
875 guint32 saved_match_uint;
878 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
879 if (dtbl_entry != NULL) {
881 * Is there currently a dissector handle for this entry?
883 handle = dtbl_entry->current;
884 if (handle == NULL) {
886 * No - pretend this dissector didn't exist,
887 * so that other dissectors might have a chance
888 * to dissect this packet.
894 * Save the current value of "pinfo->match_uint",
895 * set it to the uint_val that matched, call the
896 * dissector, and restore "pinfo->match_uint".
898 saved_match_uint = pinfo->match_uint;
899 pinfo->match_uint = uint_val;
900 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name);
901 pinfo->match_uint = saved_match_uint;
904 * If a new-style dissector returned 0, it means that
905 * it didn't think this tvbuff represented a packet for
906 * its protocol, and didn't dissect anything.
908 * Old-style dissectors can't reject the packet.
910 * 0 is also returned if the protocol wasn't enabled.
912 * If the packet was rejected, we return FALSE, so that
913 * other dissectors might have a chance to dissect this
914 * packet, otherwise we return TRUE.
922 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
923 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
926 return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE);
929 /* Look for a given value in a given uint dissector table and, if found,
930 return the dissector handle for that value. */
932 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
934 dtbl_entry_t *dtbl_entry;
936 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
937 if (dtbl_entry != NULL)
938 return dtbl_entry->current;
943 /* Find an entry in a string dissector table. */
944 static dtbl_entry_t *
945 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
947 switch (sub_dissectors->type) {
952 * You can do a string lookup in these tables.
958 * But you can't do a string lookup in any other types
961 g_assert_not_reached();
967 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
970 /* Add an entry to a string dissector table. */
972 dissector_add_string(const char *name, const gchar *pattern,
973 dissector_handle_t handle)
975 dissector_table_t sub_dissectors = find_dissector_table( name);
976 dtbl_entry_t *dtbl_entry;
979 * Make sure the dissector table exists.
981 if (sub_dissectors == NULL) {
982 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
984 fprintf(stderr, "Protocol being registered is \"%s\"\n",
985 proto_get_protocol_long_name(handle->protocol));
986 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
992 g_assert(handle!=NULL);
993 switch (sub_dissectors->type) {
998 * You can do a string lookup in these tables.
1004 * But you can't do a string lookup in any other types
1007 g_assert_not_reached();
1010 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1011 dtbl_entry->current = handle;
1012 dtbl_entry->initial = dtbl_entry->current;
1014 /* do the table insertion */
1015 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1016 (gpointer)dtbl_entry);
1019 * Now add it to the list of handles that could be used with this
1020 * table, because it *is* being used with this table.
1022 dissector_add_handle(name, handle);
1025 /* Delete the entry for a dissector in a string dissector table
1026 with a particular pattern. */
1028 /* NOTE: this doesn't use the dissector call variable. It is included to */
1029 /* be consistant with the dissector_add_string and more importantly to */
1030 /* be used if the technique of adding a temporary dissector is */
1032 /* If temporary dissectors are deleted, then the original dissector must */
1035 dissector_delete_string(const char *name, const gchar *pattern,
1036 dissector_handle_t handle _U_)
1038 dissector_table_t sub_dissectors = find_dissector_table( name);
1039 dtbl_entry_t *dtbl_entry;
1042 g_assert( sub_dissectors);
1047 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1049 if (dtbl_entry != NULL) {
1051 * Found - remove it.
1053 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1056 * Now free up the entry.
1062 /* Change the entry for a dissector in a string dissector table
1063 with a particular pattern to use a new dissector handle. */
1065 dissector_change_string(const char *name, const gchar *pattern,
1066 dissector_handle_t handle)
1068 dissector_table_t sub_dissectors = find_dissector_table( name);
1069 dtbl_entry_t *dtbl_entry;
1072 g_assert( sub_dissectors);
1075 * See if the entry already exists. If so, reuse it.
1077 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1078 if (dtbl_entry != NULL) {
1079 dtbl_entry->current = handle;
1084 * Don't create an entry if there is no dissector handle - I.E. the
1085 * user said not to decode something that wasn't being decoded
1086 * in the first place.
1091 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1092 dtbl_entry->initial = NULL;
1093 dtbl_entry->current = handle;
1095 /* do the table insertion */
1096 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1097 (gpointer)dtbl_entry);
1100 /* Reset an entry in a string sub-dissector table to its initial value. */
1102 dissector_reset_string(const char *name, const gchar *pattern)
1104 dissector_table_t sub_dissectors = find_dissector_table( name);
1105 dtbl_entry_t *dtbl_entry;
1108 g_assert( sub_dissectors);
1113 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1115 if (dtbl_entry == NULL)
1119 * Found - is there an initial value?
1121 if (dtbl_entry->initial != NULL) {
1122 dtbl_entry->current = dtbl_entry->initial;
1124 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1129 /* Look for a given string in a given dissector table and, if found, call
1130 the dissector with the arguments supplied, and return TRUE, otherwise
1133 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1134 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1136 dtbl_entry_t *dtbl_entry;
1137 struct dissector_handle *handle;
1139 const gchar *saved_match_string;
1141 /* XXX ASSERT instead ? */
1142 if (!string) return FALSE;
1143 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1144 if (dtbl_entry != NULL) {
1146 * Is there currently a dissector handle for this entry?
1148 handle = dtbl_entry->current;
1149 if (handle == NULL) {
1151 * No - pretend this dissector didn't exist,
1152 * so that other dissectors might have a chance
1153 * to dissect this packet.
1159 * Save the current value of "pinfo->match_string",
1160 * set it to the string that matched, call the
1161 * dissector, and restore "pinfo->match_string".
1163 saved_match_string = pinfo->match_string;
1164 pinfo->match_string = string;
1165 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1166 pinfo->match_string = saved_match_string;
1169 * If a new-style dissector returned 0, it means that
1170 * it didn't think this tvbuff represented a packet for
1171 * its protocol, and didn't dissect anything.
1173 * Old-style dissectors can't reject the packet.
1175 * 0 is also returned if the protocol wasn't enabled.
1177 * If the packet was rejected, we return FALSE, so that
1178 * other dissectors might have a chance to dissect this
1179 * packet, otherwise we return TRUE.
1186 /* Look for a given value in a given string dissector table and, if found,
1187 return the dissector handle for that value. */
1189 dissector_get_string_handle(dissector_table_t sub_dissectors,
1190 const gchar *string)
1192 dtbl_entry_t *dtbl_entry;
1194 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1195 if (dtbl_entry != NULL)
1196 return dtbl_entry->current;
1202 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1204 return dtbl_entry->current;
1207 /* Add a handle to the list of handles that *could* be used with this
1208 table. That list is used by code in the UI. */
1210 dissector_add_handle(const char *name, dissector_handle_t handle)
1212 dissector_table_t sub_dissectors = find_dissector_table( name);
1216 * Make sure the dissector table exists.
1218 if (sub_dissectors == NULL) {
1219 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1221 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1222 proto_get_protocol_long_name(handle->protocol));
1223 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1228 /* Is it already in this list? */
1229 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1230 if (entry != NULL) {
1232 * Yes - don't insert it again.
1237 /* Add it to the list. */
1238 sub_dissectors->dissector_handles =
1239 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1243 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1245 return dtbl_entry->initial;
1248 /**************************************************/
1250 /* Routines to walk dissector tables */
1252 /**************************************************/
1254 typedef struct dissector_foreach_info {
1255 gpointer caller_data;
1256 DATFunc caller_func;
1258 const gchar *table_name;
1259 ftenum_t selector_type;
1260 } dissector_foreach_info_t;
1263 * Called for each entry in a dissector table.
1266 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1268 dissector_foreach_info_t *info;
1269 dtbl_entry_t *dtbl_entry;
1272 g_assert(user_data);
1275 if (dtbl_entry->current == NULL ||
1276 dtbl_entry->current->protocol == NULL) {
1278 * Either there is no dissector for this entry, or
1279 * the dissector doesn't have a protocol associated
1282 * XXX - should the latter check be done?
1288 info->caller_func(info->table_name, info->selector_type, key, value,
1293 * Called for each entry in the table of all dissector tables.
1296 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1298 dissector_table_t sub_dissectors;
1299 dissector_foreach_info_t *info;
1302 g_assert(user_data);
1304 sub_dissectors = value;
1306 info->table_name = (gchar*) key;
1307 info->selector_type = get_dissector_table_selector_type(info->table_name);
1308 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1312 * Walk all dissector tables calling a user supplied function on each
1316 dissector_all_tables_foreach (DATFunc func,
1319 dissector_foreach_info_t info;
1321 info.caller_data = user_data;
1322 info.caller_func = func;
1323 info.next_func = dissector_table_foreach_func;
1324 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1328 * Walk one dissector table's hash table calling a user supplied function
1332 dissector_table_foreach (const char *name,
1336 dissector_foreach_info_t info;
1337 dissector_table_t sub_dissectors = find_dissector_table( name);
1339 info.table_name = name;
1340 info.selector_type = sub_dissectors->type;
1341 info.caller_func = func;
1342 info.caller_data = user_data;
1343 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1347 * Walk one dissector table's list of handles calling a user supplied
1348 * function on each entry.
1351 dissector_table_foreach_handle(const char *name,
1352 DATFunc_handle func,
1355 dissector_table_t sub_dissectors = find_dissector_table( name);
1358 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1359 tmp = g_slist_next(tmp))
1360 func(name, tmp->data, user_data);
1364 * Called for each entry in a dissector table.
1367 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1369 dtbl_entry_t *dtbl_entry;
1370 dissector_foreach_info_t *info;
1373 g_assert(user_data);
1376 if (dtbl_entry->initial == dtbl_entry->current) {
1378 * Entry hasn't changed - don't call the function.
1384 info->caller_func(info->table_name, info->selector_type, key, value,
1389 * Walk all dissector tables calling a user supplied function only on
1390 * any entry that has been changed from its original state.
1393 dissector_all_tables_foreach_changed (DATFunc func,
1396 dissector_foreach_info_t info;
1398 info.caller_data = user_data;
1399 info.caller_func = func;
1400 info.next_func = dissector_table_foreach_changed_func;
1401 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1405 * Walk one dissector table calling a user supplied function only on
1406 * any entry that has been changed from its original state.
1409 dissector_table_foreach_changed (const char *name,
1413 dissector_foreach_info_t info;
1414 dissector_table_t sub_dissectors = find_dissector_table( name);
1416 info.table_name = name;
1417 info.selector_type = sub_dissectors->type;
1418 info.caller_func = func;
1419 info.caller_data = user_data;
1420 g_hash_table_foreach(sub_dissectors->hash_table,
1421 dissector_table_foreach_changed_func, &info);
1424 typedef struct dissector_foreach_table_info {
1425 gpointer caller_data;
1426 DATFunc_table caller_func;
1427 } dissector_foreach_table_info_t;
1430 * Called for each entry in the table of all dissector tables.
1433 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1435 dissector_table_t table;
1436 dissector_foreach_table_info_t *info;
1440 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1444 * Walk all dissector tables calling a user supplied function on each
1448 dissector_all_tables_foreach_table (DATFunc_table func,
1451 dissector_foreach_table_info_t info;
1453 info.caller_data = user_data;
1454 info.caller_func = func;
1455 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1459 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1462 dissector_table_t sub_dissectors;
1464 /* Create our hash-of-hashes if it doesn't already exist */
1465 if (!dissector_tables) {
1466 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1467 g_assert(dissector_tables);
1470 /* Make sure the registration is unique */
1471 if(g_hash_table_lookup( dissector_tables, name )) {
1472 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1475 /* Create and register the dissector table for this name; returns */
1476 /* a pointer to the dissector table. */
1477 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1485 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1486 * so we use "g_direct_hash()" and "g_direct_equal()".
1488 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1494 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1499 g_assert_not_reached();
1501 sub_dissectors->dissector_handles = NULL;
1502 sub_dissectors->ui_name = ui_name;
1503 sub_dissectors->type = type;
1504 sub_dissectors->base = base;
1505 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1506 return sub_dissectors;
1510 get_dissector_table_ui_name(const char *name)
1512 dissector_table_t sub_dissectors = find_dissector_table( name);
1514 return sub_dissectors->ui_name;
1518 get_dissector_table_selector_type(const char *name)
1520 dissector_table_t sub_dissectors = find_dissector_table( name);
1522 return sub_dissectors->type;
1526 get_dissector_table_base(const char *name)
1528 dissector_table_t sub_dissectors = find_dissector_table( name);
1530 return sub_dissectors->base;
1533 static GHashTable *heur_dissector_lists = NULL;
1536 /* Finds a heuristic dissector table by field name. */
1537 static heur_dissector_list_t *
1538 find_heur_dissector_list(const char *name)
1540 g_assert(heur_dissector_lists != NULL);
1541 return g_hash_table_lookup(heur_dissector_lists, name);
1545 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1547 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1548 const char *proto_name;
1549 heur_dtbl_entry_t *dtbl_entry;
1552 * Make sure the dissector table exists.
1554 if (sub_dissectors == NULL) {
1555 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1557 proto_name = proto_get_protocol_name(proto);
1558 if (proto_name != NULL) {
1559 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1562 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1567 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1568 dtbl_entry->dissector = dissector;
1569 dtbl_entry->protocol = find_protocol_by_id(proto);
1571 /* do the table insertion */
1572 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1577 static int find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1578 const heur_dtbl_entry_t *dtbl_entry_a = (const heur_dtbl_entry_t *) a;
1579 const heur_dtbl_entry_t *dtbl_entry_b = (const heur_dtbl_entry_t *) b;
1580 return (dtbl_entry_a->dissector == dtbl_entry_b->dissector) &&
1581 (dtbl_entry_a->protocol == dtbl_entry_b->protocol) ? 0 : 1;
1584 void heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1585 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1586 heur_dtbl_entry_t dtbl_entry;
1587 GSList* found_entry;
1590 g_assert(sub_dissectors != NULL);
1592 dtbl_entry.dissector = dissector;
1594 dtbl_entry.protocol = find_protocol_by_id(proto);
1596 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &dtbl_entry, find_matching_heur_dissector);
1599 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1600 g_free(g_slist_nth_data(found_entry, 1));
1601 g_slist_free_1(found_entry);
1607 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1608 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1611 const char *saved_proto;
1613 heur_dtbl_entry_t *dtbl_entry;
1614 guint16 saved_can_desegment;
1615 gint saved_layer_names_len = 0;
1617 /* can_desegment is set to 2 by anyone which offers this api/service.
1618 then everytime a subdissector is called it is decremented by one.
1619 thus only the subdissector immediately ontop of whoever offers this
1621 We save the current value of "can_desegment" for the
1622 benefit of TCP proxying dissectors such as SOCKS, so they
1623 can restore it and allow the dissectors they call to use
1624 the desegmentation service.
1626 saved_can_desegment=pinfo->can_desegment;
1627 pinfo->saved_can_desegment = saved_can_desegment;
1628 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1631 saved_proto = pinfo->current_proto;
1633 if (pinfo->layer_names != NULL)
1634 saved_layer_names_len = (gint) pinfo->layer_names->len;
1636 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1637 /* XXX - why set this now and above? */
1638 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1639 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1641 if (dtbl_entry->protocol != NULL &&
1642 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1644 * No - don't try this dissector.
1649 if (dtbl_entry->protocol != NULL) {
1650 pinfo->current_proto =
1651 proto_get_protocol_short_name(dtbl_entry->protocol);
1654 * Add the protocol name to the layers; we'll remove it
1655 * if the dissector fails.
1657 if (pinfo->layer_names) {
1658 if (pinfo->layer_names->len > 0)
1659 g_string_append(pinfo->layer_names, ":");
1660 g_string_append(pinfo->layer_names,
1661 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol)));
1664 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1665 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1666 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1667 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1668 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1672 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1673 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1676 * That dissector didn't accept the packet, so
1677 * remove its protocol's name from the list
1680 if (pinfo->layer_names != NULL) {
1681 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1685 pinfo->current_proto = saved_proto;
1686 pinfo->can_desegment=saved_can_desegment;
1691 * Called for each entry in the table of all heuristic dissector tables.
1693 typedef struct heur_dissector_foreach_table_info {
1694 gpointer caller_data;
1695 DATFunc_heur_table caller_func;
1696 } heur_dissector_foreach_table_info_t;
1699 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1701 heur_dissector_foreach_table_info_t *info;
1704 (*info->caller_func)((gchar*)key, value, info->caller_data);
1708 * Walk all heuristic dissector tables calling a user supplied function on each
1712 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1715 heur_dissector_foreach_table_info_t info;
1717 info.caller_data = user_data;
1718 info.caller_func = func;
1719 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1723 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1725 /* Create our hash-of-lists if it doesn't already exist */
1726 if (heur_dissector_lists == NULL) {
1727 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1728 g_assert(heur_dissector_lists != NULL);
1731 /* Make sure the registration is unique */
1732 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1734 *sub_dissectors = NULL; /* initially empty */
1735 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1736 (gpointer) sub_dissectors);
1740 * Register dissectors by name; used if one dissector always calls a
1741 * particular dissector, or if it bases the decision of which dissector
1742 * to call on something other than a numerical value or on "try a bunch
1743 * of dissectors until one likes the packet".
1747 * List of registered dissectors.
1749 static GHashTable *registered_dissectors = NULL;
1751 /* Get the long name of the protocol for a dissector handle, if it has
1754 dissector_handle_get_long_name(const dissector_handle_t handle)
1756 if (handle == NULL || handle->protocol == NULL) {
1759 return proto_get_protocol_long_name(handle->protocol);
1762 /* Get the short name of the protocol for a dissector handle, if it has
1765 dissector_handle_get_short_name(const dissector_handle_t handle)
1767 if (handle->protocol == NULL) {
1769 * No protocol (see, for example, the handle for
1770 * dissecting the set of protocols where the first
1771 * octet of the payload is an OSI network layer protocol
1776 return proto_get_protocol_short_name(handle->protocol);
1779 /* Get the index of the protocol for a dissector handle, if it has
1782 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1784 if (handle->protocol == NULL) {
1786 * No protocol (see, for example, the handle for
1787 * dissecting the set of protocols where the first
1788 * octet of the payload is an OSI network layer protocol
1793 return proto_get_id(handle->protocol);
1796 /* Find a registered dissector by name. */
1798 find_dissector(const char *name)
1800 g_assert(registered_dissectors != NULL);
1801 return g_hash_table_lookup(registered_dissectors, name);
1804 /* Create an anonymous handle for a dissector. */
1806 create_dissector_handle(dissector_t dissector, const int proto)
1808 struct dissector_handle *handle;
1810 handle = g_malloc(sizeof (struct dissector_handle));
1811 handle->name = NULL;
1812 handle->is_new = FALSE;
1813 handle->dissector.old = dissector;
1814 handle->protocol = find_protocol_by_id(proto);
1820 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1822 struct dissector_handle *handle;
1824 handle = g_malloc(sizeof (struct dissector_handle));
1825 handle->name = NULL;
1826 handle->is_new = TRUE;
1827 handle->dissector.new = dissector;
1828 handle->protocol = find_protocol_by_id(proto);
1833 /* Register a dissector by name. */
1835 register_dissector(const char *name, dissector_t dissector, const int proto)
1837 struct dissector_handle *handle;
1839 /* Create our hash table if it doesn't already exist */
1840 if (registered_dissectors == NULL) {
1841 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1842 g_assert(registered_dissectors != NULL);
1845 /* Make sure the registration is unique */
1846 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1848 handle = g_malloc(sizeof (struct dissector_handle));
1849 handle->name = name;
1850 handle->is_new = FALSE;
1851 handle->dissector.old = dissector;
1852 handle->protocol = find_protocol_by_id(proto);
1854 g_hash_table_insert(registered_dissectors, (gpointer)name,
1859 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
1861 struct dissector_handle *handle;
1863 /* Create our hash table if it doesn't already exist */
1864 if (registered_dissectors == NULL) {
1865 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1866 g_assert(registered_dissectors != NULL);
1869 /* Make sure the registration is unique */
1870 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1872 handle = g_malloc(sizeof (struct dissector_handle));
1873 handle->name = name;
1874 handle->is_new = TRUE;
1875 handle->dissector.new = dissector;
1876 handle->protocol = find_protocol_by_id(proto);
1878 g_hash_table_insert(registered_dissectors, (gpointer)name,
1882 /* Call a dissector through a handle but if the dissector rejected it
1886 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
1887 packet_info *pinfo, proto_tree *tree)
1891 g_assert(handle != NULL);
1892 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1896 /* Call a dissector through a handle and if this fails call the "data"
1900 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1901 packet_info *pinfo, proto_tree *tree)
1905 ret = call_dissector_only(handle, tvb, pinfo, tree);
1908 * The protocol was disabled, or the dissector rejected
1909 * it. Just dissect this packet as data.
1911 g_assert(data_handle != NULL);
1912 g_assert(data_handle->protocol != NULL);
1913 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE);
1914 return tvb_length(tvb);
1920 * Dumps the "layer type"/"decode as" associations to stdout, similar
1921 * to the proto_registrar_dump_*() routines.
1923 * There is one record per line. The fields are tab-delimited.
1925 * Field 1 = layer type, e.g. "tcp.port"
1926 * Field 2 = selector in decimal
1927 * Field 3 = "decode as" name, e.g. "http"
1932 dissector_dump_decodes_display(const gchar *table_name,
1933 ftenum_t selector_type _U_, const gpointer key, const gpointer value,
1934 gpointer user_data _U_)
1936 guint32 selector = (guint32)(unsigned long) key;
1937 dissector_table_t sub_dissectors = find_dissector_table(table_name);
1938 dtbl_entry_t *dtbl_entry;
1939 dissector_handle_t handle;
1941 const gchar *decode_as;
1943 g_assert(sub_dissectors);
1944 switch (sub_dissectors->type) {
1951 g_assert(dtbl_entry);
1953 handle = dtbl_entry->current;
1956 proto_id = dissector_handle_get_protocol_index(handle);
1958 if (proto_id != -1) {
1959 decode_as = proto_get_protocol_filter_name(proto_id);
1960 g_assert(decode_as != NULL);
1961 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
1971 dissector_dump_decodes(void)
1973 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
1976 static GPtrArray* post_dissectors = NULL;
1977 static guint num_of_postdissectors = 0;
1980 register_postdissector(dissector_handle_t handle)
1982 if (!post_dissectors)
1983 post_dissectors = g_ptr_array_new();
1985 g_ptr_array_add(post_dissectors, handle);
1986 num_of_postdissectors++;
1990 have_postdissector(void)
1993 dissector_handle_t handle;
1995 for(i = 0; i < num_of_postdissectors; i++) {
1996 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
1998 if (handle->protocol != NULL
1999 && proto_is_protocol_enabled(handle->protocol)) {
2000 /* We have at least one enabled postdissector */
2008 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2012 for(i = 0; i < num_of_postdissectors; i++) {
2013 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),