2 * Routines for packet disassembly
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, (gpointer)func);
111 typedef void (*void_func_t)(void);
113 /* Initialize all data structures used for dissection. */
115 call_init_routine(gpointer routine, gpointer dummy _U_)
117 void_func_t func = (void_func_t)routine;
122 init_dissection(void)
124 /* Initialize the table of conversations. */
125 epan_conversation_init();
127 /* Initialize the table of circuits. */
130 /* Initialize protocol-specific variables. */
131 g_slist_foreach(init_routines, &call_init_routine, NULL);
133 /* Initialize the common data structures for fragment reassembly.
134 Must be done *after* calling init routines, as those routines
135 may free up space for fragments, which they find by using the
136 data structures that "reassemble_init()" frees. */
140 /* Allow protocols to register a "cleanup" routine to be
141 * run after the initial sequential run through the packets.
142 * Note that the file can still be open after this; this is not
143 * the final cleanup. */
144 static GSList *postseq_cleanup_routines;
147 register_postseq_cleanup_routine(void_func_t func)
149 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
153 /* Call all the registered "postseq_cleanup" routines. */
155 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
157 void_func_t func = (void_func_t)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_printf("%s (%u bytes)", name, tvb_length(tvb));
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_t func = (void_func_t)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;
521 * An entry in the hash table portion of a dissector table.
524 dissector_handle_t initial;
525 dissector_handle_t current;
531 * "hash_table" is a hash table, indexed by port number, supplying
532 * a "struct dtbl_entry"; it records what dissector is assigned to
533 * that port number in that table.
535 * "dissector_handles" is a list of all dissectors that *could* be
536 * used in that table; not all of them are necessarily in the table,
537 * as they may be for protocols that don't have a fixed port number.
539 * "ui_name" is the name the dissector table has in the user interface.
541 * "type" is a field type giving the width of the port number for that
544 * "base" is the base in which to display the port number for that
547 struct dissector_table {
548 GHashTable *hash_table;
549 GSList *dissector_handles;
555 static GHashTable *dissector_tables = NULL;
557 /* Finds a dissector table by table name. */
559 find_dissector_table(const char *name)
561 g_assert(dissector_tables);
562 return g_hash_table_lookup( dissector_tables, name );
565 /* Find an entry in a uint dissector table. */
566 static dtbl_entry_t *
567 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
569 switch (sub_dissectors->type) {
576 * You can do a port lookup in these tables.
582 * But you can't do a port lookup in any other types
585 g_assert_not_reached();
591 return g_hash_table_lookup(sub_dissectors->hash_table,
592 GUINT_TO_POINTER(pattern));
595 /* Add an entry to a uint dissector table. */
597 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
599 dissector_table_t sub_dissectors = find_dissector_table( name);
600 dtbl_entry_t *dtbl_entry;
603 g_assert( sub_dissectors);
604 switch (sub_dissectors->type) {
611 * You can do a port lookup in these tables.
617 * But you can't do a port lookup in any other types
620 g_assert_not_reached();
623 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
624 dtbl_entry->current = handle;
625 dtbl_entry->initial = dtbl_entry->current;
627 /* do the table insertion */
628 g_hash_table_insert( sub_dissectors->hash_table,
629 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
632 * Now add it to the list of handles that could be used with this
633 * table, because it *is* being used with this table.
635 dissector_add_handle(name, handle);
638 /* Delete the entry for a dissector in a uint dissector table
639 with a particular pattern. */
641 /* NOTE: this doesn't use the dissector call variable. It is included to */
642 /* be consistant with the dissector_add and more importantly to be used */
643 /* if the technique of adding a temporary dissector is implemented. */
644 /* If temporary dissectors are deleted, then the original dissector must */
647 dissector_delete(const char *name, guint32 pattern,
648 dissector_handle_t handle _U_)
650 dissector_table_t sub_dissectors = find_dissector_table( name);
651 dtbl_entry_t *dtbl_entry;
654 g_assert( sub_dissectors);
659 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
661 if (dtbl_entry != NULL) {
665 g_hash_table_remove(sub_dissectors->hash_table,
666 GUINT_TO_POINTER(pattern));
669 * Now free up the entry.
675 /* Change the entry for a dissector in a uint dissector table
676 with a particular pattern to use a new dissector handle. */
678 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
680 dissector_table_t sub_dissectors = find_dissector_table( name);
681 dtbl_entry_t *dtbl_entry;
684 g_assert( sub_dissectors);
687 * See if the entry already exists. If so, reuse it.
689 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
690 if (dtbl_entry != NULL) {
691 dtbl_entry->current = handle;
696 * Don't create an entry if there is no dissector handle - I.E. the
697 * user said not to decode something that wasn't being decoded
698 * in the first place.
703 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
704 dtbl_entry->initial = NULL;
705 dtbl_entry->current = handle;
707 /* do the table insertion */
708 g_hash_table_insert( sub_dissectors->hash_table,
709 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
712 /* Reset an entry in a uint dissector table to its initial value. */
714 dissector_reset(const char *name, guint32 pattern)
716 dissector_table_t sub_dissectors = find_dissector_table( name);
717 dtbl_entry_t *dtbl_entry;
720 g_assert( sub_dissectors);
725 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
727 if (dtbl_entry == NULL)
731 * Found - is there an initial value?
733 if (dtbl_entry->initial != NULL) {
734 dtbl_entry->current = dtbl_entry->initial;
736 g_hash_table_remove(sub_dissectors->hash_table,
737 GUINT_TO_POINTER(pattern));
742 /* Look for a given value in a given uint dissector table and, if found,
743 call the dissector with the arguments supplied, and return TRUE,
744 otherwise return FALSE. */
746 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
747 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
749 dtbl_entry_t *dtbl_entry;
750 struct dissector_handle *handle;
751 guint32 saved_match_port;
754 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
755 if (dtbl_entry != NULL) {
757 * Is there currently a dissector handle for this entry?
759 handle = dtbl_entry->current;
760 if (handle == NULL) {
762 * No - pretend this dissector didn't exist,
763 * so that other dissectors might have a chance
764 * to dissect this packet.
770 * Save the current value of "pinfo->match_port",
771 * set it to the port that matched, call the
772 * dissector, and restore "pinfo->match_port".
774 saved_match_port = pinfo->match_port;
775 pinfo->match_port = port;
776 ret = call_dissector_work(handle, tvb, pinfo, tree);
777 pinfo->match_port = saved_match_port;
780 * If a new-style dissector returned 0, it means that
781 * it didn't think this tvbuff represented a packet for
782 * its protocol, and didn't dissect anything.
784 * Old-style dissectors can't reject the packet.
786 * 0 is also returned if the protocol wasn't enabled.
788 * If the packet was rejected, we return FALSE, so that
789 * other dissectors might have a chance to dissect this
790 * packet, otherwise we return TRUE.
797 /* Look for a given value in a given uint dissector table and, if found,
798 return the dissector handle for that value. */
800 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
802 dtbl_entry_t *dtbl_entry;
804 dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
805 if (dtbl_entry != NULL)
806 return dtbl_entry->current;
811 /* Find an entry in a string dissector table. */
812 static dtbl_entry_t *
813 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
815 switch (sub_dissectors->type) {
820 * You can do a string lookup in these tables.
826 * But you can't do a string lookup in any other types
829 g_assert_not_reached();
835 return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
838 /* Add an entry to a string dissector table. */
840 dissector_add_string(const char *name, gchar *pattern,
841 dissector_handle_t handle)
843 dissector_table_t sub_dissectors = find_dissector_table( name);
844 dtbl_entry_t *dtbl_entry;
847 g_assert( sub_dissectors);
849 switch (sub_dissectors->type) {
854 * You can do a string lookup in these tables.
860 * But you can't do a string lookup in any other types
863 g_assert_not_reached();
866 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
867 dtbl_entry->current = handle;
868 dtbl_entry->initial = dtbl_entry->current;
870 /* do the table insertion */
871 g_hash_table_insert( sub_dissectors->hash_table, pattern,
872 (gpointer)dtbl_entry);
875 * Now add it to the list of handles that could be used with this
876 * table, because it *is* being used with this table.
878 dissector_add_handle(name, handle);
881 /* Delete the entry for a dissector in a string dissector table
882 with a particular pattern. */
884 /* NOTE: this doesn't use the dissector call variable. It is included to */
885 /* be consistant with the dissector_add_string and more importantly to */
886 /* be used if the technique of adding a temporary dissector is */
888 /* If temporary dissectors are deleted, then the original dissector must */
891 dissector_delete_string(const char *name, const gchar *pattern,
892 dissector_handle_t handle _U_)
894 dissector_table_t sub_dissectors = find_dissector_table( name);
895 dtbl_entry_t *dtbl_entry;
898 g_assert( sub_dissectors);
903 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
905 if (dtbl_entry != NULL) {
909 g_hash_table_remove(sub_dissectors->hash_table, pattern);
912 * Now free up the entry.
918 /* Change the entry for a dissector in a string dissector table
919 with a particular pattern to use a new dissector handle. */
921 dissector_change_string(const char *name, gchar *pattern,
922 dissector_handle_t handle)
924 dissector_table_t sub_dissectors = find_dissector_table( name);
925 dtbl_entry_t *dtbl_entry;
928 g_assert( sub_dissectors);
931 * See if the entry already exists. If so, reuse it.
933 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
934 if (dtbl_entry != NULL) {
935 dtbl_entry->current = handle;
940 * Don't create an entry if there is no dissector handle - I.E. the
941 * user said not to decode something that wasn't being decoded
942 * in the first place.
947 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
948 dtbl_entry->initial = NULL;
949 dtbl_entry->current = handle;
951 /* do the table insertion */
952 g_hash_table_insert( sub_dissectors->hash_table, pattern,
953 (gpointer)dtbl_entry);
956 /* Reset an entry in a string sub-dissector table to its initial value. */
958 dissector_reset_string(const char *name, const gchar *pattern)
960 dissector_table_t sub_dissectors = find_dissector_table( name);
961 dtbl_entry_t *dtbl_entry;
964 g_assert( sub_dissectors);
969 dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
971 if (dtbl_entry == NULL)
975 * Found - is there an initial value?
977 if (dtbl_entry->initial != NULL) {
978 dtbl_entry->current = dtbl_entry->initial;
980 g_hash_table_remove(sub_dissectors->hash_table, pattern);
985 /* Look for a given string in a given dissector table and, if found, call
986 the dissector with the arguments supplied, and return TRUE, otherwise
989 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
990 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
992 dtbl_entry_t *dtbl_entry;
993 struct dissector_handle *handle;
995 const gchar *saved_match_string;
997 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
998 if (dtbl_entry != NULL) {
1000 * Is there currently a dissector handle for this entry?
1002 handle = dtbl_entry->current;
1003 if (handle == NULL) {
1005 * No - pretend this dissector didn't exist,
1006 * so that other dissectors might have a chance
1007 * to dissect this packet.
1013 * Save the current value of "pinfo->match_string",
1014 * set it to the string that matched, call the
1015 * dissector, and restore "pinfo->match_string".
1017 saved_match_string = pinfo->match_string;
1018 pinfo->match_string = string;
1019 ret = call_dissector_work(handle, tvb, pinfo, tree);
1020 pinfo->match_string = saved_match_string;
1023 * If a new-style dissector returned 0, it means that
1024 * it didn't think this tvbuff represented a packet for
1025 * its protocol, and didn't dissect anything.
1027 * Old-style dissectors can't reject the packet.
1029 * 0 is also returned if the protocol wasn't enabled.
1031 * If the packet was rejected, we return FALSE, so that
1032 * other dissectors might have a chance to dissect this
1033 * packet, otherwise we return TRUE.
1040 /* Look for a given value in a given string dissector table and, if found,
1041 return the dissector handle for that value. */
1043 dissector_get_string_handle(dissector_table_t sub_dissectors,
1044 const gchar *string)
1046 dtbl_entry_t *dtbl_entry;
1048 dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1049 if (dtbl_entry != NULL)
1050 return dtbl_entry->current;
1056 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1058 return dtbl_entry->current;
1061 /* Add a handle to the list of handles that *could* be used with this
1062 table. That list is used by code in the UI. */
1064 dissector_add_handle(const char *name, dissector_handle_t handle)
1066 dissector_table_t sub_dissectors = find_dissector_table( name);
1070 g_assert(sub_dissectors != NULL);
1072 /* Is it already in this list? */
1073 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1074 if (entry != NULL) {
1076 * Yes - don't insert it again.
1081 /* Add it to the list. */
1082 sub_dissectors->dissector_handles =
1083 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1087 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1089 return dtbl_entry->initial;
1092 /**************************************************/
1094 /* Routines to walk dissector tables */
1096 /**************************************************/
1098 typedef struct dissector_foreach_info {
1099 gpointer caller_data;
1100 DATFunc caller_func;
1103 ftenum_t selector_type;
1104 } dissector_foreach_info_t;
1107 * Called for each entry in a dissector table.
1110 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1112 dissector_foreach_info_t *info;
1113 dtbl_entry_t *dtbl_entry;
1116 g_assert(user_data);
1119 if (dtbl_entry->current == NULL ||
1120 dtbl_entry->current->protocol == NULL) {
1122 * Either there is no dissector for this entry, or
1123 * the dissector doesn't have a protocol associated
1126 * XXX - should the latter check be done?
1132 info->caller_func(info->table_name, info->selector_type, key, value,
1137 * Called for each entry in the table of all dissector tables.
1140 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1142 dissector_table_t sub_dissectors;
1143 dissector_foreach_info_t *info;
1146 g_assert(user_data);
1148 sub_dissectors = value;
1150 info->table_name = (gchar*) key;
1151 info->selector_type = get_dissector_table_selector_type(info->table_name);
1152 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1156 * Walk all dissector tables calling a user supplied function on each
1160 dissector_all_tables_foreach (DATFunc func,
1163 dissector_foreach_info_t info;
1165 info.caller_data = user_data;
1166 info.caller_func = func;
1167 info.next_func = dissector_table_foreach_func;
1168 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1172 * Walk one dissector table's hash table calling a user supplied function
1176 dissector_table_foreach (char *name,
1180 dissector_foreach_info_t info;
1181 dissector_table_t sub_dissectors = find_dissector_table( name);
1183 info.table_name = name;
1184 info.selector_type = sub_dissectors->type;
1185 info.caller_func = func;
1186 info.caller_data = user_data;
1187 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1191 * Walk one dissector table's list of handles calling a user supplied
1192 * function on each entry.
1195 dissector_table_foreach_handle(char *name,
1196 DATFunc_handle func,
1199 dissector_table_t sub_dissectors = find_dissector_table( name);
1202 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1203 tmp = g_slist_next(tmp))
1204 func(name, tmp->data, user_data);
1208 * Called for each entry in a dissector table.
1211 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1213 dtbl_entry_t *dtbl_entry;
1214 dissector_foreach_info_t *info;
1217 g_assert(user_data);
1220 if (dtbl_entry->initial == dtbl_entry->current) {
1222 * Entry hasn't changed - don't call the function.
1228 info->caller_func(info->table_name, info->selector_type, key, value,
1233 * Walk all dissector tables calling a user supplied function only on
1234 * any entry that has been changed from its original state.
1237 dissector_all_tables_foreach_changed (DATFunc func,
1240 dissector_foreach_info_t info;
1242 info.caller_data = user_data;
1243 info.caller_func = func;
1244 info.next_func = dissector_table_foreach_changed_func;
1245 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1249 * Walk one dissector table calling a user supplied function only on
1250 * any entry that has been changed from its original state.
1253 dissector_table_foreach_changed (char *name,
1257 dissector_foreach_info_t info;
1258 dissector_table_t sub_dissectors = find_dissector_table( name);
1260 info.table_name = name;
1261 info.selector_type = sub_dissectors->type;
1262 info.caller_func = func;
1263 info.caller_data = user_data;
1264 g_hash_table_foreach(sub_dissectors->hash_table,
1265 dissector_table_foreach_changed_func, &info);
1268 typedef struct dissector_foreach_table_info {
1269 gpointer caller_data;
1270 DATFunc_table caller_func;
1271 } dissector_foreach_table_info_t;
1274 * Called for each entry in the table of all dissector tables.
1277 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1279 dissector_table_t table;
1280 dissector_foreach_table_info_t *info;
1284 (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1288 * Walk all dissector tables calling a user supplied function on each
1292 dissector_all_tables_foreach_table (DATFunc_table func,
1295 dissector_foreach_table_info_t info;
1297 info.caller_data = user_data;
1298 info.caller_func = func;
1299 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1303 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
1306 dissector_table_t sub_dissectors;
1308 /* Create our hash-of-hashes if it doesn't already exist */
1309 if (!dissector_tables) {
1310 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1311 g_assert(dissector_tables);
1314 /* Make sure the registration is unique */
1315 g_assert(!g_hash_table_lookup( dissector_tables, name ));
1317 /* Create and register the dissector table for this name; returns */
1318 /* a pointer to the dissector table. */
1319 sub_dissectors = g_malloc(sizeof (struct dissector_table));
1327 * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1328 * so we use "g_direct_hash()" and "g_direct_equal()".
1330 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1336 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1341 g_assert_not_reached();
1343 sub_dissectors->dissector_handles = NULL;
1344 sub_dissectors->ui_name = ui_name;
1345 sub_dissectors->type = type;
1346 sub_dissectors->base = base;
1347 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1348 return sub_dissectors;
1352 get_dissector_table_ui_name(const char *name)
1354 dissector_table_t sub_dissectors = find_dissector_table( name);
1356 return sub_dissectors->ui_name;
1360 get_dissector_table_selector_type(const char *name)
1362 dissector_table_t sub_dissectors = find_dissector_table( name);
1364 return sub_dissectors->type;
1368 get_dissector_table_base(const char *name)
1370 dissector_table_t sub_dissectors = find_dissector_table( name);
1372 return sub_dissectors->base;
1375 static GHashTable *heur_dissector_lists = NULL;
1378 heur_dissector_t dissector;
1379 protocol_t *protocol;
1380 } heur_dtbl_entry_t;
1382 /* Finds a heuristic dissector table by field name. */
1383 static heur_dissector_list_t *
1384 find_heur_dissector_list(const char *name)
1386 g_assert(heur_dissector_lists != NULL);
1387 return g_hash_table_lookup(heur_dissector_lists, name);
1391 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1393 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1394 heur_dtbl_entry_t *dtbl_entry;
1397 g_assert(sub_dissectors != NULL);
1399 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1400 dtbl_entry->dissector = dissector;
1401 dtbl_entry->protocol = find_protocol_by_id(proto);
1403 /* do the table insertion */
1404 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1408 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1409 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1412 const char *saved_proto;
1414 heur_dtbl_entry_t *dtbl_entry;
1415 guint16 saved_can_desegment;
1417 /* can_desegment is set to 2 by anyone which offers this api/service.
1418 then everytime a subdissector is called it is decremented by one.
1419 thus only the subdissector immediately ontop of whoever offers this
1421 We save the current value of "can_desegment" for the
1422 benefit of TCP proxying dissectors such as SOCKS, so they
1423 can restore it and allow the dissectors they call to use
1424 the desegmentation service.
1426 saved_can_desegment=pinfo->can_desegment;
1427 pinfo->saved_can_desegment = saved_can_desegment;
1428 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1431 saved_proto = pinfo->current_proto;
1432 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1433 /* XXX - why set this now and above? */
1434 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1435 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1436 if (dtbl_entry->protocol != NULL &&
1437 !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1439 * No - don't try this dissector.
1444 if (dtbl_entry->protocol != NULL) {
1445 pinfo->current_proto =
1446 proto_get_protocol_short_name(dtbl_entry->protocol);
1448 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1453 pinfo->current_proto = saved_proto;
1454 pinfo->can_desegment=saved_can_desegment;
1459 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1461 /* Create our hash-of-lists if it doesn't already exist */
1462 if (heur_dissector_lists == NULL) {
1463 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1464 g_assert(heur_dissector_lists != NULL);
1467 /* Make sure the registration is unique */
1468 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1470 *sub_dissectors = NULL; /* initially empty */
1471 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1472 (gpointer) sub_dissectors);
1476 * Register dissectors by name; used if one dissector always calls a
1477 * particular dissector, or if it bases the decision of which dissector
1478 * to call on something other than a numerical value or on "try a bunch
1479 * of dissectors until one likes the packet".
1483 * List of registered dissectors.
1485 static GHashTable *registered_dissectors = NULL;
1487 /* Get the short name of the protocol for a dissector handle, if it has
1490 dissector_handle_get_short_name(dissector_handle_t handle)
1492 if (handle->protocol == NULL) {
1494 * No protocol (see, for example, the handle for
1495 * dissecting the set of protocols where the first
1496 * octet of the payload is an OSI network layer protocol
1501 return proto_get_protocol_short_name(handle->protocol);
1504 /* Get the index of the protocol for a dissector handle, if it has
1507 dissector_handle_get_protocol_index(dissector_handle_t handle)
1509 if (handle->protocol == NULL) {
1511 * No protocol (see, for example, the handle for
1512 * dissecting the set of protocols where the first
1513 * octet of the payload is an OSI network layer protocol
1518 return proto_get_id(handle->protocol);
1521 /* Find a registered dissector by name. */
1523 find_dissector(const char *name)
1525 g_assert(registered_dissectors != NULL);
1526 return g_hash_table_lookup(registered_dissectors, name);
1529 /* Create an anonymous handle for a dissector. */
1531 create_dissector_handle(dissector_t dissector, int proto)
1533 struct dissector_handle *handle;
1535 handle = g_malloc(sizeof (struct dissector_handle));
1536 handle->name = NULL;
1537 handle->is_new = FALSE;
1538 handle->dissector.old = dissector;
1539 handle->protocol = find_protocol_by_id(proto);
1545 new_create_dissector_handle(new_dissector_t dissector, int proto)
1547 struct dissector_handle *handle;
1549 handle = g_malloc(sizeof (struct dissector_handle));
1550 handle->name = NULL;
1551 handle->is_new = TRUE;
1552 handle->dissector.new = dissector;
1553 handle->protocol = find_protocol_by_id(proto);
1558 /* Register a dissector by name. */
1560 register_dissector(const char *name, dissector_t dissector, int proto)
1562 struct dissector_handle *handle;
1564 /* Create our hash table if it doesn't already exist */
1565 if (registered_dissectors == NULL) {
1566 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1567 g_assert(registered_dissectors != NULL);
1570 /* Make sure the registration is unique */
1571 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1573 handle = g_malloc(sizeof (struct dissector_handle));
1574 handle->name = name;
1575 handle->is_new = FALSE;
1576 handle->dissector.old = dissector;
1577 handle->protocol = find_protocol_by_id(proto);
1579 g_hash_table_insert(registered_dissectors, (gpointer)name,
1584 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1586 struct dissector_handle *handle;
1588 /* Create our hash table if it doesn't already exist */
1589 if (registered_dissectors == NULL) {
1590 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1591 g_assert(registered_dissectors != NULL);
1594 /* Make sure the registration is unique */
1595 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1597 handle = g_malloc(sizeof (struct dissector_handle));
1598 handle->name = name;
1599 handle->is_new = TRUE;
1600 handle->dissector.new = dissector;
1601 handle->protocol = find_protocol_by_id(proto);
1603 g_hash_table_insert(registered_dissectors, (gpointer)name,
1607 /* Call a dissector through a handle. */
1609 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1610 packet_info *pinfo, proto_tree *tree)
1614 ret = call_dissector_work(handle, tvb, pinfo, tree);
1617 * The protocol was disabled, or the dissector rejected
1618 * it. Just dissect this packet as data.
1620 g_assert(data_handle != NULL);
1621 g_assert(data_handle->protocol != NULL);
1622 call_dissector(data_handle, tvb, pinfo, tree);
1623 return tvb_length(tvb);