90193e05dc78c4e06f3805ad984f72a0144ddda0
[obnox/wireshark/wip.git] / packet.h
1 /* packet.h
2  * Definitions for packet disassembly structures and routines
3  *
4  * $Id: packet.h,v 1.30 1999/01/02 06:10:54 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 /* Useful when highlighting regions inside a dissect_*() function. With this
55  * macro, you can highlight from the start of the packet to the end of the
56  * frame. See dissect_data() for an example.
57  */
58 #define END_OF_FRAME    (fd->cap_len - offset)
59
60 #define IEEE_802_3_MAX_LEN 1500
61 #define BYTE_VIEW_WIDTH    16
62
63 typedef struct _column_info {
64   gint       num_cols; /* Number of columns */
65   gboolean **fmt_matx; /* Specifies which formats apply to a column */
66   gchar    **col_data; /* Column data */
67 } column_info;
68
69 #define COL_MAX_LEN 256
70
71 typedef struct _frame_data {
72   guint32      pkt_len;   /* Packet length */
73   guint32      cap_len;   /* Amount actually captured */
74   guint32      rel_secs;  /* Relative seconds */
75   guint32      rel_usecs; /* Relative microseconds */
76   guint32      abs_secs;  /* Absolute seconds */
77   guint32      abs_usecs; /* Absolute microseconds */
78   guint32      del_secs;  /* Delta seconds */
79   guint32      del_usecs; /* Delta microseconds */
80   long         file_off;  /* File offset */
81   column_info *cinfo;     /* Column formatting information */
82 #ifdef WITH_WIRETAP
83   int           lnk_t;     /* Per-packet encapsulation/data-link type */
84 #endif
85 } frame_data;
86
87 typedef struct _packet_info {
88   char *srcip;
89   int ip_src;
90   char *destip;
91   int ipproto;
92   int srcport;
93   int destport;
94   int iplen;
95   int iphdrlen;
96 } packet_info;
97
98 /* Struct for the match_strval function */
99
100 typedef struct _value_string {
101   guint32  value;
102   gchar   *strptr;
103 } value_string;
104
105 /* Many of the structs and definitions below were taken from include files
106  * in the Linux distribution. */
107
108 /* ARP / RARP structs and definitions */
109
110 #ifndef ARPOP_REQUEST
111 #define ARPOP_REQUEST  1       /* ARP request.  */
112 #endif
113 #ifndef ARPOP_REPLY
114 #define ARPOP_REPLY    2       /* ARP reply.  */
115 #endif
116 /* Some OSes have different names, or don't define these at all */
117 #ifndef ARPOP_RREQUEST
118 #define ARPOP_RREQUEST 3       /* RARP request.  */
119 #endif
120 #ifndef ARPOP_RREPLY
121 #define ARPOP_RREPLY   4       /* RARP reply.  */
122 #endif
123
124 /* ICMP structs and definitions */
125
126 typedef struct _e_icmp {
127   guint8  icmp_type;
128   guint8  icmp_code;
129   guint16 icmp_cksum;
130   union {
131     struct {  /* Address mask request/reply */
132       guint16 id;
133       guint16 seq;
134       guint32 sn_mask;
135     } am;
136     struct {  /* Timestap request/reply */
137       guint16 id;
138       guint16 seq;
139       guint32 orig;
140       guint32 recv;
141       guint32 xmit;
142     } ts;
143     guint32 zero;  /* Unreachable */
144   } opt;
145 } e_icmp;
146
147 #define ICMP_ECHOREPLY     0
148 #define ICMP_UNREACH       3
149 #define ICMP_SOURCEQUENCH  4
150 #define ICMP_REDIRECT      5
151 #define ICMP_ECHO          8
152 #define ICMP_TIMXCEED     11
153 #define ICMP_PARAMPROB    12
154 #define ICMP_TSTAMP       13
155 #define ICMP_TSTAMPREPLY  14
156 #define ICMP_IREQ         15
157 #define ICMP_IREQREPLY    16
158 #define ICMP_MASKREQ      17
159 #define ICMP_MASKREPLY    18
160
161 /* IGMP structs and definitions */
162
163 typedef struct _e_igmp {
164 #if BYTE_ORDER == BIG_ENDIAN
165   guint8  igmp_v:4;
166   guint8  igmp_t:4;
167 #else /* Little endian */
168   guint8  igmp_t:4;
169   guint8  igmp_v:4;
170 #endif
171   guint8  igmp_unused;
172   guint16 igmp_cksum;
173   guint32 igmp_gaddr;
174 } e_igmp;
175
176 #define IGMP_M_QRY     0x01
177 #define IGMP_V1_M_RPT  0x02
178 #define IGMP_V2_LV_GRP 0x07
179 #define IGMP_DVMRP     0x03
180 #define IGMP_PIM       0x04
181 #define IGMP_V2_M_RPT  0x06
182 #define IGMP_MTRC_RESP 0x1e
183 #define IGMP_MTRC      0x1f
184
185 /* IP structs and definitions */
186
187 typedef struct _e_ip {
188 #if BYTE_ORDER == BIG_ENDIAN
189   guint8  ip_v:4;
190   guint8  ip_hl:4;
191 #else /* Little endian */
192   guint8  ip_hl:4;
193   guint8  ip_v:4;
194 #endif
195   guint8  ip_tos;
196   guint16 ip_len;
197   guint16 ip_id;
198   guint16 ip_off;
199   guint8  ip_ttl;
200   guint8  ip_p;
201   guint16 ip_sum;
202   guint32 ip_src;
203   guint32 ip_dst;
204 } e_ip;
205
206 /* IP flags. */
207 #define IP_CE           0x8000          /* Flag: "Congestion"           */
208 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
209 #define IP_MF           0x2000          /* Flag: "More Fragments"       */
210 #define IP_OFFSET       0x1FFF          /* "Fragment Offset" part       */
211
212 #define IPTOS_TOS_MASK    0x1E
213 #define IPTOS_TOS(tos)    ((tos) & IPTOS_TOS_MASK)
214 #define IPTOS_NONE        0x00
215 #define IPTOS_LOWCOST     0x02
216 #define IPTOS_RELIABILITY 0x04
217 #define IPTOS_THROUGHPUT  0x08
218 #define IPTOS_LOWDELAY    0x10
219 #define IPTOS_SECURITY    0x1E
220
221 #define IPTOS_PREC_MASK         0xE0
222 #define IPTOS_PREC(tos)         ((tos)&IPTOS_PREC_MASK)
223 #define IPTOS_PREC_NETCONTROL           0xe0
224 #define IPTOS_PREC_INTERNETCONTROL      0xc0
225 #define IPTOS_PREC_CRITIC_ECP           0xa0
226 #define IPTOS_PREC_FLASHOVERRIDE        0x80
227 #define IPTOS_PREC_FLASH                0x60
228 #define IPTOS_PREC_IMMEDIATE            0x40
229 #define IPTOS_PREC_PRIORITY             0x20
230 #define IPTOS_PREC_ROUTINE              0x00
231
232 /* IP options */
233 #define IPOPT_COPY              0x80
234
235 #define IPOPT_CONTROL           0x00
236 #define IPOPT_RESERVED1         0x20
237 #define IPOPT_MEASUREMENT       0x40
238 #define IPOPT_RESERVED2         0x60
239
240 #define IPOPT_END       (0 |IPOPT_CONTROL)
241 #define IPOPT_NOOP      (1 |IPOPT_CONTROL)
242 #define IPOPT_SEC       (2 |IPOPT_CONTROL|IPOPT_COPY)
243 #define IPOPT_LSRR      (3 |IPOPT_CONTROL|IPOPT_COPY)
244 #define IPOPT_TIMESTAMP (4 |IPOPT_MEASUREMENT)
245 #define IPOPT_RR        (7 |IPOPT_CONTROL)
246 #define IPOPT_SID       (8 |IPOPT_CONTROL|IPOPT_COPY)
247 #define IPOPT_SSRR      (9 |IPOPT_CONTROL|IPOPT_COPY)
248 #define IPOPT_RA        (20|IPOPT_CONTROL|IPOPT_COPY)
249
250 /* IP option lengths */
251 #define IPOLEN_SEC      11
252 #define IPOLEN_LSRR_MIN 3
253 #define IPOLEN_TIMESTAMP_MIN 5
254 #define IPOLEN_RR_MIN   3
255 #define IPOLEN_SID      4
256 #define IPOLEN_SSRR_MIN 3
257
258 #define IPSEC_UNCLASSIFIED      0x0000
259 #define IPSEC_CONFIDENTIAL      0xF135
260 #define IPSEC_EFTO              0x789A
261 #define IPSEC_MMMM              0xBC4D
262 #define IPSEC_RESTRICTED        0xAF13
263 #define IPSEC_SECRET            0xD788
264 #define IPSEC_TOPSECRET         0x6BC5
265 #define IPSEC_RESERVED1         0x35E2
266 #define IPSEC_RESERVED2         0x9AF1
267 #define IPSEC_RESERVED3         0x4D78
268 #define IPSEC_RESERVED4         0x24BD
269 #define IPSEC_RESERVED5         0x135E
270 #define IPSEC_RESERVED6         0x89AF
271 #define IPSEC_RESERVED7         0xC4D6
272 #define IPSEC_RESERVED8         0xE26B
273
274 #define IPOPT_TS_TSONLY         0               /* timestamps only */
275 #define IPOPT_TS_TSANDADDR      1               /* timestamps and addresses */
276 #define IPOPT_TS_PRESPEC        3               /* specified modules only */
277
278 #define IP_PROTO_ICMP  1
279 #define IP_PROTO_IGMP  2
280 #define IP_PROTO_TCP   6
281 #define IP_PROTO_UDP  17
282 #define IP_PROTO_OSPF 89
283
284 /* Null/loopback structs and definitions */
285
286 typedef struct _e_nullhdr {
287   guint8  null_next;
288   guint8  null_len;
289   guint16 null_family;
290 } e_nullhdr;
291
292 /* PPP structs and definitions */
293
294 typedef struct _e_ppphdr {
295   guint8  ppp_addr;
296   guint8  ppp_ctl;
297   guint16 ppp_prot;
298 } e_ppphdr;
299
300 /* TCP structs and definitions */
301
302 typedef struct _e_tcphdr {
303   guint16 th_sport;
304   guint16 th_dport;
305   guint32 th_seq;
306   guint32 th_ack;
307 #if BYTE_ORDER == LITTLE_ENDIAN
308   guint8  th_x2:4;
309   guint8  th_off:4;
310 #else
311   guint8  th_off:4;
312   guint8  th_x2:4;
313 #endif
314   guint8  th_flags;
315 #define TH_FIN  0x01
316 #define TH_SYN  0x02
317 #define TH_RST  0x04
318 #define TH_PUSH 0x08
319 #define TH_ACK  0x10
320 #define TH_URG  0x20
321   guint16 th_win;
322   guint16 th_sum;
323   guint16 th_urp;
324 } e_tcphdr;
325
326 /*
327  *      TCP option
328  */
329  
330 #define TCPOPT_NOP              1       /* Padding */
331 #define TCPOPT_EOL              0       /* End of options */
332 #define TCPOPT_MSS              2       /* Segment size negotiating */
333 #define TCPOPT_WINDOW           3       /* Window scaling */
334 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
335 #define TCPOPT_SACK             5       /* SACK Block */
336 #define TCPOPT_ECHO             6
337 #define TCPOPT_ECHOREPLY        7
338 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
339 #define TCPOPT_CC               11
340 #define TCPOPT_CCNEW            12
341 #define TCPOPT_CCECHO           13
342
343 /*
344  *     TCP option lengths
345  */
346
347 #define TCPOLEN_MSS            4
348 #define TCPOLEN_WINDOW         3
349 #define TCPOLEN_SACK_PERM      2
350 #define TCPOLEN_SACK_MIN       2
351 #define TCPOLEN_ECHO           6
352 #define TCPOLEN_ECHOREPLY      6
353 #define TCPOLEN_TIMESTAMP      10
354 #define TCPOLEN_CC             6
355 #define TCPOLEN_CCNEW          6
356 #define TCPOLEN_CCECHO         6
357
358 /* UDP structs and definitions */
359
360 typedef struct _e_udphdr {
361   guint16 uh_sport;
362   guint16 uh_dport;
363   guint16 uh_ulen;
364   guint16 uh_sum;
365 } e_udphdr;
366
367 /* UDP Ports -> should go in packet-udp.h */
368
369 #define UDP_PORT_DNS     53
370 #define UDP_PORT_BOOTPS  67
371 #define UDP_PORT_IPX    213
372 #define UDP_PORT_NBNS   137
373 #define UDP_PORT_NBDGM  138
374 #define UDP_PORT_RIP    520
375 #define UDP_PORT_VINES  573
376
377 /* TCP Ports */
378
379 #define TCP_PORT_PRINTER 515
380
381 /* Tree types.  Each dissect_* routine should have one for each
382    add_subtree() call. */
383
384 enum {
385         ETT_FRAME,
386         ETT_IEEE8023,
387         ETT_ETHER2,
388         ETT_LLC,
389         ETT_TOKEN_RING,
390         ETT_TR_IERR_CNT,
391         ETT_TR_NERR_CNT,
392         ETT_TR_MAC,
393         ETT_PPP,
394         ETT_ARP,
395         ETT_FDDI,
396         ETT_NULL,
397         ETT_IP,
398         ETT_IP_OPTIONS,
399         ETT_IP_OPTION_SEC,
400         ETT_IP_OPTION_ROUTE,
401         ETT_IP_OPTION_TIMESTAMP,
402         ETT_IP_TOS,
403         ETT_IP_OFF,
404         ETT_UDP,
405         ETT_TCP,
406         ETT_TCP_OPTIONS,
407         ETT_TCP_OPTION_SACK,
408         ETT_TCP_FLAGS,
409         ETT_ICMP,
410         ETT_IGMP,
411         ETT_IPX,
412         ETT_SPX,
413         ETT_NCP,
414         ETT_DNS,
415         ETT_DNS_QRY,
416         ETT_DNS_QD,
417         ETT_DNS_ANS,
418         ETT_DNS_RR,
419         ETT_RIP,
420         ETT_RIP_VEC,
421         ETT_OSPF,
422         ETT_OSPF_HDR,
423         ETT_OSPF_HELLO,
424         ETT_OSPF_DESC,
425         ETT_OSPF_LSR,
426         ETT_OSPF_LSA_UPD,
427         ETT_OSPF_LSA,
428         ETT_LPD,
429         ETT_RAW,
430         ETT_BOOTP,
431         ETT_BOOTP_OPTION,
432         ETT_IPv6,
433         ETT_CLNP,
434         ETT_COTP,
435         ETT_VINES_FRP,
436         ETT_VINES,
437         ETT_VINES_ARP,
438         ETT_VINES_ICP,
439         ETT_VINES_IPC,
440         ETT_VINES_RTP,
441         ETT_VINES_SPP,
442         ETT_IPXRIP,
443         ETT_IPXSAP,
444         ETT_IPXSAP_SERVER,
445         ETT_NBNS,
446         ETT_NBNS_QRY,
447         ETT_NBNS_QD,
448         ETT_NBNS_ANS,
449         ETT_NBNS_RR,
450         ETT_NBIPX,
451         ETT_AARP,
452         ETT_GIOP,
453         ETT_NBDGM,
454         ETT_CDP,
455         NUM_TREE_TYPES  /* last item number plus one */
456 };
457
458 /* The version of pcap.h that comes with some systems is missing these
459  * #defines.
460  */
461
462 #ifndef DLT_RAW
463 #define DLT_RAW 12
464 #endif
465
466 #ifndef DLT_SLIP_BSDOS
467 #define DLT_SLIP_BSDOS 13
468 #endif
469
470 #ifndef DLT_PPP_BSDOS
471 #define DLT_PPP_BSDOS 14
472 #endif
473
474 typedef enum {
475   NO_LENGTH,            /* option has no data, hence no length */
476   FIXED_LENGTH,         /* option always has the same length */
477   VARIABLE_LENGTH       /* option is variable-length - optlen is minimum */
478 } opt_len_type;
479
480 /* Member of table of IP or TCP options. */
481 typedef struct {
482   int   optcode;        /* code for option */
483   char *name;           /* name of option */
484   opt_len_type len_type; /* type of option length field */
485   int   optlen;         /* value length should be (minimum if VARIABLE) */
486   void  (*dissect)(GtkWidget *, const char *, const u_char *, int, guint);
487                         /* routine to dissect option */
488 } ip_tcp_opt;
489
490 /* Routine to dissect IP or TCP options. */
491 void       dissect_ip_tcp_options(GtkWidget *, const u_char *, int, guint,
492     ip_tcp_opt *, int, int);
493
494 /* Utility routines used by packet*.c */
495 gchar*     ether_to_str(guint8 *);
496 gchar*     ip_to_str(guint8 *);
497 void       packet_hex_print(GtkText *, guint8 *, gint, gint, gint);
498 #define E_TREEINFO_START_KEY "tree_info_start"
499 #define E_TREEINFO_LEN_KEY   "tree_info_len"
500 #if __GNUC__ == 2
501 GtkWidget* add_item_to_tree(GtkWidget *, gint, gint, gchar *, ...)
502     __attribute__((format (printf, 4, 5)));
503 #else
504 GtkWidget* add_item_to_tree(GtkWidget *, gint, gint, gchar *, ...);
505 #endif
506 void       set_item_len(GtkWidget *, gint);
507 gchar*     val_to_str(guint32, const value_string *, const char *);
508 gchar*     match_strval(guint32, const value_string*);
509 gint       check_col(frame_data *, gint);
510 #if __GNUC__ == 2
511 void       col_add_fstr(frame_data *, gint, gchar *, ...)
512     __attribute__((format (printf, 3, 4)));
513 #else
514 void       col_add_fstr(frame_data *, gint, gchar *, ...);
515 #endif
516 void       col_add_str(frame_data *, gint, gchar *);
517
518 /* Routines in packet.c */
519
520 void dissect_packet(const u_char *, frame_data *, GtkTree *);
521 void add_subtree(GtkWidget *, GtkWidget*, gint);
522 void expand_tree(GtkWidget *, gpointer);
523 void collapse_tree(GtkWidget *, gpointer);
524
525 /*
526  * Routines in packet-*.c
527  * Routines should take three args: packet data *, frame_data *, tree *
528  * They should never modify the packet data.
529  */
530 void dissect_eth(const u_char *, frame_data *, GtkTree *);
531 void dissect_fddi(const u_char *, frame_data *, GtkTree *);
532 void dissect_null(const u_char *, frame_data *, GtkTree *);
533 void dissect_ppp(const u_char *, frame_data *, GtkTree *);
534 void dissect_raw(const u_char *, frame_data *, GtkTree *);
535 void dissect_tr(const u_char *, frame_data *, GtkTree *);
536
537 /*
538  * Routines in packet-*.c
539  * Routines should take four args: packet data *, offset, frame_data *,
540  * tree *
541  * They should never modify the packet data.
542  */
543 void dissect_aarp(const u_char *, int, frame_data *, GtkTree *);
544 void dissect_arp(const u_char *, int, frame_data *, GtkTree *);
545 void dissect_bootp(const u_char *, int, frame_data *, GtkTree *);
546 void dissect_cdp(const u_char *, int, frame_data *, GtkTree *);
547 void dissect_data(const u_char *, int, frame_data *, GtkTree *);
548 void dissect_ddp(const u_char *, int, frame_data *, GtkTree *);
549 void dissect_dns(const u_char *, int, frame_data *, GtkTree *);
550 void dissect_giop(const u_char *, int, frame_data *, GtkTree *);
551 void dissect_icmp(const u_char *, int, frame_data *, GtkTree *);
552 void dissect_igmp(const u_char *, int, frame_data *, GtkTree *);
553 void dissect_ip(const u_char *, int, frame_data *, GtkTree *);
554 void dissect_ipv6(const u_char *, int, frame_data *, GtkTree *);
555 void dissect_ipx(const u_char *, int, frame_data *, GtkTree *);
556 void dissect_llc(const u_char *, int, frame_data *, GtkTree *);
557 void dissect_lpd(const u_char *, int, frame_data *, GtkTree *);
558 void dissect_nbdgm(const u_char *, int, frame_data *, GtkTree *);
559 void dissect_nbipx_ns(const u_char *, int, frame_data *, GtkTree *);
560 void dissect_nbns(const u_char *, int, frame_data *, GtkTree *);
561 void dissect_ncp(const u_char *, int, frame_data *, GtkTree *);
562 void dissect_nwlink_dg(const u_char *, int, frame_data *, GtkTree *);
563 void dissect_osi(const u_char *, int, frame_data *, GtkTree *);
564 void dissect_ospf(const u_char *, int, frame_data *, GtkTree *);
565 void dissect_ospf_hello(const u_char *, int, frame_data *, GtkTree *);
566 void dissect_rip(const u_char *, int, frame_data *, GtkTree *);
567 void dissect_tcp(const u_char *, int, frame_data *, GtkTree *);
568 void dissect_trmac(const u_char *, int, frame_data *, GtkTree *);
569 void dissect_udp(const u_char *, int, frame_data *, GtkTree *);
570 void dissect_vines(const u_char *, int, frame_data *, GtkTree *);
571 void dissect_vines_arp(const u_char *, int, frame_data *, GtkTree *);
572 void dissect_vines_frp(const u_char *, int, frame_data *, GtkTree *);
573 void dissect_vines_icp(const u_char *, int, frame_data *, GtkTree *);
574 void dissect_vines_ipc(const u_char *, int, frame_data *, GtkTree *);
575 void dissect_vines_rtp(const u_char *, int, frame_data *, GtkTree *);
576 void dissect_vines_spp(const u_char *, int, frame_data *, GtkTree *);
577
578 /* These functions are in ethertype.c */
579 gchar *ethertype_to_str(guint16 etype, const char *fmt);
580 void ethertype(guint16 etype, int offset,
581                 const u_char *pd, frame_data *fd, GtkTree *tree,
582                 GtkWidget *fh_tree);
583
584 #endif /* packet.h */