以太坊原始碼分析(50)p2p-udp.go原始碼分析

尹成發表於2018-05-14
p2p的網路發現協議使用了Kademlia protocol 來處理網路的節點發現。節點查詢和節點更新。Kademlia protocol使用了UDP協議來進行網路通訊。

閱讀這部分的程式碼建議先看看references裡面的Kademlia協議簡介來看看什麼是Kademlia協議。

首先看看資料結構。 網路傳輸了4種資料包(UDP協議是基於報文的協議。傳輸的是一個一個資料包),分別是ping,pong,findnode和neighbors。 下面分別定義了4種報文的格式。


    // RPC packet types
    const (
        pingPacket = iota + 1 // zero is 'reserved'
        pongPacket
        findnodePacket
        neighborsPacket
    )
    // RPC request structures
    type (
        ping struct {
            Version uint //協議版本
            From, To rpcEndpoint      //源IP地址 目的IP地址
            Expiration uint64           //超時時間
            // Ignore additional fields (for forward compatibility).
            //可以忽略的欄位。 為了向前相容
            Rest []rlp.RawValue `rlp:"tail"`
        }
    
        // pong is the reply to ping.
        // ping包的迴應
        pong struct {
            // This field should mirror the UDP envelope address
            // of the ping packet, which provides a way to discover the
            // the external address (after NAT).
            // 目的IP地址
            To rpcEndpoint
            // 說明這個pong包是迴應那個ping包的。 包含了ping包的hash值
            ReplyTok []byte // This contains the hash of the ping packet.
            //包超時的絕對時間。 如果收到包的時候超過了這個時間,那麼包被認為是超時的。
            Expiration uint64 // Absolute timestamp at which the packet becomes invalid.
            // Ignore additional fields (for forward compatibility).
            Rest []rlp.RawValue `rlp:"tail"`
        }
        // findnode 是用來查詢距離target比較近的節點
        // findnode is a query for nodes close to the given target.
        findnode struct {
            // 目的節點
            Target NodeID // doesn't need to be an actual public key
            Expiration uint64
            // Ignore additional fields (for forward compatibility).
            Rest []rlp.RawValue `rlp:"tail"`
        }
    
        // reply to findnode
        // findnode的迴應
        neighbors struct {
            //距離target比較近的節點值。
            Nodes []rpcNode
            Expiration uint64
            // Ignore additional fields (for forward compatibility).
            Rest []rlp.RawValue `rlp:"tail"`
        }
    
        rpcNode struct {
            IP net.IP // len 4 for IPv4 or 16 for IPv6
            UDP uint16 // for discovery protocol
            TCP uint16 // for RLPx protocol
            ID NodeID
        }
    
        rpcEndpoint struct {
            IP net.IP // len 4 for IPv4 or 16 for IPv6
            UDP uint16 // for discovery protocol
            TCP uint16 // for RLPx protocol
        }
    )


定義了兩個介面型別,packet介面型別應該是給4種不同型別的包分派不同的handle方法。 conn介面定義了一個udp的連線的功能。


    type packet interface {
        handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error
        name() string
    }
    
    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
    }


udp的結構, 需要注意的是最後一個欄位*Table是go裡面的匿名欄位。 也就是說udp可以直接呼叫匿名欄位Table的方法。


    // udp implements the RPC protocol.
    type udp struct {
        conn conn                    //網路連線
        netrestrict *netutil.Netlist
        priv *ecdsa.PrivateKey       //私鑰,自己的ID是通過這個來生成的。
        ourEndpoint rpcEndpoint
    
        addpending chan *pending            //用來申請一個pending
        gotreply chan reply               //用來獲取迴應的佇列
    
        closing chan struct{}               //用來關閉的佇列
        nat nat.Interface               
    
        *Table
    }



pending 和reply 結構。 這兩個結構使用者內部的go routine之間進行通訊的結構體。


    // 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.
    // pending結構 代表正在等待一個reply
    // 我們通過為每一個pending reply 儲存一個callback來實現這個功能。從一個節點來的所有資料包都會分配到這個節點對應的callback上面。
    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.
        //如果返回值是true。那麼callback會從佇列裡面移除。 如果返回false,那麼認為reply還沒有完成,會繼續等待下一次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{}
        // loop indicates whether there was
        // a matching request by sending on this channel.
        //通過往這個channel上面傳送訊息來表示匹配到一個請求。
        matched chan<- bool
    }


