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