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