Skip to content

p2p: prevent connections from same ip#1520

Merged
ebuchman merged 14 commits intodevelopfrom
bucky/p2p-same-ip
May 24, 2018
Merged

p2p: prevent connections from same ip#1520
ebuchman merged 14 commits intodevelopfrom
bucky/p2p-same-ip

Conversation

@ebuchman
Copy link
Contributor

@ebuchman ebuchman commented Apr 28, 2018

#1264

TODO: fix and write tests

  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG.md

p2p/peer_set.go Outdated
func (ps *PeerSet) Remove(peer Peer) {
ps.mtx.Lock()
defer ps.mtx.Unlock()
delete(ps.lookupIP[peer.RemoteIP()])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this compile?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol nope

Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇 🏎 🍰

p2p/peer_set.go Outdated

// HasIP returns true iff the PeerSet contains
// the peer referred to by this IP address.
func (ps *PeerSet) HasIP(peerIP string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should use net.IP here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@xla xla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we have to overhaul the majority of our switch tets, as we use netpipe excessively which reports pipe as the remote addr?

p2p/peer_set.go Outdated

// HasIP returns true iff the PeerSet contains
// the peer referred to by this IP address.
func (ps *PeerSet) HasIP(peerIP string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

func (pc peerConn) RemoteIP() string {
host, _, err := net.SplitHostPort(pc.conn.RemoteAddr().String())
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case worth a panic?

p2p/peer.go Outdated
}

// Return the IP from the connection RemoteAddr
func (pc peerConn) RemoteIP() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we truly want to return an ip address I think we should do a lookup in case RemoteAddr() is a dns name.

@ebuchman ebuchman changed the base branch from bucky/p2p-cleanup to develop April 30, 2018 22:30
@xla xla force-pushed the bucky/p2p-same-ip branch from 4382dbe to b698a9f Compare May 16, 2018 17:25
@ebuchman
Copy link
Contributor Author

:o same test failure as anton is having in #1574 (comment)

@ebuchman
Copy link
Contributor Author

Oh well, here at least presumably it's becuase they all have the same IP, so they disconnect when they realize thaT?

@ebuchman
Copy link
Contributor Author

maybe what we need is some kind of "filter" object that we can set that determines what peers we disconnect to ... that way in tests we don't need to apply the same level of filters so we can not worry about connecting to self when we're trying to test something else ... ?

p2p/switch.go Outdated
return err
}

// dont connect to multiple peers on the same IP
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of place ?

return pc.ips[0]
}

if pc.conn == nil || pc.conn.RemoteAddr().String() == "pipe" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this only nil during test? we should add comment either way that this little section is just for tests.

return pc.ips[0]
}

host, _, err := net.SplitHostPort(pc.conn.RemoteAddr().String())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does RemoteAddr return host names or only IPs ?

p2p/peer.go Outdated
persistent bool
config *PeerConfig
conn net.Conn // source connection
ips []net.IP
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you thinking we'll use this for something ? looks like we only ever use the [0]

p2p/switch.go Outdated
}

// check ips for both the connection addr and the self reported addr
if sw.peers.HasIP(pc.RemoteIP()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the connection address. Maybe we should check the self-reported address from the NodeInfo as well ?

assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount")
}

func TestPeerSetAddDuplicateIP(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

@ebuchman
Copy link
Contributor Author

Looks good. Some minor comments, but mostly just need to address the test issue before merge.

Note the test failure here actually has a switch with no peers and makes sense why its not working ( I think ). The fail in #1574 might be different

@xla
Copy link
Contributor

xla commented May 22, 2018

@ebuchman Thanks for the thorough review.

@xla xla force-pushed the bucky/p2p-same-ip branch 2 times, most recently from 2dee41b to 01fd102 Compare May 22, 2018 23:54
@codecov-io
Copy link

Codecov Report

Merging #1520 into develop will increase coverage by 3.3%.
The diff coverage is 60.97%.

@@            Coverage Diff             @@
##           develop    #1520     +/-   ##
==========================================
+ Coverage    60.62%   63.93%   +3.3%     
==========================================
  Files          115      103     -12     
  Lines         9971     9571    -400     
==========================================
+ Hits          6045     6119     +74     
+ Misses        3356     2891    -465     
+ Partials       570      561      -9
Impacted Files Coverage Δ
p2p/errors.go 0% <0%> (ø) ⬆️
p2p/peer_set.go 100% <100%> (ø) ⬆️
p2p/test_util.go 56.47% <100%> (ø) ⬆️
p2p/switch.go 53.03% <28.57%> (+0.53%) ⬆️
p2p/peer.go 66.45% <71.42%> (+0.49%) ⬆️
state/execution.go 70.39% <0%> (-0.47%) ⬇️
consensus/wal_generator.go 79.2% <0%> (-0.41%) ⬇️
rpc/client/httpclient.go 69.3% <0%> (-0.31%) ⬇️
node/node.go 64.37% <0%> (-0.12%) ⬇️
config/config.go 81.28% <0%> (-0.11%) ⬇️
... and 26 more

@xla
Copy link
Contributor

xla commented May 23, 2018

@ebuchman @melekes green at last and ready for a final review.

@ebuchman
Copy link
Contributor Author

LGTM!

@ebuchman ebuchman merged commit caf5afc into develop May 24, 2018
@ebuchman ebuchman deleted the bucky/p2p-same-ip branch May 24, 2018 00:57
cboh4 pushed a commit to scrtlabs/tendermint that referenced this pull request Apr 7, 2025
…ndermint#1520)

Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.27.0 to 1.27.1.
- [Release notes](https://github.com/bufbuild/buf-setup-action/releases)
- [Commits](bufbuild/buf-setup-action@v1.27.0...v1.27.1)

---
updated-dependencies:
- dependency-name: bufbuild/buf-setup-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants