2019-01-24 02:54:21 +01:00
|
|
|
package respondd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
2019-05-03 17:48:17 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/respond"
|
2019-01-24 02:54:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (d *Daemon) Start() {
|
2019-04-10 13:18:05 +02:00
|
|
|
d.dataByInterface = make(map[string]*data.ResponseData)
|
|
|
|
if d.AnswerByZones == nil {
|
|
|
|
d.AnswerByZones = make(map[string]*AnswerConfig)
|
2019-01-24 02:54:21 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 19:41:44 +02:00
|
|
|
d.dataMX.Lock()
|
2019-01-24 02:54:21 +01:00
|
|
|
d.updateData()
|
2019-06-16 19:41:44 +02:00
|
|
|
d.dataMX.Unlock()
|
2019-01-24 02:54:21 +01:00
|
|
|
go d.updateWorker()
|
|
|
|
|
2019-05-18 23:33:13 +02:00
|
|
|
if d.Babel != "" {
|
|
|
|
go d.babelConnect()
|
|
|
|
}
|
|
|
|
|
2019-01-24 02:54:21 +01:00
|
|
|
for _, listen := range d.Listen {
|
|
|
|
var socket *net.UDPConn
|
|
|
|
var err error
|
2019-05-03 17:48:17 +02:00
|
|
|
addrString := listen.Address
|
|
|
|
if addrString == "" {
|
|
|
|
addrString = respond.MulticastAddressDefault
|
|
|
|
}
|
|
|
|
port := listen.Port
|
|
|
|
if port == 0 {
|
|
|
|
port = respond.PortDefault
|
|
|
|
} else if port < 0 {
|
|
|
|
port = 0
|
|
|
|
}
|
|
|
|
addr := net.ParseIP(addrString)
|
2019-01-24 02:54:21 +01:00
|
|
|
|
|
|
|
if addr.IsMulticast() {
|
|
|
|
var iface *net.Interface
|
|
|
|
if listen.Interface != "" {
|
|
|
|
iface, err = net.InterfaceByName(listen.Interface)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if socket, err = net.ListenMulticastUDP("udp6", iface, &net.UDPAddr{
|
|
|
|
IP: addr,
|
2019-05-03 17:48:17 +02:00
|
|
|
Port: port,
|
2019-01-24 02:54:21 +01:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if socket, err = net.ListenUDP("udp6", &net.UDPAddr{
|
|
|
|
IP: addr,
|
2019-05-03 17:48:17 +02:00
|
|
|
Port: port,
|
2019-01-24 02:54:21 +01:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go d.handler(socket)
|
|
|
|
}
|
|
|
|
log.Debug("all listener started")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) updateWorker() {
|
|
|
|
c := time.Tick(d.DataInterval.Duration)
|
|
|
|
|
|
|
|
for range c {
|
2019-06-16 19:41:44 +02:00
|
|
|
d.dataMX.Lock()
|
2019-01-24 02:54:21 +01:00
|
|
|
d.updateData()
|
2019-06-16 19:41:44 +02:00
|
|
|
d.dataMX.Unlock()
|
2019-01-24 02:54:21 +01:00
|
|
|
}
|
|
|
|
}
|