misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
Simone Margaritelli 2024-09-22 13:23:30 +02:00
commit 5652d15426
6 changed files with 177 additions and 14 deletions

View file

@ -105,9 +105,46 @@ func listMulticastInterfaces() []net.Interface {
return nil
}
for _, ifi := range ifaces {
// not up
if (ifi.Flags & net.FlagUp) == 0 {
continue
}
// localhost
if (ifi.Flags & net.FlagLoopback) != 0 {
continue
}
// not running
if (ifi.Flags & net.FlagRunning) == 0 {
continue
}
// vpn and similar
if (ifi.Flags & net.FlagPointToPoint) != 0 {
continue
}
// at least one ipv4 address assigned
hasIPv4 := false
if addresses, _ := ifi.Addrs(); addresses != nil {
for _, addr := range addresses {
// ipv4 or ipv4 CIDR
if ip, ipnet, err := net.ParseCIDR(addr.String()); err == nil {
if ip.To4() != nil || ipnet.IP.To4() != nil {
hasIPv4 = true
break
}
} else if ipAddr, ok := addr.(*net.IPAddr); ok {
if ipAddr.IP.To4() != nil {
hasIPv4 = true
break
}
}
}
}
if !hasIPv4 {
continue
}
if (ifi.Flags & net.FlagMulticast) > 0 {
interfaces = append(interfaces, ifi)
}

View file

@ -48,9 +48,10 @@ func Register(instance, service, domain string, port int, text []string, ifaces
if err != nil {
return nil, fmt.Errorf("could not determine host")
}
entry.HostName = strings.ReplaceAll(entry.HostName, ".local", "")
}
if !strings.HasSuffix(trimDot(entry.HostName), entry.Domain) {
if !strings.HasSuffix(trimDot(entry.HostName), trimDot(entry.Domain)) {
entry.HostName = fmt.Sprintf("%s.%s.", trimDot(entry.HostName), trimDot(entry.Domain))
}
@ -431,7 +432,7 @@ func (s *Server) composeBrowsingAnswers(resp *dns.Msg, ifIndex int) {
Ttl: s.ttl,
},
Priority: 0,
Weight: 0,
Weight: 0xffff,
Port: uint16(s.service.Port),
Target: s.service.HostName,
}
@ -463,7 +464,7 @@ func (s *Server) composeLookupAnswers(resp *dns.Msg, ttl uint32, ifIndex int, fl
Ttl: ttl,
},
Priority: 0,
Weight: 0,
Weight: 0xffff,
Port: uint16(s.service.Port),
Target: s.service.HostName,
}
@ -539,7 +540,7 @@ func (s *Server) probe() {
Ttl: s.ttl,
},
Priority: 0,
Weight: 0,
Weight: 0xffff,
Port: uint16(s.service.Port),
Target: s.service.HostName,
}