From e07af9a315d54a3f619b9bba211e6c4480fbfa17 Mon Sep 17 00:00:00 2001 From: Brian Candler Date: Fri, 6 May 2011 09:49:47 +0100 Subject: [PATCH] Reorganise under Linux:: --- README | 5 +++ examples/add_addr.rb | 4 +- examples/add_route.rb | 10 ++--- examples/add_vlan.rb | 6 +-- examples/firewall.rb | 6 +-- examples/nflog.rb | 6 +-- examples/route_hi.rb | 8 ++-- examples/route_lo.rb | 4 +- lib/{netlink => linux}/c_struct.rb | 6 +-- lib/{netlink => linux}/constants.rb | 2 +- lib/linux/error.rb | 15 +++++++ lib/{netlink => linux}/iptables4.rb | 10 ++--- lib/{ => linux}/netlink/firewall.rb | 8 ++-- lib/{ => linux}/netlink/message.rb | 6 ++- lib/{ => linux}/netlink/nflog.rb | 8 ++-- lib/{ => linux}/netlink/nlsocket.rb | 31 +++++--------- lib/{ => linux}/netlink/route.rb | 40 ++++++++++--------- lib/{ => linux}/netlink/route/addr_handler.rb | 12 +++--- lib/{ => linux}/netlink/route/handler.rb | 10 +++-- lib/{ => linux}/netlink/route/link_handler.rb | 32 ++++++++------- .../netlink/route/route_handler.rb | 28 +++++++------ lib/{ => linux}/netlink/route/vlan_handler.rb | 8 ++-- 22 files changed, 146 insertions(+), 119 deletions(-) rename lib/{netlink => linux}/c_struct.rb (98%) rename lib/{netlink => linux}/constants.rb (99%) create mode 100644 lib/linux/error.rb rename lib/{netlink => linux}/iptables4.rb (93%) rename lib/{ => linux}/netlink/firewall.rb (93%) rename lib/{ => linux}/netlink/message.rb (98%) rename lib/{ => linux}/netlink/nflog.rb (89%) rename lib/{ => linux}/netlink/nlsocket.rb (92%) rename lib/{ => linux}/netlink/route.rb (52%) rename lib/{ => linux}/netlink/route/addr_handler.rb (93%) rename lib/{ => linux}/netlink/route/handler.rb (80%) rename lib/{ => linux}/netlink/route/link_handler.rb (89%) rename lib/{ => linux}/netlink/route/route_handler.rb (90%) rename lib/{ => linux}/netlink/route/vlan_handler.rb (92%) diff --git a/README b/README index 8338111..447858f 100644 --- a/README +++ b/README @@ -5,6 +5,11 @@ This library provides an API for using a Linux Netlink socket, for doing things like manipulating IP interfaces, routes and firewall rules programmatically. +Requirements +============ + +ruby 1.9 (tested with ruby 1.9.2), OR ruby 1.8.7 with the ffi library. + Code organisation ================= diff --git a/examples/add_addr.rb b/examples/add_addr.rb index e1907fe..1d3fcf3 100644 --- a/examples/add_addr.rb +++ b/examples/add_addr.rb @@ -1,9 +1,9 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR -require 'netlink/route' +require 'linux/netlink/route' -ip = Netlink::Route::Socket.new +ip = Linux::Netlink::Route::Socket.new puts "\n*** Before adding address" ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address } diff --git a/examples/add_route.rb b/examples/add_route.rb index 4eb8151..b642e2e 100644 --- a/examples/add_route.rb +++ b/examples/add_route.rb @@ -1,11 +1,11 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR -require 'netlink/route' +require 'linux/netlink/route' -ip = Netlink::Route::Socket.new +ip = Linux::Netlink::Route::Socket.new puts "\n*** Before adding route" -ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x } +ip.route.list(:family=>Socket::AF_INET, :table=>Linux::RT_TABLE_MAIN) { |x| p x } puts "\n*** After adding route" begin @@ -13,9 +13,9 @@ begin rescue Errno::EEXIST puts "Already exists" end -ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN) { |x| p x } +ip.route.list(:family=>Socket::AF_INET, :table=>Linux::RT_TABLE_MAIN) { |x| p x } puts "\n*** After deleting route" 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 } +ip.route.list(:family=>Socket::AF_INET, :table=>Linux::RT_TABLE_MAIN) { |x| p x } diff --git a/examples/add_vlan.rb b/examples/add_vlan.rb index 318ea46..aed2a30 100644 --- a/examples/add_vlan.rb +++ b/examples/add_vlan.rb @@ -1,10 +1,10 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR -require 'netlink/route' +require 'linux/netlink/route' require 'pp' -ip = Netlink::Route::Socket.new +ip = Linux::Netlink::Route::Socket.new puts "\n*** Before adding VLAN" pp ip.vlan.list(:link=>"lo").to_a @@ -16,6 +16,6 @@ rescue Errno::EEXIST end pp ip.vlan.list(:link=>"lo").to_a -puts "\n*** After deleting VLANs from lo" +puts "\n*** After deleting VLAN from lo" ip.vlan.delete(:link=>"lo", :vlan_id=>1234) pp ip.vlan.list(:link=>"lo").to_a diff --git a/examples/firewall.rb b/examples/firewall.rb index e074c79..08d2f18 100644 --- a/examples/firewall.rb +++ b/examples/firewall.rb @@ -2,7 +2,7 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR require 'pp' -require 'netlink/firewall' +require 'linux/netlink/firewall' # Example of using Netlink::Firewall to capture all outbound packets # to TCP port 7551. Use "telnet 127.0.0.1 7551" to test. @@ -10,9 +10,9 @@ require 'netlink/firewall' #system("modprobe ip_queue") #system("modprobe iptable_filter") #system("iptables -I OUTPUT -j QUEUE -p tcp --destination-port 7551") -nl = Netlink::Firewall::Socket.new +nl = Linux::Netlink::Firewall::Socket.new nl.set_mode(Netlink::IPQ_COPY_PACKET, 128) nl.dequeue_packets do |pkt| p pkt - Netlink::NF_ACCEPT # Netlink::NF_DROP + Linux::NF_ACCEPT # Linux::NF_DROP end diff --git a/examples/nflog.rb b/examples/nflog.rb index 6a2ff78..41bae63 100644 --- a/examples/nflog.rb +++ b/examples/nflog.rb @@ -2,13 +2,13 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR require 'pp' -require 'netlink/nflog' +require 'linux/netlink/nflog' -# Example of using Netlink::NFLog to capture all outbound packets +# Example of using Linux::Netlink::NFLog to capture all outbound packets # to TCP port 7551. Use "telnet 127.0.0.1 7551" to test. #system("iptables -I OUTPUT -j ULOG --ulog-nlgroup 1 -p tcp --destination-port 7551") -nl = Netlink::NFLog::Socket.new(:group => 1) +nl = Linux::Netlink::NFLog::Socket.new(:group => 1) nl.dequeue_packets do |pkt| p pkt end diff --git a/examples/route_hi.rb b/examples/route_hi.rb index b2afa70..10a547c 100644 --- a/examples/route_hi.rb +++ b/examples/route_hi.rb @@ -2,13 +2,13 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR require 'pp' -require 'netlink/route' +require 'linux/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. -ip = Netlink::Route::Socket.new +ip = Linux::Netlink::Route::Socket.new puts "\nInterface eth0:" pp ip.link["eth0"] @@ -17,10 +17,10 @@ puts "\nAddresses on interface eth0:" pp ip.addr.list(:index=>"eth0").to_a puts "\nAll routes in main routing table:" -pp ip.route.list(:table=>Netlink::RT_TABLE_MAIN).to_a +pp ip.route.list(:table=>Linux::RT_TABLE_MAIN).to_a puts "\nV4 default route is probably:" -pp ip.route.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN). +pp ip.route.list(:family=>Socket::AF_INET, :table=>Linux::RT_TABLE_MAIN). min_by { |route| route.dst_len } puts "\nTraffic to 192.168.1.1 goes out via:" diff --git a/examples/route_lo.rb b/examples/route_lo.rb index 162848a..d6e89ef 100644 --- a/examples/route_lo.rb +++ b/examples/route_lo.rb @@ -2,12 +2,12 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift LIBDIR require 'pp' -require 'netlink/route' +require 'linux/netlink/route' # Example of use of low-level API for NETLINK_ROUTE socket. # Each of these method calls performs a netlink protocol exchange. -ip = Netlink::Route::Socket.new +ip = Linux::Netlink::Route::Socket.new puts "*** links ***" pp ip.link.read_link puts "*** addrs ***" diff --git a/lib/netlink/c_struct.rb b/lib/linux/c_struct.rb similarity index 98% rename from lib/netlink/c_struct.rb rename to lib/linux/c_struct.rb index d1ba3cc..de0e2cd 100644 --- a/lib/netlink/c_struct.rb +++ b/lib/linux/c_struct.rb @@ -1,4 +1,4 @@ -module Netlink +module Linux # This class allows defining of C-style structures, and converting # object instances to and from a packed binary representation. @@ -6,7 +6,7 @@ module Netlink # A new structure is created by subclassing CStruct, and then using the # 'field' metaprogramming macro to define each field: # -# class Foo < CStruct +# class Foo < Linux::CStruct # field :bar, :char # field :baz, :long # @@ -196,4 +196,4 @@ class CStruct (val + (m-1)) & ~(m-1) end end -end # module Netlink +end # module Linux diff --git a/lib/netlink/constants.rb b/lib/linux/constants.rb similarity index 99% rename from lib/netlink/constants.rb rename to lib/linux/constants.rb index 10197c3..0e777c5 100644 --- a/lib/netlink/constants.rb +++ b/lib/linux/constants.rb @@ -7,7 +7,7 @@ class Socket IPPROTO_IPV6 = 41 unless defined? Socket::IPPROTO_IPV6 end -module Netlink +module Linux # From linux/netlink.h NETLINK_ROUTE = 0 NETLINK_UNUSED = 1 diff --git a/lib/linux/error.rb b/lib/linux/error.rb new file mode 100644 index 0000000..4b04bd8 --- /dev/null +++ b/lib/linux/error.rb @@ -0,0 +1,15 @@ +module Linux + ERRNO_MAP = {} #:nodoc: + Errno.constants.each do |k| + klass = Errno.const_get(k) + next unless klass.is_a?(Class) and Class.const_defined?(:Errno) + ERRNO_MAP[klass::Errno] = klass + end + + # Raise an Errno exception if the given rc is negative + def self.check_error(rc) + if rc < 0 + raise ERRNO_MAP[-rc] || "System error #{-rc}" + end + end +end diff --git a/lib/netlink/iptables4.rb b/lib/linux/iptables4.rb similarity index 93% rename from lib/netlink/iptables4.rb rename to lib/linux/iptables4.rb index 475e2bc..1b00bce 100644 --- a/lib/netlink/iptables4.rb +++ b/lib/linux/iptables4.rb @@ -1,9 +1,9 @@ require 'socket' -require 'netlink/c_struct' -require 'netlink/message' # just for :dev_name type -require 'netlink/constants' +require 'linux/constants' +require 'linux/c_struct' +require 'linux/netlink/message' # just for :dev_name type -module Netlink +module Linux #- # Definitions mainly from linux/netfilter_ipv4/ip_tables.h #+ @@ -72,5 +72,5 @@ module Netlink end if __FILE__ == $0 - iptables = Netlink::Iptables4.new + iptables = Linux::Iptables4.new end diff --git a/lib/netlink/firewall.rb b/lib/linux/netlink/firewall.rb similarity index 93% rename from lib/netlink/firewall.rb rename to lib/linux/netlink/firewall.rb index 88a225e..1f17d7c 100644 --- a/lib/netlink/firewall.rb +++ b/lib/linux/netlink/firewall.rb @@ -3,9 +3,10 @@ # # TODO: implement multiple queue support (NFQUEUE) -require 'netlink/nlsocket' -require 'netlink/message' +require 'linux/netlink/nlsocket' +require 'linux/netlink/message' +module Linux module Netlink # struct ipq_packet_msg class IPQPacket < Message @@ -54,7 +55,7 @@ module Netlink module Firewall class Socket < NLSocket def initialize(opt={}) - super(opt.merge(:protocol => Netlink::NETLINK_FIREWALL)) + super(opt.merge(:protocol => Linux::NETLINK_FIREWALL)) end # Set mode to IPQ_COPY_META to receive metadata only, IPQ_COPY_PACKET @@ -79,3 +80,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/message.rb b/lib/linux/netlink/message.rb similarity index 98% rename from lib/netlink/message.rb rename to lib/linux/netlink/message.rb index 8e6ae0e..c45609a 100644 --- a/lib/netlink/message.rb +++ b/lib/linux/netlink/message.rb @@ -1,7 +1,8 @@ -require 'netlink/c_struct' -require 'netlink/constants' +require 'linux/c_struct' +require 'linux/constants' require 'ipaddr' +module Linux module Netlink NLMSGHDR_PACK = "LSSLL".freeze # :nodoc: NLMSGHDR_SIZE = [0,0,0,0,0].pack(NLMSGHDR_PACK).bytesize # :nodoc: @@ -220,3 +221,4 @@ module Netlink field :msg_pid, :uint32 end end +end # module Linux diff --git a/lib/netlink/nflog.rb b/lib/linux/netlink/nflog.rb similarity index 89% rename from lib/netlink/nflog.rb rename to lib/linux/netlink/nflog.rb index bd52ecc..2197bad 100644 --- a/lib/netlink/nflog.rb +++ b/lib/linux/netlink/nflog.rb @@ -1,6 +1,7 @@ -require 'netlink/message' -require 'netlink/nlsocket' +require 'linux/netlink/message' +require 'linux/netlink/nlsocket' +module Linux module Netlink ULOG_NL_EVENT = 111 # from ipv4/netfilter/ipt_ULOG.c @@ -34,7 +35,7 @@ module Netlink unless opt[:groups] opt[:groups] = 1 << (opt.fetch(:group) - 1) end - super(opt.merge(:protocol => Netlink::NETLINK_NFLOG)) + super(opt.merge(:protocol => NETLINK_NFLOG)) end # Receive packets and yield them to the block @@ -44,3 +45,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/nlsocket.rb b/lib/linux/netlink/nlsocket.rb similarity index 92% rename from lib/netlink/nlsocket.rb rename to lib/linux/netlink/nlsocket.rb index 0489cc4..a31d7ed 100644 --- a/lib/netlink/nlsocket.rb +++ b/lib/linux/netlink/nlsocket.rb @@ -1,22 +1,10 @@ require 'socket' -require 'netlink/constants' -require 'netlink/message' +require 'linux/constants' +require 'linux/error' +require 'linux/netlink/message' +module Linux module Netlink - ERRNO_MAP = {} #:nodoc: - Errno.constants.each do |k| - klass = Errno.const_get(k) - next unless klass.is_a?(Class) and Class.const_defined?(:Errno) - ERRNO_MAP[klass::Errno] = klass - end - - # Raise an Errno exception if the given rc is not NOERROR - def self.check_error(rc) - if -rc != Errno::NOERROR::Errno - raise ERRNO_MAP[-rc] || "Netlink Error: #{msg.inspect}" - end - end - # NLSocket provides low-level sending and receiving of messages across # a netlink socket, adding headers to sent messages and parsing # received messages. @@ -48,9 +36,9 @@ module Netlink attr_accessor :junk_handler # proc to log or handle unexpected messages # Create a new Netlink socket. Pass in chosen protocol: - # :protocol => Netlink::NETLINK_ARPD - # :protocol => Netlink::NETLINK_FIREWALL - # :protocol => Netlink::NETLINK_ROUTE + # :protocol => Linux::NETLINK_ARPD + # :protocol => Linux::NETLINK_FIREWALL + # :protocol => Linux::NETLINK_ROUTE # etc. Other options: # :groups => N (subscribe to multicast groups, default to 0) # :seq => N (override initial sequence number) @@ -190,7 +178,7 @@ module Netlink loop do parse_yield(recvmsg(timeout)) do |type, flags, seq, pid, msg| if !check_pid_seq || (pid == @pid && seq == @seq) - Netlink.check_error(msg.error) if type == NLMSG_ERROR + Linux.check_error(msg.error) if type == NLMSG_ERROR res = yield type, msg next unless res == false end @@ -244,8 +232,9 @@ module Netlink STDERR.puts " data=#{data.inspect}" if $DEBUG && !data.empty? yield type, flags, seq, pid, data ptr = ptr + Message.nlmsg_align(len) - break unless flags & Netlink::NLM_F_MULTI + break unless flags & Linux::NLM_F_MULTI end end end end +end # module Linux diff --git a/lib/netlink/route.rb b/lib/linux/netlink/route.rb similarity index 52% rename from lib/netlink/route.rb rename to lib/linux/netlink/route.rb index 9042ffb..f50ee22 100644 --- a/lib/netlink/route.rb +++ b/lib/linux/netlink/route.rb @@ -3,49 +3,50 @@ # versa, the logic is delegated to separate classes for each entity # (links, addresses etc) -require 'netlink/nlsocket' -require 'netlink/message' +require 'linux/netlink/nlsocket' +require 'linux/netlink/message' +module Linux module Netlink module Route - autoload :LinkHandler, 'netlink/route/link_handler' - autoload :VlanHandler, 'netlink/route/vlan_handler' - autoload :AddrHandler, 'netlink/route/addr_handler' - autoload :RouteHandler, 'netlink/route/route_handler' + autoload :LinkHandler, 'linux/netlink/route/link_handler' + autoload :VlanHandler, 'linux/netlink/route/vlan_handler' + autoload :AddrHandler, 'linux/netlink/route/addr_handler' + autoload :RouteHandler, 'linux/netlink/route/route_handler' # This class formats and receives messages using NETLINK_ROUTE protocol class Socket < NLSocket def initialize(opt={}) - super(opt.merge(:protocol => Netlink::NETLINK_ROUTE)) + super(opt.merge(:protocol => Linux::NETLINK_ROUTE)) end - # Return a Netlink::Route::LinkHandler object for manipulating links + # Return a LinkHandler object for manipulating links def link - @link ||= Netlink::Route::LinkHandler.new(self) + @link ||= LinkHandler.new(self) end - # Return a Netlink::Route::VlanHandler object for manipulating vlans + # Return a VlanHandler object for manipulating vlans def vlan - @vlan ||= Netlink::Route::VlanHandler.new(self) + @vlan ||= VlanHandler.new(self) end - # Return a Netlink::Route::AddrHandler object for manipulating addresses + # Return a AddrHandler object for manipulating addresses def addr - @addr ||= Netlink::Route::AddrHandler.new(self) + @addr ||= AddrHandler.new(self) end - # Return a Netlink::Route::RT object for manipulating routes + # Return a RT object for manipulating routes def route - @route ||= Netlink::Route::RouteHandler.new(self) + @route ||= RouteHandler.new(self) end # Convert an interface index into name string, or nil if the # index is nil or 0. Raises exception for unknown values. # - # 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)}" + # ip = Linux::Netlink::Route::Socket.new + # ip.route(:family=>Socket::AF_INET) do |route| + # puts "iif=#{ip.ifname(route.iif)}" + # puts "oif=#{ip.ifname(route.oif)}" # end def ifname(index) return nil if index.nil? || index == 0 @@ -67,3 +68,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/route/addr_handler.rb b/lib/linux/netlink/route/addr_handler.rb similarity index 93% rename from lib/netlink/route/addr_handler.rb rename to lib/linux/netlink/route/addr_handler.rb index c50f6c5..792d794 100644 --- a/lib/netlink/route/addr_handler.rb +++ b/lib/linux/netlink/route/addr_handler.rb @@ -1,6 +1,7 @@ -require 'netlink/route' -require 'netlink/route/handler' +require 'linux/netlink/route' +require 'linux/netlink/route/handler' +module Linux module Netlink # struct ifa_cacheinfo IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp @@ -41,7 +42,7 @@ module Netlink # # res = nl.read_addr(:family => Socket::AF_INET) # p res - # [#2, :prefixlen=>8, :flags=>128, :scope=>254, + # [#2, :prefixlen=>8, :flags=>128, :scope=>254, # :index=>1, :address=>#, # :local=>#, :label=>"lo"}>, ...] def read_addr(opt=nil, &blk) @@ -70,7 +71,7 @@ module Netlink def list(filter=nil, &blk) @addr ||= read_addr filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index) - do_list(@addr, filter, &blk) + filter_list(@addr, filter, &blk) end alias :each :list @@ -90,7 +91,7 @@ module Netlink # Add an IP address to an interface # # require 'netlink/route' - # ip = Netlink::Route::Socket.new + # ip = Linux::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) @@ -122,3 +123,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/route/handler.rb b/lib/linux/netlink/route/handler.rb similarity index 80% rename from lib/netlink/route/handler.rb rename to lib/linux/netlink/route/handler.rb index 79c0fc8..307e772 100644 --- a/lib/netlink/route/handler.rb +++ b/lib/linux/netlink/route/handler.rb @@ -1,7 +1,8 @@ +module Linux module Netlink module Route - # This class allows objects to be created representing the - # conditions given to the 'list' method + # The base class containing shared methods between all the + # NETLINK_ROUTE handler classes. class BaseFilter #:nodoc: def self.filter name, &blk define_method "#{name}=" do |matchval| @@ -21,7 +22,7 @@ module Netlink # Code which is common to all the NETLINK_ROUTE handlers class Handler - def initialize(rtsocket = Netlink::Route::Socket.new) + def initialize(rtsocket = Route::Socket.new) @rtsocket = rtsocket clear_cache end @@ -35,7 +36,7 @@ module Netlink end # Generic listing and filtering - def do_list(data, filter, &blk) + def filter_list(data, filter, &blk) return data.each(&blk) unless filter return to_enum(:list, filter) unless block_given? fm = self.class::Filter.new(filter) @@ -44,3 +45,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/route/link_handler.rb b/lib/linux/netlink/route/link_handler.rb similarity index 89% rename from lib/netlink/route/link_handler.rb rename to lib/linux/netlink/route/link_handler.rb index 62a9603..955fa4f 100644 --- a/lib/netlink/route/link_handler.rb +++ b/lib/linux/netlink/route/link_handler.rb @@ -1,6 +1,7 @@ -require 'netlink/route' -require 'netlink/route/handler' +require 'linux/netlink/route' +require 'linux/netlink/route/handler' +module Linux module Netlink # struct rtnl_link_stats / rtnl_link_stats64 LinkStats = Struct.new :rx_packets, :tx_packets, @@ -131,12 +132,12 @@ module Netlink # # res = ip.link.read_link # p res - # [#0, :type=>772, :index=>1, + # [#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", - # :stats32=>#, - # :stats64=>#}>, ...] + # :stats32=>#, + # :stats64=>#}>, ...] def read_link(opt=nil, &blk) @rtsocket.send_request RTM_GETLINK, IFInfo.new(opt), NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST @@ -158,15 +159,15 @@ module Netlink # it is efficient to call this method multiple times. # # ip.link.list { |x| p x } - # ethers = ip.link.list(:type => Netlink::ARPHRD_ETHER).to_a + # ethers = ip.link.list(:type => Linux::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(:flags => Linux::IFF_RUNNING) + # ip.link.list(:noflags => Linux::IFF_POINTOPOINT) # ip.link.list(:link => "lo") # vlan etc attached to this interface def list(filter=nil, &blk) @link ||= read_link filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link) - do_list(@link, filter, &blk) + filter_list(@link, filter, &blk) end alias :each :list @@ -187,16 +188,16 @@ module Netlink # Add an interface (raw). e.g. # - # require 'netlink/route' - # ip = Netlink::Route::Socket.new + # require 'linux/netlink/route' + # ip = Linux::Netlink::Route::Socket.new # ip.link.add( # :link=>"lo", - # :linkinfo=>Netlink::LinkInfo.new( + # :linkinfo=>Linux::Netlink::LinkInfo.new( # :kind=>"vlan", - # :data=>Netlink::VlanInfo.new( + # :data=>Linux::Netlink::VlanInfo.new( # :id=>1234, - # :flags => Netlink::VlanFlags.new( - # :flags=>Netlink::VLAN_FLAG_LOOSE_BINDING, + # :flags => Linux::Netlink::VlanFlags.new( + # :flags=>Linux::VLAN_FLAG_LOOSE_BINDING, # :mask=>0xffffffff # )))) @@ -243,3 +244,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/route/route_handler.rb b/lib/linux/netlink/route/route_handler.rb similarity index 90% rename from lib/netlink/route/route_handler.rb rename to lib/linux/netlink/route/route_handler.rb index 237e707..0992311 100644 --- a/lib/netlink/route/route_handler.rb +++ b/lib/linux/netlink/route/route_handler.rb @@ -1,6 +1,7 @@ -require 'netlink/route' -require 'netlink/route/handler' +require 'linux/netlink/route' +require 'linux/netlink/route/handler' +module Linux module Netlink # struct rta_cacheinfo RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage @@ -64,12 +65,12 @@ module Netlink # # A hash of kernel options may be supplied, but you might also have # to perform your own filtering. e.g. - # read_route(:family=>Socket::AF_INET) # works - # read_route(:protocol=>Netlink::RTPROT_STATIC) # ignored + # read_route(:family=>Socket::AF_INET) # works + # read_route(:protocol=>Linux::RTPROT_STATIC) # ignored # # res = ip.route.read_route(:family => Socket::AF_INET) # p res - # [#2, :dst_len=>32, :src_len=>0, :tos=>0, + # [#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}>, ...] @@ -77,7 +78,7 @@ module Netlink # 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, + # [#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_route(opt=nil, &blk) @@ -101,19 +102,19 @@ module Netlink # Return the memoized route table, filtered according to # the optional criteria. Examples: # :family => Socket::AF_INET - # :table => Netlink::RT_TABLE_DEFAULT - # :protocol => Netlink::RTPROT_STATIC - # :type => Netlink::RTN_UNICAST - # :scope => Netlink::RT_SCOPE_HOST - # :flags => Netlink::RTM_F_NOTIFY - # :noflags => Netlink::RTM_F_CLONED + # :table => Linux::RT_TABLE_DEFAULT + # :protocol => Linux::RTPROT_STATIC + # :type => Linux::RTN_UNICAST + # :scope => Linux::RT_SCOPE_HOST + # :flags => Linux::RTM_F_NOTIFY + # :noflags => Linux::RTM_F_CLONED # :oif => "eth0" # :iif => "eth1" def list(filter=nil, &blk) @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(@route, filter, &blk) + filter_list(@route, filter, &blk) end alias :each :list @@ -195,3 +196,4 @@ module Netlink end end end +end # module Linux diff --git a/lib/netlink/route/vlan_handler.rb b/lib/linux/netlink/route/vlan_handler.rb similarity index 92% rename from lib/netlink/route/vlan_handler.rb rename to lib/linux/netlink/route/vlan_handler.rb index 58dbe54..f3105f3 100644 --- a/lib/netlink/route/vlan_handler.rb +++ b/lib/linux/netlink/route/vlan_handler.rb @@ -1,6 +1,7 @@ -require 'netlink/route' -require 'netlink/route/handler' +require 'linux/netlink/route' +require 'linux/netlink/route/handler' +module Linux module Netlink module Route class VlanHandler < Handler @@ -17,7 +18,7 @@ module Netlink # nl.vlans.add( # :link=>"lo", # :vlan_id=>1234, - # :vlan_flags=>Netlink::VLAN_FLAG_LOOSE_BINDING, + # :vlan_flags=>Linux::VLAN_FLAG_LOOSE_BINDING, # :vlan_mask=>0xffffffff # ) def add(opt) @@ -63,3 +64,4 @@ module Netlink end end end +end # module Linux