| 1 |
package dbhost |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"sync" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns" |
| 9 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsctx" |
| 10 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnslog" |
| 11 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone" |
| 12 |
"codeberg.org/miekg/dns/dnsutil" |
| 13 |
) |
| 14 |
|
| 15 |
type Dbhost struct { |
| 16 |
Path string |
| 17 |
ttl uint32 |
| 18 |
|
| 19 |
Data map[string]dnszone.Node |
| 20 |
sync.RWMutex // protects Data |
| 21 |
|
| 22 |
ctx context.Context |
| 23 |
cancel context.CancelFunc |
| 24 |
} |
| 25 |
|
| 26 |
func (d *Dbhost) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc { |
| 27 |
return dns.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) { |
| 28 |
// dbhosts can only answer ptr, a, and aaaa questions |
| 29 |
qname, qtype := dnsutil.Question(r) |
| 30 |
if qtype != dns.TypeA && qtype != dns.TypeAAAA && qtype != dns.TypePTR { |
| 31 |
next.ServeDNS(ctx, w, r) |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
d.RLock() |
| 36 |
n, ok := d.Data[dnsutil.Canonical(qname)] |
| 37 |
d.RUnlock() |
| 38 |
if !ok { |
| 39 |
// we only own the exact name |
| 40 |
next.ServeDNS(ctx, w, r) |
| 41 |
return |
| 42 |
} |
| 43 |
m := r.Copy() |
| 44 |
dnsutil.SetReply(m, r) |
| 45 |
for _, rr := range n.RRs { |
| 46 |
if dns.RRToType(rr) == qtype { |
| 47 |
m.Answer = append(m.Answer, rr) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
m = dnsctx.Funcs(ctx, m) |
| 52 |
if err := m.Pack(); err != nil { |
| 53 |
dnslog.PackFail(ctx, log(), Err(err)) |
| 54 |
} |
| 55 |
io.Copy(w, m) |
| 56 |
}) |
| 57 |
} |