aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-01-20 07:42:13 +0800
committerFelix Lange <fjl@twurst.com>2015-02-06 07:00:35 +0800
commite252c634cb40c8ef7f9bcd542f5418a937929620 (patch)
treeacc946f71f724eed5f8244d302c74fce7f5bd0b8 /p2p/peer.go
parent1803c65e4097b9d6cb83f72a8a09aeddcc01f685 (diff)
downloaddexon-e252c634cb40c8ef7f9bcd542f5418a937929620.tar.gz
dexon-e252c634cb40c8ef7f9bcd542f5418a937929620.tar.zst
dexon-e252c634cb40c8ef7f9bcd542f5418a937929620.zip
first stab at integrating crypto in our p2p
- abstract the entire handshake logic in cryptoId.Run() taking session-relevant parameters - changes in peer to accomodate how the encryption layer would be switched on - modify arguments of handshake components - fixed test getting the wrong pubkey but it till crashes on DH in newSession()
Diffstat (limited to 'p2p/peer.go')
-rw-r--r--p2p/peer.go31
1 files changed, 22 insertions, 9 deletions
diff --git a/p2p/peer.go b/p2p/peer.go
index e98c3d560..e3e04ee65 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -222,9 +222,9 @@ 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)
+ var readLoop func(chan<- Msg, chan<- error, <-chan bool)
if p.cryptoHandshake {
- if readLoop, 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
}
@@ -332,20 +332,33 @@ func (p *Peer) dispatch(msg Msg, protoDone chan struct{}) (wait bool, err error)
return wait, nil
}
-func (p *Peer) handleCryptoHandshake() (err error) {
+type readLoop func(chan<- Msg, chan<- error, <-chan bool)
+
+func (p *Peer) handleCryptoHandshake() (loop readLoop, 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
+ var initiator bool
+ var sessionToken []byte
+ if p.dialAddr != nil { // this should have its own method Outgoing() bool
initiator = true
}
// create crypto layer
- cryptoId := newCryptoId(p.identity, initiator, sessionToken)
+ // this could in principle run only once but maybe we want to allow
+ // identity switching
+ var crypto *cryptoId
+ if crypto, err = newCryptoId(p.ourID); err != nil {
+ return
+ }
// run on peer
- if rw, err := cryptoId.Run(p.Pubkey()); err != nil {
- return err
+ // this bit handles the handshake and creates a secure communications channel with
+ // var rw *secretRW
+ if sessionToken, _, err = crypto.Run(p.conn, p.Pubkey(), sessionToken, initiator); err != nil {
+ return
}
- p.conn = rw.Run(p.conn)
-
+ loop = func(msg chan<- Msg, err chan<- error, next <-chan bool) {
+ // this is the readloop :)
+ }
+ return
}
func (p *Peer) startBaseProtocol() {