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