Skip to content

Commit

Permalink
Merge pull request #110 from cpuchip/fix-multicast-xmit-windows
Browse files Browse the repository at this point in the history
Fix multicast xmit windows
  • Loading branch information
grandcat authored Jan 19, 2023
2 parents e1d6e57 + 90750ac commit e4f60f8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
26 changes: 24 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package zeroconf
import (
"context"
"fmt"
"log"
"net"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -443,16 +445,36 @@ func (c *client) sendQuery(msg *dns.Msg) error {
return err
}
if c.ipv4conn != nil {
// See https://pkg.go.dev/golang.org/x/net/ipv4#pkg-note-BUG
// As of Golang 1.18.4
// On Windows, the ControlMessage for ReadFrom and WriteTo methods of PacketConn is not implemented.
var wcm ipv4.ControlMessage
for ifi := range c.ifaces {
wcm.IfIndex = c.ifaces[ifi].Index
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = c.ifaces[ifi].Index
default:
if err := c.ipv4conn.SetMulticastInterface(&c.ifaces[ifi]); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
c.ipv4conn.WriteTo(buf, &wcm, ipv4Addr)
}
}
if c.ipv6conn != nil {
// See https://pkg.go.dev/golang.org/x/net/ipv6#pkg-note-BUG
// As of Golang 1.18.4
// On Windows, the ControlMessage for ReadFrom and WriteTo methods of PacketConn is not implemented.
var wcm ipv6.ControlMessage
for ifi := range c.ifaces {
wcm.IfIndex = c.ifaces[ifi].Index
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = c.ifaces[ifi].Index
default:
if err := c.ipv6conn.SetMulticastInterface(&c.ifaces[ifi]); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
c.ipv6conn.WriteTo(buf, &wcm, ipv6Addr)
}
}
Expand Down
45 changes: 41 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"net"
"os"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -721,26 +722,62 @@ func (s *Server) multicastResponse(msg *dns.Msg, ifIndex int) error {
return err
}
if s.ipv4conn != nil {
// See https://pkg.go.dev/golang.org/x/net/ipv4#pkg-note-BUG
// As of Golang 1.18.4
// On Windows, the ControlMessage for ReadFrom and WriteTo methods of PacketConn is not implemented.
var wcm ipv4.ControlMessage
if ifIndex != 0 {
wcm.IfIndex = ifIndex
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = ifIndex
default:
iface, _ := net.InterfaceByIndex(ifIndex)
if err := s.ipv4conn.SetMulticastInterface(iface); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
s.ipv4conn.WriteTo(buf, &wcm, ipv4Addr)
} else {
for _, intf := range s.ifaces {
wcm.IfIndex = intf.Index
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = intf.Index
default:
if err := s.ipv4conn.SetMulticastInterface(&intf); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
s.ipv4conn.WriteTo(buf, &wcm, ipv4Addr)
}
}
}

if s.ipv6conn != nil {
// See https://pkg.go.dev/golang.org/x/net/ipv6#pkg-note-BUG
// As of Golang 1.18.4
// On Windows, the ControlMessage for ReadFrom and WriteTo methods of PacketConn is not implemented.
var wcm ipv6.ControlMessage
if ifIndex != 0 {
wcm.IfIndex = ifIndex
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = ifIndex
default:
iface, _ := net.InterfaceByIndex(ifIndex)
if err := s.ipv6conn.SetMulticastInterface(iface); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
s.ipv6conn.WriteTo(buf, &wcm, ipv6Addr)
} else {
for _, intf := range s.ifaces {
wcm.IfIndex = intf.Index
switch runtime.GOOS {
case "darwin", "ios", "linux":
wcm.IfIndex = intf.Index
default:
if err := s.ipv6conn.SetMulticastInterface(&intf); err != nil {
log.Printf("[WARN] mdns: Failed to set multicast interface: %v", err)
}
}
s.ipv6conn.WriteTo(buf, &wcm, ipv6Addr)
}
}
Expand Down

0 comments on commit e4f60f8

Please sign in to comment.