2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-08 00:49:28 +00:00
|
|
|
"container/list"
|
2015-01-27 13:33:26 +00:00
|
|
|
"crypto/ecdsa"
|
2019-04-30 11:13:22 +00:00
|
|
|
crand "crypto/rand"
|
2015-01-27 13:33:26 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-30 11:13:22 +00:00
|
|
|
"io"
|
2015-01-27 13:33:26 +00:00
|
|
|
"net"
|
2018-10-12 09:47:24 +00:00
|
|
|
"sync"
|
2015-01-27 13:33:26 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 12:10:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-05-15 04:47:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2016-11-21 17:11:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2015-01-27 13:33:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
var (
|
2015-03-25 15:45:53 +00:00
|
|
|
errPacketTooSmall = errors.New("too small")
|
|
|
|
errBadHash = errors.New("bad hash")
|
|
|
|
errExpired = errors.New("expired")
|
|
|
|
errUnsolicitedReply = errors.New("unsolicited reply")
|
|
|
|
errUnknownNode = errors.New("unknown node")
|
|
|
|
errTimeout = errors.New("RPC timeout")
|
2015-08-08 00:49:28 +00:00
|
|
|
errClockWarp = errors.New("reply deadline too far in the future")
|
2015-03-25 15:45:53 +00:00
|
|
|
errClosed = errors.New("socket closed")
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
respTimeout = 500 * time.Millisecond
|
|
|
|
expiration = 20 * time.Second
|
|
|
|
bondExpiration = 24 * time.Hour
|
2016-02-15 18:31:58 +00:00
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
maxFindnodeFailures = 5 // nodes exceeding this limit are dropped
|
2016-02-19 14:18:55 +00:00
|
|
|
ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP
|
|
|
|
ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning
|
|
|
|
driftThreshold = 10 * time.Second // Allowed clock drift before warning user
|
2019-02-19 11:27:29 +00:00
|
|
|
|
|
|
|
// Discovery packets are defined to be no larger than 1280 bytes.
|
|
|
|
// Packets larger than this size will be cut at the end and treated
|
|
|
|
// as invalid because their hash won't match.
|
|
|
|
maxPacketSize = 1280
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RPC packet types
|
|
|
|
const (
|
2019-04-30 11:13:22 +00:00
|
|
|
p_pingV4 = iota + 1 // zero is 'reserved'
|
|
|
|
p_pongV4
|
|
|
|
p_findnodeV4
|
|
|
|
p_neighborsV4
|
2019-05-15 04:47:45 +00:00
|
|
|
p_enrRequestV4
|
|
|
|
p_enrResponseV4
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RPC request structures
|
|
|
|
type (
|
2019-04-30 11:13:22 +00:00
|
|
|
pingV4 struct {
|
2019-01-29 16:39:20 +00:00
|
|
|
senderKey *ecdsa.PublicKey // filled in by preverify
|
|
|
|
|
2015-04-17 23:50:31 +00:00
|
|
|
Version uint
|
|
|
|
From, To rpcEndpoint
|
2015-01-27 13:33:26 +00:00
|
|
|
Expiration uint64
|
2015-12-23 00:52:40 +00:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// pongV4 is the reply to pingV4.
|
|
|
|
pongV4 struct {
|
2015-04-17 23:50:31 +00:00
|
|
|
// This field should mirror the UDP envelope address
|
|
|
|
// of the ping packet, which provides a way to discover the
|
|
|
|
// the external address (after NAT).
|
|
|
|
To rpcEndpoint
|
|
|
|
|
|
|
|
ReplyTok []byte // This contains the hash of the ping packet.
|
|
|
|
Expiration uint64 // Absolute timestamp at which the packet becomes invalid.
|
2015-12-23 00:52:40 +00:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// findnodeV4 is a query for nodes close to the given target.
|
|
|
|
findnodeV4 struct {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
Target encPubkey
|
2015-01-27 13:33:26 +00:00
|
|
|
Expiration uint64
|
2015-12-23 00:52:40 +00:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// neighborsV4 is the reply to findnodeV4.
|
|
|
|
neighborsV4 struct {
|
2015-04-23 10:11:21 +00:00
|
|
|
Nodes []rpcNode
|
2015-01-27 13:33:26 +00:00
|
|
|
Expiration uint64
|
2015-12-23 00:52:40 +00:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-04-17 23:50:31 +00:00
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// enrRequestV4 queries for the remote node's record.
|
|
|
|
enrRequestV4 struct {
|
|
|
|
Expiration uint64
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// enrResponseV4 is the reply to enrRequestV4.
|
|
|
|
enrResponseV4 struct {
|
|
|
|
ReplyTok []byte // Hash of the enrRequest packet.
|
|
|
|
Record enr.Record
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
2015-04-23 10:11:21 +00:00
|
|
|
rpcNode struct {
|
|
|
|
IP net.IP // len 4 for IPv4 or 16 for IPv6
|
|
|
|
UDP uint16 // for discovery protocol
|
|
|
|
TCP uint16 // for RLPx protocol
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
ID encPubkey
|
2015-04-23 10:11:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 23:50:31 +00:00
|
|
|
rpcEndpoint struct {
|
|
|
|
IP net.IP // len 4 for IPv4 or 16 for IPv6
|
|
|
|
UDP uint16 // for discovery protocol
|
|
|
|
TCP uint16 // for RLPx protocol
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// packetV4 is implemented by all v4 protocol messages.
|
2019-04-30 11:13:22 +00:00
|
|
|
type packetV4 interface {
|
|
|
|
// preverify checks whether the packet is valid and should be handled at all.
|
|
|
|
preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error
|
|
|
|
// handle handles the packet.
|
|
|
|
handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte)
|
2019-05-15 04:47:45 +00:00
|
|
|
// packet name and type for logging purposes.
|
2019-04-30 11:13:22 +00:00
|
|
|
name() string
|
2019-05-15 04:47:45 +00:00
|
|
|
kind() byte
|
2019-04-30 11:13:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 23:50:31 +00:00
|
|
|
func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
|
2018-10-12 09:47:24 +00:00
|
|
|
ip := net.IP{}
|
|
|
|
if ip4 := addr.IP.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
} else if ip6 := addr.IP.To16(); ip6 != nil {
|
|
|
|
ip = ip6
|
2015-04-17 23:50:31 +00:00
|
|
|
}
|
|
|
|
return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*node, error) {
|
2016-11-21 17:11:54 +00:00
|
|
|
if rn.UDP <= 1024 {
|
|
|
|
return nil, errors.New("low port")
|
|
|
|
}
|
|
|
|
if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-22 19:51:59 +00:00
|
|
|
if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) {
|
|
|
|
return nil, errors.New("not contained in netrestrict whitelist")
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
key, err := decodePubkey(rn.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n := wrapNode(enode.NewV4(key, rn.IP, int(rn.TCP), int(rn.UDP)))
|
|
|
|
err = n.ValidateComplete()
|
2015-12-07 11:06:49 +00:00
|
|
|
return n, err
|
2015-04-23 10:11:21 +00:00
|
|
|
}
|
|
|
|
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
func nodeToRPC(n *node) rpcNode {
|
|
|
|
var key ecdsa.PublicKey
|
|
|
|
var ekey encPubkey
|
|
|
|
if err := n.Load((*enode.Secp256k1)(&key)); err == nil {
|
|
|
|
ekey = encodePubkey(&key)
|
|
|
|
}
|
|
|
|
return rpcNode{ID: ekey, IP: n.IP(), UDP: uint16(n.UDP()), TCP: uint16(n.TCP())}
|
2015-02-06 13:40:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// UDPv4 implements the v4 wire protocol.
|
|
|
|
type UDPv4 struct {
|
|
|
|
conn UDPConn
|
|
|
|
log log.Logger
|
2016-11-22 19:51:59 +00:00
|
|
|
netrestrict *netutil.Netlist
|
2015-04-17 23:50:31 +00:00
|
|
|
priv *ecdsa.PrivateKey
|
2018-10-12 09:47:24 +00:00
|
|
|
localNode *enode.LocalNode
|
|
|
|
db *enode.DB
|
|
|
|
tab *Table
|
2019-04-30 11:13:22 +00:00
|
|
|
closeOnce sync.Once
|
2018-10-12 09:47:24 +00:00
|
|
|
wg sync.WaitGroup
|
2015-03-25 15:45:53 +00:00
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
addReplyMatcher chan *replyMatcher
|
|
|
|
gotreply chan reply
|
|
|
|
closing chan struct{}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// replyMatcher represents a pending reply.
|
2015-01-27 13:33:26 +00:00
|
|
|
//
|
2019-01-29 16:39:20 +00:00
|
|
|
// Some implementations of the protocol wish to send more than one
|
|
|
|
// reply packet to findnode. In general, any neighbors packet cannot
|
2015-01-27 13:33:26 +00:00
|
|
|
// be matched up with a specific findnode packet.
|
|
|
|
//
|
2019-01-29 16:39:20 +00:00
|
|
|
// Our implementation handles this by storing a callback function for
|
|
|
|
// each pending reply. Incoming packets from a node are dispatched
|
|
|
|
// to all callback functions for that node.
|
|
|
|
type replyMatcher struct {
|
2015-01-27 13:33:26 +00:00
|
|
|
// these fields must match in the reply.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
from enode.ID
|
2019-01-29 16:39:20 +00:00
|
|
|
ip net.IP
|
2015-01-27 13:33:26 +00:00
|
|
|
ptype byte
|
|
|
|
|
|
|
|
// time when the request must complete
|
|
|
|
deadline time.Time
|
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
// callback is called when a matching reply arrives. If it returns matched == true, the
|
|
|
|
// reply was acceptable. The second return value indicates whether the callback should
|
|
|
|
// be removed from the pending reply queue. If it returns false, the reply is considered
|
|
|
|
// incomplete and the callback will be invoked again for the next matching reply.
|
|
|
|
callback replyMatchFunc
|
2015-01-27 13:33:26 +00:00
|
|
|
|
|
|
|
// errc receives nil when the callback indicates completion or an
|
|
|
|
// error if no further reply is received within the timeout.
|
2019-05-15 04:47:45 +00:00
|
|
|
errc chan error
|
|
|
|
|
|
|
|
// reply contains the most recent reply. This field is safe for reading after errc has
|
|
|
|
// received a value.
|
|
|
|
reply packetV4
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
type replyMatchFunc func(interface{}) (matched bool, requestDone bool)
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// reply is a reply packet from a certain node.
|
2015-01-27 13:33:26 +00:00
|
|
|
type reply struct {
|
2019-05-15 04:47:45 +00:00
|
|
|
from enode.ID
|
|
|
|
ip net.IP
|
|
|
|
data packetV4
|
2015-03-25 15:45:53 +00:00
|
|
|
// loop indicates whether there was
|
|
|
|
// a matching request by sending on this channel.
|
|
|
|
matched chan<- bool
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func ListenV4(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
|
|
|
|
t := &UDPv4{
|
2019-01-29 16:39:20 +00:00
|
|
|
conn: c,
|
|
|
|
priv: cfg.PrivateKey,
|
|
|
|
netrestrict: cfg.NetRestrict,
|
|
|
|
localNode: ln,
|
|
|
|
db: ln.Database(),
|
|
|
|
closing: make(chan struct{}),
|
|
|
|
gotreply: make(chan reply),
|
|
|
|
addReplyMatcher: make(chan *replyMatcher),
|
2019-04-30 11:13:22 +00:00
|
|
|
log: cfg.Log,
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2019-04-30 11:13:22 +00:00
|
|
|
if t.log == nil {
|
|
|
|
t.log = log.Root()
|
|
|
|
}
|
|
|
|
tab, err := newTable(t, ln.Database(), cfg.Bootnodes, t.log)
|
2015-11-05 21:57:57 +00:00
|
|
|
if err != nil {
|
2019-04-30 11:13:22 +00:00
|
|
|
return nil, err
|
2015-11-05 21:57:57 +00:00
|
|
|
}
|
2019-04-30 11:13:22 +00:00
|
|
|
t.tab = tab
|
2019-05-13 09:25:54 +00:00
|
|
|
go tab.loop()
|
2015-11-05 21:57:57 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
t.wg.Add(2)
|
|
|
|
go t.loop()
|
|
|
|
go t.readLoop(cfg.Unhandled)
|
|
|
|
return t, nil
|
2018-10-12 09:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// Self returns the local node.
|
|
|
|
func (t *UDPv4) Self() *enode.Node {
|
2018-10-12 09:47:24 +00:00
|
|
|
return t.localNode.Node()
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
// Close shuts down the socket and aborts any running queries.
|
|
|
|
func (t *UDPv4) Close() {
|
|
|
|
t.closeOnce.Do(func() {
|
|
|
|
close(t.closing)
|
|
|
|
t.conn.Close()
|
|
|
|
t.wg.Wait()
|
|
|
|
t.tab.close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadRandomNodes reads random nodes from the local table.
|
|
|
|
func (t *UDPv4) ReadRandomNodes(buf []*enode.Node) int {
|
|
|
|
return t.tab.ReadRandomNodes(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupRandom finds random nodes in the network.
|
|
|
|
func (t *UDPv4) LookupRandom() []*enode.Node {
|
|
|
|
if t.tab.len() == 0 {
|
|
|
|
// All nodes were dropped, refresh. The very first query will hit this
|
|
|
|
// case and run the bootstrapping logic.
|
|
|
|
<-t.tab.refresh()
|
|
|
|
}
|
|
|
|
return t.lookupRandom()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) LookupPubkey(key *ecdsa.PublicKey) []*enode.Node {
|
|
|
|
if t.tab.len() == 0 {
|
|
|
|
// All nodes were dropped, refresh. The very first query will hit this
|
|
|
|
// case and run the bootstrapping logic.
|
|
|
|
<-t.tab.refresh()
|
|
|
|
}
|
|
|
|
return unwrapNodes(t.lookup(encodePubkey(key)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) lookupRandom() []*enode.Node {
|
|
|
|
var target encPubkey
|
|
|
|
crand.Read(target[:])
|
|
|
|
return unwrapNodes(t.lookup(target))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) lookupSelf() []*enode.Node {
|
|
|
|
return unwrapNodes(t.lookup(encodePubkey(&t.priv.PublicKey)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup performs a network search for nodes close to the given target. It approaches the
|
|
|
|
// target by querying nodes that are closer to it on each iteration. The given target does
|
|
|
|
// not need to be an actual node identifier.
|
|
|
|
func (t *UDPv4) lookup(targetKey encPubkey) []*node {
|
|
|
|
var (
|
|
|
|
target = enode.ID(crypto.Keccak256Hash(targetKey[:]))
|
|
|
|
asked = make(map[enode.ID]bool)
|
|
|
|
seen = make(map[enode.ID]bool)
|
|
|
|
reply = make(chan []*node, alpha)
|
|
|
|
pendingQueries = 0
|
|
|
|
result *nodesByDistance
|
|
|
|
)
|
|
|
|
// Don't query further if we hit ourself.
|
|
|
|
// Unlikely to happen often in practice.
|
|
|
|
asked[t.Self().ID()] = true
|
|
|
|
|
|
|
|
// Generate the initial result set.
|
|
|
|
t.tab.mutex.Lock()
|
|
|
|
result = t.tab.closest(target, bucketSize, false)
|
|
|
|
t.tab.mutex.Unlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// ask the alpha closest nodes that we haven't asked yet
|
|
|
|
for i := 0; i < len(result.entries) && pendingQueries < alpha; i++ {
|
|
|
|
n := result.entries[i]
|
|
|
|
if !asked[n.ID()] {
|
|
|
|
asked[n.ID()] = true
|
|
|
|
pendingQueries++
|
|
|
|
go t.lookupWorker(n, targetKey, reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pendingQueries == 0 {
|
|
|
|
// we have asked all closest nodes, stop the search
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case nodes := <-reply:
|
|
|
|
for _, n := range nodes {
|
|
|
|
if n != nil && !seen[n.ID()] {
|
|
|
|
seen[n.ID()] = true
|
|
|
|
result.push(n, bucketSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-t.tab.closeReq:
|
|
|
|
return nil // shutdown, no need to continue.
|
|
|
|
}
|
|
|
|
pendingQueries--
|
|
|
|
}
|
|
|
|
return result.entries
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) lookupWorker(n *node, targetKey encPubkey, reply chan<- []*node) {
|
|
|
|
fails := t.db.FindFails(n.ID(), n.IP())
|
|
|
|
r, err := t.findnode(n.ID(), n.addr(), targetKey)
|
|
|
|
if err == errClosed {
|
|
|
|
// Avoid recording failures on shutdown.
|
|
|
|
reply <- nil
|
|
|
|
return
|
|
|
|
} else if len(r) == 0 {
|
|
|
|
fails++
|
|
|
|
t.db.UpdateFindFails(n.ID(), n.IP(), fails)
|
|
|
|
t.log.Trace("Findnode failed", "id", n.ID(), "failcount", fails, "err", err)
|
|
|
|
if fails >= maxFindnodeFailures {
|
|
|
|
t.log.Trace("Too many findnode failures, dropping", "id", n.ID(), "failcount", fails)
|
|
|
|
t.tab.delete(n)
|
|
|
|
}
|
|
|
|
} else if fails > 0 {
|
2019-05-15 04:47:45 +00:00
|
|
|
// Reset failure counter because it counts _consecutive_ failures.
|
|
|
|
t.db.UpdateFindFails(n.ID(), n.IP(), 0)
|
2019-04-30 11:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab as many nodes as possible. Some of them might not be alive anymore, but we'll
|
|
|
|
// just remove those again during revalidation.
|
|
|
|
for _, n := range r {
|
|
|
|
t.tab.addSeenNode(n)
|
|
|
|
}
|
|
|
|
reply <- r
|
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// Resolve searches for a specific node with the given ID and tries to get the most recent
|
|
|
|
// version of the node record for it. It returns n if the node could not be resolved.
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) Resolve(n *enode.Node) *enode.Node {
|
2019-05-15 04:47:45 +00:00
|
|
|
// Try asking directly. This works if the node is still responding on the endpoint we have.
|
|
|
|
if rn, err := t.requestENR(n); err == nil {
|
|
|
|
return rn
|
|
|
|
}
|
|
|
|
// Check table for the ID, we might have a newer version there.
|
|
|
|
if intable := t.tab.getNode(n.ID()); intable != nil && intable.Seq() > n.Seq() {
|
|
|
|
n = intable
|
|
|
|
if rn, err := t.requestENR(n); err == nil {
|
|
|
|
return rn
|
2019-04-30 11:13:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 04:47:45 +00:00
|
|
|
// Otherwise perform a network lookup.
|
2019-05-15 15:11:17 +00:00
|
|
|
var key enode.Secp256k1
|
|
|
|
if n.Load(&key) != nil {
|
2019-05-15 04:47:45 +00:00
|
|
|
return n // no secp256k1 key
|
|
|
|
}
|
2019-05-15 15:11:17 +00:00
|
|
|
result := t.LookupPubkey((*ecdsa.PublicKey)(&key))
|
2019-05-15 04:47:45 +00:00
|
|
|
for _, rn := range result {
|
|
|
|
if rn.ID() == n.ID() {
|
|
|
|
if rn, err := t.requestENR(rn); err == nil {
|
|
|
|
return rn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
2018-10-12 09:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) ourEndpoint() rpcEndpoint {
|
|
|
|
n := t.Self()
|
2018-10-12 09:47:24 +00:00
|
|
|
a := &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
|
|
|
return makeEndpoint(a, uint16(n.TCP()))
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ping sends a ping message to the given node and waits for a reply.
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
|
|
|
|
rm := t.sendPing(n.ID(), &net.UDPAddr{IP: n.IP(), Port: n.UDP()}, nil)
|
|
|
|
if err = <-rm.errc; err == nil {
|
|
|
|
seq = seqFromTail(rm.reply.(*pongV4).Rest)
|
|
|
|
}
|
|
|
|
return seq, err
|
2018-07-03 13:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendPing sends a ping message to the given node and invokes the callback
|
|
|
|
// when the reply arrives.
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) sendPing(toid enode.ID, toaddr *net.UDPAddr, callback func()) *replyMatcher {
|
|
|
|
req := t.makePing(toaddr)
|
|
|
|
packet, hash, err := t.encode(t.priv, req)
|
2018-02-12 12:36:09 +00:00
|
|
|
if err != nil {
|
2018-07-03 13:24:12 +00:00
|
|
|
errc := make(chan error, 1)
|
|
|
|
errc <- err
|
2019-05-15 04:47:45 +00:00
|
|
|
return &replyMatcher{errc: errc}
|
2018-02-12 12:36:09 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
// Add a matcher for the reply to the pending reply queue. Pongs are matched if they
|
|
|
|
// reference the ping we're about to send.
|
2019-05-15 04:47:45 +00:00
|
|
|
rm := t.pending(toid, toaddr.IP, p_pongV4, func(p interface{}) (matched bool, requestDone bool) {
|
2019-04-30 11:13:22 +00:00
|
|
|
matched = bytes.Equal(p.(*pongV4).ReplyTok, hash)
|
2019-01-29 16:39:20 +00:00
|
|
|
if matched && callback != nil {
|
2018-07-03 13:24:12 +00:00
|
|
|
callback()
|
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
return matched, matched
|
2015-01-27 13:33:26 +00:00
|
|
|
})
|
2019-01-29 16:39:20 +00:00
|
|
|
// Send the packet.
|
2018-10-12 09:47:24 +00:00
|
|
|
t.localNode.UDPContact(toaddr)
|
2019-01-29 16:39:20 +00:00
|
|
|
t.write(toaddr, toid, req.name(), packet)
|
2019-05-15 04:47:45 +00:00
|
|
|
return rm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) makePing(toaddr *net.UDPAddr) *pingV4 {
|
|
|
|
seq, _ := rlp.EncodeToBytes(t.localNode.Node().Seq())
|
|
|
|
return &pingV4{
|
|
|
|
Version: 4,
|
|
|
|
From: t.ourEndpoint(),
|
|
|
|
To: makeEndpoint(toaddr, 0),
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
Rest: []rlp.RawValue{seq},
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// findnode sends a findnode request to the given node and waits until
|
|
|
|
// the node has sent up to k neighbors.
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) findnode(toid enode.ID, toaddr *net.UDPAddr, target encPubkey) ([]*node, error) {
|
2019-05-15 04:47:45 +00:00
|
|
|
t.ensureBond(toid, toaddr)
|
2018-07-03 13:24:12 +00:00
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
// Add a matcher for 'neighbours' replies to the pending reply queue. The matcher is
|
|
|
|
// active until enough nodes have been received.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
nodes := make([]*node, 0, bucketSize)
|
2015-01-27 13:33:26 +00:00
|
|
|
nreceived := 0
|
2019-05-15 04:47:45 +00:00
|
|
|
rm := t.pending(toid, toaddr.IP, p_neighborsV4, func(r interface{}) (matched bool, requestDone bool) {
|
2019-04-30 11:13:22 +00:00
|
|
|
reply := r.(*neighborsV4)
|
2015-04-23 10:11:21 +00:00
|
|
|
for _, rn := range reply.Nodes {
|
2015-01-27 13:33:26 +00:00
|
|
|
nreceived++
|
2016-11-22 19:51:59 +00:00
|
|
|
n, err := t.nodeFromRPC(toaddr, rn)
|
2016-11-21 17:11:54 +00:00
|
|
|
if err != nil {
|
2019-04-30 11:13:22 +00:00
|
|
|
t.log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err)
|
2016-11-21 17:11:54 +00:00
|
|
|
continue
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2016-11-21 17:11:54 +00:00
|
|
|
nodes = append(nodes, n)
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
return true, nreceived >= bucketSize
|
2015-01-27 13:33:26 +00:00
|
|
|
})
|
2019-05-15 04:47:45 +00:00
|
|
|
t.send(toaddr, toid, &findnodeV4{
|
2015-01-27 13:33:26 +00:00
|
|
|
Target: target,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2019-05-15 04:47:45 +00:00
|
|
|
return nodes, <-rm.errc
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestENR sends enrRequest to the given node and waits for a response.
|
|
|
|
func (t *UDPv4) requestENR(n *enode.Node) (*enode.Node, error) {
|
|
|
|
addr := &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
|
|
|
t.ensureBond(n.ID(), addr)
|
|
|
|
|
|
|
|
req := &enrRequestV4{
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
}
|
|
|
|
packet, hash, err := t.encode(t.priv, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Add a matcher for the reply to the pending reply queue. Responses are matched if
|
|
|
|
// they reference the request we're about to send.
|
|
|
|
rm := t.pending(n.ID(), addr.IP, p_enrResponseV4, func(r interface{}) (matched bool, requestDone bool) {
|
|
|
|
matched = bytes.Equal(r.(*enrResponseV4).ReplyTok, hash)
|
|
|
|
return matched, matched
|
|
|
|
})
|
|
|
|
// Send the packet and wait for the reply.
|
|
|
|
t.write(addr, n.ID(), req.name(), packet)
|
|
|
|
if err := <-rm.errc; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Verify the response record.
|
|
|
|
respN, err := enode.New(enode.ValidSchemes, &rm.reply.(*enrResponseV4).Record)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if respN.ID() != n.ID() {
|
|
|
|
return nil, fmt.Errorf("invalid ID in response record")
|
|
|
|
}
|
|
|
|
if respN.Seq() < n.Seq() {
|
|
|
|
return n, nil // response record is older
|
|
|
|
}
|
|
|
|
if err := netutil.CheckRelayIP(addr.IP, respN.IP()); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid IP in response record: %v", err)
|
|
|
|
}
|
|
|
|
return respN, nil
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
// pending adds a reply matcher to the pending reply queue.
|
|
|
|
// see the documentation of type replyMatcher for a detailed explanation.
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) pending(id enode.ID, ip net.IP, ptype byte, callback replyMatchFunc) *replyMatcher {
|
2015-01-27 13:33:26 +00:00
|
|
|
ch := make(chan error, 1)
|
2019-01-29 16:39:20 +00:00
|
|
|
p := &replyMatcher{from: id, ip: ip, ptype: ptype, callback: callback, errc: ch}
|
2015-01-27 13:33:26 +00:00
|
|
|
select {
|
2019-01-29 16:39:20 +00:00
|
|
|
case t.addReplyMatcher <- p:
|
2015-01-27 13:33:26 +00:00
|
|
|
// loop will handle it
|
|
|
|
case <-t.closing:
|
|
|
|
ch <- errClosed
|
|
|
|
}
|
2019-05-15 04:47:45 +00:00
|
|
|
return p
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
// handleReply dispatches a reply packet, invoking reply matchers. It returns
|
|
|
|
// whether any matcher considered the packet acceptable.
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) handleReply(from enode.ID, fromIP net.IP, req packetV4) bool {
|
2015-08-08 00:49:28 +00:00
|
|
|
matched := make(chan bool, 1)
|
2015-03-25 15:45:53 +00:00
|
|
|
select {
|
2019-05-15 04:47:45 +00:00
|
|
|
case t.gotreply <- reply{from, fromIP, req, matched}:
|
2015-03-25 15:45:53 +00:00
|
|
|
// loop will handle it
|
|
|
|
return <-matched
|
|
|
|
case <-t.closing:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:31:58 +00:00
|
|
|
// loop runs in its own goroutine. it keeps track of
|
2015-01-27 13:33:26 +00:00
|
|
|
// the refresh timer and the pending reply queue.
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) loop() {
|
2018-10-12 09:47:24 +00:00
|
|
|
defer t.wg.Done()
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
var (
|
2016-02-15 18:31:58 +00:00
|
|
|
plist = list.New()
|
|
|
|
timeout = time.NewTimer(0)
|
2019-01-29 16:39:20 +00:00
|
|
|
nextTimeout *replyMatcher // head of plist when timeout was last reset
|
|
|
|
contTimeouts = 0 // number of continuous timeouts to do NTP checks
|
2016-02-19 14:18:55 +00:00
|
|
|
ntpWarnTime = time.Unix(0, 0)
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
<-timeout.C // ignore first timeout
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2015-08-08 00:49:28 +00:00
|
|
|
resetTimeout := func() {
|
|
|
|
if plist.Front() == nil || nextTimeout == plist.Front().Value {
|
2015-01-27 13:33:26 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-08 00:49:28 +00:00
|
|
|
// Start the timer so it fires when the next pending reply has expired.
|
|
|
|
now := time.Now()
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 16:39:20 +00:00
|
|
|
nextTimeout = el.Value.(*replyMatcher)
|
2015-08-08 00:49:28 +00:00
|
|
|
if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout {
|
|
|
|
timeout.Reset(dist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove pending replies whose deadline is too far in the
|
|
|
|
// future. These can occur if the system clock jumped
|
|
|
|
// backwards after the deadline was assigned.
|
|
|
|
nextTimeout.errc <- errClockWarp
|
|
|
|
plist.Remove(el)
|
|
|
|
}
|
|
|
|
nextTimeout = nil
|
|
|
|
timeout.Stop()
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-08-08 00:49:28 +00:00
|
|
|
resetTimeout()
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
select {
|
|
|
|
case <-t.closing:
|
2015-08-08 00:49:28 +00:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 16:39:20 +00:00
|
|
|
el.Value.(*replyMatcher).errc <- errClosed
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
|
2019-01-29 16:39:20 +00:00
|
|
|
case p := <-t.addReplyMatcher:
|
2015-01-27 13:33:26 +00:00
|
|
|
p.deadline = time.Now().Add(respTimeout)
|
2015-08-08 00:49:28 +00:00
|
|
|
plist.PushBack(p)
|
2015-01-27 13:33:26 +00:00
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
case r := <-t.gotreply:
|
2019-01-29 16:39:20 +00:00
|
|
|
var matched bool // whether any replyMatcher considered the reply acceptable.
|
2015-08-08 00:49:28 +00:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 16:39:20 +00:00
|
|
|
p := el.Value.(*replyMatcher)
|
2019-05-15 04:47:45 +00:00
|
|
|
if p.from == r.from && p.ptype == r.data.kind() && p.ip.Equal(r.ip) {
|
2019-01-29 16:39:20 +00:00
|
|
|
ok, requestDone := p.callback(r.data)
|
|
|
|
matched = matched || ok
|
|
|
|
// Remove the matcher if callback indicates that all replies have been received.
|
|
|
|
if requestDone {
|
2019-05-15 04:47:45 +00:00
|
|
|
p.reply = r.data
|
2015-03-25 15:45:53 +00:00
|
|
|
p.errc <- nil
|
2015-08-08 00:49:28 +00:00
|
|
|
plist.Remove(el)
|
2015-03-25 15:45:53 +00:00
|
|
|
}
|
2016-02-15 18:31:58 +00:00
|
|
|
// Reset the continuous timeout counter (time drift detection)
|
|
|
|
contTimeouts = 0
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
r.matched <- matched
|
2015-01-27 13:33:26 +00:00
|
|
|
|
|
|
|
case now := <-timeout.C:
|
2015-08-08 00:49:28 +00:00
|
|
|
nextTimeout = nil
|
2016-02-15 18:31:58 +00:00
|
|
|
|
2015-08-08 00:49:28 +00:00
|
|
|
// Notify and remove callbacks whose deadline is in the past.
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 16:39:20 +00:00
|
|
|
p := el.Value.(*replyMatcher)
|
2015-08-08 00:49:28 +00:00
|
|
|
if now.After(p.deadline) || now.Equal(p.deadline) {
|
|
|
|
p.errc <- errTimeout
|
|
|
|
plist.Remove(el)
|
2016-02-15 18:31:58 +00:00
|
|
|
contTimeouts++
|
2015-08-08 00:49:28 +00:00
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2016-02-15 18:31:58 +00:00
|
|
|
// If we've accumulated too many timeouts, do an NTP time sync check
|
2016-02-19 14:18:55 +00:00
|
|
|
if contTimeouts > ntpFailureThreshold {
|
|
|
|
if time.Since(ntpWarnTime) >= ntpWarningCooldown {
|
|
|
|
ntpWarnTime = time.Now()
|
|
|
|
go checkClockDrift()
|
|
|
|
}
|
2016-02-15 18:31:58 +00:00
|
|
|
contTimeouts = 0
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
macSize = 256 / 8
|
|
|
|
sigSize = 520 / 8
|
|
|
|
headSize = macSize + sigSize // space of packet frame data
|
|
|
|
)
|
|
|
|
|
2015-05-13 19:29:32 +00:00
|
|
|
var (
|
|
|
|
headSpace = make([]byte, headSize)
|
|
|
|
|
2015-08-08 00:49:28 +00:00
|
|
|
// Neighbors replies are sent across multiple packets to
|
2019-02-19 11:27:29 +00:00
|
|
|
// stay below the packet size limit. We compute the maximum number
|
2015-05-13 19:29:32 +00:00
|
|
|
// of entries by stuffing a packet until it grows too large.
|
|
|
|
maxNeighbors int
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-04-30 11:13:22 +00:00
|
|
|
p := neighborsV4{Expiration: ^uint64(0)}
|
2015-05-13 19:29:32 +00:00
|
|
|
maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)}
|
|
|
|
for n := 0; ; n++ {
|
|
|
|
p.Nodes = append(p.Nodes, maxSizeNode)
|
|
|
|
size, _, err := rlp.EncodeToReader(p)
|
|
|
|
if err != nil {
|
|
|
|
// If this ever happens, it will be caught by the unit tests.
|
|
|
|
panic("cannot encode: " + err.Error())
|
|
|
|
}
|
2019-02-19 11:27:29 +00:00
|
|
|
if headSize+size+1 >= maxPacketSize {
|
2015-05-13 19:29:32 +00:00
|
|
|
maxNeighbors = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) send(toaddr *net.UDPAddr, toid enode.ID, req packetV4) ([]byte, error) {
|
|
|
|
packet, hash, err := t.encode(t.priv, req)
|
2015-03-25 15:45:53 +00:00
|
|
|
if err != nil {
|
2018-02-12 12:36:09 +00:00
|
|
|
return hash, err
|
2015-03-25 15:45:53 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
return hash, t.write(toaddr, toid, req.name(), packet)
|
2018-02-12 12:36:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) write(toaddr *net.UDPAddr, toid enode.ID, what string, packet []byte) error {
|
2018-02-12 12:36:09 +00:00
|
|
|
_, err := t.conn.WriteToUDP(packet, toaddr)
|
2019-04-30 11:13:22 +00:00
|
|
|
t.log.Trace(">> "+what, "id", toid, "addr", toaddr, "err", err)
|
2015-03-25 15:45:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
func (t *UDPv4) encode(priv *ecdsa.PrivateKey, req packetV4) (packet, hash []byte, err error) {
|
|
|
|
name := req.name()
|
2015-01-27 13:33:26 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.Write(headSpace)
|
2019-05-15 04:47:45 +00:00
|
|
|
b.WriteByte(req.kind())
|
2015-01-27 13:33:26 +00:00
|
|
|
if err := rlp.Encode(b, req); err != nil {
|
2019-05-15 04:47:45 +00:00
|
|
|
t.log.Error(fmt.Sprintf("Can't encode %s packet", name), "err", err)
|
2018-02-12 12:36:09 +00:00
|
|
|
return nil, nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2018-02-12 12:36:09 +00:00
|
|
|
packet = b.Bytes()
|
2016-02-21 18:40:27 +00:00
|
|
|
sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
|
2015-01-27 13:33:26 +00:00
|
|
|
if err != nil {
|
2019-05-15 04:47:45 +00:00
|
|
|
t.log.Error(fmt.Sprintf("Can't sign %s packet", name), "err", err)
|
2018-02-12 12:36:09 +00:00
|
|
|
return nil, nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
copy(packet[macSize:], sig)
|
|
|
|
// add the hash to the front. Note: this doesn't protect the
|
|
|
|
// packet in any way. Our public key will be part of this hash in
|
2015-03-25 15:45:53 +00:00
|
|
|
// The future.
|
2018-02-12 12:36:09 +00:00
|
|
|
hash = crypto.Keccak256(packet[macSize:])
|
|
|
|
copy(packet, hash)
|
|
|
|
return packet, hash, nil
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) readLoop(unhandled chan<- ReadPacket) {
|
2018-10-12 09:47:24 +00:00
|
|
|
defer t.wg.Done()
|
2018-01-22 12:38:34 +00:00
|
|
|
if unhandled != nil {
|
|
|
|
defer close(unhandled)
|
|
|
|
}
|
2018-10-12 09:47:24 +00:00
|
|
|
|
2019-02-19 11:27:29 +00:00
|
|
|
buf := make([]byte, maxPacketSize)
|
2015-01-27 13:33:26 +00:00
|
|
|
for {
|
|
|
|
nbytes, from, err := t.conn.ReadFromUDP(buf)
|
2016-11-21 17:39:36 +00:00
|
|
|
if netutil.IsTemporaryError(err) {
|
2015-08-19 12:11:12 +00:00
|
|
|
// Ignore temporary read errors.
|
2019-04-30 11:13:22 +00:00
|
|
|
t.log.Debug("Temporary UDP read error", "err", err)
|
2015-08-19 12:11:12 +00:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
// Shut down the loop for permament errors.
|
2019-04-30 11:13:22 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
t.log.Debug("UDP read error", "err", err)
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-22 12:38:34 +00:00
|
|
|
if t.handlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
|
|
|
|
select {
|
|
|
|
case unhandled <- ReadPacket{buf[:nbytes], from}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 23:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (t *UDPv4) handlePacket(from *net.UDPAddr, buf []byte) error {
|
|
|
|
packet, fromKey, hash, err := decodeV4(buf)
|
2015-04-17 23:50:31 +00:00
|
|
|
if err != nil {
|
2019-04-30 11:13:22 +00:00
|
|
|
t.log.Debug("Bad discv4 packet", "addr", from, "err", err)
|
2015-04-17 23:50:31 +00:00
|
|
|
return err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
fromID := fromKey.id()
|
|
|
|
if err == nil {
|
|
|
|
err = packet.preverify(t, from, fromID, fromKey)
|
|
|
|
}
|
2019-04-30 11:13:22 +00:00
|
|
|
t.log.Trace("<< "+packet.name(), "id", fromID, "addr", from, "err", err)
|
2019-01-29 16:39:20 +00:00
|
|
|
if err == nil {
|
|
|
|
packet.handle(t, from, fromID, hash)
|
|
|
|
}
|
2015-04-17 23:50:31 +00:00
|
|
|
return err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func decodeV4(buf []byte) (packetV4, encPubkey, []byte, error) {
|
2015-01-27 13:33:26 +00:00
|
|
|
if len(buf) < headSize+1 {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
return nil, encPubkey{}, nil, errPacketTooSmall
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
|
2016-02-21 18:40:27 +00:00
|
|
|
shouldhash := crypto.Keccak256(buf[macSize:])
|
2015-01-27 13:33:26 +00:00
|
|
|
if !bytes.Equal(hash, shouldhash) {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
return nil, encPubkey{}, nil, errBadHash
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
fromKey, err := recoverNodeKey(crypto.Keccak256(buf[headSize:]), sig)
|
2015-01-27 13:33:26 +00:00
|
|
|
if err != nil {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
return nil, fromKey, hash, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
var req packetV4
|
2015-01-27 13:33:26 +00:00
|
|
|
switch ptype := sigdata[0]; ptype {
|
2019-04-30 11:13:22 +00:00
|
|
|
case p_pingV4:
|
|
|
|
req = new(pingV4)
|
|
|
|
case p_pongV4:
|
|
|
|
req = new(pongV4)
|
|
|
|
case p_findnodeV4:
|
|
|
|
req = new(findnodeV4)
|
|
|
|
case p_neighborsV4:
|
|
|
|
req = new(neighborsV4)
|
2019-05-15 04:47:45 +00:00
|
|
|
case p_enrRequestV4:
|
|
|
|
req = new(enrRequestV4)
|
|
|
|
case p_enrResponseV4:
|
|
|
|
req = new(enrResponseV4)
|
2015-01-27 13:33:26 +00:00
|
|
|
default:
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
return nil, fromKey, hash, fmt.Errorf("unknown type: %d", ptype)
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-12-23 00:52:40 +00:00
|
|
|
s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
|
|
|
|
err = s.Decode(req)
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
return req, fromKey, hash, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// checkBond checks if the given node has a recent enough endpoint proof.
|
|
|
|
func (t *UDPv4) checkBond(id enode.ID, ip net.IP) bool {
|
|
|
|
return time.Since(t.db.LastPongReceived(id, ip)) < bondExpiration
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureBond solicits a ping from a node if we haven't seen a ping from it for a while.
|
|
|
|
// This ensures there is a valid endpoint proof on the remote end.
|
|
|
|
func (t *UDPv4) ensureBond(toid enode.ID, toaddr *net.UDPAddr) {
|
|
|
|
tooOld := time.Since(t.db.LastPingReceived(toid, toaddr.IP)) > bondExpiration
|
|
|
|
if tooOld || t.db.FindFails(toid, toaddr.IP) > maxFindnodeFailures {
|
|
|
|
rm := t.sendPing(toid, toaddr, nil)
|
|
|
|
<-rm.errc
|
|
|
|
// Wait for them to ping back and process our pong.
|
|
|
|
time.Sleep(respTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expired checks whether the given UNIX time stamp is in the past.
|
|
|
|
func expired(ts uint64) bool {
|
|
|
|
return time.Unix(int64(ts), 0).Before(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
func seqFromTail(tail []rlp.RawValue) uint64 {
|
|
|
|
if len(tail) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var seq uint64
|
|
|
|
rlp.DecodeBytes(tail[0], &seq)
|
|
|
|
return seq
|
|
|
|
}
|
|
|
|
|
|
|
|
// PING/v4
|
|
|
|
|
|
|
|
func (req *pingV4) name() string { return "PING/v4" }
|
|
|
|
func (req *pingV4) kind() byte { return p_pingV4 }
|
2019-01-29 16:39:20 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *pingV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
2015-01-27 13:33:26 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
key, err := decodePubkey(fromKey)
|
|
|
|
if err != nil {
|
2019-01-29 16:39:20 +00:00
|
|
|
return errors.New("invalid public key")
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
req.senderKey = key
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *pingV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
2019-01-29 16:39:20 +00:00
|
|
|
// Reply.
|
2019-05-15 04:47:45 +00:00
|
|
|
seq, _ := rlp.EncodeToBytes(t.localNode.Node().Seq())
|
|
|
|
t.send(from, fromID, &pongV4{
|
2015-04-17 23:50:31 +00:00
|
|
|
To: makeEndpoint(from, req.From.TCP),
|
2015-01-27 13:33:26 +00:00
|
|
|
ReplyTok: mac,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
2019-05-15 04:47:45 +00:00
|
|
|
Rest: []rlp.RawValue{seq},
|
2015-01-27 13:33:26 +00:00
|
|
|
})
|
2019-01-29 16:39:20 +00:00
|
|
|
|
|
|
|
// Ping back if our last pong on file is too far in the past.
|
|
|
|
n := wrapNode(enode.NewV4(req.senderKey, from.IP, int(req.From.TCP), from.Port))
|
|
|
|
if time.Since(t.db.LastPongReceived(n.ID(), from.IP)) > bondExpiration {
|
|
|
|
t.sendPing(fromID, from, func() {
|
2019-01-31 10:48:54 +00:00
|
|
|
t.tab.addVerifiedNode(n)
|
2019-01-29 16:39:20 +00:00
|
|
|
})
|
2018-07-03 13:24:12 +00:00
|
|
|
} else {
|
2019-01-31 10:48:54 +00:00
|
|
|
t.tab.addVerifiedNode(n)
|
2015-03-25 15:45:53 +00:00
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
|
|
|
|
// Update node database and endpoint predictor.
|
|
|
|
t.db.UpdateLastPingReceived(n.ID(), from.IP, time.Now())
|
2018-10-12 09:47:24 +00:00
|
|
|
t.localNode.UDPEndpointStatement(from, &net.UDPAddr{IP: req.To.IP, Port: int(req.To.UDP)})
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// PONG/v4
|
|
|
|
|
|
|
|
func (req *pongV4) name() string { return "PONG/v4" }
|
|
|
|
func (req *pongV4) kind() byte { return p_pongV4 }
|
2017-02-24 08:58:04 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *pongV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
2015-01-27 13:33:26 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2019-05-15 04:47:45 +00:00
|
|
|
if !t.handleReply(fromID, from.IP, req) {
|
2015-03-25 15:45:53 +00:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *pongV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
2019-01-29 16:39:20 +00:00
|
|
|
t.localNode.UDPEndpointStatement(from, &net.UDPAddr{IP: req.To.IP, Port: int(req.To.UDP)})
|
|
|
|
t.db.UpdateLastPongReceived(fromID, from.IP, time.Now())
|
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// FINDNODE/v4
|
|
|
|
|
|
|
|
func (req *findnodeV4) name() string { return "FINDNODE/v4" }
|
|
|
|
func (req *findnodeV4) kind() byte { return p_findnodeV4 }
|
2017-02-24 08:58:04 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *findnodeV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
2015-01-27 13:33:26 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2019-05-15 04:47:45 +00:00
|
|
|
if !t.checkBond(fromID, from.IP) {
|
2018-07-03 13:24:12 +00:00
|
|
|
// No endpoint proof pong exists, we don't process the packet. This prevents an
|
|
|
|
// attack vector where the discovery protocol could be used to amplify traffic in a
|
|
|
|
// DDOS attack. A malicious actor would send a findnode request with the IP address
|
|
|
|
// and UDP port of the target as the source address. The recipient of the findnode
|
|
|
|
// packet would then send a neighbors packet (which is a much bigger packet than
|
|
|
|
// findnode) to the victim.
|
2015-03-25 15:45:53 +00:00
|
|
|
return errUnknownNode
|
|
|
|
}
|
2019-01-29 16:39:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *findnodeV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
2019-01-29 16:39:20 +00:00
|
|
|
// Determine closest nodes.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
target := enode.ID(crypto.Keccak256Hash(req.Target[:]))
|
2018-10-12 09:47:24 +00:00
|
|
|
t.tab.mutex.Lock()
|
2019-04-30 11:13:22 +00:00
|
|
|
closest := t.tab.closest(target, bucketSize, true).entries
|
2018-10-12 09:47:24 +00:00
|
|
|
t.tab.mutex.Unlock()
|
2015-01-27 13:33:26 +00:00
|
|
|
|
2015-05-13 19:29:32 +00:00
|
|
|
// Send neighbors in chunks with at most maxNeighbors per packet
|
2019-02-19 11:27:29 +00:00
|
|
|
// to stay below the packet size limit.
|
2019-04-30 11:13:22 +00:00
|
|
|
p := neighborsV4{Expiration: uint64(time.Now().Add(expiration).Unix())}
|
2019-01-29 16:39:20 +00:00
|
|
|
var sent bool
|
2018-02-12 12:36:09 +00:00
|
|
|
for _, n := range closest {
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-24 22:59:00 +00:00
|
|
|
if netutil.CheckRelayIP(from.IP, n.IP()) == nil {
|
2018-02-12 12:36:09 +00:00
|
|
|
p.Nodes = append(p.Nodes, nodeToRPC(n))
|
2016-11-21 17:11:54 +00:00
|
|
|
}
|
2018-02-12 12:36:09 +00:00
|
|
|
if len(p.Nodes) == maxNeighbors {
|
2019-05-15 04:47:45 +00:00
|
|
|
t.send(from, fromID, &p)
|
2015-05-13 19:29:32 +00:00
|
|
|
p.Nodes = p.Nodes[:0]
|
2018-02-12 12:36:09 +00:00
|
|
|
sent = true
|
2015-05-13 19:29:32 +00:00
|
|
|
}
|
2015-05-13 18:03:17 +00:00
|
|
|
}
|
2018-02-12 12:36:09 +00:00
|
|
|
if len(p.Nodes) > 0 || !sent {
|
2019-05-15 04:47:45 +00:00
|
|
|
t.send(from, fromID, &p)
|
2018-02-12 12:36:09 +00:00
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// NEIGHBORS/v4
|
|
|
|
|
|
|
|
func (req *neighborsV4) name() string { return "NEIGHBORS/v4" }
|
|
|
|
func (req *neighborsV4) kind() byte { return p_neighborsV4 }
|
2017-02-24 08:58:04 +00:00
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *neighborsV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
2015-01-27 13:33:26 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2019-05-15 04:47:45 +00:00
|
|
|
if !t.handleReply(fromID, from.IP, req) {
|
2015-03-25 15:45:53 +00:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:13:22 +00:00
|
|
|
func (req *neighborsV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
2019-01-29 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
// ENRREQUEST/v4
|
2017-02-24 08:58:04 +00:00
|
|
|
|
2019-05-15 04:47:45 +00:00
|
|
|
func (req *enrRequestV4) name() string { return "ENRREQUEST/v4" }
|
|
|
|
func (req *enrRequestV4) kind() byte { return p_enrRequestV4 }
|
|
|
|
|
|
|
|
func (req *enrRequestV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
|
|
|
if !t.checkBond(fromID, from.IP) {
|
|
|
|
return errUnknownNode
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *enrRequestV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
|
|
|
t.send(from, fromID, &enrResponseV4{
|
|
|
|
ReplyTok: mac,
|
|
|
|
Record: *t.localNode.Node().Record(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ENRRESPONSE/v4
|
|
|
|
|
|
|
|
func (req *enrResponseV4) name() string { return "ENRRESPONSE/v4" }
|
|
|
|
func (req *enrResponseV4) kind() byte { return p_enrResponseV4 }
|
|
|
|
|
|
|
|
func (req *enrResponseV4) preverify(t *UDPv4, from *net.UDPAddr, fromID enode.ID, fromKey encPubkey) error {
|
|
|
|
if !t.handleReply(fromID, from.IP, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *enrResponseV4) handle(t *UDPv4, from *net.UDPAddr, fromID enode.ID, mac []byte) {
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|