Debian Codegraph @

cmd/atomdns/handlers/dbfile/notify.go

raw
1 package dbfile
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "log/slog"
8 "net"
9 "slices"
10 "time"
11
12 "codeberg.org/miekg/dns"
13 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnslog"
14 "codeberg.org/miekg/dns/cmd/atomdns/internal/localaddr"
15 "codeberg.org/miekg/dns/dnsutil"
16 )
17
18 // Transfer holds all the information to perform in incoming or outgoing zone transfer.
19 // The families from IPs, notifies and sources will be matched upon sending the actual notifies.
20 type Transfer struct {
21 IPs []string
22
23 TSIG *dns.TSIG
24 TSIGSecret string // base64
25
26 Notifies []string
27 Sources []string
28 }
29
30 func (d *Dbfile) HandlerFuncNotify(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) {
31 if !slices.Contains(d.From.IPs, dnsutil.RemoteIP(w)) {
32 return // ignore request
33 }
34 m := new(dns.Msg)
35 dnsutil.SetReply(m, r)
36 m.Authoritative = true
37 m.Data = r.Data
38 m.Pack()
39 io.Copy(w, m)
40
41 z := d.Zone(dns.Zone(ctx))
42 apex := z.Apex()
43 serial := uint32(0)
44 for _, rr := range apex.RRs {
45 if s, ok := rr.(*dns.SOA); ok {
46 serial = s.Serial
47 break
48 }
49 }
50 if !d.From.AvailableFrom(z.Origin(), serial) {
51 log().With(slog.Uint64("serial", uint64(serial))).Warn("Notify seen, but no newer zone available", "zone", z.Origin())
52 return
53 }
54
55 d.TransferIn(dns.Zone(ctx)) // TODO(miek): error handling
56 }
57
58 // Notify will send notifies to all configured to IP addresses.
59 func (t *Transfer) Notify(origin string) error {
60 if len(t.IPs) == 0 {
61 return nil
62 }
63
64 m := new(dns.Msg)
65 m.Authoritative = true
66 m.Opcode = dns.OpcodeNotify
67 dnsutil.SetQuestion(m, origin, dns.TypeSOA)
68
69 var lasterr error
70 for _, ip := range t.IPs {
71 if err := notify(m, ip, t.Sources); err != nil {
72 lasterr = err
73 }
74 }
75 alog := log().With("upstreams", dnslog.GroupValues("upstream", t.IPs), "zone", origin)
76 alog.Debug("Sent notifies")
77 return lasterr
78 }
79
80 func notify(m *dns.Msg, ip string, sources []string) error {
81 c := new(dns.Client)
82 c.Transport = dns.NewTransport()
83 host, _, _ := net.SplitHostPort(ip)
84
85 family := dnsutil.IPv4Family
86 if net.ParseIP(host).To4() == nil {
87 family = dnsutil.IPv6Family
88 }
89
90 c.Dialer.LocalAddr = &net.UDPAddr{IP: localaddr.Source(family, sources)}
91 for range 2 {
92 alog := log().With("upstream", ip, "zone", m.Question[0].Header().Name)
93 r, _, err := c.Exchange(context.TODO(), m, "udp", ip)
94 if err != nil {
95 alog.Error("Failed to sent notify", Err(err))
96 time.Sleep(time.Second)
97 continue
98 }
99 if r.Rcode == dns.RcodeSuccess {
100 return nil
101 }
102 time.Sleep(time.Second)
103 }
104 return fmt.Errorf("upstream %s did not accept our notify for zone %s", ip, m.Question[0].Header().Name)
105 }
106
107 // AvailableFrom return true if the "other side" has a newer SOA then we have. The first IP that answers
108 // with a higher serial is enough to return true.
109 func (t *Transfer) AvailableFrom(origin string, serial uint32) bool {
110 c := dns.NewClient()
111 m := dns.NewMsg(origin, dns.TypeSOA)
112
113 for _, ip := range t.IPs {
114 alog := log().With("upstream", ip, "zone", origin)
115 m, _, err := c.Exchange(context.TODO(), m, "tcp", ip)
116 if err != nil {
117 alog.Error("Upstream did not accept our query", Err(err))
118 continue
119 }
120 for _, rr := range m.Answer {
121 if s, ok := rr.(*dns.SOA); ok {
122 if dns.CompareSerial(serial, s.Serial) == -1 {
123 alog.Debug("Upstream serial is higher than ours", "serial", serial, "upstream-serial", s.Serial)
124 return true
125 }
126 }
127 }
128 }
129 return false
130 }