aboutsummaryrefslogtreecommitdiffstats
path: root/optimize.c
diff options
context:
space:
mode:
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)