github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/p2p/discv5/udp.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package discv5
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/ecdsa"
    22  	"errors"
    23  	"fmt"
    24  	"net"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/log"
    30  	"github.com/ethereum/go-ethereum/p2p/nat"
    31  	"github.com/ethereum/go-ethereum/p2p/netutil"
    32  	"github.com/ethereum/go-ethereum/rlp"
    33  )
    34  
    35  const Version = 4
    36  
    37  // Errors
    38  var (
    39  	errPacketTooSmall   = errors.New("too small")
    40  	errBadHash          = errors.New("bad hash")
    41  	errExpired          = errors.New("expired")
    42  	errUnsolicitedReply = errors.New("unsolicited reply")
    43  	errUnknownNode      = errors.New("unknown node")
    44  	errTimeout          = errors.New("RPC timeout")
    45  	errClockWarp        = errors.New("reply deadline too far in the future")
    46  	errClosed           = errors.New("socket closed")
    47  )
    48  
    49  // Timeouts
    50  const (
    51  	respTimeout = 500 * time.Millisecond
    52  	sendTimeout = 500 * time.Millisecond
    53  	expiration  = 20 * time.Second
    54  
    55  	ntpFailureThreshold = 32               // Continuous timeouts after which to check NTP
    56  	ntpWarningCooldown  = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning
    57  	driftThreshold      = 10 * time.Second // Allowed clock drift before warning user
    58  )
    59  
    60  // RPC request structures
    61  type (
    62  	ping struct {
    63  		Version    uint
    64  		From, To   rpcEndpoint
    65  		Expiration uint64
    66  
    67  		// v5
    68  		Topics []Topic
    69  
    70  		// Ignore additional fields (for forward compatibility).
    71  		Rest []rlp.RawValue `rlp:"tail"`
    72  	}
    73  
    74  	// pong is the reply to ping.
    75  	pong struct {
    76  		// This field should mirror the UDP envelope address
    77  		// of the ping packet, which provides a way to discover the
    78  		// the external address (after NAT).
    79  		To rpcEndpoint
    80  
    81  		ReplyTok   []byte // This contains the hash of the ping packet.
    82  		Expiration uint64 // Absolute timestamp at which the packet becomes invalid.
    83  
    84  		// v5
    85  		TopicHash    common.Hash
    86  		TicketSerial uint32
    87  		WaitPeriods  []uint32
    88  
    89  		// Ignore additional fields (for forward compatibility).
    90  		Rest []rlp.RawValue `rlp:"tail"`
    91  	}
    92  
    93  	// findnode is a query for nodes close to the given target.
    94  	findnode struct {
    95  		Target     NodeID // doesn't need to be an actual public key
    96  		Expiration uint64
    97  		// Ignore additional fields (for forward compatibility).
    98  		Rest []rlp.RawValue `rlp:"tail"`
    99  	}
   100  
   101  	// findnode is a query for nodes close to the given target.
   102  	findnodeHash struct {
   103  		Target     common.Hash
   104  		Expiration uint64
   105  		// Ignore additional fields (for forward compatibility).
   106  		Rest []rlp.RawValue `rlp:"tail"`
   107  	}
   108  
   109  	// reply to findnode
   110  	neighbors struct {
   111  		Nodes      []rpcNode
   112  		Expiration uint64
   113  		// Ignore additional fields (for forward compatibility).
   114  		Rest []rlp.RawValue `rlp:"tail"`
   115  	}
   116  
   117  	topicRegister struct {
   118  		Topics []Topic
   119  		Idx    uint
   120  		Pong   []byte
   121  	}
   122  
   123  	topicQuery struct {
   124  		Topic      Topic
   125  		Expiration uint64
   126  	}
   127  
   128  	// reply to topicQuery
   129  	topicNodes struct {
   130  		Echo  common.Hash
   131  		Nodes []rpcNode
   132  	}
   133  
   134  	rpcNode struct {
   135  		IP  net.IP // len 4 for IPv4 or 16 for IPv6
   136  		UDP uint16 // for discovery protocol
   137  		TCP uint16 // for RLPx protocol
   138  		ID  NodeID
   139  	}
   140  
   141  	rpcEndpoint struct {
   142  		IP  net.IP // len 4 for IPv4 or 16 for IPv6
   143  		UDP uint16 // for discovery protocol
   144  		TCP uint16 // for RLPx protocol
   145  	}
   146  )
   147  
   148  const (
   149  	macSize  = 256 / 8
   150  	sigSize  = 520 / 8
   151  	headSize = macSize + sigSize // space of packet frame data
   152  )
   153  
   154  // Neighbors replies are sent across multiple packets to
   155  // stay below the 1280 byte limit. We compute the maximum number
   156  // of entries by stuffing a packet until it grows too large.
   157  var maxNeighbors = func() int {
   158  	p := neighbors{Expiration: ^uint64(0)}
   159  	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)}
   160  	for n := 0; ; n++ {
   161  		p.Nodes = append(p.Nodes, maxSizeNode)
   162  		size, _, err := rlp.EncodeToReader(p)
   163  		if err != nil {
   164  			// If this ever happens, it will be caught by the unit tests.
   165  			panic("cannot encode: " + err.Error())
   166  		}
   167  		if headSize+size+1 >= 1280 {
   168  			return n
   169  		}
   170  	}
   171  }()
   172  
   173  var maxTopicNodes = func() int {
   174  	p := topicNodes{}
   175  	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)}
   176  	for n := 0; ; n++ {
   177  		p.Nodes = append(p.Nodes, maxSizeNode)
   178  		size, _, err := rlp.EncodeToReader(p)
   179  		if err != nil {
   180  			// If this ever happens, it will be caught by the unit tests.
   181  			panic("cannot encode: " + err.Error())
   182  		}
   183  		if headSize+size+1 >= 1280 {
   184  			return n
   185  		}
   186  	}
   187  }()
   188  
   189  func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
   190  	ip := addr.IP.To4()
   191  	if ip == nil {
   192  		ip = addr.IP.To16()
   193  	}
   194  	return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
   195  }
   196  
   197  func (e1 rpcEndpoint) equal(e2 rpcEndpoint) bool {
   198  	return e1.UDP == e2.UDP && e1.TCP == e2.TCP && e1.IP.Equal(e2.IP)
   199  }
   200  
   201  func nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) {
   202  	if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
   203  		return nil, err
   204  	}
   205  	n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP)
   206  	err := n.validateComplete()
   207  	return n, err
   208  }
   209  
   210  func nodeToRPC(n *Node) rpcNode {
   211  	return rpcNode{ID: n.ID, IP: n.IP, UDP: n.UDP, TCP: n.TCP}
   212  }
   213  
   214  type ingressPacket struct {
   215  	remoteID   NodeID
   216  	remoteAddr *net.UDPAddr
   217  	ev         nodeEvent
   218  	hash       []byte
   219  	data       interface{} // one of the RPC structs
   220  	rawData    []byte
   221  }
   222  
   223  type conn interface {
   224  	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
   225  	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
   226  	Close() error
   227  	LocalAddr() net.Addr
   228  }
   229  
   230  // udp implements the RPC protocol.
   231  type udp struct {
   232  	conn        conn
   233  	priv        *ecdsa.PrivateKey
   234  	ourEndpoint rpcEndpoint
   235  	nat         nat.Interface
   236  	net         *Network
   237  }
   238  
   239  // ListenUDP returns a new table that listens for UDP packets on laddr.
   240  func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Network, error) {
   241  
   242  	transport, err := listenUDP(priv, laddr)
   243  	if err != nil {
   244  		return nil, err
   245  	}
   246  	net, err := newNetwork(transport, priv.PublicKey, natm, nodeDBPath, netrestrict)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  	transport.net = net
   251  	go transport.readLoop()
   252  	return net, nil
   253  }
   254  
   255  func listenUDP(priv *ecdsa.PrivateKey, laddr string) (*udp, error) {
   256  	addr, err := net.ResolveUDPAddr("udp", laddr)
   257  	if err != nil {
   258  		return nil, err
   259  	}
   260  	conn, err := net.ListenUDP("udp", addr)
   261  	if err != nil {
   262  		return nil, err
   263  	}
   264  	return &udp{conn: conn, priv: priv, ourEndpoint: makeEndpoint(addr, uint16(addr.Port))}, nil
   265  }
   266  
   267  func (t *udp) localAddr() *net.UDPAddr {
   268  	return t.conn.LocalAddr().(*net.UDPAddr)
   269  }
   270  
   271  func (t *udp) Close() {
   272  	t.conn.Close()
   273  }
   274  
   275  func (t *udp) send(remote *Node, ptype nodeEvent, data interface{}) (hash []byte) {
   276  	hash, _ = t.sendPacket(remote.ID, remote.addr(), byte(ptype), data)
   277  	return hash
   278  }
   279  
   280  func (t *udp) sendPing(remote *Node, toaddr *net.UDPAddr, topics []Topic) (hash []byte) {
   281  	hash, _ = t.sendPacket(remote.ID, toaddr, byte(pingPacket), ping{
   282  		Version:    Version,
   283  		From:       t.ourEndpoint,
   284  		To:         makeEndpoint(toaddr, uint16(toaddr.Port)), // TODO: maybe use known TCP port from DB
   285  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   286  		Topics:     topics,
   287  	})
   288  	return hash
   289  }
   290  
   291  func (t *udp) sendFindnode(remote *Node, target NodeID) {
   292  	t.sendPacket(remote.ID, remote.addr(), byte(findnodePacket), findnode{
   293  		Target:     target,
   294  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   295  	})
   296  }
   297  
   298  func (t *udp) sendNeighbours(remote *Node, results []*Node) {
   299  	// Send neighbors in chunks with at most maxNeighbors per packet
   300  	// to stay below the 1280 byte limit.
   301  	p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
   302  	for i, result := range results {
   303  		p.Nodes = append(p.Nodes, nodeToRPC(result))
   304  		if len(p.Nodes) == maxNeighbors || i == len(results)-1 {
   305  			t.sendPacket(remote.ID, remote.addr(), byte(neighborsPacket), p)
   306  			p.Nodes = p.Nodes[:0]
   307  		}
   308  	}
   309  }
   310  
   311  func (t *udp) sendFindnodeHash(remote *Node, target common.Hash) {
   312  	t.sendPacket(remote.ID, remote.addr(), byte(findnodeHashPacket), findnodeHash{
   313  		Target:     target,
   314  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   315  	})
   316  }
   317  
   318  func (t *udp) sendTopicRegister(remote *Node, topics []Topic, idx int, pong []byte) {
   319  	t.sendPacket(remote.ID, remote.addr(), byte(topicRegisterPacket), topicRegister{
   320  		Topics: topics,
   321  		Idx:    uint(idx),
   322  		Pong:   pong,
   323  	})
   324  }
   325  
   326  func (t *udp) sendTopicNodes(remote *Node, queryHash common.Hash, nodes []*Node) {
   327  	p := topicNodes{Echo: queryHash}
   328  	if len(nodes) == 0 {
   329  		t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p)
   330  		return
   331  	}
   332  	for i, result := range nodes {
   333  		if netutil.CheckRelayIP(remote.IP, result.IP) != nil {
   334  			continue
   335  		}
   336  		p.Nodes = append(p.Nodes, nodeToRPC(result))
   337  		if len(p.Nodes) == maxTopicNodes || i == len(nodes)-1 {
   338  			t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p)
   339  			p.Nodes = p.Nodes[:0]
   340  		}
   341  	}
   342  }
   343  
   344  func (t *udp) sendPacket(toid NodeID, toaddr *net.UDPAddr, ptype byte, req interface{}) (hash []byte, err error) {
   345  	//fmt.Println("sendPacket", nodeEvent(ptype), toaddr.String(), toid.String())
   346  	packet, hash, err := encodePacket(t.priv, ptype, req)
   347  	if err != nil {
   348  		//fmt.Println(err)
   349  		return hash, err
   350  	}
   351  	log.Trace(fmt.Sprintf(">>> %v to %x@%v", nodeEvent(ptype), toid[:8], toaddr))
   352  	if _, err = t.conn.WriteToUDP(packet, toaddr); err != nil {
   353  		log.Trace(fmt.Sprint("UDP send failed:", err))
   354  	}
   355  	//fmt.Println(err)
   356  	return hash, err
   357  }
   358  
   359  // zeroed padding space for encodePacket.
   360  var headSpace = make([]byte, headSize)
   361  
   362  func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) (p, hash []byte, err error) {
   363  	b := new(bytes.Buffer)
   364  	b.Write(headSpace)
   365  	b.WriteByte(ptype)
   366  	if err := rlp.Encode(b, req); err != nil {
   367  		log.Error(fmt.Sprint("error encoding packet:", err))
   368  		return nil, nil, err
   369  	}
   370  	packet := b.Bytes()
   371  	sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
   372  	if err != nil {
   373  		log.Error(fmt.Sprint("could not sign packet:", err))
   374  		return nil, nil, err
   375  	}
   376  	copy(packet[macSize:], sig)
   377  	// add the hash to the front. Note: this doesn't protect the
   378  	// packet in any way.
   379  	hash = crypto.Keccak256(packet[macSize:])
   380  	copy(packet, hash)
   381  	return packet, hash, nil
   382  }
   383  
   384  // readLoop runs in its own goroutine. it injects ingress UDP packets
   385  // into the network loop.
   386  func (t *udp) readLoop() {
   387  	defer t.conn.Close()
   388  	// Discovery packets are defined to be no larger than 1280 bytes.
   389  	// Packets larger than this size will be cut at the end and treated
   390  	// as invalid because their hash won't match.
   391  	buf := make([]byte, 1280)
   392  	for {
   393  		nbytes, from, err := t.conn.ReadFromUDP(buf)
   394  		if netutil.IsTemporaryError(err) {
   395  			// Ignore temporary read errors.
   396  			log.Debug(fmt.Sprintf("Temporary read error: %v", err))
   397  			continue
   398  		} else if err != nil {
   399  			// Shut down the loop for permament errors.
   400  			log.Debug(fmt.Sprintf("Read error: %v", err))
   401  			return
   402  		}
   403  		t.handlePacket(from, buf[:nbytes])
   404  	}
   405  }
   406  
   407  func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error {
   408  	pkt := ingressPacket{remoteAddr: from}
   409  	if err := decodePacket(buf, &pkt); err != nil {
   410  		log.Debug(fmt.Sprintf("Bad packet from %v: %v", from, err))
   411  		//fmt.Println("bad packet", err)
   412  		return err
   413  	}
   414  	t.net.reqReadPacket(pkt)
   415  	return nil
   416  }
   417  
   418  func decodePacket(buffer []byte, pkt *ingressPacket) error {
   419  	if len(buffer) < headSize+1 {
   420  		return errPacketTooSmall
   421  	}
   422  	buf := make([]byte, len(buffer))
   423  	copy(buf, buffer)
   424  	hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
   425  	shouldhash := crypto.Keccak256(buf[macSize:])
   426  	if !bytes.Equal(hash, shouldhash) {
   427  		return errBadHash
   428  	}
   429  	fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig)
   430  	if err != nil {
   431  		return err
   432  	}
   433  	pkt.rawData = buf
   434  	pkt.hash = hash
   435  	pkt.remoteID = fromID
   436  	switch pkt.ev = nodeEvent(sigdata[0]); pkt.ev {
   437  	case pingPacket:
   438  		pkt.data = new(ping)
   439  	case pongPacket:
   440  		pkt.data = new(pong)
   441  	case findnodePacket:
   442  		pkt.data = new(findnode)
   443  	case neighborsPacket:
   444  		pkt.data = new(neighbors)
   445  	case findnodeHashPacket:
   446  		pkt.data = new(findnodeHash)
   447  	case topicRegisterPacket:
   448  		pkt.data = new(topicRegister)
   449  	case topicQueryPacket:
   450  		pkt.data = new(topicQuery)
   451  	case topicNodesPacket:
   452  		pkt.data = new(topicNodes)
   453  	default:
   454  		return fmt.Errorf("unknown packet type: %d", sigdata[0])
   455  	}
   456  	s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
   457  	err = s.Decode(pkt.data)
   458  	return err
   459  }