Move NETLINK_ROUTE user objects under Netlink::Route
This commit is contained in:
@@ -8,7 +8,7 @@ require 'netlink/route'
|
|||||||
# The data is memoized - that is, it's downloaded from the kernel once
|
# The data is memoized - that is, it's downloaded from the kernel once
|
||||||
# and then manipulated internally.
|
# and then manipulated internally.
|
||||||
|
|
||||||
nl = Netlink::RTSocket.new
|
nl = Netlink::Route::Socket.new
|
||||||
pp nl.link["eth0"]
|
pp nl.link["eth0"]
|
||||||
pp nl.addrs["eth0"]
|
pp nl.addrs["eth0"]
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ require 'netlink/route'
|
|||||||
# Example of use of low-level API for NETLINK_ROUTE socket.
|
# Example of use of low-level API for NETLINK_ROUTE socket.
|
||||||
# Each of these method calls performs a netlink protocol exchange.
|
# Each of these method calls performs a netlink protocol exchange.
|
||||||
|
|
||||||
nl = Netlink::RTSocket.new
|
nl = Netlink::Route::Socket.new
|
||||||
puts "*** links ***"
|
puts "*** links ***"
|
||||||
pp nl.read_links
|
pp nl.read_links
|
||||||
puts "*** addrs ***"
|
puts "*** addrs ***"
|
||||||
|
@@ -136,152 +136,154 @@ module Netlink
|
|||||||
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
|
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
|
||||||
end
|
end
|
||||||
|
|
||||||
# This is the medium and high-level API using a NETLINK_ROUTE protocol socket
|
module Route
|
||||||
class RTSocket < NLSocket
|
# This is the medium and high-level API using a NETLINK_ROUTE protocol socket
|
||||||
def initialize(opt={})
|
class Socket < NLSocket
|
||||||
super(opt.merge(:protocol => Netlink::NETLINK_ROUTE))
|
def initialize(opt={})
|
||||||
clear_cache
|
super(opt.merge(:protocol => Netlink::NETLINK_ROUTE))
|
||||||
end
|
clear_cache
|
||||||
|
end
|
||||||
|
|
||||||
# Download a list of links (interfaces). Either returns an array of
|
# Download a list of links (interfaces). Either returns an array of
|
||||||
# Netlink::IFInfo objects, or yields them to the supplied block.
|
# Netlink::IFInfo objects, or yields them to the supplied block.
|
||||||
#
|
#
|
||||||
# res = nl.read_links
|
# res = nl.read_links
|
||||||
# p res
|
# p res
|
||||||
# [#<Netlink::IFInfo {:family=>0, :pad=>0, :type=>772, :index=>1,
|
# [#<Netlink::IFInfo {:family=>0, :pad=>0, :type=>772, :index=>1,
|
||||||
# :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0,
|
# :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0,
|
||||||
# :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...",
|
# :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...",
|
||||||
# :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00",
|
# :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00",
|
||||||
# :stats32=>#<struct Netlink::LinkStats rx_packets=22, ...>,
|
# :stats32=>#<struct Netlink::LinkStats rx_packets=22, ...>,
|
||||||
# :stats64=>#<struct Netlink::LinkStats rx_packets=22, ...>}>, ...]
|
# :stats64=>#<struct Netlink::LinkStats rx_packets=22, ...>}>, ...]
|
||||||
def read_links(opt=nil, &blk)
|
def read_links(opt=nil, &blk)
|
||||||
send_request RTM_GETLINK, IFInfo.new(opt),
|
send_request RTM_GETLINK, IFInfo.new(opt),
|
||||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||||
receive_until_done(RTM_NEWLINK, &blk)
|
receive_until_done(RTM_NEWLINK, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Download a list of routes. Either returns an array of
|
# Download a list of routes. Either returns an array of
|
||||||
# Netlink::RT objects, or yields them to the supplied block.
|
# Netlink::RT objects, or yields them to the supplied block.
|
||||||
#
|
#
|
||||||
# A hash of kernel options may be supplied, but you might also have
|
# A hash of kernel options may be supplied, but you might also have
|
||||||
# to perform your own filtering. e.g.
|
# to perform your own filtering. e.g.
|
||||||
# rt.read_routes(:family=>Socket::AF_INET) # works
|
# rt.read_routes(:family=>Socket::AF_INET) # works
|
||||||
# rt.read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored
|
# rt.read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored
|
||||||
#
|
#
|
||||||
# res = nl.read_routes(:family => Socket::AF_INET)
|
# res = nl.read_routes(:family => Socket::AF_INET)
|
||||||
# p res
|
# p res
|
||||||
# [#<Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0,
|
# [#<Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0,
|
||||||
# :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255,
|
# :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255,
|
||||||
# :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>,
|
# :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>,
|
||||||
# :prefsrc=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :oif=>1}>, ...]
|
# :prefsrc=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :oif=>1}>, ...]
|
||||||
#
|
#
|
||||||
# Note that not all attributes will always be present. In particular,
|
# Note that not all attributes will always be present. In particular,
|
||||||
# a defaultroute (dst_len=0) misses out the dst address completely:
|
# a defaultroute (dst_len=0) misses out the dst address completely:
|
||||||
#
|
#
|
||||||
# [#<Netlink::RT {:family=>2, :dst_len=>0, :src_len=>0, :tos=>0,
|
# [#<Netlink::RT {:family=>2, :dst_len=>0, :src_len=>0, :tos=>0,
|
||||||
# :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254,
|
# :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254,
|
||||||
# :gateway=>#<IPAddr: IPv4:10.69.255.253/255.255.255.255>, :oif=>2}>, ...]
|
# :gateway=>#<IPAddr: IPv4:10.69.255.253/255.255.255.255>, :oif=>2}>, ...]
|
||||||
def read_routes(opt=nil, &blk)
|
def read_routes(opt=nil, &blk)
|
||||||
send_request RTM_GETROUTE, RT.new(opt),
|
send_request RTM_GETROUTE, RT.new(opt),
|
||||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||||
receive_until_done(RTM_NEWROUTE, &blk)
|
receive_until_done(RTM_NEWROUTE, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Download a list of link addresses. Either returns an array of
|
# Download a list of link addresses. Either returns an array of
|
||||||
# Netlink::IFAddr objects, or yields them to the supplied block.
|
# Netlink::IFAddr objects, or yields them to the supplied block.
|
||||||
# You will need to use the 'index' to cross reference to the interface.
|
# You will need to use the 'index' to cross reference to the interface.
|
||||||
#
|
#
|
||||||
# A hash of kernel options may be supplied, but likely only :family
|
# A hash of kernel options may be supplied, but likely only :family
|
||||||
# is honoured.
|
# is honoured.
|
||||||
#
|
#
|
||||||
# res = nl.read_addrs(:family => Socket::AF_INET)
|
# res = nl.read_addrs(:family => Socket::AF_INET)
|
||||||
# p res
|
# p res
|
||||||
# [#<Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254,
|
# [#<Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254,
|
||||||
# :index=>1, :address=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>,
|
# :index=>1, :address=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>,
|
||||||
# :local=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :label=>"lo"}>, ...]
|
# :local=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :label=>"lo"}>, ...]
|
||||||
def read_addrs(opt=nil, &blk)
|
def read_addrs(opt=nil, &blk)
|
||||||
send_request RTM_GETADDR, IFAddr.new(opt),
|
send_request RTM_GETADDR, IFAddr.new(opt),
|
||||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||||
receive_until_done(RTM_NEWADDR, &blk)
|
receive_until_done(RTM_NEWADDR, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Download a list of addresses, grouped as {index=>[addr,addr], index=>[addr,addr]}
|
# Download a list of addresses, grouped as {index=>[addr,addr], index=>[addr,addr]}
|
||||||
def read_addrs_by_ifindex(opt=nil)
|
def read_addrs_by_ifindex(opt=nil)
|
||||||
res = read_addrs(opt).group_by { |obj| obj.index }
|
res = read_addrs(opt).group_by { |obj| obj.index }
|
||||||
res.default = [].freeze
|
res.default = [].freeze
|
||||||
res
|
res
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clear the memoization cache
|
# Clear the memoization cache
|
||||||
def clear_cache
|
def clear_cache
|
||||||
@links = nil
|
@links = nil
|
||||||
@link = nil
|
@link = nil
|
||||||
@addrs = nil
|
@addrs = nil
|
||||||
@routes = nil
|
@routes = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the memoized interface table as a flat array, suitable for
|
# Return the memoized interface table as a flat array, suitable for
|
||||||
# iteration. e.g.
|
# iteration. e.g.
|
||||||
# rt.links.each { |link| puts link.ifname }
|
# rt.links.each { |link| puts link.ifname }
|
||||||
def links
|
def links
|
||||||
@links ||= read_links
|
@links ||= read_links
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the memoized interface table, keyed by both ifname and ifindex. e.g.
|
# Return the memoized interface table, keyed by both ifname and ifindex. e.g.
|
||||||
# puts rt.link["eth0"].index
|
# puts rt.link["eth0"].index
|
||||||
# puts rt.link[1].ifname
|
# puts rt.link[1].ifname
|
||||||
def link
|
def link
|
||||||
@link ||= (
|
@link ||= (
|
||||||
h = {}
|
h = {}
|
||||||
links.each { |link| h[link.index] = h[link.ifname] = link }
|
links.each { |link| h[link.index] = h[link.ifname] = link }
|
||||||
h
|
h
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the memoized address table, keyed by interface name and
|
# Return the memoized address table, keyed by interface name and
|
||||||
# address family, containing an array of addresses for each
|
# address family, containing an array of addresses for each
|
||||||
# interface/family combination. i.e.
|
# interface/family combination. i.e.
|
||||||
#
|
#
|
||||||
# # {ifname=>{family=>[addr,addr,...], ...}, ...}
|
# # {ifname=>{family=>[addr,addr,...], ...}, ...}
|
||||||
# puts rt.addrs["eth0"][Socket::AF_INET][0].address
|
# puts rt.addrs["eth0"][Socket::AF_INET][0].address
|
||||||
#
|
#
|
||||||
# If there are no addresses for a particular family then it will
|
# If there are no addresses for a particular family then it will
|
||||||
# return a (frozen) empty array, to make iteration eaiser.
|
# return a (frozen) empty array, to make iteration eaiser.
|
||||||
def addrs
|
def addrs
|
||||||
@addrs ||= (
|
@addrs ||= (
|
||||||
h = {}
|
h = {}
|
||||||
links.each do |link|
|
links.each do |link|
|
||||||
h[link.ifname] = {}
|
h[link.ifname] = {}
|
||||||
end
|
end
|
||||||
read_addrs.each do |addr|
|
read_addrs.each do |addr|
|
||||||
ifname = link[addr.index].ifname
|
ifname = link[addr.index].ifname
|
||||||
h[ifname] ||= Hash.new(EMPTY_ARRAY)
|
h[ifname] ||= Hash.new(EMPTY_ARRAY)
|
||||||
(h[ifname][addr.family] ||= []) << addr
|
(h[ifname][addr.family] ||= []) << addr
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the memoized route table, keyed by address family, containing
|
# Return the memoized route table, keyed by address family, containing
|
||||||
# an array of routes for each address family. i.e.
|
# an array of routes for each address family. i.e.
|
||||||
# family combination. i.e.
|
# family combination. i.e.
|
||||||
#
|
#
|
||||||
# # {family=>[route,route,...], ...}, ...}
|
# # {family=>[route,route,...], ...}, ...}
|
||||||
# puts rt.routes[Socket::AF_INET].first.dst
|
# puts rt.routes[Socket::AF_INET].first.dst
|
||||||
#
|
#
|
||||||
# If there are no routes for a particular family then it will
|
# If there are no routes for a particular family then it will
|
||||||
# return a (frozen) empty array.
|
# return a (frozen) empty array.
|
||||||
def routes
|
def routes
|
||||||
@routes ||= (
|
@routes ||= (
|
||||||
h = {}
|
h = {}
|
||||||
links.each do |link|
|
links.each do |link|
|
||||||
h[link.ifname] = Hash.new(EMPTY_ARRAY)
|
h[link.ifname] = Hash.new(EMPTY_ARRAY)
|
||||||
end
|
end
|
||||||
read_routes.each do |route|
|
read_routes.each do |route|
|
||||||
(h[route.family] ||= []) << route
|
(h[route.family] ||= []) << route
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
)
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user