More handler separation: e.g. RouteHandler or VlanHandler can call LinkHandler

This commit is contained in:
Brian Candler
2011-05-03 15:59:07 +01:00
parent a2735b4d4d
commit 3f4c6c7235
12 changed files with 667 additions and 498 deletions

View File

@@ -5,17 +5,17 @@ require 'netlink/route'
nl = Netlink::Route::Socket.new
puts "\n*** Before adding address"
nl.if.addrs["lo"][Socket::AF_INET].each { |x| puts x.address }
nl.addrs.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }
puts "\n*** After adding address"
begin
nl.if.add_addr(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
nl.addrs.add(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
rescue Errno::EEXIST
puts "Already exists"
end
nl.if.addrs["lo"][Socket::AF_INET].each { |x| puts x.address }
nl.addrs.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }
puts "\n*** After deleting address"
nl.if.delete_addr(:index=>"lo", :local=>"1.2.3.4", :prefixlen=>32)
nl.if.addrs["lo"][Socket::AF_INET].each { |x| puts x.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 }

21
examples/add_route.rb Normal file
View File

@@ -0,0 +1,21 @@
LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR
require 'netlink/route'
nl = Netlink::Route::Socket.new
puts "\n*** Before adding route"
nl.routes.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")
rescue Errno::EEXIST
puts "Already exists"
end
nl.routes.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 }

View File

@@ -6,16 +6,16 @@ require 'pp'
nl = Netlink::Route::Socket.new
puts "\n*** Before adding VLAN"
pp nl.if.links(:kind=>"vlan").to_a
pp nl.vlans.list(:link=>"lo").to_a
puts "\n*** After adding VLAN on lo"
begin
nl.if.add_vlan(:link=>"lo", :vlan_id=>1234)
nl.vlans.add(:link=>"lo", :vlan_id=>1234)
rescue Errno::EEXIST
puts "Already present"
end
pp nl.if.links(:kind=>"vlan").to_a
pp nl.vlans.list(:link=>"lo").to_a
puts "\n*** After deleting VLANs from lo"
nl.if.delete_vlan(:link=>"lo", :vlan_id=>1234)
pp nl.if.links(:kind=>"vlan").to_a
nl.vlans.delete(:link=>"lo", :vlan_id=>1234)
pp nl.vlans.list(:link=>"lo").to_a

View File

@@ -8,9 +8,17 @@ require 'netlink/route'
# The data is memoized - that is, it's downloaded from the kernel once
# and then manipulated internally.
nl = Netlink::Route::Socket.new
pp nl.if["eth0"]
pp nl.if.addrs["eth0"]
rt = Netlink::Route::Socket.new
# Find the route with the shortest prefix len (probably default route)
pp nl.rt[Socket::AF_INET].min_by { |route| route.dst_len }
puts "\nInterface eth0:"
pp rt.links["eth0"]
puts "\nAddresses on interface eth0:"
pp rt.addrs.list(:index=>"eth0").to_a
puts "\nAll routes in main routing table:"
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).to_a
puts "\nDefault route is probably:"
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).
min_by { |route| route.dst_len }

View File

@@ -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.
nl = Netlink::Route::Socket.new
rt = Netlink::Route::Socket.new
puts "*** links ***"
pp nl.if.read_links
pp rt.links.read_links
puts "*** addrs ***"
pp nl.if.read_addrs(:family => Socket::AF_INET)
pp rt.addrs.read_addrs
puts "*** routes ***"
pp nl.rt.read_routes(:family => Socket::AF_INET)
pp rt.routes.read_routes