DIY-pcap 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/pcap +1 -1
- data/bin/rpcap +1 -1
- data/lib/diy/builder.rb +5 -0
- data/lib/diy/controller.rb +7 -2
- data/lib/diy/dig.rb +1 -0
- data/lib/diy/ext/capture_wrapper.rb +29 -0
- data/lib/diy/live.rb +32 -0
- data/lib/diy/version.rb +1 -1
- data/lib/diy/worker.rb +16 -3
- data/spec/controller_spec.rb +3 -2
- metadata +9 -9
data/bin/pcap
CHANGED
@@ -61,7 +61,7 @@ else
|
|
61
61
|
device_name = DIY::DeviceFinder.smart_select
|
62
62
|
end
|
63
63
|
DIY::Logger.info( "Initialize Live: #{device_name}" )
|
64
|
-
device = FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
64
|
+
device = DIY::Live.new(device_name) #FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
65
65
|
worker = DIY::Worker.new(device)
|
66
66
|
DIY::WorkerKeeper.new(worker, uri).run
|
67
67
|
end
|
data/bin/rpcap
CHANGED
@@ -62,7 +62,7 @@ else
|
|
62
62
|
device_name = DIY::DeviceFinder.smart_select
|
63
63
|
end
|
64
64
|
DIY::Logger.info( "Initialize Live: #{device_name}" )
|
65
|
-
device = FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
65
|
+
device = DIY::Live.new(device_name) #FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
66
66
|
worker = DIY::Worker.new(device)
|
67
67
|
DIY::WorkerKeeper.new(worker, uri).run
|
68
68
|
end
|
data/lib/diy/builder.rb
CHANGED
@@ -30,6 +30,10 @@ module DIY
|
|
30
30
|
@server = DRbObject.new_with_uri(@suri)
|
31
31
|
end
|
32
32
|
|
33
|
+
def timeout(timeout)
|
34
|
+
@timeout = timeout
|
35
|
+
end
|
36
|
+
|
33
37
|
def client(ip_or_iport)
|
34
38
|
@curi = ip_or_iport_with_default(ip_or_iport, 7878)
|
35
39
|
end
|
@@ -73,6 +77,7 @@ module DIY
|
|
73
77
|
find_worker_keepers
|
74
78
|
controller = Controller.new( @client, @server, @offline, @strategy_builder )
|
75
79
|
controller.before_send(&@before_send_hook)
|
80
|
+
controller.timeout(@timeout) if @timeout
|
76
81
|
controller.run
|
77
82
|
end
|
78
83
|
|
data/lib/diy/controller.rb
CHANGED
@@ -9,6 +9,7 @@ module DIY
|
|
9
9
|
@offline = offline
|
10
10
|
@strategy = strategy
|
11
11
|
@before_send = nil
|
12
|
+
@timeout = nil
|
12
13
|
end
|
13
14
|
|
14
15
|
def run
|
@@ -79,17 +80,21 @@ module DIY
|
|
79
80
|
@before_send = block
|
80
81
|
end
|
81
82
|
|
83
|
+
def timeout(timeout)
|
84
|
+
@timeout = timeout
|
85
|
+
end
|
86
|
+
|
82
87
|
def stats_result( cost_time, fail_count )
|
83
88
|
DIY::Logger.info " ====== Finished in #{cost_time} seconds"
|
84
89
|
DIY::Logger.info " ====== Total fail_count: #{fail_count} failures"
|
85
90
|
end
|
86
91
|
|
87
92
|
def wait_recv_ok(pkts)
|
88
|
-
wait_until { pkts.empty? }
|
93
|
+
wait_until(@timeout ||= 10) { pkts.empty? }
|
89
94
|
end
|
90
95
|
|
91
96
|
def wait_until( timeout = 10, &block )
|
92
|
-
timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do
|
97
|
+
Timeout.timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do
|
93
98
|
loop do
|
94
99
|
break if block.call
|
95
100
|
sleep 0.01
|
data/lib/diy/dig.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'ffi/pcap'
|
2
|
+
|
3
|
+
unless defined?(FFI::PCap::CaptureWrapper)
|
4
|
+
raise "must define FFI::PCap::CaptureWrapper before monkey fix"
|
5
|
+
end
|
6
|
+
|
7
|
+
module FFI
|
8
|
+
module PCap
|
9
|
+
class CaptureWrapper
|
10
|
+
# Fix bug: dispatch but call pcap_loop
|
11
|
+
#
|
12
|
+
def dispatch(opts={}, &block)
|
13
|
+
cnt = opts[:count] || -1 # default to infinite loop
|
14
|
+
h = opts[:handler]
|
15
|
+
|
16
|
+
ret = FFI::PCap.pcap_dispatch(_pcap, cnt, _wrap_callback(h, block),nil)
|
17
|
+
if ret == -1
|
18
|
+
raise(ReadError, "pcap_dispatch(): #{geterr()}")
|
19
|
+
elsif ret -2
|
20
|
+
return nil
|
21
|
+
elsif ret > -1
|
22
|
+
return ret
|
23
|
+
else
|
24
|
+
raise(ReadError, "unexpected return from pcap_dispatch() -> #{ret}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/diy/live.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding : utf-8
|
2
|
+
|
3
|
+
require 'diy/ext/capture_wrapper'
|
4
|
+
|
5
|
+
module DIY
|
6
|
+
class Live
|
7
|
+
def initialize(device_name)
|
8
|
+
DIY::Logger.info( "Initialize Live: #{device_name}" )
|
9
|
+
@live = FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::CopyHandler, :promisc => true)
|
10
|
+
#~ @live.non_blocking= true
|
11
|
+
end
|
12
|
+
attr_reader :live
|
13
|
+
|
14
|
+
# 发包
|
15
|
+
def inject(packet_str)
|
16
|
+
@live.send_packet(packet_str)
|
17
|
+
end
|
18
|
+
alias send_packet inject
|
19
|
+
|
20
|
+
def loop(&block)
|
21
|
+
Kernel.loop do
|
22
|
+
@live.dispatch do |this, pkt|
|
23
|
+
next unless pkt
|
24
|
+
block.call(this, pkt)
|
25
|
+
end
|
26
|
+
sleep 0.1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end # end of class Live
|
31
|
+
|
32
|
+
end
|
data/lib/diy/version.rb
CHANGED
data/lib/diy/worker.rb
CHANGED
@@ -11,7 +11,9 @@ module DIY
|
|
11
11
|
@recv_t = nil
|
12
12
|
@start = false
|
13
13
|
@m = Mutex.new
|
14
|
+
@queue = Queue.new
|
14
15
|
loop_recv
|
16
|
+
loop_callback
|
15
17
|
end
|
16
18
|
|
17
19
|
# 发包
|
@@ -27,10 +29,19 @@ module DIY
|
|
27
29
|
DIY::Logger.info "start thread recving pkt..."
|
28
30
|
@live.loop do |this, pkt|
|
29
31
|
next unless @start
|
32
|
+
@queue.push(pkt.body)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def loop_callback
|
38
|
+
@callback_t = Thread.new do
|
39
|
+
#~ DIY::Logger.info "start thread callbacking pkt..."
|
40
|
+
loop do
|
30
41
|
begin
|
31
|
-
@
|
32
|
-
|
33
|
-
|
42
|
+
pkt = @queue.pop
|
43
|
+
#~ DIY::Logger.info "callback: #{pkt}"
|
44
|
+
@block.call(pkt) if @block
|
34
45
|
rescue DRb::DRbConnError
|
35
46
|
DIY::Logger.info "closed connection by controller"
|
36
47
|
end
|
@@ -42,12 +53,14 @@ module DIY
|
|
42
53
|
def ready(&block)
|
43
54
|
DIY::Logger.info("start recv pkt")
|
44
55
|
@block = block
|
56
|
+
@queue.clear
|
45
57
|
@start = true
|
46
58
|
end
|
47
59
|
|
48
60
|
def terminal
|
49
61
|
DIY::Logger.info("stop recv pkt")
|
50
62
|
@start = false
|
63
|
+
@queue.clear
|
51
64
|
end
|
52
65
|
|
53
66
|
def inspect
|
data/spec/controller_spec.rb
CHANGED
@@ -5,8 +5,8 @@ describe DIY::Builder do
|
|
5
5
|
before(:each) do
|
6
6
|
@device_name = FFI::PCap.dump_devices[0][0]
|
7
7
|
DIY::Logger.info( "Initialize Live: #{@device_name}" )
|
8
|
-
@live = FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
9
|
-
@live2 = FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
8
|
+
@live = DIY::Live.new(@device_name)#FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
9
|
+
@live2 = DIY::Live.new(@device_name)#FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
|
10
10
|
|
11
11
|
@curi = "druby://localhost:7878"
|
12
12
|
@suri = "druby://localhost:7879"
|
@@ -30,6 +30,7 @@ describe DIY::Builder do
|
|
30
30
|
builder = DIY::Builder.new do
|
31
31
|
pcapfiles "helper/http.pcap"
|
32
32
|
use DIY::SimpleStrategy.new
|
33
|
+
timeout 10
|
33
34
|
end
|
34
35
|
builder.run
|
35
36
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: DIY-pcap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
8
|
+
- 5
|
9
|
+
version: 0.2.5
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- yafei Lee
|
@@ -15,7 +14,8 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2012-10-08 00:00:00
|
17
|
+
date: 2012-10-08 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: ffi-pcap
|
@@ -25,7 +25,6 @@ dependencies:
|
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash: 23
|
29
28
|
segments:
|
30
29
|
- 0
|
31
30
|
- 2
|
@@ -58,6 +57,8 @@ files:
|
|
58
57
|
- lib/diy/device_finder.rb
|
59
58
|
- lib/diy/dig.rb
|
60
59
|
- lib/diy/exceptions.rb
|
60
|
+
- lib/diy/ext/capture_wrapper.rb
|
61
|
+
- lib/diy/live.rb
|
61
62
|
- lib/diy/logger.rb
|
62
63
|
- lib/diy/offline.rb
|
63
64
|
- lib/diy/packet.rb
|
@@ -111,6 +112,7 @@ files:
|
|
111
112
|
- spec/spec_helper.rb
|
112
113
|
- spec/utils_spec.rb
|
113
114
|
- spec/worker_spec.rb
|
115
|
+
has_rdoc: true
|
114
116
|
homepage: ""
|
115
117
|
licenses: []
|
116
118
|
|
@@ -124,7 +126,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
126
|
requirements:
|
125
127
|
- - ">="
|
126
128
|
- !ruby/object:Gem::Version
|
127
|
-
hash: 3
|
128
129
|
segments:
|
129
130
|
- 0
|
130
131
|
version: "0"
|
@@ -133,14 +134,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
134
|
requirements:
|
134
135
|
- - ">="
|
135
136
|
- !ruby/object:Gem::Version
|
136
|
-
hash: 3
|
137
137
|
segments:
|
138
138
|
- 0
|
139
139
|
version: "0"
|
140
140
|
requirements: []
|
141
141
|
|
142
142
|
rubyforge_project:
|
143
|
-
rubygems_version: 1.
|
143
|
+
rubygems_version: 1.3.7
|
144
144
|
signing_key:
|
145
145
|
specification_version: 3
|
146
146
|
summary: DIY pcap send and recv
|