2019-01-19 00:14:10 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
2019-08-04 20:21:04 +00:00
|
|
|
"bufio"
|
2019-01-19 00:14:10 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-22 03:27:52 +00:00
|
|
|
"io"
|
2019-08-04 19:18:59 +00:00
|
|
|
"net"
|
2019-01-19 00:14:10 +00:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
|
|
|
)
|
|
|
|
|
2019-01-22 03:27:52 +00: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 20:21:04 +00:00
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
inputBuffer *bufio.Reader
|
2019-08-04 19:18:59 +00:00
|
|
|
outputBuffer net.Buffers
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 03:27:52 +00: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-22 03:27:52 +00: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 20:21:04 +00:00
|
|
|
s.inputBuffer = bufio.NewReaderSize(s.rwc, 2*streamMsgSize)
|
2019-01-22 03:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeMsg writes a message with stream padding, and is *not* thread safe.
|
2019-08-18 17:17:54 +00:00
|
|
|
func (s *stream) writeMsgs(bss [][]byte) (int, error) {
|
2019-02-05 23:39:59 +00:00
|
|
|
buf := s.outputBuffer[:0]
|
2019-08-18 17:17:54 +00:00
|
|
|
var written int
|
|
|
|
for _, bs := range bss {
|
|
|
|
buf = append(buf, streamMsg[:])
|
|
|
|
buf = append(buf, wire_encode_uint64(uint64(len(bs))))
|
|
|
|
buf = append(buf, bs)
|
|
|
|
written += len(bs)
|
2019-01-22 03:27:52 +00:00
|
|
|
}
|
2019-08-18 17:17:54 +00:00
|
|
|
s.outputBuffer = buf[:0] // So we can reuse the same underlying array later
|
|
|
|
_, err := buf.WriteTo(s.rwc)
|
|
|
|
// TODO only include number of bytes from bs *successfully* written?
|
|
|
|
return written, err
|
2019-01-22 03:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 20:21:04 +00:00
|
|
|
bs, err := s.readMsgFromBuffer()
|
|
|
|
if err != nil {
|
2019-01-22 03:27:52 +00:00
|
|
|
return nil, fmt.Errorf("message error: %v", err)
|
|
|
|
}
|
2019-08-04 20:21:04 +00:00
|
|
|
return bs, err
|
2019-01-22 03:27:52 +00: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 20:21:04 +00: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 20:21:04 +00: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 20:21:04 +00:00
|
|
|
lenSlice = append(lenSlice, nextByte)
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 20:21:04 +00:00
|
|
|
msgLen, _ := wire_decode_uint64(lenSlice)
|
2019-01-19 00:14:10 +00:00
|
|
|
if msgLen > streamMsgSize {
|
2019-08-04 20:21:04 +00:00
|
|
|
return nil, errors.New("oversized message")
|
2019-01-19 00:14:10 +00:00
|
|
|
}
|
2019-08-04 20:21:04 +00: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
|
|
|
}
|