Class: DIY::PCAP
- Inherits:
-
Object
- Object
- DIY::PCAP
- Defined in:
- lib/diy/pcap.rb,
lib/diy/version.rb
Constant Summary collapse
- VERSION =
"0.4.3"
Instance Attribute Summary collapse
-
#device_name ⇒ Object
Returns the value of attribute device_name.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #fill60(pkt) ⇒ Object
-
#initialize {|_self| ... } ⇒ PCAP
constructor
A new instance of PCAP.
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
- #pause_for_user ⇒ Object
- #pkt_dir2pkt(dir) ⇒ Object
- #recv(pkt_dir) ⇒ Object
- #recv_pkt(pkt) ⇒ Object
- #run ⇒ Object
- #run_client ⇒ Object
- #run_server ⇒ Object
- #send(pkt_dir) ⇒ Object
- #send_pkt(pkt) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ PCAP
Returns a new instance of PCAP.
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_name ⇒ Object
Returns the value of attribute device_name.
17 18 19 |
# File 'lib/diy/pcap.rb', line 17 def device_name @device_name end |
#dir ⇒ Object
Returns the value of attribute dir.
17 18 19 |
# File 'lib/diy/pcap.rb', line 17 def dir @dir end |
#timeout ⇒ Object
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 |
#logger ⇒ Object
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_user ⇒ Object
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 |
#run ⇒ Object
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_client ⇒ Object
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_server ⇒ Object
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 |