brontide: add new ReadMessage method to brontide.Conn

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.
This commit is contained in:
Olaoluwa Osuntokun 2017-04-20 15:35:29 -07:00
parent f867252139
commit 38d3c72dc8
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 13 additions and 5 deletions

@ -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 {

@ -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])
}