Files
netlinkrb/lib/netlink/message.rb

313 lines
9.7 KiB
Ruby
Raw Normal View History

2011-04-29 11:51:10 +01:00
require 'netlink/constants'
require 'ipaddr'
2011-04-29 11:51:10 +01:00
module Netlink
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
2011-04-29 16:34:04 +01:00
RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage
IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp
2011-04-29 11:51:10 +01:00
# Base class for Netlink messages
class Message
# Map of numeric message type code => message class
CODE_TO_MESSAGE = {}
# Defines each of the possible field types
TYPE_INFO = {
:uchar => { :pattern => "C" },
:uint16 => { :pattern => "S" },
:uint32 => { :pattern => "L" },
:char => { :pattern => "c" },
:int16 => { :pattern => "s" },
:int32 => { :pattern => "l" },
:ushort => { :pattern => "S_" },
:uint => { :pattern => "I" },
:ulong => { :pattern => "L_" },
:short => { :pattern => "s_" },
:int => { :pattern => "i" },
:long => { :pattern => "l_" },
:binary => { :pattern => "a*", :default => "".freeze },
:cstring => { :pattern => "Z*", :default => "".freeze },
:stats32 => {
:pack => lambda { |val| val.to_a.pack("L23") },
:unpack => lambda { |str| LinkStats.new(*(str.unpack("L23"))) },
},
:stats64 => {
:pack => lambda { |val| val.to_a.pack("Q23") },
:unpack => lambda { |str| LinkStats.new(*(str.unpack("Q23"))) },
},
2011-04-29 16:34:04 +01:00
:rta_cacheinfo => {
:pack => lambda { |val| val.to_a.pack("L*") },
:unpack => lambda { |str| RTACacheInfo.new(*(str.unpack("L*"))) },
},
:ifa_cacheinfo => {
:pack => lambda { |val| val.to_a.pack("L*") },
:unpack => lambda { |str| IFACacheInfo.new(*(str.unpack("L*"))) },
},
:l3addr => {
:pack => lambda { |val| val.hton },
:unpack => lambda { |val| IPAddr.new_ntoh(val) },
},
}
2011-04-29 11:51:10 +01:00
# You can initialize a message from a Hash or from another
# instance of itself.
#
# class Foo < Message
# field :foo, :char, :default=>255
# field :bar, :long
2011-04-29 11:51:10 +01:00
# end
# msg = Foo.new(:bar => 123) # or ("bar" => 123)
# msg2 = Foo.new(msg)
# msg3 = Foo.new(:qux => 999) # error, no method "qux="
2011-04-29 16:33:08 +01:00
def initialize(h=nil)
2011-04-29 11:51:10 +01:00
if h.instance_of?(self.class)
@attrs = h.to_hash.dup
else
@attrs = self.class::DEFAULTS.dup
2011-04-29 16:33:08 +01:00
h.each { |k,v| self[k] = v } if h
2011-04-29 11:51:10 +01:00
end
end
def to_hash
@attrs
end
def each(&blk)
@attrs.each(&blk)
end
# Set a field by name. Can use either symbol or string as key.
def []=(k,v)
send "#{k}=", v
end
# Retrieve a field by name. Must use symbol as key.
def [](k)
@attrs[k]
end
def self.inherited(subclass) #:nodoc:
subclass.const_set(:FIELDS, [])
subclass.const_set(:FORMAT, "")
subclass.const_set(:DEFAULTS, {})
end
# Define which message type code(s) use this structure
def self.code(*codes)
codes.each { |code| CODE_TO_MESSAGE[code] = self }
end
# Define a field for this message, which creates accessor methods and
# sets up data required to pack and unpack the structure.
def self.field(name, type, opt={})
info = TYPE_INFO[type]
2011-04-29 11:51:10 +01:00
self::FIELDS << name
self::FORMAT << info[:pattern]
self::DEFAULTS[name] = opt.fetch(:default) { info.fetch(:default, 0) }
2011-04-29 11:51:10 +01:00
define_method name do
@attrs.fetch name
end
define_method "#{name}=" do |val|
@attrs.store name, val
end
end
# Returns the packed binary representation of this message (without
# header, and not padded to NLMSG_ALIGNTO bytes)
def to_s
self.class::FIELDS.map { |key| self[key] }.pack(self.class::FORMAT)
end
def inspect
"#<#{self.class} #{@attrs.inspect}>"
end
# Convert a binary representation of this message into an object instance
def self.parse(data)
2011-04-29 11:51:10 +01:00
res = new
data.unpack(self::FORMAT).zip(self::FIELDS).each do |val, key|
2011-04-29 11:51:10 +01:00
res[key] = val
end
res
end
NLMSG_ALIGNTO_1 = NLMSG_ALIGNTO-1 #:nodoc:
NLMSG_ALIGNTO_1_MASK = ~NLMSG_ALIGNTO_1 #:nodoc:
# Round up a length to a multiple of NLMSG_ALIGNTO bytes
def self.align(n)
(n + NLMSG_ALIGNTO_1) & NLMSG_ALIGNTO_1_MASK
end
PADDING = ("\000" * NLMSG_ALIGNTO).freeze #:nodoc:
# Pad a string up to a multiple of NLMSG_ALIGNTO bytes. Returns str.
def self.pad(str)
str << PADDING[0, align(str.bytesize) - str.bytesize]
end
end
# This is a class for a Message which is followed by Rtattr key/value pairs.
# We assume that any particular attribute is not repeated, so it maps to
# a single attribute in the underlying hash.
class RtattrMessage < Message
RTATTR_PACK = "S_S_".freeze #:nodoc:
RTATTR_SIZE = [0,0].pack(RTATTR_PACK).bytesize #:nodoc:
def self.inherited(subclass) #:nodoc:
super
subclass.const_set(:RTATTRS, {})
end
def self.rtattr(name, code, type=nil, opt={})
info = TYPE_INFO[type]
self::RTATTRS[code] = [name, info]
define_method name do
@attrs[name] # rtattrs are optional, non-existent returns nil
end
define_method "#{name}=" do |val|
@attrs.store name, val
end
end
2011-04-29 11:51:10 +01:00
def self.attr_offset #:nodoc:
@attr_offset ||= Message.align(new.to_s.bytesize)
end
def to_s
data = super
self.class::RTATTRS.each do |code, (name, info)|
if val = @attrs[name]
Message.pad(data)
if pack = info[:pack]
val = pack[val]
elsif pattern = info[:pattern]
val = Array(val).pack(pattern)
end
data << [val.bytesize+RTATTR_SIZE, code].pack(RTATTR_PACK) << val
end
end
data
end
def self.parse(data)
res = super
ptr = attr_offset
while ptr < data.bytesize
raise "Truncated rtattr header!" if ptr + RTATTR_SIZE > data.bytesize
len, code = data[ptr, RTATTR_SIZE].unpack(RTATTR_PACK)
raise "Truncated rtattr body!" if ptr + len > data.bytesize
raise "Invalid rtattr len!" if len < RTATTR_SIZE
res._add_attr(code, data[ptr+RTATTR_SIZE, len-RTATTR_SIZE])
ptr = Message.align(ptr + len)
end
res
end
def _add_attr(code, val) # :nodoc:
name, info = self.class::RTATTRS[code]
if name
if !info
# skip
elsif unpack = info[:unpack]
val = unpack[val]
elsif pattern = info[:pattern]
val = val.unpack(pattern).first
end
warn "Duplicate attribute #{name} (#{code}): #{@attrs[name].inspect} -> #{val.inspect}" if @attrs[name]
@attrs[name] = val
else
warn "Unknown attribute #{code}, value #{val.inspect}"
@attrs[code] = val
end
end
2011-04-29 11:51:10 +01:00
end
class Link < RtattrMessage
2011-04-29 11:51:10 +01:00
code RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK
2011-04-29 16:33:09 +01:00
field :family, :uchar # Socket::AF_*
field :pad, :uchar
2011-04-29 16:33:09 +01:00
field :type, :ushort # ARPHRD_*
field :index, :int
2011-04-29 16:33:09 +01:00
field :flags, :uint # IFF_*
field :change, :uint, :default=>0xffffffff
rtattr :address, IFLA_ADDRESS
rtattr :broadcast, IFLA_BROADCAST
rtattr :ifname, IFLA_IFNAME, :cstring
rtattr :mtu, IFLA_MTU, :uint32
rtattr :link, IFLA_LINK, :int32
rtattr :qdisc, IFLA_QDISC, :cstring
rtattr :stats, IFLA_STATS, :stats32
rtattr :cost, IFLA_COST
rtattr :master, IFLA_MASTER
rtattr :wireless, IFLA_WIRELESS
rtattr :protinfo, IFLA_PROTINFO
rtattr :txqlen, IFLA_TXQLEN, :uint32
rtattr :map, IFLA_MAP
rtattr :weight, IFLA_WEIGHT
rtattr :operstate, IFLA_OPERSTATE, :uchar
rtattr :linkmode, IFLA_LINKMODE, :uchar
rtattr :linkinfo, IFLA_LINKINFO
rtattr :net_ns_pid, IFLA_NET_NS_PID
rtattr :ifalias, IFLA_IFALIAS
rtattr :num_vf, IFLA_NUM_VF, :uint32
rtattr :vfinfo_list, IFLA_VFINFO_LIST
rtattr :stats64, IFLA_STATS64, :stats64
rtattr :vf_ports, IFLA_VF_PORTS
rtattr :port_self, IFLA_PORT_SELF
2011-04-29 11:51:10 +01:00
end
class Addr < RtattrMessage
2011-04-29 11:51:10 +01:00
code RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
2011-04-29 16:33:09 +01:00
field :family, :uchar # Socket::AF_*
field :prefixlen, :uchar
2011-04-29 16:33:09 +01:00
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
2011-04-29 16:34:04 +01:00
rtattr :cacheinfo, IFA_CACHEINFO, :ifa_cacheinfo
rtattr :multicast, IFA_MULTICAST, :l3addr
2011-04-29 11:51:10 +01:00
end
class Route < RtattrMessage
2011-04-29 11:51:10 +01:00
code RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE
2011-04-29 16:33:09 +01:00
field :family, :uchar # Socket::AF_*
field :dst_len, :uchar
field :src_len, :uchar
field :tos, :uchar
2011-04-29 16:33:09 +01:00
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
rtattr :multipath, RTA_MULTIPATH
rtattr :flow, RTA_FLOW
2011-04-29 16:34:04 +01:00
rtattr :cacheinfo, RTA_CACHEINFO, :rta_cacheinfo
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
2011-04-29 11:51:10 +01:00
end
end