From 38d3c72dc8a5b8909a249f2d54cb344efad888a9 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 20 Apr 2017 15:35:29 -0700 Subject: [PATCH] brontide: add new ReadMessage method to brontide.Conn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a new message to the brontide.Conn struct which allows callers to read an _entire_ message from the stream. As defined now, brontide is a message crypto messaging protocol. Previously the only method that allowed callers to read attempted to hide this feature with a stream-like abstraction. However, having this as the sole interface is at odds with the message oriented Lightning wire protocol, and isn’t sufficient to allow parsing messages that have been padded as is allowed by the protocol. This new ReadNextMessage is intended to be used by higher level systems which implement the Lightning p2p protocol. --- brontide/conn.go | 17 ++++++++++++----- brontide/noise.go | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/brontide/conn.go b/brontide/conn.go index a56cf5c6..fb0d1d7a 100644 --- a/brontide/conn.go +++ b/brontide/conn.go @@ -84,17 +84,24 @@ func Dial(localPriv *btcec.PrivateKey, netAddr *lnwire.NetAddress) (*Conn, error return b, nil } +// ReadMessage uses the connection in a message-oriented instructing it to read +// the next _full_ message with the brontide stream. This function will block +// until the read succeeds. +func (c *Conn) ReadNextMessage() ([]byte, error) { + return c.noise.ReadMessage(c.conn) +} + // Read reads data from the connection. Read can be made to time out and // return a Error with Timeout() == true after a fixed time limit; see // SetDeadline and SetReadDeadline. // // Part of the net.Conn interface. func (c *Conn) Read(b []byte) (n int, err error) { - // In order to reconcile the differences between the record abstraction of - // our AEAD connection, and the stream abstraction of TCP, we maintain an - // intermediate read buffer. If this buffer becomes depleated, then we read - // the next record, and feed it into the buffer. Otherwise, we read - // directly from the buffer. + // In order to reconcile the differences between the record abstraction + // of our AEAD connection, and the stream abstraction of TCP, we + // maintain an intermediate read buffer. If this buffer becomes + // depleated, then we read the next record, and feed it into the + // buffer. Otherwise, we read directly from the buffer. if c.readBuf.Len() == 0 { plaintext, err := c.noise.ReadMessage(c.conn) if err != nil { diff --git a/brontide/noise.go b/brontide/noise.go index 77a448fd..ea86d0bc 100644 --- a/brontide/noise.go +++ b/brontide/noise.go @@ -686,5 +686,6 @@ func (b *Machine) ReadMessage(r io.Reader) ([]byte, error) { return nil, err } + // TODO(roasbeef): modify to let pass in slice return b.recvCipher.Decrypt(nil, nil, cipherText[:pktLen]) }