This commit is contained in:
Miek Gieben 2012-08-25 19:57:25 +02:00
parent c146770f82
commit 23d4972158
1 changed files with 13 additions and 1 deletions

14
zone.go
View File

@ -5,6 +5,7 @@ package dns
import (
"github.com/miekg/radix"
"strings"
"sync"
)
// Zone represents a DNS zone. Currently there is no locking implemented.
@ -12,6 +13,7 @@ type Zone struct {
Origin string // Origin of the zone
Wildcard int // Whenever we see a wildcard name, this is incremented
*radix.Radix // Zone data
mutex *sync.RWMutex
}
// ZoneData holds all the RRs having their ownername equal to Name.
@ -20,7 +22,7 @@ type ZoneData struct {
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
NonAuth bool // Always false, except for NSsets that differ from z.Origin
mutex *sync.RWMutex // lock for reading/writing
mutex *sync.RWMutex
}
// toRadixName reverses a domainname so that when we store it in the radix tree
@ -59,8 +61,10 @@ func (z *Zone) Insert(r RR) error {
}
key := toRadixName(r.Header().Name)
z.mutex.Lock()
zd := z.Radix.Find(key)
if zd == nil {
defer z.mutex.Unlock()
// Check if its a wildcard name
if len(r.Header().Name) > 1 && r.Header().Name[0] == '*' && r.Header().Name[1] == '.' {
z.Wildcard++
@ -85,6 +89,9 @@ func (z *Zone) Insert(r RR) error {
z.Radix.Insert(key, zd)
return nil
}
z.mutex.Unlock()
zd.Value.(*ZoneData).mutex.Lock()
defer zd.Value.(*ZoneData).mutex.Unlock()
// Name already there
switch t := r.Header().Rrtype; t {
case TypeRRSIG:
@ -105,10 +112,15 @@ func (z *Zone) Insert(r RR) error {
// this is a no-op.
func (z *Zone) Remove(r RR) error {
key := toRadixName(r.Header().Name)
z.mutex.Lock()
zd := z.Radix.Find(key)
if zd == nil {
defer z.mutex.Unlock()
return nil
}
z.mutex.Unlock()
zd.Value.(*ZoneData).mutex.Lock()
defer zd.Value.(*ZoneData).mutex.Unlock()
remove := false
switch t := r.Header().Rrtype; t {
case TypeRRSIG: