diff options
author | zelig <viktor.tron@gmail.com> | 2015-01-19 19:21:13 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-02-06 07:00:35 +0800 |
commit | 1803c65e4097b9d6cb83f72a8a09aeddcc01f685 (patch) | |
tree | 0393c085d821d9f65b83568b54f243b2f037f13c /p2p/peer.go | |
parent | 489d956283390b701473edd4a597afea2c426d41 (diff) | |
download | dexon-1803c65e4097b9d6cb83f72a8a09aeddcc01f685.tar.gz dexon-1803c65e4097b9d6cb83f72a8a09aeddcc01f685.tar.zst dexon-1803c65e4097b9d6cb83f72a8a09aeddcc01f685.zip |
integrate cryptoId into peer and connection lifecycle
Diffstat (limited to 'p2p/peer.go')
-rw-r--r-- | p2p/peer.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/p2p/peer.go b/p2p/peer.go index 886b95a80..e98c3d560 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -222,10 +222,14 @@ func (p *Peer) loop() (reason DiscReason, err error) { defer close(p.closed) defer p.conn.Close() + var readLoop func(chan Msg, chan error, chan bool) if p.cryptoHandshake { - if err := p.handleCryptoHandshake(); err != nil { + if readLoop, err := p.handleCryptoHandshake(); err != nil { + // from here on everything can be encrypted, authenticated return DiscProtocolError, err // no graceful disconnect } + } else { + readLoop = p.readLoop } // read loop @@ -233,7 +237,7 @@ func (p *Peer) loop() (reason DiscReason, err error) { readErr := make(chan error) readNext := make(chan bool, 1) protoDone := make(chan struct{}, 1) - go p.readLoop(readMsg, readErr, readNext) + go readLoop(readMsg, readErr, readNext) readNext <- true if p.runBaseProtocol { @@ -329,8 +333,19 @@ func (p *Peer) dispatch(msg Msg, protoDone chan struct{}) (wait bool, err error) } func (p *Peer) handleCryptoHandshake() (err error) { + // cryptoId is just created for the lifecycle of the handshake + // it is survived by an encrypted readwriter + if p.dialAddr != 0 { // this should have its own method Outgoing() bool + initiator = true + } + // create crypto layer + cryptoId := newCryptoId(p.identity, initiator, sessionToken) + // run on peer + if rw, err := cryptoId.Run(p.Pubkey()); err != nil { + return err + } + p.conn = rw.Run(p.conn) - return nil } func (p *Peer) startBaseProtocol() { |