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