summaryrefslogtreecommitdiff
path: root/utils/dns_utils.go
diff options
context:
space:
mode:
authorMDK <[email protected]>2024-03-25 17:15:25 +0800
committerMDK <[email protected]>2024-03-25 17:15:25 +0800
commit7115311c42ccd8a7565a5f8728bcaa18bf20bf02 (patch)
tree48eaac3c73914db31600fa8c9e96d6ffbf22c0f6 /utils/dns_utils.go
programs for edns, svcb, https support measurement
Diffstat (limited to 'utils/dns_utils.go')
-rw-r--r--utils/dns_utils.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/utils/dns_utils.go b/utils/dns_utils.go
new file mode 100644
index 0000000..4a4131d
--- /dev/null
+++ b/utils/dns_utils.go
@@ -0,0 +1,62 @@
+// dns utils
+package utils
+
+import (
+ "github.com/miekg/dns"
+)
+
+type DNSOptions struct {
+ Domain string
+ RD bool
+ Qclass uint16
+ Qtype uint16
+ EDNS bool
+}
+
+// build the question section of a dns packet
+func questionMaker(domain string, qclass uint16, qtype uint16) *dns.Question {
+ return &dns.Question{Name: dns.Fqdn(domain), Qtype: qtype, Qclass: qclass}
+}
+
+// build a specific query message
+func queryMaker(domain string, rd bool, qclass uint16, qtype uint16, edns bool) *dns.Msg {
+ msg := new(dns.Msg)
+ msg.Id = dns.Id()
+ msg.RecursionDesired = rd
+ msg.Question = make([]dns.Question, 1)
+ msg.Question[0] = *questionMaker(domain, qclass, qtype)
+ if edns {
+ msg = msg.SetEdns0(1232, false)
+ }
+ return msg
+}
+
+// query and receive the response
+// addr must contain dest port
+// func DNSQuery(addr string, domain string, rd bool, qclass uint16, qtype uint16, edns bool) (*dns.Msg, error) {
+func DNSQuery(addr string, opt DNSOptions) (*dns.Msg, error) {
+ if opt.Qclass == 0 {
+ opt.Qclass = 1
+ }
+ if opt.Qtype == 0 {
+ opt.Qtype = 1
+ }
+ msg := queryMaker(opt.Domain, opt.RD, opt.Qclass, opt.Qtype, opt.EDNS)
+ res, err := dns.Exchange(msg, addr)
+ return res, err
+}
+
+func AsyncDNSQuery(addr string, domain string, rd bool, qclass uint16, qtype uint16, edns bool) error {
+ msg := queryMaker(domain, rd, qclass, qtype, edns)
+ client := dns.Client{Net: "udp"}
+ conn, err := client.Dial(addr)
+ if err != nil {
+ return err
+ }
+ defer conn.Close()
+ err = conn.WriteMsg(msg)
+ if err != nil {
+ return err
+ }
+ return nil
+}