Module: DIY::Utils

Defined in:
lib/diy/utils.rb

Class Method Summary collapse

Class Method Details

.ary_match(ary, msg) ⇒ Object



57
58
59
60
61
62
# File 'lib/diy/utils.rb', line 57

def ary_match(ary, msg)
  ary.each do |e|
    return true if /#{Regexp.escape(e)}/ === msg
  end
  nil
end

.dst_mac(pkt) ⇒ Object



30
31
32
33
# File 'lib/diy/utils.rb', line 30

def dst_mac(pkt)
  pkt = pkt.content if pkt.kind_of?(DIY::Packet)
  pkt[0..5]
end

.filter_backtrace(e) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/diy/utils.rb', line 44

def filter_backtrace(e) 
  filter_ary = [ "/lib/diy/controller.rb", "/lib/diy/strategy_builder.rb" ]
  new_bt = []
  e.backtrace.each do |msg|
    if ! Utils.ary_match(filter_ary, msg)
     new_bt << msg
    else
      break
    end
  end
  new_bt
end

.pp(pkt, size_print = true) ⇒ Object

漂亮输出包的前十个内容



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

def pp(pkt, size_print = true)
  pkt = pkt.content if pkt.kind_of?(DIY::Packet)
  return nil if pkt.nil?
  size = pkt.size
  size_print_str = ""
  
  if size_print
    size_print_str = "(#{size} sizes)"
  end
  
  begin
    new_pkt = pkt.dup
    Mu::Pcap::Ethernet.from_bytes(new_pkt).to_s + size_print_str
  rescue Mu::Pcap::ParseError =>e
    DIY::Logger.debug "parse error from pkt: " + ( pkt[0..10] + "..." ).dump + size_print_str
    return  ( pkt[0..10] + "..." ).dump + size_print_str + "( parse failed )"
  end
end

.src_mac(pkt) ⇒ Object



25
26
27
28
# File 'lib/diy/utils.rb', line 25

def src_mac(pkt)
  pkt = pkt.content if pkt.kind_of?(DIY::Packet)
  pkt[6..11]
end

.wait_until(timeout = 20, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/diy/utils.rb', line 35

def wait_until( timeout = 20, &block )
  timeout(timeout) do
    loop do
      break if block.call
      sleep 0.01
    end
  end
end