DIY-pcap 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,7 +56,7 @@ module DIY
56
56
  @round_count += 1
57
57
  DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].pretty_print}:(queue= #{pkts.size})"
58
58
  if pkts.size >= 10
59
- DIY::Logger.warn "queue size too big: #{pkts.size}, maybe something error"
59
+ DIY::Logger.info "queue size too big: #{pkts.size}, maybe something error"
60
60
  end
61
61
  server.ready do |recv_pkt|
62
62
  next if @error_flag # error accur waiting other thread do with it
data/lib/diy/offline.rb CHANGED
@@ -112,6 +112,8 @@ module DIY
112
112
  if @position >= @pcap_files.size - 1
113
113
  raise EOFError, " end of pcaps "
114
114
  end
115
+ # must close before's handle
116
+ @off.close
115
117
  @position += 1
116
118
  DIY::Logger.info("pcap file changed: #{@pcap_files[@position]} ( #{@position} of #{@pcap_files.size} )")
117
119
  @off = FFI::PCap::Offline.new(@pcap_files[@position])
@@ -21,7 +21,13 @@ class Ethernet < Packet
21
21
  super()
22
22
  @src = src
23
23
  @dst = dst
24
- @type = type
24
+ @type = type
25
+ @vlan = false
26
+ end
27
+ attr_accessor :vlan
28
+
29
+ def vlan?
30
+ vlan
25
31
  end
26
32
 
27
33
  def flow_id
@@ -44,13 +50,19 @@ class Ethernet < Packet
44
50
  dst = MAC_TEMPLATE % dst
45
51
  src = bytes.slice!(0,6).unpack FMT_MAC
46
52
  src = MAC_TEMPLATE % src
47
- type = bytes.slice!(0,2).unpack(FMT_n)[0]
53
+ type = bytes.slice!(0,2).unpack(FMT_n)[0]
54
+ if type == ETHERTYPE_802_1Q
55
+ @vlan = true
56
+ else
57
+ @vlan = false
58
+ end
48
59
  while (type == ETHERTYPE_802_1Q)
49
60
  # Skip 4 bytes for 802.1q vlan tag field
50
61
  bytes.slice!(0,2)
51
62
  type = bytes.slice!(0,2).unpack(FMT_n)[0]
52
63
  end
53
- ethernet = Ethernet.new src, dst, type
64
+ ethernet = Ethernet.new src, dst, type
65
+ ethernet.vlan = @vlan
54
66
  ethernet.payload = bytes
55
67
  ethernet.payload_raw = bytes
56
68
  begin
data/lib/diy/strategy.rb CHANGED
@@ -2,10 +2,11 @@ module DIY
2
2
  # 这个策略是一个最基本的:
3
3
  # 具体返回值含义见 @BasicStrategy
4
4
  class Strategy
5
- OK = true
6
- OK_NO_POP = SKIP = 1
7
- FAIL = false
8
- NONE = nil
5
+ OK = "S_OK"
6
+ OK_NO_POP = SKIP = "S_OK_NO_POP"
7
+ FAIL = "S_FAIL"
8
+ NONE = "S_NONE"
9
+ NONE_HOPE_SKIP = NONE_HOPE_POP = "S_NONE_HOPE_POP"
9
10
  end
10
11
 
11
12
  class BasicStrategy < Strategy
@@ -21,6 +22,7 @@ module DIY
21
22
  # SKIP: 同上, 可用于跳过以后所有策略队列使用.
22
23
  # FAIL: 肯定失败时使用
23
24
  # NONE: 不匹配, 让框架进行下一个报文匹配
25
+ # NONE_HOPE_POP: 跳过期望报文, 但继续让框架进行下一个报文匹配
24
26
  def call(hope_pkt, recv_pkt, queue)
25
27
  raise "write code here"
26
28
  end
@@ -41,7 +43,7 @@ module DIY
41
43
  class SkipSameMacStrategy < BasicStrategy
42
44
  def call(hope_pkt, recv_pkt, queue)
43
45
  if hope_pkt[0..5] == hope_pkt[6..11]
44
- return OK
46
+ return NONE_HOPE_POP
45
47
  else
46
48
  return NONE
47
49
  end
@@ -50,6 +50,10 @@ module DIY
50
50
  elsif ret == Strategy::NONE
51
51
  #~ logger.debug("pkt jumpped:")
52
52
  next
53
+ elsif ret == Strategy::NONE_HOPE_POP
54
+ queue.shift
55
+ hope_pkt = queue.first
56
+ next
53
57
  end # end of if
54
58
  end # end of begin
55
59
  end # end of each
data/lib/diy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module DIY
2
2
  class PCAP
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -45,6 +45,27 @@ describe "Controller" do
45
45
  end
46
46
  lambda { builder.run }.should_not raise_error
47
47
  end
48
+
49
+ it "#run none_hope_skip" do
50
+ running = false
51
+ hope_skip = lambda { |h, r, q|
52
+ if running == false
53
+ running == true
54
+ return DIY::Strategy::NONE_HOPE_SKIP
55
+ else
56
+ return DIY::Strategy::NONE
57
+ end
58
+ }
59
+
60
+ sleep 1
61
+ builder = DIY::Builder.new do
62
+ pcapfiles "helper/http.pcap"
63
+ use hope_skip
64
+ use DIY::SimpleStrategy.new
65
+ timeout 10
66
+ end
67
+ lambda { builder.run }.should_not raise_error
68
+ end
48
69
 
49
70
  it "#run stragety error" do
50
71
 
Binary file
@@ -2,11 +2,19 @@ require 'spec_helper'
2
2
 
3
3
  describe Mu do
4
4
  let(:pkt) { File.open("helper/tcp.dat", "rb") { |f| f.read } }
5
+ let(:vlan_pkt) { File.open("helper/vlan.dat", "rb") { |f| f.read } }
5
6
  it "#ethernet parse" do
6
7
  eth = Mu::Pcap::Ethernet.from_bytes(pkt)
8
+ eth.vlan.should == false
7
9
  eth.should be_ip
8
10
  ip = eth.payload
9
11
  tcp = ip.payload
10
12
  tcp.should be_kind_of(Mu::Pcap::TCP)
11
13
  end
14
+
15
+ it "#ethernet vlan" do
16
+ eth = Mu::Pcap::Ethernet.from_bytes(vlan_pkt)
17
+ eth.vlan.should == true
18
+ eth.should be_vlan
19
+ end
12
20
  end
data/spec/offline_spec.rb CHANGED
@@ -41,4 +41,17 @@ describe DIY::Offline do
41
41
  lambda { loop do offline.nexts end }.should raise_error(DIY::EOFError)
42
42
  end
43
43
 
44
+ it "should open many files" do
45
+ files = []
46
+ 600.times do
47
+ files << "helper/http.pcap"
48
+ end
49
+ puts "files size = #{files.size}"
50
+ offline = DIY::Offline.new(files)
51
+ lambda {
52
+ loop do
53
+ offline.next_pcap
54
+ end }.should raise_error(DIY::EOFError)
55
+ end
56
+
44
57
  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: 17
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 3
8
- - 0
9
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
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-11 00:00:00 +08:00
18
- default_executable:
18
+ date: 2012-10-12 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
@@ -137,6 +138,7 @@ files:
137
138
  - spec/helper/pkt5
138
139
  - spec/helper/ssh.pcap
139
140
  - spec/helper/tcp.dat
141
+ - spec/helper/vlan.dat
140
142
  - spec/live_spec.rb
141
143
  - spec/logger_spec.rb
142
144
  - spec/mu_parser_spec.rb
@@ -145,7 +147,6 @@ files:
145
147
  - spec/spec_helper.rb
146
148
  - spec/utils_spec.rb
147
149
  - spec/worker_spec.rb
148
- has_rdoc: true
149
150
  homepage: ""
150
151
  licenses: []
151
152
 
@@ -159,6 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
160
  requirements:
160
161
  - - ">="
161
162
  - !ruby/object:Gem::Version
163
+ hash: 3
162
164
  segments:
163
165
  - 0
164
166
  version: "0"
@@ -167,13 +169,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
169
  requirements:
168
170
  - - ">="
169
171
  - !ruby/object:Gem::Version
172
+ hash: 3
170
173
  segments:
171
174
  - 0
172
175
  version: "0"
173
176
  requirements: []
174
177
 
175
178
  rubyforge_project:
176
- rubygems_version: 1.3.7
179
+ rubygems_version: 1.8.24
177
180
  signing_key:
178
181
  specification_version: 3
179
182
  summary: DIY pcap send and recv