Switch ipfw from ipfw1 to ipfw2.
[dragonfly/port-amd64.git] / contrib / ipfilter / misc.c
blobe39b98fd76f3d91d3e8e063e57086f3d15ad8163
1 /*
2 * Copyright (C) 1993-2002 by Darren Reed.
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"
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
59 extern int opts;
62 void printpacket(ip)
63 ip_t *ip;
65 tcphdr_t *tcp;
66 u_short len;
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;
75 if ((opts & OPT_HEX) == OPT_HEX) {
76 u_char *s;
77 int i;
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);
85 if (i + 1 != len)
86 putchar(' ');
88 putchar('\n');
89 return;
92 if (ip->ip_v == 6) {
93 printpacket6(ip);
94 return;
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');
130 putchar('\n');
135 * This is meant to work without the IPv6 header files being present or
136 * the inet_ntop() library.
138 void printpacket6(ip)
139 ip_t *ip;
141 u_char *buf, p, hops;
142 u_short plen, *addrs;
143 tcphdr_t *tcp;
144 u_32_t flow;
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;
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');
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
184 va_list pvar;
186 va_start(pvar, fmt);
187 if (opts & OPT_VERBOSE)
188 vprintf(fmt, pvar);
189 va_end(pvar);
193 #ifdef __STDC__
194 void debug(char *fmt, ...)
195 #else
196 void debug(fmt, va_alist)
197 char *fmt;
198 va_dcl
199 #endif
201 va_list pvar;
203 va_start(pvar, fmt);
204 if (opts & OPT_DEBUG)
205 vprintf(fmt, pvar);
206 va_end(pvar);