Export "protocol_t" as an opaque type.
[obnox/wireshark/wip.git] / epan / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id: packet.c,v 1.98 2003/11/16 23:17:24 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #ifdef HAVE_STDARG_H
35 #include <stdarg.h>
36 #endif
37
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 #include "packet.h"
43 #include "timestamp.h"
44
45 #include "atalk-utils.h"
46 #include "ipv6-utils.h"
47 #include "sna-utils.h"
48 #include "osi-utils.h"
49 #include "to_str.h"
50
51 #include "resolv.h"
52 #include "tvbuff.h"
53 #include "plugins.h"
54 #include "epan_dissect.h"
55
56 #include "../reassemble.h"
57
58 static gint proto_malformed = -1;
59 static dissector_handle_t frame_handle = NULL;
60 static dissector_handle_t data_handle = NULL;
61
62 const true_false_string flags_set_truth = {
63   "Set",
64   "Not set"
65 };
66
67 void
68 packet_init(void)
69 {
70   frame_handle = find_dissector("frame");
71   data_handle = find_dissector("data");
72   proto_malformed = proto_get_id_by_filter_name("malformed");
73 }
74
75 void
76 packet_cleanup(void)
77 {
78         /* nothing */
79 }
80
81 /*
82  * Given a tvbuff, and a length from a packet header, adjust the length
83  * of the tvbuff to reflect the specified length.
84  */
85 void
86 set_actual_length(tvbuff_t *tvb, guint specified_len)
87 {
88   if (specified_len < tvb_reported_length(tvb)) {
89     /* Adjust the length of this tvbuff to include only the specified
90        payload length.
91
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
94        was padding. */
95     tvb_set_reported_length(tvb, specified_len);
96   }
97 }
98
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;
104
105 void
106 register_init_routine(void (*func)(void))
107 {
108         init_routines = g_slist_append(init_routines, func);
109 }
110
111 /* Initialize all data structures used for dissection. */
112 static void
113 call_init_routine(gpointer routine, gpointer dummy _U_)
114 {
115         void (*func)(void) = routine;
116
117         (*func)();
118 }
119
120 void
121 init_dissection(void)
122 {
123         /* Initialize the table of conversations. */
124         epan_conversation_init();
125
126         /* Initialize the table of circuits. */
127         epan_circuit_init();
128
129         /* Initialize protocol-specific variables. */
130         g_slist_foreach(init_routines, &call_init_routine, NULL);
131
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. */
136         reassemble_init();
137 }
138
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;
144
145 void
146 register_postseq_cleanup_routine(void (*func)(void))
147 {
148         postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
149                         func);
150 }
151
152 /* Call all the registered "postseq_cleanup" routines. */
153 static void
154 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
155 {
156         void (*func)(void) = routine;
157
158         (*func)();
159 }
160
161 void
162 postseq_cleanup_all_protocols(void)
163 {
164         g_slist_foreach(postseq_cleanup_routines,
165                         &call_postseq_cleanup_routine, NULL);
166 }
167
168 /* Contains information about data sources. */
169 static GMemChunk *data_source_chunk = NULL;
170
171 /*
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.
174  */
175 void
176 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, char *name)
177 {
178         data_source *src;
179
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),
183                     G_ALLOC_AND_FREE);
184         }
185         src = g_mem_chunk_alloc(data_source_chunk);
186         src->tvb = tvb;
187         /*
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.
191          */
192         src->name = g_strdup(name);
193         pinfo->data_src = g_slist_append(pinfo->data_src, src);
194 }
195
196 /*
197  * Free up a frame's list of data sources.
198  */
199 void
200 free_data_sources(packet_info *pinfo)
201 {
202         GSList *src_le;
203         data_source *src;
204
205         for (src_le = pinfo->data_src; src_le != NULL; src_le = src_le->next) {
206                 src = src_le->data;
207                 g_free(src->name);
208                 g_mem_chunk_free(data_source_chunk, src);
209         }
210         g_slist_free(pinfo->data_src);
211         pinfo->data_src = NULL;
212 }
213
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;
222
223 void
224 register_final_registration_routine(void (*func)(void))
225 {
226         final_registration_routines = g_slist_append(final_registration_routines,
227                         func);
228 }
229
230 /* Call all the registered "final_registration" routines. */
231 static void
232 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
233 {
234         void (*func)(void) = routine;
235
236         (*func)();
237 }
238
239 void
240 final_registration_all_protocols(void)
241 {
242         g_slist_foreach(final_registration_routines,
243                         &call_final_registration_routine, NULL);
244 }
245
246
247 /* Creates the top-most tvbuff and calls dissect_frame() */
248 void
249 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
250                const guchar *pd, frame_data *fd, column_info *cinfo)
251 {
252         if (cinfo != NULL)
253                 col_init(cinfo);
254         edt->pi.current_proto = "<Missing Protocol Name>";
255         edt->pi.cinfo = cinfo;
256         edt->pi.fd = fd;
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;
272         edt->pi.src.len = 0;
273         edt->pi.src.data = NULL;
274         edt->pi.dst.type = AT_NONE;
275         edt->pi.dst.len = 0;
276         edt->pi.dst.data = NULL;
277         edt->pi.ethertype = 0;
278         edt->pi.ipproto  = 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;
286         edt->pi.srcport  = 0;
287         edt->pi.destport = 0;
288         edt->pi.match_port = 0;
289         edt->pi.can_desegment = 0;
290         edt->pi.want_pdu_tracking = 0;
291         edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
292         edt->pi.private_data = NULL;
293         edt->pi.oxid = 0;
294         edt->pi.rxid = 0;
295         edt->pi.r_ctl = 0;
296         edt->pi.src_idx = 0;
297         edt->pi.dst_idx = 0;
298         edt->pi.vsan = 0;
299
300         TRY {
301                 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
302                 /* Add this tvbuffer into the data_src list */
303                 add_new_data_source(&edt->pi, edt->tvb, "Frame");
304
305                 /* Even though dissect_frame() catches all the exceptions a
306                  * sub-dissector can throw, dissect_frame() itself may throw
307                  * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
308                  * in this function. */
309                 if(frame_handle != NULL)
310                   call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
311
312         }
313         CATCH(BoundsError) {
314                 g_assert_not_reached();
315         }
316         CATCH(ReportedBoundsError) {
317           if(proto_malformed != -1){
318                 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
319                                 "[Malformed Frame: Packet Length]" );
320           }
321           else {
322             g_assert_not_reached();
323           }
324         }
325         ENDTRY;
326
327         fd->flags.visited = 1;
328 }
329
330 /*********************** code added for sub-dissector lookup *********************/
331
332 /*
333  * An dissector handle.
334  */
335 struct dissector_handle {
336         const char      *name;          /* dissector name */
337         gboolean        is_new;         /* TRUE if new-style dissector */
338         union {
339                 dissector_t     old;
340                 new_dissector_t new;
341         } dissector;
342         protocol_t      *protocol;
343 };
344
345 static int
346 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
347     packet_info *pinfo, proto_tree *tree)
348 {
349         const char *saved_proto;
350         int ret;
351
352         saved_proto = pinfo->current_proto;
353
354         if (handle->protocol != NULL) {
355                 pinfo->current_proto =
356                     proto_get_protocol_short_name(handle->protocol);
357         }
358
359         if (handle->is_new)
360                 ret = (*handle->dissector.new)(tvb, pinfo, tree);
361         else {
362                 (*handle->dissector.old)(tvb, pinfo, tree);
363                 ret = tvb_length(tvb);
364                 if (ret == 0) {
365                         /*
366                          * XXX - a tvbuff can have 0 bytes of data in
367                          * it, so we have to make sure we don't return
368                          * 0.
369                          */
370                         ret = 1;
371                 }
372         }
373
374         pinfo->current_proto = saved_proto;
375
376         return ret;
377 }
378
379 /*
380  * Call a dissector through a handle.
381  * If the protocol for that handle isn't enabled, return 0 without
382  * calling the dissector.
383  * Otherwise, if the handle refers to a new-style dissector, call the
384  * dissector and return its return value, otherwise call it and return
385  * the length of the tvbuff pointed to by the argument.
386  */
387 static int
388 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
389     packet_info *pinfo, proto_tree *tree)
390 {
391         const char *saved_proto;
392         guint16 saved_can_desegment;
393         volatile int ret;
394         gboolean save_writable;
395         volatile address save_dl_src;
396         volatile address save_dl_dst;
397         volatile address save_net_src;
398         volatile address save_net_dst;
399         volatile address save_src;
400         volatile address save_dst;
401
402         if (handle->protocol != NULL &&
403             !proto_is_protocol_enabled(handle->protocol)) {
404                 /*
405                  * The protocol isn't enabled.
406                  */
407                 return 0;
408         }
409
410         saved_proto = pinfo->current_proto;
411         saved_can_desegment = pinfo->can_desegment;
412
413         /*
414          * can_desegment is set to 2 by anyone which offers the
415          * desegmentation api/service.
416          * Then everytime a subdissector is called it is decremented
417          * by one.
418          * Thus only the subdissector immediately on top of whoever
419          * offers this service can use it.
420          */
421         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
422         if (handle->protocol != NULL) {
423                 pinfo->current_proto =
424                     proto_get_protocol_short_name(handle->protocol);
425         }
426
427         if (pinfo->in_error_pkt) {
428                 /*
429                  * This isn't a packet being transported inside
430                  * the protocol whose dissector is calling us,
431                  * it's a copy of a packet that caused an error
432                  * in some protocol included in a packet that
433                  * reports the error (e.g., an ICMP Unreachable
434                  * packet).
435                  */
436
437                 /*
438                  * Save the current state of the writability of
439                  * the columns, and restore them after the
440                  * dissector returns, so that the columns
441                  * don't reflect the packet that got the error,
442                  * they reflect the packet that reported the
443                  * error.
444                  */
445                 save_writable = col_get_writable(pinfo->cinfo);
446                 col_set_writable(pinfo->cinfo, FALSE);
447                 save_dl_src = pinfo->dl_src;
448                 save_dl_dst = pinfo->dl_dst;
449                 save_net_src = pinfo->net_src;
450                 save_net_dst = pinfo->net_dst;
451                 save_src = pinfo->src;
452                 save_dst = pinfo->dst;
453
454                 /* Dissect the contained packet. */
455                 TRY {
456                         ret = call_dissector_through_handle(handle, tvb,
457                             pinfo, tree);
458                 }
459                 CATCH(BoundsError) {
460                         /*
461                          * Restore the column writability and addresses.
462                          */
463                         col_set_writable(pinfo->cinfo, save_writable);
464                         pinfo->dl_src = save_dl_src;
465                         pinfo->dl_dst = save_dl_dst;
466                         pinfo->net_src = save_net_src;
467                         pinfo->net_dst = save_net_dst;
468                         pinfo->src = save_src;
469                         pinfo->dst = save_dst;
470
471                         /*
472                          * Restore the current protocol, so any
473                          * "Short Frame" indication reflects that
474                          * protocol, not the protocol for the
475                          * packet that got the error.
476                          */
477                         pinfo->current_proto = saved_proto;
478
479                         /*
480                          * Restore the desegmentability state.
481                          */
482                         pinfo->can_desegment = saved_can_desegment;
483
484                         /*
485                          * Rethrow the exception, so this will be
486                          * reported as a short frame.
487                          */
488                         RETHROW;
489                 }
490                 CATCH(ReportedBoundsError) {
491                         ; /* do nothing */
492                 }
493                 ENDTRY;
494
495                 col_set_writable(pinfo->cinfo, save_writable);
496                 pinfo->dl_src = save_dl_src;
497                 pinfo->dl_dst = save_dl_dst;
498                 pinfo->net_src = save_net_src;
499                 pinfo->net_dst = save_net_dst;
500                 pinfo->src = save_src;
501                 pinfo->dst = save_dst;
502                 pinfo->want_pdu_tracking = 0;
503         } else {
504                 /*
505                  * Just call the subdissector.
506                  */
507                 ret = call_dissector_through_handle(handle, tvb, pinfo, tree);
508         }
509         pinfo->current_proto = saved_proto;
510         pinfo->can_desegment = saved_can_desegment;
511         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
512         return ret;
513 }
514
515 /*
516  * An entry in the hash table portion of a dissector table.
517  */
518 struct dtbl_entry {
519         dissector_handle_t initial;
520         dissector_handle_t current;
521 };
522
523 /*
524  * A dissector table.
525  *
526  * "hash_table" is a hash table, indexed by port number, supplying
527  * a "struct dtbl_entry"; it records what dissector is assigned to
528  * that port number in that table.
529  *
530  * "dissector_handles" is a list of all dissectors that *could* be
531  * used in that table; not all of them are necessarily in the table,
532  * as they may be for protocols that don't have a fixed port number.
533  *
534  * "ui_name" is the name the dissector table has in the user interface.
535  *
536  * "type" is a field type giving the width of the port number for that
537  * dissector table.
538  *
539  * "base" is the base in which to display the port number for that
540  * dissector table.
541  */
542 struct dissector_table {
543         GHashTable      *hash_table;
544         GSList          *dissector_handles;
545         char            *ui_name;
546         ftenum_t        type;
547         int             base;
548 };
549
550 static GHashTable *dissector_tables = NULL;
551
552 /* Finds a dissector table by table name. */
553 dissector_table_t
554 find_dissector_table(const char *name)
555 {
556         g_assert(dissector_tables);
557         return g_hash_table_lookup( dissector_tables, name );
558 }
559
560 /* Find an entry in a uint dissector table. */ 
561 static dtbl_entry_t *
562 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
563 {
564         switch (sub_dissectors->type) {
565
566         case FT_UINT8:
567         case FT_UINT16:
568         case FT_UINT24:
569         case FT_UINT32:
570                 /*
571                  * You can do a port lookup in these tables.
572                  */
573                 break;
574
575         default:
576                 /*
577                  * But you can't do a port lookup in any other types
578                  * of tables.
579                  */
580                 g_assert_not_reached();
581         }
582
583         /*
584          * Find the entry.
585          */
586         return g_hash_table_lookup(sub_dissectors->hash_table,
587             GUINT_TO_POINTER(pattern));
588 }
589
590 /* Add an entry to a uint dissector table. */
591 void
592 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
593 {
594         dissector_table_t sub_dissectors = find_dissector_table( name);
595         dtbl_entry_t *dtbl_entry;
596
597 /* sanity checks */
598         g_assert( sub_dissectors);
599         switch (sub_dissectors->type) {
600
601         case FT_UINT8:
602         case FT_UINT16:
603         case FT_UINT24:
604         case FT_UINT32:
605                 /*
606                  * You can do a port lookup in these tables.
607                  */
608                 break;
609
610         default:
611                 /*
612                  * But you can't do a port lookup in any other types
613                  * of tables.
614                  */
615                 g_assert_not_reached();
616         }
617
618         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
619         dtbl_entry->current = handle;
620         dtbl_entry->initial = dtbl_entry->current;
621
622 /* do the table insertion */
623         g_hash_table_insert( sub_dissectors->hash_table,
624             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
625
626         /*
627          * Now add it to the list of handles that could be used with this
628          * table, because it *is* being used with this table.
629          */
630         dissector_add_handle(name, handle);
631 }
632
633 /* Delete the entry for a dissector in a uint dissector table
634    with a particular pattern. */
635
636 /* NOTE: this doesn't use the dissector call variable. It is included to */
637 /*      be consistant with the dissector_add and more importantly to be used */
638 /*      if the technique of adding a temporary dissector is implemented.  */
639 /*      If temporary dissectors are deleted, then the original dissector must */
640 /*      be available. */
641 void
642 dissector_delete(const char *name, guint32 pattern,
643         dissector_handle_t handle _U_)
644 {
645         dissector_table_t sub_dissectors = find_dissector_table( name);
646         dtbl_entry_t *dtbl_entry;
647
648 /* sanity check */
649         g_assert( sub_dissectors);
650
651         /*
652          * Find the entry.
653          */
654         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
655
656         if (dtbl_entry != NULL) {
657                 /*
658                  * Found - remove it.
659                  */
660                 g_hash_table_remove(sub_dissectors->hash_table,
661                     GUINT_TO_POINTER(pattern));
662
663                 /*
664                  * Now free up the entry.
665                  */
666                 g_free(dtbl_entry);
667         }
668 }
669
670 /* Change the entry for a dissector in a uint dissector table
671    with a particular pattern to use a new dissector handle. */
672 void
673 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
674 {
675         dissector_table_t sub_dissectors = find_dissector_table( name);
676         dtbl_entry_t *dtbl_entry;
677
678 /* sanity check */
679         g_assert( sub_dissectors);
680
681         /*
682          * See if the entry already exists. If so, reuse it.
683          */
684         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
685         if (dtbl_entry != NULL) {
686           dtbl_entry->current = handle;
687           return;
688         }
689
690         /*
691          * Don't create an entry if there is no dissector handle - I.E. the
692          * user said not to decode something that wasn't being decoded
693          * in the first place.
694          */
695         if (handle == NULL)
696           return;
697
698         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
699         dtbl_entry->initial = NULL;
700         dtbl_entry->current = handle;
701
702 /* do the table insertion */
703         g_hash_table_insert( sub_dissectors->hash_table,
704             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
705 }
706
707 /* Reset an entry in a uint dissector table to its initial value. */
708 void
709 dissector_reset(const char *name, guint32 pattern)
710 {
711         dissector_table_t sub_dissectors = find_dissector_table( name);
712         dtbl_entry_t *dtbl_entry;
713
714 /* sanity check */
715         g_assert( sub_dissectors);
716
717         /*
718          * Find the entry.
719          */
720         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
721
722         if (dtbl_entry == NULL)
723                 return;
724
725         /*
726          * Found - is there an initial value?
727          */
728         if (dtbl_entry->initial != NULL) {
729                 dtbl_entry->current = dtbl_entry->initial;
730         } else {
731                 g_hash_table_remove(sub_dissectors->hash_table,
732                     GUINT_TO_POINTER(pattern));
733                 g_free(dtbl_entry);
734         }
735 }
736
737 /* Look for a given value in a given uint dissector table and, if found,
738    call the dissector with the arguments supplied, and return TRUE,
739    otherwise return FALSE. */
740 gboolean
741 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
742     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
743 {
744         dtbl_entry_t *dtbl_entry;
745         struct dissector_handle *handle;
746         guint32 saved_match_port;
747         int ret;
748
749         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
750         if (dtbl_entry != NULL) {
751                 /*
752                  * Is there currently a dissector handle for this entry?
753                  */
754                 handle = dtbl_entry->current;
755                 if (handle == NULL) {
756                         /*
757                          * No - pretend this dissector didn't exist,
758                          * so that other dissectors might have a chance
759                          * to dissect this packet.
760                          */
761                         return FALSE;
762                 }
763
764                 /*
765                  * Save the current value of "pinfo->match_port",
766                  * set it to the port that matched, call the
767                  * dissector, and restore "pinfo->match_port".
768                  */
769                 saved_match_port = pinfo->match_port;
770                 pinfo->match_port = port;
771                 ret = call_dissector_work(handle, tvb, pinfo, tree);
772                 pinfo->match_port = saved_match_port;
773
774                 /*
775                  * If a new-style dissector returned 0, it means that
776                  * it didn't think this tvbuff represented a packet for
777                  * its protocol, and didn't dissect anything.
778                  *
779                  * Old-style dissectors can't reject the packet.
780                  *
781                  * 0 is also returned if the protocol wasn't enabled.
782                  *
783                  * If the packet was rejected, we return FALSE, so that
784                  * other dissectors might have a chance to dissect this
785                  * packet, otherwise we return TRUE.
786                  */
787                 return ret != 0;
788         }
789         return FALSE;
790 }
791
792 /* Look for a given value in a given uint dissector table and, if found,
793    return the dissector handle for that value. */
794 dissector_handle_t
795 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
796 {
797         dtbl_entry_t *dtbl_entry;
798
799         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
800         if (dtbl_entry != NULL)
801                 return dtbl_entry->current;
802         else
803                 return NULL;
804 }
805
806 /* Find an entry in a string dissector table. */ 
807 static dtbl_entry_t *
808 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
809 {
810         switch (sub_dissectors->type) {
811
812         case FT_STRING:
813         case FT_STRINGZ:
814                 /*
815                  * You can do a string lookup in these tables.
816                  */
817                 break;
818
819         default:
820                 /*
821                  * But you can't do a string lookup in any other types
822                  * of tables.
823                  */
824                 g_assert_not_reached();
825         }
826
827         /*
828          * Find the entry.
829          */
830         return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
831 }
832
833 /* Add an entry to a string dissector table. */
834 void
835 dissector_add_string(const char *name, gchar *pattern,
836     dissector_handle_t handle)
837 {
838         dissector_table_t sub_dissectors = find_dissector_table( name);
839         dtbl_entry_t *dtbl_entry;
840
841 /* sanity check */
842         g_assert( sub_dissectors);
843
844         switch (sub_dissectors->type) {
845
846         case FT_STRING:
847         case FT_STRINGZ:
848                 /*
849                  * You can do a string lookup in these tables.
850                  */
851                 break;
852
853         default:
854                 /*
855                  * But you can't do a string lookup in any other types
856                  * of tables.
857                  */
858                 g_assert_not_reached();
859         }
860
861         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
862         dtbl_entry->current = handle;
863         dtbl_entry->initial = dtbl_entry->current;
864
865 /* do the table insertion */
866         g_hash_table_insert( sub_dissectors->hash_table, pattern,
867             (gpointer)dtbl_entry);
868
869         /*
870          * Now add it to the list of handles that could be used with this
871          * table, because it *is* being used with this table.
872          */
873         dissector_add_handle(name, handle);
874 }
875
876 /* Delete the entry for a dissector in a string dissector table
877    with a particular pattern. */
878
879 /* NOTE: this doesn't use the dissector call variable. It is included to */
880 /*      be consistant with the dissector_add_string and more importantly to */
881 /*      be used if the technique of adding a temporary dissector is */
882 /*      implemented.  */
883 /*      If temporary dissectors are deleted, then the original dissector must */
884 /*      be available. */
885 void
886 dissector_delete_string(const char *name, const gchar *pattern,
887         dissector_handle_t handle _U_)
888 {
889         dissector_table_t sub_dissectors = find_dissector_table( name);
890         dtbl_entry_t *dtbl_entry;
891
892 /* sanity check */
893         g_assert( sub_dissectors);
894
895         /*
896          * Find the entry.
897          */
898         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
899
900         if (dtbl_entry != NULL) {
901                 /*
902                  * Found - remove it.
903                  */
904                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
905
906                 /*
907                  * Now free up the entry.
908                  */
909                 g_free(dtbl_entry);
910         }
911 }
912
913 /* Change the entry for a dissector in a string dissector table
914    with a particular pattern to use a new dissector handle. */
915 void
916 dissector_change_string(const char *name, gchar *pattern,
917     dissector_handle_t handle)
918 {
919         dissector_table_t sub_dissectors = find_dissector_table( name);
920         dtbl_entry_t *dtbl_entry;
921
922 /* sanity check */
923         g_assert( sub_dissectors);
924
925         /*
926          * See if the entry already exists. If so, reuse it.
927          */
928         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
929         if (dtbl_entry != NULL) {
930           dtbl_entry->current = handle;
931           return;
932         }
933
934         /*
935          * Don't create an entry if there is no dissector handle - I.E. the
936          * user said not to decode something that wasn't being decoded
937          * in the first place.
938          */
939         if (handle == NULL)
940           return;
941
942         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
943         dtbl_entry->initial = NULL;
944         dtbl_entry->current = handle;
945
946 /* do the table insertion */
947         g_hash_table_insert( sub_dissectors->hash_table, pattern,
948             (gpointer)dtbl_entry);
949 }
950
951 /* Reset an entry in a string sub-dissector table to its initial value. */
952 void
953 dissector_reset_string(const char *name, const gchar *pattern)
954 {
955         dissector_table_t sub_dissectors = find_dissector_table( name);
956         dtbl_entry_t *dtbl_entry;
957
958 /* sanity check */
959         g_assert( sub_dissectors);
960
961         /*
962          * Find the entry.
963          */
964         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
965
966         if (dtbl_entry == NULL)
967                 return;
968
969         /*
970          * Found - is there an initial value?
971          */
972         if (dtbl_entry->initial != NULL) {
973                 dtbl_entry->current = dtbl_entry->initial;
974         } else {
975                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
976                 g_free(dtbl_entry);
977         }
978 }
979
980 /* Look for a given string in a given dissector table and, if found, call
981    the dissector with the arguments supplied, and return TRUE, otherwise
982    return FALSE. */
983 gboolean
984 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
985     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
986 {
987         dtbl_entry_t *dtbl_entry;
988         struct dissector_handle *handle;
989         int ret;
990
991         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
992         if (dtbl_entry != NULL) {
993                 /*
994                  * Is there currently a dissector handle for this entry?
995                  */
996                 handle = dtbl_entry->current;
997                 if (handle == NULL) {
998                         /*
999                          * No - pretend this dissector didn't exist,
1000                          * so that other dissectors might have a chance
1001                          * to dissect this packet.
1002                          */
1003                         return FALSE;
1004                 }
1005
1006                 ret = call_dissector_work(handle, tvb, pinfo, tree);
1007
1008                 /*
1009                  * If a new-style dissector returned 0, it means that
1010                  * it didn't think this tvbuff represented a packet for
1011                  * its protocol, and didn't dissect anything.
1012                  *
1013                  * Old-style dissectors can't reject the packet.
1014                  *
1015                  * 0 is also returned if the protocol wasn't enabled.
1016                  *
1017                  * If the packet was rejected, we return FALSE, so that
1018                  * other dissectors might have a chance to dissect this
1019                  * packet, otherwise we return TRUE.
1020                  */
1021                 return ret != 0;
1022         }
1023         return FALSE;
1024 }
1025
1026 /* Look for a given value in a given string dissector table and, if found,
1027    return the dissector handle for that value. */
1028 dissector_handle_t
1029 dissector_get_string_handle(dissector_table_t sub_dissectors,
1030     const gchar *string)
1031 {
1032         dtbl_entry_t *dtbl_entry;
1033
1034         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1035         if (dtbl_entry != NULL)
1036                 return dtbl_entry->current;
1037         else
1038                 return NULL;
1039 }
1040
1041 dissector_handle_t
1042 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1043 {
1044         return dtbl_entry->current;
1045 }
1046
1047 /* Add a handle to the list of handles that *could* be used with this
1048    table.  That list is used by code in the UI. */
1049 void
1050 dissector_add_handle(const char *name, dissector_handle_t handle)
1051 {
1052         dissector_table_t sub_dissectors = find_dissector_table( name);
1053         GSList *entry;
1054
1055         /* sanity check */
1056         g_assert(sub_dissectors != NULL);
1057
1058         /* Is it already in this list? */
1059         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1060         if (entry != NULL) {
1061                 /*
1062                  * Yes - don't insert it again.
1063                  */
1064                 return;
1065         }
1066
1067         /* Add it to the list. */
1068         sub_dissectors->dissector_handles =
1069             g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1070 }
1071
1072 dissector_handle_t
1073 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1074 {
1075         return dtbl_entry->initial;
1076 }
1077
1078 /**************************************************/
1079 /*                                                */
1080 /*       Routines to walk dissector tables        */
1081 /*                                                */
1082 /**************************************************/
1083
1084 typedef struct dissector_foreach_info {
1085   gpointer     caller_data;
1086   DATFunc      caller_func;
1087   GHFunc       next_func;
1088   gchar       *table_name;
1089   ftenum_t     selector_type;
1090 } dissector_foreach_info_t;
1091
1092 /*
1093  * Called for each entry in a dissector table.
1094  */
1095 static void
1096 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1097 {
1098         dissector_foreach_info_t *info;
1099         dtbl_entry_t *dtbl_entry;
1100
1101         g_assert(value);
1102         g_assert(user_data);
1103
1104         dtbl_entry = value;
1105         if (dtbl_entry->current == NULL ||
1106             dtbl_entry->current->protocol == NULL) {
1107                 /*
1108                  * Either there is no dissector for this entry, or
1109                  * the dissector doesn't have a protocol associated
1110                  * with it.
1111                  *
1112                  * XXX - should the latter check be done?
1113                  */
1114                 return;
1115         }
1116
1117         info = user_data;
1118         info->caller_func(info->table_name, info->selector_type, key, value,
1119             info->caller_data);
1120 }
1121
1122 /*
1123  * Called for each entry in the table of all dissector tables.
1124  */
1125 static void
1126 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1127 {
1128         dissector_table_t sub_dissectors;
1129         dissector_foreach_info_t *info;
1130
1131         g_assert(value);
1132         g_assert(user_data);
1133
1134         sub_dissectors = value;
1135         info = user_data;
1136         info->table_name = (gchar*) key;
1137         info->selector_type = get_dissector_table_selector_type(info->table_name);
1138         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1139 }
1140
1141 /*
1142  * Walk all dissector tables calling a user supplied function on each
1143  * entry.
1144  */
1145 void
1146 dissector_all_tables_foreach (DATFunc func,
1147                               gpointer user_data)
1148 {
1149         dissector_foreach_info_t info;
1150
1151         info.caller_data = user_data;
1152         info.caller_func = func;
1153         info.next_func = dissector_table_foreach_func;
1154         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1155 }
1156
1157 /*
1158  * Walk one dissector table's hash table calling a user supplied function
1159  * on each entry.
1160  */
1161 void
1162 dissector_table_foreach (char *name,
1163                          DATFunc func,
1164                          gpointer user_data)
1165 {
1166         dissector_foreach_info_t info;
1167         dissector_table_t sub_dissectors = find_dissector_table( name);
1168
1169         info.table_name = name;
1170         info.selector_type = sub_dissectors->type;
1171         info.caller_func = func;
1172         info.caller_data = user_data;
1173         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1174 }
1175
1176 /*
1177  * Walk one dissector table's list of handles calling a user supplied
1178  * function on each entry.
1179  */
1180 void
1181 dissector_table_foreach_handle(char *name,
1182                                DATFunc_handle func,
1183                                gpointer user_data)
1184 {
1185         dissector_table_t sub_dissectors = find_dissector_table( name);
1186         GSList *tmp;
1187
1188         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1189             tmp = g_slist_next(tmp))
1190                 func(name, tmp->data, user_data);
1191 }
1192
1193 /*
1194  * Called for each entry in a dissector table.
1195  */
1196 static void
1197 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1198 {
1199         dtbl_entry_t *dtbl_entry;
1200         dissector_foreach_info_t *info;
1201
1202         g_assert(value);
1203         g_assert(user_data);
1204
1205         dtbl_entry = value;
1206         if (dtbl_entry->initial == dtbl_entry->current) {
1207                 /*
1208                  * Entry hasn't changed - don't call the function.
1209                  */
1210                 return;
1211         }
1212
1213         info = user_data;
1214         info->caller_func(info->table_name, info->selector_type, key, value,
1215             info->caller_data);
1216 }
1217
1218 /*
1219  * Walk all dissector tables calling a user supplied function only on
1220  * any entry that has been changed from its original state.
1221  */
1222 void
1223 dissector_all_tables_foreach_changed (DATFunc func,
1224                                       gpointer user_data)
1225 {
1226         dissector_foreach_info_t info;
1227
1228         info.caller_data = user_data;
1229         info.caller_func = func;
1230         info.next_func = dissector_table_foreach_changed_func;
1231         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1232 }
1233
1234 /*
1235  * Walk one dissector table calling a user supplied function only on
1236  * any entry that has been changed from its original state.
1237  */
1238 void
1239 dissector_table_foreach_changed (char *name,
1240                                  DATFunc func,
1241                                  gpointer user_data)
1242 {
1243         dissector_foreach_info_t info;
1244         dissector_table_t sub_dissectors = find_dissector_table( name);
1245
1246         info.table_name = name;
1247         info.selector_type = sub_dissectors->type;
1248         info.caller_func = func;
1249         info.caller_data = user_data;
1250         g_hash_table_foreach(sub_dissectors->hash_table,
1251             dissector_table_foreach_changed_func, &info);
1252 }
1253
1254 typedef struct dissector_foreach_table_info {
1255   gpointer      caller_data;
1256   DATFunc_table caller_func;
1257 } dissector_foreach_table_info_t;
1258
1259 /*
1260  * Called for each entry in the table of all dissector tables.
1261  */
1262 static void
1263 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1264 {
1265         dissector_table_t table;
1266         dissector_foreach_table_info_t *info;
1267
1268         table = value;
1269         info = user_data;
1270         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1271 }
1272
1273 /*
1274  * Walk all dissector tables calling a user supplied function on each
1275  * table.
1276  */
1277 void
1278 dissector_all_tables_foreach_table (DATFunc_table func,
1279                                     gpointer user_data)
1280 {
1281         dissector_foreach_table_info_t info;
1282
1283         info.caller_data = user_data;
1284         info.caller_func = func;
1285         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1286 }
1287
1288 dissector_table_t
1289 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
1290     int base)
1291 {
1292         dissector_table_t       sub_dissectors;
1293
1294         /* Create our hash-of-hashes if it doesn't already exist */
1295         if (!dissector_tables) {
1296                 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1297                 g_assert(dissector_tables);
1298         }
1299
1300         /* Make sure the registration is unique */
1301         g_assert(!g_hash_table_lookup( dissector_tables, name ));
1302
1303         /* Create and register the dissector table for this name; returns */
1304         /* a pointer to the dissector table. */
1305         sub_dissectors = g_malloc(sizeof (struct dissector_table));
1306         switch (type) {
1307
1308         case FT_UINT8:
1309         case FT_UINT16:
1310         case FT_UINT24:
1311         case FT_UINT32:
1312                 /*
1313                  * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1314                  * so we use "g_direct_hash()" and "g_direct_equal()".
1315                  */
1316                 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1317                     g_direct_equal );
1318                 break;
1319
1320         case FT_STRING:
1321         case FT_STRINGZ:
1322                 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1323                     g_str_equal );
1324                 break;
1325
1326         default:
1327                 g_assert_not_reached();
1328         }
1329         sub_dissectors->dissector_handles = NULL;
1330         sub_dissectors->ui_name = ui_name;
1331         sub_dissectors->type = type;
1332         sub_dissectors->base = base;
1333         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1334         return sub_dissectors;
1335 }
1336
1337 char *
1338 get_dissector_table_ui_name(const char *name)
1339 {
1340         dissector_table_t sub_dissectors = find_dissector_table( name);
1341
1342         return sub_dissectors->ui_name;
1343 }
1344
1345 ftenum_t
1346 get_dissector_table_selector_type(const char *name)
1347 {
1348         dissector_table_t sub_dissectors = find_dissector_table( name);
1349
1350         return sub_dissectors->type;
1351 }
1352
1353 int
1354 get_dissector_table_base(const char *name)
1355 {
1356         dissector_table_t sub_dissectors = find_dissector_table( name);
1357
1358         return sub_dissectors->base;
1359 }
1360
1361 static GHashTable *heur_dissector_lists = NULL;
1362
1363 typedef struct {
1364         heur_dissector_t dissector;
1365         protocol_t *protocol;
1366 } heur_dtbl_entry_t;
1367
1368 /* Finds a heuristic dissector table by field name. */
1369 static heur_dissector_list_t *
1370 find_heur_dissector_list(const char *name)
1371 {
1372         g_assert(heur_dissector_lists != NULL);
1373         return g_hash_table_lookup(heur_dissector_lists, name);
1374 }
1375
1376 void
1377 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1378 {
1379         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1380         heur_dtbl_entry_t *dtbl_entry;
1381
1382         /* sanity check */
1383         g_assert(sub_dissectors != NULL);
1384
1385         dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1386         dtbl_entry->dissector = dissector;
1387         dtbl_entry->protocol = find_protocol_by_id(proto);
1388
1389         /* do the table insertion */
1390         *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1391 }
1392
1393 gboolean
1394 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1395     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1396 {
1397         gboolean status;
1398         const char *saved_proto;
1399         GSList *entry;
1400         heur_dtbl_entry_t *dtbl_entry;
1401         guint16 saved_can_desegment;
1402
1403         /* can_desegment is set to 2 by anyone which offers this api/service.
1404            then everytime a subdissector is called it is decremented by one.
1405            thus only the subdissector immediately ontop of whoever offers this
1406            service can use it.
1407         */
1408         saved_can_desegment=pinfo->can_desegment;
1409         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1410
1411         status = FALSE;
1412         saved_proto = pinfo->current_proto;
1413         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1414                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1415                 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1416                 if (dtbl_entry->protocol != NULL &&
1417                     !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1418                         /*
1419                          * No - don't try this dissector.
1420                          */
1421                         continue;
1422                 }
1423
1424                 if (dtbl_entry->protocol != NULL) {
1425                         pinfo->current_proto =
1426                             proto_get_protocol_short_name(dtbl_entry->protocol);
1427                 }
1428                 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1429                         status = TRUE;
1430                         break;
1431                 }
1432         }
1433         pinfo->current_proto = saved_proto;
1434         pinfo->can_desegment=saved_can_desegment;
1435         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
1436         return status;
1437 }
1438
1439 void
1440 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1441 {
1442         /* Create our hash-of-lists if it doesn't already exist */
1443         if (heur_dissector_lists == NULL) {
1444                 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1445                 g_assert(heur_dissector_lists != NULL);
1446         }
1447
1448         /* Make sure the registration is unique */
1449         g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1450
1451         *sub_dissectors = NULL; /* initially empty */
1452         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1453             (gpointer) sub_dissectors);
1454 }
1455
1456 /*
1457  * Register dissectors by name; used if one dissector always calls a
1458  * particular dissector, or if it bases the decision of which dissector
1459  * to call on something other than a numerical value or on "try a bunch
1460  * of dissectors until one likes the packet".
1461  */
1462
1463 /*
1464  * List of registered dissectors.
1465  */
1466 static GHashTable *registered_dissectors = NULL;
1467
1468 /* Get the short name of the protocol for a dissector handle. */
1469 char *
1470 dissector_handle_get_short_name(dissector_handle_t handle)
1471 {
1472         return proto_get_protocol_short_name(handle->protocol);
1473 }
1474
1475 /* Get the index of the protocol for a dissector handle. */
1476 int
1477 dissector_handle_get_protocol_index(dissector_handle_t handle)
1478 {
1479         return proto_get_id(handle->protocol);
1480 }
1481
1482 /* Find a registered dissector by name. */
1483 dissector_handle_t
1484 find_dissector(const char *name)
1485 {
1486         g_assert(registered_dissectors != NULL);
1487         return g_hash_table_lookup(registered_dissectors, name);
1488 }
1489
1490 /* Create an anonymous handle for a dissector. */
1491 dissector_handle_t
1492 create_dissector_handle(dissector_t dissector, int proto)
1493 {
1494         struct dissector_handle *handle;
1495
1496         handle = g_malloc(sizeof (struct dissector_handle));
1497         handle->name = NULL;
1498         handle->is_new = FALSE;
1499         handle->dissector.old = dissector;
1500         handle->protocol = find_protocol_by_id(proto);
1501
1502         return handle;
1503 }
1504
1505 dissector_handle_t
1506 new_create_dissector_handle(new_dissector_t dissector, int proto)
1507 {
1508         struct dissector_handle *handle;
1509
1510         handle = g_malloc(sizeof (struct dissector_handle));
1511         handle->name = NULL;
1512         handle->is_new = TRUE;
1513         handle->dissector.new = dissector;
1514         handle->protocol = find_protocol_by_id(proto);
1515
1516         return handle;
1517 }
1518
1519 /* Register a dissector by name. */
1520 void
1521 register_dissector(const char *name, dissector_t dissector, int proto)
1522 {
1523         struct dissector_handle *handle;
1524
1525         /* Create our hash table if it doesn't already exist */
1526         if (registered_dissectors == NULL) {
1527                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1528                 g_assert(registered_dissectors != NULL);
1529         }
1530
1531         /* Make sure the registration is unique */
1532         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1533
1534         handle = g_malloc(sizeof (struct dissector_handle));
1535         handle->name = name;
1536         handle->is_new = FALSE;
1537         handle->dissector.old = dissector;
1538         handle->protocol = find_protocol_by_id(proto);
1539
1540         g_hash_table_insert(registered_dissectors, (gpointer)name,
1541             (gpointer) handle);
1542 }
1543
1544 void
1545 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1546 {
1547         struct dissector_handle *handle;
1548
1549         /* Create our hash table if it doesn't already exist */
1550         if (registered_dissectors == NULL) {
1551                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1552                 g_assert(registered_dissectors != NULL);
1553         }
1554
1555         /* Make sure the registration is unique */
1556         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1557
1558         handle = g_malloc(sizeof (struct dissector_handle));
1559         handle->name = name;
1560         handle->is_new = TRUE;
1561         handle->dissector.new = dissector;
1562         handle->protocol = find_protocol_by_id(proto);
1563
1564         g_hash_table_insert(registered_dissectors, (gpointer)name,
1565             (gpointer) handle);
1566 }
1567
1568 /* Call a dissector through a handle. */
1569 int
1570 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1571     packet_info *pinfo, proto_tree *tree)
1572 {
1573         int ret;
1574
1575         ret = call_dissector_work(handle, tvb, pinfo, tree);
1576         if (ret == 0) {
1577                 /*
1578                  * The protocol was disabled, or the dissector rejected
1579                  * it.  Just dissect this packet as data.
1580                  */
1581                 g_assert(data_handle != NULL);
1582                 g_assert(data_handle->protocol != NULL);
1583                 call_dissector(data_handle, tvb, pinfo, tree);
1584                 return tvb_length(tvb);
1585         }
1586         return ret;
1587 }