Skip to content

New option in server config: DisableMulticastLoopback. #32

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 31 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"sync"

"github.com/hashicorp/go.net/ipv4"
"github.com/hashicorp/go.net/ipv6"
"github.com/miekg/dns"
)

Expand Down Expand Up @@ -37,6 +39,12 @@ type Config struct {
// interface. If not provided, the system default multicase interface
// is used.
Iface *net.Interface

// Whether to set the IP_MULTICAST_LOOP socket option on the multicast sockets
// opened. Setting this to false allows mDNS clients on the same machine to
// discover the service. See
// http://stackoverflow.com/questions/1719156/is-there-a-way-to-test-multicast-ip-on-same-box
DisableMulticastLoopback bool
}

// mDNS server is used to listen for mDNS queries and respond if we
Expand All @@ -55,8 +63,29 @@ type Server struct {
// NewServer is used to create a new mDNS server from a config
func NewServer(config *Config) (*Server, error) {
// Create the listeners
ipv4List, _ := net.ListenMulticastUDP("udp4", config.Iface, ipv4Addr)
ipv6List, _ := net.ListenMulticastUDP("udp6", config.Iface, ipv6Addr)
ipv4List, err := net.ListenMulticastUDP("udp4", config.Iface, ipv4Addr)
if err != nil {
// TODO(reddaly): Handle this error beyond printing a log message.
log.Printf("[ERR] mdns: Failed to listen to IPv4 mdns multicast address: %v", err)
}
ipv6List, err := net.ListenMulticastUDP("udp6", config.Iface, ipv6Addr)
if err != nil {
// TODO(reddaly): Handle this error beyond printing a log message.
log.Printf("[ERR] mdns: Failed to listen to IPv6 mdns multicast address: %v", err)
}

if ipv4List != nil {
p := ipv4.NewPacketConn(ipv4List)
if err := p.SetMulticastLoopback(!config.DisableMulticastLoopback); err != nil {
return nil, fmt.Errorf("could not set multicast loopback attribute of ipv4 connection: %v", err)
}
}
if ipv6List != nil {
p := ipv6.NewPacketConn(ipv6List)
if err := p.SetMulticastLoopback(!config.DisableMulticastLoopback); err != nil {
return nil, fmt.Errorf("could not set multicast loopback attribute of ipv6 connection: %v", err)
}
}

// Check if we have any listener
if ipv4List == nil && ipv6List == nil {
Expand Down