summaryrefslogtreecommitdiff
path: root/dns/cilent.go
blob: 989bb4dfc70a9b35f767164cd16153d1e2763eee (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package dns

import (
	"github.com/miekg/dns"

	"code.crute.me/mcrute/go_ddns_manager/bind"
)

type DNSClient struct {
	Server string
}

type DNSTransaction struct {
	zone *bind.Zone
	key  *bind.Key
	msg  *dns.Msg
}

func (t *DNSTransaction) Upsert(rrs ...RR) *DNSTransaction {
	t.RemoveAll(rrs...).Insert(rrs...)
	return t
}

func (t *DNSTransaction) Insert(rrs ...RR) *DNSTransaction {
	t.msg.Insert(toRRSet(t.zone, rrs...))
	return t
}

func (t *DNSTransaction) Remove(rrs ...RR) *DNSTransaction {
	t.msg.Remove(toRRSet(t.zone, rrs...))
	return t
}

func (t *DNSTransaction) RemoveAll(rrs ...RR) *DNSTransaction {
	t.msg.RemoveRRset(toRRSet(t.zone, rrs...))
	return t
}

func (c *DNSClient) AXFR(zone *bind.Zone) (chan *dns.Envelope, error) {
	k := zone.Keys()[0]
	t := &dns.Transfer{TsigSecret: k.AsMap()} // Always uses tcp

	m := &dns.Msg{}
	m.SetAxfr(zone.Name)
	k.Sign(m)

	return t.In(m, c.Server)
}

func (c *DNSClient) StartUpdate(zone *bind.Zone) *DNSTransaction {
	m := &dns.Msg{}
	m.SetUpdate(zone.Name)

	return &DNSTransaction{
		zone: zone,
		key:  zone.Keys()[0],
		msg:  m,
	}
}

func (c *DNSClient) SendUpdate(t *DNSTransaction) error {
	udp := &dns.Client{Net: "udp", TsigSecret: t.key.AsMap()}
	tcp := &dns.Client{Net: "tcp", TsigSecret: t.key.AsMap()}

	t.msg.SetEdns0(4096, false)
	t.key.Sign(t.msg)

	in, _, err := udp.Exchange(t.msg, c.Server)
	if in != nil && in.Truncated {
		// If the TCP request succeeds, the err will reset to nil
		in, _, err = tcp.Exchange(t.msg, c.Server)
	}

	if err != nil {
		return err
	}

	return nil
}