Reorganise under Linux::
This commit is contained in:
5
README
5
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
|
||||
=================
|
||||
|
||||
|
@@ -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 }
|
||||
|
||||
|
@@ -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 }
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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:"
|
||||
|
@@ -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 ***"
|
||||
|
@@ -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
|
@@ -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
|
15
lib/linux/error.rb
Normal file
15
lib/linux/error.rb
Normal 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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|
||||
# [#<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>,
|
||||
# :local=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :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
|
@@ -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
|
@@ -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
|
||||
# [#<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,
|
||||
# :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...",
|
||||
# :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00",
|
||||
# :stats32=>#<struct Netlink::LinkStats rx_packets=22, ...>,
|
||||
# :stats64=>#<struct Netlink::LinkStats rx_packets=22, ...>}>, ...]
|
||||
# :stats32=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>,
|
||||
# :stats64=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>}>, ...]
|
||||
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
|
@@ -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
|
||||
# [#<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,
|
||||
# :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>,
|
||||
# :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,
|
||||
# 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,
|
||||
# :gateway=>#<IPAddr: IPv4:10.69.255.253/255.255.255.255>, :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
|
@@ -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
|
Reference in New Issue
Block a user