2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-07 19:49:28 -05:00
|
|
|
"container/list"
|
2015-01-27 07:33:26 -06:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2015-02-13 04:38:34 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2016-11-21 11:11:54 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2015-01-27 07:33:26 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2015-04-27 06:42:30 -05:00
|
|
|
const Version = 4
|
2015-03-24 09:36:11 -05:00
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
// Errors
|
|
|
|
var (
|
2015-03-25 10:45:53 -05: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-07 19:49:28 -05:00
|
|
|
errClockWarp = errors.New("reply deadline too far in the future")
|
2015-03-25 10:45:53 -05:00
|
|
|
errClosed = errors.New("socket closed")
|
2015-01-27 07:33:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Timeouts
|
|
|
|
const (
|
2015-05-06 15:59:00 -05:00
|
|
|
respTimeout = 500 * time.Millisecond
|
|
|
|
sendTimeout = 500 * time.Millisecond
|
2015-02-12 04:59:52 -06:00
|
|
|
expiration = 20 * time.Second
|
2016-02-15 12:31:58 -06:00
|
|
|
|
2016-02-19 08:18:55 -06: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
|
2015-01-27 07:33:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// RPC packet types
|
|
|
|
const (
|
|
|
|
pingPacket = iota + 1 // zero is 'reserved'
|
|
|
|
pongPacket
|
|
|
|
findnodePacket
|
|
|
|
neighborsPacket
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC request structures
|
|
|
|
type (
|
|
|
|
ping struct {
|
2015-04-17 18:50:31 -05:00
|
|
|
Version uint
|
|
|
|
From, To rpcEndpoint
|
2015-01-27 07:33:26 -06:00
|
|
|
Expiration uint64
|
2015-12-22 18:52:40 -06:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
2015-04-17 18:50:31 -05:00
|
|
|
// pong is the reply to ping.
|
2015-01-27 07:33:26 -06:00
|
|
|
pong struct {
|
2015-04-17 18:50:31 -05: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-22 18:52:40 -06:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
2015-04-26 17:50:18 -05:00
|
|
|
// findnode is a query for nodes close to the given target.
|
2015-01-27 07:33:26 -06:00
|
|
|
findnode struct {
|
2015-04-26 17:50:18 -05:00
|
|
|
Target NodeID // doesn't need to be an actual public key
|
2015-01-27 07:33:26 -06:00
|
|
|
Expiration uint64
|
2015-12-22 18:52:40 -06:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// reply to findnode
|
|
|
|
neighbors struct {
|
2015-04-23 05:11:21 -05:00
|
|
|
Nodes []rpcNode
|
2015-01-27 07:33:26 -06:00
|
|
|
Expiration uint64
|
2015-12-22 18:52:40 -06:00
|
|
|
// Ignore additional fields (for forward compatibility).
|
|
|
|
Rest []rlp.RawValue `rlp:"tail"`
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2015-04-17 18:50:31 -05:00
|
|
|
|
2015-04-23 05:11:21 -05:00
|
|
|
rpcNode struct {
|
|
|
|
IP net.IP // len 4 for IPv4 or 16 for IPv6
|
|
|
|
UDP uint16 // for discovery protocol
|
|
|
|
TCP uint16 // for RLPx protocol
|
|
|
|
ID NodeID
|
|
|
|
}
|
|
|
|
|
2015-04-17 18:50:31 -05: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 07:33:26 -06:00
|
|
|
)
|
|
|
|
|
2015-04-17 18:50:31 -05:00
|
|
|
func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
|
|
|
|
ip := addr.IP.To4()
|
|
|
|
if ip == nil {
|
|
|
|
ip = addr.IP.To16()
|
|
|
|
}
|
|
|
|
return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:51:59 -06:00
|
|
|
func (t *udp) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) {
|
2016-11-21 11:11:54 -06: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 13:51:59 -06:00
|
|
|
if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) {
|
|
|
|
return nil, errors.New("not contained in netrestrict whitelist")
|
|
|
|
}
|
2015-12-07 05:06:49 -06:00
|
|
|
n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP)
|
|
|
|
err := n.validateComplete()
|
|
|
|
return n, err
|
2015-04-23 05:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func nodeToRPC(n *Node) rpcNode {
|
|
|
|
return rpcNode{ID: n.ID, IP: n.IP, UDP: n.UDP, TCP: n.TCP}
|
2015-02-06 07:40:53 -06:00
|
|
|
}
|
|
|
|
|
2015-03-25 10:45:53 -05:00
|
|
|
type packet interface {
|
|
|
|
handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error
|
2017-02-24 02:58:04 -06:00
|
|
|
name() string
|
2015-03-25 10:45:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
// udp implements the RPC protocol.
|
|
|
|
type udp struct {
|
2015-04-17 18:50:31 -05:00
|
|
|
conn conn
|
2016-11-22 13:51:59 -06:00
|
|
|
netrestrict *netutil.Netlist
|
2015-04-17 18:50:31 -05:00
|
|
|
priv *ecdsa.PrivateKey
|
|
|
|
ourEndpoint rpcEndpoint
|
2015-03-25 10:45:53 -05:00
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
addpending chan *pending
|
2015-03-25 10:45:53 -05:00
|
|
|
gotreply chan reply
|
|
|
|
|
|
|
|
closing chan struct{}
|
|
|
|
nat nat.Interface
|
2015-01-27 07:33:26 -06:00
|
|
|
|
|
|
|
*Table
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
from NodeID
|
|
|
|
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 {
|
|
|
|
from NodeID
|
|
|
|
ptype byte
|
|
|
|
data interface{}
|
2015-03-25 10:45:53 -05:00
|
|
|
// loop indicates whether there was
|
|
|
|
// a matching request by sending on this channel.
|
|
|
|
matched chan<- bool
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
2018-01-22 06:38:34 -06:00
|
|
|
// ReadPacket is sent to the unhandled channel when it could not be processed
|
|
|
|
type ReadPacket struct {
|
|
|
|
Data []byte
|
|
|
|
Addr *net.UDPAddr
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
// ListenUDP returns a new table that listens for UDP packets on laddr.
|
2018-01-22 06:38:34 -06:00
|
|
|
func ListenUDP(priv *ecdsa.PrivateKey, conn conn, realaddr *net.UDPAddr, unhandled chan ReadPacket, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) {
|
|
|
|
tab, _, err := newUDP(priv, conn, realaddr, unhandled, nodeDBPath, netrestrict)
|
2015-11-05 15:57:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-20 06:10:59 -05:00
|
|
|
log.Info("UDP listener up", "self", tab.self)
|
2015-03-25 10:45:53 -05:00
|
|
|
return tab, nil
|
|
|
|
}
|
|
|
|
|
2018-01-22 06:38:34 -06:00
|
|
|
func newUDP(priv *ecdsa.PrivateKey, c conn, realaddr *net.UDPAddr, unhandled chan ReadPacket, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) {
|
2015-01-27 07:33:26 -06:00
|
|
|
udp := &udp{
|
2016-11-22 13:51:59 -06:00
|
|
|
conn: c,
|
|
|
|
priv: priv,
|
|
|
|
netrestrict: netrestrict,
|
|
|
|
closing: make(chan struct{}),
|
|
|
|
gotreply: make(chan reply),
|
|
|
|
addpending: make(chan *pending),
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2015-04-17 18:50:31 -05:00
|
|
|
// TODO: separate TCP port
|
|
|
|
udp.ourEndpoint = makeEndpoint(realaddr, uint16(realaddr.Port))
|
2015-11-05 15:57:57 -06:00
|
|
|
tab, err := newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
udp.Table = tab
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
go udp.loop()
|
2018-01-22 06:38:34 -06:00
|
|
|
go udp.readLoop(unhandled)
|
2015-11-05 15:57:57 -06:00
|
|
|
return udp.Table, udp, nil
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) close() {
|
|
|
|
close(t.closing)
|
|
|
|
t.conn.Close()
|
|
|
|
// TODO: wait for the loops to end.
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping sends a ping message to the given node and waits for a reply.
|
2015-03-25 10:45:53 -05:00
|
|
|
func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error {
|
2015-01-27 07:33:26 -06:00
|
|
|
// TODO: maybe check for ReplyTo field in callback to measure RTT
|
2015-03-25 10:45:53 -05:00
|
|
|
errc := t.pending(toid, pongPacket, func(interface{}) bool { return true })
|
2017-02-24 02:58:04 -06:00
|
|
|
t.send(toaddr, pingPacket, &ping{
|
2015-03-24 09:36:11 -05:00
|
|
|
Version: Version,
|
2015-04-17 18:50:31 -05:00
|
|
|
From: t.ourEndpoint,
|
|
|
|
To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB
|
2015-01-27 07:33:26 -06:00
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
|
|
|
return <-errc
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:45:53 -05:00
|
|
|
func (t *udp) waitping(from NodeID) error {
|
|
|
|
return <-t.pending(from, pingPacket, func(interface{}) bool { return true })
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
// findnode sends a findnode request to the given node and waits until
|
|
|
|
// the node has sent up to k neighbors.
|
2015-03-25 10:45:53 -05:00
|
|
|
func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
|
2015-01-27 07:33:26 -06:00
|
|
|
nodes := make([]*Node, 0, bucketSize)
|
|
|
|
nreceived := 0
|
2015-03-25 10:45:53 -05:00
|
|
|
errc := t.pending(toid, neighborsPacket, func(r interface{}) bool {
|
2015-01-27 07:33:26 -06:00
|
|
|
reply := r.(*neighbors)
|
2015-04-23 05:11:21 -05:00
|
|
|
for _, rn := range reply.Nodes {
|
2015-01-27 07:33:26 -06:00
|
|
|
nreceived++
|
2016-11-22 13:51:59 -06:00
|
|
|
n, err := t.nodeFromRPC(toaddr, rn)
|
2016-11-21 11:11:54 -06:00
|
|
|
if err != nil {
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err)
|
2016-11-21 11:11:54 -06:00
|
|
|
continue
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2016-11-21 11:11:54 -06:00
|
|
|
nodes = append(nodes, n)
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2015-02-12 04:59:52 -06:00
|
|
|
return nreceived >= bucketSize
|
2015-01-27 07:33:26 -06:00
|
|
|
})
|
2017-02-24 02:58:04 -06:00
|
|
|
t.send(toaddr, findnodePacket, &findnode{
|
2015-01-27 07:33:26 -06:00
|
|
|
Target: target,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
|
|
|
err := <-errc
|
|
|
|
return nodes, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// pending adds a reply callback to the pending reply queue.
|
|
|
|
// see the documentation of type pending for a detailed explanation.
|
|
|
|
func (t *udp) pending(id NodeID, ptype byte, callback func(interface{}) bool) <-chan error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:45:53 -05:00
|
|
|
func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool {
|
2015-08-07 19:49:28 -05:00
|
|
|
matched := make(chan bool, 1)
|
2015-03-25 10:45:53 -05:00
|
|
|
select {
|
|
|
|
case t.gotreply <- reply{from, ptype, req, matched}:
|
|
|
|
// loop will handle it
|
|
|
|
return <-matched
|
|
|
|
case <-t.closing:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 12:31:58 -06:00
|
|
|
// loop runs in its own goroutine. it keeps track of
|
2015-01-27 07:33:26 -06:00
|
|
|
// the refresh timer and the pending reply queue.
|
|
|
|
func (t *udp) loop() {
|
|
|
|
var (
|
2016-02-15 12:31:58 -06:00
|
|
|
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
|
2016-02-19 08:18:55 -06:00
|
|
|
ntpWarnTime = time.Unix(0, 0)
|
2015-01-27 07:33:26 -06:00
|
|
|
)
|
|
|
|
<-timeout.C // ignore first timeout
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2015-08-07 19:49:28 -05:00
|
|
|
resetTimeout := func() {
|
|
|
|
if plist.Front() == nil || nextTimeout == plist.Front().Value {
|
2015-01-27 07:33:26 -06:00
|
|
|
return
|
|
|
|
}
|
2015-08-07 19:49:28 -05: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() {
|
|
|
|
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()
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-08-07 19:49:28 -05:00
|
|
|
resetTimeout()
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
select {
|
|
|
|
case <-t.closing:
|
2015-08-07 19:49:28 -05:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
|
|
|
el.Value.(*pending).errc <- errClosed
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
case p := <-t.addpending:
|
|
|
|
p.deadline = time.Now().Add(respTimeout)
|
2015-08-07 19:49:28 -05:00
|
|
|
plist.PushBack(p)
|
2015-01-27 07:33:26 -06:00
|
|
|
|
2015-03-25 10:45:53 -05:00
|
|
|
case r := <-t.gotreply:
|
|
|
|
var matched bool
|
2015-08-07 19:49:28 -05:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
|
|
|
p := el.Value.(*pending)
|
|
|
|
if p.from == r.from && p.ptype == r.ptype {
|
2015-03-25 10:45:53 -05:00
|
|
|
matched = true
|
2015-08-07 19:49:28 -05:00
|
|
|
// Remove the matcher if its callback indicates
|
|
|
|
// that all replies have been received. This is
|
|
|
|
// required for packet types that expect multiple
|
|
|
|
// reply packets.
|
2015-03-25 10:45:53 -05:00
|
|
|
if p.callback(r.data) {
|
|
|
|
p.errc <- nil
|
2015-08-07 19:49:28 -05:00
|
|
|
plist.Remove(el)
|
2015-03-25 10:45:53 -05:00
|
|
|
}
|
2016-02-15 12:31:58 -06:00
|
|
|
// Reset the continuous timeout counter (time drift detection)
|
|
|
|
contTimeouts = 0
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 10:45:53 -05:00
|
|
|
r.matched <- matched
|
2015-01-27 07:33:26 -06:00
|
|
|
|
|
|
|
case now := <-timeout.C:
|
2015-08-07 19:49:28 -05:00
|
|
|
nextTimeout = nil
|
2016-02-15 12:31:58 -06:00
|
|
|
|
2015-08-07 19:49:28 -05:00
|
|
|
// 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)
|
2016-02-15 12:31:58 -06:00
|
|
|
contTimeouts++
|
2015-08-07 19:49:28 -05:00
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2016-02-15 12:31:58 -06:00
|
|
|
// If we've accumulated too many timeouts, do an NTP time sync check
|
2016-02-19 08:18:55 -06:00
|
|
|
if contTimeouts > ntpFailureThreshold {
|
|
|
|
if time.Since(ntpWarnTime) >= ntpWarningCooldown {
|
|
|
|
ntpWarnTime = time.Now()
|
|
|
|
go checkClockDrift()
|
|
|
|
}
|
2016-02-15 12:31:58 -06:00
|
|
|
contTimeouts = 0
|
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
macSize = 256 / 8
|
|
|
|
sigSize = 520 / 8
|
|
|
|
headSize = macSize + sigSize // space of packet frame data
|
|
|
|
)
|
|
|
|
|
2015-05-13 14:29:32 -05:00
|
|
|
var (
|
|
|
|
headSpace = make([]byte, headSize)
|
|
|
|
|
2015-08-07 19:49:28 -05:00
|
|
|
// Neighbors replies are sent across multiple packets to
|
2015-05-13 14:29:32 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req packet) error {
|
2015-03-25 10:45:53 -05:00
|
|
|
packet, err := encodePacket(t.priv, ptype, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-24 02:58:04 -06:00
|
|
|
_, err = t.conn.WriteToUDP(packet, toaddr)
|
|
|
|
log.Trace(">> "+req.name(), "addr", toaddr, "err", err)
|
2015-03-25 10:45:53 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, error) {
|
2015-01-27 07:33:26 -06:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.Write(headSpace)
|
|
|
|
b.WriteByte(ptype)
|
|
|
|
if err := rlp.Encode(b, req); err != nil {
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Error("Can't encode discv4 packet", "err", err)
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, err
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
packet := b.Bytes()
|
2016-02-21 12:40:27 -06:00
|
|
|
sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
|
2015-01-27 07:33:26 -06:00
|
|
|
if err != nil {
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Error("Can't sign discv4 packet", "err", err)
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, err
|
2015-01-27 07:33:26 -06: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 10:45:53 -05:00
|
|
|
// The future.
|
2016-02-21 12:40:27 -06:00
|
|
|
copy(packet, crypto.Keccak256(packet[macSize:]))
|
2015-03-25 10:45:53 -05:00
|
|
|
return packet, nil
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
2018-01-22 06:38:34 -06:00
|
|
|
func (t *udp) readLoop(unhandled chan ReadPacket) {
|
2015-01-27 07:33:26 -06:00
|
|
|
defer t.conn.Close()
|
2018-01-22 06:38:34 -06:00
|
|
|
if unhandled != nil {
|
|
|
|
defer close(unhandled)
|
|
|
|
}
|
2015-05-13 14:29:32 -05: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.
|
UDP Interop. Limit datagrams to 1280bytes.
We don't have a UDP which specifies any messages that will be 4KB. Aside from being implemented for months and a necessity for encryption and piggy-backing packets, 1280bytes is ideal, and, means this TODO can be completed!
Why 1280 bytes?
* It's less than the default MTU for most WAN/LAN networks. That means fewer fragmented datagrams (esp on well-connected networks).
* Fragmented datagrams and dropped packets suck and add latency while OS waits for a dropped fragment to never arrive (blocking readLoop())
* Most of our packets are < 1280 bytes.
* 1280 bytes is minimum datagram size and MTU for IPv6 -- on IPv6, a datagram < 1280bytes will *never* be fragmented.
UDP datagrams are dropped. A lot! And fragmented datagrams are worse. If a datagram has a 30% chance of being dropped, then a fragmented datagram has a 60% chance of being dropped. More importantly, we have signed packets and can't do anything with a packet unless we receive the entire datagram because the signature can't be verified. The same is true when we have encrypted packets.
So the solution here to picking an ideal buffer size for receiving datagrams is a number under 1400bytes. And the lower-bound value for IPv6 of 1280 bytes make's it a non-decision. On IPv4 most ISPs and 3g/4g/let networks have an MTU just over 1400 -- and *never* over 1500. Never -- that means packets over 1500 (in reality: ~1450) bytes are fragmented. And probably dropped a lot.
Just to prove the point, here are pings sending non-fragmented packets over wifi/ISP, and a second set of pings via cell-phone tethering. It's important to note that, if *any* router between my system and the EC2 node has a lower MTU, the message would not go through:
On wifi w/normal ISP:
localhost:Debug $ ping -D -s 1450 52.6.250.242
PING 52.6.250.242 (52.6.250.242): 1450 data bytes
1458 bytes from 52.6.250.242: icmp_seq=0 ttl=42 time=104.831 ms
1458 bytes from 52.6.250.242: icmp_seq=1 ttl=42 time=119.004 ms
^C
--- 52.6.250.242 ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 104.831/111.918/119.004/7.087 ms
localhost:Debug $ ping -D -s 1480 52.6.250.242
PING 52.6.250.242 (52.6.250.242): 1480 data bytes
ping: sendto: Message too long
ping: sendto: Message too long
Request timeout for icmp_seq 0
ping: sendto: Message too long
Request timeout for icmp_seq 1
Tethering to O2:
localhost:Debug $ ping -D -s 1480 52.6.250.242
PING 52.6.250.242 (52.6.250.242): 1480 data bytes
ping: sendto: Message too long
ping: sendto: Message too long
Request timeout for icmp_seq 0
^C
--- 52.6.250.242 ping statistics ---
2 packets transmitted, 0 packets received, 100.0% packet loss
localhost:Debug $ ping -D -s 1450 52.6.250.242
PING 52.6.250.242 (52.6.250.242): 1450 data bytes
1458 bytes from 52.6.250.242: icmp_seq=0 ttl=42 time=107.844 ms
1458 bytes from 52.6.250.242: icmp_seq=1 ttl=42 time=105.127 ms
1458 bytes from 52.6.250.242: icmp_seq=2 ttl=42 time=120.483 ms
1458 bytes from 52.6.250.242: icmp_seq=3 ttl=42 time=102.136 ms
2015-05-13 12:03:00 -05:00
|
|
|
buf := make([]byte, 1280)
|
2015-01-27 07:33:26 -06:00
|
|
|
for {
|
|
|
|
nbytes, from, err := t.conn.ReadFromUDP(buf)
|
2016-11-21 11:39:36 -06:00
|
|
|
if netutil.IsTemporaryError(err) {
|
2015-08-19 07:11:12 -05:00
|
|
|
// Ignore temporary read errors.
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Debug("Temporary UDP read error", "err", err)
|
2015-08-19 07:11:12 -05:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
// Shut down the loop for permament errors.
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Debug("UDP read error", "err", err)
|
2015-01-27 07:33:26 -06:00
|
|
|
return
|
|
|
|
}
|
2018-01-22 06:38:34 -06:00
|
|
|
if t.handlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
|
|
|
|
select {
|
|
|
|
case unhandled <- ReadPacket{buf[:nbytes], from}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 18:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error {
|
|
|
|
packet, fromID, hash, err := decodePacket(buf)
|
|
|
|
if err != nil {
|
2017-02-24 02:58:04 -06:00
|
|
|
log.Debug("Bad discv4 packet", "addr", from, "err", err)
|
2015-04-17 18:50:31 -05:00
|
|
|
return err
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2017-02-24 02:58:04 -06:00
|
|
|
err = packet.handle(t, from, fromID, hash)
|
|
|
|
log.Trace("<< "+packet.name(), "addr", from, "err", err)
|
2015-04-17 18:50:31 -05:00
|
|
|
return err
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
2015-03-25 10:45:53 -05:00
|
|
|
func decodePacket(buf []byte) (packet, NodeID, []byte, error) {
|
2015-01-27 07:33:26 -06:00
|
|
|
if len(buf) < headSize+1 {
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, NodeID{}, nil, errPacketTooSmall
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
|
2016-02-21 12:40:27 -06:00
|
|
|
shouldhash := crypto.Keccak256(buf[macSize:])
|
2015-01-27 07:33:26 -06:00
|
|
|
if !bytes.Equal(hash, shouldhash) {
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, NodeID{}, nil, errBadHash
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2016-02-21 12:40:27 -06:00
|
|
|
fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig)
|
2015-01-27 07:33:26 -06:00
|
|
|
if err != nil {
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, NodeID{}, hash, err
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2015-03-25 10:45:53 -05:00
|
|
|
var req packet
|
2015-01-27 07:33:26 -06:00
|
|
|
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:
|
2015-03-25 10:45:53 -05:00
|
|
|
return nil, fromID, hash, fmt.Errorf("unknown type: %d", ptype)
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
2015-12-22 18:52:40 -06:00
|
|
|
s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
|
|
|
|
err = s.Decode(req)
|
2015-03-25 10:45:53 -05:00
|
|
|
return req, fromID, hash, err
|
2015-01-27 07:33:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2017-02-24 02:58:04 -06:00
|
|
|
t.send(from, pongPacket, &pong{
|
2015-04-17 18:50:31 -05:00
|
|
|
To: makeEndpoint(from, req.From.TCP),
|
2015-01-27 07:33:26 -06:00
|
|
|
ReplyTok: mac,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2015-03-25 10:45:53 -05:00
|
|
|
if !t.handleReply(fromID, pingPacket, req) {
|
|
|
|
// Note: we're ignoring the provided IP address right now
|
2015-04-17 18:50:31 -05:00
|
|
|
go t.bond(true, fromID, from, req.From.TCP)
|
2015-03-25 10:45:53 -05:00
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (req *ping) name() string { return "PING/v4" }
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-25 10:45:53 -05:00
|
|
|
if !t.handleReply(fromID, pongPacket, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (req *pong) name() string { return "PONG/v4" }
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-04-24 10:04:41 -05:00
|
|
|
if t.db.node(fromID) == nil {
|
2015-03-25 10:45:53 -05:00
|
|
|
// No bond 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.
|
|
|
|
return errUnknownNode
|
|
|
|
}
|
2016-02-21 12:40:27 -06:00
|
|
|
target := crypto.Keccak256Hash(req.Target[:])
|
2015-01-27 07:33:26 -06:00
|
|
|
t.mutex.Lock()
|
2015-04-26 17:50:18 -05:00
|
|
|
closest := t.closest(target, bucketSize).entries
|
2015-01-27 07:33:26 -06:00
|
|
|
t.mutex.Unlock()
|
|
|
|
|
2015-05-13 14:29:32 -05:00
|
|
|
p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
|
|
|
|
// Send neighbors in chunks with at most maxNeighbors per packet
|
|
|
|
// to stay below the 1280 byte limit.
|
2015-04-23 05:11:21 -05:00
|
|
|
for i, n := range closest {
|
2016-11-21 11:11:54 -06:00
|
|
|
if netutil.CheckRelayIP(from.IP, n.IP) != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-13 14:29:32 -05:00
|
|
|
p.Nodes = append(p.Nodes, nodeToRPC(n))
|
|
|
|
if len(p.Nodes) == maxNeighbors || i == len(closest)-1 {
|
2017-02-24 02:58:04 -06:00
|
|
|
t.send(from, neighborsPacket, &p)
|
2015-05-13 14:29:32 -05:00
|
|
|
p.Nodes = p.Nodes[:0]
|
|
|
|
}
|
2015-05-13 13:03:17 -05:00
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (req *findnode) name() string { return "FINDNODE/v4" }
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-25 10:45:53 -05:00
|
|
|
if !t.handleReply(fromID, neighborsPacket, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 07:33:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:58:04 -06:00
|
|
|
func (req *neighbors) name() string { return "NEIGHBORS/v4" }
|
|
|
|
|
2015-01-27 07:33:26 -06:00
|
|
|
func expired(ts uint64) bool {
|
|
|
|
return time.Unix(int64(ts), 0).Before(time.Now())
|
|
|
|
}
|