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