Class: Orion6Rep::DetectReps

Inherits:
Object
  • Object
show all
Defined in:
lib/orion6_rep/detect_reps.rb

Constant Summary collapse

UDP_DETECTION_PORT =
65535
UDP_DETECTION_DATA =
[0x58].pack("C")

Instance Method Summary collapse

Constructor Details

#initializeDetectReps

Returns a new instance of DetectReps.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/orion6_rep/detect_reps.rb', line 28

def initialize
  ifaces_names = Orion6Rep::Interface.get_active_interfaces_names
  # it's useless to try to detect REPs in the localhost
  ifaces_names.delete("lo")

  @interfaces = {}
  @responses = {}
  ifaces_names.each do |name|
    broadcast_address = Orion6Rep::Interface.broadcast_address(name)
    @interfaces[name] = {}
    @interfaces[name][:broadcast_addr] = broadcast_address
  end
  raise "No active network interfaces found" if @interfaces.empty?

  @interfaces.each_key do |name|
    socket = UDPSocket.new
    socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
    @interfaces[name][:socket] = socket
  end
end

Instance Method Details

#executeObject



49
50
51
52
53
# File 'lib/orion6_rep/detect_reps.rb', line 49

def execute
  @interfaces.each do |_, data|
    data[:socket].send(UDP_DETECTION_DATA, 0, data[:broadcast_addr], UDP_DETECTION_PORT)
  end
end

#poolObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/orion6_rep/detect_reps.rb', line 55

def pool
  rep_responses = IO.select(get_sockets, nil, nil, 1)

  unless rep_responses.nil? or rep_responses.empty?
    rep_responses.first.each do |socket|
      raw_data, socket_data = socket.recvfrom(33)
      rep_data, tcp_port = raw_data.unpack("a18a4")
      ip = socket_data.last
      interface = get_assossiated_interface(socket)
      @responses[interface] = {} if @responses[interface].nil?
      @responses[interface][ip] = {:port => tcp_port.to_i, :rep_data => rep_data}
    end
  end

  return @responses
end

#stopObject



72
73
74
75
# File 'lib/orion6_rep/detect_reps.rb', line 72

def stop
  get_sockets.each{|socket| socket.close}
  true
end