Give "globals.h" an RCS ID and copyright/GPL notice.
[obnox/wireshark/wip.git] / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.95 1999/09/10 07:19:40 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 /* To pass one of two strings, singular or plural */
77 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
78
79 typedef struct _column_info {
80   gint       num_cols;  /* Number of columns */
81   gint      *col_fmt;   /* Format of column */
82   gboolean **fmt_matx;  /* Specifies which formats apply to a column */
83   gint      *col_width; /* Column widths to use during a "-S" capture */
84   gchar    **col_data;  /* Column data */
85 } column_info;
86
87 #define COL_MAX_LEN 256
88
89 typedef struct _packet_counts {
90   gint           tcp;
91   gint           udp;
92   gint           icmp;
93   gint           ospf;
94   gint           gre;
95   gint           netbios;
96   gint           other;
97   gint           total;
98 } packet_counts;
99
100 typedef struct _frame_data {
101   struct _frame_data *next; /* Next element in list */
102   guint32      pkt_len;   /* Packet length */
103   guint32      cap_len;   /* Amount actually captured */
104   guint32      rel_secs;  /* Relative seconds */
105   guint32      rel_usecs; /* Relative microseconds */
106   guint32      abs_secs;  /* Absolute seconds */
107   guint32      abs_usecs; /* Absolute microseconds */
108   guint32      del_secs;  /* Delta seconds */
109   guint32      del_usecs; /* Delta microseconds */
110   long         file_off;  /* File offset */
111   column_info *cinfo;     /* Column formatting information */
112   gint         row;       /* Row number for this packet in the display */
113   int          lnk_t;     /* Per-packet encapsulation/data-link type */
114   gboolean     passed_dfilter; /* TRUE = display, FALSE = no display */
115   union pseudo_header pseudo_header; /* "pseudo-header" from wiretap */
116 } frame_data;
117
118 typedef struct _packet_info {
119   int len;
120   int captured_len;
121   guint32 ip_src;
122   guint32 ip_dst;
123   guint32 ipproto;
124   guint32 srcport;
125   guint32 destport;
126   guint32 match_port;
127   int iplen;
128   int iphdrlen;
129 } packet_info;
130
131 extern packet_info pi;
132
133 /* Struct for the match_strval function */
134
135 typedef struct _value_string {
136   guint32  value;
137   gchar   *strptr;
138 } value_string;
139
140 /* Many of the structs and definitions below and in packet-*.c files
141  * were taken from include files in the Linux distribution. */
142
143 typedef struct tcp_extra_data {
144   int match_port;
145   int sport;
146   int dport;
147 } tcp_extra_data;
148
149 /* Tree types.  Each dissect_* routine should have one for each
150    add_subtree() call. */
151
152 enum {
153         ETT_NONE,
154         ETT_FRAME,
155         ETT_IEEE8023,
156         ETT_ETHER2,
157         ETT_LLC,
158         ETT_TOKEN_RING,
159         ETT_TOKEN_RING_AC,
160         ETT_TOKEN_RING_FC,
161         ETT_TR_IERR_CNT,
162         ETT_TR_NERR_CNT,
163         ETT_TR_MAC,
164         ETT_PPP,
165         ETT_ARP,
166         ETT_FDDI,
167         ETT_NULL,
168         ETT_IP,
169         ETT_IP_OPTIONS,
170         ETT_IP_OPTION_SEC,
171         ETT_IP_OPTION_ROUTE,
172         ETT_IP_OPTION_TIMESTAMP,
173         ETT_IP_TOS,
174         ETT_IP_OFF,
175         ETT_UDP,
176         ETT_TCP,
177         ETT_TCP_OPTIONS,
178         ETT_TCP_OPTION_SACK,
179         ETT_TCP_FLAGS,
180         ETT_ICMP,
181         ETT_IGMP,
182         ETT_IPX,
183         ETT_SPX,
184         ETT_NCP,
185         ETT_NCP_REQUEST_FIELDS,
186         ETT_NCP_REPLY_FIELDS,
187         ETT_DNS,
188         ETT_DNS_FLAGS,
189         ETT_DNS_QRY,
190         ETT_DNS_QD,
191         ETT_DNS_ANS,
192         ETT_DNS_RR,
193         ETT_ISAKMP,
194         ETT_ISAKMP_FLAGS,
195         ETT_ISAKMP_PAYLOAD,
196         ETT_RIP,
197         ETT_RIP_VEC,
198         ETT_OSPF,
199         ETT_OSPF_HDR,
200         ETT_OSPF_HELLO,
201         ETT_OSPF_DESC,
202         ETT_OSPF_LSR,
203         ETT_OSPF_LSA_UPD,
204         ETT_OSPF_LSA,
205         ETT_LPD,
206         ETT_RAW,
207         ETT_CLIP,
208         ETT_BOOTP,
209         ETT_BOOTP_OPTION,
210         ETT_IPv6,
211         ETT_CLNP,
212         ETT_COTP,
213         ETT_VINES_FRP,
214         ETT_VINES,
215         ETT_VINES_ARP,
216         ETT_VINES_ICP,
217         ETT_VINES_IPC,
218         ETT_VINES_RTP,
219         ETT_VINES_SPP,
220         ETT_IPXRIP,
221         ETT_IPXSAP,
222         ETT_IPXSAP_SERVER,
223         ETT_NBNS,
224         ETT_NBNS_FLAGS,
225         ETT_NBNS_NB_FLAGS,
226         ETT_NBNS_NAME_FLAGS,
227         ETT_NBNS_QRY,
228         ETT_NBNS_QD,
229         ETT_NETB,
230         ETT_NETB_FLAGS,
231         ETT_NETB_STATUS,
232         ETT_NETB_NAME,
233         ETT_NBNS_ANS,
234         ETT_NBNS_RR,
235         ETT_NBIPX,
236         ETT_NBIPX_NAME_TYPE_FLAGS,
237         ETT_AARP,
238         ETT_GIOP,
239         ETT_NBDGM,
240         ETT_CDP,
241         ETT_CDP_TLV,
242         ETT_HTTP,
243         ETT_TFTP,
244         ETT_AH,
245         ETT_ESP,
246         ETT_ICMPv6,
247         ETT_ICMPv6OPT,
248         ETT_ICMPv6FLAG,
249         ETT_POP,
250         ETT_FTP,
251         ETT_TELNET,
252         ETT_TELNET_SUBOPT,
253         ETT_NNTP,
254         ETT_SNMP,
255         ETT_NBSS,
256         ETT_NBSS_FLAGS,
257         ETT_SMB,
258         ETT_SMB_FLAGS,
259         ETT_SMB_FLAGS2,
260         ETT_SMB_DIALECTS,
261         ETT_SMB_MODE,
262         ETT_SMB_CAPABILITIES,
263         ETT_SMB_RAWMODE,
264         ETT_SMB_AFLAGS,
265         ETT_SMB_DESIREDACCESS,
266         ETT_SMB_SEARCH,
267         ETT_SMB_FILE,
268         ETT_SMB_OPENFUNCTION,
269         ETT_SMB_FILEATTRIBUTES,
270         ETT_SMB_FILETYPE,
271         ETT_SMB_ACTION,
272         ETT_PPTP,
273         ETT_GRE,
274         ETT_GRE_FLAGS,
275         ETT_PPPOED,
276         ETT_PPPOED_TAGS,
277         ETT_PPPOES,
278         ETT_LCP,
279         ETT_LCP_OPTIONS,
280         ETT_LCP_MRU_OPT,
281         ETT_LCP_ASYNC_MAP_OPT,
282         ETT_LCP_AUTHPROT_OPT,
283         ETT_LCP_QUALPROT_OPT,
284         ETT_LCP_MAGICNUM_OPT,
285         ETT_LCP_FCS_ALTERNATIVES_OPT,
286         ETT_LCP_NUMBERED_MODE_OPT,
287         ETT_LCP_CALLBACK_OPT,
288         ETT_LCP_MULTILINK_EP_DISC_OPT,
289         ETT_LCP_INTERNATIONALIZATION_OPT,
290         ETT_IPCP,
291         ETT_IPCP_OPTIONS,
292         ETT_IPCP_IPADDRS_OPT,
293         ETT_IPCP_COMPRESSPROT_OPT,
294         ETT_RSVP,
295         ETT_RSVP_UNKNOWN_CLASS,
296         ETT_RSVP_HDR,
297         ETT_RSVP_SESSION,
298         ETT_RSVP_SGROUP,
299         ETT_RSVP_HOP,
300         ETT_RSVP_INTEGRITY,
301         ETT_RSVP_TIME_VALUES,
302         ETT_RSVP_ERROR,
303         ETT_RSVP_SCOPE,
304         ETT_RSVP_STYLE,
305         ETT_RSVP_FLOWSPEC,
306         ETT_RSVP_FILTER_SPEC,
307         ETT_RSVP_SENDER_TEMPLATE,
308         ETT_RSVP_SENDER_TSPEC,
309         ETT_RSVP_ADSPEC,
310         ETT_RSVP_POLICY,
311         ETT_RSVP_CONFIRM,
312         ETT_RSVP_ADSPEC_SUBTREE1,
313         ETT_RSVP_ADSPEC_SUBTREE2,
314         ETT_RSVP_ADSPEC_SUBTREE3,
315         ETT_RTSP,
316         ETT_SDP,
317         ETT_RADIUS,
318         ETT_RADIUS_AVP,
319         ETT_LAPB,
320         ETT_X25,
321         ETT_XDLC_CONTROL,
322         ETT_ATM,
323         ETT_ATM_LANE,
324         ETT_ATM_LANE_LC_FLAGS,
325         ETT_ATM_LANE_LC_LAN_DEST,
326         ETT_ATM_LANE_LC_LAN_DEST_RD,
327         NUM_TREE_TYPES  /* last item number plus one */
328 };
329
330
331 /* Utility routines used by packet*.c */
332 gchar*     ether_to_str(const guint8 *);
333 gchar*     ip_to_str(const guint8 *);
334 gchar*     abs_time_to_str(struct timeval*);
335 gchar*     time_secs_to_str(guint32);
336 gchar*     bytes_to_str(const guint8 *, int);
337 const u_char *find_line_end(const u_char *data, const u_char *dataend,
338     const u_char **eol);
339 int        get_token_len(const u_char *linep, const u_char *lineend,
340     const u_char **next_token);
341 gchar*     format_text(const u_char *line, int len);
342 gchar*     val_to_str(guint32, const value_string *, const char *);
343 gchar*     match_strval(guint32, const value_string*);
344 const char *decode_boolean_bitfield(guint32 val, guint32 mask, int width,
345   const char *truedesc, const char *falsedesc);
346 const char *decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
347   const value_string *tab, const char *fmt);
348 const char *decode_numeric_bitfield(guint32 val, guint32 mask, int width,
349   const char *fmt);
350 gint       check_col(frame_data *, gint);
351 #if __GNUC__ == 2
352 void       col_add_fstr(frame_data *, gint, gchar *, ...)
353     __attribute__((format (printf, 3, 4)));
354 void       col_append_fstr(frame_data *, gint, gchar *, ...)
355     __attribute__((format (printf, 3, 4)));
356 #else
357 void       col_add_fstr(frame_data *, gint, gchar *, ...);
358 void       col_append_fstr(frame_data *, gint, gchar *, ...);
359 #endif
360 void       col_add_str(frame_data *, gint, const gchar *);
361 void       col_append_str(frame_data *, gint, gchar *);
362
363
364 void dissect_packet(const u_char *, frame_data *, proto_tree *);
365 /*
366  * Routines in packet-*.c
367  * Routines should take three args: packet data *, cap_len, packet_counts *
368  * They should never modify the packet data.
369  */
370 void capture_clip(const u_char *, guint32, packet_counts *);
371 void capture_eth(const u_char *, guint32, packet_counts *);
372 void capture_fddi(const u_char *, guint32, packet_counts *);
373 void capture_null(const u_char *, guint32, packet_counts *);
374 void capture_ppp(const u_char *, guint32, packet_counts *);
375 void capture_raw(const u_char *, guint32, packet_counts *);
376 void capture_tr(const u_char *, guint32, packet_counts *);
377
378 /*
379  * Routines in packet-*.c
380  * Routines should take four args: packet data *, offset, cap_len,
381  * packet_counts *
382  * They should never modify the packet data.
383  */
384 void capture_netbios(const u_char *, int, guint32, packet_counts *);
385 void capture_llc(const u_char *, int, guint32, packet_counts *);
386 void capture_ip(const u_char *, int, guint32, packet_counts *);
387
388 /*
389  * Routines in packet-*.c
390  * Routines should take three args: packet data *, frame_data *, tree *
391  * They should never modify the packet data.
392  */
393 void dissect_atm(const u_char *, frame_data *, proto_tree *);
394 void dissect_clip(const u_char *, frame_data *, proto_tree *);
395 void dissect_lapb(const u_char *, frame_data *, proto_tree *);
396 void dissect_null(const u_char *, frame_data *, proto_tree *);
397 void dissect_ppp(const u_char *, frame_data *, proto_tree *);
398 void dissect_raw(const u_char *, frame_data *, proto_tree *);
399
400 /*
401  * Routines in packet-*.c
402  * Routines should take four args: packet data *, frame_data *, tree *,
403  * gboolean
404  * They should never modify the packet data.
405  */
406 void dissect_fddi(const u_char *, frame_data *, proto_tree *, gboolean);
407
408 /*
409  * Routines in packet-*.c
410  * Routines should take four args: packet data *, offset, frame_data *,
411  * tree *
412  * They should never modify the packet data.
413  */
414 int dissect_ah(const u_char *, int, frame_data *, proto_tree *);
415 void dissect_aarp(const u_char *, int, frame_data *, proto_tree *);
416 void dissect_arp(const u_char *, int, frame_data *, proto_tree *);
417 void dissect_bootp(const u_char *, int, frame_data *, proto_tree *);
418 void dissect_cdp(const u_char *, int, frame_data *, proto_tree *);
419 void dissect_cotp(const u_char *, int, frame_data *, proto_tree *);
420 void dissect_data(const u_char *, int, frame_data *, proto_tree *);
421 void dissect_ddp(const u_char *, int, frame_data *, proto_tree *);
422 void dissect_dns(const u_char *, int, frame_data *, proto_tree *);
423 void dissect_esp(const u_char *, int, frame_data *, proto_tree *);
424 void dissect_eth(const u_char *, int, frame_data *, proto_tree *);
425 void dissect_ftp(const u_char *, int, frame_data *, proto_tree *);
426 void dissect_ftpdata(const u_char *, int, frame_data *, proto_tree *);
427 void dissect_giop(const u_char *, int, frame_data *, proto_tree *);
428 void dissect_http(const u_char *, int, frame_data *, proto_tree *);
429 void dissect_icmp(const u_char *, int, frame_data *, proto_tree *);
430 void dissect_icmpv6(const u_char *, int, frame_data *, proto_tree *);
431 void dissect_igmp(const u_char *, int, frame_data *, proto_tree *);
432 void dissect_ip(const u_char *, int, frame_data *, proto_tree *);
433 void dissect_ipv6(const u_char *, int, frame_data *, proto_tree *);
434 void dissect_ipx(const u_char *, int, frame_data *, proto_tree *);
435 void dissect_llc(const u_char *, int, frame_data *, proto_tree *);
436 void dissect_lpd(const u_char *, int, frame_data *, proto_tree *);
437 void dissect_nbdgm(const u_char *, int, frame_data *, proto_tree *);
438 void dissect_netbios(const u_char *, int, frame_data *, proto_tree *);
439 void dissect_nbipx(const u_char *, int, frame_data *, proto_tree *);
440 void dissect_nbns(const u_char *, int, frame_data *, proto_tree *);
441 void dissect_nbss(const u_char *, int, frame_data *, proto_tree *);
442 void dissect_ncp(const u_char *, int, frame_data *, proto_tree *);
443 void dissect_nntp(const u_char *, int, frame_data *, proto_tree *);
444 void dissect_nwlink_dg(const u_char *, int, frame_data *, proto_tree *);
445 void dissect_osi(const u_char *, int, frame_data *, proto_tree *);
446 void dissect_ospf(const u_char *, int, frame_data *, proto_tree *);
447 void dissect_ospf_hello(const u_char *, int, frame_data *, proto_tree *);
448 void dissect_pop(const u_char *, int, frame_data *, proto_tree *);
449 void dissect_pppoed(const u_char *, int, frame_data *, proto_tree *);
450 void dissect_pppoes(const u_char *, int, frame_data *, proto_tree *);
451 void dissect_isakmp(const u_char *, int, frame_data *, proto_tree *);
452 void dissect_radius(const u_char *, int, frame_data *, proto_tree *);
453 void dissect_rip(const u_char *, int, frame_data *, proto_tree *);
454 void dissect_rsvp(const u_char *, int, frame_data *, proto_tree *);
455 void dissect_rtsp(const u_char *, int, frame_data *, proto_tree *);
456 void dissect_sdp(const u_char *, int, frame_data *, proto_tree *);
457 void dissect_snmp(const u_char *, int, frame_data *, proto_tree *);
458 void dissect_tcp(const u_char *, int, frame_data *, proto_tree *);
459 void dissect_telnet(const u_char *, int, frame_data *, proto_tree *);
460 void dissect_tftp(const u_char *, int, frame_data *, proto_tree *);
461 void dissect_tr(const u_char *, int, frame_data *, proto_tree *);
462 void dissect_trmac(const u_char *, int, frame_data *, proto_tree *);
463 void dissect_udp(const u_char *, int, frame_data *, proto_tree *);
464 void dissect_vines(const u_char *, int, frame_data *, proto_tree *);
465 void dissect_vines_arp(const u_char *, int, frame_data *, proto_tree *);
466 void dissect_vines_frp(const u_char *, int, frame_data *, proto_tree *);
467 void dissect_vines_icp(const u_char *, int, frame_data *, proto_tree *);
468 void dissect_vines_ipc(const u_char *, int, frame_data *, proto_tree *);
469 void dissect_vines_rtp(const u_char *, int, frame_data *, proto_tree *);
470 void dissect_vines_spp(const u_char *, int, frame_data *, proto_tree *);
471 void dissect_payload_ppp(const u_char *, int, frame_data *, proto_tree *);
472 void dissect_x25(const u_char *, int, frame_data *, proto_tree *);
473
474 void dissect_smb(const u_char *, int, frame_data *, proto_tree *, int);
475 void dissect_pptp(const u_char *, int, frame_data *, proto_tree *);
476 void dissect_gre(const u_char *, int, frame_data *, proto_tree *);
477
478 void init_dissect_udp(void);
479 void init_dissect_x25(void);
480
481 /* These functions are in ethertype.c */
482 void capture_ethertype(guint16 etype, int offset,
483                 const u_char *pd, guint32 cap_len, packet_counts *ld);
484 void ethertype(guint16 etype, int offset,
485                 const u_char *pd, frame_data *fd, proto_tree *tree,
486                 proto_tree *fh_tree, int item_id);
487 extern const value_string etype_vals[];
488
489 /* These functions are in packet-arp.c */
490 gchar *arphrdaddr_to_str(guint8 *ad, int ad_len, guint16 type);
491 gchar *arphrdtype_to_str(guint16 hwtype, const char *fmt);
492
493 /*
494  * All of the possible columns in summary listing.
495  *
496  * NOTE: The SRC and DST entries MUST remain in this order, or else you
497  * need to fix the offset #defines before get_column_format!
498  */
499 enum {
500   COL_NUMBER,         /* Packet list item number */
501   COL_CLS_TIME,       /* Command line-specified time (default relative) */
502   COL_REL_TIME,       /* Relative time */
503   COL_ABS_TIME,       /* Absolute time */
504   COL_DELTA_TIME,     /* Delta time */
505   COL_DEF_SRC,        /* Source address */
506   COL_RES_SRC,        /* Resolved source */
507   COL_UNRES_SRC,      /* Unresolved source */
508   COL_DEF_DL_SRC,     /* Data link layer source address */
509   COL_RES_DL_SRC,     /* Resolved DL source */
510   COL_UNRES_DL_SRC,   /* Unresolved DL source */
511   COL_DEF_NET_SRC,    /* Network layer source address */
512   COL_RES_NET_SRC,    /* Resolved net source */
513   COL_UNRES_NET_SRC,  /* Unresolved net source */
514   COL_DEF_DST,        /* Destination address */
515   COL_RES_DST,        /* Resolved dest */
516   COL_UNRES_DST,      /* Unresolved dest */
517   COL_DEF_DL_DST,     /* Data link layer dest address */
518   COL_RES_DL_DST,     /* Resolved DL dest */
519   COL_UNRES_DL_DST,   /* Unresolved DL dest */
520   COL_DEF_NET_DST,    /* Network layer dest address */
521   COL_RES_NET_DST,    /* Resolved net dest */
522   COL_UNRES_NET_DST,  /* Unresolved net dest */
523   COL_DEF_SRC_PORT,   /* Source port */
524   COL_RES_SRC_PORT,   /* Resolved source port */
525   COL_UNRES_SRC_PORT, /* Unresolved source port */
526   COL_DEF_DST_PORT,   /* Destination port */
527   COL_RES_DST_PORT,   /* Resolved dest port */
528   COL_UNRES_DST_PORT, /* Unresolved dest port */
529   COL_PROTOCOL,       /* Protocol */
530   COL_INFO,           /* Description */
531   COL_PACKET_LENGTH,  /* Packet length in bytes */
532   NUM_COL_FMTS        /* Should always be last */
533 };
534
535 #endif /* packet.h */