82a2ca3a40e070d656d3a100354ca231f4e24e9c
[obnox/wireshark/wip.git] / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.10 1998/09/27 22:12:42 gerald 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  * Handy for 
33  */
34
35 #if BYTE_ORDER == LITTLE_ENDIAN
36 #define pntohs(p)        ((guint16)                             \
37                           ((guint16)*((guint8 *)p+0)<<8|        \
38                            (guint16)*((guint8 *)p+1)<<0))
39
40 #define pntohl(p)       ((guint32)*((guint8 *)p+0)<<24|          \
41                          (guint32)*((guint8 *)p+1)<<16|          \
42                          (guint32)*((guint8 *)p+2)<<8|           \
43                          (guint32)*((guint8 *)p+3)<<0)
44 #else /* BIG_ENDIAN */
45 #define pntohs(p)        ((guint16)                             \
46                           ((guint16)*((guint8 *)p+1)<<8|        \
47                            (guint16)*((guint8 *)p+0)<<0))
48
49 #define pntohl(p)       ((guint32)*((guint8 *)p+3)<<24|          \
50                          (guint32)*((guint8 *)p+2)<<16|          \
51                          (guint32)*((guint8 *)p+1)<<8|           \
52                          (guint32)*((guint8 *)p+0)<<0)
53 #endif /* LITTLE_ENDIAN */
54
55 /* Useful when highlighting regions inside a dissect_*() function. With this
56  * macro, you can highlight from the start of the packet to the end of the
57  * frame. See dissect_data() for an example.
58  */
59 #define END_OF_FRAME    (fd->cap_len - offset)
60
61 #define IEEE_802_3_MAX_LEN 1500
62 #define BYTE_VIEW_WIDTH    16
63
64 typedef struct _frame_data {
65   guint32  pkt_len;         /* Packet length */
66   guint32  cap_len;         /* Amount actually captured */
67   guint32  secs;            /* Seconds */
68   guint32  usecs;           /* Microseconds */
69   long     file_off;        /* File offset */
70   gchar   *win_info[NUM_COLS]; /* Text for packet summary list fields */
71 } frame_data;
72
73 typedef struct _packet_info {
74   char *srcip;
75   int ip_src;
76   char *destip;
77   int ipproto;
78   int srcport;
79   int destport;
80   int iplen;
81   int iphdrlen;
82 } packet_info;
83
84 /* Many of the structs and definitions below were taken from include files
85  * in the Linux distribution. */
86
87 /* ARP / RARP structs and definitions */
88
89 typedef struct _e_ether_arp {
90   guint16 ar_hrd;
91   guint16 ar_pro;
92   guint8  ar_hln;
93   guint8  ar_pln;
94   guint16 ar_op;
95   guint8  arp_sha[6];
96   guint8  arp_spa[4];
97   guint8  arp_tha[6];
98   guint8  arp_tpa[4];
99 } e_ether_arp;
100
101 #ifndef ARPOP_REQUEST
102 #define ARPOP_REQUEST  1       /* ARP request.  */
103 #endif
104 #ifndef ARPOP_REPLY
105 #define ARPOP_REPLY    2       /* ARP reply.  */
106 #endif
107 /* Some OSes have different names, or don't define these at all */
108 #ifndef ARPOP_RREQUEST
109 #define ARPOP_RREQUEST 3       /* RARP request.  */
110 #endif
111 #ifndef ARPOP_RREPLY
112 #define ARPOP_RREPLY   4       /* RARP reply.  */
113 #endif
114
115 /* ICMP structs and definitions */
116
117 typedef struct _e_icmp {
118   guint8  icmp_type;
119   guint8  icmp_code;
120   guint16 icmp_cksum;
121   union {
122     struct {  /* Address mask request/reply */
123       guint16 id;
124       guint16 seq;
125       guint32 sn_mask;
126     } am;
127     struct {  /* Timestap request/reply */
128       guint16 id;
129       guint16 seq;
130       guint32 orig;
131       guint32 recv;
132       guint32 xmit;
133     } ts;
134     guint32 zero;  /* Unreachable */
135   } opt;
136 } e_icmp;
137
138 #define ICMP_ECHOREPLY     0
139 #define ICMP_UNREACH       3
140 #define ICMP_SOURCEQUENCH  4
141 #define ICMP_REDIRECT      5
142 #define ICMP_ECHO          8
143 #define ICMP_TIMXCEED     11
144 #define ICMP_PARAMPROB    12
145 #define ICMP_TSTAMP       13
146 #define ICMP_TSTAMPREPLY  14
147 #define ICMP_IREQ         15
148 #define ICMP_IREQREPLY    16
149 #define ICMP_MASKREQ      17
150 #define ICMP_MASKREPLY    18
151
152 /* IGMP structs and definitions */
153
154 typedef struct _e_igmp {
155 #if BYTE_ORDER == BIG_ENDIAN
156   guint8  igmp_v:4;
157   guint8  igmp_t:4;
158 #else /* Little endian */
159   guint8  igmp_t:4;
160   guint8  igmp_v:4;
161 #endif
162   guint8  igmp_unused;
163   guint16 igmp_cksum;
164   guint32 igmp_gaddr;
165 } e_igmp;
166
167 #define IGMP_M_QRY     0x01
168 #define IGMP_V1_M_RPT  0x02
169 #define IGMP_V2_LV_GRP 0x07
170 #define IGMP_DVMRP     0x03
171 #define IGMP_PIM       0x04
172 #define IGMP_V2_M_RPT  0x06
173 #define IGMP_MTRC_RESP 0x1e
174 #define IGMP_MTRC      0x1f
175
176 /* IP structs and definitions */
177
178 typedef struct _e_ip {
179 #if BYTE_ORDER == BIG_ENDIAN
180   guint8  ip_v:4;
181   guint8  ip_hl:4;
182 #else /* Little endian */
183   guint8  ip_hl:4;
184   guint8  ip_v:4;
185 #endif
186   guint8  ip_tos;
187   guint16 ip_len;
188   guint16 ip_id;
189   guint16 ip_off;
190   guint8  ip_ttl;
191   guint8  ip_p;
192   guint16 ip_sum;
193   guint32 ip_src;
194   guint32 ip_dst;
195 } e_ip;
196
197 #define IPTOS_TOS_MASK    0x1E
198 #define IPTOS_TOS(tos)    ((tos) & IPTOS_TOS_MASK)
199 #define IPTOS_NONE        0x00
200 #define IPTOS_LOWDELAY    0x10
201 #define IPTOS_THROUGHPUT  0x08
202 #define IPTOS_RELIABILITY 0x04
203 #define IPTOS_LOWCOST     0x02
204
205 #define IP_PROTO_ICMP  1
206 #define IP_PROTO_IGMP  2
207 #define IP_PROTO_TCP   6
208 #define IP_PROTO_UDP  17
209 #define IP_PROTO_OSPF 89
210
211 /* Null/loopback structs and definitions */
212
213 typedef struct _e_nullhdr {
214   guint8  null_next;
215   guint8  null_len;
216   guint16 null_family;
217 } e_nullhdr;
218
219 /* PPP structs and definitions */
220
221 typedef struct _e_ppphdr {
222   guint8  ppp_addr;
223   guint8  ppp_ctl;
224   guint16 ppp_prot;
225 } e_ppphdr;
226
227 /* TCP structs and definitions */
228
229 typedef struct _e_tcphdr {
230   guint16 th_sport;
231   guint16 th_dport;
232   guint32 th_seq;
233   guint32 th_ack;
234 #if BYTE_ORDER == LITTLE_ENDIAN
235   guint8  th_x2:4;
236   guint8  th_off:4;
237 #else
238   guint8  th_off:4;
239   guint8  th_x2:4;
240 #endif
241   guint8  th_flags;
242 #define TH_FIN  0x01
243 #define TH_SYN  0x02
244 #define TH_RST  0x04
245 #define TH_PUSH 0x08
246 #define TH_ACK  0x10
247 #define TH_URG  0x20
248   guint16 th_win;
249   guint16 th_sum;
250   guint16 th_urp;
251 } e_tcphdr;
252
253 /* UDP structs and definitions */
254
255 typedef struct _e_udphdr {
256   guint16 uh_sport;
257   guint16 uh_dport;
258   guint16 uh_ulen;
259   guint16 uh_sum;
260 } e_udphdr;
261
262 /* UDP Ports -> should go in packet-udp.h */
263
264 #define UDP_PORT_DNS     53
265 #define UDP_PORT_BOOTPS  67
266 #define UDP_PORT_IPX    213
267 #define UDP_PORT_RIP    520
268
269 /* TCP Ports */
270
271 #define TCP_PORT_PRINTER 515
272
273 /* Tree types.  Each dissect_* routine should have one for each
274    add_subtree() call. */
275
276 #define ETT_FRAME          0
277 #define ETT_IEEE8023       1
278 #define ETT_ETHER2         2
279 #define ETT_LLC            3
280 #define ETT_TOKEN_RING     4
281 #define ETT_TR_IERR_CNT    5
282 #define ETT_TR_NERR_CNT    6
283 #define ETT_TR_MAC         7
284 #define ETT_PPP            8
285 #define ETT_ARP            9
286 #define ETT_IP            10
287 #define ETT_UDP           11
288 #define ETT_TCP           12
289 #define ETT_ICMP          13
290 #define ETT_IGMP          14
291 #define ETT_IPX           15
292 #define ETT_SPX           16
293 #define ETT_NCP           17
294 #define ETT_DNS           18
295 #define ETT_DNS_ANS       19
296 #define ETT_DNS_QRY       20
297 #define ETT_RIP           21
298 #define ETT_RIP_VEC       22
299 #define ETT_OSPF          23
300 #define ETT_OSPF_HDR      24
301 #define ETT_OSPF_HELLO    25
302 #define ETT_OSPF_DESC     26
303 #define ETT_OSPF_LSR      27
304 #define ETT_OSPF_LSA_UPD  28
305 #define ETT_OSPF_LSA      29
306 #define ETT_LPD           30
307 #define ETT_RAW           31
308 #define ETT_BOOTP         32
309 #define ETT_BOOTP_OPTION  33
310 #define ETT_IPv6          34
311 #define ETT_CLNP          35
312 #define ETT_COTP          36
313 #define ETT_VINES         37
314 #define ETT_VSPP          38
315 #define ETT_IPXRIP        39
316 #define ETT_IPXSAP        40
317 #define ETT_IPXSAP_SERVER 41
318 #define ETT_NULL          42
319
320 /* Should be the last item number plus one */
321 #define NUM_TREE_TYPES 43
322
323 /* The version of pcap.h that comes with some systems is missing these
324  * #defines.
325  */
326
327 #ifndef DLT_RAW
328 #define DLT_RAW 12
329 #endif
330
331 #ifndef DLT_SLIP_BSDOS
332 #define DLT_SLIP_BSDOS 13
333 #endif
334
335 #ifndef DLT_PPP_BSDOS
336 #define DLT_PPP_BSDOS 14
337 #endif
338
339 /* Utility routines used by packet*.c */
340 gchar*     ether_to_str(guint8 *);
341 gchar*     ip_to_str(guint8 *);
342 void       packet_hex_print(GtkText *, guint8 *, gint, gint, gint);
343 #if __GNUC__ == 2
344 GtkWidget* add_item_to_tree(GtkWidget *, gint, gint, gchar *, ...)
345     __attribute__((format (printf, 4, 5)));
346 #else
347 GtkWidget* add_item_to_tree(GtkWidget *, gint, gint, gchar *, ...);
348 #endif
349 void       decode_start_len(GtkTreeItem *, gint*, gint*);
350
351 /* Routines in packet.c */
352 void dissect_packet(const u_char *, guint32 ts_secs, guint32 ts_usecs,
353   frame_data *, GtkTree *);
354 void add_subtree(GtkWidget *, GtkWidget*, gint);
355 void expand_tree(GtkWidget *, gpointer);
356 void collapse_tree(GtkWidget *, gpointer);
357
358 /*
359  * Routines in packet-*.c
360  * Routines should take three args: packet data *, frame_data *, tree *
361  * They should never modify the packet data.
362  */
363 void dissect_eth(const u_char *, frame_data *, GtkTree *);
364 void dissect_null(const u_char *, frame_data *, GtkTree *);
365 void dissect_ppp(const u_char *, frame_data *, GtkTree *);
366 void dissect_raw(const u_char *, frame_data *, GtkTree *);
367 void dissect_tr(const u_char *, frame_data *, GtkTree *);
368
369 /*
370  * Routines in packet-*.c
371  * Routines should take four args: packet data *, offset, frame_data *,
372  * tree *
373  * They should never modify the packet data.
374  */
375 void dissect_arp(const u_char *, int, frame_data *, GtkTree *);
376 void dissect_bootp(const u_char *, int, frame_data *, GtkTree *);
377 void dissect_data(const u_char *, int, frame_data *, GtkTree *);
378 void dissect_dns(const u_char *, int, frame_data *, GtkTree *);
379 void dissect_icmp(const u_char *, int, frame_data *, GtkTree *);
380 void dissect_igmp(const u_char *, int, frame_data *, GtkTree *);
381 void dissect_ip(const u_char *, int, frame_data *, GtkTree *);
382 void dissect_ipv6(const u_char *, int, frame_data *, GtkTree *);
383 void dissect_ipx(const u_char *, int, frame_data *, GtkTree *);
384 void dissect_llc(const u_char *, int, frame_data *, GtkTree *);
385 void dissect_lpd(const u_char *, int, frame_data *, GtkTree *);
386 void dissect_ncp(const u_char *, int, frame_data *, GtkTree *);
387 void dissect_osi(const u_char *, int, frame_data *, GtkTree *);
388 void dissect_ospf(const u_char *, int, frame_data *, GtkTree *);
389 void dissect_ospf_hello(const u_char *, int, frame_data *, GtkTree *);
390 void dissect_rip(const u_char *, int, frame_data *, GtkTree *);
391 void dissect_tcp(const u_char *, int, frame_data *, GtkTree *);
392 void dissect_trmac(const u_char *, int, frame_data *, GtkTree *);
393 void dissect_udp(const u_char *, int, frame_data *, GtkTree *);
394 void dissect_vines(const u_char *, int, frame_data *, GtkTree *);
395 void dissect_vspp(const u_char *, int, frame_data *, GtkTree *);
396
397 /* This function is in ethertype.c */
398 void ethertype(guint16 etype, int offset,
399                 const u_char *pd, frame_data *fd, GtkTree *tree,
400                 GtkWidget *fh_tree);
401
402 #endif /* packet.h */