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, 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 common data structures for fragment reassembly.
143 Must be done *after* calling init routines, as those routines
144 may free up space for fragments, which they find by using the
145 data structures that "reassemble_init()" frees. */
148 /* Initialize the stream-handling tables */
151 /* Initialize the expert infos */
156 cleanup_dissection(void)
161 /* Allow protocols to register a "cleanup" routine to be
162 * run after the initial sequential run through the packets.
163 * Note that the file can still be open after this; this is not
164 * the final cleanup. */
165 static GSList *postseq_cleanup_routines;
168 register_postseq_cleanup_routine(void_func_t func)
170 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
174 /* Call all the registered "postseq_cleanup" routines. */
176 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
178 void_func_t func = (void_func_t)routine;
183 postseq_cleanup_all_protocols(void)
185 g_slist_foreach(postseq_cleanup_routines,
186 &call_postseq_cleanup_routine, NULL);
190 * Add a new data source to the list of data sources for a frame, given
191 * the tvbuff for the data source and its name.
194 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
198 src = ep_alloc(sizeof (data_source));
200 src->name_initialized = FALSE;
202 pinfo->data_src = g_slist_append(pinfo->data_src, src);
206 get_data_source_name(data_source *src)
208 if (!src->name_initialized) {
209 src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
210 src->name_initialized = TRUE;
217 * Free up a frame's list of data sources.
220 free_data_sources(packet_info *pinfo)
222 g_slist_free(pinfo->data_src);
223 pinfo->data_src = NULL;
226 /* Allow dissectors to register a "final_registration" routine
227 * that is run like the proto_register_XXX() routine, but at the
228 * end of the epan_init() function; that is, *after* all other
229 * subsystems, like dfilters, have finished initializing. This is
230 * useful for dissector registration routines which need to compile
231 * display filters. dfilters can't initialize itself until all protocols
232 * have registered themselves. */
233 static GSList *final_registration_routines;
236 register_final_registration_routine(void (*func)(void))
238 final_registration_routines = g_slist_append(final_registration_routines,
242 /* Call all the registered "final_registration" routines. */
244 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
246 void_func_t func = (void_func_t)routine;
252 final_registration_all_protocols(void)
254 g_slist_foreach(final_registration_routines,
255 &call_final_registration_routine, NULL);
259 /* Creates the top-most tvbuff and calls dissect_frame() */
261 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
262 const guchar *pd, frame_data *fd, column_info *cinfo)
266 memset(&edt->pi, 0, sizeof(edt->pi));
267 edt->pi.current_proto = "<Missing Protocol Name>";
268 edt->pi.cinfo = cinfo;
270 edt->pi.pseudo_header = pseudo_header;
271 edt->pi.dl_src.type = AT_NONE;
272 edt->pi.dl_dst.type = AT_NONE;
273 edt->pi.net_src.type = AT_NONE;
274 edt->pi.net_dst.type = AT_NONE;
275 edt->pi.src.type = AT_NONE;
276 edt->pi.dst.type = AT_NONE;
277 edt->pi.ctype = CT_NONE;
278 edt->pi.noreassembly_reason = "";
279 edt->pi.ptype = PT_NONE;
280 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
281 edt->pi.dcetransporttype = -1;
282 edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
283 edt->pi.dcerpc_procedure_name="";
284 edt->pi.link_dir = LINK_DIR_UNKNOWN;
286 EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
289 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
290 /* Add this tvbuffer into the data_src list */
291 add_new_data_source(&edt->pi, edt->tvb, "Frame");
293 /* Even though dissect_frame() catches all the exceptions a
294 * sub-dissector can throw, dissect_frame() itself may throw
295 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
296 * in this function. */
297 if(frame_handle != NULL)
298 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
302 g_assert_not_reached();
304 CATCH(ReportedBoundsError) {
305 if(proto_malformed != -1){
306 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
307 "[Malformed Frame: Packet Length]" );
309 g_assert_not_reached();
312 CATCH(OutOfMemoryError) {
317 EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
319 fd->flags.visited = 1;
322 /*********************** code added for sub-dissector lookup *********************/
325 * An dissector handle.
327 struct dissector_handle {
328 const char *name; /* dissector name */
329 gboolean is_new; /* TRUE if new-style dissector */
334 protocol_t *protocol;
337 /* This function will return
338 * old style dissector :
339 * length of the payload or 1 of the payload is empty
341 * >0 this protocol was successfully dissected and this was this protocol.
342 * 0 this packet did not match this protocol.
344 * The only time this function will return 0 is if it is a new style dissector
345 * and if the dissector rejected the packet.
348 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
349 packet_info *pinfo, proto_tree *tree)
351 const char *saved_proto;
354 saved_proto = pinfo->current_proto;
356 if (handle->protocol != NULL) {
357 pinfo->current_proto =
358 proto_get_protocol_short_name(handle->protocol);
361 if (handle->is_new) {
362 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
363 ret = (*handle->dissector.new)(tvb, pinfo, tree);
364 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
366 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
367 (*handle->dissector.old)(tvb, pinfo, tree);
368 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
369 ret = tvb_length(tvb);
372 * XXX - a tvbuff can have 0 bytes of data in
373 * it, so we have to make sure we don't return
380 pinfo->current_proto = saved_proto;
386 * Call a dissector through a handle.
387 * If the protocol for that handle isn't enabled, return 0 without
388 * calling the dissector.
389 * Otherwise, if the handle refers to a new-style dissector, call the
390 * dissector and return its return value, otherwise call it and return
391 * the length of the tvbuff pointed to by the argument.
395 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
396 packet_info *pinfo_arg, proto_tree *tree);
399 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
400 packet_info *pinfo_arg, proto_tree *tree, gboolean add_proto_name)
402 packet_info *pinfo = pinfo_arg;
403 const char *saved_proto;
404 guint16 saved_can_desegment;
406 gint saved_layer_names_len = 0;
408 if (handle->protocol != NULL &&
409 !proto_is_protocol_enabled(handle->protocol)) {
411 * The protocol isn't enabled.
416 saved_proto = pinfo->current_proto;
417 saved_can_desegment = pinfo->can_desegment;
419 if (pinfo->layer_names != NULL)
420 saved_layer_names_len = (gint) pinfo->layer_names->len;
423 * can_desegment is set to 2 by anyone which offers the
424 * desegmentation api/service.
425 * Then everytime a subdissector is called it is decremented
427 * Thus only the subdissector immediately on top of whoever
428 * offers this service can use it.
429 * We save the current value of "can_desegment" for the
430 * benefit of TCP proxying dissectors such as SOCKS, so they
431 * can restore it and allow the dissectors they call to use
432 * the desegmentation service.
434 pinfo->saved_can_desegment = saved_can_desegment;
435 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
436 if (handle->protocol != NULL) {
437 pinfo->current_proto =
438 proto_get_protocol_short_name(handle->protocol);
441 * Add the protocol name to the layers
442 * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
444 if ((pinfo->layer_names)&&(add_proto_name)) {
445 if (pinfo->layer_names->len > 0)
446 g_string_append(pinfo->layer_names, ":");
447 g_string_append(pinfo->layer_names,
448 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
452 if (pinfo->in_error_pkt) {
453 ret = call_dissector_work_error(handle, tvb, pinfo, tree);
456 * Just call the subdissector.
458 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
462 * That dissector didn't accept the packet, so
463 * remove its protocol's name from the list
466 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
467 g_string_truncate(pinfo->layer_names,
468 saved_layer_names_len);
471 pinfo->current_proto = saved_proto;
472 pinfo->can_desegment = saved_can_desegment;
478 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
479 packet_info *pinfo_arg, proto_tree *tree)
481 packet_info *pinfo = pinfo_arg;
482 const char *saved_proto;
483 guint16 saved_can_desegment;
484 volatile int ret = 0;
485 gboolean save_writable;
488 address save_net_src;
489 address save_net_dst;
494 * This isn't a packet being transported inside
495 * the protocol whose dissector is calling us,
496 * it's a copy of a packet that caused an error
497 * in some protocol included in a packet that
498 * reports the error (e.g., an ICMP Unreachable
503 * Save the current state of the writability of
504 * the columns, and restore them after the
505 * dissector returns, so that the columns
506 * don't reflect the packet that got the error,
507 * they reflect the packet that reported the
510 saved_proto = pinfo->current_proto;
511 saved_can_desegment = pinfo->can_desegment;
513 save_writable = col_get_writable(pinfo->cinfo);
514 col_set_writable(pinfo->cinfo, FALSE);
515 save_dl_src = pinfo->dl_src;
516 save_dl_dst = pinfo->dl_dst;
517 save_net_src = pinfo->net_src;
518 save_net_dst = pinfo->net_dst;
519 save_src = pinfo->src;
520 save_dst = pinfo->dst;
522 /* Dissect the contained packet. */
524 ret = call_dissector_through_handle(handle, tvb,pinfo, tree);
528 * Restore the column writability and addresses.
530 col_set_writable(pinfo->cinfo, save_writable);
531 pinfo->dl_src = save_dl_src;
532 pinfo->dl_dst = save_dl_dst;
533 pinfo->net_src = save_net_src;
534 pinfo->net_dst = save_net_dst;
535 pinfo->src = save_src;
536 pinfo->dst = save_dst;
539 * Restore the current protocol, so any
540 * "Short Frame" indication reflects that
541 * protocol, not the protocol for the
542 * packet that got the error.
544 pinfo->current_proto = saved_proto;
547 * Restore the desegmentability state.
549 pinfo->can_desegment = saved_can_desegment;
552 * Rethrow the exception, so this will be
553 * reported as a short frame.
557 CATCH(ReportedBoundsError) {
559 * "ret" wasn't set because an exception was thrown
560 * before "call_dissector_through_handle()" returned.
561 * As it called something, at least one dissector
562 * accepted the packet, and, as an exception was
563 * thrown, not only was all the tvbuff dissected,
564 * a dissector tried dissecting past the end of
565 * the data in some tvbuff, so we'll assume that
566 * the entire tvbuff was dissected.
568 ret = tvb_length(tvb);
570 CATCH(OutOfMemoryError) {
575 col_set_writable(pinfo->cinfo, save_writable);
576 pinfo->dl_src = save_dl_src;
577 pinfo->dl_dst = save_dl_dst;
578 pinfo->net_src = save_net_src;
579 pinfo->net_dst = save_net_dst;
580 pinfo->src = save_src;
581 pinfo->dst = save_dst;
582 pinfo->want_pdu_tracking = 0;
587 * An entry in the hash table portion of a dissector table.
590 dissector_handle_t initial;
591 dissector_handle_t current;
597 * "hash_table" is a hash table, indexed by port number, supplying
598 * a "struct dtbl_entry"; it records what dissector is assigned to
599 * that port number in that table.
601 * "dissector_handles" is a list of all dissectors that *could* be
602 * used in that table; not all of them are necessarily in the table,
603 * as they may be for protocols that don't have a fixed port number.
605 * "ui_name" is the name the dissector table has in the user interface.
607 * "type" is a field type giving the width of the port number for that
610 * "base" is the base in which to display the port number for that
613 struct dissector_table {
614 GHashTable *hash_table;
615 GSList *dissector_handles;
621 static GHashTable *dissector_tables = NULL;
623 /* Finds a dissector table by table name. */
625 find_dissector_table(const char *name)
627 g_assert(dissector_tables);
628 return g_hash_table_lookup( dissector_tables, name );
631 /* Find an entry in a uint dissector table. */
632 static dtbl_entry_t *
633 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
635 switch (sub_dissectors->type) {
642 * You can do a port lookup in these tables.
648 * But you can't do a port lookup in any other types
651 g_assert_not_reached();
657 return g_hash_table_lookup(sub_dissectors->hash_table,
658 GUINT_TO_POINTER(pattern));
661 /* Add an entry to a uint dissector table. */
663 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
665 dissector_table_t sub_dissectors;
666 dtbl_entry_t *dtbl_entry;
668 sub_dissectors = find_dissector_table(name);
670 g_assert(sub_dissectors);
671 switch (sub_dissectors->type) {
678 * You can do a port lookup in these tables.
684 * But you can't do a port lookup in any other types
687 g_assert_not_reached();
692 g_warning("%s: %s registering using a pattern of 0",
693 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
696 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
697 if (dtbl_entry != NULL) {
698 g_warning("%s: %s registering using pattern %d already registered by %s",
699 name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
700 pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
703 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
704 dtbl_entry->current = handle;
705 dtbl_entry->initial = dtbl_entry->current;
707 /* do the table insertion */
708 g_hash_table_insert( sub_dissectors->hash_table,
709 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
712 * Now add it to the list of handles that could be used with this
713 * table, because it *is* being used with this table.
715 dissector_add_handle(name, handle);
718 /* Delete the entry for a dissector in a uint dissector table
719 with a particular pattern. */
721 /* NOTE: this doesn't use the dissector call variable. It is included to */
722 /* be consistant with the dissector_add and more importantly to be used */
723 /* if the technique of adding a temporary dissector is implemented. */
724 /* If temporary dissectors are deleted, then the original dissector must */
727 dissector_delete(const char *name, guint32 pattern,
728 dissector_handle_t handle _U_)
730 dissector_table_t sub_dissectors = find_dissector_table( name);
731 dtbl_entry_t *dtbl_entry;
734 g_assert( sub_dissectors);
739 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
741 if (dtbl_entry != NULL) {
745 g_hash_table_remove(sub_dissectors->hash_table,
746 GUINT_TO_POINTER(pattern));
749 * Now free up the entry.
755 /* Change the entry for a dissector in a uint dissector table
756 with a particular pattern to use a new dissector handle. */
758 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
760 dissector_table_t sub_dissectors = find_dissector_table( name);
761 dtbl_entry_t *dtbl_entry;
764 g_assert( sub_dissectors);
767 * See if the entry already exists. If so, reuse it.
769 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
770 if (dtbl_entry != NULL) {
771 dtbl_entry->current = handle;
776 * Don't create an entry if there is no dissector handle - I.E. the
777 * user said not to decode something that wasn't being decoded
778 * in the first place.
783 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
784 dtbl_entry->initial = NULL;
785 dtbl_entry->current = handle;
787 /* do the table insertion */
788 g_hash_table_insert( sub_dissectors->hash_table,
789 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
792 /* Reset an entry in a uint dissector table to its initial value. */
794 dissector_reset(const char *name, guint32 pattern)
796 dissector_table_t sub_dissectors = find_dissector_table( name);
797 dtbl_entry_t *dtbl_entry;
800 g_assert( sub_dissectors);
805 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
807 if (dtbl_entry == NULL)
811 * Found - is there an initial value?
813 if (dtbl_entry->initial != NULL) {
814 dtbl_entry->current = dtbl_entry->initial;
816 g_hash_table_remove(sub_dissectors->hash_table,
817 GUINT_TO_POINTER(pattern));
822 /* Look for a given value in a given uint dissector table and, if found,
823 call the dissector with the arguments supplied, and return TRUE,
824 otherwise return FALSE. */
827 dissector_try_port_new(dissector_table_t sub_dissectors, guint32 port,
828 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean add_proto_name)
830 dtbl_entry_t *dtbl_entry;
831 struct dissector_handle *handle;
832 guint32 saved_match_port;
835 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
836 if (dtbl_entry != NULL) {
838 * Is there currently a dissector handle for this entry?
840 handle = dtbl_entry->current;
841 if (handle == NULL) {
843 * No - pretend this dissector didn't exist,
844 * so that other dissectors might have a chance
845 * to dissect this packet.
851 * Save the current value of "pinfo->match_port",
852 * set it to the port that matched, call the
853 * dissector, and restore "pinfo->match_port".
855 saved_match_port = pinfo->match_port;
856 pinfo->match_port = port;
857 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name);
858 pinfo->match_port = saved_match_port;
861 * If a new-style dissector returned 0, it means that
862 * it didn't think this tvbuff represented a packet for
863 * its protocol, and didn't dissect anything.
865 * Old-style dissectors can't reject the packet.
867 * 0 is also returned if the protocol wasn't enabled.
869 * If the packet was rejected, we return FALSE, so that
870 * other dissectors might have a chance to dissect this
871 * packet, otherwise we return TRUE.
879 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
880 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
883 return dissector_try_port_new(sub_dissectors, port, tvb, pinfo, tree, TRUE);
885 /* Look for a given value in a given uint dissector table and, if found,
886 return the dissector handle for that value. */
888 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
890 dtbl_entry_t *dtbl_entry;
892 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
893 if (dtbl_entry != NULL)
894 return dtbl_entry->current;
899 /* Find an entry in a string dissector table. */
900 static dtbl_entry_t *
901 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
903 switch (sub_dissectors->type) {
908 * You can do a string lookup in these tables.
914 * But you can't do a string lookup in any other types
917 g_assert_not_reached();
923 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
926 /* Add an entry to a string dissector table. */
928 dissector_add_string(const char *name, const gchar *pattern,
929 dissector_handle_t handle)
931 dissector_table_t sub_dissectors = find_dissector_table( name);
932 dtbl_entry_t *dtbl_entry;
935 g_assert( sub_dissectors);
937 switch (sub_dissectors->type) {
942 * You can do a string lookup in these tables.
948 * But you can't do a string lookup in any other types
951 g_assert_not_reached();
954 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
955 dtbl_entry->current = handle;
956 dtbl_entry->initial = dtbl_entry->current;
958 /* do the table insertion */
959 g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
960 (gpointer)dtbl_entry);
963 * Now add it to the list of handles that could be used with this
964 * table, because it *is* being used with this table.
966 dissector_add_handle(name, handle);
969 /* Delete the entry for a dissector in a string dissector table
970 with a particular pattern. */
972 /* NOTE: this doesn't use the dissector call variable. It is included to */
973 /* be consistant with the dissector_add_string and more importantly to */
974 /* be used if the technique of adding a temporary dissector is */
976 /* If temporary dissectors are deleted, then the original dissector must */
979 dissector_delete_string(const char *name, const gchar *pattern,
980 dissector_handle_t handle _U_)
982 dissector_table_t sub_dissectors = find_dissector_table( name);
983 dtbl_entry_t *dtbl_entry;
986 g_assert( sub_dissectors);
991 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
993 if (dtbl_entry != NULL) {
997 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1000 * Now free up the entry.
1006 /* Change the entry for a dissector in a string dissector table
1007 with a particular pattern to use a new dissector handle. */
1009 dissector_change_string(const char *name, gchar *pattern,
1010 dissector_handle_t handle)
1012 dissector_table_t sub_dissectors = find_dissector_table( name);
1013 dtbl_entry_t *dtbl_entry;
1016 g_assert( sub_dissectors);
1019 * See if the entry already exists. If so, reuse it.
1021 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1022 if (dtbl_entry != NULL) {
1023 dtbl_entry->current = handle;
1028 * Don't create an entry if there is no dissector handle - I.E. the
1029 * user said not to decode something that wasn't being decoded
1030 * in the first place.
1035 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1036 dtbl_entry->initial = NULL;
1037 dtbl_entry->current = handle;
1039 /* do the table insertion */
1040 g_hash_table_insert( sub_dissectors->hash_table, pattern,
1041 (gpointer)dtbl_entry);
1044 /* Reset an entry in a string sub-dissector table to its initial value. */
1046 dissector_reset_string(const char *name, const gchar *pattern)
1048 dissector_table_t sub_dissectors = find_dissector_table( name);
1049 dtbl_entry_t *dtbl_entry;
1052 g_assert( sub_dissectors);
1057 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1059 if (dtbl_entry == NULL)
1063 * Found - is there an initial value?
1065 if (dtbl_entry->initial != NULL) {
1066 dtbl_entry->current = dtbl_entry->initial;
1068 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1073 /* Look for a given string in a given dissector table and, if found, call
1074 the dissector with the arguments supplied, and return TRUE, otherwise
1077 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1078 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1080 dtbl_entry_t *dtbl_entry;
1081 struct dissector_handle *handle;
1083 const gchar *saved_match_string;
1085 /* XXX ASSERT instead ? */
1086 if (!string) return FALSE;
1087 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1088 if (dtbl_entry != NULL) {
1090 * Is there currently a dissector handle for this entry?
1092 handle = dtbl_entry->current;
1093 if (handle == NULL) {
1095 * No - pretend this dissector didn't exist,
1096 * so that other dissectors might have a chance
1097 * to dissect this packet.
1103 * Save the current value of "pinfo->match_string",
1104 * set it to the string that matched, call the
1105 * dissector, and restore "pinfo->match_string".
1107 saved_match_string = pinfo->match_string;
1108 pinfo->match_string = string;
1109 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1110 pinfo->match_string = saved_match_string;
1113 * If a new-style dissector returned 0, it means that
1114 * it didn't think this tvbuff represented a packet for
1115 * its protocol, and didn't dissect anything.
1117 * Old-style dissectors can't reject the packet.
1119 * 0 is also returned if the protocol wasn't enabled.
1121 * If the packet was rejected, we return FALSE, so that
1122 * other dissectors might have a chance to dissect this
1123 * packet, otherwise we return TRUE.
1130 /* Look for a given value in a given string dissector table and, if found,
1131 return the dissector handle for that value. */
1133 dissector_get_string_handle(dissector_table_t sub_dissectors,
1134 const gchar *string)
1136 dtbl_entry_t *dtbl_entry;
1138 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1139 if (dtbl_entry != NULL)
1140 return dtbl_entry->current;
1146 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1148 return dtbl_entry->current;
1151 /* Add a handle to the list of handles that *could* be used with this
1152 table. That list is used by code in the UI. */
1154 dissector_add_handle(const char *name, dissector_handle_t handle)
1156 dissector_table_t sub_dissectors = find_dissector_table( name);
1160 g_assert(sub_dissectors != NULL);
1162 /* Is it already in this list? */
1163 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1164 if (entry != NULL) {
1166 * Yes - don't insert it again.
1171 /* Add it to the list. */
1172 sub_dissectors->dissector_handles =
1173 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1177 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1179 return dtbl_entry->initial;
1182 /**************************************************/
1184 /* Routines to walk dissector tables */
1186 /**************************************************/
1188 typedef struct dissector_foreach_info {
1189 gpointer caller_data;
1190 DATFunc caller_func;
1192 const gchar *table_name;
1193 ftenum_t selector_type;
1194 } dissector_foreach_info_t;
1197 * Called for each entry in a dissector table.
1200 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1202 dissector_foreach_info_t *info;
1203 dtbl_entry_t *dtbl_entry;
1206 g_assert(user_data);
1209 if (dtbl_entry->current == NULL ||
1210 dtbl_entry->current->protocol == NULL) {
1212 * Either there is no dissector for this entry, or
1213 * the dissector doesn't have a protocol associated
1216 * XXX - should the latter check be done?
1222 info->caller_func(info->table_name, info->selector_type, key, value,
1227 * Called for each entry in the table of all dissector tables.
1230 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1232 dissector_table_t sub_dissectors;
1233 dissector_foreach_info_t *info;
1236 g_assert(user_data);
1238 sub_dissectors = value;
1240 info->table_name = (gchar*) key;
1241 info->selector_type = get_dissector_table_selector_type(info->table_name);
1242 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1246 * Walk all dissector tables calling a user supplied function on each
1250 dissector_all_tables_foreach (DATFunc func,
1253 dissector_foreach_info_t info;
1255 info.caller_data = user_data;
1256 info.caller_func = func;
1257 info.next_func = dissector_table_foreach_func;
1258 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1262 * Walk one dissector table's hash table calling a user supplied function
1266 dissector_table_foreach (const char *name,
1270 dissector_foreach_info_t info;
1271 dissector_table_t sub_dissectors = find_dissector_table( name);
1273 info.table_name = name;
1274 info.selector_type = sub_dissectors->type;
1275 info.caller_func = func;
1276 info.caller_data = user_data;
1277 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1281 * Walk one dissector table's list of handles calling a user supplied
1282 * function on each entry.
1285 dissector_table_foreach_handle(const char *name,
1286 DATFunc_handle func,
1289 dissector_table_t sub_dissectors = find_dissector_table( name);
1292 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1293 tmp = g_slist_next(tmp))
1294 func(name, tmp->data, user_data);
1298 * Called for each entry in a dissector table.
1301 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1303 dtbl_entry_t *dtbl_entry;
1304 dissector_foreach_info_t *info;
1307 g_assert(user_data);
1310 if (dtbl_entry->initial == dtbl_entry->current) {
1312 * Entry hasn't changed - don't call the function.
1318 info->caller_func(info->table_name, info->selector_type, key, value,
1323 * Walk all dissector tables calling a user supplied function only on
1324 * any entry that has been changed from its original state.
1327 dissector_all_tables_foreach_changed (DATFunc func,
1330 dissector_foreach_info_t info;
1332 info.caller_data = user_data;
1333 info.caller_func = func;
1334 info.next_func = dissector_table_foreach_changed_func;
1335 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1339 * Walk one dissector table calling a user supplied function only on
1340 * any entry that has been changed from its original state.
1343 dissector_table_foreach_changed (const char *name,
1347 dissector_foreach_info_t info;
1348 dissector_table_t sub_dissectors = find_dissector_table( name);
1350 info.table_name = name;
1351 info.selector_type = sub_dissectors->type;
1352 info.caller_func = func;
1353 info.caller_data = user_data;
1354 g_hash_table_foreach(sub_dissectors->hash_table,
1355 dissector_table_foreach_changed_func, &info);
1358 typedef struct dissector_foreach_table_info {
1359 gpointer caller_data;
1360 DATFunc_table caller_func;
1361 } dissector_foreach_table_info_t;
1364 * Called for each entry in the table of all dissector tables.
1367 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1369 dissector_table_t table;
1370 dissector_foreach_table_info_t *info;
1374 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1378 * Walk all dissector tables calling a user supplied function on each
1382 dissector_all_tables_foreach_table (DATFunc_table func,
1385 dissector_foreach_table_info_t info;
1387 info.caller_data = user_data;
1388 info.caller_func = func;
1389 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1393 register_dissector_table(const char *name, const char *ui_name, ftenum_t type,
1396 dissector_table_t sub_dissectors;
1398 /* Create our hash-of-hashes if it doesn't already exist */
1399 if (!dissector_tables) {
1400 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1401 g_assert(dissector_tables);
1404 /* Make sure the registration is unique */
1405 if(g_hash_table_lookup( dissector_tables, name )) {
1406 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1409 /* Create and register the dissector table for this name; returns */
1410 /* a pointer to the dissector table. */
1411 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1419 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1420 * so we use "g_direct_hash()" and "g_direct_equal()".
1422 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1428 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1433 g_assert_not_reached();
1435 sub_dissectors->dissector_handles = NULL;
1436 sub_dissectors->ui_name = ui_name;
1437 sub_dissectors->type = type;
1438 sub_dissectors->base = base;
1439 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1440 return sub_dissectors;
1444 get_dissector_table_ui_name(const char *name)
1446 dissector_table_t sub_dissectors = find_dissector_table( name);
1448 return sub_dissectors->ui_name;
1452 get_dissector_table_selector_type(const char *name)
1454 dissector_table_t sub_dissectors = find_dissector_table( name);
1456 return sub_dissectors->type;
1460 get_dissector_table_base(const char *name)
1462 dissector_table_t sub_dissectors = find_dissector_table( name);
1464 return sub_dissectors->base;
1467 static GHashTable *heur_dissector_lists = NULL;
1470 heur_dissector_t dissector;
1471 protocol_t *protocol;
1472 } heur_dtbl_entry_t;
1474 /* Finds a heuristic dissector table by field name. */
1475 static heur_dissector_list_t *
1476 find_heur_dissector_list(const char *name)
1478 g_assert(heur_dissector_lists != NULL);
1479 return g_hash_table_lookup(heur_dissector_lists, name);
1483 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1485 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1486 heur_dtbl_entry_t *dtbl_entry;
1489 g_assert(sub_dissectors != NULL);
1491 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1492 dtbl_entry->dissector = dissector;
1493 dtbl_entry->protocol = find_protocol_by_id(proto);
1495 /* do the table insertion */
1496 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1501 static int find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1502 const heur_dtbl_entry_t *dtbl_entry_a = (const heur_dtbl_entry_t *) a;
1503 const heur_dtbl_entry_t *dtbl_entry_b = (const heur_dtbl_entry_t *) b;
1504 return (dtbl_entry_a->dissector == dtbl_entry_b->dissector) &&
1505 (dtbl_entry_a->protocol == dtbl_entry_b->protocol) ? 0 : 1;
1508 void heur_dissector_delete(const char *name, heur_dissector_t dissector, int proto) {
1509 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1510 heur_dtbl_entry_t dtbl_entry;
1511 GSList* found_entry;
1514 g_assert(sub_dissectors != NULL);
1516 dtbl_entry.dissector = dissector;
1518 dtbl_entry.protocol = find_protocol_by_id(proto);
1520 found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &dtbl_entry, find_matching_heur_dissector);
1523 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1524 g_free(g_slist_nth_data(found_entry, 1));
1525 g_slist_free_1(found_entry);
1531 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1532 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1535 const char *saved_proto;
1537 heur_dtbl_entry_t *dtbl_entry;
1538 guint16 saved_can_desegment;
1539 gint saved_layer_names_len = 0;
1541 /* can_desegment is set to 2 by anyone which offers this api/service.
1542 then everytime a subdissector is called it is decremented by one.
1543 thus only the subdissector immediately ontop of whoever offers this
1545 We save the current value of "can_desegment" for the
1546 benefit of TCP proxying dissectors such as SOCKS, so they
1547 can restore it and allow the dissectors they call to use
1548 the desegmentation service.
1550 saved_can_desegment=pinfo->can_desegment;
1551 pinfo->saved_can_desegment = saved_can_desegment;
1552 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1555 saved_proto = pinfo->current_proto;
1557 if (pinfo->layer_names != NULL)
1558 saved_layer_names_len = (gint) pinfo->layer_names->len;
1560 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1561 /* XXX - why set this now and above? */
1562 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1563 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1565 if (dtbl_entry->protocol != NULL &&
1566 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1568 * No - don't try this dissector.
1573 if (dtbl_entry->protocol != NULL) {
1574 pinfo->current_proto =
1575 proto_get_protocol_short_name(dtbl_entry->protocol);
1578 * Add the protocol name to the layers; we'll remove it
1579 * if the dissector fails.
1581 if (pinfo->layer_names) {
1582 if (pinfo->layer_names->len > 0)
1583 g_string_append(pinfo->layer_names, ":");
1584 g_string_append(pinfo->layer_names,
1585 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol)));
1588 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1589 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1590 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1591 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1592 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1596 EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned true",
1597 proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1600 * That dissector didn't accept the packet, so
1601 * remove its protocol's name from the list
1604 if (pinfo->layer_names != NULL) {
1605 g_string_truncate(pinfo->layer_names,
1606 saved_layer_names_len);
1610 pinfo->current_proto = saved_proto;
1611 pinfo->can_desegment=saved_can_desegment;
1616 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1618 /* Create our hash-of-lists if it doesn't already exist */
1619 if (heur_dissector_lists == NULL) {
1620 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1621 g_assert(heur_dissector_lists != NULL);
1624 /* Make sure the registration is unique */
1625 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1627 *sub_dissectors = NULL; /* initially empty */
1628 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1629 (gpointer) sub_dissectors);
1633 * Register dissectors by name; used if one dissector always calls a
1634 * particular dissector, or if it bases the decision of which dissector
1635 * to call on something other than a numerical value or on "try a bunch
1636 * of dissectors until one likes the packet".
1640 * List of registered dissectors.
1642 static GHashTable *registered_dissectors = NULL;
1644 /* Get the short name of the protocol for a dissector handle, if it has
1647 dissector_handle_get_short_name(dissector_handle_t handle)
1649 if (handle->protocol == NULL) {
1651 * No protocol (see, for example, the handle for
1652 * dissecting the set of protocols where the first
1653 * octet of the payload is an OSI network layer protocol
1658 return proto_get_protocol_short_name(handle->protocol);
1661 /* Get the index of the protocol for a dissector handle, if it has
1664 dissector_handle_get_protocol_index(dissector_handle_t handle)
1666 if (handle->protocol == NULL) {
1668 * No protocol (see, for example, the handle for
1669 * dissecting the set of protocols where the first
1670 * octet of the payload is an OSI network layer protocol
1675 return proto_get_id(handle->protocol);
1678 /* Find a registered dissector by name. */
1680 find_dissector(const char *name)
1682 g_assert(registered_dissectors != NULL);
1683 return g_hash_table_lookup(registered_dissectors, name);
1686 /* Create an anonymous handle for a dissector. */
1688 create_dissector_handle(dissector_t dissector, int proto)
1690 struct dissector_handle *handle;
1692 handle = g_malloc(sizeof (struct dissector_handle));
1693 handle->name = NULL;
1694 handle->is_new = FALSE;
1695 handle->dissector.old = dissector;
1696 handle->protocol = find_protocol_by_id(proto);
1702 new_create_dissector_handle(new_dissector_t dissector, int proto)
1704 struct dissector_handle *handle;
1706 handle = g_malloc(sizeof (struct dissector_handle));
1707 handle->name = NULL;
1708 handle->is_new = TRUE;
1709 handle->dissector.new = dissector;
1710 handle->protocol = find_protocol_by_id(proto);
1715 /* Register a dissector by name. */
1717 register_dissector(const char *name, dissector_t dissector, int proto)
1719 struct dissector_handle *handle;
1721 /* Create our hash table if it doesn't already exist */
1722 if (registered_dissectors == NULL) {
1723 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1724 g_assert(registered_dissectors != NULL);
1727 /* Make sure the registration is unique */
1728 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1730 handle = g_malloc(sizeof (struct dissector_handle));
1731 handle->name = name;
1732 handle->is_new = FALSE;
1733 handle->dissector.old = dissector;
1734 handle->protocol = find_protocol_by_id(proto);
1736 g_hash_table_insert(registered_dissectors, (gpointer)name,
1741 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1743 struct dissector_handle *handle;
1745 /* Create our hash table if it doesn't already exist */
1746 if (registered_dissectors == NULL) {
1747 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1748 g_assert(registered_dissectors != NULL);
1751 /* Make sure the registration is unique */
1752 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1754 handle = g_malloc(sizeof (struct dissector_handle));
1755 handle->name = name;
1756 handle->is_new = TRUE;
1757 handle->dissector.new = dissector;
1758 handle->protocol = find_protocol_by_id(proto);
1760 g_hash_table_insert(registered_dissectors, (gpointer)name,
1764 /* Call a dissector through a handle but if the dissector rejected it
1768 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
1769 packet_info *pinfo, proto_tree *tree)
1773 g_assert(handle != NULL);
1774 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1778 /* Call a dissector through a handle and if this fails call the "data"
1782 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1783 packet_info *pinfo, proto_tree *tree)
1787 ret = call_dissector_only(handle, tvb, pinfo, tree);
1790 * The protocol was disabled, or the dissector rejected
1791 * it. Just dissect this packet as data.
1793 g_assert(data_handle != NULL);
1794 g_assert(data_handle->protocol != NULL);
1795 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE);
1796 return tvb_length(tvb);
1802 * Dumps the "layer type"/"decode as" associations to stdout, similar
1803 * to the proto_registrar_dump_*() routines.
1805 * There is one record per line. The fields are tab-delimited.
1807 * Field 1 = layer type, e.g. "tcp.port"
1808 * Field 2 = selector in decimal
1809 * Field 3 = "decode as" name, e.g. "http"
1814 dissector_dump_decodes_display(const gchar *table_name,
1815 ftenum_t selector_type _U_, gpointer key, gpointer value,
1816 gpointer user_data _U_)
1818 guint32 selector = (guint32)(unsigned long) key;
1819 dissector_table_t sub_dissectors = find_dissector_table(table_name);
1820 dtbl_entry_t *dtbl_entry;
1821 dissector_handle_t handle;
1823 const gchar *decode_as;
1825 g_assert(sub_dissectors);
1826 switch (sub_dissectors->type) {
1833 g_assert(dtbl_entry);
1835 handle = dtbl_entry->current;
1838 proto_id = dissector_handle_get_protocol_index(handle);
1840 if (proto_id != -1) {
1841 decode_as = proto_get_protocol_filter_name(proto_id);
1842 g_assert(decode_as != NULL);
1843 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
1853 dissector_dump_decodes()
1855 dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
1858 static GPtrArray* post_dissectors = NULL;
1859 static guint num_of_postdissectors = 0;
1862 register_postdissector(dissector_handle_t handle)
1864 if (!post_dissectors)
1865 post_dissectors = g_ptr_array_new();
1867 g_ptr_array_add(post_dissectors, handle);
1868 num_of_postdissectors++;
1872 have_postdissector()
1875 dissector_handle_t handle;
1877 for(i = 0; i < num_of_postdissectors; i++) {
1878 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
1880 if (handle->protocol != NULL
1881 && proto_is_protocol_enabled(handle->protocol)) {
1882 /* We have at least one enabled postdissector */
1890 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1894 for(i = 0; i < num_of_postdissectors; i++) {
1895 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),