Rename plural to singular; cleaner and more consistent with 'ip' utility

This commit is contained in:
Brian Candler
2011-05-03 19:13:37 +01:00
parent 834f7a83d6
commit 9bfa6e5b2c
10 changed files with 76 additions and 76 deletions

View File

@@ -3,19 +3,19 @@ $LOAD_PATH.unshift LIBDIR
require 'netlink/route' require 'netlink/route'
nl = Netlink::Route::Socket.new ip = Netlink::Route::Socket.new
puts "\n*** Before adding address" puts "\n*** Before adding address"
nl.addrs.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address } ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }
puts "\n*** After adding address" puts "\n*** After adding address"
begin begin
nl.addrs.add(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32) ip.addr.add(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
rescue Errno::EEXIST rescue Errno::EEXIST
puts "Already exists" puts "Already exists"
end end
nl.addrs.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address } ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }
puts "\n*** After deleting address" puts "\n*** After deleting address"
nl.addrs.delete(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32) ip.addr.delete(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
nl.addrs.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address } ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }

View File

@@ -3,19 +3,19 @@ $LOAD_PATH.unshift LIBDIR
require 'netlink/route' require 'netlink/route'
nl = Netlink::Route::Socket.new ip = Netlink::Route::Socket.new
puts "\n*** Before adding route" puts "\n*** Before adding route"
nl.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x } ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x }
puts "\n*** After adding route" puts "\n*** After adding route"
begin begin
nl.routes.add(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1") ip.route.add(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1")
rescue Errno::EEXIST rescue Errno::EEXIST
puts "Already exists" puts "Already exists"
end end
nl.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x } ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x }
puts "\n*** After deleting route" puts "\n*** After deleting route"
nl.routes.delete(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1") ip.route.delete(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1")
nl.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x } ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x }

View File

@@ -4,18 +4,18 @@ $LOAD_PATH.unshift LIBDIR
require 'netlink/route' require 'netlink/route'
require 'pp' require 'pp'
nl = Netlink::Route::Socket.new ip = Netlink::Route::Socket.new
puts "\n*** Before adding VLAN" puts "\n*** Before adding VLAN"
pp nl.vlans.list(:link=>"lo").to_a pp ip.vlan.list(:link=>"lo").to_a
puts "\n*** After adding VLAN on lo" puts "\n*** After adding VLAN on lo"
begin begin
nl.vlans.add(:link=>"lo", :vlan_id=>1234) ip.vlan.add(:link=>"lo", :vlan_id=>1234)
rescue Errno::EEXIST rescue Errno::EEXIST
puts "Already present" puts "Already present"
end end
pp nl.vlans.list(:link=>"lo").to_a pp ip.vlan.list(:link=>"lo").to_a
puts "\n*** After deleting VLANs from lo" puts "\n*** After deleting VLANs from lo"
nl.vlans.delete(:link=>"lo", :vlan_id=>1234) ip.vlan.delete(:link=>"lo", :vlan_id=>1234)
pp nl.vlans.list(:link=>"lo").to_a pp ip.vlan.list(:link=>"lo").to_a

View File

@@ -8,20 +8,20 @@ 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.
rt = Netlink::Route::Socket.new ip = Netlink::Route::Socket.new
puts "\nInterface eth0:" puts "\nInterface eth0:"
pp rt.links["eth0"] pp ip.link["eth0"]
puts "\nAddresses on interface eth0:" puts "\nAddresses on interface eth0:"
pp rt.addrs.list(:index=>"eth0").to_a pp ip.addr.list(:index=>"eth0").to_a
puts "\nAll routes in main routing table:" puts "\nAll routes in main routing table:"
pp rt.routes.list(:table=>Netlink::RT_TABLE_MAIN).to_a pp ip.route.list(:table=>Netlink::RT_TABLE_MAIN).to_a
puts "\nV4 default route is probably:" puts "\nV4 default route is probably:"
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN). pp ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).
min_by { |route| route.dst_len } min_by { |route| route.dst_len }
puts "\nTraffic to 192.168.1.1 goes out via:" puts "\nTraffic to 192.168.1.1 goes out via:"
puts rt.ifname(rt.routes.get(:dst=>"192.168.1.1").oif) puts ip.ifname(ip.route.get(:dst=>"192.168.1.1").oif)

