snj doesn't like my accent, so use proper English month names.
[netbsd-mini2440.git] / dist / ipf / ipsd / slinux.c
blob3b786b04b88c9abb74bab0c9ce45656e5f809a74
1 /* $NetBSD$ */
3 /*
4 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
8 */
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"
30 #ifndef lint
31 static const char sccsid[] = "@(#)slinux.c 1.1 12/3/95 (C) 1995 Darren Reed";
32 #endif
34 #define BUFSPACE 32768
37 * Be careful to only include those defined in the flags option for the
38 * interface are included in the header size.
41 static int timeout;
42 static char *eth_dev = NULL;
45 int ack_recv(bp)
46 char *bp;
48 struct tcpip tip;
49 tcphdr_t *tcp;
50 ip_t *ip;
52 ip = (struct ip *)&tip;
53 tcp = (tcphdr_t *)(ip + 1);
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;
63 void readloop(fd, port, dst)
64 int fd, port;
65 struct in_addr dst;
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);
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;
85 if (bp[12] != 0x8 || bp[13] != 0)
86 continue; /* not ip */
89 * get rid of non-tcp or fragmented packets here.
91 if (cc >= sizeof(struct tcpiphdr))
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);
99 } while (cc >= 0);
100 perror("read");
101 exit(-1);
104 int initdevice(dev, tout)
105 char *dev;
106 int tout;
108 int fd;
110 eth_dev = strdup(dev);
111 if ((fd = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_IP))) == -1)
113 perror("socket(SOCK_PACKET)");
114 exit(-1);
117 return fd;