p2p/discover: add version number to ping packet
The primary motivation for doing this right now is that old PoC 8 nodes and newer PoC 9 nodes keep discovering each other, causing handshake failures.
This commit is contained in:
		@@ -16,11 +16,14 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var log = logger.NewLogger("P2P Discovery")
 | 
					var log = logger.NewLogger("P2P Discovery")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Version = 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Errors
 | 
					// Errors
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	errPacketTooSmall = errors.New("too small")
 | 
						errPacketTooSmall = errors.New("too small")
 | 
				
			||||||
	errBadHash        = errors.New("bad hash")
 | 
						errBadHash        = errors.New("bad hash")
 | 
				
			||||||
	errExpired        = errors.New("expired")
 | 
						errExpired        = errors.New("expired")
 | 
				
			||||||
 | 
						errBadVersion     = errors.New("version mismatch")
 | 
				
			||||||
	errTimeout        = errors.New("RPC timeout")
 | 
						errTimeout        = errors.New("RPC timeout")
 | 
				
			||||||
	errClosed         = errors.New("socket closed")
 | 
						errClosed         = errors.New("socket closed")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -45,6 +48,7 @@ const (
 | 
				
			|||||||
// RPC request structures
 | 
					// RPC request structures
 | 
				
			||||||
type (
 | 
					type (
 | 
				
			||||||
	ping struct {
 | 
						ping struct {
 | 
				
			||||||
 | 
							Version    uint   // must match Version
 | 
				
			||||||
		IP         string // our IP
 | 
							IP         string // our IP
 | 
				
			||||||
		Port       uint16 // our port
 | 
							Port       uint16 // our port
 | 
				
			||||||
		Expiration uint64
 | 
							Expiration uint64
 | 
				
			||||||
@@ -169,6 +173,7 @@ func (t *udp) ping(e *Node) error {
 | 
				
			|||||||
	// TODO: maybe check for ReplyTo field in callback to measure RTT
 | 
						// TODO: maybe check for ReplyTo field in callback to measure RTT
 | 
				
			||||||
	errc := t.pending(e.ID, pongPacket, func(interface{}) bool { return true })
 | 
						errc := t.pending(e.ID, pongPacket, func(interface{}) bool { return true })
 | 
				
			||||||
	t.send(e, pingPacket, ping{
 | 
						t.send(e, pingPacket, ping{
 | 
				
			||||||
 | 
							Version:    Version,
 | 
				
			||||||
		IP:         t.self.IP.String(),
 | 
							IP:         t.self.IP.String(),
 | 
				
			||||||
		Port:       uint16(t.self.TCPPort),
 | 
							Port:       uint16(t.self.TCPPort),
 | 
				
			||||||
		Expiration: uint64(time.Now().Add(expiration).Unix()),
 | 
							Expiration: uint64(time.Now().Add(expiration).Unix()),
 | 
				
			||||||
@@ -371,6 +376,9 @@ func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) er
 | 
				
			|||||||
	if expired(req.Expiration) {
 | 
						if expired(req.Expiration) {
 | 
				
			||||||
		return errExpired
 | 
							return errExpired
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if req.Version != Version {
 | 
				
			||||||
 | 
							return errBadVersion
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	t.mutex.Lock()
 | 
						t.mutex.Lock()
 | 
				
			||||||
	// Note: we're ignoring the provided IP address right now
 | 
						// Note: we're ignoring the provided IP address right now
 | 
				
			||||||
	n := t.bumpOrAdd(fromID, from)
 | 
						n := t.bumpOrAdd(fromID, from)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user