2 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
4 * See the IPFILTER.LICENCE file for details on licencing.
12 #include <sys/types.h>
13 #include <sys/param.h>
16 #include <sys/timeb.h>
17 #include <sys/socket.h>
19 #include <sys/ioctl.h>
21 #include <sys/fcntlcom.h>
23 #include <sys/dirent.h>
27 #include <netinet/in.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <netinet/if_ether.h>
31 #include <netinet/ip_var.h>
32 #include <netinet/udp.h>
33 #include <netinet/udp_var.h>
34 #include <netinet/tcp.h>
35 #include <netinet/tcpip.h>
36 #include "ip_compat.h"
39 static char sbpf
[] = "@(#)sbpf.c 1.2 12/3/95 (C)1995 Darren Reed";
44 (001) jeq #0x800 jt 2 jf 5
46 (003) jeq #0x6 jt 4 jf 5
50 struct bpf_insn filter
[] = {
51 /* 0. */ { BPF_LD
|BPF_H
|BPF_ABS
, 0, 0, 12 },
52 /* 1. */ { BPF_JMP
|BPF_JEQ
, 0, 3, 0x0800 },
53 /* 2. */ { BPF_LD
|BPF_B
|BPF_ABS
, 0, 0, 23 },
54 /* 3. */ { BPF_JMP
|BPF_JEQ
, 0, 1, 0x06 },
55 /* 4. */ { BPF_RET
, 0, 0, 68 },
56 /* 5. */ { BPF_RET
, 0, 0, 0 }
59 * the code herein is dervied from libpcap.
61 static u_char
*buf
= NULL
;
62 static u_int bufsize
= 32768, timeout
= 1;
73 tcp
= (tcphdr_t
*)(ip
+ 1);
74 bcopy(ep
+ 14, (char *)ip
, sizeof(*ip
));
75 bcopy(ep
+ 14 + (ip
->ip_hl
<< 2), (char *)tcp
, sizeof(*tcp
));
76 if (ip
->ip_p
!= IPPROTO_TCP
&& ip
->ip_p
!= IPPROTO_UDP
)
78 if (ip
->ip_p
& 0x1fff != 0)
80 if (0 == detect(ip
, tcp
))
86 int readloop(fd
, port
, dst
)
90 register u_char
*bp
, *cp
, *bufend
;
91 register struct bpf_hdr
*bh
;
93 time_t in
= time(NULL
);
96 while ((cc
= read(fd
, buf
, bufsize
)) >= 0) {
97 if (!cc
&& (time(NULL
) - in
) > timeout
)
102 * loop through each snapshot in the chunk
104 while (bp
< bufend
) {
105 bh
= (struct bpf_hdr
*)bp
;
106 cp
= bp
+ bh
->bh_hdrlen
;
107 done
+= ack_recv(cp
);
108 bp
+= BPF_WORDALIGN(bh
->bh_caplen
+ bh
->bh_hdrlen
);
116 int initdevice(device
, tout
)
120 struct bpf_program prog
;
121 struct bpf_version bv
;
127 for (i
= 0; i
< 16; i
++)
129 (void) sprintf(bpfname
, "/dev/bpf%d", i
);
130 if ((fd
= open(bpfname
, O_RDWR
)) >= 0)
135 fprintf(stderr
, "no bpf devices available as /dev/bpfxx\n");
139 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0)
141 perror("BIOCVERSION");
144 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
145 bv
.bv_minor
< BPF_MINOR_VERSION
)
147 fprintf(stderr
, "kernel bpf (v%d.%d) filter out of date:\n",
148 bv
.bv_major
, bv
.bv_minor
);
149 fprintf(stderr
, "current version: %d.%d\n",
150 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
);
154 (void) strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
155 if (ioctl(fd
, BIOCSETIF
, &ifr
) == -1)
157 fprintf(stderr
, "%s(%d):", ifr
.ifr_name
, fd
);
167 if (ioctl(fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) == -1)
169 perror("BIOCSRTIMEOUT");
173 * get kernel buffer size
175 if (ioctl(fd
, BIOCSBLEN
, &bufsize
) == -1)
177 if (ioctl(fd
, BIOCGBLEN
, &bufsize
) == -1)
182 printf("BPF buffer size: %d\n", bufsize
);
183 buf
= (u_char
*)malloc(bufsize
);
185 prog
.bf_len
= sizeof(filter
) / sizeof(struct bpf_insn
);
186 prog
.bf_insns
= filter
;
187 if (ioctl(fd
, BIOCSETF
, (caddr_t
)&prog
) == -1)
192 (void) ioctl(fd
, BIOCFLUSH
, 0);