dnsutils

package module
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 12 Imported by: 1

README

dnsutils

codecov Go Report Card

  • Utility struct and function for miekg/dns

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSupport returns when method is not implemented.
	ErrNotSupport = fmt.Errorf("not support")
	// ErrNotChangeAble returns by change methods when can not change values.
	ErrNotChangeAble = fmt.Errorf("not changeable")
)
View Source
var (
	// ErrBadName returns when name is not domain name.
	ErrBadName = fmt.Errorf("bad name")
	// ErrNotDirectlyName returns by AddChildNode when arg node is not child name
	ErrNotDirectlyName = fmt.Errorf("add name's label count must be equals to parent label count +1")
	// ErrNotInDomain returns when arg node is in-domain.
	ErrNotInDomain = fmt.Errorf("name is not subdomain")
	// ErrChildExist returns when already exist arg node's name node.
	ErrChildExist = fmt.Errorf("child name is exist")
	// ErrNameNotEqual returns when arg name node's name is not equal.
	ErrNameNotEqual = fmt.Errorf("name not equals")
	// ErrClassNotEqual returns when arg name node's class is not equal.
	ErrClassNotEqual = fmt.Errorf("class not equals")
	// ErrConflictCNAME returns by SetRRSet when there is more than one SOA RDATA.
	ErrConflictCNAME = fmt.Errorf("name node can't set both CNAME and other")
	// ErrConflictDNAME returns by SetRRSet when there is more than one RDATA RDATA.
	ErrConflictDNAME = fmt.Errorf("name node can't set both DNAME and other")
	// ErrRemoveItself by RemoveChildNameNode when remove itself.
	ErrRemoveItself = fmt.Errorf("can not remove itself")
)
View Source
var (
	// ErrTTL returns when rrset's ttl and rr's ttl are not equals.
	ErrTTLNotEqual = fmt.Errorf("not equals ttl")
	// ErrRRType returns when rrset's rrtype and rr's rrtype are not equals.
	ErrRRTypeNotEqual = fmt.Errorf("not equals rrtype")
	// ErrConflict returns when there is more than one SOA RDATA or CNAME RDATA.
	ErrConflict = fmt.Errorf("conflict RR")
	// ErrInvalid returns when class or type is invalid format.
	ErrInvalid = fmt.Errorf("invalid data")
	// ErrFormat returns when input invalid format data.
	ErrFormat = fmt.Errorf("input format error")
)
View Source
var (
	// ErrRdata returns when rdata is invalid while parsing RDATA.
	ErrRdata = dns.ErrRdata

	// ErrNotTreeBroken returns tree is broken
	ErrNameTreeBroken = fmt.Errorf("name tree broken")
)

Functions

func ConvertClassToString added in v0.4.0

func ConvertClassToString(i dns.Class) string

ConvertClassToString returns DNS Class string by dns.Class

func ConvertNumberToString added in v1.1.0

func ConvertNumberToString[T constraints.Integer](def map[T]string, prefix string, i T) string

func ConvertStringToClass added in v0.4.0

func ConvertStringToClass(s string) (dns.Class, error)

ConvertStringToClass returns dns.Class by string If it failed to parse, returns ErrInvalid

func ConvertStringToType added in v0.4.0

func ConvertStringToType(s string) (uint16, error)

ConvertStringToType returns uint16 dns rrtype by string If it failed to parse, returns ErrInvalid

func ConvertToStringToNumber added in v1.1.0

func ConvertToStringToNumber[T constraints.Integer](def map[string]T, prefix, s string) (T, error)

func ConvertTypeToString added in v0.4.0

func ConvertTypeToString(i uint16) string

ConvertTypeToString returns RRType string by uint16 dns rrtype.

func Equals

func Equals(a, b string) bool

Equals check that both names are equal. Input names can accept non-normalized name.

func GetAllParentNames added in v1.6.0

func GetAllParentNames(name string, level uint) ([]string, bool)

GetAllParentNames returns a name slice containing parent names and itself.

func GetRDATA

func GetRDATA(rr dns.RR) string

GetRDATA returns RDATA from dns.RR

func GetRDATASlice added in v0.4.0

func GetRDATASlice(rrset RRSetInterface) []string

GetRDATASlice returns RDATA from rrset

func IsCompleteEqualsRRSet added in v0.2.2

func IsCompleteEqualsRRSet(a, b RRSetInterface) bool

