2 * Routines for packet disassembly
4 * $Id: packet.c,v 1.98 2003/11/16 23:17:24 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 #include "timestamp.h"
45 #include "atalk-utils.h"
46 #include "ipv6-utils.h"
47 #include "sna-utils.h"
48 #include "osi-utils.h"
54 #include "epan_dissect.h"
56 #include "../reassemble.h"
58 static gint proto_malformed = -1;
59 static dissector_handle_t frame_handle = NULL;
60 static dissector_handle_t data_handle = NULL;
62 const true_false_string flags_set_truth = {
70 frame_handle = find_dissector("frame");
71 data_handle = find_dissector("data");
72 proto_malformed = proto_get_id_by_filter_name("malformed");
82 * Given a tvbuff, and a length from a packet header, adjust the length
83 * of the tvbuff to reflect the specified length.
86 set_actual_length(tvbuff_t *tvb, guint specified_len)
88 if (specified_len < tvb_reported_length(tvb)) {
89 /* Adjust the length of this tvbuff to include only the specified
92 The dissector above the one calling us (the dissector above is
93 probably us) may use that to determine how much of its packet
95 tvb_set_reported_length(tvb, specified_len);
99 /* Allow protocols to register "init" routines, which are called before
100 we make a pass through a capture file and dissect all its packets
101 (e.g., when we read in a new capture file, or run a "filter packets"
102 or "colorize packets" pass over the current capture file). */
103 static GSList *init_routines;
106 register_init_routine(void (*func)(void))
108 init_routines = g_slist_append(init_routines, func);
111 /* Initialize all data structures used for dissection. */
113 call_init_routine(gpointer routine, gpointer dummy _U_)
115 void (*func)(void) = routine;
121 init_dissection(void)
123 /* Initialize the table of conversations. */
124 epan_conversation_init();
126 /* Initialize the table of circuits. */
129 /* Initialize protocol-specific variables. */
130 g_slist_foreach(init_routines, &call_init_routine, NULL);
132 /* Initialize the common data structures for fragment reassembly.
133 Must be done *after* calling init routines, as those routines
134 may free up space for fragments, which they find by using the
135 data structures that "reassemble_init()" frees. */
139 /* Allow protocols to register a "cleanup" routine to be
140 * run after the initial sequential run through the packets.
141 * Note that the file can still be open after this; this is not
142 * the final cleanup. */
143 static GSList *postseq_cleanup_routines;
146 register_postseq_cleanup_routine(void (*func)(void))
148 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
152 /* Call all the registered "postseq_cleanup" routines. */
154 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
156 void (*func)(void) = routine;
162 postseq_cleanup_all_protocols(void)
164 g_slist_foreach(postseq_cleanup_routines,
165 &call_postseq_cleanup_routine, NULL);
168 /* Contains information about data sources. */
169 static GMemChunk *data_source_chunk = NULL;
172 * Add a new data source to the list of data sources for a frame, given
173 * the tvbuff for the data source and its name.
176 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, char *name)
180 if (data_source_chunk == NULL) {
181 data_source_chunk = g_mem_chunk_new("data_source_chunk",
182 sizeof (data_source), 10 * sizeof (data_source),
185 src = g_mem_chunk_alloc(data_source_chunk);
188 * XXX - if we require this argument to be a string constant,
189 * we don't need to allocate a buffer for a copy and make a
190 * copy, and wouldn't need to free the buffer, either.
192 src->name = g_strdup(name);
193 pinfo->data_src = g_slist_append(pinfo->data_src, src);
197 * Free up a frame's list of data sources.
200 free_data_sources(packet_info *pinfo)
205 for (src_le = pinfo->data_src; src_le != NULL; src_le = src_le->next) {
208 g_mem_chunk_free(data_source_chunk, src);
210 g_slist_free(pinfo->data_src);
211 pinfo->data_src = NULL;
214 /* Allow dissectors to register a "final_registration" routine
215 * that is run like the proto_register_XXX() routine, but the end
216 * end of the epan_init() function; that is, *after* all other
217 * subsystems, like dfilters, have finished initializing. This is
218 * useful for dissector registration routines which need to compile
219 * display filters. dfilters can't initialize itself until all protocols
220 * have registereed themselves. */
221 static GSList *final_registration_routines;
224 register_final_registration_routine(void (*func)(void))
226 final_registration_routines = g_slist_append(final_registration_routines,
230 /* Call all the registered "final_registration" routines. */
232 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
234 void (*func)(void) = routine;
240 final_registration_all_protocols(void)
242 g_slist_foreach(final_registration_routines,
243 &call_final_registration_routine, NULL);
247 /* Creates the top-most tvbuff and calls dissect_frame() */
249 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
250 const guchar *pd, frame_data *fd, column_info *cinfo)
254 edt->pi.current_proto = "<Missing Protocol Name>";
255 edt->pi.cinfo = cinfo;
257 edt->pi.pseudo_header = pseudo_header;
258 edt->pi.data_src = NULL;
259 edt->pi.dl_src.type = AT_NONE;
260 edt->pi.dl_src.len = 0;
261 edt->pi.dl_src.data = NULL;
262 edt->pi.dl_dst.type = AT_NONE;
263 edt->pi.dl_dst.len = 0;
264 edt->pi.dl_dst.data = NULL;
265 edt->pi.net_src.type = AT_NONE;
266 edt->pi.net_src.len = 0;
267 edt->pi.net_src.data = NULL;
268 edt->pi.net_dst.type = AT_NONE;
269 edt->pi.net_dst.len = 0;
270 edt->pi.net_dst.data = NULL;
271 edt->pi.src.type = AT_NONE;
273 edt->pi.src.data = NULL;
274 edt->pi.dst.type = AT_NONE;
276 edt->pi.dst.data = NULL;
277 edt->pi.ethertype = 0;
279 edt->pi.ipxptype = 0;
280 edt->pi.ctype = CT_NONE;
281 edt->pi.circuit_id = 0;
282 edt->pi.noreassembly_reason = "";
283 edt->pi.fragmented = FALSE;
284 edt->pi.in_error_pkt = FALSE;
285 edt->pi.ptype = PT_NONE;
287 edt->pi.destport = 0;
288 edt->pi.match_port = 0;
289 edt->pi.can_desegment = 0;
290 edt->pi.want_pdu_tracking = 0;
291 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
292 edt->pi.private_data = NULL;
301 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
302 /* Add this tvbuffer into the data_src list */
303 add_new_data_source(&edt->pi, edt->tvb, "Frame");
305 /* Even though dissect_frame() catches all the exceptions a
306 * sub-dissector can throw, dissect_frame() itself may throw
307 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
308 * in this function. */
309 if(frame_handle != NULL)
310 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
314 g_assert_not_reached();
316 CATCH(ReportedBoundsError) {
317 if(proto_malformed != -1){
318 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
319 "[Malformed Frame: Packet Length]" );
322 g_assert_not_reached();
327 fd->flags.visited = 1;
330 /*********************** code added for sub-dissector lookup *********************/
333 * An dissector handle.
335 struct dissector_handle {
336 const char *name; /* dissector name */
337 gboolean is_new; /* TRUE if new-style dissector */
342 protocol_t *protocol;
346 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
347 packet_info *pinfo, proto_tree *tree)
349 const char *saved_proto;
352 saved_proto = pinfo->current_proto;
354 if (handle->protocol != NULL) {
355 pinfo->current_proto =
356 proto_get_protocol_short_name(handle->protocol);
360 ret = (*handle->dissector.new)(tvb, pinfo, tree);
362 (*handle->dissector.old)(tvb, pinfo, tree);
363 ret = tvb_length(tvb);
366 * XXX - a tvbuff can have 0 bytes of data in
367 * it, so we have to make sure we don't return
374 pinfo->current_proto = saved_proto;
380 * Call a dissector through a handle.
381 * If the protocol for that handle isn't enabled, return 0 without
382 * calling the dissector.
383 * Otherwise, if the handle refers to a new-style dissector, call the
384 * dissector and return its return value, otherwise call it and return
385 * the length of the tvbuff pointed to by the argument.
388 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
389 packet_info *pinfo, proto_tree *tree)
391 const char *saved_proto;
392 guint16 saved_can_desegment;
394 gboolean save_writable;
395 volatile address save_dl_src;
396 volatile address save_dl_dst;
397 volatile address save_net_src;
398 volatile address save_net_dst;
399 volatile address save_src;
400 volatile address save_dst;
402 if (handle->protocol != NULL &&
403 !proto_is_protocol_enabled(handle->protocol)) {
405 * The protocol isn't enabled.
410 saved_proto = pinfo->current_proto;
411 saved_can_desegment = pinfo->can_desegment;
414 * can_desegment is set to 2 by anyone which offers the
415 * desegmentation api/service.
416 * Then everytime a subdissector is called it is decremented
418 * Thus only the subdissector immediately on top of whoever
419 * offers this service can use it.
421 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
422 if (handle->protocol != NULL) {
423 pinfo->current_proto =
424 proto_get_protocol_short_name(handle->protocol);
427 if (pinfo->in_error_pkt) {
429 * This isn't a packet being transported inside
430 * the protocol whose dissector is calling us,
431 * it's a copy of a packet that caused an error
432 * in some protocol included in a packet that
433 * reports the error (e.g., an ICMP Unreachable
438 * Save the current state of the writability of
439 * the columns, and restore them after the
440 * dissector returns, so that the columns
441 * don't reflect the packet that got the error,
442 * they reflect the packet that reported the
445 save_writable = col_get_writable(pinfo->cinfo);
446 col_set_writable(pinfo->cinfo, FALSE);
447 save_dl_src = pinfo->dl_src;
448 save_dl_dst = pinfo->dl_dst;
449 save_net_src = pinfo->net_src;
450 save_net_dst = pinfo->net_dst;
451 save_src = pinfo->src;
452 save_dst = pinfo->dst;
454 /* Dissect the contained packet. */
456 ret = call_dissector_through_handle(handle, tvb,
461 * Restore the column writability and addresses.
463 col_set_writable(pinfo->cinfo, save_writable);
464 pinfo->dl_src = save_dl_src;
465 pinfo->dl_dst = save_dl_dst;
466 pinfo->net_src = save_net_src;
467 pinfo->net_dst = save_net_dst;
468 pinfo->src = save_src;
469 pinfo->dst = save_dst;
472 * Restore the current protocol, so any
473 * "Short Frame" indication reflects that
474 * protocol, not the protocol for the
475 * packet that got the error.
477 pinfo->current_proto = saved_proto;
480 * Restore the desegmentability state.
482 pinfo->can_desegment = saved_can_desegment;
485 * Rethrow the exception, so this will be
486 * reported as a short frame.
490 CATCH(ReportedBoundsError) {
495 col_set_writable(pinfo->cinfo, save_writable);
496 pinfo->dl_src = save_dl_src;
497 pinfo->dl_dst = save_dl_dst;
498 pinfo->net_src = save_net_src;
499 pinfo->net_dst = save_net_dst;
500 pinfo->src = save_src;
501 pinfo->dst = save_dst;
502 pinfo->want_pdu_tracking = 0;
505 * Just call the subdissector.
507 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
509 pinfo->current_proto = saved_proto;
510 pinfo->can_desegment = saved_can_desegment;
511 pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
516 * An entry in the hash table portion of a dissector table.
519 dissector_handle_t initial;
520 dissector_handle_t current;
526 * "hash_table" is a hash table, indexed by port number, supplying
527 * a "struct dtbl_entry"; it records what dissector is assigned to
528 * that port number in that table.
530 * "dissector_handles" is a list of all dissectors that *could* be
531 * used in that table; not all of them are necessarily in the table,
532 * as they may be for protocols that don't have a fixed port number.
534 * "ui_name" is the name the dissector table has in the user interface.
536 * "type" is a field type giving the width of the port number for that
539 * "base" is the base in which to display the port number for that
542 struct dissector_table {
543 GHashTable *hash_table;
544 GSList *dissector_handles;
550 static GHashTable *dissector_tables = NULL;
552 /* Finds a dissector table by table name. */
554 find_dissector_table(const char *name)
556 g_assert(dissector_tables);
557 return g_hash_table_lookup( dissector_tables, name );
560 /* Find an entry in a uint dissector table. */
561 static dtbl_entry_t *
562 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
564 switch (sub_dissectors->type) {
571 * You can do a port lookup in these tables.
577 * But you can't do a port lookup in any other types
580 g_assert_not_reached();
586 return g_hash_table_lookup(sub_dissectors->hash_table,
587 GUINT_TO_POINTER(pattern));
590 /* Add an entry to a uint dissector table. */
592 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
594 dissector_table_t sub_dissectors = find_dissector_table( name);
595 dtbl_entry_t *dtbl_entry;
598 g_assert( sub_dissectors);
599 switch (sub_dissectors->type) {
606 * You can do a port lookup in these tables.
612 * But you can't do a port lookup in any other types
615 g_assert_not_reached();
618 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
619 dtbl_entry->current = handle;
620 dtbl_entry->initial = dtbl_entry->current;
622 /* do the table insertion */
623 g_hash_table_insert( sub_dissectors->hash_table,
624 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
627 * Now add it to the list of handles that could be used with this
628 * table, because it *is* being used with this table.
630 dissector_add_handle(name, handle);
633 /* Delete the entry for a dissector in a uint dissector table
634 with a particular pattern. */
636 /* NOTE: this doesn't use the dissector call variable. It is included to */
637 /* be consistant with the dissector_add and more importantly to be used */
638 /* if the technique of adding a temporary dissector is implemented. */
639 /* If temporary dissectors are deleted, then the original dissector must */
642 dissector_delete(const char *name, guint32 pattern,
643 dissector_handle_t handle _U_)
645 dissector_table_t sub_dissectors = find_dissector_table( name);
646 dtbl_entry_t *dtbl_entry;
649 g_assert( sub_dissectors);
654 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
656 if (dtbl_entry != NULL) {
660 g_hash_table_remove(sub_dissectors->hash_table,
661 GUINT_TO_POINTER(pattern));
664 * Now free up the entry.
670 /* Change the entry for a dissector in a uint dissector table
671 with a particular pattern to use a new dissector handle. */
673 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
675 dissector_table_t sub_dissectors = find_dissector_table( name);
676 dtbl_entry_t *dtbl_entry;
679 g_assert( sub_dissectors);
682 * See if the entry already exists. If so, reuse it.
684 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
685 if (dtbl_entry != NULL) {
686 dtbl_entry->current = handle;
691 * Don't create an entry if there is no dissector handle - I.E. the
692 * user said not to decode something that wasn't being decoded
693 * in the first place.
698 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
699 dtbl_entry->initial = NULL;
700 dtbl_entry->current = handle;
702 /* do the table insertion */
703 g_hash_table_insert( sub_dissectors->hash_table,
704 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
707 /* Reset an entry in a uint dissector table to its initial value. */
709 dissector_reset(const char *name, guint32 pattern)
711 dissector_table_t sub_dissectors = find_dissector_table( name);
712 dtbl_entry_t *dtbl_entry;
715 g_assert( sub_dissectors);
720 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
722 if (dtbl_entry == NULL)
726 * Found - is there an initial value?
728 if (dtbl_entry->initial != NULL) {
729 dtbl_entry->current = dtbl_entry->initial;
731 g_hash_table_remove(sub_dissectors->hash_table,
732 GUINT_TO_POINTER(pattern));
737 /* Look for a given value in a given uint dissector table and, if found,
738 call the dissector with the arguments supplied, and return TRUE,
739 otherwise return FALSE. */
741 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
742 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
744 dtbl_entry_t *dtbl_entry;
745 struct dissector_handle *handle;
746 guint32 saved_match_port;
749 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
750 if (dtbl_entry != NULL) {
752 * Is there currently a dissector handle for this entry?
754 handle = dtbl_entry->current;
755 if (handle == NULL) {
757 * No - pretend this dissector didn't exist,
758 * so that other dissectors might have a chance
759 * to dissect this packet.
765 * Save the current value of "pinfo->match_port",
766 * set it to the port that matched, call the
767 * dissector, and restore "pinfo->match_port".
769 saved_match_port = pinfo->match_port;
770 pinfo->match_port = port;
771 ret = call_dissector_work(handle, tvb, pinfo, tree);
772 pinfo->match_port = saved_match_port;
775 * If a new-style dissector returned 0, it means that
776 * it didn't think this tvbuff represented a packet for
777 * its protocol, and didn't dissect anything.
779 * Old-style dissectors can't reject the packet.
781 * 0 is also returned if the protocol wasn't enabled.
783 * If the packet was rejected, we return FALSE, so that
784 * other dissectors might have a chance to dissect this
785 * packet, otherwise we return TRUE.
792 /* Look for a given value in a given uint dissector table and, if found,
793 return the dissector handle for that value. */
795 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
797 dtbl_entry_t *dtbl_entry;
799 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
800 if (dtbl_entry != NULL)
801 return dtbl_entry->current;
806 /* Find an entry in a string dissector table. */
807 static dtbl_entry_t *
808 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
810 switch (sub_dissectors->type) {
815 * You can do a string lookup in these tables.
821 * But you can't do a string lookup in any other types
824 g_assert_not_reached();
830 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
833 /* Add an entry to a string dissector table. */
835 dissector_add_string(const char *name, gchar *pattern,
836 dissector_handle_t handle)
838 dissector_table_t sub_dissectors = find_dissector_table( name);
839 dtbl_entry_t *dtbl_entry;
842 g_assert( sub_dissectors);
844 switch (sub_dissectors->type) {
849 * You can do a string lookup in these tables.
855 * But you can't do a string lookup in any other types
858 g_assert_not_reached();
861 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
862 dtbl_entry->current = handle;
863 dtbl_entry->initial = dtbl_entry->current;
865 /* do the table insertion */
866 g_hash_table_insert( sub_dissectors->hash_table, pattern,
867 (gpointer)dtbl_entry);
870 * Now add it to the list of handles that could be used with this
871 * table, because it *is* being used with this table.
873 dissector_add_handle(name, handle);
876 /* Delete the entry for a dissector in a string dissector table
877 with a particular pattern. */
879 /* NOTE: this doesn't use the dissector call variable. It is included to */
880 /* be consistant with the dissector_add_string and more importantly to */
881 /* be used if the technique of adding a temporary dissector is */
883 /* If temporary dissectors are deleted, then the original dissector must */
886 dissector_delete_string(const char *name, const gchar *pattern,
887 dissector_handle_t handle _U_)
889 dissector_table_t sub_dissectors = find_dissector_table( name);
890 dtbl_entry_t *dtbl_entry;
893 g_assert( sub_dissectors);
898 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
900 if (dtbl_entry != NULL) {
904 g_hash_table_remove(sub_dissectors->hash_table, pattern);
907 * Now free up the entry.
913 /* Change the entry for a dissector in a string dissector table
914 with a particular pattern to use a new dissector handle. */
916 dissector_change_string(const char *name, gchar *pattern,
917 dissector_handle_t handle)
919 dissector_table_t sub_dissectors = find_dissector_table( name);
920 dtbl_entry_t *dtbl_entry;
923 g_assert( sub_dissectors);
926 * See if the entry already exists. If so, reuse it.
928 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
929 if (dtbl_entry != NULL) {
930 dtbl_entry->current = handle;
935 * Don't create an entry if there is no dissector handle - I.E. the
936 * user said not to decode something that wasn't being decoded
937 * in the first place.
942 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
943 dtbl_entry->initial = NULL;
944 dtbl_entry->current = handle;
946 /* do the table insertion */
947 g_hash_table_insert( sub_dissectors->hash_table, pattern,
948 (gpointer)dtbl_entry);
951 /* Reset an entry in a string sub-dissector table to its initial value. */
953 dissector_reset_string(const char *name, const gchar *pattern)
955 dissector_table_t sub_dissectors = find_dissector_table( name);
956 dtbl_entry_t *dtbl_entry;
959 g_assert( sub_dissectors);
964 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
966 if (dtbl_entry == NULL)
970 * Found - is there an initial value?
972 if (dtbl_entry->initial != NULL) {
973 dtbl_entry->current = dtbl_entry->initial;
975 g_hash_table_remove(sub_dissectors->hash_table, pattern);
980 /* Look for a given string in a given dissector table and, if found, call
981 the dissector with the arguments supplied, and return TRUE, otherwise
984 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
985 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
987 dtbl_entry_t *dtbl_entry;
988 struct dissector_handle *handle;
991 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
992 if (dtbl_entry != NULL) {
994 * Is there currently a dissector handle for this entry?
996 handle = dtbl_entry->current;
997 if (handle == NULL) {
999 * No - pretend this dissector didn't exist,
1000 * so that other dissectors might have a chance
1001 * to dissect this packet.
1006 ret = call_dissector_work(handle, tvb, pinfo, tree);
1009 * If a new-style dissector returned 0, it means that
1010 * it didn't think this tvbuff represented a packet for
1011 * its protocol, and didn't dissect anything.
1013 * Old-style dissectors can't reject the packet.
1015 * 0 is also returned if the protocol wasn't enabled.
1017 * If the packet was rejected, we return FALSE, so that
1018 * other dissectors might have a chance to dissect this
1019 * packet, otherwise we return TRUE.
1026 /* Look for a given value in a given string dissector table and, if found,
1027 return the dissector handle for that value. */
1029 dissector_get_string_handle(dissector_table_t sub_dissectors,
1030 const gchar *string)
1032 dtbl_entry_t *dtbl_entry;
1034 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1035 if (dtbl_entry != NULL)
1036 return dtbl_entry->current;
1042 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1044 return dtbl_entry->current;
1047 /* Add a handle to the list of handles that *could* be used with this
1048 table. That list is used by code in the UI. */
1050 dissector_add_handle(const char *name, dissector_handle_t handle)
1052 dissector_table_t sub_dissectors = find_dissector_table( name);
1056 g_assert(sub_dissectors != NULL);
1058 /* Is it already in this list? */
1059 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1060 if (entry != NULL) {
1062 * Yes - don't insert it again.
1067 /* Add it to the list. */
1068 sub_dissectors->dissector_handles =
1069 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1073 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1075 return dtbl_entry->initial;
1078 /**************************************************/
1080 /* Routines to walk dissector tables */
1082 /**************************************************/
1084 typedef struct dissector_foreach_info {
1085 gpointer caller_data;
1086 DATFunc caller_func;
1089 ftenum_t selector_type;
1090 } dissector_foreach_info_t;
1093 * Called for each entry in a dissector table.
1096 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1098 dissector_foreach_info_t *info;
1099 dtbl_entry_t *dtbl_entry;
1102 g_assert(user_data);
1105 if (dtbl_entry->current == NULL ||
1106 dtbl_entry->current->protocol == NULL) {
1108 * Either there is no dissector for this entry, or
1109 * the dissector doesn't have a protocol associated
1112 * XXX - should the latter check be done?
1118 info->caller_func(info->table_name, info->selector_type, key, value,
1123 * Called for each entry in the table of all dissector tables.
1126 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1128 dissector_table_t sub_dissectors;
1129 dissector_foreach_info_t *info;
1132 g_assert(user_data);
1134 sub_dissectors = value;
1136 info->table_name = (gchar*) key;
1137 info->selector_type = get_dissector_table_selector_type(info->table_name);
1138 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1142 * Walk all dissector tables calling a user supplied function on each
1146 dissector_all_tables_foreach (DATFunc func,
1149 dissector_foreach_info_t info;
1151 info.caller_data = user_data;
1152 info.caller_func = func;
1153 info.next_func = dissector_table_foreach_func;
1154 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1158 * Walk one dissector table's hash table calling a user supplied function
1162 dissector_table_foreach (char *name,
1166 dissector_foreach_info_t info;
1167 dissector_table_t sub_dissectors = find_dissector_table( name);
1169 info.table_name = name;
1170 info.selector_type = sub_dissectors->type;
1171 info.caller_func = func;
1172 info.caller_data = user_data;
1173 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1177 * Walk one dissector table's list of handles calling a user supplied
1178 * function on each entry.
1181 dissector_table_foreach_handle(char *name,
1182 DATFunc_handle func,
1185 dissector_table_t sub_dissectors = find_dissector_table( name);
1188 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1189 tmp = g_slist_next(tmp))
1190 func(name, tmp->data, user_data);
1194 * Called for each entry in a dissector table.
1197 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1199 dtbl_entry_t *dtbl_entry;
1200 dissector_foreach_info_t *info;
1203 g_assert(user_data);
1206 if (dtbl_entry->initial == dtbl_entry->current) {
1208 * Entry hasn't changed - don't call the function.
1214 info->caller_func(info->table_name, info->selector_type, key, value,
1219 * Walk all dissector tables calling a user supplied function only on
1220 * any entry that has been changed from its original state.
1223 dissector_all_tables_foreach_changed (DATFunc func,
1226 dissector_foreach_info_t info;
1228 info.caller_data = user_data;
1229 info.caller_func = func;
1230 info.next_func = dissector_table_foreach_changed_func;
1231 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1235 * Walk one dissector table calling a user supplied function only on
1236 * any entry that has been changed from its original state.
1239 dissector_table_foreach_changed (char *name,
1243 dissector_foreach_info_t info;
1244 dissector_table_t sub_dissectors = find_dissector_table( name);
1246 info.table_name = name;
1247 info.selector_type = sub_dissectors->type;
1248 info.caller_func = func;
1249 info.caller_data = user_data;
1250 g_hash_table_foreach(sub_dissectors->hash_table,
1251 dissector_table_foreach_changed_func, &info);
1254 typedef struct dissector_foreach_table_info {
1255 gpointer caller_data;
1256 DATFunc_table caller_func;
1257 } dissector_foreach_table_info_t;
1260 * Called for each entry in the table of all dissector tables.
1263 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1265 dissector_table_t table;
1266 dissector_foreach_table_info_t *info;
1270 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1274 * Walk all dissector tables calling a user supplied function on each
1278 dissector_all_tables_foreach_table (DATFunc_table func,
1281 dissector_foreach_table_info_t info;
1283 info.caller_data = user_data;
1284 info.caller_func = func;
1285 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1289 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
1292 dissector_table_t sub_dissectors;
1294 /* Create our hash-of-hashes if it doesn't already exist */
1295 if (!dissector_tables) {
1296 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1297 g_assert(dissector_tables);
1300 /* Make sure the registration is unique */
1301 g_assert(!g_hash_table_lookup( dissector_tables, name ));
1303 /* Create and register the dissector table for this name; returns */
1304 /* a pointer to the dissector table. */
1305 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1313 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1314 * so we use "g_direct_hash()" and "g_direct_equal()".
1316 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1322 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1327 g_assert_not_reached();
1329 sub_dissectors->dissector_handles = NULL;
1330 sub_dissectors->ui_name = ui_name;
1331 sub_dissectors->type = type;
1332 sub_dissectors->base = base;
1333 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1334 return sub_dissectors;
1338 get_dissector_table_ui_name(const char *name)
1340 dissector_table_t sub_dissectors = find_dissector_table( name);
1342 return sub_dissectors->ui_name;
1346 get_dissector_table_selector_type(const char *name)
1348 dissector_table_t sub_dissectors = find_dissector_table( name);
1350 return sub_dissectors->type;
1354 get_dissector_table_base(const char *name)
1356 dissector_table_t sub_dissectors = find_dissector_table( name);
1358 return sub_dissectors->base;
1361 static GHashTable *heur_dissector_lists = NULL;
1364 heur_dissector_t dissector;
1365 protocol_t *protocol;
1366 } heur_dtbl_entry_t;
1368 /* Finds a heuristic dissector table by field name. */
1369 static heur_dissector_list_t *
1370 find_heur_dissector_list(const char *name)
1372 g_assert(heur_dissector_lists != NULL);
1373 return g_hash_table_lookup(heur_dissector_lists, name);
1377 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1379 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1380 heur_dtbl_entry_t *dtbl_entry;
1383 g_assert(sub_dissectors != NULL);
1385 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1386 dtbl_entry->dissector = dissector;
1387 dtbl_entry->protocol = find_protocol_by_id(proto);
1389 /* do the table insertion */
1390 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1394 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1395 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1398 const char *saved_proto;
1400 heur_dtbl_entry_t *dtbl_entry;
1401 guint16 saved_can_desegment;
1403 /* can_desegment is set to 2 by anyone which offers this api/service.
1404 then everytime a subdissector is called it is decremented by one.
1405 thus only the subdissector immediately ontop of whoever offers this
1408 saved_can_desegment=pinfo->can_desegment;
1409 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1412 saved_proto = pinfo->current_proto;
1413 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1414 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1415 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1416 if (dtbl_entry->protocol != NULL &&
1417 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1419 * No - don't try this dissector.
1424 if (dtbl_entry->protocol != NULL) {
1425 pinfo->current_proto =
1426 proto_get_protocol_short_name(dtbl_entry->protocol);
1428 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1433 pinfo->current_proto = saved_proto;
1434 pinfo->can_desegment=saved_can_desegment;
1435 pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
1440 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1442 /* Create our hash-of-lists if it doesn't already exist */
1443 if (heur_dissector_lists == NULL) {
1444 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1445 g_assert(heur_dissector_lists != NULL);
1448 /* Make sure the registration is unique */
1449 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1451 *sub_dissectors = NULL; /* initially empty */
1452 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1453 (gpointer) sub_dissectors);
1457 * Register dissectors by name; used if one dissector always calls a
1458 * particular dissector, or if it bases the decision of which dissector
1459 * to call on something other than a numerical value or on "try a bunch
1460 * of dissectors until one likes the packet".
1464 * List of registered dissectors.
1466 static GHashTable *registered_dissectors = NULL;
1468 /* Get the short name of the protocol for a dissector handle. */
1470 dissector_handle_get_short_name(dissector_handle_t handle)
1472 return proto_get_protocol_short_name(handle->protocol);
1475 /* Get the index of the protocol for a dissector handle. */
1477 dissector_handle_get_protocol_index(dissector_handle_t handle)
1479 return proto_get_id(handle->protocol);
1482 /* Find a registered dissector by name. */
1484 find_dissector(const char *name)
1486 g_assert(registered_dissectors != NULL);
1487 return g_hash_table_lookup(registered_dissectors, name);
1490 /* Create an anonymous handle for a dissector. */
1492 create_dissector_handle(dissector_t dissector, int proto)
1494 struct dissector_handle *handle;
1496 handle = g_malloc(sizeof (struct dissector_handle));
1497 handle->name = NULL;
1498 handle->is_new = FALSE;
1499 handle->dissector.old = dissector;
1500 handle->protocol = find_protocol_by_id(proto);
1506 new_create_dissector_handle(new_dissector_t dissector, int proto)
1508 struct dissector_handle *handle;
1510 handle = g_malloc(sizeof (struct dissector_handle));
1511 handle->name = NULL;
1512 handle->is_new = TRUE;
1513 handle->dissector.new = dissector;
1514 handle->protocol = find_protocol_by_id(proto);
1519 /* Register a dissector by name. */
1521 register_dissector(const char *name, dissector_t dissector, int proto)
1523 struct dissector_handle *handle;
1525 /* Create our hash table if it doesn't already exist */
1526 if (registered_dissectors == NULL) {
1527 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1528 g_assert(registered_dissectors != NULL);
1531 /* Make sure the registration is unique */
1532 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1534 handle = g_malloc(sizeof (struct dissector_handle));
1535 handle->name = name;
1536 handle->is_new = FALSE;
1537 handle->dissector.old = dissector;
1538 handle->protocol = find_protocol_by_id(proto);
1540 g_hash_table_insert(registered_dissectors, (gpointer)name,
1545 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1547 struct dissector_handle *handle;
1549 /* Create our hash table if it doesn't already exist */
1550 if (registered_dissectors == NULL) {
1551 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1552 g_assert(registered_dissectors != NULL);
1555 /* Make sure the registration is unique */
1556 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1558 handle = g_malloc(sizeof (struct dissector_handle));
1559 handle->name = name;
1560 handle->is_new = TRUE;
1561 handle->dissector.new = dissector;
1562 handle->protocol = find_protocol_by_id(proto);
1564 g_hash_table_insert(registered_dissectors, (gpointer)name,
1568 /* Call a dissector through a handle. */
1570 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1571 packet_info *pinfo, proto_tree *tree)
1575 ret = call_dissector_work(handle, tvb, pinfo, tree);
1578 * The protocol was disabled, or the dissector rejected
1579 * it. Just dissect this packet as data.
1581 g_assert(data_handle != NULL);
1582 g_assert(data_handle->protocol != NULL);
1583 call_dissector(data_handle, tvb, pinfo, tree);
1584 return tvb_length(tvb);