View File

@@ -7,10 +7,10 @@ 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.
rt = Netlink::Route::Socket.new ip = Netlink::Route::Socket.new
puts "*** links ***" puts "*** links ***"
pp rt.links.read_links pp ip.link.read_link
puts "*** addrs ***" puts "*** addrs ***"
pp rt.addrs.read_addrs pp ip.addr.read_addr
puts "*** routes ***" puts "*** routes ***"
pp rt.routes.read_routes pp ip.route.read_route

View File

@@ -20,23 +20,23 @@ module Netlink
end end
# Return a Netlink::Route::LinkHandler object for manipulating links # Return a Netlink::Route::LinkHandler object for manipulating links
def links def link
@links ||= Netlink::Route::LinkHandler.new(self) @link ||= Netlink::Route::LinkHandler.new(self)
end end
# Return a Netlink::Route::VlanHandler object for manipulating vlans # Return a Netlink::Route::VlanHandler object for manipulating vlans
def vlans def vlan
@vlans ||= Netlink::Route::VlanHandler.new(self) @vlan ||= Netlink::Route::VlanHandler.new(self)
end end
# Return a Netlink::Route::AddrHandler object for manipulating addresses # Return a Netlink::Route::AddrHandler object for manipulating addresses
def addrs def addr
@addrs ||= Netlink::Route::AddrHandler.new(self) @addr ||= Netlink::Route::AddrHandler.new(self)
end end
# Return a Netlink::Route::RT object for manipulating routes # Return a Netlink::Route::RT object for manipulating routes
def routes def route
@routes ||= Netlink::Route::RouteHandler.new(self) @route ||= Netlink::Route::RouteHandler.new(self)
end end
# Convert an interface index into name string, or nil if the # Convert an interface index into name string, or nil if the
@@ -49,7 +49,7 @@ module Netlink
# end # end
def ifname(index) def ifname(index)
return nil if index.nil? || index == 0 return nil if index.nil? || index == 0
links[index].ifname link[index].ifname
end end
# Convert an interface name into index. Returns 0 for nil or empty # Convert an interface name into index. Returns 0 for nil or empty
@@ -61,7 +61,7 @@ module Netlink
when nil, EMPTY_STRING when nil, EMPTY_STRING
0 0
else else
links[name].index link[name].index
end end
end end
end end

View File

