2020-04-01 04:14:43 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2020-11-10 20:32:40 +08:00
|
|
|
"sync"
|
2020-04-01 04:14:43 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pexEventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
pexAdd pexEventType = iota
|
|
|
|
pexDrop
|
|
|
|
)
|
|
|
|
|
|
|
|
// internal, based on BEP11
|
|
|
|
const (
|
|
|
|
pexTargAdded = 25 // put drops on hold when the number of alive connections is lower than this
|
|
|
|
pexMaxHold = 25 // length of the drop hold-back buffer
|
|
|
|
pexMaxDelta = 50 // upper bound on added+added6 and dropped+dropped6 in a single PEX message
|
|
|
|
)
|
|
|
|
|
|
|
|
// represents a single connection (t=pexAdd) or disconnection (t=pexDrop) event
|
|
|
|
type pexEvent struct {
|
|
|
|
t pexEventType
|
2021-01-25 11:22:24 +08:00
|
|
|
addr PeerRemoteAddr
|
2020-04-01 04:14:43 +08:00
|
|
|
f pp.PexPeerFlags
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:05:47 +08:00
|
|
|
// facilitates efficient de-duplication while generating PEX messages
|
2020-10-23 05:58:55 +08:00
|
|
|
type pexMsgFactory struct {
|
2020-11-13 05:24:33 +08:00
|
|
|
msg pp.PexMsg
|
|
|
|
added map[addrKey]struct{}
|
|
|
|
dropped map[addrKey]struct{}
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *pexMsgFactory) DeltaLen() int {
|
|
|
|
return int(max(
|
|
|
|
int64(len(me.added)),
|
|
|
|
int64(len(me.dropped))))
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:05:47 +08:00
|
|
|
type addrKey string
|
|
|
|
|
|
|
|
// Returns the key to use to identify a given addr in the factory.
|
2021-01-25 11:22:24 +08:00
|
|
|
func (me *pexMsgFactory) addrKey(addr PeerRemoteAddr) addrKey {
|
2020-11-09 23:05:47 +08:00
|
|
|
return addrKey(addr.String())
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 05:24:33 +08:00
|
|
|
func addrEqual(a, b *krpc.NodeAddr) bool {
|
|
|
|
return a.IP.Equal(b.IP) && a.Port == b.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
func addrIndex(v []krpc.NodeAddr, a *krpc.NodeAddr) int {
|
|
|
|
for i := range v {
|
|
|
|
if addrEqual(&v[i], a) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:58:55 +08:00
|
|
|
// Returns whether the entry was added (we can check if we're cancelling out another entry and so
|
|
|
|
// won't hit the limit consuming this event).
|
2020-11-09 23:05:47 +08:00
|
|
|
func (me *pexMsgFactory) add(e pexEvent) {
|
|
|
|
key := me.addrKey(e.addr)
|
2020-11-13 05:24:33 +08:00
|
|
|
if _, ok := me.added[key]; ok {
|
2020-11-06 13:23:23 +08:00
|
|
|
return
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
if me.added == nil {
|
2020-11-13 05:24:33 +08:00
|
|
|
me.added = make(map[addrKey]struct{}, pexMaxDelta)
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
2020-11-13 05:24:33 +08:00
|
|
|
addr, ok := nodeAddr(e.addr)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := &me.msg
|
|
|
|
switch {
|
|
|
|
case addr.IP.To4() != nil:
|
|
|
|
if _, ok := me.dropped[key]; ok {
|
|
|
|
if i := addrIndex(m.Dropped.NodeAddrs(), &addr); i >= 0 {
|
|
|
|
m.Dropped = append(m.Dropped[:i], m.Dropped[i+1:]...)
|
|
|
|
}
|
|
|
|
delete(me.dropped, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Added = append(m.Added, addr)
|
|
|
|
m.AddedFlags = append(m.AddedFlags, e.f)
|
|
|
|
case len(addr.IP) == net.IPv6len:
|
|
|
|
if _, ok := me.dropped[key]; ok {
|
|
|
|
if i := addrIndex(m.Dropped6.NodeAddrs(), &addr); i >= 0 {
|
|
|
|
m.Dropped6 = append(m.Dropped6[:i], m.Dropped6[i+1:]...)
|
|
|
|
}
|
|
|
|
delete(me.dropped, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Added6 = append(m.Added6, addr)
|
|
|
|
m.Added6Flags = append(m.Added6Flags, e.f)
|
|
|
|
default:
|
|
|
|
panic(addr)
|
|
|
|
}
|
|
|
|
me.added[key] = struct{}{}
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the entry was added (we can check if we're cancelling out another entry and so
|
|
|
|
// won't hit the limit consuming this event).
|
2020-11-09 23:05:47 +08:00
|
|
|
func (me *pexMsgFactory) drop(e pexEvent) {
|
2020-11-13 05:24:33 +08:00
|
|
|
addr, ok := nodeAddr(e.addr)
|
|
|
|
if !ok {
|
2020-11-06 13:23:23 +08:00
|
|
|
return
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
2020-11-13 05:24:33 +08:00
|
|
|
key := me.addrKey(e.addr)
|
2020-10-23 05:58:55 +08:00
|
|
|
if me.dropped == nil {
|
2020-11-13 05:24:33 +08:00
|
|
|
me.dropped = make(map[addrKey]struct{}, pexMaxDelta)
|
|
|
|
}
|
|
|
|
if _, ok := me.dropped[key]; ok {
|
|
|
|
return
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
2020-11-13 05:24:33 +08:00
|
|
|
m := &me.msg
|
|
|
|
switch {
|
|
|
|
case addr.IP.To4() != nil:
|
|
|
|
if _, ok := me.added[key]; ok {
|
|
|
|
if i := addrIndex(m.Added.NodeAddrs(), &addr); i >= 0 {
|
|
|
|
m.Added = append(m.Added[:i], m.Added[i+1:]...)
|
|
|
|
m.AddedFlags = append(m.AddedFlags[:i], m.AddedFlags[i+1:]...)
|
|
|
|
}
|
|
|
|
delete(me.added, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Dropped = append(m.Dropped, addr)
|
|
|
|
case len(addr.IP) == net.IPv6len:
|
|
|
|
if _, ok := me.added[key]; ok {
|
|
|
|
if i := addrIndex(m.Added6.NodeAddrs(), &addr); i >= 0 {
|
|
|
|
m.Added6 = append(m.Added6[:i], m.Added6[i+1:]...)
|
|
|
|
m.Added6Flags = append(m.Added6Flags[:i], m.Added6Flags[i+1:]...)
|
|
|
|
}
|
|
|
|
delete(me.added, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.Dropped6 = append(m.Dropped6, addr)
|
|
|
|
}
|
|
|
|
me.dropped[key] = struct{}{}
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:23:23 +08:00
|
|
|
func (me *pexMsgFactory) addEvent(event pexEvent) {
|
2020-10-23 05:58:55 +08:00
|
|
|
switch event.t {
|
2020-04-01 04:14:43 +08:00
|
|
|
case pexAdd:
|
2020-11-09 23:05:47 +08:00
|
|
|
me.add(event)
|
2020-04-01 04:14:43 +08:00
|
|
|
case pexDrop:
|
2020-11-09 23:05:47 +08:00
|
|
|
me.drop(event)
|
2020-10-23 05:58:55 +08:00
|
|
|
default:
|
|
|
|
panic(event.t)
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 05:24:33 +08:00
|
|
|
func (me *pexMsgFactory) PexMsg() pp.PexMsg {
|
|
|
|
return me.msg
|
2020-10-23 05:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an arbitrary torrent peer Addr into one that can be represented by the compact addr
|
|
|
|
// format.
|
2021-01-25 11:22:24 +08:00
|
|
|
func nodeAddr(addr PeerRemoteAddr) (krpc.NodeAddr, bool) {
|
|
|
|
ipport, _ := tryIpPortFromNetAddr(addr)
|
|
|
|
ok := ipport.IP != nil
|
|
|
|
return krpc.NodeAddr{IP: shortestIP(ipport.IP), Port: ipport.Port}, ok
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// mainly for the krpc marshallers
|
|
|
|
func shortestIP(ip net.IP) net.IP {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
// Per-torrent PEX state
|
|
|
|
type pexState struct {
|
2020-11-10 20:32:40 +08:00
|
|
|
ev []pexEvent // event feed, append-only
|
|
|
|
hold []pexEvent // delayed drops
|
|
|
|
nc int // net number of alive conns
|
|
|
|
initCache pexMsgFactory // last generated initial message
|
|
|
|
initSeq int // number of events which went into initCache
|
|
|
|
initLock sync.RWMutex // serialise access to initCache and initSeq
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-10 20:32:40 +08:00
|
|
|
// Reset wipes the state clean, releasing resources. Called from Torrent.Close().
|
2020-04-01 04:14:43 +08:00
|
|
|
func (s *pexState) Reset() {
|
|
|
|
s.ev = nil
|
|
|
|
s.hold = nil
|
|
|
|
s.nc = 0
|
2020-11-10 20:32:40 +08:00
|
|
|
s.initLock.Lock()
|
|
|
|
s.initCache = pexMsgFactory{}
|
|
|
|
s.initSeq = 0
|
|
|
|
s.initLock.Unlock()
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *pexState) Add(c *PeerConn) {
|
|
|
|
s.nc++
|
|
|
|
if s.nc >= pexTargAdded {
|
|
|
|
s.ev = append(s.ev, s.hold...)
|
|
|
|
s.hold = s.hold[:0]
|
|
|
|
}
|
|
|
|
e := c.pexEvent(pexAdd)
|
|
|
|
s.ev = append(s.ev, e)
|
2020-04-09 00:03:29 +08:00
|
|
|
c.pex.Listed = true
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *pexState) Drop(c *PeerConn) {
|
2020-04-09 00:03:29 +08:00
|
|
|
if !c.pex.Listed {
|
|
|
|
// skip connections which were not previously Added
|
|
|
|
return
|
|
|
|
}
|
2020-04-01 04:14:43 +08:00
|
|
|
e := c.pexEvent(pexDrop)
|
|
|
|
s.nc--
|
|
|
|
if s.nc < pexTargAdded && len(s.hold) < pexMaxHold {
|
|
|
|
s.hold = append(s.hold, e)
|
|
|
|
} else {
|
|
|
|
s.ev = append(s.ev, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:58:55 +08:00
|
|
|
// Generate a PEX message based on the event feed. Also returns an index to pass to the subsequent
|
|
|
|
// calls, producing incremental deltas.
|
|
|
|
func (s *pexState) Genmsg(start int) (pp.PexMsg, int) {
|
2020-11-10 20:32:40 +08:00
|
|
|
if start == 0 {
|
|
|
|
return s.genmsg0()
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:58:55 +08:00
|
|
|
var factory pexMsgFactory
|
2020-04-01 04:14:43 +08:00
|
|
|
n := start
|
|
|
|
for _, e := range s.ev[start:] {
|
2020-11-06 13:23:23 +08:00
|
|
|
if start > 0 && factory.DeltaLen() >= pexMaxDelta {
|
2020-04-01 04:14:43 +08:00
|
|
|
break
|
|
|
|
}
|
2020-11-06 13:23:23 +08:00
|
|
|
factory.addEvent(e)
|
2020-04-01 04:14:43 +08:00
|
|
|
n++
|
|
|
|
}
|
2020-10-23 05:58:55 +08:00
|
|
|
return factory.PexMsg(), n
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
2020-11-10 20:32:40 +08:00
|
|
|
|
|
|
|
func (s *pexState) genmsg0() (pp.PexMsg, int) {
|
|
|
|
s.initLock.Lock()
|
|
|
|
for _, e := range s.ev[s.initSeq:] {
|
|
|
|
s.initCache.addEvent(e)
|
|
|
|
s.initSeq++
|
|
|
|
}
|
|
|
|
s.initLock.Unlock()
|
|
|
|
s.initLock.RLock()
|
|
|
|
n := s.initSeq
|
|
|
|
msg := s.initCache.PexMsg()
|
|
|
|
s.initLock.RUnlock()
|
|
|
|
return msg, n
|
|
|
|
}
|