dns/zone.go

128 lines
3.4 KiB
Go
Raw Normal View History

2012-07-15 06:01:52 +10:00
package dns
// A structure for handling zone data
import (
2012-08-08 19:08:25 +10:00
"github.com/miekg/radix"
2012-08-02 00:30:45 +10:00
"strings"
2012-07-15 06:01:52 +10:00
)
2012-08-22 01:21:47 +10:00
// Zone represents a DNS zone. Currently there is no locking implemented.
2012-07-15 06:01:52 +10:00
type Zone struct {
2012-07-17 03:46:16 +10:00
Origin string // Origin of the zone
2012-08-17 16:29:45 +10:00
Wildcard int // Whenever we see a wildcard name, this is incremented
2012-07-16 02:11:17 +10:00
*radix.Radix // Zone data
2012-07-15 06:01:52 +10:00
}
2012-07-17 05:16:42 +10:00
// ZoneData holds all the RRs having their ownername equal to Name.
2012-07-15 06:01:52 +10:00
type ZoneData struct {
2012-08-05 16:13:09 +10:00
Name string // Domain name for this node
RR map[uint16][]RR // Map of the RR type to the RR
Signatures map[uint16][]*RR_RRSIG // DNSSEC signatures for the RRs, stored under type covered
2012-08-02 00:30:45 +10:00
// Always false, except for NSsets that differ from z.Origin
2012-07-17 05:20:58 +10:00
NonAuth bool
2012-07-15 06:54:49 +10:00
}
2012-08-02 00:30:45 +10:00
// toRadixName reverses a domainname so that when we store it in the radix tree
// we preserve the nsec ordering of the zone (this idea was stolen from NSD).
// each label is also lowercased.
func toRadixName(d string) string {
2012-08-05 16:13:09 +10:00
if d == "." {
return "."
}
2012-08-02 00:30:45 +10:00
s := ""
for _, l := range SplitLabels(d) {
2012-08-04 02:28:00 +10:00
s = strings.ToLower(l) + "." + s
2012-08-02 00:30:45 +10:00
}
2012-08-05 16:13:09 +10:00
return "." + s
2012-08-02 00:30:45 +10:00
}
2012-07-17 05:31:52 +10:00
// NewZone creates an initialized zone with Origin set to origin.
2012-07-17 03:09:50 +10:00
func NewZone(origin string) *Zone {
2012-07-17 03:46:16 +10:00
if origin == "" {
origin = "."
}
if _, _, ok := IsDomainName(origin); !ok {
return nil
}
2012-07-15 06:54:49 +10:00
z := new(Zone)
2012-07-17 03:46:16 +10:00
z.Origin = Fqdn(origin)
2012-07-16 02:11:17 +10:00
z.Radix = radix.New()
2012-07-15 06:54:49 +10:00
return z
}
2012-08-02 00:30:45 +10:00
// Insert inserts an RR into the zone. Duplicate data overwrites the old data.
2012-07-17 05:16:42 +10:00
func (z *Zone) Insert(r RR) error {
2012-08-10 05:05:20 +10:00
if !IsSubDomain(z.Origin, r.Header().Name) {
2012-07-17 05:16:42 +10:00
return &Error{Err: "out of zone data", Name: r.Header().Name}
2012-07-17 03:46:16 +10:00
}
2012-08-02 00:30:45 +10:00
key := toRadixName(r.Header().Name)
zd := z.Radix.Find(key)
2012-07-16 02:11:17 +10:00
if zd == nil {
2012-08-10 07:00:51 +10:00
// Check if its a wildcard name
if len(r.Header().Name) > 1 && r.Header().Name[0] == '*' && r.Header().Name[1] == '.' {
z.Wildcard++
}
2012-07-16 02:11:17 +10:00
zd := new(ZoneData)
zd.Name = r.Header().Name
2012-07-16 03:16:39 +10:00
zd.RR = make(map[uint16][]RR)
zd.Signatures = make(map[uint16][]*RR_RRSIG)
2012-07-16 02:11:17 +10:00
switch t := r.Header().Rrtype; t {
case TypeRRSIG:
sigtype := r.(*RR_RRSIG).TypeCovered
zd.Signatures[sigtype] = append(zd.Signatures[sigtype], r.(*RR_RRSIG))
2012-07-17 05:20:58 +10:00
case TypeNS:
// NS records with other names than z.Origin are non-auth
2012-07-17 05:24:05 +10:00
if r.Header().Name != z.Origin {
2012-07-17 05:20:58 +10:00
zd.NonAuth = true
}
fallthrough
2012-07-16 02:11:17 +10:00
default:
zd.RR[t] = append(zd.RR[t], r)
}
2012-08-02 00:30:45 +10:00
z.Radix.Insert(key, zd)
2012-07-17 05:24:05 +10:00
return nil
2012-07-16 02:11:17 +10:00
}
2012-07-17 05:24:05 +10:00
// Name already there
2012-07-16 02:11:17 +10:00
switch t := r.Header().Rrtype; t {
case TypeRRSIG:
sigtype := r.(*RR_RRSIG).TypeCovered
zd.Value.(*ZoneData).Signatures[sigtype] = append(zd.Value.(*ZoneData).Signatures[sigtype], r.(*RR_RRSIG))
2012-07-17 05:20:58 +10:00
case TypeNS:
2012-07-17 05:24:05 +10:00
if r.Header().Name != z.Origin {
zd.Value.(*ZoneData).NonAuth = true
2012-07-17 05:20:58 +10:00
}
fallthrough
2012-07-16 02:11:17 +10:00
default:
zd.Value.(*ZoneData).RR[t] = append(zd.Value.(*ZoneData).RR[t], r)
2012-07-16 02:11:17 +10:00
}
2012-07-17 05:24:05 +10:00
return nil
2012-07-15 06:54:49 +10:00
}
2012-08-22 01:21:47 +10:00
// Remove removes the RR r from the zone. If there RR can not be found,
// this is a no-op. TODO(mg): not implemented.
2012-07-17 05:24:05 +10:00
func (z *Zone) Remove(r RR) error {
2012-08-10 07:00:51 +10:00
// Wildcards
2012-07-17 05:24:05 +10:00
return nil
2012-07-15 06:01:52 +10:00
}
2012-08-04 02:28:00 +10:00
2012-08-22 01:21:47 +10:00
// Find looks up the ownername s in the zone and returns the
// data when found or nil when nothing is found.
2012-08-04 02:28:00 +10:00
func (z *Zone) Find(s string) *ZoneData {
2012-08-05 04:56:57 +10:00
zd := z.Radix.Find(toRadixName(s))
if zd == nil {
2012-08-04 02:28:00 +10:00
return nil
}
2012-08-05 04:56:57 +10:00
return zd.Value.(*ZoneData)
}
2012-08-22 01:21:47 +10:00
// Predecessor searches the zone for a name shorter than s.
2012-08-05 04:56:57 +10:00
func (z *Zone) Predecessor(s string) *ZoneData {
zd := z.Radix.Predecessor(toRadixName(s))
2012-08-04 02:28:00 +10:00
if zd == nil {
return nil
}
return zd.Value.(*ZoneData)
}