xref: /illumos-gate/usr/src/cmd/ipf/lib/printpacket.c (revision 6fa29843813e354e472ca1ef80590ab80e2362b7)
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: printpacket.c,v 1.12.4.1 2005/02/21 05:09:24 darrenr Exp $
7  */
8 
9 #include "ipf.h"
10 
11 #ifndef	IP_OFFMASK
12 # define	IP_OFFMASK	0x3fff
13 #endif
14 
15 
16 void printpacket(ip)
17 struct ip *ip;
18 {
19 	struct	tcphdr	*tcp;
20 	u_short len;
21 	u_short off;
22 
23 	if (IP_V(ip) == 6) {
24 		off = 0;
25 		len = ntohs(((u_short *)ip)[2]) + 40;
26 	} else {
27 		off = ntohs(ip->ip_off);
28 		len = ntohs(ip->ip_len);
29 	}
30 
31 	if ((opts & OPT_HEX) == OPT_HEX) {
32 		u_char *s;
33 		int i;
34 
35 		for (s = (u_char *)ip, i = 0; i < len; i++) {
36 			printf("%02x", *s++ & 0xff);
37 			if (len - i > 1) {
38 				i++;
39 				printf("%02x", *s++ & 0xff);
40 			}
41 			putchar(' ');
42 		}
43 		putchar('\n');
44 		return;
45 	}
46 
47 	if (IP_V(ip) == 6) {
48 		printpacket6(ip);
49 		return;
50 	}
51 
52 	tcp = (struct tcphdr *)((char *)ip + (IP_HL(ip) << 2));
53 	printf("ip %d(%d) %d", ntohs(ip->ip_len), IP_HL(ip) << 2, ip->ip_p);
54 	if (off & IP_OFFMASK)
55 		printf(" @%d", off << 3);
56 	printf(" %s", inet_ntoa(ip->ip_src));
57 	if (!(off & IP_OFFMASK))
58 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
59 			printf(",%d", ntohs(tcp->th_sport));
60 	printf(" > ");
61 	printf("%s", inet_ntoa(ip->ip_dst));
62 	if (!(off & IP_OFFMASK)) {
63 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
64 			printf(",%d", ntohs(tcp->th_dport));
65 		if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags != 0)) {
66 			putchar(' ');
67 			if (tcp->th_flags & TH_FIN)
68 				putchar('F');
69 			if (tcp->th_flags & TH_SYN)
70 				putchar('S');
71 			if (tcp->th_flags & TH_RST)
72 				putchar('R');
73 			if (tcp->th_flags & TH_PUSH)
74 				putchar('P');
75 			if (tcp->th_flags & TH_ACK)
76 				putchar('A');
77 			if (tcp->th_flags & TH_URG)
78 				putchar('U');
79 			if (tcp->th_flags & TH_ECN)
80 				putchar('E');
81 			if (tcp->th_flags & TH_CWR)
82 				putchar('C');
83 		}
84 	}
85 
86 	putchar('\n');
87 }
88