Reorganise under Linux::

This commit is contained in:
Brian Candler
2011-05-06 09:49:47 +01:00
parent f1c29980a8
commit e07af9a315
22 changed files with 146 additions and 119 deletions

5
README
View File

@@ -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 things like manipulating IP interfaces, routes and firewall rules
programmatically. programmatically.
Requirements
============
ruby 1.9 (tested with ruby 1.9.2), OR ruby 1.8.7 with the ffi library.
Code organisation Code organisation
================= =================

View File

@@ -1,9 +1,9 @@
LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $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" puts "\n*** Before adding address"
ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address } ip.addr.list(:index=>"lo", :family=>Socket::AF_INET) { |x| puts x.address }

View File

@@ -1,11 +1,11 @@
LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $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" 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" puts "\n*** After adding route"
begin begin
@@ -13,9 +13,9 @@ begin
rescue Errno::EEXIST rescue Errno::EEXIST
puts "Already exists" puts "Already exists"
end 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" 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.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 }

View File

@@ -1,10 +1,10 @@
LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $LOAD_PATH.unshift LIBDIR
require 'netlink/route' require 'linux/netlink/route'
require 'pp' require 'pp'
ip = Netlink::Route::Socket.new ip = Linux::Netlink::Route::Socket.new
puts "\n*** Before adding VLAN" puts "\n*** Before adding VLAN"
pp ip.vlan.list(:link=>"lo").to_a pp ip.vlan.list(:link=>"lo").to_a
@@ -16,6 +16,6 @@ rescue Errno::EEXIST
end end
pp ip.vlan.list(:link=>"lo").to_a 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) ip.vlan.delete(:link=>"lo", :vlan_id=>1234)
pp ip.vlan.list(:link=>"lo").to_a pp ip.vlan.list(:link=>"lo").to_a

View File

@@ -2,7 +2,7 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $LOAD_PATH.unshift LIBDIR
require 'pp' require 'pp'
require 'netlink/firewall' require 'linux/netlink/firewall'
# Example of using Netlink::Firewall to capture all outbound packets # Example of using Netlink::Firewall to capture all outbound packets
# to TCP port 7551. Use "telnet 127.0.0.1 7551" to test. # 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 ip_queue")
#system("modprobe iptable_filter") #system("modprobe iptable_filter")
#system("iptables -I OUTPUT -j QUEUE -p tcp --destination-port 7551") #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.set_mode(Netlink::IPQ_COPY_PACKET, 128)
nl.dequeue_packets do |pkt| nl.dequeue_packets do |pkt|
p pkt p pkt
Netlink::NF_ACCEPT # Netlink::NF_DROP Linux::NF_ACCEPT # Linux::NF_DROP
end end

View File

@@ -2,13 +2,13 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $LOAD_PATH.unshift LIBDIR
require 'pp' 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. # 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") #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| nl.dequeue_packets do |pkt|
p pkt p pkt
end end

View File

@@ -2,13 +2,13 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $LOAD_PATH.unshift LIBDIR
require 'pp' require 'pp'
require 'netlink/route' require 'linux/netlink/route'
# Example of use of high-level API for NETLINK_ROUTE socket. # Example of use of high-level API for NETLINK_ROUTE socket.
# The data is memoized - that is, it's downloaded from the kernel once # The data is memoized - that is, it's downloaded from the kernel once
# and then manipulated internally. # and then manipulated internally.
ip = Netlink::Route::Socket.new ip = Linux::Netlink::Route::Socket.new
puts "\nInterface eth0:" puts "\nInterface eth0:"
pp ip.link["eth0"] pp ip.link["eth0"]
@@ -17,10 +17,10 @@ puts "\nAddresses on interface eth0:"
pp ip.addr.list(:index=>"eth0").to_a pp ip.addr.list(:index=>"eth0").to_a
puts "\nAll routes in main routing table:" 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:" 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 } min_by { |route| route.dst_len }
puts "\nTraffic to 192.168.1.1 goes out via:" puts "\nTraffic to 192.168.1.1 goes out via:"

View File

@@ -2,12 +2,12 @@ LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift LIBDIR $LOAD_PATH.unshift LIBDIR
require 'pp' require 'pp'
require 'netlink/route' require 'linux/netlink/route'
# Example of use of low-level API for NETLINK_ROUTE socket. # Example of use of low-level API for NETLINK_ROUTE socket.
# Each of these method calls performs a netlink protocol exchange. # Each of these method calls performs a netlink protocol exchange.
ip = Netlink::Route::Socket.new ip = Linux::Netlink::Route::Socket.new
puts "*** links ***" puts "*** links ***"
pp ip.link.read_link pp ip.link.read_link
puts "*** addrs ***" puts "*** addrs ***"

View File

@@ -1,4 +1,4 @@
module Netlink module Linux
# This class allows defining of C-style structures, and converting # This class allows defining of C-style structures, and converting
# object instances to and from a packed binary representation. # 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 # A new structure is created by subclassing CStruct, and then using the
# 'field' metaprogramming macro to define each field: # 'field' metaprogramming macro to define each field:
# #
# class Foo < CStruct # class Foo < Linux::CStruct
# field :bar, :char # field :bar, :char
# field :baz, :long # field :baz, :long
# #
@@ -196,4 +196,4 @@ class CStruct
(val + (m-1)) & ~(m-1) (val + (m-1)) & ~(m-1)
end end
end end
end # module Netlink end # module Linux

View File

@@ -7,7 +7,7 @@ class Socket
IPPROTO_IPV6 = 41 unless defined? Socket::IPPROTO_IPV6 IPPROTO_IPV6 = 41 unless defined? Socket::IPPROTO_IPV6
end end
module Netlink module Linux
# From linux/netlink.h # From linux/netlink.h
NETLINK_ROUTE = 0 NETLINK_ROUTE = 0
NETLINK_UNUSED = 1 NETLINK_UNUSED = 1

15
lib/linux/error.rb Normal file
View File

@@ -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

View File

@@ -1,9 +1,9 @@
require 'socket' require 'socket'
require 'netlink/c_struct' require 'linux/constants'
require 'netlink/message' # just for :dev_name type require 'linux/c_struct'
require 'netlink/constants' require 'linux/netlink/message' # just for :dev_name type
module Netlink module Linux
#- #-
# Definitions mainly from linux/netfilter_ipv4/ip_tables.h # Definitions mainly from linux/netfilter_ipv4/ip_tables.h
#+ #+
@@ -72,5 +72,5 @@ module Netlink
end end
if __FILE__ == $0 if __FILE__ == $0
iptables = Netlink::Iptables4.new iptables = Linux::Iptables4.new
end end

View File

@@ -3,9 +3,10 @@
# #
# TODO: implement multiple queue support (NFQUEUE) # TODO: implement multiple queue support (NFQUEUE)
require 'netlink/nlsocket' require 'linux/netlink/nlsocket'
require 'netlink/message' require 'linux/netlink/message'
module Linux
module Netlink module Netlink
# struct ipq_packet_msg # struct ipq_packet_msg
class IPQPacket < Message class IPQPacket < Message
@@ -54,7 +55,7 @@ module Netlink
module Firewall module Firewall
class Socket < NLSocket class Socket < NLSocket
def initialize(opt={}) def initialize(opt={})
super(opt.merge(:protocol => Netlink::NETLINK_FIREWALL)) super(opt.merge(:protocol => Linux::NETLINK_FIREWALL))
end end
# Set mode to IPQ_COPY_META to receive metadata only, IPQ_COPY_PACKET # Set mode to IPQ_COPY_META to receive metadata only, IPQ_COPY_PACKET
@@ -79,3 +80,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,7 +1,8 @@
require 'netlink/c_struct' require 'linux/c_struct'
require 'netlink/constants' require 'linux/constants'
require 'ipaddr' require 'ipaddr'
module Linux
module Netlink module Netlink
NLMSGHDR_PACK = "LSSLL".freeze # :nodoc: NLMSGHDR_PACK = "LSSLL".freeze # :nodoc:
NLMSGHDR_SIZE = [0,0,0,0,0].pack(NLMSGHDR_PACK).bytesize # :nodoc: NLMSGHDR_SIZE = [0,0,0,0,0].pack(NLMSGHDR_PACK).bytesize # :nodoc:
@@ -220,3 +221,4 @@ module Netlink
field :msg_pid, :uint32 field :msg_pid, :uint32
end end
end end
end # module Linux

