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