UDP的建立

    // ListenUDP returns a new table that listens for UDP packets on laddr.
    func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) {
        addr, err := net.ResolveUDPAddr("udp", laddr)
        if err != nil {
            return nil, err
        }
        conn, err := net.ListenUDP("udp", addr)
        if err != nil {
            return nil, err
        }
        tab, _, err := newUDP(priv, conn, natm, nodeDBPath, netrestrict)
        if err != nil {
            return nil, err
        }
        log.Info("UDP listener up", "self", tab.self)
        return tab, nil
    }
    
    func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) {
        udp := &udp{
            conn: c,
            priv: priv,
            netrestrict: netrestrict,
            closing: make(chan struct{}),
            gotreply: make(chan reply),
            addpending: make(chan *pending),
        }
        realaddr := c.LocalAddr().(*net.UDPAddr)
        if natm != nil { //natm nat mapping 用來獲取外網地址
            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}
            }
        }
        // TODO: separate TCP port
        udp.ourEndpoint = makeEndpoint(realaddr, uint16(realaddr.Port))
        //建立一個table 後續會介紹。 Kademlia的主要邏輯在這個類裡面實現。
        tab, err := newTable(udp, PubkeyID(&priv.PublicKey), realaddr, nodeDBPath)
        if err != nil {
            return nil, nil, err
        }
        udp.Table = tab //匿名欄位的賦值
        
        go udp.loop()       //go routine
        go udp.readLoop()   //用來網路資料讀取。
        return udp.Table, udp, nil
    }

ping方法與pending的處理,之前談到了pending是等待一個reply。 這裡通過程式碼來分析是如何實現等待reply的。

pending方法把pending結構體傳送給addpending. 然後等待訊息的處理和接收。

    // ping sends a ping message to the given node and waits for a reply.
    func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error {
        // TODO: maybe check for ReplyTo field in callback to measure RTT
        errc := t.pending(toid, pongPacket, func(interface{}) bool { return true })
        t.send(toaddr, pingPacket, &ping{
            Version: Version,
            From: t.ourEndpoint,
            To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB
            Expiration: uint64(time.Now().Add(expiration).Unix()),
        })
        return <-errc
    }
    // 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
    }

addpending訊息的處理。 之前建立udp的時候呼叫了newUDP方法。裡面啟動了兩個goroutine。 其中的loop()就是用來處理pending訊息的。


    // loop runs in its own goroutine. it keeps track of
    // the refresh timer and the pending reply queue.
    func (t *udp) loop() {
        var (
            plist = list.New()
            timeout = time.NewTimer(0)
            nextTimeout *pending // head of plist when timeout was last reset
            contTimeouts = 0 // number of continuous timeouts to do NTP checks
            ntpWarnTime = time.Unix(0, 0)
        )
        <-timeout.C // ignore first timeout
        defer timeout.Stop()
    
        resetTimeout := func() {
            //這個方法的主要功能是檢視佇列裡面是否有需要超時的pending訊息。 如果有。那麼
            //根據最先超時的時間設定超時醒來。
            if plist.Front() == nil || nextTimeout == plist.Front().Value {
                return
            }
            // Start the timer so it fires when the next pending reply has expired.
            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
                }
                // Remove pending replies whose deadline is too far in the
                // future. These can occur if the system clock jumped
                // backwards after the deadline was assigned.
                //如果有訊息的deadline在很遠的未來,那麼直接設定超時,然後移除。
                //這種情況在修改系統時間的時候有可能發生,如果不處理可能導致堵塞太長時間。
                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: //增加一個pending 設定deadline
                p.deadline = time.Now().Add(respTimeout)
                plist.PushBack(p)
    
            case r := <-t.gotreply: //收到一個reply 尋找匹配的pending
                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
                        // Remove the matcher if its callback indicates
                        // that all replies have been received. This is
                        // required for packet types that expect multiple
                        // reply packets.
                        if p.callback(r.data) { //如果callback返回值是true 。說明pending已經完成。 給p.errc寫入nil。 pending完成。
                            p.errc <- nil
                            plist.Remove(el)
                        }
                        // Reset the continuous timeout counter (time drift detection)
                        contTimeouts = 0
                    }
                }
                r.matched <- matched //寫入reply的matched
    
            case now := <-timeout.C: //處理超時資訊
                nextTimeout = nil
    
                // Notify and remove callbacks whose deadline is in the past.
                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 we've accumulated too many timeouts, do an NTP time sync check
                if contTimeouts > ntpFailureThreshold {
                    //如果連續超時很多次。 那麼檢視是否是時間不同步。 和NTP伺服器進行同步。
                    if time.Since(ntpWarnTime) >= ntpWarningCooldown {
                        ntpWarnTime = time.Now()
                        go checkClockDrift()
                    }
                    contTimeouts = 0
                }
            }
        }
    }

