Class: OSPFv2::SendSocket

Inherits:
Object show all
Defined in:
lib/infra/ospf_socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, options = {}) ⇒ SendSocket

Returns a new instance of SendSocket.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/infra/ospf_socket.rb', line 34

def initialize(src, options={})
  @src = src
  @sock = Socket.open(Socket::PF_INET, Socket::SOCK_RAW, IPPROTO_OSPF)
  add_membership OSPFv2::AllSPFRouters
  add_membership OSPFv2::AllDRouters
rescue Errno::EPERM
  $stderr.puts "#{e}: You are not root, cannot run: #{$0}!"
  exit(1)
rescue Errno::EADDRNOTAVAIL
  STDERR.puts "TODO: check if 127/8 and exit if not."
  # for testing with 127/8
rescue Exception => e
  STDERR.puts "#{e} Cannot Open Socket!"
  exit(1)
end

Instance Attribute Details

#sockObject (readonly)

Returns the value of attribute sock.



32
33
34
# File 'lib/infra/ospf_socket.rb', line 32

def sock
  @sock
end

Instance Method Details

#closeObject



79
80
81
# File 'lib/infra/ospf_socket.rb', line 79

def close
  @sock.close unless @sock.closed?
end

#send(packet, dest) ⇒ Object

TODO: use all_spf_routers, all_dr_routers, …

8.1      Sending protocol packets .............................. 58


53
54
55
56
57
58
59
60
# File 'lib/infra/ospf_socket.rb', line 53

def send(packet, location={:to=> :all_spf_routers})
  case location
  when :all_spf_routers ; send_all_spf_routers(packet)
  when :all_dr_routers  ; send_all_dr_routers(packet)
  else
    send_to(packet, location[:to])
  end
end

#send_all_dr_routersObject



66
67
68
# File 'lib/infra/ospf_socket.rb', line 66

def send_all_dr_routers
  _send_ (packet.respond_to?(:encode) ? packet.encode : packet), 0, @send_all_dr_routers
end

#send_all_spf_routersObject



62
63
64
# File 'lib/infra/ospf_socket.rb', line 62

def send_all_spf_routers
  _send_ (packet.respond_to?(:encode) ? packet.encode : packet), 0, @sock_addr_all_spf_routers
end

#send_to(packet, dest) ⇒ Object



70
71
72
# File 'lib/infra/ospf_socket.rb', line 70

def send_to(packet, dest)
  _send_ (packet.respond_to?(:encode) ? packet.encode : packet), 0, Socket.pack_sockaddr_in(0, dest)
end