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