1*07967fb1Smrg /* $NetBSD: ipresend.c,v 1.3 2018/02/04 08:19:42 mrg Exp $ */
2bc4097aaSchristos
3bc4097aaSchristos /*
4bc4097aaSchristos * ipresend.c (C) 1995-1998 Darren Reed
5bc4097aaSchristos *
6bc4097aaSchristos * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos *
8bc4097aaSchristos */
9bc4097aaSchristos #if !defined(lint)
10*07967fb1Smrg static __attribute__((__used__)) const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
11*07967fb1Smrg static __attribute__((__used__)) const char rcsid[] = "@(#)Id: ipresend.c,v 1.1.1.2 2012/07/22 13:44:36 darrenr Exp $";
12bc4097aaSchristos #endif
13bc4097aaSchristos #include <sys/param.h>
14bc4097aaSchristos #include <sys/types.h>
15bc4097aaSchristos #include <sys/time.h>
16bc4097aaSchristos #include <sys/socket.h>
17bc4097aaSchristos #include <netinet/in.h>
18bc4097aaSchristos #include <arpa/inet.h>
19bc4097aaSchristos #include <netinet/in_systm.h>
20bc4097aaSchristos #include <netinet/ip.h>
21bc4097aaSchristos #ifndef linux
22bc4097aaSchristos #include <netinet/ip_var.h>
23bc4097aaSchristos #endif
24bc4097aaSchristos #include <stdio.h>
25bc4097aaSchristos #include <stdlib.h>
26bc4097aaSchristos #include <unistd.h>
27bc4097aaSchristos #include <netdb.h>
28bc4097aaSchristos #include <string.h>
29bc4097aaSchristos #include "ipsend.h"
30bc4097aaSchristos
31bc4097aaSchristos
32bc4097aaSchristos extern char *optarg;
33bc4097aaSchristos extern int optind;
34bc4097aaSchristos #ifndef NO_IPF
35bc4097aaSchristos extern struct ipread pcap, iphex, iptext;
36bc4097aaSchristos #endif
37bc4097aaSchristos
38bc4097aaSchristos int opts = 0;
39bc4097aaSchristos #ifndef DEFAULT_DEVICE
40bc4097aaSchristos # ifdef linux
41bc4097aaSchristos char default_device[] = "eth0";
42bc4097aaSchristos # else
43bc4097aaSchristos # ifdef sun
44bc4097aaSchristos char default_device[] = "le0";
45bc4097aaSchristos # else
46bc4097aaSchristos # ifdef ultrix
47bc4097aaSchristos char default_device[] = "ln0";
48bc4097aaSchristos # else
49bc4097aaSchristos # ifdef __bsdi__
50bc4097aaSchristos char default_device[] = "ef0";
51bc4097aaSchristos # else
52bc4097aaSchristos # ifdef __sgi
53bc4097aaSchristos char default_device[] = "ec0";
54bc4097aaSchristos # else
55bc4097aaSchristos char default_device[] = "lan0";
56bc4097aaSchristos # endif
57bc4097aaSchristos # endif
58bc4097aaSchristos # endif
59bc4097aaSchristos # endif
60bc4097aaSchristos # endif
61bc4097aaSchristos #else
62bc4097aaSchristos char default_device[] = DEFAULT_DEVICE;
63bc4097aaSchristos #endif
64bc4097aaSchristos
65bc4097aaSchristos
66bc4097aaSchristos static void usage __P((char *));
67bc4097aaSchristos int main __P((int, char **));
68bc4097aaSchristos
69bc4097aaSchristos
usage(prog)70bc4097aaSchristos static void usage(prog)
71bc4097aaSchristos char *prog;
72bc4097aaSchristos {
73bc4097aaSchristos fprintf(stderr, "Usage: %s [options] <-r filename|-R filename>\n\
74bc4097aaSchristos \t\t-r filename\tsnoop data file to resend\n\
75bc4097aaSchristos \t\t-R filename\tlibpcap data file to resend\n\
76bc4097aaSchristos \toptions:\n\
77bc4097aaSchristos \t\t-d device\tSend out on this device\n\
78bc4097aaSchristos \t\t-g gateway\tIP gateway to use if non-local dest.\n\
79bc4097aaSchristos \t\t-m mtu\t\tfake MTU to use when sending out\n\
80bc4097aaSchristos ", prog);
81bc4097aaSchristos exit(1);
82bc4097aaSchristos }
83bc4097aaSchristos
84bc4097aaSchristos
main(argc,argv)85bc4097aaSchristos int main(argc, argv)
86bc4097aaSchristos int argc;
87bc4097aaSchristos char **argv;
88bc4097aaSchristos {
89bc4097aaSchristos struct in_addr gwip;
90bc4097aaSchristos struct ipread *ipr = NULL;
91bc4097aaSchristos char *name = argv[0], *gateway = NULL, *dev = NULL;
92bc4097aaSchristos char *resend = NULL;
93bc4097aaSchristos int mtu = 1500, c;
94bc4097aaSchristos
95bc4097aaSchristos while ((c = getopt(argc, argv, "EHPRSTXd:g:m:r:")) != -1)
96bc4097aaSchristos switch (c)
97bc4097aaSchristos {
98bc4097aaSchristos case 'd' :
99bc4097aaSchristos dev = optarg;
100bc4097aaSchristos break;
101bc4097aaSchristos case 'g' :
102bc4097aaSchristos gateway = optarg;
103bc4097aaSchristos break;
104bc4097aaSchristos case 'm' :
105bc4097aaSchristos mtu = atoi(optarg);
106bc4097aaSchristos if (mtu < 28)
107bc4097aaSchristos {
108bc4097aaSchristos fprintf(stderr, "mtu must be > 28\n");
109bc4097aaSchristos exit(1);
110bc4097aaSchristos }
111bc4097aaSchristos case 'r' :
112bc4097aaSchristos resend = optarg;
113bc4097aaSchristos break;
114bc4097aaSchristos case 'R' :
115bc4097aaSchristos opts |= OPT_RAW;
116bc4097aaSchristos break;
117bc4097aaSchristos #ifndef NO_IPF
118bc4097aaSchristos case 'H' :
119bc4097aaSchristos ipr = &iphex;
120bc4097aaSchristos break;
121bc4097aaSchristos case 'P' :
122bc4097aaSchristos ipr = &pcap;
123bc4097aaSchristos break;
124bc4097aaSchristos case 'X' :
125bc4097aaSchristos ipr = &iptext;
126bc4097aaSchristos break;
127bc4097aaSchristos #endif
128bc4097aaSchristos default :
129bc4097aaSchristos fprintf(stderr, "Unknown option \"%c\"\n", c);
130bc4097aaSchristos usage(name);
131bc4097aaSchristos }
132bc4097aaSchristos
133bc4097aaSchristos if (!ipr || !resend)
134bc4097aaSchristos usage(name);
135bc4097aaSchristos
136bc4097aaSchristos gwip.s_addr = 0;
137bc4097aaSchristos if (gateway && resolve(gateway, (char *)&gwip) == -1)
138bc4097aaSchristos {
139bc4097aaSchristos fprintf(stderr,"Cant resolve %s\n", gateway);
140bc4097aaSchristos exit(2);
141bc4097aaSchristos }
142bc4097aaSchristos
143bc4097aaSchristos if (!dev)
144bc4097aaSchristos dev = default_device;
145bc4097aaSchristos
146bc4097aaSchristos printf("Device: %s\n", dev);
147bc4097aaSchristos printf("Gateway: %s\n", inet_ntoa(gwip));
148bc4097aaSchristos printf("mtu: %d\n", mtu);
149bc4097aaSchristos
150bc4097aaSchristos return ip_resend(dev, mtu, ipr, gwip, resend);
151bc4097aaSchristos }
152