2019-01-19 00:14:10 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
2019-08-04 15:21:04 -05:00
|
|
|
"bufio"
|
2019-01-19 00:14:10 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-21 21:27:52 -06:00
|
|
|
"io"
|
2019-08-04 14:18:59 -05:00
|
|
|
"net"
|
2019-01-19 00:14:10 +00:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
|
|
|
)
|
|
|
|
|
2019-01-21 21:27:52 -06:00
|
|
|
// Test that this matches the interface we expect
|
|
|
|
var _ = linkInterfaceMsgIO(&stream{})
|
|
|
|
|
2019-01-19 00:14:10 +00:00
|
|
|
type stream struct {
|
2019-08-04 15:21:04 -05:00
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
inputBuffer *bufio.Reader
|
2019-08-04 14:18:59 -05:00
|
|
|
outputBuffer net.Buffers
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 21:27:52 -06:00
|
|
|
func (s *stream) close() error {
|
|
|
|
return s.rwc.Close()
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:14:10 +00:00
|
|
|
const streamMsgSize = 2048 + 65535
|
|
|
|
|
|
|
|
var streamMsg = [...]byte{0xde, 0xad, 0xb1, 0x75} // "dead bits"
|
|
|
|
|
2019-01-23 15:16:22 +00:00
|
|
|
func (s *stream) init(rwc io.ReadWriteCloser) {
|
2019-01-21 21:27:52 -06:00
|
|
|
// TODO have this also do the metadata handshake and create the peer struct
|
|
|
|
s.rwc = rwc
|
|
|
|
// TODO call something to do the metadata exchange
|
2019-08-04 15:21:04 -05:00
|
|
|
s.inputBuffer = bufio.NewReaderSize(s.rwc, 2*streamMsgSize)
|
2019-01-21 21:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeMsg writes a message with stream padding, and is *not* thread safe.
|
|
|
|
func (s *stream) writeMsg(bs []byte) (int, error) {
|
2019-02-05 17:39:59 -06:00
|
|
|
buf := s.outputBuffer[:0]
|
2019-08-04 14:18:59 -05:00
|
|
|
buf = append(buf, streamMsg[:])
|
|
|
|
l := wire_put_uint64(uint64(len(bs)), util.GetBytes())
|
|
|
|
defer util.PutBytes(l)
|
|
|
|
buf = append(buf, l)
|
|
|
|
padLen := len(buf[0]) + len(buf[1])
|
|
|
|
buf = append(buf, bs)
|
|
|
|
totalLen := padLen + len(bs)
|
2019-08-06 20:51:38 -05:00
|
|
|
s.outputBuffer = buf[:0] // So we can reuse the same underlying array later
|
2019-01-21 21:27:52 -06:00
|
|
|
var bn int
|
2019-08-04 14:18:59 -05:00
|
|
|
for bn < totalLen {
|
|
|
|
n, err := buf.WriteTo(s.rwc)
|
|
|
|
bn += int(n)
|
2019-01-21 21:27:52 -06:00
|
|
|
if err != nil {
|
|
|
|
l := bn - padLen
|
|
|
|
if l < 0 {
|
|
|
|
l = 0
|
|
|
|
}
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(bs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readMsg reads a message from the stream, accounting for stream padding, and is *not* thread safe.
|
|
|
|
func (s *stream) readMsg() ([]byte, error) {
|
|
|
|
for {
|
2019-08-04 15:21:04 -05:00
|
|
|
bs, err := s.readMsgFromBuffer()
|
|
|
|
if err != nil {
|
2019-01-21 21:27:52 -06:00
|
|
|
return nil, fmt.Errorf("message error: %v", err)
|
|
|
|
}
|
2019-08-04 15:21:04 -05:00
|
|
|
return bs, err
|
2019-01-21 21:27:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes metadata bytes without stream padding, meant to be temporary
|
|
|
|
func (s *stream) _sendMetaBytes(metaBytes []byte) error {
|
|
|
|
var written int
|
|
|
|
for written < len(metaBytes) {
|
|
|
|
n, err := s.rwc.Write(metaBytes)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads metadata bytes without stream padding, meant to be temporary
|
|
|
|
func (s *stream) _recvMetaBytes() ([]byte, error) {
|
|
|
|
var meta version_metadata
|
|
|
|
frag := meta.encode()
|
|
|
|
metaBytes := make([]byte, 0, len(frag))
|
|
|
|
for len(metaBytes) < len(frag) {
|
|
|
|
n, err := s.rwc.Read(frag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metaBytes = append(metaBytes, frag[:n]...)
|
|
|
|
}
|
|
|
|
return metaBytes, nil
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 15:21:04 -05:00
|
|
|
// Reads bytes from the underlying rwc and returns 1 full message
|
|
|
|
func (s *stream) readMsgFromBuffer() ([]byte, error) {
|
|
|
|
pad := streamMsg // Copy
|
|
|
|
_, err := io.ReadFull(s.inputBuffer, pad[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if pad != streamMsg {
|
|
|
|
return nil, errors.New("bad message")
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 15:21:04 -05:00
|
|
|
lenSlice := make([]byte, 0, 10)
|
|
|
|
// FIXME this nextByte stuff depends on wire.go format, kind of ugly to have it here
|
|
|
|
nextByte := byte(0xff)
|
|
|
|
for nextByte > 127 {
|
|
|
|
nextByte, err = s.inputBuffer.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 15:21:04 -05:00
|
|
|
lenSlice = append(lenSlice, nextByte)
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 15:21:04 -05:00
|
|
|
msgLen, _ := wire_decode_uint64(lenSlice)
|
2019-01-19 00:14:10 +00:00
|
|
|
if msgLen > streamMsgSize {
|
2019-08-04 15:21:04 -05:00
|
|
|
return nil, errors.New("oversized message")
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 15:21:04 -05:00
|
|
|
msg := util.ResizeBytes(util.GetBytes(), int(msgLen))
|
|
|
|
_, err = io.ReadFull(s.inputBuffer, msg)
|
|
|
|
return msg, err
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|