dns

package
v0.0.0-...-bdaf5c8 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DNS_PORT_NUMBER        = 53
	MESSAGE_PROTOCOL       = "udp"
	DOMAIN_LABEL_SEPERATOR = "."
	UDP_MESSAGE_SIZE_LIMIT = 4096
	MESSAGE_HEADER_LENGTH  = 12
	WHITESPACE             = " "
	NEWLINE_SEPERATOR      = "\n"
	ADDRESS_IPv4           = "IPv4"
	ADDRESS_IPv6           = "IPv6"
)
View Source
const (
	TYPE_A     RecordType = 1
	TYPE_NS    RecordType = 2
	TYPE_CNAME RecordType = 5
	TYPE_TXT   RecordType = 16
	TYPE_AAAA  RecordType = 28

	OPCODE_QUERY  Flag = 0
	OPCODE_IQUERY Flag = 1
	OPCODE_STATUS Flag = 2

	CLASS_IN ClassType = 1
	CLASS_CH ClassType = 3

	// DNS Query completed successfully
	RC_NOERROR ResponseCode = 0
	// DNS Query Format Error
	RC_FORMERR ResponseCode = 1
	// Server failed to complete the DNS request
	RC_SERVFAIL ResponseCode = 2
	// Domain name does not exist
	RC_NXDOMAIN ResponseCode = 3
	// Function not implemented
	RC_NOTIMP ResponseCode = 4
	// The server refused to answer for the query
	RC_REFUSED ResponseCode = 5
	// Name that should not exist, does exist
	RC_YXDOMAIN ResponseCode = 6
	// RRset that should not exist, does exist
	RC_XRRSET ResponseCode = 7
	// Server not authoritative for the zone
	RC_NOTAUTH ResponseCode = 8
	// Name not in zone
	RC_NOTZONE ResponseCode = 9

	MSG_REQUEST           MessageType = 0
	MSG_RESPONSE          MessageType = 1
	MSG_RESOLVER_RESPONSE MessageType = 2

	QR_BIT           = uint16(1 << 15)
	AA_BIT           = uint16(1 << 10)
	TR_BIT           = uint16(1 << 9)
	RD_BIT           = uint16(1 << 8)
	RA_BIT           = uint16(1 << 7)
	AUTH_BIT         = uint16(1 << 5)
	CHK_BIT          = uint16(1 << 4)
	RCODE_BITS       = uint16(15)
	OPCODE_BITS      = uint16(15 << 11)
	PTR_DETECT_VALUE = uint16(3 << 14)
	PTR_OFFSET_FETCH = uint16(65535 >> 2)
)

Variables

View Source
var ErrAuthNameServerFetch error = errors.New("unable to fetch Authoritative Name Server details")
View Source
var ErrBitCount error = errors.New("bit count for the given number is larger than the required bit count")
View Source
var ErrInvalidClassType = errors.New("class type not available")
View Source
var ErrInvalidRecordType error = errors.New("record type requested is not allowed by the resolver")
View Source
var ErrMessageTooLong error = errors.New("udp message size is too long")
View Source
var ErrNameServerFetch error = errors.New("unable to fetch Name Server details")
View Source
var ErrNotAbsolutePath error = errors.New("file path must be an absolute")
View Source
var ErrParametersMissing error = errors.New("parameters are missing")

Functions

func Canonicalize

func Canonicalize(domainName string) string

Returns a string representing the canonicalized value of given domain name.

func Id

func Id() uint16

Generates a random 16-bit integer as DNS Message Id.

func PackUInt16

func PackUInt16(number uint16) []byte

Packs a 16-bit unsigned integer into a stream of octets (or bytes) and returns them as an array of byte values.

func PackUInt32

func PackUInt32(number uint32) []byte

Packs a 16-bit unsigned integer into a stream of octets (or bytes) and returns them as an array of byte values.

func UnpackUInt16

func UnpackUInt16(buffer []byte) uint16

Unpacks a stream of bytes into a uint16 number.

func UnpackUInt32

func UnpackUInt32(buffer []byte) uint32

Unpacks a stream of bytes into a uint32 number.

Types

type AAAAResource

type AAAAResource struct {
	IPv6Address string
}

Represents a AAAA-type Resource Record body.

func (*AAAAResource) String

func (aaar *AAAAResource) String() string