@@ -29,7 +29,7 @@ module Netlink
# This class provides an API for manipulating iaddresses. # This class provides an API for manipulating iaddresses.
class AddrHandler < Handler class AddrHandler < Handler
def clear_cache def clear_cache
@addrs = nil @addr = nil
end end
# Download a list of link addresses. Either returns an array of # Download a list of link addresses. Either returns an array of
@@ -39,12 +39,12 @@ module Netlink
# 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_addr(: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_addr(opt=nil, &blk)
@rtsocket.send_request RTM_GETADDR, IFAddr.new(opt), @rtsocket.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
@rtsocket.receive_until_done(RTM_NEWADDR, &blk) @rtsocket.receive_until_done(RTM_NEWADDR, &blk)
@@ -64,22 +64,22 @@ module Netlink
# The full address list is read once and memoized, so # The full address list is read once and memoized, so
# it is efficient to call this method multiple times. # it is efficient to call this method multiple times.
# #
# nl.addrs.list { |x| p x } # nl.addr.list { |x| p x }
# addrs_eth0 = nl.addrs.list(:index=>"eth0").to_a # addrs_eth0 = nl.addr.list(:index=>"eth0").to_a
# addrs_eth0_v4 = nl.addrs.list(:index=>"eth0", :family=>Socket::AF_INET).to_a # addrs_eth0_v4 = nl.addr.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
def list(filter=nil, &blk) def list(filter=nil, &blk)
@addrs ||= read_addrs @addr ||= read_addr
filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index) filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index)
do_list(@addrs, filter, &blk) do_list(@addr, filter, &blk)
end end
alias :each :list alias :each :list
# Return addresses grouped by interface name. e.g. # Return addresses grouped by interface name. e.g.
# addrs_by_interface(:family => Socket::AF_INET).to_a # group_by_interface(:family => Socket::AF_INET).to_a
# #=> {"eth0"=>[addr, addr,...], "lo"=>[addr, addr,...] # #=> {"eth0"=>[addr, addr,...], "lo"=>[addr, addr,...]
# #
# The hash has an empty array as its default, so it's safe to do # The hash has an empty array as its default, so it's safe to do
# addrs_by_interface(...)["eth0"].each { |a| ... } # group_by_interface(...)["eth0"].each { |a| ... }
# even if eth0 has no addresses matching the given filter. # even if eth0 has no addresses matching the given filter.
def group_by_interface(*filter) def group_by_interface(*filter)
res = list(*filter).group_by { |a| ifname(a.index) } res = list(*filter).group_by { |a| ifname(a.index) }
@@ -90,8 +90,8 @@ module Netlink
# Add an IP address to an interface # Add an IP address to an interface
# #
# require 'netlink/route' # require 'netlink/route'
# rt = Netlink::Route::Socket.new # ip = Netlink::Route::Socket.new
# rt.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24) # ip.addr.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24)
def add(opt) def add(opt)
ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt) ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt)
end end

View File

@@ -122,14 +122,14 @@ module Netlink
# you should create a new instance of this object. # you should create a new instance of this object.
class LinkHandler < Handler class LinkHandler < Handler
def clear_cache def clear_cache
@links = nil @link = nil
@linkmap = nil @linkmap = nil
end 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 = rt.links.read_links # res = ip.link.read_link
# p res # p res
# [#<Netlink::IFInfo {:family=>0, :type=>772, :index=>1, # [#<Netlink::IFInfo {:family=>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,
@@ -137,7 +137,7 @@ module Netlink
# :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_link(opt=nil, &blk)
@rtsocket.send_request RTM_GETLINK, IFInfo.new(opt), @rtsocket.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
@rtsocket.receive_until_done(RTM_NEWLINK, &blk) @rtsocket.receive_until_done(RTM_NEWLINK, &blk)
@@ -157,16 +157,16 @@ module Netlink
# The full interface list is read once and memoized, so # The full interface list is read once and memoized, so
# it is efficient to call this method multiple times. # it is efficient to call this method multiple times.
# #
# rt.links.list { |x| p x } # ip.link.list { |x| p x }
# ethers = rt.links.list(:type => Netlink::ARPHRD_ETHER).to_a # ethers = ip.link.list(:type => Netlink::ARPHRD_ETHER).to_a
# vlans = rt.links.list(:kind => "vlan").to_a # vlans = ip.link.list(:kind => "vlan").to_a
# rt.links.list(:flags => Netlink::IFF_RUNNING) # ip.link.list(:flags => Netlink::IFF_RUNNING)
# rt.links.list(:noflags => Netlink::IFF_POINTOPOINT) # ip.link.list(:noflags => Netlink::IFF_POINTOPOINT)
# rt.links.list(:link => "lo") # vlan etc attached to this interface # ip.link.list(:link => "lo") # vlan etc attached to this interface
def list(filter=nil, &blk) def list(filter=nil, &blk)
@links ||= read_links @link ||= read_link
filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link) filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link)
do_list(@links, filter, &blk) do_list(@link, filter, &blk)
end end
alias :each :list alias :each :list
@@ -188,8 +188,8 @@ module Netlink
# Add an interface (raw). e.g. # Add an interface (raw). e.g.
# #
# require 'netlink/route' # require 'netlink/route'
# rt = Netlink::Route::Socket.new # ip = Netlink::Route::Socket.new
# rt.links.add_link( # ip.link.add(
# :link=>"lo", # :link=>"lo",
# :linkinfo=>Netlink::LinkInfo.new( # :linkinfo=>Netlink::LinkInfo.new(
# :kind=>"vlan", # :kind=>"vlan",

