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