IsCompleteEqualsRRSet check that both rrset equal.

func IsENT

func IsENT(n NameNodeInterface) bool

IsENT check that node is empty non terminal.

func IsEmptyRRSet

func IsEmptyRRSet(set RRSetInterface) bool

IsEmptyRRSet check that rrset is empty. if rrset is nil, it returns false. if radata is empty, return false. other than that return true.

func IsEqualsRRSet

func IsEqualsRRSet(a, b RRSetInterface) bool

IsEqualsRRSet check that both rrset equal.However ttl value will be ignored.

func IsHostname added in v0.7.0

func IsHostname(name string) bool

IsHostname checks if name is a valid RFC1123 hostname.

func MakeRR

func MakeRR(r RRSetInterface, rdata string) (dns.RR, error)

MakeRR returns dns.RR by RRSet and rdata string

func MarshalJSONRRSet added in v0.4.0

func MarshalJSONRRSet(set RRSetInterface) ([]byte, error)

MarshalJSONRRset returns json.RawMessage by rrset.

func RemoveNameNode added in v0.4.0

func RemoveNameNode(n NameNodeInterface, name string) error

RemoveNameNode remove NameNodeInterface from tree.

func SetNameNode added in v0.4.0

func SetNameNode(n, nn NameNodeInterface, generator NameNodeGenerator) error

SetNameNode adds NameNode into tree. if not exist parent, create ENT NameNodeInterface by newFunc.s if exist same node, it overrides children and rrests.

func SetRdata added in v0.4.0

func SetRdata(set RRSetInterface, rdata []string) error

SetRdata set rdata into rrset

Types

type DefaultGenerator added in v0.4.0

type DefaultGenerator struct{}

func (DefaultGenerator) NewNameNode added in v0.4.0

func (DefaultGenerator) NewNameNode(name string, class dns.Class) (NameNodeInterface, error)

func (DefaultGenerator) NewRRSet added in v0.4.0

func (DefaultGenerator) NewRRSet(name string, ttl uint32, class dns.Class, rrtype uint16) (RRSetInterface, error)

type Generator added in v0.4.0

type Generator interface {
	NameNodeGenerator
	RRSetGenerator
}

type NameNode

type NameNode struct {
	sync.Mutex
	// contains filtered or unexported fields
}

NameNode is implement of NameNodeInterface

func NewNameNode

func NewNameNode(name string, class dns.Class) (*NameNode, error)

NewNameNode create NameNode

func (*NameNode) AddChildNameNode added in v0.4.0

func (n *NameNode) AddChildNameNode(nn NameNodeInterface) error

AddChildNameNode is implement of NameNodeInterface.AddChildNameNode

func (*NameNode) CopyChildNodes

func (n *NameNode) CopyChildNodes() map[string]NameNodeInterface

CopyChildNodes is implement of NameNodeInterface.CopyChildNodes

func (*NameNode) CopyRRSetMap

func (n *NameNode) CopyRRSetMap() map[uint16]RRSetInterface

CopyRRSetMap is implement of NameNodeInterface.CopyRRSetMap

func (*NameNode) GetClass

func (n *NameNode) GetClass() dns.Class

GetClass is implement of NameNodeInterface.GetClass

func (*NameNode) GetName

func (n *NameNode) GetName() string

GetName is implement of NameNodeInterface.GetName

func (*NameNode) GetNameNode

func (n *NameNode) GetNameNode(name string) (node NameNodeInterface, strict bool)

GetNameNode is implement of NameNodeInterface.GetNameNode

func (*NameNode) GetRRSet

func (n *NameNode) GetRRSet(rrtype uint16) RRSetInterface

GetRRSet is implement of NameNodeInterface.GetRRSet

func (*NameNode) IterateNameNode

func (n *NameNode) IterateNameNode(f func(NameNodeInterface) error) error

IterateNameNode is implement of NameNodeInterface.IterateNameNode sort order using sort.StringSlice Sort.

func (*NameNode) IterateNameRRSet

func (n *NameNode) IterateNameRRSet(f func(RRSetInterface) error) error

IterateNameRRSet is implement of NameNodeInterface.IterateNameRRSet first order is SOA. Other than, sort order by ASC.

func (*NameNode) RRSetLen

func (n *NameNode) RRSetLen() int

RRSetLen is implement of NameNodeInterface.RRSetLen

