forked from docker-archive/dnsserver
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Erik Hollensbe <github@hollensbe.org>
- Loading branch information
Erik Hollensbe
committed
Jan 4, 2020
1 parent
77d0957
commit e827786
Showing
5 changed files
with
199 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
// DB is for pluggable backends. To swap out the DB implementation, implement | ||
// this interface and provide it at construction time to the DNS server struct. | ||
type DB interface { | ||
SetA(string, net.IP) error | ||
GetA(string) (net.IP, error) | ||
DeleteA(string) | ||
SetSRV(string, *SRVRecord) error | ||
GetSRV(string) (*SRVRecord, error) | ||
DeleteSRV(string) | ||
Close() error | ||
} | ||
|
||
// ErrNotFound is for when the record cannot be located | ||
var ErrNotFound = errors.New("not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package db | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
) | ||
|
||
// Map is a simple in-memory map of DNS entries. These are 1:1 entries, no | ||
// multiple address forms are permitted. | ||
type Map struct { | ||
aRecords map[string]net.IP // FQDN -> IP | ||
srvRecords map[string]*SRVRecord // service (e.g., _test._tcp) -> SRV | ||
aMutex sync.RWMutex // mutex for A record operations | ||
srvMutex sync.RWMutex // mutex for SRV record operations | ||
} | ||
|
||
// NewMap makes a new *Map. | ||
func NewMap() *Map { | ||
return &Map{ | ||
aRecords: map[string]net.IP{}, | ||
srvRecords: map[string]*SRVRecord{}, | ||
} | ||
} | ||
|
||
// Close does nothing. | ||
func (m *Map) Close() error { | ||
return nil | ||
} | ||
|
||
// SetA overwrites or sets the A record for the entry. | ||
func (m *Map) SetA(host string, ip net.IP) error { | ||
m.aMutex.Lock() | ||
m.aRecords[host] = ip | ||
m.aMutex.Unlock() | ||
return nil | ||
} | ||
|
||
// DeleteA deletes an A record for a host. Note that this is not the FQDN, but a hostname. | ||
func (m *Map) DeleteA(host string) { | ||
m.aMutex.Lock() | ||
delete(m.aRecords, host) | ||
m.aMutex.Unlock() | ||
} | ||
|
||
// GetA retrieves an A record by FQDN. | ||
func (m *Map) GetA(fqdn string) (net.IP, error) { | ||
m.aMutex.RLock() | ||
defer m.aMutex.RUnlock() | ||
val, ok := m.aRecords[fqdn] | ||
if !ok { | ||
return nil, ErrNotFound | ||
} | ||
|
||
return val, nil | ||
} | ||
|
||
// SetSRV sets a srv record with service and protocol pointing at a name and port. | ||
func (m *Map) SetSRV(spec string, srv *SRVRecord) error { | ||
m.srvMutex.Lock() | ||
m.srvRecords[spec] = srv | ||
m.srvMutex.Unlock() | ||
return nil | ||
} | ||
|
||
// GetSRV gets a service based on a name | ||
func (m *Map) GetSRV(spec string) (*SRVRecord, error) { | ||
m.srvMutex.RLock() | ||
defer m.srvMutex.RUnlock() | ||
|
||
srv, ok := m.srvRecords[spec] | ||
if !ok { | ||
return nil, ErrNotFound | ||
} | ||
|
||
return srv, nil | ||
} | ||
|
||
// DeleteSRV deletes a SRV record based on the service and protocol. | ||
func (m *Map) DeleteSRV(spec string) { | ||
m.srvMutex.Lock() | ||
delete(m.srvRecords, spec) | ||
m.srvMutex.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package db | ||
|
||
// SRVRecord encapsulates the data segment of a SRV record. Priority and Weight | ||
// are always 0 in our SRV records. | ||
type SRVRecord struct { | ||
Port uint16 | ||
Host string | ||
} | ||
|
||
// Equal tests if the srvrecords are equal. | ||
func (s SRVRecord) Equal(s2 SRVRecord) bool { | ||
return s.Port == s2.Port && s.Host == s2.Host | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.