Returns the string representation of AAAA-type data.

func (*AAAAResource) UnpackBody

func (aaaar *AAAAResource) UnpackBody(buffer []byte, offset int, dataLength int) int

Unpacks a stream of bytes into a AAAA-type resource record value.

type AResource

type AResource struct {
	IPv4Address string
}

Represents an A-type Resource Record value.

func (*AResource) String

func (ar *AResource) String() string

Returns the string representation of A-type record data

func (*AResource) UnpackBody

func (ar *AResource) UnpackBody(buffer []byte, offset int, dataLength int) int

Unpacks a stream of bytes into a A-type resource record value.

type BindFile

type BindFile struct {
	//Resource records present in the BIND file.
	ResourceRecords []LocalResource
	//Local file path of the BIND file
	LocalFilePath string
}

In-Memory representation of a BIND file.

func (*BindFile) Add

func (bf *BindFile) Add(name string, ttl uint32, class string, recType string, data string)

Creates a new local resource record and adds it to the BIND file if it has not already expired.

func (*BindFile) FindResources

func (bf *BindFile) FindResources(name string, recType RecordType) ([]Resource, bool)

Returns all cached records matching the given domain name and record type.

func (*BindFile) HasRecordExpired

func (bf *BindFile) HasRecordExpired(ttl uint32, LastModified time.Time) bool

Checks if the local resource is expired and returns true if it is and false if it has not expired.

func (*BindFile) Initialize

func (bf *BindFile) Initialize(filePath string) error

Initialize the attributes of BindFile instance.

func (*BindFile) Load

func (bf *BindFile) Load() error

Load the RRs from the BIND file into memory.

func (*BindFile) NewLocalResource

func (bf *BindFile) NewLocalResource(name string, ttl uint32, class string, recType string, data string, LastModified string) *LocalResource

Creates a new local resource object and returns a pointer to the object.

func (*BindFile) Resolve

func (bf *BindFile) Resolve(name string, recType RecordType) ([]Resource, bool)

Resolves the given domain name and record type using data available in the BIND file.

func (*BindFile) Sync

func (bf *BindFile) Sync() error

Persists the in-memory RR changes to the disk.

type CNAMEResource

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

Represents a CNAME-type Resouce Record body.

func (*CNAMEResource) String

func (cname *CNAMEResource) String() string

Returns the string representation of CNAME-type record value.

func (*CNAMEResource) UnpackBody

func (cname *CNAMEResource) UnpackBody(buffer []byte, offset int, dataLength int) int

Unpacks a stream of bytes into a CNAME-type resource record value.

type ClassType

type ClassType uint16

Identifies a protocol family or instance of a protocol.

func (ClassType) String

func (ct ClassType) String() string

Returns string representation of RR Class type.

type ClassTypes

type ClassTypes map[string]ClassType

Maps a class type string with its enumerated value.

var AllowedClassTypes ClassTypes = ClassTypes{
	"IN": CLASS_IN,
	"CH": CLASS_CH,
}

func (ClassTypes) GetClassType

func (clsTypes ClassTypes) GetClassType(key string) ClassType

Gets the class type associated with the given string.

type CompressionMap

type CompressionMap map[string]int

Holds the domain name and their compression offsets used while packing them into a stream of octets.

func (CompressionMap) Get

func (cmpMap CompressionMap) Get(name string) (int, bool)

Gets the offset assosciated with a domain name from the compression map.

func (CompressionMap) Set

func (cmpMap CompressionMap) Set(name string, offset int)

Sets the offset for a domain name in the compression map. If domain name does not exist already, a new entry is added to the compression map.

type DomainName

type DomainName struct {
	//Contains a byte stream representation of the domain name.
	Data []byte
	//Contains the string value representation of the domain name
	Value string
	//Contains the number of labels (domains & subdomains) in the domain name.
	Length uint8
}

Represents a domain name as defined in RFC 1035.

func (*DomainName) GetLength

func (name *DomainName) GetLength() int

Gets the byte length of the given domain name.

func (*DomainName) Initialize

func (name *DomainName) Initialize(dName string)

Initialises the instance of DomainName

func (*DomainName) Pack

func (name *DomainName) Pack(compressionMap CompressionMap, offset int) []byte

Parse the given domain name string and pack it as sequence of octets.

func (*DomainName) String

