Module: ICMP4EM::Proxy::IcmpHandler

Defined in:
lib/icmp4em/proxy/icmp_handler.rb

Instance Method Summary collapse

Instance Method Details

#initialize(args = {}) ⇒ Object



4
5
6
# File 'lib/icmp4em/proxy/icmp_handler.rb', line 4

def initialize args = {}
  @pending_requests = args[:pending_requests]
end

#log(msg) ⇒ Object



8
9
10
# File 'lib/icmp4em/proxy/icmp_handler.rb', line 8

def log msg
  puts "[#{Time.now}] #{msg}" if $debug
end

#notify_readableObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/icmp4em/proxy/icmp_handler.rb', line 12

def notify_readable
  data, host = @io.recvfrom(1500)
  icmp_data = data[20, data.length]
  
  log "Received ICMP response from #{host.ip_address}"

  begin
    packet = Packet.from_bytes(icmp_data)
  rescue ArgumentError
    log "  Got exception while parsing packet: #{$!}"
    return
  end

  log "  Key = #{packet.key_string}"

  request = @pending_requests.delete(packet.key)

  if request.nil?
    log "  Unexpected packet, dropping"
    return
  end

  request.succeed packet
end

#unbindObject



37
38
39
# File 'lib/icmp4em/proxy/icmp_handler.rb', line 37

def unbind
  @socket.close if @socket
end