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