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
# 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)
  @sock.setsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_IF,  IPAddr.new(src).hton)
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



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

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

#send(packet, dest) ⇒ Object



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

def send(packet, dest)
  addr = Socket.pack_sockaddr_in(0, dest)
  @sock.send((packet.respond_to?(:encode) ? packet.encode : packet),0,addr)
end

#send_all_dr_routersObject



57
58
59
# File 'lib/infra/ospf_socket.rb', line 57

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

#send_all_spf_routersObject



53
54
55
# File 'lib/infra/ospf_socket.rb', line 53

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



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

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