Rename plural to singular; cleaner and more consistent with 'ip' utility
This commit is contained in:
@@ -3,19 +3,19 @@ $LOAD_PATH.unshift LIBDIR
|
||||
|
||||
require 'netlink/route'
|
||||
|
||||
nl = Netlink::Route::Socket.new
|
||||
ip = Netlink::Route::Socket.new
|
||||
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"
|
||||
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
|
||||
puts "Already exists"
|
||||
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"
|
||||
nl.addrs.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.delete(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
|
||||
ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }
|
||||
|
||||
|
@@ -3,19 +3,19 @@ $LOAD_PATH.unshift LIBDIR
|
||||
|
||||
require 'netlink/route'
|
||||
|
||||
nl = Netlink::Route::Socket.new
|
||||
ip = Netlink::Route::Socket.new
|
||||
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"
|
||||
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
|
||||
puts "Already exists"
|
||||
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"
|
||||
nl.routes.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.delete(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1")
|
||||
ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x }
|
||||
|
||||
|
@@ -4,18 +4,18 @@ $LOAD_PATH.unshift LIBDIR
|
||||
require 'netlink/route'
|
||||
require 'pp'
|
||||
|
||||
nl = Netlink::Route::Socket.new
|
||||
ip = Netlink::Route::Socket.new
|
||||
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"
|
||||
begin
|
||||
nl.vlans.add(:link=>"lo", :vlan_id=>1234)
|
||||
ip.vlan.add(:link=>"lo", :vlan_id=>1234)
|
||||
rescue Errno::EEXIST
|
||||
puts "Already present"
|
||||
end
|
||||
pp nl.vlans.list(:link=>"lo").to_a
|
||||
pp ip.vlan.list(:link=>"lo").to_a
|
||||
|
||||
puts "\n*** After deleting VLANs from lo"
|
||||
nl.vlans.delete(:link=>"lo", :vlan_id=>1234)
|
||||
pp nl.vlans.list(:link=>"lo").to_a
|
||||
ip.vlan.delete(:link=>"lo", :vlan_id=>1234)
|
||||
pp ip.vlan.list(:link=>"lo").to_a
|
||||
|
@@ -8,20 +8,20 @@ require 'netlink/route'
|
||||
# The data is memoized - that is, it's downloaded from the kernel once
|
||||
# and then manipulated internally.
|
||||
|
||||
rt = Netlink::Route::Socket.new
|
||||
ip = Netlink::Route::Socket.new
|
||||
|
||||
puts "\nInterface eth0:"
|
||||
pp rt.links["eth0"]
|
||||
pp ip.link["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:"
|
||||
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:"
|
||||
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 }
|
||||
|
||||
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)
|
||||
|
@@ -7,10 +7,10 @@ require 'netlink/route'
|
||||
# Example of use of low-level API for NETLINK_ROUTE socket.
|
||||
# Each of these method calls performs a netlink protocol exchange.
|
||||
|
||||
rt = Netlink::Route::Socket.new
|
||||
ip = Netlink::Route::Socket.new
|
||||
puts "*** links ***"
|
||||
pp rt.links.read_links
|
||||
pp ip.link.read_link
|
||||
puts "*** addrs ***"
|
||||
pp rt.addrs.read_addrs
|
||||
pp ip.addr.read_addr
|
||||
puts "*** routes ***"
|
||||
pp rt.routes.read_routes
|
||||
pp ip.route.read_route
|
||||
|
@@ -20,23 +20,23 @@ module Netlink
|
||||
end
|
||||
|
||||
# Return a Netlink::Route::LinkHandler object for manipulating links
|
||||
def links
|
||||
@links ||= Netlink::Route::LinkHandler.new(self)
|
||||
def link
|
||||
@link ||= Netlink::Route::LinkHandler.new(self)
|
||||
end
|
||||
|
||||
# Return a Netlink::Route::VlanHandler object for manipulating vlans
|
||||
def vlans
|
||||
@vlans ||= Netlink::Route::VlanHandler.new(self)
|
||||
def vlan
|
||||
@vlan ||= Netlink::Route::VlanHandler.new(self)
|
||||
end
|
||||
|
||||
# Return a Netlink::Route::AddrHandler object for manipulating addresses
|
||||
def addrs
|
||||
@addrs ||= Netlink::Route::AddrHandler.new(self)
|
||||
def addr
|
||||
@addr ||= Netlink::Route::AddrHandler.new(self)
|
||||
end
|
||||
|
||||
# Return a Netlink::Route::RT object for manipulating routes
|
||||
def routes
|
||||
@routes ||= Netlink::Route::RouteHandler.new(self)
|
||||
def route
|
||||
@route ||= Netlink::Route::RouteHandler.new(self)
|
||||
end
|
||||
|
||||
# Convert an interface index into name string, or nil if the
|
||||
@@ -49,7 +49,7 @@ module Netlink
|
||||
# end
|
||||
def ifname(index)
|
||||
return nil if index.nil? || index == 0
|
||||
links[index].ifname
|
||||
link[index].ifname
|
||||
end
|
||||
|
||||
# Convert an interface name into index. Returns 0 for nil or empty
|
||||
@@ -61,7 +61,7 @@ module Netlink
|
||||
when nil, EMPTY_STRING
|
||||
0
|
||||
else
|
||||
links[name].index
|
||||
link[name].index
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -29,7 +29,7 @@ module Netlink
|
||||
# This class provides an API for manipulating iaddresses.
|
||||
class AddrHandler < Handler
|
||||
def clear_cache
|
||||
@addrs = nil
|
||||
@addr = nil
|
||||
end
|
||||
|
||||
# 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
|
||||
# is honoured.
|
||||
#
|
||||
# res = nl.read_addrs(:family => Socket::AF_INET)
|
||||
# res = nl.read_addr(:family => Socket::AF_INET)
|
||||
# p res
|
||||
# [#<Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254,
|
||||
# :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"}>, ...]
|
||||
def read_addrs(opt=nil, &blk)
|
||||
def read_addr(opt=nil, &blk)
|
||||
@rtsocket.send_request RTM_GETADDR, IFAddr.new(opt),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWADDR, &blk)
|
||||
@@ -64,22 +64,22 @@ module Netlink
|
||||
# The full address list is read once and memoized, so
|
||||
# it is efficient to call this method multiple times.
|
||||
#
|
||||
# nl.addrs.list { |x| p x }
|
||||
# addrs_eth0 = nl.addrs.list(:index=>"eth0").to_a
|
||||
# addrs_eth0_v4 = nl.addrs.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
|
||||
# nl.addr.list { |x| p x }
|
||||
# addrs_eth0 = nl.addr.list(:index=>"eth0").to_a
|
||||
# addrs_eth0_v4 = nl.addr.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
|
||||
def list(filter=nil, &blk)
|
||||
@addrs ||= read_addrs
|
||||
@addr ||= read_addr
|
||||
filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index)
|
||||
do_list(@addrs, filter, &blk)
|
||||
do_list(@addr, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
# 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,...]
|
||||
#
|
||||
# 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.
|
||||
def group_by_interface(*filter)
|
||||
res = list(*filter).group_by { |a| ifname(a.index) }
|
||||
@@ -90,8 +90,8 @@ module Netlink
|
||||
# Add an IP address to an interface
|
||||
#
|
||||
# require 'netlink/route'
|
||||
# rt = Netlink::Route::Socket.new
|
||||
# rt.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24)
|
||||
# ip = Netlink::Route::Socket.new
|
||||
# ip.addr.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24)
|
||||
def add(opt)
|
||||
ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt)
|
||||
end
|
||||
|
@@ -122,14 +122,14 @@ module Netlink
|
||||
# you should create a new instance of this object.
|
||||
class LinkHandler < Handler
|
||||
def clear_cache
|
||||
@links = nil
|
||||
@link = nil
|
||||
@linkmap = nil
|
||||
end
|
||||
|
||||
# Download a list of links (interfaces). Either returns an array of
|
||||
# Netlink::IFInfo objects, or yields them to the supplied block.
|
||||
#
|
||||
# res = rt.links.read_links
|
||||
# res = ip.link.read_link
|
||||
# p res
|
||||
# [#<Netlink::IFInfo {:family=>0, :type=>772, :index=>1,
|
||||
# :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",
|
||||
# :stats32=>#<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),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWLINK, &blk)
|
||||
@@ -157,16 +157,16 @@ module Netlink
|
||||
# The full interface list is read once and memoized, so
|
||||
# it is efficient to call this method multiple times.
|
||||
#
|
||||
# rt.links.list { |x| p x }
|
||||
# ethers = rt.links.list(:type => Netlink::ARPHRD_ETHER).to_a
|
||||
# vlans = rt.links.list(:kind => "vlan").to_a
|
||||
# rt.links.list(:flags => Netlink::IFF_RUNNING)
|
||||
# rt.links.list(:noflags => Netlink::IFF_POINTOPOINT)
|
||||
# rt.links.list(:link => "lo") # vlan etc attached to this interface
|
||||
# ip.link.list { |x| p x }
|
||||
# ethers = ip.link.list(:type => Netlink::ARPHRD_ETHER).to_a
|
||||
# vlans = ip.link.list(:kind => "vlan").to_a
|
||||
# ip.link.list(:flags => Netlink::IFF_RUNNING)
|
||||
# ip.link.list(:noflags => Netlink::IFF_POINTOPOINT)
|
||||
# ip.link.list(:link => "lo") # vlan etc attached to this interface
|
||||
def list(filter=nil, &blk)
|
||||
@links ||= read_links
|
||||
@link ||= read_link
|
||||
filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link)
|
||||
do_list(@links, filter, &blk)
|
||||
do_list(@link, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
@@ -188,8 +188,8 @@ module Netlink
|
||||
# Add an interface (raw). e.g.
|
||||
#
|
||||
# require 'netlink/route'
|
||||
# rt = Netlink::Route::Socket.new
|
||||
# rt.links.add_link(
|
||||
# ip = Netlink::Route::Socket.new
|
||||
# ip.link.add(
|
||||
# :link=>"lo",
|
||||
# :linkinfo=>Netlink::LinkInfo.new(
|
||||
# :kind=>"vlan",
|
||||
|
@@ -56,7 +56,7 @@ module Netlink
|
||||
# This class manipulates the kernel routing table
|
||||
class RouteHandler < Handler
|
||||
def clear_cache
|
||||
@routes = nil
|
||||
@route = nil
|
||||
end
|
||||
|
||||
# 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
|
||||
# to perform your own filtering. e.g.
|
||||
# read_routes(:family=>Socket::AF_INET) # works
|
||||
# read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored
|
||||
# read_route(:family=>Socket::AF_INET) # works
|
||||
# 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
|
||||
# [#<Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0,
|
||||
# :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,
|
||||
# :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}>, ...]
|
||||
def read_routes(opt=nil, &blk)
|
||||
def read_route(opt=nil, &blk)
|
||||
@rtsocket.send_request RTM_GETROUTE, RT.new(opt),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWROUTE, &blk)
|
||||
@@ -110,10 +110,10 @@ module Netlink
|
||||
# :oif => "eth0"
|
||||
# :iif => "eth1"
|
||||
def list(filter=nil, &blk)
|
||||
@routes ||= read_routes
|
||||
@route ||= read_route
|
||||
filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif)
|
||||
filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif)
|
||||
do_list(@routes, filter, &blk)
|
||||
do_list(@route, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
|
@@ -9,7 +9,7 @@ module Netlink
|
||||
end
|
||||
|
||||
def list(filter={}, &blk)
|
||||
@rtsocket.links.list(filter.merge(:kind=>"vlan"))
|
||||
@rtsocket.link.list(filter.merge(:kind=>"vlan"))
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
@@ -21,19 +21,19 @@ module Netlink
|
||||
# :vlan_mask=>0xffffffff
|
||||
# )
|
||||
def add(opt)
|
||||
@rtsocket.links.add(vlan_options(opt))
|
||||
@rtsocket.link.add(vlan_options(opt))
|
||||
end
|
||||
|
||||
def change(opt)
|
||||
@rtsocket.links.change(vlan_options(opt))
|
||||
@rtsocket.link.change(vlan_options(opt))
|
||||
end
|
||||
|
||||
def replace(opt)
|
||||
@rtsocket.links.replace(vlan_options(opt))
|
||||
@rtsocket.link.replace(vlan_options(opt))
|
||||
end
|
||||
|
||||
# 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)
|
||||
raise "Missing vlan_id" unless opt[:vlan_id]
|
||||
raise "Missing link" unless opt[:link]
|
||||
@@ -42,7 +42,7 @@ module Netlink
|
||||
l.linkinfo.data.id == opt[:vlan_id]
|
||||
}
|
||||
raise Errno::ENODEV unless link
|
||||
@rtsocket.links.delete(link.index)
|
||||
@rtsocket.link.delete(link.index)
|
||||
end
|
||||
|
||||
def vlan_options(orig) #:nodoc:
|
||||
|
Reference in New Issue
Block a user