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: uint16(addr.Port)}
}
TODO use real tpc port
package discover
import (
	"bytes"
	"container/list"
	"errors"
	"fmt"
	"net"
	"time"
	"github.com/ThinkiumGroup/go-common"
	"github.com/ThinkiumGroup/go-common/log"
	"github.com/ThinkiumGroup/go-thinkium/config"
	"github.com/ThinkiumGroup/go-thinkium/network/nat"
	"github.com/stephenfire/go-rtl"
)
var (
	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")
	errClockWarp        = errors.New("reply deadline too far in the future")
	errClosed           = errors.New("socket closed")
	errEmptyTable       = errors.New("empty table")
	errChainID          = errors.New("chain miss match")
	errNetType          = errors.New("net miss match")
	errVersion          = errors.New("version miss match")
)
const (
	pingPacket = iota + 1  
	pongPacket
	findnodePacket
	neighborsPacket
)
const (
	kadVersion = 2000000  
	respTimeout = 500 * time.Millisecond
	expiration  = 20 * time.Second
	ntpFailureThreshold = 32                
	ntpWarningCooldown  = 10 * time.Minute  
	driftThreshold      = 10 * time.Second  
)
const (
	macSize  = 256 / 8
	pubSize  = 520 / 8
	sigSize  = 520 / 8
	headSize = macSize + pubSize + sigSize  
)
var (
	headSpace = make([]byte, headSize)
	maxNeighbors int
)
func init() {
	p := neighbors{Version: kadVersion, ChainID: common.NilChainID, NetType: common.BranchDataNet, Expiration: ^uint64(0)}
	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0), RPC: ^uint16(0), ID: nodeDBNilNodeID}
	for n := 0; ; n++ {
		p.Nodes = append(p.Nodes, maxSizeNode)
		bs, err := rtl.Marshal(p)
		if err != nil {
			panic("cannot encode: " + err.Error())
		}
		if headSize+len(bs)+1 >= 1280 {
			maxNeighbors = n
			break
		}
	}
}
type (
	rpcNode struct {
		IP  net.IP  
		UDP uint16  
		TCP uint16  
		RPC uint16
		ID  common.NodeID
	}
	rpcEndpoint struct {
		IP  net.IP  
		UDP uint16  
		TCP uint16  
		RPC uint16
	}
)
type udp_kad struct {
	conn        *net.UDPConn
	netrestrict *Netlist
	ourEndpoint rpcEndpoint
	addpending chan *pending
	gotreply   chan reply
	closing chan struct{}
	nat     nat.Nat
	*Table
}
type pending struct {
	from  common.NodeID
	ptype byte
	deadline time.Time
	callback func(resp interface{}) (done bool)
	errc chan<- error
}
type reply struct {
	from  common.NodeID
	ptype byte
	data  interface{}
	matched chan<- bool
}
type ReadPacket struct {
	Data []byte
	Addr *net.UDPAddr
}
type UDPConfig struct {
	ChainID common.ChainID
	BootId  common.ChainID
	NetType common.NetType
	AnnounceAddr   *net.UDPAddr  
	NodeDBPath     string        
	Bootnodes      []*Node       
	ChainDataNodes []*ChainDataNodes
}
func SetupDiscoveryKAD(self *Node, c *net.UDPConn, cfg UDPConfig) (Discovery, error) {
	tab, udp, err := setupKADUDP(self, c, cfg)
	if err != nil {
		return nil, err
	}
	log.Infof("KAD UDP setup %s", tab.self)
	return udp, nil
}
func setupKADUDP(self *Node, c *net.UDPConn, cfg UDPConfig) (*Table, *udp_kad, error) {
	udp := &udp_kad{
		conn:       c,
		closing:    make(chan struct{}),
		gotreply:   make(chan reply),
		addpending: make(chan *pending),
	}
	realaddr := c.LocalAddr().(*net.UDPAddr)
	if cfg.AnnounceAddr != nil {
		realaddr = cfg.AnnounceAddr
	}
	udp.ourEndpoint = makeEndpoint(realaddr, self.TCP)
	tab, err := newTable(udp, self, cfg)
	if err != nil {
		return nil, nil, err
	}
	udp.Table = tab
	go tab.loop()
	go udp.loop()
	go udp.readLoop(nil)
	return udp.Table, udp, nil
}
func (t *udp_kad) loop() {
	var (
		plist        = list.New()
		timeout      = time.NewTimer(0)
		nextTimeout  *pending  
		contTimeouts = 0       
		ntpWarnTime  = time.Unix(0, 0)
	)
	<-timeout.C  
	defer timeout.Stop()
	resetTimeout := func() {
		if plist.Front() == nil || nextTimeout == plist.Front().Value {
			return
		}
		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
			}
			nextTimeout.errc <- errClockWarp
			plist.Remove(el)
		}
		nextTimeout = nil
		timeout.Stop()
	}
	for {
		resetTimeout()
		select {
		case <-t.closing:
			for el := plist.Front(); el != nil; el = el.Next() {
				el.Value.(*pending).errc <- errClosed
			}
			return
		case p := <-t.addpending:
			p.deadline = time.Now().Add(respTimeout)
			plist.PushBack(p)
		case r := <-t.gotreply:
			var matched bool
			for el := plist.Front(); el != nil; el = el.Next() {
				p := el.Value.(*pending)
				if p.from == r.from && p.ptype == r.ptype {
					matched = true
					if p.callback(r.data) {
						p.errc <- nil
						plist.Remove(el)
					}
					contTimeouts = 0
				}
			}
			r.matched <- matched
		case now := <-timeout.C:
			nextTimeout = nil
			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)
					contTimeouts++
				}
			}
			if contTimeouts > ntpFailureThreshold {
				if time.Since(ntpWarnTime) >= ntpWarningCooldown {
					ntpWarnTime = time.Now()
					go checkClockDrift()
				}
				contTimeouts = 0
			}
		}
	}
}
func (t *udp_kad) write(toaddr *net.UDPAddr, packet []byte) error {
	_, err := t.conn.WriteToUDP(packet, toaddr)
	return err
}
func (t *udp_kad) readLoop(unhandled chan<- ReadPacket) {
	if unhandled != nil {
		defer close(unhandled)
	}
	buf := make([]byte, 1280)
	for {
		nbytes, from, err := t.conn.ReadFromUDP(buf)
		if err != nil {
			if IsTemporaryError(err) {
				if config.IsLogOn(config.NetDebugLog) {
					log.Debugf("Temporary UDP read error %v", err)
				}
				continue
			}
			if config.IsLogOn(config.NetDebugLog) {
				log.Warnf("UDP read error %v", err)
			}
			return
		}
		if t.HandlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
			select {
			case unhandled <- ReadPacket{buf[:nbytes], from}:
			default:
			}
		}
	}
}
func (t *udp_kad) HandlePacket(from *net.UDPAddr, buf []byte) error {
	packet, fromID, hash, err := decodePacket(buf)
	if err != nil {
		log.Errorf("Bad discover packet addr %s err %v", from, err)
		return err
	}
	return packet.handle(t, from, fromID, hash)
}
func (t *udp_kad) Type() DiscoveryType {
	return KAD
}
func (t *udp_kad) Version() uint32 {
	return kadVersion
}
func (t *udp_kad) NodeTable() DiscoverTable {
	return t.Table
}
func (t *udp_kad) GetChainID(id common.NodeID) (common.ChainID, error) {
	ChainDataNodes := t.GetDataNodes()
	for _, chaindatanode := range ChainDataNodes {
		for _, datanode := range chaindatanode.DataNodes {
			if id == datanode.ID {
				return chaindatanode.ChainId, nil
			}
		}
	}
	return 0, errors.New("The nodeid is not in the datanodes")
}
func (t *udp_kad) Ping(toid common.NodeID, toaddr *net.UDPAddr) error {
	return <-t.SendPing(toid, toaddr, nil)
}
func (t *udp_kad) FindNode(toid common.NodeID, toaddr *net.UDPAddr, target interface{}) (map[common.ChainID][]*Node, error) {
	if t.Table == nil {
		return nil, errEmptyTable
	}
	if time.Since(t.db.lastPingReceived(toid)) > nodeDBNodeExpiration {
		t.Ping(toid, toaddr)
		t.waitping(toid)
	}
	return t.SendFindNode(toid, toaddr, target.(common.NodeID))
}
func (t *udp_kad) Close() error {
	t.Table.Close()
	close(t.closing)
	return t.conn.Close()
}
func (t *udp_kad) Send(toaddr *net.UDPAddr, ptype byte, req packet) ([]byte, error) {
	packet, hash, err := encodePacket(ptype, req)
	if err != nil {
		return hash, err
	}
	return hash, t.write(toaddr, packet)
}
func (t *udp_kad) SendPing(toid common.NodeID, toaddr *net.UDPAddr, callback func()) <-chan error {
	req := &ping{
		Version:    kadVersion,
		ChainID:    t.bootId,
		NetType:    t.netType,
		From:       t.ourEndpoint,
		To:         makeEndpoint(toaddr, 0),  
		Expiration: uint64(time.Now().Add(expiration).Unix()),
	}
	packet, hash, err := encodePacket(pingPacket, req)
	if err != nil {
		errc := make(chan error, 1)
		errc <- err
		return errc
	}
	errc := t.pending(toid, pongPacket, func(p interface{}) bool {
		ok := bytes.Equal(p.(*pong).ReplyTok, hash)
		if ok && callback != nil {
			callback()
		}
		return ok
	})
	t.write(toaddr, packet)
	return errc
}
func (t *udp_kad) SendFindNode(toid common.NodeID, toaddr *net.UDPAddr, target common.NodeID) (map[common.ChainID][]*Node, error) {
	ret := make(map[common.ChainID][]*Node)
	nreceived := 0
	errc := t.pending(toid, neighborsPacket, func(r interface{}) bool {
		reply := r.(*neighbors)
		for _, rn := range reply.Nodes {
			nreceived++
			n, err := t.nodeFromRPC(toaddr, rn)
			if err != nil {
				continue
			}
			ret[t.bootId] = append(ret[t.bootId], n)
		}
		return nreceived >= bucketSize
	})
	_, err := t.Send(toaddr, findnodePacket, &findnode{
		Version:    kadVersion,
		ChainID:    t.bootId,
		NetType:    t.netType,
		Target:     target,
		Expiration: uint64(time.Now().Add(expiration).Unix()),
	})
	if err != nil {
		return ret, err
	}
	return ret, <-errc
}
func (t *udp_kad) waitping(from common.NodeID) error {
	return <-t.pending(from, pingPacket, func(interface{}) bool { return true })
}
func (t *udp_kad) pending(id common.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:
	case <-t.closing:
		ch <- errClosed
	}
	return ch
}
func (t *udp_kad) handleReply(from common.NodeID, ptype byte, req packet) bool {
	matched := make(chan bool, 1)
	select {
	case t.gotreply <- reply{from, ptype, req, matched}:
		return <-matched
	case <-t.closing:
		return false
	}
}
func encodePacket(ptype byte, req interface{}) (packet, hash []byte, err error) {
	b := new(bytes.Buffer)
	b.Write(headSpace)
	b.WriteByte(ptype)
	if err := rtl.Encode(req, b); err != nil {
		log.Error("Can't encode packet", "err", err)
		return nil, nil, err
	}
	packet = b.Bytes()
	pub, sig, err := common.SignHash(common.SystemHash256(packet[headSize:]))
	if err != nil {
		log.Error("Can't sign packet", "err", err)
		return nil, nil, err
	}
	copy(packet[macSize:], pub)
	copy(packet[macSize+pubSize:], sig)
	hash = common.SystemHash256(packet[macSize:])
	copy(packet, hash)
	return packet, hash, nil
}
func decodePacket(buf []byte) (packet, common.NodeID, []byte, error) {
	if len(buf) < headSize+1 {
		return nil, common.NodeID{}, nil, errPacketTooSmall
	}
	hash, pub, sig, sigdata := buf[:macSize], buf[macSize:macSize+pubSize], buf[headSize-sigSize:headSize], buf[headSize:]
	shouldhash := common.SystemHash256(buf[macSize:])
	if !bytes.Equal(hash, shouldhash) {
		return nil, common.NodeID{}, nil, errBadHash
	}
	if !common.VerifyHash(common.SystemHash256(buf[headSize:]), pub, sig) {
		return nil, common.NodeID{}, hash, errors.New("hash signature verify failed")
	}
	pubkey, err := common.RealCipher.BytesToPub(pub)
	if err != nil {
		return nil, common.NodeID{}, nil, err
	}
	fromID := common.BytesToNodeID(common.RealCipher.PubToBytes(pubkey))
	var req packet
	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:
		return nil, fromID, hash, fmt.Errorf("unknown type: %d", ptype)
	}
	err = rtl.Decode(bytes.NewReader(sigdata[1:]), req)
	return req, fromID, hash, err
}