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