Class: Lanet::Traceroute

Inherits:
Object
  • Object
show all
Defined in:
lib/lanet/traceroute.rb

Constant Summary collapse

PROTOCOLS =

Supported protocols

i[icmp udp tcp].freeze
DEFAULT_MAX_HOPS =

Default settings

30
DEFAULT_TIMEOUT =
1
DEFAULT_QUERIES =
3
DEFAULT_PORT =

Starting port for UDP traceroute

33_434

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol: :udp, max_hops: DEFAULT_MAX_HOPS, timeout: DEFAULT_TIMEOUT, queries: DEFAULT_QUERIES) ⇒ Traceroute

Returns a new instance of Traceroute.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lanet/traceroute.rb', line 20

def initialize(protocol: :udp, max_hops: DEFAULT_MAX_HOPS, timeout: DEFAULT_TIMEOUT, queries: DEFAULT_QUERIES)
  @protocol = protocol.to_sym
  @max_hops = max_hops
  @timeout = timeout
  @queries = queries
  @results = []

  return if PROTOCOLS.include?(@protocol)

  raise ArgumentError, "Protocol must be one of #{PROTOCOLS.join(", ")}"
end

Instance Attribute Details

#max_hopsObject (readonly)

Returns the value of attribute max_hops.



18
19
20
# File 'lib/lanet/traceroute.rb', line 18

def max_hops
  @max_hops
end

#protocolObject (readonly)

Returns the value of attribute protocol.



18
19
20
# File 'lib/lanet/traceroute.rb', line 18

def protocol
  @protocol
end

#queriesObject (readonly)

Returns the value of attribute queries.



18
19
20
# File 'lib/lanet/traceroute.rb', line 18

def queries
  @queries
end

#resultsObject (readonly)

Returns the value of attribute results.



18
19
20
# File 'lib/lanet/traceroute.rb', line 18

def results
  @results
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



18
19
20
# File 'lib/lanet/traceroute.rb', line 18

def timeout
  @timeout
end

Instance Method Details

#trace(destination) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lanet/traceroute.rb', line 32

def trace(destination)
  @results = []
  destination_ip = resolve_destination(destination)

  begin
    trace_protocol(destination_ip)
  rescue StandardError => e
    raise e unless e.message.include?("Must run as root/administrator")

    # Fall back to system traceroute command if we don't have root privileges
    trace_using_system_command(destination)
  end

  @results
end