mirror of
https://github.com/andwati/dns-resolver.git
synced 2026-10-20 03:14:26 +00:00
implemented dns question structure and serialization
This commit is contained in:
@@ -1 +1,33 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DNSQuestion struct {
|
||||
QName string
|
||||
QType uint16
|
||||
QClass uint16
|
||||
}
|
||||
|
||||
// convert dns question into a byte slice
|
||||
func (q *DNSQuestion) Serialize() []byte {
|
||||
buf := EncodeQName(q.QName)
|
||||
question := make([]byte, 4)
|
||||
binary.BigEndian.PutUint16(question[0:2], q.QType)
|
||||
binary.BigEndian.PutUint16(question[2:4], q.QClass)
|
||||
return append(buf, question...)
|
||||
}
|
||||
|
||||
// encode a domain name to dns format
|
||||
func EncodeQName(domain string) []byte {
|
||||
parts := strings.Split(domain, ".")
|
||||
var encoded []byte
|
||||
for _, part := range parts {
|
||||
encoded = append(encoded, byte(len(part)))
|
||||
encoded = append(encoded, part...)
|
||||
}
|
||||
encoded = append(encoded, 0)
|
||||
return encoded
|
||||
}
|
||||
|
||||
@@ -1,33 +1 @@
|
||||
package main
|
||||
|
||||
type DNSHeader struct {
|
||||
ID uint16
|
||||
Flags uint16
|
||||
QDCount uint16
|
||||
ANCount uint16
|
||||
NSCount uint16
|
||||
ARCount uint16
|
||||
}
|
||||
|
||||
type DNSQuestion struct {
|
||||
QName string
|
||||
QType uint16
|
||||
QClass uint16
|
||||
}
|
||||
|
||||
type DNSRecord struct {
|
||||
Name string
|
||||
Type uint16
|
||||
Class uint16
|
||||
TTL uint32
|
||||
RDLength uint16
|
||||
RData []byte
|
||||
}
|
||||
|
||||
type DNSMessage struct {
|
||||
Header DNSHeader
|
||||
Question []DNSQuestion
|
||||
Answers []DNSRecord
|
||||
Authorities []DNSRecord
|
||||
Additionals []DNSRecord
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user