Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / ipsend / iptest.c
1 /*
2  * ipsend.c (C) 1995-1998 Darren Reed
3  *
4  * This was written to test what size TCP fragments would get through
5  * various TCP/IP packet filters, as used in IP firewalls.  In certain
6  * conditions, enough of the TCP header is missing for unpredictable
7  * results unless the filter is aware that this can happen.
8  *
9  * See the IPFILTER.LICENCE file for details on licencing.
10  */
11 #if defined(__sgi) && (IRIX > 602)
12 # include <sys/ptimers.h>
13 #endif
14 #include <stdio.h>
15 #include <netdb.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netinet/in_systm.h>
26 #include <netinet/ip.h>
27 #include <netinet/tcp.h>
28 #include <netinet/udp.h>
29 #include <netinet/ip_icmp.h>
30 #ifndef linux
31 #include <netinet/ip_var.h>
32 #endif
33 #ifdef  linux
34 #include <linux/sockios.h>
35 #endif
36 #include "ipsend.h"
37
38 #if !defined(lint)
39 static const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
40 static const char rcsid[] = "@(#)$Id: iptest.c,v 2.2.2.4 2002/12/06 11:40:35 darrenr Exp $";
41 #endif
42
43
44 extern  char    *optarg;
45 extern  int     optind;
46
47 char    options[68];
48 #ifdef  linux
49 char    default_device[] = "eth0";
50 #else
51 # ifdef sun
52 char    default_device[] = "le0";
53 # else
54 #  ifdef        ultrix
55 char    default_device[] = "ln0";
56 #  else
57 #   ifdef       __bsdi__
58 char    default_device[] = "ef0";
59 #   else
60 #    ifdef      __sgi
61 char    default_device[] = "ec0";
62 #    else
63 char    default_device[] = "lan0";
64 #    endif
65 #   endif
66 #  endif
67 # endif
68 #endif
69
70 static  void    usage __P((char *));
71 int     main __P((int, char **));
72
73
74 static void usage(prog)
75 char *prog;
76 {
77         fprintf(stderr, "Usage: %s [options] dest\n\
78 \toptions:\n\
79 \t\t-d device\tSend out on this device\n\
80 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
81 \t\t-m mtu\t\tfake MTU to use when sending out\n\
82 \t\t-p pointtest\t\n\
83 \t\t-s src\t\tsource address for IP packet\n\
84 \t\t-1 \t\tPerform test 1 (IP header)\n\
85 \t\t-2 \t\tPerform test 2 (IP options)\n\
86 \t\t-3 \t\tPerform test 3 (ICMP)\n\
87 \t\t-4 \t\tPerform test 4 (UDP)\n\
88 \t\t-5 \t\tPerform test 5 (TCP)\n\
89 \t\t-6 \t\tPerform test 6 (overlapping fragments)\n\
90 \t\t-7 \t\tPerform test 7 (random packets)\n\
91 ", prog);
92         exit(1);
93 }
94
95
96 int main(argc, argv)
97 int argc;
98 char **argv;
99 {
100         struct  tcpiphdr *ti;
101         struct  in_addr gwip;
102         ip_t    *ip;
103         char    *name =  argv[0], host[MAXHOSTNAMELEN + 1];
104         char    *gateway = NULL, *dev = NULL;
105         char    *src = NULL, *dst;
106         int     mtu = 1500, tests = 0, pointtest = 0, c;
107
108         /*
109          * 65535 is maximum packet size...you never know...
110          */
111         ip = (ip_t *)calloc(1, 65536);
112         ti = (struct tcpiphdr *)ip;
113         ip->ip_len = sizeof(*ip);
114         ip->ip_hl = sizeof(*ip) >> 2;
115
116         while ((c = getopt(argc, argv, "1234567d:g:m:p:s:")) != -1)
117                 switch (c)
118                 {
119                 case '1' :
120                 case '2' :
121                 case '3' :
122                 case '4' :
123                 case '5' :
124                 case '6' :
125                 case '7' :
126                         tests = c - '0';
127                         break;
128                 case 'd' :
129                         dev = optarg;
130                         break;
131                 case 'g' :
132                         gateway = optarg;
133                         break;
134                 case 'm' :
135                         mtu = atoi(optarg);
136                         if (mtu < 28)
137                             {
138                                 fprintf(stderr, "mtu must be > 28\n");
139                                 exit(1);
140                             }
141                         break;
142                 case 'p' :
143                         pointtest = atoi(optarg);
144                         break;
145                 case 's' :
146                         src = optarg;
147                         break;
148                 default :
149                         fprintf(stderr, "Unknown option \"%c\"\n", c);
150                         usage(name);
151                 }
152
153         if ((argc <= optind) || !argv[optind])
154                 usage(name);
155         dst = argv[optind++];
156
157         if (!src)
158             {
159                 gethostname(host, sizeof(host));
160                 host[sizeof(host) - 1] = '\0';
161                 src = host;
162             }
163
164         if (resolve(dst, (char *)&ip->ip_dst) == -1)
165             {
166                 fprintf(stderr,"Cant resolve %s\n", dst);
167                 exit(2);
168             }
169
170         if (resolve(src, (char *)&ip->ip_src) == -1)
171             {
172                 fprintf(stderr,"Cant resolve %s\n", src);
173                 exit(2);
174             }
175
176         if (!gateway)
177                 gwip = ip->ip_dst;
178         else if (resolve(gateway, (char *)&gwip) == -1)
179             {
180                 fprintf(stderr,"Cant resolve %s\n", gateway);
181                 exit(2);
182             }
183
184
185         if (!dev)
186                 dev = default_device;
187         printf("Device:  %s\n", dev);
188         printf("Source:  %s\n", inet_ntoa(ip->ip_src));
189         printf("Dest:    %s\n", inet_ntoa(ip->ip_dst));
190         printf("Gateway: %s\n", inet_ntoa(gwip));
191         printf("mtu:     %d\n", mtu);
192
193         switch (tests)
194         {
195         case 1 :
196                 ip_test1(dev, mtu, (ip_t *)ti, gwip, pointtest);
197                 break;
198         case 2 :
199                 ip_test2(dev, mtu, (ip_t *)ti, gwip, pointtest);
200                 break;
201         case 3 :
202                 ip_test3(dev, mtu, (ip_t *)ti, gwip, pointtest);
203                 break;
204         case 4 :
205                 ip_test4(dev, mtu, (ip_t *)ti, gwip, pointtest);
206                 break;
207         case 5 :
208                 ip_test5(dev, mtu, (ip_t *)ti, gwip, pointtest);
209                 break;
210         case 6 :
211                 ip_test6(dev, mtu, (ip_t *)ti, gwip, pointtest);
212                 break;
213         case 7 :
214                 ip_test7(dev, mtu, (ip_t *)ti, gwip, pointtest);
215                 break;
216         default :
217                 ip_test1(dev, mtu, (ip_t *)ti, gwip, pointtest);
218                 ip_test2(dev, mtu, (ip_t *)ti, gwip, pointtest);
219                 ip_test3(dev, mtu, (ip_t *)ti, gwip, pointtest);
220                 ip_test4(dev, mtu, (ip_t *)ti, gwip, pointtest);
221                 ip_test5(dev, mtu, (ip_t *)ti, gwip, pointtest);
222                 ip_test6(dev, mtu, (ip_t *)ti, gwip, pointtest);
223                 ip_test7(dev, mtu, (ip_t *)ti, gwip, pointtest);
224                 break;
225         }
226         return 0;
227 }