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