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