From 0af010b358b7f2f241af6adbde7c7243fd1596dc Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 20 Jun 2020 23:01:42 +0200 Subject: [PATCH 01/17] spec name knot-ruby. ignore Gemfile.lock --- .gitignore | 2 ++ knot-ruby.gemspec | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f5587e6..3e7b48d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ /spec/reports/ /tmp/ +/Gemfile.lock + # ---> Vim # Swap [._]*.s[a-v][a-z] diff --git a/knot-ruby.gemspec b/knot-ruby.gemspec index 9076bd2..5a51a4b 100644 --- a/knot-ruby.gemspec +++ b/knot-ruby.gemspec @@ -1,7 +1,7 @@ require_relative 'lib/knot/version' Gem::Specification.new do |spec| - spec.name = "knot" + spec.name = "knot-ruby" spec.version = Knot::VERSION spec.authors = ['Denis Knauf'] spec.email = ['gems+knot@denkn.at'] From d51b56a67bd6e0186630451e8c71164b46b68541 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 2 Dec 2020 16:20:58 +0100 Subject: [PATCH 02/17] Comments for Conf-interface. get-method added. --- lib/knot/interface.rb | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/knot/interface.rb b/lib/knot/interface.rb index 5130950..62b1ff5 100644 --- a/lib/knot/interface.rb +++ b/lib/knot/interface.rb @@ -11,22 +11,35 @@ class Knot::Zone @zone, @transaction_opened = zone, 0 end + # If no transaction opened, yet, it opens a new transaction. + # Counts calling begin. + # Knot allowes only one global transaction, so if two clients tries to open a transaction, + # the second will fail. But the second client can change items in the same + # transaction like the first client. + # The first client, which calls commit or abort, wins and the transaction will be closed. + # So, the transaction-handling is only safe, if one client connects to knot. def begin @transaction_opened += 1 @protocol.call command: 'zone-begin', zone: @zone if 1 == @transaction_opened end + # Decreases opened transactions. + # If opened transactions is zero, it stores items and closes transaction successfully. def commit @protocol.call command: 'zone-commit', zone: @zone if 1 == @transaction_opened @transaction_opened -= 1 if 0 < @transaction_opened end + # Closes transaction without saving immidiatly. + # Counter of opened transaction will be reset to 0. def abort @protocol.call command: 'zone-abort', zone: @zone @transaction_opened = 0 end - def transaction + # Opens transaction, calls yielded Proc and closes transaction after return. + # If exception was raised, the transaction will be aborted. + def transaction # :yield: self.begin yield self rescue Object @@ -72,6 +85,7 @@ class Knot::Zone rescue Knot::Errors::ENONODE, Knot::Errors::ENOENT end alias delete unset + alias del unset def get owner = nil, type = nil @protocol.call command: 'zone-get', @@ -115,10 +129,12 @@ class Knot::Conf def parse_item k case k - when k + when Hash case k.keys.sort - when %w[section], %w[id section], %w[item section], %w[id item section] then k - else raise ArgumentError, "Invalid Item-format" + when %w[section], %w[id section], %w[item section], %w[id item section] + k + else + raise ArgumentError, "Invalid Item-format" end when Array @@ -134,16 +150,26 @@ class Knot::Conf (?: \[ (? [a-z0-9_.-]+) \] )? (?: \. (?[a-z0-9_-]+) )? \z/xi + $~.named_captures.delete_if {|_,v| v.nil? } - else raise ArgumentError, "Invalid Item-format" + + else + raise ArgumentError, "Invalid Item-format" end end + def get item = nil + @protocol.call (item ? parse_item( item) : {}).update( command: 'conf-get') + end + + # Sets or adds a new value to item. + # knot knows single-value items like `server.rundir` and multi-value items like `server.listen`. + # If you set a single-value, it will replace the old value. On a multi-value, it will add it. def set item, value @protocol.call parse_item( item).update( command: 'conf-set', data: value) end - alias [] set + # Removes value from item. If you provide a value, this value will be removed. def unset item, value = nil @protocol.call parse_item( item).update( command: 'conf-unset', data: value) end From ad44237905f7de809aea7bd7da7f2aeefeef3ed9 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 2 Dec 2020 16:38:59 +0100 Subject: [PATCH 03/17] v0.1.1 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index 4db3f7f..7f7ffce 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.1.0" + VERSION = "0.1.1" end From d25e1a97627c968e78a2f3b08acdf5d067948098 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Fri, 17 Jun 2022 12:46:37 +0200 Subject: [PATCH 04/17] ruby-2.7 needs ** for key-value-pair-arguments --- lib/knot/protocol.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 903697b..bdb7c02 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -179,10 +179,10 @@ class Knot::Protocol def zone( zone) @zones[zone.to_s.to_sym] end - def conf_set( **opts) call opts.update( command: 'conf-set') end - def conf_unset( **opts) call opts.update( command: 'conf-set') end + def conf_set( **opts) call **opts.update( command: 'conf-set') end + def conf_unset( **opts) call **opts.update( command: 'conf-set') end - def zone_set( **opts) call opts.update( command: 'zone-set') end - def zone_unset( **opts) call opts.update( command: 'zone-unset') end - def zone_get( **opts) call opts.update( command: 'zone-get') end + def zone_set( **opts) call **opts.update( command: 'zone-set') end + def zone_unset( **opts) call **opts.update( command: 'zone-unset') end + def zone_get( **opts) call **opts.update( command: 'zone-get') end end From 0a0392d5325396e21adcf1137f85d65ba5c3e5ca Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Fri, 17 Jun 2022 12:47:37 +0200 Subject: [PATCH 05/17] 0.1.2 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index 7f7ffce..26b7ac8 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.1.1" + VERSION = "0.1.2" end From 6af5922217411bd0962c614bb7e9f184f966cde3 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 27 Sep 2022 20:58:19 +0200 Subject: [PATCH 06/17] Version 0.2.0 - required ruby>=2.7. **-kw-options. BETA --- Gemfile | 1 + knot-ruby.gemspec | 2 +- lib/knot.rb | 10 ++++++++++ lib/knot/interface.rb | 15 +++++++++------ lib/knot/protocol.rb | 35 ++++++++++++++++++++++++++++------- lib/knot/version.rb | 2 +- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 548b363..844a1ab 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ source "https://rubygems.org" +ruby '>=2.7' # Specify your gem's dependencies in knot.gemspec gemspec diff --git a/knot-ruby.gemspec b/knot-ruby.gemspec index 5a51a4b..d8dded8 100644 --- a/knot-ruby.gemspec +++ b/knot-ruby.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |spec| spec.summary = %q{Provides interface to knot-server.} spec.description = %q{Implements knot-protocol to provide an interface to knot-DNS-server} spec.homepage = 'https://git.denkn.at/deac/knot' - spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0") + spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0") #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" diff --git a/lib/knot.rb b/lib/knot.rb index 4231108..32f0701 100644 --- a/lib/knot.rb +++ b/lib/knot.rb @@ -2,3 +2,13 @@ require 'knot/version' require 'knot/errors' require 'knot/protocol' require 'knot/interface' + +module Knot + class < Date: Wed, 28 Sep 2022 11:01:29 +0200 Subject: [PATCH 07/17] binary strings encoded binary. Idx::Id instead of Symbol in Idx. parse_items only allowes Symbols as options. --- lib/knot/interface.rb | 34 ++++++++-------- lib/knot/protocol.rb | 93 +++++++++++++++++++++++++++++-------------- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/lib/knot/interface.rb b/lib/knot/interface.rb index 221af51..2825e97 100644 --- a/lib/knot/interface.rb +++ b/lib/knot/interface.rb @@ -127,37 +127,39 @@ class Knot::Conf self.commit end - def parse_item k - case k + def parse_item kv + case kv when Hash - case k.keys.sort - when %w[section], %w[id section], %w[item section], %w[id item section] - k + r = {} + kv.each {|k,v| r[k.to_s.to_sym] = v } + case r.keys.sort + when %i[section], %i[id section], %i[item section], %i[id item section] + r else - raise ArgumentError, "Invalid Item-format" + raise ArgumentError, "Invalid Item-format: #{k}" end when Array - case k.length - when 1 then {section: k[0]} - when 2 then {section: k[0], item: k[1]} - when 3 then {section: k[0], id: k[1], item: k[2]} - else raise ArgumentError, "Invalid Item-format" + case kv.length + when 1 then {section: kv[0]} + when 2 then {section: kv[0], item: kv[1]} + when 3 then {section: kv[0], id: kv[1], item: kv[2]} + else raise ArgumentError, "Invalid Item-format: #{kv}" end when /\A - (?
[a-z0-9_-]+ ) - (?: \[ (? [a-z0-9_.-]+) \] )? - (?: \. (?[a-z0-9_-]+) )? + (?
[a-z0-9_-]+ ) + (?: \[ (? [a-z0-9_.-]+) \] )? + (?: \. (?[a-z0-9_-]+) )? \z/xi - $~.named_captures.delete_if {|_,v| v.nil? } + $~.named_captures.delete_if {|_,v| v.nil? } when nil {} else - raise ArgumentError, "Invalid Item-format" + raise ArgumentError, "Invalid Item-format: #{kv}" end end diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 247e7d8..21c8463 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -73,34 +73,69 @@ end #end module Knot::Protocol::Idx + class Id + include Comparable + attr_reader :name, :code, :cname, :description + + def initialize name, code, cname, description + raise ArgumentError, "Expecting Symbol for Idx::Id instead of: #{name.inspect}" if Symbol === name + raise ArgumentError, "Expecting Integer for Idx::Id instead of: #{code.inspect}" if Integer === code + @name, @code, @cname, @description = name, code, cname, description + freeze + end + + def === x + case x + when self.class then self == x + when Symbol then @name == x + when String then @name == x.to_sym + when Integer then @code == x + else nil + end + end + + def <=>( x) @id <=> x.id end + def to_s() @name.to_s end + def to_sym() @name end + def to_i() @code end + end + Idx = [ - :command, # 10, :CMD, # Control command name. - :flags, # 11, :FLAGS, # Control command flags. - :error, # 12, :ERROR, # Error message. - :section, # 13, :SECTION, # Configuration section name. - :item, # 14, :ITEM, # Configuration item name. - :id, # 15, :ID, # Congiguration item identifier. - :zone, # 16, :ZONE, # Zone name. - :owner, # 17, :OWNER, # Zone record owner - :ttl, # 18, :TTL, # Zone record TTL. - :type, # 19, :TYPE, # Zone record type name. - :data, # 1a, :DATA, # Configuration item/zone record data. - :filter, # 1b, :FILTER, # An option or a filter for output data processing. + Id.new( :command, 0x10, :CMD, 'Control command name.'), + Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'), + Id.new( :error, 0x12, :ERROR, 'Error message.'), + Id.new( :section, 0x13, :SECTION, 'Configuration section name.'), + Id.new( :item, 0x14, :ITEM, 'Configuration item name.'), + Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'), + Id.new( :zone, 0x16, :ZONE, 'Zone name.'), + Id.new( :owner, 0x17, :OWNER, 'Zone record owner'), + Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), + Id.new( :type, 0x19, :TYPE, 'Zone record type name.'), + Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), + Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), ] Name = {} Code = {} - Idx.each_with_index do |v, i| - Code[0x10+i] = v - Name[v] = i + + Idx.each do |id| + Code[id.to_i] = id + Name[id.to_sym] = id end - def self.[] k - case k - when Symbol - Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}" - when Integer - Idx[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}" - else - raise ArgumentError, "Unknown Idx-Type: #{k}" + + class < Date: Wed, 28 Sep 2022 11:04:35 +0200 Subject: [PATCH 08/17] Knot::Protocol::Idx fix: if -> unless --- lib/knot/protocol.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 21c8463..0fda3b3 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -78,8 +78,8 @@ module Knot::Protocol::Idx attr_reader :name, :code, :cname, :description def initialize name, code, cname, description - raise ArgumentError, "Expecting Symbol for Idx::Id instead of: #{name.inspect}" if Symbol === name - raise ArgumentError, "Expecting Integer for Idx::Id instead of: #{code.inspect}" if Integer === code + raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name + raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code @name, @code, @cname, @description = name, code, cname, description freeze end From 0282694a287ac0a9b4dfe288fb258af2ea215ca8 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 28 Sep 2022 11:06:53 +0200 Subject: [PATCH 09/17] Knot::Protocol: Fix iteration. --- lib/knot/protocol.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 0fda3b3..3ba7b2b 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -164,9 +164,9 @@ class Knot::Protocol sock = StringIO.new s sock.write [1].pack( 'c') data[:flags] ||= '' - Idx::Idx.each_with_index do |n, i| - v = data[n]&.to_s - sock.write [0x10+i, v.size, v].pack( 'c na*') if v + Idx::Idx.each do |n| + v = data[n.to_sym]&.to_s&.b + sock.write [n.to_i, v.size, v].pack( 'c na*') if v end sock.write [3].pack( 'c') sock.flush From 18c0b6434a96128381df5d88d51f73ca838d9994 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 28 Sep 2022 12:34:03 +0200 Subject: [PATCH 10/17] Knot::Protocol#debug replaced by #logger, which could be provided by #new(logger:), default Logger.new(STDERR) --- lib/knot/protocol.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 3ba7b2b..784c81c 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -53,7 +53,7 @@ module Knot::Protocol::Type end #class Knot::KnotC -# attr_accessor :debug, :binary +# attr_accessor :binary # # def initialize path = nil, binary: nil # @path = path @@ -141,10 +141,9 @@ module Knot::Protocol::Idx end class Knot::Protocol - attr_reader :sock, :conf, :zones - attr_accessor :debug + attr_reader :sock, :conf, :zones, :logger - def initialize path_or_sock = nil + def initialize path_or_sock = nil, logger: nil case path_or_sock when String, Pathname @sock = UNIXSocket.new path_or_sock.to_s @@ -153,7 +152,7 @@ class Knot::Protocol when nil @sock = UNIXSocket.new '/run/knot/knot.sock' end - @debug = false + @logger = logger || Logger.new(STDERR) @conf = Knot::Conf.new self @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self } end @@ -170,7 +169,10 @@ class Knot::Protocol end sock.write [3].pack( 'c') sock.flush - STDERR.puts( {send: data, _: s}.inspect) if @debug + if 0 >= @logger.sev_threshold + @logger.debug "send data #{data.inspect}" + @logger.debug "send raw #{data.inspect}" + end rsock.write s rsock.flush end @@ -200,7 +202,7 @@ class Knot::Protocol def rcv sock: nil ret, r = [], nil sock = sock || @sock - sock = RecordIO.new sock if @debug + sock = RecordIO.new sock if 0 >= @logger.sev_threshold loop do t = sock.unpack1 'c' case t @@ -217,7 +219,10 @@ class Knot::Protocol end end ensure - STDERR.puts( {rcvd: ret, _: sock.str}.inspect) if @debug + if RecordIO === sock + @logger.debug "rcvd raw #{sock.str.inspect}" + @logger.debug "rcvd data #{ret.inspect}" + end ret end From 7e02cbbe84ee4bf1585b15e1507b79701f79d462 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 29 Sep 2022 11:38:11 +0200 Subject: [PATCH 11/17] Protocol#snd: StringIO eliminated, more functional. --- lib/knot/protocol.rb | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 784c81c..98bf7c6 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -159,19 +159,24 @@ class Knot::Protocol def snd sock: nil, **data rsock = sock || @sock - s = ''.b - sock = StringIO.new s - sock.write [1].pack( 'c') + #s = ''.b + #sock = StringIO.new s + #sock.write [1].pack( 'c') data[:flags] ||= '' - Idx::Idx.each do |n| - v = data[n.to_sym]&.to_s&.b - sock.write [n.to_i, v.size, v].pack( 'c na*') if v - end - sock.write [3].pack( 'c') - sock.flush + ds = + Idx::Idx. + select {|n| data[n.to_sym] }. + map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] } + s = (ds.flatten+[3]).pack( ('c na*'*ds.length)+'c').b + #Idx::Idx.each do |n| + # v = data[n.to_sym]&.to_s&.b + # sock.write [n.to_i, v.size, v].pack( 'c na*') if v + #end + #sock.write [3].pack( 'c') + #sock.flush if 0 >= @logger.sev_threshold @logger.debug "send data #{data.inspect}" - @logger.debug "send raw #{data.inspect}" + @logger.debug "send raw #{s.inspect}" end rsock.write s rsock.flush @@ -194,8 +199,8 @@ class Knot::Protocol def read n s = @sock.read n - @str.insert -1, s - s + @str.insert -1, s unless s.nil? + s || '' end end From 2255c784bc017a38b32393edd3c4e31d9527043e Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 20:48:04 +0100 Subject: [PATCH 12/17] v0.2.1 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index 74f9cdb..a8d42ca 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.2.0" + VERSION = "0.2.1" end From e9321d4de66f0bcaba67d8a9e15f7d8444280408 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 22:44:06 +0100 Subject: [PATCH 13/17] Protocol-Types like Idx. Codes for sharing code. --- lib/knot/protocol.rb | 146 +++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 61 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 98bf7c6..cf1abf2 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -1,5 +1,7 @@ require 'iounpack' -require 'stringio' +require 'pathname' +require 'socket' +require 'logger' require_relative 'errors' module Knot @@ -72,71 +74,93 @@ end # end #end -module Knot::Protocol::Idx - class Id - include Comparable - attr_reader :name, :code, :cname, :description +class Knot::Protocol::Code + include Comparable + attr_reader :name, :code, :cname, :description - def initialize name, code, cname, description - raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name - raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code - @name, @code, @cname, @description = name, code, cname, description - freeze - end - - def === x - case x - when self.class then self == x - when Symbol then @name == x - when String then @name == x.to_sym - when Integer then @code == x - else nil - end - end - - def <=>( x) @id <=> x.id end - def to_s() @name.to_s end - def to_sym() @name end - def to_i() @code end + def initialize name, code, cname, description + raise ArgumentError, "Expecting Symbol for #{self.class.name} instead of: #{name.inspect}" unless Symbol === name + raise ArgumentError, "Expecting Integer for #{self.class.name} instead of: #{code.inspect}" unless Integer === code + @name, @code, @cname, @description = name, code, cname, description + freeze end - Idx = [ - Id.new( :command, 0x10, :CMD, 'Control command name.'), - Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'), - Id.new( :error, 0x12, :ERROR, 'Error message.'), - Id.new( :section, 0x13, :SECTION, 'Configuration section name.'), - Id.new( :item, 0x14, :ITEM, 'Configuration item name.'), - Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'), - Id.new( :zone, 0x16, :ZONE, 'Zone name.'), - Id.new( :owner, 0x17, :OWNER, 'Zone record owner'), - Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), - Id.new( :type, 0x19, :TYPE, 'Zone record type name.'), - Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), - Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), + def === x + case x + when self.class then @id == x.id + when Symbol then @name == x + when String then @name == x.to_sym + when Integer then @code == x + else nil + end + end + + def <=>( x) @id <=> x.id end + def to_s() @name.to_s end + def to_sym() @name end + def to_i() @code end +end + +module Knot::Protocol::Codes + include Enumerable + def [] k + case k + when Symbol + self::Name[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}" + when Integer + self::Code[k] or raise Knot::Errors::EINVAL, "Unknown Codes: #{k}" + else + raise ArgumentError, "Unknown Codes-Type: #{k}" + end + end + + def each &exe + block_given? ? self::Codes.each( &exe) : self::Codes.to_enum( :each) + end +end + +module Knot::Protocol::Idx + extend Knot::Protocol::Codes + + Codes = [ + Knot::Protocol::Code.new( :command, 0x10, :CMD, 'Control command name.'), + Knot::Protocol::Code.new( :flags, 0x11, :FLAGS, 'Control command flags.'), + Knot::Protocol::Code.new( :error, 0x12, :ERROR, 'Error message.'), + Knot::Protocol::Code.new( :section, 0x13, :SECTION, 'Configuration section name.'), + Knot::Protocol::Code.new( :item, 0x14, :ITEM, 'Configuration item name.'), + Knot::Protocol::Code.new( :id, 0x15, :ID, 'Congiguration item identifier.'), + Knot::Protocol::Code.new( :zone, 0x16, :ZONE, 'Zone name.'), + Knot::Protocol::Code.new( :owner, 0x17, :OWNER, 'Zone record owner'), + Knot::Protocol::Code.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), + Knot::Protocol::Code.new( :type, 0x19, :TYPE, 'Zone record type name.'), + Knot::Protocol::Code.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), + Knot::Protocol::Code.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), ] Name = {} Code = {} - Idx.each do |id| + Codes.each do |id| Code[id.to_i] = id Name[id.to_sym] = id end +end - class < Date: Tue, 13 Dec 2022 22:45:39 +0100 Subject: [PATCH 14/17] v0.3.0 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index a8d42ca..a66d07a 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.2.1" + VERSION = "0.3.0" end From 12639f766c85709309b579709f23e3e0127e8a16 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:38:43 +0100 Subject: [PATCH 15/17] Knot::Protocol#snd supports strings as key, too (calls to_sym) --- lib/knot/protocol.rb | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index cf1abf2..48dcfea 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -183,21 +183,13 @@ class Knot::Protocol def snd sock: nil, **data rsock = sock || @sock - #s = ''.b - #sock = StringIO.new s - #sock.write [1].pack( 'c') + data = Hash[ *data.map {|k,v| [k.to_sym, v]}.flatten] data[:flags] ||= '' ds = Idx. - select {|n| data[n.to_sym] }. - map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] } + select {|n| p [n.to_sym, data[n.to_sym]]; data[n.to_sym] }. + map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v] } s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b - #Idx.each do |n| - # v = data[n.to_sym]&.to_s&.b - # sock.write [n.to_i, v.size, v].pack( 'c na*') if v - #end - #sock.write [3].pack( 'c') - #sock.flush if 0 >= @logger.sev_threshold @logger.debug "send data #{data.inspect}" @logger.debug "send raw #{s.inspect}" From 95cabef56784298d709fff8704d5297613c5a6c2 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:41:16 +0100 Subject: [PATCH 16/17] v0.3.1 --- lib/knot/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/knot/version.rb b/lib/knot/version.rb index a66d07a..b29fb5c 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.3.0" + VERSION = "0.3.1" end From b21f7c319de89f346a15662f95ecfd0bdfdf1688 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Tue, 13 Dec 2022 23:43:18 +0100 Subject: [PATCH 17/17] v0.3.2 (debugging removed) --- lib/knot/protocol.rb | 2 +- lib/knot/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 48dcfea..e05a112 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -187,7 +187,7 @@ class Knot::Protocol data[:flags] ||= '' ds = Idx. - select {|n| p [n.to_sym, data[n.to_sym]]; data[n.to_sym] }. + select {|n| data[n.to_sym] }. map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v] } s = [Types[:data].to_i, ds, Types[:block].to_i].flatten.pack( "c #{'c na*'*ds.length} c").b if 0 >= @logger.sev_threshold diff --git a/lib/knot/version.rb b/lib/knot/version.rb index b29fb5c..46de30f 100644 --- a/lib/knot/version.rb +++ b/lib/knot/version.rb @@ -1,3 +1,3 @@ module Knot - VERSION = "0.3.1" + VERSION = "0.3.2" end