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