95d6665e168f160cf05be0f505a25cc75b9b2431
[jlayton/wireshark.git] / epan / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <stdarg.h>
31 #include <string.h>
32 #include <time.h>
33
34 #include "packet.h"
35 #include "timestamp.h"
36
37 #include "osi-utils.h"
38 #include "to_str.h"
39
40 #include "addr_resolv.h"
41 #include "tvbuff.h"
42 #include "epan_dissect.h"
43
44 #include "wmem/wmem.h"
45
46 #include <epan/exceptions.h>
47 #include <epan/reassemble.h>
48 #include <epan/stream.h>
49 #include <epan/expert.h>
50 #include <epan/range.h>
51 #include <epan/asm_utils.h>
52
53 #include <wsutil/str_util.h>
54
55 static gint proto_malformed = -1;
56 static dissector_handle_t frame_handle = NULL;
57 static dissector_handle_t file_handle = NULL;
58 static dissector_handle_t data_handle = NULL;
59
60 /**
61  * A data source.
62  * Has a tvbuff and a name.
63  */
64 struct data_source {
65         tvbuff_t *tvb;
66         char *name;
67 };
68
69 /*
70  * A dissector table.
71  *
72  * "hash_table" is a hash table, indexed by port number, supplying
73  * a "struct dtbl_entry"; it records what dissector is assigned to
74  * that uint or string value in that table.
75  *
76  * "dissector_handles" is a list of all dissectors that *could* be
77  * used in that table; not all of them are necessarily in the table,
78  * as they may be for protocols that don't have a fixed uint value,
79  * e.g. for TCP or UDP port number tables and protocols with no fixed
80  * port number.
81  *
82  * "ui_name" is the name the dissector table has in the user interface.
83  *
84  * "type" is a field type giving the width of the uint value for that
85  * dissector table, if it's a uint dissector table.
86  *
87  * "param" is the base in which to display the uint value for that
88  * dissector table, if it's a uint dissector table, or if it's a string
89  * table, TRUE/FALSE to indicate case-insensitive or not.
90  *
91  * "protocol" is the protocol associated with the dissector table. Used
92  * for determining dependencies.
93  */
94 struct dissector_table {
95         GHashTable      *hash_table;
96         GSList          *dissector_handles;
97         const char      *ui_name;
98         ftenum_t        type;
99         int             param;
100         protocol_t      *protocol;
101         GHashFunc hash_func;
102         dissector_table_allow_e allow_dup_proto; /* XXX - Could be converted to a flag-like field */
103 };
104
105 static GHashTable *dissector_tables = NULL;
106
107 /*
108  * List of registered dissectors.
109  */
110 static GHashTable *registered_dissectors = NULL;
111
112 /*
113  * A dissector dependency list.
114  */
115 struct depend_dissector_list {
116         GSList          *dissectors;
117 };
118
119 /* Maps char *dissector_name to depend_dissector_list_t */
120 static GHashTable *depend_dissector_lists = NULL;
121
122 static void
123 destroy_depend_dissector_list(void *data)
124 {
125         depend_dissector_list_t dissector_list = (depend_dissector_list_t)data;
126         GSList **list = &(dissector_list->dissectors);
127
128         g_slist_foreach(*list, (GFunc)g_free, NULL);
129         g_slist_free(*list);
130         g_slice_free(struct depend_dissector_list, dissector_list);
131 }
132
133 /*
134  * A heuristics dissector list.
135  */
136 struct heur_dissector_list {
137         protocol_t      *protocol;
138         GSList          *dissectors;
139 };
140
141 static GHashTable *heur_dissector_lists = NULL;
142
143 /* Name hashtables for fast detection of duplicate names */
144 static GHashTable* heuristic_short_names  = NULL;
145
146 static void
147 destroy_heuristic_dissector_entry(gpointer data, gpointer user_data _U_)
148 {
149         g_free(((heur_dtbl_entry_t*)data)->list_name);
150         g_slice_free(heur_dtbl_entry_t, data);
151 }
152
153 static void
154 destroy_heuristic_dissector_list(void *data)
155 {
156         heur_dissector_list_t dissector_list = (heur_dissector_list_t)data;
157         GSList **list = &(dissector_list->dissectors);
158
159         g_slist_foreach(*list, destroy_heuristic_dissector_entry, NULL);
160         g_slist_free(*list);
161         g_slice_free(struct heur_dissector_list, dissector_list);
162 }
163
164 static void
165 destroy_dissector_table(void *data)
166 {
167         struct dissector_table *table = (struct dissector_table *)data;
168
169         g_hash_table_destroy(table->hash_table);
170         g_slist_free(table->dissector_handles);
171         g_slice_free(struct dissector_table, data);
172 }
173
174 void
175 packet_init(void)
176 {
177         dissector_tables = g_hash_table_new_full(g_str_hash, g_str_equal,
178                         NULL, destroy_dissector_table);
179
180         registered_dissectors = g_hash_table_new_full(g_str_hash, g_str_equal,
181                         NULL, NULL);
182
183         depend_dissector_lists = g_hash_table_new_full(g_str_hash, g_str_equal,
184                         g_free, destroy_depend_dissector_list);
185
186         heur_dissector_lists = g_hash_table_new_full(g_str_hash, g_str_equal,
187                         NULL, destroy_heuristic_dissector_list);
188
189         heuristic_short_names  = g_hash_table_new(wrs_str_hash, g_str_equal);
190 }
191
192 void
193 packet_cache_proto_handles(void)
194 {
195         frame_handle = find_dissector("frame");
196         g_assert(frame_handle != NULL);
197
198         file_handle = find_dissector("file");
199         g_assert(file_handle != NULL);
200
201         data_handle = find_dissector("data");
202         g_assert(data_handle != NULL);
203
204         proto_malformed = proto_get_id_by_filter_name("_ws.malformed");
205         g_assert(proto_malformed != -1);
206 }
207
208 void
209 packet_cleanup(void)
210 {
211         g_hash_table_destroy(dissector_tables);
212         g_hash_table_destroy(registered_dissectors);
213         g_hash_table_destroy(depend_dissector_lists);
214         g_hash_table_destroy(heur_dissector_lists);
215         g_hash_table_destroy(heuristic_short_names);
216 }
217
218 /*
219  * Given a tvbuff, and a length from a packet header, adjust the length
220  * of the tvbuff to reflect the specified length.
221  */
222 void
223 set_actual_length(tvbuff_t *tvb, const guint specified_len)
224 {
225         if (specified_len < tvb_reported_length(tvb)) {
226                 /* Adjust the length of this tvbuff to include only the specified
227                    payload length.
228
229                    The dissector above the one calling us (the dissector above is
230                    probably us) may use that to determine how much of its packet
231                    was padding. */
232                 tvb_set_reported_length(tvb, specified_len);
233         }
234 }
235
236 /* List of routines that are called before we make a pass through a capture file
237  * and dissect all its packets. See register_init_routine and
238  * register_cleanup_routine in packet.h */
239 static GSList *init_routines;
240 static GSList *cleanup_routines;
241
242 void
243 register_init_routine(void (*func)(void))
244 {
245         init_routines = g_slist_prepend(init_routines, (gpointer)func);
246 }
247
248 void
249 register_cleanup_routine(void (*func)(void))
250 {
251         cleanup_routines = g_slist_prepend(cleanup_routines, (gpointer)func);
252 }
253
254 typedef void (*void_func_t)(void);
255
256 /* Initialize all data structures used for dissection. */
257 static void
258 call_routine(gpointer routine, gpointer dummy _U_)
259 {
260         void_func_t func = (void_func_t)routine;
261         (*func)();
262 }
263
264 void
265 init_dissection(void)
266 {
267         wmem_enter_file_scope();
268
269         /*
270          * Reinitialize resolution information. We do initialization here in
271          * case we need to resolve between captures.
272          */
273         host_name_lookup_init();
274
275         /* Initialize the table of conversations. */
276         epan_conversation_init();
277
278         /* Initialize the table of circuits. */
279         epan_circuit_init();
280
281         /* Initialize protocol-specific variables. */
282         g_slist_foreach(init_routines, &call_routine, NULL);
283
284         /* Initialize the stream-handling tables */
285         stream_init();
286
287         /* Initialize the expert infos */
288         expert_packet_init();
289 }
290
291 void
292 cleanup_dissection(void)
293 {
294         /* Cleanup the table of conversations. Do this before freeing seasonal
295          * memory (at least until conversation's use of g_slist is changed).
296          */
297         epan_conversation_cleanup();
298
299         /* Cleanup the table of circuits. */
300         epan_circuit_cleanup();
301
302         /* Cleanup protocol-specific variables. */
303         g_slist_foreach(cleanup_routines, &call_routine, NULL);
304
305         /* Cleanup the stream-handling tables */
306         stream_cleanup();
307
308         /* Cleanup the expert infos */
309         expert_packet_cleanup();
310
311         wmem_leave_file_scope();
312
313         /*
314          * Reinitialize resolution information. We do initialization here in
315          * case we need to resolve between captures.
316          */
317         host_name_lookup_cleanup();
318 }
319
320 /* Allow protocols to register a "cleanup" routine to be
321  * run after the initial sequential run through the packets.
322  * Note that the file can still be open after this; this is not
323  * the final cleanup. */
324 static GSList *postseq_cleanup_routines;
325
326 void
327 register_postseq_cleanup_routine(void_func_t func)
328 {
329         postseq_cleanup_routines = g_slist_prepend(postseq_cleanup_routines,
330                         (gpointer)func);
331 }
332
333 /* Call all the registered "postseq_cleanup" routines. */
334 static void
335 call_postseq_cleanup_routine(gpointer routine, gpointer dummy _U_)
336 {
337         void_func_t func = (void_func_t)routine;
338         (*func)();
339 }
340
341 void
342 postseq_cleanup_all_protocols(void)
343 {
344         g_slist_foreach(postseq_cleanup_routines,
345                         &call_postseq_cleanup_routine, NULL);
346 }
347
348 /*
349  * Add a new data source to the list of data sources for a frame, given
350  * the tvbuff for the data source and its name.
351  */
352 void
353 add_new_data_source(packet_info *pinfo, tvbuff_t *tvb, const char *name)
354 {
355         struct data_source *src;
356
357         src = g_slice_new(struct data_source);
358         src->tvb = tvb;
359         src->name = g_strdup(name);
360         /* This could end up slow, but we should never have that many data
361          * sources so it probably doesn't matter */
362         pinfo->data_src = g_slist_append(pinfo->data_src, src);
363 }
364
365 void
366 remove_last_data_source(packet_info *pinfo)
367 {
368         struct data_source *src;
369         GSList *last;
370
371         last = g_slist_last(pinfo->data_src);
372         src = (struct data_source *)last->data;
373         pinfo->data_src = g_slist_delete_link(pinfo->data_src, last);
374         g_free(src->name);
375         g_slice_free(struct data_source, src);
376 }
377
378 char*
379 get_data_source_name(const struct data_source *src)
380 {
381         guint length = tvb_captured_length(src->tvb);
382
383         return wmem_strdup_printf(NULL, "%s (%u byte%s)", src->name, length,
384                                 plurality(length, "", "s"));
385 }
386
387 tvbuff_t *
388 get_data_source_tvb(const struct data_source *src)
389 {
390         return src->tvb;
391 }
392
393 /*
394  * Free up a frame's list of data sources.
395  */
396 void
397 free_data_sources(packet_info *pinfo)
398 {
399         if (pinfo->data_src) {
400                 GSList *l;
401
402                 for (l = pinfo->data_src; l; l = l->next) {
403                         struct data_source *src = (struct data_source *)l->data;
404
405                         g_free(src->name);
406                         g_slice_free(struct data_source, src);
407                 }
408                 g_slist_free(pinfo->data_src);
409                 pinfo->data_src = NULL;
410         }
411 }
412
413 void
414 mark_frame_as_depended_upon(packet_info *pinfo, guint32 frame_num)
415 {
416         /* Don't mark a frame as dependent on itself */
417         if (frame_num != pinfo->num) {
418                 pinfo->dependent_frames = g_slist_prepend(pinfo->dependent_frames, GUINT_TO_POINTER(frame_num));
419         }
420 }
421
422 /* Allow dissectors to register a "final_registration" routine
423  * that is run like the proto_register_XXX() routine, but at the
424  * end of the epan_init() function; that is, *after* all other
425  * subsystems, like dfilters, have finished initializing. This is
426  * useful for dissector registration routines which need to compile
427  * display filters. dfilters can't initialize itself until all protocols
428  * have registered themselves. */
429 static GSList *final_registration_routines;
430
431 void
432 register_final_registration_routine(void (*func)(void))
433 {
434         final_registration_routines = g_slist_prepend(final_registration_routines,
435                         (gpointer)func);
436 }
437
438 /* Call all the registered "final_registration" routines. */
439 static void
440 call_final_registration_routine(gpointer routine, gpointer dummy _U_)
441 {
442         void_func_t func = (void_func_t)routine;
443
444         (*func)();
445 }
446
447 void
448 final_registration_all_protocols(void)
449 {
450         g_slist_foreach(final_registration_routines,
451                         &call_final_registration_routine, NULL);
452 }
453
454
455 /* Creates the top-most tvbuff and calls dissect_frame() */
456 void
457 dissect_record(epan_dissect_t *edt, int file_type_subtype,
458     struct wtap_pkthdr *phdr, tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
459 {
460         const char *volatile record_type;
461         frame_data_t frame_dissector_data;
462
463         switch (phdr->rec_type) {
464
465         case REC_TYPE_PACKET:
466                 record_type = "Frame";
467                 break;
468
469         case REC_TYPE_FT_SPECIFIC_EVENT:
470                 record_type = "Event";
471                 break;
472
473         case REC_TYPE_FT_SPECIFIC_REPORT:
474                 record_type = "Report";
475                 break;
476
477         case REC_TYPE_SYSCALL:
478                 record_type = "System Call";
479                 break;
480
481         default:
482                 /*
483                  * XXX - if we add record types that shouldn't be
484                  * dissected and displayed, but that need to at
485                  * least be processed somewhere, we need to somehow
486                  * indicate that to our caller.
487                  */
488                 g_assert_not_reached();
489                 break;
490         }
491
492         if (cinfo != NULL)
493                 col_init(cinfo, edt->session);
494         edt->pi.epan = edt->session;
495         /* edt->pi.pool created in epan_dissect_init() */
496         edt->pi.current_proto = "<Missing Protocol Name>";
497         edt->pi.cinfo = cinfo;
498         edt->pi.presence_flags = 0;
499         edt->pi.num = fd->num;
500         if (fd->flags.has_ts) {
501                 edt->pi.presence_flags |= PINFO_HAS_TS;
502                 edt->pi.abs_ts = fd->abs_ts;
503         }
504         edt->pi.pkt_encap     = phdr->pkt_encap;
505         edt->pi.fd            = fd;
506         edt->pi.phdr          = phdr;
507         edt->pi.pseudo_header = &phdr->pseudo_header;
508         clear_address(&edt->pi.dl_src);
509         clear_address(&edt->pi.dl_dst);
510         clear_address(&edt->pi.net_src);
511         clear_address(&edt->pi.net_dst);
512         clear_address(&edt->pi.src);
513         clear_address(&edt->pi.dst);
514         edt->pi.ctype = CT_NONE;
515         edt->pi.noreassembly_reason = "";
516         edt->pi.ptype = PT_NONE;
517         edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
518         edt->pi.link_dir = LINK_DIR_UNKNOWN;
519         edt->pi.layers = wmem_list_new(edt->pi.pool);
520         edt->tvb = tvb;
521
522
523         frame_delta_abs_time(edt->session, fd, fd->frame_ref_num, &edt->pi.rel_ts);
524
525         /* pkt comment use first user, later from phdr */
526         if (fd->flags.has_user_comment)
527                 frame_dissector_data.pkt_comment = epan_get_user_comment(edt->session, fd);
528         else if (fd->flags.has_phdr_comment)
529                 frame_dissector_data.pkt_comment = phdr->opt_comment;
530         else
531                 frame_dissector_data.pkt_comment = NULL;
532         frame_dissector_data.file_type_subtype = file_type_subtype;
533         frame_dissector_data.color_edt = edt; /* Used strictly for "coloring rules" */
534
535         TRY {
536                 /* Add this tvbuffer into the data_src list */
537                 add_new_data_source(&edt->pi, edt->tvb, record_type);
538
539                 /* Even though dissect_frame() catches all the exceptions a
540                  * sub-dissector can throw, dissect_frame() itself may throw
541                  * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
542                  * in this function. */
543                 call_dissector_with_data(frame_handle, edt->tvb, &edt->pi, edt->tree, &frame_dissector_data);
544         }
545         CATCH(BoundsError) {
546                 g_assert_not_reached();
547         }
548         CATCH2(FragmentBoundsError, ReportedBoundsError) {
549                 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
550                                                "[Malformed %s: Packet Length]",
551                                                record_type);
552         }
553         ENDTRY;
554
555         fd->flags.visited = 1;
556 }
557
558 /* Creates the top-most tvbuff and calls dissect_file() */
559 void
560 dissect_file(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
561                tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
562 {
563         file_data_t file_dissector_data;
564
565         if (cinfo != NULL)
566                 col_init(cinfo, edt->session);
567         edt->pi.epan = edt->session;
568         /* edt->pi.pool created in epan_dissect_init() */
569         edt->pi.current_proto = "<Missing Filetype Name>";
570         edt->pi.cinfo = cinfo;
571         edt->pi.fd    = fd;
572         edt->pi.phdr  = phdr;
573         edt->pi.pseudo_header = &phdr->pseudo_header;
574         clear_address(&edt->pi.dl_src);
575         clear_address(&edt->pi.dl_dst);
576         clear_address(&edt->pi.net_src);
577         clear_address(&edt->pi.net_dst);
578         clear_address(&edt->pi.src);
579         clear_address(&edt->pi.dst);
580         edt->pi.ctype = CT_NONE;
581         edt->pi.noreassembly_reason = "";
582         edt->pi.ptype = PT_NONE;
583         edt->pi.p2p_dir = P2P_DIR_UNKNOWN;
584         edt->pi.link_dir = LINK_DIR_UNKNOWN;
585         edt->pi.layers = wmem_list_new(edt->pi.pool);
586         edt->tvb = tvb;
587
588
589         frame_delta_abs_time(edt->session, fd, fd->frame_ref_num, &edt->pi.rel_ts);
590
591
592         TRY {
593                 /* pkt comment use first user, later from phdr */
594                 if (fd->flags.has_user_comment)
595                         file_dissector_data.pkt_comment = epan_get_user_comment(edt->session, fd);
596                 else if (fd->flags.has_phdr_comment)
597                         file_dissector_data.pkt_comment = phdr->opt_comment;
598                 else
599                         file_dissector_data.pkt_comment = NULL;
600                 file_dissector_data.color_edt = edt; /* Used strictly for "coloring rules" */
601
602
603                 /* Add this tvbuffer into the data_src list */
604                 add_new_data_source(&edt->pi, edt->tvb, "File");
605
606                 /* Even though dissect_file() catches all the exceptions a
607                  * sub-dissector can throw, dissect_frame() itself may throw
608                  * a ReportedBoundsError in bizarre cases. Thus, we catch the exception
609                  * in this function. */
610                 call_dissector_with_data(file_handle, edt->tvb, &edt->pi, edt->tree, &file_dissector_data);
611
612         }
613         CATCH(BoundsError) {
614                 g_assert_not_reached();
615         }
616         CATCH2(FragmentBoundsError, ReportedBoundsError) {
617                 proto_tree_add_protocol_format(edt->tree, proto_malformed, edt->tvb, 0, 0,
618                                                "[Malformed Record: Packet Length]" );
619         }
620         ENDTRY;
621
622         fd->flags.visited = 1;
623 }
624
625 /*********************** code added for sub-dissector lookup *********************/
626
627 /*
628  * A dissector handle.
629  */
630 struct dissector_handle {
631         const char      *name;          /* dissector name */
632         dissector_t     dissector;
633         protocol_t      *protocol;
634 };
635
636 /* This function will return
637  * old style dissector :
638  *   length of the payload or 1 of the payload is empty
639  * new dissector :
640  *   >0  this protocol was successfully dissected and this was this protocol.
641  *   0   this packet did not match this protocol.
642  *
643  * The only time this function will return 0 is if it is a new style dissector
644  * and if the dissector rejected the packet.
645  */
646 static int
647 call_dissector_through_handle(dissector_handle_t handle, tvbuff_t *tvb,
648                               packet_info *pinfo, proto_tree *tree, void *data)
649 {
650         const char *saved_proto;
651         int         len;
652
653         saved_proto = pinfo->current_proto;
654
655         if (handle->protocol != NULL) {
656                 pinfo->current_proto =
657                         proto_get_protocol_short_name(handle->protocol);
658         }
659
660         len = (*handle->dissector)(tvb, pinfo, tree, data);
661         pinfo->current_proto = saved_proto;
662
663         return len;
664 }
665
666 /*
667  * Call a dissector through a handle.
668  * If the protocol for that handle isn't enabled, return 0 without
669  * calling the dissector.
670  * Otherwise, if the handle refers to a new-style dissector, call the
671  * dissector and return its return value, otherwise call it and return
672  * the length of the tvbuff pointed to by the argument.
673  */
674
675 static int
676 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
677                           packet_info *pinfo_arg, proto_tree *tree, void *);
678
679 static int
680 call_dissector_work(dissector_handle_t handle, tvbuff_t *tvb, packet_info *pinfo_arg,
681                     proto_tree *tree, gboolean add_proto_name, void *data)
682 {
683         packet_info *pinfo = pinfo_arg;
684         const char  *saved_proto;
685         guint16      saved_can_desegment;
686         int          len;
687         guint        saved_layers_len = 0;
688
689         if (handle->protocol != NULL &&
690             !proto_is_protocol_enabled(handle->protocol)) {
691                 /*
692                  * The protocol isn't enabled.
693                  */
694                 return 0;
695         }
696
697         saved_proto = pinfo->current_proto;
698         saved_can_desegment = pinfo->can_desegment;
699         saved_layers_len = wmem_list_count(pinfo->layers);
700
701         /*
702          * can_desegment is set to 2 by anyone which offers the
703          * desegmentation api/service.
704          * Then everytime a subdissector is called it is decremented
705          * by one.
706          * Thus only the subdissector immediately on top of whoever
707          * offers this service can use it.
708          * We save the current value of "can_desegment" for the
709          * benefit of TCP proxying dissectors such as SOCKS, so they
710          * can restore it and allow the dissectors they call to use
711          * the desegmentation service.
712          */
713         pinfo->saved_can_desegment = saved_can_desegment;
714         pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
715         if (handle->protocol != NULL) {
716                 pinfo->current_proto =
717                         proto_get_protocol_short_name(handle->protocol);
718
719                 /*
720                  * Add the protocol name to the layers
721                  * if not told not to. Asn2wrs generated dissectors may be added multiple times otherwise.
722                  */
723                 if (add_proto_name) {
724                         pinfo->curr_layer_num++;
725                         wmem_list_append(pinfo->layers, GINT_TO_POINTER(proto_get_id(handle->protocol)));
726                 }
727         }
728
729         if (pinfo->flags.in_error_pkt) {
730                 len = call_dissector_work_error(handle, tvb, pinfo, tree, data);
731         } else {
732                 /*
733                  * Just call the subdissector.
734                  */
735                 len = call_dissector_through_handle(handle, tvb, pinfo, tree, data);
736         }
737         if (len == 0) {
738                 /*
739                  * That dissector didn't accept the packet, so
740                  * remove its protocol's name from the list
741                  * of protocols.
742                  */
743                 while (wmem_list_count(pinfo->layers) > saved_layers_len) {
744                         wmem_list_remove_frame(pinfo->layers, wmem_list_tail(pinfo->layers));
745                 }
746         }
747         pinfo->current_proto = saved_proto;
748         pinfo->can_desegment = saved_can_desegment;
749         return len;
750 }
751
752
753 static int
754 call_dissector_work_error(dissector_handle_t handle, tvbuff_t *tvb,
755                           packet_info *pinfo_arg, proto_tree *tree, void *data)
756 {
757         packet_info  *pinfo = pinfo_arg;
758         const char   *saved_proto;
759         guint16       saved_can_desegment;
760         volatile int  len = 0;
761         gboolean      save_writable;
762         address       save_dl_src;
763         address       save_dl_dst;
764         address       save_net_src;
765         address       save_net_dst;
766         address       save_src;
767         address       save_dst;
768
769         /*
770         * This isn't a packet being transported inside
771         * the protocol whose dissector is calling us,
772         * it's a copy of a packet that caused an error
773         * in some protocol included in a packet that
774         * reports the error (e.g., an ICMP Unreachable
775         * packet).
776         */
777
778         /*
779         * Save the current state of the writability of
780         * the columns, and restore them after the
781         * dissector returns, so that the columns
782         * don't reflect the packet that got the error,
783         * they reflect the packet that reported the
784         * error.
785         */
786         saved_proto = pinfo->current_proto;
787         saved_can_desegment = pinfo->can_desegment;
788
789         save_writable = col_get_writable(pinfo->cinfo, -1);
790         col_set_writable(pinfo->cinfo, -1, FALSE);
791         copy_address_shallow(&save_dl_src, &pinfo->dl_src);
792         copy_address_shallow(&save_dl_dst, &pinfo->dl_dst);
793         copy_address_shallow(&save_net_src, &pinfo->net_src);
794         copy_address_shallow(&save_net_dst, &pinfo->net_dst);
795         copy_address_shallow(&save_src, &pinfo->src);
796         copy_address_shallow(&save_dst, &pinfo->dst);
797
798         /* Dissect the contained packet. */
799         TRY {
800                 len = call_dissector_through_handle(handle, tvb,pinfo, tree, data);
801         }
802         CATCH(BoundsError) {
803                 /*
804                 * Restore the column writability and addresses.
805                 */
806                 col_set_writable(pinfo->cinfo, -1, save_writable);
807                 copy_address_shallow(&pinfo->dl_src, &save_dl_src);
808                 copy_address_shallow(&pinfo->dl_dst, &save_dl_dst);
809                 copy_address_shallow(&pinfo->net_src, &save_net_src);
810                 copy_address_shallow(&pinfo->net_dst, &save_net_dst);
811                 copy_address_shallow(&pinfo->src, &save_src);
812                 copy_address_shallow(&pinfo->dst, &save_dst);
813
814                 /*
815                 * Restore the current protocol, so any
816                 * "Short Frame" indication reflects that
817                 * protocol, not the protocol for the
818                 * packet that got the error.
819                 */
820                 pinfo->current_proto = saved_proto;
821
822                 /*
823                 * Restore the desegmentability state.
824                 */
825                 pinfo->can_desegment = saved_can_desegment;
826
827                 /*
828                 * Rethrow the exception, so this will be
829                 * reported as a short frame.
830                 */
831                 RETHROW;
832         }
833         CATCH2(FragmentBoundsError, ReportedBoundsError) {
834                 /*
835                 * "ret" wasn't set because an exception was thrown
836                 * before "call_dissector_through_handle()" returned.
837                 * As it called something, at least one dissector
838                 * accepted the packet, and, as an exception was
839                 * thrown, not only was all the tvbuff dissected,
840                 * a dissector tried dissecting past the end of
841                 * the data in some tvbuff, so we'll assume that
842                 * the entire tvbuff was dissected.
843                 */
844                 len = tvb_captured_length(tvb);
845         }
846         ENDTRY;
847
848         col_set_writable(pinfo->cinfo, -1, save_writable);
849         copy_address_shallow(&pinfo->dl_src, &save_dl_src);
850         copy_address_shallow(&pinfo->dl_dst, &save_dl_dst);
851         copy_address_shallow(&pinfo->net_src, &save_net_src);
852         copy_address_shallow(&pinfo->net_dst, &save_net_dst);
853         copy_address_shallow(&pinfo->src, &save_src);
854         copy_address_shallow(&pinfo->dst, &save_dst);
855         pinfo->want_pdu_tracking = 0;
856         return len;
857 }
858
859 /*
860  * An entry in the hash table portion of a dissector table.
861  */
862 struct dtbl_entry {
863         dissector_handle_t initial;
864         dissector_handle_t current;
865 };
866
867 /* Finds a dissector table by table name. */
868 dissector_table_t
869 find_dissector_table(const char *name)
870 {
871         return (dissector_table_t)g_hash_table_lookup( dissector_tables, name );
872 }
873
874 /* Find an entry in a uint dissector table. */
875 static dtbl_entry_t *
876 find_uint_dtbl_entry(dissector_table_t sub_dissectors, const guint32 pattern)
877 {
878         switch (sub_dissectors->type) {
879
880         case FT_UINT8:
881         case FT_UINT16:
882         case FT_UINT24:
883         case FT_UINT32:
884                 /*
885                  * You can do a uint lookup in these tables.
886                  */
887                 break;
888
889         default:
890                 /*
891                  * But you can't do a uint lookup in any other types
892                  * of tables.
893                  */
894                 g_assert_not_reached();
895         }
896
897         /*
898          * Find the entry.
899          */
900         return (dtbl_entry_t *)g_hash_table_lookup(sub_dissectors->hash_table,
901                                    GUINT_TO_POINTER(pattern));
902 }
903
904 #if 0
905 static void
906 dissector_add_uint_sanity_check(const char *name, guint32 pattern, dissector_handle_t handle, dissector_table_t sub_dissectors)
907 {
908         dtbl_entry_t *dtbl_entry;
909
910         if (pattern == 0) {
911                 g_warning("%s: %s registering using a pattern of 0",
912                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)));
913         }
914
915         dtbl_entry = g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
916         if (dtbl_entry != NULL) {
917                 g_warning("%s: %s registering using pattern %d already registered by %s",
918                           name, proto_get_protocol_filter_name(proto_get_id(handle->protocol)),
919                           pattern, proto_get_protocol_filter_name(proto_get_id(dtbl_entry->initial->protocol)));
920         }
921 }
922 #endif
923
924 /* Add an entry to a uint dissector table. */
925 void
926 dissector_add_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
927 {
928         dissector_table_t  sub_dissectors;
929         dtbl_entry_t      *dtbl_entry;
930
931         sub_dissectors = find_dissector_table(name);
932
933         /*
934          * Make sure the handle and the dissector table exist.
935          */
936         if (handle == NULL) {
937                 fprintf(stderr, "OOPS: handle to register \"%s\" to doesn't exist\n",
938                     name);
939                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
940                         abort();
941                 return;
942         }
943         if (sub_dissectors == NULL) {
944                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
945                     name);
946                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
947                     proto_get_protocol_long_name(handle->protocol));
948                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
949                         abort();
950                 return;
951         }
952
953         switch (sub_dissectors->type) {
954
955         case FT_UINT8:
956         case FT_UINT16:
957         case FT_UINT24:
958         case FT_UINT32:
959                 /*
960                  * You can do a uint lookup in these tables.
961                  */
962                 break;
963
964         default:
965                 /*
966                  * But you can't do a uint lookup in any other types
967                  * of tables.
968                  */
969                 g_assert_not_reached();
970         }
971
972 #if 0
973         dissector_add_uint_sanity_check(name, pattern, handle, sub_dissectors);
974 #endif
975
976         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
977         dtbl_entry->current = handle;
978         dtbl_entry->initial = dtbl_entry->current;
979
980         /* do the table insertion */
981         g_hash_table_insert( sub_dissectors->hash_table,
982                              GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
983
984         /*
985          * Now add it to the list of handles that could be used for
986          * "Decode As" with this table, because it *is* being used
987          * with this table.
988          */
989         dissector_add_for_decode_as(name, handle);
990 }
991
992
993
994 void dissector_add_uint_range(const char *abbrev, range_t *range,
995                               dissector_handle_t handle)
996 {
997         guint32 i, j;
998
999         if (range) {
1000                 for (i = 0; i < range->nranges; i++) {
1001                         for (j = range->ranges[i].low; j < range->ranges[i].high; j++)
1002                                 dissector_add_uint(abbrev, j, handle);
1003                         dissector_add_uint(abbrev, range->ranges[i].high, handle);
1004                 }
1005         }
1006 }
1007
1008 /* Delete the entry for a dissector in a uint dissector table
1009    with a particular pattern. */
1010
1011 /* NOTE: this doesn't use the dissector call variable. It is included to */
1012 /*      be consistant with the dissector_add_uint and more importantly to be used */
1013 /*      if the technique of adding a temporary dissector is implemented.  */
1014 /*      If temporary dissectors are deleted, then the original dissector must */
1015 /*      be available. */
1016 void
1017 dissector_delete_uint(const char *name, const guint32 pattern,
1018         dissector_handle_t handle _U_)
1019 {
1020         dissector_table_t sub_dissectors = find_dissector_table( name);
1021         dtbl_entry_t *dtbl_entry;
1022
1023         /* sanity check */
1024         g_assert( sub_dissectors);
1025
1026         /*
1027          * Find the entry.
1028          */
1029         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
1030
1031         if (dtbl_entry != NULL) {
1032                 /*
1033                  * Found - remove it.
1034                  */
1035                 g_hash_table_remove(sub_dissectors->hash_table,
1036                                     GUINT_TO_POINTER(pattern));
1037         }
1038 }
1039
1040 void dissector_delete_uint_range(const char *abbrev, range_t *range,
1041                                  dissector_handle_t handle)
1042 {
1043         guint32 i, j;
1044
1045         if (range) {
1046                 for (i = 0; i < range->nranges; i++) {
1047                         for (j = range->ranges[i].low; j < range->ranges[i].high; j++)
1048                                 dissector_delete_uint(abbrev, j, handle);
1049                         dissector_delete_uint(abbrev, range->ranges[i].high, handle);
1050                 }
1051         }
1052 }
1053
1054 static gboolean
1055 dissector_delete_all_check (gpointer key _U_, gpointer value, gpointer user_data)
1056 {
1057         dtbl_entry_t *dtbl_entry = (dtbl_entry_t *) value;
1058         dissector_handle_t handle = (dissector_handle_t) user_data;
1059
1060         if (!dtbl_entry->current->protocol) {
1061                 /*
1062                  * Not all dissectors are registered with a protocol, so we need this
1063                  * check when running from dissector_delete_from_all_tables.
1064                  */
1065                 return FALSE;
1066         }
1067
1068         return (proto_get_id (dtbl_entry->current->protocol) == proto_get_id (handle->protocol));
1069 }
1070
1071 /* Delete all entries from a dissector table. */
1072 void dissector_delete_all(const char *name, dissector_handle_t handle)
1073 {
1074         dissector_table_t sub_dissectors = find_dissector_table(name);
1075         g_assert (sub_dissectors);
1076
1077         g_hash_table_foreach_remove (sub_dissectors->hash_table, dissector_delete_all_check, handle);
1078 }
1079
1080 static void
1081 dissector_delete_from_table(gpointer key _U_, gpointer value, gpointer user_data)
1082 {
1083         dissector_table_t sub_dissectors = (dissector_table_t) value;
1084         g_assert (sub_dissectors);
1085
1086         g_hash_table_foreach_remove(sub_dissectors->hash_table, dissector_delete_all_check, user_data);
1087         sub_dissectors->dissector_handles = g_slist_remove(sub_dissectors->dissector_handles, user_data);
1088 }
1089
1090 /* Delete handle from all tables and dissector_handles lists */
1091 static void
1092 dissector_delete_from_all_tables(dissector_handle_t handle)
1093 {
1094         g_hash_table_foreach(dissector_tables, dissector_delete_from_table, handle);
1095 }
1096
1097 /* Change the entry for a dissector in a uint dissector table
1098    with a particular pattern to use a new dissector handle. */
1099 void
1100 dissector_change_uint(const char *name, const guint32 pattern, dissector_handle_t handle)
1101 {
1102         dissector_table_t sub_dissectors = find_dissector_table( name);
1103         dtbl_entry_t *dtbl_entry;
1104
1105         /* sanity check */
1106         g_assert( sub_dissectors);
1107
1108         /*
1109          * See if the entry already exists. If so, reuse it.
1110          */
1111         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
1112         if (dtbl_entry != NULL) {
1113                 dtbl_entry->current = handle;
1114                 return;
1115         }
1116
1117         /*
1118          * Don't create an entry if there is no dissector handle - I.E. the
1119          * user said not to decode something that wasn't being decoded
1120          * in the first place.
1121          */
1122         if (handle == NULL)
1123                 return;
1124
1125         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
1126         dtbl_entry->initial = NULL;
1127         dtbl_entry->current = handle;
1128
1129         /* do the table insertion */
1130         g_hash_table_insert( sub_dissectors->hash_table,
1131                              GUINT_TO_POINTER( pattern), (gpointer)dtbl_entry);
1132 }
1133
1134 /* Reset an entry in a uint dissector table to its initial value. */
1135 void
1136 dissector_reset_uint(const char *name, const guint32 pattern)
1137 {
1138         dissector_table_t  sub_dissectors = find_dissector_table( name);
1139         dtbl_entry_t      *dtbl_entry;
1140
1141         /* sanity check */
1142         g_assert( sub_dissectors);
1143
1144         /*
1145          * Find the entry.
1146          */
1147         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, pattern);
1148
1149         if (dtbl_entry == NULL)
1150                 return;
1151
1152         /*
1153          * Found - is there an initial value?
1154          */
1155         if (dtbl_entry->initial != NULL) {
1156                 dtbl_entry->current = dtbl_entry->initial;
1157         } else {
1158                 g_hash_table_remove(sub_dissectors->hash_table,
1159                                     GUINT_TO_POINTER(pattern));
1160         }
1161 }
1162
1163 /* Look for a given value in a given uint dissector table and, if found,
1164    call the dissector with the arguments supplied, and return TRUE,
1165    otherwise return FALSE. */
1166
1167 int
1168 dissector_try_uint_new(dissector_table_t sub_dissectors, const guint32 uint_val,
1169                        tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
1170                        const gboolean add_proto_name, void *data)
1171 {
1172         dtbl_entry_t            *dtbl_entry;
1173         struct dissector_handle *handle;
1174         guint32                  saved_match_uint;
1175         int len;
1176
1177         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
1178         if (dtbl_entry != NULL) {
1179                 /*
1180                  * Is there currently a dissector handle for this entry?
1181                  */
1182                 handle = dtbl_entry->current;
1183                 if (handle == NULL) {
1184                         /*
1185                          * No - pretend this dissector didn't exist,
1186                          * so that other dissectors might have a chance
1187                          * to dissect this packet.
1188                          */
1189                         return 0;
1190                 }
1191
1192                 /*
1193                  * Save the current value of "pinfo->match_uint",
1194                  * set it to the uint_val that matched, call the
1195                  * dissector, and restore "pinfo->match_uint".
1196                  */
1197                 saved_match_uint  = pinfo->match_uint;
1198                 pinfo->match_uint = uint_val;
1199                 len = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, data);
1200                 pinfo->match_uint = saved_match_uint;
1201
1202                 /*
1203                  * If a new-style dissector returned 0, it means that
1204                  * it didn't think this tvbuff represented a packet for
1205                  * its protocol, and didn't dissect anything.
1206                  *
1207                  * Old-style dissectors can't reject the packet.
1208                  *
1209                  * 0 is also returned if the protocol wasn't enabled.
1210                  *
1211                  * If the packet was rejected, we return 0, so that
1212                  * other dissectors might have a chance to dissect this
1213                  * packet, otherwise we return the dissected length.
1214                  */
1215                 return len;
1216         }
1217         return 0;
1218 }
1219
1220 int
1221 dissector_try_uint(dissector_table_t sub_dissectors, const guint32 uint_val,
1222                    tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1223 {
1224
1225         return dissector_try_uint_new(sub_dissectors, uint_val, tvb, pinfo, tree, TRUE, NULL);
1226 }
1227
1228 /* Look for a given value in a given uint dissector table and, if found,
1229    return the dissector handle for that value. */
1230 dissector_handle_t
1231 dissector_get_uint_handle(dissector_table_t const sub_dissectors, const guint32 uint_val)
1232 {
1233         dtbl_entry_t *dtbl_entry;
1234
1235         dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
1236         if (dtbl_entry != NULL)
1237                 return dtbl_entry->current;
1238         else
1239                 return NULL;
1240 }
1241
1242 dissector_handle_t
1243 dissector_get_default_uint_handle(const char *name, const guint32 uint_val)
1244 {
1245         dissector_table_t sub_dissectors = find_dissector_table(name);
1246
1247         if (sub_dissectors != NULL) {
1248                 dtbl_entry_t *dtbl_entry = find_uint_dtbl_entry(sub_dissectors, uint_val);
1249                 if (dtbl_entry != NULL)
1250                         return dtbl_entry->initial;
1251         }
1252         return NULL;
1253 }
1254
1255 /* Find an entry in a string dissector table. */
1256 static dtbl_entry_t *
1257 find_string_dtbl_entry(dissector_table_t const sub_dissectors, const gchar *pattern)
1258 {
1259         dtbl_entry_t *ret;
1260         char *key;
1261
1262         switch (sub_dissectors->type) {
1263
1264         case FT_STRING:
1265         case FT_STRINGZ:
1266         case FT_STRINGZPAD:
1267                 /*
1268                  * You can do a string lookup in these tables.
1269                  */
1270                 break;
1271
1272         default:
1273                 /*
1274                  * But you can't do a string lookup in any other types
1275                  * of tables.
1276                  */
1277                 g_assert_not_reached();
1278         }
1279
1280         if (sub_dissectors->param == TRUE) {
1281                 key = g_ascii_strdown(pattern, -1);
1282         } else {
1283                 key = g_strdup(pattern);
1284         }
1285
1286         /*
1287          * Find the entry.
1288          */
1289         ret = (dtbl_entry_t *)g_hash_table_lookup(sub_dissectors->hash_table, key);
1290
1291         g_free(key);
1292
1293         return ret;
1294 }
1295
1296 /* Add an entry to a string dissector table. */
1297 void
1298 dissector_add_string(const char *name, const gchar *pattern,
1299                      dissector_handle_t handle)
1300 {
1301         dissector_table_t  sub_dissectors = find_dissector_table( name);
1302         dtbl_entry_t      *dtbl_entry;
1303         char *key;
1304
1305         /*
1306          * Make sure the dissector table exists.
1307          */
1308         if (sub_dissectors == NULL) {
1309                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1310                     name);
1311                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1312                     proto_get_protocol_long_name(handle->protocol));
1313                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1314                         abort();
1315                 return;
1316         }
1317
1318         /* sanity checks */
1319         g_assert(handle!=NULL);
1320         switch (sub_dissectors->type) {
1321
1322         case FT_STRING:
1323         case FT_STRINGZ:
1324         case FT_STRINGZPAD:
1325                 /*
1326                  * You can do a string lookup in these tables.
1327                  */
1328                 break;
1329
1330         default:
1331                 /*
1332                  * But you can't do a string lookup in any other types
1333                  * of tables.
1334                  */
1335                 g_assert_not_reached();
1336         }
1337
1338         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
1339         dtbl_entry->current = handle;
1340         dtbl_entry->initial = dtbl_entry->current;
1341
1342         if (sub_dissectors->param == TRUE) {
1343                 key = g_ascii_strdown(pattern, -1);
1344         } else {
1345                 key = g_strdup(pattern);
1346         }
1347
1348         /* do the table insertion */
1349         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)key,
1350                              (gpointer)dtbl_entry);
1351
1352         /*
1353          * Now add it to the list of handles that could be used for
1354          * "Decode As" with this table, because it *is* being used
1355          * with this table.
1356          */
1357         dissector_add_for_decode_as(name, handle);
1358 }
1359
1360 /* Delete the entry for a dissector in a string dissector table
1361    with a particular pattern. */
1362
1363 /* NOTE: this doesn't use the dissector call variable. It is included to */
1364 /*      be consistant with the dissector_add_string and more importantly to */
1365 /*      be used if the technique of adding a temporary dissector is */
1366 /*      implemented.  */
1367 /*      If temporary dissectors are deleted, then the original dissector must */
1368 /*      be available. */
1369 void
1370 dissector_delete_string(const char *name, const gchar *pattern,
1371         dissector_handle_t handle _U_)
1372 {
1373         dissector_table_t  sub_dissectors = find_dissector_table( name);
1374         dtbl_entry_t      *dtbl_entry;
1375
1376         /* sanity check */
1377         g_assert( sub_dissectors);
1378
1379         /*
1380          * Find the entry.
1381          */
1382         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1383
1384         if (dtbl_entry != NULL) {
1385                 /*
1386                  * Found - remove it.
1387                  */
1388                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1389         }
1390 }
1391
1392 /* Change the entry for a dissector in a string dissector table
1393    with a particular pattern to use a new dissector handle. */
1394 void
1395 dissector_change_string(const char *name, const gchar *pattern,
1396                         dissector_handle_t handle)
1397 {
1398         dissector_table_t  sub_dissectors = find_dissector_table( name);
1399         dtbl_entry_t      *dtbl_entry;
1400
1401         /* sanity check */
1402         g_assert( sub_dissectors);
1403
1404         /*
1405          * See if the entry already exists. If so, reuse it.
1406          */
1407         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1408         if (dtbl_entry != NULL) {
1409                 dtbl_entry->current = handle;
1410                 return;
1411         }
1412
1413         /*
1414          * Don't create an entry if there is no dissector handle - I.E. the
1415          * user said not to decode something that wasn't being decoded
1416          * in the first place.
1417          */
1418         if (handle == NULL)
1419                 return;
1420
1421         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
1422         dtbl_entry->initial = NULL;
1423         dtbl_entry->current = handle;
1424
1425         /* do the table insertion */
1426         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)g_strdup(pattern),
1427                              (gpointer)dtbl_entry);
1428 }
1429
1430 /* Reset an entry in a string sub-dissector table to its initial value. */
1431 void
1432 dissector_reset_string(const char *name, const gchar *pattern)
1433 {
1434         dissector_table_t  sub_dissectors = find_dissector_table( name);
1435         dtbl_entry_t      *dtbl_entry;
1436
1437         /* sanity check */
1438         g_assert( sub_dissectors);
1439
1440         /*
1441          * Find the entry.
1442          */
1443         dtbl_entry = find_string_dtbl_entry(sub_dissectors, pattern);
1444
1445         if (dtbl_entry == NULL)
1446                 return;
1447
1448         /*
1449          * Found - is there an initial value?
1450          */
1451         if (dtbl_entry->initial != NULL) {
1452                 dtbl_entry->current = dtbl_entry->initial;
1453         } else {
1454                 g_hash_table_remove(sub_dissectors->hash_table, pattern);
1455         }
1456 }
1457
1458 /* Look for a given string in a given dissector table and, if found, call
1459    the dissector with the arguments supplied, and return length of dissected data,
1460    otherwise return 0. */
1461 int
1462 dissector_try_string(dissector_table_t sub_dissectors, const gchar *string,
1463                      tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1464 {
1465         dtbl_entry_t            *dtbl_entry;
1466         struct dissector_handle *handle;
1467         int                      len;
1468         const gchar             *saved_match_string;
1469
1470         /* XXX ASSERT instead ? */
1471         if (!string) return 0;
1472         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1473         if (dtbl_entry != NULL) {
1474                 /*
1475                  * Is there currently a dissector handle for this entry?
1476                  */
1477                 handle = dtbl_entry->current;
1478                 if (handle == NULL) {
1479                         /*
1480                          * No - pretend this dissector didn't exist,
1481                          * so that other dissectors might have a chance
1482                          * to dissect this packet.
1483                          */
1484                         return 0;
1485                 }
1486
1487                 /*
1488                  * Save the current value of "pinfo->match_string",
1489                  * set it to the string that matched, call the
1490                  * dissector, and restore "pinfo->match_string".
1491                  */
1492                 saved_match_string = pinfo->match_string;
1493                 pinfo->match_string = string;
1494                 len = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
1495                 pinfo->match_string = saved_match_string;
1496
1497                 /*
1498                  * If a new-style dissector returned 0, it means that
1499                  * it didn't think this tvbuff represented a packet for
1500                  * its protocol, and didn't dissect anything.
1501                  *
1502                  * Old-style dissectors can't reject the packet.
1503                  *
1504                  * 0 is also returned if the protocol wasn't enabled.
1505                  *
1506                  * If the packet was rejected, we return 0, so that
1507                  * other dissectors might have a chance to dissect this
1508                  * packet, otherwise we return the dissected length.
1509                  */
1510                 return len;
1511         }
1512         return 0;
1513 }
1514
1515 /* Look for a given value in a given string dissector table and, if found,
1516    return the dissector handle for that value. */
1517 dissector_handle_t
1518 dissector_get_string_handle(dissector_table_t sub_dissectors,
1519                             const gchar *string)
1520 {
1521         dtbl_entry_t *dtbl_entry;
1522
1523         /* XXX ASSERT instead ? */
1524         if (!string) return NULL;
1525         dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1526         if (dtbl_entry != NULL)
1527                 return dtbl_entry->current;
1528         else
1529                 return NULL;
1530 }
1531
1532 dissector_handle_t
1533 dissector_get_default_string_handle(const char *name, const gchar *string)
1534 {
1535         dissector_table_t sub_dissectors;
1536
1537         /* XXX ASSERT instead ? */
1538         if (!string) return NULL;
1539         sub_dissectors = find_dissector_table(name);
1540         if (sub_dissectors != NULL) {
1541                 dtbl_entry_t *dtbl_entry = find_string_dtbl_entry(sub_dissectors, string);
1542                 if (dtbl_entry != NULL)
1543                         return dtbl_entry->initial;
1544         }
1545         return NULL;
1546 }
1547
1548 /* Add an entry to a "custom" dissector table. */
1549 void dissector_add_custom_table_handle(const char *name, void *pattern, dissector_handle_t handle)
1550 {
1551         dissector_table_t  sub_dissectors = find_dissector_table( name);
1552         dtbl_entry_t      *dtbl_entry;
1553
1554         /*
1555          * Make sure the dissector table exists.
1556          */
1557         if (sub_dissectors == NULL) {
1558                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1559                     name);
1560                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1561                     proto_get_protocol_long_name(handle->protocol));
1562                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1563                         abort();
1564                 return;
1565         }
1566
1567         g_assert(sub_dissectors->type == FT_BYTES);
1568
1569         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
1570         dtbl_entry->current = handle;
1571         dtbl_entry->initial = dtbl_entry->current;
1572
1573         /* do the table insertion */
1574         g_hash_table_insert( sub_dissectors->hash_table, (gpointer)pattern,
1575                              (gpointer)dtbl_entry);
1576
1577         /*
1578          * Now add it to the list of handles that could be used for
1579          * "Decode As" with this table, because it *is* being used
1580          * with this table.
1581          */
1582         dissector_add_for_decode_as(name, handle);
1583 }
1584
1585 dissector_handle_t dissector_get_custom_table_handle(dissector_table_t sub_dissectors, void *key)
1586 {
1587         dtbl_entry_t *dtbl_entry = (dtbl_entry_t *)g_hash_table_lookup(sub_dissectors->hash_table, key);
1588
1589         if (dtbl_entry != NULL)
1590                 return dtbl_entry->current;
1591
1592         return NULL;
1593 }
1594 /* Add an entry to a guid dissector table. */
1595 void dissector_add_guid(const char *name, guid_key* guid_val, dissector_handle_t handle)
1596 {
1597         dissector_table_t  sub_dissectors;
1598         dtbl_entry_t      *dtbl_entry;
1599
1600         sub_dissectors = find_dissector_table(name);
1601
1602         /*
1603          * Make sure the dissector table exists.
1604          */
1605         if (sub_dissectors == NULL) {
1606                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1607                     name);
1608                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1609                     proto_get_protocol_long_name(handle->protocol));
1610                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1611                         abort();
1612                 return;
1613         }
1614
1615         /* sanity checks */
1616         g_assert(handle!=NULL);
1617     if (sub_dissectors->type != FT_GUID) {
1618                 g_assert_not_reached();
1619         }
1620
1621         dtbl_entry = (dtbl_entry_t *)g_malloc(sizeof (dtbl_entry_t));
1622         dtbl_entry->current = handle;
1623         dtbl_entry->initial = dtbl_entry->current;
1624
1625         /* do the table insertion */
1626         g_hash_table_insert( sub_dissectors->hash_table,
1627                              guid_val, (gpointer)dtbl_entry);
1628
1629         /*
1630          * Now add it to the list of handles that could be used with this
1631          * table, because it *is* being used with this table.
1632          */
1633         dissector_add_for_decode_as(name, handle);
1634
1635 }
1636
1637 /* Look for a given value in a given guid dissector table and, if found,
1638    call the dissector with the arguments supplied, and return TRUE,
1639    otherwise return FALSE. */
1640 int dissector_try_guid_new(dissector_table_t sub_dissectors,
1641     guid_key* guid_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const gboolean add_proto_name, void *data)
1642 {
1643         dtbl_entry_t            *dtbl_entry;
1644         struct dissector_handle *handle;
1645         int len;
1646
1647         dtbl_entry = (dtbl_entry_t *)g_hash_table_lookup(sub_dissectors->hash_table, guid_val);
1648         if (dtbl_entry != NULL) {
1649                 /*
1650                  * Is there currently a dissector handle for this entry?
1651                  */
1652                 handle = dtbl_entry->current;
1653                 if (handle == NULL) {
1654                         /*
1655                          * No - pretend this dissector didn't exist,
1656                          * so that other dissectors might have a chance
1657                          * to dissect this packet.
1658                          */
1659                         return 0;
1660                 }
1661
1662                 /*
1663                  * Save the current value of "pinfo->match_uint",
1664                  * set it to the uint_val that matched, call the
1665                  * dissector, and restore "pinfo->match_uint".
1666                  */
1667                 len = call_dissector_work(handle, tvb, pinfo, tree, add_proto_name, data);
1668
1669                 /*
1670                  * If a new-style dissector returned 0, it means that
1671                  * it didn't think this tvbuff represented a packet for
1672                  * its protocol, and didn't dissect anything.
1673                  *
1674                  * Old-style dissectors can't reject the packet.
1675                  *
1676                  * 0 is also returned if the protocol wasn't enabled.
1677                  *
1678                  * If the packet was rejected, we return 0, so that
1679                  * other dissectors might have a chance to dissect this
1680                  * packet, otherwise we return the dissected length.
1681                  */
1682                 return len;
1683         }
1684         return 0;
1685 }
1686
1687 int dissector_try_guid(dissector_table_t sub_dissectors,
1688     guid_key* guid_val, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1689 {
1690     return dissector_try_guid_new(sub_dissectors, guid_val, tvb, pinfo, tree, TRUE, NULL);
1691 }
1692
1693 /** Look for a given value in a given guid dissector table and, if found,
1694  * return the current dissector handle for that value.
1695  *
1696  * @param[in] sub_dissectors Dissector table to search.
1697  * @param[in] uint_val Value to match, e.g. the port number for the TCP dissector.
1698  * @return The matching dissector handle on success, NULL if no match is found.
1699  */
1700 dissector_handle_t dissector_get_guid_handle(
1701     dissector_table_t const sub_dissectors, guid_key* guid_val)
1702 {
1703         dtbl_entry_t *dtbl_entry;
1704
1705         dtbl_entry = (dtbl_entry_t *)g_hash_table_lookup(sub_dissectors->hash_table, guid_val);
1706         if (dtbl_entry != NULL)
1707                 return dtbl_entry->current;
1708         else
1709                 return NULL;
1710 }
1711
1712
1713 dissector_handle_t
1714 dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry)
1715 {
1716         return dtbl_entry->current;
1717 }
1718
1719 static gint
1720 dissector_compare_filter_name(gconstpointer dissector_a, gconstpointer dissector_b)
1721 {
1722         const struct dissector_handle *a = (const struct dissector_handle *)dissector_a;
1723         const struct dissector_handle *b = (const struct dissector_handle *)dissector_b;
1724         const char *a_name, *b_name;
1725         gint ret;
1726
1727         if (a->protocol == NULL)
1728                 a_name = "";
1729         else
1730                 a_name = proto_get_protocol_filter_name(proto_get_id(a->protocol));
1731
1732         if (b->protocol == NULL)
1733                 b_name = "";
1734         else
1735                 b_name = proto_get_protocol_filter_name(proto_get_id(b->protocol));
1736
1737         ret = strcmp(a_name, b_name);
1738         return ret;
1739 }
1740
1741 /* Add a handle to the list of handles that *could* be used with this
1742    table.  That list is used by the "Decode As"/"-d" code in the UI. */
1743 void
1744 dissector_add_for_decode_as(const char *name, dissector_handle_t handle)
1745 {
1746         dissector_table_t  sub_dissectors = find_dissector_table( name);
1747         GSList            *entry;
1748         dissector_handle_t dup_handle;
1749
1750         /*
1751          * Make sure the dissector table exists.
1752          */
1753         if (sub_dissectors == NULL) {
1754                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
1755                     name);
1756                 fprintf(stderr, "Protocol being registered is \"%s\"\n",
1757                     proto_get_protocol_long_name(handle->protocol));
1758                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1759                         abort();
1760                 return;
1761         }
1762
1763         /* Add the dissector as a dependency
1764           (some dissector tables don't have protocol association, so there is
1765           the need for the NULL check */
1766         if (sub_dissectors->protocol != NULL)
1767                 register_depend_dissector(proto_get_protocol_short_name(sub_dissectors->protocol), proto_get_protocol_short_name(handle->protocol));
1768
1769         /* Is it already in this list? */
1770         entry = g_slist_find(sub_dissectors->dissector_handles, (gpointer)handle);
1771         if (entry != NULL) {
1772                 /*
1773                  * Yes - don't insert it again.
1774                  */
1775                 return;
1776         }
1777
1778         /* Ensure the protocol is unique.  This prevents confusion when using Decode As
1779            with duplicative entries */
1780         if (sub_dissectors->allow_dup_proto == DISSECTOR_TABLE_NOT_ALLOW_DUPLICATE)
1781         {
1782                 for (entry = sub_dissectors->dissector_handles; entry != NULL; entry = g_slist_next(entry))
1783                 {
1784                         dup_handle = (dissector_handle_t)entry->data;
1785                         if (dup_handle->protocol == handle->protocol)
1786                         {
1787                                 fprintf(stderr, "Duplicate dissectors %s and %s for protocol %s in dissector table %s\n",
1788                                     dissector_handle_get_dissector_name(handle),
1789                                     dissector_handle_get_dissector_name(dup_handle),
1790                                     proto_get_protocol_short_name(handle->protocol),
1791                                     name);
1792                                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
1793                                         abort();
1794                         }
1795                 }
1796         }
1797
1798         /* Add it to the list. */
1799         sub_dissectors->dissector_handles =
1800                 g_slist_insert_sorted(sub_dissectors->dissector_handles, (gpointer)handle, (GCompareFunc)dissector_compare_filter_name);
1801 }
1802
1803 dissector_handle_t
1804 dtbl_entry_get_initial_handle (dtbl_entry_t *dtbl_entry)
1805 {
1806         return dtbl_entry->initial;
1807 }
1808
1809 GSList *
1810 dissector_table_get_dissector_handles(dissector_table_t dissector_table) {
1811         if (!dissector_table) return NULL;
1812         return dissector_table->dissector_handles;
1813 }
1814
1815 ftenum_t
1816 dissector_table_get_type(dissector_table_t dissector_table) {
1817         if (!dissector_table) return FT_NONE;
1818         return dissector_table->type;
1819 }
1820
1821 dissector_table_allow_e
1822 dissector_table_get_proto_allowed(dissector_table_t dissector_table)
1823 {
1824         if (!dissector_table) return DISSECTOR_TABLE_NOT_ALLOW_DUPLICATE;
1825         return dissector_table->allow_dup_proto;
1826 }
1827
1828 static gint
1829 uuid_equal(gconstpointer k1, gconstpointer k2)
1830 {
1831     const guid_key *key1 = (const guid_key *)k1;
1832     const guid_key *key2 = (const guid_key *)k2;
1833     return ((memcmp(&key1->guid, &key2->guid, sizeof (e_guid_t)) == 0)
1834             && (key1->ver == key2->ver));
1835 }
1836
1837 static guint
1838 uuid_hash(gconstpointer k)
1839 {
1840     const guid_key *key = (const guid_key *)k;
1841     /* This isn't perfect, but the Data1 part of these is almost always
1842        unique. */
1843     return key->guid.data1;
1844 }
1845
1846 /**************************************************/
1847 /*                                                */
1848 /*       Routines to walk dissector tables        */
1849 /*                                                */
1850 /**************************************************/
1851
1852 typedef struct dissector_foreach_info {
1853         gpointer      caller_data;
1854         DATFunc       caller_func;
1855         GHFunc        next_func;
1856         const gchar  *table_name;
1857         ftenum_t      selector_type;
1858 } dissector_foreach_info_t;
1859
1860 /*
1861  * Called for each entry in a dissector table.
1862  */
1863 static void
1864 dissector_table_foreach_func (gpointer key, gpointer value, gpointer user_data)
1865 {
1866         dissector_foreach_info_t *info;
1867         dtbl_entry_t             *dtbl_entry;
1868
1869         g_assert(value);
1870         g_assert(user_data);
1871
1872         dtbl_entry = (dtbl_entry_t *)value;
1873         if (dtbl_entry->current == NULL ||
1874             dtbl_entry->current->protocol == NULL) {
1875                 /*
1876                  * Either there is no dissector for this entry, or
1877                  * the dissector doesn't have a protocol associated
1878                  * with it.
1879                  *
1880                  * XXX - should the latter check be done?
1881                  */
1882                 return;
1883         }
1884
1885         info = (dissector_foreach_info_t *)user_data;
1886         info->caller_func(info->table_name, info->selector_type, key, value,
1887                           info->caller_data);
1888 }
1889
1890 /*
1891  * Called for each entry in the table of all dissector tables.
1892  */
1893 static void
1894 dissector_all_tables_foreach_func (gpointer key, gpointer value, gpointer user_data)
1895 {
1896         dissector_table_t         sub_dissectors;
1897         dissector_foreach_info_t *info;
1898
1899         g_assert(value);
1900         g_assert(user_data);
1901
1902         sub_dissectors = (dissector_table_t)value;
1903         info = (dissector_foreach_info_t *)user_data;
1904         info->table_name = (gchar*) key;
1905         info->selector_type = get_dissector_table_selector_type(info->table_name);
1906         g_hash_table_foreach(sub_dissectors->hash_table, info->next_func, info);
1907 }
1908
1909 /*
1910  * Walk all dissector tables calling a user supplied function on each
1911  * entry.
1912  */
1913 static void
1914 dissector_all_tables_foreach (DATFunc func,
1915                               gpointer user_data)
1916 {
1917         dissector_foreach_info_t info;
1918
1919         info.caller_data = user_data;
1920         info.caller_func = func;
1921         info.next_func   = dissector_table_foreach_func;
1922         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
1923 }
1924
1925 /*
1926  * Walk one dissector table's hash table calling a user supplied function
1927  * on each entry.
1928  */
1929 void
1930 dissector_table_foreach (const char *table_name,
1931                          DATFunc     func,
1932                          gpointer    user_data)
1933 {
1934         dissector_foreach_info_t info;
1935         dissector_table_t        sub_dissectors = find_dissector_table(table_name);
1936
1937         info.table_name    = table_name;
1938         info.selector_type = sub_dissectors->type;
1939         info.caller_func   = func;
1940         info.caller_data   = user_data;
1941         g_hash_table_foreach(sub_dissectors->hash_table, dissector_table_foreach_func, &info);
1942 }
1943
1944 /*
1945  * Walk one dissector table's list of handles calling a user supplied
1946  * function on each entry.
1947  */
1948 void
1949 dissector_table_foreach_handle(const char     *table_name,
1950                                DATFunc_handle  func,
1951                                gpointer        user_data)
1952 {
1953         dissector_table_t sub_dissectors = find_dissector_table(table_name);
1954         GSList *tmp;
1955
1956         for (tmp = sub_dissectors->dissector_handles; tmp != NULL;
1957              tmp = g_slist_next(tmp))
1958         func(table_name, tmp->data, user_data);
1959 }
1960
1961 /*
1962  * Called for each entry in a dissector table.
1963  */
1964 static void
1965 dissector_table_foreach_changed_func (gpointer key, gpointer value, gpointer user_data)
1966 {
1967         dtbl_entry_t             *dtbl_entry;
1968         dissector_foreach_info_t *info;
1969
1970         g_assert(value);
1971         g_assert(user_data);
1972
1973         dtbl_entry = (dtbl_entry_t *)value;
1974         if (dtbl_entry->initial == dtbl_entry->current) {
1975                 /*
1976                  * Entry hasn't changed - don't call the function.
1977                  */
1978                 return;
1979         }
1980
1981         info = (dissector_foreach_info_t *)user_data;
1982         info->caller_func(info->table_name, info->selector_type, key, value,
1983                           info->caller_data);
1984 }
1985
1986 /*
1987  * Walk all dissector tables calling a user supplied function only on
1988  * any entry that has been changed from its original state.
1989  */
1990 void
1991 dissector_all_tables_foreach_changed (DATFunc  func,
1992                                       gpointer user_data)
1993 {
1994         dissector_foreach_info_t info;
1995
1996         info.caller_data = user_data;
1997         info.caller_func = func;
1998         info.next_func   = dissector_table_foreach_changed_func;
1999         g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_func, &info);
2000 }
2001
2002 /*
2003  * Walk one dissector table calling a user supplied function only on
2004  * any entry that has been changed from its original state.
2005  */
2006 void
2007 dissector_table_foreach_changed (const char *table_name,
2008                                  DATFunc     func,
2009                                  gpointer    user_data)
2010 {
2011         dissector_foreach_info_t info;
2012         dissector_table_t sub_dissectors = find_dissector_table(table_name);
2013
2014         info.table_name    = table_name;
2015         info.selector_type = sub_dissectors->type;
2016         info.caller_func   = func;
2017         info.caller_data   = user_data;
2018         g_hash_table_foreach(sub_dissectors->hash_table,
2019                              dissector_table_foreach_changed_func, &info);
2020 }
2021
2022 typedef struct dissector_foreach_table_info {
2023         gpointer      caller_data;
2024         DATFunc_table caller_func;
2025 } dissector_foreach_table_info_t;
2026
2027 /*
2028  * Called for each entry in the table of all dissector tables.
2029  * This is used if we directly process the hash table.
2030  */
2031 static void
2032 dissector_all_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
2033 {
2034         dissector_table_t               table;
2035         dissector_foreach_table_info_t *info;
2036
2037         table = (dissector_table_t)value;
2038         info  = (dissector_foreach_table_info_t *)user_data;
2039         (*info->caller_func)((gchar *)key, table->ui_name, info->caller_data);
2040 }
2041
2042 /*
2043  * Called for each key in the table of all dissector tables.
2044  * This is used if we get a list of table names, sort it, and process the list.
2045  */
2046 static void
2047 dissector_all_tables_foreach_list_func (gpointer key, gpointer user_data)
2048 {
2049         dissector_table_t               table;
2050         dissector_foreach_table_info_t *info;
2051
2052         table = (dissector_table_t)g_hash_table_lookup( dissector_tables, key );
2053         info  = (dissector_foreach_table_info_t *)user_data;
2054         (*info->caller_func)((gchar*)key, table->ui_name, info->caller_data);
2055 }
2056
2057 /*
2058  * Walk all dissector tables calling a user supplied function on each
2059  * table.
2060  */
2061 void
2062 dissector_all_tables_foreach_table (DATFunc_table func,
2063                                     gpointer      user_data,
2064                                     GCompareFunc  compare_key_func)
2065 {
2066         dissector_foreach_table_info_t info;
2067         GList *list;
2068
2069         info.caller_data = user_data;
2070         info.caller_func = func;
2071         if (compare_key_func != NULL)
2072         {
2073                 list = g_hash_table_get_keys(dissector_tables);
2074                 list = g_list_sort(list, compare_key_func);
2075                 g_list_foreach(list, dissector_all_tables_foreach_list_func, &info);
2076                 g_list_free(list);
2077         }
2078         else
2079         {
2080                 g_hash_table_foreach(dissector_tables, dissector_all_tables_foreach_table_func, &info);
2081         }
2082 }
2083
2084 dissector_table_t
2085 register_dissector_table(const char *name, const char *ui_name, const int proto, const ftenum_t type,
2086                          const int param, dissector_table_allow_e allow_dup)
2087 {
2088         dissector_table_t       sub_dissectors;
2089
2090         /* Make sure the registration is unique */
2091         if(g_hash_table_lookup( dissector_tables, name )) {
2092                 g_error("The dissector table %s (%s) is already registered - are you using a buggy plugin?", name, ui_name);
2093         }
2094
2095         /* Create and register the dissector table for this name; returns */
2096         /* a pointer to the dissector table. */
2097         sub_dissectors = g_slice_new(struct dissector_table);
2098         switch (type) {
2099
2100         case FT_UINT8:
2101         case FT_UINT16:
2102         case FT_UINT24:
2103         case FT_UINT32:
2104                 /*
2105                  * XXX - there's no "g_uint_hash()" or "g_uint_equal()",
2106                  * so we use "g_direct_hash()" and "g_direct_equal()".
2107                  */
2108                 sub_dissectors->hash_func = g_direct_hash;
2109                 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash,
2110                                                                g_direct_equal,
2111                                                                NULL,
2112                                                                &g_free );
2113                 break;
2114
2115         case FT_STRING:
2116         case FT_STRINGZ:
2117         case FT_STRINGZPAD:
2118                 sub_dissectors->hash_func = g_str_hash;
2119                 sub_dissectors->hash_table = g_hash_table_new_full( g_str_hash,
2120                                                                g_str_equal,
2121                                                                &g_free,
2122                                                                &g_free );
2123                 break;
2124         case FT_GUID:
2125                 sub_dissectors->hash_table = g_hash_table_new_full( uuid_hash,
2126                                                                uuid_equal,
2127                                                                NULL,
2128                                                                &g_free );
2129                 break;
2130         default:
2131                 g_error("The dissector table %s (%s) is registering an unsupported type - are you using a buggy plugin?", name, ui_name);
2132                 g_assert_not_reached();
2133         }
2134         sub_dissectors->dissector_handles = NULL;
2135         sub_dissectors->ui_name = ui_name;
2136         sub_dissectors->type    = type;
2137         sub_dissectors->param   = param;
2138         sub_dissectors->protocol  = find_protocol_by_id(proto);
2139         sub_dissectors->allow_dup_proto   = allow_dup;
2140         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
2141         return sub_dissectors;
2142 }
2143
2144 dissector_table_t register_custom_dissector_table(const char *name,
2145         const char *ui_name, const int proto, GHashFunc hash_func, GEqualFunc key_equal_func, dissector_table_allow_e allow_dup)
2146 {
2147         dissector_table_t       sub_dissectors;
2148
2149         /* Make sure the registration is unique */
2150         if(g_hash_table_lookup( dissector_tables, name )) {
2151                 g_error("The dissector table %s (%s) is already registered - are you using a buggy plugin?", name, ui_name);
2152         }
2153
2154         /* Create and register the dissector table for this name; returns */
2155         /* a pointer to the dissector table. */
2156         sub_dissectors = g_slice_new(struct dissector_table);
2157         sub_dissectors->hash_func = hash_func;
2158         sub_dissectors->hash_table = g_hash_table_new_full(hash_func,
2159                                                                key_equal_func,
2160                                                                &g_free,
2161                                                                &g_free );
2162
2163         sub_dissectors->dissector_handles = NULL;
2164         sub_dissectors->ui_name = ui_name;
2165         sub_dissectors->type    = FT_BYTES; /* Consider key a "blob" of data, no need to really create new type */
2166         sub_dissectors->param   = BASE_NONE;
2167         sub_dissectors->protocol  = find_protocol_by_id(proto);
2168         sub_dissectors->allow_dup_proto   = allow_dup;
2169         g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
2170         return sub_dissectors;
2171 }
2172
2173 void
2174 deregister_dissector_table(const char *name)
2175 {
2176         dissector_table_t sub_dissectors = find_dissector_table(name);
2177         if (!sub_dissectors) return;
2178
2179         g_hash_table_remove(dissector_tables, name);
2180 }
2181
2182 const char *
2183 get_dissector_table_ui_name(const char *name)
2184 {
2185         dissector_table_t sub_dissectors = find_dissector_table(name);
2186         if (!sub_dissectors) return NULL;
2187
2188         return sub_dissectors->ui_name;
2189 }
2190
2191 ftenum_t
2192 get_dissector_table_selector_type(const char *name)
2193 {
2194         dissector_table_t sub_dissectors = find_dissector_table(name);
2195         if (!sub_dissectors) return FT_NONE;
2196
2197         return sub_dissectors->type;
2198 }
2199
2200 int
2201 get_dissector_table_param(const char *name)
2202 {
2203         dissector_table_t sub_dissectors = find_dissector_table(name);
2204         if (!sub_dissectors) return 0;
2205
2206         return sub_dissectors->param;
2207 }
2208
2209 /* Finds a heuristic dissector table by table name. */
2210 heur_dissector_list_t
2211 find_heur_dissector_list(const char *name)
2212 {
2213         return (heur_dissector_list_t)g_hash_table_lookup(heur_dissector_lists, name);
2214 }
2215
2216 gboolean
2217 has_heur_dissector_list(const gchar *name) {
2218         return (find_heur_dissector_list(name) != NULL);
2219 }
2220
2221 heur_dtbl_entry_t* find_heur_dissector_by_unique_short_name(const char *short_name)
2222 {
2223         return (heur_dtbl_entry_t*)g_hash_table_lookup(heuristic_short_names, short_name);
2224 }
2225
2226 void
2227 heur_dissector_add(const char *name, heur_dissector_t dissector, const char *display_name, const char *short_name, const int proto, heuristic_enable_e enable)
2228 {
2229         heur_dissector_list_t  sub_dissectors = find_heur_dissector_list(name);
2230         const char            *proto_name;
2231         heur_dtbl_entry_t     *hdtbl_entry;
2232         guint                  i, list_size;
2233         GSList                *list_entry;
2234
2235         /*
2236          * Make sure the dissector table exists.
2237          */
2238         if (sub_dissectors == NULL) {
2239                 fprintf(stderr, "OOPS: dissector table \"%s\" doesn't exist\n",
2240                     name);
2241                 proto_name = proto_get_protocol_name(proto);
2242                 if (proto_name != NULL) {
2243                         fprintf(stderr, "Protocol being registered is \"%s\"\n",
2244                             proto_name);
2245                 }
2246                 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
2247                         abort();
2248                 return;
2249         }
2250
2251         /* Verify that sub-dissector is not already in the list */
2252         list_size = g_slist_length(sub_dissectors->dissectors);
2253         for (i = 0; i < list_size; i++)
2254         {
2255                 list_entry = g_slist_nth(sub_dissectors->dissectors, i);
2256                 hdtbl_entry = (heur_dtbl_entry_t *)list_entry->data;
2257                 if ((hdtbl_entry->dissector == dissector) &&
2258                         (hdtbl_entry->protocol == find_protocol_by_id(proto)))
2259                 {
2260                         proto_name = proto_get_protocol_name(proto);
2261                         if (proto_name != NULL) {
2262                                 fprintf(stderr, "Protocol %s is already registered in \"%s\" table\n",
2263                                     proto_name, name);
2264                         }
2265                         if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
2266                                 abort();
2267                         return;
2268                 }
2269         }
2270
2271         /* Ensure short_name is unique */
2272         if (g_hash_table_lookup(heuristic_short_names, short_name) != NULL) {
2273                 g_error("Duplicate heuristic short_name \"%s\"!"
2274                         " This might be caused by an inappropriate plugin or a development error.", short_name);
2275         }
2276
2277         hdtbl_entry = g_slice_new(heur_dtbl_entry_t);
2278         hdtbl_entry->dissector = dissector;
2279         hdtbl_entry->protocol  = find_protocol_by_id(proto);
2280         hdtbl_entry->display_name = display_name;
2281         hdtbl_entry->short_name = short_name;
2282         hdtbl_entry->list_name = g_strdup(name);
2283         hdtbl_entry->enabled   = (enable == HEURISTIC_ENABLE);
2284
2285         /* do the table insertion */
2286         g_hash_table_insert(heuristic_short_names, (gpointer)short_name, hdtbl_entry);
2287
2288         sub_dissectors->dissectors = g_slist_prepend(sub_dissectors->dissectors,
2289             (gpointer)hdtbl_entry);
2290
2291         /* XXX - could be optimized to pass hdtbl_entry directly */
2292         proto_add_heuristic_dissector(hdtbl_entry->protocol, short_name);
2293
2294         /* Add the dissector as a dependency
2295           (some heuristic tables don't have protocol association, so there is
2296           the need for the NULL check */
2297         if (sub_dissectors->protocol != NULL)
2298                 register_depend_dissector(proto_get_protocol_short_name(sub_dissectors->protocol), proto_get_protocol_short_name(hdtbl_entry->protocol));
2299 }
2300
2301
2302
2303 static int
2304 find_matching_heur_dissector( gconstpointer a, gconstpointer b) {
2305         const heur_dtbl_entry_t *hdtbl_entry_a = (const heur_dtbl_entry_t *) a;
2306         const heur_dtbl_entry_t *hdtbl_entry_b = (const heur_dtbl_entry_t *) b;
2307
2308         return (hdtbl_entry_a->dissector == hdtbl_entry_b->dissector) &&
2309                 (hdtbl_entry_a->protocol == hdtbl_entry_b->protocol) ? 0 : 1;
2310 }
2311
2312 void
2313 heur_dissector_delete(const char *name, heur_dissector_t dissector, const int proto) {
2314         heur_dissector_list_t  sub_dissectors = find_heur_dissector_list(name);
2315         heur_dtbl_entry_t      hdtbl_entry;
2316         GSList                *found_entry;
2317
2318         /* sanity check */
2319         g_assert(sub_dissectors != NULL);
2320
2321         hdtbl_entry.dissector = dissector;
2322         hdtbl_entry.protocol  = find_protocol_by_id(proto);
2323
2324         found_entry = g_slist_find_custom(sub_dissectors->dissectors,
2325             (gpointer) &hdtbl_entry, find_matching_heur_dissector);
2326
2327         if (found_entry) {
2328                 heur_dtbl_entry_t *found_hdtbl_entry = (heur_dtbl_entry_t *)(found_entry->data);
2329                 g_free(found_hdtbl_entry->list_name);
2330                 g_hash_table_remove(heuristic_short_names, found_hdtbl_entry->short_name);
2331                 g_slice_free(heur_dtbl_entry_t, found_entry->data);
2332                 sub_dissectors->dissectors = g_slist_delete_link(sub_dissectors->dissectors,
2333                     found_entry);
2334         }
2335 }
2336
2337 gboolean
2338 dissector_try_heuristic(heur_dissector_list_t sub_dissectors, tvbuff_t *tvb,
2339                         packet_info *pinfo, proto_tree *tree, heur_dtbl_entry_t **heur_dtbl_entry, void *data)
2340 {
2341         gboolean           status;
2342         const char        *saved_curr_proto;
2343         const char        *saved_heur_list_name;
2344         GSList            *entry;
2345         guint16            saved_can_desegment;
2346         guint              saved_layers_len = 0;
2347         heur_dtbl_entry_t *hdtbl_entry;
2348         int                proto_id;
2349
2350         /* can_desegment is set to 2 by anyone which offers this api/service.
2351            then everytime a subdissector is called it is decremented by one.
2352            thus only the subdissector immediately ontop of whoever offers this
2353            service can use it.
2354            We save the current value of "can_desegment" for the
2355            benefit of TCP proxying dissectors such as SOCKS, so they
2356            can restore it and allow the dissectors they call to use
2357            the desegmentation service.
2358         */
2359         saved_can_desegment        = pinfo->can_desegment;
2360         pinfo->saved_can_desegment = saved_can_desegment;
2361         pinfo->can_desegment       = saved_can_desegment-(saved_can_desegment>0);
2362
2363         status      = FALSE;
2364         saved_curr_proto = pinfo->current_proto;
2365         saved_heur_list_name = pinfo->heur_list_name;
2366
2367         saved_layers_len = wmem_list_count(pinfo->layers);
2368         *heur_dtbl_entry = NULL;
2369
2370         for (entry = sub_dissectors->dissectors; entry != NULL;
2371             entry = g_slist_next(entry)) {
2372                 /* XXX - why set this now and above? */
2373                 pinfo->can_desegment = saved_can_desegment-(saved_can_desegment>0);
2374                 hdtbl_entry = (heur_dtbl_entry_t *)entry->data;
2375
2376                 if (hdtbl_entry->protocol != NULL &&
2377                         (!proto_is_protocol_enabled(hdtbl_entry->protocol)||(hdtbl_entry->enabled==FALSE))) {
2378                         /*
2379                          * No - don't try this dissector.
2380                          */
2381                         continue;
2382                 }
2383
2384                 if (hdtbl_entry->protocol != NULL) {
2385                         proto_id = proto_get_id(hdtbl_entry->protocol);
2386                         /* do NOT change this behavior - wslua uses the protocol short name set here in order
2387                            to determine which Lua-based heurisitc dissector to call */
2388                         pinfo->current_proto =
2389                                 proto_get_protocol_short_name(hdtbl_entry->protocol);
2390
2391                         /*
2392                          * Add the protocol name to the layers; we'll remove it
2393                          * if the dissector fails.
2394                          */
2395                         wmem_list_append(pinfo->layers, GINT_TO_POINTER(proto_id));
2396                 }
2397
2398                 pinfo->heur_list_name = hdtbl_entry->list_name;
2399
2400                 if ((hdtbl_entry->dissector)(tvb, pinfo, tree, data)) {
2401                         *heur_dtbl_entry = hdtbl_entry;
2402                         status = TRUE;
2403                         break;
2404                 } else {
2405                         /*
2406                          * That dissector didn't accept the packet, so
2407                          * remove its protocol's name from the list
2408                          * of protocols.
2409                          */
2410                         while (wmem_list_count(pinfo->layers) > saved_layers_len) {
2411                                 wmem_list_remove_frame(pinfo->layers, wmem_list_tail(pinfo->layers));
2412                         }
2413                 }
2414         }
2415
2416         pinfo->current_proto = saved_curr_proto;
2417         pinfo->heur_list_name = saved_heur_list_name;
2418         pinfo->can_desegment = saved_can_desegment;
2419         return status;
2420 }
2421
2422 typedef struct heur_dissector_foreach_info {
2423         gpointer      caller_data;
2424         DATFunc_heur  caller_func;
2425         GHFunc        next_func;
2426         const gchar  *table_name;
2427 } heur_dissector_foreach_info_t;
2428
2429 /*
2430  * Called for each entry in a heuristic dissector table.
2431  */
2432 static void
2433 heur_dissector_table_foreach_func (gpointer data, gpointer user_data)
2434 {
2435         heur_dissector_foreach_info_t *info;
2436
2437         g_assert(data);
2438         g_assert(user_data);
2439
2440         info = (heur_dissector_foreach_info_t *)user_data;
2441         info->caller_func(info->table_name, (heur_dtbl_entry_t *)data,
2442                           info->caller_data);
2443 }
2444
2445 /*
2446  * Walk one heuristic dissector table's list calling a user supplied function
2447  * on each entry.
2448  */
2449 void
2450 heur_dissector_table_foreach (const char  *table_name,
2451                               DATFunc_heur func,
2452                               gpointer     user_data)
2453 {
2454         heur_dissector_foreach_info_t info;
2455         heur_dissector_list_t         sub_dissectors = find_heur_dissector_list(table_name);
2456
2457         info.table_name    = table_name;
2458         info.caller_func   = func;
2459         info.caller_data   = user_data;
2460         g_slist_foreach(sub_dissectors->dissectors,
2461                         heur_dissector_table_foreach_func, &info);
2462 }
2463
2464 /*
2465  * Called for each entry in the table of all heuristic dissector tables.
2466  */
2467 typedef struct heur_dissector_foreach_table_info {
2468         gpointer           caller_data;
2469         DATFunc_heur_table caller_func;
2470 } heur_dissector_foreach_table_info_t;
2471
2472 /*
2473  * Called for each entry in the table of all heuristic dissector tables.
2474  * This is used if we directly process the hash table.
2475  */
2476 static void
2477 dissector_all_heur_tables_foreach_table_func (gpointer key, gpointer value, gpointer user_data)
2478 {
2479         heur_dissector_foreach_table_info_t *info;
2480
2481         info = (heur_dissector_foreach_table_info_t *)user_data;
2482     (*info->caller_func)((gchar *)key, (struct heur_dissector_list *)value, info->caller_data);
2483 }
2484
2485 /*
2486  * Called for each key in the table of all dissector tables.
2487  * This is used if we get a list of table names, sort it, and process the list.
2488  */
2489 static void
2490 dissector_all_heur_tables_foreach_list_func (gpointer key, gpointer user_data)
2491 {
2492     struct heur_dissector_list          *list;
2493         heur_dissector_foreach_table_info_t *info;
2494
2495     list = (struct heur_dissector_list *)g_hash_table_lookup(heur_dissector_lists, key);
2496         info = (heur_dissector_foreach_table_info_t *)user_data;
2497         (*info->caller_func)((gchar*)key, list, info->caller_data);
2498 }
2499
2500 /*
2501  * Walk all heuristic dissector tables calling a user supplied function on each
2502  * table.
2503  */
2504 void
2505 dissector_all_heur_tables_foreach_table (DATFunc_heur_table func,
2506                                          gpointer           user_data,
2507                                          GCompareFunc       compare_key_func)
2508 {
2509         heur_dissector_foreach_table_info_t info;
2510         GList *list;
2511
2512         info.caller_data = user_data;
2513         info.caller_func = func;
2514         if (compare_key_func != NULL)
2515         {
2516                 list = g_hash_table_get_keys(dissector_tables);
2517                 list = g_list_sort(list, compare_key_func);
2518                 g_list_foreach(list, dissector_all_heur_tables_foreach_list_func, &info);
2519                 g_list_free(list);
2520         }
2521         else
2522         {
2523                 g_hash_table_foreach(heur_dissector_lists, dissector_all_heur_tables_foreach_table_func, &info);
2524         }
2525 }
2526
2527 static void
2528 display_heur_dissector_table_entries(const char *table_name,
2529     heur_dtbl_entry_t *hdtbl_entry, gpointer user_data _U_)
2530 {
2531         if (hdtbl_entry->protocol != NULL) {
2532                 printf("%s\t%s\t%c\n",
2533                        table_name,
2534                        proto_get_protocol_filter_name(proto_get_id(hdtbl_entry->protocol)),
2535                        (proto_is_protocol_enabled(hdtbl_entry->protocol) && hdtbl_entry->enabled) ? 'T' : 'F');
2536         }
2537 }
2538
2539 static void
2540 dissector_dump_heur_decodes_display(const gchar *table_name, struct heur_dissector_list *listptr _U_, gpointer user_data _U_)
2541 {
2542         heur_dissector_table_foreach(table_name, display_heur_dissector_table_entries, NULL);
2543 }
2544
2545 /*
2546  * For each heuristic dissector table, dump list of dissectors (filter_names) for that table
2547  */
2548 void
2549 dissector_dump_heur_decodes(void)
2550 {
2551         dissector_all_heur_tables_foreach_table(dissector_dump_heur_decodes_display, NULL, NULL);
2552 }
2553
2554
2555 heur_dissector_list_t
2556 register_heur_dissector_list(const char *name, const int proto)
2557 {
2558         heur_dissector_list_t sub_dissectors;
2559
2560         /* Make sure the registration is unique */
2561         if (g_hash_table_lookup(heur_dissector_lists, name) != NULL) {
2562                 g_error("The heuristic dissector list %s is already registered - are you using a buggy plugin?", name);
2563         }
2564
2565         /* Create and register the dissector table for this name; returns */
2566         /* a pointer to the dissector table. */
2567         sub_dissectors = g_slice_new(struct heur_dissector_list);
2568         sub_dissectors->protocol  = find_protocol_by_id(proto);
2569         sub_dissectors->dissectors = NULL;      /* initially empty */
2570         g_hash_table_insert(heur_dissector_lists, (gpointer)name,
2571                             (gpointer) sub_dissectors);
2572         return sub_dissectors;
2573 }
2574
2575 /*
2576  * Register dissectors by name; used if one dissector always calls a
2577  * particular dissector, or if it bases the decision of which dissector
2578  * to call on something other than a numerical value or on "try a bunch
2579  * of dissectors until one likes the packet".
2580  */
2581
2582 /* Get the long name of the protocol for a dissector handle, if it has
2583    a protocol. */
2584 const char *
2585 dissector_handle_get_long_name(const dissector_handle_t handle)
2586 {
2587         if (handle == NULL || handle->protocol == NULL) {
2588                 return NULL;
2589         }
2590         return proto_get_protocol_long_name(handle->protocol);
2591 }
2592
2593 /* Get the short name of the protocol for a dissector handle, if it has
2594    a protocol. */
2595 const char *
2596 dissector_handle_get_short_name(const dissector_handle_t handle)
2597 {
2598         if (handle->protocol == NULL) {
2599                 /*
2600                  * No protocol (see, for example, the handle for
2601                  * dissecting the set of protocols where the first
2602                  * octet of the payload is an OSI network layer protocol
2603                  * ID).
2604                  */
2605                 return NULL;
2606         }
2607         return proto_get_protocol_short_name(handle->protocol);
2608 }
2609
2610 /* Get the index of the protocol for a dissector handle, if it has
2611    a protocol. */
2612 int
2613 dissector_handle_get_protocol_index(const dissector_handle_t handle)
2614 {
2615         if (handle->protocol == NULL) {
2616                 /*
2617                  * No protocol (see, for example, the handle for
2618                  * dissecting the set of protocols where the first
2619                  * octet of the payload is an OSI network layer protocol
2620                  * ID).
2621                  */
2622                 return -1;
2623         }
2624         return proto_get_id(handle->protocol);
2625 }
2626
2627 /* Get a GList of all registered dissector names. The content of the list
2628    is owned by the hash table and should not be modified or freed.
2629    Use g_list_free() when done using the list. */
2630 GList*
2631 get_dissector_names(void)
2632 {
2633         return g_hash_table_get_keys(registered_dissectors);
2634 }
2635
2636 /* Find a registered dissector by name. */
2637 dissector_handle_t
2638 find_dissector(const char *name)
2639 {
2640         return (dissector_handle_t)g_hash_table_lookup(registered_dissectors, name);
2641 }
2642
2643 /** Find a dissector by name and add parent protocol as a depedency*/
2644 dissector_handle_t find_dissector_add_dependency(const char *name, const int parent_proto)
2645 {
2646         dissector_handle_t handle = (dissector_handle_t)g_hash_table_lookup(registered_dissectors, name);
2647         if ((handle != NULL) && (parent_proto > 0))
2648         {
2649                 register_depend_dissector(proto_get_protocol_short_name(find_protocol_by_id(parent_proto)), dissector_handle_get_short_name(handle));
2650         }
2651
2652         return handle;
2653 }
2654
2655 /* Get a dissector name from handle. */
2656 const char *
2657 dissector_handle_get_dissector_name(const dissector_handle_t handle)
2658 {
2659         if (handle == NULL) {
2660                 return NULL;
2661         }
2662         return handle->name;
2663 }
2664
2665 /* Create an anonymous handle for a new dissector. */
2666 dissector_handle_t
2667 create_dissector_handle(dissector_t dissector, const int proto)
2668 {
2669         struct dissector_handle *handle;
2670
2671         handle                  = wmem_new(wmem_epan_scope(), struct dissector_handle);
2672         handle->name            = NULL;
2673         handle->dissector       = dissector;
2674         handle->protocol        = find_protocol_by_id(proto);
2675
2676         return handle;
2677 }
2678
2679 dissector_handle_t create_dissector_handle_with_name(dissector_t dissector,
2680     const int proto, const char* name)
2681 {
2682         struct dissector_handle *handle;
2683
2684         handle                  = wmem_new(wmem_epan_scope(), struct dissector_handle);
2685         handle->name            = name;
2686         handle->dissector       = dissector;
2687         handle->protocol        = find_protocol_by_id(proto);
2688
2689         return handle;
2690 }
2691
2692 /* Destroy an anonymous handle for a dissector. */
2693 static void
2694 destroy_dissector_handle(dissector_handle_t handle)
2695 {
2696         if (handle == NULL) return;
2697
2698         dissector_delete_from_all_tables(handle);
2699         deregister_postdissector(handle);
2700         wmem_free(wmem_epan_scope(), handle);
2701 }
2702
2703 /* Register a new dissector by name. */
2704 dissector_handle_t
2705 register_dissector(const char *name, dissector_t dissector, const int proto)
2706 {
2707         struct dissector_handle *handle;
2708
2709         /* Make sure the registration is unique */
2710         g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
2711
2712         handle                = wmem_new(wmem_epan_scope(), struct dissector_handle);
2713         handle->name          = name;
2714         handle->dissector     = dissector;
2715         handle->protocol      = find_protocol_by_id(proto);
2716
2717         g_hash_table_insert(registered_dissectors, (gpointer)name,
2718                             (gpointer) handle);
2719
2720         return handle;
2721 }
2722
2723 static gboolean
2724 remove_depend_dissector_from_list(depend_dissector_list_t sub_dissectors, const char *dependent)
2725 {
2726         GSList *found_entry;
2727
2728         found_entry = g_slist_find_custom(sub_dissectors->dissectors,
2729                 dependent, (GCompareFunc)strcmp);
2730
2731         if (found_entry) {
2732                 g_free(found_entry->data);
2733                 sub_dissectors->dissectors = g_slist_delete_link(sub_dissectors->dissectors, found_entry);
2734                 return TRUE;
2735         }
2736
2737         return FALSE;
2738 }
2739
2740 static void
2741 remove_depend_dissector_ghfunc(gpointer key _U_, gpointer value, gpointer user_data)
2742 {
2743         depend_dissector_list_t sub_dissectors = (depend_dissector_list_t) value;
2744         const char *dependent = (const char *)user_data;
2745
2746         remove_depend_dissector_from_list(sub_dissectors, dependent);
2747 }
2748
2749 /* Deregister a dissector by name. */
2750 void
2751 deregister_dissector(const char *name)
2752 {
2753         dissector_handle_t handle = find_dissector(name);
2754         if (handle == NULL) return;
2755
2756         g_hash_table_remove(registered_dissectors, name);
2757         g_hash_table_remove(depend_dissector_lists, name);
2758         g_hash_table_foreach(depend_dissector_lists, remove_depend_dissector_ghfunc, (gpointer)name);
2759         g_hash_table_remove(heur_dissector_lists, name);
2760
2761         destroy_dissector_handle(handle);
2762 }
2763
2764 /* Call a dissector through a handle but if the dissector rejected it
2765  * return 0.
2766  */
2767 int
2768 call_dissector_only(dissector_handle_t handle, tvbuff_t *tvb,
2769                     packet_info *pinfo, proto_tree *tree, void *data)
2770 {
2771         int ret;
2772
2773         g_assert(handle != NULL);
2774         ret = call_dissector_work(handle, tvb, pinfo, tree, TRUE, data);
2775         return ret;
2776 }
2777
2778 /* Call a dissector through a handle and if this fails call the "data"
2779  * dissector.
2780  */
2781 int
2782 call_dissector_with_data(dissector_handle_t handle, tvbuff_t *tvb,
2783                          packet_info *pinfo, proto_tree *tree, void *data)
2784 {
2785         int ret;
2786
2787         ret = call_dissector_only(handle, tvb, pinfo, tree, data);
2788         if (ret == 0) {
2789                 /*
2790                  * The protocol was disabled, or the dissector rejected
2791                  * it.  Just dissect this packet as data.
2792                  */
2793                 g_assert(data_handle->protocol != NULL);
2794                 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2795                 return tvb_captured_length(tvb);
2796         }
2797         return ret;
2798 }
2799
2800 int
2801 call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
2802                packet_info *pinfo, proto_tree *tree)
2803 {
2804         return call_dissector_with_data(handle, tvb, pinfo, tree, NULL);
2805 }
2806
2807 int
2808 call_data_dissector(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2809 {
2810         return call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2811 }
2812
2813 /*
2814  * Call a heuristic dissector through a heur_dtbl_entry
2815  */
2816 void call_heur_dissector_direct(heur_dtbl_entry_t *heur_dtbl_entry, tvbuff_t *tvb,
2817         packet_info *pinfo, proto_tree *tree, void *data)
2818 {
2819         const char        *saved_curr_proto;
2820         const char        *saved_heur_list_name;
2821         guint16            saved_can_desegment;
2822
2823         g_assert(heur_dtbl_entry);
2824
2825         /* can_desegment is set to 2 by anyone which offers this api/service.
2826            then everytime a subdissector is called it is decremented by one.
2827            thus only the subdissector immediately ontop of whoever offers this
2828            service can use it.
2829            We save the current value of "can_desegment" for the
2830            benefit of TCP proxying dissectors such as SOCKS, so they
2831            can restore it and allow the dissectors they call to use
2832            the desegmentation service.
2833         */
2834         saved_can_desegment        = pinfo->can_desegment;
2835         pinfo->saved_can_desegment = saved_can_desegment;
2836         pinfo->can_desegment       = saved_can_desegment-(saved_can_desegment>0);
2837
2838         saved_curr_proto = pinfo->current_proto;
2839         saved_heur_list_name = pinfo->heur_list_name;
2840
2841         if (!heur_dtbl_entry->enabled ||
2842                 (heur_dtbl_entry->protocol != NULL && !proto_is_protocol_enabled(heur_dtbl_entry->protocol))) {
2843                 g_assert(data_handle->protocol != NULL);
2844                 call_dissector_work(data_handle, tvb, pinfo, tree, TRUE, NULL);
2845                 return;
2846         }
2847
2848         if (heur_dtbl_entry->protocol != NULL) {
2849                 /* do NOT change this behavior - wslua uses the protocol short name set here in order
2850                         to determine which Lua-based heurisitc dissector to call */
2851                 pinfo->current_proto = proto_get_protocol_short_name(heur_dtbl_entry->protocol);
2852                 wmem_list_append(pinfo->layers, GINT_TO_POINTER(proto_get_id(heur_dtbl_entry->protocol)));
2853         }
2854
2855         pinfo->heur_list_name = heur_dtbl_entry->list_name;
2856
2857         /* call the dissector, as we have saved the result heuristic failure is an error */
2858         if(!(*heur_dtbl_entry->dissector)(tvb, pinfo, tree, data))
2859                 g_assert_not_reached();
2860
2861         /* Restore info from caller */
2862         pinfo->can_desegment = saved_can_desegment;
2863         pinfo->current_proto = saved_curr_proto;
2864         pinfo->heur_list_name = saved_heur_list_name;
2865
2866 }
2867
2868 gboolean register_depend_dissector(const char* parent, const char* dependent)
2869 {
2870         guint                  i, list_size;
2871         GSList                *list_entry;
2872         const char            *protocol_name;
2873         depend_dissector_list_t sub_dissectors;
2874
2875         if ((parent == NULL) || (dependent == NULL))
2876         {
2877                 /* XXX - assert on parent? */
2878                 return FALSE;
2879         }
2880
2881         sub_dissectors = find_depend_dissector_list(parent);
2882         if (sub_dissectors == NULL) {
2883                 /* parent protocol doesn't exist, create it */
2884                 sub_dissectors = g_slice_new(struct depend_dissector_list);
2885                 sub_dissectors->dissectors = NULL;      /* initially empty */
2886                 g_hash_table_insert(depend_dissector_lists, (gpointer)g_strdup(parent), (gpointer) sub_dissectors);
2887         }
2888
2889         /* Verify that sub-dissector is not already in the list */
2890         list_size = g_slist_length(sub_dissectors->dissectors);
2891         for (i = 0; i < list_size; i++)
2892         {
2893                 list_entry = g_slist_nth(sub_dissectors->dissectors, i);
2894                 protocol_name = (const char*)list_entry->data;
2895                 if (strcmp(dependent, protocol_name) == 0)
2896                         return TRUE; /* Dependency already exists */
2897         }
2898
2899         sub_dissectors->dissectors = g_slist_prepend(sub_dissectors->dissectors, (gpointer)g_strdup(dependent));
2900         return TRUE;
2901 }
2902
2903 gboolean deregister_depend_dissector(const char* parent, const char* dependent)
2904 {
2905         depend_dissector_list_t  sub_dissectors = find_depend_dissector_list(parent);
2906
2907         /* sanity check */
2908         g_assert(sub_dissectors != NULL);
2909
2910         return remove_depend_dissector_from_list(sub_dissectors, dependent);
2911 }
2912
2913 depend_dissector_list_t find_depend_dissector_list(const char* name)
2914 {
2915         return (depend_dissector_list_t)g_hash_table_lookup(depend_dissector_lists, name);
2916 }
2917
2918 /*
2919  * Dumps the "layer type"/"decode as" associations to stdout, similar
2920  * to the proto_registrar_dump_*() routines.
2921  *
2922  * There is one record per line. The fields are tab-delimited.
2923  *
2924  * Field 1 = layer type, e.g. "tcp.port"
2925  * Field 2 = selector in decimal
2926  * Field 3 = "decode as" name, e.g. "http"
2927  */
2928
2929
2930 static void
2931 dissector_dump_decodes_display(const gchar *table_name,
2932                                ftenum_t selector_type _U_, gpointer key, gpointer value,
2933                                gpointer user_data _U_)
2934 {
2935         guint32             selector       = GPOINTER_TO_UINT (key);
2936         dissector_table_t   sub_dissectors = find_dissector_table(table_name);
2937         dtbl_entry_t       *dtbl_entry;
2938         dissector_handle_t  handle;
2939         gint                proto_id;
2940         const gchar        *decode_as;
2941
2942         g_assert(sub_dissectors);
2943         switch (sub_dissectors->type) {
2944
2945                 case FT_UINT8:
2946                 case FT_UINT16:
2947                 case FT_UINT24:
2948                 case FT_UINT32:
2949                         dtbl_entry = (dtbl_entry_t *)value;
2950                         g_assert(dtbl_entry);
2951
2952                         handle   = dtbl_entry->current;
2953                         g_assert(handle);
2954
2955                         proto_id = dissector_handle_get_protocol_index(handle);
2956
2957                         if (proto_id != -1) {
2958                                 decode_as = proto_get_protocol_filter_name(proto_id);
2959                                 g_assert(decode_as != NULL);
2960                                 printf("%s\t%u\t%s\n", table_name, selector, decode_as);
2961                         }
2962                         break;
2963
2964         default:
2965                 break;
2966         }
2967 }
2968
2969 void
2970 dissector_dump_decodes(void)
2971 {
2972         dissector_all_tables_foreach(dissector_dump_decodes_display, NULL);
2973 }
2974
2975 /*
2976  * Dumps the "layer type"/"decode as" associations to stdout, similar
2977  * to the proto_registrar_dump_*() routines.
2978  *
2979  * There is one record per line. The fields are tab-delimited.
2980  *
2981  * Field 1 = layer type, e.g. "tcp.port"
2982  * Field 2 = selector in decimal
2983  * Field 3 = "decode as" name, e.g. "http"
2984  */
2985
2986
2987 static void
2988 dissector_dump_dissector_tables_display (gpointer key, gpointer user_data _U_)
2989 {
2990         const char              *table_name = (const char *)key;
2991         dissector_table_t       table;
2992
2993         table = (dissector_table_t)g_hash_table_lookup(dissector_tables, key);
2994         printf("%s\t%s\t%s", table_name, table->ui_name, ftype_name(table->type));
2995         switch (table->type) {
2996
2997         case FT_UINT8:
2998         case FT_UINT16:
2999         case FT_UINT24:
3000         case FT_UINT32:
3001                 switch(table->param) {
3002
3003                 case BASE_NONE:
3004                         printf("\tBASE_NONE");
3005                         break;
3006
3007                 case BASE_DEC:
3008                         printf("\tBASE_DEC");
3009                         break;
3010
3011                 case BASE_HEX:
3012                         printf("\tBASE_HEX");
3013                         break;
3014
3015                 case BASE_DEC_HEX:
3016                         printf("\tBASE_DEC_HEX");
3017                         break;
3018
3019                 case BASE_HEX_DEC:
3020                         printf("\tBASE_HEX_DEC");
3021                         break;
3022
3023                 default:
3024                         printf("\t%d", table->param);
3025                         break;
3026                 }
3027                 break;
3028
3029         default:
3030                 break;
3031         }
3032         printf("\n");
3033 }
3034
3035 static gint
3036 compare_dissector_key_name(gconstpointer dissector_a, gconstpointer dissector_b)
3037 {
3038   return strcmp((const char*)dissector_a, (const char*)dissector_b);
3039 }
3040
3041 void
3042 dissector_dump_dissector_tables(void)
3043 {
3044         GList *list;
3045
3046         list = g_hash_table_get_keys(dissector_tables);
3047         list = g_list_sort(list, compare_dissector_key_name);
3048         g_list_foreach(list, dissector_dump_dissector_tables_display, NULL);
3049         g_list_free(list);
3050 }
3051
3052 static GPtrArray* post_dissectors = NULL;
3053 static guint num_of_postdissectors = 0;
3054
3055 void
3056 register_postdissector(dissector_handle_t handle)
3057 {
3058         if (!post_dissectors)
3059                 post_dissectors = g_ptr_array_new();
3060
3061         g_ptr_array_add(post_dissectors, handle);
3062         num_of_postdissectors++;
3063 }
3064
3065 void
3066 deregister_postdissector(dissector_handle_t handle)
3067 {
3068     if (!post_dissectors) return;
3069
3070     if (g_ptr_array_remove(post_dissectors, handle)) {
3071         num_of_postdissectors--;
3072     }
3073 }
3074
3075 gboolean
3076 have_postdissector(void)
3077 {
3078         guint i;
3079         dissector_handle_t handle;
3080
3081         for(i = 0; i < num_of_postdissectors; i++) {
3082                 handle = (dissector_handle_t) g_ptr_array_index(post_dissectors,i);
3083
3084                 if (handle->protocol != NULL
3085                     && proto_is_protocol_enabled(handle->protocol)) {
3086                         /* We have at least one enabled postdissector */
3087                         return TRUE;
3088                 }
3089         }
3090         return FALSE;
3091 }
3092
3093 void
3094 call_all_postdissectors(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3095 {
3096         guint i;
3097
3098         for(i = 0; i < num_of_postdissectors; i++) {
3099                 call_dissector_only((dissector_handle_t) g_ptr_array_index(post_dissectors,i),
3100                                     tvb,pinfo,tree, NULL);
3101         }
3102 }
3103
3104 /*
3105  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
3106  *
3107  * Local variables:
3108  * c-basic-offset: 8
3109  * tab-width: 8
3110  * indent-tabs-mode: t
3111  * End:
3112  *
3113  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3114  * :indentSize=8:tabSize=8:noTabs=false:
3115  */