diff --git a/client.go b/client.go index 3c43d45e..48b945a5 100644 --- a/client.go +++ b/client.go @@ -26,14 +26,24 @@ const ( IPv4AndIPv6 = (IPv4 | IPv6) //< Default option. ) +type FilterFunc func(net.Interface) bool + type clientOpts struct { listenOn IPType ifaces []net.Interface + filter FilterFunc } // ClientOption fills the option struct to configure intefaces, etc. type ClientOption func(*clientOpts) +// SelectFilter +func SelectFilter(filter FilterFunc) ClientOption { + return func(o *clientOpts) { + o.filter = filter + } +} + // SelectIPTraffic selects the type of IP packets (IPv4, IPv6, or both) this // instance listens for. // This does not guarantee that only mDNS entries of this sepcific @@ -155,7 +165,7 @@ func newClient(opts clientOpts) (*client, error) { var ipv4conn *ipv4.PacketConn if (opts.listenOn & IPv4) > 0 { var err error - ipv4conn, err = joinUdp4Multicast(ifaces) + ipv4conn, err = joinUdp4Multicast(ifaces, opts.filter) if err != nil { return nil, err } @@ -164,7 +174,7 @@ func newClient(opts clientOpts) (*client, error) { var ipv6conn *ipv6.PacketConn if (opts.listenOn & IPv6) > 0 { var err error - ipv6conn, err = joinUdp6Multicast(ifaces) + ipv6conn, err = joinUdp6Multicast(ifaces, opts.filter) if err != nil { return nil, err } diff --git a/connection.go b/connection.go index daac9c12..5c853aed 100644 --- a/connection.go +++ b/connection.go @@ -35,7 +35,7 @@ var ( } ) -func joinUdp6Multicast(interfaces []net.Interface) (*ipv6.PacketConn, error) { +func joinUdp6Multicast(interfaces []net.Interface, filter FilterFunc) (*ipv6.PacketConn, error) { udpConn, err := net.ListenUDP("udp6", mdnsWildcardAddrIPv6) if err != nil { return nil, err @@ -53,6 +53,13 @@ func joinUdp6Multicast(interfaces []net.Interface) (*ipv6.PacketConn, error) { var failedJoins int for _, iface := range interfaces { + if filter != nil { + if !filter(iface) { + failedJoins++ + continue + } + } + if err := pkConn.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv6}); err != nil { // log.Println("Udp6 JoinGroup failed for iface ", iface) failedJoins++ @@ -66,7 +73,7 @@ func joinUdp6Multicast(interfaces []net.Interface) (*ipv6.PacketConn, error) { return pkConn, nil } -func joinUdp4Multicast(interfaces []net.Interface) (*ipv4.PacketConn, error) { +func joinUdp4Multicast(interfaces []net.Interface, filter FilterFunc) (*ipv4.PacketConn, error) { udpConn, err := net.ListenUDP("udp4", mdnsWildcardAddrIPv4) if err != nil { // log.Printf("[ERR] bonjour: Failed to bind to udp4 mutlicast: %v", err) @@ -85,6 +92,13 @@ func joinUdp4Multicast(interfaces []net.Interface) (*ipv4.PacketConn, error) { var failedJoins int for _, iface := range interfaces { + if filter != nil { + if !filter(iface) { + failedJoins++ + continue + } + } + if err := pkConn.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv4}); err != nil { // log.Println("Udp4 JoinGroup failed for iface ", iface) failedJoins++ diff --git a/server.go b/server.go index 70fd11ac..95f588c0 100644 --- a/server.go +++ b/server.go @@ -156,11 +156,11 @@ type Server struct { // Constructs server structure func newServer(ifaces []net.Interface) (*Server, error) { - ipv4conn, err4 := joinUdp4Multicast(ifaces) + ipv4conn, err4 := joinUdp4Multicast(ifaces, nil) if err4 != nil { log.Printf("[zeroconf] no suitable IPv4 interface: %s", err4.Error()) } - ipv6conn, err6 := joinUdp6Multicast(ifaces) + ipv6conn, err6 := joinUdp6Multicast(ifaces, nil) if err6 != nil { log.Printf("[zeroconf] no suitable IPv6 interface: %s", err6.Error()) }