View File

@@ -56,7 +56,7 @@ module Netlink
# This class manipulates the kernel routing table # This class manipulates the kernel routing table
class RouteHandler < Handler class RouteHandler < Handler
def clear_cache def clear_cache
@routes = nil @route = nil
end end
# Send message to download the kernel routing table. Either returns an # Send message to download the kernel routing table. Either returns an
@@ -64,10 +64,10 @@ module Netlink
# #
# 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.
# read_routes(:family=>Socket::AF_INET) # works # read_route(:family=>Socket::AF_INET) # works
# read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored # read_route(:protocol=>Netlink::RTPROT_STATIC) # ignored
# #
# res = rt.routes.read_routes(:family => Socket::AF_INET) # res = ip.route.read_route(: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,
@@ -80,7 +80,7 @@ module Netlink
# [#<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_route(opt=nil, &blk)
@rtsocket.send_request RTM_GETROUTE, RT.new(opt), @rtsocket.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
@rtsocket.receive_until_done(RTM_NEWROUTE, &blk) @rtsocket.receive_until_done(RTM_NEWROUTE, &blk)
@@ -110,10 +110,10 @@ module Netlink
# :oif => "eth0" # :oif => "eth0"
# :iif => "eth1" # :iif => "eth1"
def list(filter=nil, &blk) def list(filter=nil, &blk)
@routes ||= read_routes @route ||= read_route
filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif) filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif)
filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif) filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif)
do_list(@routes, filter, &blk) do_list(@route, filter, &blk)
end end
alias :each :list alias :each :list

View File

@@ -9,7 +9,7 @@ module Netlink
end end
def list(filter={}, &blk) def list(filter={}, &blk)
@rtsocket.links.list(filter.merge(:kind=>"vlan")) @rtsocket.link.list(filter.merge(:kind=>"vlan"))
end end
alias :each :list alias :each :list
@@ -21,19 +21,19 @@ module Netlink
# :vlan_mask=>0xffffffff # :vlan_mask=>0xffffffff
# ) # )
def add(opt) def add(opt)
@rtsocket.links.add(vlan_options(opt)) @rtsocket.link.add(vlan_options(opt))
end end
def change(opt) def change(opt)
@rtsocket.links.change(vlan_options(opt)) @rtsocket.link.change(vlan_options(opt))
end end
def replace(opt) def replace(opt)
@rtsocket.links.replace(vlan_options(opt)) @rtsocket.link.replace(vlan_options(opt))
end end
# Delete vlan given :link and :vlan_id. If you want to delete # Delete vlan given :link and :vlan_id. If you want to delete
# by :index then call links.delete instead. # by :index then call link.delete instead.
def delete(opt) def delete(opt)
raise "Missing vlan_id" unless opt[:vlan_id] raise "Missing vlan_id" unless opt[:vlan_id]
raise "Missing link" unless opt[:link] raise "Missing link" unless opt[:link]
@@ -42,7 +42,7 @@ module Netlink
l.linkinfo.data.id == opt[:vlan_id] l.linkinfo.data.id == opt[:vlan_id]
} }
raise Errno::ENODEV unless link raise Errno::ENODEV unless link
@rtsocket.links.delete(link.index) @rtsocket.link.delete(link.index)
end end
def vlan_options(orig) #:nodoc: def vlan_options(orig) #:nodoc: