DIY-pcap 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ module DIY
28
28
  one_round( client, server, pkts )
29
29
  client, server = server, client
30
30
  rescue HopePacketTimeoutError, UserError, FFI::PCap::LibError => e
31
- DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].inspect} ") if e.kind_of?(HopePacketTimeoutError)
31
+ DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].pretty_print} ") if e.kind_of?(HopePacketTimeoutError)
32
32
  @fail_count += 1
33
33
  begin
34
34
  @offline.next_pcap
@@ -53,7 +53,10 @@ module DIY
53
53
  @error_flag = nil
54
54
  @round_count = 0 unless @round_count
55
55
  @round_count += 1
56
- DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].inspect}:(queue= #{pkts.size})"
56
+ DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].pretty_print}:(queue= #{pkts.size})"
57
+ if pkts.size >= 10
58
+ DIY::Logger.warn "queue size too big: #{pkts.size}, maybe something error"
59
+ end
57
60
  server.ready do |recv_pkt|
58
61
  next if @error_flag # error accur waiting other thread do with it
59
62
  recv_pkt = Packet.new(recv_pkt)
@@ -8,6 +8,7 @@ module DIY
8
8
  DIY::Logger.info( "Initialize Live: #{device_name}" )
9
9
  @live = FFI::PCap::Live.new(:dev=>device_name, :handler => FFI::PCap::CopyHandler, :promisc => true)
10
10
  DIY::Logger.info( "Listen on: #{net} " )
11
+ @running = false
11
12
  #~ @live.non_blocking= true
12
13
  end
13
14
  attr_reader :live
@@ -19,13 +20,20 @@ module DIY
19
20
  alias send_packet inject
20
21
 
21
22
  def loop(&block)
22
- Kernel.loop do
23
+ @running = true
24
+ while @running do
23
25
  @live.dispatch do |this, pkt|
24
26
  next unless pkt
25
27
  block.call(this, pkt)
26
28
  end
27
29
  sleep 0.1
28
30
  end
31
+ DIY::Logger.debug "stopped loop recv..."
32
+ end
33
+
34
+ def break
35
+ DIY::Logger.debug "stopping loop recv..."
36
+ @running = false
29
37
  end
30
38
 
31
39
  def net
@@ -113,7 +113,7 @@ module DIY
113
113
  raise EOFError, " end of pcaps "
114
114
  end
115
115
  @position += 1
116
- DIY::Logger.info("pcap file changed: #{@pcap_files[@position]}")
116
+ DIY::Logger.info("pcap file changed: #{@pcap_files[@position]} ( #{@position} of #{@pcap_files.size} )")
117
117
  @off = FFI::PCap::Offline.new(@pcap_files[@position])
118
118
  @num = 0
119
119
  clear_cached_mac
@@ -12,7 +12,16 @@ module DIY
12
12
  end
13
13
 
14
14
  def inspect
15
- "#{Utils.pp(@content)} : from #{@detail_msg}"
15
+ "#<#{self.class.name}: #{Utils.pp(@content)}, from #{@detail_msg}>"
16
+ end
17
+
18
+ def pretty_print(lsize = 150)
19
+ real = Utils.pp(@content, false)
20
+ dot = nil
21
+ if inspect.size >= lsize
22
+ dot = "..."
23
+ end
24
+ sprintf "%-#{lsize+2}.#{lsize}s%3s (size= %4d), from %20s", real,dot,@content.size,@detail_msg
16
25
  end
17
26
  end
18
27
  end
@@ -37,5 +37,16 @@ module DIY
37
37
  end
38
38
  end
39
39
 
40
+ # 跳过相同源与目的MAC
41
+ class SkipSameMacStrategy < BasicStrategy
42
+ def call(hope_pkt, recv_pkt, queue)
43
+ if hope_pkt[0..5] == hope_pkt[6..11]
44
+ return OK
45
+ else
46
+ return NONE
47
+ end
48
+ end
49
+ end
50
+
40
51
  end
41
52
 
@@ -2,17 +2,21 @@ module DIY
2
2
  module Utils
3
3
  class << self
4
4
  # 漂亮输出包的前十个内容
5
- def pp(pkt)
5
+ def pp(pkt, size_print = true)
6
6
  pkt = pkt.content if pkt.kind_of?(DIY::Packet)
7
7
  return nil if pkt.nil?
8
8
  #~ ( pkt[0..10] + "..." ).dump + "(#{pkt.size} sizes)"
9
9
  size = pkt.size
10
+ size_print_str = ""
11
+ if size_print
12
+ size_print_str = "(#{size} sizes)"
13
+ end
10
14
  begin
11
15
  new_pkt = pkt.dup
12
- Mu::Pcap::Ethernet.from_bytes(new_pkt).to_s + "(#{size} sizes)"
16
+ Mu::Pcap::Ethernet.from_bytes(new_pkt).to_s + size_print_str
13
17
  rescue Mu::Pcap::ParseError =>e
14
- DIY::Logger.warn "parse error from pkt: " + ( pkt[0..10] + "..." ).dump + "(#{pkt.size} sizes)"
15
- return ( pkt[0..10] + "..." ).dump + "(#{pkt.size} sizes)"
18
+ DIY::Logger.warn "parse error from pkt: " + ( pkt[0..10] + "..." ).dump + size_print_str
19
+ return ( pkt[0..10] + "..." ).dump + size_print_str
16
20
  end
17
21
  end
18
22
 
@@ -1,5 +1,5 @@
1
1
  module DIY
2
2
  class PCAP
3
- VERSION = "0.2.7"
3
+ VERSION = "0.2.8"
4
4
  end
5
5
  end
@@ -13,6 +13,7 @@ module DIY
13
13
  @recv_t = nil
14
14
  @start = false
15
15
  @queue = Queue.new
16
+ @running = false
16
17
  loop_recv
17
18
  loop_callback
18
19
  end
@@ -20,7 +21,7 @@ module DIY
20
21
  # 发包
21
22
  def inject(pkts)
22
23
  pkts.each do |pkt|
23
- DIY::Logger.info "send pkt: #{pkt.inspect}"
24
+ DIY::Logger.info "send pkt: #{pkt.pretty_print}"
24
25
  @live.send_packet(pkt.content)
25
26
  end
26
27
  end
@@ -32,21 +33,25 @@ module DIY
32
33
  next unless @start
33
34
  @queue.push(pkt.body)
34
35
  end
36
+ DIY::Logger.debug "worker: stopped loop recv"
35
37
  end
36
38
  end
37
39
 
38
40
  def loop_callback
41
+ @running = true
39
42
  @callback_t = Thread.new do
40
43
  #~ DIY::Logger.info "start thread callbacking pkt..."
41
- loop do
44
+ while @running do
42
45
  begin
43
46
  pkt = @queue.pop
44
47
  #~ DIY::Logger.info "callback: #{pkt}"
45
48
  @block.call(pkt) if @block
46
49
  rescue DRb::DRbConnError
47
50
  DIY::Logger.info "closed connection by controller"
51
+ @queue.clear
48
52
  end
49
53
  end
54
+ DIY::Logger.debug "stopped loop callback"
50
55
  end
51
56
  end
52
57
 
@@ -58,12 +63,22 @@ module DIY
58
63
  @start = true
59
64
  end
60
65
 
66
+ # 停止收发
61
67
  def terminal
62
68
  DIY::Logger.info("stop recv pkt")
63
69
  @start = false
64
70
  @queue.clear
65
71
  end
66
72
 
73
+ # 停止线程
74
+ def stop
75
+ @running = false
76
+ @queue.push nil
77
+ @live.break
78
+ Utils.wait_until { @recv_t && ! @recv_t.alive? }
79
+ Utils.wait_until { @callback_t && ! @callback_t.alive? }
80
+ end
81
+
67
82
  def inspect
68
83
  "<Worker: #{@live.net}>"
69
84
  end
@@ -8,8 +8,11 @@ module DIY
8
8
  def initialize(worker, uri)
9
9
  @worker = worker
10
10
  @uri = uri
11
+ @running = false
12
+ @over = false
11
13
  yield self if block_given?
12
14
  end
15
+ attr_accessor :running
13
16
 
14
17
  def use_timeridconv
15
18
  require 'drb/timeridconv'
@@ -20,13 +23,21 @@ module DIY
20
23
  Thread.abort_on_exception = true
21
24
  DIY::Logger.info "serving at #{@uri}"
22
25
  DRb.start_service(@uri, @worker)
23
- running = true
24
- trap("INT") { running = false }
25
- while running
26
+ @running = true
27
+ @over = false
28
+ trap("INT") { @running = false }
29
+ while @running
26
30
  sleep 0.5
27
31
  end
28
32
  DIY::Logger.info "bye..."
33
+ @worker.stop
29
34
  DRb.stop_service
35
+ @over = true
36
+ end
37
+
38
+ def stop
39
+ @running = false
40
+ Utils.wait_until { @over }
30
41
  end
31
42
  end
32
43
  end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+ require 'diy/device_finder'
3
+
4
+ def raise_me
5
+ raise "Error on me"
6
+ end
7
+
8
+ describe "Controller" do
9
+ Thread.abort_on_exception = true
10
+ before(:all) do
11
+ @device_name = DIY::DeviceFinder.smart_select
12
+ @live = DIY::Live.new(@device_name)
13
+ @live2 = DIY::Live.new(@device_name)
14
+
15
+ @curi = "druby://localhost:7878"
16
+ @suri = "druby://localhost:7879"
17
+ @c1 = @s1 = nil
18
+ # client create
19
+ Thread.abort_on_exception = true
20
+ @client_t ||= Thread.new do
21
+ client = DIY::Worker.new(@live)
22
+ @c1 = DIY::WorkerKeeper.new(client, @curi)
23
+ @c1.run
24
+ end
25
+
26
+ @server_t ||= Thread.new do
27
+ server = DIY::Worker.new(@live2)
28
+ @s1 = DIY::WorkerKeeper.new(server, @suri)
29
+ @s1.run
30
+ end
31
+ end
32
+
33
+ after(:all) do
34
+ #~ @c1.stop #if @c
35
+ #~ @s1.stop #if @s
36
+ #~ DIY::Utils.wait_until { ! @client_t.alive? && ! @server_t.alive? }
37
+ end
38
+
39
+ it "#run " do
40
+ sleep 1
41
+ builder = DIY::Builder.new do
42
+ pcapfiles "helper/http.pcap"
43
+ use DIY::SimpleStrategy.new
44
+ timeout 10
45
+ end
46
+ lambda { builder.run }.should_not raise_error
47
+ end
48
+
49
+ it "#run stragety error" do
50
+
51
+
52
+ wrongUserStragety = lambda {
53
+ raise_me
54
+ #~ raise "error one me"
55
+ }
56
+
57
+ sleep 1
58
+ builder = DIY::Builder.new do
59
+ pcapfiles "helper/http.pcap"
60
+ use wrongUserStragety
61
+ timeout 10
62
+ end
63
+ lambda { builder.run }.should_not raise_error
64
+ end
65
+
66
+ it "#run before_send error" do
67
+ sleep 1
68
+ build2 = DIY::Builder.new do
69
+ before_send do
70
+ raise "error on me"
71
+ end
72
+ pcapfiles "helper/http.pcap"
73
+ end
74
+ lambda { build2.run }.should_not raise_error
75
+ end
76
+
77
+ it "#run big packet " do
78
+ sleep 1
79
+ build2 = DIY::Builder.new do
80
+ before_send do |pkt|
81
+ new_pkt = "a" * 10000
82
+ end
83
+ pcapfiles "helper/http.pcap"
84
+ end
85
+ lambda { build2.run }.should_not raise_error
86
+ end
87
+
88
+ end
Binary file
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe DIY::Packet do
4
+ let(:normal_content) { File.open('helper/tcp.dat', 'rb') { |io| io.read } }
5
+ let(:long_content) { File.open('helper/long.dat', 'rb') { |io| io.read } }
6
+ it "#pretty_print normal_content" do
7
+ pkt = DIY::Packet.new(normal_content, "xxx")
8
+ puts pkt.pretty_print
9
+ end
10
+
11
+ it "#pretty_print long_content" do
12
+ pkt = DIY::Packet.new(long_content, "xxx")
13
+ puts pkt.pretty_print
14
+ end
15
+ end
@@ -5,6 +5,10 @@ describe DIY::Utils do
5
5
  DIY::Utils.pp('a' * 100).should match(/\(100 sizes\)/)
6
6
  end
7
7
 
8
+ it "#pp false" do
9
+ DIY::Utils.pp('a' * 100, false).should_not match(/\(100 sizes\)/)
10
+ end
11
+
8
12
  it "#src_mac" do
9
13
  DIY::Utils.src_mac( 'a' * 100 ).should == "a" * 6
10
14
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DIY-pcap
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 7
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 2
8
- - 7
9
- version: 0.2.7
9
+ - 8
10
+ version: 0.2.8
10
11
  platform: ruby
11
12
  authors:
12
13
  - yafei Lee
@@ -14,8 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2012-10-10 00:00:00 +08:00
18
- default_executable:
18
+ date: 2012-10-11 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: ffi-pcap
@@ -25,6 +25,7 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
+ hash: 23
28
29
  segments:
29
30
  - 0
30
31
  - 2
@@ -129,6 +130,7 @@ files:
129
130
  - spec/helper/app.pcap
130
131
  - spec/helper/gre.pcap
131
132
  - spec/helper/http.pcap
133
+ - spec/helper/long.dat
132
134
  - spec/helper/pkt1
133
135
  - spec/helper/pkt2
134
136
  - spec/helper/pkt3
@@ -140,11 +142,10 @@ files:
140
142
  - spec/logger_spec.rb
141
143
  - spec/mu_parser_spec.rb
142
144
  - spec/offline_spec.rb
145
+ - spec/packet_spec.rb
143
146
  - spec/spec_helper.rb
144
- - spec/usererror_spec.rb
145
147
  - spec/utils_spec.rb
146
148
  - spec/worker_spec.rb
147
- has_rdoc: true
148
149
  homepage: ""
149
150
  licenses: []
150
151
 
@@ -158,6 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
159
  requirements:
159
160
  - - ">="
160
161
  - !ruby/object:Gem::Version
162
+ hash: 3
161
163
  segments:
162
164
  - 0
163
165
  version: "0"
@@ -166,13 +168,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
168
  requirements:
167
169
  - - ">="
168
170
  - !ruby/object:Gem::Version
171
+ hash: 3
169
172
  segments:
170
173
  - 0
171
174
  version: "0"
172
175
  requirements: []
173
176
 
174
177
  rubyforge_project:
175
- rubygems_version: 1.3.7
178
+ rubygems_version: 1.8.24
176
179
  signing_key:
177
180
  specification_version: 3
178
181
  summary: DIY pcap send and recv
@@ -1,85 +0,0 @@
1
- require 'spec_helper'
2
- require 'diy/device_finder'
3
-
4
- def raise_me
5
- raise "Error on me"
6
- end
7
-
8
- describe "Controller" do
9
-
10
- before(:all) do
11
- @device_name = DIY::DeviceFinder.smart_select
12
- @live = DIY::Live.new(@device_name)
13
- @live2 = DIY::Live.new(@device_name)
14
-
15
- @curi = "druby://localhost:7878"
16
- @suri = "druby://localhost:7879"
17
-
18
- # client create
19
- Thread.abort_on_exception = true
20
- @client_t ||= Thread.new do
21
- client = DIY::Worker.new(@live)
22
- DIY::WorkerKeeper.new(client, @curi).run
23
- end
24
-
25
- @server_t ||= Thread.new do
26
- server = DIY::Worker.new(@live2)
27
- DIY::WorkerKeeper.new(server, @suri).run
28
- end
29
- end
30
-
31
- after(:all) do
32
- @client_t.kill
33
- @server_t.kill
34
- end
35
-
36
- it "#run " do
37
- sleep 1
38
- builder = DIY::Builder.new do
39
- pcapfiles "helper/http.pcap"
40
- use DIY::SimpleStrategy.new
41
- timeout 10
42
- end
43
- lambda { builder.run }.should_not raise_error
44
- end
45
-
46
- it "#run stragety error" do
47
-
48
-
49
- wrongUserStragety = lambda {
50
- raise_me
51
- #~ raise "error one me"
52
- }
53
-
54
- sleep 1
55
- builder = DIY::Builder.new do
56
- pcapfiles "helper/http.pcap"
57
- use wrongUserStragety
58
- timeout 10
59
- end
60
- lambda { builder.run }.should_not raise_error
61
- end
62
-
63
- it "#run before_send error" do
64
- sleep 1
65
- build2 = DIY::Builder.new do
66
- before_send do
67
- raise "error on me"
68
- end
69
- pcapfiles "helper/http.pcap"
70
- end
71
- lambda { build2.run }.should_not raise_error
72
- end
73
-
74
- it "#run big packet " do
75
- sleep 1
76
- build2 = DIY::Builder.new do
77
- before_send do |pkt|
78
- new_pkt = "a" * 10000
79
- end
80
- pcapfiles "helper/http.pcap"
81
- end
82
- lambda { build2.run }.should_not raise_error
83
- end
84
-
85
- end