当前位置: 首页>>代码示例>>Golang>>正文


Golang Node.FindPeer方法代码示例

本文整理汇总了Golang中github.com/jddixon/xlNode_go.Node.FindPeer方法的典型用法代码示例。如果您正苦于以下问题:Golang Node.FindPeer方法的具体用法?Golang Node.FindPeer怎么用?Golang Node.FindPeer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jddixon/xlNode_go.Node的用法示例。


在下文中一共展示了Node.FindPeer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handleHello

func (h *InHandler) handleHello(n *xn.Node) (err error) {
	var (
		m                     *XLatticeMsg
		msgN                  uint64
		id, ck, sk, sig, salt []byte
		peer                  *xn.Peer
	)
	m, err = h.readMsg()

	// message must be a Hello
	if err == nil {
		if m.GetOp() != XLatticeMsg_Hello {
			err = MissingHello
		}
	}
	if err == nil {
		//  the message is a hello; is its NodeID that of a known peer?
		id = m.GetID()
		if id == nil {
			// DEBUG
			fmt.Printf("handleHello: message has no ID field\n")
			// END
			err = NilPeer
		} else {
			peer, err = n.FindPeer(id)
			if err == nil {
				if peer == nil {
					err = xn.NotAKnownPeer
				} else {
					h.Peer = peer
				}
			}
		}
	}

	// On any error up to here silently close the connection and delete
	// the handler.
	if err != nil {
		h.Cnx.Close()
		h = nil
		return
	}
	// message is a hello from a known peer -------------------------

	// MsgN must be 1
	msgN = m.GetMsgN()
	h.MsgN = msgN
	ck = m.GetCommsKey() // comms key as byte slice
	sk = m.GetSigKey()   // sig key as byte slice
	salt = m.GetSalt()
	sig = m.GetSig() // digital signature

	var serCK, serSK []byte

	if h.MsgN != 1 {
		err = ExpectedMsgOne
	}
	if err == nil {
		peerID := peer.GetNodeID().Value()
		if !bytes.Equal(id, peerID) {
			fmt.Println("NOT SAME NODE ID") // XXX should log
			err = NotExpectedNodeID
		}
	}
	if err == nil {
		serCK, err = xc.RSAPubKeyToWire(peer.GetCommsPublicKey())
		if err == nil {
			if !bytes.Equal(serCK, ck) {
				fmt.Println("NOT SAME COMMS KEY") // XXX should log
				err = NotExpectedCommsKey
			}
		}
	}
	if err == nil {
		serSK, err = xc.RSAPubKeyToWire(peer.GetSigPublicKey())
		if err == nil {
			if !bytes.Equal(serSK, sk) {
				fmt.Println("NOT SAME SIG KEY") // XXX should log
				err = NotExpectedSigKey
			}
		}
	}
	if err == nil {
		sigPubKey := peer.GetSigPublicKey()
		d := sha1.New()
		d.Write(id)
		d.Write(ck)
		d.Write(sk)
		d.Write(salt)
		hash := d.Sum(nil)
		err = rsa.VerifyPKCS1v15(sigPubKey, cr.SHA1, hash, sig)
	}
	if err == nil {
		// Everything is good; so Ack, leaving cnx open.
		h.Peer.MarkUp() // we consider the peer live
		h.Peer.LastContact()
		err = h.simpleAck(msgN)
	} else {
		// Send the text of the error to the peer; the send itself
		// may of course cause an error, but we will ignore that.
//.........这里部分代码省略.........
开发者ID:jddixon,项目名称:xlattice_go,代码行数:101,代码来源:in_q.go


注:本文中的github.com/jddixon/xlNode_go.Node.FindPeer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。