Merge from vendor branch NTPD:
[dragonfly.git] / contrib / ipfilter / misc.c
1 /*
2  * Copyright (C) 1993-2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
8 #endif
9 #if (SOLARIS2 >= 7)
10 # define _SYS_VARARGS_H
11 # define _VARARGS_H
12 #endif
13 #if defined(__STDC__)
14 # include <stdarg.h>
15 #else
16 # include <varargs.h>
17 #endif
18 #include <stdio.h>
19 #include <assert.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #if !defined(__SVR4) && !defined(__svr4__)
23 #include <strings.h>
24 #else
25 #include <sys/byteorder.h>
26 #endif
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stddef.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netinet/in_systm.h>
37 #ifndef linux
38 #include <netinet/ip_var.h>
39 #endif
40 #include <netinet/ip.h>
41 #include <netinet/udp.h>
42 #include <netinet/tcp.h>
43 #include <netinet/ip_icmp.h>
44 #include <net/if.h>
45 #include <netdb.h>
46 #include <arpa/nameser.h>
47 #include <resolv.h>
48 #include "ip_compat.h"
49 #include <netinet/tcpip.h>
50 #include "ip_fil.h"
51 #include "ipf.h"
52 #include "ipt.h"
53
54 #if !defined(lint)
55 static const char sccsid[] = "@(#)misc.c        1.3 2/4/96 (C) 1995 Darren Reed";
56 static const char rcsid[] = "@(#)$Id: misc.c,v 2.2.2.9 2002/12/06 11:40:27 darrenr Exp $";
57 #endif
58
59 extern  int     opts;
60
61
62 void    printpacket(ip)
63 ip_t    *ip;
64 {
65         tcphdr_t        *tcp;
66         u_short len;
67
68         if (ip->ip_v == 4)
69                 len = ntohs(ip->ip_len);
70         else if (ip->ip_v == 6)
71                 len = ntohs(((u_short *)ip)[2]) + 40;
72         else
73                 len = 0;
74
75         if ((opts & OPT_HEX) == OPT_HEX) {
76                 u_char *s;
77                 int i;
78
79                 for (s = (u_char *)ip, i = 0; i < len; i++) {
80                         printf("%02x", *s++ & 0xff);
81                         if (len - i > 1) {
82                                 i++;
83                                 printf("%02x", *s++ & 0xff);
84                         }
85                         if (i + 1 != len)
86                                 putchar(' ');
87                 }
88                 putchar('\n');
89                 return;
90         }
91
92         if (ip->ip_v == 6) {
93                 printpacket6(ip);
94                 return;
95         }
96
97         tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
98         printf("ip %d(%d) %d", ntohs(ip->ip_len), ip->ip_hl << 2, ip->ip_p);
99         if (ip->ip_off & IP_OFFMASK)
100                 printf(" @%d", ip->ip_off << 3);
101         (void)printf(" %s", inet_ntoa(ip->ip_src));
102         if (!(ip->ip_off & IP_OFFMASK))
103                 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
104                         (void)printf(",%d", ntohs(tcp->th_sport));
105         (void)printf(" > ");
106         (void)printf("%s", inet_ntoa(ip->ip_dst));
107         if (!(ip->ip_off & IP_OFFMASK)) {
108                 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
109                         (void)printf(",%d", ntohs(tcp->th_dport));
110                 if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags)) {
111                         putchar(' ');
112                         if (tcp->th_flags & TH_FIN)
113                                 putchar('F');
114                         if (tcp->th_flags & TH_SYN)
115                                 putchar('S');
116                         if (tcp->th_flags & TH_RST)
117                                 putchar('R');
118                         if (tcp->th_flags & TH_PUSH)
119                                 putchar('P');
120                         if (tcp->th_flags & TH_ACK)
121                                 putchar('A');
122                         if (tcp->th_flags & TH_URG)
123                                 putchar('U');
124                         if (tcp->th_flags & TH_ECN)
125                                 putchar('E');
126                         if (tcp->th_flags & TH_CWR)
127                                 putchar('C');
128                 }
129         }
130         putchar('\n');
131 }
132
133
134 /*
135  * This is meant to work without the IPv6 header files being present or
136  * the inet_ntop() library.
137  */
138 void    printpacket6(ip)
139 ip_t    *ip;
140 {
141         u_char *buf, p, hops;
142         u_short plen, *addrs;
143         tcphdr_t *tcp;
144         u_32_t flow;
145
146         buf = (u_char *)ip;
147         tcp = (tcphdr_t *)(buf + 40);
148         p = buf[6];
149         hops = buf[7];
150         flow = ntohl(*(u_32_t *)buf);
151         flow &= 0xfffff;
152         plen = ntohs(*((u_short *)buf +2));
153         addrs = (u_short *)buf + 4;
154
155         printf("ip6/%d %d %#x %d", buf[0] & 0xf, plen, flow, p);
156         printf(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
157                 ntohs(addrs[0]), ntohs(addrs[1]), ntohs(addrs[2]),
158                 ntohs(addrs[3]), ntohs(addrs[4]), ntohs(addrs[5]),
159                 ntohs(addrs[6]), ntohs(addrs[7]));
160         if (plen >= 4)
161                 if (p == IPPROTO_TCP || p == IPPROTO_UDP)
162                         (void)printf(",%d", ntohs(tcp->th_sport));
163         printf(" >");
164         addrs += 8;
165         printf(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
166                 ntohs(addrs[0]), ntohs(addrs[1]), ntohs(addrs[2]),
167                 ntohs(addrs[3]), ntohs(addrs[4]), ntohs(addrs[5]),
168                 ntohs(addrs[6]), ntohs(addrs[7]));
169         if (plen >= 4)
170                 if (p == IPPROTO_TCP || p == IPPROTO_UDP)
171                         (void)printf(",%d", ntohs(tcp->th_dport));
172         putchar('\n');
173 }
174
175
176 #if defined(__STDC__)
177 void    verbose(char *fmt, ...)
178 #else
179 void    verbose(fmt, va_alist)
180 char    *fmt;
181 va_dcl
182 #endif
183 {
184         va_list pvar;
185
186         va_start(pvar, fmt);
187         if (opts & OPT_VERBOSE)
188                 vprintf(fmt, pvar);
189         va_end(pvar);
190 }
191
192
193 #ifdef  __STDC__
194 void    debug(char *fmt, ...)
195 #else
196 void    debug(fmt, va_alist)
197 char *fmt;
198 va_dcl
199 #endif
200 {
201         va_list pvar;
202
203         va_start(pvar, fmt);
204         if (opts & OPT_DEBUG)
205                 vprintf(fmt, pvar);
206         va_end(pvar);
207 }