| 1 |
package dbfile |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"sync" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns" |
| 9 |
"codeberg.org/miekg/dns/cmd/atomdns/handlers/dbfile/zone" |
| 10 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsctx" |
| 11 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnslog" |
| 12 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone" |
| 13 |
"codeberg.org/miekg/dns/dnsutil" |
| 14 |
) |
| 15 |
|
| 16 |
type Dbfile struct { |
| 17 |
Path string |
| 18 |
|
| 19 |
// Zones holds all the zone this instance of Dbfile is called for. |
| 20 |
Zones map[string]*zone.Zone |
| 21 |
sync.RWMutex // protects Zones |
| 22 |
|
| 23 |
ctx context.Context |
| 24 |
cancel context.CancelFunc |
| 25 |
|
| 26 |
To *Transfer |
| 27 |
From *Transfer |
| 28 |
} |
| 29 |
|
| 30 |
func (d *Dbfile) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc { |
| 31 |
return dns.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) { |
| 32 |
if r.Opcode == dns.OpcodeNotify { |
| 33 |
d.HandlerFuncNotify(ctx, w, r) |
| 34 |
return |
| 35 |
} |
| 36 |
if _, qtype := dnsutil.Question(r); qtype == dns.TypeAXFR || qtype == dns.TypeIXFR { |
| 37 |
d.HandlerFuncTransfer(ctx, w, r) |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
z := d.Zone(dns.Zone(ctx)) |
| 42 |
m := dnszone.Retrieve(z, r, nil) |
| 43 |
|
| 44 |
m = dnsctx.Funcs(ctx, m) |
| 45 |
if err := m.Pack(); err != nil { |
| 46 |
dnslog.PackFail(ctx, log(), Err(err)) |
| 47 |
} |
| 48 |
io.Copy(w, m) |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
func (d *Dbfile) Zone(origin string) *zone.Zone { |
| 53 |
d.RLock() |
| 54 |
z := d.Zones[origin] |
| 55 |
d.RUnlock() |
| 56 |
return z |
| 57 |
} |