上面看到了pending的處理。 不過loop()方法種還有一個gotreply的處理。 這個實在readLoop()這個goroutine中產生的。

    // readLoop runs in its own goroutine. it handles incoming UDP packets.
    func (t *udp) readLoop() {
        defer t.conn.Close()
        // Discovery packets are defined to be no larger than 1280 bytes.
        // Packets larger than this size will be cut at the end and treated
        // as invalid because their hash won't match.
        buf := make([]byte, 1280)
        for {
            nbytes, from, err := t.conn.ReadFromUDP(buf)
            if netutil.IsTemporaryError(err) {
                // Ignore temporary read errors.
                log.Debug("Temporary UDP read error", "err", err)
                continue
            } else if err != nil {
                // Shut down the loop for permament errors.
                log.Debug("UDP read error", "err", err)
                return
            }
            t.handlePacket(from, buf[:nbytes])
        }
    }

    func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error {
        packet, fromID, hash, err := decodePacket(buf)
        if err != nil {
            log.Debug("Bad discv4 packet", "addr", from, "err", err)
            return err
        }
        err = packet.handle(t, from, fromID, hash)
        log.Trace("<< "+packet.name(), "addr", from, "err", err)
        return err
    }
    
    func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
        if expired(req.Expiration) {
            return errExpired
        }
        t.send(from, pongPacket, &pong{
            To: makeEndpoint(from, req.From.TCP),
            ReplyTok: mac,
            Expiration: uint64(time.Now().Add(expiration).Unix()),
        })
        if !t.handleReply(fromID, pingPacket, req) {
            // Note: we're ignoring the provided IP address right now
            go t.bond(true, fromID, from, req.From.TCP)
        }
        return nil
    }
    
    func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool {
        matched := make(chan bool, 1)
        select {
        case t.gotreply <- reply{from, ptype, req, matched}:
            // loop will handle it
            return <-matched
        case <-t.closing:
            return false
        }
    }


上面介紹了udp的大致處理的流程。 下面介紹下udp的主要處理的業務。 udp主要傳送兩種請求,對應的也會接收別人傳送的這兩種請求, 對應這兩種請求又會產生兩種迴應。

ping請求,可以看到ping請求希望得到一個pong回答。 然後返回。

    // ping sends a ping message to the given node and waits for a reply.
    func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error {
        // TODO: maybe check for ReplyTo field in callback to measure RTT
        errc := t.pending(toid, pongPacket, func(interface{}) bool { return true })
        t.send(toaddr, pingPacket, &ping{
            Version: Version,
            From: t.ourEndpoint,
            To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB
            Expiration: uint64(time.Now().Add(expiration).Unix()),
        })
        return <-errc
    }

pong回答,如果pong回答沒有匹配到一個對應的ping請求。那麼返回errUnsolicitedReply異常。

    func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
        if expired(req.Expiration) {
            return errExpired
        }
        if !t.handleReply(fromID, pongPacket, req) {
            return errUnsolicitedReply
        }
        return nil
    }

