From a6eda43b2ef5fc871545b2957f03145388ed92ef Mon Sep 17 00:00:00 2001 From: Brian Candler Date: Mon, 2 May 2011 18:17:24 +0100 Subject: [PATCH] Another little reorg; we are quite tightly bound to implementation of CStruct --- README | 25 +++++++++++++++++++++++++ lib/{cstruct.rb => netlink/c_struct.rb} | 9 ++++++--- lib/netlink/message.rb | 8 ++++---- 3 files changed, 35 insertions(+), 7 deletions(-) rename lib/{cstruct.rb => netlink/c_struct.rb} (97%) diff --git a/README b/README index b7793b2..0397761 100644 --- a/README +++ b/README @@ -5,6 +5,29 @@ This library provides an API for using a Linux Netlink socket, for doing things like manipulating IP interfaces, routes and firewall rules programmatically. +Code organisation +================= + +There are separate classes for each Netlink protocol providing a high-level +API. These all in turn use the NLSocket class, which has methods for adding +the headers to messages and sending them over a socket. The messages +themselves are built using class Message or RtattrMessage, which in turn are +subclasses of CStruct, which performs the low-level packing and unpacking of +the message bodies. + + Route Firewall ...etc + | | | + +-------+-------+ + | + v + NLSocket + | + v + Message / RtattrMessage + | + v + CStruct + Useful reference material ========================= @@ -41,6 +64,8 @@ TODO ==== * Exception hierarchy +* Unit tests +* Integration tests Copyright ========= diff --git a/lib/cstruct.rb b/lib/netlink/c_struct.rb similarity index 97% rename from lib/cstruct.rb rename to lib/netlink/c_struct.rb index e7da8ce..0e1db8d 100644 --- a/lib/cstruct.rb +++ b/lib/netlink/c_struct.rb @@ -1,10 +1,12 @@ +module Netlink + # This class allows defining of C-style structures, and converting # object instances to and from a packed binary representation. # -# 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: # -# class Foo < Cstruct +# class Foo < CStruct # field :bar, :char # field :baz, :long # @@ -25,7 +27,7 @@ # str = msg.to_s # convert to binary # msg2 = Foo.parse(str) # convert from binary # msg2 = Foo.new(msg) # copy an existing object -class Cstruct +class CStruct EMPTY_STRING = "".freeze #:nodoc: EMPTY_ARRAY = [].freeze #:nodoc: @@ -183,3 +185,4 @@ class Cstruct (val + (m-1)) & ~(m-1) end end +end # module Netlink diff --git a/lib/netlink/message.rb b/lib/netlink/message.rb index e1bf227..be8701f 100644 --- a/lib/netlink/message.rb +++ b/lib/netlink/message.rb @@ -1,4 +1,4 @@ -require 'cstruct' +require 'netlink/c_struct' require 'netlink/constants' require 'ipaddr' @@ -6,8 +6,8 @@ module Netlink NLMSGHDR_PACK = "LSSLL".freeze # :nodoc: NLMSGHDR_SIZE = [0,0,0,0,0].pack(NLMSGHDR_PACK).bytesize # :nodoc: - EMPTY_STRING = Cstruct::EMPTY_STRING #:nodoc: - EMPTY_ARRAY = Cstruct::EMPTY_ARRAY #:nodoc: + EMPTY_STRING = "".freeze #:nodoc: + EMPTY_ARRAY = [].freeze #:nodoc: # This is the base class from which all Netlink messages are derived. # To define a new Netlink message, make a subclass and then call the @@ -16,7 +16,7 @@ module Netlink # types are to be built using this structure. # # Use RtattrMessage instead for messages which are followed by variable rtattrs. - class Message < Cstruct + class Message < CStruct # Map of numeric message type code => message class CODE_TO_MESSAGE = {}