xref: /netbsd-src/external/bsd/ipf/dist/ipsd/slinux.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: slinux.c,v 1.1.1.1 2012/03/23 21:20:06 christos Exp $	*/
2 
3 /*
4  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  */
9 
10 #include <stdio.h>
11 #include <netdb.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/timeb.h>
18 #include <sys/socket.h>
19 #include <sys/file.h>
20 #include <sys/ioctl.h>
21 #include <sys/dir.h>
22 #include <linux/netdevice.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
26 #include <netinet/tcp.h>
27 #include "ip_compat.h"
28 #include "tcpip.h"
29 
30 #ifndef	lint
31 static const char sccsid[] = "@(#)slinux.c	1.1 12/3/95 (C) 1995 Darren Reed";
32 #endif
33 
34 #define BUFSPACE	32768
35 
36 /*
37  * Be careful to only include those defined in the flags option for the
38  * interface are included in the header size.
39  */
40 
41 static	int	timeout;
42 static	char	*eth_dev = NULL;
43 
44 
45 int	ack_recv(bp)
46 	char	*bp;
47 {
48 	struct	tcpip	tip;
49 	tcphdr_t	*tcp;
50 	ip_t	*ip;
51 
52 	ip = (struct ip *)&tip;
53 	tcp = (tcphdr_t *)(ip + 1);
54 
55 	bcopy(bp, (char *)&tip, sizeof(tip));
56 	bcopy(bp + (ip.ip_hl << 2), (char *)tcp, sizeof(*tcp));
57 	if (0 == detect(ip, tcp))
58 		return 1;
59 	return 0;
60 }
61 
62 
63 void	readloop(fd, port, dst)
64 	int 	fd, port;
65 	struct	in_addr dst;
66 {
67 	static	u_char	buf[BUFSPACE];
68 	struct	sockaddr dest;
69 	register u_char	*bp = buf;
70 	register int	cc;
71 	int	dlen, done = 0;
72 	time_t	now = time(NULL);
73 
74 	do {
75 		fflush(stdout);
76 		dlen = sizeof(dest);
77 		bzero((char *)&dest, dlen);
78 		cc = recvfrom(fd, buf, BUFSPACE, 0, &dest, &dlen);
79 		if (!cc)
80 			if ((time(NULL) - now) > timeout)
81 				return done;
82 			else
83 				continue;
84 
85 		if (bp[12] != 0x8 || bp[13] != 0)
86 			continue;	/* not ip */
87 
88 		/*
89 		 * get rid of non-tcp or fragmented packets here.
90 		 */
91 		if (cc >= sizeof(struct tcpiphdr))
92 		    {
93 			if (((bp[14+9] != IPPROTO_TCP) &&
94 			     (bp[14+9] != IPPROTO_UDP)) ||
95 			    (bp[14+6] & 0x1f) || (bp[14+6] & 0xff))
96 				continue;
97 			done += ack_recv(bp + 14);
98 		    }
99 	} while (cc >= 0);
100 	perror("read");
101 	exit(-1);
102 }
103 
104 int	initdevice(dev, tout)
105 	char	*dev;
106 	int	tout;
107 {
108 	int fd;
109 
110 	eth_dev = strdup(dev);
111 	if ((fd = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_IP))) == -1)
112 	    {
113 		perror("socket(SOCK_PACKET)");
114 		exit(-1);
115 	    }
116 
117 	return fd;
118 }
119