2020-06-20 22:30:05 +02:00
|
|
|
require 'iounpack'
|
2022-09-27 20:58:19 +02:00
|
|
|
require 'stringio'
|
2020-06-20 22:30:05 +02:00
|
|
|
require_relative 'errors'
|
|
|
|
|
|
|
|
module Knot
|
|
|
|
class Protocol
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Knot::Protocol::Type
|
|
|
|
def [] code
|
|
|
|
case code
|
|
|
|
when 0, End then return End
|
|
|
|
when 1, Data then return Data
|
|
|
|
when 2, Extra then return Extra
|
|
|
|
when 3, Block then return Block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Base
|
|
|
|
def self.expect_data?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def expect_data?() self.class.expect_data? end
|
|
|
|
def code() self.class.code end
|
|
|
|
end
|
|
|
|
class End < Base
|
|
|
|
def self.code() 0 end
|
|
|
|
end
|
|
|
|
class Data < Base
|
|
|
|
def code() 1 end
|
|
|
|
def expect_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
attr_reader :data
|
|
|
|
def initialize data
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class Extra < Base
|
|
|
|
def code() 2 end
|
|
|
|
def expect_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
attr_reader :data
|
|
|
|
def initialize data
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class Block < Base
|
|
|
|
def code() 3 end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-27 20:58:19 +02:00
|
|
|
#class Knot::KnotC
|
|
|
|
# attr_accessor :debug, :binary
|
|
|
|
#
|
|
|
|
# def initialize path = nil, binary: nil
|
|
|
|
# @path = path
|
|
|
|
# @binary = binary || 'knotc'
|
|
|
|
# @conf = Knot::Conf.new self
|
|
|
|
# @zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def call command:, flags: nil, section: nil, item: nil, id: nil, zone: nil, owner: nil, ttl: nil, type: nil, data: nil, filter: nil
|
|
|
|
# cs =
|
|
|
|
# case command.to_s
|
|
|
|
# when 'conf-begin', 'conf-commit', 'conf-abort', 'status', 'stop', 'reload'
|
|
|
|
# [@binary, command, ]
|
|
|
|
# else raise ArgumentError, "Unknown Command: #{command}"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#end
|
|
|
|
|
2020-06-20 22:30:05 +02:00
|
|
|
module Knot::Protocol::Idx
|
2022-09-28 11:01:29 +02:00
|
|
|
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
|
|
|
|
|
2020-06-20 22:30:05 +02:00
|
|
|
Idx = [
|
2022-09-28 11:01:29 +02:00
|
|
|
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.'),
|
2020-06-20 22:30:05 +02:00
|
|
|
]
|
|
|
|
Name = {}
|
|
|
|
Code = {}
|
2022-09-28 11:01:29 +02:00
|
|
|
|
|
|
|
Idx.each do |id|
|
|
|
|
Code[id.to_i] = id
|
|
|
|
Name[id.to_sym] = id
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|
2022-09-28 11:01:29 +02:00
|
|
|
|
|
|
|
class <<self
|
|
|
|
def [] k
|
|
|
|
case k
|
|
|
|
when Symbol
|
|
|
|
Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
|
|
|
when Integer
|
|
|
|
Code[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown Idx-Type: #{k}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each &exe
|
|
|
|
block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Knot::Protocol
|
|
|
|
attr_reader :sock, :conf, :zones
|
|
|
|
attr_accessor :debug
|
|
|
|
|
|
|
|
def initialize path_or_sock = nil
|
|
|
|
case path_or_sock
|
|
|
|
when String, Pathname
|
|
|
|
@sock = UNIXSocket.new path_or_sock.to_s
|
|
|
|
when Socket
|
|
|
|
@sock = path_or_sock
|
|
|
|
when nil
|
|
|
|
@sock = UNIXSocket.new '/run/knot/knot.sock'
|
|
|
|
end
|
|
|
|
@debug = false
|
|
|
|
@conf = Knot::Conf.new self
|
|
|
|
@zones = Hash.new {|h, zone| h[zone] = Knot::Zone.new zone, self }
|
|
|
|
end
|
|
|
|
|
|
|
|
def snd sock: nil, **data
|
|
|
|
rsock = sock || @sock
|
2022-09-28 11:01:29 +02:00
|
|
|
s = ''.b
|
2020-06-20 22:30:05 +02:00
|
|
|
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
|
|
|
|
end
|
|
|
|
sock.write [3].pack( 'c')
|
|
|
|
sock.flush
|
2022-09-28 11:01:29 +02:00
|
|
|
STDERR.puts( {send: data, _: s}.inspect) if @debug
|
2020-06-20 22:30:05 +02:00
|
|
|
rsock.write s
|
|
|
|
rsock.flush
|
2022-09-27 20:58:19 +02:00
|
|
|
end
|
2020-06-20 22:30:05 +02:00
|
|
|
|
|
|
|
class RecordIO
|
|
|
|
attr_reader :str
|
|
|
|
|
|
|
|
def initialize sock, str = nil
|
2022-09-28 11:01:29 +02:00
|
|
|
@str, @sock = str || ''.b, sock
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def unpack pattern
|
2022-09-28 11:01:29 +02:00
|
|
|
IOUnpack.new( pattern).unpack self
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def unpack1 pattern
|
2022-09-28 11:01:29 +02:00
|
|
|
IOUnpack.new( pattern).unpack1 self
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def read n
|
|
|
|
s = @sock.read n
|
|
|
|
@str.insert -1, s
|
|
|
|
s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rcv sock: nil
|
|
|
|
ret, r = [], nil
|
|
|
|
sock = sock || @sock
|
|
|
|
sock = RecordIO.new sock if @debug
|
|
|
|
loop do
|
|
|
|
t = sock.unpack1 'c'
|
|
|
|
case t
|
|
|
|
when 0, 3
|
|
|
|
return ret
|
|
|
|
when 1, 2
|
|
|
|
type = t
|
|
|
|
ret.push( r = {})
|
2022-09-27 20:58:19 +02:00
|
|
|
else
|
2020-06-20 22:30:05 +02:00
|
|
|
raise Knot::Errors::EINVAL, "Missing Type before: #{t}" if ret.empty?
|
|
|
|
i = Idx::Idx[t - 0x10] or raise Knot::Errors::EINVAL, "Unknown index: #{t-0x10}"
|
|
|
|
l = sock.unpack1 'n'
|
|
|
|
r[i] = sock.read( l)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
2022-09-28 11:01:29 +02:00
|
|
|
STDERR.puts( {rcvd: ret, _: sock.str}.inspect) if @debug
|
2020-06-20 22:30:05 +02:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def call sock: nil, **data
|
|
|
|
snd sock: sock, **data
|
2022-09-27 20:58:19 +02:00
|
|
|
rcv( sock: sock).each do |r|
|
2020-06-20 22:30:05 +02:00
|
|
|
if r[:error]
|
|
|
|
if e = Knot::Errors.err2exc[r[:error]]
|
|
|
|
raise e, r[:error]
|
|
|
|
end
|
|
|
|
raise Knot::Error, r[:error]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def zone( zone) @zones[zone.to_s.to_sym] end
|
|
|
|
|
2022-09-27 20:58:19 +02:00
|
|
|
def conf_set( **opts) call **opts.update( command: 'conf-set') end
|
|
|
|
def conf_unset( **opts) call **opts.update( command: 'conf-unset') end
|
2020-06-20 22:30:05 +02:00
|
|
|
|
2022-09-27 20:58:19 +02:00
|
|
|
def zone_set( **opts) call **opts.update( command: 'zone-set') end
|
2022-06-17 12:46:37 +02:00
|
|
|
def zone_unset( **opts) call **opts.update( command: 'zone-unset') end
|
2022-09-27 20:58:19 +02:00
|
|
|
def zone_get( **opts) call **opts.update( command: 'zone-get') end
|
2020-06-20 22:30:05 +02:00
|
|
|
end
|