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;
241 /* Allow dissectors to register a "final_registration" routine
242 * that is run like the proto_register_XXX() routine, but at the
243 * end of the epan_init() function; that is, *after* all other
244 * subsystems, like dfilters, have finished initializing. This is
245 * useful for dissector registration routines which need to compile
246 * display filters. dfilters can't initialize itself until all protocols
247 * have registered themselves. */
248 static GSList *final_registration_routines;
251 register_final_registration_routine(void (*func)(void))
253 final_registration_routines = g_slist_append(final_registration_routines,
257 /* Call all the registered "final_registration" routines. */
259 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
261 void_func_t func = (void_func_t)routine;
267 final_registration_all_protocols(void)
269 g_slist_foreach(final_registration_routines,
270 &call_final_registration_routine, NULL);
274 /* Creates the top-most tvbuff and calls dissect_frame() */
276 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
277 const guchar *pd, frame_data *fd, column_info *cinfo)
281 memset(&edt->pi, 0, sizeof(edt->pi));
282 edt->pi.current_proto = "<Missing Protocol Name>";
283 edt->pi.cinfo = cinfo;
285 edt->pi.pseudo_header = pseudo_header;
286 edt->pi.dl_src.type = AT_NONE;
287 edt->pi.dl_dst.type = AT_NONE;
288 edt->pi.net_src.type = AT_NONE;
289 edt->pi.net_dst.type = AT_NONE;
290 edt->pi.src.type = AT_NONE;
291 edt->pi.dst.type = AT_NONE;
292 edt->pi.ctype = CT_NONE;
293 edt->pi.noreassembly_reason = "";
294 edt->pi.ptype = PT_NONE;
295 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
296 edt->pi.dcetransporttype = -1;
297 edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
298 edt->pi.dcerpc_procedure_name="";
299 edt->pi.link_dir = LINK_DIR_UNKNOWN;
302 /* to enable decode as for ethertype=0x0000 (fix for bug 4721) */
303 edt->pi.ethertype = G_MAXINT;
305 EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
309 * XXX - currently, the length arguments to
310 * tvb_new_real_data() are signed, but the captured
311 * and reported length values are unsigned; this means
312 * that length values > 2^31 - 1 will appear as
313 * negative lengths in tvb_new_real_data().
315 * Captured length values that large will already
316 * have been filtered out by the Wiretap modules
317 * (the file will be reported as corrupted), to
318 * avoid trying to allocate large chunks of data.
320 * Reported length values will not have been
321 * filtered out, and should not be filtered out,
322 * as those lengths are not necessarily invalid.
324 * For now, we clip the reported length at G_MAXINT,
325 * so that tvb_new_real_data() doesn't fail. It
326 * would throw an exception, which we'd catch, but
327 * that would mean we would have no tvbuffs
328 * associated with edt, which would upset much of
329 * the rest of the application.
331 edt->tvb = tvb_new_real_data(pd, fd->cap_len,
332 fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len);
333 /* Add this tvbuffer into the data_src list */
334 add_new_data_source(&edt->pi, edt->tvb, "Frame");
336 /* Even though dissect_frame() catches all the exceptions a
337 * sub-dissector can throw, dissect_frame() itself may throw
338 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
339 * in this function. */
340 if(frame_handle != NULL)
341 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
345 g_assert_not_reached();
347 CATCH(ReportedBoundsError) {
348 if(proto_malformed != -1){
349 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
350 "[Malformed Frame: Packet Length]" );
352 g_assert_not_reached();
355 CATCH(OutOfMemoryError) {
360 EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
362 fd->flags.visited = 1;
365 /*********************** code added for sub-dissector lookup *********************/
368 * A dissector handle.
370 struct dissector_handle {
371 const char *name; /* dissector name */
372 gboolean is_new; /* TRUE if new-style dissector */
377 protocol_t *protocol;
380 /* This function will return
381 * old style dissector :
382 * length of the payload or 1 of the payload is empty
384 * >0 this protocol was successfully dissected and this was this protocol.
385 * 0 this packet did not match this protocol.
387 * The only time this function will return 0 is if it is a new style dissector
388 * and if the dissector rejected the packet.
391 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
392 packet_info *pinfo, proto_tree *tree)
394 const char *saved_proto;
397 saved_proto = pinfo->current_proto;
399 if (handle->protocol != NULL) {
400 pinfo->current_proto =
401 proto_get_protocol_short_name(handle->protocol);
404 if (handle->is_new) {
405 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
406 ret = (*handle->dissector.new)(tvb, pinfo, tree);
407 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
409 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
410 (*handle->dissector.old)(tvb, pinfo, tree);
411 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
412 ret = tvb_length(tvb);
415 * XXX - a tvbuff can have 0 bytes of data in
416 * it, so we have to make sure we don't return
423 pinfo->current_proto = saved_proto;
429 * Call a dissector through a handle.
430 * If the protocol for that handle isn't enabled, return 0 without
431 * calling the dissector.
432 * Otherwise, if the handle refers to a new-style dissector, call the
433 * dissector and return its return value, otherwise call it and return
434 * the length of the tvbuff pointed to by the argument.
438 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
439 packet_info *pinfo_arg, proto_tree *tree);
442 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
443 packet_info *pinfo_arg, proto_tree *tree, gboolean add_proto_name)
445 packet_info *pinfo = pinfo_arg;
446 const char *saved_proto;
447 guint16 saved_can_desegment;
449 gint saved_layer_names_len = 0;
451 if (handle->protocol != NULL &&
452 !proto_is_protocol_enabled(handle->protocol)) {
454 * The protocol isn't enabled.
459 saved_proto = pinfo->current_proto;
460 saved_can_desegment = pinfo->can_desegment;
462 if (pinfo->layer_names != NULL)
463 saved_layer_names_len = (gint) pinfo->layer_names->len;
466 * can_desegment is set to 2 by anyone which offers the
467 * desegmentation api/service.
468 * Then everytime a subdissector is called it is decremented
470 * Thus only the subdissector immediately on top of whoever
471 * offers this service can use it.
472 * We save the current value of "can_desegment" for the
473 * benefit of TCP proxying dissectors such as SOCKS, so they
474 * can restore it and allow the dissectors they call to use
475 * the desegmentation service.
477 pinfo->saved_can_desegment = saved_can_desegment;
478 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
479 if (handle->protocol != NULL) {
480 pinfo->current_proto =
481 proto_get_protocol_short_name(handle->protocol);
484 * Add the protocol name to the layers
485 * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
487 if ((pinfo->layer_names)&&(add_proto_name)) {
488 if (pinfo->layer_names->len > 0)
489 g_string_append(pinfo->layer_names, ":");
490 g_string_append(pinfo->layer_names,
491 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
495 if (pinfo->flags.in_error_pkt) {
496 ret = call_dissector_work_error(handle, tvb, pinfo, tree);
499 * Just call the subdissector.
501 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
505 * That dissector didn't accept the packet, so
506 * remove its protocol's name from the list
509 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
510 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
513 pinfo->current_proto = saved_proto;
514 pinfo->can_desegment = saved_can_desegment;
520 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
521 packet_info *pinfo_arg, proto_tree *tree)
523 packet_info *pinfo = pinfo_arg;
524 const char *saved_proto;
525 guint16 saved_can_desegment;
526 volatile int ret = 0;
527 gboolean save_writable;
530 address save_net_src;
531 address save_net_dst;
536 * This isn't a packet being transported inside
537 * the protocol whose dissector is calling us,
538 * it's a copy of a packet that caused an error
539 * in some protocol included in a packet that
540 * reports the error (e.g., an ICMP Unreachable
545 * Save the current state of the writability of
546 * the columns, and restore them after the
547 * dissector returns, so that the columns
548 * don't reflect the packet that got the error,
549 * they reflect the packet that reported the
552 saved_proto = pinfo->current_proto;
553 saved_can_desegment = pinfo->can_desegment;
555 save_writable = col_get_writable(pinfo->cinfo);
556 col_set_writable(pinfo->cinfo, FALSE);
557 save_dl_src = pinfo->dl_src;
558 save_dl_dst = pinfo->dl_dst;
559 save_net_src = pinfo->net_src;
560 save_net_dst = pinfo->net_dst;
561 save_src = pinfo->src;
562 save_dst = pinfo->dst;
564 /* Dissect the contained packet. */
566 ret = call_dissector_through_handle(handle, tvb,pinfo, tree);
570 * Restore the column writability and addresses.
572 col_set_writable(pinfo->cinfo, save_writable);
573 pinfo->dl_src = save_dl_src;
574 pinfo->dl_dst = save_dl_dst;
575 pinfo->net_src = save_net_src;
576 pinfo->net_dst = save_net_dst;
577 pinfo->src = save_src;
578 pinfo->dst = save_dst;
581 * Restore the current protocol, so any
582 * "Short Frame" indication reflects that
583 * protocol, not the protocol for the
584 * packet that got the error.
586 pinfo->current_proto = saved_proto;
589 * Restore the desegmentability state.
591 pinfo->can_desegment = saved_can_desegment;
594 * Rethrow the exception, so this will be
595 * reported as a short frame.
599 CATCH(ReportedBoundsError) {
601 * "ret" wasn't set because an exception was thrown
602 * before "call_dissector_through_handle()" returned.
603 * As it called something, at least one dissector
604 * accepted the packet, and, as an exception was
605 * thrown, not only was all the tvbuff dissected,
606 * a dissector tried dissecting past the end of
607 * the data in some tvbuff, so we'll assume that
608 * the entire tvbuff was dissected.
610 ret = tvb_length(tvb);
612 CATCH(OutOfMemoryError) {
617 col_set_writable(pinfo->cinfo, save_writable);
618 pinfo->dl_src = save_dl_src;
619 pinfo->dl_dst = save_dl_dst;
620 pinfo->net_src = save_net_src;
621 pinfo->net_dst = save_net_dst;
622 pinfo->src = save_src;
623 pinfo->dst = save_dst;
624 pinfo->want_pdu_tracking = 0;
629 * An entry in the hash table portion of a dissector table.
632 dissector_handle_t initial;
633 dissector_handle_t current;
639 * "hash_table" is a hash table, indexed by port number, supplying
640 * a "struct dtbl_entry"; it records what dissector is assigned to
641 * that uint or string value in that table.
643 * "dissector_handles" is a list of all dissectors that *could* be
644 * used in that table; not all of them are necessarily in the table,
645 * as they may be for protocols that don't have a fixed uint value,
646 * e.g. for TCP or UDP port number tables and protocols with no fixed
649 * "ui_name" is the name the dissector table has in the user interface.
651 * "type" is a field type giving the width of the uint value for that
652 * dissector table, if it's a uint dissector table.
654 * "base" is the base in which to display the uint value for that
655 * dissector table, if it's a uint dissector table.
657 struct dissector_table {
658 GHashTable *hash_table;
659 GSList *dissector_handles;
665 static GHashTable *dissector_tables = NULL;
667 /* Finds a dissector table by table name. */
669 find_dissector_table(const char *name)
671 g_assert(dissector_tables);
672 return g_hash_table_lookup( dissector_tables, name );
675 /* Find an entry in a uint dissector table. */
676 static dtbl_entry_t *
677 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
679 switch (sub_dissectors->type) {
686 * You can do a uint lookup in these tables.
692 * But you can't do a uint lookup in any other types
695 g_assert_not_reached();
701 return g_hash_table_lookup(sub_dissectors->hash_table,
702 GUINT_TO_POINTER(pattern));
707 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
709 dtbl_entry_t *dtbl_entry;
712 g_warning("%s: %s registering using a pattern of 0",
713 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
716 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
717 if (dtbl_entry != NULL) {
718 g_warning("%s: %s registering using pattern %d already registered by %s",
719 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
720 pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
725 /* Add an entry to a uint dissector table. */
727 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
729 dissector_table_t sub_dissectors;
730 dtbl_entry_t *dtbl_entry;
732 sub_dissectors = find_dissector_table(name);
735 * Make sure the dissector table exists.
737 if (sub_dissectors == NULL) {
738 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
740 fprintf(stderr, "Protocol being registered is \"%s\"\n",
741 proto_get_protocol_long_name(handle->protocol));
742 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
748 g_assert(handle!=NULL);
749 switch (sub_dissectors->type) {
756 * You can do a uint lookup in these tables.
762 * But you can't do a uint lookup in any other types
765 g_assert_not_reached();
769 dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
772 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
773 dtbl_entry->current = handle;
774 dtbl_entry->initial = dtbl_entry->current;
776 /* do the table insertion */
777 g_hash_table_insert( sub_dissectors->hash_table,
778 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
781 * Now add it to the list of handles that could be used with this
782 * table, because it *is* being used with this table.
784 dissector_add_handle(name, handle);
787 /* Delete the entry for a dissector in a uint dissector table
788 with a particular pattern. */
790 /* NOTE: this doesn't use the dissector call variable. It is included to */
791 /* be consistant with the dissector_add_uint and more importantly to be used */
792 /* if the technique of adding a temporary dissector is implemented. */
793 /* If temporary dissectors are deleted, then the original dissector must */
796 dissector_delete_uint(const char *name, const guint32 pattern,
797 dissector_handle_t handle _U_)
799 dissector_table_t sub_dissectors = find_dissector_table( name);
800 dtbl_entry_t *dtbl_entry;
803 g_assert( sub_dissectors);
808 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
810 if (dtbl_entry != NULL) {
814 g_hash_table_remove(sub_dissectors->hash_table,
815 GUINT_TO_POINTER(pattern));
818 * Now free up the entry.
824 /* Change the entry for a dissector in a uint dissector table
825 with a particular pattern to use a new dissector handle. */
827 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
829 dissector_table_t sub_dissectors = find_dissector_table( name);
830 dtbl_entry_t *dtbl_entry;
833 g_assert( sub_dissectors);
836 * See if the entry already exists. If so, reuse it.
838 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
839 if (dtbl_entry != NULL) {
840 dtbl_entry->current = handle;
845 * Don't create an entry if there is no dissector handle - I.E. the
846 * user said not to decode something that wasn't being decoded
847 * in the first place.
852 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
853 dtbl_entry->initial = NULL;
854 dtbl_entry->current = handle;
856 /* do the table insertion */
857 g_hash_table_insert( sub_dissectors->hash_table,
858 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
861 /* Reset an entry in a uint dissector table to its initial value. */
863 dissector_reset_uint(const char *name, const guint32 pattern)
865 dissector_table_t sub_dissectors = find_dissector_table( name);
866 dtbl_entry_t *dtbl_entry;
869 g_assert( sub_dissectors);
874 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
876 if (dtbl_entry == NULL)
880 * Found - is there an initial value?
882 if (dtbl_entry->initial != NULL) {
883 dtbl_entry->current = dtbl_entry->initial;
885 g_hash_table_remove(sub_dissectors->hash_table,
886 GUINT_TO_POINTER(pattern));
891 /* Look for a given value in a given uint dissector table and, if found,
892 call the dissector with the arguments supplied, and return TRUE,
893 otherwise return FALSE. */
896 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
897 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const gboolean add_proto_name)
899 dtbl_entry_t *dtbl_entry;
900 struct dissector_handle *handle;
901 guint32 saved_match_uint;
904 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
905 if (dtbl_entry != NULL) {
907 * Is there currently a dissector handle for this entry?
909 handle = dtbl_entry->current;
910 if (handle == NULL) {
912 * No - pretend this dissector didn't exist,
913 * so that other dissectors might have a chance
914 * to dissect this packet.
920 * Save the current value of "pinfo->match_uint",
921 * set it to the uint_val that matched, call the
922 * dissector, and restore "pinfo->match_uint".
924 saved_match_uint = pinfo->match_uint;
925 pinfo->match_uint = uint_val;
926 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name);
927 pinfo->match_uint = saved_match_uint;
930 * If a new-style dissector returned 0, it means that
931 * it didn't think this tvbuff represented a packet for
932 * its protocol, and didn't dissect anything.
934 * Old-style dissectors can't reject the packet.
936 * 0 is also returned if the protocol wasn't enabled.
938 * If the packet was rejected, we return FALSE, so that
939 * other dissectors might have a chance to dissect this
940 * packet, otherwise we return TRUE.
948 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
949 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
952 return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE);
955 /* Look for a given value in a given uint dissector table and, if found,
956 return the dissector handle for that value. */
958 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
960 dtbl_entry_t *dtbl_entry;
962 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
963 if (dtbl_entry != NULL)
964 return dtbl_entry->current;
969 /* Find an entry in a string dissector table. */
970 static dtbl_entry_t *
971 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
973 switch (sub_dissectors->type) {
978 * You can do a string lookup in these tables.
984 * But you can't do a string lookup in any other types
987 g_assert_not_reached();
993 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
996 /* Add an entry to a string dissector table. */
998 dissector_add_string(const char *name, const gchar *pattern,
999 dissector_handle_t handle)
1001 dissector_table_t sub_dissectors = find_dissector_table( name);
1002 dtbl_entry_t *dtbl_entry;
1005 * Make sure the dissector table exists.
1007 if (sub_dissectors == NULL) {
1008 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1010 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1011 proto_get_protocol_long_name(handle->protocol));
1012 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1018 g_assert(handle!=NULL);
1019 switch (sub_dissectors->type) {
1024 * You can do a string lookup in these tables.
1030 * But you can't do a string lookup in any other types
1033 g_assert_not_reached();
1036 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1037 dtbl_entry->current = handle;
1038 dtbl_entry->initial = dtbl_entry->current;
1040 /* do the table insertion */
1041 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1042 (gpointer)dtbl_entry);
1045 * Now add it to the list of handles that could be used with this
1046 * table, because it *is* being used with this table.
1048 dissector_add_handle(name, handle);
1051 /* Delete the entry for a dissector in a string dissector table
1052 with a particular pattern. */
1054 /* NOTE: this doesn't use the dissector call variable. It is included to */
1055 /* be consistant with the dissector_add_string and more importantly to */
1056 /* be used if the technique of adding a temporary dissector is */
1058 /* If temporary dissectors are deleted, then the original dissector must */
1061 dissector_delete_string(const char *name, const gchar *pattern,
1062 dissector_handle_t handle _U_)
1064 dissector_table_t sub_dissectors = find_dissector_table( name);
1065 dtbl_entry_t *dtbl_entry;
1068 g_assert( sub_dissectors);
1073 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1075 if (dtbl_entry != NULL) {
1077 * Found - remove it.
1079 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1082 * Now free up the entry.
1088 /* Change the entry for a dissector in a string dissector table
1089 with a particular pattern to use a new dissector handle. */
1091 dissector_change_string(const char *name, const gchar *pattern,
1092 dissector_handle_t handle)
1094 dissector_table_t sub_dissectors = find_dissector_table( name);
1095 dtbl_entry_t *dtbl_entry;
1098 g_assert( sub_dissectors);
1101 * See if the entry already exists. If so, reuse it.
1103 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1104 if (dtbl_entry != NULL) {
1105 dtbl_entry->current = handle;
1110 * Don't create an entry if there is no dissector handle - I.E. the
1111 * user said not to decode something that wasn't being decoded
1112 * in the first place.
1117 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1118 dtbl_entry->initial = NULL;
1119 dtbl_entry->current = handle;
1121 /* do the table insertion */
1122 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1123 (gpointer)dtbl_entry);
1126 /* Reset an entry in a string sub-dissector table to its initial value. */
1128 dissector_reset_string(const char *name, const gchar *pattern)
1130 dissector_table_t sub_dissectors = find_dissector_table( name);
1131 dtbl_entry_t *dtbl_entry;
1134 g_assert( sub_dissectors);
1139 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1141 if (dtbl_entry == NULL)
1145 * Found - is there an initial value?
1147 if (dtbl_entry->initial != NULL) {
1148 dtbl_entry->current = dtbl_entry->initial;
1150 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1155 /* Look for a given string in a given dissector table and, if found, call
1156 the dissector with the arguments supplied, and return TRUE, otherwise
1159 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1160 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1162 dtbl_entry_t *dtbl_entry;
1163 struct dissector_handle *handle;
1165 const gchar *saved_match_string;
1167 /* XXX ASSERT instead ? */
1168 if (!string) return FALSE;
1169 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1170 if (dtbl_entry != NULL) {
1172 * Is there currently a dissector handle for this entry?
1174 handle = dtbl_entry->current;
1175 if (handle == NULL) {
1177 * No - pretend this dissector didn't exist,
1178 * so that other dissectors might have a chance
1179 * to dissect this packet.
1185 * Save the current value of "pinfo->match_string",
1186 * set it to the string that matched, call the
1187 * dissector, and restore "pinfo->match_string".
1189 saved_match_string = pinfo->match_string;
1190 pinfo->match_string = string;
1191 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1192 pinfo->match_string = saved_match_string;
1195 * If a new-style dissector returned 0, it means that
1196 * it didn't think this tvbuff represented a packet for
1197 * its protocol, and didn't dissect anything.
1199 * Old-style dissectors can't reject the packet.
1201 * 0 is also returned if the protocol wasn't enabled.
1203 * If the packet was rejected, we return FALSE, so that
1204 * other dissectors might have a chance to dissect this
1205 * packet, otherwise we return TRUE.
1212 /* Look for a given value in a given string dissector table and, if found,
1213 return the dissector handle for that value. */
1215 dissector_get_string_handle(dissector_table_t sub_dissectors,
1216 const gchar *string)
1218 dtbl_entry_t *dtbl_entry;
1220 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1221 if (dtbl_entry != NULL)
1222 return dtbl_entry->current;
1228 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1230 return dtbl_entry->current;
1233 /* Add a handle to the list of handles that *could* be used with this
1234 table. That list is used by code in the UI. */
1236 dissector_add_handle(const char *name, dissector_handle_t handle)
1238 dissector_table_t sub_dissectors = find_dissector_table( name);
1242 * Make sure the dissector table exists.
1244 if (sub_dissectors == NULL) {
1245 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1247 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1248 proto_get_protocol_long_name(handle->protocol));
1249 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1254 /* Is it already in this list? */
1255 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1256 if (entry != NULL) {
1258 * Yes - don't insert it again.
1263 /* Add it to the list. */
1264 sub_dissectors->dissector_handles =
1265 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1269 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1271 return dtbl_entry->initial;
1274 /**************************************************/
1276 /* Routines to walk dissector tables */
1278 /**************************************************/
1280 typedef struct dissector_foreach_info {
1281 gpointer caller_data;
1282 DATFunc caller_func;
1284 const gchar *table_name;
1285 ftenum_t selector_type;
1286 } dissector_foreach_info_t;
1289 * Called for each entry in a dissector table.
1292 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1294 dissector_foreach_info_t *info;
1295 dtbl_entry_t *dtbl_entry;
1298 g_assert(user_data);
1301 if (dtbl_entry->current == NULL ||
1302 dtbl_entry->current->protocol == NULL) {
1304 * Either there is no dissector for this entry, or
1305 * the dissector doesn't have a protocol associated
1308 * XXX - should the latter check be done?
1314 info->caller_func(info->table_name, info->selector_type, key, value,
1319 * Called for each entry in the table of all dissector tables.
1322 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1324 dissector_table_t sub_dissectors;
1325 dissector_foreach_info_t *info;
1328 g_assert(user_data);
1330 sub_dissectors = value;
1332 info->table_name = (gchar*) key;
1333 info->selector_type = get_dissector_table_selector_type(info->table_name);
1334 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1338 * Walk all dissector tables calling a user supplied function on each
1342 dissector_all_tables_foreach (DATFunc func,
1345 dissector_foreach_info_t info;
1347 info.caller_data = user_data;
1348 info.caller_func = func;
1349 info.next_func = dissector_table_foreach_func;
1350 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1354 * Walk one dissector table's hash table calling a user supplied function
1358 dissector_table_foreach (const char *name,
1362 dissector_foreach_info_t info;
1363 dissector_table_t sub_dissectors = find_dissector_table( name);
1365 info.table_name = name;
1366 info.selector_type = sub_dissectors->type;
1367 info.caller_func = func;
1368 info.caller_data = user_data;
1369 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1373 * Walk one dissector table's list of handles calling a user supplied
1374 * function on each entry.
1377 dissector_table_foreach_handle(const char *name,
1378 DATFunc_handle func,
1381 dissector_table_t sub_dissectors = find_dissector_table( name);
1384 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1385 tmp = g_slist_next(tmp))
1386 func(name, tmp->data, user_data);
1390 * Called for each entry in a dissector table.
1393 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1395 dtbl_entry_t *dtbl_entry;
1396 dissector_foreach_info_t *info;
1399 g_assert(user_data);
1402 if (dtbl_entry->initial == dtbl_entry->current) {
1404 * Entry hasn't changed - don't call the function.
1410 info->caller_func(info->table_name, info->selector_type, key, value,
1415 * Walk all dissector tables calling a user supplied function only on
1416 * any entry that has been changed from its original state.
1419 dissector_all_tables_foreach_changed (DATFunc func,
1422 dissector_foreach_info_t info;
1424 info.caller_data = user_data;
1425 info.caller_func = func;
1426 info.next_func = dissector_table_foreach_changed_func;
1427 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1431 * Walk one dissector table calling a user supplied function only on
1432 * any entry that has been changed from its original state.
1435 dissector_table_foreach_changed (const char *name,
1439 dissector_foreach_info_t info;
1440 dissector_table_t sub_dissectors = find_dissector_table( name);
1442 info.table_name = name;
1443 info.selector_type = sub_dissectors->type;
1444 info.caller_func = func;
1445 info.caller_data = user_data;
1446 g_hash_table_foreach(sub_dissectors->hash_table,
1447 dissector_table_foreach_changed_func, &info);
1450 typedef struct dissector_foreach_table_info {
1451 gpointer caller_data;
1452 DATFunc_table caller_func;
1453 } dissector_foreach_table_info_t;
1456 * Called for each entry in the table of all dissector tables.
1459 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1461 dissector_table_t table;
1462 dissector_foreach_table_info_t *info;
1466 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1470 * Walk all dissector tables calling a user supplied function on each
1474 dissector_all_tables_foreach_table (DATFunc_table func,
1477 dissector_foreach_table_info_t info;
1479 info.caller_data = user_data;
1480 info.caller_func = func;
1481 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1485 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1488 dissector_table_t sub_dissectors;
1490 /* Create our hash-of-hashes if it doesn't already exist */
1491 if (!dissector_tables) {
1492 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1493 g_assert(dissector_tables);
1496 /* Make sure the registration is unique */
1497 if(g_hash_table_lookup( dissector_tables, name )) {
1498 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1501 /* Create and register the dissector table for this name; returns */
1502 /* a pointer to the dissector table. */
1503 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1511 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1512 * so we use "g_direct_hash()" and "g_direct_equal()".
1514 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1520 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1525 g_assert_not_reached();
1527 sub_dissectors->dissector_handles = NULL;
1528 sub_dissectors->ui_name = ui_name;
1529 sub_dissectors->type = type;
1530 sub_dissectors->base = base;
1531 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1532 return sub_dissectors;
1536 get_dissector_table_ui_name(const char *name)
1538 dissector_table_t sub_dissectors = find_dissector_table( name);
1540 return sub_dissectors->ui_name;
1544 get_dissector_table_selector_type(const char *name)
1546 dissector_table_t sub_dissectors = find_dissector_table( name);
1548 return sub_dissectors->type;
1552 get_dissector_table_base(const char *name)
1554 dissector_table_t sub_dissectors = find_dissector_table( name);
1556 return sub_dissectors->base;
1559 static GHashTable *heur_dissector_lists = NULL;
1562 /* Finds a heuristic dissector table by field name. */
1563 static heur_dissector_list_t *
1564 find_heur_dissector_list(const char *name)
1566 g_assert(heur_dissector_lists != NULL);
1567 return g_hash_table_lookup(heur_dissector_lists, name);
1571 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1573 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1574 const char *proto_name;
1575 heur_dtbl_entry_t *dtbl_entry;
1578 * Make sure the dissector table exists.
1580 if (sub_dissectors == NULL) {
1581 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1583 proto_name = proto_get_protocol_name(proto);
1584 if (proto_name != NULL) {
1585 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1588 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1593 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1594 dtbl_entry->dissector = dissector;
1595 dtbl_entry->protocol = find_protocol_by_id(proto);
1597 /* do the table insertion */
1598 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1603 static int find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1604 const heur_dtbl_entry_t *dtbl_entry_a = (const heur_dtbl_entry_t *) a;
1605 const heur_dtbl_entry_t *dtbl_entry_b = (const heur_dtbl_entry_t *) b;
1606 return (dtbl_entry_a->dissector == dtbl_entry_b->dissector) &&
1607 (dtbl_entry_a->protocol == dtbl_entry_b->protocol) ? 0 : 1;
1610 void heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1611 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1612 heur_dtbl_entry_t dtbl_entry;
1613 GSList* found_entry;
1616 g_assert(sub_dissectors != NULL);
1618 dtbl_entry.dissector = dissector;
1620 dtbl_entry.protocol = find_protocol_by_id(proto);
1622 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &dtbl_entry, find_matching_heur_dissector);
1625 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1626 g_free(g_slist_nth_data(found_entry, 1));
1627 g_slist_free_1(found_entry);
1633 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1634 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1637 const char *saved_proto;
1639 heur_dtbl_entry_t *dtbl_entry;
1640 guint16 saved_can_desegment;
1641 gint saved_layer_names_len = 0;
1643 /* can_desegment is set to 2 by anyone which offers this api/service.
1644 then everytime a subdissector is called it is decremented by one.
1645 thus only the subdissector immediately ontop of whoever offers this
1647 We save the current value of "can_desegment" for the
1648 benefit of TCP proxying dissectors such as SOCKS, so they
1649 can restore it and allow the dissectors they call to use
1650 the desegmentation service.
1652 saved_can_desegment=pinfo->can_desegment;
1653 pinfo->saved_can_desegment = saved_can_desegment;
1654 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1657 saved_proto = pinfo->current_proto;
1659 if (pinfo->layer_names != NULL)
1660 saved_layer_names_len = (gint) pinfo->layer_names->len;
1662 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1663 /* XXX - why set this now and above? */
1664 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1665 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1667 if (dtbl_entry->protocol != NULL &&
1668 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1670 * No - don't try this dissector.
1675 if (dtbl_entry->protocol != NULL) {
1676 pinfo->current_proto =
1677 proto_get_protocol_short_name(dtbl_entry->protocol);
1680 * Add the protocol name to the layers; we'll remove it
1681 * if the dissector fails.
1683 if (pinfo->layer_names) {
1684 if (pinfo->layer_names->len > 0)
1685 g_string_append(pinfo->layer_names, ":");
1686 g_string_append(pinfo->layer_names,
1687 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol)));
1690 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1691 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1692 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1693 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1694 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1698 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1699 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1702 * That dissector didn't accept the packet, so
1703 * remove its protocol's name from the list
1706 if (pinfo->layer_names != NULL) {
1707 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1711 pinfo->current_proto = saved_proto;
1712 pinfo->can_desegment=saved_can_desegment;
1717 * Called for each entry in the table of all heuristic dissector tables.
1719 typedef struct heur_dissector_foreach_table_info {
1720 gpointer caller_data;
1721 DATFunc_heur_table caller_func;
1722 } heur_dissector_foreach_table_info_t;
1725 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1727 heur_dissector_foreach_table_info_t *info;
1730 (*info->caller_func)((gchar*)key, value, info->caller_data);
1734 * Walk all heuristic dissector tables calling a user supplied function on each
1738 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1741 heur_dissector_foreach_table_info_t info;
1743 info.caller_data = user_data;
1744 info.caller_func = func;
1745 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1749 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1751 /* Create our hash-of-lists if it doesn't already exist */
1752 if (heur_dissector_lists == NULL) {
1753 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1754 g_assert(heur_dissector_lists != NULL);
1757 /* Make sure the registration is unique */
1758 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1760 *sub_dissectors = NULL; /* initially empty */
1761 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1762 (gpointer) sub_dissectors);
1766 * Register dissectors by name; used if one dissector always calls a
1767 * particular dissector, or if it bases the decision of which dissector
1768 * to call on something other than a numerical value or on "try a bunch
1769 * of dissectors until one likes the packet".
1773 * List of registered dissectors.
1775 static GHashTable *registered_dissectors = NULL;
1777 /* Get the long name of the protocol for a dissector handle, if it has
1780 dissector_handle_get_long_name(const dissector_handle_t handle)
1782 if (handle == NULL || handle->protocol == NULL) {
1785 return proto_get_protocol_long_name(handle->protocol);
1788 /* Get the short name of the protocol for a dissector handle, if it has
1791 dissector_handle_get_short_name(const dissector_handle_t handle)
1793 if (handle->protocol == NULL) {
1795 * No protocol (see, for example, the handle for
1796 * dissecting the set of protocols where the first
1797 * octet of the payload is an OSI network layer protocol
1802 return proto_get_protocol_short_name(handle->protocol);
1805 /* Get the index of the protocol for a dissector handle, if it has
1808 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1810 if (handle->protocol == NULL) {
1812 * No protocol (see, for example, the handle for
1813 * dissecting the set of protocols where the first
1814 * octet of the payload is an OSI network layer protocol
1819 return proto_get_id(handle->protocol);
1822 /* Find a registered dissector by name. */
1824 find_dissector(const char *name)
1826 g_assert(registered_dissectors != NULL);
1827 return g_hash_table_lookup(registered_dissectors, name);
1830 /* Create an anonymous handle for a dissector. */
1832 create_dissector_handle(dissector_t dissector, const int proto)
1834 struct dissector_handle *handle;
1836 handle = g_malloc(sizeof (struct dissector_handle));
1837 handle->name = NULL;
1838 handle->is_new = FALSE;
1839 handle->dissector.old = dissector;
1840 handle->protocol = find_protocol_by_id(proto);
1846 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1848 struct dissector_handle *handle;
1850 handle = g_malloc(sizeof (struct dissector_handle));
1851 handle->name = NULL;
1852 handle->is_new = TRUE;
1853 handle->dissector.new = dissector;
1854 handle->protocol = find_protocol_by_id(proto);
1859 /* Register a dissector by name. */
1861 register_dissector(const char *name, dissector_t dissector, const int proto)
1863 struct dissector_handle *handle;
1865 /* Create our hash table if it doesn't already exist */
1866 if (registered_dissectors == NULL) {
1867 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1868 g_assert(registered_dissectors != NULL);
1871 /* Make sure the registration is unique */
1872 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1874 handle = g_malloc(sizeof (struct dissector_handle));
1875 handle->name = name;
1876 handle->is_new = FALSE;
1877 handle->dissector.old = dissector;
1878 handle->protocol = find_protocol_by_id(proto);
1880 g_hash_table_insert(registered_dissectors, (gpointer)name,
1885 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
1887 struct dissector_handle *handle;
1889 /* Create our hash table if it doesn't already exist */
1890 if (registered_dissectors == NULL) {
1891 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1892 g_assert(registered_dissectors != NULL);
1895 /* Make sure the registration is unique */
1896 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1898 handle = g_malloc(sizeof (struct dissector_handle));
1899 handle->name = name;
1900 handle->is_new = TRUE;
1901 handle->dissector.new = dissector;
1902 handle->protocol = find_protocol_by_id(proto);
1904 g_hash_table_insert(registered_dissectors, (gpointer)name,
1908 /* Call a dissector through a handle but if the dissector rejected it
1912 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
1913 packet_info *pinfo, proto_tree *tree)
1917 g_assert(handle != NULL);
1918 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1922 /* Call a dissector through a handle and if this fails call the "data"
1926 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1927 packet_info *pinfo, proto_tree *tree)
1931 ret = call_dissector_only(handle, tvb, pinfo, tree);
1934 * The protocol was disabled, or the dissector rejected
1935 * it. Just dissect this packet as data.
1937 g_assert(data_handle != NULL);
1938 g_assert(data_handle->protocol != NULL);
1939 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE);
1940 return tvb_length(tvb);
1946 * Dumps the "layer type"/"decode as" associations to stdout, similar
1947 * to the proto_registrar_dump_*() routines.
1949 * There is one record per line. The fields are tab-delimited.
1951 * Field 1 = layer type, e.g. "tcp.port"
1952 * Field 2 = selector in decimal
1953 * Field 3 = "decode as" name, e.g. "http"
1958 dissector_dump_decodes_display(const gchar *table_name,
1959 ftenum_t selector_type _U_, const gpointer key, const gpointer value,
1960 gpointer user_data _U_)
1962 guint32 selector = (guint32)(unsigned long) key;
1963 dissector_table_t sub_dissectors = find_dissector_table(table_name);
1964 dtbl_entry_t *dtbl_entry;
1965 dissector_handle_t handle;
1967 const gchar *decode_as;
1969 g_assert(sub_dissectors);
1970 switch (sub_dissectors->type) {
1977 g_assert(dtbl_entry);
1979 handle = dtbl_entry->current;
1982 proto_id = dissector_handle_get_protocol_index(handle);
1984 if (proto_id != -1) {
1985 decode_as = proto_get_protocol_filter_name(proto_id);
1986 g_assert(decode_as != NULL);
1987 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
1997 dissector_dump_decodes(void)
1999 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2002 static GPtrArray* post_dissectors = NULL;
2003 static guint num_of_postdissectors = 0;
2006 register_postdissector(dissector_handle_t handle)
2008 if (!post_dissectors)
2009 post_dissectors = g_ptr_array_new();
2011 g_ptr_array_add(post_dissectors, handle);
2012 num_of_postdissectors++;
2016 have_postdissector(void)
2019 dissector_handle_t handle;
2021 for(i = 0; i < num_of_postdissectors; i++) {
2022 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
2024 if (handle->protocol != NULL
2025 && proto_is_protocol_enabled(handle->protocol)) {
2026 /* We have at least one enabled postdissector */
2034 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2038 for(i = 0; i < num_of_postdissectors; i++) {
2039 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),