You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.1 KiB

p2p: Implement PeerTransport This is the implementation for the design described in ADR 12[0]. It's the first step of a larger refactor of the p2p package as tracked in interface bundling all concerns of low-level connection handling and isolating the rest of peer lifecycle management from the specifics of the low-level internet protocols. Even if the swappable implementation will never be utilised, already the isolation of conn related code in one place will help with the reasoning about execution path and addressation of security sensitive issues surfaced through bounty programs and audits. We deliberately decided to not have Peer filtering and other management in the Transport, its sole responsibility is the translation of connections to Peers, handing those to the caller fully setup. It's the responsibility of the caller to reject those and or keep track. Peer filtering will take place in the Switch and can be inspected in a the following commit. This changeset additionally is an exercise in clean separation of logic and other infrastructural concerns like logging and instrumentation. By leveraging a clean and minimal interface. How this looks can be seen in a follow-up change. Design #2069[2] Refs #2067[3] Fixes #2047[4] Fixes #2046[5] changes: * describe Transport interface * implement new default Transport: MultiplexTransport * test MultiplexTransport with new constraints * implement ConnSet for concurrent management of net.Conn, synchronous to PeerSet * implement and expose duplicate IP filter * implemnt TransportOption for optional parametirisation [0] https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-012-peer-transport.md [1] https://github.com/tendermint/tendermint/issues/2067 [2] https://github.com/tendermint/tendermint/pull/2069 [3] https://github.com/tendermint/tendermint/issues/2067 [4] https://github.com/tendermint/tendermint/issues/2047 [5] https://github.com/tendermint/tendermint/issues/2046
6 years ago
  1. package p2p
  2. import (
  3. "net"
  4. "sync"
  5. )
  6. // ConnSet is a lookup table for connections and all their ips.
  7. type ConnSet interface {
  8. Has(net.Conn) bool
  9. HasIP(net.IP) bool
  10. Set(net.Conn, []net.IP)
  11. Remove(net.Conn)
  12. }
  13. type connSetItem struct {
  14. conn net.Conn
  15. ips []net.IP
  16. }
  17. type connSet struct {
  18. sync.RWMutex
  19. conns map[string]connSetItem
  20. }
  21. // NewConnSet returns a ConnSet implementation.
  22. func NewConnSet() *connSet {
  23. return &connSet{
  24. conns: map[string]connSetItem{},
  25. }
  26. }
  27. func (cs *connSet) Has(c net.Conn) bool {
  28. cs.RLock()
  29. defer cs.RUnlock()
  30. _, ok := cs.conns[c.RemoteAddr().String()]
  31. return ok
  32. }
  33. func (cs *connSet) HasIP(ip net.IP) bool {
  34. cs.RLock()
  35. defer cs.RUnlock()
  36. for _, c := range cs.conns {
  37. for _, known := range c.ips {
  38. if known.Equal(ip) {
  39. return true
  40. }
  41. }
  42. }
  43. return false
  44. }
  45. func (cs *connSet) Remove(c net.Conn) {
  46. cs.Lock()
  47. defer cs.Unlock()
  48. delete(cs.conns, c.RemoteAddr().String())
  49. }
  50. func (cs *connSet) Set(c net.Conn, ips []net.IP) {
  51. cs.Lock()
  52. defer cs.Unlock()
  53. cs.conns[c.RemoteAddr().String()] = connSetItem{
  54. conn: c,
  55. ips: ips,
  56. }
  57. }