From Chris Waters: export "find_dissector_table()" and add
[obnox/wireshark/wip.git] / epan / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id: packet.c,v 1.82 2002/11/15 03:10:36 guy Exp $
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 #ifdef NEED_INET_V6DEFS_H
43 # include "inet_v6defs.h"
44 #endif
45
46 #include "packet.h"
47 #include "timestamp.h"
48
49 #include "atalk-utils.h"
50 #include "ipv6-utils.h"
51 #include "sna-utils.h"
52 #include "osi-utils.h"
53 #include "to_str.h"
54
55 #include "resolv.h"
56 #include "tvbuff.h"
57 #include "plugins.h"
58 #include "epan_dissect.h"
59
60 #include "../reassemble.h"
61
62 static gint proto_malformed = -1;
63 static dissector_handle_t frame_handle = NULL;
64 static dissector_handle_t data_handle = NULL;
65
66 const true_false_string flags_set_truth = {
67   "Set",
68   "Not set"
69 };
70
71 void
72 packet_init(void)
73 {
74   frame_handle = find_dissector("frame");
75   data_handle = find_dissector("data");
76   proto_malformed = proto_get_id_by_filter_name("malformed");
77 }
78
79 void
80 packet_cleanup(void)
81 {
82         /* nothing */
83 }
84
85 /*
86  * Given a tvbuff, and a length from a packet header, adjust the length
87  * of the tvbuff to reflect the specified length.
88  */
89 void
90 set_actual_length(tvbuff_t *tvb, guint specified_len)
91 {
92   if (specified_len < tvb_reported_length(tvb)) {
93     /* Adjust the length of this tvbuff to include only the specified
94        payload length.
95
96        The dissector above the one calling us (the dissector above is
97        probably us) may use that to determine how much of its packet
98        was padding. */
99     tvb_set_reported_length(tvb, specified_len);
100   }
101 }
102
103 /* Allow protocols to register "init" routines, which are called before
104    we make a pass through a capture file and dissect all its packets
105    (e.g., when we read in a new capture file, or run a "filter packets"
106    or "colorize packets" pass over the current capture file). */
107 static GSList *init_routines;
108
109 void
110 register_init_routine(void (*func)(void))
111 {
112         init_routines = g_slist_append(init_routines, func);
113 }
114
115 /* Initialize all data structures used for dissection. */
116 static void
117 call_init_routine(gpointer routine, gpointer dummy _U_)
118 {
119         void (*func)(void) = routine;
120
121         (*func)();
122 }
123
124 void
125 init_dissection(void)
126 {
127         /* Initialize the table of conversations. */
128         epan_conversation_init();
129
130         /* Initialize the table of circuits. */
131         epan_circuit_init();
132
133         /* Initialize protocol-specific variables. */
134         g_slist_foreach(init_routines, &call_init_routine, NULL);
135
136         /* Initialize the common data structures for fragment reassembly.
137            Must be done *after* calling init routines, as those routines
138            may free up space for fragments, which they find by using the
139            data structures that "reassemble_init()" frees. */
140         reassemble_init();
141 }
142
143 /* Allow protocols to register a "cleanup" routine to be
144  * run after the initial sequential run through the packets.
145  * Note that the file can still be open after this; this is not
146  * the final cleanup. */
147 static GSList *postseq_cleanup_routines;
148
149 void
150 register_postseq_cleanup_routine(void (*func)(void))
151 {
152         postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
153                         func);
154 }
155
156 /* Call all the registered "postseq_cleanup" routines. */
157 static void
158 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
159 {
160         void (*func)(void) = routine;
161
162         (*func)();
163 }
164
165 void
166 postseq_cleanup_all_protocols(void)
167 {
168         g_slist_foreach(postseq_cleanup_routines,
169                         &call_postseq_cleanup_routine, NULL);
170 }
171
172 /* Contains information about data sources. */
173 static GMemChunk *data_source_chunk = NULL;
174
175 /*
176  * Add a new data source to the list of data sources for a frame, given
177  * the tvbuff for the data source and its name.
178  */
179 void
180 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, char *name)
181 {
182         data_source *src;
183
184         if (data_source_chunk == NULL) {
185                 data_source_chunk = g_mem_chunk_new("data_source_chunk",
186                     sizeof (data_source), 10 * sizeof (data_source),
187                     G_ALLOC_AND_FREE);
188         }
189         src = g_mem_chunk_alloc(data_source_chunk);
190         src->tvb = tvb;
191         /*
192          * XXX - if we require this argument to be a string constant,
193          * we don't need to allocate a buffer for a copy and make a
194          * copy, and wouldn't need to free the buffer, either.
195          */
196         src->name = g_strdup(name);
197         pinfo->data_src = g_slist_append(pinfo->data_src, src);
198 }
199
200 /*
201  * Free up a frame's list of data sources.
202  */
203 void
204 free_data_sources(packet_info *pinfo)
205 {
206         GSList *src_le;
207         data_source *src;
208
209         for (src_le = pinfo->data_src; src_le != NULL; src_le = src_le->next) {
210                 src = src_le->data;
211                 g_free(src->name);
212                 g_mem_chunk_free(data_source_chunk, src);
213         }
214         g_slist_free(pinfo->data_src);
215         pinfo->data_src = NULL;
216 }
217
218 /* Allow dissectors to register a "final_registration" routine
219  * that is run like the proto_register_XXX() routine, but the end
220  * end of the epan_init() function; that is, *after* all other
221  * subsystems, like dfilters, have finished initializing. This is
222  * useful for dissector registration routines which need to compile
223  * display filters. dfilters can't initialize itself until all protocols
224  * have registereed themselves. */
225 static GSList *final_registration_routines;
226
227 void
228 register_final_registration_routine(void (*func)(void))
229 {
230         final_registration_routines = g_slist_append(final_registration_routines,
231                         func);
232 }
233
234 /* Call all the registered "final_registration" routines. */
235 static void
236 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
237 {
238         void (*func)(void) = routine;
239
240         (*func)();
241 }
242
243 void
244 final_registration_all_protocols(void)
245 {
246         g_slist_foreach(final_registration_routines,
247                         &call_final_registration_routine, NULL);
248 }
249
250
251 /* Creates the top-most tvbuff and calls dissect_frame() */
252 void
253 dissect_packet(epan_dissect_t *edt, union wtap_pseudo_header *pseudo_header,
254                const guchar *pd, frame_data *fd, column_info *cinfo)
255 {
256         int i;
257
258         if (cinfo != NULL) {
259                 for (i = 0; i < cinfo->num_cols; i++) {
260                         cinfo->col_buf[i][0] = '\0';
261                         cinfo->col_data[i] = cinfo->col_buf[i];
262                         cinfo->col_expr[i][0] = '\0';
263                         cinfo->col_expr_val[i][0] = '\0';
264                 }
265
266                 col_set_writable(cinfo, TRUE);
267         }
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_dst.type = AT_NONE;
275         edt->pi.net_src.type = AT_NONE;
276         edt->pi.net_dst.type = AT_NONE;
277         edt->pi.src.type = AT_NONE;
278         edt->pi.dst.type = AT_NONE;
279         edt->pi.ethertype = 0;
280         edt->pi.ipproto  = 0;
281         edt->pi.ipxptype = 0;
282         edt->pi.ctype = CT_NONE;
283         edt->pi.circuit_id = 0;
284         edt->pi.fragmented = FALSE;
285         edt->pi.in_error_pkt = FALSE;
286         edt->pi.ptype = PT_NONE;
287         edt->pi.srcport  = 0;
288         edt->pi.destport = 0;
289         edt->pi.match_port = 0;
290         edt->pi.can_desegment = 0;
291         edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
292         edt->pi.private_data = NULL;
293
294         TRY {
295                 edt->tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
296                 /* Add this tvbuffer into the data_src list */
297                 add_new_data_source(&edt->pi, edt->tvb, "Frame");
298
299                 /* Even though dissect_frame() catches all the exceptions a
300                  * sub-dissector can throw, dissect_frame() itself may throw
301                  * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
302                  * in this function. */
303                 if(frame_handle != NULL)
304                   call_dissector(frame_handle, edt->tvb, &edt->pi, edt->tree);
305
306         }
307         CATCH(BoundsError) {
308                 g_assert_not_reached();
309         }
310         CATCH(ReportedBoundsError) {
311           if(proto_malformed != -1){
312                 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
313                                 "[Malformed Frame: Packet Length]" );
314           }
315           else {
316             g_assert_not_reached();
317           }
318         }
319         ENDTRY;
320
321         fd->flags.visited = 1;
322 }
323
324 /*********************** code added for sub-dissector lookup *********************/
325
326 /*
327  * An dissector handle.
328  */
329 struct dissector_handle {
330         const char      *name;          /* dissector name */
331         gboolean        is_new;         /* TRUE if new-style dissector */
332         union {
333                 dissector_t     old;
334                 new_dissector_t new;
335         } dissector;
336         int             proto_index;
337 };
338
339 /*
340  * Call a dissector through a handle.
341  * If the protocol for that handle isn't enabled, return 0 without
342  * calling the dissector.
343  * Otherwise, if the handle refers to a new-style dissector, call the
344  * dissector and return its return value, otherwise call it and return
345  * the length of the tvbuff pointed to by the argument.
346  */
347 static int
348 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb,
349     packet_info *pinfo, proto_tree *tree)
350 {
351         const char *saved_proto;
352         guint16 saved_can_desegment;
353         int ret;
354
355         if (handle->proto_index != -1 &&
356             !proto_is_protocol_enabled(handle->proto_index)) {
357                 /*
358                  * The protocol isn't enabled.
359                  */
360                 return 0;
361         }
362
363         saved_proto = pinfo->current_proto;
364         saved_can_desegment = pinfo->can_desegment;
365
366         /*
367          * can_desegment is set to 2 by anyone which offers the
368          * desegmentation api/service.
369          * Then everytime a subdissector is called it is decremented
370          * by one.
371          * Thus only the subdissector immediately on top of whoever
372          * offers this service can use it.
373          */
374         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
375         if (handle->proto_index != -1) {
376                 pinfo->current_proto =
377                     proto_get_protocol_short_name(handle->proto_index);
378         }
379         if (handle->is_new)
380                 ret = (*handle->dissector.new)(tvb, pinfo, tree);
381         else {
382                 (*handle->dissector.old)(tvb, pinfo, tree);
383                 ret = tvb_length(tvb);
384                 if (ret == 0) {
385                         /*
386                          * XXX - a tvbuff can have 0 bytes of data in
387                          * it, so we have to make sure we don't return
388                          * 0.
389                          */
390                         ret = 1;
391                 }
392         }
393         pinfo->current_proto = saved_proto;
394         pinfo->can_desegment = saved_can_desegment;
395         return ret;
396 }
397
398 /*
399  * An entry in the hash table portion of a dissector table.
400  */
401 struct dtbl_entry {
402         dissector_handle_t initial;
403         dissector_handle_t current;
404 };
405
406 /*
407  * A dissector table.
408  *
409  * "hash_table" is a hash table, indexed by port number, supplying
410  * a "struct dtbl_entry"; it records what dissector is assigned to
411  * that port number in that table.
412  *
413  * "dissector_handles" is a list of all dissectors that *could* be
414  * used in that table; not all of them are necessarily in the table,
415  * as they may be for protocols that don't have a fixed port number.
416  *
417  * "ui_name" is the name the dissector table has in the user interface.
418  *
419  * "type" is a field type giving the width of the port number for that
420  * dissector table.
421  *
422  * "base" is the base in which to display the port number for that
423  * dissector table.
424  */
425 struct dissector_table {
426         GHashTable      *hash_table;
427         GSList          *dissector_handles;
428         char            *ui_name;
429         ftenum_t        type;
430         int             base;
431 };
432
433 static GHashTable *dissector_tables = NULL;
434
435 /* Finds a dissector table by table name. */
436 dissector_table_t
437 find_dissector_table(const char *name)
438 {
439         g_assert(dissector_tables);
440         return g_hash_table_lookup( dissector_tables, name );
441 }
442
443 void
444 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
445 {
446         dissector_table_t sub_dissectors = find_dissector_table( name);
447         dtbl_entry_t *dtbl_entry;
448
449 /* sanity check */
450         g_assert( sub_dissectors);
451
452         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
453         dtbl_entry->current = handle;
454         dtbl_entry->initial = dtbl_entry->current;
455
456 /* do the table insertion */
457         g_hash_table_insert( sub_dissectors->hash_table,
458             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
459
460         /*
461          * Now add it to the list of handles that could be used with this
462          * table, because it *is* being used with this table.
463          */
464         dissector_add_handle(name, handle);
465 }
466
467 /* delete the entry for this dissector at this pattern */
468
469 /* NOTE: this doesn't use the dissector call variable. It is included to */
470 /*      be consistant with the dissector_add and more importantly to be used */
471 /*      if the technique of adding a temporary dissector is implemented.  */
472 /*      If temporary dissectors are deleted, then the original dissector must */
473 /*      be available. */
474 void
475 dissector_delete(const char *name, guint32 pattern,
476         dissector_handle_t handle _U_)
477 {
478         dissector_table_t sub_dissectors = find_dissector_table( name);
479         dtbl_entry_t *dtbl_entry;
480
481 /* sanity check */
482         g_assert( sub_dissectors);
483
484         /*
485          * Find the entry.
486          */
487         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
488             GUINT_TO_POINTER(pattern));
489
490         if (dtbl_entry != NULL) {
491                 /*
492                  * Found - remove it.
493                  */
494                 g_hash_table_remove(sub_dissectors->hash_table,
495                     GUINT_TO_POINTER(pattern));
496
497                 /*
498                  * Now free up the entry.
499                  */
500                 g_free(dtbl_entry);
501         }
502 }
503
504 void
505 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
506 {
507         dissector_table_t sub_dissectors = find_dissector_table( name);
508         dtbl_entry_t *dtbl_entry;
509
510 /* sanity check */
511         g_assert( sub_dissectors);
512
513         /*
514          * See if the entry already exists. If so, reuse it.
515          */
516         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
517             GUINT_TO_POINTER(pattern));
518         if (dtbl_entry != NULL) {
519           dtbl_entry->current = handle;
520           return;
521         }
522
523         /*
524          * Don't create an entry if there is no dissector handle - I.E. the
525          * user said not to decode something that wasn't being decoded
526          * in the first place.
527          */
528         if (handle == NULL)
529           return;
530
531         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
532         dtbl_entry->initial = NULL;
533         dtbl_entry->current = handle;
534
535 /* do the table insertion */
536         g_hash_table_insert( sub_dissectors->hash_table,
537             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
538 }
539
540 /* Reset a dissector in a sub-dissector table to its initial value. */
541 void
542 dissector_reset(const char *name, guint32 pattern)
543 {
544         dissector_table_t sub_dissectors = find_dissector_table( name);
545         dtbl_entry_t *dtbl_entry;
546
547 /* sanity check */
548         g_assert( sub_dissectors);
549
550         /*
551          * Find the entry.
552          */
553         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
554             GUINT_TO_POINTER(pattern));
555
556         if (dtbl_entry == NULL)
557                 return;
558
559         /*
560          * Found - is there an initial value?
561          */
562         if (dtbl_entry->initial != NULL) {
563                 dtbl_entry->current = dtbl_entry->initial;
564         } else {
565                 g_hash_table_remove(sub_dissectors->hash_table,
566                     GUINT_TO_POINTER(pattern));
567                 g_free(dtbl_entry);
568         }
569 }
570
571 /* Look for a given port in a given dissector table and, if found, call
572    the dissector with the arguments supplied, and return TRUE, otherwise
573    return FALSE. */
574 gboolean
575 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
576     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
577 {
578         dtbl_entry_t *dtbl_entry;
579         struct dissector_handle *handle;
580         guint32 saved_match_port;
581         int ret;
582
583         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
584             GUINT_TO_POINTER(port));
585         if (dtbl_entry != NULL) {
586                 /*
587                  * Is there currently a dissector handle for this entry?
588                  */
589                 handle = dtbl_entry->current;
590                 if (handle == NULL) {
591                         /*
592                          * No - pretend this dissector didn't exist,
593                          * so that other dissectors might have a chance
594                          * to dissect this packet.
595                          */
596                         return FALSE;
597                 }
598
599                 /*
600                  * Save the current value of "pinfo->match_port",
601                  * set it to the port that matched, call the
602                  * dissector, and restore "pinfo->match_port".
603                  */
604                 saved_match_port = pinfo->match_port;
605                 pinfo->match_port = port;
606                 ret = call_dissector_work(handle, tvb, pinfo, tree);
607                 pinfo->match_port = saved_match_port;
608
609                 /*
610                  * If a new-style dissector returned 0, it means that
611                  * it didn't think this tvbuff represented a packet for
612                  * its protocol, and didn't dissect anything.
613                  *
614                  * Old-style dissectors can't reject the packet.
615                  *
616                  * 0 is also returned if the protocol wasn't enabled.
617                  *
618                  * If the packet was rejected, we return FALSE, so that
619                  * other dissectors might have a chance to dissect this
620                  * packet, otherwise we return TRUE.
621                  */
622                 return ret != 0;
623         }
624         return FALSE;
625 }
626
627 /* Look for a given port in a given dissector table and, if found, return
628    the dissector handle for that port. */
629 dissector_handle_t
630 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
631 {
632         dtbl_entry_t *dtbl_entry;
633
634         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table,
635             GUINT_TO_POINTER(port));
636         if (dtbl_entry != NULL)
637                 return dtbl_entry->current;
638         else
639                 return NULL;
640 }
641
642 dissector_handle_t
643 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
644 {
645         return dtbl_entry->current;
646 }
647
648 /* Add a handle to the list of handles that *could* be used with this
649    table.  That list is used by code in the UI. */
650 void
651 dissector_add_handle(const char *name, dissector_handle_t handle)
652 {
653         dissector_table_t sub_dissectors = find_dissector_table( name);
654         GSList *entry;
655
656         /* sanity check */
657         g_assert(sub_dissectors != NULL);
658
659         /* Is it already in this list? */
660         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
661         if (entry != NULL) {
662                 /*
663                  * Yes - don't insert it again.
664                  */
665                 return;
666         }
667
668         /* Add it to the list. */
669         sub_dissectors->dissector_handles =
670             g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
671 }
672
673 dissector_handle_t
674 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
675 {
676         return dtbl_entry->initial;
677 }
678
679 /**************************************************/
680 /*                                                */
681 /*       Routines to walk dissector tables        */
682 /*                                                */
683 /**************************************************/
684
685 typedef struct dissector_foreach_info {
686   gpointer     caller_data;
687   DATFunc      caller_func;
688   GHFunc       next_func;
689   gchar       *table_name;
690 } dissector_foreach_info_t;
691
692 /*
693  * Called for each entry in a dissector table.
694  */
695 static void
696 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
697 {
698         dissector_foreach_info_t *info;
699         dtbl_entry_t *dtbl_entry;
700
701         g_assert(value);
702         g_assert(user_data);
703
704         dtbl_entry = value;
705         if (dtbl_entry->current == NULL ||
706             dtbl_entry->current->proto_index == -1) {
707                 /*
708                  * Either there is no dissector for this entry, or
709                  * the dissector doesn't have a protocol associated
710                  * with it.
711                  *
712                  * XXX - should the latter check be done?
713                  */
714                 return;
715         }
716
717         info = user_data;
718         info->caller_func(info->table_name, key, value, info->caller_data);
719 }
720
721 /*
722  * Called for each entry in the table of all dissector tables.
723  */
724 static void
725 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
726 {
727         dissector_table_t sub_dissectors;
728         dissector_foreach_info_t *info;
729
730         g_assert(value);
731         g_assert(user_data);
732
733         sub_dissectors = value;
734         info = user_data;
735         info->table_name = (gchar*) key;
736         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
737 }
738
739 /*
740  * Walk all dissector tables calling a user supplied function on each
741  * entry.
742  */
743 void
744 dissector_all_tables_foreach (DATFunc func,
745                               gpointer user_data)
746 {
747         dissector_foreach_info_t info;
748
749         info.caller_data = user_data;
750         info.caller_func = func;
751         info.next_func = dissector_table_foreach_func;
752         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
753 }
754
755 /*
756  * Walk one dissector table's hash table calling a user supplied function
757  * on each entry.
758  */
759 void
760 dissector_table_foreach (char *name,
761                          DATFunc func,
762                          gpointer user_data)
763 {
764         dissector_foreach_info_t info;
765         dissector_table_t sub_dissectors = find_dissector_table( name);
766
767         info.table_name = name;
768         info.caller_func = func;
769         info.caller_data = user_data;
770         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
771 }
772
773 /*
774  * Walk one dissector table's list of handles calling a user supplied
775  * function on each entry.
776  */
777 void
778 dissector_table_foreach_handle(char *name, DATFunc_handle func, gpointer user_data)
779 {
780         dissector_table_t sub_dissectors = find_dissector_table( name);
781         GSList *tmp;
782
783         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
784             tmp = g_slist_next(tmp))
785                 func(name, tmp->data, user_data);
786 }
787
788 /*
789  * Called for each entry in a dissector table.
790  */
791 static void
792 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
793 {
794         dtbl_entry_t *dtbl_entry;
795         dissector_foreach_info_t *info;
796
797         g_assert(value);
798         g_assert(user_data);
799
800         dtbl_entry = value;
801         if (dtbl_entry->initial == dtbl_entry->current) {
802                 /*
803                  * Entry hasn't changed - don't call the function.
804                  */
805                 return;
806         }
807
808         info = user_data;
809         info->caller_func(info->table_name, key, value, info->caller_data);
810 }
811
812 /*
813  * Walk all dissector tables calling a user supplied function only on
814  * any entry that has been changed from its original state.
815  */
816 void
817 dissector_all_tables_foreach_changed (DATFunc func,
818                                       gpointer user_data)
819 {
820         dissector_foreach_info_t info;
821
822         info.caller_data = user_data;
823         info.caller_func = func;
824         info.next_func = dissector_table_foreach_changed_func;
825         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
826 }
827
828 /*
829  * Walk one dissector table calling a user supplied function only on
830  * any entry that has been changed from its original state.
831  */
832 void
833 dissector_table_foreach_changed (char *name,
834                                  DATFunc func,
835                                  gpointer user_data)
836 {
837         dissector_foreach_info_t info;
838         dissector_table_t sub_dissectors = find_dissector_table( name);
839
840         info.table_name = name;
841         info.caller_func = func;
842         info.caller_data = user_data;
843         g_hash_table_foreach(sub_dissectors->hash_table,
844             dissector_table_foreach_changed_func, &info);
845 }
846
847 dissector_table_t
848 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
849     int base)
850 {
851         dissector_table_t       sub_dissectors;
852
853         /* Create our hash-of-hashes if it doesn't already exist */
854         if (!dissector_tables) {
855                 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
856                 g_assert(dissector_tables);
857         }
858
859         /* Make sure the registration is unique */
860         g_assert(!g_hash_table_lookup( dissector_tables, name ));
861
862         /* Create and register the dissector table for this name; returns */
863         /* a pointer to the dissector table. */
864         sub_dissectors = g_malloc(sizeof (struct dissector_table));
865         sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
866             g_direct_equal );
867         sub_dissectors->dissector_handles = NULL;
868         sub_dissectors->ui_name = ui_name;
869         sub_dissectors->type = type;
870         sub_dissectors->base = base;
871         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
872         return sub_dissectors;
873 }
874
875 char *
876 get_dissector_table_ui_name(const char *name)
877 {
878         dissector_table_t sub_dissectors = find_dissector_table( name);
879
880         return sub_dissectors->ui_name;
881 }
882
883 ftenum_t
884 get_dissector_table_type(const char *name)
885 {
886         dissector_table_t sub_dissectors = find_dissector_table( name);
887
888         return sub_dissectors->type;
889 }
890
891 int
892 get_dissector_table_base(const char *name)
893 {
894         dissector_table_t sub_dissectors = find_dissector_table( name);
895
896         return sub_dissectors->base;
897 }
898
899 static GHashTable *heur_dissector_lists = NULL;
900
901 typedef struct {
902         heur_dissector_t dissector;
903         int     proto_index;
904 } heur_dtbl_entry_t;
905
906 /* Finds a heuristic dissector table by field name. */
907 static heur_dissector_list_t *
908 find_heur_dissector_list(const char *name)
909 {
910         g_assert(heur_dissector_lists != NULL);
911         return g_hash_table_lookup(heur_dissector_lists, name);
912 }
913
914 void
915 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
916 {
917         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
918         heur_dtbl_entry_t *dtbl_entry;
919
920         /* sanity check */
921         g_assert(sub_dissectors != NULL);
922
923         dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
924         dtbl_entry->dissector = dissector;
925         dtbl_entry->proto_index = proto;
926
927         /* do the table insertion */
928         *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
929 }
930
931 gboolean
932 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
933     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
934 {
935         gboolean status;
936         const char *saved_proto;
937         GSList *entry;
938         heur_dtbl_entry_t *dtbl_entry;
939         guint16 saved_can_desegment;
940
941         /* can_desegment is set to 2 by anyone which offers this api/service.
942            then everytime a subdissector is called it is decremented by one.
943            thus only the subdissector immediately ontop of whoever offers this
944            service can use it.
945         */
946         saved_can_desegment=pinfo->can_desegment;
947         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
948
949         status = FALSE;
950         saved_proto = pinfo->current_proto;
951         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
952                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
953                 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
954                 if (dtbl_entry->proto_index != -1 &&
955                     !proto_is_protocol_enabled(dtbl_entry->proto_index)) {
956                         /*
957                          * No - don't try this dissector.
958                          */
959                         continue;
960                 }
961
962                 if (dtbl_entry->proto_index != -1) {
963                         pinfo->current_proto =
964                             proto_get_protocol_short_name(dtbl_entry->proto_index);
965                 }
966                 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
967                         status = TRUE;
968                         break;
969                 }
970         }
971         pinfo->current_proto = saved_proto;
972         pinfo->can_desegment=saved_can_desegment;
973         return status;
974 }
975
976 void
977 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
978 {
979         /* Create our hash-of-lists if it doesn't already exist */
980         if (heur_dissector_lists == NULL) {
981                 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
982                 g_assert(heur_dissector_lists != NULL);
983         }
984
985         /* Make sure the registration is unique */
986         g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
987
988         *sub_dissectors = NULL; /* initially empty */
989         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
990             (gpointer) sub_dissectors);
991 }
992
993 /*
994  * Register dissectors by name; used if one dissector always calls a
995  * particular dissector, or if it bases the decision of which dissector
996  * to call on something other than a numerical value or on "try a bunch
997  * of dissectors until one likes the packet".
998  */
999
1000 /*
1001  * List of registered dissectors.
1002  */
1003 static GHashTable *registered_dissectors = NULL;
1004
1005 /* Get the short name of the protocol for a dissector handle. */
1006 char *
1007 dissector_handle_get_short_name(dissector_handle_t handle)
1008 {
1009         return proto_get_protocol_short_name(handle->proto_index);
1010 }
1011
1012 /* Get the index of the protocol for a dissector handle. */
1013 int
1014 dissector_handle_get_protocol_index(dissector_handle_t handle)
1015 {
1016   return handle->proto_index;
1017 }
1018
1019 /* Find a registered dissector by name. */
1020 dissector_handle_t
1021 find_dissector(const char *name)
1022 {
1023         g_assert(registered_dissectors != NULL);
1024         return g_hash_table_lookup(registered_dissectors, name);
1025 }
1026
1027 /* Create an anonymous handle for a dissector. */
1028 dissector_handle_t
1029 create_dissector_handle(dissector_t dissector, int proto)
1030 {
1031         struct dissector_handle *handle;
1032
1033         handle = g_malloc(sizeof (struct dissector_handle));
1034         handle->name = NULL;
1035         handle->is_new = FALSE;
1036         handle->dissector.old = dissector;
1037         handle->proto_index = proto;
1038
1039         return handle;
1040 }
1041
1042 dissector_handle_t
1043 new_create_dissector_handle(new_dissector_t dissector, int proto)
1044 {
1045         struct dissector_handle *handle;
1046
1047         handle = g_malloc(sizeof (struct dissector_handle));
1048         handle->name = NULL;
1049         handle->is_new = TRUE;
1050         handle->dissector.new = dissector;
1051         handle->proto_index = proto;
1052
1053         return handle;
1054 }
1055
1056 /* Register a dissector by name. */
1057 void
1058 register_dissector(const char *name, dissector_t dissector, int proto)
1059 {
1060         struct dissector_handle *handle;
1061
1062         /* Create our hash table if it doesn't already exist */
1063         if (registered_dissectors == NULL) {
1064                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1065                 g_assert(registered_dissectors != NULL);
1066         }
1067
1068         /* Make sure the registration is unique */
1069         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1070
1071         handle = g_malloc(sizeof (struct dissector_handle));
1072         handle->name = name;
1073         handle->is_new = FALSE;
1074         handle->dissector.old = dissector;
1075         handle->proto_index = proto;
1076
1077         g_hash_table_insert(registered_dissectors, (gpointer)name,
1078             (gpointer) handle);
1079 }
1080
1081 void
1082 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1083 {
1084         struct dissector_handle *handle;
1085
1086         /* Create our hash table if it doesn't already exist */
1087         if (registered_dissectors == NULL) {
1088                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1089                 g_assert(registered_dissectors != NULL);
1090         }
1091
1092         /* Make sure the registration is unique */
1093         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1094
1095         handle = g_malloc(sizeof (struct dissector_handle));
1096         handle->name = name;
1097         handle->is_new = TRUE;
1098         handle->dissector.new = dissector;
1099         handle->proto_index = proto;
1100
1101         g_hash_table_insert(registered_dissectors, (gpointer)name,
1102             (gpointer) handle);
1103 }
1104
1105 /* Call a dissector through a handle. */
1106 int
1107 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1108     packet_info *pinfo, proto_tree *tree)
1109 {
1110         int ret;
1111
1112         ret = call_dissector_work(handle, tvb, pinfo, tree);
1113         if (ret == 0) {
1114                 /*
1115                  * The protocol was disabled, or the dissector rejected
1116                  * it.  Just dissect this packet as data.
1117                  */
1118                 g_assert(data_handle != NULL);
1119                 g_assert(data_handle->proto_index != -1);
1120                 call_dissector(data_handle, tvb, pinfo, tree);
1121                 return tvb_length(tvb);
1122         }
1123         return ret;
1124 }