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 2002/11/02 13:27:29 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 22 if (IP_V(ip) == 6) 23 len = ntohs(((u_short *)ip)[2]) + 40; 24 else 25 len = ntohs(ip->ip_len); 26 27 if ((opts & OPT_HEX) == OPT_HEX) { 28 u_char *s; 29 int i; 30 31 for (s = (u_char *)ip, i = 0; i < len; i++) { 32 printf("%02x", *s++ & 0xff); 33 if (len - i > 1) { 34 i++; 35 printf("%02x", *s++ & 0xff); 36 } 37 putchar(' '); 38 } 39 putchar('\n'); 40 return; 41 } 42 43 if (IP_V(ip) == 6) { 44 printpacket6(ip); 45 return; 46 } 47 48 tcp = (struct tcphdr *)((char *)ip + (IP_HL(ip) << 2)); 49 printf("ip %d(%d) %d", ntohs(ip->ip_len), IP_HL(ip) << 2, ip->ip_p); 50 if (ip->ip_off & IP_OFFMASK) 51 printf(" @%d", ip->ip_off << 3); 52 printf(" %s", inet_ntoa(ip->ip_src)); 53 if (!(ip->ip_off & IP_OFFMASK)) 54 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) 55 printf(",%d", ntohs(tcp->th_sport)); 56 printf(" > "); 57 printf("%s", inet_ntoa(ip->ip_dst)); 58 if (!(ip->ip_off & IP_OFFMASK)) { 59 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) 60 printf(",%d", ntohs(tcp->th_dport)); 61 if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags != 0)) { 62 putchar(' '); 63 if (tcp->th_flags & TH_FIN) 64 putchar('F'); 65 if (tcp->th_flags & TH_SYN) 66 putchar('S'); 67 if (tcp->th_flags & TH_RST) 68 putchar('R'); 69 if (tcp->th_flags & TH_PUSH) 70 putchar('P'); 71 if (tcp->th_flags & TH_ACK) 72 putchar('A'); 73 if (tcp->th_flags & TH_URG) 74 putchar('U'); 75 if (tcp->th_flags & TH_ECN) 76 putchar('E'); 77 if (tcp->th_flags & TH_CWR) 78 putchar('C'); 79 } 80 } 81 82 putchar('\n'); 83 } 84