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.

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/diy/offline.rb', line 6

def initialize( pcap_files )
  @pcap_files = [ pcap_files ] if pcap_files.kind_of?(String)
  @pcap_files ||= pcap_files
  raise ZeroOfflineError, "no files found" if @pcap_files.size == 0
  @off = FFI::PCap::Offline.new(@pcap_files[0])
  
  @ml = MacLearner.new
  
  # 记录文件在目录中的位置
  @position = 0
  # 记录包在当前文件的位置
  @num = 0
  # 记录所有包的个数
  @total = 0
  
  @tmp_pcap = nil
end

Instance Method Details

#_nextsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/diy/offline.rb', line 38

def _nexts
  ret = []
  # 取一个
  pkt = fetch_one
  if pkt.nil?
    next_pcap
    pkt = fetch_one
  end
  
  ret << pkt
  where = @ml.tellme(pkt.content)
  
  loop do
    pkt = fetch_one
    return [ret, where] if pkt.nil?
    #~ if pkt.nil?
      #~ next_pcap
      #~ pkt = fetch_one
    #~ end
    if @ml.tellme(pkt.content) != where
      cached(pkt)
      return [ret, where]
    else
      ret << pkt
    end
  end
  
end

#fetch_oneObject



67
68
69
70
71
72
73
# File 'lib/diy/offline.rb', line 67

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

#filenameObject



116
117
118
# File 'lib/diy/offline.rb', line 116

def filename
  @pcap_files[@position]
end

#files_sizeObject



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

def files_size
  @pcap_files.size
end

#fullnameObject



120
121
122
# File 'lib/diy/offline.rb', line 120

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

#mac_learnerObject



24
25
26
# File 'lib/diy/offline.rb', line 24

def mac_learner
  @ml
end

#next_pcapObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/diy/offline.rb', line 103

def next_pcap
  if @position >= @pcap_files.size - 1
    raise EOFError, " end of pcaps "
  end
  # must close before's handle
  @off.close
  @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
  fetch_cache
end

#nextsObject



28
29
30
31
32
33
34
35
36
# File 'lib/diy/offline.rb', line 28

def nexts
  begin
    return _nexts
  rescue DIY::MacLearnConflictError, DIY::PacketInvalidError =>e
    DIY::Logger.warn "Found Error when parse #{fullname}: #{e.message}"
    next_pcap
    retry
  end
end

#now_sizeObject



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

def now_size
  @total
end