DIY-pcap 0.0.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ require 'diy/worker'
4
+ describe DIY::Worker do
5
+
6
+ class FakeLive
7
+ def inject
8
+ end
9
+ class ::String
10
+ def body
11
+ self
12
+ end
13
+ end
14
+ def loop
15
+ pkt = 1
16
+ @go = true
17
+ while(@go) do
18
+ pkt += 1
19
+ sleep 0.01
20
+ yield( nil, "pkt #{pkt}" )
21
+ end
22
+ end
23
+
24
+ def stop
25
+ #~ @go = false
26
+ end
27
+
28
+ end
29
+
30
+ let(:live) { FakeLive.new }
31
+
32
+ it "should create ok" do
33
+ DIY::Worker.new(live)
34
+ end
35
+
36
+ it "#inject" do
37
+ end
38
+
39
+ it "#ready" do
40
+ end
41
+
42
+ it "#terminal" do
43
+ #~ live.should_recieve(:stop)
44
+ worker = DIY::Worker.new(live)
45
+ worker.terminal
46
+
47
+
48
+ worker.ready do |pkt|
49
+ puts pkt.inspect
50
+ end
51
+
52
+ sleep 0.1
53
+ worker.terminal
54
+ worker.terminal
55
+
56
+ end
57
+
58
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 2
7
8
  - 0
8
- - 4
9
- version: 0.0.4
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - yafei Lee
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-09-13 00:00:00 +08:00
17
+ date: 2012-09-19 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -54,21 +54,36 @@ files:
54
54
  - lib/DIY-pcap.rb
55
55
  - lib/diy/builder.rb
56
56
  - lib/diy/controller.rb
57
+ - lib/diy/device_finder.rb
57
58
  - lib/diy/dig.rb
58
59
  - lib/diy/exceptions.rb
59
60
  - lib/diy/logger.rb
60
61
  - lib/diy/offline.rb
62
+ - lib/diy/packet.rb
61
63
  - lib/diy/pcap.rb
62
- - lib/diy/queue.rb
63
- - lib/diy/recver.rb
64
- - lib/diy/sender.rb
65
64
  - lib/diy/strategy.rb
66
65
  - lib/diy/strategy_builder.rb
67
66
  - lib/diy/task.rb
68
67
  - lib/diy/utils.rb
69
68
  - lib/diy/version.rb
69
+ - lib/diy/worker.rb
70
+ - lib/diy/worker_keeper.rb
71
+ - simple/4000port.rb
72
+ - simple/4000port/r1.dat
73
+ - simple/4000port/r3.dat
74
+ - simple/4000port/r4.dat
75
+ - simple/4000port/r6.dat
76
+ - simple/4000port/r7.dat
77
+ - simple/4000port/s1.dat
78
+ - simple/4000port/s2.dat
79
+ - simple/4000port/s3.dat
80
+ - simple/4000port/s4.dat
81
+ - simple/4000port/s5.dat
82
+ - simple/4000port/s6.dat
83
+ - simple/4000port/s8.dat
70
84
  - simple/cmd-pcap.rb
71
85
  - simple/diy-strategy-pcap.rb
86
+ - simple/howto.vsd
72
87
  - simple/pcap.rb
73
88
  - simple/pcaps/app.pcap
74
89
  - simple/pcaps/gre.pcap
@@ -79,7 +94,8 @@ files:
79
94
  - simple/pcaps/s2.dat
80
95
  - simple/pcaps/s3.dat
81
96
  - simple/pcaps/s4.dat
82
- - spec/builder_spec.rb
97
+ - spec/controller_spec.rb
98
+ - spec/device_finder_spec.rb
83
99
  - spec/helper/app.pcap
84
100
  - spec/helper/gre.pcap
85
101
  - spec/helper/http.pcap
@@ -91,10 +107,9 @@ files:
91
107
  - spec/helper/ssh.pcap
92
108
  - spec/logger_spec.rb
93
109
  - spec/offline_spec.rb
94
- - spec/queue_spec.rb
95
- - spec/sender_spec.rb
96
110
  - spec/spec_helper.rb
97
111
  - spec/utils_spec.rb
112
+ - spec/worker_spec.rb
98
113
  has_rdoc: true
99
114
  homepage: ""
100
115
  licenses: []
