xref: /freebsd/sbin/ipf/ipsend/iptest.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
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
67 main(int argc, char **argv)
68 {
69 	struct	tcpiphdr *ti;
70 	struct	in_addr	gwip;
71 	ip_t	*ip;
72 	char	*name =  argv[0], host[MAXHOSTNAMELEN + 1];
73 	char	*gateway = NULL, *dev = NULL;
74 	char	*src = NULL, *dst;
75 	int	mtu = 1500, tests = 0, pointtest = 0, c;
76 
77 	/*
78 	 * 65535 is maximum packet size...you never know...
79 	 */
80 	ip = (ip_t *)calloc(1, 65536);
81 	ti = (struct tcpiphdr *)ip;
82 	ip->ip_len = sizeof(*ip);
83 	IP_HL_A(ip, sizeof(*ip) >> 2);
84 
85 	while ((c = getopt(argc, argv, "1234567d:g:m:p:s:")) != -1)
86 		switch (c)
87 		{
88 		case '1' :
89 		case '2' :
90 		case '3' :
91 		case '4' :
92 		case '5' :
93 		case '6' :
94 		case '7' :
95 			tests = c - '0';
96 			break;
97 		case 'd' :
98 			dev = optarg;
99 			break;
100 		case 'g' :
101 			gateway = optarg;
102 			break;
103 		case 'm' :
104 			mtu = atoi(optarg);
105 			if (mtu < 28)
106 			    {
107 				fprintf(stderr, "mtu must be > 28\n");
108 				exit(1);
109 			    }
110 			break;
111 		case 'p' :
112 			pointtest = atoi(optarg);
113 			break;
114 		case 's' :
115 			src = optarg;
116 			break;
117 		default :
118 			fprintf(stderr, "Unknown option \"%c\"\n", c);
119 			usage(name);
120 		}
121 
122 	if ((argc <= optind) || !argv[optind])
123 		usage(name);
124 	dst = argv[optind++];
125 
126 	if (!src)
127 	    {
128 		gethostname(host, sizeof(host));
129 		host[sizeof(host) - 1] = '\0';
130 		src = host;
131 	    }
132 
133 	if (resolve(dst, (char *)&ip->ip_dst) == -1)
134 	    {
135 		fprintf(stderr,"Cant resolve %s\n", dst);
136 		exit(2);
137 	    }
138 
139 	if (resolve(src, (char *)&ip->ip_src) == -1)
140 	    {
141 		fprintf(stderr,"Cant resolve %s\n", src);
142 		exit(2);
143 	    }
144 
145 	if (!gateway)
146 		gwip = ip->ip_dst;
147 	else if (resolve(gateway, (char *)&gwip) == -1)
148 	    {
149 		fprintf(stderr,"Cant resolve %s\n", gateway);
150 		exit(2);
151 	    }
152 
153 
154 	if (!dev)
155 		dev = default_device;
156 	printf("Device:  %s\n", dev);
157 	printf("Source:  %s\n", inet_ntoa(ip->ip_src));
158 	printf("Dest:    %s\n", inet_ntoa(ip->ip_dst));
159 	printf("Gateway: %s\n", inet_ntoa(gwip));
160 	printf("mtu:     %d\n", mtu);
161 
162 	switch (tests)
163 	{
164 	case 1 :
165 		ip_test1(dev, mtu, (ip_t *)ti, gwip, pointtest);
166 		break;
167 	case 2 :
168 		ip_test2(dev, mtu, (ip_t *)ti, gwip, pointtest);
169 		break;
170 	case 3 :
171 		ip_test3(dev, mtu, (ip_t *)ti, gwip, pointtest);
172 		break;
173 	case 4 :
174 		ip_test4(dev, mtu, (ip_t *)ti, gwip, pointtest);
175 		break;
176 	case 5 :
177 		ip_test5(dev, mtu, (ip_t *)ti, gwip, pointtest);
178 		break;
179 	case 6 :
180 		ip_test6(dev, mtu, (ip_t *)ti, gwip, pointtest);
181 		break;
182 	case 7 :
183 		ip_test7(dev, mtu, (ip_t *)ti, gwip, pointtest);
184 		break;
185 	default :
186 		ip_test1(dev, mtu, (ip_t *)ti, gwip, pointtest);
187 		ip_test2(dev, mtu, (ip_t *)ti, gwip, pointtest);
188 		ip_test3(dev, mtu, (ip_t *)ti, gwip, pointtest);
189 		ip_test4(dev, mtu, (ip_t *)ti, gwip, pointtest);
190 		ip_test5(dev, mtu, (ip_t *)ti, gwip, pointtest);
191 		ip_test6(dev, mtu, (ip_t *)ti, gwip, pointtest);
192 		ip_test7(dev, mtu, (ip_t *)ti, gwip, pointtest);
193 		break;
194 	}
195 	return (0);
196 }
197