Adjust for symbol name changes.
[dragonfly.git] / contrib / ipfilter / ipsend / resend.c
1 /*
2  * resend.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 <netdb.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/socket.h>
22 #include <net/if.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 # include <netinet/if_ether.h>
33 # if __FreeBSD_version >= 300000
34 #  include <net/if_var.h>
35 # endif
36 #endif
37 #include "ipsend.h"
38
39 #if !defined(lint)
40 static const char sccsid[] = "@(#)resend.c      1.3 1/11/96 (C)1995 Darren Reed";
41 static const char rcsid[] = "@(#)$Id: resend.c,v 2.1.4.5 2002/12/06 11:40:36 darrenr Exp $";
42 #endif
43
44
45 extern  int     opts;
46
47 static  u_char  pbuf[65536];    /* 1 big packet */
48 void    printpacket __P((ip_t *));
49
50
51 void printpacket(ip)
52 ip_t    *ip;
53 {
54         tcphdr_t *t;
55         int i, j;
56
57         t = (tcphdr_t *)((char *)ip + (ip->ip_hl << 2));
58         if (ip->ip_tos)
59                 printf("tos %#x ", ip->ip_tos);
60         if (ip->ip_off & 0x3fff)
61                 printf("frag @%#x ", (ip->ip_off & 0x1fff) << 3);
62         printf("len %d id %d ", ip->ip_len, ip->ip_id);
63         printf("ttl %d p %d src %s", ip->ip_ttl, ip->ip_p,
64                 inet_ntoa(ip->ip_src));
65         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
66                 printf(",%d", t->th_sport);
67         printf(" dst %s", inet_ntoa(ip->ip_dst));
68         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
69                 printf(",%d", t->th_dport);
70         if (ip->ip_p == IPPROTO_TCP) {
71                 printf(" seq %lu:%lu flags ",
72                         (u_long)t->th_seq, (u_long)t->th_ack);
73                 for (j = 0, i = 1; i < 256; i *= 2, j++)
74                         if (t->th_flags & i)
75                                 printf("%c", "FSRPAU--"[j]);
76         }
77         putchar('\n');
78 }
79
80
81 int     ip_resend(dev, mtu, r, gwip, datain)
82 char    *dev;
83 int     mtu;
84 struct  in_addr gwip;
85 struct  ipread  *r;
86 char    *datain;
87 {
88         ether_header_t  *eh;
89         char    dhost[6];
90         ip_t    *ip;
91         int     fd, wfd = initdevice(dev, 0, 5), len, i;
92
93         if (datain)
94                 fd = (*r->r_open)(datain);
95         else
96                 fd = (*r->r_open)("-");
97  
98         if (fd < 0)
99                 exit(-1);
100
101         ip = (struct ip *)pbuf;
102         eh = (ether_header_t *)malloc(sizeof(*eh));
103         if(!eh)
104             {
105                 perror("malloc failed");
106                 return -2;
107             }
108
109         bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
110         if (gwip.s_addr && (arp((char *)&gwip, dhost) == -1))
111             {
112                 perror("arp");
113                 return -2;
114             }
115
116         while ((i = (*r->r_readip)((char *)pbuf, sizeof(pbuf), NULL, NULL)) > 0)
117             {
118                 if (!(opts & OPT_RAW)) {
119                         len = ntohs(ip->ip_len);
120                         eh = (ether_header_t *)realloc((char *)eh, sizeof(*eh) + len);
121                         eh->ether_type = htons((u_short)ETHERTYPE_IP);
122                         if (!gwip.s_addr) {
123                                 if (arp((char *)&gwip,
124                                         (char *)A_A eh->ether_dhost) == -1) {
125                                         perror("arp");
126                                         continue;
127                                 }
128                         } else
129                                 bcopy(dhost, (char *)A_A eh->ether_dhost,
130                                       sizeof(dhost));
131                         if (!ip->ip_sum)
132                                 ip->ip_sum = chksum((u_short *)ip,
133                                                     ip->ip_hl << 2);
134                         bcopy(ip, (char *)(eh + 1), len);
135                         len += sizeof(*eh);
136                         printpacket(ip);
137                 } else {
138                         eh = (ether_header_t *)pbuf;
139                         len = i;
140                 }
141
142                 if (sendip(wfd, (char *)eh, len) == -1)
143                     {
144                         perror("send_packet");
145                         break;
146                     }
147             }
148         (*r->r_close)();
149         return 0;
150 }