Add a "saved_can_desegment" field to the "packet_info" structure, so
[obnox/wireshark/wip.git] / epan / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id: packet.c,v 1.100 2003/12/29 22:44:50 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 #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, func);
109 }
110
111 /* Initialize all data structures used for dissection. */
112 static void
113 call_init_routine(gpointer routine, gpointer dummy _U_)
114 {
115         void (*func)(void) = routine;
116
117         (*func)();
118 }
119
120 void
121 init_dissection(void)
122 {
123         /* Initialize the table of conversations. */
124         epan_conversation_init();
125
126         /* Initialize the table of circuits. */
127         epan_circuit_init();
128
129         /* Initialize protocol-specific variables. */
130         g_slist_foreach(init_routines, &call_init_routine, NULL);
131
132         /* Initialize the common data structures for fragment reassembly.
133            Must be done *after* calling init routines, as those routines
134            may free up space for fragments, which they find by using the
135            data structures that "reassemble_init()" frees. */
136         reassemble_init();
137 }
138
139 /* Allow protocols to register a "cleanup" routine to be
140  * run after the initial sequential run through the packets.
141  * Note that the file can still be open after this; this is not
142  * the final cleanup. */
143 static GSList *postseq_cleanup_routines;
144
145 void
146 register_postseq_cleanup_routine(void (*func)(void))
147 {
148         postseq_cleanup_routines = g_slist_append(postseq_cleanup_routines,
149                         func);
150 }
151
152 /* Call all the registered "postseq_cleanup" routines. */
153 static void
154 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
155 {
156         void (*func)(void) = routine;
157
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(name);
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                         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)(void) = 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         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
518         return ret;
519 }
520
521 /*
522  * An entry in the hash table portion of a dissector table.
523  */
524 struct dtbl_entry {
525         dissector_handle_t initial;
526         dissector_handle_t current;
527 };
528
529 /*
530  * A dissector table.
531  *
532  * "hash_table" is a hash table, indexed by port number, supplying
533  * a "struct dtbl_entry"; it records what dissector is assigned to
534  * that port number in that table.
535  *
536  * "dissector_handles" is a list of all dissectors that *could* be
537  * used in that table; not all of them are necessarily in the table,
538  * as they may be for protocols that don't have a fixed port number.
539  *
540  * "ui_name" is the name the dissector table has in the user interface.
541  *
542  * "type" is a field type giving the width of the port number for that
543  * dissector table.
544  *
545  * "base" is the base in which to display the port number for that
546  * dissector table.
547  */
548 struct dissector_table {
549         GHashTable      *hash_table;
550         GSList          *dissector_handles;
551         char            *ui_name;
552         ftenum_t        type;
553         int             base;
554 };
555
556 static GHashTable *dissector_tables = NULL;
557
558 /* Finds a dissector table by table name. */
559 dissector_table_t
560 find_dissector_table(const char *name)
561 {
562         g_assert(dissector_tables);
563         return g_hash_table_lookup( dissector_tables, name );
564 }
565
566 /* Find an entry in a uint dissector table. */ 
567 static dtbl_entry_t *
568 find_uint_dtbl_entry(dissector_table_t sub_dissectors, guint32 pattern)
569 {
570         switch (sub_dissectors->type) {
571
572         case FT_UINT8:
573         case FT_UINT16:
574         case FT_UINT24:
575         case FT_UINT32:
576                 /*
577                  * You can do a port lookup in these tables.
578                  */
579                 break;
580
581         default:
582                 /*
583                  * But you can't do a port lookup in any other types
584                  * of tables.
585                  */
586                 g_assert_not_reached();
587         }
588
589         /*
590          * Find the entry.
591          */
592         return g_hash_table_lookup(sub_dissectors->hash_table,
593             GUINT_TO_POINTER(pattern));
594 }
595
596 /* Add an entry to a uint dissector table. */
597 void
598 dissector_add(const char *name, guint32 pattern, dissector_handle_t handle)
599 {
600         dissector_table_t sub_dissectors = find_dissector_table( name);
601         dtbl_entry_t *dtbl_entry;
602
603 /* sanity checks */
604         g_assert( sub_dissectors);
605         switch (sub_dissectors->type) {
606
607         case FT_UINT8:
608         case FT_UINT16:
609         case FT_UINT24:
610         case FT_UINT32:
611                 /*
612                  * You can do a port lookup in these tables.
613                  */
614                 break;
615
616         default:
617                 /*
618                  * But you can't do a port lookup in any other types
619                  * of tables.
620                  */
621                 g_assert_not_reached();
622         }
623
624         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
625         dtbl_entry->current = handle;
626         dtbl_entry->initial = dtbl_entry->current;
627
628 /* do the table insertion */
629         g_hash_table_insert( sub_dissectors->hash_table,
630             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
631
632         /*
633          * Now add it to the list of handles that could be used with this
634          * table, because it *is* being used with this table.
635          */
636         dissector_add_handle(name, handle);
637 }
638
639 /* Delete the entry for a dissector in a uint dissector table
640    with a particular pattern. */
641
642 /* NOTE: this doesn't use the dissector call variable. It is included to */
643 /*      be consistant with the dissector_add and more importantly to be used */
644 /*      if the technique of adding a temporary dissector is implemented.  */
645 /*      If temporary dissectors are deleted, then the original dissector must */
646 /*      be available. */
647 void
648 dissector_delete(const char *name, guint32 pattern,
649         dissector_handle_t handle _U_)
650 {
651         dissector_table_t sub_dissectors = find_dissector_table( name);
652         dtbl_entry_t *dtbl_entry;
653
654 /* sanity check */
655         g_assert( sub_dissectors);
656
657         /*
658          * Find the entry.
659          */
660         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
661
662         if (dtbl_entry != NULL) {
663                 /*
664                  * Found - remove it.
665                  */
666                 g_hash_table_remove(sub_dissectors->hash_table,
667                     GUINT_TO_POINTER(pattern));
668
669                 /*
670                  * Now free up the entry.
671                  */
672                 g_free(dtbl_entry);
673         }
674 }
675
676 /* Change the entry for a dissector in a uint dissector table
677    with a particular pattern to use a new dissector handle. */
678 void
679 dissector_change(const char *name, guint32 pattern, dissector_handle_t handle)
680 {
681         dissector_table_t sub_dissectors = find_dissector_table( name);
682         dtbl_entry_t *dtbl_entry;
683
684 /* sanity check */
685         g_assert( sub_dissectors);
686
687         /*
688          * See if the entry already exists. If so, reuse it.
689          */
690         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
691         if (dtbl_entry != NULL) {
692           dtbl_entry->current = handle;
693           return;
694         }
695
696         /*
697          * Don't create an entry if there is no dissector handle - I.E. the
698          * user said not to decode something that wasn't being decoded
699          * in the first place.
700          */
701         if (handle == NULL)
702           return;
703
704         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
705         dtbl_entry->initial = NULL;
706         dtbl_entry->current = handle;
707
708 /* do the table insertion */
709         g_hash_table_insert( sub_dissectors->hash_table,
710             GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
711 }
712
713 /* Reset an entry in a uint dissector table to its initial value. */
714 void
715 dissector_reset(const char *name, guint32 pattern)
716 {
717         dissector_table_t sub_dissectors = find_dissector_table( name);
718         dtbl_entry_t *dtbl_entry;
719
720 /* sanity check */
721         g_assert( sub_dissectors);
722
723         /*
724          * Find the entry.
725          */
726         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
727
728         if (dtbl_entry == NULL)
729                 return;
730
731         /*
732          * Found - is there an initial value?
733          */
734         if (dtbl_entry->initial != NULL) {
735                 dtbl_entry->current = dtbl_entry->initial;
736         } else {
737                 g_hash_table_remove(sub_dissectors->hash_table,
738                     GUINT_TO_POINTER(pattern));
739                 g_free(dtbl_entry);
740         }
741 }
742
743 /* Look for a given value in a given uint dissector table and, if found,
744    call the dissector with the arguments supplied, and return TRUE,
745    otherwise return FALSE. */
746 gboolean
747 dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
748     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
749 {
750         dtbl_entry_t *dtbl_entry;
751         struct dissector_handle *handle;
752         guint32 saved_match_port;
753         int ret;
754
755         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
756         if (dtbl_entry != NULL) {
757                 /*
758                  * Is there currently a dissector handle for this entry?
759                  */
760                 handle = dtbl_entry->current;
761                 if (handle == NULL) {
762                         /*
763                          * No - pretend this dissector didn't exist,
764                          * so that other dissectors might have a chance
765                          * to dissect this packet.
766                          */
767                         return FALSE;
768                 }
769
770                 /*
771                  * Save the current value of "pinfo->match_port",
772                  * set it to the port that matched, call the
773                  * dissector, and restore "pinfo->match_port".
774                  */
775                 saved_match_port = pinfo->match_port;
776                 pinfo->match_port = port;
777                 ret = call_dissector_work(handle, tvb, pinfo, tree);
778                 pinfo->match_port = saved_match_port;
779
780                 /*
781                  * If a new-style dissector returned 0, it means that
782                  * it didn't think this tvbuff represented a packet for
783                  * its protocol, and didn't dissect anything.
784                  *
785                  * Old-style dissectors can't reject the packet.
786                  *
787                  * 0 is also returned if the protocol wasn't enabled.
788                  *
789                  * If the packet was rejected, we return FALSE, so that
790                  * other dissectors might have a chance to dissect this
791                  * packet, otherwise we return TRUE.
792                  */
793                 return ret != 0;
794         }
795         return FALSE;
796 }
797
798 /* Look for a given value in a given uint dissector table and, if found,
799    return the dissector handle for that value. */
800 dissector_handle_t
801 dissector_get_port_handle(dissector_table_t sub_dissectors, guint32 port)
802 {
803         dtbl_entry_t *dtbl_entry;
804
805         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, port);
806         if (dtbl_entry != NULL)
807                 return dtbl_entry->current;
808         else
809                 return NULL;
810 }
811
812 /* Find an entry in a string dissector table. */ 
813 static dtbl_entry_t *
814 find_string_dtbl_entry(dissector_table_t sub_dissectors, const gchar *pattern)
815 {
816         switch (sub_dissectors->type) {
817
818         case FT_STRING:
819         case FT_STRINGZ:
820                 /*
821                  * You can do a string lookup in these tables.
822                  */
823                 break;
824
825         default:
826                 /*
827                  * But you can't do a string lookup in any other types
828                  * of tables.
829                  */
830                 g_assert_not_reached();
831         }
832
833         /*
834          * Find the entry.
835          */
836         return g_hash_table_lookup(sub_dissectors->hash_table, pattern);
837 }
838
839 /* Add an entry to a string dissector table. */
840 void
841 dissector_add_string(const char *name, gchar *pattern,
842     dissector_handle_t handle)
843 {
844         dissector_table_t sub_dissectors = find_dissector_table( name);
845         dtbl_entry_t *dtbl_entry;
846
847 /* sanity check */
848         g_assert( sub_dissectors);
849
850         switch (sub_dissectors->type) {
851
852         case FT_STRING:
853         case FT_STRINGZ:
854                 /*
855                  * You can do a string lookup in these tables.
856                  */
857                 break;
858
859         default:
860                 /*
861                  * But you can't do a string lookup in any other types
862                  * of tables.
863                  */
864                 g_assert_not_reached();
865         }
866
867         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
868         dtbl_entry->current = handle;
869         dtbl_entry->initial = dtbl_entry->current;
870
871 /* do the table insertion */
872         g_hash_table_insert( sub_dissectors->hash_table, pattern,
873             (gpointer)dtbl_entry);
874
875         /*
876          * Now add it to the list of handles that could be used with this
877          * table, because it *is* being used with this table.
878          */
879         dissector_add_handle(name, handle);
880 }
881
882 /* Delete the entry for a dissector in a string dissector table
883    with a particular pattern. */
884
885 /* NOTE: this doesn't use the dissector call variable. It is included to */
886 /*      be consistant with the dissector_add_string and more importantly to */
887 /*      be used if the technique of adding a temporary dissector is */
888 /*      implemented.  */
889 /*      If temporary dissectors are deleted, then the original dissector must */
890 /*      be available. */
891 void
892 dissector_delete_string(const char *name, const gchar *pattern,
893         dissector_handle_t handle _U_)
894 {
895         dissector_table_t sub_dissectors = find_dissector_table( name);
896         dtbl_entry_t *dtbl_entry;
897
898 /* sanity check */
899         g_assert( sub_dissectors);
900
901         /*
902          * Find the entry.
903          */
904         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
905
906         if (dtbl_entry != NULL) {
907                 /*
908                  * Found - remove it.
909                  */
910                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
911
912                 /*
913                  * Now free up the entry.
914                  */
915                 g_free(dtbl_entry);
916         }
917 }
918
919 /* Change the entry for a dissector in a string dissector table
920    with a particular pattern to use a new dissector handle. */
921 void
922 dissector_change_string(const char *name, gchar *pattern,
923     dissector_handle_t handle)
924 {
925         dissector_table_t sub_dissectors = find_dissector_table( name);
926         dtbl_entry_t *dtbl_entry;
927
928 /* sanity check */
929         g_assert( sub_dissectors);
930
931         /*
932          * See if the entry already exists. If so, reuse it.
933          */
934         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
935         if (dtbl_entry != NULL) {
936           dtbl_entry->current = handle;
937           return;
938         }
939
940         /*
941          * Don't create an entry if there is no dissector handle - I.E. the
942          * user said not to decode something that wasn't being decoded
943          * in the first place.
944          */
945         if (handle == NULL)
946           return;
947
948         dtbl_entry = g_malloc(sizeof (dtbl_entry_t));
949         dtbl_entry->initial = NULL;
950         dtbl_entry->current = handle;
951
952 /* do the table insertion */
953         g_hash_table_insert( sub_dissectors->hash_table, pattern,
954             (gpointer)dtbl_entry);
955 }
956
957 /* Reset an entry in a string sub-dissector table to its initial value. */
958 void
959 dissector_reset_string(const char *name, const gchar *pattern)
960 {
961         dissector_table_t sub_dissectors = find_dissector_table( name);
962         dtbl_entry_t *dtbl_entry;
963
964 /* sanity check */
965         g_assert( sub_dissectors);
966
967         /*
968          * Find the entry.
969          */
970         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
971
972         if (dtbl_entry == NULL)
973                 return;
974
975         /*
976          * Found - is there an initial value?
977          */
978         if (dtbl_entry->initial != NULL) {
979                 dtbl_entry->current = dtbl_entry->initial;
980         } else {
981                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
982                 g_free(dtbl_entry);
983         }
984 }
985
986 /* Look for a given string in a given dissector table and, if found, call
987    the dissector with the arguments supplied, and return TRUE, otherwise
988    return FALSE. */
989 gboolean
990 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
991     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
992 {
993         dtbl_entry_t *dtbl_entry;
994         struct dissector_handle *handle;
995         int ret;
996         const gchar *saved_match_string;
997
998         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
999         if (dtbl_entry != NULL) {
1000                 /*
1001                  * Is there currently a dissector handle for this entry?
1002                  */
1003                 handle = dtbl_entry->current;
1004                 if (handle == NULL) {
1005                         /*
1006                          * No - pretend this dissector didn't exist,
1007                          * so that other dissectors might have a chance
1008                          * to dissect this packet.
1009                          */
1010                         return FALSE;
1011                 }
1012
1013                 /*
1014                  * Save the current value of "pinfo->match_string",
1015                  * set it to the string that matched, call the
1016                  * dissector, and restore "pinfo->match_string".
1017                  */
1018                 saved_match_string = pinfo->match_string;
1019                 pinfo->match_string = string;
1020                 ret = call_dissector_work(handle, tvb, pinfo, tree);
1021                 pinfo->match_string = saved_match_string;
1022
1023                 /*
1024                  * If a new-style dissector returned 0, it means that
1025                  * it didn't think this tvbuff represented a packet for
1026                  * its protocol, and didn't dissect anything.
1027                  *
1028                  * Old-style dissectors can't reject the packet.
1029                  *
1030                  * 0 is also returned if the protocol wasn't enabled.
1031                  *
1032                  * If the packet was rejected, we return FALSE, so that
1033                  * other dissectors might have a chance to dissect this
1034                  * packet, otherwise we return TRUE.
1035                  */
1036                 return ret != 0;
1037         }
1038         return FALSE;
1039 }
1040
1041 /* Look for a given value in a given string dissector table and, if found,
1042    return the dissector handle for that value. */
1043 dissector_handle_t
1044 dissector_get_string_handle(dissector_table_t sub_dissectors,
1045     const gchar *string)
1046 {
1047         dtbl_entry_t *dtbl_entry;
1048
1049         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1050         if (dtbl_entry != NULL)
1051                 return dtbl_entry->current;
1052         else
1053                 return NULL;
1054 }
1055
1056 dissector_handle_t
1057 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1058 {
1059         return dtbl_entry->current;
1060 }
1061
1062 /* Add a handle to the list of handles that *could* be used with this
1063    table.  That list is used by code in the UI. */
1064 void
1065 dissector_add_handle(const char *name, dissector_handle_t handle)
1066 {
1067         dissector_table_t sub_dissectors = find_dissector_table( name);
1068         GSList *entry;
1069
1070         /* sanity check */
1071         g_assert(sub_dissectors != NULL);
1072
1073         /* Is it already in this list? */
1074         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1075         if (entry != NULL) {
1076                 /*
1077                  * Yes - don't insert it again.
1078                  */
1079                 return;
1080         }
1081
1082         /* Add it to the list. */
1083         sub_dissectors->dissector_handles =
1084             g_slist_append(sub_dissectors->dissector_handles, (gpointer)handle);
1085 }
1086
1087 dissector_handle_t
1088 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1089 {
1090         return dtbl_entry->initial;
1091 }
1092
1093 /**************************************************/
1094 /*                                                */
1095 /*       Routines to walk dissector tables        */
1096 /*                                                */
1097 /**************************************************/
1098
1099 typedef struct dissector_foreach_info {
1100   gpointer     caller_data;
1101   DATFunc      caller_func;
1102   GHFunc       next_func;
1103   gchar       *table_name;
1104   ftenum_t     selector_type;
1105 } dissector_foreach_info_t;
1106
1107 /*
1108  * Called for each entry in a dissector table.
1109  */
1110 static void
1111 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1112 {
1113         dissector_foreach_info_t *info;
1114         dtbl_entry_t *dtbl_entry;
1115
1116         g_assert(value);
1117         g_assert(user_data);
1118
1119         dtbl_entry = value;
1120         if (dtbl_entry->current == NULL ||
1121             dtbl_entry->current->protocol == NULL) {
1122                 /*
1123                  * Either there is no dissector for this entry, or
1124                  * the dissector doesn't have a protocol associated
1125                  * with it.
1126                  *
1127                  * XXX - should the latter check be done?
1128                  */
1129                 return;
1130         }
1131
1132         info = user_data;
1133         info->caller_func(info->table_name, info->selector_type, key, value,
1134             info->caller_data);
1135 }
1136
1137 /*
1138  * Called for each entry in the table of all dissector tables.
1139  */
1140 static void
1141 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1142 {
1143         dissector_table_t sub_dissectors;
1144         dissector_foreach_info_t *info;
1145
1146         g_assert(value);
1147         g_assert(user_data);
1148
1149         sub_dissectors = value;
1150         info = user_data;
1151         info->table_name = (gchar*) key;
1152         info->selector_type = get_dissector_table_selector_type(info->table_name);
1153         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1154 }
1155
1156 /*
1157  * Walk all dissector tables calling a user supplied function on each
1158  * entry.
1159  */
1160 void
1161 dissector_all_tables_foreach (DATFunc func,
1162                               gpointer user_data)
1163 {
1164         dissector_foreach_info_t info;
1165
1166         info.caller_data = user_data;
1167         info.caller_func = func;
1168         info.next_func = dissector_table_foreach_func;
1169         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1170 }
1171
1172 /*
1173  * Walk one dissector table's hash table calling a user supplied function
1174  * on each entry.
1175  */
1176 void
1177 dissector_table_foreach (char *name,
1178                          DATFunc func,
1179                          gpointer user_data)
1180 {
1181         dissector_foreach_info_t info;
1182         dissector_table_t sub_dissectors = find_dissector_table( name);
1183
1184         info.table_name = name;
1185         info.selector_type = sub_dissectors->type;
1186         info.caller_func = func;
1187         info.caller_data = user_data;
1188         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1189 }
1190
1191 /*
1192  * Walk one dissector table's list of handles calling a user supplied
1193  * function on each entry.
1194  */
1195 void
1196 dissector_table_foreach_handle(char *name,
1197                                DATFunc_handle func,
1198                                gpointer user_data)
1199 {
1200         dissector_table_t sub_dissectors = find_dissector_table( name);
1201         GSList *tmp;
1202
1203         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1204             tmp = g_slist_next(tmp))
1205                 func(name, tmp->data, user_data);
1206 }
1207
1208 /*
1209  * Called for each entry in a dissector table.
1210  */
1211 static void
1212 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1213 {
1214         dtbl_entry_t *dtbl_entry;
1215         dissector_foreach_info_t *info;
1216
1217         g_assert(value);
1218         g_assert(user_data);
1219
1220         dtbl_entry = value;
1221         if (dtbl_entry->initial == dtbl_entry->current) {
1222                 /*
1223                  * Entry hasn't changed - don't call the function.
1224                  */
1225                 return;
1226         }
1227
1228         info = user_data;
1229         info->caller_func(info->table_name, info->selector_type, key, value,
1230             info->caller_data);
1231 }
1232
1233 /*
1234  * Walk all dissector tables calling a user supplied function only on
1235  * any entry that has been changed from its original state.
1236  */
1237 void
1238 dissector_all_tables_foreach_changed (DATFunc func,
1239                                       gpointer user_data)
1240 {
1241         dissector_foreach_info_t info;
1242
1243         info.caller_data = user_data;
1244         info.caller_func = func;
1245         info.next_func = dissector_table_foreach_changed_func;
1246         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1247 }
1248
1249 /*
1250  * Walk one dissector table calling a user supplied function only on
1251  * any entry that has been changed from its original state.
1252  */
1253 void
1254 dissector_table_foreach_changed (char *name,
1255                                  DATFunc func,
1256                                  gpointer user_data)
1257 {
1258         dissector_foreach_info_t info;
1259         dissector_table_t sub_dissectors = find_dissector_table( name);
1260
1261         info.table_name = name;
1262         info.selector_type = sub_dissectors->type;
1263         info.caller_func = func;
1264         info.caller_data = user_data;
1265         g_hash_table_foreach(sub_dissectors->hash_table,
1266             dissector_table_foreach_changed_func, &info);
1267 }
1268
1269 typedef struct dissector_foreach_table_info {
1270   gpointer      caller_data;
1271   DATFunc_table caller_func;
1272 } dissector_foreach_table_info_t;
1273
1274 /*
1275  * Called for each entry in the table of all dissector tables.
1276  */
1277 static void
1278 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
1279 {
1280         dissector_table_t table;
1281         dissector_foreach_table_info_t *info;
1282
1283         table = value;
1284         info = user_data;
1285         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
1286 }
1287
1288 /*
1289  * Walk all dissector tables calling a user supplied function on each
1290  * table.
1291  */
1292 void
1293 dissector_all_tables_foreach_table (DATFunc_table func,
1294                                     gpointer user_data)
1295 {
1296         dissector_foreach_table_info_t info;
1297
1298         info.caller_data = user_data;
1299         info.caller_func = func;
1300         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
1301 }
1302
1303 dissector_table_t
1304 register_dissector_table(const char *name, char *ui_name, ftenum_t type,
1305     int base)
1306 {
1307         dissector_table_t       sub_dissectors;
1308
1309         /* Create our hash-of-hashes if it doesn't already exist */
1310         if (!dissector_tables) {
1311                 dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
1312                 g_assert(dissector_tables);
1313         }
1314
1315         /* Make sure the registration is unique */
1316         g_assert(!g_hash_table_lookup( dissector_tables, name ));
1317
1318         /* Create and register the dissector table for this name; returns */
1319         /* a pointer to the dissector table. */
1320         sub_dissectors = g_malloc(sizeof (struct dissector_table));
1321         switch (type) {
1322
1323         case FT_UINT8:
1324         case FT_UINT16:
1325         case FT_UINT24:
1326         case FT_UINT32:
1327                 /*
1328                  * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
1329                  * so we use "g_direct_hash()" and "g_direct_equal()".
1330                  */
1331                 sub_dissectors->hash_table = g_hash_table_new( g_direct_hash,
1332                     g_direct_equal );
1333                 break;
1334
1335         case FT_STRING:
1336         case FT_STRINGZ:
1337                 sub_dissectors->hash_table = g_hash_table_new( g_str_hash,
1338                     g_str_equal );
1339                 break;
1340
1341         default:
1342                 g_assert_not_reached();
1343         }
1344         sub_dissectors->dissector_handles = NULL;
1345         sub_dissectors->ui_name = ui_name;
1346         sub_dissectors->type = type;
1347         sub_dissectors->base = base;
1348         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
1349         return sub_dissectors;
1350 }
1351
1352 char *
1353 get_dissector_table_ui_name(const char *name)
1354 {
1355         dissector_table_t sub_dissectors = find_dissector_table( name);
1356
1357         return sub_dissectors->ui_name;
1358 }
1359
1360 ftenum_t
1361 get_dissector_table_selector_type(const char *name)
1362 {
1363         dissector_table_t sub_dissectors = find_dissector_table( name);
1364
1365         return sub_dissectors->type;
1366 }
1367
1368 int
1369 get_dissector_table_base(const char *name)
1370 {
1371         dissector_table_t sub_dissectors = find_dissector_table( name);
1372
1373         return sub_dissectors->base;
1374 }
1375
1376 static GHashTable *heur_dissector_lists = NULL;
1377
1378 typedef struct {
1379         heur_dissector_t dissector;
1380         protocol_t *protocol;
1381 } heur_dtbl_entry_t;
1382
1383 /* Finds a heuristic dissector table by field name. */
1384 static heur_dissector_list_t *
1385 find_heur_dissector_list(const char *name)
1386 {
1387         g_assert(heur_dissector_lists != NULL);
1388         return g_hash_table_lookup(heur_dissector_lists, name);
1389 }
1390
1391 void
1392 heur_dissector_add(const char *name, heur_dissector_t dissector, int proto)
1393 {
1394         heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
1395         heur_dtbl_entry_t *dtbl_entry;
1396
1397         /* sanity check */
1398         g_assert(sub_dissectors != NULL);
1399
1400         dtbl_entry = g_malloc(sizeof (heur_dtbl_entry_t));
1401         dtbl_entry->dissector = dissector;
1402         dtbl_entry->protocol = find_protocol_by_id(proto);
1403
1404         /* do the table insertion */
1405         *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dtbl_entry);
1406 }
1407
1408 gboolean
1409 dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
1410     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1411 {
1412         gboolean status;
1413         const char *saved_proto;
1414         GSList *entry;
1415         heur_dtbl_entry_t *dtbl_entry;
1416         guint16 saved_can_desegment;
1417
1418         /* can_desegment is set to 2 by anyone which offers this api/service.
1419            then everytime a subdissector is called it is decremented by one.
1420            thus only the subdissector immediately ontop of whoever offers this
1421            service can use it.
1422            We save the current value of "can_desegment" for the
1423            benefit of TCP proxying dissectors such as SOCKS, so they
1424            can restore it and allow the dissectors they call to use
1425            the desegmentation service.
1426         */
1427         saved_can_desegment=pinfo->can_desegment;
1428         pinfo->saved_can_desegment = saved_can_desegment;
1429         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1430
1431         status = FALSE;
1432         saved_proto = pinfo->current_proto;
1433         for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
1434                 /* XXX - why set this now and above? */
1435                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
1436                 dtbl_entry = (heur_dtbl_entry_t *)entry->data;
1437                 if (dtbl_entry->protocol != NULL &&
1438                     !proto_is_protocol_enabled(dtbl_entry->protocol)) {
1439                         /*
1440                          * No - don't try this dissector.
1441                          */
1442                         continue;
1443                 }
1444
1445                 if (dtbl_entry->protocol != NULL) {
1446                         pinfo->current_proto =
1447                             proto_get_protocol_short_name(dtbl_entry->protocol);
1448                 }
1449                 if ((*dtbl_entry->dissector)(tvb, pinfo, tree)) {
1450                         status = TRUE;
1451                         break;
1452                 }
1453         }
1454         pinfo->current_proto = saved_proto;
1455         pinfo->can_desegment=saved_can_desegment;
1456         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
1457         return status;
1458 }
1459
1460 void
1461 register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
1462 {
1463         /* Create our hash-of-lists if it doesn't already exist */
1464         if (heur_dissector_lists == NULL) {
1465                 heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
1466                 g_assert(heur_dissector_lists != NULL);
1467         }
1468
1469         /* Make sure the registration is unique */
1470         g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
1471
1472         *sub_dissectors = NULL; /* initially empty */
1473         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
1474             (gpointer) sub_dissectors);
1475 }
1476
1477 /*
1478  * Register dissectors by name; used if one dissector always calls a
1479  * particular dissector, or if it bases the decision of which dissector
1480  * to call on something other than a numerical value or on "try a bunch
1481  * of dissectors until one likes the packet".
1482  */
1483
1484 /*
1485  * List of registered dissectors.
1486  */
1487 static GHashTable *registered_dissectors = NULL;
1488
1489 /* Get the short name of the protocol for a dissector handle. */
1490 char *
1491 dissector_handle_get_short_name(dissector_handle_t handle)
1492 {
1493         return proto_get_protocol_short_name(handle->protocol);
1494 }
1495
1496 /* Get the index of the protocol for a dissector handle. */
1497 int
1498 dissector_handle_get_protocol_index(dissector_handle_t handle)
1499 {
1500         return proto_get_id(handle->protocol);
1501 }
1502
1503 /* Find a registered dissector by name. */
1504 dissector_handle_t
1505 find_dissector(const char *name)
1506 {
1507         g_assert(registered_dissectors != NULL);
1508         return g_hash_table_lookup(registered_dissectors, name);
1509 }
1510
1511 /* Create an anonymous handle for a dissector. */
1512 dissector_handle_t
1513 create_dissector_handle(dissector_t dissector, int proto)
1514 {
1515         struct dissector_handle *handle;
1516
1517         handle = g_malloc(sizeof (struct dissector_handle));
1518         handle->name = NULL;
1519         handle->is_new = FALSE;
1520         handle->dissector.old = dissector;
1521         handle->protocol = find_protocol_by_id(proto);
1522
1523         return handle;
1524 }
1525
1526 dissector_handle_t
1527 new_create_dissector_handle(new_dissector_t dissector, int proto)
1528 {
1529         struct dissector_handle *handle;
1530
1531         handle = g_malloc(sizeof (struct dissector_handle));
1532         handle->name = NULL;
1533         handle->is_new = TRUE;
1534         handle->dissector.new = dissector;
1535         handle->protocol = find_protocol_by_id(proto);
1536
1537         return handle;
1538 }
1539
1540 /* Register a dissector by name. */
1541 void
1542 register_dissector(const char *name, dissector_t dissector, int proto)
1543 {
1544         struct dissector_handle *handle;
1545
1546         /* Create our hash table if it doesn't already exist */
1547         if (registered_dissectors == NULL) {
1548                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1549                 g_assert(registered_dissectors != NULL);
1550         }
1551
1552         /* Make sure the registration is unique */
1553         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1554
1555         handle = g_malloc(sizeof (struct dissector_handle));
1556         handle->name = name;
1557         handle->is_new = FALSE;
1558         handle->dissector.old = dissector;
1559         handle->protocol = find_protocol_by_id(proto);
1560
1561         g_hash_table_insert(registered_dissectors, (gpointer)name,
1562             (gpointer) handle);
1563 }
1564
1565 void
1566 new_register_dissector(const char *name, new_dissector_t dissector, int proto)
1567 {
1568         struct dissector_handle *handle;
1569
1570         /* Create our hash table if it doesn't already exist */
1571         if (registered_dissectors == NULL) {
1572                 registered_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
1573                 g_assert(registered_dissectors != NULL);
1574         }
1575
1576         /* Make sure the registration is unique */
1577         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
1578
1579         handle = g_malloc(sizeof (struct dissector_handle));
1580         handle->name = name;
1581         handle->is_new = TRUE;
1582         handle->dissector.new = dissector;
1583         handle->protocol = find_protocol_by_id(proto);
1584
1585         g_hash_table_insert(registered_dissectors, (gpointer)name,
1586             (gpointer) handle);
1587 }
1588
1589 /* Call a dissector through a handle. */
1590 int
1591 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
1592     packet_info *pinfo, proto_tree *tree)
1593 {
1594         int ret;
1595
1596         ret = call_dissector_work(handle, tvb, pinfo, tree);
1597         if (ret == 0) {
1598                 /*
1599                  * The protocol was disabled, or the dissector rejected
1600                  * it.  Just dissect this packet as data.
1601                  */
1602                 g_assert(data_handle != NULL);
1603                 g_assert(data_handle->protocol != NULL);
1604                 call_dissector(data_handle, tvb, pinfo, tree);
1605                 return tvb_length(tvb);
1606         }
1607         return ret;
1608 }