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, const gboolean add_proto_name)
902 dtbl_entry_t *dtbl_entry;
903 struct dissector_handle *handle;
904 guint32 saved_match_uint;
907 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
908 if (dtbl_entry != NULL) {
910 * Is there currently a dissector handle for this entry?
912 handle = dtbl_entry->current;
913 if (handle == NULL) {
915 * No - pretend this dissector didn't exist,
916 * so that other dissectors might have a chance
917 * to dissect this packet.
923 * Save the current value of "pinfo->match_uint",
924 * set it to the uint_val that matched, call the
925 * dissector, and restore "pinfo->match_uint".
927 saved_match_uint = pinfo->match_uint;
928 pinfo->match_uint = uint_val;
929 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, NULL);
930 pinfo->match_uint = saved_match_uint;
933 * If a new-style dissector returned 0, it means that
934 * it didn't think this tvbuff represented a packet for
935 * its protocol, and didn't dissect anything.
937 * Old-style dissectors can't reject the packet.
939 * 0 is also returned if the protocol wasn't enabled.
941 * If the packet was rejected, we return FALSE, so that
942 * other dissectors might have a chance to dissect this
943 * packet, otherwise we return TRUE.
951 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
952 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
955 return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE);
958 /* Look for a given value in a given uint dissector table and, if found,
959 return the dissector handle for that value. */
961 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
963 dtbl_entry_t *dtbl_entry;
965 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
966 if (dtbl_entry != NULL)
967 return dtbl_entry->current;
972 /* Find an entry in a string dissector table. */
973 static dtbl_entry_t *
974 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
976 switch (sub_dissectors->type) {
981 * You can do a string lookup in these tables.
987 * But you can't do a string lookup in any other types
990 g_assert_not_reached();
996 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
999 /* Add an entry to a string dissector table. */
1001 dissector_add_string(const char *name, const gchar *pattern,
1002 dissector_handle_t handle)
1004 dissector_table_t sub_dissectors = find_dissector_table( name);
1005 dtbl_entry_t *dtbl_entry;
1008 * Make sure the dissector table exists.
1010 if (sub_dissectors == NULL) {
1011 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1013 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1014 proto_get_protocol_long_name(handle->protocol));
1015 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1021 g_assert(handle!=NULL);
1022 switch (sub_dissectors->type) {
1027 * You can do a string lookup in these tables.
1033 * But you can't do a string lookup in any other types
1036 g_assert_not_reached();
1039 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1040 dtbl_entry->current = handle;
1041 dtbl_entry->initial = dtbl_entry->current;
1043 /* do the table insertion */
1044 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1045 (gpointer)dtbl_entry);
1048 * Now add it to the list of handles that could be used with this
1049 * table, because it *is* being used with this table.
1051 dissector_add_handle(name, handle);
1054 /* Delete the entry for a dissector in a string dissector table
1055 with a particular pattern. */
1057 /* NOTE: this doesn't use the dissector call variable. It is included to */
1058 /* be consistant with the dissector_add_string and more importantly to */
1059 /* be used if the technique of adding a temporary dissector is */
1061 /* If temporary dissectors are deleted, then the original dissector must */
1064 dissector_delete_string(const char *name, const gchar *pattern,
1065 dissector_handle_t handle _U_)
1067 dissector_table_t sub_dissectors = find_dissector_table( name);
1068 dtbl_entry_t *dtbl_entry;
1071 g_assert( sub_dissectors);
1076 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1078 if (dtbl_entry != NULL) {
1080 * Found - remove it.
1082 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1086 /* Change the entry for a dissector in a string dissector table
1087 with a particular pattern to use a new dissector handle. */
1089 dissector_change_string(const char *name, const gchar *pattern,
1090 dissector_handle_t handle)
1092 dissector_table_t sub_dissectors = find_dissector_table( name);
1093 dtbl_entry_t *dtbl_entry;
1096 g_assert( sub_dissectors);
1099 * See if the entry already exists. If so, reuse it.
1101 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1102 if (dtbl_entry != NULL) {
1103 dtbl_entry->current = handle;
1108 * Don't create an entry if there is no dissector handle - I.E. the
1109 * user said not to decode something that wasn't being decoded
1110 * in the first place.
1115 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1116 dtbl_entry->initial = NULL;
1117 dtbl_entry->current = handle;
1119 /* do the table insertion */
1120 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1121 (gpointer)dtbl_entry);
1124 /* Reset an entry in a string sub-dissector table to its initial value. */
1126 dissector_reset_string(const char *name, const gchar *pattern)
1128 dissector_table_t sub_dissectors = find_dissector_table( name);
1129 dtbl_entry_t *dtbl_entry;
1132 g_assert( sub_dissectors);
1137 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1139 if (dtbl_entry == NULL)
1143 * Found - is there an initial value?
1145 if (dtbl_entry->initial != NULL) {
1146 dtbl_entry->current = dtbl_entry->initial;
1148 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1152 /* Look for a given string in a given dissector table and, if found, call
1153 the dissector with the arguments supplied, and return TRUE, otherwise
1156 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1157 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1159 dtbl_entry_t *dtbl_entry;
1160 struct dissector_handle *handle;
1162 const gchar *saved_match_string;
1164 /* XXX ASSERT instead ? */
1165 if (!string) return FALSE;
1166 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1167 if (dtbl_entry != NULL) {
1169 * Is there currently a dissector handle for this entry?
1171 handle = dtbl_entry->current;
1172 if (handle == NULL) {
1174 * No - pretend this dissector didn't exist,
1175 * so that other dissectors might have a chance
1176 * to dissect this packet.
1182 * Save the current value of "pinfo->match_string",
1183 * set it to the string that matched, call the
1184 * dissector, and restore "pinfo->match_string".
1186 saved_match_string = pinfo->match_string;
1187 pinfo->match_string = string;
1188 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, NULL);
1189 pinfo->match_string = saved_match_string;
1192 * If a new-style dissector returned 0, it means that
1193 * it didn't think this tvbuff represented a packet for
1194 * its protocol, and didn't dissect anything.
1196 * Old-style dissectors can't reject the packet.
1198 * 0 is also returned if the protocol wasn't enabled.
1200 * If the packet was rejected, we return FALSE, so that
1201 * other dissectors might have a chance to dissect this
1202 * packet, otherwise we return TRUE.
1209 /* Look for a given value in a given string dissector table and, if found,
1210 return the dissector handle for that value. */
1212 dissector_get_string_handle(dissector_table_t sub_dissectors,
1213 const gchar *string)
1215 dtbl_entry_t *dtbl_entry;
1217 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1218 if (dtbl_entry != NULL)
1219 return dtbl_entry->current;
1225 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1227 return dtbl_entry->current;
1231 dissector_compare_filter_name(gconstpointer dissector_a, gconstpointer dissector_b)
1233 dissector_handle_t a = (dissector_handle_t)dissector_a;
1234 dissector_handle_t b = (dissector_handle_t)dissector_b;
1235 const char *a_name, *b_name;
1238 if (a->protocol == NULL)
1241 a_name = proto_get_protocol_filter_name(proto_get_id(a->protocol));
1243 if (b->protocol == NULL)
1246 b_name = proto_get_protocol_filter_name(proto_get_id(b->protocol));
1248 ret = strcmp(a_name, b_name);
1252 /* Add a handle to the list of handles that *could* be used with this
1253 table. That list is used by code in the UI. */
1255 dissector_add_handle(const char *name, dissector_handle_t handle)
1257 dissector_table_t sub_dissectors = find_dissector_table( name);
1261 * Make sure the dissector table exists.
1263 if (sub_dissectors == NULL) {
1264 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1266 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1267 proto_get_protocol_long_name(handle->protocol));
1268 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1273 /* Is it already in this list? */
1274 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1275 if (entry != NULL) {
1277 * Yes - don't insert it again.
1282 /* Add it to the list. */
1283 sub_dissectors->dissector_handles =
1284 g_slist_insert_sorted(sub_dissectors->dissector_handles, (gpointer)handle, (GCompareFunc)dissector_compare_filter_name);
1288 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1290 return dtbl_entry->initial;
1293 /**************************************************/
1295 /* Routines to walk dissector tables */
1297 /**************************************************/
1299 typedef struct dissector_foreach_info {
1300 gpointer caller_data;
1301 DATFunc caller_func;
1303 const gchar *table_name;
1304 ftenum_t selector_type;
1305 } dissector_foreach_info_t;
1308 * Called for each entry in a dissector table.
1311 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1313 dissector_foreach_info_t *info;
1314 dtbl_entry_t *dtbl_entry;
1317 g_assert(user_data);
1320 if (dtbl_entry->current == NULL ||
1321 dtbl_entry->current->protocol == NULL) {
1323 * Either there is no dissector for this entry, or
1324 * the dissector doesn't have a protocol associated
1327 * XXX - should the latter check be done?
1333 info->caller_func(info->table_name, info->selector_type, key, value,
1338 * Called for each entry in the table of all dissector tables.
1341 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1343 dissector_table_t sub_dissectors;
1344 dissector_foreach_info_t *info;
1347 g_assert(user_data);
1349 sub_dissectors = value;
1351 info->table_name = (gchar*) key;
1352 info->selector_type = get_dissector_table_selector_type(info->table_name);
1353 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1357 * Walk all dissector tables calling a user supplied function on each
1361 dissector_all_tables_foreach (DATFunc func,
1364 dissector_foreach_info_t info;
1366 info.caller_data = user_data;
1367 info.caller_func = func;
1368 info.next_func = dissector_table_foreach_func;
1369 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1373 * Walk one dissector table's hash table calling a user supplied function
1377 dissector_table_foreach (const char *name,
1381 dissector_foreach_info_t info;
1382 dissector_table_t sub_dissectors = find_dissector_table( name);
1384 info.table_name = name;
1385 info.selector_type = sub_dissectors->type;
1386 info.caller_func = func;
1387 info.caller_data = user_data;
1388 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1392 * Walk one dissector table's list of handles calling a user supplied
1393 * function on each entry.
1396 dissector_table_foreach_handle(const char *name,
1397 DATFunc_handle func,
1400 dissector_table_t sub_dissectors = find_dissector_table( name);
1403 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1404 tmp = g_slist_next(tmp))
1405 func(name, tmp->data, user_data);
1409 * Called for each entry in a dissector table.
1412 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1414 dtbl_entry_t *dtbl_entry;
1415 dissector_foreach_info_t *info;
1418 g_assert(user_data);
1421 if (dtbl_entry->initial == dtbl_entry->current) {
1423 * Entry hasn't changed - don't call the function.
1429 info->caller_func(info->table_name, info->selector_type, key, value,
1434 * Walk all dissector tables calling a user supplied function only on
1435 * any entry that has been changed from its original state.
1438 dissector_all_tables_foreach_changed (DATFunc func,
1441 dissector_foreach_info_t info;
1443 info.caller_data = user_data;
1444 info.caller_func = func;
1445 info.next_func = dissector_table_foreach_changed_func;
1446 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1450 * Walk one dissector table calling a user supplied function only on
1451 * any entry that has been changed from its original state.
1454 dissector_table_foreach_changed (const char *name,
1458 dissector_foreach_info_t info;
1459 dissector_table_t sub_dissectors = find_dissector_table( name);
1461 info.table_name = name;
1462 info.selector_type = sub_dissectors->type;
1463 info.caller_func = func;
1464 info.caller_data = user_data;
1465 g_hash_table_foreach(sub_dissectors->hash_table,
1466 dissector_table_foreach_changed_func, &info);
1469 typedef struct dissector_foreach_table_info {
1470 gpointer caller_data;
1471 DATFunc_table caller_func;
1472 } dissector_foreach_table_info_t;
1475 * Called for each entry in the table of all dissector tables.
1478 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1480 dissector_table_t table;
1481 dissector_foreach_table_info_t *info;
1485 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1489 * Called for each key in the table of all dissector tables.
1492 dissector_all_tables_foreach_list_func (gpointer key, gpointer user_data)
1494 dissector_table_t table;
1495 dissector_foreach_table_info_t *info;
1497 table = g_hash_table_lookup( dissector_tables, key );
1499 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1503 * Walk all dissector tables calling a user supplied function on each
1507 dissector_all_tables_foreach_table (DATFunc_table func,
1509 GCompareFunc compare_key_func)
1511 dissector_foreach_table_info_t info;
1514 info.caller_data = user_data;
1515 info.caller_func = func;
1516 if (compare_key_func != NULL)
1518 list = g_hash_table_get_keys(dissector_tables);
1519 list = g_list_sort(list, compare_key_func);
1520 g_list_foreach(list, dissector_all_tables_foreach_list_func, &info);
1525 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1530 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1533 dissector_table_t sub_dissectors;
1535 /* Create our hash-of-hashes if it doesn't already exist */
1536 if (!dissector_tables) {
1537 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1538 g_assert(dissector_tables);
1541 /* Make sure the registration is unique */
1542 if(g_hash_table_lookup( dissector_tables, name )) {
1543 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1546 /* Create and register the dissector table for this name; returns */
1547 /* a pointer to the dissector table. */
1548 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1556 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1557 * so we use "g_direct_hash()" and "g_direct_equal()".
1559 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash,
1567 sub_dissectors->hash_table = g_hash_table_new_full( g_str_hash,
1574 g_assert_not_reached();
1576 sub_dissectors->dissector_handles = NULL;
1577 sub_dissectors->ui_name = ui_name;
1578 sub_dissectors->type = type;
1579 sub_dissectors->base = base;
1580 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1581 return sub_dissectors;
1585 get_dissector_table_ui_name(const char *name)
1587 dissector_table_t sub_dissectors = find_dissector_table( name);
1589 return sub_dissectors->ui_name;
1593 get_dissector_table_selector_type(const char *name)
1595 dissector_table_t sub_dissectors = find_dissector_table( name);
1597 return sub_dissectors->type;
1601 get_dissector_table_base(const char *name)
1603 dissector_table_t sub_dissectors = find_dissector_table( name);
1605 return sub_dissectors->base;
1608 static GHashTable *heur_dissector_lists = NULL;
1611 /* Finds a heuristic dissector table by table name. */
1612 static heur_dissector_list_t *
1613 find_heur_dissector_list(const char *name)
1615 g_assert(heur_dissector_lists != NULL);
1616 return g_hash_table_lookup(heur_dissector_lists, name);
1620 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1622 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1623 const char *proto_name;
1624 heur_dtbl_entry_t *hdtbl_entry;
1627 * Make sure the dissector table exists.
1629 if (sub_dissectors == NULL) {
1630 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1632 proto_name = proto_get_protocol_name(proto);
1633 if (proto_name != NULL) {
1634 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1637 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1642 /* XXX: Should verify that sub-dissector is not already in the list ? */
1644 hdtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1645 hdtbl_entry->dissector = dissector;
1646 hdtbl_entry->protocol = find_protocol_by_id(proto);
1647 hdtbl_entry->enabled = TRUE;
1649 /* do the table insertion */
1650 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)hdtbl_entry);
1656 find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1657 const heur_dtbl_entry_t *hdtbl_entry_a = (const heur_dtbl_entry_t *) a;
1658 const heur_dtbl_entry_t *hdtbl_entry_b = (const heur_dtbl_entry_t *) b;
1659 return (hdtbl_entry_a->dissector == hdtbl_entry_b->dissector) &&
1660 (hdtbl_entry_a->protocol == hdtbl_entry_b->protocol) ? 0 : 1;
1664 heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1665 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1666 heur_dtbl_entry_t hdtbl_entry;
1667 GSList *found_entry;
1670 g_assert(sub_dissectors != NULL);
1672 hdtbl_entry.dissector = dissector;
1674 hdtbl_entry.protocol = find_protocol_by_id(proto);
1676 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1679 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1680 g_free(g_slist_nth_data(found_entry, 1));
1681 g_slist_free_1(found_entry);
1686 heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled) {
1687 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1688 GSList *found_entry;
1689 heur_dtbl_entry_t hdtbl_entry;
1692 g_assert(sub_dissectors != NULL);
1694 hdtbl_entry.dissector = dissector;
1696 hdtbl_entry.protocol = find_protocol_by_id(proto);
1698 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1701 heur_dtbl_entry_t *hdtbl_entry_p;
1702 hdtbl_entry_p = (heur_dtbl_entry_t *)found_entry->data;
1703 hdtbl_entry_p->enabled = enabled;
1708 dissector_try_heuristic(heur_dissector_list_t sub_dissectors, tvbuff_t *tvb,
1709 packet_info *pinfo, proto_tree *tree, void *data)
1712 const char *saved_proto;
1714 heur_dtbl_entry_t *hdtbl_entry;
1715 guint16 saved_can_desegment;
1716 gint saved_layer_names_len = 0;
1718 /* can_desegment is set to 2 by anyone which offers this api/service.
1719 then everytime a subdissector is called it is decremented by one.
1720 thus only the subdissector immediately ontop of whoever offers this
1722 We save the current value of "can_desegment" for the
1723 benefit of TCP proxying dissectors such as SOCKS, so they
1724 can restore it and allow the dissectors they call to use
1725 the desegmentation service.
1727 saved_can_desegment = pinfo->can_desegment;
1728 pinfo->saved_can_desegment = saved_can_desegment;
1729 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1732 saved_proto = pinfo->current_proto;
1734 if (pinfo->layer_names != NULL)
1735 saved_layer_names_len = (gint) pinfo->layer_names->len;
1737 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1738 /* XXX - why set this now and above? */
1739 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1740 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1742 if (hdtbl_entry->protocol != NULL &&
1743 (!proto_is_protocol_enabled(hdtbl_entry->protocol)||(hdtbl_entry->enabled==FALSE))) {
1745 * No - don't try this dissector.
1750 if (hdtbl_entry->protocol != NULL) {
1751 pinfo->current_proto =
1752 proto_get_protocol_short_name(hdtbl_entry->protocol);
1755 * Add the protocol name to the layers; we'll remove it
1756 * if the dissector fails.
1758 if (pinfo->layer_names) {
1759 if (pinfo->layer_names->len > 0)
1760 g_string_append(pinfo->layer_names, ":");
1761 g_string_append(pinfo->layer_names,
1762 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)));
1765 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1766 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1767 if ((*hdtbl_entry->dissector)(tvb, pinfo, tree, data)) {
1768 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1769 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1773 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1774 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1777 * That dissector didn't accept the packet, so
1778 * remove its protocol's name from the list
1781 if (pinfo->layer_names != NULL) {
1782 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1786 pinfo->current_proto = saved_proto;
1787 pinfo->can_desegment=saved_can_desegment;
1792 * Called for each entry in the table of all heuristic dissector tables.
1794 typedef struct heur_dissector_foreach_table_info {
1795 gpointer caller_data;
1796 DATFunc_heur_table caller_func;
1797 } heur_dissector_foreach_table_info_t;
1801 dissector_dump_heur_decodes_display(const gchar *table_name, const gpointer value, const gpointer user_data _U_)
1803 heur_dissector_list_t sub_dissectors = *(heur_dissector_list_t *)value;
1805 heur_dtbl_entry_t *hdtbl_entry;
1807 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1808 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1809 if (hdtbl_entry->protocol != NULL) {
1810 printf("%s\t%s\t%c\n",
1812 proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)),
1813 (proto_is_protocol_enabled(hdtbl_entry->protocol) && hdtbl_entry->enabled) ? 'T' : 'F');
1820 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1822 heur_dissector_foreach_table_info_t *info;
1825 (*info->caller_func)((gchar*)key, value, info->caller_data);
1829 * Walk all heuristic dissector tables calling a user supplied function on each
1833 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1836 heur_dissector_foreach_table_info_t info;
1838 info.caller_data = user_data;
1839 info.caller_func = func;
1840 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1844 * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
1847 dissector_dump_heur_decodes(void)
1849 dissector_all_heur_tables_foreach_table(dissector_dump_heur_decodes_display, NULL);
1854 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1856 /* Create our hash-of-lists if it doesn't already exist */
1857 if (heur_dissector_lists == NULL) {
1858 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1859 g_assert(heur_dissector_lists != NULL);
1862 /* Make sure the registration is unique */
1863 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1865 *sub_dissectors = NULL; /* initially empty */
1866 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1867 (gpointer) sub_dissectors);
1871 * Register dissectors by name; used if one dissector always calls a
1872 * particular dissector, or if it bases the decision of which dissector
1873 * to call on something other than a numerical value or on "try a bunch
1874 * of dissectors until one likes the packet".
1878 * List of registered dissectors.
1880 static GHashTable *registered_dissectors = NULL;
1882 /* Get the long name of the protocol for a dissector handle, if it has
1885 dissector_handle_get_long_name(const dissector_handle_t handle)
1887 if (handle == NULL || handle->protocol == NULL) {
1890 return proto_get_protocol_long_name(handle->protocol);
1893 /* Get the short name of the protocol for a dissector handle, if it has
1896 dissector_handle_get_short_name(const dissector_handle_t handle)
1898 if (handle->protocol == NULL) {
1900 * No protocol (see, for example, the handle for
1901 * dissecting the set of protocols where the first
1902 * octet of the payload is an OSI network layer protocol
1907 return proto_get_protocol_short_name(handle->protocol);
1910 /* Get the index of the protocol for a dissector handle, if it has
1913 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1915 if (handle->protocol == NULL) {
1917 * No protocol (see, for example, the handle for
1918 * dissecting the set of protocols where the first
1919 * octet of the payload is an OSI network layer protocol
1924 return proto_get_id(handle->protocol);
1927 /* Find a registered dissector by name. */
1929 find_dissector(const char *name)
1931 g_assert(registered_dissectors != NULL);
1932 return g_hash_table_lookup(registered_dissectors, name);
1935 /* Create an anonymous handle for a dissector. */
1937 create_dissector_handle(dissector_t dissector, const int proto)
1939 struct dissector_handle *handle;
1941 handle = g_malloc(sizeof (struct dissector_handle));
1942 handle->name = NULL;
1943 handle->is_new = FALSE;
1944 handle->dissector.old = dissector;
1945 handle->protocol = find_protocol_by_id(proto);
1951 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1953 struct dissector_handle *handle;
1955 handle = g_malloc(sizeof (struct dissector_handle));
1956 handle->name = NULL;
1957 handle->is_new = TRUE;
1958 handle->dissector.new = dissector;
1959 handle->protocol = find_protocol_by_id(proto);
1964 /* Register a dissector by name. */
1966 register_dissector(const char *name, dissector_t dissector, const int proto)
1968 struct dissector_handle *handle;
1970 /* Create our hash table if it doesn't already exist */
1971 if (registered_dissectors == NULL) {
1972 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1973 g_assert(registered_dissectors != NULL);
1976 /* Make sure the registration is unique */
1977 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1979 handle = g_malloc(sizeof (struct dissector_handle));
1980 handle->name = name;
1981 handle->is_new = FALSE;
1982 handle->dissector.old = dissector;
1983 handle->protocol = find_protocol_by_id(proto);
1985 g_hash_table_insert(registered_dissectors, (gpointer)name,
1990 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
1992 struct dissector_handle *handle;
1994 /* Create our hash table if it doesn't already exist */
1995 if (registered_dissectors == NULL) {
1996 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1997 g_assert(registered_dissectors != NULL);
2000 /* Make sure the registration is unique */
2001 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
2003 handle = g_malloc(sizeof (struct dissector_handle));
2004 handle->name = name;
2005 handle->is_new = TRUE;
2006 handle->dissector.new = dissector;
2007 handle->protocol = find_protocol_by_id(proto);
2009 g_hash_table_insert(registered_dissectors, (gpointer)name,
2013 /* Call a dissector through a handle but if the dissector rejected it
2017 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
2018 packet_info *pinfo, proto_tree *tree, void *data)
2022 g_assert(handle != NULL);
2023 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
2027 /* Call a dissector through a handle and if this fails call the "data"
2031 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
2032 packet_info *pinfo, proto_tree *tree)
2036 ret = call_dissector_only(handle, tvb, pinfo, tree, NULL);
2039 * The protocol was disabled, or the dissector rejected
2040 * it. Just dissect this packet as data.
2042 g_assert(data_handle != NULL);
2043 g_assert(data_handle->protocol != NULL);
2044 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2045 return tvb_length(tvb);
2051 * Dumps the "layer type"/"decode as" associations to stdout, similar
2052 * to the proto_registrar_dump_*() routines.
2054 * There is one record per line. The fields are tab-delimited.
2056 * Field 1 = layer type, e.g. "tcp.port"
2057 * Field 2 = selector in decimal
2058 * Field 3 = "decode as" name, e.g. "http"
2063 dissector_dump_decodes_display(const gchar *table_name,
2064 ftenum_t selector_type _U_, const gpointer key, const gpointer value,
2065 gpointer user_data _U_)
2067 guint32 selector = (guint32)(unsigned long) key;
2068 dissector_table_t sub_dissectors = find_dissector_table(table_name);
2069 dtbl_entry_t *dtbl_entry;
2070 dissector_handle_t handle;
2072 const gchar *decode_as;
2074 g_assert(sub_dissectors);
2075 switch (sub_dissectors->type) {
2082 g_assert(dtbl_entry);
2084 handle = dtbl_entry->current;
2087 proto_id = dissector_handle_get_protocol_index(handle);
2089 if (proto_id != -1) {
2090 decode_as = proto_get_protocol_filter_name(proto_id);
2091 g_assert(decode_as != NULL);
2092 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
2102 dissector_dump_decodes(void)
2104 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2107 static GPtrArray* post_dissectors = NULL;
2108 static guint num_of_postdissectors = 0;
2111 register_postdissector(dissector_handle_t handle)
2113 if (!post_dissectors)
2114 post_dissectors = g_ptr_array_new();
2116 g_ptr_array_add(post_dissectors, handle);
2117 num_of_postdissectors++;
2121 have_postdissector(void)
2124 dissector_handle_t handle;
2126 for(i = 0; i < num_of_postdissectors; i++) {
2127 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
2129 if (handle->protocol != NULL
2130 && proto_is_protocol_enabled(handle->protocol)) {
2131 /* We have at least one enabled postdissector */
2139 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2143 for(i = 0; i < num_of_postdissectors; i++) {
2144 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
2145 tvb,pinfo,tree, NULL);
2150 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2155 * indent-tabs-mode: t
2158 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2159 * :indentSize=8:tabSize=8:noTabs=false: