Add wtap_pseudo_header union to wtap_pkthdr structure.
[metze/wireshark/wip.git] / epan / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #ifdef HAVE_STDARG_H
33 #include <stdarg.h>
34 #endif
35
36 #include <string.h>
37 #include <ctype.h>
38 #include <time.h>
39
40 #include "packet.h"
41 #include "timestamp.h"
42
43 #include "atalk-utils.h"
44 #include "sna-utils.h"
45 #include "osi-utils.h"
46 #include "to_str.h"
47
48 #include "addr_resolv.h"
49 #include "tvbuff.h"
50 #include "plugins.h"
51 #include "epan_dissect.h"
52
53 #include "emem.h"
54
55 #include <epan/reassemble.h>
56 #include <epan/stream.h>
57 #include <epan/expert.h>
58
59 static gint proto_malformed = -1;
60 static dissector_handle_t frame_handle = NULL;
61 static dissector_handle_t data_handle = NULL;
62
63 void
64 packet_init(void)
65 {
66         frame_handle = find_dissector("frame");
67         data_handle = find_dissector("data");
68         proto_malformed = proto_get_id_by_filter_name("malformed");
69 }
70
71 void
72 packet_cleanup(void)
73 {
74         /* nothing */
75 }
76
77 /*
78  * Given a tvbuff, and a length from a packet header, adjust the length
79  * of the tvbuff to reflect the specified length.
80  */
81 void
82 set_actual_length(tvbuff_t *tvb, const guint specified_len)
83 {
84         if (specified_len < tvb_reported_length(tvb)) {
85                 /* Adjust the length of this tvbuff to include only the specified
86                    payload length.
87
88                    The dissector above the one calling us (the dissector above is
89                    probably us) may use that to determine how much of its packet
90                    was padding. */
91                 tvb_set_reported_length(tvb, specified_len);
92         }
93 }
94
95 /* Allow protocols to register "init" routines, which are called before
96    we make a pass through a capture file and dissect all its packets
97    (e.g., when we read in a new capture file, or run a "filter packets"
98    or "colorize packets" pass over the current capture file). */
99 static GSList *init_routines;
100
101 void
102 register_init_routine(void (*func)(void))
103 {
104         init_routines = g_slist_append(init_routines, (gpointer)func);
105 }
106
107 typedef void (*void_func_t)(void);
108
109 /* Initialize all data structures used for dissection. */
110 static void
111 call_init_routine(gpointer routine, gpointer dummy _U_)
112 {
113         void_func_t func = (void_func_t)routine;
114         (*func)();
115 }
116
117 /*
118  * XXX - for now, these are the same; the "init" routines free whatever
119  * stuff is left over from any previous dissection, and then initialize
120  * their tables.
121  *
122  * We should probably split that into "init" and "cleanup" routines, for
123  * cleanliness' sake.
124  */
125 void
126 init_dissection(void)
127 {
128         /* Reclaim and reinitialize all memory of seasonal scope */
129         se_free_all();
130
131         /*
132          * Reinitialize resolution information. We do initialization here in
133          * case we need to resolve between captures.
134          */
135         host_name_lookup_cleanup();
136         host_name_lookup_init();
137
138         /* Initialize the table of conversations. */
139         epan_conversation_init();
140
141         /* Initialize the table of circuits. */
142         epan_circuit_init();
143
144         /* Initialize protocol-specific variables. */
145         g_slist_foreach(init_routines, &call_init_routine, NULL);
146
147         /* Initialize the stream-handling tables */
148         stream_init();
149
150         /* Initialize the expert infos */
151         expert_init();
152 }
153
154 void
155 cleanup_dissection(void)
156 {
157         /* Cleanup the table of conversations. Do this before freeing seasonal
158          * memory (at least until conversation's use of g_slist is changed).
159          */
160         epan_conversation_cleanup();
161
162         /* Reclaim all memory of seasonal scope */
163         se_free_all();
164
165         /* Cleanup the table of circuits. */
166         epan_circuit_cleanup();
167
168         /* TODO: Introduce cleanup_routines */
169         /* Cleanup protocol-specific variables. */
170         g_slist_foreach(init_routines, &call_init_routine, NULL);
171
172         /* Cleanup the stream-handling tables */
173         stream_cleanup();
174
175         /* Initialize the expert infos */
176         expert_cleanup();
177
178         /*
179          * Reinitialize resolution information. We do initialization here in
180          * case we need to resolve between captures.
181          */
182         host_name_lookup_cleanup();
183         host_name_lookup_init();
184 }
185
186 /* Allow protocols to register a "cleanup" routine to be
187  * run after the initial sequential run through the packets.
188  * Note that the file can still be open after this; this is not
189  * the final cleanup. */
190 static GSList *postseq_cleanup_routines;
191
192 void
193 register_postseq_cleanup_routine(void_func_t func)
194 {
195         postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
196                         (gpointer)func);
197 }
198
199 /* Call all the registered "postseq_cleanup" routines. */
200 static void
201 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
202 {
203         void_func_t func = (void_func_t)routine;
204         (*func)();
205 }
206
207 void
208 postseq_cleanup_all_protocols(void)
209 {
210         g_slist_foreach(postseq_cleanup_routines,
211                         &call_postseq_cleanup_routine, NULL);
212 }
213
214 /*
215  * Add a new data source to the list of data sources for a frame, given
216  * the tvbuff for the data source and its name.
217  */
218 void
219 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
220 {
221         data_source *src;
222
223         src = ep_alloc(sizeof (data_source));
224         src->tvb = tvb;
225         src->name_initialized = FALSE;
226         src->name = name;
227         pinfo->data_src = g_slist_append(pinfo->data_src, src);
228 }
229
230 const char*
231 get_data_source_name(data_source *src)
232 {
233         if (!src->name_initialized) {
234                 src->name = ep_strdup_printf("%s (%u bytes)", src->name, tvb_length(src->tvb));
235                 src->name_initialized = TRUE;
236         }
237
238         return src->name;
239 }
240
241 /*
242  * Free up a frame's list of data sources.
243  */
244 void
245 free_data_sources(packet_info *pinfo)
246 {
247         if (pinfo->data_src) {
248                 g_slist_free(pinfo->data_src);
249                 pinfo->data_src = NULL;
250         }
251 }
252
253 void
254 mark_frame_as_depended_upon(packet_info *pinfo, guint32 frame_num)
255 {
256         /* Don't mark a frame as dependent on itself */
257         if (frame_num != PINFO_FD_NUM(pinfo)) {
258                 pinfo->dependent_frames = g_slist_prepend(pinfo->dependent_frames, GUINT_TO_POINTER(frame_num));
259         }
260 }
261
262 /* Allow dissectors to register a "final_registration" routine
263  * that is run like the proto_register_XXX() routine, but at the
264  * end of the epan_init() function; that is, *after* all other
265  * subsystems, like dfilters, have finished initializing. This is
266  * useful for dissector registration routines which need to compile
267  * display filters. dfilters can't initialize itself until all protocols
268  * have registered themselves. */
269 static GSList *final_registration_routines;
270
271 void
272 register_final_registration_routine(void (*func)(void))
273 {
274         final_registration_routines = g_slist_append(final_registration_routines,
275                         (gpointer)func);
276 }
277
278 /* Call all the registered "final_registration" routines. */
279 static void
280 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
281 {
282         void_func_t func = (void_func_t)routine;
283
284         (*func)();
285 }
286
287 void
288 final_registration_all_protocols(void)
289 {
290         g_slist_foreach(final_registration_routines,
291                         &call_final_registration_routine, NULL);
292 }
293
294
295 /* Creates the top-most tvbuff and calls dissect_frame() */
296 void
297 dissect_packet(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
298                const guchar *pd, frame_data *fd, column_info *cinfo)
299 {
300         if (cinfo != NULL)
301                 col_init(cinfo);
302         memset(&edt->pi, 0, sizeof(edt->pi));
303         edt->pi.current_proto = "<Missing Protocol Name>";
304         edt->pi.cinfo = cinfo;
305         edt->pi.fd = fd;
306         edt->pi.phdr = phdr;
307         edt->pi.pseudo_header = &phdr->pseudo_header;
308         edt->pi.dl_src.type = AT_NONE;
309         edt->pi.dl_dst.type = AT_NONE;
310         edt->pi.net_src.type = AT_NONE;
311         edt->pi.net_dst.type = AT_NONE;
312         edt->pi.src.type = AT_NONE;
313         edt->pi.dst.type = AT_NONE;
314         edt->pi.ctype = CT_NONE;
315         edt->pi.noreassembly_reason = "";
316         edt->pi.ptype = PT_NONE;
317         edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
318         edt->pi.dcetransporttype = -1;
319         edt->pi.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
320         edt->pi.dcerpc_procedure_name="";
321         edt->pi.link_dir = LINK_DIR_UNKNOWN;
322         edt->tvb = NULL;
323
324         /* to enable decode as for ethertype=0x0000 (fix for bug 4721) */
325         edt->pi.ethertype = G_MAXINT;
326
327         EP_CHECK_CANARY(("before dissecting frame %d",fd->num));
328
329         TRY {
330                 /*
331                  * XXX - currently, the length arguments to
332                  * tvb_new_real_data() are signed, but the captured
333                  * and reported length values are unsigned; this means
334                  * that length values > 2^31 - 1 will appear as
335                  * negative lengths in tvb_new_real_data().
336                  *
337                  * Captured length values that large will already
338                  * have been filtered out by the Wiretap modules
339                  * (the file will be reported as corrupted), to
340                  * avoid trying to allocate large chunks of data.
341                  *
342                  * Reported length values will not have been
343                  * filtered out, and should not be filtered out,
344                  * as those lengths are not necessarily invalid.
345                  *
346                  * For now, we clip the reported length at G_MAXINT,
347                  * so that tvb_new_real_data() doesn't fail.  It
348                  * would throw an exception, which we'd catch, but
349                  * that would mean we would have no tvbuffs
350                  * associated with edt, which would upset much of
351                  * the rest of the application.
352                  */
353                 edt->tvb = tvb_new_real_data(pd, fd->cap_len,
354                     fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len);
355                 /* Add this tvbuffer into the data_src list */
356                 add_new_data_source(&edt->pi, edt->tvb, "Frame");
357
358                 /* Even though dissect_frame() catches all the exceptions a
359                  * sub-dissector can throw, dissect_frame() itself may throw
360                  * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
361                  * in this function. */
362                 if(frame_handle != NULL)
363                   call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
364
365         }
366         CATCH(BoundsError) {
367                 g_assert_not_reached();
368         }
369         CATCH(ReportedBoundsError) {
370                 if(proto_malformed != -1){
371                         proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
372                                                        "[Malformed Frame: Packet Length]" );
373                 } else {
374                         g_assert_not_reached();
375                 }
376         }
377         CATCH(OutOfMemoryError) {
378                 RETHROW;
379         }
380         ENDTRY;
381
382         EP_CHECK_CANARY(("after dissecting frame %d",fd->num));
383
384         fd->flags.visited = 1;
385 }
386
387 /*********************** code added for sub-dissector lookup *********************/
388
389 /*
390  * A dissector handle.
391  */
392 struct dissector_handle {
393         const char      *name;          /* dissector name */
394         gboolean        is_new;         /* TRUE if new-style dissector */
395         union {
396                 dissector_t     old;
397                 new_dissector_t new;
398         } dissector;
399         protocol_t      *protocol;
400 };
401
402 /* This function will return
403  * old style dissector :
404  *   length of the payload or 1 of the payload is empty
405  * new dissector :
406  *   >0  this protocol was successfully dissected and this was this protocol.
407  *   0   this packet did not match this protocol.
408  *
409  * The only time this function will return 0 is if it is a new style dissector
410  * and if the dissector rejected the packet.
411  */
412 static int
413 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
414                               packet_info *pinfo, proto_tree *tree, void *data)
415 {
416         const char *saved_proto;
417         int         ret;
418
419         saved_proto = pinfo->current_proto;
420
421         if (handle->protocol != NULL) {
422                 pinfo->current_proto =
423                         proto_get_protocol_short_name(handle->protocol);
424         }
425
426         if (handle->is_new) {
427                 EP_CHECK_CANARY(("before calling handle->dissector.new for %s",handle->name));
428                 ret = (*handle->dissector.new)(tvb, pinfo, tree, data);
429                 EP_CHECK_CANARY(("after calling handle->dissector.new for %s",handle->name));
430         } else {
431                 EP_CHECK_CANARY(("before calling handle->dissector.old for %s",handle->name));
432                 (*handle->dissector.old)(tvb, pinfo, tree);
433                 EP_CHECK_CANARY(("after calling handle->dissector.old for %s",handle->name));
434                 ret = tvb_length(tvb);
435                 if (ret == 0) {
436                         /*
437                          * XXX - a tvbuff can have 0 bytes of data in
438                          * it, so we have to make sure we don't return
439                          * 0.
440                          */
441                         ret = 1;
442                 }
443         }
444
445         pinfo->current_proto = saved_proto;
446
447         return ret;
448 }
449
450 /*
451  * Call a dissector through a handle.
452  * If the protocol for that handle isn't enabled, return 0 without
453  * calling the dissector.
454  * Otherwise, if the handle refers to a new-style dissector, call the
455  * dissector and return its return value, otherwise call it and return
456  * the length of the tvbuff pointed to by the argument.
457  */
458
459 static int
460 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
461                           packet_info *pinfo_arg, proto_tree *tree, void *);
462
463 static int
464 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb, packet_info *pinfo_arg,
465                     proto_tree *tree, gboolean add_proto_name, void *data)
466 {
467         packet_info *pinfo = pinfo_arg;
468         const char  *saved_proto;
469         guint16      saved_can_desegment;
470         int          ret;
471         gint         saved_layer_names_len = 0;
472
473         if (handle->protocol != NULL &&
474             !proto_is_protocol_enabled(handle->protocol)) {
475                 /*
476                  * The protocol isn't enabled.
477                  */
478                 return 0;
479         }
480
481         saved_proto = pinfo->current_proto;
482         saved_can_desegment = pinfo->can_desegment;
483
484         if (pinfo->layer_names != NULL)
485                 saved_layer_names_len = (gint) pinfo->layer_names->len;
486
487         /*
488          * can_desegment is set to 2 by anyone which offers the
489          * desegmentation api/service.
490          * Then everytime a subdissector is called it is decremented
491          * by one.
492          * Thus only the subdissector immediately on top of whoever
493          * offers this service can use it.
494          * We save the current value of "can_desegment" for the
495          * benefit of TCP proxying dissectors such as SOCKS, so they
496          * can restore it and allow the dissectors they call to use
497          * the desegmentation service.
498          */
499         pinfo->saved_can_desegment = saved_can_desegment;
500         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
501         if (handle->protocol != NULL) {
502                 pinfo->current_proto =
503                         proto_get_protocol_short_name(handle->protocol);
504
505                 /*
506                  * Add the protocol name to the layers
507                  * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
508                  */
509                 if ((pinfo->layer_names)&&(add_proto_name)) {
510                         if (pinfo->layer_names->len > 0)
511                                 g_string_append(pinfo->layer_names, ":");
512                                 g_string_append(pinfo->layer_names,
513                                 proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
514                 }
515         }
516
517         if (pinfo->flags.in_error_pkt) {
518                 ret = call_dissector_work_error(handle, tvb, pinfo, tree, data);
519         } else {
520                 /*
521                  * Just call the subdissector.
522                  */
523                 ret = call_dissector_through_handle(handle, tvb, pinfo, tree, data);
524         }
525         if (ret == 0) {
526                 /*
527                  * That dissector didn't accept the packet, so
528                  * remove its protocol's name from the list
529                  * of protocols.
530                  */
531                 if ((pinfo->layer_names != NULL)&&(add_proto_name)) {
532                         g_string_truncate(pinfo->layer_names, saved_layer_names_len);
533                 }
534         }
535         pinfo->current_proto = saved_proto;
536         pinfo->can_desegment = saved_can_desegment;
537         return ret;
538 }
539
540
541 static int
542 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
543                           packet_info *pinfo_arg, proto_tree *tree, void *data)
544 {
545         packet_info  *pinfo = pinfo_arg;
546         const char   *saved_proto;
547         guint16       saved_can_desegment;
548         volatile int  ret = 0;
549         gboolean      save_writable;
550         address       save_dl_src;
551         address       save_dl_dst;
552         address       save_net_src;
553         address       save_net_dst;
554         address       save_src;
555         address       save_dst;
556
557         /*
558         * This isn't a packet being transported inside
559         * the protocol whose dissector is calling us,
560         * it's a copy of a packet that caused an error
561         * in some protocol included in a packet that
562         * reports the error (e.g., an ICMP Unreachable
563         * packet).
564         */
565
566         /*
567         * Save the current state of the writability of
568         * the columns, and restore them after the
569         * dissector returns, so that the columns
570         * don't reflect the packet that got the error,
571         * they reflect the packet that reported the
572         * error.
573         */
574         saved_proto = pinfo->current_proto;
575         saved_can_desegment = pinfo->can_desegment;
576
577         save_writable = col_get_writable(pinfo->cinfo);
578         col_set_writable(pinfo->cinfo, FALSE);
579         save_dl_src   = pinfo->dl_src;
580         save_dl_dst   = pinfo->dl_dst;
581         save_net_src  = pinfo->net_src;
582         save_net_dst  = pinfo->net_dst;
583         save_src      = pinfo->src;
584         save_dst      = pinfo->dst;
585
586         /* Dissect the contained packet. */
587         TRY {
588                 ret = call_dissector_through_handle(handle, tvb,pinfo, tree, data);
589         }
590         CATCH(BoundsError) {
591                 /*
592                 * Restore the column writability and addresses.
593                 */
594                 col_set_writable(pinfo->cinfo, save_writable);
595                 pinfo->dl_src  = save_dl_src;
596                 pinfo->dl_dst  = save_dl_dst;
597                 pinfo->net_src = save_net_src;
598                 pinfo->net_dst = save_net_dst;
599                 pinfo->src     = save_src;
600                 pinfo->dst     = save_dst;
601
602                 /*
603                 * Restore the current protocol, so any
604                 * "Short Frame" indication reflects that
605                 * protocol, not the protocol for the
606                 * packet that got the error.
607                 */
608                 pinfo->current_proto = saved_proto;
609
610                 /*
611                 * Restore the desegmentability state.
612                 */
613                 pinfo->can_desegment = saved_can_desegment;
614
615                 /*
616                 * Rethrow the exception, so this will be
617                 * reported as a short frame.
618                 */
619                 RETHROW;
620         }
621         CATCH(ReportedBoundsError) {
622                 /*
623                 * "ret" wasn't set because an exception was thrown
624                 * before "call_dissector_through_handle()" returned.
625                 * As it called something, at least one dissector
626                 * accepted the packet, and, as an exception was
627                 * thrown, not only was all the tvbuff dissected,
628                 * a dissector tried dissecting past the end of
629                 * the data in some tvbuff, so we'll assume that
630                 * the entire tvbuff was dissected.
631                 */
632                 ret = tvb_length(tvb);
633         }
634         CATCH(OutOfMemoryError) {
635                 RETHROW;
636         }
637         ENDTRY;
638
639         col_set_writable(pinfo->cinfo, save_writable);
640         pinfo->dl_src  = save_dl_src;
641         pinfo->dl_dst  = save_dl_dst;
642         pinfo->net_src = save_net_src;
643         pinfo->net_dst = save_net_dst;
644         pinfo->src     = save_src;
645         pinfo->dst     = save_dst;
646         pinfo->want_pdu_tracking = 0;
647         return ret;
648 }
649
650 /*
651  * An entry in the hash table portion of a dissector table.
652  */
653 struct dtbl_entry {
654         dissector_handle_t initial;
655         dissector_handle_t current;
656 };
657
658 /*
659  * A dissector table.
660  *
661  * "hash_table" is a hash table, indexed by port number, supplying
662  * a "struct dtbl_entry"; it records what dissector is assigned to
663  * that uint or string value in that table.
664  *
665  * "dissector_handles" is a list of all dissectors that *could* be
666  * used in that table; not all of them are necessarily in the table,
667  * as they may be for protocols that don't have a fixed uint value,
668  * e.g. for TCP or UDP port number tables and protocols with no fixed
669  * port number.
670  *
671  * "ui_name" is the name the dissector table has in the user interface.
672  *
673  * "type" is a field type giving the width of the uint value for that
674  * dissector table, if it's a uint dissector table.
675  *
676  * "base" is the base in which to display the uint value for that
677  * dissector table, if it's a uint dissector table.
678  */
679 struct dissector_table {
680         GHashTable      *hash_table;
681         GSList          *dissector_handles;
682         const char      *ui_name;
683         ftenum_t        type;
684         int             base;
685 };
686
687 static GHashTable *dissector_tables = NULL;
688
689 /* Finds a dissector table by table name. */
690 dissector_table_t
691 find_dissector_table(const char *name)
692 {
693         g_assert(dissector_tables);
694         return g_hash_table_lookup( dissector_tables, name );
695 }
696
697 /* Find an entry in a uint dissector table. */
698 static dtbl_entry_t *
699 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
700 {
701         switch (sub_dissectors->type) {
702
703         case FT_UINT8:
704         case FT_UINT16:
705         case FT_UINT24:
706         case FT_UINT32:
707                 /*
708                  * You can do a uint lookup in these tables.
709                  */
710                 break;
711
712         default:
713                 /*
714                  * But you can't do a uint lookup in any other types
715                  * of tables.
716                  */
717                 g_assert_not_reached();
718         }
719
720         /*
721          * Find the entry.
722          */
723         return g_hash_table_lookup(sub_dissectors->hash_table,
724                                    GUINT_TO_POINTER(pattern));
725 }
726
727 #if 0
728 static void
729 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
730 {
731         dtbl_entry_t *dtbl_entry;
732
733         if (pattern == 0) {
734                 g_warning("%s: %s registering using a pattern of 0",
735                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
736         }
737
738         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
739         if (dtbl_entry != NULL) {
740                 g_warning("%s: %s registering using pattern %d already registered by %s",
741                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
742                           pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
743         }
744 }
745 #endif
746
747 /* Add an entry to a uint dissector table. */
748 void
749 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
750 {
751         dissector_table_t  sub_dissectors;
752         dtbl_entry_t      *dtbl_entry;
753
754         sub_dissectors = find_dissector_table(name);
755
756         /*
757          * Make sure the dissector table exists.
758          */
759         if (sub_dissectors == NULL) {
760                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
761                     name);
762                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
763                     proto_get_protocol_long_name(handle->protocol));
764                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
765                         abort();
766                 return;
767         }
768
769         /* sanity checks */
770         g_assert(handle!=NULL);
771         switch (sub_dissectors->type) {
772
773         case FT_UINT8:
774         case FT_UINT16:
775         case FT_UINT24:
776         case FT_UINT32:
777                 /*
778                  * You can do a uint lookup in these tables.
779                  */
780                 break;
781
782         default:
783                 /*
784                  * But you can't do a uint lookup in any other types
785                  * of tables.
786                  */
787                 g_assert_not_reached();
788         }
789
790 #if 0
791         dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
792 #endif
793
794         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
795         dtbl_entry->current = handle;
796         dtbl_entry->initial = dtbl_entry->current;
797
798         /* do the table insertion */
799         g_hash_table_insert( sub_dissectors->hash_table,
800                              GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
801
802         /*
803          * Now add it to the list of handles that could be used with this
804          * table, because it *is* being used with this table.
805          */
806         dissector_add_handle(name, handle);
807 }
808
809 /* Delete the entry for a dissector in a uint dissector table
810    with a particular pattern. */
811
812 /* NOTE: this doesn't use the dissector call variable. It is included to */
813 /*      be consistant with the dissector_add_uint and more importantly to be used */
814 /*      if the technique of adding a temporary dissector is implemented.  */
815 /*      If temporary dissectors are deleted, then the original dissector must */
816 /*      be available. */
817 void
818 dissector_delete_uint(const char *name, const guint32 pattern,
819         dissector_handle_t handle _U_)
820 {
821         dissector_table_t sub_dissectors = find_dissector_table( name);
822         dtbl_entry_t *dtbl_entry;
823
824         /* sanity check */
825         g_assert( sub_dissectors);
826
827         /*
828          * Find the entry.
829          */
830         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
831
832         if (dtbl_entry != NULL) {
833                 /*
834                  * Found - remove it.
835                  */
836                 g_hash_table_remove(sub_dissectors->hash_table,
837                                     GUINT_TO_POINTER(pattern));
838         }
839 }
840
841 /* Change the entry for a dissector in a uint dissector table
842    with a particular pattern to use a new dissector handle. */
843 void
844 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
845 {
846         dissector_table_t sub_dissectors = find_dissector_table( name);
847         dtbl_entry_t *dtbl_entry;
848
849         /* sanity check */
850         g_assert( sub_dissectors);
851
852         /*
853          * See if the entry already exists. If so, reuse it.
854          */
855         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
856         if (dtbl_entry != NULL) {
857                 dtbl_entry->current = handle;
858                 return;
859         }
860
861         /*
862          * Don't create an entry if there is no dissector handle - I.E. the
863          * user said not to decode something that wasn't being decoded
864          * in the first place.
865          */
866         if (handle == NULL)
867                 return;
868
869         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
870         dtbl_entry->initial = NULL;
871         dtbl_entry->current = handle;
872
873         /* do the table insertion */
874         g_hash_table_insert( sub_dissectors->hash_table,
875                              GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
876 }
877
878 /* Reset an entry in a uint dissector table to its initial value. */
879 void
880 dissector_reset_uint(const char *name, const guint32 pattern)
881 {
882         dissector_table_t  sub_dissectors = find_dissector_table( name);
883         dtbl_entry_t      *dtbl_entry;
884
885         /* sanity check */
886         g_assert( sub_dissectors);
887
888         /*
889          * Find the entry.
890          */
891         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
892
893         if (dtbl_entry == NULL)
894                 return;
895
896         /*
897          * Found - is there an initial value?
898          */
899         if (dtbl_entry->initial != NULL) {
900                 dtbl_entry->current = dtbl_entry->initial;
901         } else {
902                 g_hash_table_remove(sub_dissectors->hash_table,
903                                     GUINT_TO_POINTER(pattern));
904         }
905 }
906
907 /* Look for a given value in a given uint dissector table and, if found,
908    call the dissector with the arguments supplied, and return TRUE,
909    otherwise return FALSE. */
910
911 gboolean
912 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
913                        tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
914                        const gboolean add_proto_name, void *data)
915 {
916         dtbl_entry_t            *dtbl_entry;
917         struct dissector_handle *handle;
918         guint32                  saved_match_uint;
919         int ret;
920
921         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
922         if (dtbl_entry != NULL) {
923                 /*
924                  * Is there currently a dissector handle for this entry?
925                  */
926                 handle = dtbl_entry->current;
927                 if (handle == NULL) {
928                         /*
929                          * No - pretend this dissector didn't exist,
930                          * so that other dissectors might have a chance
931                          * to dissect this packet.
932                          */
933                         return FALSE;
934                 }
935
936                 /*
937                  * Save the current value of "pinfo->match_uint",
938                  * set it to the uint_val that matched, call the
939                  * dissector, and restore "pinfo->match_uint".
940                  */
941                 saved_match_uint  = pinfo->match_uint;
942                 pinfo->match_uint = uint_val;
943                 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, data);
944                 pinfo->match_uint = saved_match_uint;
945
946                 /*
947                  * If a new-style dissector returned 0, it means that
948                  * it didn't think this tvbuff represented a packet for
949                  * its protocol, and didn't dissect anything.
950                  *
951                  * Old-style dissectors can't reject the packet.
952                  *
953                  * 0 is also returned if the protocol wasn't enabled.
954                  *
955                  * If the packet was rejected, we return FALSE, so that
956                  * other dissectors might have a chance to dissect this
957                  * packet, otherwise we return TRUE.
958                  */
959                 return ret != 0;
960         }
961         return FALSE;
962 }
963
964 gboolean
965 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
966                    tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
967 {
968
969         return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE, NULL);
970 }
971
972 /* Look for a given value in a given uint dissector table and, if found,
973    return the dissector handle for that value. */
974 dissector_handle_t
975 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
976 {
977         dtbl_entry_t *dtbl_entry;
978
979         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
980         if (dtbl_entry != NULL)
981                 return dtbl_entry->current;
982         else
983                 return NULL;
984 }
985
986 /* Find an entry in a string dissector table. */
987 static dtbl_entry_t *
988 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
989 {
990         switch (sub_dissectors->type) {
991
992         case FT_STRING:
993         case FT_STRINGZ:
994                 /*
995                  * You can do a string lookup in these tables.
996                  */
997                 break;
998
999         default:
1000                 /*
1001                  * But you can't do a string lookup in any other types
1002                  * of tables.
1003                  */
1004                 g_assert_not_reached();
1005         }
1006
1007         /*
1008          * Find the entry.
1009          */
1010         return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
1011 }
1012
1013 /* Add an entry to a string dissector table. */
1014 void
1015 dissector_add_string(const char *name, const gchar *pattern,
1016                      dissector_handle_t handle)
1017 {
1018         dissector_table_t  sub_dissectors = find_dissector_table( name);
1019         dtbl_entry_t      *dtbl_entry;
1020
1021         /*
1022          * Make sure the dissector table exists.
1023          */
1024         if (sub_dissectors == NULL) {
1025                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1026                     name);
1027                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1028                     proto_get_protocol_long_name(handle->protocol));
1029                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1030                         abort();
1031                 return;
1032         }
1033
1034         /* sanity checks */
1035         g_assert(handle!=NULL);
1036         switch (sub_dissectors->type) {
1037
1038         case FT_STRING:
1039         case FT_STRINGZ:
1040                 /*
1041                  * You can do a string lookup in these tables.
1042                  */
1043                 break;
1044
1045         default:
1046                 /*
1047                  * But you can't do a string lookup in any other types
1048                  * of tables.
1049                  */
1050                 g_assert_not_reached();
1051         }
1052
1053         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1054         dtbl_entry->current = handle;
1055         dtbl_entry->initial = dtbl_entry->current;
1056
1057         /* do the table insertion */
1058         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1059                              (gpointer)dtbl_entry);
1060
1061         /*
1062          * Now add it to the list of handles that could be used with this
1063          * table, because it *is* being used with this table.
1064          */
1065         dissector_add_handle(name, handle);
1066 }
1067
1068 /* Delete the entry for a dissector in a string dissector table
1069    with a particular pattern. */
1070
1071 /* NOTE: this doesn't use the dissector call variable. It is included to */
1072 /*      be consistant with the dissector_add_string and more importantly to */
1073 /*      be used if the technique of adding a temporary dissector is */
1074 /*      implemented.  */
1075 /*      If temporary dissectors are deleted, then the original dissector must */
1076 /*      be available. */
1077 void
1078 dissector_delete_string(const char *name, const gchar *pattern,
1079         dissector_handle_t handle _U_)
1080 {
1081         dissector_table_t  sub_dissectors = find_dissector_table( name);
1082         dtbl_entry_t      *dtbl_entry;
1083
1084         /* sanity check */
1085         g_assert( sub_dissectors);
1086
1087         /*
1088          * Find the entry.
1089          */
1090         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1091
1092         if (dtbl_entry != NULL) {
1093                 /*
1094                  * Found - remove it.
1095                  */
1096                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1097         }
1098 }
1099
1100 /* Change the entry for a dissector in a string dissector table
1101    with a particular pattern to use a new dissector handle. */
1102 void
1103 dissector_change_string(const char *name, const gchar *pattern,
1104                         dissector_handle_t handle)
1105 {
1106         dissector_table_t  sub_dissectors = find_dissector_table( name);
1107         dtbl_entry_t      *dtbl_entry;
1108
1109         /* sanity check */
1110         g_assert( sub_dissectors);
1111
1112         /*
1113          * See if the entry already exists. If so, reuse it.
1114          */
1115         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1116         if (dtbl_entry != NULL) {
1117                 dtbl_entry->current = handle;
1118                 return;
1119         }
1120
1121         /*
1122          * Don't create an entry if there is no dissector handle - I.E. the
1123          * user said not to decode something that wasn't being decoded
1124          * in the first place.
1125          */
1126         if (handle == NULL)
1127                 return;
1128
1129         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1130         dtbl_entry->initial = NULL;
1131         dtbl_entry->current = handle;
1132
1133         /* do the table insertion */
1134         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1135                              (gpointer)dtbl_entry);
1136 }
1137
1138 /* Reset an entry in a string sub-dissector table to its initial value. */
1139 void
1140 dissector_reset_string(const char *name, const gchar *pattern)
1141 {
1142         dissector_table_t  sub_dissectors = find_dissector_table( name);
1143         dtbl_entry_t      *dtbl_entry;
1144
1145         /* sanity check */
1146         g_assert( sub_dissectors);
1147
1148         /*
1149          * Find the entry.
1150          */
1151         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1152
1153         if (dtbl_entry == NULL)
1154                 return;
1155
1156         /*
1157          * Found - is there an initial value?
1158          */
1159         if (dtbl_entry->initial != NULL) {
1160                 dtbl_entry->current = dtbl_entry->initial;
1161         } else {
1162                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1163         }
1164 }
1165
1166 /* Look for a given string in a given dissector table and, if found, call
1167    the dissector with the arguments supplied, and return TRUE, otherwise
1168    return FALSE. */
1169 gboolean
1170 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1171                      tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1172 {
1173         dtbl_entry_t            *dtbl_entry;
1174         struct dissector_handle *handle;
1175         int                      ret;
1176         const gchar             *saved_match_string;
1177
1178         /* XXX ASSERT instead ? */
1179         if (!string) return FALSE;
1180         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1181         if (dtbl_entry != NULL) {
1182                 /*
1183                  * Is there currently a dissector handle for this entry?
1184                  */
1185                 handle = dtbl_entry->current;
1186                 if (handle == NULL) {
1187                         /*
1188                          * No - pretend this dissector didn't exist,
1189                          * so that other dissectors might have a chance
1190                          * to dissect this packet.
1191                          */
1192                         return FALSE;
1193                 }
1194
1195                 /*
1196                  * Save the current value of "pinfo->match_string",
1197                  * set it to the string that matched, call the
1198                  * dissector, and restore "pinfo->match_string".
1199                  */
1200                 saved_match_string = pinfo->match_string;
1201                 pinfo->match_string = string;
1202                 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, NULL);
1203                 pinfo->match_string = saved_match_string;
1204
1205                 /*
1206                  * If a new-style dissector returned 0, it means that
1207                  * it didn't think this tvbuff represented a packet for
1208                  * its protocol, and didn't dissect anything.
1209                  *
1210                  * Old-style dissectors can't reject the packet.
1211                  *
1212                  * 0 is also returned if the protocol wasn't enabled.
1213                  *
1214                  * If the packet was rejected, we return FALSE, so that
1215                  * other dissectors might have a chance to dissect this
1216                  * packet, otherwise we return TRUE.
1217                  */
1218                 return ret != 0;
1219         }
1220         return FALSE;
1221 }
1222
1223 /* Look for a given value in a given string dissector table and, if found,
1224    return the dissector handle for that value. */
1225 dissector_handle_t
1226 dissector_get_string_handle(dissector_table_t sub_dissectors,
1227                             const gchar *string)
1228 {
1229         dtbl_entry_t *dtbl_entry;
1230
1231         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1232         if (dtbl_entry != NULL)
1233                 return dtbl_entry->current;
1234         else
1235                 return NULL;
1236 }
1237
1238 dissector_handle_t
1239 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1240 {
1241         return dtbl_entry->current;
1242 }
1243
1244 gint
1245 dissector_compare_filter_name(gconstpointer dissector_a, gconstpointer dissector_b)
1246 {
1247         dissector_handle_t a = (dissector_handle_t)dissector_a;
1248         dissector_handle_t b = (dissector_handle_t)dissector_b;
1249         const char *a_name, *b_name;
1250         gint ret;
1251
1252         if (a->protocol == NULL)
1253                 a_name = "";
1254         else
1255                 a_name = proto_get_protocol_filter_name(proto_get_id(a->protocol));
1256
1257         if (b->protocol == NULL)
1258                 b_name = "";
1259         else
1260                 b_name = proto_get_protocol_filter_name(proto_get_id(b->protocol));
1261
1262         ret = strcmp(a_name, b_name);
1263         return ret;
1264 }
1265
1266 /* Add a handle to the list of handles that *could* be used with this
1267    table.  That list is used by code in the UI. */
1268 void
1269 dissector_add_handle(const char *name, dissector_handle_t handle)
1270 {
1271         dissector_table_t  sub_dissectors = find_dissector_table( name);
1272         GSList            *entry;
1273
1274         /*
1275          * Make sure the dissector table exists.
1276          */
1277         if (sub_dissectors == NULL) {
1278                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1279                     name);
1280                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1281                     proto_get_protocol_long_name(handle->protocol));
1282                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1283                         abort();
1284                 return;
1285         }
1286
1287         /* Is it already in this list? */
1288         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1289         if (entry != NULL) {
1290                 /*
1291                  * Yes - don't insert it again.
1292                  */
1293                 return;
1294         }
1295
1296         /* Add it to the list. */
1297         sub_dissectors->dissector_handles =
1298                 g_slist_insert_sorted(sub_dissectors->dissector_handles, (gpointer)handle, (GCompareFunc)dissector_compare_filter_name);
1299 }
1300
1301 dissector_handle_t
1302 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1303 {
1304         return dtbl_entry->initial;
1305 }
1306
1307 /**************************************************/
1308 /*                                                */
1309 /*       Routines to walk dissector tables        */
1310 /*                                                */
1311 /**************************************************/
1312
1313 typedef struct dissector_foreach_info {
1314         gpointer      caller_data;
1315         DATFunc       caller_func;
1316         GHFunc        next_func;
1317         const gchar  *table_name;
1318         ftenum_t      selector_type;
1319 } dissector_foreach_info_t;
1320
1321 /*
1322  * Called for each entry in a dissector table.
1323  */
1324 static void
1325 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1326 {
1327         dissector_foreach_info_t *info;
1328         dtbl_entry_t             *dtbl_entry;
1329
1330         g_assert(value);
1331         g_assert(user_data);
1332
1333         dtbl_entry = value;
1334         if (dtbl_entry->current == NULL ||
1335             dtbl_entry->current->protocol == NULL) {
1336                 /*
1337                  * Either there is no dissector for this entry, or
1338                  * the dissector doesn't have a protocol associated
1339                  * with it.
1340                  *
1341                  * XXX - should the latter check be done?
1342                  */
1343                 return;
1344         }
1345
1346         info = user_data;
1347         info->caller_func(info->table_name, info->selector_type, key, value,
1348                           info->caller_data);
1349 }
1350
1351 /*
1352  * Called for each entry in the table of all dissector tables.
1353  */
1354 static void
1355 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1356 {
1357         dissector_table_t         sub_dissectors;
1358         dissector_foreach_info_t *info;
1359
1360         g_assert(value);
1361         g_assert(user_data);
1362
1363         sub_dissectors = value;
1364         info = user_data;
1365         info->table_name = (gchar*) key;
1366         info->selector_type = get_dissector_table_selector_type(info->table_name);
1367         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1368 }
1369
1370 /*
1371  * Walk all dissector tables calling a user supplied function on each
1372  * entry.
1373  */
1374 static void
1375 dissector_all_tables_foreach (DATFunc func,
1376                               gpointer user_data)
1377 {
1378         dissector_foreach_info_t info;
1379
1380         info.caller_data = user_data;
1381         info.caller_func = func;
1382         info.next_func   = dissector_table_foreach_func;
1383         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1384 }
1385
1386 /*
1387  * Walk one dissector table's hash table calling a user supplied function
1388  * on each entry.
1389  */
1390 void
1391 dissector_table_foreach (const char *name,
1392                          DATFunc     func,
1393                          gpointer    user_data)
1394 {
1395         dissector_foreach_info_t info;
1396         dissector_table_t        sub_dissectors = find_dissector_table( name);
1397
1398         info.table_name    = name;
1399         info.selector_type = sub_dissectors->type;
1400         info.caller_func   = func;
1401         info.caller_data   = user_data;
1402         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1403 }
1404
1405 /*
1406  * Walk one dissector table's list of handles calling a user supplied
1407  * function on each entry.
1408  */
1409 void
1410 dissector_table_foreach_handle(const char     *name,
1411                                DATFunc_handle  func,
1412                                gpointer        user_data)
1413 {
1414         dissector_table_t sub_dissectors = find_dissector_table( name);
1415         GSList *tmp;
1416
1417         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1418              tmp = g_slist_next(tmp))
1419                 func(name, tmp->data, user_data);
1420 }
1421
1422 /*
1423  * Called for each entry in a dissector table.
1424  */
1425 static void
1426 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1427 {
1428         dtbl_entry_t             *dtbl_entry;
1429         dissector_foreach_info_t *info;
1430
1431         g_assert(value);
1432         g_assert(user_data);
1433
1434         dtbl_entry = value;
1435         if (dtbl_entry->initial == dtbl_entry->current) {
1436                 /*
1437                  * Entry hasn't changed - don't call the function.
1438                  */
1439                 return;
1440         }
1441
1442         info = user_data;
1443         info->caller_func(info->table_name, info->selector_type, key, value,
1444                           info->caller_data);
1445 }
1446
1447 /*
1448  * Walk all dissector tables calling a user supplied function only on
1449  * any entry that has been changed from its original state.
1450  */
1451 void
1452 dissector_all_tables_foreach_changed (DATFunc  func,
1453                                       gpointer user_data)
1454 {
1455         dissector_foreach_info_t info;
1456
1457         info.caller_data = user_data;
1458         info.caller_func = func;
1459         info.next_func   = dissector_table_foreach_changed_func;
1460         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1461 }
1462
1463 /*
1464  * Walk one dissector table calling a user supplied function only on
1465  * any entry that has been changed from its original state.
1466  */
1467 void
1468 dissector_table_foreach_changed (const char *name,
1469                                  DATFunc     func,
1470                                  gpointer    user_data)
1471 {
1472         dissector_foreach_info_t info;
1473         dissector_table_t sub_dissectors = find_dissector_table( name);
1474
1475         info.table_name    = name;
1476         info.selector_type = sub_dissectors->type;
1477         info.caller_func   = func;
1478         info.caller_data   = user_data;
1479         g_hash_table_foreach(sub_dissectors->hash_table,
1480                              dissector_table_foreach_changed_func, &info);
1481 }
1482
1483 typedef struct dissector_foreach_table_info {
1484         gpointer      caller_data;
1485         DATFunc_table caller_func;
1486 } dissector_foreach_table_info_t;
1487
1488 /*
1489  * Called for each entry in the table of all dissector tables.
1490  */
1491 static void
1492 dissector_all_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1493 {
1494         dissector_table_t               table;
1495         dissector_foreach_table_info_t *info;
1496
1497         table = value;
1498         info = user_data;
1499         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1500 }
1501
1502 /*
1503  * Called for each key in the table of all dissector tables.
1504  */
1505 static void
1506 dissector_all_tables_foreach_list_func (gpointer key, gpointer user_data)
1507 {
1508         dissector_table_t               table;
1509         dissector_foreach_table_info_t *info;
1510
1511         table = g_hash_table_lookup( dissector_tables, key );
1512         info = user_data;
1513         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1514 }
1515
1516 /*
1517  * Walk all dissector tables calling a user supplied function on each
1518  * table.
1519  */
1520 void
1521 dissector_all_tables_foreach_table (DATFunc_table func,
1522                                         gpointer      user_data,
1523                                         GCompareFunc compare_key_func)
1524 {
1525         dissector_foreach_table_info_t info;
1526         GList* list;
1527
1528         info.caller_data = user_data;
1529         info.caller_func = func;
1530         if (compare_key_func != NULL)
1531         {
1532                 list = g_hash_table_get_keys(dissector_tables);
1533                 list = g_list_sort(list, compare_key_func);
1534                 g_list_foreach(list, dissector_all_tables_foreach_list_func, &info);
1535                 g_list_free(list);
1536         }
1537         else
1538         {
1539                 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1540         }
1541 }
1542
1543 dissector_table_t
1544 register_dissector_table(const char *name, const char *ui_name, const ftenum_t type,
1545                          const int base)
1546 {
1547         dissector_table_t       sub_dissectors;
1548
1549         /* Create our hash-of-hashes if it doesn't already exist */
1550         if (!dissector_tables) {
1551                 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1552                 g_assert(dissector_tables);
1553         }
1554
1555         /* Make sure the registration is unique */
1556         if(g_hash_table_lookup( dissector_tables, name )) {
1557                 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1558         }
1559
1560         /* Create and register the dissector table for this name; returns */
1561         /* a pointer to the dissector table. */
1562         sub_dissectors = g_malloc(sizeof (struct dissector_table));
1563         switch (type) {
1564
1565         case FT_UINT8:
1566         case FT_UINT16:
1567         case FT_UINT24:
1568         case FT_UINT32:
1569                 /*
1570                  * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1571                  * so we use "g_direct_hash()" and "g_direct_equal()".
1572                  */
1573                 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash,
1574                                                                g_direct_equal,
1575                                                                NULL,
1576                                                                &g_free );
1577                 break;
1578
1579         case FT_STRING:
1580         case FT_STRINGZ:
1581                 sub_dissectors->hash_table = g_hash_table_new_full( g_str_hash,
1582                                                                g_str_equal,
1583                                                                NULL,
1584                                                                &g_free );
1585                 break;
1586
1587         default:
1588                 g_assert_not_reached();
1589         }
1590         sub_dissectors->dissector_handles = NULL;
1591         sub_dissectors->ui_name = ui_name;
1592         sub_dissectors->type    = type;
1593         sub_dissectors->base    = base;
1594         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1595         return sub_dissectors;
1596 }
1597
1598 const char *
1599 get_dissector_table_ui_name(const char *name)
1600 {
1601         dissector_table_t sub_dissectors = find_dissector_table( name);
1602
1603         return sub_dissectors->ui_name;
1604 }
1605
1606 ftenum_t
1607 get_dissector_table_selector_type(const char *name)
1608 {
1609         dissector_table_t sub_dissectors = find_dissector_table( name);
1610
1611         return sub_dissectors->type;
1612 }
1613
1614 int
1615 get_dissector_table_base(const char *name)
1616 {
1617         dissector_table_t sub_dissectors = find_dissector_table( name);
1618
1619         return sub_dissectors->base;
1620 }
1621
1622 static GHashTable *heur_dissector_lists = NULL;
1623
1624
1625 /* Finds a heuristic dissector table by table name. */
1626 static heur_dissector_list_t *
1627 find_heur_dissector_list(const char *name)
1628 {
1629         g_assert(heur_dissector_lists != NULL);
1630         return g_hash_table_lookup(heur_dissector_lists, name);
1631 }
1632
1633 void
1634 heur_dissector_add(const char *name, heur_dissector_t dissector, const int proto)
1635 {
1636         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1637         const char            *proto_name;
1638         heur_dtbl_entry_t     *hdtbl_entry;
1639
1640         /*
1641          * Make sure the dissector table exists.
1642          */
1643         if (sub_dissectors == NULL) {
1644                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1645                     name);
1646                 proto_name = proto_get_protocol_name(proto);
1647                 if (proto_name != NULL) {
1648                         fprintf(stderr, "Protocol being registered is \"%s\"\n",
1649                             proto_name);
1650                 }
1651                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1652                         abort();
1653                 return;
1654         }
1655
1656         /* XXX: Should verify that sub-dissector is not already in the list ? */
1657
1658         hdtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1659         hdtbl_entry->dissector = dissector;
1660         hdtbl_entry->protocol  = find_protocol_by_id(proto);
1661         hdtbl_entry->enabled   = TRUE;
1662
1663         /* do the table insertion */
1664         *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)hdtbl_entry);
1665 }
1666
1667
1668
1669 static int
1670 find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1671         const heur_dtbl_entry_t *hdtbl_entry_a = (const heur_dtbl_entry_t *) a;
1672         const heur_dtbl_entry_t *hdtbl_entry_b = (const heur_dtbl_entry_t *) b;
1673         return (hdtbl_entry_a->dissector == hdtbl_entry_b->dissector) &&
1674                 (hdtbl_entry_a->protocol == hdtbl_entry_b->protocol) ? 0 : 1;
1675 }
1676
1677 void
1678 heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
1679         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1680         heur_dtbl_entry_t      hdtbl_entry;
1681         GSList                *found_entry;
1682
1683         /* sanity check */
1684         g_assert(sub_dissectors != NULL);
1685
1686         hdtbl_entry.dissector = dissector;
1687
1688         hdtbl_entry.protocol  = find_protocol_by_id(proto);
1689
1690         found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1691
1692         if (found_entry) {
1693                 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1694                 g_free(g_slist_nth_data(found_entry, 1));
1695                 g_slist_free_1(found_entry);
1696         }
1697 }
1698
1699 void
1700 heur_dissector_set_enabled(const char *name, heur_dissector_t dissector, const int proto, const gboolean enabled) {
1701         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1702         GSList                *found_entry;
1703         heur_dtbl_entry_t      hdtbl_entry;
1704
1705         /* sanity check */
1706         g_assert(sub_dissectors != NULL);
1707
1708         hdtbl_entry.dissector = dissector;
1709
1710         hdtbl_entry.protocol  = find_protocol_by_id(proto);
1711
1712         found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &hdtbl_entry, find_matching_heur_dissector);
1713
1714         if (found_entry) {
1715                 heur_dtbl_entry_t *hdtbl_entry_p;
1716                 hdtbl_entry_p = (heur_dtbl_entry_t *)found_entry->data;
1717                 hdtbl_entry_p->enabled = enabled;
1718         }
1719 }
1720
1721 gboolean
1722 dissector_try_heuristic(heur_dissector_list_t sub_dissectors, tvbuff_t *tvb,
1723                         packet_info *pinfo, proto_tree *tree, void *data)
1724 {
1725         gboolean           status;
1726         const char        *saved_proto;
1727         GSList            *entry;
1728         heur_dtbl_entry_t *hdtbl_entry;
1729         guint16            saved_can_desegment;
1730         gint               saved_layer_names_len = 0;
1731
1732         /* can_desegment is set to 2 by anyone which offers this api/service.
1733            then everytime a subdissector is called it is decremented by one.
1734            thus only the subdissector immediately ontop of whoever offers this
1735            service can use it.
1736            We save the current value of "can_desegment" for the
1737            benefit of TCP proxying dissectors such as SOCKS, so they
1738            can restore it and allow the dissectors they call to use
1739            the desegmentation service.
1740         */
1741         saved_can_desegment        = pinfo->can_desegment;
1742         pinfo->saved_can_desegment = saved_can_desegment;
1743         pinfo->can_desegment       = saved_can_desegment-(saved_can_desegment>0);
1744
1745         status      = FALSE;
1746         saved_proto = pinfo->current_proto;
1747
1748         if (pinfo->layer_names != NULL)
1749                 saved_layer_names_len = (gint) pinfo->layer_names->len;
1750
1751         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1752                 /* XXX - why set this now and above? */
1753                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1754                 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1755
1756                 if (hdtbl_entry->protocol != NULL &&
1757                     (!proto_is_protocol_enabled(hdtbl_entry->protocol)||(hdtbl_entry->enabled==FALSE))) {
1758                         /*
1759                          * No - don't try this dissector.
1760                          */
1761                         continue;
1762                 }
1763
1764                 if (hdtbl_entry->protocol != NULL) {
1765                         pinfo->current_proto =
1766                                 proto_get_protocol_short_name(hdtbl_entry->protocol);
1767
1768                         /*
1769                          * Add the protocol name to the layers; we'll remove it
1770                          * if the dissector fails.
1771                          */
1772                         if (pinfo->layer_names) {
1773                                 if (pinfo->layer_names->len > 0)
1774                                         g_string_append(pinfo->layer_names, ":");
1775                                         g_string_append(pinfo->layer_names,
1776                                         proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)));
1777                         }
1778                 }
1779                 EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1780                                  proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1781                 if ((*hdtbl_entry->dissector)(tvb, pinfo, tree, data)) {
1782                         EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1783                                          proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1784                         status = TRUE;
1785                         break;
1786                 } else {
1787                         EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned false",
1788                                          proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol))));
1789
1790                         /*
1791                          * That dissector didn't accept the packet, so
1792                          * remove its protocol's name from the list
1793                          * of protocols.
1794                          */
1795                         if (pinfo->layer_names != NULL) {
1796                                 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1797                         }
1798                 }
1799         }
1800         pinfo->current_proto = saved_proto;
1801         pinfo->can_desegment=saved_can_desegment;
1802         return status;
1803 }
1804
1805 /*
1806  * Called for each entry in the table of all heuristic dissector tables.
1807  */
1808 typedef struct heur_dissector_foreach_table_info {
1809         gpointer           caller_data;
1810         DATFunc_heur_table caller_func;
1811 } heur_dissector_foreach_table_info_t;
1812
1813
1814 static void
1815 dissector_dump_heur_decodes_display(const gchar *table_name, const gpointer value, const gpointer user_data _U_)
1816 {
1817         heur_dissector_list_t  sub_dissectors = *(heur_dissector_list_t *)value;
1818         GSList                *entry;
1819         heur_dtbl_entry_t     *hdtbl_entry;
1820
1821         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1822                 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
1823                 if (hdtbl_entry->protocol != NULL) {
1824                         printf("%s\t%s\t%c\n",
1825                                table_name,
1826                                proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)),
1827                                (proto_is_protocol_enabled(hdtbl_entry->protocol) && hdtbl_entry->enabled) ? 'T' : 'F');
1828                 }
1829         }
1830 }
1831
1832
1833 static void
1834 dissector_all_heur_tables_foreach_table_func (gpointer key, const gpointer value, const gpointer user_data)
1835 {
1836         heur_dissector_foreach_table_info_t *info;
1837
1838         info = user_data;
1839         (*info->caller_func)((gchar*)key, value, info->caller_data);
1840 }
1841
1842 /*
1843  * Walk all heuristic dissector tables calling a user supplied function on each
1844  * table.
1845  */
1846 void
1847 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
1848                                          gpointer user_data)
1849 {
1850         heur_dissector_foreach_table_info_t info;
1851
1852         info.caller_data = user_data;
1853         info.caller_func = func;
1854         g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
1855 }
1856
1857 /*
1858  * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
1859  */
1860 void
1861 dissector_dump_heur_decodes(void)
1862 {
1863         dissector_all_heur_tables_foreach_table(dissector_dump_heur_decodes_display, NULL);
1864 }
1865
1866
1867 void
1868 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1869 {
1870         /* Create our hash-of-lists if it doesn't already exist */
1871         if (heur_dissector_lists == NULL) {
1872                 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1873                 g_assert(heur_dissector_lists != NULL);
1874         }
1875
1876         /* Make sure the registration is unique */
1877         g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1878
1879         *sub_dissectors = NULL; /* initially empty */
1880         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1881                             (gpointer) sub_dissectors);
1882 }
1883
1884 /*
1885  * Register dissectors by name; used if one dissector always calls a
1886  * particular dissector, or if it bases the decision of which dissector
1887  * to call on something other than a numerical value or on "try a bunch
1888  * of dissectors until one likes the packet".
1889  */
1890
1891 /*
1892  * List of registered dissectors.
1893  */
1894 static GHashTable *registered_dissectors = NULL;
1895
1896 /* Get the long name of the protocol for a dissector handle, if it has
1897    a protocol. */
1898 const char *
1899 dissector_handle_get_long_name(const dissector_handle_t handle)
1900 {
1901         if (handle == NULL || handle->protocol == NULL) {
1902                 return NULL;
1903         }
1904         return proto_get_protocol_long_name(handle->protocol);
1905 }
1906
1907 /* Get the short name of the protocol for a dissector handle, if it has
1908    a protocol. */
1909 const char *
1910 dissector_handle_get_short_name(const dissector_handle_t handle)
1911 {
1912         if (handle->protocol == NULL) {
1913                 /*
1914                  * No protocol (see, for example, the handle for
1915                  * dissecting the set of protocols where the first
1916                  * octet of the payload is an OSI network layer protocol
1917                  * ID).
1918                  */
1919                 return NULL;
1920         }
1921         return proto_get_protocol_short_name(handle->protocol);
1922 }
1923
1924 /* Get the index of the protocol for a dissector handle, if it has
1925    a protocol. */
1926 int
1927 dissector_handle_get_protocol_index(const dissector_handle_t handle)
1928 {
1929         if (handle->protocol == NULL) {
1930                 /*
1931                  * No protocol (see, for example, the handle for
1932                  * dissecting the set of protocols where the first
1933                  * octet of the payload is an OSI network layer protocol
1934                  * ID).
1935                  */
1936                 return -1;
1937         }
1938         return proto_get_id(handle->protocol);
1939 }
1940
1941 /* Find a registered dissector by name. */
1942 dissector_handle_t
1943 find_dissector(const char *name)
1944 {
1945         g_assert(registered_dissectors != NULL);
1946         return g_hash_table_lookup(registered_dissectors, name);
1947 }
1948
1949 /* Create an anonymous handle for a dissector. */
1950 dissector_handle_t
1951 create_dissector_handle(dissector_t dissector, const int proto)
1952 {
1953         struct dissector_handle *handle;
1954
1955         handle                = g_malloc(sizeof (struct dissector_handle));
1956         handle->name          = NULL;
1957         handle->is_new        = FALSE;
1958         handle->dissector.old = dissector;
1959         handle->protocol      = find_protocol_by_id(proto);
1960
1961         return handle;
1962 }
1963
1964 dissector_handle_t
1965 new_create_dissector_handle(new_dissector_t dissector, const int proto)
1966 {
1967         struct dissector_handle *handle;
1968
1969         handle                = g_malloc(sizeof (struct dissector_handle));
1970         handle->name          = NULL;
1971         handle->is_new        = TRUE;
1972         handle->dissector.new = dissector;
1973         handle->protocol      = find_protocol_by_id(proto);
1974
1975         return handle;
1976 }
1977
1978 /* Register a dissector by name. */
1979 void
1980 register_dissector(const char *name, dissector_t dissector, const int proto)
1981 {
1982         struct dissector_handle *handle;
1983
1984         /* Create our hash table if it doesn't already exist */
1985         if (registered_dissectors == NULL) {
1986                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1987                 g_assert(registered_dissectors != NULL);
1988         }
1989
1990         /* Make sure the registration is unique */
1991         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1992
1993         handle                = g_malloc(sizeof (struct dissector_handle));
1994         handle->name          = name;
1995         handle->is_new        = FALSE;
1996         handle->dissector.old = dissector;
1997         handle->protocol      = find_protocol_by_id(proto);
1998
1999         g_hash_table_insert(registered_dissectors, (gpointer)name,
2000                             (gpointer) handle);
2001 }
2002
2003 void
2004 new_register_dissector(const char *name, new_dissector_t dissector, const int proto)
2005 {
2006         struct dissector_handle *handle;
2007
2008         /* Create our hash table if it doesn't already exist */
2009         if (registered_dissectors == NULL) {
2010                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
2011                 g_assert(registered_dissectors != NULL);
2012         }
2013
2014         /* Make sure the registration is unique */
2015         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
2016
2017         handle                = g_malloc(sizeof (struct dissector_handle));
2018         handle->name          = name;
2019         handle->is_new        = TRUE;
2020         handle->dissector.new = dissector;
2021         handle->protocol      = find_protocol_by_id(proto);
2022
2023         g_hash_table_insert(registered_dissectors, (gpointer)name,
2024                             (gpointer) handle);
2025 }
2026
2027 /* Call a dissector through a handle but if the dissector rejected it
2028  * return 0.
2029  */
2030 int
2031 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
2032                     packet_info *pinfo, proto_tree *tree, void *data)
2033 {
2034         int ret;
2035
2036         g_assert(handle != NULL);
2037         ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
2038         return ret;
2039 }
2040
2041 /* Call a dissector through a handle and if this fails call the "data"
2042  * dissector.
2043  */
2044 int
2045 call_dissector_with_data(dissector_handle_t handle, tvbuff_t *tvb,
2046                          packet_info *pinfo, proto_tree *tree, void *data)
2047 {
2048         int ret;
2049
2050         ret = call_dissector_only(handle, tvb, pinfo, tree, data);
2051         if (ret == 0) {
2052                 /*
2053                  * The protocol was disabled, or the dissector rejected
2054                  * it.  Just dissect this packet as data.
2055                  */
2056                 g_assert(data_handle != NULL);
2057                 g_assert(data_handle->protocol != NULL);
2058                 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2059                 return tvb_length(tvb);
2060         }
2061         return ret;
2062 }
2063
2064 int
2065 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
2066                packet_info *pinfo, proto_tree *tree)
2067 {
2068         return call_dissector_with_data(handle, tvb, pinfo, tree, NULL);
2069 }
2070
2071 /*
2072  * Dumps the "layer type"/"decode as" associations to stdout, similar
2073  * to the proto_registrar_dump_*() routines.
2074  *
2075  * There is one record per line. The fields are tab-delimited.
2076  *
2077  * Field 1 = layer type, e.g. "tcp.port"
2078  * Field 2 = selector in decimal
2079  * Field 3 = "decode as" name, e.g. "http"
2080  */
2081
2082
2083 static void
2084 dissector_dump_decodes_display(const gchar *table_name,
2085                                ftenum_t selector_type _U_, const gpointer key, const gpointer value,
2086                                gpointer user_data _U_)
2087 {
2088         guint32             selector       = (guint32)(unsigned long) key;
2089         dissector_table_t   sub_dissectors = find_dissector_table(table_name);
2090         dtbl_entry_t       *dtbl_entry;
2091         dissector_handle_t  handle;
2092         gint                proto_id;
2093         const gchar        *decode_as;
2094
2095         g_assert(sub_dissectors);
2096         switch (sub_dissectors->type) {
2097
2098                 case FT_UINT8:
2099                 case FT_UINT16:
2100                 case FT_UINT24:
2101                 case FT_UINT32:
2102                         dtbl_entry = value;
2103                         g_assert(dtbl_entry);
2104
2105                         handle   = dtbl_entry->current;
2106                         g_assert(handle);
2107
2108                         proto_id = dissector_handle_get_protocol_index(handle);
2109
2110                         if (proto_id != -1) {
2111                                 decode_as = proto_get_protocol_filter_name(proto_id);
2112                                 g_assert(decode_as != NULL);
2113                                 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
2114                         }
2115                         break;
2116
2117         default:
2118                 break;
2119         }
2120 }
2121
2122 void
2123 dissector_dump_decodes(void)
2124 {
2125         dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2126 }
2127
2128 static GPtrArray* post_dissectors = NULL;
2129 static guint num_of_postdissectors = 0;
2130
2131 void
2132 register_postdissector(dissector_handle_t handle)
2133 {
2134         if (!post_dissectors)
2135                 post_dissectors = g_ptr_array_new();
2136
2137         g_ptr_array_add(post_dissectors, handle);
2138         num_of_postdissectors++;
2139 }
2140
2141 gboolean
2142 have_postdissector(void)
2143 {
2144         guint i;
2145         dissector_handle_t handle;
2146
2147         for(i = 0; i < num_of_postdissectors; i++) {
2148                 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
2149
2150                 if (handle->protocol != NULL
2151                     && proto_is_protocol_enabled(handle->protocol)) {
2152                         /* We have at least one enabled postdissector */
2153                         return TRUE;
2154                 }
2155         }
2156         return FALSE;
2157 }
2158
2159 void
2160 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2161 {
2162         guint i;
2163
2164         for(i = 0; i < num_of_postdissectors; i++) {
2165                 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
2166                                     tvb,pinfo,tree, NULL);
2167         }
2168 }
2169
2170 /*
2171  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2172  *
2173  * Local variables:
2174  * c-basic-offset: 8
2175  * tab-width: 8
2176  * indent-tabs-mode: t
2177  * End:
2178  *
2179  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2180  * :indentSize=8:tabSize=8:noTabs=false:
2181  */