Branch data Line data Source code
1 : : // $Id: BPF_Program.cc 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #include "config.h"
6 : :
7 : : #include "util.h"
8 : : #include "BPF_Program.h"
9 : :
10 : : #ifdef DONT_HAVE_LIBPCAP_PCAP_FREECODE
11 : : extern "C" {
12 : : #include "pcap-int.h"
13 : :
14 : : int pcap_freecode(pcap_t* unused, struct bpf_program* program)
15 : : {
16 : : program->bf_len = 0;
17 : :
18 : : if ( program->bf_insns )
19 : : {
20 : : free((char*) program->bf_insns); // copied from libpcap
21 : : program->bf_insns = 0;
22 : : }
23 : :
24 : : return 0;
25 : : }
26 : :
27 : : pcap_t* pcap_open_dead(int linktype, int snaplen)
28 : : {
29 : : pcap_t* p;
30 : :
31 : : p = (pcap_t*) malloc(sizeof(*p));
32 : : if ( ! p )
33 : : return 0;
34 : :
35 : : memset(p, 0, sizeof(*p));
36 : :
37 : : p->fd = -1;
38 : : p->snapshot = snaplen;
39 : : p->linktype = linktype;
40 : :
41 : : return p;
42 : : }
43 : :
44 : : int pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
45 : : struct bpf_program* program, char* buf,
46 : : int optimize, bpf_u_int32 mask)
47 : : {
48 : : pcap_t* p;
49 : : int ret;
50 : :
51 : : p = pcap_open_dead(linktype_arg, snaplen_arg);
52 : : if ( ! p )
53 : : return -1;
54 : :
55 : : ret = pcap_compile(p, program, buf, optimize, mask);
56 : : pcap_close(p);
57 : :
58 : : return ret;
59 : : }
60 : : }
61 : : #endif
62 : :
63 : 2 : BPF_Program::BPF_Program()
64 : : {
65 : 2 : m_compiled = false;
66 : 2 : }
67 : :
68 : 0 : BPF_Program::~BPF_Program()
69 : : {
70 : 0 : FreeCode();
71 : 0 : }
72 : :
73 : : bool BPF_Program::Compile(pcap_t* pcap, const char* filter, uint32 netmask,
74 : 2 : char* errbuf, unsigned int errbuf_len, bool optimize)
75 : : {
76 [ - + ]: 2 : if ( ! pcap )
77 : 0 : return false;
78 : :
79 : 2 : FreeCode();
80 : :
81 [ - + ]: 2 : if ( pcap_compile(pcap, &m_program, (char *) filter, optimize, netmask) < 0 )
82 : : {
83 [ # # ]: 0 : if ( errbuf )
84 : : safe_snprintf(errbuf, errbuf_len,
85 : : "pcap_compile(%s): %s", filter,
86 : 0 : pcap_geterr(pcap));
87 : :
88 : 0 : return false;
89 : : }
90 : :
91 : 2 : m_compiled = true;
92 : :
93 : 2 : return true;
94 : : }
95 : :
96 : : bool BPF_Program::Compile(int snaplen, int linktype, const char* filter,
97 : 0 : uint32 netmask, char* errbuf, bool optimize)
98 : : {
99 : 0 : FreeCode();
100 : :
101 : : #ifdef LIBPCAP_PCAP_COMPILE_NOPCAP_HAS_ERROR_PARAMETER
102 : : char my_error[PCAP_ERRBUF_SIZE];
103 : :
104 : : int err = pcap_compile_nopcap(snaplen, linktype, &m_program,
105 : : (char *) filter, optimize, netmask, error);
106 : : if ( err < 0 && errbuf )
107 : : safe_strncpy(errbuf, my_errbuf, PCAP_ERRBUF_SIZE);
108 : : #else
109 : : int err = pcap_compile_nopcap(snaplen, linktype, &m_program,
110 : 0 : (char*) filter, optimize, netmask);
111 : : #endif
112 [ # # ]: 0 : if ( err == 0 )
113 : 0 : m_compiled = true;
114 : :
115 : 0 : return err == 0;
116 : : }
117 : :
118 : 1 : bpf_program* BPF_Program::GetProgram()
119 : : {
120 [ + - ]: 1 : return m_compiled ? &m_program : 0;
121 : : }
122 : :
123 : 2 : void BPF_Program::FreeCode()
124 : : {
125 [ - + ]: 2 : if ( m_compiled )
126 : : {
127 : : #ifdef DONT_HAVE_LIBPCAP_PCAP_FREECODE
128 : : pcap_freecode(NULL, &m_program);
129 : : #else
130 : 0 : pcap_freecode(&m_program);
131 : : #endif
132 : 0 : m_compiled = false;
133 : : }
134 : 2 : }
|