From 223a835f33dd6eddf81faf828fe867a0eb73bccf Mon Sep 17 00:00:00 2001 From: Cedric Staniewski Date: Sun, 3 Jul 2016 20:43:26 +0000 Subject: [PATCH] lib/discover: Respect the listen address scheme (fixes #3346) This is a supplement patch to commit a58f69b which only fixed global discovery. This patch adds the missing parts for the local discovery. If the listen address scheme is set to tcp4:// or tcp6:// and no explicit host is specified, an address should not be considered if the source address does not match this scheme. This prevents invalid URIs like tcp4://: or tcp6://: for local discovery. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3380 --- lib/discover/local.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/discover/local.go b/lib/discover/local.go index 2c4ceedc..4334cd99 100644 --- a/lib/discover/local.go +++ b/lib/discover/local.go @@ -197,6 +197,21 @@ func (c *localClient) registerDevice(src net.Addr, device Device) bool { } if len(tcpAddr.IP) == 0 || tcpAddr.IP.IsUnspecified() { + srcAddr, err := net.ResolveTCPAddr("tcp", src.String()) + if err != nil { + continue + } + + // Do not use IPv6 source address if requested scheme is tcp4 + if u.Scheme == "tcp4" && srcAddr.IP.To4() == nil { + continue + } + + // Do not use IPv4 source address if requested scheme is tcp6 + if u.Scheme == "tcp6" && srcAddr.IP.To4() != nil { + continue + } + host, _, err := net.SplitHostPort(src.String()) if err != nil { continue