Added working NLSocket.open and scope handling.
This commit is contained in:
5
README
5
README
@@ -96,5 +96,8 @@ TODO
|
|||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
=========
|
=========
|
||||||
|
(C) 2011 Bytemark Hosting
|
||||||
|
Written by Brian Candler <B.Candler@pobox.com>
|
||||||
|
|
||||||
Copyright (C) 2011 Bytemark Computer Consulting Ltd
|
Distribute under the same terms as Ruby
|
||||||
|
http://www.ruby-lang.org/en/LICENSE.txt
|
||||||
|
@@ -29,6 +29,17 @@ module Netlink
|
|||||||
raise "Bad AF #{af}!" if af != Socket::AF_NETLINK
|
raise "Bad AF #{af}!" if af != Socket::AF_NETLINK
|
||||||
raise "Bad PID #{pid}!" if pid != 0
|
raise "Bad PID #{pid}!" if pid != 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Create a new Netlink socket, and pass it to the given block. Ensures
|
||||||
|
# the the socket is closed when we're finished.
|
||||||
|
def self.open(opt={})
|
||||||
|
sock = self.new(opt)
|
||||||
|
begin
|
||||||
|
yield(sock)
|
||||||
|
ensure
|
||||||
|
sock.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
attr_accessor :socket # the underlying Socket
|
attr_accessor :socket # the underlying Socket
|
||||||
attr_accessor :seq # the last sequence number used
|
attr_accessor :seq # the last sequence number used
|
||||||
@@ -64,6 +75,11 @@ module Netlink
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Close the Netlink socket
|
||||||
|
def close
|
||||||
|
@socket.close
|
||||||
|
end
|
||||||
|
|
||||||
# Generate the next sequence number
|
# Generate the next sequence number
|
||||||
def next_seq
|
def next_seq
|
||||||
|
@@ -111,10 +111,20 @@ module Netlink
|
|||||||
ipaddr_modify(RTM_DELADDR, 0, opt)
|
ipaddr_modify(RTM_DELADDR, 0, opt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SCOPES = {
|
||||||
|
:global => 0,
|
||||||
|
:nowhere => 1,
|
||||||
|
:host => 254,
|
||||||
|
:link => 253,
|
||||||
|
:site => 200
|
||||||
|
}
|
||||||
|
|
||||||
def ipaddr_modify(code, flags, msg) #:nodoc:
|
def ipaddr_modify(code, flags, msg) #:nodoc:
|
||||||
msg = IFAddr.new(msg)
|
msg = IFAddr.new(msg)
|
||||||
msg.index = index(msg.index) unless msg.index.is_a?(Integer)
|
msg.index = index(msg.index) unless msg.index.is_a?(Integer)
|
||||||
msg.address ||= msg.local
|
msg.address ||= msg.local
|
||||||
|
msg.scope = SCOPES[msg.scope.to_s.downcase.to_sym] if
|
||||||
|
msg.scope && !msg.scope.is_a?(Integer)
|
||||||
# Note: IPAddr doesn't support addresses off the subnet base,
|
# Note: IPAddr doesn't support addresses off the subnet base,
|
||||||
# so there's no point trying to set msg.prefixlen from the IPAddr mask
|
# so there's no point trying to set msg.prefixlen from the IPAddr mask
|
||||||
@rtsocket.cmd code, msg, flags|NLM_F_REQUEST
|
@rtsocket.cmd code, msg, flags|NLM_F_REQUEST
|
||||||
|
Reference in New Issue
Block a user