connection.go | 26 ++++++++++++++++++++++++++ diff --git a/connection.go b/connection.go index 8a10b265522d6ae2a007c7af5b469712c13a3bb6..d6e2dcc3b333f52960e3ea2fd7df75ecc39a8764 100644 --- a/connection.go +++ b/connection.go @@ -106,6 +106,32 @@ uploadTimer *time.Timer writerCond sync.Cond } +// Returns true if the connection is over IPv6. +func (cn *connection) ipv6() bool { + ip := missinggo.AddrIP(cn.remoteAddr()) + if ip.To4() != nil { + return false + } + return len(ip) == net.IPv6len +} + +// Returns true the dialer has the lower client peer ID. TODO: Find the +// specification for this. +func (cn *connection) isPreferredDirection() bool { + return bytes.Compare(cn.t.cl.peerID[:], cn.PeerID[:]) < 0 == cn.outgoing +} + +// Returns whether the left connection should be preferred over the right one, +// considering only their networking properties. If ok is false, we can't +// decide. +func (l *connection) hasPreferredNetworkOver(r *connection) (left, ok bool) { + var ml multiLess + ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection()) + ml.NextBool(!l.utp(), !r.utp()) + ml.NextBool(l.ipv6(), r.ipv6()) + return ml.FinalOk() +} + func (cn *connection) cumInterest() time.Duration { ret := cn.priorInterest if cn.Interested {