2 * Routines for packet disassembly
4 * $Id: packet.c,v 1.100 2003/12/29 22:44:50 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.match_string = NULL;
290 edt->pi.can_desegment = 0;
291 edt->pi.want_pdu_tracking = 0;
292 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
293 edt->pi.private_data = NULL;
302 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
303 /* Add this tvbuffer into the data_src list */
304 add_new_data_source(&edt->pi, edt->tvb, "Frame");
306 /* Even though dissect_frame() catches all the exceptions a
307 * sub-dissector can throw, dissect_frame() itself may throw
308 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
309 * in this function. */
310 if(frame_handle != NULL)
311 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
315 g_assert_not_reached();
317 CATCH(ReportedBoundsError) {
318 if(proto_malformed != -1){
319 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
320 "[Malformed Frame: Packet Length]" );
323 g_assert_not_reached();
328 fd->flags.visited = 1;
331 /*********************** code added for sub-dissector lookup *********************/
334 * An dissector handle.
336 struct dissector_handle {
337 const char *name; /* dissector name */
338 gboolean is_new; /* TRUE if new-style dissector */
343 protocol_t *protocol;
347 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
348 packet_info *pinfo, proto_tree *tree)
350 const char *saved_proto;
353 saved_proto = pinfo->current_proto;
355 if (handle->protocol != NULL) {
356 pinfo->current_proto =
357 proto_get_protocol_short_name(handle->protocol);
361 ret = (*handle->dissector.new)(tvb, pinfo, tree);
363 (*handle->dissector.old)(tvb, pinfo, tree);
364 ret = tvb_length(tvb);
367 * XXX - a tvbuff can have 0 bytes of data in
368 * it, so we have to make sure we don't return
375 pinfo->current_proto = saved_proto;
381 * Call a dissector through a handle.
382 * If the protocol for that handle isn't enabled, return 0 without
383 * calling the dissector.
384 * Otherwise, if the handle refers to a new-style dissector, call the
385 * dissector and return its return value, otherwise call it and return
386 * the length of the tvbuff pointed to by the argument.
389 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
390 packet_info *pinfo, proto_tree *tree)
392 const char *saved_proto;
393 guint16 saved_can_desegment;
395 gboolean save_writable;
396 volatile address save_dl_src;
397 volatile address save_dl_dst;
398 volatile address save_net_src;
399 volatile address save_net_dst;
400 volatile address save_src;
401 volatile address save_dst;
403 if (handle->protocol != NULL &&
404 !proto_is_protocol_enabled(handle->protocol)) {
406 * The protocol isn't enabled.
411 saved_proto = pinfo->current_proto;
412 saved_can_desegment = pinfo->can_desegment;
415 * can_desegment is set to 2 by anyone which offers the
416 * desegmentation api/service.
417 * Then everytime a subdissector is called it is decremented
419 * Thus only the subdissector immediately on top of whoever
420 * offers this service can use it.
421 * We save the current value of "can_desegment" for the
422 * benefit of TCP proxying dissectors such as SOCKS, so they
423 * can restore it and allow the dissectors they call to use
424 * the desegmentation service.
426 pinfo->saved_can_desegment = saved_can_desegment;
427 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
428 if (handle->protocol != NULL) {
429 pinfo->current_proto =
430 proto_get_protocol_short_name(handle->protocol);
433 if (pinfo->in_error_pkt) {
435 * This isn't a packet being transported inside
436 * the protocol whose dissector is calling us,
437 * it's a copy of a packet that caused an error
438 * in some protocol included in a packet that
439 * reports the error (e.g., an ICMP Unreachable
444 * Save the current state of the writability of
445 * the columns, and restore them after the
446 * dissector returns, so that the columns
447 * don't reflect the packet that got the error,
448 * they reflect the packet that reported the
451 save_writable = col_get_writable(pinfo->cinfo);
452 col_set_writable(pinfo->cinfo, FALSE);
453 save_dl_src = pinfo->dl_src;
454 save_dl_dst = pinfo->dl_dst;
455 save_net_src = pinfo->net_src;
456 save_net_dst = pinfo->net_dst;
457 save_src = pinfo->src;
458 save_dst = pinfo->dst;
460 /* Dissect the contained packet. */
462 ret = call_dissector_through_handle(handle, tvb,
467 * Restore the column writability and addresses.
469 col_set_writable(pinfo->cinfo, save_writable);
470 pinfo->dl_src = save_dl_src;
471 pinfo->dl_dst = save_dl_dst;
472 pinfo->net_src = save_net_src;
473 pinfo->net_dst = save_net_dst;
474 pinfo->src = save_src;
475 pinfo->dst = save_dst;
478 * Restore the current protocol, so any
479 * "Short Frame" indication reflects that
480 * protocol, not the protocol for the
481 * packet that got the error.
483 pinfo->current_proto = saved_proto;
486 * Restore the desegmentability state.
488 pinfo->can_desegment = saved_can_desegment;
491 * Rethrow the exception, so this will be
492 * reported as a short frame.
496 CATCH(ReportedBoundsError) {
501 col_set_writable(pinfo->cinfo, save_writable);
502 pinfo->dl_src = save_dl_src;
503 pinfo->dl_dst = save_dl_dst;
504 pinfo->net_src = save_net_src;
505 pinfo->net_dst = save_net_dst;
506 pinfo->src = save_src;
507 pinfo->dst = save_dst;
508 pinfo->want_pdu_tracking = 0;
511 * Just call the subdissector.
513 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
515 pinfo->current_proto = saved_proto;
516 pinfo->can_desegment = saved_can_desegment;
517 pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
522 * An entry in the hash table portion of a dissector table.
525 dissector_handle_t initial;
526 dissector_handle_t current;
532 * "hash_table" is a hash table, indexed by port number, supplying
533 * a "struct dtbl_entry"; it records what dissector is assigned to
534 * that port number in that table.
536 * "dissector_handles" is a list of all dissectors that *could* be
537 * used in that table; not all of them are necessarily in the table,
538 * as they may be for protocols that don't have a fixed port number.
540 * "ui_name" is the name the dissector table has in the user interface.
542 * "type" is a field type giving the width of the port number for that
545 * "base" is the base in which to display the port number for that
548 struct dissector_table {
549 GHashTable *hash_table;
550 GSList *dissector_handles;
556 static GHashTable *dissector_tables = NULL;
558 /* Finds a dissector table by table name. */
560 find_dissector_table(const char *name)
562 g_assert(dissector_tables);
563 return g_hash_table_lookup( dissector_tables, name );
566 /* Find an entry in a uint dissector table. */
567 static dtbl_entry_t *
568 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
570 switch (sub_dissectors->type) {
577 * You can do a port lookup in these tables.
583 * But you can't do a port lookup in any other types
586 g_assert_not_reached();
592 return g_hash_table_lookup(sub_dissectors->hash_table,
593 GUINT_TO_POINTER(pattern));
596 /* Add an entry to a uint dissector table. */
598 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
600 dissector_table_t sub_dissectors = find_dissector_table( name);
601 dtbl_entry_t *dtbl_entry;
604 g_assert( sub_dissectors);
605 switch (sub_dissectors->type) {
612 * You can do a port lookup in these tables.
618 * But you can't do a port lookup in any other types
621 g_assert_not_reached();
624 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
625 dtbl_entry->current = handle;
626 dtbl_entry->initial = dtbl_entry->current;
628 /* do the table insertion */
629 g_hash_table_insert( sub_dissectors->hash_table,
630 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
633 * Now add it to the list of handles that could be used with this
634 * table, because it *is* being used with this table.
636 dissector_add_handle(name, handle);
639 /* Delete the entry for a dissector in a uint dissector table
640 with a particular pattern. */
642 /* NOTE: this doesn't use the dissector call variable. It is included to */
643 /* be consistant with the dissector_add and more importantly to be used */
644 /* if the technique of adding a temporary dissector is implemented. */
645 /* If temporary dissectors are deleted, then the original dissector must */
648 dissector_delete(const char *name, guint32 pattern,
649 dissector_handle_t handle _U_)
651 dissector_table_t sub_dissectors = find_dissector_table( name);
652 dtbl_entry_t *dtbl_entry;
655 g_assert( sub_dissectors);
660 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
662 if (dtbl_entry != NULL) {
666 g_hash_table_remove(sub_dissectors->hash_table,
667 GUINT_TO_POINTER(pattern));
670 * Now free up the entry.
676 /* Change the entry for a dissector in a uint dissector table
677 with a particular pattern to use a new dissector handle. */
679 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
681 dissector_table_t sub_dissectors = find_dissector_table( name);
682 dtbl_entry_t *dtbl_entry;
685 g_assert( sub_dissectors);
688 * See if the entry already exists. If so, reuse it.
690 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
691 if (dtbl_entry != NULL) {
692 dtbl_entry->current = handle;
697 * Don't create an entry if there is no dissector handle - I.E. the
698 * user said not to decode something that wasn't being decoded
699 * in the first place.
704 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
705 dtbl_entry->initial = NULL;
706 dtbl_entry->current = handle;
708 /* do the table insertion */
709 g_hash_table_insert( sub_dissectors->hash_table,
710 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
713 /* Reset an entry in a uint dissector table to its initial value. */
715 dissector_reset(const char *name, guint32 pattern)
717 dissector_table_t sub_dissectors = find_dissector_table( name);
718 dtbl_entry_t *dtbl_entry;
721 g_assert( sub_dissectors);
726 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
728 if (dtbl_entry == NULL)
732 * Found - is there an initial value?
734 if (dtbl_entry->initial != NULL) {
735 dtbl_entry->current = dtbl_entry->initial;
737 g_hash_table_remove(sub_dissectors->hash_table,
738 GUINT_TO_POINTER(pattern));
743 /* Look for a given value in a given uint dissector table and, if found,
744 call the dissector with the arguments supplied, and return TRUE,
745 otherwise return FALSE. */
747 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
748 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
750 dtbl_entry_t *dtbl_entry;
751 struct dissector_handle *handle;
752 guint32 saved_match_port;
755 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
756 if (dtbl_entry != NULL) {
758 * Is there currently a dissector handle for this entry?
760 handle = dtbl_entry->current;
761 if (handle == NULL) {
763 * No - pretend this dissector didn't exist,
764 * so that other dissectors might have a chance
765 * to dissect this packet.
771 * Save the current value of "pinfo->match_port",
772 * set it to the port that matched, call the
773 * dissector, and restore "pinfo->match_port".
775 saved_match_port = pinfo->match_port;
776 pinfo->match_port = port;
777 ret = call_dissector_work(handle, tvb, pinfo, tree);
778 pinfo->match_port = saved_match_port;
781 * If a new-style dissector returned 0, it means that
782 * it didn't think this tvbuff represented a packet for
783 * its protocol, and didn't dissect anything.
785 * Old-style dissectors can't reject the packet.
787 * 0 is also returned if the protocol wasn't enabled.
789 * If the packet was rejected, we return FALSE, so that
790 * other dissectors might have a chance to dissect this
791 * packet, otherwise we return TRUE.
798 /* Look for a given value in a given uint dissector table and, if found,
799 return the dissector handle for that value. */
801 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
803 dtbl_entry_t *dtbl_entry;
805 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
806 if (dtbl_entry != NULL)
807 return dtbl_entry->current;
812 /* Find an entry in a string dissector table. */
813 static dtbl_entry_t *
814 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
816 switch (sub_dissectors->type) {
821 * You can do a string lookup in these tables.
827 * But you can't do a string lookup in any other types
830 g_assert_not_reached();
836 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
839 /* Add an entry to a string dissector table. */
841 dissector_add_string(const char *name, gchar *pattern,
842 dissector_handle_t handle)
844 dissector_table_t sub_dissectors = find_dissector_table( name);
845 dtbl_entry_t *dtbl_entry;
848 g_assert( sub_dissectors);
850 switch (sub_dissectors->type) {
855 * You can do a string lookup in these tables.
861 * But you can't do a string lookup in any other types
864 g_assert_not_reached();
867 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
868 dtbl_entry->current = handle;
869 dtbl_entry->initial = dtbl_entry->current;
871 /* do the table insertion */
872 g_hash_table_insert( sub_dissectors->hash_table, pattern,
873 (gpointer)dtbl_entry);
876 * Now add it to the list of handles that could be used with this
877 * table, because it *is* being used with this table.
879 dissector_add_handle(name, handle);
882 /* Delete the entry for a dissector in a string dissector table
883 with a particular pattern. */
885 /* NOTE: this doesn't use the dissector call variable. It is included to */
886 /* be consistant with the dissector_add_string and more importantly to */
887 /* be used if the technique of adding a temporary dissector is */
889 /* If temporary dissectors are deleted, then the original dissector must */
892 dissector_delete_string(const char *name, const gchar *pattern,
893 dissector_handle_t handle _U_)
895 dissector_table_t sub_dissectors = find_dissector_table( name);
896 dtbl_entry_t *dtbl_entry;
899 g_assert( sub_dissectors);
904 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
906 if (dtbl_entry != NULL) {
910 g_hash_table_remove(sub_dissectors->hash_table, pattern);
913 * Now free up the entry.
919 /* Change the entry for a dissector in a string dissector table
920 with a particular pattern to use a new dissector handle. */
922 dissector_change_string(const char *name, gchar *pattern,
923 dissector_handle_t handle)
925 dissector_table_t sub_dissectors = find_dissector_table( name);
926 dtbl_entry_t *dtbl_entry;
929 g_assert( sub_dissectors);
932 * See if the entry already exists. If so, reuse it.
934 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
935 if (dtbl_entry != NULL) {
936 dtbl_entry->current = handle;
941 * Don't create an entry if there is no dissector handle - I.E. the
942 * user said not to decode something that wasn't being decoded
943 * in the first place.
948 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
949 dtbl_entry->initial = NULL;
950 dtbl_entry->current = handle;
952 /* do the table insertion */
953 g_hash_table_insert( sub_dissectors->hash_table, pattern,
954 (gpointer)dtbl_entry);
957 /* Reset an entry in a string sub-dissector table to its initial value. */
959 dissector_reset_string(const char *name, const gchar *pattern)
961 dissector_table_t sub_dissectors = find_dissector_table( name);
962 dtbl_entry_t *dtbl_entry;
965 g_assert( sub_dissectors);
970 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
972 if (dtbl_entry == NULL)
976 * Found - is there an initial value?
978 if (dtbl_entry->initial != NULL) {
979 dtbl_entry->current = dtbl_entry->initial;
981 g_hash_table_remove(sub_dissectors->hash_table, pattern);
986 /* Look for a given string in a given dissector table and, if found, call
987 the dissector with the arguments supplied, and return TRUE, otherwise
990 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
991 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
993 dtbl_entry_t *dtbl_entry;
994 struct dissector_handle *handle;
996 const gchar *saved_match_string;
998 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
999 if (dtbl_entry != NULL) {
1001 * Is there currently a dissector handle for this entry?
1003 handle = dtbl_entry->current;
1004 if (handle == NULL) {
1006 * No - pretend this dissector didn't exist,
1007 * so that other dissectors might have a chance
1008 * to dissect this packet.
1014 * Save the current value of "pinfo->match_string",
1015 * set it to the string that matched, call the
1016 * dissector, and restore "pinfo->match_string".
1018 saved_match_string = pinfo->match_string;
1019 pinfo->match_string = string;
1020 ret = call_dissector_work(handle, tvb, pinfo, tree);
1021 pinfo->match_string = saved_match_string;
1024 * If a new-style dissector returned 0, it means that
1025 * it didn't think this tvbuff represented a packet for
1026 * its protocol, and didn't dissect anything.
1028 * Old-style dissectors can't reject the packet.
1030 * 0 is also returned if the protocol wasn't enabled.
1032 * If the packet was rejected, we return FALSE, so that
1033 * other dissectors might have a chance to dissect this
1034 * packet, otherwise we return TRUE.
1041 /* Look for a given value in a given string dissector table and, if found,
1042 return the dissector handle for that value. */
1044 dissector_get_string_handle(dissector_table_t sub_dissectors,
1045 const gchar *string)
1047 dtbl_entry_t *dtbl_entry;
1049 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1050 if (dtbl_entry != NULL)
1051 return dtbl_entry->current;
1057 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1059 return dtbl_entry->current;
1062 /* Add a handle to the list of handles that *could* be used with this
1063 table. That list is used by code in the UI. */
1065 dissector_add_handle(const char *name, dissector_handle_t handle)
1067 dissector_table_t sub_dissectors = find_dissector_table( name);
1071 g_assert(sub_dissectors != NULL);
1073 /* Is it already in this list? */
1074 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1075 if (entry != NULL) {
1077 * Yes - don't insert it again.
1082 /* Add it to the list. */
1083 sub_dissectors->dissector_handles =
1084 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1088 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1090 return dtbl_entry->initial;
1093 /**************************************************/
1095 /* Routines to walk dissector tables */
1097 /**************************************************/
1099 typedef struct dissector_foreach_info {
1100 gpointer caller_data;
1101 DATFunc caller_func;
1104 ftenum_t selector_type;
1105 } dissector_foreach_info_t;
1108 * Called for each entry in a dissector table.
1111 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1113 dissector_foreach_info_t *info;
1114 dtbl_entry_t *dtbl_entry;
1117 g_assert(user_data);
1120 if (dtbl_entry->current == NULL ||
1121 dtbl_entry->current->protocol == NULL) {
1123 * Either there is no dissector for this entry, or
1124 * the dissector doesn't have a protocol associated
1127 * XXX - should the latter check be done?
1133 info->caller_func(info->table_name, info->selector_type, key, value,
1138 * Called for each entry in the table of all dissector tables.
1141 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1143 dissector_table_t sub_dissectors;
1144 dissector_foreach_info_t *info;
1147 g_assert(user_data);
1149 sub_dissectors = value;
1151 info->table_name = (gchar*) key;
1152 info->selector_type = get_dissector_table_selector_type(info->table_name);
1153 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1157 * Walk all dissector tables calling a user supplied function on each
1161 dissector_all_tables_foreach (DATFunc func,
1164 dissector_foreach_info_t info;
1166 info.caller_data = user_data;
1167 info.caller_func = func;
1168 info.next_func = dissector_table_foreach_func;
1169 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1173 * Walk one dissector table's hash table calling a user supplied function
1177 dissector_table_foreach (char *name,
1181 dissector_foreach_info_t info;
1182 dissector_table_t sub_dissectors = find_dissector_table( name);
1184 info.table_name = name;
1185 info.selector_type = sub_dissectors->type;
1186 info.caller_func = func;
1187 info.caller_data = user_data;
1188 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1192 * Walk one dissector table's list of handles calling a user supplied
1193 * function on each entry.
1196 dissector_table_foreach_handle(char *name,
1197 DATFunc_handle func,
1200 dissector_table_t sub_dissectors = find_dissector_table( name);
1203 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1204 tmp = g_slist_next(tmp))
1205 func(name, tmp->data, user_data);
1209 * Called for each entry in a dissector table.
1212 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1214 dtbl_entry_t *dtbl_entry;
1215 dissector_foreach_info_t *info;
1218 g_assert(user_data);
1221 if (dtbl_entry->initial == dtbl_entry->current) {
1223 * Entry hasn't changed - don't call the function.
1229 info->caller_func(info->table_name, info->selector_type, key, value,
1234 * Walk all dissector tables calling a user supplied function only on
1235 * any entry that has been changed from its original state.
1238 dissector_all_tables_foreach_changed (DATFunc func,
1241 dissector_foreach_info_t info;
1243 info.caller_data = user_data;
1244 info.caller_func = func;
1245 info.next_func = dissector_table_foreach_changed_func;
1246 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1250 * Walk one dissector table calling a user supplied function only on
1251 * any entry that has been changed from its original state.
1254 dissector_table_foreach_changed (char *name,
1258 dissector_foreach_info_t info;
1259 dissector_table_t sub_dissectors = find_dissector_table( name);
1261 info.table_name = name;
1262 info.selector_type = sub_dissectors->type;
1263 info.caller_func = func;
1264 info.caller_data = user_data;
1265 g_hash_table_foreach(sub_dissectors->hash_table,
1266 dissector_table_foreach_changed_func, &info);
1269 typedef struct dissector_foreach_table_info {
1270 gpointer caller_data;
1271 DATFunc_table caller_func;
1272 } dissector_foreach_table_info_t;
1275 * Called for each entry in the table of all dissector tables.
1278 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1280 dissector_table_t table;
1281 dissector_foreach_table_info_t *info;
1285 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1289 * Walk all dissector tables calling a user supplied function on each
1293 dissector_all_tables_foreach_table (DATFunc_table func,
1296 dissector_foreach_table_info_t info;
1298 info.caller_data = user_data;
1299 info.caller_func = func;
1300 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1304 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
1307 dissector_table_t sub_dissectors;
1309 /* Create our hash-of-hashes if it doesn't already exist */
1310 if (!dissector_tables) {
1311 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1312 g_assert(dissector_tables);
1315 /* Make sure the registration is unique */
1316 g_assert(!g_hash_table_lookup( dissector_tables, name ));
1318 /* Create and register the dissector table for this name; returns */
1319 /* a pointer to the dissector table. */
1320 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1328 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1329 * so we use "g_direct_hash()" and "g_direct_equal()".
1331 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1337 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1342 g_assert_not_reached();
1344 sub_dissectors->dissector_handles = NULL;
1345 sub_dissectors->ui_name = ui_name;
1346 sub_dissectors->type = type;
1347 sub_dissectors->base = base;
1348 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1349 return sub_dissectors;
1353 get_dissector_table_ui_name(const char *name)
1355 dissector_table_t sub_dissectors = find_dissector_table( name);
1357 return sub_dissectors->ui_name;
1361 get_dissector_table_selector_type(const char *name)
1363 dissector_table_t sub_dissectors = find_dissector_table( name);
1365 return sub_dissectors->type;
1369 get_dissector_table_base(const char *name)
1371 dissector_table_t sub_dissectors = find_dissector_table( name);
1373 return sub_dissectors->base;
1376 static GHashTable *heur_dissector_lists = NULL;
1379 heur_dissector_t dissector;
1380 protocol_t *protocol;
1381 } heur_dtbl_entry_t;
1383 /* Finds a heuristic dissector table by field name. */
1384 static heur_dissector_list_t *
1385 find_heur_dissector_list(const char *name)
1387 g_assert(heur_dissector_lists != NULL);
1388 return g_hash_table_lookup(heur_dissector_lists, name);
1392 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1394 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1395 heur_dtbl_entry_t *dtbl_entry;
1398 g_assert(sub_dissectors != NULL);
1400 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1401 dtbl_entry->dissector = dissector;
1402 dtbl_entry->protocol = find_protocol_by_id(proto);
1404 /* do the table insertion */
1405 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1409 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1410 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1413 const char *saved_proto;
1415 heur_dtbl_entry_t *dtbl_entry;
1416 guint16 saved_can_desegment;
1418 /* can_desegment is set to 2 by anyone which offers this api/service.
1419 then everytime a subdissector is called it is decremented by one.
1420 thus only the subdissector immediately ontop of whoever offers this
1422 We save the current value of "can_desegment" for the
1423 benefit of TCP proxying dissectors such as SOCKS, so they
1424 can restore it and allow the dissectors they call to use
1425 the desegmentation service.
1427 saved_can_desegment=pinfo->can_desegment;
1428 pinfo->saved_can_desegment = saved_can_desegment;
1429 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1432 saved_proto = pinfo->current_proto;
1433 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1434 /* XXX - why set this now and above? */
1435 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1436 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1437 if (dtbl_entry->protocol != NULL &&
1438 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1440 * No - don't try this dissector.
1445 if (dtbl_entry->protocol != NULL) {
1446 pinfo->current_proto =
1447 proto_get_protocol_short_name(dtbl_entry->protocol);
1449 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1454 pinfo->current_proto = saved_proto;
1455 pinfo->can_desegment=saved_can_desegment;
1456 pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
1461 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1463 /* Create our hash-of-lists if it doesn't already exist */
1464 if (heur_dissector_lists == NULL) {
1465 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1466 g_assert(heur_dissector_lists != NULL);
1469 /* Make sure the registration is unique */
1470 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1472 *sub_dissectors = NULL; /* initially empty */
1473 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1474 (gpointer) sub_dissectors);
1478 * Register dissectors by name; used if one dissector always calls a
1479 * particular dissector, or if it bases the decision of which dissector
1480 * to call on something other than a numerical value or on "try a bunch
1481 * of dissectors until one likes the packet".
1485 * List of registered dissectors.
1487 static GHashTable *registered_dissectors = NULL;
1489 /* Get the short name of the protocol for a dissector handle. */
1491 dissector_handle_get_short_name(dissector_handle_t handle)
1493 return proto_get_protocol_short_name(handle->protocol);
1496 /* Get the index of the protocol for a dissector handle. */
1498 dissector_handle_get_protocol_index(dissector_handle_t handle)
1500 return proto_get_id(handle->protocol);
1503 /* Find a registered dissector by name. */
1505 find_dissector(const char *name)
1507 g_assert(registered_dissectors != NULL);
1508 return g_hash_table_lookup(registered_dissectors, name);
1511 /* Create an anonymous handle for a dissector. */
1513 create_dissector_handle(dissector_t dissector, int proto)
1515 struct dissector_handle *handle;
1517 handle = g_malloc(sizeof (struct dissector_handle));
1518 handle->name = NULL;
1519 handle->is_new = FALSE;
1520 handle->dissector.old = dissector;
1521 handle->protocol = find_protocol_by_id(proto);
1527 new_create_dissector_handle(new_dissector_t dissector, int proto)
1529 struct dissector_handle *handle;
1531 handle = g_malloc(sizeof (struct dissector_handle));
1532 handle->name = NULL;
1533 handle->is_new = TRUE;
1534 handle->dissector.new = dissector;
1535 handle->protocol = find_protocol_by_id(proto);
1540 /* Register a dissector by name. */
1542 register_dissector(const char *name, dissector_t dissector, int proto)
1544 struct dissector_handle *handle;
1546 /* Create our hash table if it doesn't already exist */
1547 if (registered_dissectors == NULL) {
1548 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1549 g_assert(registered_dissectors != NULL);
1552 /* Make sure the registration is unique */
1553 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1555 handle = g_malloc(sizeof (struct dissector_handle));
1556 handle->name = name;
1557 handle->is_new = FALSE;
1558 handle->dissector.old = dissector;
1559 handle->protocol = find_protocol_by_id(proto);
1561 g_hash_table_insert(registered_dissectors, (gpointer)name,
1566 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1568 struct dissector_handle *handle;
1570 /* Create our hash table if it doesn't already exist */
1571 if (registered_dissectors == NULL) {
1572 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1573 g_assert(registered_dissectors != NULL);
1576 /* Make sure the registration is unique */
1577 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1579 handle = g_malloc(sizeof (struct dissector_handle));
1580 handle->name = name;
1581 handle->is_new = TRUE;
1582 handle->dissector.new = dissector;
1583 handle->protocol = find_protocol_by_id(proto);
1585 g_hash_table_insert(registered_dissectors, (gpointer)name,
1589 /* Call a dissector through a handle. */
1591 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1592 packet_info *pinfo, proto_tree *tree)
1596 ret = call_dissector_work(handle, tvb, pinfo, tree);
1599 * The protocol was disabled, or the dissector rejected
1600 * it. Just dissect this packet as data.
1602 g_assert(data_handle != NULL);
1603 g_assert(data_handle->protocol != NULL);
1604 call_dissector(data_handle, tvb, pinfo, tree);
1605 return tvb_length(tvb);