The data sources can be used even if the protocol tree isn't being built
[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, 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, 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 /* Add an entry to a uint dissector table. */
700 void
701 dissector_add(const char *name, 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 /* sanity checks */
708         g_assert(sub_dissectors);
709         switch (sub_dissectors->type) {
710
711         case FT_UINT8:
712         case FT_UINT16:
713         case FT_UINT24:
714         case FT_UINT32:
715                 /*
716                  * You can do a port lookup in these tables.
717                  */
718                 break;
719
720         default:
721                 /*
722                  * But you can't do a port lookup in any other types
723                  * of tables.
724                  */
725                 g_assert_not_reached();
726         }
727
728 #if 0
729         if (pattern == 0) {
730                 g_warning("%s: %s registering using a pattern of 0",
731                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
732         }
733
734         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
735         if (dtbl_entry != NULL) {
736                 g_warning("%s: %s registering using pattern %d already registered by %s",
737                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
738                           pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
739         }
740 #endif
741         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
742         dtbl_entry->current = handle;
743         dtbl_entry->initial = dtbl_entry->current;
744
745 /* do the table insertion */
746         g_hash_table_insert( sub_dissectors->hash_table,
747             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
748
749         /*
750          * Now add it to the list of handles that could be used with this
751          * table, because it *is* being used with this table.
752          */
753         dissector_add_handle(name, handle);
754 }
755
756 /* Delete the entry for a dissector in a uint dissector table
757    with a particular pattern. */
758
759 /* NOTE: this doesn't use the dissector call variable. It is included to */
760 /*      be consistant with the dissector_add and more importantly to be used */
761 /*      if the technique of adding a temporary dissector is implemented.  */
762 /*      If temporary dissectors are deleted, then the original dissector must */
763 /*      be available. */
764 void
765 dissector_delete(const char *name, guint32 pattern,
766         dissector_handle_t handle _U_)
767 {
768         dissector_table_t sub_dissectors = find_dissector_table( name);
769         dtbl_entry_t *dtbl_entry;
770
771 /* sanity check */
772         g_assert( sub_dissectors);
773
774         /*
775          * Find the entry.
776          */
777         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
778
779         if (dtbl_entry != NULL) {
780                 /*
781                  * Found - remove it.
782                  */
783                 g_hash_table_remove(sub_dissectors->hash_table,
784                     GUINT_TO_POINTER(pattern));
785
786                 /*
787                  * Now free up the entry.
788                  */
789                 g_free(dtbl_entry);
790         }
791 }
792
793 /* Change the entry for a dissector in a uint dissector table
794    with a particular pattern to use a new dissector handle. */
795 void
796 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
797 {
798         dissector_table_t sub_dissectors = find_dissector_table( name);
799         dtbl_entry_t *dtbl_entry;
800
801 /* sanity check */
802         g_assert( sub_dissectors);
803
804         /*
805          * See if the entry already exists. If so, reuse it.
806          */
807         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
808         if (dtbl_entry != NULL) {
809           dtbl_entry->current = handle;
810           return;
811         }
812
813         /*
814          * Don't create an entry if there is no dissector handle - I.E. the
815          * user said not to decode something that wasn't being decoded
816          * in the first place.
817          */
818         if (handle == NULL)
819           return;
820
821         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
822         dtbl_entry->initial = NULL;
823         dtbl_entry->current = handle;
824
825 /* do the table insertion */
826         g_hash_table_insert( sub_dissectors->hash_table,
827             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
828 }
829
830 /* Reset an entry in a uint dissector table to its initial value. */
831 void
832 dissector_reset(const char *name, guint32 pattern)
833 {
834         dissector_table_t sub_dissectors = find_dissector_table( name);
835         dtbl_entry_t *dtbl_entry;
836
837 /* sanity check */
838         g_assert( sub_dissectors);
839
840         /*
841          * Find the entry.
842          */
843         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
844
845         if (dtbl_entry == NULL)
846                 return;
847
848         /*
849          * Found - is there an initial value?
850          */
851         if (dtbl_entry->initial != NULL) {
852                 dtbl_entry->current = dtbl_entry->initial;
853         } else {
854                 g_hash_table_remove(sub_dissectors->hash_table,
855                     GUINT_TO_POINTER(pattern));
856                 g_free(dtbl_entry);
857         }
858 }
859
860 /* Look for a given value in a given uint dissector table and, if found,
861    call the dissector with the arguments supplied, and return TRUE,
862    otherwise return FALSE. */
863
864 gboolean
865 dissector_try_port_new(dissector_table_t sub_dissectors, guint32 port,
866     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean add_proto_name)
867 {
868         dtbl_entry_t *dtbl_entry;
869         struct dissector_handle *handle;
870         guint32 saved_match_port;
871         int ret;
872
873         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
874         if (dtbl_entry != NULL) {
875                 /*
876                  * Is there currently a dissector handle for this entry?
877                  */
878                 handle = dtbl_entry->current;
879                 if (handle == NULL) {
880                         /*
881                          * No - pretend this dissector didn't exist,
882                          * so that other dissectors might have a chance
883                          * to dissect this packet.
884                          */
885                         return FALSE;
886                 }
887
888                 /*
889                  * Save the current value of "pinfo->match_port",
890                  * set it to the port that matched, call the
891                  * dissector, and restore "pinfo->match_port".
892                  */
893                 saved_match_port = pinfo->match_port;
894                 pinfo->match_port = port;
895                 ret = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name);
896                 pinfo->match_port = saved_match_port;
897
898                 /*
899                  * If a new-style dissector returned 0, it means that
900                  * it didn't think this tvbuff represented a packet for
901                  * its protocol, and didn't dissect anything.
902                  *
903                  * Old-style dissectors can't reject the packet.
904                  *
905                  * 0 is also returned if the protocol wasn't enabled.
906                  *
907                  * If the packet was rejected, we return FALSE, so that
908                  * other dissectors might have a chance to dissect this
909                  * packet, otherwise we return TRUE.
910                  */
911                 return ret != 0;
912         }
913         return FALSE;
914 }
915
916 gboolean
917 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
918     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
919 {
920
921         return dissector_try_port_new(sub_dissectors, port, tvb, pinfo, tree, TRUE);
922 }
923 /* Look for a given value in a given uint dissector table and, if found,
924    return the dissector handle for that value. */
925 dissector_handle_t
926 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
927 {
928         dtbl_entry_t *dtbl_entry;
929
930         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
931         if (dtbl_entry != NULL)
932                 return dtbl_entry->current;
933         else
934                 return NULL;
935 }
936
937 /* Find an entry in a string dissector table. */
938 static dtbl_entry_t *
939 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
940 {
941         switch (sub_dissectors->type) {
942
943         case FT_STRING:
944         case FT_STRINGZ:
945                 /*
946                  * You can do a string lookup in these tables.
947                  */
948                 break;
949
950         default:
951                 /*
952                  * But you can't do a string lookup in any other types
953                  * of tables.
954                  */
955                 g_assert_not_reached();
956         }
957
958         /*
959          * Find the entry.
960          */
961         return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
962 }
963
964 /* Add an entry to a string dissector table. */
965 void
966 dissector_add_string(const char *name, const gchar *pattern,
967     dissector_handle_t handle)
968 {
969         dissector_table_t sub_dissectors = find_dissector_table( name);
970         dtbl_entry_t *dtbl_entry;
971
972 /* sanity check */
973         g_assert( sub_dissectors);
974
975         switch (sub_dissectors->type) {
976
977         case FT_STRING:
978         case FT_STRINGZ:
979                 /*
980                  * You can do a string lookup in these tables.
981                  */
982                 break;
983
984         default:
985                 /*
986                  * But you can't do a string lookup in any other types
987                  * of tables.
988                  */
989                 g_assert_not_reached();
990         }
991
992         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
993         dtbl_entry->current = handle;
994         dtbl_entry->initial = dtbl_entry->current;
995
996 /* do the table insertion */
997         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
998             (gpointer)dtbl_entry);
999
1000         /*
1001          * Now add it to the list of handles that could be used with this
1002          * table, because it *is* being used with this table.
1003          */
1004         dissector_add_handle(name, handle);
1005 }
1006
1007 /* Delete the entry for a dissector in a string dissector table
1008    with a particular pattern. */
1009
1010 /* NOTE: this doesn't use the dissector call variable. It is included to */
1011 /*      be consistant with the dissector_add_string and more importantly to */
1012 /*      be used if the technique of adding a temporary dissector is */
1013 /*      implemented.  */
1014 /*      If temporary dissectors are deleted, then the original dissector must */
1015 /*      be available. */
1016 void
1017 dissector_delete_string(const char *name, const gchar *pattern,
1018         dissector_handle_t handle _U_)
1019 {
1020         dissector_table_t sub_dissectors = find_dissector_table( name);
1021         dtbl_entry_t *dtbl_entry;
1022
1023 /* sanity check */
1024         g_assert( sub_dissectors);
1025
1026         /*
1027          * Find the entry.
1028          */
1029         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1030
1031         if (dtbl_entry != NULL) {
1032                 /*
1033                  * Found - remove it.
1034                  */
1035                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1036
1037                 /*
1038                  * Now free up the entry.
1039                  */
1040                 g_free(dtbl_entry);
1041         }
1042 }
1043
1044 /* Change the entry for a dissector in a string dissector table
1045    with a particular pattern to use a new dissector handle. */
1046 void
1047 dissector_change_string(const char *name, gchar *pattern,
1048     dissector_handle_t handle)
1049 {
1050         dissector_table_t sub_dissectors = find_dissector_table( name);
1051         dtbl_entry_t *dtbl_entry;
1052
1053 /* sanity check */
1054         g_assert( sub_dissectors);
1055
1056         /*
1057          * See if the entry already exists. If so, reuse it.
1058          */
1059         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1060         if (dtbl_entry != NULL) {
1061           dtbl_entry->current = handle;
1062           return;
1063         }
1064
1065         /*
1066          * Don't create an entry if there is no dissector handle - I.E. the
1067          * user said not to decode something that wasn't being decoded
1068          * in the first place.
1069          */
1070         if (handle == NULL)
1071           return;
1072
1073         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
1074         dtbl_entry->initial = NULL;
1075         dtbl_entry->current = handle;
1076
1077 /* do the table insertion */
1078         g_hash_table_insert( sub_dissectors->hash_table, pattern,
1079             (gpointer)dtbl_entry);
1080 }
1081
1082 /* Reset an entry in a string sub-dissector table to its initial value. */
1083 void
1084 dissector_reset_string(const char *name, const gchar *pattern)
1085 {
1086         dissector_table_t sub_dissectors = find_dissector_table( name);
1087         dtbl_entry_t *dtbl_entry;
1088
1089 /* sanity check */
1090         g_assert( sub_dissectors);
1091
1092         /*
1093          * Find the entry.
1094          */
1095         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1096
1097         if (dtbl_entry == NULL)
1098                 return;
1099
1100         /*
1101          * Found - is there an initial value?
1102          */
1103         if (dtbl_entry->initial != NULL) {
1104                 dtbl_entry->current = dtbl_entry->initial;
1105         } else {
1106                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1107                 g_free(dtbl_entry);
1108         }
1109 }
1110
1111 /* Look for a given string in a given dissector table and, if found, call
1112    the dissector with the arguments supplied, and return TRUE, otherwise
1113    return FALSE. */
1114 gboolean
1115 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1116     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1117 {
1118         dtbl_entry_t *dtbl_entry;
1119         struct dissector_handle *handle;
1120         int ret;
1121         const gchar *saved_match_string;
1122
1123         /* XXX ASSERT instead ? */
1124         if (!string) return FALSE;
1125         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1126         if (dtbl_entry != NULL) {
1127                 /*
1128                  * Is there currently a dissector handle for this entry?
1129                  */
1130                 handle = dtbl_entry->current;
1131                 if (handle == NULL) {
1132                         /*
1133                          * No - pretend this dissector didn't exist,
1134                          * so that other dissectors might have a chance
1135                          * to dissect this packet.
1136                          */
1137                         return FALSE;
1138                 }
1139
1140                 /*
1141                  * Save the current value of "pinfo->match_string",
1142                  * set it to the string that matched, call the
1143                  * dissector, and restore "pinfo->match_string".
1144                  */
1145                 saved_match_string = pinfo->match_string;
1146                 pinfo->match_string = string;
1147                 ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1148                 pinfo->match_string = saved_match_string;
1149
1150                 /*
1151                  * If a new-style dissector returned 0, it means that
1152                  * it didn't think this tvbuff represented a packet for
1153                  * its protocol, and didn't dissect anything.
1154                  *
1155                  * Old-style dissectors can't reject the packet.
1156                  *
1157                  * 0 is also returned if the protocol wasn't enabled.
1158                  *
1159                  * If the packet was rejected, we return FALSE, so that
1160                  * other dissectors might have a chance to dissect this
1161                  * packet, otherwise we return TRUE.
1162                  */
1163                 return ret != 0;
1164         }
1165         return FALSE;
1166 }
1167
1168 /* Look for a given value in a given string dissector table and, if found,
1169    return the dissector handle for that value. */
1170 dissector_handle_t
1171 dissector_get_string_handle(dissector_table_t sub_dissectors,
1172     const gchar *string)
1173 {
1174         dtbl_entry_t *dtbl_entry;
1175
1176         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1177         if (dtbl_entry != NULL)
1178                 return dtbl_entry->current;
1179         else
1180                 return NULL;
1181 }
1182
1183 dissector_handle_t
1184 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1185 {
1186         return dtbl_entry->current;
1187 }
1188
1189 /* Add a handle to the list of handles that *could* be used with this
1190    table.  That list is used by code in the UI. */
1191 void
1192 dissector_add_handle(const char *name, dissector_handle_t handle)
1193 {
1194         dissector_table_t sub_dissectors = find_dissector_table( name);
1195         GSList *entry;
1196
1197         /* sanity check */
1198         g_assert(sub_dissectors != NULL);
1199
1200         /* Is it already in this list? */
1201         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1202         if (entry != NULL) {
1203                 /*
1204                  * Yes - don't insert it again.
1205                  */
1206                 return;
1207         }
1208
1209         /* Add it to the list. */
1210         sub_dissectors->dissector_handles =
1211             g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1212 }
1213
1214 dissector_handle_t
1215 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1216 {
1217         return dtbl_entry->initial;
1218 }
1219
1220 /**************************************************/
1221 /*                                                */
1222 /*       Routines to walk dissector tables        */
1223 /*                                                */
1224 /**************************************************/
1225
1226 typedef struct dissector_foreach_info {
1227   gpointer     caller_data;
1228   DATFunc      caller_func;
1229   GHFunc       next_func;
1230   const gchar  *table_name;
1231   ftenum_t     selector_type;
1232 } dissector_foreach_info_t;
1233
1234 /*
1235  * Called for each entry in a dissector table.
1236  */
1237 static void
1238 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1239 {
1240         dissector_foreach_info_t *info;
1241         dtbl_entry_t *dtbl_entry;
1242
1243         g_assert(value);
1244         g_assert(user_data);
1245
1246         dtbl_entry = value;
1247         if (dtbl_entry->current == NULL ||
1248             dtbl_entry->current->protocol == NULL) {
1249                 /*
1250                  * Either there is no dissector for this entry, or
1251                  * the dissector doesn't have a protocol associated
1252                  * with it.
1253                  *
1254                  * XXX - should the latter check be done?
1255                  */
1256                 return;
1257         }
1258
1259         info = user_data;
1260         info->caller_func(info->table_name, info->selector_type, key, value,
1261             info->caller_data);
1262 }
1263
1264 /*
1265  * Called for each entry in the table of all dissector tables.
1266  */
1267 static void
1268 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1269 {
1270         dissector_table_t sub_dissectors;
1271         dissector_foreach_info_t *info;
1272
1273         g_assert(value);
1274         g_assert(user_data);
1275
1276         sub_dissectors = value;
1277         info = user_data;
1278         info->table_name = (gchar*) key;
1279         info->selector_type = get_dissector_table_selector_type(info->table_name);
1280         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1281 }
1282
1283 /*
1284  * Walk all dissector tables calling a user supplied function on each
1285  * entry.
1286  */
1287 static void
1288 dissector_all_tables_foreach (DATFunc func,
1289                               gpointer user_data)
1290 {
1291         dissector_foreach_info_t info;
1292
1293         info.caller_data = user_data;
1294         info.caller_func = func;
1295         info.next_func = dissector_table_foreach_func;
1296         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1297 }
1298
1299 /*
1300  * Walk one dissector table's hash table calling a user supplied function
1301  * on each entry.
1302  */
1303 void
1304 dissector_table_foreach (const char *name,
1305                          DATFunc func,
1306                          gpointer user_data)
1307 {
1308         dissector_foreach_info_t info;
1309         dissector_table_t sub_dissectors = find_dissector_table( name);
1310
1311         info.table_name = name;
1312         info.selector_type = sub_dissectors->type;
1313         info.caller_func = func;
1314         info.caller_data = user_data;
1315         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1316 }
1317
1318 /*
1319  * Walk one dissector table's list of handles calling a user supplied
1320  * function on each entry.
1321  */
1322 void
1323 dissector_table_foreach_handle(const char *name,
1324                                DATFunc_handle func,
1325                                gpointer user_data)
1326 {
1327         dissector_table_t sub_dissectors = find_dissector_table( name);
1328         GSList *tmp;
1329
1330         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1331             tmp = g_slist_next(tmp))
1332                 func(name, tmp->data, user_data);
1333 }
1334
1335 /*
1336  * Called for each entry in a dissector table.
1337  */
1338 static void
1339 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1340 {
1341         dtbl_entry_t *dtbl_entry;
1342         dissector_foreach_info_t *info;
1343
1344         g_assert(value);
1345         g_assert(user_data);
1346
1347         dtbl_entry = value;
1348         if (dtbl_entry->initial == dtbl_entry->current) {
1349                 /*
1350                  * Entry hasn't changed - don't call the function.
1351                  */
1352                 return;
1353         }
1354
1355         info = user_data;
1356         info->caller_func(info->table_name, info->selector_type, key, value,
1357             info->caller_data);
1358 }
1359
1360 /*
1361  * Walk all dissector tables calling a user supplied function only on
1362  * any entry that has been changed from its original state.
1363  */
1364 void
1365 dissector_all_tables_foreach_changed (DATFunc func,
1366                                       gpointer user_data)
1367 {
1368         dissector_foreach_info_t info;
1369
1370         info.caller_data = user_data;
1371         info.caller_func = func;
1372         info.next_func = dissector_table_foreach_changed_func;
1373         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1374 }
1375
1376 /*
1377  * Walk one dissector table calling a user supplied function only on
1378  * any entry that has been changed from its original state.
1379  */
1380 void
1381 dissector_table_foreach_changed (const char *name,
1382                                  DATFunc func,
1383                                  gpointer user_data)
1384 {
1385         dissector_foreach_info_t info;
1386         dissector_table_t sub_dissectors = find_dissector_table( name);
1387
1388         info.table_name = name;
1389         info.selector_type = sub_dissectors->type;
1390         info.caller_func = func;
1391         info.caller_data = user_data;
1392         g_hash_table_foreach(sub_dissectors->hash_table,
1393             dissector_table_foreach_changed_func, &info);
1394 }
1395
1396 typedef struct dissector_foreach_table_info {
1397   gpointer      caller_data;
1398   DATFunc_table caller_func;
1399 } dissector_foreach_table_info_t;
1400
1401 /*
1402  * Called for each entry in the table of all dissector tables.
1403  */
1404 static void
1405 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1406 {
1407         dissector_table_t table;
1408         dissector_foreach_table_info_t *info;
1409
1410         table = value;
1411         info = user_data;
1412         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1413 }
1414
1415 /*
1416  * Walk all dissector tables calling a user supplied function on each
1417  * table.
1418  */
1419 void
1420 dissector_all_tables_foreach_table (DATFunc_table func,
1421                                     gpointer user_data)
1422 {
1423         dissector_foreach_table_info_t info;
1424
1425         info.caller_data = user_data;
1426         info.caller_func = func;
1427         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1428 }
1429
1430 dissector_table_t
1431 register_dissector_table(const char *name, const char *ui_name, ftenum_t type,
1432     int base)
1433 {
1434         dissector_table_t       sub_dissectors;
1435
1436         /* Create our hash-of-hashes if it doesn't already exist */
1437         if (!dissector_tables) {
1438                 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1439                 g_assert(dissector_tables);
1440         }
1441
1442         /* Make sure the registration is unique */
1443         if(g_hash_table_lookup( dissector_tables, name )) {
1444                 g_error("The filter name %s (%s) is already registered - do you use a buggy plugin?", name, ui_name);
1445         }
1446
1447         /* Create and register the dissector table for this name; returns */
1448         /* a pointer to the dissector table. */
1449         sub_dissectors = g_malloc(sizeof (struct dissector_table));
1450         switch (type) {
1451
1452         case FT_UINT8:
1453         case FT_UINT16:
1454         case FT_UINT24:
1455         case FT_UINT32:
1456                 /*
1457                  * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1458                  * so we use "g_direct_hash()" and "g_direct_equal()".
1459                  */
1460                 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1461                     g_direct_equal );
1462                 break;
1463
1464         case FT_STRING:
1465         case FT_STRINGZ:
1466                 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1467                     g_str_equal );
1468                 break;
1469
1470         default:
1471                 g_assert_not_reached();
1472         }
1473         sub_dissectors->dissector_handles = NULL;
1474         sub_dissectors->ui_name = ui_name;
1475         sub_dissectors->type = type;
1476         sub_dissectors->base = base;
1477         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1478         return sub_dissectors;
1479 }
1480
1481 const char *
1482 get_dissector_table_ui_name(const char *name)
1483 {
1484         dissector_table_t sub_dissectors = find_dissector_table( name);
1485
1486         return sub_dissectors->ui_name;
1487 }
1488
1489 ftenum_t
1490 get_dissector_table_selector_type(const char *name)
1491 {
1492         dissector_table_t sub_dissectors = find_dissector_table( name);
1493
1494         return sub_dissectors->type;
1495 }
1496
1497 int
1498 get_dissector_table_base(const char *name)
1499 {
1500         dissector_table_t sub_dissectors = find_dissector_table( name);
1501
1502         return sub_dissectors->base;
1503 }
1504
1505 static GHashTable *heur_dissector_lists = NULL;
1506
1507 typedef struct {
1508         heur_dissector_t dissector;
1509         protocol_t *protocol;
1510 } heur_dtbl_entry_t;
1511
1512 /* Finds a heuristic dissector table by field name. */
1513 static heur_dissector_list_t *
1514 find_heur_dissector_list(const char *name)
1515 {
1516         g_assert(heur_dissector_lists != NULL);
1517         return g_hash_table_lookup(heur_dissector_lists, name);
1518 }
1519
1520 void
1521 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1522 {
1523         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1524         heur_dtbl_entry_t *dtbl_entry;
1525
1526         /* sanity check */
1527         g_assert(sub_dissectors != NULL);
1528
1529         dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1530         dtbl_entry->dissector = dissector;
1531         dtbl_entry->protocol = find_protocol_by_id(proto);
1532
1533         /* do the table insertion */
1534         *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1535 }
1536
1537
1538
1539 static int find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
1540     const heur_dtbl_entry_t *dtbl_entry_a = (const heur_dtbl_entry_t *) a;
1541     const heur_dtbl_entry_t *dtbl_entry_b = (const heur_dtbl_entry_t *) b;
1542     return (dtbl_entry_a->dissector == dtbl_entry_b->dissector) &&
1543                 (dtbl_entry_a->protocol == dtbl_entry_b->protocol) ? 0 : 1;
1544 }
1545
1546 void heur_dissector_delete(const char *name, heur_dissector_t dissector, int proto) {
1547         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1548         heur_dtbl_entry_t dtbl_entry;
1549         GSList* found_entry;
1550
1551         /* sanity check */
1552         g_assert(sub_dissectors != NULL);
1553
1554         dtbl_entry.dissector = dissector;
1555
1556         dtbl_entry.protocol = find_protocol_by_id(proto);
1557
1558         found_entry = g_slist_find_custom(*sub_dissectors, (gpointer) &dtbl_entry, find_matching_heur_dissector);
1559
1560         if (found_entry) {
1561                 *sub_dissectors = g_slist_remove_link(*sub_dissectors, found_entry);
1562                 g_free(g_slist_nth_data(found_entry, 1));
1563                 g_slist_free_1(found_entry);
1564         }
1565 }
1566
1567
1568 gboolean
1569 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1570     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1571 {
1572         gboolean status;
1573         const char *saved_proto;
1574         GSList *entry;
1575         heur_dtbl_entry_t *dtbl_entry;
1576         guint16 saved_can_desegment;
1577         gint saved_layer_names_len = 0;
1578
1579         /* can_desegment is set to 2 by anyone which offers this api/service.
1580            then everytime a subdissector is called it is decremented by one.
1581            thus only the subdissector immediately ontop of whoever offers this
1582            service can use it.
1583            We save the current value of "can_desegment" for the
1584            benefit of TCP proxying dissectors such as SOCKS, so they
1585            can restore it and allow the dissectors they call to use
1586            the desegmentation service.
1587         */
1588         saved_can_desegment=pinfo->can_desegment;
1589         pinfo->saved_can_desegment = saved_can_desegment;
1590         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1591
1592         status = FALSE;
1593         saved_proto = pinfo->current_proto;
1594
1595         if (pinfo->layer_names != NULL)
1596                 saved_layer_names_len = (gint) pinfo->layer_names->len;
1597
1598         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1599                 /* XXX - why set this now and above? */
1600                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1601                 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1602
1603                 if (dtbl_entry->protocol != NULL &&
1604                     !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1605                         /*
1606                          * No - don't try this dissector.
1607                          */
1608                         continue;
1609                 }
1610
1611                 if (dtbl_entry->protocol != NULL) {
1612                         pinfo->current_proto =
1613                             proto_get_protocol_short_name(dtbl_entry->protocol);
1614
1615                         /*
1616                          * Add the protocol name to the layers; we'll remove it
1617                          * if the dissector fails.
1618                          */
1619                         if (pinfo->layer_names) {
1620                                 if (pinfo->layer_names->len > 0)
1621                                         g_string_append(pinfo->layer_names, ":");
1622                                         g_string_append(pinfo->layer_names,
1623                                         proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol)));
1624                         }
1625                 }
1626         EP_CHECK_CANARY(("before calling heuristic dissector for protocol: %s",
1627                          proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1628                 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1629             EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has accepted and dissected packet",
1630                              proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1631                         status = TRUE;
1632                         break;
1633                 } else {
1634             EP_CHECK_CANARY(("after heuristic dissector for protocol: %s has returned true",
1635                              proto_get_protocol_filter_name(proto_get_id(dtbl_entry->protocol))));
1636
1637                         /*
1638                          * That dissector didn't accept the packet, so
1639                          * remove its protocol's name from the list
1640                          * of protocols.
1641                          */
1642                         if (pinfo->layer_names != NULL) {
1643                                 g_string_truncate(pinfo->layer_names, saved_layer_names_len);
1644             }
1645                 }
1646         }
1647         pinfo->current_proto = saved_proto;
1648         pinfo->can_desegment=saved_can_desegment;
1649         return status;
1650 }
1651
1652 void
1653 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1654 {
1655         /* Create our hash-of-lists if it doesn't already exist */
1656         if (heur_dissector_lists == NULL) {
1657                 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1658                 g_assert(heur_dissector_lists != NULL);
1659         }
1660
1661         /* Make sure the registration is unique */
1662         g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1663
1664         *sub_dissectors = NULL; /* initially empty */
1665         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1666             (gpointer) sub_dissectors);
1667 }
1668
1669 /*
1670  * Register dissectors by name; used if one dissector always calls a
1671  * particular dissector, or if it bases the decision of which dissector
1672  * to call on something other than a numerical value or on "try a bunch
1673  * of dissectors until one likes the packet".
1674  */
1675
1676 /*
1677  * List of registered dissectors.
1678  */
1679 static GHashTable *registered_dissectors = NULL;
1680
1681 /* Get the short name of the protocol for a dissector handle, if it has
1682    a protocol. */
1683 const char *
1684 dissector_handle_get_short_name(dissector_handle_t handle)
1685 {
1686         if (handle->protocol == NULL) {
1687                 /*
1688                  * No protocol (see, for example, the handle for
1689                  * dissecting the set of protocols where the first
1690                  * octet of the payload is an OSI network layer protocol
1691                  * ID).
1692                  */
1693                 return NULL;
1694         }
1695         return proto_get_protocol_short_name(handle->protocol);
1696 }
1697
1698 /* Get the index of the protocol for a dissector handle, if it has
1699    a protocol. */
1700 int
1701 dissector_handle_get_protocol_index(dissector_handle_t handle)
1702 {
1703         if (handle->protocol == NULL) {
1704                 /*
1705                  * No protocol (see, for example, the handle for
1706                  * dissecting the set of protocols where the first
1707                  * octet of the payload is an OSI network layer protocol
1708                  * ID).
1709                  */
1710                 return -1;
1711         }
1712         return proto_get_id(handle->protocol);
1713 }
1714
1715 /* Find a registered dissector by name. */
1716 dissector_handle_t
1717 find_dissector(const char *name)
1718 {
1719         g_assert(registered_dissectors != NULL);
1720         return g_hash_table_lookup(registered_dissectors, name);
1721 }
1722
1723 /* Create an anonymous handle for a dissector. */
1724 dissector_handle_t
1725 create_dissector_handle(dissector_t dissector, int proto)
1726 {
1727         struct dissector_handle *handle;
1728
1729         handle = g_malloc(sizeof (struct dissector_handle));
1730         handle->name = NULL;
1731         handle->is_new = FALSE;
1732         handle->dissector.old = dissector;
1733         handle->protocol = find_protocol_by_id(proto);
1734
1735         return handle;
1736 }
1737
1738 dissector_handle_t
1739 new_create_dissector_handle(new_dissector_t dissector, int proto)
1740 {
1741         struct dissector_handle *handle;
1742
1743         handle = g_malloc(sizeof (struct dissector_handle));
1744         handle->name = NULL;
1745         handle->is_new = TRUE;
1746         handle->dissector.new = dissector;
1747         handle->protocol = find_protocol_by_id(proto);
1748
1749         return handle;
1750 }
1751
1752 /* Register a dissector by name. */
1753 void
1754 register_dissector(const char *name, dissector_t dissector, int proto)
1755 {
1756         struct dissector_handle *handle;
1757
1758         /* Create our hash table if it doesn't already exist */
1759         if (registered_dissectors == NULL) {
1760                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1761                 g_assert(registered_dissectors != NULL);
1762         }
1763
1764         /* Make sure the registration is unique */
1765         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1766
1767         handle = g_malloc(sizeof (struct dissector_handle));
1768         handle->name = name;
1769         handle->is_new = FALSE;
1770         handle->dissector.old = dissector;
1771         handle->protocol = find_protocol_by_id(proto);
1772
1773         g_hash_table_insert(registered_dissectors, (gpointer)name,
1774             (gpointer) handle);
1775 }
1776
1777 void
1778 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1779 {
1780         struct dissector_handle *handle;
1781
1782         /* Create our hash table if it doesn't already exist */
1783         if (registered_dissectors == NULL) {
1784                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1785                 g_assert(registered_dissectors != NULL);
1786         }
1787
1788         /* Make sure the registration is unique */
1789         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1790
1791         handle = g_malloc(sizeof (struct dissector_handle));
1792         handle->name = name;
1793         handle->is_new = TRUE;
1794         handle->dissector.new = dissector;
1795         handle->protocol = find_protocol_by_id(proto);
1796
1797         g_hash_table_insert(registered_dissectors, (gpointer)name,
1798             (gpointer) handle);
1799 }
1800
1801 /* Call a dissector through a handle but if the dissector rejected it
1802  * return 0.
1803  */
1804 int
1805 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
1806     packet_info *pinfo, proto_tree *tree)
1807 {
1808         int ret;
1809
1810         g_assert(handle != NULL);
1811         ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE);
1812         return ret;
1813 }
1814
1815 /* Call a dissector through a handle and if this fails call the "data"
1816  * dissector.
1817  */
1818 int
1819 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1820     packet_info *pinfo, proto_tree *tree)
1821 {
1822         int ret;
1823
1824         ret = call_dissector_only(handle, tvb, pinfo, tree);
1825         if (ret == 0) {
1826                 /*
1827                  * The protocol was disabled, or the dissector rejected
1828                  * it.  Just dissect this packet as data.
1829                  */
1830                 g_assert(data_handle != NULL);
1831                 g_assert(data_handle->protocol != NULL);
1832                 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE);
1833                 return tvb_length(tvb);
1834         }
1835         return ret;
1836 }
1837
1838 /*
1839  * Dumps the "layer type"/"decode as" associations to stdout, similar
1840  * to the proto_registrar_dump_*() routines.
1841  *
1842  * There is one record per line. The fields are tab-delimited.
1843  *
1844  * Field 1 = layer type, e.g. "tcp.port"
1845  * Field 2 = selector in decimal
1846  * Field 3 = "decode as" name, e.g. "http"
1847  */
1848
1849
1850 static void
1851 dissector_dump_decodes_display(const gchar *table_name,
1852     ftenum_t selector_type _U_, gpointer key, gpointer value,
1853     gpointer user_data _U_)
1854 {
1855         guint32 selector = (guint32)(unsigned long) key;
1856         dissector_table_t sub_dissectors = find_dissector_table(table_name);
1857         dtbl_entry_t *dtbl_entry;
1858         dissector_handle_t handle;
1859         gint proto_id;
1860         const gchar *decode_as;
1861
1862         g_assert(sub_dissectors);
1863         switch (sub_dissectors->type) {
1864
1865                 case FT_UINT8:
1866                 case FT_UINT16:
1867                 case FT_UINT24:
1868                 case FT_UINT32:
1869                         dtbl_entry = value;
1870                         g_assert(dtbl_entry);
1871
1872                         handle = dtbl_entry->current;
1873                         g_assert(handle);
1874
1875                         proto_id = dissector_handle_get_protocol_index(handle);
1876
1877                         if (proto_id != -1) {
1878                                 decode_as = proto_get_protocol_filter_name(proto_id);
1879                                 g_assert(decode_as != NULL);
1880                                 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
1881                         }
1882                         break;
1883
1884         default:
1885                 break;
1886         }
1887 }
1888
1889 void
1890 dissector_dump_decodes()
1891 {
1892         dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
1893 }
1894
1895 static GPtrArray* post_dissectors = NULL;
1896 static guint num_of_postdissectors = 0;
1897
1898 void
1899 register_postdissector(dissector_handle_t handle)
1900 {
1901     if (!post_dissectors)
1902         post_dissectors = g_ptr_array_new();
1903
1904     g_ptr_array_add(post_dissectors, handle);
1905     num_of_postdissectors++;
1906 }
1907
1908 gboolean
1909 have_postdissector()
1910 {
1911     guint i;
1912     dissector_handle_t handle;
1913
1914     for(i = 0; i < num_of_postdissectors; i++) {
1915         handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
1916
1917         if (handle->protocol != NULL
1918             && proto_is_protocol_enabled(handle->protocol)) {
1919             /* We have at least one enabled postdissector */
1920             return TRUE;
1921         }
1922     }
1923     return FALSE;
1924 }
1925
1926 void
1927 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1928 {
1929     guint i;
1930
1931     for(i = 0; i < num_of_postdissectors; i++) {
1932         call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
1933                             tvb,pinfo,tree);
1934     }
1935 }