FedP2P/pex.go

193 lines
4.2 KiB
Go
Raw Normal View History

2020-04-01 04:14:43 +08:00
package torrent
import (
"net"
"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
addr net.Addr
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-09 23:05:47 +08:00
added map[addrKey]pexEvent
dropped map[addrKey]pexEvent
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.
func (me *pexMsgFactory) addrKey(addr net.Addr) addrKey {
return addrKey(addr.String())
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-10-23 05:58:55 +08:00
if _, ok := me.dropped[key]; ok {
delete(me.dropped, key)
return
2020-10-23 05:58:55 +08:00
}
if me.added == nil {
2020-11-09 23:05:47 +08:00
me.added = make(map[addrKey]pexEvent, pexMaxDelta)
2020-10-23 05:58:55 +08:00
}
2020-11-09 23:05:47 +08:00
me.added[key] = e
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) {
key := me.addrKey(e.addr)
2020-10-23 05:58:55 +08:00
if _, ok := me.added[key]; ok {
delete(me.added, key)
return
2020-10-23 05:58:55 +08:00
}
if me.dropped == nil {
2020-11-09 23:05:47 +08:00
me.dropped = make(map[addrKey]pexEvent, pexMaxDelta)
2020-10-23 05:58:55 +08:00
}
2020-11-09 23:05:47 +08:00
me.dropped[key] = e
2020-10-23 05:58:55 +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-10-23 05:58:55 +08:00
func (me *pexMsgFactory) PexMsg() (ret pp.PexMsg) {
for key, added := range me.added {
2020-11-09 23:05:47 +08:00
addr, ok := nodeAddr(added.addr)
if !ok {
continue
}
switch len(addr.IP) {
case net.IPv4len:
ret.Added = append(ret.Added, addr)
ret.AddedFlags = append(ret.AddedFlags, added.f)
case net.IPv6len:
ret.Added6 = append(ret.Added6, addr)
ret.Added6Flags = append(ret.Added6Flags, added.f)
2020-10-23 05:58:55 +08:00
default:
panic(key)
}
}
2020-11-09 23:05:47 +08:00
for key, dropped := range me.dropped {
addr, ok := nodeAddr(dropped.addr)
if !ok {
continue
}
switch len(addr.IP) {
case net.IPv4len:
2020-10-23 05:58:55 +08:00
ret.Dropped = append(ret.Dropped, addr)
2020-11-09 23:05:47 +08:00
case net.IPv6len:
2020-10-23 05:58:55 +08:00
ret.Dropped6 = append(ret.Dropped6, addr)
default:
panic(key)
}
}
return
}
// Convert an arbitrary torrent peer Addr into one that can be represented by the compact addr
// format.
func nodeAddr(addr net.Addr) (_ krpc.NodeAddr, ok bool) {
ipport, ok := tryIpPortFromNetAddr(addr)
if !ok {
return
}
return krpc.NodeAddr{IP: shortestIP(ipport.IP), Port: ipport.Port}, true
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 {
ev []pexEvent // event feed, append-only
hold []pexEvent // delayed drops
nc int // net number of alive conns
}
func (s *pexState) Reset() {
s.ev = nil
s.hold = nil
s.nc = 0
}
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) {
var factory pexMsgFactory
2020-04-01 04:14:43 +08:00
n := start
for _, e := range s.ev[start:] {
if start > 0 && factory.DeltaLen() >= pexMaxDelta {
2020-04-01 04:14:43 +08:00
break
}
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
}