Class: OSPFv2::RecvSocket

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RecvSocket.



90
91
92
93
94
95
# File 'lib/infra/ospf_socket.rb', line 90

def initialize(src, options={})
  @src=src
  @sock = Socket.open(Socket::PF_INET, Socket::SOCK_RAW,89)
  add_membership OSPFv2::AllSPFRouters, src
  add_membership OSPFv2::AllDRouters, src
end

Instance Attribute Details

#sockObject (readonly)

Returns the value of attribute sock.



89
90
91
# File 'lib/infra/ospf_socket.rb', line 89

def sock
  @sock
end

Instance Method Details

#add_membership(group, src) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/infra/ospf_socket.rb', line 109

def add_membership(group,src)
  optval = IPAddr.new(group).hton + IPAddr.new(src).hton
  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)
rescue Errno::EADDRNOTAVAIL => e
  p e
  raise
end

#closeObject



105
106
107
108
# File 'lib/infra/ospf_socket.rb', line 105

def close
  begin ; @sock.close ; rescue ; end
  @sock=nil
end

#recv(size = 8192) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/infra/ospf_socket.rb', line 96

def recv(size=8192)
  begin
    data, sender = @sock.recvfrom(size)
    port, host = Socket.unpack_sockaddr_in(sender)
    [host,port,data]
  rescue => e
    STDERR.puts "RSocket recv() error: #{e}"
  end
end