2 * Definitions for packet disassembly structures and routines
4 * $Id: packet.h,v 1.27 2001/04/01 03:42:00 hagbard Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
30 #include "wiretap/wtap.h"
35 #include "value_string.h"
36 #include "column_info.h"
38 #define hi_nibble(b) (((b) & 0xf0) >> 4)
39 #define lo_nibble(b) ((b) & 0x0f)
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])
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.
49 #define END_OF_FRAME (pi.captured_len - offset)
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)
55 /* Check whether there's any data at all starting at "offset". */
56 #define IS_DATA_IN_FRAME(offset) ((offset) < pi.captured_len)
58 /* To pass one of two strings, singular or plural */
59 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
61 /* Allocate all the data structures for constructing column data, given
62 the number of columns. */
63 void col_init(column_info *, gint);
65 typedef struct _packet_counts {
79 /* Types of character encodings */
81 CHAR_ASCII = 0, /* ASCII */
82 CHAR_EBCDIC = 1 /* EBCDIC */
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
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 */
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 */
113 /* Types of addresses Ethereal knows about. */
115 AT_NONE, /* no link-layer address */
116 AT_ETHER, /* MAC (Ethernet, 802.x, FDDI) address */
121 AT_ATALK, /* Appletalk DDP */
122 AT_VINES, /* Banyan Vines */
123 AT_OSI, /* OSI NSAP */
124 AT_DLCI /* Frame Relay DLCI */
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 */
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); \
139 /* Types of port numbers Ethereal knows about. */
141 PT_NONE, /* no port number */
145 PT_NCP /* NCP connection */
148 #define P2P_DIR_UNKNOWN -1
149 #define P2P_DIR_SENT 0
150 #define P2P_DIR_RECV 1
152 typedef struct _packet_info {
153 const char *current_proto; /* name of protocol currently being dissected */
155 tvbuff_t *compat_top_tvb; /* only needed while converting Ethereal to use tvbuffs */
156 union wtap_pseudo_header *pseudo_header;
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 */
178 extern packet_info pi;
180 /* Struct for boolean enumerations */
181 typedef struct true_false_string {
186 void packet_init(void);
187 void packet_cleanup(void);
189 /* Hash table for matching port numbers and dissectors */
190 typedef GHashTable* dissector_table_t;
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 *);
196 typedef void (*DATFunc) (gchar *table_name, gpointer key, gpointer value, gpointer user_data);
198 /* Opaque structure - provides type checking but no access to components */
199 typedef struct dtbl_entry dtbl_entry_t;
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);
208 /* a protocol uses the function to register a sub-dissector table */
209 dissector_table_t register_dissector_table(const char *name);
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);
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);
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);
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
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);
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;
242 /* Type of a heuristic dissector */
243 typedef gboolean (*old_heur_dissector_t)(const u_char *, int, frame_data *,
245 typedef gboolean (*heur_dissector_t)(tvbuff_t *, packet_info *,
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);
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,
255 void heur_dissector_add(const char *name, heur_dissector_t dissector,
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);
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).
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;
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);
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,
285 void conv_dissector_add(const char *name, dissector_t dissector,
288 /* Opaque structure - provides type checking but no access to components */
289 typedef struct conv_dtbl_entry conv_dtbl_entry_t;
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);
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;
301 /* Register a dissector. */
302 void register_dissector(const char *name, dissector_t dissector, int proto);
304 /* Find a dissector by name. */
305 dissector_handle_t find_dissector(const char *name);
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);
311 /* Utility routines used by packet*.c */
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 *);
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)));
323 void col_add_fstr(frame_data *, gint, gchar *, ...);
324 void col_append_fstr(frame_data *, gint, gchar *, ...);
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 *);
331 void p_add_proto_data(frame_data *, int, void *);
332 void *p_get_proto_data(frame_data *, int);
334 void blank_packetinfo(void);
336 /* Do all one-time initialization. */
337 void dissect_init(void);
339 void dissect_cleanup(void);
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));
347 /* Call all the registered "init" routines. */
348 void init_all_protocols(void);
351 * Dissectors should never modify the packet data.
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);
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[];
368 extern const char *ipprotostr(int proto);
370 #endif /* packet.h */