Factor out list/filter code
This commit is contained in:
@@ -16,7 +16,7 @@ pp rt.links["eth0"]
|
|||||||
puts "\nAddresses on interface eth0:"
|
puts "\nAddresses on interface eth0:"
|
||||||
pp rt.addrs.list(:index=>"eth0").to_a
|
pp rt.addrs.list(:index=>"eth0").to_a
|
||||||
|
|
||||||
puts "\nAll routes in main routing table:"
|
puts "\nAll v4 routes in main routing table:"
|
||||||
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).to_a
|
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).to_a
|
||||||
|
|
||||||
puts "\nDefault route is probably:"
|
puts "\nDefault route is probably:"
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
require 'netlink/route'
|
require 'netlink/route'
|
||||||
|
require 'netlink/route/handler'
|
||||||
|
|
||||||
module Netlink
|
module Netlink
|
||||||
# struct ifa_cacheinfo
|
# struct ifa_cacheinfo
|
||||||
@@ -25,28 +26,12 @@ module Netlink
|
|||||||
end
|
end
|
||||||
|
|
||||||
module Route
|
module Route
|
||||||
# This class provides an API for manipulating interfaces and addresses.
|
# This class provides an API for manipulating iaddresses.
|
||||||
# Since we frequently need to map ifname to ifindex, or vice versa,
|
class AddrHandler < Handler
|
||||||
# we keep a memoized list of interfaces. If the interface list changes,
|
|
||||||
# you should create a new instance of this object.
|
|
||||||
class AddrHandler
|
|
||||||
def initialize(rtsocket = Netlink::Route::Socket.new)
|
|
||||||
@rtsocket = rtsocket
|
|
||||||
clear_cache
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear_cache
|
def clear_cache
|
||||||
@addrs = nil
|
@addrs = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def index(v)
|
|
||||||
@rtsocket.index(v)
|
|
||||||
end
|
|
||||||
|
|
||||||
def ifname(v)
|
|
||||||
@rtsocket.ifname(v)
|
|
||||||
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.
|
||||||
@@ -65,6 +50,14 @@ module Netlink
|
|||||||
@rtsocket.receive_until_done(RTM_NEWADDR, &blk)
|
@rtsocket.receive_until_done(RTM_NEWADDR, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Filter < BaseFilter #:nodoc:
|
||||||
|
filter(:family) { |o,v| o.family == v }
|
||||||
|
filter(:scope) { |o,v| o.scope == scope }
|
||||||
|
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||||
|
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||||
|
filter(:index) { |o,v| o.index == v }
|
||||||
|
end
|
||||||
|
|
||||||
# Iterate over all addresses, or addressees matching the given
|
# Iterate over all addresses, or addressees matching the given
|
||||||
# criteria. Returns an Enumerator if no block given.
|
# criteria. Returns an Enumerator if no block given.
|
||||||
#
|
#
|
||||||
@@ -74,20 +67,10 @@ module Netlink
|
|||||||
# nl.addrs.list { |x| p x }
|
# nl.addrs.list { |x| p x }
|
||||||
# addrs_eth0 = nl.addrs.list(:index=>"eth0").to_a
|
# addrs_eth0 = nl.addrs.list(:index=>"eth0").to_a
|
||||||
# addrs_eth0_v4 = nl.addrs.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
|
# addrs_eth0_v4 = nl.addrs.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
|
||||||
#
|
|
||||||
# TODO: error on unknown filter conditions
|
|
||||||
def list(filter=nil, &blk)
|
def list(filter=nil, &blk)
|
||||||
@addrs ||= read_addrs
|
@addrs ||= read_addrs
|
||||||
return @addrs.each(&blk) unless filter
|
filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index)
|
||||||
return to_enum(:list, filter) unless block_given?
|
do_list(@addrs, filter, &blk)
|
||||||
filter[:index] = index(filter[:index]) if filter.has_key?(:index)
|
|
||||||
@addrs.each do |o|
|
|
||||||
yield o if (!filter[:family] || o.family == filter[:family]) &&
|
|
||||||
(!filter[:scope] || o.kind?(filter[:scope])) &&
|
|
||||||
(!filter[:flags] || (o.flags & filter[:flags]) == filter[:flags]) &&
|
|
||||||
(!filter[:noflags] || (o.flags & filter[:noflags]) == 0) &&
|
|
||||||
(!filter[:index] || o.index == filter[:index])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias :each :list
|
alias :each :list
|
||||||
|
|
||||||
|
46
lib/netlink/route/handler.rb
Normal file
46
lib/netlink/route/handler.rb
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
module Netlink
|
||||||
|
module Route
|
||||||
|
# This class allows objects to be created representing the
|
||||||
|
# conditions given to the 'list' method
|
||||||
|
class BaseFilter #:nodoc:
|
||||||
|
def self.filter name, &blk
|
||||||
|
define_method "#{name}=" do |matchval|
|
||||||
|
@conds << [blk, matchval]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(h)
|
||||||
|
@conds = []
|
||||||
|
h.each { |k,v| send "#{k}=", v }
|
||||||
|
end
|
||||||
|
|
||||||
|
def match(obj)
|
||||||
|
!@conds.find { |blk,matchval| !blk[obj,matchval] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Code which is common to all the NETLINK_ROUTE handlers
|
||||||
|
class Handler
|
||||||
|
def initialize(rtsocket = Netlink::Route::Socket.new)
|
||||||
|
@rtsocket = rtsocket
|
||||||
|
clear_cache
|
||||||
|
end
|
||||||
|
|
||||||
|
def index(v)
|
||||||
|
@rtsocket.index(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
def ifname(v)
|
||||||
|
@rtsocket.ifname(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generic listing and filtering
|
||||||
|
def do_list(data, filter, &blk)
|
||||||
|
return data.each(&blk) unless filter
|
||||||
|
return to_enum(:list, filter) unless block_given?
|
||||||
|
fm = self.class::Filter.new(filter)
|
||||||
|
data.each { |o| yield o if fm.match(o) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@@ -1,4 +1,5 @@
|
|||||||
require 'netlink/route'
|
require 'netlink/route'
|
||||||
|
require 'netlink/route/handler'
|
||||||
|
|
||||||
module Netlink
|
module Netlink
|
||||||
# struct rtnl_link_stats / rtnl_link_stats64
|
# struct rtnl_link_stats / rtnl_link_stats64
|
||||||
@@ -119,21 +120,12 @@ module Netlink
|
|||||||
# Since we frequently need to map ifname to ifindex, or vice versa,
|
# Since we frequently need to map ifname to ifindex, or vice versa,
|
||||||
# we keep a memoized list of interfaces. If the interface list changes,
|
# we keep a memoized list of interfaces. If the interface list changes,
|
||||||
# you should create a new instance of this object.
|
# you should create a new instance of this object.
|
||||||
class LinkHandler
|
class LinkHandler < Handler
|
||||||
def initialize(rtsocket = Netlink::Route::Socket.new)
|
|
||||||
@rtsocket = rtsocket
|
|
||||||
clear_cache
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear_cache
|
def clear_cache
|
||||||
@links = nil
|
@links = nil
|
||||||
@linkmap = nil
|
@linkmap = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def index(v)
|
|
||||||
@rtsocket.index(v)
|
|
||||||
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.
|
||||||
#
|
#
|
||||||
@@ -150,7 +142,15 @@ module Netlink
|
|||||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||||
@rtsocket.receive_until_done(RTM_NEWLINK, &blk)
|
@rtsocket.receive_until_done(RTM_NEWLINK, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Filter < BaseFilter #:nodoc:
|
||||||
|
filter(:type) { |o,v| o.type == v }
|
||||||
|
filter(:kind) { |o,v| o.kind?(v) }
|
||||||
|
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||||
|
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||||
|
filter(:link) { |o,v| o.link == v }
|
||||||
|
end
|
||||||
|
|
||||||
# Iterate over all interfaces, or interfaces matching the given
|
# Iterate over all interfaces, or interfaces matching the given
|
||||||
# criteria. Returns an Enumerator if no block given.
|
# criteria. Returns an Enumerator if no block given.
|
||||||
#
|
#
|
||||||
@@ -165,16 +165,8 @@ module Netlink
|
|||||||
# rt.links.list(:link => "lo") # vlan etc attached to this interface
|
# rt.links.list(:link => "lo") # vlan etc attached to this interface
|
||||||
def list(filter=nil, &blk)
|
def list(filter=nil, &blk)
|
||||||
@links ||= read_links
|
@links ||= read_links
|
||||||
return @links.each(&blk) unless filter
|
filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link)
|
||||||
return to_enum(:list, filter) unless block_given?
|
do_list(@links, filter, &blk)
|
||||||
filter[:link] = index(filter[:link]) if filter.has_key?(:link)
|
|
||||||
@links.each do |o|
|
|
||||||
yield o if (!filter[:type] || o.type == filter[:type]) &&
|
|
||||||
(!filter[:kind] || o.kind?(filter[:kind])) &&
|
|
||||||
(!filter[:flags] || (o.flags & filter[:flags]) == filter[:flags]) &&
|
|
||||||
(!filter[:noflags] || (o.flags & filter[:noflags]) == 0) &&
|
|
||||||
(!filter[:link] || o.link == filter[:link])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias :each :list
|
alias :each :list
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
require 'netlink/route'
|
require 'netlink/route'
|
||||||
|
require 'netlink/route/handler'
|
||||||
|
|
||||||
module Netlink
|
module Netlink
|
||||||
# struct rta_cacheinfo
|
# struct rta_cacheinfo
|
||||||
@@ -47,20 +48,11 @@ module Netlink
|
|||||||
|
|
||||||
module Route
|
module Route
|
||||||
# This class manipulates the kernel routing table
|
# This class manipulates the kernel routing table
|
||||||
class RouteHandler
|
class RouteHandler < Handler
|
||||||
def initialize(rtsocket = Netlink::Route::Socket.new)
|
|
||||||
@rtsocket = rtsocket
|
|
||||||
clear_cache
|
|
||||||
end
|
|
||||||
|
|
||||||
def clear_cache
|
def clear_cache
|
||||||
@routes = nil
|
@routes = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def index(v)
|
|
||||||
@rtsocket.index(v)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Send message to download the kernel routing table. Either returns an
|
# Send message to download the kernel routing table. Either returns an
|
||||||
# array of Netlink::RT objects, or yields them to the supplied block.
|
# array of Netlink::RT objects, or yields them to the supplied block.
|
||||||
#
|
#
|
||||||
@@ -88,6 +80,18 @@ module Netlink
|
|||||||
@rtsocket.receive_until_done(RTM_NEWROUTE, &blk)
|
@rtsocket.receive_until_done(RTM_NEWROUTE, &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Filter < BaseFilter #:nodoc:
|
||||||
|
filter(:family) { |o,v| o.family == v }
|
||||||
|
filter(:table) { |o,v| o.table == v }
|
||||||
|
filter(:protocol) { |o,v| o.protocol == v }
|
||||||
|
filter(:type) { |o,v| o.type == v }
|
||||||
|
filter(:scope) { |o,v| o.scope == v }
|
||||||
|
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||||
|
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||||
|
filter(:oif) { |o,v| o.oif == v }
|
||||||
|
filter(:iif) { |o,v| o.iif == v }
|
||||||
|
end
|
||||||
|
|
||||||
# Return the memoized route table, filtered according to
|
# Return the memoized route table, filtered according to
|
||||||
# the optional criteria. Examples:
|
# the optional criteria. Examples:
|
||||||
# :family => Socket::AF_INET
|
# :family => Socket::AF_INET
|
||||||
@@ -100,22 +104,10 @@ module Netlink
|
|||||||
# :oif => "eth0"
|
# :oif => "eth0"
|
||||||
# :iif => "eth1"
|
# :iif => "eth1"
|
||||||
def list(filter=nil, &blk)
|
def list(filter=nil, &blk)
|
||||||
@routes = read_routes
|
@routes ||= read_routes
|
||||||
return @routes.each(&blk) unless filter
|
filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif)
|
||||||
return to_enum(:list, filter) unless block_given?
|
filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif)
|
||||||
filter[:oif] = index(filter[:oif]) if filter.has_key?(:oif)
|
do_list(@routes, filter, &blk)
|
||||||
filter[:iif] = index(filter[:iif]) if filter.has_key?(:iif)
|
|
||||||
@routes.each do |o|
|
|
||||||
yield o if (!filter[:family] || o.family == filter[:family]) &&
|
|
||||||
(!filter[:table] || o.table == filter[:table]) &&
|
|
||||||
(!filter[:protocol] || o.protocol == filter[:protocol]) &&
|
|
||||||
(!filter[:type] || o.scope == filter[:protocol]) &&
|
|
||||||
(!filter[:scope] || o.type == filter[:type]) &&
|
|
||||||
(!filter[:flags] || (o.flags & filter[:flags]) == filter[:flags]) &&
|
|
||||||
(!filter[:noflags] || (o.flags & filter[:noflags]) == 0) &&
|
|
||||||
(!filter[:oif] || o.oif == filter[:oif]) &&
|
|
||||||
(!filter[:iif] || o.iif == filter[:iif])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias :each :list
|
alias :each :list
|
||||||
|
|
||||||
|
@@ -1,14 +1,11 @@
|
|||||||
require 'netlink/route'
|
require 'netlink/route'
|
||||||
|
require 'netlink/route/handler'
|
||||||
|
|
||||||
module Netlink
|
module Netlink
|
||||||
module Route
|
module Route
|
||||||
class VlanHandler
|
class VlanHandler < Handler
|
||||||
def initialize(rtsocket = Netlink::Route::Socket.new)
|
def clear_cache
|
||||||
@rtsocket = rtsocket
|
# No cache
|
||||||
end
|
|
||||||
|
|
||||||
def index(v)
|
|
||||||
@rtsocket.index(v)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def list(filter={}, &blk)
|
def list(filter={}, &blk)
|
||||||
|
Reference in New Issue
Block a user