Documentation
¶
Index ¶
- Constants
- Variables
- func Canonicalize(domainName string) string
- func Id() uint16
- func PackUInt16(number uint16) []byte
- func PackUInt32(number uint32) []byte
- func UnpackUInt16(buffer []byte) uint16
- func UnpackUInt32(buffer []byte) uint32
- type AAAAResource
- type AResource
- type BindFile
- func (bf *BindFile) Add(name string, ttl uint32, class string, recType string, data string)
- func (bf *BindFile) FindResources(name string, recType RecordType) ([]Resource, bool)
- func (bf *BindFile) HasRecordExpired(ttl uint32, LastModified time.Time) bool
- func (bf *BindFile) Initialize(filePath string) error
- func (bf *BindFile) Load() error
- func (bf *BindFile) NewLocalResource(name string, ttl uint32, class string, recType string, data string, ...) *LocalResource
- func (bf *BindFile) Resolve(name string, recType RecordType) ([]Resource, bool)
- func (bf *BindFile) Sync() error
- type CNAMEResource
- type ClassType
- type ClassTypes
- type CompressionMap
- type DomainName
- type Flag
- type Header
- func (hdr *Header) Initialize(mt MessageType, MsgId uint16)
- func (hdr *Header) Pack() []byte
- func (hdr *Header) PackFlag() []byte
- func (hdr *Header) SetAdditionalRecordCount(count uint16)
- func (hdr *Header) SetAnswerCount(count uint16)
- func (hdr *Header) SetIdentifier(value uint16)
- func (hdr *Header) SetNameServerCount(count uint16)
- func (hdr *Header) SetQuestionCount(count uint16)
- func (hdr *Header) SetRecursionAvailable(value bool)
- func (hdr *Header) SetRecursionDesired(value bool)
- func (hdr *Header) SetResponse(value bool)
- func (hdr *Header) SetResponseCode(value ResponseCode)
- func (hdr *Header) String() string
- func (hdr *Header) Unpack(buffer []byte, offset int) int
- func (hdr *Header) UnpackFlag(buffer []byte)
- type LocalResource
- type Message
- func (msg *Message) AddAnswers(resources []Resource)
- func (msg *Message) FindAdditionalRecords(recType RecordType) ([]Resource, bool)
- func (msg *Message) FindAnswerRecords(recType RecordType) ([]Resource, bool)
- func (msg *Message) FindAuthorityRecords(recType RecordType) ([]Resource, bool)
- func (msg *Message) HasAnswer(name string) bool
- func (msg *Message) HasQuestion(name string) bool
- func (msg *Message) Initialize(mt MessageType, MsgId uint16)
- func (msg *Message) IsResponse(request *Message) bool
- func (msg *Message) NewQuestion(name string, recType RecordType)
- func (msg *Message) Pack() []byte
- func (msg *Message) String() string
- func (msg *Message) Unpack(response []byte)
- type MessageType
- type NSResource
- type Question
- type RecordType
- type RecordTypes
- type Resolver
- type Resource
- func (resource *Resource) CacheString() string
- func (resource *Resource) GetData() string
- func (resource *Resource) Initialize(domainName string, recType string, classType string, ttl uint32, data string)
- func (resource *Resource) Pack(compressionMap CompressionMap, offset int) []byte
- func (resource *Resource) PackBody(compressionMap CompressionMap, offset int) []byte
- func (resource *Resource) String() string
- func (resource *Resource) Unpack(buffer []byte, offset int) int
- type ResourceBody
- type ResponseCode
- type TXTResource
- type UdpConnect
Constants ¶
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" )
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 ¶
var ErrAuthNameServerFetch error = errors.New("unable to fetch Authoritative Name Server details")
var ErrBitCount error = errors.New("bit count for the given number is larger than the required bit count")
var ErrInvalidClassType = errors.New("class type not available")
var ErrInvalidRecordType error = errors.New("record type requested is not allowed by the resolver")
var ErrMessageTooLong error = errors.New("udp message size is too long")
var ErrNameServerFetch error = errors.New("unable to fetch Name Server details")
var ErrNotAbsolutePath error = errors.New("file path must be an absolute")
var ErrParametersMissing error = errors.New("parameters are missing")
Functions ¶
func Canonicalize ¶
Returns a string representing the canonicalized value of given domain name.
func PackUInt16 ¶
Packs a 16-bit unsigned integer into a stream of octets (or bytes) and returns them as an array of byte values.
func PackUInt32 ¶
Packs a 16-bit unsigned integer into a stream of octets (or bytes) and returns them as an array of byte values.
func UnpackUInt16 ¶
Unpacks a stream of bytes into a uint16 number.
func UnpackUInt32 ¶
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.
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 ¶
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 ¶
Checks if the local resource is expired and returns true if it is and false if it has not expired.
func (*BindFile) Initialize ¶
Initialize the attributes of BindFile instance.
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.
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 ClassTypes ¶
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 ¶
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.
type Header ¶
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) SetAdditionalRecordCount ¶
Sets the number of Additional RRs present in the DNS Message.
func (*Header) SetAnswerCount ¶
Sets the number of answer RRs in the DNS Message.
func (*Header) SetIdentifier ¶
Sets the Identifier value in header.
func (*Header) SetNameServerCount ¶
Sets the number of authoritative RRs present in the DNS Message.
func (*Header) SetQuestionCount ¶
Sets the number of questions in the DNS Message.
func (*Header) SetRecursionAvailable ¶
Sets the Recursion Available flag
func (*Header) SetRecursionDesired ¶
Sets the Recursion Desired flag
func (*Header) SetResponse ¶
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) UnpackFlag ¶
Unpacks a flag byte stream into the Header instance.
type LocalResource ¶
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 ¶
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 ¶
Checks if the message contains an answer record for the given domain name.
func (*Message) HasQuestion ¶
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 ¶
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.
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.
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 ¶
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 ¶
Returns true if the record type provided is accepted by the resolver, else returns false.
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 ¶
Returns a string representation of the Resource instance for caching
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
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) 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 ¶
Structure to manage a single 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.