View File

@@ -1,6 +1,7 @@
require 'netlink/message' require 'linux/netlink/message'
require 'netlink/nlsocket' require 'linux/netlink/nlsocket'
module Linux
module Netlink module Netlink
ULOG_NL_EVENT = 111 # from ipv4/netfilter/ipt_ULOG.c ULOG_NL_EVENT = 111 # from ipv4/netfilter/ipt_ULOG.c
@@ -34,7 +35,7 @@ module Netlink
unless opt[:groups] unless opt[:groups]
opt[:groups] = 1 << (opt.fetch(:group) - 1) opt[:groups] = 1 << (opt.fetch(:group) - 1)
end end
super(opt.merge(:protocol => Netlink::NETLINK_NFLOG)) super(opt.merge(:protocol => NETLINK_NFLOG))
end end
# Receive packets and yield them to the block # Receive packets and yield them to the block
@@ -44,3 +45,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,22 +1,10 @@
require 'socket' require 'socket'
require 'netlink/constants' require 'linux/constants'
require 'netlink/message' require 'linux/error'
require 'linux/netlink/message'
module Linux
module Netlink 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 # NLSocket provides low-level sending and receiving of messages across
# a netlink socket, adding headers to sent messages and parsing # a netlink socket, adding headers to sent messages and parsing
# received messages. # received messages.
@@ -48,9 +36,9 @@ module Netlink
attr_accessor :junk_handler # proc to log or handle unexpected messages attr_accessor :junk_handler # proc to log or handle unexpected messages
# Create a new Netlink socket. Pass in chosen protocol: # Create a new Netlink socket. Pass in chosen protocol:
# :protocol => Netlink::NETLINK_ARPD # :protocol => Linux::NETLINK_ARPD
# :protocol => Netlink::NETLINK_FIREWALL # :protocol => Linux::NETLINK_FIREWALL
# :protocol => Netlink::NETLINK_ROUTE # :protocol => Linux::NETLINK_ROUTE
# etc. Other options: # etc. Other options:
# :groups => N (subscribe to multicast groups, default to 0) # :groups => N (subscribe to multicast groups, default to 0)
# :seq => N (override initial sequence number) # :seq => N (override initial sequence number)
@@ -190,7 +178,7 @@ module Netlink
loop do loop do
parse_yield(recvmsg(timeout)) do |type, flags, seq, pid, msg| parse_yield(recvmsg(timeout)) do |type, flags, seq, pid, msg|
if !check_pid_seq || (pid == @pid && seq == @seq) 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 res = yield type, msg
next unless res == false next unless res == false
end end
@@ -244,8 +232,9 @@ module Netlink
STDERR.puts " data=#{data.inspect}" if $DEBUG && !data.empty? STDERR.puts " data=#{data.inspect}" if $DEBUG && !data.empty?
yield type, flags, seq, pid, data yield type, flags, seq, pid, data
ptr = ptr + Message.nlmsg_align(len) 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 end
end end
end # module Linux

View File

