aboutsummaryrefslogtreecommitdiffstats
path: root/optimize.c
diff options
context:
space:
mode:
authorguy <guy>2000-10-28 00:01:26 +0000
committerguy <guy>2000-10-28 00:01:26 +0000
commit82547471f79c2cf4a9943a222d006edc4add2d76 (patch)
tree803d1cceed386d4025205dc5ae836cc38d62b6a9 /optimize.c
parent188fee53ccbbe6b11e84ae01b10955205be5ac62 (diff)
When attaching a "bpf_program" to a "pcap_t" to use as a userland
filter, always attach a copy, as "pcap-linux.c" does; that way, after a program uses "pcap_setfilter()", it can safely use "pcap_freecode()" to free up the BPF instructions allocated by "pcap_compile()". Also, always free it up when the "pcap_t" is closed. Get rid of the "pcap_t *" argument to "pcap_freecode()", as it's not necessary. Document "pcap_freecode()", for the benefit of programs that might repeatedly compile filter programs and attach them, so that they can free them up after attaching them and avoid leaking memory for them.
Diffstat (limited to 'optimize.c')
-rw-r--r--optimize.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/optimize.c b/optimize.c
index c39f899..fe2c2ca 100644
--- a/optimize.c
+++ b/optimize.c
@@ -22,7 +22,7 @@
*/
#ifndef lint
static const char rcsid[] =
- "@(#) $Header: /tcpdump/master/libpcap/optimize.c,v 1.64 2000-09-06 07:40:03 itojun Exp $ (LBL)";
+ "@(#) $Header: /tcpdump/master/libpcap/optimize.c,v 1.65 2000-10-28 00:01:27 guy Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
@@ -36,6 +36,8 @@ static const char rcsid[] =
#include <stdlib.h>
#include <memory.h>
+#include <errno.h>
+
#include "pcap-int.h"
#include "gencode.h"
@@ -2077,6 +2079,36 @@ icode_to_fcode(root, lenp)
return fp;
}
+/*
+ * Make a copy of a BPF program and put it in the "fcode" member of
+ * a "pcap_t".
+ *
+ * If we fail to allocate memory for the copy, fill in the "errbuf"
+ * member of the "pcap_t" with an error message, and return -1;
+ * otherwise, return 0.
+ */
+int
+install_bpf_program(pcap_t *p, struct bpf_program *fp)
+{
+ size_t prog_size;
+
+ /*
+ * Free up any already installed program.
+ */
+ pcap_freecode(&p->fcode);
+
+ prog_size = sizeof(*fp->bf_insns) * fp->bf_len;
+ p->fcode.bf_len = fp->bf_len;
+ p->fcode.bf_insns = (struct bpf_insn *)malloc(prog_size);
+ if (p->fcode.bf_insns == NULL) {
+ snprintf(p->errbuf, sizeof(p->errbuf),
+ "malloc: %s", pcap_strerror(errno));
+ return (-1);
+ }
+ memcpy(p->fcode.bf_insns, fp->bf_insns, prog_size);
+ return (0);
+}
+
#ifdef BDEBUG
static void
opt_dump(root)