yanic/respond/daemon/nodeinfo.go

70 lines
1.5 KiB
Go
Raw Normal View History

2019-04-10 13:18:05 +02:00
package respondd
import (
"fmt"
2019-04-10 20:09:55 +02:00
"net"
2019-04-10 13:18:05 +02:00
"os"
"runtime"
"github.com/FreifunkBremen/yanic/data"
)
func (d *Daemon) updateNodeinfo(iface string, data *data.ResponseData) {
config, nodeID := d.getAnswer(iface)
data.Nodeinfo.NodeID = nodeID
if config.Hostname == "" {
data.Nodeinfo.Hostname, _ = os.Hostname()
} else {
data.Nodeinfo.Hostname = config.Hostname
}
data.Nodeinfo.VPN = config.VPN
data.Nodeinfo.Location = config.Location
data.Nodeinfo.System.SiteCode = config.SiteCode
data.Nodeinfo.System.DomainCode = config.DomainCode
data.Nodeinfo.Hardware.Nproc = runtime.NumCPU()
if data.Nodeinfo.Network.Mac == "" {
data.Nodeinfo.Network.Mac = fmt.Sprintf("%s:%s:%s:%s:%s:%s", nodeID[0:2], nodeID[2:4], nodeID[4:6], nodeID[6:8], nodeID[8:10], nodeID[10:12])
}
if iface == "" {
data.Nodeinfo.Network.Addresses = []string{}
for _, i := range d.Interfaces {
addrs := getAddresses(i)
data.Nodeinfo.Network.Addresses = append(data.Nodeinfo.Network.Addresses, addrs...)
}
} else {
data.Nodeinfo.Network.Addresses = getAddresses(iface)
}
}
func getAddresses(iface string) (addrs []string) {
in, err := net.InterfaceByName(iface)
if err != nil {
return
}
2019-04-10 20:09:55 +02:00
inAddrs, err := in.Addrs()
2019-04-10 13:18:05 +02:00
if err != nil {
return
}
for _, a := range inAddrs {
var ip net.IP
switch v := a.(type) {
2019-04-10 20:09:55 +02:00
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
2019-04-10 13:18:05 +02:00
}
if ip4 := ip.To4(); ip4 == nil {
addrs = append(addrs, ip.String())
}
}
return
}