Class: DIY::PCAP

Inherits:
Object
  • Object
show all
Defined in:
lib/diy/pcap.rb,
lib/diy/version.rb

Constant Summary collapse

VERSION =
"0.4.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ PCAP

Returns a new instance of PCAP.

Yields:

  • (_self)

Yield Parameters:

  • _self (DIY::PCAP)

    the object that the method was called on



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

def initialize
  @timeout = 10
  @dir = Dir.pwd
  @pkt_stack = []
  @device_name = FFI::PCap.dump_devices[0][0]
  yield self
  @driver = FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
  #~ pause_for_user
  run
end

Instance Attribute Details

#device_nameObject

Returns the value of attribute device_name.



17
18
19
# File 'lib/diy/pcap.rb', line 17

def device_name
  @device_name
end

#dirObject

Returns the value of attribute dir.



17
18
19
# File 'lib/diy/pcap.rb', line 17

def dir
  @dir
end

#timeoutObject

Returns the value of attribute timeout.



17
18
19
# File 'lib/diy/pcap.rb', line 17

def timeout
  @timeout
end

Instance Method Details

#fill60(pkt) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/diy/pcap.rb', line 92

def fill60(pkt)
  if pkt.size < 60
    logger.debug "pkt size #{pkt.size} less than 60, fill with zero"
    pkt += "0" * (60 - pkt.size)
  end
  pkt
end

#loggerObject



100
101
102
# File 'lib/diy/pcap.rb', line 100

def logger
  @@logger ||= DIY::Logger
end

#logger=(logger) ⇒ Object



104
105
106
# File 'lib/diy/pcap.rb', line 104

def logger=(logger)
  @@logger = logger
end

#pause_for_userObject



44
45
46
47
# File 'lib/diy/pcap.rb', line 44

def pause_for_user
  puts "Input ENTER for going..."
  gets
end

#pkt_dir2pkt(dir) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/diy/pcap.rb', line 27

def pkt_dir2pkt(dir)
  #~ File.read( File.join( @dir, dir ) )
  ret = ""
  File.open( File.join( @dir, dir), "rb") do |f|
    ret += f.read(65535) until f.eof?
  end
  ret
end

#recv(pkt_dir) ⇒ Object



23
24
25
# File 'lib/diy/pcap.rb', line 23

def recv(pkt_dir)
  @pkt_stack << PacketEx.new( pkt_dir2pkt(pkt_dir), PacketEx::RECV, pkt_dir)
end

#recv_pkt(pkt) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/diy/pcap.rb', line 77

def recv_pkt(pkt)
  logger.info("I hope pkt: #{pkt.pkt[0..10].dump}(file: #{pkt.filename}, size: #{pkt.size})...")
  pkt = pkt.pkt
  pkt = fill60(pkt)
  @driver.loop do |this, new_pkt|
    #~ logger.info("recv pkt: [ #{new_pkt.time} ]: #{new_pkt.body[0..10].dump}..." )
    new_pkt_body = fill60(new_pkt.body)
    if new_pkt_body == pkt
      logger.info("recv pkt: [ #{new_pkt.time} ]: #{new_pkt_body[0..10].dump}..." )
      logger.info "got the same pkt,next"
      return true
    end
  end
end

#runObject



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

def run
  if $SERVER
    run_server
  else
    run_client
  end
end

#run_clientObject



49
50
51
52
53
54
55
56
57
# File 'lib/diy/pcap.rb', line 49

def run_client
  @pkt_stack.each do |pkt|
    if pkt.to_outer?
      send_pkt(pkt)
    else
      recv_pkt(pkt)
    end
  end
end

#run_serverObject



59
60
61
62
63
64
65
66
67
# File 'lib/diy/pcap.rb', line 59

def run_server
  @pkt_stack.each do |pkt|
    if pkt.to_inner?
      send_pkt(pkt)
    else
      recv_pkt(pkt)
    end
  end
end

#send(pkt_dir) ⇒ Object



19
20
21
# File 'lib/diy/pcap.rb', line 19

def send(pkt_dir)
  @pkt_stack << PacketEx.new( pkt_dir2pkt(pkt_dir), PacketEx::SEND, pkt_dir)
end

#send_pkt(pkt) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/diy/pcap.rb', line 69

def send_pkt(pkt)
  sleep 1
  logger.info("send pkt: [ #{Time.now} ]#{pkt.pkt[0..10].dump}(file: #{pkt.filename}, size: #{pkt.size})...")
  pkt = pkt.pkt
  pkt = fill60(pkt)
  @driver.send_packet(pkt)
end