File tree Expand file tree Collapse file tree 5 files changed +47
-10
lines changed Expand file tree Collapse file tree 5 files changed +47
-10
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ edition = "2018"
7
7
[dependencies ]
8
8
failure = " ~0.1"
9
9
failure_derive = " ~0.1"
10
- futures-preview = { version =" 0.3.0-alpha.13 " , features = [" compat" ] }
10
+ futures-preview = { version =" 0.3.0-alpha.14 " , features = [" compat" ] }
11
11
libc = " ~0.2"
12
12
log = " ~0.4"
13
13
pcap-sys = { git = " https://github.com/protectwise/pcap-sys.git" }
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ impl Config {
51
51
& self . bpf
52
52
}
53
53
54
- pub fn with_bf ( & mut self , amt : String ) -> & mut Self {
54
+ pub fn with_bpf ( & mut self , amt : String ) -> & mut Self {
55
55
self . bpf = Some ( amt) ;
56
56
self
57
57
}
Original file line number Diff line number Diff line change @@ -147,7 +147,7 @@ impl Handle {
147
147
bf_insns : std:: ptr:: null_mut ( ) ,
148
148
} ;
149
149
150
- let bpf_str = std:: ffi:: CString :: new ( bpf. to_string ( ) ) . map_err ( Error :: Ffi ) ?;
150
+ let bpf_str = std:: ffi:: CString :: new ( bpf. clone ( ) ) . map_err ( Error :: Ffi ) ?;
151
151
152
152
if 0 != unsafe {
153
153
pcap_sys:: pcap_compile (
@@ -218,13 +218,17 @@ impl Handle {
218
218
Ok ( stats)
219
219
}
220
220
}
221
+
222
+ pub fn close ( & self ) {
223
+ unsafe {
224
+ pcap_sys:: pcap_close ( self . handle )
225
+ }
226
+ }
221
227
}
222
228
223
229
impl Drop for Handle {
224
230
fn drop ( & mut self ) {
225
- unsafe {
226
- pcap_sys:: pcap_close ( self . handle ) ;
227
- }
231
+ self . close ( ) ;
228
232
}
229
233
}
230
234
Original file line number Diff line number Diff line change @@ -36,8 +36,8 @@ impl PacketProvider {
36
36
. set_buffer_size ( config. buffer_size ( ) ) ?
37
37
. activate ( ) ?;
38
38
39
- if let Some ( ref s ) = config. bpf ( ) {
40
- handle. set_bpf ( s ) ?;
39
+ if let Some ( bpf ) = config. bpf ( ) {
40
+ handle. set_bpf ( bpf ) ?;
41
41
}
42
42
}
43
43
@@ -111,6 +111,23 @@ mod tests {
111
111
) ;
112
112
}
113
113
114
+ #[ test]
115
+ fn packets_from_lookup_with_bpf ( ) {
116
+ let _ = env_logger:: try_init ( ) ;
117
+
118
+ let mut cfg = Config :: default ( ) ;
119
+ cfg. with_bpf ( "(not (net 172.16.0.0/16 and port 443)) and (not (host 172.17.76.33 and port 443))" . to_owned ( ) ) ;
120
+
121
+ let handle = Handle :: lookup ( ) . expect ( "No handle created" ) ;
122
+
123
+ let stream = PacketProvider :: new ( cfg, handle) ;
124
+
125
+ assert ! (
126
+ stream. is_ok( ) ,
127
+ format!( "Could not build stream {}" , stream. err( ) . unwrap( ) )
128
+ ) ;
129
+ }
130
+
114
131
#[ bench]
115
132
fn bench_packets_from_large_file ( b : & mut Bencher ) {
116
133
let _ = env_logger:: try_init ( ) ;
Original file line number Diff line number Diff line change @@ -28,8 +28,8 @@ impl PacketStream {
28
28
. set_buffer_size ( config. buffer_size ( ) ) ?
29
29
. activate ( ) ?;
30
30
31
- if let Some ( ref s ) = config. bpf ( ) {
32
- handle. set_bpf ( s ) ?;
31
+ if let Some ( bpf ) = config. bpf ( ) {
32
+ handle. set_bpf ( bpf ) ?;
33
33
}
34
34
}
35
35
@@ -103,6 +103,22 @@ mod tests {
103
103
) ;
104
104
}
105
105
106
+ #[ test]
107
+ fn packets_from_lookup_with_bpf ( ) {
108
+ let _ = env_logger:: try_init ( ) ;
109
+
110
+ let mut cfg = Config :: default ( ) ;
111
+ cfg. with_bpf ( "(not (net 172.16.0.0/16 and port 443)) and (not (host 172.17.76.33 and port 443))" . to_owned ( ) ) ;
112
+ let handle = Handle :: lookup ( ) . expect ( "No handle created" ) ;
113
+
114
+ let stream = PacketStream :: new ( cfg, handle) ;
115
+
116
+ assert ! (
117
+ stream. is_ok( ) ,
118
+ format!( "Could not build stream {}" , stream. err( ) . unwrap( ) )
119
+ ) ;
120
+ }
121
+
106
122
#[ bench]
107
123
fn bench_packets_from_large_file ( b : & mut Bencher ) {
108
124
let _ = env_logger:: try_init ( ) ;
You can’t perform that action at this time.
0 commit comments