dnsmsg

package
v0.0.0-...-1d8e347 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

README

Note

Part of this package is copied and modified from golang.org/x/net/dns/dnsmessage.

This package is for fast dns message preliminary packing and unpacking with minimum allocations.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSmallBuffer = errors.New("buffer is too small")
)

Functions

func ParseName

func ParseName[T []byte | string](n *Name, s T) error

Parse a readable domain to empty n. Empty s or "." will be the root domain. Both FQDN/non-FQDN are OK. escaping "\", e.g. "\.", "\DDD", is supported.

func ParseNameLabels

func ParseNameLabels[T []byte | string](n *Name, ls []T) error

func ParseNameRaw

func ParseNameRaw[T []byte | string](n *Name, s T) error

func ReleaseA

func ReleaseA(r *A)

func ReleaseAAAA

func ReleaseAAAA(r *AAAA)

func ReleaseMX

func ReleaseMX(r *MX)

func ReleaseMsg

func ReleaseMsg(m *Msg)

func ReleaseNAME

func ReleaseNAME(r *NAMEResource)

func ReleaseName

func ReleaseName(n *Name)

Release this name to buffer pool.

func ReleaseQuestion

func ReleaseQuestion(q *Question)

func ReleaseRaw

func ReleaseRaw(r *RawResource)

func ReleaseResource

func ReleaseResource(r Resource)

func ReleaseSOA

func ReleaseSOA(r *SOA)

func ReleaseSRV

func ReleaseSRV(r *SRV)

func RemoveEDNS0

func RemoveEDNS0(m *Msg)

func UnpackHdr

func UnpackHdr(msg []byte, off int) (Header, HeaderCount, int, error)

Types

type A

type A struct {
	ResourceHdr
	A [4]byte
}

func NewA

func NewA() *A

func (*A) Copy

func (r *A) Copy() Resource

func (*A) Hdr

func (r *A) Hdr() *ResourceHdr

func (*A) Lock

func (*A) Lock()

func (*A) Unlock

func (*A) Unlock()

type AAAA

type AAAA struct {
	ResourceHdr
	AAAA [16]byte
}

func NewAAAA

func NewAAAA() *AAAA

func (*AAAA) Copy

func (r *AAAA) Copy() Resource

func (*AAAA) Hdr

func (r *AAAA) Hdr() *ResourceHdr

func (*AAAA) Lock

func (*AAAA) Lock()

func (*AAAA) Unlock

func (*AAAA) Unlock()

type Class

type Class uint16
const (
	// ResourceHeader.Class and Question.Class
	ClassINET   Class = 1
	ClassCSNET  Class = 2
	ClassCHAOS  Class = 3
	ClassHESIOD Class = 4

	// Question.Class
	ClassANY Class = 255
)
type Header struct {
	ID                 uint16
	Response           bool
	OpCode             OpCode
	Authoritative      bool
	Truncated          bool
	RecursionDesired   bool
	RecursionAvailable bool
	AuthenticData      bool
	CheckingDisabled   bool
	RCode              RCode
}

Header is a representation of a DNS message header.

func (*Header) Pack

func (m *Header) Pack() (id uint16, bits uint16)

type HeaderCount

type HeaderCount struct {
	QD uint16
	AN uint16
	NS uint16
	AD uint16
}

HeaderCount is a dns msg header section. Useful when manually unpacking a dns msg.

type MX

type MX struct {
	ResourceHdr
	Pref uint16
	MX   Name
}

func NewMX

func NewMX() *MX

func (*MX) Copy

func (r *MX) Copy() Resource

func (*MX) Hdr

func (r *MX) Hdr() *ResourceHdr

func (*MX) Lock

func (*MX) Lock()

func (*MX) Unlock

func (*MX) Unlock()

type Msg

type Msg struct {
	Header
	Questions   []*Question
	Answers     []Resource
	Authorities []Resource
	Additionals []Resource
	// contains filtered or unexported fields
}

func NewMsg

func NewMsg() *Msg

func UnpackMsg

func UnpackMsg(msg []byte) (*Msg, error)

func (*Msg) Copy

func (m *Msg) Copy() *Msg

func (*Msg) Lock

func (*Msg) Lock()

func (*Msg) MaxPackLen

func (m *Msg) MaxPackLen() (s int, err error)

Pack length if no compression.

func (*Msg) Pack

func (m *Msg) Pack(b []byte, compression bool, size int) ([]byte, error)

Pack and append data into b. Size is the msg size limit. Upon reach the limit, no rr will be packed and the msg will be "Truncated". Minimum is 512. 0 means no limit.

func (*Msg) Unlock

func (*Msg) Unlock()

func (*Msg) Unpack

func (m *Msg) Unpack(msg []byte) error

type NAMEResource

type NAMEResource struct {
	ResourceHdr
	NameData Name
}

CNAME, NS, PTR

func NewNAME

func NewNAME() *NAMEResource

func (*NAMEResource) Copy

func (r *NAMEResource) Copy() Resource

func (*NAMEResource) Hdr

