SampleDNSTool is the DNS protocol Golang implementation. Currently, it has implemented simple A/CNAME records
Address: gitee.com/pdudo/Sampl…
As the operation and maintenance of small white, I have learned DNS related protocols for a long time, always feel poor, behind I want to write a simple DNS server out, so write the tool, write a blog record, because SampleDNSTool supports CNAME.
Construct the request message using SampleDNSTool
You only need to call GenerateHeaders and GenerateQuestion to build the message
Construct [query A record message of www.baidu.com]
package main
import (
"fmt"
"time"
"gitee.com/pdudo/SampleDNSTool"
)
func main(a) {
// Define DnsInfo
var dnsInfo SampleDNSTool.DNSInfo
// Construct request message: request header and request message
/ / heade parts
dnsInfo.Header.ID = uint16(time.Now().Unix())
// Qcount: indicates the problem bar honey carried in the packet
dnsInfo.Header.QCOUNT = 1
/ / question
// Define the QNAME of the query
dnsInfo.QueryInfo.QNAMEString = "www.baidu.com"
// Define QType and QClass
dnsInfo.QueryInfo.QTYPE = 1
dnsInfo.QueryInfo.QCLASS = 1
// Generate Header and question packets respectively
header := dnsInfo.GenerateHeaders()
requests := dnsInfo.GenerateQuestion()
// Assemble the message
var requestBuf []byte
requestBuf = append(requestBuf, header...)
requestBuf = append(requestBuf,requests...)
fmt.Println("Request message:" , requestBuf)
}
Copy the code
Obtain the response packet using SampleDNSTool
Send the packet request in [1] to the DNS server to obtain its response packet information
Package the main import (" FMT "" log" ".net "" strconv" "time" "gitee.com/pdudo/SampleDNSTool") func main () {/ / define DnsInfo information Var dnsInfo samplednstool. dnsInfo Dnsinfo.header. ID = uint16(time.now ().unix ())) Article message carried in the problem of the honey dnsInfo. Header. QCOUNT = 1 / part/question / / definition of the query QNAME dnsInfo. QueryInfo. QNAMEString = "www.baidu.com" / / definition QType and QClass dnsInfo.queryInfo. QType = 1 dnsinfo.queryinfo. QClass = 1 // Generate Header and question packet Header := Dnsinfo.generateheaders () requests := dnsinfo.generateQuestion () var requestBuf []byte requestBuf = append(requestBuf, header...) requestBuf = append(requestBuf,requests...) FMT. Printf (" query domain: % s \ t, "dnsInfo. QueryInfo. QNAMEString) switch dnsInfo. QueryInfo. QTYPE {case 1: the FMT. Printf (" query types: A record \n\n")} // send appeal message udpConn, err := net.dialudp ("udp",nil, &net.udpaddr {IP: net.ip {114,114,114,114}, Port: 53, }) if err ! = nil { log.Panic("net dial udp error " , Write(requestBuf) // Waiting for the DNS server to respond buf := make([]byte,1024) n, err := udpconn. Read(buf) if err! = nil { log.Println("udp conn read error " , Answer := dnsInfo.getAnswerInfo (buf[:n]) // Prints the parsed packet value fmt.Println(" Query result: ") for _,v := range answer {fmt.Printf(" request domain name: %s \t", v.name) switch v.type {case 1: fmt.Printf(" request type: %s \t", v.name) A record \t") case 5: fmt.Printf(" request type: CNAME record \t")} switch v.TYPE {case 1: var ipString string for I :=0; i<len(v.RDATA)-1; i++ { ipString += strconv.Itoa(int(uint8(v.RDATA[i]))) + "." } ipString += Strconv.itoa (int(uint8(v.data [len(v.data)-1])))) FMT.Printf(" uint8(v.data [len(v.data)-1])) %s \t" , v.RDATAString) } fmt.Println() } }Copy the code
The execution result
Domain name: www.baidu.com Query type: A Record Query result: Request domain name: www.baidu.com Request type: CNAME Record result value: www.a.shifen.com. Request domain name: www.a.shifen.com Request type: A Record result Value: 14.215.177.39 Request domain name: www.a.shifen.com Request type: A Record result value: 14.215.177.38Copy the code
How about using this tool to write A DNS server that returns A record in half an hour
The code is placed at gitee: gitee.com/pdudo/Sampl…
Slip away! Slip away!