findnode請求, 傳送findnode請求,然後等待node迴應 k個鄰居。

    // findnode sends a findnode request to the given node and waits until
    // the node has sent up to k neighbors.
    func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
        nodes := make([]*Node, 0, bucketSize)
        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 {
                    log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err)
                    continue
                }
                nodes = append(nodes, n)
            }
            return nreceived >= bucketSize
        })
        t.send(toaddr, findnodePacket, &findnode{
            Target: target,
            Expiration: uint64(time.Now().Add(expiration).Unix()),
        })
        err := <-errc
        return nodes, err
    }

neighbors迴應, 很簡單。 把迴應傳送給gotreply佇列。 如果沒有找到匹配的findnode請求。返回errUnsolicitedReply錯誤

    func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
        if expired(req.Expiration) {
            return errExpired
        }
        if !t.handleReply(fromID, neighborsPacket, req) {
            return errUnsolicitedReply
        }
        return nil
    }



收到別的節點傳送的ping請求,傳送pong回答。 如果沒有匹配上一個pending(說明不是自己方請求的結果)。 就呼叫bond方法把這個節點加入自己的bucket快取。(這部分原理在table.go裡面會詳細介紹)

    func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
        if expired(req.Expiration) {
            return errExpired
        }
        t.send(from, pongPacket, &pong{
            To: makeEndpoint(from, req.From.TCP),
            ReplyTok: mac,
            Expiration: uint64(time.Now().Add(expiration).Unix()),
        })
        if !t.handleReply(fromID, pingPacket, req) {
            // Note: we're ignoring the provided IP address right now
            go t.bond(true, fromID, from, req.From.TCP)
        }
        return nil
    }

收到別人傳送的findnode請求。這個請求希望把和target距離相近的k個節點傳送回去。 演算法的詳細請參考references目錄下面的pdf文件。

    
    func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error {
        if expired(req.Expiration) {
            return errExpired
        }
        if t.db.node(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
        }
        target := crypto.Keccak256Hash(req.Target[:])
        t.mutex.Lock()
        //獲取bucketSize個和target距離相近的節點。 這個方法在table.go內部實現。後續會詳細介紹
        closest := t.closest(target, bucketSize).entries
        t.mutex.Unlock()
    
        p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
        // Send neighbors in chunks with at most maxNeighbors per packet
        // to stay below the 1280 byte limit.
        for i, n := range closest {
            if netutil.CheckRelayIP(from.IP, n.IP) != nil {
                continue
            }
            p.Nodes = append(p.Nodes, nodeToRPC(n))
            if len(p.Nodes) == maxNeighbors || i == len(closest)-1 {
                t.send(from, neighborsPacket, &p)
                p.Nodes = p.Nodes[:0]
            }
        }
        return nil
    }


### udp資訊加密和安全問題
discover協議因為沒有承載什麼敏感資料,所以資料是以明文傳輸,但是為了確保資料的完整性和不被篡改,所以在資料包的包頭加上了數字簽名。

    
    func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, error) {
        b := new(bytes.Buffer)
        b.Write(headSpace)
        b.WriteByte(ptype)
        if err := rlp.Encode(b, req); err != nil {
            log.Error("Can't encode discv4 packet", "err", err)
            return nil, err
        }
        packet := b.Bytes()
        sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv)
        if err != nil {
            log.Error("Can't sign discv4 packet", "err", err)
            return nil, err
        }
        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
        // The future.
        copy(packet, crypto.Keccak256(packet[macSize:]))
        return packet, nil
    }

    func decodePacket(buf []byte) (packet, NodeID, []byte, error) {
        if len(buf) < headSize+1 {
            return nil, NodeID{}, nil, errPacketTooSmall
        }
        hash, sig, sigdata := buf[:macSize], buf[macSize:headSize], buf[headSize:]
        shouldhash := crypto.Keccak256(buf[macSize:])
        if !bytes.Equal(hash, shouldhash) {
            return nil, NodeID{}, nil, errBadHash
        }
        fromID, err := recoverNodeID(crypto.Keccak256(buf[headSize:]), sig)
        if err != nil {
            return nil, NodeID{}, hash, err
        }
        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)
        }
        s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
        err = s.Decode(req)
        return req, fromID, hash, err
    }




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN



相關文章