Provide for per-protocol-tree data in the proto_tree code.
[obnox/wireshark/wip.git] / epan / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.48 2001/12/18 19:09:03 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __PACKET_H__
26 #define __PACKET_H__
27
28 #include "wiretap/wtap.h"
29 #include "proto.h"
30 #include "tvbuff.h"
31 #include "pint.h"
32 #include "to_str.h"
33 #include "value_string.h"
34 #include "column_info.h"
35 #include "frame_data.h"
36 #include "packet_info.h"
37 #include "column-utils.h"
38 #include "epan.h"
39
40 #define hi_nibble(b) (((b) & 0xf0) >> 4)
41 #define lo_nibble(b) ((b) & 0x0f)
42
43 /* Useful when you have an array whose size you can tell at compile-time */
44 #define array_length(x) (sizeof x / sizeof x[0])
45
46 /* Check whether the "len" bytes of data starting at "offset" is
47  * entirely inside the captured data for this packet. */
48 #define BYTES_ARE_IN_FRAME(offset, captured_len, len) \
49         ((offset) + (len) <= (captured_len))
50
51 /* To pass one of two strings, singular or plural */
52 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
53
54 typedef struct _packet_counts {
55   gint           sctp;
56   gint           tcp;
57   gint           udp;
58   gint           icmp;
59   gint           ospf;
60   gint           gre;
61   gint           netbios;
62   gint           ipx;
63   gint           vines;
64   gint           other;
65   gint           total;
66 } packet_counts;
67
68 /* Types of character encodings */
69 typedef enum {
70         CHAR_ASCII       = 0,   /* ASCII */
71         CHAR_EBCDIC      = 1    /* EBCDIC */
72 } char_enc;
73
74 /* Struct for boolean enumerations */
75 typedef struct true_false_string {
76         char    *true_string;
77         char    *false_string;
78 } true_false_string;
79
80 extern void packet_init(void);
81 extern void packet_cleanup(void);
82
83 /* Handle for dissectors you call directly or register with "dissector_add()".
84    This handle is opaque outside of "packet.c". */
85 struct dissector_handle;
86 typedef struct dissector_handle *dissector_handle_t;
87
88 /* Hash table for matching port numbers and dissectors; this is opaque
89    outside of "packet.c". */
90 struct dissector_table;
91 typedef struct dissector_table *dissector_table_t;
92
93 /* types for sub-dissector lookup */
94 typedef void (*old_dissector_t)(const u_char *, int, frame_data *, proto_tree *);
95 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
96
97 typedef void (*DATFunc) (gchar *table_name, gpointer key, gpointer value, gpointer user_data);
98 typedef void (*DATFunc_handle) (gchar *table_name, gpointer value, gpointer user_data);
99
100 /* Opaque structure - provides type checking but no access to components */
101 typedef struct dtbl_entry dtbl_entry_t;
102
103 extern dissector_handle_t dtbl_entry_get_handle (dtbl_entry_t *dtbl_entry);
104 extern dissector_handle_t dtbl_entry_get_initial_handle (dtbl_entry_t * entry);
105 extern void dissector_table_foreach_changed (char *name, DATFunc func,
106     gpointer user_data);
107 extern void dissector_table_foreach (char *name, DATFunc func,
108     gpointer user_data);
109 extern void dissector_all_tables_foreach_changed (DATFunc func,
110     gpointer user_data);
111 extern void dissector_table_foreach_handle(char *name, DATFunc_handle func,
112     gpointer user_data);
113
114 /* a protocol uses the function to register a sub-dissector table */
115 extern dissector_table_t register_dissector_table(const char *name,
116     char *ui_name, ftenum_t type, int base);
117
118 /* Get the UI name for a sub-dissector table, given its internal name */
119 extern char *get_dissector_table_ui_name(const char *name);
120
121 /* Get the field type to use when displaying values of the selector for a
122    sub-dissector table, given the table's internal name */
123 ftenum_t get_dissector_table_type(const char *name);
124
125 /* Get the base to use when displaying values of the selector for a
126    sub-dissector table, given the table's internal name */
127 extern int get_dissector_table_base(const char *name);
128
129 /* Add a sub-dissector to a dissector table.  Called by the protocol routine */
130 /* that wants to register a sub-dissector.  */
131 extern void dissector_add(const char *abbrev, guint32 pattern,
132     dissector_handle_t handle);
133
134 /* Add a sub-dissector to a dissector table.  Called by the protocol routine */
135 /* that wants to de-register a sub-dissector.  */
136 extern void dissector_delete(const char *name, guint32 pattern,
137     dissector_handle_t handle);
138
139 extern void dissector_change(const char *abbrev, guint32 pattern,
140     dissector_handle_t handle);
141
142 /* Reset a dissector in a sub-dissector table to its initial value. */
143 extern void dissector_reset(const char *name, guint32 pattern);
144
145 /* Look for a given port in a given dissector table and, if found, call
146    the dissector with the arguments supplied, and return TRUE, otherwise
147    return FALSE. */
148 extern gboolean dissector_try_port(dissector_table_t sub_dissectors,
149     guint32 port, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
150
151 /* Look for a given port in a given dissector table and, if found, return
152    the dissector handle for that port. */
153 extern dissector_handle_t dissector_get_port_handle(
154     dissector_table_t sub_dissectors, guint32 port);
155
156 /* Add a handle to the list of handles that *could* be used with this
157    table.  That list is used by code in the UI. */
158 extern void dissector_add_handle(const char *name, dissector_handle_t handle);
159
160 /* List of "heuristic" dissectors (which get handed a packet, look at it,
161    and either recognize it as being for their protocol, dissect it, and
162    return TRUE, or don't recognize it and return FALSE) to be called
163    by another dissector. */
164 typedef GSList *heur_dissector_list_t;
165
166 /* Type of a heuristic dissector */
167 typedef gboolean (*heur_dissector_t)(tvbuff_t *, packet_info *,
168         proto_tree *);
169
170 /* A protocol uses this function to register a heuristic dissector list */
171 extern void register_heur_dissector_list(const char *name,
172     heur_dissector_list_t *list);
173
174 /* Add a sub-dissector to a heuristic dissector list.  Called by the
175    protocol routine that wants to register a sub-dissector.  */
176 extern void heur_dissector_add(const char *name, heur_dissector_t dissector,
177     int proto);
178
179 /* Try all the dissectors in a given heuristic dissector list until
180    we find one that recognizes the protocol, in which case we return
181    TRUE, or we run out of dissectors, in which case we return FALSE. */
182 extern gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
183     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
184
185 /* Register a dissector. */
186 extern void register_dissector(const char *name, dissector_t dissector,
187     int proto);
188
189 /* Get the short name of the protocol for a dissector handle. */
190 extern char *dissector_handle_get_short_name(dissector_handle_t handle);
191
192 /* Find a dissector by name. */
193 extern dissector_handle_t find_dissector(const char *name);
194
195 /* Create an anonymous handle for a dissector. */
196 extern dissector_handle_t create_dissector_handle(dissector_t dissector,
197     int proto);
198
199 /* Call a dissector through a handle. */
200 extern void call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
201     packet_info *pinfo, proto_tree *tree);
202
203 /* Do all one-time initialization. */
204 extern void dissect_init(void);
205
206 extern void dissect_cleanup(void);
207
208 /*
209  * Given a tvbuff, a packet_info *, and a length from a packet header,
210  * adjust the length of the tvbuff, and the "len" and "captured_len"
211  * members of the "packet_info" structure, to reflect the specified
212  * length.
213  */
214 extern void set_actual_length(tvbuff_t *tvb, packet_info *pinfo,
215     guint specified_len);
216
217 /* Allow protocols to register "init" routines, which are called before
218    we make a pass through a capture file and dissect all its packets
219    (e.g., when we read in a new capture file, or run a "filter packets"
220    or "colorize packets" pass over the current capture file). */
221 extern void register_init_routine(void (*func)(void));
222
223 /* Call all the registered "init" routines. */
224 extern void init_all_protocols(void);
225
226 /*
227  * Dissectors should never modify the packet data.
228  */
229 extern void dissect_packet(epan_dissect_t *edt,
230     union wtap_pseudo_header *pseudo_header, const u_char *pd,
231     frame_data *fd, column_info *cinfo);
232
233 /* These functions are in packet-ethertype.c */
234 extern void capture_ethertype(guint16 etype, const u_char *pd, int offset,
235                 int len, packet_counts *ld);
236 extern void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
237                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
238                 int etype_id, int trailer_id);
239
240 #endif /* packet.h */