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