5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 08:45:39 +00:00

Discriminate multicast peers more loosely

This commit is contained in:
Neil Alexander 2023-05-20 21:18:49 +01:00
parent e0b39b303f
commit c0188f5600
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"math" "math"
"net" "net"
"net/netip"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
@ -146,8 +147,9 @@ const ErrLinkUnrecognisedSchema = linkError("link schema unknown")
func (l *links) add(u *url.URL, sintf string, linkType linkType) error { func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
// Generate the link info and see whether we think we already // Generate the link info and see whether we think we already
// have an open peering to this peer. // have an open peering to this peer.
lu := urlForLinkInfo(*u)
info := linkInfo{ info := linkInfo{
uri: u.String(), uri: lu.String(),
sintf: sintf, sintf: sintf,
linkType: linkType, linkType: linkType,
} }
@ -322,10 +324,11 @@ func (l *links) listen(u *url.URL, sintf string) (*Listener, error) {
} }
pu := *u pu := *u
pu.Host = conn.RemoteAddr().String() pu.Host = conn.RemoteAddr().String()
lu := urlForLinkInfo(pu)
info := linkInfo{ info := linkInfo{
uri: pu.String(), uri: lu.String(),
sintf: sintf, sintf: sintf,
linkType: linkTypeIncoming, linkType: linkTypeEphemeral, // TODO: should be incoming
} }
if l.isConnectedTo(info) { if l.isConnectedTo(info) {
_ = conn.Close() _ = conn.Close()
@ -480,6 +483,21 @@ func (l *links) handler(info *linkInfo, options linkOptions, conn net.Conn) erro
return nil return nil
} }
func urlForLinkInfo(u url.URL) url.URL {
u.RawQuery = ""
if host, _, err := net.SplitHostPort(u.Host); err == nil {
if addr, err := netip.ParseAddr(host); err == nil {
// For peers that look like multicast peers (i.e.
// link-local addresses), we will ignore the port number,
// otherwise we might open multiple connections to them.
if addr.IsLinkLocalUnicast() {
u.Host = fmt.Sprintf("[%s]", addr.String())
}
}
}
return u
}
type linkConn struct { type linkConn struct {
// tx and rx are at the beginning of the struct to ensure 64-bit alignment // tx and rx are at the beginning of the struct to ensure 64-bit alignment
// on 32-bit platforms, see https://pkg.go.dev/sync/atomic#pkg-note-BUG // on 32-bit platforms, see https://pkg.go.dev/sync/atomic#pkg-note-BUG