func (r *NAMEResource) Hdr() *ResourceHdr

func (*NAMEResource) Lock

func (*NAMEResource) Lock()

func (*NAMEResource) Unlock

func (*NAMEResource) Unlock()

type Name

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

Name is a dns name.

func NewName

func NewName() *Name

Get a empty name from buffer pool.

func (*Name) AppendReadableTo

func (n *Name) AppendReadableTo(b []byte) []byte

Convert n from wire format to common readable format. Root domain will be '.' . Labels will be split by '.' . No '.' at the end of the name. Unprintable characters will be escaped as "\DDD". '.' and '\' will be "\.", "\\". If n is invalid, returns nil, err.

func (*Name) CopyFrom

func (n *Name) CopyFrom(n2 Name)

func (*Name) Data

func (n *Name) Data() []byte

Returns the domain name in wire format. Do not modify the it.

func (*Name) Labels

func (n *Name) Labels() [][]byte

Returns the labels. Do not modify the it.

func (*Name) Parse

func (n *Name) Parse(s string) error

func (*Name) Reset

func (n *Name) Reset()

Clear all data in n. n will become empty and unfinished.

func (*Name) ToLower

func (n *Name) ToLower()

type OpCode

type OpCode uint16

An OpCode is a DNS operation code.

type Question

type Question struct {
	Name  Name
	Type  Type
	Class Class
}

func NewQuestion

func NewQuestion() *Question

func UnpackQuestion

func UnpackQuestion(msg []byte, off int) (*Question, int, error)

func (*Question) Copy

func (q *Question) Copy() *Question

func (*Question) CopyFrom

func (q *Question) CopyFrom(q2 *Question)

func (*Question) Reset

func (q *Question) Reset()

type RCode

type RCode uint16

An RCode is a DNS response status code.

const (
	RCodeSuccess        RCode = 0 // NoError
	RCodeFormatError    RCode = 1 // FormErr
	RCodeServerFailure  RCode = 2 // ServFail
	RCodeNameError      RCode = 3 // NXDomain
	RCodeNotImplemented RCode = 4 // NotImp
	RCodeRefused        RCode = 5 // Refused
)

type RawResource

type RawResource struct {
	ResourceHdr
	Data pool.Buffer
}

func NewRaw

func NewRaw() *RawResource

func (*RawResource) Copy

func (r *RawResource) Copy() Resource

func (*RawResource) Hdr

func (r *RawResource) Hdr() *ResourceHdr

func (*RawResource) Lock

func (*RawResource) Lock()

func (*RawResource) Unlock

func (*RawResource) Unlock()

type Resource

type Resource interface {
	Hdr() *ResourceHdr

	Copy() Resource
	// contains filtered or unexported methods
}

func PopEDNS0

func PopEDNS0(m *Msg) Resource

func UnpackResource

func UnpackResource(msg []byte, off int) (Resource, int, error)

Unpack one rr starting at msg[off:]. Return unpacked rr, next offset, error.

type ResourceHdr

type ResourceHdr struct {
	Name   Name
	Type   Type
	Class  Class
	TTL    uint32
	Length uint16 // ignored when packing
	// contains filtered or unexported fields
}

func (*ResourceHdr) Lock

func (*ResourceHdr) Lock()

func (*ResourceHdr) Unlock

func (*ResourceHdr) Unlock()

type SOA

type SOA struct {
	ResourceHdr
	NS      Name
	MBox    Name
	Serial  uint32
	Refresh uint32
	Retry   uint32
	Expire  uint32
	MinTTL  uint32
}

func NewSOA

func NewSOA() *SOA

func (*SOA) Copy

func (r *SOA) Copy() Resource

func (*SOA) Hdr

func (r *SOA) Hdr() *ResourceHdr

func (*SOA) Lock

func (*SOA) Lock()

func (*SOA) Unlock

func (*SOA) Unlock()

type SRV

type SRV struct {
	ResourceHdr
	Priority uint16
	Weight   uint16
	Port     uint16
	Target   Name // Not compressed as per RFC 2782.
}

func NewSRV

func NewSRV() *SRV

func (*SRV) Copy

func (r *SRV) Copy() Resource

func (*SRV) Hdr

func (r *SRV) Hdr() *ResourceHdr

func (*SRV) Lock

func (*SRV) Lock()

func (*SRV) Unlock

func (*SRV) Unlock()

type Type

type Type uint16

A Type is a type of DNS request and response.

const (
	// ResourceHeader.Type and Question.Type
	TypeA     Type = 1
	TypeNS    Type = 2
	TypeCNAME Type = 5
	TypeSOA   Type = 6
	TypePTR   Type = 12
	TypeMX    Type = 15
	TypeTXT   Type = 16
	TypeAAAA  Type = 28
	TypeSRV   Type = 33
	TypeOPT   Type = 41

	// Question.Type
	TypeWKS   Type = 11
	TypeHINFO Type = 13
	TypeMINFO Type = 14
	TypeAXFR  Type = 252
	TypeALL   Type = 255
)

Jump to

Keyboard shortcuts

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