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