Class: EM::Traceroute::Hop

Inherits:
Object
  • Object
show all
Defined in:
lib/emtraceroute/hop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, ttl) ⇒ Hop

Returns a new instance of Hop.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/emtraceroute/hop.rb', line 4

def initialize target, ttl
  @found = false
  @tries = 0
  @last_try = 0
  @remote_ip = nil
  @remote_icmp = nil
  @location = ""
  @ttl = ttl
  @ip = Iphdr.new(Socket::IPPROTO_ICMP, '0.0.0.0', target) # IP header
  @ip.ttl = ttl
  @ip.id += ttl

  @icmp = Icmphdr.new("traceroute") # ICMP header
  @icmp.id = @ip.id
  @ip.data = @icmp.assemble

  @pkt = @ip.assemble
end

Instance Attribute Details

#foundObject

Returns the value of attribute found.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def found
  @found
end

#icmpObject

Returns the value of attribute icmp.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def icmp
  @icmp
end

#ipObject

Returns the value of attribute ip.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def ip
  @ip
end

#locationObject

Returns the value of attribute location.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def location
  @location
end

#pktObject

Returns the value of attribute pkt.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def pkt
  @pkt
end

#remote_icmpObject

Returns the value of attribute remote_icmp.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def remote_icmp
  @remote_icmp
end

#remote_ipObject

Returns the value of attribute remote_ip.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def remote_ip
  @remote_ip
end

#triesObject

Returns the value of attribute tries.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def tries
  @tries
end

#ttlObject

Returns the value of attribute ttl.



3
4
5
# File 'lib/emtraceroute/hop.rb', line 3

def ttl
  @ttl
end

Instance Method Details

#to_sObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/emtraceroute/hop.rb', line 29

def to_s
  if @found
    ip = ":: #{remote_ip.src}"
    ping = "#{(@found - @last_try).round(3)}s"
  else
    ip = "??"
    ping = "-"
  end

  location = ":: #{@location}"
  "#{@ttl}. #{ping} #{ip} #{location}"
end