Fix a bunch of dissectors to use "pi.captured_len" rather than
[obnox/wireshark/wip.git] / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.167 2000/01/23 08:55:37 guy 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 #ifndef __WTAP_H__
31 #include "wiretap/wtap.h"
32 #endif
33
34 #ifndef __PROTO_H__
35 #include "proto.h"
36 #endif
37
38 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
39  * byte array, returns the value of the two or four bytes at the pointer.
40  * The pletoh[sl] versions return the little-endian representation.
41  */
42
43 #define pntohs(p)  ((guint16)                       \
44                     ((guint16)*((guint8 *)p+0)<<8|  \
45                      (guint16)*((guint8 *)p+1)<<0))
46
47 #define pntohl(p)  ((guint32)*((guint8 *)p+0)<<24|  \
48                     (guint32)*((guint8 *)p+1)<<16|  \
49                     (guint32)*((guint8 *)p+2)<<8|   \
50                     (guint32)*((guint8 *)p+3)<<0)
51
52 #define pletohs(p) ((guint16)                       \
53                     ((guint16)*((guint8 *)p+1)<<8|  \
54                      (guint16)*((guint8 *)p+0)<<0))
55
56 #define pletohl(p) ((guint32)*((guint8 *)p+3)<<24|  \
57                     (guint32)*((guint8 *)p+2)<<16|  \
58                     (guint32)*((guint8 *)p+1)<<8|   \
59                     (guint32)*((guint8 *)p+0)<<0)
60
61
62 #define hi_nibble(b) ((b & 0xf0) >> 4)
63 #define lo_nibble(b) (b & 0x0f)
64
65 /* Useful when you have an array whose size you can tell at compile-time */
66 #define array_length(x) (sizeof x / sizeof x[0])
67
68
69 /* Useful when highlighting regions inside a dissect_*() function. With this
70  * macro, you can highlight from an arbitrary offset to the end of the
71  * packet (which may come before the end of the frame).
72  * See dissect_data() for an example.
73  */
74 #define END_OF_FRAME    (pi.captured_len - offset)
75
76 /* Check whether the "len" bytes of data starting at "offset" is
77  * entirely inside the captured data for this packet. */
78 #define BYTES_ARE_IN_FRAME(offset, len) ((offset) + (len) <= pi.captured_len)
79
80 /* Check whether there's any data at all starting at "offset". */
81 #define IS_DATA_IN_FRAME(offset)        ((offset) < pi.captured_len)
82                 
83 /* To pass one of two strings, singular or plural */
84 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
85
86 typedef struct _column_info {
87   gint       num_cols;  /* Number of columns */
88   gint      *col_fmt;   /* Format of column */
89   gboolean **fmt_matx;  /* Specifies which formats apply to a column */
90   gint      *col_width; /* Column widths to use during a "-S" capture */
91   gchar    **col_title; /* Column titles */
92   gchar    **col_data;  /* Column data */
93 } column_info;
94
95 #define COL_MAX_LEN 256
96 #define COL_MAX_INFO_LEN 4096
97
98 typedef struct _packet_counts {
99   gint           tcp;
100   gint           udp;
101   gint           icmp;
102   gint           ospf;
103   gint           gre;
104   gint           netbios;
105   gint           ipx;
106   gint           vines;
107   gint           other;
108   gint           total;
109 } packet_counts;
110
111 /* Types of character encodings */
112 typedef enum {
113         CHAR_ASCII,     /* ASCII */
114         CHAR_EBCDIC     /* EBCDIC */
115 } char_enc;
116
117 /* XXX - some of this stuff is used only while a packet is being dissected;
118    should we keep around a separate data structure for that, to save
119    memory?
120
121    Also, should the pseudo-header be supplied by Wiretap when you do a
122    seek-and-read, so that we don't have to save it for all frames? */
123 typedef struct _frame_data {
124   struct _frame_data *next; /* Next element in list */
125   struct _frame_data *prev; /* Previous element in list */
126   guint32      num;       /* Frame number */
127   guint32      pkt_len;   /* Packet length */
128   guint32      cap_len;   /* Amount actually captured */
129   guint32      rel_secs;  /* Relative seconds */
130   guint32      rel_usecs; /* Relative microseconds */
131   guint32      abs_secs;  /* Absolute seconds */
132   guint32      abs_usecs; /* Absolute microseconds */
133   guint32      del_secs;  /* Delta seconds */
134   guint32      del_usecs; /* Delta microseconds */
135   long         file_off;  /* File offset */
136   column_info *cinfo;     /* Column formatting information */
137   gint         row;       /* Row number for this packet in the display */
138   int          lnk_t;     /* Per-packet encapsulation/data-link type */
139   gboolean     passed_dfilter; /* TRUE = display, FALSE = no display */
140   char_enc     encoding;  /* Character encoding (ASCII, EBCDIC...) */
141   union pseudo_header pseudo_header; /* "pseudo-header" from wiretap */
142 } frame_data;
143
144 /* Types of addresses Ethereal knows about. */
145 typedef enum {
146   AT_NONE,              /* no link-layer address */
147   AT_ETHER,             /* MAC (Ethernet, 802.x, FDDI) address */
148   AT_IPv4,              /* IPv4 */
149   AT_IPv6,              /* IPv6 */
150   AT_IPX,               /* IPX */
151   AT_SNA,               /* SNA */
152   AT_ATALK,             /* Appletalk DDP */
153   AT_VINES              /* Banyan Vines */
154 } address_type;
155
156 typedef struct _address {
157   address_type  type;           /* type of address */
158   int           len;            /* length of address, in bytes */
159   const guint8 *data;           /* bytes that constitute address */
160 } address;
161
162 #define SET_ADDRESS(addr, addr_type, addr_len, addr_data) { \
163         (addr)->type = (addr_type); \
164         (addr)->len = (addr_len); \
165         (addr)->data = (addr_data); \
166         }
167
168 /* Types of port numbers Ethereal knows about. */
169 typedef enum {
170   PT_NONE,              /* no port number */
171   PT_TCP,               /* TCP */
172   PT_UDP                /* UDP */
173 } port_type;
174
175 typedef struct _packet_info {
176   int     len;
177   int     captured_len;
178   address dl_src;               /* link-layer source address */
179   address dl_dst;               /* link-layer destination address */
180   address net_src;              /* network-layer source address */
181   address net_dst;              /* network-layer destination address */
182   address src;                  /* source address (net if present, DL otherwise )*/
183   address dst;                  /* destination address (net if present, DL otherwise )*/
184   guint32 ipproto;
185   port_type ptype;              /* type of the following two port numbers */
186   guint32 srcport;              /* source port */
187   guint32 destport;             /* destination port */
188   guint32 match_port;
189   int     iplen;
190   int     iphdrlen;
191 } packet_info;
192
193 extern packet_info pi;
194
195 /* Struct for the match_strval function */
196
197 typedef struct _value_string {
198   guint32  value;
199   gchar   *strptr;
200 } value_string;
201
202 /* Struct for boolean enumerations */
203 typedef struct true_false_string {
204         char    *true_string;
205         char    *false_string;
206 } true_false_string;
207
208
209 /* Many of the structs and definitions below and in packet-*.c files
210  * were taken from include files in the Linux distribution. */
211
212 typedef struct tcp_extra_data {
213   int match_port;
214   int sport;
215   int dport;
216 } tcp_extra_data;
217
218 /* Utility routines used by packet*.c */
219 gchar*     ether_to_str(const guint8 *);
220 gchar*     ether_to_str_punct(const guint8 *, char);
221 gchar*     ip_to_str(const guint8 *);
222 struct e_in6_addr;
223 gchar*     ip6_to_str(struct e_in6_addr *);
224 gchar*     ipx_addr_to_str(guint32, const guint8 *);
225 gchar*     abs_time_to_str(struct timeval*);
226 gchar*     rel_time_to_str(struct timeval*);
227 gchar*     time_secs_to_str(guint32);
228 gchar*     bytes_to_str(const guint8 *, int);
229 const u_char *find_line_end(const u_char *data, const u_char *dataend,
230     const u_char **eol);
231 int        get_token_len(const u_char *linep, const u_char *lineend,
232     const u_char **next_token);
233 gchar*     format_text(const u_char *line, int len);
234 gchar*     val_to_str(guint32, const value_string *, const char *);
235 gchar*     match_strval(guint32, const value_string*);
236 char * decode_bitfield_value(char *buf, guint32 val, guint32 mask, int width);
237 const char *decode_boolean_bitfield(guint32 val, guint32 mask, int width,
238   const char *truedesc, const char *falsedesc);
239 const char *decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
240   const value_string *tab, const char *fmt);
241 const char *decode_numeric_bitfield(guint32 val, guint32 mask, int width,
242   const char *fmt);
243 gint       check_col(frame_data *, gint);
244 #if __GNUC__ == 2
245 void       col_add_fstr(frame_data *, gint, gchar *, ...)
246     __attribute__((format (printf, 3, 4)));
247 void       col_append_fstr(frame_data *, gint, gchar *, ...)
248     __attribute__((format (printf, 3, 4)));
249 #else
250 void       col_add_fstr(frame_data *, gint, gchar *, ...);
251 void       col_append_fstr(frame_data *, gint, gchar *, ...);
252 #endif
253 void       col_add_str(frame_data *, gint, const gchar *);
254 void       col_append_str(frame_data *, gint, gchar *);
255 void       col_set_cls_time(frame_data *, int);
256 void       fill_in_columns(frame_data *);
257
258 void blank_packetinfo(void);
259
260 /* Allow protocols to register "init" routines, which are called before
261    we make a pass through a capture file and dissect all its packets
262    (e.g., when we read in a new capture file, or run a "filter packets"
263    or "colorize packets" pass over the current capture file). */
264 void register_init_routine(void (*func)(void));
265
266 /* Call all the registered "init" routines. */
267 void init_all_protocols(void);
268
269 void dissect_packet(const u_char *, frame_data *, proto_tree *);
270
271 /*
272  * Routines in packet-*.c
273  * Routines should take two args: packet data *, packet_counts *
274  * They should never modify the packet data.
275  */
276 void capture_clip(const u_char *, packet_counts *);
277 void capture_fddi(const u_char *, packet_counts *);
278 void capture_null(const u_char *, packet_counts *);
279 void capture_ppp(const u_char *, packet_counts *);
280 void capture_raw(const u_char *, packet_counts *);
281
282 /*
283  * Routines in packet-*.c
284  * Routines should take three args: packet data *, offset, packet_counts *
285  * They should never modify the packet data.
286  */
287 void capture_eth(const u_char *, int, packet_counts *);
288 void capture_ip(const u_char *, int, packet_counts *);
289 void capture_ipx(const u_char *, int, packet_counts *);
290 void capture_llc(const u_char *, int, packet_counts *);
291 void capture_netbios(const u_char *, int, packet_counts *);
292 void capture_tr(const u_char *, int, packet_counts *);
293 void capture_vines(const u_char *, int, packet_counts *);
294 void capture_vlan(const u_char *, int, packet_counts *);
295
296 /*
297  * Routines in packet-*.c
298  * Routines should take three args: packet data *, frame_data *, tree *
299  * They should never modify the packet data.
300  */
301 void dissect_ascend(const u_char *, frame_data *, proto_tree *);
302 void dissect_atm(const u_char *, frame_data *, proto_tree *);
303 void dissect_clip(const u_char *, frame_data *, proto_tree *);
304 void dissect_lapb(const u_char *, frame_data *, proto_tree *);
305 void dissect_lapd(const u_char *, frame_data *, proto_tree *);
306 void dissect_null(const u_char *, frame_data *, proto_tree *);
307 void dissect_ppp(const u_char *, frame_data *, proto_tree *);
308 void dissect_raw(const u_char *, frame_data *, proto_tree *);
309 void dissect_v120(const u_char *, frame_data *, proto_tree *);
310
311 /*
312  * Routines in packet-*.c
313  * Routines should take four args: packet data *, frame_data *, tree *,
314  * gboolean
315  * They should never modify the packet data.
316  */
317 void dissect_fddi(const u_char *, frame_data *, proto_tree *, gboolean);
318
319 typedef void    (*DissectFunc)  (const u_char*, int, frame_data*, proto_tree*);
320
321 /*
322  * Routines in packet-*.c
323  * Routines should take four args: packet data *, offset, frame_data *,
324  * tree *
325  * They should never modify the packet data.
326  */
327 int dissect_ah(const u_char *, int, frame_data *, proto_tree *);
328 void dissect_aarp(const u_char *, int, frame_data *, proto_tree *);
329 void dissect_afs(const u_char *, int, frame_data *, proto_tree *);
330 void dissect_arp(const u_char *, int, frame_data *, proto_tree *);
331 void dissect_auto_rp(const u_char *, int, frame_data *, proto_tree *);
332 void dissect_bgp(const u_char *, int, frame_data *, proto_tree *);
333 void dissect_bootp(const u_char *, int, frame_data *, proto_tree *);
334 void dissect_bpdu(const u_char *, int, frame_data *, proto_tree *);
335 void dissect_cdp(const u_char *, int, frame_data *, proto_tree *);
336 void dissect_cotp(const u_char *, int, frame_data *, proto_tree *);
337 void dissect_data(const u_char *, int, frame_data *, proto_tree *);
338 void dissect_ddp(const u_char *, int, frame_data *, proto_tree *);
339 void dissect_dns(const u_char *, int, frame_data *, proto_tree *);
340 void dissect_eigrp(const u_char *, int, frame_data *, proto_tree *);            
341 void dissect_esp(const u_char *, int, frame_data *, proto_tree *);
342 void dissect_eth(const u_char *, int, frame_data *, proto_tree *);
343 void dissect_ftp(const u_char *, int, frame_data *, proto_tree *);
344 void dissect_ftpdata(const u_char *, int, frame_data *, proto_tree *);
345 void dissect_giop(const u_char *, int, frame_data *, proto_tree *);
346 void dissect_hsrp(const u_char *, int, frame_data *, proto_tree *);
347 void dissect_http(const u_char *, int, frame_data *, proto_tree *);
348 void dissect_icmp(const u_char *, int, frame_data *, proto_tree *);
349 void dissect_icmpv6(const u_char *, int, frame_data *, proto_tree *);
350 void dissect_igmp(const u_char *, int, frame_data *, proto_tree *);
351 void dissect_ip(const u_char *, int, frame_data *, proto_tree *);
352 void dissect_ipcomp(const u_char *, int, frame_data *, proto_tree *);
353 void dissect_ipp(const u_char *, int, frame_data *, proto_tree *);
354 void dissect_ipv6(const u_char *, int, frame_data *, proto_tree *);
355 void dissect_ipx(const u_char *, int, frame_data *, proto_tree *);
356 void dissect_irc(const u_char *, int, frame_data *, proto_tree *);
357 void dissect_isis(const u_char *, int, frame_data *, proto_tree *);
358 void dissect_ldap(const u_char *, int, frame_data *, proto_tree *);
359 void dissect_llc(const u_char *, int, frame_data *, proto_tree *);
360 void dissect_lpd(const u_char *, int, frame_data *, proto_tree *);
361 void dissect_mapi(const u_char *, int, frame_data *, proto_tree *);
362 void dissect_nbdgm(const u_char *, int, frame_data *, proto_tree *);
363 void dissect_netbios(const u_char *, int, frame_data *, proto_tree *);
364 void dissect_nbipx(const u_char *, int, frame_data *, proto_tree *);
365 void dissect_nbns(const u_char *, int, frame_data *, proto_tree *);
366 void dissect_nbss(const u_char *, int, frame_data *, proto_tree *);
367 void dissect_ncp(const u_char *, int, frame_data *, proto_tree *);
368 void dissect_nntp(const u_char *, int, frame_data *, proto_tree *);
369 void dissect_ntp(const u_char *, int, frame_data *, proto_tree *);
370 void dissect_nwlink_dg(const u_char *, int, frame_data *, proto_tree *);
371 void dissect_osi(const u_char *, int, frame_data *, proto_tree *);
372 void dissect_ospf(const u_char *, int, frame_data *, proto_tree *);
373 void dissect_ospf_hello(const u_char *, int, frame_data *, proto_tree *);
374 void dissect_pim(const u_char *, int, frame_data *, proto_tree *);
375 void dissect_pop(const u_char *, int, frame_data *, proto_tree *);
376 void dissect_pppoed(const u_char *, int, frame_data *, proto_tree *);
377 void dissect_pppoes(const u_char *, int, frame_data *, proto_tree *);
378 void dissect_icp(const u_char *,int, frame_data *, proto_tree *);
379 void dissect_icq(const u_char *,int, frame_data *, proto_tree *);
380 void dissect_imap(const u_char *,int, frame_data *, proto_tree *);
381 void dissect_isakmp(const u_char *, int, frame_data *, proto_tree *);
382 void dissect_pim(const u_char *, int, frame_data *, proto_tree *);
383 void dissect_q931(const u_char *, int, frame_data *, proto_tree *);
384 void dissect_q2931(const u_char *, int, frame_data *, proto_tree *);
385 void dissect_radius(const u_char *, int, frame_data *, proto_tree *);
386 void dissect_l2tp(const u_char *, int, frame_data *, proto_tree *);
387 void dissect_rip(const u_char *, int, frame_data *, proto_tree *);
388 void dissect_ripng(const u_char *, int, frame_data *, proto_tree *);
389 void dissect_rsvp(const u_char *, int, frame_data *, proto_tree *);
390 void dissect_rtsp(const u_char *, int, frame_data *, proto_tree *);
391 void dissect_rx(const u_char *, int, frame_data *, proto_tree *);
392 void dissect_sap(const u_char *, int, frame_data *, proto_tree *);
393 void dissect_sdp(const u_char *, int, frame_data *, proto_tree *);
394 void dissect_sna(const u_char *, int, frame_data *, proto_tree *);
395 void dissect_snmp(const u_char *, int, frame_data *, proto_tree *);
396 void dissect_sscop(const u_char *, int, frame_data *, proto_tree *);
397 void dissect_tacacs(const u_char *, int, frame_data *, proto_tree *);
398 void dissect_tacplus(const u_char *, int, frame_data *, proto_tree *);
399 void dissect_tcp(const u_char *, int, frame_data *, proto_tree *);
400 void dissect_telnet(const u_char *, int, frame_data *, proto_tree *);
401 void dissect_tftp(const u_char *, int, frame_data *, proto_tree *);
402 void dissect_tns(const u_char *, int, frame_data *, proto_tree *);
403 void dissect_tr(const u_char *, int, frame_data *, proto_tree *);
404 void dissect_trmac(const u_char *, int, frame_data *, proto_tree *);
405 void dissect_udp(const u_char *, int, frame_data *, proto_tree *);
406 void dissect_vines(const u_char *, int, frame_data *, proto_tree *);
407 void dissect_vines_arp(const u_char *, int, frame_data *, proto_tree *);
408 void dissect_vines_frp(const u_char *, int, frame_data *, proto_tree *);
409 void dissect_vines_icp(const u_char *, int, frame_data *, proto_tree *);
410 void dissect_vines_ipc(const u_char *, int, frame_data *, proto_tree *);
411 void dissect_vines_rtp(const u_char *, int, frame_data *, proto_tree *);
412 void dissect_vines_spp(const u_char *, int, frame_data *, proto_tree *);
413 void dissect_vlan(const u_char *, int, frame_data *, proto_tree *);
414 void dissect_vrrp(const u_char *, int, frame_data *, proto_tree *);
415 void dissect_wccp(const u_char *, int, frame_data *, proto_tree *);
416 void dissect_who(const u_char *, int, frame_data *, proto_tree *);
417 void dissect_payload_ppp(const u_char *, int, frame_data *, proto_tree *);
418 void dissect_x25(const u_char *, int, frame_data *, proto_tree *);
419 void dissect_yhoo(const u_char *, int, frame_data *, proto_tree *);
420 void dissect_srvloc(const u_char *, int, frame_data *, proto_tree *);
421
422 void dissect_smb(const u_char *, int, frame_data *, proto_tree *, int);
423 void dissect_pptp(const u_char *, int, frame_data *, proto_tree *);
424 void dissect_gre(const u_char *, int, frame_data *, proto_tree *);
425
426 /*
427  * Routines in packet-*.c
428  * Routines should take four args: packet data *, offset, frame_data *,
429  * tree *
430  * They should never modify the packet data.
431  * They should return TRUE if the packet is of the type the routine would
432  * dissect, FALSE otherwise.
433  */
434 gboolean dissect_rpc(const u_char *, int, frame_data *, proto_tree *);
435
436 void init_dissect_rpc(void);
437 void init_dissect_udp(void);
438 void init_dissect_x25(void);
439
440 /* These functions are in ethertype.c */
441 void capture_ethertype(guint16 etype, int offset,
442                 const u_char *pd, packet_counts *ld);
443 void ethertype(guint16 etype, int offset,
444                 const u_char *pd, frame_data *fd, proto_tree *tree,
445                 proto_tree *fh_tree, int item_id);
446 extern const value_string etype_vals[];
447
448 /* ipproto.c */
449 extern const char *ipprotostr(int proto);
450
451 /*
452  * All of the possible columns in summary listing.
453  *
454  * NOTE: The SRC and DST entries MUST remain in this order, or else you
455  * need to fix the offset #defines before get_column_format!
456  */
457 enum {
458   COL_NUMBER,         /* Packet list item number */
459   COL_CLS_TIME,       /* Command line-specified time (default relative) */
460   COL_REL_TIME,       /* Relative time */
461   COL_ABS_TIME,       /* Absolute time */
462   COL_DELTA_TIME,     /* Delta time */
463   COL_DEF_SRC,        /* Source address */
464   COL_RES_SRC,        /* Resolved source */
465   COL_UNRES_SRC,      /* Unresolved source */
466   COL_DEF_DL_SRC,     /* Data link layer source address */
467   COL_RES_DL_SRC,     /* Resolved DL source */
468   COL_UNRES_DL_SRC,   /* Unresolved DL source */
469   COL_DEF_NET_SRC,    /* Network layer source address */
470   COL_RES_NET_SRC,    /* Resolved net source */
471   COL_UNRES_NET_SRC,  /* Unresolved net source */
472   COL_DEF_DST,        /* Destination address */
473   COL_RES_DST,        /* Resolved dest */
474   COL_UNRES_DST,      /* Unresolved dest */
475   COL_DEF_DL_DST,     /* Data link layer dest address */
476   COL_RES_DL_DST,     /* Resolved DL dest */
477   COL_UNRES_DL_DST,   /* Unresolved DL dest */
478   COL_DEF_NET_DST,    /* Network layer dest address */
479   COL_RES_NET_DST,    /* Resolved net dest */
480   COL_UNRES_NET_DST,  /* Unresolved net dest */
481   COL_DEF_SRC_PORT,   /* Source port */
482   COL_RES_SRC_PORT,   /* Resolved source port */
483   COL_UNRES_SRC_PORT, /* Unresolved source port */
484   COL_DEF_DST_PORT,   /* Destination port */
485   COL_RES_DST_PORT,   /* Resolved dest port */
486   COL_UNRES_DST_PORT, /* Unresolved dest port */
487   COL_PROTOCOL,       /* Protocol */
488   COL_INFO,           /* Description */
489   COL_PACKET_LENGTH,  /* Packet length in bytes */
490   NUM_COL_FMTS        /* Should always be last */
491 };
492
493 #endif /* packet.h */