data/lib/diy/queue.rb DELETED
@@ -1,174 +0,0 @@
1
- require 'thread'
2
- require 'timeout'
3
- module DIY
4
- class Queue
5
-
6
- def initialize(offline, force_server = false)
7
- @expect_recv_queue = []
8
- @offline = offline
9
- @m = Mutex.new
10
- # 暂存 next_send_pkt 数据
11
- @tmp_send_pkt = nil
12
-
13
- @force_server = force_server
14
- end
15
-
16
- def expect_recv_queue
17
- @expect_recv_queue
18
- end
19
-
20
- def pop
21
- return nil if @expect_recv_queue.empty?
22
- @m.synchronize {
23
- return @expect_recv_queue.shift[0]
24
- }
25
- end
26
-
27
- def delete(what)
28
- if @expect_recv_queue.find { |i| i[0] == what }
29
- @m.synchronize {
30
- if @expect_recv_queue.find { |i| i[0] == what }
31
- @expect_recv_queue.delete_if { |i| i[0] == what }
32
- return what
33
- end
34
- }
35
- end
36
- return nil
37
- end
38
-
39
- def delete_at(index)
40
- @m.synchronize {
41
- deleted = @expect_recv_queue.delete_at(index)
42
- return deleted.nil? ? nil : deleted[0]
43
- }
44
- end
45
-
46
- def peek
47
- return nil if @expect_recv_queue.empty?
48
- @expect_recv_queue[0][0]
49
- end
50
-
51
- def peek_full
52
- return nil if @expect_recv_queue.empty?
53
- @expect_recv_queue[0]
54
- end
55
-
56
- def clear_and_next_pcap
57
- @offline.next_pcap
58
- clear
59
- end
60
-
61
- def clear
62
- @m.synchronize {
63
- return @expect_recv_queue.clear
64
- }
65
- end
66
-
67
- # 处理发送报文
68
- #
69
- # 等待接受报文完成后, 返回发送报文, 并重新填充接受报文
70
- # TODO: 支持多个pcap文件
71
- def next_send_pkt(&block)
72
- begin
73
- wait_until { @expect_recv_queue.empty? }
74
- rescue HopePacketTimeoutError
75
- raise HopePacketTimeoutError, "#{peek_full && peek_full[1]}"
76
- end
77
- if @tmp_send_pkt
78
- pkt = @tmp_send_pkt
79
- @tmp_send_pkt = nil
80
- else
81
- pkt = write_recv_pkt
82
- begin
83
- wait_until { @expect_recv_queue.empty? }
84
- rescue HopePacketTimeoutError
85
- raise HopePacketTimeoutError, "#{peek_full && peek_full[1]}"
86
- end
87
- end
88
- raise EOFError, " no pkt to send " unless pkt
89
- pkt = pkt.copy
90
-
91
- need_wait_for_seconds = nil
92
- # 刚切换pcap文件后需要等待
93
- if @offline.first_pkt?
94
- need_wait_for_seconds = true
95
- end
96
-
97
- recv_pkt = write_recv_pkt
98
-
99
- if need_wait_for_seconds
100
- wait_for_seconds
101
- end
102
-
103
- yield(pkt.body) if block_given?
104
-
105
- @tmp_send_pkt = recv_pkt.copy if recv_pkt
106
- pkt.body
107
- end
108
- alias_method :next, :next_send_pkt
109
-
110
- def wait_for_seconds
111
- DIY::Logger.info("new pcap file, sleep 3...")
112
- sleep 3
113
- end
114
-
115
- def write_recv_pkt
116
- while ( (recv_pkt = @offline.next) && ( @offline.first_pkt? && set_first_gout(recv_pkt.body); comein?(recv_pkt.body) ) )
117
- @m.synchronize {
118
- @expect_recv_queue << [ recv_pkt.copy.body, @offline.fullname ]
119
- }
120
- end
121
- recv_pkt
122
- end
123
-
124
- def do_loop(&block)
125
- raise "Must give me block" unless block_given?
126
- while(true) do
127
- next_send_pkt(&block)
128
- end
129
- end
130
-
131
- def set_first_gout(pkt)
132
- #~ return @src_mac if @src_mac
133
- if pkt.size < 12
134
- raise PktError,"can't find src mac: error format packet"
135
- end
136
- @src_mac = pkt[6..11]
137
- end
138
-
139
- def comein?(pkt)
140
- ret = judge_direct(pkt) do | pkt_mac, src_mac|
141
- (pkt_mac != src_mac) ^ server?
142
- end
143
- ret
144
- end
145
-
146
- def gout?(pkt)
147
- judge_direct(pkt) do | pkt_mac, src_mac|
148
- (pkt_mac == src_mac) ^ server?
149
- end
150
- end
151
-
152
- def server?
153
- $SERVER || @force_server
154
- end
155
-
156
- def judge_direct(pkt,&block)
157
- if pkt.size < 12
158
- raise PktError,"can't find src mac: error format packet"
159
- end
160
- raise "src_mac not set" unless @src_mac
161
- yield( pkt[6..11], @src_mac )
162
- end
163
-
164
- def wait_until( timeout = 20, &block )
165
- timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} senconds") ) do
166
- loop do
167
- break if block.call
168
- sleep 0.01
169
- end
170
- end
171
- end
172
-
173
- end # end Queue
174
- end
data/lib/diy/recver.rb DELETED
@@ -1,33 +0,0 @@
1
- module DIY
2
- class Recver
3
- def initialize(live)
4
- @live = live
5
- @watchers = []
6
- end
7
-
8
- def run
9
- @live.loop do |this, pkt|
10
- notify_recv_pkt(pkt)
11
- end
12
- end
13
-
14
- def stop
15
- @live.stop
16
- end
17
-
18
- def notify_recv_pkt(pkt)
19
- @watchers.each do |watcher|
20
- watcher.recv_pkt(pkt.body)
21
- end
22
- end
23
-
24
- def add_watcher(watcher)
25
- @watchers = [] unless @watchers
26
- @watchers << watcher
27
- end
28
-
29
- def del_watcher(watcher)
30
- @watchers.delete(watcher)
31
- end
32
- end
33
- end
data/lib/diy/sender.rb DELETED
@@ -1,26 +0,0 @@
1
- module DIY
2
- class Sender
3
- def initialize(live)
4
- @live = live
5
- @before_send_hook = nil
6
- end
7
-
8
- def inject(pkt)
9
- pkt = before_send_call(pkt)
10
- @live.inject(pkt)
11
- end
12
-
13
- def before_send(&block)
14
- @before_send_hook = block
15
- end
16
-
17
- def before_send_call(pkt)
18
- if @before_send_hook
19
- @before_send_hook.call(pkt)
20
- else
21
- pkt
22
- end
23
- end
24
-
25
- end
26
- end
data/spec/builder_spec.rb DELETED
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DIY::Builder do
4
- it "should respond before_send" do
5
- DIY::Builder.new { ; }.should be_respond_to("before_send")
6
- end
7
-
8
- it "should run two pcaps success " do
9
- builder = DIY::Builder.new(true) do
10
- use DIY::SimpleStrategy.new
11
- pcapfile ["helper/ssh.pcap", "helper/http.pcap"]
12
- end
13
-
14
- b = Thread.new do
15
- builder.run
16
- end
17
-
18
- builder2 = DIY::Builder.new do
19
- use DIY::SimpleStrategy.new
20
- pcapfile ["helper/ssh.pcap", "helper/http.pcap"]
21
- end
22
- builder2.run
23
- b.join
24
- end
25
-
26
- it "should run one pcap success" do
27
- builder = DIY::Builder.new(true) do
28
- use DIY::SimpleStrategy.new
29
- pcapfile ["helper/ssh.pcap"]
30
- end
31
-
32
- b = Thread.new do
33
- builder.run
34
- end
35
-
36
- builder2 = DIY::Builder.new do
37
- use DIY::SimpleStrategy.new
38
- pcapfile ["helper/ssh.pcap"]
39
- end
40
- builder2.run
41
- b.join
42
- end
43
-
44
- it "should run one pcap error" do
45
- builder = DIY::Builder.new(true) do
46
- use DIY::SimpleStrategy.new
47
- pcapfile ["helper/ssh.pcap"]
48
- end
49
-
50
- b = Thread.new do
51
- builder.run
52
- end
53
-
54
- builder2 = DIY::Builder.new do
55
- use DIY::SimpleStrategy.new
56
- pcapfile ["helper/http.pcap"]
57
- end
58
- lambda { builder2.run }.should raise_error(DIY::HopePacketTimeoutError)
59
- lambda { b.join }.should raise_error(DIY::HopePacketTimeoutError)
60
- end
61
-
62
- it "should run two pcap error" do
63
- builder = DIY::Builder.new(true) do
64
- use DIY::SimpleStrategy.new
65
- pcapfile ["helper/ssh.pcap", "helper/http.pcap"]
66
- end
67
-
68
- b = Thread.new do
69
- builder.run
70
- end
71
-
72
- builder2 = DIY::Builder.new do
73
- use DIY::SimpleStrategy.new
74
- pcapfile ["helper/http.pcap"]
75
- end
76
- lambda { builder2.run }.should raise_error(DIY::HopePacketTimeoutError)
77
- lambda { b.join }.should raise_error(DIY::HopePacketTimeoutError)
78
- end
79
-
80
-
81
-
82
- end