summaryrefslogtreecommitdiff
path: root/utils/dns_utils.go
blob: 4a4131da063870208e03d36343ca0e25f287a804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
}