dnsmsg

package
v0.0.0-...-264f9d0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: GPL-3.0 Imports: 7 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 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)

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 ToLowerName

func ToLowerName(n []byte) error

func ToReadable

func ToReadable(n []byte) (pool.Buffer, error)

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.

Types

type A

type A struct {
	ResourceHdr
	A [4]byte
}

func NewA

func NewA() *A

func (*A) Hdr

func (r *A) Hdr() *ResourceHdr

type AAAA

type AAAA struct {
	ResourceHdr
	AAAA [16]byte
}

func NewAAAA

func NewAAAA() *AAAA

func (*AAAA) Hdr

func (r *AAAA) Hdr() *ResourceHdr

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 MX

type MX struct {
	ResourceHdr
	Pref uint16
	MX   Name
}

func NewMX

func NewMX() *MX

func (*MX) Hdr

func (r *MX) Hdr() *ResourceHdr

type Msg

type Msg struct {
	Header
	Questions   []*Question
	Answers     []Resource
	Authorities []Resource
	Additionals []Resource
}

func NewMsg

func NewMsg() *Msg

func UnpackMsg

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

func (*Msg) Len

func (m *Msg) Len() (l int)

Len is the msg length without compression.

func (*Msg) Pack

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

Pack m into b. Returns the msg size. compression == true will only compress rr header. 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. The size of b should be m.Len(). If b is not big enough, an error will be returned. Without compression, the msg will have the size of m.Len(). TODO: Calculate compressed length?

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) Hdr

func (r *NAMEResource) Hdr() *ResourceHdr

type Name

type Name []byte

Name is a reusable buffer from bytespool.

func (Name) PackLen

func (n Name) PackLen() int

Always returns 1~255.

type NameBuilder

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

func (*NameBuilder) AppendLabel

func (b *NameBuilder) AppendLabel(s []byte) error

func (*NameBuilder) Data

func (b *NameBuilder) Data() []byte

func (*NameBuilder) Parse

func (b *NameBuilder) Parse(labels [][]byte) error

func (*NameBuilder) ParseReadable

func (b *NameBuilder) ParseReadable(s []byte) error

Empty s or "." will be the root domain. Both FQDN/non-FQDN are OK.

Note: escaping ("\.", "\DDD" etc.) is not supported yet and will be parsed as part of the label. Which is not as expected. TODO: Support escaping or return an error.

func (*NameBuilder) Reset

func (b *NameBuilder) Reset()

func (*NameBuilder) ToName

func (b *NameBuilder) ToName() Name

type NameScanner

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

func NewNameScanner

func NewNameScanner(n []byte) NameScanner

func (*NameScanner) Err

func (s *NameScanner) Err() error

func (*NameScanner) Label

func (s *NameScanner) Label() []byte

func (*NameScanner) LabelOff

func (s *NameScanner) LabelOff() int

func (*NameScanner) Scan

func (s *NameScanner) Scan() bool

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 (*Question) Copy

func (q *Question) Copy() *Question

func (*Question) Len

func (q *Question) Len() int

no compression Len

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) Hdr

func (r *RawResource) Hdr() *ResourceHdr

type Resource

type Resource interface {
	Hdr() *ResourceHdr
	// contains filtered or unexported methods
}

func PopEDNS0

func PopEDNS0(m *Msg) Resource

type ResourceHdr

type ResourceHdr struct {
	Name   Name
	Type   Type
	Class  Class
	TTL    uint32
	Length uint16 // When unpacking is field is ignored.
}

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) Hdr

func (r *SOA) Hdr() *ResourceHdr

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) Hdr

func (r *SRV) Hdr() *ResourceHdr

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