func (name *DomainName) String() string

Returns the domain name as a string.

func (*DomainName) Unpack

func (name *DomainName) Unpack(buffer []byte, offset int) int

Unpack the given byte stream and extract the domain name.

type Flag

type Flag uint16

Represents the multi-bit flags to be configured in DNS message header.

func (Flag) String

func (flg Flag) String() string

Returns the string representation of the flag value.

type Header struct {
	//Unique Identifier assigned to the DNS Request.
	Identifier uint16
	//Indicates if the DNS Message object is a request or response.
	IsResponse bool
	//Represents the type of query - Standard query or IQuery.
	Opcode Flag
	//Indicates if the RRs were returned by an authoritative server
	Authoritative bool
	//Indicates if the RRs in the DNS response are truncated.
	Truncation bool
	//Indicates if the resolver requires the target DNS server to fetch results through recursive name queries
	RecursionDesired bool
	//Indicates if the DNS server is capable of performing recursive name queries.
	RecursionAvailable bool
	//1-bit code reserved for future use.
	Zero Flag
	//1-bit code to indicate if data present after DNS header has been authenticated by the server.
	Authenticated bool
	//1-bit code to indicate if checking is disabled by the DNS Resolver.
	CheckingDisabled bool
	//Response code
	Rcode ResponseCode
	//Number of Question records in the DNS message
	QdCount uint16
	//Number of Answer records in the DNS message
	AnCount uint16
	//Number of Authoritative RRs in the DNS message
	NsCount uint16
	//Number of additional records present in the DNS message
	ArCount uint16
}

Represents the header of a DNS message.

func (*Header) Initialize

func (hdr *Header) Initialize(mt MessageType, MsgId uint16)

Initialises an instance of Header with the default values based on MessageType.

func (*Header) Pack

func (hdr *Header) Pack() []byte

Pack the header instance as a sequence of octets.

func (*Header) PackFlag

func (hdr *Header) PackFlag() []byte

Pack the flag values in Message Header as a binary string.

func (*Header) SetAdditionalRecordCount

func (hdr *Header) SetAdditionalRecordCount(count uint16)

Sets the number of Additional RRs present in the DNS Message.

func (*Header) SetAnswerCount

func (hdr *Header) SetAnswerCount(count uint16)

Sets the number of answer RRs in the DNS Message.

func (*Header) SetIdentifier

func (hdr *Header) SetIdentifier(value uint16)

Sets the Identifier value in header.

func (*Header) SetNameServerCount

func (hdr *Header) SetNameServerCount(count uint16)

Sets the number of authoritative RRs present in the DNS Message.

func (*Header) SetQuestionCount

func (hdr *Header) SetQuestionCount(count uint16)

Sets the number of questions in the DNS Message.

func (*Header) SetRecursionAvailable

func (hdr *Header) SetRecursionAvailable(value bool)

Sets the Recursion Available flag

func (*Header) SetRecursionDesired

func (hdr *Header) SetRecursionDesired(value bool)

Sets the Recursion Desired flag

func (*Header) SetResponse

func (hdr *Header) SetResponse(value bool)

Sets the flag to indicate if header belongs to DNS request or response.

func (*Header) SetResponseCode

func (hdr *Header) SetResponseCode(value ResponseCode)

Sets the response code for the DNS Response message.

func (*Header) String

func (hdr *Header) String() string

Returns the string representation of DNS Message Header.

func (*Header) Unpack

func (hdr *Header) Unpack(buffer []byte, offset int) int

Unpacks a stream of bytes to a header instance of DNS Message.

func (*Header) UnpackFlag

func (hdr *Header) UnpackFlag(buffer []byte)

Unpacks a flag byte stream into the Header instance.

type LocalResource

type LocalResource struct {
	LastModified time.Time
	// contains filtered or unexported fields
}

In-memory representation of a local resource stored in a BIND file.

func (*LocalResource) String

func (lr *LocalResource) String() string

Returns the string representation of the local resource record.

type Message

type Message struct {
	//Represents all the data present in header section of the DNS Message
	Header Header
	//Array of Question records in the DNS Message
	Questions []Question
	//Array of answer RRs present in the DNS Message.
	Answers []Resource
	//Array of authoritative RRs present in the DNS Message.
	Authoritative []Resource
	//Array of additional RRs present in the DNS Message.
	Additional []Resource
	// contains filtered or unexported fields
}

