begin implementing dns frontend
This commit is contained in:
parent
6ad3c63866
commit
ad75934761
2 changed files with 77 additions and 0 deletions
63
node/lux_dns.go
Normal file
63
node/lux_dns.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package node
|
||||
|
||||
import "net"
|
||||
|
||||
type LuxDnsServer struct {
|
||||
node *LuxNode
|
||||
stopChan chan bool
|
||||
}
|
||||
|
||||
func NewLuxDnsServer(node *LuxNode) LuxDnsServer {
|
||||
return LuxDnsServer{
|
||||
node: node,
|
||||
stopChan: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sv *LuxDnsServer) CreateFrontend(udpListen string) error {
|
||||
listenAddr, err := net.ResolveUDPAddr("udp", udpListen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
conn, err := net.ListenUDP("udp", listenAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func(sv *LuxDnsServer) {
|
||||
defer conn.Close()
|
||||
|
||||
buf := make([]byte, 512)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-sv.stopChan:
|
||||
return
|
||||
default:
|
||||
n, addr, err := conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
log.Debugf("failed to recv dns udp: %v\n", err)
|
||||
}
|
||||
|
||||
res := sv.HandleRequest(buf[:n])
|
||||
if len(res) > 0 {
|
||||
_, err := conn.WriteToUDP(res, addr)
|
||||
if err != nil {
|
||||
log.Debugf("failed to send dns reply: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}(sv)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sv *LuxDnsServer) Stop() {
|
||||
sv.stopChan <- true
|
||||
}
|
||||
|
|
@ -89,6 +89,20 @@ func (node *LuxNode) GetStateLock() *sync.RWMutex {
|
|||
return &node.stateLock
|
||||
}
|
||||
|
||||
func (node *LuxNode) GetHostByName(name string) (LuxHostState, bool) {
|
||||
node.stateLock.RLock()
|
||||
defer node.stateLock.RUnlock()
|
||||
|
||||
for _, host := range node.state.hosts {
|
||||
if host.State.Hostname == name {
|
||||
// explicitly make a copy to avoid concurrency problems
|
||||
return *host, true
|
||||
}
|
||||
}
|
||||
|
||||
return LuxHostState{}, false
|
||||
}
|
||||
|
||||
func (node *LuxNode) GetHostStateChannel() <-chan LuxHostState {
|
||||
return node.state.GetStateChannel()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue