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