Add IsStarted method, update SetZoneConfig for concurrency

This commit is contained in:
George 2020-05-19 16:04:12 -04:00
parent 5c5e938742
commit cad772d305
2 changed files with 42 additions and 12 deletions

View File

@ -83,9 +83,7 @@ func main() {
return return
case _ = <-r: case _ = <-r:
if *useconffile != "" { if *useconffile != "" {
s.Stop()
s.LoadConfig(*useconffile) s.LoadConfig(*useconffile)
s.Start()
} }
} }
} }

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"strings" "strings"
"sync"
"github.com/gologme/log" "github.com/gologme/log"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -78,10 +79,15 @@ func ParseZoneConfigMap(zoneConfigMap map[string][]string) (map[string][]dns.RR,
type MeshnameServer struct { type MeshnameServer struct {
log *log.Logger log *log.Logger
listenAddr string listenAddr string
zoneConfig map[string][]dns.RR
dnsClient *dns.Client dnsClient *dns.Client
dnsServer *dns.Server dnsServer *dns.Server
networks map[string]*net.IPNet networks map[string]*net.IPNet
zoneConfigLock sync.RWMutex
zoneConfig map[string][]dns.RR
startedLock sync.RWMutex
started bool
} }
func (s *MeshnameServer) Init(log *log.Logger, listenAddr string) { func (s *MeshnameServer) Init(log *log.Logger, listenAddr string) {
@ -97,13 +103,23 @@ func (s *MeshnameServer) Init(log *log.Logger, listenAddr string) {
} }
func (s *MeshnameServer) Stop() error { func (s *MeshnameServer) Stop() error {
if s.dnsServer != nil { s.startedLock.Lock()
defer s.startedLock.Unlock()
if s.started == true {
s.dnsServer.Shutdown() s.dnsServer.Shutdown()
} s.started = false
return nil return nil
} else {
return errors.New("MeshnameServer is not running")
}
} }
func (s *MeshnameServer) Start() error { func (s *MeshnameServer) Start() error {
s.startedLock.Lock()
defer s.startedLock.Unlock()
if s.started == false {
s.dnsServer = &dns.Server{Addr: s.listenAddr, Net: "udp"} s.dnsServer = &dns.Server{Addr: s.listenAddr, Net: "udp"}
for tld, subnet := range s.networks { for tld, subnet := range s.networks {
dns.HandleFunc(tld, s.handleRequest) dns.HandleFunc(tld, s.handleRequest)
@ -111,19 +127,27 @@ func (s *MeshnameServer) Start() error {
} }
go s.dnsServer.ListenAndServe() go s.dnsServer.ListenAndServe()
s.log.Infoln("Started meshnamed on:", s.listenAddr) s.log.Infoln("Started meshnamed on:", s.listenAddr)
s.started = true
return nil return nil
} else {
return errors.New("MeshnameServer is already started")
}
} }
func (s *MeshnameServer) LoadConfig(confPath string) { func (s *MeshnameServer) LoadConfig(confPath string) {
if zoneConf, err := ParseConfigFile(confPath); err == nil { if zoneConf, err := ParseConfigFile(confPath); err == nil {
s.zoneConfigLock.Lock()
s.zoneConfig = zoneConf s.zoneConfig = zoneConf
s.zoneConfigLock.Unlock()
} else { } else {
s.log.Errorln("Can't parse config file:", err) s.log.Errorln("Can't parse config file:", err)
} }
} }
func (s *MeshnameServer) SetZoneConfig(zoneConfig map[string][]dns.RR) { func (s *MeshnameServer) SetZoneConfig(zoneConfig map[string][]dns.RR) {
s.zoneConfigLock.Lock()
s.zoneConfig = zoneConfig s.zoneConfig = zoneConfig
s.zoneConfigLock.Unlock()
} }
func (s *MeshnameServer) SetNetworks(networks map[string]*net.IPNet) { func (s *MeshnameServer) SetNetworks(networks map[string]*net.IPNet) {
@ -135,6 +159,7 @@ func (s *MeshnameServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
s.zoneConfigLock.RLock()
for _, q := range r.Question { for _, q := range r.Question {
labels := dns.SplitDomainName(q.Name) labels := dns.SplitDomainName(q.Name)
if len(labels) < 2 { if len(labels) < 2 {
@ -166,6 +191,7 @@ func (s *MeshnameServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
} }
} }
} }
s.zoneConfigLock.RUnlock()
for remoteServer, questions := range remoteLookups { for remoteServer, questions := range remoteLookups {
rm := new(dns.Msg) rm := new(dns.Msg)
@ -186,3 +212,9 @@ func (s *MeshnameServer) isRemoteLookupAllowed(addr net.Addr) bool {
return strings.HasPrefix(ra, "[::1]:") || strings.HasPrefix(ra, "127.0.0.1:") return strings.HasPrefix(ra, "[::1]:") || strings.HasPrefix(ra, "127.0.0.1:")
} }
func (s *MeshnameServer) IsStarted() bool {
s.startedLock.RLock()
started := s.started
s.startedLock.RUnlock()
return started
}