2015-01-27 13:33:26 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-02-13 10:38:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2015-01-27 13:33:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logger.NewLogger("P2P Discovery")
|
|
|
|
|
2015-03-24 14:36:11 +00:00
|
|
|
const Version = 3
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
// Errors
|
|
|
|
var (
|
2015-03-25 15:45:53 +00:00
|
|
|
errPacketTooSmall = errors.New("too small")
|
|
|
|
errBadHash = errors.New("bad hash")
|
|
|
|
errExpired = errors.New("expired")
|
|
|
|
errBadVersion = errors.New("version mismatch")
|
|
|
|
errUnsolicitedReply = errors.New("unsolicited reply")
|
|
|
|
errUnknownNode = errors.New("unknown node")
|
|
|
|
errTimeout = errors.New("RPC timeout")
|
|
|
|
errClosed = errors.New("socket closed")
|
2015-01-27 13:33:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Timeouts
|
|
|
|
const (
|
|
|
|
respTimeout = 300 * time.Millisecond
|
|
|
|
sendTimeout = 300 * time.Millisecond
|
2015-02-12 10:59:52 +00:00
|
|
|
expiration = 20 * time.Second
|
2015-01-27 13:33:26 +00:00
|
|
|
|
|
|
|
refreshInterval = 1 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC packet types
|
|
|
|
const (
|
|
|
|
pingPacket = iota + 1 // zero is 'reserved'
|
|
|
|
pongPacket
|
|
|
|
findnodePacket
|
|
|
|
neighborsPacket
|
|
|
|
)
|
|
|
|
|
|
|
|
// RPC request structures
|
|
|
|
type (
|
|
|
|
ping struct {
|
2015-03-24 14:36:11 +00:00
|
|
|
Version uint // must match Version
|
2015-01-27 13:33:26 +00:00
|
|
|
IP string // our IP
|
|
|
|
Port uint16 // our port
|
|
|
|
Expiration uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply to Ping
|
|
|
|
pong struct {
|
|
|
|
ReplyTok []byte
|
|
|
|
Expiration uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
findnode struct {
|
|
|
|
// Id to look up. The responding node will send back nodes
|
|
|
|
// closest to the target.
|
|
|
|
Target NodeID
|
|
|
|
Expiration uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply to findnode
|
|
|
|
neighbors struct {
|
|
|
|
Nodes []*Node
|
|
|
|
Expiration uint64
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-02-06 13:40:53 +00:00
|
|
|
type rpcNode struct {
|
|
|
|
IP string
|
|
|
|
Port uint16
|
|
|
|
ID NodeID
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
type packet interface {
|
|
|
|
handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
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 13:33:26 +00:00
|
|
|
// udp implements the RPC protocol.
|
|
|
|
type udp struct {
|
2015-03-25 15:45:53 +00:00
|
|
|
conn conn
|
|
|
|
priv *ecdsa.PrivateKey
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
addpending chan *pending
|
2015-03-25 15:45:53 +00:00
|
|
|
gotreply chan reply
|
|
|
|
|
|
|
|
closing chan struct{}
|
|
|
|
nat nat.Interface
|
2015-01-27 13:33:26 +00: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 15:45:53 +00:00
|
|
|
// loop indicates whether there was
|
|
|
|
// a matching request by sending on this channel.
|
|
|
|
matched chan<- bool
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUDP returns a new table that listens for UDP packets on laddr.
|
2015-02-13 10:38:34 +00:00
|
|
|
func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface) (*Table, error) {
|
2015-01-27 13:33:26 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", laddr)
|
|
|
|
if err != nil {
|
2015-02-13 14:04:30 +00:00
|
|
|
return nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
2015-02-13 14:04:30 +00:00
|
|
|
return nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
tab, _ := newUDP(priv, conn, natm)
|
|
|
|
log.Infoln("Listening,", tab.self)
|
|
|
|
return tab, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface) (*Table, *udp) {
|
2015-01-27 13:33:26 +00:00
|
|
|
udp := &udp{
|
2015-03-25 15:45:53 +00:00
|
|
|
conn: c,
|
2015-01-27 13:33:26 +00:00
|
|
|
priv: priv,
|
|
|
|
closing: make(chan struct{}),
|
2015-03-25 15:45:53 +00:00
|
|
|
gotreply: make(chan reply),
|
2015-01-27 13:33:26 +00:00
|
|
|
addpending: make(chan *pending),
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
realaddr := c.LocalAddr().(*net.UDPAddr)
|
2015-02-13 14:04:30 +00:00
|
|
|
if natm != nil {
|
|
|
|
if !realaddr.IP.IsLoopback() {
|
|
|
|
go nat.Map(natm, udp.closing, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
|
|
|
|
}
|
|
|
|
// TODO: react to external IP changes over time.
|
|
|
|
if ext, err := natm.ExternalIP(); err == nil {
|
|
|
|
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
udp.Table = newTable(udp, PubkeyID(&priv.PublicKey), realaddr)
|
2015-01-27 13:33:26 +00:00
|
|
|
go udp.loop()
|
|
|
|
go udp.readLoop()
|
2015-03-25 15:45:53 +00:00
|
|
|
return udp.Table, udp
|
2015-01-27 13:33:26 +00: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 15:45:53 +00:00
|
|
|
func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error {
|
2015-01-27 13:33:26 +00:00
|
|
|
// TODO: maybe check for ReplyTo field in callback to measure RTT
|
2015-03-25 15:45:53 +00:00
|
|
|
errc := t.pending(toid, pongPacket, func(interface{}) bool { return true })
|
|
|
|
t.send(toaddr, pingPacket, ping{
|
2015-03-24 14:36:11 +00:00
|
|
|
Version: Version,
|
2015-02-06 13:40:53 +00:00
|
|
|
IP: t.self.IP.String(),
|
|
|
|
Port: uint16(t.self.TCPPort),
|
2015-01-27 13:33:26 +00:00
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
|
|
|
return <-errc
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
func (t *udp) waitping(from NodeID) error {
|
|
|
|
return <-t.pending(from, pingPacket, func(interface{}) bool { return true })
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
// findnode sends a findnode request to the given node and waits until
|
|
|
|
// the node has sent up to k neighbors.
|
2015-03-25 15:45:53 +00:00
|
|
|
func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
|
2015-01-27 13:33:26 +00:00
|
|
|
nodes := make([]*Node, 0, bucketSize)
|
|
|
|
nreceived := 0
|
2015-03-25 15:45:53 +00:00
|
|
|
errc := t.pending(toid, neighborsPacket, func(r interface{}) bool {
|
2015-01-27 13:33:26 +00:00
|
|
|
reply := r.(*neighbors)
|
2015-02-09 10:02:32 +00:00
|
|
|
for _, n := range reply.Nodes {
|
2015-01-27 13:33:26 +00:00
|
|
|
nreceived++
|
2015-02-09 10:02:32 +00:00
|
|
|
if n.isValid() {
|
2015-01-27 13:33:26 +00:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 10:59:52 +00:00
|
|
|
return nreceived >= bucketSize
|
2015-01-27 13:33:26 +00:00
|
|
|
})
|
2015-03-25 15:45:53 +00:00
|
|
|
t.send(toaddr, findnodePacket, findnode{
|
2015-01-27 13:33:26 +00: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 15:45:53 +00:00
|
|
|
func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool {
|
|
|
|
matched := make(chan bool)
|
|
|
|
select {
|
|
|
|
case t.gotreply <- reply{from, ptype, req, matched}:
|
|
|
|
// loop will handle it
|
|
|
|
return <-matched
|
|
|
|
case <-t.closing:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:33:26 +00:00
|
|
|
// loop runs in its own goroutin. it keeps track of
|
|
|
|
// the refresh timer and the pending reply queue.
|
|
|
|
func (t *udp) loop() {
|
|
|
|
var (
|
|
|
|
pending []*pending
|
|
|
|
nextDeadline time.Time
|
|
|
|
timeout = time.NewTimer(0)
|
|
|
|
refresh = time.NewTicker(refreshInterval)
|
|
|
|
)
|
|
|
|
<-timeout.C // ignore first timeout
|
|
|
|
defer refresh.Stop()
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
rearmTimeout := func() {
|
|
|
|
if len(pending) == 0 || nextDeadline == pending[0].deadline {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nextDeadline = pending[0].deadline
|
|
|
|
timeout.Reset(nextDeadline.Sub(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-refresh.C:
|
|
|
|
go t.refresh()
|
|
|
|
|
|
|
|
case <-t.closing:
|
|
|
|
for _, p := range pending {
|
|
|
|
p.errc <- errClosed
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
pending = nil
|
2015-01-27 13:33:26 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case p := <-t.addpending:
|
|
|
|
p.deadline = time.Now().Add(respTimeout)
|
|
|
|
pending = append(pending, p)
|
|
|
|
rearmTimeout()
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
case r := <-t.gotreply:
|
|
|
|
var matched bool
|
2015-02-17 14:14:12 +00:00
|
|
|
for i := 0; i < len(pending); i++ {
|
2015-03-25 15:45:53 +00:00
|
|
|
if p := pending[i]; p.from == r.from && p.ptype == r.ptype {
|
|
|
|
matched = true
|
|
|
|
if p.callback(r.data) {
|
|
|
|
// callback indicates the request is done, remove it.
|
|
|
|
p.errc <- nil
|
|
|
|
copy(pending[i:], pending[i+1:])
|
|
|
|
pending = pending[:len(pending)-1]
|
|
|
|
i--
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
r.matched <- matched
|
2015-01-27 13:33:26 +00:00
|
|
|
|
|
|
|
case now := <-timeout.C:
|
|
|
|
// notify and remove callbacks whose deadline is in the past.
|
|
|
|
i := 0
|
|
|
|
for ; i < len(pending) && now.After(pending[i].deadline); i++ {
|
|
|
|
pending[i].errc <- errTimeout
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
copy(pending, pending[i:])
|
|
|
|
pending = pending[:len(pending)-i]
|
|
|
|
}
|
|
|
|
rearmTimeout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
macSize = 256 / 8
|
|
|
|
sigSize = 520 / 8
|
|
|
|
headSize = macSize + sigSize // space of packet frame data
|
|
|
|
)
|
|
|
|
|
|
|
|
var headSpace = make([]byte, headSize)
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req interface{}) error {
|
|
|
|
packet, err := encodePacket(t.priv, ptype, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.DebugDetailf(">>> %v %T %v\n", toaddr, req, req)
|
|
|
|
if _, err = t.conn.WriteToUDP(packet, toaddr); err != nil {
|
|
|
|
log.DebugDetailln("UDP send failed:", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, error) {
|
2015-01-27 13:33:26 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.Write(headSpace)
|
|
|
|
b.WriteByte(ptype)
|
|
|
|
if err := rlp.Encode(b, req); err != nil {
|
|
|
|
log.Errorln("error encoding packet:", err)
|
2015-03-25 15:45:53 +00:00
|
|
|
return nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
packet := b.Bytes()
|
2015-03-25 15:45:53 +00:00
|
|
|
sig, err := crypto.Sign(crypto.Sha3(packet[headSize:]), priv)
|
2015-01-27 13:33:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln("could not sign packet:", err)
|
2015-03-25 15:45:53 +00:00
|
|
|
return nil, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
copy(packet[macSize:], sig)
|
|
|
|
// add the hash to the front. Note: this doesn't protect the
|
|
|
|
// packet in any way. Our public key will be part of this hash in
|
2015-03-25 15:45:53 +00:00
|
|
|
// The future.
|
2015-01-27 13:33:26 +00:00
|
|
|
copy(packet, crypto.Sha3(packet[macSize:]))
|
2015-03-25 15:45:53 +00:00
|
|
|
return packet, nil
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
|
|
|
func (t *udp) readLoop() {
|
|
|
|
defer t.conn.Close()
|
|
|
|
buf := make([]byte, 4096) // TODO: good buffer size
|
|
|
|
for {
|
|
|
|
nbytes, from, err := t.conn.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
packet, fromID, hash, err := decodePacket(buf[:nbytes])
|
|
|
|
if err != nil {
|
2015-01-27 13:33:26 +00:00
|
|
|
log.Debugf("Bad packet from %v: %v\n", from, err)
|
2015-03-25 15:45:53 +00:00
|
|
|
continue
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
log.DebugDetailf("<<< %v %T %v\n", from, packet, packet)
|
|
|
|
go func() {
|
|
|
|
if err := packet.handle(t, from, fromID, hash); err != nil {
|
|
|
|
log.Debugf("error handling %T from %v: %v", packet, from, err)
|
|
|
|
}
|
|
|
|
}()
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
func decodePacket(buf []byte) (packet, NodeID, []byte, error) {
|
2015-01-27 13:33:26 +00:00
|
|
|
if len(buf) < headSize+1 {
|
2015-03-25 15:45:53 +00:00
|
|
|
return nil, NodeID{}, nil, errPacketTooSmall
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
|
|
|
|
shouldhash := crypto.Sha3(buf[macSize:])
|
|
|
|
if !bytes.Equal(hash, shouldhash) {
|
2015-03-25 15:45:53 +00:00
|
|
|
return nil, NodeID{}, nil, errBadHash
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
fromID, err := recoverNodeID(crypto.Sha3(buf[headSize:]), sig)
|
|
|
|
if err != nil {
|
2015-03-25 15:45:53 +00:00
|
|
|
return nil, NodeID{}, hash, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
var req packet
|
2015-01-27 13:33:26 +00: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 15:45:53 +00:00
|
|
|
return nil, fromID, hash, fmt.Errorf("unknown type: %d", ptype)
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
err = rlp.Decode(bytes.NewReader(sigdata[1:]), req)
|
|
|
|
return req, fromID, hash, err
|
2015-01-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-24 14:36:11 +00:00
|
|
|
if req.Version != Version {
|
|
|
|
return errBadVersion
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
t.send(from, pongPacket, pong{
|
2015-01-27 13:33:26 +00:00
|
|
|
ReplyTok: mac,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2015-03-25 15:45:53 +00:00
|
|
|
if !t.handleReply(fromID, pingPacket, req) {
|
|
|
|
// Note: we're ignoring the provided IP address right now
|
|
|
|
t.bond(true, fromID, from, req.Port)
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
if !t.handleReply(fromID, pongPacket, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
if t.db.get(fromID) == nil {
|
|
|
|
// 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
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
t.mutex.Lock()
|
|
|
|
closest := t.closest(req.Target, bucketSize).entries
|
|
|
|
t.mutex.Unlock()
|
|
|
|
|
2015-03-25 15:45:53 +00:00
|
|
|
t.send(from, neighborsPacket, neighbors{
|
2015-01-27 13:33:26 +00:00
|
|
|
Nodes: closest,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
|
|
|
|
if expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2015-03-25 15:45:53 +00:00
|
|
|
if !t.handleReply(fromID, neighborsPacket, req) {
|
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 13:33:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expired(ts uint64) bool {
|
|
|
|
return time.Unix(int64(ts), 0).Before(time.Now())
|
|
|
|
}
|