Class: TincScanner::TincMetaconnection

Inherits:
Object
  • Object
show all
Defined in:
lib/tinc_scanner/tinc_metaconnection.rb

Overview

Represents a Tinc Metaconnection that has been set up with the Legacy Authentication Protocol

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 123) ⇒ TincMetaconnection

Initializes a Tinc metaconnection

Parameters:

  • host (String)
  • port (Integer) (defaults to: 123)

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/tinc_scanner/tinc_metaconnection.rb', line 17

def initialize(host, port = 123)
  raise ArgumentError, 'Host must be a string' unless host.is_a? String
  raise ArgumentError, 'Port must be an integer' unless port.is_a? Integer

  begin
    @socket = TCPSocket.new(host, port)
  rescue SocketError, EncodingError, EOFError, IO::TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNRESET => e
    raise TincConnectionError, "Failed to connect to Tinc: #{e.message}"
  end
end

Instance Method Details

#receive_idHash

Returns:

  • (Hash)

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tinc_scanner/tinc_metaconnection.rb', line 35

def receive_id
  answer = @socket.gets
  answer.chomp!
  request_code, *request = answer.split

  raise InvalidTincMessageError, "Request code is not 0: #{request_code}" unless request_code == '0'

  nodename, protocol_version = request

  result = {
    nodename:,
    protocol_version:
  }

  return result
end

#send_id(nodename, protocol_version = '17') ⇒ Object

Parameters:

  • nodename (String)
  • protocol_version (String) (defaults to: '17')


30
31
32
# File 'lib/tinc_scanner/tinc_metaconnection.rb', line 30

def send_id(nodename, protocol_version = '17')
  @socket.puts "0 #{nodename} #{protocol_version}"
end