Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / ipsend / ipresend.c
1 /*
2  * ipresend.c (C) 1995-1998 Darren Reed
3  *
4  * This was written to test what size TCP fragments would get through
5  * various TCP/IP packet filters, as used in IP firewalls.  In certain
6  * conditions, enough of the TCP header is missing for unpredictable
7  * results unless the filter is aware that this can happen.
8  *
9  * See the IPFILTER.LICENCE file for details on licencing.
10  */
11 #if defined(__sgi) && (IRIX > 602)
12 # include <sys/ptimers.h>
13 #endif
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/time.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netinet/in_systm.h>
26 #include <netinet/ip.h>
27 #include <netinet/tcp.h>
28 #include <netinet/udp.h>
29 #include <netinet/ip_icmp.h>
30 #ifndef linux
31 #include <netinet/ip_var.h>
32 #endif
33 #include "ipsend.h"
34
35 #if !defined(lint)
36 static const char sccsid[] = "%W% %G% (C)1995 Darren Reed";
37 static const char rcsid[] = "@(#)$Id: ipresend.c,v 2.1.4.4 2002/12/06 11:40:35 darrenr Exp $";
38 #endif
39
40
41 extern  char    *optarg;
42 extern  int     optind;
43 #ifndef NO_IPF
44 extern  struct  ipread  snoop, pcap, etherf, iphex, tcpd, iptext;
45 #endif
46
47 int     opts = 0;
48 #ifndef DEFAULT_DEVICE
49 # ifdef linux
50 char    default_device[] = "eth0";
51 # else
52 #  ifdef        sun
53 char    default_device[] = "le0";
54 #  else
55 #   ifdef       ultrix
56 char    default_device[] = "ln0";
57 #   else
58 #    ifdef      __bsdi__
59 char    default_device[] = "ef0";
60 #    else
61 #     ifdef     __sgi
62 char    default_device[] = "ec0";
63 #     else
64 char    default_device[] = "lan0";
65 #     endif
66 #    endif
67 #   endif
68 #  endif
69 # endif
70 #else
71 char    default_device[] = DEFAULT_DEVICE;
72 #endif
73
74
75 static  void    usage __P((char *));
76 int     main __P((int, char **));
77
78
79 static void usage(prog)
80 char    *prog;
81 {
82         fprintf(stderr, "Usage: %s [options] <-r filename|-R filename>\n\
83 \t\t-r filename\tsnoop data file to resend\n\
84 \t\t-R filename\tlibpcap data file to resend\n\
85 \toptions:\n\
86 \t\t-d device\tSend out on this device\n\
87 \t\t-g gateway\tIP gateway to use if non-local dest.\n\
88 \t\t-m mtu\t\tfake MTU to use when sending out\n\
89 ", prog);
90         exit(1);
91 }
92
93
94 int main(argc, argv)
95 int     argc;
96 char    **argv;
97 {
98         struct  in_addr gwip;
99         struct  ipread  *ipr = NULL;
100         char    *name =  argv[0], *gateway = NULL, *dev = NULL;
101         char    *resend = NULL;
102         int     mtu = 1500, c;
103
104         while ((c = getopt(argc, argv, "EHPRSTXd:g:m:r:")) != -1)
105                 switch (c)
106                 {
107                 case 'd' :
108                         dev = optarg;
109                         break;
110                 case 'g' :
111                         gateway = optarg;
112                         break;
113                 case 'm' :
114                         mtu = atoi(optarg);
115                         if (mtu < 28)
116                             {
117                                 fprintf(stderr, "mtu must be > 28\n");
118                                 exit(1);
119                             }
120                 case 'r' :
121                         resend = optarg;
122                         break;
123                 case 'R' :
124                         opts |= OPT_RAW;
125                         break;
126 #ifndef NO_IPF
127                 case 'E' :
128                         ipr = &etherf;
129                         break;
130                 case 'H' :
131                         ipr = &iphex;
132                         break;
133                 case 'P' :
134                         ipr = &pcap;
135                         break;
136                 case 'S' :
137                         ipr = &snoop;
138                         break;
139                 case 'T' :
140                         ipr = &tcpd;
141                         break;
142                 case 'X' :
143                         ipr = &iptext;
144                         break;
145 #endif
146                 default :
147                         fprintf(stderr, "Unknown option \"%c\"\n", c);
148                         usage(name);
149                 }
150
151         if (!ipr || !resend)
152                 usage(name);
153
154         gwip.s_addr = 0;
155         if (gateway && resolve(gateway, (char *)&gwip) == -1)
156             {
157                 fprintf(stderr,"Cant resolve %s\n", gateway);
158                 exit(2);
159             }
160
161         if (!dev)
162                 dev = default_device;
163
164         printf("Device:  %s\n", dev);
165         printf("Gateway: %s\n", inet_ntoa(gwip));
166         printf("mtu:     %d\n", mtu);
167
168         return ip_resend(dev, mtu, ipr, gwip, resend);
169 }