2022-05-24 20:39:40 +02:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2020-04-08 09:57:23 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
crand "crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2024-05-29 15:02:26 +02:00
|
|
|
"net/netip"
|
2024-04-04 18:24:49 +08:00
|
|
|
"slices"
|
2020-04-08 09:57:23 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-10-14 12:28:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover/v5wire"
|
2020-04-08 09:57:23 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lookupRequestLimit = 3 // max requests against a single node during lookup
|
2020-10-14 12:28:17 +02:00
|
|
|
findnodeResultLimit = 16 // applies in FINDNODE handler
|
2020-04-08 09:57:23 +02:00
|
|
|
totalNodesResponseLimit = 5 // applies in waitForNodes
|
|
|
|
|
|
|
|
respTimeoutV5 = 700 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// codecV5 is implemented by v5wire.Codec (and testCodec).
|
2020-04-08 09:57:23 +02:00
|
|
|
//
|
|
|
|
// The UDPv5 transport is split into two objects: the codec object deals with
|
|
|
|
// encoding/decoding and with the handshake; the UDPv5 object handles higher-level concerns.
|
|
|
|
type codecV5 interface {
|
2020-10-14 12:28:17 +02:00
|
|
|
// Encode encodes a packet.
|
|
|
|
Encode(enode.ID, string, v5wire.Packet, *v5wire.Whoareyou) ([]byte, v5wire.Nonce, error)
|
2020-04-08 09:57:23 +02:00
|
|
|
|
2022-10-04 08:44:05 +02:00
|
|
|
// Decode decodes a packet. It returns a *v5wire.Unknown packet if decryption fails.
|
2020-10-14 12:28:17 +02:00
|
|
|
// The *enode.Node return value is non-nil when the input contains a handshake response.
|
|
|
|
Decode([]byte, string) (enode.ID, *enode.Node, v5wire.Packet, error)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UDPv5 is the implementation of protocol version 5.
|
|
|
|
type UDPv5 struct {
|
|
|
|
// static fields
|
|
|
|
conn UDPConn
|
|
|
|
tab *Table
|
|
|
|
netrestrict *netutil.Netlist
|
|
|
|
priv *ecdsa.PrivateKey
|
|
|
|
localNode *enode.LocalNode
|
|
|
|
db *enode.DB
|
|
|
|
log log.Logger
|
|
|
|
clock mclock.Clock
|
|
|
|
validSchemes enr.IdentityScheme
|
|
|
|
|
2023-01-03 12:36:38 +01:00
|
|
|
// misc buffers used during message handling
|
|
|
|
logcontext []interface{}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// talkreq handler registry
|
2023-04-28 11:03:43 +02:00
|
|
|
talk *talkSystem
|
2020-10-14 12:28:17 +02:00
|
|
|
|
2020-04-08 09:57:23 +02:00
|
|
|
// channels into dispatch
|
|
|
|
packetInCh chan ReadPacket
|
|
|
|
readNextCh chan struct{}
|
|
|
|
callCh chan *callV5
|
|
|
|
callDoneCh chan *callV5
|
|
|
|
respTimeoutCh chan *callTimeout
|
2023-04-28 11:03:43 +02:00
|
|
|
sendCh chan sendRequest
|
2023-03-14 07:40:40 -04:00
|
|
|
unhandled chan<- ReadPacket
|
2020-04-08 09:57:23 +02:00
|
|
|
|
|
|
|
// state of dispatch
|
|
|
|
codec codecV5
|
|
|
|
activeCallByNode map[enode.ID]*callV5
|
2020-10-14 12:28:17 +02:00
|
|
|
activeCallByAuth map[v5wire.Nonce]*callV5
|
2020-04-08 09:57:23 +02:00
|
|
|
callQueue map[enode.ID][]*callV5
|
|
|
|
|
|
|
|
// shutdown stuff
|
|
|
|
closeOnce sync.Once
|
|
|
|
closeCtx context.Context
|
|
|
|
cancelCloseCtx context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2023-04-28 11:03:43 +02:00
|
|
|
type sendRequest struct {
|
|
|
|
destID enode.ID
|
2024-05-29 15:02:26 +02:00
|
|
|
destAddr netip.AddrPort
|
2023-04-28 11:03:43 +02:00
|
|
|
msg v5wire.Packet
|
|
|
|
}
|
2021-03-01 10:24:20 +01:00
|
|
|
|
2020-04-08 09:57:23 +02:00
|
|
|
// callV5 represents a remote procedure call against another node.
|
|
|
|
type callV5 struct {
|
2023-04-28 11:03:43 +02:00
|
|
|
id enode.ID
|
2024-05-29 15:02:26 +02:00
|
|
|
addr netip.AddrPort
|
2023-04-28 11:03:43 +02:00
|
|
|
node *enode.Node // This is required to perform handshakes.
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
packet v5wire.Packet
|
2020-04-08 09:57:23 +02:00
|
|
|
responseType byte // expected packet type of response
|
|
|
|
reqid []byte
|
2020-10-14 12:28:17 +02:00
|
|
|
ch chan v5wire.Packet // responses sent here
|
|
|
|
err chan error // errors sent here
|
2020-04-08 09:57:23 +02:00
|
|
|
|
|
|
|
// Valid for active calls only:
|
2020-10-14 12:28:17 +02:00
|
|
|
nonce v5wire.Nonce // nonce of request packet
|
|
|
|
handshakeCount int // # times we attempted handshake for this call
|
|
|
|
challenge *v5wire.Whoareyou // last sent handshake challenge
|
2020-04-08 09:57:23 +02:00
|
|
|
timeout mclock.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
// callTimeout is the response timeout event of a call.
|
|
|
|
type callTimeout struct {
|
|
|
|
c *callV5
|
|
|
|
timer mclock.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenV5 listens on the given connection.
|
|
|
|
func ListenV5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
|
|
|
|
t, err := newUDPv5(conn, ln, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go t.tab.loop()
|
|
|
|
t.wg.Add(2)
|
|
|
|
go t.readLoop()
|
|
|
|
go t.dispatch()
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newUDPv5 creates a UDPv5 transport, but doesn't start any goroutines.
|
|
|
|
func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) {
|
|
|
|
closeCtx, cancelCloseCtx := context.WithCancel(context.Background())
|
|
|
|
cfg = cfg.withDefaults()
|
|
|
|
t := &UDPv5{
|
|
|
|
// static fields
|
2023-04-25 12:12:34 +02:00
|
|
|
conn: newMeteredConn(conn),
|
2020-04-08 09:57:23 +02:00
|
|
|
localNode: ln,
|
|
|
|
db: ln.Database(),
|
|
|
|
netrestrict: cfg.NetRestrict,
|
|
|
|
priv: cfg.PrivateKey,
|
|
|
|
log: cfg.Log,
|
|
|
|
validSchemes: cfg.ValidSchemes,
|
|
|
|
clock: cfg.Clock,
|
|
|
|
// channels into dispatch
|
|
|
|
packetInCh: make(chan ReadPacket, 1),
|
|
|
|
readNextCh: make(chan struct{}, 1),
|
|
|
|
callCh: make(chan *callV5),
|
|
|
|
callDoneCh: make(chan *callV5),
|
2023-04-28 11:03:43 +02:00
|
|
|
sendCh: make(chan sendRequest),
|
2020-04-08 09:57:23 +02:00
|
|
|
respTimeoutCh: make(chan *callTimeout),
|
2023-03-14 07:40:40 -04:00
|
|
|
unhandled: cfg.Unhandled,
|
2020-04-08 09:57:23 +02:00
|
|
|
// state of dispatch
|
2022-11-30 17:03:34 -04:00
|
|
|
codec: v5wire.NewCodec(ln, cfg.PrivateKey, cfg.Clock, cfg.V5ProtocolID),
|
2020-04-08 09:57:23 +02:00
|
|
|
activeCallByNode: make(map[enode.ID]*callV5),
|
2020-10-14 12:28:17 +02:00
|
|
|
activeCallByAuth: make(map[v5wire.Nonce]*callV5),
|
2020-04-08 09:57:23 +02:00
|
|
|
callQueue: make(map[enode.ID][]*callV5),
|
|
|
|
// shutdown
|
|
|
|
closeCtx: closeCtx,
|
|
|
|
cancelCloseCtx: cancelCloseCtx,
|
|
|
|
}
|
2023-04-28 11:03:43 +02:00
|
|
|
t.talk = newTalkSystem(t)
|
p2p/discover: improved node revalidation (#29572)
Node discovery periodically revalidates the nodes in its table by sending PING, checking
if they are still alive. I recently noticed some issues with the implementation of this
process, which can cause strange results such as nodes dropping unexpectedly, certain
nodes not getting revalidated often enough, and bad results being returned to incoming
FINDNODE queries.
In this change, the revalidation process is improved with the following logic:
- We maintain two 'revalidation lists' containing the table nodes, named 'fast' and 'slow'.
- The process chooses random nodes from each list on a randomized interval, the interval being
faster for the 'fast' list, and performs revalidation for the chosen node.
- Whenever a node is newly inserted into the table, it goes into the 'fast' list.
Once validation passes, it transfers to the 'slow' list. If a request fails, or the
node changes endpoint, it transfers back into 'fast'.
- livenessChecks is incremented by one for successful checks. Unlike the old implementation,
we will not drop the node on the first failing check. We instead quickly decay the
livenessChecks give it another chance.
- Order of nodes in bucket doesn't matter anymore.
I am also adding a debug API endpoint to dump the node table content.
Co-authored-by: Martin HS <martin@swende.se>
2024-05-23 14:26:09 +02:00
|
|
|
tab, err := newTable(t, t.db, cfg)
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.tab = tab
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Self returns the local node record.
|
|
|
|
func (t *UDPv5) Self() *enode.Node {
|
|
|
|
return t.localNode.Node()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down packet processing.
|
|
|
|
func (t *UDPv5) Close() {
|
|
|
|
t.closeOnce.Do(func() {
|
|
|
|
t.cancelCloseCtx()
|
|
|
|
t.conn.Close()
|
2023-04-28 11:03:43 +02:00
|
|
|
t.talk.wait()
|
2020-04-08 09:57:23 +02:00
|
|
|
t.wg.Wait()
|
|
|
|
t.tab.close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping sends a ping message to the given node.
|
|
|
|
func (t *UDPv5) Ping(n *enode.Node) error {
|
|
|
|
_, err := t.ping(n)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (t *UDPv5) Resolve(n *enode.Node) *enode.Node {
|
|
|
|
if intable := t.tab.getNode(n.ID()); intable != nil && intable.Seq() > n.Seq() {
|
|
|
|
n = intable
|
|
|
|
}
|
|
|
|
// Try asking directly. This works if the node is still responding on the endpoint we have.
|
|
|
|
if resp, err := t.RequestENR(n); err == nil {
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
// Otherwise do a network lookup.
|
|
|
|
result := t.Lookup(n.ID())
|
|
|
|
for _, rn := range result {
|
|
|
|
if rn.ID() == n.ID() && rn.Seq() > n.Seq() {
|
|
|
|
return rn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:58:37 +08:00
|
|
|
// AllNodes returns all the nodes stored in the local table.
|
|
|
|
func (t *UDPv5) AllNodes() []*enode.Node {
|
|
|
|
t.tab.mutex.Lock()
|
|
|
|
defer t.tab.mutex.Unlock()
|
|
|
|
nodes := make([]*enode.Node, 0)
|
|
|
|
|
|
|
|
for _, b := range &t.tab.buckets {
|
|
|
|
for _, n := range b.entries {
|
2024-05-29 15:02:26 +02:00
|
|
|
nodes = append(nodes, n.Node)
|
2020-04-16 21:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalNode returns the current local node running the
|
|
|
|
// protocol.
|
|
|
|
func (t *UDPv5) LocalNode() *enode.LocalNode {
|
|
|
|
return t.localNode
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// RegisterTalkHandler adds a handler for 'talk requests'. The handler function is called
|
|
|
|
// whenever a request for the given protocol is received and should return the response
|
|
|
|
// data or nil.
|
2021-03-01 10:24:20 +01:00
|
|
|
func (t *UDPv5) RegisterTalkHandler(protocol string, handler TalkRequestHandler) {
|
2023-04-28 11:03:43 +02:00
|
|
|
t.talk.register(protocol, handler)
|
2020-10-14 12:28:17 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 11:03:43 +02:00
|
|
|
// TalkRequest sends a talk request to a node and waits for a response.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (t *UDPv5) TalkRequest(n *enode.Node, protocol string, request []byte) ([]byte, error) {
|
|
|
|
req := &v5wire.TalkRequest{Protocol: protocol, Message: request}
|
2023-04-28 11:03:43 +02:00
|
|
|
resp := t.callToNode(n, v5wire.TalkResponseMsg, req)
|
|
|
|
defer t.callDone(resp)
|
|
|
|
select {
|
|
|
|
case respMsg := <-resp.ch:
|
|
|
|
return respMsg.(*v5wire.TalkResponse).Message, nil
|
|
|
|
case err := <-resp.err:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 13:34:08 -05:00
|
|
|
// TalkRequestToID sends a talk request to a node and waits for a response.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) TalkRequestToID(id enode.ID, addr netip.AddrPort, protocol string, request []byte) ([]byte, error) {
|
2023-04-28 11:03:43 +02:00
|
|
|
req := &v5wire.TalkRequest{Protocol: protocol, Message: request}
|
|
|
|
resp := t.callToID(id, addr, v5wire.TalkResponseMsg, req)
|
2020-10-14 12:28:17 +02:00
|
|
|
defer t.callDone(resp)
|
|
|
|
select {
|
|
|
|
case respMsg := <-resp.ch:
|
|
|
|
return respMsg.(*v5wire.TalkResponse).Message, nil
|
|
|
|
case err := <-resp.err:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomNodes returns an iterator that finds random nodes in the DHT.
|
2020-04-08 09:57:23 +02:00
|
|
|
func (t *UDPv5) RandomNodes() enode.Iterator {
|
|
|
|
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 newLookupIterator(t.closeCtx, t.newRandomLookup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup performs a recursive lookup for the given target.
|
|
|
|
// It returns the closest nodes to target.
|
|
|
|
func (t *UDPv5) Lookup(target enode.ID) []*enode.Node {
|
|
|
|
return t.newLookup(t.closeCtx, target).run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupRandom looks up a random target.
|
|
|
|
// This is needed to satisfy the transport interface.
|
|
|
|
func (t *UDPv5) lookupRandom() []*enode.Node {
|
|
|
|
return t.newRandomLookup(t.closeCtx).run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupSelf looks up our own node ID.
|
|
|
|
// This is needed to satisfy the transport interface.
|
|
|
|
func (t *UDPv5) lookupSelf() []*enode.Node {
|
|
|
|
return t.newLookup(t.closeCtx, t.Self().ID()).run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv5) newRandomLookup(ctx context.Context) *lookup {
|
|
|
|
var target enode.ID
|
|
|
|
crand.Read(target[:])
|
|
|
|
return t.newLookup(ctx, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv5) newLookup(ctx context.Context, target enode.ID) *lookup {
|
2024-05-29 15:02:26 +02:00
|
|
|
return newLookup(ctx, t.tab, target, func(n *enode.Node) ([]*enode.Node, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
return t.lookupWorker(n, target)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupWorker performs FINDNODE calls against a single node during lookup.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) lookupWorker(destNode *enode.Node, target enode.ID) ([]*enode.Node, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
var (
|
|
|
|
dists = lookupDistances(target, destNode.ID())
|
|
|
|
nodes = nodesByDistance{target: target}
|
|
|
|
err error
|
|
|
|
)
|
2020-10-14 12:28:17 +02:00
|
|
|
var r []*enode.Node
|
2024-05-29 15:02:26 +02:00
|
|
|
r, err = t.findnode(destNode, dists)
|
2022-06-07 17:27:21 +02:00
|
|
|
if errors.Is(err, errClosed) {
|
2020-10-14 12:28:17 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, n := range r {
|
|
|
|
if n.ID() != t.Self().ID() {
|
2024-05-29 15:02:26 +02:00
|
|
|
nodes.push(n, findnodeResultLimit)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes.entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupDistances computes the distance parameter for FINDNODE calls to dest.
|
|
|
|
// It chooses distances adjacent to logdist(target, dest), e.g. for a target
|
|
|
|
// with logdist(target, dest) = 255 the result is [255, 256, 254].
|
2020-10-14 12:28:17 +02:00
|
|
|
func lookupDistances(target, dest enode.ID) (dists []uint) {
|
2020-04-08 09:57:23 +02:00
|
|
|
td := enode.LogDist(target, dest)
|
2020-10-14 12:28:17 +02:00
|
|
|
dists = append(dists, uint(td))
|
2020-04-08 09:57:23 +02:00
|
|
|
for i := 1; len(dists) < lookupRequestLimit; i++ {
|
2022-11-02 08:50:07 -05:00
|
|
|
if td+i <= 256 {
|
2020-10-14 12:28:17 +02:00
|
|
|
dists = append(dists, uint(td+i))
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
if td-i > 0 {
|
2020-10-14 12:28:17 +02:00
|
|
|
dists = append(dists, uint(td-i))
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dists
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping calls PING on a node and waits for a PONG response.
|
|
|
|
func (t *UDPv5) ping(n *enode.Node) (uint64, error) {
|
2020-10-14 12:28:17 +02:00
|
|
|
req := &v5wire.Ping{ENRSeq: t.localNode.Node().Seq()}
|
2023-04-28 11:03:43 +02:00
|
|
|
resp := t.callToNode(n, v5wire.PongMsg, req)
|
2020-04-08 09:57:23 +02:00
|
|
|
defer t.callDone(resp)
|
2020-10-14 12:28:17 +02:00
|
|
|
|
2020-04-08 09:57:23 +02:00
|
|
|
select {
|
|
|
|
case pong := <-resp.ch:
|
2020-10-14 12:28:17 +02:00
|
|
|
return pong.(*v5wire.Pong).ENRSeq, nil
|
2020-04-08 09:57:23 +02:00
|
|
|
case err := <-resp.err:
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 17:15:54 +09:00
|
|
|
// RequestENR requests n's record.
|
2020-04-08 09:57:23 +02:00
|
|
|
func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) {
|
2020-10-14 12:28:17 +02:00
|
|
|
nodes, err := t.findnode(n, []uint{0})
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
return nil, fmt.Errorf("%d nodes in response for distance zero", len(nodes))
|
|
|
|
}
|
|
|
|
return nodes[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// findnode calls FINDNODE on a node and waits for responses.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (t *UDPv5) findnode(n *enode.Node, distances []uint) ([]*enode.Node, error) {
|
2023-04-28 11:03:43 +02:00
|
|
|
resp := t.callToNode(n, v5wire.NodesMsg, &v5wire.Findnode{Distances: distances})
|
2020-10-14 12:28:17 +02:00
|
|
|
return t.waitForNodes(resp, distances)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForNodes waits for NODES responses to the given call.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (t *UDPv5) waitForNodes(c *callV5, distances []uint) ([]*enode.Node, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
defer t.callDone(c)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nodes []*enode.Node
|
|
|
|
seen = make(map[enode.ID]struct{})
|
|
|
|
received, total = 0, -1
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case responseP := <-c.ch:
|
2020-10-14 12:28:17 +02:00
|
|
|
response := responseP.(*v5wire.Nodes)
|
2020-04-08 09:57:23 +02:00
|
|
|
for _, record := range response.Nodes {
|
2020-10-14 12:28:17 +02:00
|
|
|
node, err := t.verifyResponseNode(c, record, distances, seen)
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
2020-10-14 12:28:17 +02:00
|
|
|
t.log.Debug("Invalid record in "+response.Name(), "id", c.node.ID(), "err", err)
|
2020-04-08 09:57:23 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
if total == -1 {
|
2023-01-03 12:36:38 +01:00
|
|
|
total = min(int(response.RespCount), totalNodesResponseLimit)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
if received++; received == total {
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
case err := <-c.err:
|
|
|
|
return nodes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyResponseNode checks validity of a record in a NODES response.
|
2020-10-14 12:28:17 +02:00
|
|
|
func (t *UDPv5) verifyResponseNode(c *callV5, r *enr.Record, distances []uint, seen map[enode.ID]struct{}) (*enode.Node, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
node, err := enode.New(t.validSchemes, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if err := netutil.CheckRelayIP(c.addr.Addr().AsSlice(), node.IP()); err != nil {
|
2020-04-08 09:57:23 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-15 23:37:51 +07:00
|
|
|
if t.netrestrict != nil && !t.netrestrict.Contains(node.IP()) {
|
|
|
|
return nil, errors.New("not contained in netrestrict list")
|
|
|
|
}
|
2023-04-28 11:03:43 +02:00
|
|
|
if node.UDP() <= 1024 {
|
2020-04-08 09:57:23 +02:00
|
|
|
return nil, errLowPort
|
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
if distances != nil {
|
2023-04-28 11:03:43 +02:00
|
|
|
nd := enode.LogDist(c.id, node.ID())
|
2024-04-04 18:24:49 +08:00
|
|
|
if !slices.Contains(distances, uint(nd)) {
|
2020-10-14 12:28:17 +02:00
|
|
|
return nil, errors.New("does not match any requested distance")
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := seen[node.ID()]; ok {
|
2024-02-29 17:56:46 +08:00
|
|
|
return nil, errors.New("duplicate record")
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
seen[node.ID()] = struct{}{}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 11:03:43 +02:00
|
|
|
// callToNode sends the given call and sets up a handler for response packets (of message
|
|
|
|
// type responseType). Responses are dispatched to the call's response channel.
|
|
|
|
func (t *UDPv5) callToNode(n *enode.Node, responseType byte, req v5wire.Packet) *callV5 {
|
2024-05-29 15:02:26 +02:00
|
|
|
addr, _ := n.UDPEndpoint()
|
2023-04-28 11:03:43 +02:00
|
|
|
c := &callV5{id: n.ID(), addr: addr, node: n}
|
|
|
|
t.initCall(c, responseType, req)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// callToID is like callToNode, but for cases where the node record is not available.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) callToID(id enode.ID, addr netip.AddrPort, responseType byte, req v5wire.Packet) *callV5 {
|
2023-04-28 11:03:43 +02:00
|
|
|
c := &callV5{id: id, addr: addr}
|
|
|
|
t.initCall(c, responseType, req)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv5) initCall(c *callV5, responseType byte, packet v5wire.Packet) {
|
|
|
|
c.packet = packet
|
|
|
|
c.responseType = responseType
|
|
|
|
c.reqid = make([]byte, 8)
|
|
|
|
c.ch = make(chan v5wire.Packet, 1)
|
|
|
|
c.err = make(chan error, 1)
|
2020-04-08 09:57:23 +02:00
|
|
|
// Assign request ID.
|
|
|
|
crand.Read(c.reqid)
|
2020-10-14 12:28:17 +02:00
|
|
|
packet.SetRequestID(c.reqid)
|
2020-04-08 09:57:23 +02:00
|
|
|
// Send call to dispatch.
|
|
|
|
select {
|
|
|
|
case t.callCh <- c:
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
c.err <- errClosed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// callDone tells dispatch that the active call is done.
|
|
|
|
func (t *UDPv5) callDone(c *callV5) {
|
2020-11-26 05:16:36 +08:00
|
|
|
// This needs a loop because further responses may be incoming until the
|
|
|
|
// send to callDoneCh has completed. Such responses need to be discarded
|
|
|
|
// in order to avoid blocking the dispatch loop.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.ch:
|
|
|
|
// late response, discard.
|
|
|
|
case <-c.err:
|
|
|
|
// late error, discard.
|
|
|
|
case t.callDoneCh <- c:
|
|
|
|
return
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatch runs in its own goroutine, handles incoming packets and deals with calls.
|
|
|
|
//
|
|
|
|
// For any destination node there is at most one 'active call', stored in the t.activeCall*
|
|
|
|
// maps. A call is made active when it is sent. The active call can be answered by a
|
|
|
|
// matching response, in which case c.ch receives the response; or by timing out, in which case
|
|
|
|
// c.err receives the error. When the function that created the call signals the active
|
|
|
|
// call is done through callDone, the next call from the call queue is started.
|
|
|
|
//
|
|
|
|
// Calls may also be answered by a WHOAREYOU packet referencing the call packet's authTag.
|
|
|
|
// When that happens the call is simply re-sent to complete the handshake. We allow one
|
|
|
|
// handshake attempt per call.
|
|
|
|
func (t *UDPv5) dispatch() {
|
|
|
|
defer t.wg.Done()
|
|
|
|
|
|
|
|
// Arm first read.
|
|
|
|
t.readNextCh <- struct{}{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-t.callCh:
|
2023-04-28 11:03:43 +02:00
|
|
|
t.callQueue[c.id] = append(t.callQueue[c.id], c)
|
|
|
|
t.sendNextCall(c.id)
|
2020-04-08 09:57:23 +02:00
|
|
|
|
|
|
|
case ct := <-t.respTimeoutCh:
|
2023-04-28 11:03:43 +02:00
|
|
|
active := t.activeCallByNode[ct.c.id]
|
2020-04-08 09:57:23 +02:00
|
|
|
if ct.c == active && ct.timer == active.timeout {
|
|
|
|
ct.c.err <- errTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
case c := <-t.callDoneCh:
|
2023-04-28 11:03:43 +02:00
|
|
|
active := t.activeCallByNode[c.id]
|
2020-04-08 09:57:23 +02:00
|
|
|
if active != c {
|
|
|
|
panic("BUG: callDone for inactive call")
|
|
|
|
}
|
|
|
|
c.timeout.Stop()
|
2020-10-14 12:28:17 +02:00
|
|
|
delete(t.activeCallByAuth, c.nonce)
|
2023-04-28 11:03:43 +02:00
|
|
|
delete(t.activeCallByNode, c.id)
|
|
|
|
t.sendNextCall(c.id)
|
|
|
|
|
|
|
|
case r := <-t.sendCh:
|
|
|
|
t.send(r.destID, r.destAddr, r.msg, nil)
|
2020-04-08 09:57:23 +02:00
|
|
|
|
|
|
|
case p := <-t.packetInCh:
|
|
|
|
t.handlePacket(p.Data, p.Addr)
|
|
|
|
// Arm next read.
|
|
|
|
t.readNextCh <- struct{}{}
|
|
|
|
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
close(t.readNextCh)
|
|
|
|
for id, queue := range t.callQueue {
|
|
|
|
for _, c := range queue {
|
|
|
|
c.err <- errClosed
|
|
|
|
}
|
|
|
|
delete(t.callQueue, id)
|
|
|
|
}
|
|
|
|
for id, c := range t.activeCallByNode {
|
|
|
|
c.err <- errClosed
|
|
|
|
delete(t.activeCallByNode, id)
|
2020-10-14 12:28:17 +02:00
|
|
|
delete(t.activeCallByAuth, c.nonce)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startResponseTimeout sets the response timer for a call.
|
|
|
|
func (t *UDPv5) startResponseTimeout(c *callV5) {
|
|
|
|
if c.timeout != nil {
|
|
|
|
c.timeout.Stop()
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
timer mclock.Timer
|
|
|
|
done = make(chan struct{})
|
|
|
|
)
|
|
|
|
timer = t.clock.AfterFunc(respTimeoutV5, func() {
|
|
|
|
<-done
|
|
|
|
select {
|
|
|
|
case t.respTimeoutCh <- &callTimeout{c, timer}:
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
}
|
|
|
|
})
|
|
|
|
c.timeout = timer
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendNextCall sends the next call in the call queue if there is no active call.
|
|
|
|
func (t *UDPv5) sendNextCall(id enode.ID) {
|
|
|
|
queue := t.callQueue[id]
|
|
|
|
if len(queue) == 0 || t.activeCallByNode[id] != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.activeCallByNode[id] = queue[0]
|
|
|
|
t.sendCall(t.activeCallByNode[id])
|
|
|
|
if len(queue) == 1 {
|
|
|
|
delete(t.callQueue, id)
|
|
|
|
} else {
|
|
|
|
copy(queue, queue[1:])
|
|
|
|
t.callQueue[id] = queue[:len(queue)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCall encodes and sends a request packet to the call's recipient node.
|
|
|
|
// This performs a handshake if needed.
|
|
|
|
func (t *UDPv5) sendCall(c *callV5) {
|
2020-10-14 12:28:17 +02:00
|
|
|
// The call might have a nonce from a previous handshake attempt. Remove the entry for
|
|
|
|
// the old nonce because we're about to generate a new nonce for this call.
|
|
|
|
if c.nonce != (v5wire.Nonce{}) {
|
|
|
|
delete(t.activeCallByAuth, c.nonce)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 11:03:43 +02:00
|
|
|
newNonce, _ := t.send(c.id, c.addr, c.packet, c.challenge)
|
2020-10-14 12:28:17 +02:00
|
|
|
c.nonce = newNonce
|
|
|
|
t.activeCallByAuth[newNonce] = c
|
2020-04-08 09:57:23 +02:00
|
|
|
t.startResponseTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendResponse sends a response packet to the given node.
|
|
|
|
// This doesn't trigger a handshake even if no keys are available.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) sendResponse(toID enode.ID, toAddr netip.AddrPort, packet v5wire.Packet) error {
|
2020-04-08 09:57:23 +02:00
|
|
|
_, err := t.send(toID, toAddr, packet, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) sendFromAnotherThread(toID enode.ID, toAddr netip.AddrPort, packet v5wire.Packet) {
|
2023-04-28 11:03:43 +02:00
|
|
|
select {
|
|
|
|
case t.sendCh <- sendRequest{toID, toAddr, packet}:
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:57:23 +02:00
|
|
|
// send sends a packet to the given node.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) send(toID enode.ID, toAddr netip.AddrPort, packet v5wire.Packet, c *v5wire.Whoareyou) (v5wire.Nonce, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
addr := toAddr.String()
|
2023-01-03 12:36:38 +01:00
|
|
|
t.logcontext = append(t.logcontext[:0], "id", toID, "addr", addr)
|
|
|
|
t.logcontext = packet.AppendLogInfo(t.logcontext)
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
enc, nonce, err := t.codec.Encode(toID, addr, packet, c)
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
2023-01-03 12:36:38 +01:00
|
|
|
t.logcontext = append(t.logcontext, "err", err)
|
|
|
|
t.log.Warn(">> "+packet.Name(), t.logcontext...)
|
2020-10-14 12:28:17 +02:00
|
|
|
return nonce, err
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2023-01-03 12:36:38 +01:00
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
_, err = t.conn.WriteToUDPAddrPort(enc, toAddr)
|
2023-01-03 12:36:38 +01:00
|
|
|
t.log.Trace(">> "+packet.Name(), t.logcontext...)
|
2020-10-14 12:28:17 +02:00
|
|
|
return nonce, err
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLoop runs in its own goroutine and reads packets from the network.
|
|
|
|
func (t *UDPv5) readLoop() {
|
|
|
|
defer t.wg.Done()
|
|
|
|
|
|
|
|
buf := make([]byte, maxPacketSize)
|
|
|
|
for range t.readNextCh {
|
2024-05-29 15:02:26 +02:00
|
|
|
nbytes, from, err := t.conn.ReadFromUDPAddrPort(buf)
|
2020-04-08 09:57:23 +02:00
|
|
|
if netutil.IsTemporaryError(err) {
|
|
|
|
// Ignore temporary read errors.
|
|
|
|
t.log.Debug("Temporary UDP read error", "err", err)
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
2022-08-19 01:00:21 -05:00
|
|
|
// Shut down the loop for permanent errors.
|
2022-06-07 17:27:21 +02:00
|
|
|
if !errors.Is(err, io.EOF) {
|
2020-04-08 09:57:23 +02:00
|
|
|
t.log.Debug("UDP read error", "err", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.dispatchReadPacket(from, buf[:nbytes])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatchReadPacket sends a packet into the dispatch loop.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) dispatchReadPacket(from netip.AddrPort, content []byte) bool {
|
2020-04-08 09:57:23 +02:00
|
|
|
select {
|
|
|
|
case t.packetInCh <- ReadPacket{content, from}:
|
|
|
|
return true
|
|
|
|
case <-t.closeCtx.Done():
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePacket decodes and processes an incoming packet from the network.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handlePacket(rawpacket []byte, fromAddr netip.AddrPort) error {
|
2020-04-08 09:57:23 +02:00
|
|
|
addr := fromAddr.String()
|
2020-10-14 12:28:17 +02:00
|
|
|
fromID, fromNode, packet, err := t.codec.Decode(rawpacket, addr)
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
2023-03-14 07:40:40 -04:00
|
|
|
if t.unhandled != nil && v5wire.IsInvalidHeader(err) {
|
|
|
|
// The packet seems unrelated to discv5, send it to the next protocol.
|
|
|
|
// t.log.Trace("Unhandled discv5 packet", "id", fromID, "addr", addr, "err", err)
|
|
|
|
up := ReadPacket{Data: make([]byte, len(rawpacket)), Addr: fromAddr}
|
|
|
|
copy(up.Data, rawpacket)
|
|
|
|
t.unhandled <- up
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
t.log.Debug("Bad discv5 packet", "id", fromID, "addr", addr, "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fromNode != nil {
|
|
|
|
// Handshake succeeded, add to table.
|
2024-05-29 15:02:26 +02:00
|
|
|
t.tab.addInboundNode(fromNode)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
if packet.Kind() != v5wire.WhoareyouPacket {
|
|
|
|
// WHOAREYOU logged separately to report errors.
|
2023-01-03 12:36:38 +01:00
|
|
|
t.logcontext = append(t.logcontext[:0], "id", fromID, "addr", addr)
|
|
|
|
t.logcontext = packet.AppendLogInfo(t.logcontext)
|
|
|
|
t.log.Trace("<< "+packet.Name(), t.logcontext...)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
t.handle(packet, fromID, fromAddr)
|
2020-04-08 09:57:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCallResponse dispatches a response packet to the call waiting for it.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handleCallResponse(fromID enode.ID, fromAddr netip.AddrPort, p v5wire.Packet) bool {
|
2020-04-08 09:57:23 +02:00
|
|
|
ac := t.activeCallByNode[fromID]
|
2020-10-14 12:28:17 +02:00
|
|
|
if ac == nil || !bytes.Equal(p.RequestID(), ac.reqid) {
|
|
|
|
t.log.Debug(fmt.Sprintf("Unsolicited/late %s response", p.Name()), "id", fromID, "addr", fromAddr)
|
|
|
|
return false
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if fromAddr != ac.addr {
|
2020-10-14 12:28:17 +02:00
|
|
|
t.log.Debug(fmt.Sprintf("%s from wrong endpoint", p.Name()), "id", fromID, "addr", fromAddr)
|
|
|
|
return false
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
if p.Kind() != ac.responseType {
|
|
|
|
t.log.Debug(fmt.Sprintf("Wrong discv5 response type %s", p.Name()), "id", fromID, "addr", fromAddr)
|
|
|
|
return false
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
t.startResponseTimeout(ac)
|
|
|
|
ac.ch <- p
|
2020-10-14 12:28:17 +02:00
|
|
|
return true
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNode looks for a node record in table and database.
|
|
|
|
func (t *UDPv5) getNode(id enode.ID) *enode.Node {
|
|
|
|
if n := t.tab.getNode(id); n != nil {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
if n := t.localNode.Database().Node(id); n != nil {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// handle processes incoming packets according to their message type.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handle(p v5wire.Packet, fromID enode.ID, fromAddr netip.AddrPort) {
|
2020-10-14 12:28:17 +02:00
|
|
|
switch p := p.(type) {
|
|
|
|
case *v5wire.Unknown:
|
|
|
|
t.handleUnknown(p, fromID, fromAddr)
|
|
|
|
case *v5wire.Whoareyou:
|
|
|
|
t.handleWhoareyou(p, fromID, fromAddr)
|
|
|
|
case *v5wire.Ping:
|
|
|
|
t.handlePing(p, fromID, fromAddr)
|
|
|
|
case *v5wire.Pong:
|
|
|
|
if t.handleCallResponse(fromID, fromAddr, p) {
|
2024-05-29 15:02:26 +02:00
|
|
|
fromUDPAddr := &net.UDPAddr{IP: fromAddr.Addr().AsSlice(), Port: int(fromAddr.Port())}
|
|
|
|
toUDPAddr := &net.UDPAddr{IP: p.ToIP, Port: int(p.ToPort)}
|
|
|
|
t.localNode.UDPEndpointStatement(fromUDPAddr, toUDPAddr)
|
2020-10-14 12:28:17 +02:00
|
|
|
}
|
|
|
|
case *v5wire.Findnode:
|
|
|
|
t.handleFindnode(p, fromID, fromAddr)
|
|
|
|
case *v5wire.Nodes:
|
|
|
|
t.handleCallResponse(fromID, fromAddr, p)
|
|
|
|
case *v5wire.TalkRequest:
|
2023-04-28 11:03:43 +02:00
|
|
|
t.talk.handleRequest(fromID, fromAddr, p)
|
2020-10-14 12:28:17 +02:00
|
|
|
case *v5wire.TalkResponse:
|
|
|
|
t.handleCallResponse(fromID, fromAddr, p)
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// handleUnknown initiates a handshake by responding with WHOAREYOU.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handleUnknown(p *v5wire.Unknown, fromID enode.ID, fromAddr netip.AddrPort) {
|
2020-10-14 12:28:17 +02:00
|
|
|
challenge := &v5wire.Whoareyou{Nonce: p.Nonce}
|
2020-04-08 09:57:23 +02:00
|
|
|
crand.Read(challenge.IDNonce[:])
|
|
|
|
if n := t.getNode(fromID); n != nil {
|
2020-10-14 12:28:17 +02:00
|
|
|
challenge.Node = n
|
2020-04-08 09:57:23 +02:00
|
|
|
challenge.RecordSeq = n.Seq()
|
|
|
|
}
|
|
|
|
t.sendResponse(fromID, fromAddr, challenge)
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
var (
|
|
|
|
errChallengeNoCall = errors.New("no matching call")
|
|
|
|
errChallengeTwice = errors.New("second handshake")
|
|
|
|
)
|
2020-04-08 09:57:23 +02:00
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// handleWhoareyou resends the active call as a handshake packet.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handleWhoareyou(p *v5wire.Whoareyou, fromID enode.ID, fromAddr netip.AddrPort) {
|
2020-10-14 12:28:17 +02:00
|
|
|
c, err := t.matchWithCall(fromID, p.Nonce)
|
2020-04-08 09:57:23 +02:00
|
|
|
if err != nil {
|
2020-10-14 12:28:17 +02:00
|
|
|
t.log.Debug("Invalid "+p.Name(), "addr", fromAddr, "err", err)
|
2020-04-08 09:57:23 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
|
2023-04-28 11:03:43 +02:00
|
|
|
if c.node == nil {
|
|
|
|
// Can't perform handshake because we don't have the ENR.
|
|
|
|
t.log.Debug("Can't handle "+p.Name(), "addr", fromAddr, "err", "call has no ENR")
|
|
|
|
c.err <- errors.New("remote wants handshake, but call has no ENR")
|
|
|
|
return
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
// Resend the call that was answered by WHOAREYOU.
|
2020-10-14 12:28:17 +02:00
|
|
|
t.log.Trace("<< "+p.Name(), "id", c.node.ID(), "addr", fromAddr)
|
2020-04-08 09:57:23 +02:00
|
|
|
c.handshakeCount++
|
|
|
|
c.challenge = p
|
2020-10-14 12:28:17 +02:00
|
|
|
p.Node = c.node
|
2020-04-08 09:57:23 +02:00
|
|
|
t.sendCall(c)
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// matchWithCall checks whether a handshake attempt matches the active call.
|
|
|
|
func (t *UDPv5) matchWithCall(fromID enode.ID, nonce v5wire.Nonce) (*callV5, error) {
|
|
|
|
c := t.activeCallByAuth[nonce]
|
2020-04-08 09:57:23 +02:00
|
|
|
if c == nil {
|
|
|
|
return nil, errChallengeNoCall
|
|
|
|
}
|
|
|
|
if c.handshakeCount > 0 {
|
|
|
|
return nil, errChallengeTwice
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// handlePing sends a PONG response.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handlePing(p *v5wire.Ping, fromID enode.ID, fromAddr netip.AddrPort) {
|
|
|
|
var remoteIP net.IP
|
|
|
|
// Handle IPv4 mapped IPv6 addresses in the event the local node is binded
|
|
|
|
// to an ipv6 interface.
|
|
|
|
if fromAddr.Addr().Is4() || fromAddr.Addr().Is4In6() {
|
|
|
|
ip4 := fromAddr.Addr().As4()
|
|
|
|
remoteIP = ip4[:]
|
|
|
|
} else {
|
|
|
|
remoteIP = fromAddr.Addr().AsSlice()
|
2021-04-24 00:18:10 +08:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
t.sendResponse(fromID, fromAddr, &v5wire.Pong{
|
2020-04-08 09:57:23 +02:00
|
|
|
ReqID: p.ReqID,
|
2021-04-24 00:18:10 +08:00
|
|
|
ToIP: remoteIP,
|
2024-05-29 15:02:26 +02:00
|
|
|
ToPort: fromAddr.Port(),
|
2020-04-08 09:57:23 +02:00
|
|
|
ENRSeq: t.localNode.Node().Seq(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// handleFindnode returns nodes to the requester.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) handleFindnode(p *v5wire.Findnode, fromID enode.ID, fromAddr netip.AddrPort) {
|
|
|
|
nodes := t.collectTableNodes(fromAddr.Addr(), p.Distances, findnodeResultLimit)
|
2020-10-14 12:28:17 +02:00
|
|
|
for _, resp := range packNodes(p.ReqID, nodes) {
|
|
|
|
t.sendResponse(fromID, fromAddr, resp)
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// collectTableNodes creates a FINDNODE result set for the given distances.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv5) collectTableNodes(rip netip.Addr, distances []uint, limit int) []*enode.Node {
|
|
|
|
ripSlice := rip.AsSlice()
|
2023-12-18 10:47:21 +01:00
|
|
|
var bn []*enode.Node
|
2020-10-14 12:28:17 +02:00
|
|
|
var nodes []*enode.Node
|
|
|
|
var processed = make(map[uint]struct{})
|
|
|
|
for _, dist := range distances {
|
|
|
|
// Reject duplicate / invalid distances.
|
|
|
|
_, seen := processed[dist]
|
|
|
|
if seen || dist > 256 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
processed[dist] = struct{}{}
|
2020-04-08 09:57:23 +02:00
|
|
|
|
2023-12-18 10:47:21 +01:00
|
|
|
for _, n := range t.tab.appendLiveNodes(dist, bn[:0]) {
|
|
|
|
// Apply some pre-checks to avoid sending invalid nodes.
|
|
|
|
// Note liveness is checked by appendLiveNodes.
|
2024-05-29 15:02:26 +02:00
|
|
|
if netutil.CheckRelayIP(ripSlice, n.IP()) != nil {
|
2020-10-14 12:28:17 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
if len(nodes) >= limit {
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
return nodes
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
// packNodes creates NODES response packets for the given node list.
|
|
|
|
func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
|
|
|
|
if len(nodes) == 0 {
|
2023-01-03 12:36:38 +01:00
|
|
|
return []*v5wire.Nodes{{ReqID: reqid, RespCount: 1}}
|
2020-10-14 12:28:17 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 19:19:02 +01:00
|
|
|
// This limit represents the available space for nodes in output packets. Maximum
|
|
|
|
// packet size is 1280, and out of this ~80 bytes will be taken up by the packet
|
|
|
|
// frame. So limiting to 1000 bytes here leaves 200 bytes for other fields of the
|
|
|
|
// NODES message, which is a lot.
|
|
|
|
const sizeLimit = 1000
|
|
|
|
|
2020-10-14 12:28:17 +02:00
|
|
|
var resp []*v5wire.Nodes
|
2020-04-08 09:57:23 +02:00
|
|
|
for len(nodes) > 0 {
|
2022-11-07 19:19:02 +01:00
|
|
|
p := &v5wire.Nodes{ReqID: reqid}
|
|
|
|
size := uint64(0)
|
|
|
|
for len(nodes) > 0 {
|
|
|
|
r := nodes[0].Record()
|
|
|
|
if size += r.Size(); size > sizeLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.Nodes = append(p.Nodes, r)
|
|
|
|
nodes = nodes[1:]
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
resp = append(resp, p)
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|
2022-11-07 19:19:02 +01:00
|
|
|
for _, msg := range resp {
|
2023-01-03 12:36:38 +01:00
|
|
|
msg.RespCount = uint8(len(resp))
|
2022-11-07 19:19:02 +01:00
|
|
|
}
|
2020-10-14 12:28:17 +02:00
|
|
|
return resp
|
2020-04-08 09:57:23 +02:00
|
|
|
}
|