Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SelectFilter option to support custom filters #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
18 changes: 16 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
Expand All @@ -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)
Expand All @@ -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++
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down