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