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