xref: /illumos-gate/usr/src/cmd/ipf/lib/printpacket.c (revision f3ac6781)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6ab25eeb5Syz  * $Id: printpacket.c,v 1.12.4.1 2005/02/21 05:09:24 darrenr Exp $
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #include "ipf.h"
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #ifndef	IP_OFFMASK
127c478bd9Sstevel@tonic-gate # define	IP_OFFMASK	0x3fff
137c478bd9Sstevel@tonic-gate #endif
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate 
printpacket(ip)167c478bd9Sstevel@tonic-gate void printpacket(ip)
177c478bd9Sstevel@tonic-gate struct ip *ip;
187c478bd9Sstevel@tonic-gate {
197c478bd9Sstevel@tonic-gate 	struct	tcphdr	*tcp;
207c478bd9Sstevel@tonic-gate 	u_short len;
21ab25eeb5Syz 	u_short off;
227c478bd9Sstevel@tonic-gate 
23ab25eeb5Syz 	if (IP_V(ip) == 6) {
24ab25eeb5Syz 		off = 0;
257c478bd9Sstevel@tonic-gate 		len = ntohs(((u_short *)ip)[2]) + 40;
26ab25eeb5Syz 	} else {
27ab25eeb5Syz 		off = ntohs(ip->ip_off);
287c478bd9Sstevel@tonic-gate 		len = ntohs(ip->ip_len);
29ab25eeb5Syz 	}
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 	if ((opts & OPT_HEX) == OPT_HEX) {
327c478bd9Sstevel@tonic-gate 		u_char *s;
337c478bd9Sstevel@tonic-gate 		int i;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 		for (s = (u_char *)ip, i = 0; i < len; i++) {
367c478bd9Sstevel@tonic-gate 			printf("%02x", *s++ & 0xff);
377c478bd9Sstevel@tonic-gate 			if (len - i > 1) {
387c478bd9Sstevel@tonic-gate 				i++;
397c478bd9Sstevel@tonic-gate 				printf("%02x", *s++ & 0xff);
407c478bd9Sstevel@tonic-gate 			}
417c478bd9Sstevel@tonic-gate 			putchar(' ');
427c478bd9Sstevel@tonic-gate 		}
437c478bd9Sstevel@tonic-gate 		putchar('\n');
447c478bd9Sstevel@tonic-gate 		return;
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (IP_V(ip) == 6) {
487c478bd9Sstevel@tonic-gate 		printpacket6(ip);
497c478bd9Sstevel@tonic-gate 		return;
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	tcp = (struct tcphdr *)((char *)ip + (IP_HL(ip) << 2));
537c478bd9Sstevel@tonic-gate 	printf("ip %d(%d) %d", ntohs(ip->ip_len), IP_HL(ip) << 2, ip->ip_p);
54ab25eeb5Syz 	if (off & IP_OFFMASK)
55ab25eeb5Syz 		printf(" @%d", off << 3);
567c478bd9Sstevel@tonic-gate 	printf(" %s", inet_ntoa(ip->ip_src));
57ab25eeb5Syz 	if (!(off & IP_OFFMASK))
587c478bd9Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
597c478bd9Sstevel@tonic-gate 			printf(",%d", ntohs(tcp->th_sport));
607c478bd9Sstevel@tonic-gate 	printf(" > ");
617c478bd9Sstevel@tonic-gate 	printf("%s", inet_ntoa(ip->ip_dst));
62ab25eeb5Syz 	if (!(off & IP_OFFMASK)) {
637c478bd9Sstevel@tonic-gate 		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
647c478bd9Sstevel@tonic-gate 			printf(",%d", ntohs(tcp->th_dport));
657c478bd9Sstevel@tonic-gate 		if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags != 0)) {
667c478bd9Sstevel@tonic-gate 			putchar(' ');
677c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_FIN)
687c478bd9Sstevel@tonic-gate 				putchar('F');
697c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_SYN)
707c478bd9Sstevel@tonic-gate 				putchar('S');
717c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_RST)
727c478bd9Sstevel@tonic-gate 				putchar('R');
737c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_PUSH)
747c478bd9Sstevel@tonic-gate 				putchar('P');
757c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_ACK)
767c478bd9Sstevel@tonic-gate 				putchar('A');
777c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_URG)
787c478bd9Sstevel@tonic-gate 				putchar('U');
797c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_ECN)
807c478bd9Sstevel@tonic-gate 				putchar('E');
817c478bd9Sstevel@tonic-gate 			if (tcp->th_flags & TH_CWR)
827c478bd9Sstevel@tonic-gate 				putchar('C');
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	putchar('\n');
877c478bd9Sstevel@tonic-gate }
88