github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/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  	errBadPrefix        = errors.New("bad prefix")
    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  	queryDelay  = 1000 * 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  var (
   149  	versionPrefix     = []byte("temporary discovery v5")
   150  	versionPrefixSize = len(versionPrefix)
   151  	sigSize           = 520 / 8
   152  	headSize          = versionPrefixSize + sigSize // space of packet frame data
   153  )
   154  
   155  // Neighbors replies are sent across multiple packets to
   156  // stay below the 1280 byte limit. We compute the maximum number
   157  // of entries by stuffing a packet until it grows too large.
   158  var maxNeighbors = func() int {
   159  	p := neighbors{Expiration: ^uint64(0)}
   160  	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)}
   161  	for n := 0; ; n++ {
   162  		p.Nodes = append(p.Nodes, maxSizeNode)
   163  		size, _, err := rlp.EncodeToReader(p)
   164  		if err != nil {
   165  			// If this ever happens, it will be caught by the unit tests.
   166  			panic("cannot encode: " + err.Error())
   167  		}
   168  		if headSize+size+1 >= 1280 {
   169  			return n
   170  		}
   171  	}
   172  }()
   173  
   174  var maxTopicNodes = func() int {
   175  	p := topicNodes{}
   176  	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0)}
   177  	for n := 0; ; n++ {
   178  		p.Nodes = append(p.Nodes, maxSizeNode)
   179  		size, _, err := rlp.EncodeToReader(p)
   180  		if err != nil {
   181  			// If this ever happens, it will be caught by the unit tests.
   182  			panic("cannot encode: " + err.Error())
   183  		}
   184  		if headSize+size+1 >= 1280 {
   185  			return n
   186  		}
   187  	}
   188  }()
   189  
   190  func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
   191  	ip := addr.IP.To4()
   192  	if ip == nil {
   193  		ip = addr.IP.To16()
   194  	}
   195  	return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
   196  }
   197  
   198  func (e1 rpcEndpoint) equal(e2 rpcEndpoint) bool {
   199  	return e1.UDP == e2.UDP && e1.TCP == e2.TCP && e1.IP.Equal(e2.IP)
   200  }
   201  
   202  func nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) {
   203  	if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
   204  		return nil, err
   205  	}
   206  	n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP)
   207  	err := n.validateComplete()
   208  	return n, err
   209  }
   210  
   211  func nodeToRPC(n *Node) rpcNode {
   212  	return rpcNode{ID: n.ID, IP: n.IP, UDP: n.UDP, TCP: n.TCP}
   213  }
   214  
   215  type ingressPacket struct {
   216  	remoteID   NodeID
   217  	remoteAddr *net.UDPAddr
   218  	ev         nodeEvent
   219  	hash       []byte
   220  	data       interface{} // one of the RPC structs
   221  	rawData    []byte
   222  }
   223  
   224  type conn interface {
   225  	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
   226  	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
   227  	Close() error
   228  	LocalAddr() net.Addr
   229  }
   230  
   231  // udp implements the RPC protocol.
   232  type udp struct {
   233  	conn        conn
   234  	priv        *ecdsa.PrivateKey
   235  	ourEndpoint rpcEndpoint
   236  	nat         nat.Interface
   237  	net         *Network
   238  }
   239  
   240  // ListenUDP returns a new table that listens for UDP packets on laddr.
   241  func ListenUDP(priv *ecdsa.PrivateKey, conn conn, realaddr *net.UDPAddr, nodeDBPath string, netrestrict *netutil.Netlist) (*Network, error) {
   242  	transport, err := listenUDP(priv, conn, realaddr)
   243  	if err != nil {
   244  		return nil, err
   245  	}
   246  	net, err := newNetwork(transport, priv.PublicKey, nodeDBPath, netrestrict)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  	log.Info("UDP listener up", "net", net.tab.self)
   251  	transport.net = net
   252  	go transport.readLoop()
   253  	return net, nil
   254  }
   255  
   256  func listenUDP(priv *ecdsa.PrivateKey, conn conn, realaddr *net.UDPAddr) (*udp, error) {
   257  	return &udp{conn: conn, priv: priv, ourEndpoint: makeEndpoint(realaddr, uint16(realaddr.Port))}, nil
   258  }
   259  
   260  func (t *udp) localAddr() *net.UDPAddr {
   261  	return t.conn.LocalAddr().(*net.UDPAddr)
   262  }
   263  
   264  func (t *udp) Close() {
   265  	t.conn.Close()
   266  }
   267  
   268  func (t *udp) send(remote *Node, ptype nodeEvent, data interface{}) (hash []byte) {
   269  	hash, _ = t.sendPacket(remote.ID, remote.addr(), byte(ptype), data)
   270  	return hash
   271  }
   272  
   273  func (t *udp) sendPing(remote *Node, toaddr *net.UDPAddr, topics []Topic) (hash []byte) {
   274  	hash, _ = t.sendPacket(remote.ID, toaddr, byte(pingPacket), ping{
   275  		Version:    Version,
   276  		From:       t.ourEndpoint,
   277  		To:         makeEndpoint(toaddr, uint16(toaddr.Port)), // TODO: maybe use known TCP port from DB
   278  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   279  		Topics:     topics,
   280  	})
   281  	return hash
   282  }
   283  
   284  func (t *udp) sendFindnode(remote *Node, target NodeID) {
   285  	t.sendPacket(remote.ID, remote.addr(), byte(findnodePacket), findnode{
   286  		Target:     target,
   287  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   288  	})
   289  }
   290  
   291  func (t *udp) sendNeighbours(remote *Node, results []*Node) {
   292  	// Send neighbors in chunks with at most maxNeighbors per packet
   293  	// to stay below the 1280 byte limit.
   294  	p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
   295  	for i, result := range results {
   296  		p.Nodes = append(p.Nodes, nodeToRPC(result))
   297  		if len(p.Nodes) == maxNeighbors || i == len(results)-1 {
   298  			t.sendPacket(remote.ID, remote.addr(), byte(neighborsPacket), p)
   299  			p.Nodes = p.Nodes[:0]
   300  		}
   301  	}
   302  }
   303  
   304  func (t *udp) sendFindnodeHash(remote *Node, target common.Hash) {
   305  	t.sendPacket(remote.ID, remote.addr(), byte(findnodeHashPacket), findnodeHash{
   306  		Target:     target,
   307  		Expiration: uint64(time.Now().Add(expiration).Unix()),
   308  	})
   309  }
   310  
   311  func (t *udp) sendTopicRegister(remote *Node, topics []Topic, idx int, pong []byte) {
   312  	t.sendPacket(remote.ID, remote.addr(), byte(topicRegisterPacket), topicRegister{
   313  		Topics: topics,
   314  		Idx:    uint(idx),
   315  		Pong:   pong,
   316  	})
   317  }
   318  
   319  func (t *udp) sendTopicNodes(remote *Node, queryHash common.Hash, nodes []*Node) {
   320  	p := topicNodes{Echo: queryHash}
   321  	var sent bool
   322  	for _, result := range nodes {
   323  		if result.IP.Equal(t.net.tab.self.IP) || netutil.CheckRelayIP(remote.IP, result.IP) == nil {
   324  			p.Nodes = append(p.Nodes, nodeToRPC(result))
   325  		}
   326  		if len(p.Nodes) == maxTopicNodes {
   327  			t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p)
   328  			p.Nodes = p.Nodes[:0]
   329  			sent = true
   330  		}
   331  	}
   332  	if !sent || len(p.Nodes) > 0 {
   333  		t.sendPacket(remote.ID, remote.addr(), byte(topicNodesPacket), p)
   334  	}
   335  }
   336  
   337  func (t *udp) sendPacket(toid NodeID, toaddr *net.UDPAddr, ptype byte, req interface{}) (hash []byte, err error) {
   338  	//fmt.Println("sendPacket", nodeEvent(ptype), toaddr.String(), toid.String())
   339  	packet, hash, err := encodePacket(t.priv, ptype, req)
   340  	if err != nil {
   341  		//fmt.Println(err)
   342  		return hash, err
   343  	}
   344  	log.Trace(fmt.Sprintf(">>> %v to %x@%v", nodeEvent(ptype), toid[:8], toaddr))
   345  	if _, err = t.conn.WriteToUDP(packet, toaddr); err != nil {
   346  		log.Trace(fmt.Sprint("UDP send failed:", err))
   347  	}
   348  	//fmt.Println(err)
   349  	return hash, err
   350  }
   351  
   352  // zeroed padding space for encodePacket.
   353  var headSpace = make([]byte, headSize)
   354  
   355  func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) (p, hash []byte, err error) {
   356  	b := new(bytes.Buffer)
   357  	b.Write(headSpace)
   358  	b.WriteByte(ptype)
   359  	if err := rlp.Encode(b, req); err != nil {
   360  		log.Error(fmt.Sprint("error encoding packet:", err))
   361  		return nil, nil, err
   362  	}
   363  	packet := b.Bytes()
   364  	sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
   365  	if err != nil {
   366  		log.Error(fmt.Sprint("could not sign packet:", err))
   367  		return nil, nil, err
   368  	}
   369  	copy(packet, versionPrefix)
   370  	copy(packet[versionPrefixSize:], sig)
   371  	hash = crypto.Keccak256(packet[versionPrefixSize:])
   372  	return packet, hash, nil
   373  }
   374  
   375  // readLoop runs in its own goroutine. it injects ingress UDP packets
   376  // into the network loop.
   377  func (t *udp) readLoop() {
   378  	defer t.conn.Close()
   379  	// Discovery packets are defined to be no larger than 1280 bytes.
   380  	// Packets larger than this size will be cut at the end and treated
   381  	// as invalid because their hash won't match.
   382  	buf := make([]byte, 1280)
   383  	for {
   384  		nbytes, from, err := t.conn.ReadFromUDP(buf)
   385  		if netutil.IsTemporaryError(err) {
   386  			// Ignore temporary read errors.
   387  			log.Debug(fmt.Sprintf("Temporary read error: %v", err))
   388  			continue
   389  		} else if err != nil {
   390  			// Shut down the loop for permament errors.
   391  			log.Debug(fmt.Sprintf("Read error: %v", err))
   392  			return
   393  		}
   394  		t.handlePacket(from, buf[:nbytes])
   395  	}
   396  }
   397  
   398  func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error {
   399  	pkt := ingressPacket{remoteAddr: from}
   400  	if err := decodePacket(buf, &pkt); err != nil {
   401  		log.Debug(fmt.Sprintf("Bad packet from %v: %v", from, err))
   402  		//fmt.Println("bad packet", err)
   403  		return err
   404  	}
   405  	t.net.reqReadPacket(pkt)
   406  	return nil
   407  }
   408  
   409  func decodePacket(buffer []byte, pkt *ingressPacket) error {
   410  	if len(buffer) < headSize+1 {
   411  		return errPacketTooSmall
   412  	}
   413  	buf := make([]byte, len(buffer))
   414  	copy(buf, buffer)
   415  	prefix, sig, sigdata := buf[:versionPrefixSize], buf[versionPrefixSize:headSize], buf[headSize:]
   416  	if !bytes.Equal(prefix, versionPrefix) {
   417  		return errBadPrefix
   418  	}
   419  	fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig)
   420  	if err != nil {
   421  		return err
   422  	}
   423  	pkt.rawData = buf
   424  	pkt.hash = crypto.Keccak256(buf[versionPrefixSize:])
   425  	pkt.remoteID = fromID
   426  	switch pkt.ev = nodeEvent(sigdata[0]); pkt.ev {
   427  	case pingPacket:
   428  		pkt.data = new(ping)
   429  	case pongPacket:
   430  		pkt.data = new(pong)
   431  	case findnodePacket:
   432  		pkt.data = new(findnode)
   433  	case neighborsPacket:
   434  		pkt.data = new(neighbors)
   435  	case findnodeHashPacket:
   436  		pkt.data = new(findnodeHash)
   437  	case topicRegisterPacket:
   438  		pkt.data = new(topicRegister)
   439  	case topicQueryPacket:
   440  		pkt.data = new(topicQuery)
   441  	case topicNodesPacket:
   442  		pkt.data = new(topicNodes)
   443  	default:
   444  		return fmt.Errorf("unknown packet type: %d", sigdata[0])
   445  	}
   446  	s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
   447  	err = s.Decode(pkt.data)
   448  	return err
   449  }