func (*NameNode) RemoveChildNameNode added in v0.4.0

func (n *NameNode) RemoveChildNameNode(name string) error

RemoveChildNameNode is implement of NameNodeInterface.AddChildNameNode

func (*NameNode) RemoveRRSet

func (n *NameNode) RemoveRRSet(rrtype uint16) error

RemoveRRSet is implement of NameNodeInterface.RemoveRRSet

func (*NameNode) SetRRSet

func (n *NameNode) SetRRSet(set RRSetInterface) error

SetRRSet is implement of NameNodeInterface.SetRRSet

func (*NameNode) SetValue

func (n *NameNode) SetValue(nn NameNodeInterface) error

SetValue is implement of NameNodeInterface.SetValue

type NameNodeGenerator added in v0.4.0

type NameNodeGenerator interface {
	NewNameNode(name string, class dns.Class) (NameNodeInterface, error)
}

type NameNodeInterface

type NameNodeInterface interface {
	// GetName returns canonical name
	GetName() string
	// GetName returns class
	GetClass() dns.Class

	// GetNameNode returns NameNode by target name
	// if return value isStrict is true, NameNode is target name NameNode. (strict match)
	// if isStrict is false and node nos it nil, node is nearly parrent path node. (loose match)
	// if isStrict is false and node is nil, target name is not in-domain.
	GetNameNode(target string) (node NameNodeInterface, isStrict bool)

	// CopyChildNodes returns child name node map
	// map key is canonical name
	CopyChildNodes() map[string]NameNodeInterface

	// CopyRRSetMap returns rrset map
	// map key is uint16 rrtype
	CopyRRSetMap() map[uint16]RRSetInterface

	// GetRRSet returns rrset by rrtype
	// if not exist rrset return nil
	GetRRSet(rrtype uint16) RRSetInterface

	// IterateNameRRSet can iterate function by RRSetInterface
	// sort oreder is implementation dependent.
	IterateNameRRSet(func(RRSetInterface) error) error

	// IterateNameNode can iterate function by NameNodeInterface
	// sort oreder is implementation dependent.
	IterateNameNode(func(NameNodeInterface) error) error

	// AddChildNode adds child node into children.
	AddChildNameNode(NameNodeInterface) error

	// RemoveNameNode removed child node.
	RemoveChildNameNode(name string) error

	// SetValue override child and rrsetMap
	SetValue(NameNodeInterface) error

	// SetRRSet overrides rrset
	SetRRSet(RRSetInterface) error

	// RemoveRRSet removes rrset
	RemoveRRSet(rrtype uint16) error

	// RRSetLen returns the number of not empty rrset
	RRSetLen() int
}

NameNodeInterface manages node of name tree

func GetNameNodeOrCreate added in v0.2.2

func GetNameNodeOrCreate(n NameNodeInterface, name string, generator NameNodeGenerator) (NameNodeInterface, error)

GetNameNodeOrCreate returns name node from arg name node. if exist NameNode, returns it. if not exist NameNode, It create new NameNode and return it. but new NameNode is not link to from arg name node. Maybe you can use SetNameNode.

type RRSet

type RRSet struct {
	// contains filtered or unexported fields
}

RRSet is implement of RRSetInterface

func NewRRSet

func NewRRSet(name string, ttl uint32, class dns.Class, rrtype uint16, rrs []dns.RR) (*RRSet, error)

NewRRSet creates RRSet. Returns ErrBadName when name is not domain name

func NewRRSetFromRR

func NewRRSetFromRR(rr dns.RR) *RRSet

NewRRSetFromRR creates RRSet from dns.RR If rr is nil return nil

func NewRRSetFromRRs added in v0.2.2

func NewRRSetFromRRs(rrs []dns.RR) *RRSet

NewRRSetFromRRs creates RRSet from []dns.RR. It creates RRset by first RR using NewRRSetFromRR. 2nd and subsequent RR are add rrset using RRSet.AddRR. If RRSet.AddRR failed, return nil If rrs is nil return nil

func (*RRSet) AddRR

func (r *RRSet) AddRR(rr dns.RR) error

AddRR add resource record rr is must equals al of name,ttl,class and rrtype. if duplicate RDATA, It will be ignored. It returns err when any of name, ttl, class and rrtype not equal. It returns err when rtype is SOA or CNAME, and it number is multiple.

