From 586e7c571d106f842c8567012dacd28d73fcf966 Mon Sep 17 00:00:00 2001 From: cpuchip Date: Sun, 7 Aug 2022 13:54:55 -0600 Subject: [PATCH] Fix windows libp2p (#29) * Fix client multicast sending on Windows * Put in a platform dependent fix for windows to WriteTo on client.go and server.go to allow Multicast transmit Co-authored-by: Dave Flowerday Co-authored-by: Michael Stufflebeam --- client.go | 26 ++++++++++++++++++++++++-- server.go | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 33e4c0fa..c0b2cae1 100644 --- a/client.go +++ b/client.go @@ -3,8 +3,10 @@ package zeroconf import ( "context" "fmt" + "log" "math/rand" "net" + "runtime" "strings" "time" @@ -436,16 +438,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) } } diff --git a/server.go b/server.go index fb4b4976..2dbf536e 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "math/rand" "net" "os" + "runtime" "strings" "sync" "time" @@ -764,26 +765,62 @@ func (s *Server) multicastResponse(msg *dns.Msg, ifIndex int) error { return fmt.Errorf("failed to pack msg %v: %w", msg, 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) } }