diff --git a/examples/route_hi.rb b/examples/route_hi.rb new file mode 100644 index 0000000..9753a9e --- /dev/null +++ b/examples/route_hi.rb @@ -0,0 +1,16 @@ +LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift LIBDIR + +require 'pp' +require 'netlink/route' + +# Example of use of high-level API for NETLINK_ROUTE socket. +# The data is memoized - that is, it's downloaded from the kernel once +# and then manipulated internally. + +nl = Netlink::RTSocket.new +pp nl.link["eth0"] +pp nl.addrs["eth0"] + +# Find the route with the shortest prefix len (probably default route) +pp nl.routes[Socket::AF_INET].min_by { |route| route.dst_len } diff --git a/examples/route_lo.rb b/examples/route_lo.rb new file mode 100644 index 0000000..52fe645 --- /dev/null +++ b/examples/route_lo.rb @@ -0,0 +1,16 @@ +LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift LIBDIR + +require 'pp' +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::RTSocket.new +puts "*** links ***" +pp nl.read_links +puts "*** addrs ***" +pp nl.read_addrs(:family => Socket::AF_INET) +puts "*** routes ***" +pp nl.read_routes(:family => Socket::AF_INET) diff --git a/lib/netlink/message.rb b/lib/netlink/message.rb index 15d7089..ff16360 100644 --- a/lib/netlink/message.rb +++ b/lib/netlink/message.rb @@ -5,27 +5,6 @@ module Netlink EMPTY_STRING = "".freeze #:nodoc: EMPTY_ARRAY = [].freeze #:nodoc: - # struct rtnl_link_stats / rtnl_link_stats64 - LinkStats = Struct.new :rx_packets, :tx_packets, - :rx_bytes, :tx_bytes, - :rx_errors, :tx_errors, - :rx_dropped, :tx_dropped, - :multicast, :collisions, - :rx_length_errors, :rx_over_errors, - :rx_crc_errors, :rx_frame_errors, - :rx_fifo_errors, :rx_missed_errors, - :tx_aborted_errorsr, :tx_carrier_errors, - :tx_fifo_errors, :tx_heartbeat_errors, - :tx_window_errors, - :rx_compressed, :tx_compressed - - # struct rta_cacheinfo - RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage - # struct ifa_cacheinfo - IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp - # struct ifmap - IFMap = Struct.new :mem_start, :mem_end, :base_addr, :irq, :dma, :port - # This is the base class from which all Netlink messages are derived. # To define a new Netlink message, make a subclass and then call the # "field" metaprogramming method to define the parts of the message, in @@ -45,7 +24,7 @@ module Netlink # msg2 = Foo.new(:qux => 999) # error: no method "qux=" # msg2 = Foo.new(msg) # cloning an existing message # - # Use RtattrMessage for messages which are followed by variable rtattrs. + # Use RtattrMessage instead for messages which are followed by variable rtattrs. class Message TYPE_INFO = {} #:nodoc @@ -75,39 +54,6 @@ module Netlink define_type :binary, :pattern => "a*", :default => EMPTY_STRING define_type :cstring, :pattern => "Z*", :default => EMPTY_STRING - define_type :linkstats32, - :pack => lambda { |val,obj| val.to_a.pack("L23") }, - :unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("L23"))) } - - define_type :linkstats64, - :pack => lambda { |val,obj| val.to_a.pack("Q23") }, - :unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("Q23"))) } - - define_type :rta_cacheinfo, - :pack => lambda { |val,obj| val.to_a.pack("L*") }, - :unpack => lambda { |str,obj| RTACacheInfo.new(*(str.unpack("L*"))) } - - define_type :ifa_cacheinfo, - :pack => lambda { |val,obj| val.to_a.pack("L*") }, - :unpack => lambda { |str,obj| IFACacheInfo.new(*(str.unpack("L*"))) } - - IFMAP_PACK = "QQQSCC".freeze #:nodoc: - define_type :ifmap, - :pack => lambda { |val,obj| val.to_a.pack(IFMAP_PACK) }, - :unpack => lambda { |str,obj| IFMap.new(*(str.unpack(IFMAP_PACK))) } - - METRIC_PACK = "SSL".freeze #:nodoc: - METRIC_SIZE = [0,0,0].pack(METRIC_PACK).bytesize #:nodoc: - define_type :metrics, - :pack => lambda { |metrics,obj| - metrics.map { |code,val| [METRIC_SIZE,code,val].pack(METRIC_PACK) }.join - }, - :unpack => lambda { |str,obj| - res = {} # in kernel the dst.metrics structure is array of u32 - RtattrMessage.unpack_rtattr(str) { |code,val| res[code] = val.unpack("L").first } - res - } - # L2 addresses are presented as ASCII hex. You may optionally include # colons, hyphens or dots. # Link.new(:address => "00:11:22:33:44:55") # this is OK @@ -351,84 +297,4 @@ module Netlink end end end - - class Link < RtattrMessage - code RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK - field :family, :uchar # Socket::AF_* - field :pad, :uchar - field :type, :ushort # ARPHRD_* - field :index, :int - field :flags, :uint # IFF_* - field :change, :uint, :default=>0xffffffff - rtattr :address, IFLA_ADDRESS, :l2addr - rtattr :broadcast, IFLA_BROADCAST, :l2addr - rtattr :ifname, IFLA_IFNAME, :cstring - rtattr :mtu, IFLA_MTU, :uint32 - rtattr :link, IFLA_LINK, :int32 - rtattr :qdisc, IFLA_QDISC, :cstring - rtattr :stats32, IFLA_STATS, :linkstats32 - rtattr :cost, IFLA_COST - rtattr :master, IFLA_MASTER, :uint32 - rtattr :wireless, IFLA_WIRELESS - rtattr :protinfo, IFLA_PROTINFO, :uchar - rtattr :txqlen, IFLA_TXQLEN, :uint32 - rtattr :map, IFLA_MAP, :ifmap - rtattr :weight, IFLA_WEIGHT, :uint32 - rtattr :operstate, IFLA_OPERSTATE, :uchar - rtattr :linkmode, IFLA_LINKMODE, :uchar - rtattr :linkinfo, IFLA_LINKINFO # nested - rtattr :net_ns_pid, IFLA_NET_NS_PID, :uint32 - rtattr :ifalias, IFLA_IFALIAS, :cstring - rtattr :num_vf, IFLA_NUM_VF, :uint32 - rtattr :vfinfo_list, IFLA_VFINFO_LIST - rtattr :stats64, IFLA_STATS64, :linkstats64 - rtattr :vf_ports, IFLA_VF_PORTS - rtattr :port_self, IFLA_PORT_SELF - - # Return the best stats available (64bit or 32bit) - def stats - stats64 || stats32 - end - end - - class Addr < RtattrMessage - code RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR - field :family, :uchar # Socket::AF_* - field :prefixlen, :uchar - field :flags, :uchar # IFA_F_* - field :scope, :uchar # RT_SCOPE_* - field :index, :int - rtattr :address, IFA_ADDRESS, :l3addr - rtattr :local, IFA_LOCAL, :l3addr - rtattr :label, IFA_LABEL, :cstring - rtattr :broadcast, IFA_BROADCAST, :l3addr - rtattr :anycast, IFA_ANYCAST, :l3addr - rtattr :cacheinfo, IFA_CACHEINFO, :ifa_cacheinfo - rtattr :multicast, IFA_MULTICAST, :l3addr - end - - class Route < RtattrMessage - code RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE - field :family, :uchar # Socket::AF_* - field :dst_len, :uchar - field :src_len, :uchar - field :tos, :uchar - field :table, :uchar # table id or RT_TABLE_* - field :protocol, :uchar # RTPROT_* - field :scope, :uchar # RT_SCOPE_* - field :type, :uchar # RTN_* - field :flags, :uint # RTM_F_* - rtattr :dst, RTA_DST, :l3addr - rtattr :src, RTA_SRC, :l3addr - rtattr :iif, RTA_IIF, :uint32 - rtattr :oif, RTA_OIF, :uint32 - rtattr :gateway, RTA_GATEWAY, :l3addr - rtattr :priority, RTA_PRIORITY, :uint32 - rtattr :prefsrc, RTA_PREFSRC, :l3addr - rtattr :metrics, RTA_METRICS, :metrics - rtattr :multipath, RTA_MULTIPATH - rtattr :flow, RTA_FLOW - rtattr :cacheinfo, RTA_CACHEINFO, :rta_cacheinfo - rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places! - end end diff --git a/lib/netlink/nlsocket.rb b/lib/netlink/nlsocket.rb index 673d69b..a70164b 100644 --- a/lib/netlink/nlsocket.rb +++ b/lib/netlink/nlsocket.rb @@ -3,8 +3,11 @@ require 'netlink/constants' require 'netlink/message' module Netlink + # NLSocket provides low-level sending and receiving of messages across + # a netlink socket, adding headers to sent messages and parsing + # received messages. class NLSocket - DEFAULT_TIMEOUT = 2 + DEFAULT_TIMEOUT = 5 SOCKADDR_PACK = "SSLL".freeze #:nodoc: @@ -18,28 +21,23 @@ module Netlink # Check the sockaddr on a received message. Raises an error if the AF # is not AF_NETLINK or the PID is not 0 (this is important for security) - def self.parse_sockaddr(str) + def self.check_sockaddr(str) af, pad, pid, groups = str.unpack(SOCKADDR_PACK) raise "Bad AF #{af}!" if af != Socket::AF_NETLINK raise "Bad PID #{pid}!" if pid != 0 end - attr_accessor :socket - attr_accessor :seq - attr_accessor :pid + attr_accessor :socket # the underlying Socket + attr_accessor :seq # the last sequence number used + attr_accessor :pid # default pid to include in message headers + attr_accessor :timeout # default timeout when receiving message # Create a new Netlink socket. Pass in chosen protocol: # :protocol => Netlink::NETLINK_ARPD # :protocol => Netlink::NETLINK_FIREWALL - # :protocol => Netlink::NETLINK_IP6_FW - # :protocol => Netlink::NETLINK_NFLOG # :protocol => Netlink::NETLINK_ROUTE - # :protocol => Netlink::NETLINK_ROUTE6 - # :protocol => Netlink::NETLINK_TAPBASE - # :protocol => Netlink::NETLINK_TCPDIAG - # :protocol => Netlink::NETLINK_XFRM - # Other options: - # :groups => N (subscribe to multicastgroups, default to 0) + # etc. Other options: + # :groups => N (subscribe to multicast groups, default to 0) # :seq => N (override initial sequence number) # :pid => N (override PID) # :timeout => N (seconds, default to DEFAULT_TIMEOUT. Pass nil for no timeout) @@ -55,16 +53,25 @@ module Netlink @timeout = opt.has_key?(:timeout) ? opt[:timeout] : DEFAULT_TIMEOUT end - # Send a Netlink::Message object over the socket - # obj:: the object to send (responds to #to_s) + # Generate the next sequence number + def next_seq + @seq = (@seq + 1) & 0xffffffff + end + + # Add a header and send a single message over the socket. + # type:: the message type code + # msg:: the message to send (without header) # flags:: message header flags, default NLM_F_REQUEST # sockaddr:: destination sockaddr, defaults to pid=0 and groups=0 # seq:: sequence number, defaults to bump internal sequence # pid:: pid, defaults to $$ # vflags:: sendmsg flags, defaults to 0 - def send_request(type, obj, flags=NLM_F_REQUEST, sockaddr=SOCKADDR_DEFAULT, seq=(@seq += 1), pid=@pid, vflags=0, controls=[]) + # Normally 'msg' would be an instance of a Netlink::Message subclass, + # although in fact any object which respond to #to_s will do (if you + # want to pack the message body yourself). + def send_request(type, msg, flags=NLM_F_REQUEST, sockaddr=SOCKADDR_DEFAULT, seq=next_seq, pid=@pid, vflags=0, controls=[]) @socket.sendmsg( - build_message(type, obj, flags, seq, pid), + build_message(type, msg, flags, seq, pid), vflags, sockaddr, *controls ) end @@ -73,7 +80,7 @@ module Netlink NLMSGHDR_SIZE = [0,0,0,0,0].pack(NLMSGHDR_PACK).bytesize # :nodoc: # Build a message comprising header+body. It is not padded at the end. - def build_message(type, body, flags=NLM_F_REQUEST, seq=(@seq += 1), pid=@pid) + def build_message(type, body, flags=NLM_F_REQUEST, seq=next_seq, pid=@pid) body = body.to_s header = [ body.bytesize + NLMSGHDR_SIZE, @@ -86,38 +93,44 @@ module Netlink # Send multiple Netlink::Message objects in a single message. They # need to share the same type and flags, and will be sent with sequential # sequence nos. - def send_requests(type, objs, flags=NLM_F_REQUEST, pid=@pid) - objs.each_with_index do |obj, index| - if index < objs.size - 1 - data << build_message(type, obj, flags|NLM_F_MULTI, @seq+=1, pid) + def send_requests(type, msgs, flags=NLM_F_REQUEST, pid=@pid) + msgs.each_with_index do |msg, index| + if index < msgs.size - 1 + data << build_message(type, msg, flags|NLM_F_MULTI, next_seq, pid) Message.pad(data) else - data << build_message(type, obj, flags, @seq+=1, pid) + data << build_message(type, msg, flags, next_seq, pid) end end end # Discard all waiting messages - def flush + def drain while select([@socket], nil, nil, 0) - @socket.recvmsg + mesg, sender, rflags, controls = @socket.recvmsg + raise EOFError unless mesg end end - # Loop receiving responses until Netlink::Message::Done, and yielding - # the objects found. Also filters so that only expected pid and seq - # are accepted. + # Loop receiving responses until a DONE message is received (or you + # break out of the loop, or a timeout exception occurs). Filters out + # messages with unexpected pid and seq. If you pass an expected_type then + # messages other than this type will be discarded too. + # + # Yields Netlink::Message objects, or if no block is given, returns an + # array of those objects. If you provide a junk_handler then it will be + # called for discarded messages. # # (Compare: rtnl_dump_filter_l in lib/libnetlink.c) - def receive_until_done(expect_type=nil, timeout=@timeout, junk_handler=nil, &blk) #:yields: obj + def receive_until_done(expected_type=nil, timeout=@timeout, junk_handler=nil, &blk) #:yields: msg res = [] - blk ||= lambda { |obj| res << obj } - junk_handler ||= lambda { |type, flags, seq, pid, obj| - warn "Discarding junk message (#{type}) #{obj}" } if $VERBOSE + blk ||= lambda { |msg| res << msg } + junk_handler ||= lambda { |type, flags, seq, pid, msg| + warn "Discarding junk message (#{type}) #{msg}" } if $VERBOSE loop do - receive_response(timeout) do |type, flags, seq, pid, obj| + receive_response(timeout) do |type, flags, seq, pid, msg| if pid != @pid || seq != @seq - junk_handler[type, flags, seq, pid, obj] if junk_handler + junk_handler[type, flags, seq, pid, msg] if junk_handler next end case type @@ -126,41 +139,44 @@ module Netlink when NLMSG_ERROR raise "Netlink Error received" end - if expect_type && type != expect_type - junk_handler[type, flags, seq, pid, obj] if junk_handler + if expected_type && type != expected_type + junk_handler[type, flags, seq, pid, msg] if junk_handler next end - blk.call(obj) if obj + blk.call(msg) if msg end end end - # Receive one datagram from kernel. If a block is given, then yield + # Receive one datagram from kernel. Yield header fields plus # Netlink::Message objects (maybe multiple times if the datagram - # includes multiple netlink messages). + # includes multiple netlink messages). Raise an exception if no + # datagram received within the specified or default timeout period; + # pass nil for infinite timeout. # - # receive_response { |msg| p msg } + # receive_response { |type, flags, seq, pid, msg| p msg } def receive_response(timeout=@timeout, &blk) # :yields: type, flags, seq, pid, Message if select([@socket], nil, nil, timeout) mesg, sender, rflags, controls = @socket.recvmsg raise EOFError unless mesg - NLSocket.parse_sockaddr(sender.to_sockaddr) + NLSocket.check_sockaddr(sender.to_sockaddr) parse_yield(mesg, &blk) else raise "Timeout" end end - # Parse message(s) in a string buffer and yield message object, flags, - # seq and pid - def parse_yield(mesg) # :yields: type, flags, seq, pid, Message + # Parse netlink packet in a string buffer. Yield header fields plus + # a Netlink::Message object (or nil) for each message. + def parse_yield(mesg) # :yields: type, flags, seq, pid, Message-or-nil dechunk(mesg) do |h_type, h_flags, h_seq, h_pid, data| klass = Message::CODE_TO_MESSAGE[h_type] yield h_type, h_flags, h_seq, h_pid, klass && klass.parse(data) end end - # Take message(s) in a string buffer and yield fields in turn + # Parse netlink packet in a string buffer. Yield header and body + # components for each message in turn. def dechunk(mesg) # :yields: type, flags, seq, pid, data ptr = 0 while ptr < mesg.bytesize diff --git a/lib/netlink/route.rb b/lib/netlink/route.rb new file mode 100644 index 0000000..77944cf --- /dev/null +++ b/lib/netlink/route.rb @@ -0,0 +1,297 @@ +# This file implements the messages and methods for the NETLINK_ROUTE protocol + +require 'netlink/nlsocket' +require 'netlink/message' + +module Netlink + # struct rtnl_link_stats / rtnl_link_stats64 + LinkStats = Struct.new :rx_packets, :tx_packets, + :rx_bytes, :tx_bytes, + :rx_errors, :tx_errors, + :rx_dropped, :tx_dropped, + :multicast, :collisions, + :rx_length_errors, :rx_over_errors, + :rx_crc_errors, :rx_frame_errors, + :rx_fifo_errors, :rx_missed_errors, + :tx_aborted_errorsr, :tx_carrier_errors, + :tx_fifo_errors, :tx_heartbeat_errors, + :tx_window_errors, + :rx_compressed, :tx_compressed + + # struct rta_cacheinfo + RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage + # struct ifa_cacheinfo + IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp + # struct ifmap + IFMap = Struct.new :mem_start, :mem_end, :base_addr, :irq, :dma, :port + + # struct ifinfomsg + class Link < RtattrMessage + code RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK + + IFMAP_PACK = "QQQSCC".freeze #:nodoc: + define_type :ifmap, + :pack => lambda { |val,obj| val.to_a.pack(IFMAP_PACK) }, + :unpack => lambda { |str,obj| IFMap.new(*(str.unpack(IFMAP_PACK))) } + + define_type :linkstats32, + :pack => lambda { |val,obj| val.to_a.pack("L23") }, + :unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("L23"))) } + + define_type :linkstats64, + :pack => lambda { |val,obj| val.to_a.pack("Q23") }, + :unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("Q23"))) } + + field :family, :uchar # Socket::AF_* + field :pad, :uchar + field :type, :ushort # ARPHRD_* + field :index, :int + field :flags, :uint # IFF_* + field :change, :uint, :default=>0xffffffff + rtattr :address, IFLA_ADDRESS, :l2addr + rtattr :broadcast, IFLA_BROADCAST, :l2addr + rtattr :ifname, IFLA_IFNAME, :cstring + rtattr :mtu, IFLA_MTU, :uint32 + rtattr :link, IFLA_LINK, :int32 + rtattr :qdisc, IFLA_QDISC, :cstring + rtattr :stats32, IFLA_STATS, :linkstats32 + rtattr :cost, IFLA_COST + rtattr :master, IFLA_MASTER, :uint32 + rtattr :wireless, IFLA_WIRELESS + rtattr :protinfo, IFLA_PROTINFO, :uchar + rtattr :txqlen, IFLA_TXQLEN, :uint32 + rtattr :map, IFLA_MAP, :ifmap + rtattr :weight, IFLA_WEIGHT, :uint32 + rtattr :operstate, IFLA_OPERSTATE, :uchar + rtattr :linkmode, IFLA_LINKMODE, :uchar + rtattr :linkinfo, IFLA_LINKINFO # nested + rtattr :net_ns_pid, IFLA_NET_NS_PID, :uint32 + rtattr :ifalias, IFLA_IFALIAS, :cstring + rtattr :num_vf, IFLA_NUM_VF, :uint32 + rtattr :vfinfo_list, IFLA_VFINFO_LIST + rtattr :stats64, IFLA_STATS64, :linkstats64 + rtattr :vf_ports, IFLA_VF_PORTS + rtattr :port_self, IFLA_PORT_SELF + + # Return the best stats available (64bit or 32bit) + def stats + stats64 || stats32 + end + end + + # struct ifaddrmsg + class Addr < RtattrMessage + code RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR + + define_type :ifa_cacheinfo, + :pack => lambda { |val,obj| val.to_a.pack("L*") }, + :unpack => lambda { |str,obj| IFACacheInfo.new(*(str.unpack("L*"))) } + + field :family, :uchar # Socket::AF_* + field :prefixlen, :uchar + field :flags, :uchar # IFA_F_* + field :scope, :uchar # RT_SCOPE_* + field :index, :int + rtattr :address, IFA_ADDRESS, :l3addr + rtattr :local, IFA_LOCAL, :l3addr + rtattr :label, IFA_LABEL, :cstring + rtattr :broadcast, IFA_BROADCAST, :l3addr + rtattr :anycast, IFA_ANYCAST, :l3addr + rtattr :cacheinfo, IFA_CACHEINFO, :ifa_cacheinfo + rtattr :multicast, IFA_MULTICAST, :l3addr + end + + # struct rtmsg + class Route < RtattrMessage + code RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE + + define_type :rta_cacheinfo, + :pack => lambda { |val,obj| val.to_a.pack("L*") }, + :unpack => lambda { |str,obj| RTACacheInfo.new(*(str.unpack("L*"))) } + + # Route metrics are themselves packed using the rtattr format. + # In the kernel, the dst.metrics structure is an array of u32. + METRIC_PACK = "SSL".freeze #:nodoc: + METRIC_SIZE = [0,0,0].pack(METRIC_PACK).bytesize #:nodoc: + define_type :rtmetrics, + :pack => lambda { |metrics,obj| + metrics.map { |code,val| [METRIC_SIZE,code,val].pack(METRIC_PACK) }.join + }, + :unpack => lambda { |str,obj| + res = {} # in kernel the dst.metrics structure is array of u32 + RtattrMessage.unpack_rtattr(str) { |code,val| res[code] = val.unpack("L").first } + res + } + + field :family, :uchar # Socket::AF_* + field :dst_len, :uchar + field :src_len, :uchar + field :tos, :uchar + field :table, :uchar # table id or RT_TABLE_* + field :protocol, :uchar # RTPROT_* + field :scope, :uchar # RT_SCOPE_* + field :type, :uchar # RTN_* + field :flags, :uint # RTM_F_* + rtattr :dst, RTA_DST, :l3addr + rtattr :src, RTA_SRC, :l3addr + rtattr :iif, RTA_IIF, :uint32 + rtattr :oif, RTA_OIF, :uint32 + rtattr :gateway, RTA_GATEWAY, :l3addr + rtattr :priority, RTA_PRIORITY, :uint32 + rtattr :prefsrc, RTA_PREFSRC, :l3addr + rtattr :metrics, RTA_METRICS, :rtmetrics + rtattr :multipath, RTA_MULTIPATH + rtattr :flow, RTA_FLOW + rtattr :cacheinfo, RTA_CACHEINFO, :rta_cacheinfo + rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places! + end + + # This is the medium and high-level API using a NETLINK_ROUTE protocol socket + class RTSocket < NLSocket + def initialize(opt={}) + super(opt.merge(:protocol => Netlink::NETLINK_ROUTE)) + clear_cache + end + + # Download a list of links (interfaces). Either returns an array of + # Netlink::Link objects, or yields them to the supplied block. + # + # res = nl.link_list + # p res + # [#0, :pad=>0, :type=>772, :index=>1, + # :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0, + # :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...", + # :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00", + # :stats=>#, + # :stats64=>#}>, ...] + def read_links(opt=nil, &blk) + send_request RTM_GETLINK, Link.new(opt), + NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST + receive_until_done(RTM_NEWLINK, &blk) + end + + # Download a list of routes. Either returns an array of + # Netlink::Route objects, or yields them to the supplied block. + # + # A hash of kernel options may be supplied, but you might also have + # to perform your own filtering. e.g. + # rt.read_routes(:family=>Socket::AF_INET) # works + # rt.read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored + # + # res = nl.routes(:family => Socket::AF_INET) + # p res + # [#2, :dst_len=>32, :src_len=>0, :tos=>0, + # :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255, + # :dst=>#, + # :prefsrc=>#, :oif=>1}>, ...] + # + # Note that not all attributes will always be present. In particular, + # a defaultroute (dst_len=0) misses out the dst address completely: + # + # [#2, :dst_len=>0, :src_len=>0, :tos=>0, + # :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254, + # :gateway=>#, :oif=>2}>, ...] + def read_routes(opt=nil, &blk) + send_request RTM_GETROUTE, Route.new(opt), + NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST + receive_until_done(RTM_NEWROUTE, &blk) + end + + # Download a list of link addresses. Either returns an array of + # Netlink::Addr objects, or yields them to the supplied block. + # You will need to use the 'index' to cross reference to the interface. + # + # A hash of kernel options may be supplied, but likely only :family + # is honoured. + # + # res = nl.addrs(:family => Socket::AF_INET) + # p res + # [#2, :prefixlen=>8, :flags=>128, :scope=>254, + # :index=>1, :address=>#, + # :local=>#, :label=>"lo"}>, ...] + def read_addrs(opt=nil, &blk) + send_request RTM_GETADDR, Addr.new(opt), + NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST + receive_until_done(RTM_NEWADDR, &blk) + end + + # Download a list of addresses, grouped as {index=>[addr,addr], index=>[addr,addr]} + def read_addrs_by_ifindex(opt=nil) + res = read_addrs(opt).group_by { |obj| obj.index } + res.default = [].freeze + res + end + + # Clear the memoization cache + def clear_cache + @links = nil + @link = nil + @addrs = nil + @routes = nil + end + + # Return the memoized interface table as a flat array, suitable for + # iteration. e.g. + # rt.links.each { |link| puts link.ifname } + def links + @links ||= read_links + end + + # Return the memoized interface table, keyed by both ifname and ifindex. e.g. + # puts rt.link["eth0"].index + # puts rt.link[1].ifname + def link + @link ||= ( + h = {} + links.each { |link| h[link.index] = h[link.ifname] = link } + h + ) + end + + # Return the memoized address table, keyed by interface name and + # address family, containing an array of addresses for each + # interface/family combination. i.e. + # + # # {ifname=>{family=>[addr,addr,...], ...}, ...} + # puts rt.addrs["eth0"][Socket::AF_INET][0].address + # + # If there are no addresses for a particular family then it will + # return a (frozen) empty array, to make iteration eaiser. + def addrs + @addrs ||= ( + h = {} + links.each do |link| + h[link.ifname] = {} + end + read_addrs.each do |addr| + ifname = link[addr.index].ifname + h[ifname] ||= Hash.new(EMPTY_ARRAY) + (h[ifname][addr.family] ||= []) << addr + end + h + ) + end + + # Return the memoized route table, keyed by address family, containing + # an array of routes for each address family. i.e. + # family combination. i.e. + # + # # {family=>[route,route,...], ...}, ...} + # puts rt.routes[Socket::AF_INET].first.dst + # + # If there are no routes for a particular family then it will + # return a (frozen) empty array. + def routes + @routes ||= ( + h = {} + links.each do |link| + h[link.ifname] = Hash.new(EMPTY_ARRAY) + end + read_routes.each do |route| + (h[route.family] ||= []) << route + end + h + ) + end + end +end diff --git a/lib/netlink/rtsocket.rb b/lib/netlink/rtsocket.rb deleted file mode 100644 index 9779dfd..0000000 --- a/lib/netlink/rtsocket.rb +++ /dev/null @@ -1,166 +0,0 @@ -require 'netlink/nlsocket' -require 'netlink/message' - -module Netlink - # This is the medium and high-level API using a NETLINK_ROUTE protocol socket - class RTSocket < NLSocket - def initialize(opt={}) - super(opt.merge(:protocol => Netlink::NETLINK_ROUTE)) - clear_cache - end - - # Download a list of links (interfaces). Either returns an array of - # Netlink::Link objects, or yields them to the supplied block. - # - # res = nl.link_list - # p res - # [#0, :pad=>0, :type=>772, :index=>1, - # :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0, - # :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...", - # :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00", - # :stats=>#, - # :stats64=>#}>, ...] - def read_links(opt=nil, &blk) - send_request RTM_GETLINK, Link.new(opt), - NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST - receive_until_done(RTM_NEWLINK, &blk) - end - - # Download a list of routes. Either returns an array of - # Netlink::Route objects, or yields them to the supplied block. - # - # A hash of kernel options may be supplied, but you might also have - # to perform your own filtering. e.g. - # rt.read_routes(:family=>Socket::AF_INET) # works - # rt.read_routes(:protocol=>Netlink::RTPROT_STATIC) # ignored - # - # res = nl.routes(:family => Socket::AF_INET) - # p res - # [#2, :dst_len=>32, :src_len=>0, :tos=>0, - # :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255, - # :dst=>#, - # :prefsrc=>#, :oif=>1}>, ...] - # - # Note that not all attributes will always be present. In particular, - # a defaultroute (dst_len=0) misses out the dst address completely: - # - # [#2, :dst_len=>0, :src_len=>0, :tos=>0, - # :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254, - # :gateway=>#, :oif=>2}>, ...] - def read_routes(opt=nil, &blk) - send_request RTM_GETROUTE, Route.new(opt), - NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST - receive_until_done(RTM_NEWROUTE, &blk) - end - - # Download a list of link addresses. Either returns an array of - # Netlink::Addr objects, or yields them to the supplied block. - # You will need to use the 'index' to cross reference to the interface. - # - # A hash of kernel options may be supplied, but likely only :family - # is honoured. - # - # res = nl.addrs(:family => Socket::AF_INET) - # p res - # [#2, :prefixlen=>8, :flags=>128, :scope=>254, - # :index=>1, :address=>#, - # :local=>#, :label=>"lo"}>, ...] - def read_addrs(opt=nil, &blk) - send_request RTM_GETADDR, Addr.new(opt), - NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST - receive_until_done(RTM_NEWADDR, &blk) - end - - # Download a list of addresses, grouped as {index=>[addr,addr], index=>[addr,addr]} - def read_addrs_by_ifindex(opt=nil) - res = read_addrs(opt).group_by { |obj| obj.index } - res.default = [].freeze - res - end - - # Clear the memoization cache - def clear_cache - @links = nil - @addrs = nil - @routes = nil - end - - # Return the memoized interface table, keyed by interface name. e.g. - # puts rt.links["eth0"].type - def links - @links ||= ( - res = {} - read_links.each { |obj| res[obj.ifname] = obj } - res - ) - end - - EMPTY_ARRAY = [].freeze #:nodoc: - - # Return the memoized address table, keyed by interface name and - # address family, containing an array of addresses for each - # interface/family combination. i.e. - # - # # {ifname=>{family=>[addr,addr,...], ...}, ...} - # puts rt.addrs["eth0"][Socket::AF_INET][0].address - # - # If there are no addresses for a particular family then it will - # return a (frozen) empty array, to make iteration eaiser. - def addrs - @addrs ||= ( - h = {} - index_to_link = {} - links.each do |name, link| - h[link.ifname] = {} - index_to_link[link.index] = link - end - read_addrs.each do |addr| - ifname = index_to_link[addr.index].ifname - h[ifname] ||= Hash.new(EMPTY_ARRAY) - (h[ifname][addr.family] ||= []) << addr - end - h - ) - end - - # Return the memoized route table, keyed by output interface name and - # address family, containing an array of routes for each interface/ - # family combination. i.e. - # - # # {ifname=>{family=>[route,route,...], ...}, ...} - # puts rt.routes["eth0"][Socket::AF_INET].first.dst - # - # If there are no routes for a particular family then it will - # return a (frozen) empty array, to make iteration eaiser. - def routes - @routes ||= ( - h = {} - index_to_link = {} - links.each do |name, link| - h[link.ifname] = {} - index_to_link[link.index] = link - end - read_routes.each do |route| - ifname = index_to_link[route.oif].ifname - h[ifname] ||= Hash.new(EMPTY_ARRAY) - (h[ifname][route.family] ||= []) << route - end - h - ) - end - end -end - -if __FILE__ == $0 - require 'pp' - nl = Netlink::RTSocket.new - #puts "*** routes ***" - #pp nl.read_routes(:family => Socket::AF_INET) - #puts "*** links ***" - #pp nl.read_links - #puts "*** addrs ***" - #pp nl.read_addrs(:family => Socket::AF_INET) - pp nl.links["eth0"] - pp nl.addrs["eth0"] - pp nl.routes["eth0"][Socket::AF_INET].min_by { |route| route.dst_len } -end