From: Matt Joiner Date: Sat, 16 Jun 2018 07:14:47 +0000 (+1000) Subject: Add connection.hasPreferredNetworkOver and friends X-Git-Tag: v1.0.0~127^2~12 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=4c471be8029796898db9e274789a5914ba9f6eff;p=btrtrc.git Add connection.hasPreferredNetworkOver and friends --- diff --git a/connection.go b/connection.go index 8a10b265..d6e2dcc3 100644 --- a/connection.go +++ b/connection.go @@ -106,6 +106,32 @@ type connection struct { 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 {