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