2011-05-03 15:59:07 +01:00
|
|
|
# This file implements the messages and methods for the NETLINK_ROUTE protocol.
|
|
|
|
# Apart from a few utility functions for converting ifname to index and vice
|
|
|
|
# versa, the logic is delegated to separate classes for each entity
|
|
|
|
# (links, addresses etc)
|
2011-04-30 22:39:25 +01:00
|
|
|
|
|
|
|
require 'netlink/nlsocket'
|
|
|
|
require 'netlink/message'
|
|
|
|
|
|
|
|
module Netlink
|
2011-05-01 09:29:02 +01:00
|
|
|
module Route
|
2011-05-03 15:59:07 +01:00
|
|
|
autoload :LinkHandler, 'netlink/route/link_handler'
|
|
|
|
autoload :VlanHandler, 'netlink/route/vlan_handler'
|
|
|
|
autoload :AddrHandler, 'netlink/route/addr_handler'
|
|
|
|
autoload :RouteHandler, 'netlink/route/route_handler'
|
2011-05-03 12:41:10 +01:00
|
|
|
|
2011-05-02 22:52:47 +01:00
|
|
|
# This class formats and receives messages using NETLINK_ROUTE protocol
|
2011-05-01 09:29:02 +01:00
|
|
|
class Socket < NLSocket
|
|
|
|
def initialize(opt={})
|
|
|
|
super(opt.merge(:protocol => Netlink::NETLINK_ROUTE))
|
|
|
|
end
|
2011-04-30 22:39:25 +01:00
|
|
|
|
2011-05-03 15:59:07 +01:00
|
|
|
# Return a Netlink::Route::LinkHandler object for manipulating links
|
|
|
|
def links
|
|
|
|
@links ||= Netlink::Route::LinkHandler.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return a Netlink::Route::VlanHandler object for manipulating vlans
|
|
|
|
def vlans
|
|
|
|
@vlans ||= Netlink::Route::VlanHandler.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return a Netlink::Route::AddrHandler object for manipulating addresses
|
|
|
|
def addrs
|
|
|
|
@addrs ||= Netlink::Route::AddrHandler.new(self)
|
2011-05-01 09:29:02 +01:00
|
|
|
end
|
|
|
|
|
2011-05-03 12:41:10 +01:00
|
|
|
# Return a Netlink::Route::RT object for manipulating routes
|
2011-05-03 15:59:07 +01:00
|
|
|
def routes
|
|
|
|
@routes ||= Netlink::Route::RouteHandler.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert an interface index into name string, or nil if the
|
2011-05-03 16:05:24 +01:00
|
|
|
# index is nil or 0. Raises exception for unknown values.
|
2011-05-03 15:59:07 +01:00
|
|
|
#
|
|
|
|
# nl = Netlink::Route::Socket.new
|
|
|
|
# nl.routes(:family=>Socket::AF_INET) do |route|
|
|
|
|
# puts "iif=#{nl.ifname(route.iif)}"
|
|
|
|
# puts "oif=#{nl.ifname(route.oif)}"
|
|
|
|
# end
|
|
|
|
def ifname(index)
|
|
|
|
return nil if index.nil? || index == 0
|
|
|
|
links[index].ifname
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert an interface name into index. Returns 0 for nil or empty
|
|
|
|
# string. Otherwise raises an exception for unknown values.
|
|
|
|
def index(name)
|
|
|
|
case name
|
|
|
|
when Integer
|
|
|
|
name
|
|
|
|
when nil, EMPTY_STRING
|
|
|
|
0
|
|
|
|
else
|
|
|
|
links[name].index
|
|
|
|
end
|
2011-05-01 09:29:02 +01:00
|
|
|
end
|
2011-04-30 22:39:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|