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