Moved the column_info structure and related enum from packet.{c,h} to
[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.27 2001/04/01 03:42:00 hagbard Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26
27 #ifndef __PACKET_H__
28 #define __PACKET_H__
29
30 #include "wiretap/wtap.h"
31 #include "proto.h"
32 #include "tvbuff.h"
33 #include "pint.h"
34 #include "to_str.h"
35 #include "value_string.h"
36 #include "column_info.h"
37
38 #define hi_nibble(b) (((b) & 0xf0) >> 4)
39 #define lo_nibble(b) ((b) & 0x0f)
40
41 /* Useful when you have an array whose size you can tell at compile-time */
42 #define array_length(x) (sizeof x / sizeof x[0])
43
44 /* Useful when highlighting regions inside a dissect_*() function. With this
45  * macro, you can highlight from an arbitrary offset to the end of the
46  * packet (which may come before the end of the frame).
47  * See old_dissect_data() for an example.
48  */
49 #define END_OF_FRAME    (pi.captured_len - offset)
50
51 /* Check whether the "len" bytes of data starting at "offset" is
52  * entirely inside the captured data for this packet. */
53 #define BYTES_ARE_IN_FRAME(offset, len) ((offset) + (len) <= pi.captured_len)
54
55 /* Check whether there's any data at all starting at "offset". */
56 #define IS_DATA_IN_FRAME(offset)        ((offset) < pi.captured_len)
57                 
58 /* To pass one of two strings, singular or plural */
59 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
60
61 /* Allocate all the data structures for constructing column data, given
62    the number of columns. */
63 void                 col_init(column_info *, gint);
64
65 typedef struct _packet_counts {
66   gint           sctp;
67   gint           tcp;
68   gint           udp;
69   gint           icmp;
70   gint           ospf;
71   gint           gre;
72   gint           netbios;
73   gint           ipx;
74   gint           vines;
75   gint           other;
76   gint           total;
77 } packet_counts;
78
79 /* Types of character encodings */
80 typedef enum {
81         CHAR_ASCII       = 0,   /* ASCII */
82         CHAR_EBCDIC      = 1    /* EBCDIC */
83 } char_enc;
84
85 /* XXX - some of this stuff is used only while a packet is being dissected;
86    should we keep that stuff in the "packet_info" structure, instead, to
87    save memory? */
88 typedef struct _frame_data {
89   struct _frame_data *next; /* Next element in list */
90   struct _frame_data *prev; /* Previous element in list */
91   GSList *pfd;              /* Per frame proto data */
92   GSList *data_src;         /* Frame data sources */
93   guint32      num;       /* Frame number */
94   guint32      pkt_len;   /* Packet length */
95   guint32      cap_len;   /* Amount actually captured */
96   gint32       rel_secs;  /* Relative seconds (yes, it can be negative) */
97   gint32       rel_usecs; /* Relative microseconds (yes, it can be negative) */
98   guint32      abs_secs;  /* Absolute seconds */
99   guint32      abs_usecs; /* Absolute microseconds */
100   gint32       del_secs;  /* Delta seconds (yes, it can be negative) */
101   gint32       del_usecs; /* Delta microseconds (yes, it can be negative) */
102   long         file_off;  /* File offset */
103   column_info *cinfo;     /* Column formatting information */
104   int          lnk_t;     /* Per-packet encapsulation/data-link type */
105   struct {
106         unsigned int passed_dfilter     : 1; /* 1 = display, 0 = no display */
107         unsigned int encoding           : 2; /* Character encoding (ASCII, EBCDIC...) */
108         unsigned int visited            : 1; /* Has this packet been visited yet? 1=Yes,0=No*/
109         unsigned int marked             : 1; /* 1 = marked by user, 0 = normal */
110   } flags;
111 } frame_data;
112
113 /* Types of addresses Ethereal knows about. */
114 typedef enum {
115   AT_NONE,              /* no link-layer address */
116   AT_ETHER,             /* MAC (Ethernet, 802.x, FDDI) address */
117   AT_IPv4,              /* IPv4 */
118   AT_IPv6,              /* IPv6 */
119   AT_IPX,               /* IPX */
120   AT_SNA,               /* SNA */
121   AT_ATALK,             /* Appletalk DDP */
122   AT_VINES,             /* Banyan Vines */
123   AT_OSI,               /* OSI NSAP */
124   AT_DLCI               /* Frame Relay DLCI */
125 } address_type;
126
127 typedef struct _address {
128   address_type  type;           /* type of address */
129   int           len;            /* length of address, in bytes */
130   const guint8 *data;           /* bytes that constitute address */
131 } address;
132
133 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) { \
134         (addr)->type = (addr_type); \
135         (addr)->len = (addr_len); \
136         (addr)->data = (addr_data); \
137         }
138
139 /* Types of port numbers Ethereal knows about. */
140 typedef enum {
141   PT_NONE,              /* no port number */
142   PT_SCTP,              /* SCTP */
143   PT_TCP,               /* TCP */
144   PT_UDP,               /* UDP */
145   PT_NCP                /* NCP connection */
146 } port_type;
147
148 #define P2P_DIR_UNKNOWN -1
149 #define P2P_DIR_SENT    0
150 #define P2P_DIR_RECV    1
151
152 typedef struct _packet_info {
153   const char *current_proto;    /* name of protocol currently being dissected */
154   frame_data *fd;
155   tvbuff_t *compat_top_tvb;     /* only needed while converting Ethereal to use tvbuffs */
156   union wtap_pseudo_header *pseudo_header;
157   int     len;
158   int     captured_len;
159   address dl_src;               /* link-layer source address */
160   address dl_dst;               /* link-layer destination address */
161   address net_src;              /* network-layer source address */
162   address net_dst;              /* network-layer destination address */
163   address src;                  /* source address (net if present, DL otherwise )*/
164   address dst;                  /* destination address (net if present, DL otherwise )*/
165   guint32 ethertype;            /* Ethernet Type Code, if this is an Ethernet packet */
166   guint32 ipproto;              /* IP protocol, if this is an IP packet */
167   guint32 ipxptype;             /* IPX packet type, if this is an IPX packet */
168   gboolean fragmented;          /* TRUE if the protocol is only a fragment */
169   port_type ptype;              /* type of the following two port numbers */
170   guint32 srcport;              /* source port */
171   guint32 destport;             /* destination port */
172   guint32 match_port;
173   int     iplen;
174   int     iphdrlen;
175   int     p2p_dir;
176 } packet_info;
177
178 extern packet_info pi;
179
180 /* Struct for boolean enumerations */
181 typedef struct true_false_string {
182         char    *true_string;
183         char    *false_string;
184 } true_false_string;
185
186 void packet_init(void);
187 void packet_cleanup(void);
188
189 /* Hash table for matching port numbers and dissectors */
190 typedef GHashTable* dissector_table_t;
191
192 /* types for sub-dissector lookup */
193 typedef void (*old_dissector_t)(const u_char *, int, frame_data *, proto_tree *);
194 typedef void (*dissector_t)(tvbuff_t *, packet_info *, proto_tree *);
195
196 typedef void (*DATFunc) (gchar *table_name, gpointer key, gpointer value, gpointer user_data);
197
198 /* Opaque structure - provides type checking but no access to components */
199 typedef struct dtbl_entry dtbl_entry_t;
200
201 gboolean dissector_get_old_flag (dtbl_entry_t *entry);
202 gint dissector_get_proto (dtbl_entry_t * entry);
203 gint dissector_get_initial_proto (dtbl_entry_t * entry);
204 void dissector_table_foreach_changed (char *name, DATFunc func, gpointer user_data);
205 void dissector_table_foreach (char *name, DATFunc func, gpointer user_data);
206 void dissector_all_tables_foreach_changed (DATFunc func, gpointer user_data);
207
208 /* a protocol uses the function to register a sub-dissector table */
209 dissector_table_t register_dissector_table(const char *name);
210
211 /* Add a sub-dissector to a dissector table.  Called by the protocol routine */
212 /* that wants to register a sub-dissector.  */
213 void old_dissector_add(const char *abbrev, guint32 pattern,
214     old_dissector_t dissector, int proto);
215 void dissector_add(const char *abbrev, guint32 pattern,
216     dissector_t dissector, int proto);
217
218 /* Add a sub-dissector to a dissector table.  Called by the protocol routine */
219 /* that wants to de-register a sub-dissector.  */
220 void old_dissector_delete(const char *name, guint32 pattern, old_dissector_t dissector);
221 void dissector_delete(const char *name, guint32 pattern, dissector_t dissector);
222
223 /* Reset a dissector in a sub-dissector table to its initial value. */
224 void dissector_change(const char *abbrev, guint32 pattern,
225     dissector_t dissector, gboolean old, int proto);
226 void dissector_reset(const char *name, guint32 pattern);
227
228 /* Look for a given port in a given dissector table and, if found, call
229    the dissector with the arguments supplied, and return TRUE, otherwise
230    return FALSE. */
231 gboolean old_dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
232     const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
233 gboolean dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
234     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
235
236 /* List of "heuristic" dissectors (which get handed a packet, look at it,
237    and either recognize it as being for their protocol, dissect it, and
238    return TRUE, or don't recognize it and return FALSE) to be called
239    by another dissector. */
240 typedef GSList *heur_dissector_list_t;
241
242 /* Type of a heuristic dissector */
243 typedef gboolean (*old_heur_dissector_t)(const u_char *, int, frame_data *,
244         proto_tree *);
245 typedef gboolean (*heur_dissector_t)(tvbuff_t *, packet_info *,
246         proto_tree *);
247
248 /* A protocol uses this function to register a heuristic dissector list */
249 void register_heur_dissector_list(const char *name, heur_dissector_list_t *list);
250
251 /* Add a sub-dissector to a heuristic dissector list.  Called by the
252    protocol routine that wants to register a sub-dissector.  */
253 void old_heur_dissector_add(const char *name, old_heur_dissector_t dissector,
254     int proto);
255 void heur_dissector_add(const char *name, heur_dissector_t dissector,
256     int proto);
257
258 /* Try all the dissectors in a given heuristic dissector list until
259    we find one that recognizes the protocol, in which case we return
260    TRUE, or we run out of dissectors, in which case we return FALSE. */
261 gboolean dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
262     tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
263
264 /* List of "conversation" dissectors (they're not heuristic, but are
265    assigned to a conversation if some other dissector sees some traffic
266    saying "traffic between these hosts on these ports will be of type
267    XXX", e.g. RTSP traffic doing so).
268
269    These lists are for use by the UI, which, for a given conversation,
270    would offer a list of dissectors that could be used with it; this
271    would include dissectors on the conversation dissector list for
272    the transport-layer protocol for the conversation, as well as
273    dissectors for any port-based lists for that protocol (as a conversation
274    between two ports, both of which have dissectors associated with them,
275    might have been given to the wrong one of those dissectors). */
276 typedef GSList *conv_dissector_list_t;
277
278 /* A protocol uses this function to register a conversation dissector list */
279 void register_conv_dissector_list(const char *name, conv_dissector_list_t *list);
280
281 /* Add a sub-dissector to a conversation dissector list.  Called by the
282    protocol routine that wants to register a sub-dissector.  */
283 void old_conv_dissector_add(const char *name, old_dissector_t dissector,
284     int proto);
285 void conv_dissector_add(const char *name, dissector_t dissector,
286     int proto);
287
288 /* Opaque structure - provides type checking but no access to components */
289 typedef struct conv_dtbl_entry conv_dtbl_entry_t;
290
291 gboolean conv_dissector_get_old_flag (conv_dtbl_entry_t *entry);
292 gint conv_dissector_get_proto (conv_dtbl_entry_t * entry);
293 void dissector_conv_foreach(char *name, DATFunc func, gpointer user_data);
294 void dissector_all_conv_foreach(DATFunc func, gpointer user_data);
295
296 /* Handle for dissectors you call directly.
297    This handle is opaque outside of "packet.c". */
298 struct dissector_handle;
299 typedef struct dissector_handle *dissector_handle_t;
300
301 /* Register a dissector. */
302 void register_dissector(const char *name, dissector_t dissector, int proto);
303
304 /* Find a dissector by name. */
305 dissector_handle_t find_dissector(const char *name);
306
307 /* Call a dissector through a handle. */
308 void call_dissector(dissector_handle_t handle, tvbuff_t *tvb,
309     packet_info *pinfo, proto_tree *tree);
310
311 /* Utility routines used by packet*.c */
312
313 void       col_set_writable(frame_data *fd, gboolean writable);
314 gint       check_col(frame_data *, gint);
315 void       col_clear(frame_data *, gint);
316 void       col_set_str(frame_data *, gint, gchar *);
317 #if __GNUC__ == 2
318 void       col_add_fstr(frame_data *, gint, gchar *, ...)
319     __attribute__((format (printf, 3, 4)));
320 void       col_append_fstr(frame_data *, gint, gchar *, ...)
321     __attribute__((format (printf, 3, 4)));
322 #else
323 void       col_add_fstr(frame_data *, gint, gchar *, ...);
324 void       col_append_fstr(frame_data *, gint, gchar *, ...);
325 #endif
326 void       col_add_str(frame_data *, gint, const gchar *);
327 void       col_append_str(frame_data *, gint, gchar *);
328 void       col_set_cls_time(frame_data *, int);
329 void       fill_in_columns(frame_data *);
330
331 void       p_add_proto_data(frame_data *, int, void *);
332 void       *p_get_proto_data(frame_data *, int);
333
334 void blank_packetinfo(void);
335
336 /* Do all one-time initialization. */
337 void dissect_init(void);
338
339 void dissect_cleanup(void);
340
341 /* Allow protocols to register "init" routines, which are called before
342    we make a pass through a capture file and dissect all its packets
343    (e.g., when we read in a new capture file, or run a "filter packets"
344    or "colorize packets" pass over the current capture file). */
345 void register_init_routine(void (*func)(void));
346
347 /* Call all the registered "init" routines. */
348 void init_all_protocols(void);
349
350 /*
351  * Dissectors should never modify the packet data.
352  */
353 void dissect_packet(tvbuff_t **p_tvb, union wtap_pseudo_header *pseudo_header,
354                 const u_char *pd, frame_data *fd, proto_tree *tree);
355 void old_dissect_data(const u_char *, int, frame_data *, proto_tree *);
356 void dissect_data(tvbuff_t *tvb, int, packet_info *pinfo, proto_tree *tree);
357
358
359 /* These functions are in packet-ethertype.c */
360 void capture_ethertype(guint16 etype, int offset,
361                 const u_char *pd, packet_counts *ld);
362 void ethertype(guint16 etype, tvbuff_t *tvb, int offset_after_ethertype,
363                 packet_info *pinfo, proto_tree *tree, proto_tree *fh_tree,
364                 int etype_id, int trailer_id);
365 extern const value_string etype_vals[];
366
367 /* ipproto.c */
368 extern const char *ipprotostr(int proto);
369
370 #endif /* packet.h */