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