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 /* Cleanup the table of conversations. Do this before freeing seasonal
153 * memory (at least until conversation's use of g_slist is changed).
155 epan_conversation_cleanup();
157 /* Reclaim all memory of seasonal scope */
160 /* Cleanup the table of circuits. */
161 epan_circuit_cleanup();
163 /* TODO: Introduce cleanup_routines */
164 /* Cleanup protocol-specific variables. */
165 g_slist_foreach(init_routines, &call_init_routine, NULL);
167 /* Cleanup the stream-handling tables */
170 /* Initialize the expert infos */
174 /* Allow protocols to register a "cleanup" routine to be
175 * run after the initial sequential run through the packets.
176 * Note that the file can still be open after this; this is not
177 * the final cleanup. */
178 static GSList *postseq_cleanup_routines;
181 register_postseq_cleanup_routine(void_func_t func)
183 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
187 /* Call all the registered "postseq_cleanup" routines. */
189 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
191 void_func_t func = (void_func_t)routine;
196 postseq_cleanup_all_protocols(void)
198 g_slist_foreach(postseq_cleanup_routines,
199 &call_postseq_cleanup_routine, NULL);
203 * Add a new data source to the list of data sources for a frame, given
204 * the tvbuff for the data source and its name.
207 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
211 src = ep_alloc(sizeof (data_source));
213 src->name_initialized = FALSE;
215 pinfo->data_src = g_slist_append(pinfo->data_src, src);
219 get_data_source_name(data_source *src)
221 if (!src->name_initialized) {
222 src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
223 src->name_initialized = TRUE;
230 * Free up a frame's list of data sources.
233 free_data_sources(packet_info *pinfo)
235 if (pinfo->data_src) {
236 g_slist_free(pinfo->data_src);
237 pinfo->data_src = NULL;
242 mark_frame_as_depended_upon(packet_info *pinfo, guint32 frame_num)
244 /* Don't mark a frame as dependent on itself */
245 if (frame_num != PINFO_FD_NUM(pinfo)) {
246 pinfo->dependent_frames = g_slist_prepend(pinfo->dependent_frames, GUINT_TO_POINTER(frame_num));
250 /* Allow dissectors to register a "final_registration" routine
251 * that is run like the proto_register_XXX() routine, but at the
252 * end of the epan_init() function; that is, *after* all other
253 * subsystems, like dfilters, have finished initializing. This is
254 * useful for dissector registration routines which need to compile
255 * display filters. dfilters can't initialize itself until all protocols
256 * have registered themselves. */
257 static GSList *final_registration_routines;
260 register_final_registration_routine(void (*func)(void))
262 final_registration_routines = g_slist_append(final_registration_routines,
266 /* Call all the registered "final_registration" routines. */
268 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
270 void_func_t func = (void_func_t)routine;
276 final_registration_all_protocols(void)
278 g_slist_foreach(final_registration_routines,
279 &call_final_registration_routine, NULL);
283 /* Creates the top-most tvbuff and calls dissect_frame() */
285 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
286 const guchar *pd, frame_data *fd, column_info *cinfo)
290 memset(&edt->pi, 0, sizeof(edt->pi));
291 edt->pi.current_proto = "<Missing Protocol Name>";
292 edt->pi.cinfo = cinfo;
294 edt->pi.pseudo_header = pseudo_header;
295 edt->pi.dl_src.type = AT_NONE;
296 edt->pi.dl_dst.type = AT_NONE;
297 edt->pi.net_src.type = AT_NONE;
298 edt->pi.net_dst.type = AT_NONE;
299 edt->pi.src.type = AT_NONE;
300 edt->pi.dst.type = AT_NONE;
301 edt->pi.ctype = CT_NONE;
302 edt->pi.noreassembly_reason = "";
303 edt->pi.ptype = PT_NONE;
304 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
305 edt->pi.dcetransporttype = -1;
306 edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
307 edt->pi.dcerpc_procedure_name="";
308 edt->pi.link_dir = LINK_DIR_UNKNOWN;
311 /* to enable decode as for ethertype=0x0000 (fix for bug 4721) */
312 edt->pi.ethertype = G_MAXINT;
314 EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
318 * XXX - currently, the length arguments to
319 * tvb_new_real_data() are signed, but the captured
320 * and reported length values are unsigned; this means
321 * that length values > 2^31 - 1 will appear as
322 * negative lengths in tvb_new_real_data().
324 * Captured length values that large will already
325 * have been filtered out by the Wiretap modules
326 * (the file will be reported as corrupted), to
327 * avoid trying to allocate large chunks of data.
329 * Reported length values will not have been
330 * filtered out, and should not be filtered out,
331 * as those lengths are not necessarily invalid.
333 * For now, we clip the reported length at G_MAXINT,
334 * so that tvb_new_real_data() doesn't fail. It
335 * would throw an exception, which we'd catch, but
336 * that would mean we would have no tvbuffs
337 * associated with edt, which would upset much of
338 * the rest of the application.
340 edt->tvb = tvb_new_real_data(pd, fd->cap_len,
341 fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len);
342 /* Add this tvbuffer into the data_src list */
343 add_new_data_source(&edt->pi, edt->tvb, "Frame");
345 /* Even though dissect_frame() catches all the exceptions a
346 * sub-dissector can throw, dissect_frame() itself may throw
347 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
348 * in this function. */
349 if(frame_handle != NULL)
350 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
354 g_assert_not_reached();
356 CATCH(ReportedBoundsError) {
357 if(proto_malformed != -1){
358 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
359 "[Malformed Frame: Packet Length]" );
361 g_assert_not_reached();
364 CATCH(OutOfMemoryError) {
369 EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
371 fd->flags.visited = 1;
374 /*********************** code added for sub-dissector lookup *********************/
377 * A dissector handle.
379 struct dissector_handle {
380 const char *name; /* dissector name */
381 gboolean is_new; /* TRUE if new-style dissector */
386 protocol_t *protocol;
389 /* This function will return
390 * old style dissector :
391 * length of the payload or 1 of the payload is empty
393 * >0 this protocol was successfully dissected and this was this protocol.
394 * 0 this packet did not match this protocol.
396 * The only time this function will return 0 is if it is a new style dissector
397 * and if the dissector rejected the packet.
400 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
401 packet_info *pinfo, proto_tree *tree, void *data)
403 const char *saved_proto;
406 saved_proto = pinfo->current_proto;
408 if (handle->protocol != NULL) {
409 pinfo->current_proto =
410 proto_get_protocol_short_name(handle->protocol);
413 if (handle->is_new) {
414 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
415 ret = (*handle->dissector.new)(tvb, pinfo, tree, data);
416 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
418 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
419 (*handle->dissector.old)(tvb, pinfo, tree);
420 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
421 ret = tvb_length(tvb);
424 * XXX - a tvbuff can have 0 bytes of data in
425 * it, so we have to make sure we don't return
432 pinfo->current_proto = saved_proto;
438 * Call a dissector through a handle.
439 * If the protocol for that handle isn't enabled, return 0 without
440 * calling the dissector.
441 * Otherwise, if the handle refers to a new-style dissector, call the
442 * dissector and return its return value, otherwise call it and return
443 * the length of the tvbuff pointed to by the argument.
447 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
448 packet_info *pinfo_arg, proto_tree *tree, void *);
451 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb, packet_info *pinfo_arg,
452 proto_tree *tree, gboolean add_proto_name, void *data)
454 packet_info *pinfo = pinfo_arg;
455 const char *saved_proto;
456 guint16 saved_can_desegment;
458 gint saved_layer_names_len = 0;
460 if (handle->protocol != NULL &&
461 !proto_is_protocol_enabled(handle->protocol)) {
463 * The protocol isn't enabled.
468 saved_proto = pinfo->current_proto;
469 saved_can_desegment = pinfo->can_desegment;
471 if (pinfo->layer_names != NULL)
472 saved_layer_names_len = (gint) pinfo->layer_names->len;
475 * can_desegment is set to 2 by anyone which offers the
476 * desegmentation api/service.
477 * Then everytime a subdissector is called it is decremented
479 * Thus only the subdissector immediately on top of whoever
480 * offers this service can use it.
481 * We save the current value of "can_desegment" for the
482 * benefit of TCP proxying dissectors such as SOCKS, so they
483 * can restore it and allow the dissectors they call to use
484 * the desegmentation service.
486 pinfo->saved_can_desegment = saved_can_desegment;
487 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
488 if (handle->protocol != NULL) {
489 pinfo->current_proto =
490 proto_get_protocol_short_name(handle->protocol);
493 * Add the protocol name to the layers
494 * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
496 if ((pinfo->layer_names)&&(add_proto_name)) {
497 if (pinfo->layer_names->len > 0)
498 g_string_append(pinfo->layer_names, ":");
499 g_string_append(pinfo->layer_names,
500 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
504 if (pinfo->flags.in_error_pkt) {
505 ret = call_dissector_work_error(handle, tvb, pinfo, tree, data);
508 * Just call the subdissector.
510 ret = call_dissector_through_handle(handle, tvb, pinfo, tree, data);
514 * That dissector didn't accept the packet, so
515 * remove its protocol's name from the list
518 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
519 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
522 pinfo->current_proto = saved_proto;
523 pinfo->can_desegment = saved_can_desegment;
529 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
530 packet_info *pinfo_arg, proto_tree *tree, void *data)
532 packet_info *pinfo = pinfo_arg;
533 const char *saved_proto;
534 guint16 saved_can_desegment;
535 volatile int ret = 0;
536 gboolean save_writable;
539 address save_net_src;
540 address save_net_dst;
545 * This isn't a packet being transported inside
546 * the protocol whose dissector is calling us,
547 * it's a copy of a packet that caused an error
548 * in some protocol included in a packet that
549 * reports the error (e.g., an ICMP Unreachable
554 * Save the current state of the writability of
555 * the columns, and restore them after the
556 * dissector returns, so that the columns
557 * don't reflect the packet that got the error,
558 * they reflect the packet that reported the
561 saved_proto = pinfo->current_proto;
562 saved_can_desegment = pinfo->can_desegment;
564 save_writable = col_get_writable(pinfo->cinfo);
565 col_set_writable(pinfo->cinfo, FALSE);
566 save_dl_src = pinfo->dl_src;
567 save_dl_dst = pinfo->dl_dst;
568 save_net_src = pinfo->net_src;
569 save_net_dst = pinfo->net_dst;
570 save_src = pinfo->src;
571 save_dst = pinfo->dst;
573 /* Dissect the contained packet. */
575 ret = call_dissector_through_handle(handle, tvb,pinfo, tree, data);
579 * Restore the column writability and addresses.
581 col_set_writable(pinfo->cinfo, save_writable);
582 pinfo->dl_src = save_dl_src;
583 pinfo->dl_dst = save_dl_dst;
584 pinfo->net_src = save_net_src;
585 pinfo->net_dst = save_net_dst;
586 pinfo->src = save_src;
587 pinfo->dst = save_dst;
590 * Restore the current protocol, so any
591 * "Short Frame" indication reflects that
592 * protocol, not the protocol for the
593 * packet that got the error.
595 pinfo->current_proto = saved_proto;
598 * Restore the desegmentability state.
600 pinfo->can_desegment = saved_can_desegment;
603 * Rethrow the exception, so this will be
604 * reported as a short frame.
608 CATCH(ReportedBoundsError) {
610 * "ret" wasn't set because an exception was thrown
611 * before "call_dissector_through_handle()" returned.
612 * As it called something, at least one dissector
613 * accepted the packet, and, as an exception was
614 * thrown, not only was all the tvbuff dissected,
615 * a dissector tried dissecting past the end of
616 * the data in some tvbuff, so we'll assume that
617 * the entire tvbuff was dissected.
619 ret = tvb_length(tvb);
621 CATCH(OutOfMemoryError) {
626 col_set_writable(pinfo->cinfo, save_writable);
627 pinfo->dl_src = save_dl_src;
628 pinfo->dl_dst = save_dl_dst;
629 pinfo->net_src = save_net_src;
630 pinfo->net_dst = save_net_dst;
631 pinfo->src = save_src;
632 pinfo->dst = save_dst;
633 pinfo->want_pdu_tracking = 0;
638 * An entry in the hash table portion of a dissector table.
641 dissector_handle_t initial;
642 dissector_handle_t current;
648 * "hash_table" is a hash table, indexed by port number, supplying
649 * a "struct dtbl_entry"; it records what dissector is assigned to
650 * that uint or string value in that table.
652 * "dissector_handles" is a list of all dissectors that *could* be
653 * used in that table; not all of them are necessarily in the table,
654 * as they may be for protocols that don't have a fixed uint value,
655 * e.g. for TCP or UDP port number tables and protocols with no fixed
658 * "ui_name" is the name the dissector table has in the user interface.
660 * "type" is a field type giving the width of the uint value for that
661 * dissector table, if it's a uint dissector table.
663 * "base" is the base in which to display the uint value for that
664 * dissector table, if it's a uint dissector table.
666 struct dissector_table {
667 GHashTable *hash_table;
668 GSList *dissector_handles;
674 static GHashTable *dissector_tables = NULL;
676 /* Finds a dissector table by table name. */
678 find_dissector_table(const char *name)
680 g_assert(dissector_tables);
681 return g_hash_table_lookup( dissector_tables, name );
684 /* Find an entry in a uint dissector table. */
685 static dtbl_entry_t *
686 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
688 switch (sub_dissectors->type) {
695 * You can do a uint lookup in these tables.
701 * But you can't do a uint lookup in any other types
704 g_assert_not_reached();
710 return g_hash_table_lookup(sub_dissectors->hash_table,
711 GUINT_TO_POINTER(pattern));
716 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
718 dtbl_entry_t *dtbl_entry;
721 g_warning("%s: %s registering using a pattern of 0",
722 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
725 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
726 if (dtbl_entry != NULL) {
727 g_warning("%s: %s registering using pattern %d already registered by %s",
728 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
729 pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
734 /* Add an entry to a uint dissector table. */
736 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
738 dissector_table_t sub_dissectors;
739 dtbl_entry_t *dtbl_entry;
741 sub_dissectors = find_dissector_table(name);
744 * Make sure the dissector table exists.
746 if (sub_dissectors == NULL) {
747 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
749 fprintf(stderr, "Protocol being registered is \"%s\"\n",
750 proto_get_protocol_long_name(handle->protocol));
751 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
757 g_assert(handle!=NULL);
758 switch (sub_dissectors->type) {
765 * You can do a uint lookup in these tables.
771 * But you can't do a uint lookup in any other types
774 g_assert_not_reached();
778 dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
781 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
782 dtbl_entry->current = handle;
783 dtbl_entry->initial = dtbl_entry->current;
785 /* do the table insertion */
786 g_hash_table_insert( sub_dissectors->hash_table,
787 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
790 * Now add it to the list of handles that could be used with this
791 * table, because it *is* being used with this table.
793 dissector_add_handle(name, handle);
796 /* Delete the entry for a dissector in a uint dissector table
797 with a particular pattern. */
799 /* NOTE: this doesn't use the dissector call variable. It is included to */
800 /* be consistant with the dissector_add_uint and more importantly to be used */
801 /* if the technique of adding a temporary dissector is implemented. */
802 /* If temporary dissectors are deleted, then the original dissector must */
805 dissector_delete_uint(const char *name, const guint32 pattern,
806 dissector_handle_t handle _U_)
808 dissector_table_t sub_dissectors = find_dissector_table( name);
809 dtbl_entry_t *dtbl_entry;
812 g_assert( sub_dissectors);
817 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
819 if (dtbl_entry != NULL) {
823 g_hash_table_remove(sub_dissectors->hash_table,
824 GUINT_TO_POINTER(pattern));
828 /* Change the entry for a dissector in a uint dissector table
829 with a particular pattern to use a new dissector handle. */
831 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
833 dissector_table_t sub_dissectors = find_dissector_table( name);
834 dtbl_entry_t *dtbl_entry;
837 g_assert( sub_dissectors);
840 * See if the entry already exists. If so, reuse it.
842 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
843 if (dtbl_entry != NULL) {
844 dtbl_entry->current = handle;
849 * Don't create an entry if there is no dissector handle - I.E. the
850 * user said not to decode something that wasn't being decoded
851 * in the first place.
856 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
857 dtbl_entry->initial = NULL;
858 dtbl_entry->current = handle;
860 /* do the table insertion */
861 g_hash_table_insert( sub_dissectors->hash_table,
862 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
865 /* Reset an entry in a uint dissector table to its initial value. */
867 dissector_reset_uint(const char *name, const guint32 pattern)
869 dissector_table_t sub_dissectors = find_dissector_table( name);
870 dtbl_entry_t *dtbl_entry;
873 g_assert( sub_dissectors);
878 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
880 if (dtbl_entry == NULL)
884 * Found - is there an initial value?
886 if (dtbl_entry->initial != NULL) {
887 dtbl_entry->current = dtbl_entry->initial;
889 g_hash_table_remove(sub_dissectors->hash_table,
890 GUINT_TO_POINTER(pattern));
894 /* Look for a given value in a given uint dissector table and, if found,
895 call the dissector with the arguments supplied, and return TRUE,
896 otherwise return FALSE. */
899 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
900 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
901 const gboolean add_proto_name, void *data)
903 dtbl_entry_t *dtbl_entry;
904 struct dissector_handle *handle;
905 guint32 saved_match_uint;
908 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
909 if (dtbl_entry != NULL) {
911 * Is there currently a dissector handle for this entry?
913 handle = dtbl_entry->current;
914 if (handle == NULL) {
916 * No - pretend this dissector didn't exist,
917 * so that other dissectors might have a chance
918 * to dissect this packet.
924 * Save the current value of "pinfo->match_uint",
925 * set it to the uint_val that matched, call the
926 * dissector, and restore "pinfo->match_uint".
928 saved_match_uint = pinfo->match_uint;
929 pinfo->match_uint = uint_val;
930 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, data);
931 pinfo->match_uint = saved_match_uint;
934 * If a new-style dissector returned 0, it means that
935 * it didn't think this tvbuff represented a packet for
936 * its protocol, and didn't dissect anything.
938 * Old-style dissectors can't reject the packet.
940 * 0 is also returned if the protocol wasn't enabled.
942 * If the packet was rejected, we return FALSE, so that
943 * other dissectors might have a chance to dissect this
944 * packet, otherwise we return TRUE.
952 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
953 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
956 return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE, NULL);
959 /* Look for a given value in a given uint dissector table and, if found,
960 return the dissector handle for that value. */
962 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
964 dtbl_entry_t *dtbl_entry;
966 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
967 if (dtbl_entry != NULL)
968 return dtbl_entry->current;
973 /* Find an entry in a string dissector table. */
974 static dtbl_entry_t *
975 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
977 switch (sub_dissectors->type) {
982 * You can do a string lookup in these tables.
988 * But you can't do a string lookup in any other types
991 g_assert_not_reached();
997 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
1000 /* Add an entry to a string dissector table. */
1002 dissector_add_string(const char *name, const gchar *pattern,
1003 dissector_handle_t handle)
1005 dissector_table_t sub_dissectors = find_dissector_table( name);
1006 dtbl_entry_t *dtbl_entry;
1009 * Make sure the dissector table exists.
1011 if (sub_dissectors == NULL) {
1012 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1014 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1015 proto_get_protocol_long_name(handle->protocol));
1016 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1022 g_assert(handle!=NULL);
1023 switch (sub_dissectors->type) {
1028 * You can do a string lookup in these tables.
1034 * But you can't do a string lookup in any other types
1037 g_assert_not_reached();
1040 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1041 dtbl_entry->current = handle;
1042 dtbl_entry->initial = dtbl_entry->current;
1044 /* do the table insertion */
1045 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1046 (gpointer)dtbl_entry);
1049 * Now add it to the list of handles that could be used with this
1050 * table, because it *is* being used with this table.
1052 dissector_add_handle(name, handle);
1055 /* Delete the entry for a dissector in a string dissector table
1056 with a particular pattern. */
1058 /* NOTE: this doesn't use the dissector call variable. It is included to */
1059 /* be consistant with the dissector_add_string and more importantly to */
1060 /* be used if the technique of adding a temporary dissector is */
1062 /* If temporary dissectors are deleted, then the original dissector must */
1065 dissector_delete_string(const char *name, const gchar *pattern,
1066 dissector_handle_t handle _U_)
1068 dissector_table_t sub_dissectors = find_dissector_table( name);
1069 dtbl_entry_t *dtbl_entry;
1072 g_assert( sub_dissectors);
1077 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1079 if (dtbl_entry != NULL) {
1081 * Found - remove it.
1083 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1087 /* Change the entry for a dissector in a string dissector table
1088 with a particular pattern to use a new dissector handle. */
1090 dissector_change_string(const char *name, const gchar *pattern,
1091 dissector_handle_t handle)
1093 dissector_table_t sub_dissectors = find_dissector_table( name);
1094 dtbl_entry_t *dtbl_entry;
1097 g_assert( sub_dissectors);
1100 * See if the entry already exists. If so, reuse it.
1102 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1103 if (dtbl_entry != NULL) {
1104 dtbl_entry->current = handle;
1109 * Don't create an entry if there is no dissector handle - I.E. the
1110 * user said not to decode something that wasn't being decoded
1111 * in the first place.
1116 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1117 dtbl_entry->initial = NULL;
1118 dtbl_entry->current = handle;
1120 /* do the table insertion */
1121 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1122 (gpointer)dtbl_entry);
1125 /* Reset an entry in a string sub-dissector table to its initial value. */
1127 dissector_reset_string(const char *name, const gchar *pattern)
1129 dissector_table_t sub_dissectors = find_dissector_table( name);
1130 dtbl_entry_t *dtbl_entry;
1133 g_assert( sub_dissectors);
1138 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1140 if (dtbl_entry == NULL)
1144 * Found - is there an initial value?
1146 if (dtbl_entry->initial != NULL) {
1147 dtbl_entry->current = dtbl_entry->initial;
1149 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1153 /* Look for a given string in a given dissector table and, if found, call
1154 the dissector with the arguments supplied, and return TRUE, otherwise
1157 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1158 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1160 dtbl_entry_t *dtbl_entry;
1161 struct dissector_handle *handle;
1163 const gchar *saved_match_string;
1165 /* XXX ASSERT instead ? */
1166 if (!string) return FALSE;
1167 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1168 if (dtbl_entry != NULL) {
1170 * Is there currently a dissector handle for this entry?
1172 handle = dtbl_entry->current;
1173 if (handle == NULL) {
1175 * No - pretend this dissector didn't exist,
1176 * so that other dissectors might have a chance
1177 * to dissect this packet.
1183 * Save the current value of "pinfo->match_string",
1184 * set it to the string that matched, call the
1185 * dissector, and restore "pinfo->match_string".
1187 saved_match_string = pinfo->match_string;
1188 pinfo->match_string = string;
1189 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, NULL);
1190 pinfo->match_string = saved_match_string;
1193 * If a new-style dissector returned 0, it means that
1194 * it didn't think this tvbuff represented a packet for
1195 * its protocol, and didn't dissect anything.
1197 * Old-style dissectors can't reject the packet.
1199 * 0 is also returned if the protocol wasn't enabled.
1201 * If the packet was rejected, we return FALSE, so that
1202 * other dissectors might have a chance to dissect this
1203 * packet, otherwise we return TRUE.
1210 /* Look for a given value in a given string dissector table and, if found,
1211 return the dissector handle for that value. */
1213 dissector_get_string_handle(dissector_table_t sub_dissectors,
1214 const gchar *string)
1216 dtbl_entry_t *dtbl_entry;
1218 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1219 if (dtbl_entry != NULL)
1220 return dtbl_entry->current;
1226 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1228 return dtbl_entry->current;
1232 dissector_compare_filter_name(gconstpointer dissector_a, gconstpointer dissector_b)
1234 dissector_handle_t a = (dissector_handle_t)dissector_a;
1235 dissector_handle_t b = (dissector_handle_t)dissector_b;
1236 const char *a_name, *b_name;
1239 if (a->protocol == NULL)
1242 a_name = proto_get_protocol_filter_name(proto_get_id(a->protocol));
1244 if (b->protocol == NULL)
1247 b_name = proto_get_protocol_filter_name(proto_get_id(b->protocol));
1249 ret = strcmp(a_name, b_name);
1253 /* Add a handle to the list of handles that *could* be used with this
1254 table. That list is used by code in the UI. */
1256 dissector_add_handle(const char *name, dissector_handle_t handle)
1258 dissector_table_t sub_dissectors = find_dissector_table( name);
1262 * Make sure the dissector table exists.
1264 if (sub_dissectors == NULL) {
1265 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1267 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1268 proto_get_protocol_long_name(handle->protocol));
1269 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1274 /* Is it already in this list? */
1275 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1276 if (entry != NULL) {
1278 * Yes - don't insert it again.
1283 /* Add it to the list. */
1284 sub_dissectors->dissector_handles =
1285 g_slist_insert_sorted(sub_dissectors->dissector_handles, (gpointer)handle, (GCompareFunc)dissector_compare_filter_name);
1289 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1291 return dtbl_entry->initial;
1294 /**************************************************/
1296 /* Routines to walk dissector tables */
1298 /**************************************************/
1300 typedef struct dissector_foreach_info {
1301 gpointer caller_data;
1302 DATFunc caller_func;
1304 const gchar *table_name;
1305 ftenum_t selector_type;
1306 } dissector_foreach_info_t;
1309 * Called for each entry in a dissector table.
1312 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1314 dissector_foreach_info_t *info;
1315 dtbl_entry_t *dtbl_entry;
1318 g_assert(user_data);
1321 if (dtbl_entry->current == NULL ||
1322 dtbl_entry->current->protocol == NULL) {
1324 * Either there is no dissector for this entry, or
1325 * the dissector doesn't have a protocol associated
1328 * XXX - should the latter check be done?
1334 info->caller_func(info->table_name, info->selector_type, key, value,
1339 * Called for each entry in the table of all dissector tables.
1342 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1344 dissector_table_t sub_dissectors;
1345 dissector_foreach_info_t *info;
1348 g_assert(user_data);
1350 sub_dissectors = value;
1352 info->table_name = (gchar*) key;
1353 info->selector_type = get_dissector_table_selector_type(info->table_name);
1354 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1358 * Walk all dissector tables calling a user supplied function on each
1362 dissector_all_tables_foreach (DATFunc func,
1365 dissector_foreach_info_t info;
1367 info.caller_data = user_data;
1368 info.caller_func = func;
1369 info.next_func = dissector_table_foreach_func;
1370 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1374 * Walk one dissector table's hash table calling a user supplied function
1378 dissector_table_foreach (const char *name,
1382 dissector_foreach_info_t info;
1383 dissector_table_t sub_dissectors = find_dissector_table( name);
1385 info.table_name = name;
1386 info.selector_type = sub_dissectors->type;
1387 info.caller_func = func;
1388 info.caller_data = user_data;
1389 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1393 * Walk one dissector table's list of handles calling a user supplied
1394 * function on each entry.
1397 dissector_table_foreach_handle(const char *name,
1398 DATFunc_handle func,
1401 dissector_table_t sub_dissectors = find_dissector_table( name);
1404 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1405 tmp = g_slist_next(tmp))
1406 func(name, tmp->data, user_data);
1410 * Called for each entry in a dissector table.
1413 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1415 dtbl_entry_t *dtbl_entry;
1416 dissector_foreach_info_t *info;
1419 g_assert(user_data);
1422 if (dtbl_entry->initial == dtbl_entry->current) {
1424 * Entry hasn't changed - don't call the function.
1430 info->caller_func(info->table_name, info->selector_type, key, value,
1435 * Walk all dissector tables calling a user supplied function only on
1436 * any entry that has been changed from its original state.
1439 dissector_all_tables_foreach_changed (DATFunc func,
1442 dissector_foreach_info_t info;
1444 info.caller_data = user_data;
1445 info.caller_func = func;
1446 info.next_func = dissector_table_foreach_changed_func;
1447 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1451 * Walk one dissector table calling a user supplied function only on
1452 * any entry that has been changed from its original state.
1455 dissector_table_foreach_changed (const char *name,
1459 dissector_foreach_info_t info;
1460 dissector_table_t sub_dissectors = find_dissector_table( name);
1462 info.table_name = name;
1463 info.selector_type = sub_dissectors->type;
1464 info.caller_func = func;
1465 info.caller_data = user_data;
1466 g_hash_table_foreach(sub_dissectors->hash_table,
1467 dissector_table_foreach_changed_func, &info);
1470 typedef struct dissector_foreach_table_info {
1471 gpointer caller_data;
1472 DATFunc_table caller_func;
1473 } dissector_foreach_table_info_t;
1476 * Called for each entry in the table of all dissector tables.
1479 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1481 dissector_table_t table;
1482 dissector_foreach_table_info_t *info;
1486 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1490 * Called for each key in the table of all dissector tables.
1493 dissector_all_tables_foreach_list_func (gpointer key, gpointer user_data)
1495 dissector_table_t table;
1496 dissector_foreach_table_info_t *info;
1498 table = g_hash_table_lookup( dissector_tables, key );
1500 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1504 * Walk all dissector tables calling a user supplied function on each
1508 dissector_all_tables_foreach_table (DATFunc_table func,
1510 GCompareFunc compare_key_func)
1512 dissector_foreach_table_info_t info;
1515 info.caller_data = user_data;
1516 info.caller_func = func;
1517 if (compare_key_func != NULL)
1519 list = g_hash_table_get_keys(dissector_tables);
1520 list = g_list_sort(list, compare_key_func);
1521 g_list_foreach(list, dissector_all_tables_foreach_list_func, &info);
1526 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1531 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1534 dissector_table_t sub_dissectors;
1536 /* Create our hash-of-hashes if it doesn't already exist */
1537 if (!dissector_tables) {
1538 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1539 g_assert(dissector_tables);
1542 /* Make sure the registration is unique */
1543 if(g_hash_table_lookup( dissector_tables, name )) {
1544 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1547 /* Create and register the dissector table for this name; returns */
1548 /* a pointer to the dissector table. */
1549 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1557 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1558 * so we use "g_direct_hash()" and "g_direct_equal()".
1560 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash,
1568 sub_dissectors->hash_table = g_hash_table_new_full( g_str_hash,
1575 g_assert_not_reached();
1577 sub_dissectors->dissector_handles = NULL;
1578 sub_dissectors->ui_name = ui_name;
1579 sub_dissectors->type = type;
1580 sub_dissectors->base = base;
1581 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1582 return sub_dissectors;
1586 get_dissector_table_ui_name(const char *name)
1588 dissector_table_t sub_dissectors = find_dissector_table( name);
1590 return sub_dissectors->ui_name;
1594 get_dissector_table_selector_type(const char *name)
1596 dissector_table_t sub_dissectors = find_dissector_table( name);
1598 return sub_dissectors->type;
1602 get_dissector_table_base(const char *name)
1604 dissector_table_t sub_dissectors = find_dissector_table( name);
1606 return sub_dissectors->base;
1609 static GHashTable *heur_dissector_lists = NULL;
1612 /* Finds a heuristic dissector table by table name. */
1613 static heur_dissector_list_t *
1614 find_heur_dissector_list(const char *name)
1616 g_assert(heur_dissector_lists != NULL);
1617 return g_hash_table_lookup(heur_dissector_lists, name);
1621 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1623 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1624 const char *proto_name;
1625 heur_dtbl_entry_t *hdtbl_entry;
1628 * Make sure the dissector table exists.
1630 if (sub_dissectors == NULL) {
1631 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1633 proto_name = proto_get_protocol_name(proto);
1634 if (proto_name != NULL) {
1635 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1638 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1643 /* XXX: Should verify that sub-dissector is not already in the list ? */
1645 hdtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1646 hdtbl_entry->dissector = dissector;
1647 hdtbl_entry->protocol = find_protocol_by_id(proto);
1648 hdtbl_entry->enabled = TRUE;
1650 /* do the table insertion */
1651 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)hdtbl_entry);
1657 find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1658 const heur_dtbl_entry_t *hdtbl_entry_a = (const heur_dtbl_entry_t *) a;
1659 const heur_dtbl_entry_t *hdtbl_entry_b = (const heur_dtbl_entry_t *) b;
1660 return (hdtbl_entry_a->dissector == hdtbl_entry_b->dissector) &&
1661 (hdtbl_entry_a->protocol == hdtbl_entry_b->protocol) ? 0 : 1;
1665 heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1666 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1667 heur_dtbl_entry_t hdtbl_entry;
1668 GSList *found_entry;
1671 g_assert(sub_dissectors != NULL);
1673 hdtbl_entry.dissector = dissector;
1675 hdtbl_entry.protocol = find_protocol_by_id(proto);
1677 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1680 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1681 g_free(g_slist_nth_data(found_entry, 1));
1682 g_slist_free_1(found_entry);
1687 heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled) {
1688 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1689 GSList *found_entry;
1690 heur_dtbl_entry_t hdtbl_entry;
1693 g_assert(sub_dissectors != NULL);
1695 hdtbl_entry.dissector = dissector;
1697 hdtbl_entry.protocol = find_protocol_by_id(proto);
1699 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1702 heur_dtbl_entry_t *hdtbl_entry_p;
1703 hdtbl_entry_p = (heur_dtbl_entry_t *)found_entry->data;
1704 hdtbl_entry_p->enabled = enabled;
1709 dissector_try_heuristic(heur_dissector_list_t sub_dissectors, tvbuff_t *tvb,
1710 packet_info *pinfo, proto_tree *tree, void *data)
1713 const char *saved_proto;
1715 heur_dtbl_entry_t *hdtbl_entry;
1716 guint16 saved_can_desegment;
1717 gint saved_layer_names_len = 0;
1719 /* can_desegment is set to 2 by anyone which offers this api/service.
1720 then everytime a subdissector is called it is decremented by one.
1721 thus only the subdissector immediately ontop of whoever offers this
1723 We save the current value of "can_desegment" for the
1724 benefit of TCP proxying dissectors such as SOCKS, so they
1725 can restore it and allow the dissectors they call to use
1726 the desegmentation service.
1728 saved_can_desegment = pinfo->can_desegment;
1729 pinfo->saved_can_desegment = saved_can_desegment;
1730 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1733 saved_proto = pinfo->current_proto;
1735 if (pinfo->layer_names != NULL)
1736 saved_layer_names_len = (gint) pinfo->layer_names->len;
1738 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1739 /* XXX - why set this now and above? */
1740 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1741 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1743 if (hdtbl_entry->protocol != NULL &&
1744 (!proto_is_protocol_enabled(hdtbl_entry->protocol)||(hdtbl_entry->enabled==FALSE))) {
1746 * No - don't try this dissector.
1751 if (hdtbl_entry->protocol != NULL) {
1752 pinfo->current_proto =
1753 proto_get_protocol_short_name(hdtbl_entry->protocol);
1756 * Add the protocol name to the layers; we'll remove it
1757 * if the dissector fails.
1759 if (pinfo->layer_names) {
1760 if (pinfo->layer_names->len > 0)
1761 g_string_append(pinfo->layer_names, ":");
1762 g_string_append(pinfo->layer_names,
1763 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)));
1766 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1767 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1768 if ((*hdtbl_entry->dissector)(tvb, pinfo, tree, data)) {
1769 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1770 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1774 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1775 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1778 * That dissector didn't accept the packet, so
1779 * remove its protocol's name from the list
1782 if (pinfo->layer_names != NULL) {
1783 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1787 pinfo->current_proto = saved_proto;
1788 pinfo->can_desegment=saved_can_desegment;
1793 * Called for each entry in the table of all heuristic dissector tables.
1795 typedef struct heur_dissector_foreach_table_info {
1796 gpointer caller_data;
1797 DATFunc_heur_table caller_func;
1798 } heur_dissector_foreach_table_info_t;
1802 dissector_dump_heur_decodes_display(const gchar *table_name, const gpointer value, const gpointer user_data _U_)
1804 heur_dissector_list_t sub_dissectors = *(heur_dissector_list_t *)value;
1806 heur_dtbl_entry_t *hdtbl_entry;
1808 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1809 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1810 if (hdtbl_entry->protocol != NULL) {
1811 printf("%s\t%s\t%c\n",
1813 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)),
1814 (proto_is_protocol_enabled(hdtbl_entry->protocol) && hdtbl_entry->enabled) ? 'T' : 'F');
1821 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1823 heur_dissector_foreach_table_info_t *info;
1826 (*info->caller_func)((gchar*)key, value, info->caller_data);
1830 * Walk all heuristic dissector tables calling a user supplied function on each
1834 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1837 heur_dissector_foreach_table_info_t info;
1839 info.caller_data = user_data;
1840 info.caller_func = func;
1841 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1845 * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
1848 dissector_dump_heur_decodes(void)
1850 dissector_all_heur_tables_foreach_table(dissector_dump_heur_decodes_display, NULL);
1855 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1857 /* Create our hash-of-lists if it doesn't already exist */
1858 if (heur_dissector_lists == NULL) {
1859 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1860 g_assert(heur_dissector_lists != NULL);
1863 /* Make sure the registration is unique */
1864 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1866 *sub_dissectors = NULL; /* initially empty */
1867 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1868 (gpointer) sub_dissectors);
1872 * Register dissectors by name; used if one dissector always calls a
1873 * particular dissector, or if it bases the decision of which dissector
1874 * to call on something other than a numerical value or on "try a bunch
1875 * of dissectors until one likes the packet".
1879 * List of registered dissectors.
1881 static GHashTable *registered_dissectors = NULL;
1883 /* Get the long name of the protocol for a dissector handle, if it has
1886 dissector_handle_get_long_name(const dissector_handle_t handle)
1888 if (handle == NULL || handle->protocol == NULL) {
1891 return proto_get_protocol_long_name(handle->protocol);
1894 /* Get the short name of the protocol for a dissector handle, if it has
1897 dissector_handle_get_short_name(const dissector_handle_t handle)
1899 if (handle->protocol == NULL) {
1901 * No protocol (see, for example, the handle for
1902 * dissecting the set of protocols where the first
1903 * octet of the payload is an OSI network layer protocol
1908 return proto_get_protocol_short_name(handle->protocol);
1911 /* Get the index of the protocol for a dissector handle, if it has
1914 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1916 if (handle->protocol == NULL) {
1918 * No protocol (see, for example, the handle for
1919 * dissecting the set of protocols where the first
1920 * octet of the payload is an OSI network layer protocol
1925 return proto_get_id(handle->protocol);
1928 /* Find a registered dissector by name. */
1930 find_dissector(const char *name)
1932 g_assert(registered_dissectors != NULL);
1933 return g_hash_table_lookup(registered_dissectors, name);
1936 /* Create an anonymous handle for a dissector. */
1938 create_dissector_handle(dissector_t dissector, const int proto)
1940 struct dissector_handle *handle;
1942 handle = g_malloc(sizeof (struct dissector_handle));
1943 handle->name = NULL;
1944 handle->is_new = FALSE;
1945 handle->dissector.old = dissector;
1946 handle->protocol = find_protocol_by_id(proto);
1952 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1954 struct dissector_handle *handle;
1956 handle = g_malloc(sizeof (struct dissector_handle));
1957 handle->name = NULL;
1958 handle->is_new = TRUE;
1959 handle->dissector.new = dissector;
1960 handle->protocol = find_protocol_by_id(proto);
1965 /* Register a dissector by name. */
1967 register_dissector(const char *name, dissector_t dissector, const int proto)
1969 struct dissector_handle *handle;
1971 /* Create our hash table if it doesn't already exist */
1972 if (registered_dissectors == NULL) {
1973 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1974 g_assert(registered_dissectors != NULL);
1977 /* Make sure the registration is unique */
1978 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1980 handle = g_malloc(sizeof (struct dissector_handle));
1981 handle->name = name;
1982 handle->is_new = FALSE;
1983 handle->dissector.old = dissector;
1984 handle->protocol = find_protocol_by_id(proto);
1986 g_hash_table_insert(registered_dissectors, (gpointer)name,
1991 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
1993 struct dissector_handle *handle;
1995 /* Create our hash table if it doesn't already exist */
1996 if (registered_dissectors == NULL) {
1997 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1998 g_assert(registered_dissectors != NULL);
2001 /* Make sure the registration is unique */
2002 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
2004 handle = g_malloc(sizeof (struct dissector_handle));
2005 handle->name = name;
2006 handle->is_new = TRUE;
2007 handle->dissector.new = dissector;
2008 handle->protocol = find_protocol_by_id(proto);
2010 g_hash_table_insert(registered_dissectors, (gpointer)name,
2014 /* Call a dissector through a handle but if the dissector rejected it
2018 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
2019 packet_info *pinfo, proto_tree *tree, void *data)
2023 g_assert(handle != NULL);
2024 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
2028 /* Call a dissector through a handle and if this fails call the "data"
2032 call_dissector_with_data(dissector_handle_t handle, tvbuff_t *tvb,
2033 packet_info *pinfo, proto_tree *tree, void *data)
2037 ret = call_dissector_only(handle, tvb, pinfo, tree, data);
2040 * The protocol was disabled, or the dissector rejected
2041 * it. Just dissect this packet as data.
2043 g_assert(data_handle != NULL);
2044 g_assert(data_handle->protocol != NULL);
2045 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2046 return tvb_length(tvb);
2052 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
2053 packet_info *pinfo, proto_tree *tree)
2055 return call_dissector_with_data(handle, tvb, pinfo, tree, NULL);
2059 * Dumps the "layer type"/"decode as" associations to stdout, similar
2060 * to the proto_registrar_dump_*() routines.
2062 * There is one record per line. The fields are tab-delimited.
2064 * Field 1 = layer type, e.g. "tcp.port"
2065 * Field 2 = selector in decimal
2066 * Field 3 = "decode as" name, e.g. "http"
2071 dissector_dump_decodes_display(const gchar *table_name,
2072 ftenum_t selector_type _U_, const gpointer key, const gpointer value,
2073 gpointer user_data _U_)
2075 guint32 selector = (guint32)(unsigned long) key;
2076 dissector_table_t sub_dissectors = find_dissector_table(table_name);
2077 dtbl_entry_t *dtbl_entry;
2078 dissector_handle_t handle;
2080 const gchar *decode_as;
2082 g_assert(sub_dissectors);
2083 switch (sub_dissectors->type) {
2090 g_assert(dtbl_entry);
2092 handle = dtbl_entry->current;
2095 proto_id = dissector_handle_get_protocol_index(handle);
2097 if (proto_id != -1) {
2098 decode_as = proto_get_protocol_filter_name(proto_id);
2099 g_assert(decode_as != NULL);
2100 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
2110 dissector_dump_decodes(void)
2112 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2115 static GPtrArray* post_dissectors = NULL;
2116 static guint num_of_postdissectors = 0;
2119 register_postdissector(dissector_handle_t handle)
2121 if (!post_dissectors)
2122 post_dissectors = g_ptr_array_new();
2124 g_ptr_array_add(post_dissectors, handle);
2125 num_of_postdissectors++;
2129 have_postdissector(void)
2132 dissector_handle_t handle;
2134 for(i = 0; i < num_of_postdissectors; i++) {
2135 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
2137 if (handle->protocol != NULL
2138 && proto_is_protocol_enabled(handle->protocol)) {
2139 /* We have at least one enabled postdissector */
2147 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2151 for(i = 0; i < num_of_postdissectors; i++) {
2152 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
2153 tvb,pinfo,tree, NULL);
2158 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2163 * indent-tabs-mode: t
2166 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2167 * :indentSize=8:tabSize=8:noTabs=false: