xref: /onnv-gate/usr/src/cmd/ipf/lib/common/printpacket.c (revision 2393:76e0289ce525)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
6*2393Syz155240  * $Id: printpacket.c,v 1.12.4.1 2005/02/21 05:09:24 darrenr Exp $
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
90Sstevel@tonic-gate #include "ipf.h"
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #ifndef	IP_OFFMASK
120Sstevel@tonic-gate # define	IP_OFFMASK	0x3fff
130Sstevel@tonic-gate #endif
140Sstevel@tonic-gate 
150Sstevel@tonic-gate 
printpacket(ip)160Sstevel@tonic-gate void printpacket(ip)
170Sstevel@tonic-gate struct ip *ip;
180Sstevel@tonic-gate {
190Sstevel@tonic-gate 	struct	tcphdr	*tcp;
200Sstevel@tonic-gate 	u_short len;
21*2393Syz155240 	u_short off;
220Sstevel@tonic-gate 
23*2393Syz155240 	if (IP_V(ip) == 6) {
24*2393Syz155240 		off = 0;
250Sstevel@tonic-gate 		len = ntohs(((u_short *)ip)[2]) + 40;
26*2393Syz155240 	} else {
27*2393Syz155240 		off = ntohs(ip->ip_off);
280Sstevel@tonic-gate 		len = ntohs(ip->ip_len);
29*2393Syz155240 	}
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 	if ((opts & OPT_HEX) == OPT_HEX) {
320Sstevel@tonic-gate 		u_char *s;
330Sstevel@tonic-gate 		int i;
340Sstevel@tonic-gate 
350Sstevel@tonic-gate 		for (s = (u_char *)ip, i = 0; i < len; i++) {
360Sstevel@tonic-gate 			printf("%02x", *s++ & 0xff);
370Sstevel@tonic-gate 			if (len - i > 1) {
380Sstevel@tonic-gate 				i++;
390Sstevel@tonic-gate 				printf("%02x", *s++ & 0xff);
400Sstevel@tonic-gate 			}
410Sstevel@tonic-gate 			putchar(' ');
420Sstevel@tonic-gate 		}
430Sstevel@tonic-gate 		putchar('\n');
440Sstevel@tonic-gate 		return;
450Sstevel@tonic-gate 	}
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	if (IP_V(ip) == 6) {
480Sstevel@tonic-gate 		printpacket6(ip);
490Sstevel@tonic-gate 		return;
500Sstevel@tonic-gate 	}
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	tcp = (struct tcphdr *)((char *)ip + (IP_HL(ip) << 2));
530Sstevel@tonic-gate 	printf("ip %d(%d) %d", ntohs(ip->ip_len), IP_HL(ip) << 2, ip->ip_p);
54*2393Syz155240 	if (off & IP_OFFMASK)
55*2393Syz155240 		printf(" @%d", off << 3);
560Sstevel@tonic-gate 	printf(" %s", inet_ntoa(ip->ip_src));
57*2393Syz155240 	if (!(off & IP_OFFMASK))
580Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
590Sstevel@tonic-gate 			printf(",%d", ntohs(tcp->th_sport));
600Sstevel@tonic-gate 	printf(" > ");
610Sstevel@tonic-gate 	printf("%s", inet_ntoa(ip->ip_dst));
62*2393Syz155240 	if (!(off & IP_OFFMASK)) {
630Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
640Sstevel@tonic-gate 			printf(",%d", ntohs(tcp->th_dport));
650Sstevel@tonic-gate 		if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags != 0)) {
660Sstevel@tonic-gate 			putchar(' ');
670Sstevel@tonic-gate 			if (tcp->th_flags & TH_FIN)
680Sstevel@tonic-gate 				putchar('F');
690Sstevel@tonic-gate 			if (tcp->th_flags & TH_SYN)
700Sstevel@tonic-gate 				putchar('S');
710Sstevel@tonic-gate 			if (tcp->th_flags & TH_RST)
720Sstevel@tonic-gate 				putchar('R');
730Sstevel@tonic-gate 			if (tcp->th_flags & TH_PUSH)
740Sstevel@tonic-gate 				putchar('P');
750Sstevel@tonic-gate 			if (tcp->th_flags & TH_ACK)
760Sstevel@tonic-gate 				putchar('A');
770Sstevel@tonic-gate 			if (tcp->th_flags & TH_URG)
780Sstevel@tonic-gate 				putchar('U');
790Sstevel@tonic-gate 			if (tcp->th_flags & TH_ECN)
800Sstevel@tonic-gate 				putchar('E');
810Sstevel@tonic-gate 			if (tcp->th_flags & TH_CWR)
820Sstevel@tonic-gate 				putchar('C');
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	putchar('\n');
870Sstevel@tonic-gate }
88