Allow attributes to be defined inline (singleton type)
This commit is contained in:
@@ -39,6 +39,16 @@ module Netlink
|
||||
TYPE_INFO[name] = opt
|
||||
end
|
||||
|
||||
# Return a type info hash given a type id. Raises IndexError if not found.
|
||||
def self.find_type(type)
|
||||
case type
|
||||
when nil, Hash
|
||||
type
|
||||
else
|
||||
TYPE_INFO.fetch(type)
|
||||
end
|
||||
end
|
||||
|
||||
define_type :uchar, :pattern => "C"
|
||||
define_type :uint16, :pattern => "S"
|
||||
define_type :uint32, :pattern => "L"
|
||||
@@ -52,6 +62,7 @@ module Netlink
|
||||
define_type :int, :pattern => "i"
|
||||
define_type :long, :pattern => "l_"
|
||||
define_type :binary, :pattern => "a*", :default => EMPTY_STRING
|
||||
# cstring has \x00 terminator when sent over wire
|
||||
define_type :cstring, :pattern => "Z*", :default => EMPTY_STRING
|
||||
|
||||
# L2 addresses are presented as ASCII hex. You may optionally include
|
||||
@@ -158,7 +169,7 @@ module Netlink
|
||||
# field :foo, :uchar
|
||||
# field :foo, :uchar, :default=>0xff # use this default value
|
||||
def self.field(name, type, opt={})
|
||||
info = TYPE_INFO[type]
|
||||
info = find_type(type)
|
||||
self::FIELDS << name
|
||||
self::FORMAT << info[:pattern]
|
||||
self::DEFAULTS[name] = opt.fetch(:default) { info.fetch(:default, 0) }
|
||||
@@ -223,8 +234,8 @@ module Netlink
|
||||
# type (if not provided, it will just be returned as a raw binary string)
|
||||
# rtattr :foo, 12
|
||||
# rtattr :foo, 12, :uint
|
||||
def self.rtattr(name, code, type=nil, opt={})
|
||||
info = TYPE_INFO[type]
|
||||
def self.rtattr(name, code, type=nil)
|
||||
info = find_type(type)
|
||||
self::RTATTRS[code] = [name, info]
|
||||
define_method name do
|
||||
@attrs[name] # rtattrs are optional, non-existent returns nil
|
||||
|
@@ -18,10 +18,6 @@ module Netlink
|
||||
: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
|
||||
|
||||
@@ -29,19 +25,6 @@ module Netlink
|
||||
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_*
|
||||
@@ -54,13 +37,18 @@ module Netlink
|
||||
rtattr :mtu, IFLA_MTU, :uint32
|
||||
rtattr :link, IFLA_LINK, :int32
|
||||
rtattr :qdisc, IFLA_QDISC, :cstring
|
||||
rtattr :stats32, IFLA_STATS, :linkstats32
|
||||
rtattr :stats32, IFLA_STATS,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L23") },
|
||||
:unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("L23"))) }
|
||||
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
|
||||
IFMAP_PACK = "QQQSCC".freeze #:nodoc:
|
||||
rtattr :map, IFLA_MAP,
|
||||
:pack => lambda { |val,obj| val.to_a.pack(IFMAP_PACK) },
|
||||
:unpack => lambda { |str,obj| IFMap.new(*(str.unpack(IFMAP_PACK))) }
|
||||
rtattr :weight, IFLA_WEIGHT, :uint32
|
||||
rtattr :operstate, IFLA_OPERSTATE, :uchar
|
||||
rtattr :linkmode, IFLA_LINKMODE, :uchar
|
||||
@@ -69,7 +57,9 @@ module Netlink
|
||||
rtattr :ifalias, IFLA_IFALIAS, :cstring
|
||||
rtattr :num_vf, IFLA_NUM_VF, :uint32
|
||||
rtattr :vfinfo_list, IFLA_VFINFO_LIST
|
||||
rtattr :stats64, IFLA_STATS64, :linkstats64
|
||||
rtattr :stats64, IFLA_STATS64,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("Q23") },
|
||||
:unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("Q23"))) }
|
||||
rtattr :vf_ports, IFLA_VF_PORTS
|
||||
rtattr :port_self, IFLA_PORT_SELF
|
||||
|
||||
@@ -79,14 +69,13 @@ module Netlink
|
||||
end
|
||||
end
|
||||
|
||||
# struct ifa_cacheinfo
|
||||
IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp
|
||||
|
||||
# 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_*
|
||||
@@ -97,32 +86,19 @@ module Netlink
|
||||
rtattr :label, IFA_LABEL, :cstring
|
||||
rtattr :broadcast, IFA_BROADCAST, :l3addr
|
||||
rtattr :anycast, IFA_ANYCAST, :l3addr
|
||||
rtattr :cacheinfo, IFA_CACHEINFO, :ifa_cacheinfo
|
||||
rtattr :cacheinfo, IFA_CACHEINFO,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L*") },
|
||||
:unpack => lambda { |str,obj| IFACacheInfo.new(*(str.unpack("L*"))) }
|
||||
rtattr :multicast, IFA_MULTICAST, :l3addr
|
||||
end
|
||||
|
||||
# struct rta_cacheinfo
|
||||
RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage
|
||||
|
||||
# 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
|
||||
@@ -139,10 +115,24 @@ module Netlink
|
||||
rtattr :gateway, RTA_GATEWAY, :l3addr
|
||||
rtattr :priority, RTA_PRIORITY, :uint32
|
||||
rtattr :prefsrc, RTA_PREFSRC, :l3addr
|
||||
rtattr :metrics, RTA_METRICS, :rtmetrics
|
||||
# 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:
|
||||
rtattr :metrics, RTA_METRICS, # {RTAX_* => Integer}
|
||||
:pack => lambda { |metrics,obj|
|
||||
metrics.map { |code,val| [METRIC_SIZE,code,val].pack(METRIC_PACK) }.join
|
||||
},
|
||||
:unpack => lambda { |str,obj|
|
||||
res = {}
|
||||
RtattrMessage.unpack_rtattr(str) { |code,val| res[code] = val.unpack("L").first }
|
||||
res
|
||||
}
|
||||
rtattr :multipath, RTA_MULTIPATH
|
||||
rtattr :flow, RTA_FLOW
|
||||
rtattr :cacheinfo, RTA_CACHEINFO, :rta_cacheinfo
|
||||
rtattr :cacheinfo, RTA_CACHEINFO,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L*") },
|
||||
:unpack => lambda { |str,obj| RTACacheInfo.new(*(str.unpack("L*"))) }
|
||||
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user