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