Module: DIY::Utils

Defined in:
lib/diy/utils.rb

Class Method Summary collapse

Class Method Details

.ary_match(ary, msg) ⇒ Object



74
75
76
77
78
79
# File 'lib/diy/utils.rb', line 74

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/diy/utils.rb', line 53

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, Exception =>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

.pp_mac(mac) ⇒ Object



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

def pp_mac(mac)
  raise "MAC MUST BE 6 sizes" unless mac.size == 6
  begin
    '%02x:%02x:%02x:%02x:%02x:%02x' % mac.unpack('C6')
  rescue ArgumentError
    mac.dump
  end
end


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

def print_backtrace(e)
  DIY::Logger.info "Dump Exception: #{e.class} -> #{e.message}..."
  e.backtrace.each do |msg|
    DIY::Logger.info(msg)
  end
  DIY::Logger.info("Dump 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



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

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