@@ -3,49 +3,50 @@
# versa, the logic is delegated to separate classes for each entity # versa, the logic is delegated to separate classes for each entity
# (links, addresses etc) # (links, addresses etc)
require 'netlink/nlsocket' require 'linux/netlink/nlsocket'
require 'netlink/message' require 'linux/netlink/message'
module Linux
module Netlink module Netlink
module Route module Route
autoload :LinkHandler, 'netlink/route/link_handler' autoload :LinkHandler, 'linux/netlink/route/link_handler'
autoload :VlanHandler, 'netlink/route/vlan_handler' autoload :VlanHandler, 'linux/netlink/route/vlan_handler'
autoload :AddrHandler, 'netlink/route/addr_handler' autoload :AddrHandler, 'linux/netlink/route/addr_handler'
autoload :RouteHandler, 'netlink/route/route_handler' autoload :RouteHandler, 'linux/netlink/route/route_handler'
# This class formats and receives messages using NETLINK_ROUTE protocol # This class formats and receives messages using NETLINK_ROUTE protocol
class Socket < NLSocket class Socket < NLSocket
def initialize(opt={}) def initialize(opt={})
super(opt.merge(:protocol => Netlink::NETLINK_ROUTE)) super(opt.merge(:protocol => Linux::NETLINK_ROUTE))
end end
# Return a Netlink::Route::LinkHandler object for manipulating links # Return a LinkHandler object for manipulating links
def link def link
@link ||= Netlink::Route::LinkHandler.new(self) @link ||= LinkHandler.new(self)
end end
# Return a Netlink::Route::VlanHandler object for manipulating vlans # Return a VlanHandler object for manipulating vlans
def vlan def vlan
@vlan ||= Netlink::Route::VlanHandler.new(self) @vlan ||= VlanHandler.new(self)
end end
# Return a Netlink::Route::AddrHandler object for manipulating addresses # Return a AddrHandler object for manipulating addresses
def addr def addr
@addr ||= Netlink::Route::AddrHandler.new(self) @addr ||= AddrHandler.new(self)
end end
# Return a Netlink::Route::RT object for manipulating routes # Return a RT object for manipulating routes
def route def route
@route ||= Netlink::Route::RouteHandler.new(self) @route ||= RouteHandler.new(self)
end end
# Convert an interface index into name string, or nil if the # Convert an interface index into name string, or nil if the
# index is nil or 0. Raises exception for unknown values. # index is nil or 0. Raises exception for unknown values.
# #
# nl = Netlink::Route::Socket.new # ip = Linux::Netlink::Route::Socket.new
# nl.routes(:family=>Socket::AF_INET) do |route| # ip.route(:family=>Socket::AF_INET) do |route|
# puts "iif=#{nl.ifname(route.iif)}" # puts "iif=#{ip.ifname(route.iif)}"
# puts "oif=#{nl.ifname(route.oif)}" # puts "oif=#{ip.ifname(route.oif)}"
# end # end
def ifname(index) def ifname(index)
return nil if index.nil? || index == 0 return nil if index.nil? || index == 0
@@ -67,3 +68,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,6 +1,7 @@
require 'netlink/route' require 'linux/netlink/route'
require 'netlink/route/handler' require 'linux/netlink/route/handler'
module Linux
module Netlink module Netlink
# struct ifa_cacheinfo # struct ifa_cacheinfo
IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp
@@ -41,7 +42,7 @@ module Netlink
# #
# res = nl.read_addr(:family => Socket::AF_INET) # res = nl.read_addr(:family => Socket::AF_INET)
# p res # p res
# [#<Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254, # [#<Linux::Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254,
# :index=>1, :address=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, # :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"}>, ...] # :local=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :label=>"lo"}>, ...]
def read_addr(opt=nil, &blk) def read_addr(opt=nil, &blk)
@@ -70,7 +71,7 @@ module Netlink
def list(filter=nil, &blk) def list(filter=nil, &blk)
@addr ||= read_addr @addr ||= read_addr
filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index) filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index)
do_list(@addr, filter, &blk) filter_list(@addr, filter, &blk)
end end
alias :each :list alias :each :list
@@ -90,7 +91,7 @@ module Netlink
# Add an IP address to an interface # Add an IP address to an interface
# #
# require 'netlink/route' # 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) # ip.addr.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24)
def add(opt) def add(opt)
ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt) ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt)
@@ -122,3 +123,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,7 +1,8 @@
module Linux
module Netlink module Netlink
module Route module Route
# This class allows objects to be created representing the # The base class containing shared methods between all the
# conditions given to the 'list' method # NETLINK_ROUTE handler classes.
class BaseFilter #:nodoc: class BaseFilter #:nodoc:
def self.filter name, &blk def self.filter name, &blk
define_method "#{name}=" do |matchval| define_method "#{name}=" do |matchval|
@@ -21,7 +22,7 @@ module Netlink
# Code which is common to all the NETLINK_ROUTE handlers # Code which is common to all the NETLINK_ROUTE handlers
class Handler class Handler
def initialize(rtsocket = Netlink::Route::Socket.new) def initialize(rtsocket = Route::Socket.new)
@rtsocket = rtsocket @rtsocket = rtsocket
clear_cache clear_cache
end end
@@ -35,7 +36,7 @@ module Netlink
end end
# Generic listing and filtering # Generic listing and filtering
def do_list(data, filter, &blk) def filter_list(data, filter, &blk)
return data.each(&blk) unless filter return data.each(&blk) unless filter
return to_enum(:list, filter) unless block_given? return to_enum(:list, filter) unless block_given?
fm = self.class::Filter.new(filter) fm = self.class::Filter.new(filter)
@@ -44,3 +45,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,6 +1,7 @@
require 'netlink/route' require 'linux/netlink/route'
require 'netlink/route/handler' require 'linux/netlink/route/handler'
module Linux
module Netlink module Netlink
# struct rtnl_link_stats / rtnl_link_stats64 # struct rtnl_link_stats / rtnl_link_stats64
LinkStats = Struct.new :rx_packets, :tx_packets, LinkStats = Struct.new :rx_packets, :tx_packets,
@@ -131,12 +132,12 @@ module Netlink
# #
# res = ip.link.read_link # res = ip.link.read_link
# p res # p res
# [#<Netlink::IFInfo {:family=>0, :type=>772, :index=>1, # [#<Linux::Netlink::IFInfo {:family=>0, :type=>772, :index=>1,
# :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0, # :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0,
# :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...", # :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...",
# :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00", # :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00",
# :stats32=>#<struct Netlink::LinkStats rx_packets=22, ...>, # :stats32=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>,
# :stats64=>#<struct Netlink::LinkStats rx_packets=22, ...>}>, ...] # :stats64=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>}>, ...]
def read_link(opt=nil, &blk) def read_link(opt=nil, &blk)
@rtsocket.send_request RTM_GETLINK, IFInfo.new(opt), @rtsocket.send_request RTM_GETLINK, IFInfo.new(opt),
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
@@ -158,15 +159,15 @@ module Netlink
# it is efficient to call this method multiple times. # it is efficient to call this method multiple times.
# #
# ip.link.list { |x| p x } # 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 # vlans = ip.link.list(:kind => "vlan").to_a
# ip.link.list(:flags => Netlink::IFF_RUNNING) # ip.link.list(:flags => Linux::IFF_RUNNING)
# ip.link.list(:noflags => Netlink::IFF_POINTOPOINT) # ip.link.list(:noflags => Linux::IFF_POINTOPOINT)
# ip.link.list(:link => "lo") # vlan etc attached to this interface # ip.link.list(:link => "lo") # vlan etc attached to this interface
def list(filter=nil, &blk) def list(filter=nil, &blk)
@link ||= read_link @link ||= read_link
filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link) filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link)
do_list(@link, filter, &blk) filter_list(@link, filter, &blk)
end end
alias :each :list alias :each :list
@@ -187,16 +188,16 @@ module Netlink
# Add an interface (raw). e.g. # Add an interface (raw). e.g.
# #
# require 'netlink/route' # require 'linux/netlink/route'
# ip = Netlink::Route::Socket.new # ip = Linux::Netlink::Route::Socket.new
# ip.link.add( # ip.link.add(
# :link=>"lo", # :link=>"lo",
# :linkinfo=>Netlink::LinkInfo.new( # :linkinfo=>Linux::Netlink::LinkInfo.new(
# :kind=>"vlan", # :kind=>"vlan",
# :data=>Netlink::VlanInfo.new( # :data=>Linux::Netlink::VlanInfo.new(
# :id=>1234, # :id=>1234,
# :flags => Netlink::VlanFlags.new( # :flags => Linux::Netlink::VlanFlags.new(
# :flags=>Netlink::VLAN_FLAG_LOOSE_BINDING, # :flags=>Linux::VLAN_FLAG_LOOSE_BINDING,
# :mask=>0xffffffff # :mask=>0xffffffff
# )))) # ))))
@@ -243,3 +244,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,6 +1,7 @@
require 'netlink/route' require 'linux/netlink/route'
require 'netlink/route/handler' require 'linux/netlink/route/handler'
module Linux
module Netlink module Netlink
# struct rta_cacheinfo # struct rta_cacheinfo
RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage 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 # A hash of kernel options may be supplied, but you might also have
# to perform your own filtering. e.g. # to perform your own filtering. e.g.
# read_route(:family=>Socket::AF_INET) # works # read_route(:family=>Socket::AF_INET) # works
# read_route(:protocol=>Netlink::RTPROT_STATIC) # ignored # read_route(:protocol=>Linux::RTPROT_STATIC) # ignored
# #
# res = ip.route.read_route(:family => Socket::AF_INET) # res = ip.route.read_route(:family => Socket::AF_INET)
# p res # p res
# [#<Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0, # [#<Linux::Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0,
# :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255, # :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255,
# :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>, # :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>,
# :prefsrc=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :oif=>1}>, ...] # :prefsrc=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :oif=>1}>, ...]
@@ -77,7 +78,7 @@ module Netlink
# Note that not all attributes will always be present. In particular, # Note that not all attributes will always be present. In particular,
# a defaultroute (dst_len=0) misses out the dst address completely: # a defaultroute (dst_len=0) misses out the dst address completely:
# #
# [#<Netlink::RT {:family=>2, :dst_len=>0, :src_len=>0, :tos=>0, # [#<Linux::Netlink::RT {:family=>2, :dst_len=>0, :src_len=>0, :tos=>0,
# :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254, # :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}>, ...] # :gateway=>#<IPAddr: IPv4:10.69.255.253/255.255.255.255>, :oif=>2}>, ...]
def read_route(opt=nil, &blk) def read_route(opt=nil, &blk)
@@ -101,19 +102,19 @@ module Netlink
# Return the memoized route table, filtered according to # Return the memoized route table, filtered according to
# the optional criteria. Examples: # the optional criteria. Examples:
# :family => Socket::AF_INET # :family => Socket::AF_INET
# :table => Netlink::RT_TABLE_DEFAULT # :table => Linux::RT_TABLE_DEFAULT
# :protocol => Netlink::RTPROT_STATIC # :protocol => Linux::RTPROT_STATIC
# :type => Netlink::RTN_UNICAST # :type => Linux::RTN_UNICAST
# :scope => Netlink::RT_SCOPE_HOST # :scope => Linux::RT_SCOPE_HOST
# :flags => Netlink::RTM_F_NOTIFY # :flags => Linux::RTM_F_NOTIFY
# :noflags => Netlink::RTM_F_CLONED # :noflags => Linux::RTM_F_CLONED
# :oif => "eth0" # :oif => "eth0"
# :iif => "eth1" # :iif => "eth1"
def list(filter=nil, &blk) def list(filter=nil, &blk)
@route ||= read_route @route ||= read_route
filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif) filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif)
filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif) filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif)
do_list(@route, filter, &blk) filter_list(@route, filter, &blk)
end end
alias :each :list alias :each :list
@@ -195,3 +196,4 @@ module Netlink
end end
end end
end end
end # module Linux

View File

@@ -1,6 +1,7 @@
require 'netlink/route' require 'linux/netlink/route'
require 'netlink/route/handler' require 'linux/netlink/route/handler'
module Linux
module Netlink module Netlink
module Route module Route
class VlanHandler < Handler class VlanHandler < Handler
@@ -17,7 +18,7 @@ module Netlink
# nl.vlans.add( # nl.vlans.add(
# :link=>"lo", # :link=>"lo",
# :vlan_id=>1234, # :vlan_id=>1234,
# :vlan_flags=>Netlink::VLAN_FLAG_LOOSE_BINDING, # :vlan_flags=>Linux::VLAN_FLAG_LOOSE_BINDING,
# :vlan_mask=>0xffffffff # :vlan_mask=>0xffffffff
# ) # )
def add(opt) def add(opt)
@@ -63,3 +64,4 @@ module Netlink
end end
end end
end end
end # module Linux