Class: DIY::Offline

Inherits:
Object
  • Object
show all
Defined in:
lib/diy/offline.rb

Instance Method Summary collapse

Constructor Details

#initialize(pcap_files) ⇒ Offline

Returns a new instance of Offline.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/diy/offline.rb', line 5

def initialize( pcap_files )
  @pcap_files = [ pcap_files ] if pcap_files.kind_of?(String)
  @pcap_files ||= pcap_files
  @off = FFI::PCap::Offline.new(@pcap_files[0])
  # 记录文件在目录中的位置
  @position = 0
  # 记录包在当前文件的位置
  @num = 0
  
  @tmp_pcap = nil
end

Instance Method Details

#compare_mac(op, mac1, mac2) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/diy/offline.rb', line 53

def compare_mac( op, mac1, mac2)
  if op == "=="
    mac1 == mac2
  elsif op == "!="
    mac1 != mac2
  else
    raise "error op"
  end
end

#fetch_oneObject



63
64
65
66
67
68
69
# File 'lib/diy/offline.rb', line 63

def fetch_one
  pkt = fetch_cache
  if pkt.nil?
    pkt = self.next
  end
  pkt
end

#filenameObject



123
124
125
# File 'lib/diy/offline.rb', line 123

def filename
  @pcap_files[@position]
end

#fullnameObject



127
128
129
# File 'lib/diy/offline.rb', line 127

def fullname
  "pkt: `#{filename}: #{@num}th' "
end

#next_pcapObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/diy/offline.rb', line 111

def next_pcap
  if @position >= @pcap_files.size - 1
    raise EOFError, " end of pcaps "
  end
  @position += 1
  DIY::Logger.info("pcap file changed: #{@pcap_files[@position]} ( #{@position} of #{@pcap_files.size} )")
  @off = FFI::PCap::Offline.new(@pcap_files[@position])
  @num = 0
  clear_cached_mac
  fetch_cache
end

#nextsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/diy/offline.rb', line 17

def nexts
  ret = []
  # 取一个
  pkt = fetch_one
  if pkt.nil?
    next_pcap
    pkt = fetch_one
  end
  
  ret << pkt
  op = "=="
  if ! fetch_cached_mac
    cached_mac(pkt)
  else
    if Utils.src_mac(pkt) != fetch_cached_mac
      op = "!="
    end
  end
  
  loop do
    pkt = self.next
    if pkt.nil?
      return ret
    end
    
    if compare_mac( op, Utils.src_mac(pkt), fetch_cached_mac)
      ret << pkt
    else
      cached(pkt)
      return ret
    end
    
  end
  
end