Class: HopTracker::Prober

Inherits:
Object
  • Object
show all
Defined in:
lib/hoptracker/prober.rb

Overview

The Prober class provides tools to probe the hops on the way to a destination.

A type check does not take place in any function of the class. It is assumed that the correct type (and within its value limits) is used for each argument.

Instance Method Summary collapse

Constructor Details

#initialize(destination) ⇒ Prober

Returns a new instance of Prober.

Parameters:

  • destination (String)

    The destination (an IP address or host name)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hoptracker/prober.rb', line 34

def initialize(destination)
  # If necessary, resolve the DNS name and return the corresponding Addrinfo object
  possible_destinations = Addrinfo.getaddrinfo(destination, 33434, nil, Socket::SOCK_DGRAM, Socket::IPPROTO_UDP)
  # Delete all objects that do not have an IP address (such as Unix sockets)
  possible_destinations.keep_if(&:ip?)
  # If there are no more addresses left, there is a problem.
  raise FailedToInitializeProber, 'No addresses available for probing' if possible_destinations.empty?
  # The destination should be the first remaining address.
  @destination = possible_destinations[0]

  # Next, we create a socket. This automatically binds to a fixed local port so that this no longer needs to be done later with #bind.
  # The fixed local port is one of the prerequisites for Paris-Traceroute.
  @socket = @destination.connect

  # Next, we allow errors to be received, which means that we will later have ICMP error messages. Furthermore, we set the flag for MTU Discover so that packets are not fragmented. We also want to know the TTL of the packet we are receiving.
  if @destination.ipv6?
    @socket.setsockopt Socket::IPPROTO_IPV6, Socket::IPV6_RECVERR, 1
    @socket.setsockopt Socket::IPPROTO_IPV6, Socket::IPV6_MTU_DISCOVER, 3
    @socket.setsockopt Socket::IPPROTO_IPV6, Socket::IPV6_RECVHOPLIMIT, 1
  else
    @socket.setsockopt Socket::IPPROTO_IP, Socket::IP_RECVERR, 1
    @socket.setsockopt Socket::IPPROTO_IP, Socket::IP_MTU_DISCOVER, 3
    @socket.setsockopt Socket::IPPROTO_IP, Socket::IP_RECVTTL, 1
  end
rescue SocketError => e
  # If there is an error when creating the socket, the traceroute cannot be executed.
  raise FailedToInitializeProber, e.message
end

Instance Method Details

#probe(ttl, mtu) ⇒ Object

Probe sends a probe, receives it and creates a usable result.

Parameters:

  • ttl (Integer)
  • mtu (Integer)


67
68
69
70
71
72
73
# File 'lib/hoptracker/prober.rb', line 67

def probe(ttl, mtu)
  sended_probes = send_probes ttl, mtu, 3
  received_probes = receive_probes ttl, mtu, 3, 5
  result = process_probes sended_probes, received_probes, ttl

  return result
end