2018-01-29 19:44:18 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// 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"
|
|
|
|
"container/list"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-11-21 15:30:00 +00:00
|
|
|
"sync"
|
2018-01-29 19:44:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-11-21 15:30:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-01-29 19:44:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
var (
|
|
|
|
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")
|
|
|
|
errClockWarp = errors.New("reply deadline too far in the future")
|
|
|
|
errClosed = errors.New("socket closed")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Timeouts
|
|
|
|
const (
|
2018-11-21 15:30:00 +00:00
|
|
|
respTimeout = 500 * time.Millisecond
|
|
|
|
expiration = 20 * time.Second
|
|
|
|
bondExpiration = 24 * time.Hour
|
2018-01-29 19:44:18 +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
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC packet types
|
|
|
|
const (
|
|
|
|
pingPacket = iota + 1 // zero is 'reserved'
|
|
|
|
pongPacket
|
|
|
|
findnodePacket
|
|
|
|
neighborsPacket
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC request structures
|
|
|
|
type (
|
|
|
|
ping struct {
|
|
|
|
Version uint
|
|
|
|
From, To rpcEndpoint
|
|
|
|
Expiration uint64
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// pong is the reply to ping.
|
|
|
|
pong struct {
|
|
|
|
// 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.
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// findnode is a query for nodes close to the given target.
|
|
|
|
findnode struct {
|
2018-11-21 15:30:00 +00:00
|
|
|
Target encPubkey
|
2018-01-29 19:44:18 +00:00
|
|
|
Expiration uint64
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply to findnode
|
|
|
|
neighbors struct {
|
|
|
|
Nodes []rpcNode
|
|
|
|
Expiration uint64
|
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcNode struct {
|
|
|
|
IP net.IP // len 4 for IPv4 or 16 for IPv6
|
|
|
|
UDP uint16 // for discovery protocol
|
|
|
|
TCP uint16 // for RLPx protocol
|
2018-11-21 15:30:00 +00:00
|
|
|
ID encPubkey
|
2018-01-29 19:44:18 +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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
|
2018-11-21 15:30:00 +00:00
|
|
|
ip := net.IP{}
|
|
|
|
if ip4 := addr.IP.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
} else if ip6 := addr.IP.To16(); ip6 != nil {
|
|
|
|
ip = ip6
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*node, error) {
|
2018-01-29 19:44:18 +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
|
|
|
|
}
|
|
|
|
if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) {
|
|
|
|
return nil, errors.New("not contained in netrestrict whitelist")
|
|
|
|
}
|
2018-11-21 15:30: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()
|
2018-01-29 19:44:18 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30: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())}
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type packet interface {
|
2018-11-21 15:30:00 +00:00
|
|
|
handle(t *udp, from *net.UDPAddr, fromKey encPubkey, mac []byte) error
|
2018-01-29 19:44:18 +00:00
|
|
|
name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type conn interface {
|
|
|
|
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
|
|
|
|
WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
|
|
|
|
Close() error
|
|
|
|
LocalAddr() net.Addr
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
// udp implements the discovery v4 UDP wire protocol.
|
2018-01-29 19:44:18 +00:00
|
|
|
type udp struct {
|
|
|
|
conn conn
|
|
|
|
netrestrict *netutil.Netlist
|
|
|
|
priv *ecdsa.PrivateKey
|
2018-11-21 15:30:00 +00:00
|
|
|
localNode *enode.LocalNode
|
|
|
|
db *enode.DB
|
|
|
|
tab *Table
|
|
|
|
wg sync.WaitGroup
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
addpending chan *pending
|
|
|
|
gotreply chan reply
|
2018-11-21 15:30:00 +00:00
|
|
|
closing chan struct{}
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pending represents a pending reply.
|
|
|
|
//
|
|
|
|
// some implementations of the protocol wish to send more than one
|
|
|
|
// reply packet to findnode. in general, any neighbors packet cannot
|
|
|
|
// be matched up with a specific findnode packet.
|
|
|
|
//
|
|
|
|
// our implementation handles this by storing a callback function for
|
|
|
|
// each pending reply. incoming packets from a node are dispatched
|
|
|
|
// to all the callback functions for that node.
|
|
|
|
type pending struct {
|
|
|
|
// these fields must match in the reply.
|
2018-11-21 15:30:00 +00:00
|
|
|
from enode.ID
|
2018-01-29 19:44:18 +00:00
|
|
|
ptype byte
|
|
|
|
|
|
|
|
// time when the request must complete
|
|
|
|
deadline time.Time
|
|
|
|
|
|
|
|
// callback is called when a matching reply arrives. if it returns
|
|
|
|
// true, the callback is 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 func(resp interface{}) (done bool)
|
|
|
|
|
|
|
|
// errc receives nil when the callback indicates completion or an
|
|
|
|
// error if no further reply is received within the timeout.
|
|
|
|
errc chan<- error
|
|
|
|
}
|
|
|
|
|
|
|
|
type reply struct {
|
2018-11-21 15:30:00 +00:00
|
|
|
from enode.ID
|
2018-01-29 19:44:18 +00:00
|
|
|
ptype byte
|
|
|
|
data interface{}
|
|
|
|
// loop indicates whether there was
|
|
|
|
// a matching request by sending on this channel.
|
|
|
|
matched chan<- bool
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:29:21 +00:00
|
|
|
// ReadPacket is sent to the unhandled channel when it could not be processed
|
|
|
|
type ReadPacket struct {
|
|
|
|
Data []byte
|
|
|
|
Addr *net.UDPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds Table-related settings.
|
|
|
|
type Config struct {
|
|
|
|
// These settings are required and configure the UDP listener:
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
// These settings are optional:
|
2018-11-21 15:30:00 +00:00
|
|
|
NetRestrict *netutil.Netlist // network whitelist
|
|
|
|
Bootnodes []*enode.Node // list of bootstrap nodes
|
|
|
|
Unhandled chan<- ReadPacket // unhandled packets are sent on this channel
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
// ListenUDP returns a new table that listens for UDP packets on laddr.
|
2018-11-21 15:30:00 +00:00
|
|
|
func ListenUDP(c conn, ln *enode.LocalNode, cfg Config) (*Table, error) {
|
|
|
|
tab, _, err := newUDP(c, ln, cfg)
|
2018-01-29 19:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tab, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func newUDP(c conn, ln *enode.LocalNode, cfg Config) (*Table, *udp, error) {
|
2018-01-29 19:44:18 +00:00
|
|
|
udp := &udp{
|
|
|
|
conn: c,
|
2018-03-07 21:29:21 +00:00
|
|
|
priv: cfg.PrivateKey,
|
|
|
|
netrestrict: cfg.NetRestrict,
|
2018-11-21 15:30:00 +00:00
|
|
|
localNode: ln,
|
|
|
|
db: ln.Database(),
|
2018-01-29 19:44:18 +00:00
|
|
|
closing: make(chan struct{}),
|
|
|
|
gotreply: make(chan reply),
|
|
|
|
addpending: make(chan *pending),
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
tab, err := newTable(udp, ln.Database(), cfg.Bootnodes)
|
2018-01-29 19:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
udp.tab = tab
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
udp.wg.Add(2)
|
2018-01-29 19:44:18 +00:00
|
|
|
go udp.loop()
|
2018-03-07 21:29:21 +00:00
|
|
|
go udp.readLoop(cfg.Unhandled)
|
2018-11-21 15:30:00 +00:00
|
|
|
return udp.tab, udp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) self() *enode.Node {
|
|
|
|
return t.localNode.Node()
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) close() {
|
|
|
|
close(t.closing)
|
|
|
|
t.conn.Close()
|
2018-11-21 15:30:00 +00:00
|
|
|
t.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) ourEndpoint() rpcEndpoint {
|
|
|
|
n := t.self()
|
|
|
|
a := &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
|
|
|
return makeEndpoint(a, uint16(n.TCP()))
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ping sends a ping message to the given node and waits for a reply.
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) ping(toid enode.ID, toaddr *net.UDPAddr) error {
|
2018-09-05 15:36:14 +00:00
|
|
|
return <-t.sendPing(toid, toaddr, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendPing sends a ping message to the given node and invokes the callback
|
|
|
|
// when the reply arrives.
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) sendPing(toid enode.ID, toaddr *net.UDPAddr, callback func()) <-chan error {
|
2018-03-07 21:29:21 +00:00
|
|
|
req := &ping{
|
2018-09-05 15:36:14 +00:00
|
|
|
Version: 4,
|
2018-11-21 15:30:00 +00:00
|
|
|
From: t.ourEndpoint(),
|
2018-01-29 19:44:18 +00:00
|
|
|
To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
packet, hash, err := encodePacket(t.priv, pingPacket, req)
|
|
|
|
if err != nil {
|
2018-09-05 15:36:14 +00:00
|
|
|
errc := make(chan error, 1)
|
|
|
|
errc <- err
|
|
|
|
return errc
|
2018-03-07 21:29:21 +00:00
|
|
|
}
|
|
|
|
errc := t.pending(toid, pongPacket, func(p interface{}) bool {
|
2018-09-05 15:36:14 +00:00
|
|
|
ok := bytes.Equal(p.(*pong).ReplyTok, hash)
|
|
|
|
if ok && callback != nil {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
return ok
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
2018-11-21 15:30:00 +00:00
|
|
|
t.localNode.UDPContact(toaddr)
|
2018-03-07 21:29:21 +00:00
|
|
|
t.write(toaddr, req.name(), packet)
|
2018-09-05 15:36:14 +00:00
|
|
|
return errc
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) waitping(from enode.ID) error {
|
2018-01-29 19:44:18 +00:00
|
|
|
return <-t.pending(from, pingPacket, func(interface{}) bool { return true })
|
|
|
|
}
|
|
|
|
|
|
|
|
// findnode sends a findnode request to the given node and waits until
|
|
|
|
// the node has sent up to k neighbors.
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) findnode(toid enode.ID, toaddr *net.UDPAddr, target encPubkey) ([]*node, error) {
|
2018-09-05 15:36:14 +00:00
|
|
|
// If we haven't seen a ping from the destination node for a while, it won't remember
|
|
|
|
// our endpoint proof and reject findnode. Solicit a ping first.
|
2018-11-21 15:30:00 +00:00
|
|
|
if time.Since(t.db.LastPingReceived(toid)) > bondExpiration {
|
2018-09-05 15:36:14 +00:00
|
|
|
t.ping(toid, toaddr)
|
|
|
|
t.waitping(toid)
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
nodes := make([]*node, 0, bucketSize)
|
2018-01-29 19:44:18 +00:00
|
|
|
nreceived := 0
|
|
|
|
errc := t.pending(toid, neighborsPacket, func(r interface{}) bool {
|
|
|
|
reply := r.(*neighbors)
|
|
|
|
for _, rn := range reply.Nodes {
|
|
|
|
nreceived++
|
|
|
|
n, err := t.nodeFromRPC(toaddr, rn)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
return nreceived >= bucketSize
|
|
|
|
})
|
|
|
|
t.send(toaddr, findnodePacket, &findnode{
|
|
|
|
Target: target,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
return nodes, <-errc
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pending adds a reply callback to the pending reply queue.
|
|
|
|
// see the documentation of type pending for a detailed explanation.
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) pending(id enode.ID, ptype byte, callback func(interface{}) bool) <-chan error {
|
2018-01-29 19:44:18 +00:00
|
|
|
ch := make(chan error, 1)
|
|
|
|
p := &pending{from: id, ptype: ptype, callback: callback, errc: ch}
|
|
|
|
select {
|
|
|
|
case t.addpending <- p:
|
|
|
|
// loop will handle it
|
|
|
|
case <-t.closing:
|
|
|
|
ch <- errClosed
|
|
|
|
}
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (t *udp) handleReply(from enode.ID, ptype byte, req packet) bool {
|
2018-01-29 19:44:18 +00:00
|
|
|
matched := make(chan bool, 1)
|
|
|
|
select {
|
|
|
|
case t.gotreply <- reply{from, ptype, req, matched}:
|
|
|
|
// loop will handle it
|
|
|
|
return <-matched
|
|
|
|
case <-t.closing:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop runs in its own goroutine. it keeps track of
|
|
|
|
// the refresh timer and the pending reply queue.
|
|
|
|
func (t *udp) loop() {
|
2018-11-21 15:30:00 +00:00
|
|
|
defer t.wg.Done()
|
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
var (
|
|
|
|
plist = list.New()
|
|
|
|
timeout = time.NewTimer(0)
|
|
|
|
nextTimeout *pending // head of plist when timeout was last reset
|
|
|
|
contTimeouts = 0 // number of continuous timeouts to do NTP checks
|
|
|
|
ntpWarnTime = time.Unix(0, 0)
|
|
|
|
)
|
|
|
|
<-timeout.C // ignore first timeout
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
resetTimeout := func() {
|
|
|
|
if plist.Front() == nil || nextTimeout == plist.Front().Value {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 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() {
|
|
|
|
nextTimeout = el.Value.(*pending)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
resetTimeout()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.closing:
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
|
|
|
el.Value.(*pending).errc <- errClosed
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
case p := <-t.addpending:
|
|
|
|
p.deadline = time.Now().Add(respTimeout)
|
|
|
|
plist.PushBack(p)
|
|
|
|
|
|
|
|
case r := <-t.gotreply:
|
|
|
|
var matched bool
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
|
|
|
p := el.Value.(*pending)
|
|
|
|
if p.from == r.from && p.ptype == r.ptype {
|
|
|
|
matched = true
|
|
|
|
// Remove the matcher if its callback indicates
|
|
|
|
// that all replies have been received. This is
|
|
|
|
// required for packet types that expect multiple
|
|
|
|
// reply packets.
|
|
|
|
if p.callback(r.data) {
|
|
|
|
p.errc <- nil
|
|
|
|
plist.Remove(el)
|
|
|
|
}
|
|
|
|
// Reset the continuous timeout counter (time drift detection)
|
|
|
|
contTimeouts = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.matched <- matched
|
|
|
|
|
|
|
|
case now := <-timeout.C:
|
|
|
|
nextTimeout = nil
|
|
|
|
|
|
|
|
// Notify and remove callbacks whose deadline is in the past.
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
|
|
|
p := el.Value.(*pending)
|
|
|
|
if now.After(p.deadline) || now.Equal(p.deadline) {
|
|
|
|
p.errc <- errTimeout
|
|
|
|
plist.Remove(el)
|
|
|
|
contTimeouts++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we've accumulated too many timeouts, do an NTP time sync check
|
|
|
|
if contTimeouts > ntpFailureThreshold {
|
|
|
|
if time.Since(ntpWarnTime) >= ntpWarningCooldown {
|
|
|
|
ntpWarnTime = time.Now()
|
|
|
|
go checkClockDrift()
|
|
|
|
}
|
|
|
|
contTimeouts = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
macSize = 256 / 8
|
|
|
|
sigSize = 520 / 8
|
|
|
|
headSize = macSize + sigSize // space of packet frame data
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
headSpace = make([]byte, headSize)
|
|
|
|
|
|
|
|
// Neighbors replies are sent across multiple packets to
|
|
|
|
// stay below the 1280 byte limit. We compute the maximum number
|
|
|
|
// of entries by stuffing a packet until it grows too large.
|
|
|
|
maxNeighbors int
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
p := neighbors{Expiration: ^uint64(0)}
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
if headSize+size+1 >= 1280 {
|
|
|
|
maxNeighbors = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:29:21 +00:00
|
|
|
func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req packet) ([]byte, error) {
|
|
|
|
packet, hash, err := encodePacket(t.priv, ptype, req)
|
2018-01-29 19:44:18 +00:00
|
|
|
if err != nil {
|
2018-03-07 21:29:21 +00:00
|
|
|
return hash, err
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
return hash, t.write(toaddr, req.name(), packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) write(toaddr *net.UDPAddr, what string, packet []byte) error {
|
|
|
|
_, err := t.conn.WriteToUDP(packet, toaddr)
|
|
|
|
log.Trace(">> "+what, "addr", toaddr, "err", err)
|
2018-01-29 19:44:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:29:21 +00:00
|
|
|
func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) (packet, hash []byte, err error) {
|
2018-01-29 19:44:18 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.Write(headSpace)
|
|
|
|
b.WriteByte(ptype)
|
|
|
|
if err := rlp.Encode(b, req); err != nil {
|
|
|
|
log.Error("Can't encode discv4 packet", "err", err)
|
2018-03-07 21:29:21 +00:00
|
|
|
return nil, nil, err
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
packet = b.Bytes()
|
2018-01-29 19:44:18 +00:00
|
|
|
sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't sign discv4 packet", "err", err)
|
2018-03-07 21:29:21 +00:00
|
|
|
return nil, nil, err
|
2018-01-29 19:44:18 +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
|
|
|
|
// The future.
|
2018-03-07 21:29:21 +00:00
|
|
|
hash = crypto.Keccak256(packet[macSize:])
|
|
|
|
copy(packet, hash)
|
|
|
|
return packet, hash, nil
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
2018-03-07 21:29:21 +00:00
|
|
|
func (t *udp) readLoop(unhandled chan<- ReadPacket) {
|
2018-11-21 15:30:00 +00:00
|
|
|
defer t.wg.Done()
|
2018-03-07 21:29:21 +00:00
|
|
|
if unhandled != nil {
|
|
|
|
defer close(unhandled)
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
|
2018-01-29 19:44:18 +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.
|
|
|
|
buf := make([]byte, 1280)
|
|
|
|
for {
|
|
|
|
nbytes, from, err := t.conn.ReadFromUDP(buf)
|
|
|
|
if netutil.IsTemporaryError(err) {
|
|
|
|
// Ignore temporary read errors.
|
|
|
|
log.Debug("Temporary UDP read error", "err", err)
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
// Shut down the loop for permament errors.
|
|
|
|
log.Debug("UDP read error", "err", err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
if t.handlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
|
|
|
|
select {
|
|
|
|
case unhandled <- ReadPacket{buf[:nbytes], from}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error {
|
|
|
|
packet, fromID, hash, err := decodePacket(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Bad discv4 packet", "addr", from, "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = packet.handle(t, from, fromID, hash)
|
|
|
|
log.Trace("<< "+packet.name(), "addr", from, "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func decodePacket(buf []byte) (packet, encPubkey, []byte, error) {
|
2018-01-29 19:44:18 +00:00
|
|
|
if len(buf) < headSize+1 {
|
2018-11-21 15:30:00 +00:00
|
|
|
return nil, encPubkey{}, nil, errPacketTooSmall
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
|
|
|
|
shouldhash := crypto.Keccak256(buf[macSize:])
|
|
|
|
if !bytes.Equal(hash, shouldhash) {
|
2018-11-21 15:30:00 +00:00
|
|
|
return nil, encPubkey{}, nil, errBadHash
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
fromKey, err := recoverNodeKey(crypto.Keccak256(buf[headSize:]), sig)
|
2018-01-29 19:44:18 +00:00
|
|
|
if err != nil {
|
2018-11-21 15:30:00 +00:00
|
|
|
return nil, fromKey, hash, err
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
var req packet
|
|
|
|
switch ptype := sigdata[0]; ptype {
|
|
|
|
case pingPacket:
|
|
|
|
req = new(ping)
|
|
|
|
case pongPacket:
|
|
|
|
req = new(pong)
|
|
|
|
case findnodePacket:
|
|
|
|
req = new(findnode)
|
|
|
|
case neighborsPacket:
|
|
|
|
req = new(neighbors)
|
|
|
|
default:
|
2018-11-21 15:30:00 +00:00
|
|
|
return nil, fromKey, hash, fmt.Errorf("unknown type: %d", ptype)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
|
|
|
|
err = s.Decode(req)
|
2018-11-21 15:30:00 +00:00
|
|
|
return req, fromKey, hash, err
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (req *ping) handle(t *udp, from *net.UDPAddr, fromKey encPubkey, mac []byte) error {
|
2018-01-29 19:44:18 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
key, err := decodePubkey(fromKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid public key: %v", err)
|
|
|
|
}
|
2018-01-29 19:44:18 +00:00
|
|
|
t.send(from, pongPacket, &pong{
|
|
|
|
To: makeEndpoint(from, req.From.TCP),
|
|
|
|
ReplyTok: mac,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2018-11-21 15:30:00 +00:00
|
|
|
n := wrapNode(enode.NewV4(key, from.IP, int(req.From.TCP), from.Port))
|
|
|
|
t.handleReply(n.ID(), pingPacket, req)
|
|
|
|
if time.Since(t.db.LastPongReceived(n.ID())) > bondExpiration {
|
|
|
|
t.sendPing(n.ID(), from, func() { t.tab.addThroughPing(n) })
|
2018-09-05 15:36:14 +00:00
|
|
|
} else {
|
2018-11-21 15:30:00 +00:00
|
|
|
t.tab.addThroughPing(n)
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
t.localNode.UDPEndpointStatement(from, &net.UDPAddr{IP: req.To.IP, Port: int(req.To.UDP)})
|
|
|
|
t.db.UpdateLastPingReceived(n.ID(), time.Now())
|
2018-01-29 19:44:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *ping) name() string { return "PING/v4" }
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (req *pong) handle(t *udp, from *net.UDPAddr, fromKey encPubkey, mac []byte) error {
|
2018-01-29 19:44:18 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
fromID := fromKey.id()
|
2018-01-29 19:44:18 +00:00
|
|
|
if !t.handleReply(fromID, pongPacket, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
t.localNode.UDPEndpointStatement(from, &net.UDPAddr{IP: req.To.IP, Port: int(req.To.UDP)})
|
|
|
|
t.db.UpdateLastPongReceived(fromID, time.Now())
|
2018-01-29 19:44:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *pong) name() string { return "PONG/v4" }
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (req *findnode) handle(t *udp, from *net.UDPAddr, fromKey encPubkey, mac []byte) error {
|
2018-01-29 19:44:18 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
fromID := fromKey.id()
|
|
|
|
if time.Since(t.db.LastPongReceived(fromID)) > bondExpiration {
|
2018-09-05 15:36:14 +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.
|
2018-01-29 19:44:18 +00:00
|
|
|
return errUnknownNode
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
target := enode.ID(crypto.Keccak256Hash(req.Target[:]))
|
|
|
|
t.tab.mutex.Lock()
|
|
|
|
closest := t.tab.closest(target, bucketSize).entries
|
|
|
|
t.tab.mutex.Unlock()
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
|
2018-03-07 21:29:21 +00:00
|
|
|
var sent bool
|
2018-01-29 19:44:18 +00:00
|
|
|
// Send neighbors in chunks with at most maxNeighbors per packet
|
|
|
|
// to stay below the 1280 byte limit.
|
2018-03-07 21:29:21 +00:00
|
|
|
for _, n := range closest {
|
2018-11-21 15:30:00 +00:00
|
|
|
if netutil.CheckRelayIP(from.IP, n.IP()) == nil {
|
2018-03-07 21:29:21 +00:00
|
|
|
p.Nodes = append(p.Nodes, nodeToRPC(n))
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
if len(p.Nodes) == maxNeighbors {
|
2018-01-29 19:44:18 +00:00
|
|
|
t.send(from, neighborsPacket, &p)
|
|
|
|
p.Nodes = p.Nodes[:0]
|
2018-03-07 21:29:21 +00:00
|
|
|
sent = true
|
2018-01-29 19:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 21:29:21 +00:00
|
|
|
if len(p.Nodes) > 0 || !sent {
|
|
|
|
t.send(from, neighborsPacket, &p)
|
|
|
|
}
|
2018-01-29 19:44:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *findnode) name() string { return "FINDNODE/v4" }
|
|
|
|
|
2018-11-21 15:30:00 +00:00
|
|
|
func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromKey encPubkey, mac []byte) error {
|
2018-01-29 19:44:18 +00:00
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2018-11-21 15:30:00 +00:00
|
|
|
if !t.handleReply(fromKey.id(), neighborsPacket, req) {
|
2018-01-29 19:44:18 +00:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *neighbors) name() string { return "NEIGHBORS/v4" }
|
|
|
|
|
|
|
|
func expired(ts uint64) bool {
|
|
|
|
return time.Unix(int64(ts), 0).Before(time.Now())
|
|
|
|
}
|