yanic/respond/daemon/main.go

71 lines
1.3 KiB
Go
Raw Normal View History

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
}
d.updateData()
go d.updateWorker()
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 {
d.updateData()
}
}