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