func (*RRSet) Copy

func (r *RRSet) Copy() RRSetInterface

Copy returns copy rrset

func (*RRSet) GetClass

func (r *RRSet) GetClass() dns.Class

GetClass returns dns.Class

func (*RRSet) GetName

func (r *RRSet) GetName() string

GetName returns canonical name

func (*RRSet) GetRRs

func (r *RRSet) GetRRs() []dns.RR

GetRRs returns []dns.RR

func (*RRSet) GetRRtype

func (r *RRSet) GetRRtype() uint16

GetRRtype returns rtype

func (*RRSet) GetTTL

func (r *RRSet) GetTTL() uint32

GetTTL returns ttl

func (*RRSet) Len

func (r *RRSet) Len() int

Len returns number of rdata

func (*RRSet) MarshalJSON added in v0.4.0

func (r *RRSet) MarshalJSON() ([]byte, error)

MarshalJSON returns json.RawMessage.

func (*RRSet) RemoveRR

func (r *RRSet) RemoveRR(rr dns.RR) error

RemoveRR removes resource record if not match rr. It will be ignored.

func (*RRSet) SetTTL

func (r *RRSet) SetTTL(ttl uint32) error

SetTTL changes RRSet.ttl. And changes all of RRSet rr ttl.

func (*RRSet) UnmarshalJSON added in v0.4.0

func (r *RRSet) UnmarshalJSON(bs []byte) error

UnmarshalJSON reads rrset data from json.RawMessage.

type RRSetGenerator added in v0.4.0

type RRSetGenerator interface {
	NewRRSet(name string, ttl uint32, class dns.Class, rrtype uint16) (RRSetInterface, error)
}

type RRSetInterface

type RRSetInterface interface {
	// GetName returns canonical name
	GetName() string

	// GetRRtype returns rrtype
	GetRRtype() uint16

	// GetClass returns dns.Class
	GetClass() dns.Class

	// GetTTL returns rtype
	GetTTL() uint32

	// SetTTL sets ttl
	SetTTL(uint32) error

	// GetRRs returns rr slice
	GetRRs() []dns.RR

	// Len returns number of rdata
	Len() int

	// AddRR adds dns.RR
	AddRR(dns.RR) error

	// RemoveRR removes dns.RR
	RemoveRR(dns.RR) error

	// Copy returns a copy
	Copy() RRSetInterface
}

RRSetInterface manages rrset

func GetRRSetOrCreate

func GetRRSetOrCreate(n NameNodeInterface, rrtype uint16, ttl uint32, generator RRSetGenerator) (RRSetInterface, error)

GetRRSetOrCreate returns rrset from name node. if exist rrset, returns it. if not exist rrset, It create new rrset and return it. but new rrset is not link to NameNode. Maybe you can use SetRRSet.

type Zone

type Zone struct {
	// contains filtered or unexported fields
}

Zone is implement of ZoneInterface

func NewZone

func NewZone(name string, class dns.Class, generator Generator) (*Zone, error)

NewZone creates Zone. returns ErrBadName when name is not domain name

func (*Zone) GetClass

func (z *Zone) GetClass() dns.Class

GetClass returns zone class

func (*Zone) GetGenerator added in v0.4.0

func (z *Zone) GetGenerator() Generator

GetGenerator returns Generator

func (*Zone) GetName

func (z *Zone) GetName() string

GetName returns canonical zone name

func (*Zone) GetRootNode

func (z *Zone) GetRootNode() NameNodeInterface

GetRootNode returns zone apex NameNode If zone is not created by NewZone, maybe it returns nil

func (*Zone) MarshalJSON added in v0.4.0

func (z *Zone) MarshalJSON() ([]byte, error)

MarshalJSON returns json.RawMessage.

func (*Zone) Read

func (z *Zone) Read(r io.Reader) error

Read reads zone data from zonefile (RFC1035) It overrides zone's name and class when root node not exist.

func (*Zone) UnmarshalJSON added in v0.4.0

func (z *Zone) UnmarshalJSON(bs []byte) error

UnmarshalJSON reads zone data from json.RawMessage. It override zone's name and class when root node not exist.

type ZoneInterface

type ZoneInterface interface {
	// return canonical zone name
	GetName() string
	GetRootNode() NameNodeInterface
	GetClass() dns.Class
}

ZoneInterface manages zone root node

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL