Fixed some serious bugs in the NCP hash routines. I also simplified
[obnox/wireshark/wip.git] / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.56 1999/05/13 16:42:43 gram 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 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
31  * byte array, returns the value of the two or four bytes at the pointer.
32  * The pletoh[sl] versions return the little-endian representation.
33  */
34
35 #define pntohs(p)  ((guint16)                       \
36                     ((guint16)*((guint8 *)p+0)<<8|  \
37                      (guint16)*((guint8 *)p+1)<<0))
38
39 #define pntohl(p)  ((guint32)*((guint8 *)p+0)<<24|  \
40                     (guint32)*((guint8 *)p+1)<<16|  \
41                     (guint32)*((guint8 *)p+2)<<8|   \
42                     (guint32)*((guint8 *)p+3)<<0)
43
44 #define pletohs(p) ((guint16)                       \
45                     ((guint16)*((guint8 *)p+1)<<8|  \
46                      (guint16)*((guint8 *)p+0)<<0))
47
48 #define pletohl(p) ((guint32)*((guint8 *)p+3)<<24|  \
49                     (guint32)*((guint8 *)p+2)<<16|  \
50                     (guint32)*((guint8 *)p+1)<<8|   \
51                     (guint32)*((guint8 *)p+0)<<0)
52
53
54 #define hi_nibble(b) ((b & 0xf0) >> 4)
55 #define lo_nibble(b) (b & 0x0f)
56
57 /* Byte ordering */
58 #ifndef BYTE_ORDER
59   #define LITTLE_ENDIAN 4321
60   #define BIG_ENDIAN 1234
61   #ifdef WORDS_BIGENDIAN
62     #define BYTE_ORDER BIG_ENDIAN
63   #else
64     #define BYTE_ORDER LITTLE_ENDIAN
65   #endif
66 #endif
67
68 /* Useful when highlighting regions inside a dissect_*() function. With this
69  * macro, you can highlight from an arbitrary offset to the end of the
70  * frame. See dissect_data() for an example.
71  */
72 #define END_OF_FRAME    (fd->cap_len - offset)
73
74
75 typedef struct _column_info {
76   gint       num_cols; /* Number of columns */
77   gboolean **fmt_matx; /* Specifies which formats apply to a column */
78   gchar    **col_data; /* Column data */
79 } column_info;
80
81 #define COL_MAX_LEN 256
82
83 typedef struct _packet_counts {
84   gint           tcp;
85   gint           udp;
86   gint           ospf;
87   gint           other;
88   gint           total;
89 } packet_counts;
90
91 typedef struct _frame_data {
92   guint32      pkt_len;   /* Packet length */
93   guint32      cap_len;   /* Amount actually captured */
94   guint32      rel_secs;  /* Relative seconds */
95   guint32      rel_usecs; /* Relative microseconds */
96   guint32      abs_secs;  /* Absolute seconds */
97   guint32      abs_usecs; /* Absolute microseconds */
98   guint32      del_secs;  /* Delta seconds */
99   guint32      del_usecs; /* Delta microseconds */
100   long         file_off;  /* File offset */
101   column_info *cinfo;     /* Column formatting information */
102 #ifdef WITH_WIRETAP
103   int           lnk_t;     /* Per-packet encapsulation/data-link type */
104 #endif
105 } frame_data;
106
107 typedef struct _packet_info {
108   char *srcip;
109   int ip_src;
110   char *destip;
111   int ipproto;
112   int srcport;
113   int destport;
114   int match_port;
115   int iplen;
116   int iphdrlen;
117   int payload;
118 } packet_info;
119
120 /* Struct for the match_strval function */
121
122 typedef struct _value_string {
123   guint32  value;
124   gchar   *strptr;
125 } value_string;
126
127 /* Many of the structs and definitions below and in packet-*.c files
128  * were taken from include files in the Linux distribution. */
129
130 typedef struct tcp_extra_data {
131   int match_port;
132   int sport;
133   int dport;
134 } tcp_extra_data;
135
136 /* Tree types.  Each dissect_* routine should have one for each
137    add_subtree() call. */
138
139 enum {
140         ETT_FRAME,
141         ETT_IEEE8023,
142         ETT_ETHER2,
143         ETT_LLC,
144         ETT_TOKEN_RING,
145         ETT_TR_IERR_CNT,
146         ETT_TR_NERR_CNT,
147         ETT_TR_MAC,
148         ETT_PPP,
149         ETT_ARP,
150         ETT_FDDI,
151         ETT_NULL,
152         ETT_IP,
153         ETT_IP_OPTIONS,
154         ETT_IP_OPTION_SEC,
155         ETT_IP_OPTION_ROUTE,
156         ETT_IP_OPTION_TIMESTAMP,
157         ETT_IP_TOS,
158         ETT_IP_OFF,
159         ETT_UDP,
160         ETT_TCP,
161         ETT_TCP_OPTIONS,
162         ETT_TCP_OPTION_SACK,
163         ETT_TCP_FLAGS,
164         ETT_ICMP,
165         ETT_IGMP,
166         ETT_IPX,
167         ETT_SPX,
168         ETT_NCP,
169         ETT_NCP_REQUEST_FIELDS,
170         ETT_NCP_REPLY_FIELDS,
171         ETT_DNS,
172         ETT_DNS_FLAGS,
173         ETT_DNS_QRY,
174         ETT_DNS_QD,
175         ETT_DNS_ANS,
176         ETT_DNS_RR,
177         ETT_RIP,
178         ETT_RIP_VEC,
179         ETT_OSPF,
180         ETT_OSPF_HDR,
181         ETT_OSPF_HELLO,
182         ETT_OSPF_DESC,
183         ETT_OSPF_LSR,
184         ETT_OSPF_LSA_UPD,
185         ETT_OSPF_LSA,
186         ETT_LPD,
187         ETT_RAW,
188         ETT_BOOTP,
189         ETT_BOOTP_OPTION,
190         ETT_IPv6,
191         ETT_CLNP,
192         ETT_COTP,
193         ETT_VINES_FRP,
194         ETT_VINES,
195         ETT_VINES_ARP,
196         ETT_VINES_ICP,
197         ETT_VINES_IPC,
198         ETT_VINES_RTP,
199         ETT_VINES_SPP,
200         ETT_IPXRIP,
201         ETT_IPXSAP,
202         ETT_IPXSAP_SERVER,
203         ETT_NBNS,
204         ETT_NBNS_FLAGS,
205         ETT_NBNS_NB_FLAGS,
206         ETT_NBNS_NAME_FLAGS,
207         ETT_NBNS_QRY,
208         ETT_NBNS_QD,
209         ETT_NBNS_ANS,
210         ETT_NBNS_RR,
211         ETT_NBIPX,
212         ETT_AARP,
213         ETT_GIOP,
214         ETT_NBDGM,
215         ETT_CDP,
216         ETT_HTTP,
217         ETT_TFTP,
218         ETT_AH,
219         ETT_ESP,
220         ETT_ICMPv6,
221         ETT_ICMPv6OPT,
222         ETT_ICMPv6FLAG,
223         ETT_POP,
224         ETT_FTP,
225         ETT_TELNET,
226         ETT_TELNET_SUBOPT,
227         ETT_NNTP,
228         ETT_SNMP,
229         ETT_NBSS,
230         ETT_NBSS_FLAGS,
231         ETT_SMB,
232         ETT_SMB_FLAGS,
233         ETT_SMB_FLAGS2,
234         ETT_SMB_DIALECTS,
235         ETT_SMB_MODE,
236         ETT_SMB_CAPS,
237         ETT_SMB_RAWMODE,
238         ETT_SMB_AFLAGS,
239         NUM_TREE_TYPES  /* last item number plus one */
240 };
241
242 /* The version of pcap.h that comes with some systems is missing these
243  * #defines.
244  */
245
246 #ifndef DLT_RAW
247 #define DLT_RAW 12
248 #endif
249
250 #ifndef DLT_SLIP_BSDOS
251 #define DLT_SLIP_BSDOS 13
252 #endif
253
254 #ifndef DLT_PPP_BSDOS
255 #define DLT_PPP_BSDOS 14
256 #endif
257
258
259 /* Utility routines used by packet*.c */
260 gchar*     ether_to_str(const guint8 *);
261 gchar*     ip_to_str(const guint8 *);
262 gchar*     time_secs_to_str(guint32);
263 gchar*     bytes_to_str(const guint8 *, int);
264 const u_char *find_line_end(const u_char *data, const u_char *dataend,
265     const u_char **eol);
266 int        get_token_len(const u_char *linep, const u_char *lineend,
267     const u_char **next_token);
268 gchar*     format_text(const u_char *line, int len);
269 gchar*     val_to_str(guint32, const value_string *, const char *);
270 gchar*     match_strval(guint32, const value_string*);
271 const char *decode_boolean_bitfield(guint32 val, guint32 mask, int width,
272   const char *truedesc, const char *falsedesc);
273 const char *decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
274   const value_string *tab, const char *fmt);
275 const char *decode_numeric_bitfield(guint32 val, guint32 mask, int width,
276   const char *fmt);
277 gint       check_col(frame_data *, gint);
278 #if __GNUC__ == 2
279 void       col_add_fstr(frame_data *, gint, gchar *, ...)
280     __attribute__((format (printf, 3, 4)));
281 void       col_append_fstr(frame_data *, gint, gchar *, ...)
282     __attribute__((format (printf, 3, 4)));
283 #else
284 void       col_add_fstr(frame_data *, gint, gchar *, ...);
285 void       col_append_fstr(frame_data *, gint, gchar *, ...);
286 #endif
287 void       col_add_str(frame_data *, gint, gchar *);
288 void       col_append_str(frame_data *, gint, gchar *);
289
290 /* Routines in packet.c */
291
292 typedef struct GtkWidget proto_tree;
293 typedef struct GtkWidget proto_item;
294
295 struct GtkWidget;
296
297 void proto_item_set_len(proto_item *ti, gint len);
298 proto_tree* proto_tree_new(void);
299 void proto_item_add_subtree(proto_item *ti, proto_tree *subtree, gint idx);
300
301 #if __GNUC__ == 2
302 proto_item* proto_tree_add_item(proto_tree *tree, gint start, gint len,
303                 gchar *format, ...)
304     __attribute__((format (printf, 4, 5)));
305 #else
306 proto_item* proto_tree_add_item(proto_tree *tree, gint start, gint len,
307                 gchar *format, ...);
308 #endif
309
310 void dissect_packet(const u_char *, frame_data *, proto_tree *);
311 /*
312  * Routines in packet-*.c
313  * Routines should take three args: packet data *, cap_len, packet_counts *
314  * They should never modify the packet data.
315  */
316 void capture_eth(const u_char *, guint32, packet_counts *);
317 void capture_fddi(const u_char *, guint32, packet_counts *);
318 void capture_null(const u_char *, guint32, packet_counts *);
319 void capture_ppp(const u_char *, guint32, packet_counts *);
320 void capture_raw(const u_char *, guint32, packet_counts *);
321 void capture_tr(const u_char *, guint32, packet_counts *);
322
323 /*
324  * Routines in packet-*.c
325  * Routines should take four args: packet data *, offset, cap_len,
326  * packet_counts *
327  * They should never modify the packet data.
328  */
329 void capture_llc(const u_char *, int, guint32, packet_counts *);
330 void capture_ip(const u_char *, int, guint32, packet_counts *);
331
332 /*
333  * Routines in packet-*.c
334  * Routines should take three args: packet data *, frame_data *, tree *
335  * They should never modify the packet data.
336  */
337 void dissect_eth(const u_char *, frame_data *, proto_tree *);
338 void dissect_fddi(const u_char *, frame_data *, proto_tree *);
339 void dissect_null(const u_char *, frame_data *, proto_tree *);
340 void dissect_ppp(const u_char *, frame_data *, proto_tree *);
341 void dissect_raw(const u_char *, frame_data *, proto_tree *);
342 void dissect_tr(const u_char *, frame_data *, proto_tree *);
343
344 /*
345  * Routines in packet-*.c
346  * Routines should take four args: packet data *, offset, frame_data *,
347  * tree *
348  * They should never modify the packet data.
349  */
350 int dissect_ah(const u_char *, int, frame_data *, proto_tree *);
351 void dissect_aarp(const u_char *, int, frame_data *, proto_tree *);
352 void dissect_arp(const u_char *, int, frame_data *, proto_tree *);
353 void dissect_bootp(const u_char *, int, frame_data *, proto_tree *);
354 void dissect_cdp(const u_char *, int, frame_data *, proto_tree *);
355 void dissect_data(const u_char *, int, frame_data *, proto_tree *);
356 void dissect_ddp(const u_char *, int, frame_data *, proto_tree *);
357 void dissect_dns(const u_char *, int, frame_data *, proto_tree *);
358 void dissect_esp(const u_char *, int, frame_data *, proto_tree *);
359 void dissect_giop(const u_char *, int, frame_data *, proto_tree *);
360 void dissect_http(const u_char *, int, frame_data *, proto_tree *);
361 void dissect_icmp(const u_char *, int, frame_data *, proto_tree *);
362 void dissect_icmpv6(const u_char *, int, frame_data *, proto_tree *);
363 void dissect_igmp(const u_char *, int, frame_data *, proto_tree *);
364 void dissect_ip(const u_char *, int, frame_data *, proto_tree *);
365 void dissect_ipv6(const u_char *, int, frame_data *, proto_tree *);
366 void dissect_ipx(const u_char *, int, frame_data *, proto_tree *);
367 void dissect_llc(const u_char *, int, frame_data *, proto_tree *);
368 void dissect_lpd(const u_char *, int, frame_data *, proto_tree *);
369 void dissect_nbdgm(const u_char *, int, frame_data *, proto_tree *, int);
370 void dissect_nbipx_ns(const u_char *, int, frame_data *, proto_tree *, int);
371 void dissect_nbns(const u_char *, int, frame_data *, proto_tree *);
372 void dissect_ncp(const u_char *, int, frame_data *, proto_tree *, int);
373 void dissect_nwlink_dg(const u_char *, int, frame_data *, proto_tree *, int);
374 void dissect_osi(const u_char *, int, frame_data *, proto_tree *);
375 void dissect_ospf(const u_char *, int, frame_data *, proto_tree *);
376 void dissect_ospf_hello(const u_char *, int, frame_data *, proto_tree *);
377 void dissect_rip(const u_char *, int, frame_data *, proto_tree *);
378 void dissect_snmp(const u_char *, int, frame_data *, proto_tree *);
379 void dissect_tcp(const u_char *, int, frame_data *, proto_tree *);
380 void dissect_tftp(const u_char *, int, frame_data *, proto_tree *);
381 void dissect_trmac(const u_char *, int, frame_data *, proto_tree *);
382 void dissect_udp(const u_char *, int, frame_data *, proto_tree *);
383 void dissect_vines(const u_char *, int, frame_data *, proto_tree *);
384 void dissect_vines_arp(const u_char *, int, frame_data *, proto_tree *);
385 void dissect_vines_frp(const u_char *, int, frame_data *, proto_tree *);
386 void dissect_vines_icp(const u_char *, int, frame_data *, proto_tree *);
387 void dissect_vines_ipc(const u_char *, int, frame_data *, proto_tree *);
388 void dissect_vines_rtp(const u_char *, int, frame_data *, proto_tree *);
389 void dissect_vines_spp(const u_char *, int, frame_data *, proto_tree *);
390
391 void dissect_ftp(const u_char *, int, frame_data *, proto_tree *, int);
392 void dissect_ftpdata(const u_char *, int, frame_data *, proto_tree *, int);
393 void dissect_nbss(const u_char *, int, frame_data *, proto_tree *, int);
394 void dissect_nntp(const u_char *, int, frame_data *, proto_tree *, int);
395 void dissect_pop(const u_char *, int, frame_data *, proto_tree *, int);
396 void dissect_smb(const u_char *, int, frame_data *, proto_tree *, int);
397 void dissect_telnet(const u_char *, int, frame_data *, proto_tree *, int);
398
399 void init_dissect_udp(void);
400
401 /* These functions are in ethertype.c */
402 gchar *ethertype_to_str(guint16 etype, const char *fmt);
403 void capture_ethertype(guint16 etype, int offset,
404                 const u_char *pd, guint32 cap_len, packet_counts *ld);
405 void ethertype(guint16 etype, int offset,
406                 const u_char *pd, frame_data *fd, proto_tree *tree,
407                 proto_tree *fh_tree);
408
409 /* These functions are in packet-arp.c */
410 gchar *arphrdaddr_to_str(guint8 *ad, int ad_len, guint16 type);
411 gchar *arphrdtype_to_str(guint16 hwtype, const char *fmt);
412
413 /*
414  * All of the possible columns in summary listing.
415  *
416  * NOTE: The SRC and DST entries MUST remain in this order, or else you
417  * need to fix the offset #defines before get_column_format!
418  */
419 enum {
420   COL_NUMBER,         /* Packet list item number */
421   COL_CLS_TIME,       /* Command line-specified time (default relative) */
422   COL_REL_TIME,       /* Relative time */
423   COL_ABS_TIME,       /* Absolute time */
424   COL_DELTA_TIME,     /* Delta time */
425   COL_DEF_SRC,        /* Source address */
426   COL_RES_SRC,        /* Resolved source */
427   COL_UNRES_SRC,      /* Unresolved source */
428   COL_DEF_DL_SRC,     /* Data link layer source address */
429   COL_RES_DL_SRC,     /* Resolved DL source */
430   COL_UNRES_DL_SRC,   /* Unresolved DL source */
431   COL_DEF_NET_SRC,    /* Network layer source address */
432   COL_RES_NET_SRC,    /* Resolved net source */
433   COL_UNRES_NET_SRC,  /* Unresolved net source */
434   COL_DEF_DST,        /* Destination address */
435   COL_RES_DST,        /* Resolved dest */
436   COL_UNRES_DST,      /* Unresolved dest */
437   COL_DEF_DL_DST,     /* Data link layer dest address */
438   COL_RES_DL_DST,     /* Resolved DL dest */
439   COL_UNRES_DL_DST,   /* Unresolved DL dest */
440   COL_DEF_NET_DST,    /* Network layer dest address */
441   COL_RES_NET_DST,    /* Resolved net dest */
442   COL_UNRES_NET_DST,  /* Unresolved net dest */
443   COL_DEF_SRC_PORT,   /* Source port */
444   COL_RES_SRC_PORT,   /* Resolved source port */
445   COL_UNRES_SRC_PORT, /* Unresolved source port */
446   COL_DEF_DST_PORT,   /* Destination port */
447   COL_RES_DST_PORT,   /* Resolved dest port */
448   COL_UNRES_DST_PORT, /* Unresolved dest port */
449   COL_PROTOCOL,       /* Protocol */
450   COL_INFO,           /* Description */
451   NUM_COL_FMTS        /* Should always be last */
452 };
453
454 #endif /* packet.h */