Represents a DNS Message (both Request and Response).

func NewMessage

func NewMessage(mt MessageType, MsgId uint16) *Message

Creates and returns a new Message instance.

func (*Message) AddAnswers

func (msg *Message) AddAnswers(resources []Resource)

Appends the resource records to the answers collection of the Message instance.

func (*Message) FindAdditionalRecords

func (msg *Message) FindAdditionalRecords(recType RecordType) ([]Resource, bool)

Returns the RRs from Additional section of DNS message matching the given record type.

func (*Message) FindAnswerRecords

func (msg *Message) FindAnswerRecords(recType RecordType) ([]Resource, bool)

Returns the RRs from Answer section of DNS message matching the given record type.

func (*Message) FindAuthorityRecords

func (msg *Message) FindAuthorityRecords(recType RecordType) ([]Resource, bool)

Returns the RRs from Authoritative section of DNS message matching the given record type.

func (*Message) HasAnswer

func (msg *Message) HasAnswer(name string) bool

Checks if the message contains an answer record for the given domain name.

func (*Message) HasQuestion

func (msg *Message) HasQuestion(name string) bool

Checks if the message contains a question record for the given domain name.

func (*Message) Initialize

func (msg *Message) Initialize(mt MessageType, MsgId uint16)

Initialises all the properties in the Message instance.

func (*Message) IsResponse

func (msg *Message) IsResponse(request *Message) bool

Checks if the given resource is a response for the DNS question provided in parameter.

func (*Message) NewQuestion

func (msg *Message) NewQuestion(name string, recType RecordType)

Creates a new question and adds it to the DNS Message instance.

func (*Message) Pack

func (msg *Message) Pack() []byte

Pack the message as a sequence of octets.

func (*Message) String

func (msg *Message) String() string

Returns a string representation of the DNS Message instance.

func (*Message) Unpack

func (msg *Message) Unpack(response []byte)

Unpack the sequence of bytes to a Message instance.

type MessageType

type MessageType uint8

Represents the type of DNS Message - Request or Response.

type NSResource

type NSResource struct {
	NameServer DomainName
}

Represents a NS-type Resource Record body.

func (*NSResource) String

func (ns *NSResource) String() string

Returns the string representation of NS-type record value.

func (*NSResource) UnpackBody

func (ns *NSResource) UnpackBody(buffer []byte, offset int, dataLength int) int

Unpacks a stream of bytes into a NS-type resource record value.

type Question

type Question struct {
	//Contains the domain name being queried to the DNS Server.
	Name DomainName
	//Contains the type of record being queried to the DNS Server.
	Type RecordType
	//Contains the class type of the record being queried to the DNS Server.
	Class ClassType
}

Represents a DNS Question record.

func (*Question) Pack

func (que *Question) Pack(compressionMap CompressionMap, offset int) []byte

Pack the Question instance as a sequence of octets.

func (*Question) Set

func (que *Question) Set(name string, recType RecordType)

Sets the Question instance with the given values.

func (*Question) String

func (que *Question) String() string

Returns the string representation of DNS Question instance.

func (*Question) Unpack

func (que *Question) Unpack(buffer []byte, offset int) int

Unpacks a stream of bytes to a Question instance.

type RecordType

type RecordType uint16

Represents a record type in DNS

func (RecordType) String

func (rt RecordType) String() string

Returns string representation of the DNS RecordType.

type RecordTypes

type RecordTypes map[string]RecordType

Maps a record type string with its enumerated value.

var AllowedRRTypes RecordTypes = RecordTypes{
	"A":     TYPE_A,
	"NS":    TYPE_NS,
	"CNAME": TYPE_CNAME,
	"TXT":   TYPE_TXT,
	"AAAA":  TYPE_AAAA,
}

func (RecordTypes) GetAllKeys

func (recTypes RecordTypes) GetAllKeys() []string

Gets all the keys present in RecordTypes instance.

func (RecordTypes) GetKey

func (recTypes RecordTypes) GetKey(recType RecordType) string

Fetches the key from RecordTypes instance, whose value matches 'recType'.

func (RecordTypes) GetRecordType

func (recTypes RecordTypes) GetRecordType(key string) RecordType

Gets the record type associated with the given string

type Resolver

