2 * Routines for packet disassembly
4 * $Id: packet.c,v 1.82 2002/11/15 03:10:36 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.
42 #ifdef NEED_INET_V6DEFS_H
43 # include "inet_v6defs.h"
47 #include "timestamp.h"
49 #include "atalk-utils.h"
50 #include "ipv6-utils.h"
51 #include "sna-utils.h"
52 #include "osi-utils.h"
58 #include "epan_dissect.h"
60 #include "../reassemble.h"
62 static gint proto_malformed = -1;
63 static dissector_handle_t frame_handle = NULL;
64 static dissector_handle_t data_handle = NULL;
66 const true_false_string flags_set_truth = {
74 frame_handle = find_dissector("frame");
75 data_handle = find_dissector("data");
76 proto_malformed = proto_get_id_by_filter_name("malformed");
86 * Given a tvbuff, and a length from a packet header, adjust the length
87 * of the tvbuff to reflect the specified length.
90 set_actual_length(tvbuff_t *tvb, guint specified_len)
92 if (specified_len < tvb_reported_length(tvb)) {
93 /* Adjust the length of this tvbuff to include only the specified
96 The dissector above the one calling us (the dissector above is
97 probably us) may use that to determine how much of its packet
99 tvb_set_reported_length(tvb, specified_len);
103 /* Allow protocols to register "init" routines, which are called before
104 we make a pass through a capture file and dissect all its packets
105 (e.g., when we read in a new capture file, or run a "filter packets"
106 or "colorize packets" pass over the current capture file). */
107 static GSList *init_routines;
110 register_init_routine(void (*func)(void))
112 init_routines = g_slist_append(init_routines, func);
115 /* Initialize all data structures used for dissection. */
117 call_init_routine(gpointer routine, gpointer dummy _U_)
119 void (*func)(void) = routine;
125 init_dissection(void)
127 /* Initialize the table of conversations. */
128 epan_conversation_init();
130 /* Initialize the table of circuits. */
133 /* Initialize protocol-specific variables. */
134 g_slist_foreach(init_routines, &call_init_routine, NULL);
136 /* Initialize the common data structures for fragment reassembly.
137 Must be done *after* calling init routines, as those routines
138 may free up space for fragments, which they find by using the
139 data structures that "reassemble_init()" frees. */
143 /* Allow protocols to register a "cleanup" routine to be
144 * run after the initial sequential run through the packets.
145 * Note that the file can still be open after this; this is not
146 * the final cleanup. */
147 static GSList *postseq_cleanup_routines;
150 register_postseq_cleanup_routine(void (*func)(void))
152 postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
156 /* Call all the registered "postseq_cleanup" routines. */
158 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
160 void (*func)(void) = routine;
166 postseq_cleanup_all_protocols(void)
168 g_slist_foreach(postseq_cleanup_routines,
169 &call_postseq_cleanup_routine, NULL);
172 /* Contains information about data sources. */
173 static GMemChunk *data_source_chunk = NULL;
176 * Add a new data source to the list of data sources for a frame, given
177 * the tvbuff for the data source and its name.
180 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, char *name)
184 if (data_source_chunk == NULL) {
185 data_source_chunk = g_mem_chunk_new("data_source_chunk",
186 sizeof (data_source), 10 * sizeof (data_source),
189 src = g_mem_chunk_alloc(data_source_chunk);
192 * XXX - if we require this argument to be a string constant,
193 * we don't need to allocate a buffer for a copy and make a
194 * copy, and wouldn't need to free the buffer, either.
196 src->name = g_strdup(name);
197 pinfo->data_src = g_slist_append(pinfo->data_src, src);
201 * Free up a frame's list of data sources.
204 free_data_sources(packet_info *pinfo)
209 for (src_le = pinfo->data_src; src_le != NULL; src_le = src_le->next) {
212 g_mem_chunk_free(data_source_chunk, src);
214 g_slist_free(pinfo->data_src);
215 pinfo->data_src = NULL;
218 /* Allow dissectors to register a "final_registration" routine
219 * that is run like the proto_register_XXX() routine, but the end
220 * end of the epan_init() function; that is, *after* all other
221 * subsystems, like dfilters, have finished initializing. This is
222 * useful for dissector registration routines which need to compile
223 * display filters. dfilters can't initialize itself until all protocols
224 * have registereed themselves. */
225 static GSList *final_registration_routines;
228 register_final_registration_routine(void (*func)(void))
230 final_registration_routines = g_slist_append(final_registration_routines,
234 /* Call all the registered "final_registration" routines. */
236 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
238 void (*func)(void) = routine;
244 final_registration_all_protocols(void)
246 g_slist_foreach(final_registration_routines,
247 &call_final_registration_routine, NULL);
251 /* Creates the top-most tvbuff and calls dissect_frame() */
253 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
254 const guchar *pd, frame_data *fd, column_info *cinfo)
259 for (i = 0; i < cinfo->num_cols; i++) {
260 cinfo->col_buf[i][0] = '\0';
261 cinfo->col_data[i] = cinfo->col_buf[i];
262 cinfo->col_expr[i][0] = '\0';
263 cinfo->col_expr_val[i][0] = '\0';
266 col_set_writable(cinfo, TRUE);
268 edt->pi.current_proto = "<Missing Protocol Name>";
269 edt->pi.cinfo = cinfo;
271 edt->pi.pseudo_header = pseudo_header;
272 edt->pi.data_src = NULL;
273 edt->pi.dl_src.type = AT_NONE;
274 edt->pi.dl_dst.type = AT_NONE;
275 edt->pi.net_src.type = AT_NONE;
276 edt->pi.net_dst.type = AT_NONE;
277 edt->pi.src.type = AT_NONE;
278 edt->pi.dst.type = AT_NONE;
279 edt->pi.ethertype = 0;
281 edt->pi.ipxptype = 0;
282 edt->pi.ctype = CT_NONE;
283 edt->pi.circuit_id = 0;
284 edt->pi.fragmented = FALSE;
285 edt->pi.in_error_pkt = FALSE;
286 edt->pi.ptype = PT_NONE;
288 edt->pi.destport = 0;
289 edt->pi.match_port = 0;
290 edt->pi.can_desegment = 0;
291 edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
292 edt->pi.private_data = NULL;
295 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
296 /* Add this tvbuffer into the data_src list */
297 add_new_data_source(&edt->pi, edt->tvb, "Frame");
299 /* Even though dissect_frame() catches all the exceptions a
300 * sub-dissector can throw, dissect_frame() itself may throw
301 * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
302 * in this function. */
303 if(frame_handle != NULL)
304 call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
308 g_assert_not_reached();
310 CATCH(ReportedBoundsError) {
311 if(proto_malformed != -1){
312 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
313 "[Malformed Frame: Packet Length]" );
316 g_assert_not_reached();
321 fd->flags.visited = 1;
324 /*********************** code added for sub-dissector lookup *********************/
327 * An dissector handle.
329 struct dissector_handle {
330 const char *name; /* dissector name */
331 gboolean is_new; /* TRUE if new-style dissector */
340 * Call a dissector through a handle.
341 * If the protocol for that handle isn't enabled, return 0 without
342 * calling the dissector.
343 * Otherwise, if the handle refers to a new-style dissector, call the
344 * dissector and return its return value, otherwise call it and return
345 * the length of the tvbuff pointed to by the argument.
348 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
349 packet_info *pinfo, proto_tree *tree)
351 const char *saved_proto;
352 guint16 saved_can_desegment;
355 if (handle->proto_index != -1 &&
356 !proto_is_protocol_enabled(handle->proto_index)) {
358 * The protocol isn't enabled.
363 saved_proto = pinfo->current_proto;
364 saved_can_desegment = pinfo->can_desegment;
367 * can_desegment is set to 2 by anyone which offers the
368 * desegmentation api/service.
369 * Then everytime a subdissector is called it is decremented
371 * Thus only the subdissector immediately on top of whoever
372 * offers this service can use it.
374 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
375 if (handle->proto_index != -1) {
376 pinfo->current_proto =
377 proto_get_protocol_short_name(handle->proto_index);
380 ret = (*handle->dissector.new)(tvb, pinfo, tree);
382 (*handle->dissector.old)(tvb, pinfo, tree);
383 ret = tvb_length(tvb);
386 * XXX - a tvbuff can have 0 bytes of data in
387 * it, so we have to make sure we don't return
393 pinfo->current_proto = saved_proto;
394 pinfo->can_desegment = saved_can_desegment;
399 * An entry in the hash table portion of a dissector table.
402 dissector_handle_t initial;
403 dissector_handle_t current;
409 * "hash_table" is a hash table, indexed by port number, supplying
410 * a "struct dtbl_entry"; it records what dissector is assigned to
411 * that port number in that table.
413 * "dissector_handles" is a list of all dissectors that *could* be
414 * used in that table; not all of them are necessarily in the table,
415 * as they may be for protocols that don't have a fixed port number.
417 * "ui_name" is the name the dissector table has in the user interface.
419 * "type" is a field type giving the width of the port number for that
422 * "base" is the base in which to display the port number for that
425 struct dissector_table {
426 GHashTable *hash_table;
427 GSList *dissector_handles;
433 static GHashTable *dissector_tables = NULL;
435 /* Finds a dissector table by table name. */
437 find_dissector_table(const char *name)
439 g_assert(dissector_tables);
440 return g_hash_table_lookup( dissector_tables, name );
444 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
446 dissector_table_t sub_dissectors = find_dissector_table( name);
447 dtbl_entry_t *dtbl_entry;
450 g_assert( sub_dissectors);
452 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
453 dtbl_entry->current = handle;
454 dtbl_entry->initial = dtbl_entry->current;
456 /* do the table insertion */
457 g_hash_table_insert( sub_dissectors->hash_table,
458 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
461 * Now add it to the list of handles that could be used with this
462 * table, because it *is* being used with this table.
464 dissector_add_handle(name, handle);
467 /* delete the entry for this dissector at this pattern */
469 /* NOTE: this doesn't use the dissector call variable. It is included to */
470 /* be consistant with the dissector_add and more importantly to be used */
471 /* if the technique of adding a temporary dissector is implemented. */
472 /* If temporary dissectors are deleted, then the original dissector must */
475 dissector_delete(const char *name, guint32 pattern,
476 dissector_handle_t handle _U_)
478 dissector_table_t sub_dissectors = find_dissector_table( name);
479 dtbl_entry_t *dtbl_entry;
482 g_assert( sub_dissectors);
487 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
488 GUINT_TO_POINTER(pattern));
490 if (dtbl_entry != NULL) {
494 g_hash_table_remove(sub_dissectors->hash_table,
495 GUINT_TO_POINTER(pattern));
498 * Now free up the entry.
505 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
507 dissector_table_t sub_dissectors = find_dissector_table( name);
508 dtbl_entry_t *dtbl_entry;
511 g_assert( sub_dissectors);
514 * See if the entry already exists. If so, reuse it.
516 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
517 GUINT_TO_POINTER(pattern));
518 if (dtbl_entry != NULL) {
519 dtbl_entry->current = handle;
524 * Don't create an entry if there is no dissector handle - I.E. the
525 * user said not to decode something that wasn't being decoded
526 * in the first place.
531 dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
532 dtbl_entry->initial = NULL;
533 dtbl_entry->current = handle;
535 /* do the table insertion */
536 g_hash_table_insert( sub_dissectors->hash_table,
537 GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
540 /* Reset a dissector in a sub-dissector table to its initial value. */
542 dissector_reset(const char *name, guint32 pattern)
544 dissector_table_t sub_dissectors = find_dissector_table( name);
545 dtbl_entry_t *dtbl_entry;
548 g_assert( sub_dissectors);
553 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
554 GUINT_TO_POINTER(pattern));
556 if (dtbl_entry == NULL)
560 * Found - is there an initial value?
562 if (dtbl_entry->initial != NULL) {
563 dtbl_entry->current = dtbl_entry->initial;
565 g_hash_table_remove(sub_dissectors->hash_table,
566 GUINT_TO_POINTER(pattern));
571 /* Look for a given port in a given dissector table and, if found, call
572 the dissector with the arguments supplied, and return TRUE, otherwise
575 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
576 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
578 dtbl_entry_t *dtbl_entry;
579 struct dissector_handle *handle;
580 guint32 saved_match_port;
583 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
584 GUINT_TO_POINTER(port));
585 if (dtbl_entry != NULL) {
587 * Is there currently a dissector handle for this entry?
589 handle = dtbl_entry->current;
590 if (handle == NULL) {
592 * No - pretend this dissector didn't exist,
593 * so that other dissectors might have a chance
594 * to dissect this packet.
600 * Save the current value of "pinfo->match_port",
601 * set it to the port that matched, call the
602 * dissector, and restore "pinfo->match_port".
604 saved_match_port = pinfo->match_port;
605 pinfo->match_port = port;
606 ret = call_dissector_work(handle, tvb, pinfo, tree);
607 pinfo->match_port = saved_match_port;
610 * If a new-style dissector returned 0, it means that
611 * it didn't think this tvbuff represented a packet for
612 * its protocol, and didn't dissect anything.
614 * Old-style dissectors can't reject the packet.
616 * 0 is also returned if the protocol wasn't enabled.
618 * If the packet was rejected, we return FALSE, so that
619 * other dissectors might have a chance to dissect this
620 * packet, otherwise we return TRUE.
627 /* Look for a given port in a given dissector table and, if found, return
628 the dissector handle for that port. */
630 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
632 dtbl_entry_t *dtbl_entry;
634 dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
635 GUINT_TO_POINTER(port));
636 if (dtbl_entry != NULL)
637 return dtbl_entry->current;
643 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
645 return dtbl_entry->current;
648 /* Add a handle to the list of handles that *could* be used with this
649 table. That list is used by code in the UI. */
651 dissector_add_handle(const char *name, dissector_handle_t handle)
653 dissector_table_t sub_dissectors = find_dissector_table( name);
657 g_assert(sub_dissectors != NULL);
659 /* Is it already in this list? */
660 entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
663 * Yes - don't insert it again.
668 /* Add it to the list. */
669 sub_dissectors->dissector_handles =
670 g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
674 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
676 return dtbl_entry->initial;
679 /**************************************************/
681 /* Routines to walk dissector tables */
683 /**************************************************/
685 typedef struct dissector_foreach_info {
686 gpointer caller_data;
690 } dissector_foreach_info_t;
693 * Called for each entry in a dissector table.
696 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
698 dissector_foreach_info_t *info;
699 dtbl_entry_t *dtbl_entry;
705 if (dtbl_entry->current == NULL ||
706 dtbl_entry->current->proto_index == -1) {
708 * Either there is no dissector for this entry, or
709 * the dissector doesn't have a protocol associated
712 * XXX - should the latter check be done?
718 info->caller_func(info->table_name, key, value, info->caller_data);
722 * Called for each entry in the table of all dissector tables.
725 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
727 dissector_table_t sub_dissectors;
728 dissector_foreach_info_t *info;
733 sub_dissectors = value;
735 info->table_name = (gchar*) key;
736 g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
740 * Walk all dissector tables calling a user supplied function on each
744 dissector_all_tables_foreach (DATFunc func,
747 dissector_foreach_info_t info;
749 info.caller_data = user_data;
750 info.caller_func = func;
751 info.next_func = dissector_table_foreach_func;
752 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
756 * Walk one dissector table's hash table calling a user supplied function
760 dissector_table_foreach (char *name,
764 dissector_foreach_info_t info;
765 dissector_table_t sub_dissectors = find_dissector_table( name);
767 info.table_name = name;
768 info.caller_func = func;
769 info.caller_data = user_data;
770 g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
774 * Walk one dissector table's list of handles calling a user supplied
775 * function on each entry.
778 dissector_table_foreach_handle(char *name, DATFunc_handle func, gpointer user_data)
780 dissector_table_t sub_dissectors = find_dissector_table( name);
783 for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
784 tmp = g_slist_next(tmp))
785 func(name, tmp->data, user_data);
789 * Called for each entry in a dissector table.
792 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
794 dtbl_entry_t *dtbl_entry;
795 dissector_foreach_info_t *info;
801 if (dtbl_entry->initial == dtbl_entry->current) {
803 * Entry hasn't changed - don't call the function.
809 info->caller_func(info->table_name, key, value, info->caller_data);
813 * Walk all dissector tables calling a user supplied function only on
814 * any entry that has been changed from its original state.
817 dissector_all_tables_foreach_changed (DATFunc func,
820 dissector_foreach_info_t info;
822 info.caller_data = user_data;
823 info.caller_func = func;
824 info.next_func = dissector_table_foreach_changed_func;
825 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
829 * Walk one dissector table calling a user supplied function only on
830 * any entry that has been changed from its original state.
833 dissector_table_foreach_changed (char *name,
837 dissector_foreach_info_t info;
838 dissector_table_t sub_dissectors = find_dissector_table( name);
840 info.table_name = name;
841 info.caller_func = func;
842 info.caller_data = user_data;
843 g_hash_table_foreach(sub_dissectors->hash_table,
844 dissector_table_foreach_changed_func, &info);
848 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
851 dissector_table_t sub_dissectors;
853 /* Create our hash-of-hashes if it doesn't already exist */
854 if (!dissector_tables) {
855 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
856 g_assert(dissector_tables);
859 /* Make sure the registration is unique */
860 g_assert(!g_hash_table_lookup( dissector_tables, name ));
862 /* Create and register the dissector table for this name; returns */
863 /* a pointer to the dissector table. */
864 sub_dissectors = g_malloc(sizeof (struct dissector_table));
865 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
867 sub_dissectors->dissector_handles = NULL;
868 sub_dissectors->ui_name = ui_name;
869 sub_dissectors->type = type;
870 sub_dissectors->base = base;
871 g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
872 return sub_dissectors;
876 get_dissector_table_ui_name(const char *name)
878 dissector_table_t sub_dissectors = find_dissector_table( name);
880 return sub_dissectors->ui_name;
884 get_dissector_table_type(const char *name)
886 dissector_table_t sub_dissectors = find_dissector_table( name);
888 return sub_dissectors->type;
892 get_dissector_table_base(const char *name)
894 dissector_table_t sub_dissectors = find_dissector_table( name);
896 return sub_dissectors->base;
899 static GHashTable *heur_dissector_lists = NULL;
902 heur_dissector_t dissector;
906 /* Finds a heuristic dissector table by field name. */
907 static heur_dissector_list_t *
908 find_heur_dissector_list(const char *name)
910 g_assert(heur_dissector_lists != NULL);
911 return g_hash_table_lookup(heur_dissector_lists, name);
915 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
917 heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
918 heur_dtbl_entry_t *dtbl_entry;
921 g_assert(sub_dissectors != NULL);
923 dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
924 dtbl_entry->dissector = dissector;
925 dtbl_entry->proto_index = proto;
927 /* do the table insertion */
928 *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
932 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
933 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
936 const char *saved_proto;
938 heur_dtbl_entry_t *dtbl_entry;
939 guint16 saved_can_desegment;
941 /* can_desegment is set to 2 by anyone which offers this api/service.
942 then everytime a subdissector is called it is decremented by one.
943 thus only the subdissector immediately ontop of whoever offers this
946 saved_can_desegment=pinfo->can_desegment;
947 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
950 saved_proto = pinfo->current_proto;
951 for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
952 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
953 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
954 if (dtbl_entry->proto_index != -1 &&
955 !proto_is_protocol_enabled(dtbl_entry->proto_index)) {
957 * No - don't try this dissector.
962 if (dtbl_entry->proto_index != -1) {
963 pinfo->current_proto =
964 proto_get_protocol_short_name(dtbl_entry->proto_index);
966 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
971 pinfo->current_proto = saved_proto;
972 pinfo->can_desegment=saved_can_desegment;
977 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
979 /* Create our hash-of-lists if it doesn't already exist */
980 if (heur_dissector_lists == NULL) {
981 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
982 g_assert(heur_dissector_lists != NULL);
985 /* Make sure the registration is unique */
986 g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
988 *sub_dissectors = NULL; /* initially empty */
989 g_hash_table_insert(heur_dissector_lists, (gpointer)name,
990 (gpointer) sub_dissectors);
994 * Register dissectors by name; used if one dissector always calls a
995 * particular dissector, or if it bases the decision of which dissector
996 * to call on something other than a numerical value or on "try a bunch
997 * of dissectors until one likes the packet".
1001 * List of registered dissectors.
1003 static GHashTable *registered_dissectors = NULL;
1005 /* Get the short name of the protocol for a dissector handle. */
1007 dissector_handle_get_short_name(dissector_handle_t handle)
1009 return proto_get_protocol_short_name(handle->proto_index);
1012 /* Get the index of the protocol for a dissector handle. */
1014 dissector_handle_get_protocol_index(dissector_handle_t handle)
1016 return handle->proto_index;
1019 /* Find a registered dissector by name. */
1021 find_dissector(const char *name)
1023 g_assert(registered_dissectors != NULL);
1024 return g_hash_table_lookup(registered_dissectors, name);
1027 /* Create an anonymous handle for a dissector. */
1029 create_dissector_handle(dissector_t dissector, int proto)
1031 struct dissector_handle *handle;
1033 handle = g_malloc(sizeof (struct dissector_handle));
1034 handle->name = NULL;
1035 handle->is_new = FALSE;
1036 handle->dissector.old = dissector;
1037 handle->proto_index = proto;
1043 new_create_dissector_handle(new_dissector_t dissector, int proto)
1045 struct dissector_handle *handle;
1047 handle = g_malloc(sizeof (struct dissector_handle));
1048 handle->name = NULL;
1049 handle->is_new = TRUE;
1050 handle->dissector.new = dissector;
1051 handle->proto_index = proto;
1056 /* Register a dissector by name. */
1058 register_dissector(const char *name, dissector_t dissector, int proto)
1060 struct dissector_handle *handle;
1062 /* Create our hash table if it doesn't already exist */
1063 if (registered_dissectors == NULL) {
1064 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1065 g_assert(registered_dissectors != NULL);
1068 /* Make sure the registration is unique */
1069 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1071 handle = g_malloc(sizeof (struct dissector_handle));
1072 handle->name = name;
1073 handle->is_new = FALSE;
1074 handle->dissector.old = dissector;
1075 handle->proto_index = proto;
1077 g_hash_table_insert(registered_dissectors, (gpointer)name,
1082 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1084 struct dissector_handle *handle;
1086 /* Create our hash table if it doesn't already exist */
1087 if (registered_dissectors == NULL) {
1088 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1089 g_assert(registered_dissectors != NULL);
1092 /* Make sure the registration is unique */
1093 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1095 handle = g_malloc(sizeof (struct dissector_handle));
1096 handle->name = name;
1097 handle->is_new = TRUE;
1098 handle->dissector.new = dissector;
1099 handle->proto_index = proto;
1101 g_hash_table_insert(registered_dissectors, (gpointer)name,
1105 /* Call a dissector through a handle. */
1107 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1108 packet_info *pinfo, proto_tree *tree)
1112 ret = call_dissector_work(handle, tvb, pinfo, tree);
1115 * The protocol was disabled, or the dissector rejected
1116 * it. Just dissect this packet as data.
1118 g_assert(data_handle != NULL);
1119 g_assert(data_handle->proto_index != -1);
1120 call_dissector(data_handle, tvb, pinfo, tree);
1121 return tvb_length(tvb);