xref: /freebsd/sbin/ipf/ipsend/ipresend.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * ipresend.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 <stdlib.h>
24 #include <unistd.h>
25 #include <netdb.h>
26 #include <string.h>
27 #include "ipsend.h"
28 
29 
30 extern	char	*optarg;
31 extern	int	optind;
32 #ifndef	NO_IPF
33 extern	struct	ipread	pcap, iphex, iptext;
34 #endif
35 
36 int	opts = 0;
37 #ifndef	DEFAULT_DEVICE
38 #  ifdef	sun
39 char	default_device[] = "le0";
40 #  else
41 char	default_device[] = "lan0";
42 #  endif
43 #else
44 char	default_device[] = DEFAULT_DEVICE;
45 #endif
46 
47 
48 static	void	usage(char *);
49 int	main(int, char **);
50 
51 
52 static void usage(prog)
53 	char	*prog;
54 {
55 	fprintf(stderr, "Usage: %s [options] <-r filename|-R filename>\n\
56 \t\t-r filename\tsnoop data file to resend\n\
57 \t\t-R filename\tlibpcap data file to resend\n\
58 \toptions:\n\
59 \t\t-d device\tSend out on this device\n\
60 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
61 \t\t-m mtu\t\tfake MTU to use when sending out\n\
62 ", prog);
63 	exit(1);
64 }
65 
66 
67 int
68 main(int argc, char **argv)
69 {
70 	struct	in_addr	gwip;
71 	struct	ipread	*ipr = NULL;
72 	char	*name =  argv[0], *gateway = NULL, *dev = NULL;
73 	char	*resend = NULL;
74 	int	mtu = 1500, c;
75 
76 	while ((c = getopt(argc, argv, "EHPRSTXd:g:m:r:")) != -1)
77 		switch (c)
78 		{
79 		case 'd' :
80 			dev = optarg;
81 			break;
82 		case 'g' :
83 			gateway = optarg;
84 			break;
85 		case 'm' :
86 			mtu = atoi(optarg);
87 			if (mtu < 28)
88 			    {
89 				fprintf(stderr, "mtu must be > 28\n");
90 				exit(1);
91 			    }
92 		case 'r' :
93 			resend = optarg;
94 			break;
95 		case 'R' :
96 			opts |= OPT_RAW;
97 			break;
98 #ifndef	NO_IPF
99 		case 'H' :
100 			ipr = &iphex;
101 			break;
102 		case 'P' :
103 			ipr = &pcap;
104 			break;
105 		case 'X' :
106 			ipr = &iptext;
107 			break;
108 #endif
109 		default :
110 			fprintf(stderr, "Unknown option \"%c\"\n", c);
111 			usage(name);
112 		}
113 
114 	if (!ipr || !resend)
115 		usage(name);
116 
117 	gwip.s_addr = 0;
118 	if (gateway && resolve(gateway, (char *)&gwip) == -1)
119 	    {
120 		fprintf(stderr,"Cant resolve %s\n", gateway);
121 		exit(2);
122 	    }
123 
124 	if (!dev)
125 		dev = default_device;
126 
127 	printf("Device:  %s\n", dev);
128 	printf("Gateway: %s\n", inet_ntoa(gwip));
129 	printf("mtu:     %d\n", mtu);
130 
131 	return (ip_resend(dev, mtu, ipr, gwip, resend));
132 }
133