type Resolver struct {
	//References the BIND file containing the DNS root server details.
	RootServers BindFile
	//References the BIND file containing all the cached resource records.
	Cache BindFile
	//Logger to be used to generate logs.
	Logger *log.Logger
	// contains filtered or unexported fields
}

Structure to represent a DNS Resolver.

func NewResolver

func NewResolver(RootServersPath string, CacheFilePath string, traceLogs bool) (*Resolver, error)

Returns a new instance of Resolver. In case of any errors, it returns nil instead.

func (*Resolver) Close

func (resolver *Resolver) Close()

Syncs the changes from memory to the local cache file.

func (*Resolver) GetRecordType

func (resolver *Resolver) GetRecordType(recordType string) RecordType

Returns the record type object for the given type string.

func (*Resolver) IsAllowed

func (resolver *Resolver) IsAllowed(recordType string) bool

Returns true if the record type provided is accepted by the resolver, else returns false.

func (*Resolver) Log

func (resolver *Resolver) Log(message string)

Logs information to the log file.

func (*Resolver) Resolve

func (resolver *Resolver) Resolve(name string, t RecordType)

Queries the DNS server and fetches the 't' type record for 'name'.

type Resource

type Resource struct {
	//Contains the domain name value being returned.
	Name DomainName
	//Contains the type of record represented by the RR.
	Type RecordType
	//Contains the class type represented by the RR.
	Class ClassType
	//Time-To-Live for the RR.
	TTL uint32
	//Total length (in bytes) of the Resource body.
	RdLength uint16
	//Resource Record body.
	Rdata ResourceBody
}

Represents a Resource Record in DNS.

func NewResourceRecord

func NewResourceRecord(dname string, ttl uint32, class string, recType string, data string) *Resource

Creates a new resource record and returns a pointer to the Resource instance.

func (*Resource) CacheString

func (resource *Resource) CacheString() string

Returns a string representation of the Resource instance for caching

func (*Resource) GetData

func (resource *Resource) GetData() string

Gets the value in the resource body.

func (*Resource) Initialize

func (resource *Resource) Initialize(domainName string, recType string, classType string, ttl uint32, data string)

Initialize the instance of Resource.

func (*Resource) Pack

func (resource *Resource) Pack(compressionMap CompressionMap, offset int) []byte

Packs the resource instance to a stream of bytes

func (*Resource) PackBody

func (resource *Resource) PackBody(compressionMap CompressionMap, offset int) []byte

Packs the resource data into a stream of bytes

func (*Resource) String

func (resource *Resource) String() string

Returns a string representation of the Resource instance.

func (*Resource) Unpack

func (resource *Resource) Unpack(buffer []byte, offset int) int

Unpacks a stream of bytes to a resource instance.

type ResourceBody

type ResourceBody interface {
	//Unpacks a stream of bytes into a resource record object.
	UnpackBody(buffer []byte, offset int, dataLength int) int
}

Feature(s) to be implemented for a DNS Resource Body.

type ResponseCode

type ResponseCode uint16

Represents the response code returned by the DNS server.

func (ResponseCode) String

func (rc ResponseCode) String() string

Returns the string representation of the response code.

type TXTResource

type TXTResource struct {
	TextValue string
}

Represents a TXT-type Resource Record body

func (*TXTResource) String

func (txt *TXTResource) String() string

Returns the TXT value.

func (*TXTResource) UnpackBody

func (txt *TXTResource) UnpackBody(buffer []byte, offset int, dataLength int) int

Unpacks a stream of bytes into a TXT-type resource record value.

type UdpConnect

type UdpConnect struct {
	Connection *net.UDPConn
}

Structure to manage a single UDP connection.

func (*UdpConnect) Close

func (uc *UdpConnect) Close() error

Close the given UDP connection.

func (*UdpConnect) ConnectTo

func (uc *UdpConnect) ConnectTo(RemoteAddress string, PortNumber int) error

Uses User Datagram Protocol (UDP) to connect to the remote server and port number and return the connection object.

func (*UdpConnect) Receive

func (uc *UdpConnect) Receive() ([]byte, error)

Receives a stream of bytes from the UDP connection.

func (*UdpConnect) Send

func (uc *UdpConnect) Send(buffer []byte) error

Sends the given byte stream across the UDP connection.

Jump to

Keyboard shortcuts

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