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