2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2018-04-16 18:08:50 +00:00
|
|
|
"crypto/sha256"
|
2014-09-22 19:42:11 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-12-01 12:35:32 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2014-09-22 19:42:11 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
lz4 "github.com/bkaradzic/go-lz4"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-16 18:08:50 +00:00
|
|
|
// Shifts
|
|
|
|
KiB = 10
|
|
|
|
MiB = 20
|
|
|
|
GiB = 30
|
|
|
|
)
|
2015-08-18 06:38:06 +00:00
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
const (
|
2016-07-04 10:40:29 +00:00
|
|
|
// MaxMessageLen is the largest message size allowed on the wire. (500 MB)
|
|
|
|
MaxMessageLen = 500 * 1000 * 1000
|
2018-04-16 18:08:50 +00:00
|
|
|
|
|
|
|
// MinBlockSize is the minimum block size allowed
|
|
|
|
MinBlockSize = 128 << KiB
|
|
|
|
|
|
|
|
// MaxBlockSize is the maximum block size allowed
|
|
|
|
MaxBlockSize = 16 << MiB
|
|
|
|
|
|
|
|
// DesiredPerFileBlocks is the number of blocks we aim for per file
|
|
|
|
DesiredPerFileBlocks = 2000
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
// BlockSizes is the list of valid block sizes, from min to max
|
|
|
|
var BlockSizes []int
|
|
|
|
|
|
|
|
// For each block size, the hash of a block of all zeroes
|
|
|
|
var sha256OfEmptyBlock = make(map[int][sha256.Size]byte)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for blockSize := MinBlockSize; blockSize <= MaxBlockSize; blockSize *= 2 {
|
|
|
|
BlockSizes = append(BlockSizes, blockSize)
|
|
|
|
sha256OfEmptyBlock[blockSize] = sha256.Sum256(make([]byte, blockSize))
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool = newBufferPool()
|
2018-04-16 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockSize returns the block size to use for the given file size
|
|
|
|
func BlockSize(fileSize int64) int {
|
|
|
|
var blockSize int
|
|
|
|
for _, blockSize = range BlockSizes {
|
2018-06-06 07:59:33 +00:00
|
|
|
if fileSize < DesiredPerFileBlocks*int64(blockSize) {
|
2018-04-16 18:08:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockSize
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
const (
|
|
|
|
stateInitial = iota
|
2015-06-26 13:38:56 +00:00
|
|
|
stateReady
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
// Request message flags
|
2015-02-13 23:27:01 +00:00
|
|
|
const (
|
2016-04-15 10:59:41 +00:00
|
|
|
FlagFromTemporary uint32 = 1 << iota
|
2015-02-13 23:27:01 +00:00
|
|
|
)
|
|
|
|
|
2015-09-27 11:11:34 +00:00
|
|
|
// ClusterConfigMessage.Folders flags
|
|
|
|
const (
|
2016-04-15 10:59:41 +00:00
|
|
|
FlagFolderReadOnly uint32 = 1 << 0
|
|
|
|
FlagFolderIgnorePerms = 1 << 1
|
|
|
|
FlagFolderIgnoreDelete = 1 << 2
|
|
|
|
FlagFolderDisabledTempIndexes = 1 << 3
|
|
|
|
FlagFolderAll = 1<<4 - 1
|
2015-09-27 11:11:34 +00:00
|
|
|
)
|
|
|
|
|
2015-02-13 23:27:01 +00:00
|
|
|
// ClusterConfigMessage.Folders.Devices flags
|
2014-09-22 19:42:11 +00:00
|
|
|
const (
|
|
|
|
FlagShareTrusted uint32 = 1 << 0
|
|
|
|
FlagShareReadOnly = 1 << 1
|
|
|
|
FlagIntroducer = 1 << 2
|
|
|
|
FlagShareBits = 0x000000ff
|
|
|
|
)
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
// FileInfo.LocalFlags flags
|
|
|
|
const (
|
|
|
|
FlagLocalUnsupported = 1 << 0 // The kind is unsupported, e.g. symlinks on Windows
|
|
|
|
FlagLocalIgnored = 1 << 1 // Matches local ignore patterns
|
|
|
|
FlagLocalMustRescan = 1 << 2 // Doesn't match content on disk, must be rechecked fully
|
2018-07-12 08:15:57 +00:00
|
|
|
FlagLocalReceiveOnly = 1 << 3 // Change detected on receive only folder
|
2018-06-24 07:50:18 +00:00
|
|
|
|
|
|
|
// Flags that should result in the Invalid bit on outgoing updates
|
2018-07-12 08:15:57 +00:00
|
|
|
LocalInvalidFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalMustRescan | FlagLocalReceiveOnly
|
2018-06-24 07:50:18 +00:00
|
|
|
|
|
|
|
// Flags that should result in a file being in conflict with its
|
|
|
|
// successor, due to us not having an up to date picture of its state on
|
|
|
|
// disk.
|
2018-07-12 08:15:57 +00:00
|
|
|
LocalConflictFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalReceiveOnly
|
2018-09-16 07:48:14 +00:00
|
|
|
|
|
|
|
LocalAllFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalMustRescan | FlagLocalReceiveOnly
|
2018-06-24 07:50:18 +00:00
|
|
|
)
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
var (
|
2016-04-30 04:35:38 +00:00
|
|
|
ErrClosed = errors.New("connection closed")
|
|
|
|
ErrTimeout = errors.New("read timeout")
|
|
|
|
ErrSwitchingConnections = errors.New("switching connections")
|
2016-07-04 10:40:29 +00:00
|
|
|
errUnknownMessage = errors.New("unknown message")
|
2016-12-01 12:35:32 +00:00
|
|
|
errInvalidFilename = errors.New("filename is invalid")
|
|
|
|
errUncleanFilename = errors.New("filename not in canonical format")
|
2017-03-27 07:21:08 +00:00
|
|
|
errDeletedHasBlocks = errors.New("deleted file with non-empty block list")
|
|
|
|
errDirectoryHasBlocks = errors.New("directory with non-empty block list")
|
|
|
|
errFileHasNoBlocks = errors.New("file with empty block list")
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Model interface {
|
2014-09-28 11:00:38 +00:00
|
|
|
// An index was received from the peer device
|
2016-07-04 10:40:29 +00:00
|
|
|
Index(deviceID DeviceID, folder string, files []FileInfo)
|
2014-09-28 11:00:38 +00:00
|
|
|
// An index update was received from the peer device
|
2016-07-04 10:40:29 +00:00
|
|
|
IndexUpdate(deviceID DeviceID, folder string, files []FileInfo)
|
2014-09-28 11:00:38 +00:00
|
|
|
// A request was made by the peer device
|
2018-11-13 07:53:55 +00:00
|
|
|
Request(deviceID DeviceID, folder, name string, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
|
2014-09-22 19:42:11 +00:00
|
|
|
// A cluster configuration message was received
|
2016-07-04 10:40:29 +00:00
|
|
|
ClusterConfig(deviceID DeviceID, config ClusterConfig)
|
2014-09-28 11:00:38 +00:00
|
|
|
// The peer device closed the connection
|
2016-08-10 09:37:32 +00:00
|
|
|
Closed(conn Connection, err error)
|
2016-04-15 10:59:41 +00:00
|
|
|
// The peer device sent progress updates for the files it is currently downloading
|
2016-07-04 10:40:29 +00:00
|
|
|
DownloadProgress(deviceID DeviceID, folder string, updates []FileDownloadProgressUpdate)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
type RequestResponse interface {
|
|
|
|
Data() []byte
|
|
|
|
Close() // Must always be called once the byte slice is no longer in use
|
|
|
|
Wait() // Blocks until Close is called
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
type Connection interface {
|
2015-07-10 06:34:54 +00:00
|
|
|
Start()
|
2019-01-09 16:31:09 +00:00
|
|
|
Close(err error)
|
2014-09-28 11:00:38 +00:00
|
|
|
ID() DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
Name() string
|
2016-07-04 10:40:29 +00:00
|
|
|
Index(folder string, files []FileInfo) error
|
|
|
|
IndexUpdate(folder string, files []FileInfo) error
|
2018-05-05 08:24:44 +00:00
|
|
|
Request(folder string, name string, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error)
|
2016-07-04 10:40:29 +00:00
|
|
|
ClusterConfig(config ClusterConfig)
|
|
|
|
DownloadProgress(folder string, updates []FileDownloadProgressUpdate)
|
2014-09-22 19:42:11 +00:00
|
|
|
Statistics() Statistics
|
2016-01-11 15:49:44 +00:00
|
|
|
Closed() bool
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rawConnection struct {
|
2014-09-28 11:00:38 +00:00
|
|
|
id DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
name string
|
|
|
|
receiver Model
|
|
|
|
|
|
|
|
cr *countingReader
|
|
|
|
cw *countingWriter
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
awaiting map[int32]chan asyncResult
|
2014-09-22 19:42:11 +00:00
|
|
|
awaitingMut sync.Mutex
|
|
|
|
|
|
|
|
idxMut sync.Mutex // ensures serialization of Index calls
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
nextID int32
|
|
|
|
nextIDMut sync.Mutex
|
|
|
|
|
2019-01-14 07:32:37 +00:00
|
|
|
outbox chan asyncMessage
|
|
|
|
closed chan struct{}
|
|
|
|
closeOnce sync.Once
|
|
|
|
sendCloseOnce sync.Once
|
|
|
|
compression Compression
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type asyncResult struct {
|
|
|
|
val []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
type message interface {
|
|
|
|
ProtoSize() int
|
|
|
|
Marshal() ([]byte, error)
|
|
|
|
MarshalTo([]byte) (int, error)
|
|
|
|
Unmarshal([]byte) error
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
type asyncMessage struct {
|
|
|
|
msg message
|
2018-11-13 07:53:55 +00:00
|
|
|
done chan struct{} // done closes when we're done sending the message
|
2015-01-08 13:21:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:51:42 +00:00
|
|
|
const (
|
2015-09-22 18:34:24 +00:00
|
|
|
// PingSendInterval is how often we make sure to send a message, by
|
|
|
|
// triggering pings if necessary.
|
2015-09-21 06:51:42 +00:00
|
|
|
PingSendInterval = 90 * time.Second
|
2015-09-22 18:34:24 +00:00
|
|
|
// ReceiveTimeout is the longest we'll wait for a message from the other
|
|
|
|
// side before closing the connection.
|
2015-09-21 06:51:42 +00:00
|
|
|
ReceiveTimeout = 300 * time.Second
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-02-23 08:30:47 +00:00
|
|
|
func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, receiver Model, name string, compress Compression) Connection {
|
2014-09-22 19:42:11 +00:00
|
|
|
cr := &countingReader{Reader: reader}
|
|
|
|
cw := &countingWriter{Writer: writer}
|
|
|
|
|
|
|
|
c := rawConnection{
|
2016-12-19 11:26:26 +00:00
|
|
|
id: deviceID,
|
|
|
|
name: name,
|
|
|
|
receiver: nativeModel{receiver},
|
|
|
|
cr: cr,
|
|
|
|
cw: cw,
|
|
|
|
awaiting: make(map[int32]chan asyncResult),
|
|
|
|
outbox: make(chan asyncMessage),
|
|
|
|
closed: make(chan struct{}),
|
2015-02-23 08:30:47 +00:00
|
|
|
compression: compress,
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:34:54 +00:00
|
|
|
return wireFormatConnection{&c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates the goroutines for sending and receiving of messages. It must
|
|
|
|
// be called exactly once after creating a connection.
|
|
|
|
func (c *rawConnection) Start() {
|
2019-02-02 10:45:17 +00:00
|
|
|
go func() {
|
|
|
|
err := c.readerLoop()
|
|
|
|
c.internalClose(err)
|
|
|
|
}()
|
2014-09-22 19:42:11 +00:00
|
|
|
go c.writerLoop()
|
2015-09-21 06:51:42 +00:00
|
|
|
go c.pingSender()
|
|
|
|
go c.pingReceiver()
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (c *rawConnection) ID() DeviceID {
|
2014-09-22 19:42:11 +00:00
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// Index writes the list of file information to the connected peer device
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) Index(folder string, idx []FileInfo) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2016-07-04 10:40:29 +00:00
|
|
|
c.send(&Index{
|
|
|
|
Folder: folder,
|
|
|
|
Files: idx,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
c.idxMut.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// IndexUpdate writes the list of file information to the connected peer device as an update
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2016-07-04 10:40:29 +00:00
|
|
|
c.send(&IndexUpdate{
|
|
|
|
Folder: folder,
|
|
|
|
Files: idx,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
c.idxMut.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request returns the bytes for the specified block after fetching them from the connected peer.
|
2018-05-05 08:24:44 +00:00
|
|
|
func (c *rawConnection) Request(folder string, name string, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
c.nextIDMut.Lock()
|
|
|
|
id := c.nextID
|
|
|
|
c.nextID++
|
|
|
|
c.nextIDMut.Unlock()
|
2016-04-15 10:59:41 +00:00
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
c.awaitingMut.Lock()
|
2016-07-04 10:40:29 +00:00
|
|
|
if _, ok := c.awaiting[id]; ok {
|
2014-09-22 19:42:11 +00:00
|
|
|
panic("id taken")
|
|
|
|
}
|
|
|
|
rc := make(chan asyncResult, 1)
|
|
|
|
c.awaiting[id] = rc
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
ok := c.send(&Request{
|
|
|
|
ID: id,
|
|
|
|
Folder: folder,
|
|
|
|
Name: name,
|
|
|
|
Offset: offset,
|
|
|
|
Size: int32(size),
|
|
|
|
Hash: hash,
|
2018-05-05 08:24:44 +00:00
|
|
|
WeakHash: weakHash,
|
2016-07-04 10:40:29 +00:00
|
|
|
FromTemporary: fromTemporary,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := <-rc
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
return res.val, res.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClusterConfig send the cluster configuration message to the peer and returns any error
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) ClusterConfig(config ClusterConfig) {
|
|
|
|
c.send(&config, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:49:44 +00:00
|
|
|
func (c *rawConnection) Closed() bool {
|
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
// DownloadProgress sends the progress updates for the files that are currently being downloaded.
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) DownloadProgress(folder string, updates []FileDownloadProgressUpdate) {
|
|
|
|
c.send(&DownloadProgress{
|
2016-04-15 10:59:41 +00:00
|
|
|
Folder: folder,
|
|
|
|
Updates: updates,
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
func (c *rawConnection) ping() bool {
|
2016-07-04 10:40:29 +00:00
|
|
|
return c.send(&Ping{}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) readerLoop() (err error) {
|
2018-11-13 07:53:55 +00:00
|
|
|
fourByteBuf := make([]byte, 4)
|
2015-06-26 13:38:56 +00:00
|
|
|
state := stateInitial
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
msg, err := c.readMessage(fourByteBuf)
|
2016-07-05 09:29:28 +00:00
|
|
|
if err == errUnknownMessage {
|
|
|
|
// Unknown message types are skipped, for future extensibility.
|
|
|
|
continue
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
switch msg := msg.(type) {
|
2016-07-04 10:40:29 +00:00
|
|
|
case *ClusterConfig:
|
|
|
|
l.Debugln("read ClusterConfig message")
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateInitial {
|
|
|
|
return fmt.Errorf("protocol error: cluster config message in state %d", state)
|
|
|
|
}
|
2016-07-27 21:36:25 +00:00
|
|
|
c.receiver.ClusterConfig(c.id, *msg)
|
2015-06-26 13:38:56 +00:00
|
|
|
state = stateReady
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Index:
|
|
|
|
l.Debugln("read Index message")
|
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: index message in state %d", state)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2017-03-27 07:21:08 +00:00
|
|
|
if err := checkIndexConsistency(msg.Files); err != nil {
|
2016-12-01 12:35:32 +00:00
|
|
|
return fmt.Errorf("protocol error: index: %v", err)
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
c.handleIndex(*msg)
|
|
|
|
state = stateReady
|
|
|
|
|
|
|
|
case *IndexUpdate:
|
|
|
|
l.Debugln("read IndexUpdate message")
|
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: index update message in state %d", state)
|
|
|
|
}
|
2017-03-27 07:21:08 +00:00
|
|
|
if err := checkIndexConsistency(msg.Files); err != nil {
|
2016-12-01 12:35:32 +00:00
|
|
|
return fmt.Errorf("protocol error: index update: %v", err)
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
c.handleIndexUpdate(*msg)
|
|
|
|
state = stateReady
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Request:
|
|
|
|
l.Debugln("read Request message")
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: request message in state %d", state)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2016-12-01 12:35:32 +00:00
|
|
|
if err := checkFilename(msg.Name); err != nil {
|
|
|
|
return fmt.Errorf("protocol error: request: %q: %v", msg.Name, err)
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
go c.handleRequest(*msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Response:
|
|
|
|
l.Debugln("read Response message")
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: response message in state %d", state)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
c.handleResponse(*msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *DownloadProgress:
|
|
|
|
l.Debugln("read DownloadProgress message")
|
2016-04-15 10:59:41 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: response message in state %d", state)
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
c.receiver.DownloadProgress(c.id, msg.Folder, msg.Updates)
|
2016-04-15 10:59:41 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Ping:
|
|
|
|
l.Debugln("read Ping message")
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: ping message in state %d", state)
|
|
|
|
}
|
2015-09-21 06:51:42 +00:00
|
|
|
// Nothing
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Close:
|
|
|
|
l.Debugln("read Close message")
|
2015-01-11 12:24:56 +00:00
|
|
|
return errors.New(msg.Reason)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
default:
|
2016-07-04 10:40:29 +00:00
|
|
|
l.Debugf("read unknown message: %+T", msg)
|
|
|
|
return fmt.Errorf("protocol error: %s: unknown or empty message", c.id)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
func (c *rawConnection) readMessage(fourByteBuf []byte) (message, error) {
|
|
|
|
hdr, err := c.readHeader(fourByteBuf)
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
2016-07-04 10:40:29 +00:00
|
|
|
return nil, err
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
return c.readMessageAfterHeader(hdr, fourByteBuf)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (message, error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
// First comes a 4 byte message length
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
if _, err := io.ReadFull(c.cr, fourByteBuf[:4]); err != nil {
|
2016-07-04 10:40:29 +00:00
|
|
|
return nil, fmt.Errorf("reading message length: %v", err)
|
2015-08-18 06:38:06 +00:00
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
msgLen := int32(binary.BigEndian.Uint32(fourByteBuf))
|
2016-07-04 10:40:29 +00:00
|
|
|
if msgLen < 0 {
|
|
|
|
return nil, fmt.Errorf("negative message length %d", msgLen)
|
2015-01-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// Then comes the message
|
2016-02-02 11:48:09 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(int(msgLen))
|
2016-07-04 10:40:29 +00:00
|
|
|
if _, err := io.ReadFull(c.cr, buf); err != nil {
|
|
|
|
return nil, fmt.Errorf("reading message: %v", err)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// ... which might be compressed
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
switch hdr.Compression {
|
|
|
|
case MessageCompressionNone:
|
|
|
|
// Nothing
|
2016-02-02 11:48:09 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case MessageCompressionLZ4:
|
|
|
|
decomp, err := c.lz4Decompress(buf)
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
2016-07-04 10:40:29 +00:00
|
|
|
return nil, fmt.Errorf("decompressing message: %v", err)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
buf = decomp
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown message compression %d", hdr.Compression)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// ... and is then unmarshalled
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
msg, err := c.newMessage(hdr.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := msg.Unmarshal(buf); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshalling message: %v", err)
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
return msg, nil
|
|
|
|
}
|
2015-01-11 12:24:56 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
func (c *rawConnection) readHeader(fourByteBuf []byte) (Header, error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
// First comes a 2 byte header length
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
if _, err := io.ReadFull(c.cr, fourByteBuf[:2]); err != nil {
|
2016-07-04 10:40:29 +00:00
|
|
|
return Header{}, fmt.Errorf("reading length: %v", err)
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
hdrLen := int16(binary.BigEndian.Uint16(fourByteBuf))
|
2016-07-04 10:40:29 +00:00
|
|
|
if hdrLen < 0 {
|
|
|
|
return Header{}, fmt.Errorf("negative header length %d", hdrLen)
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// Then comes the header
|
2016-04-15 10:59:41 +00:00
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(int(hdrLen))
|
2016-07-04 10:40:29 +00:00
|
|
|
if _, err := io.ReadFull(c.cr, buf); err != nil {
|
|
|
|
return Header{}, fmt.Errorf("reading header: %v", err)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
var hdr Header
|
|
|
|
if err := hdr.Unmarshal(buf); err != nil {
|
|
|
|
return Header{}, fmt.Errorf("unmarshalling header: %v", err)
|
2016-02-01 08:10:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
return hdr, nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) handleIndex(im Index) {
|
|
|
|
l.Debugf("Index(%v, %v, %d file)", c.id, im.Folder, len(im.Files))
|
2016-12-01 12:35:32 +00:00
|
|
|
c.receiver.Index(c.id, im.Folder, im.Files)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) handleIndexUpdate(im IndexUpdate) {
|
|
|
|
l.Debugf("queueing IndexUpdate(%v, %v, %d files)", c.id, im.Folder, len(im.Files))
|
2016-12-01 12:35:32 +00:00
|
|
|
c.receiver.IndexUpdate(c.id, im.Folder, im.Files)
|
2015-01-07 14:44:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 07:21:08 +00:00
|
|
|
// checkIndexConsistency verifies a number of invariants on FileInfos received in
|
|
|
|
// index messages.
|
|
|
|
func checkIndexConsistency(fs []FileInfo) error {
|
2016-12-01 12:35:32 +00:00
|
|
|
for _, f := range fs {
|
2017-03-27 07:21:08 +00:00
|
|
|
if err := checkFileInfoConsistency(f); err != nil {
|
2016-12-01 12:35:32 +00:00
|
|
|
return fmt.Errorf("%q: %v", f.Name, err)
|
2015-01-07 14:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 12:35:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 07:21:08 +00:00
|
|
|
// checkFileInfoConsistency verifies a number of invariants on the given FileInfo
|
|
|
|
func checkFileInfoConsistency(f FileInfo) error {
|
|
|
|
if err := checkFilename(f.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case f.Deleted && len(f.Blocks) != 0:
|
|
|
|
// Deleted files should have no blocks
|
|
|
|
return errDeletedHasBlocks
|
|
|
|
|
|
|
|
case f.Type == FileInfoTypeDirectory && len(f.Blocks) != 0:
|
|
|
|
// Directories should have no blocks
|
|
|
|
return errDirectoryHasBlocks
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
case !f.Deleted && !f.IsInvalid() && f.Type == FileInfoTypeFile && len(f.Blocks) == 0:
|
2017-04-12 09:28:12 +00:00
|
|
|
// Non-deleted, non-invalid files should have at least one block
|
2017-03-27 07:21:08 +00:00
|
|
|
return errFileHasNoBlocks
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 12:35:32 +00:00
|
|
|
// checkFilename verifies that the given filename is valid according to the
|
|
|
|
// spec on what's allowed over the wire. A filename failing this test is
|
|
|
|
// grounds for disconnecting the device.
|
|
|
|
func checkFilename(name string) error {
|
|
|
|
cleanedName := path.Clean(name)
|
|
|
|
if cleanedName != name {
|
|
|
|
// The filename on the wire should be in canonical format. If
|
|
|
|
// Clean() managed to clean it up, there was something wrong with
|
|
|
|
// it.
|
|
|
|
return errUncleanFilename
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "", ".", "..":
|
|
|
|
// These names are always invalid.
|
|
|
|
return errInvalidFilename
|
2015-01-07 14:44:36 +00:00
|
|
|
}
|
2016-12-01 12:35:32 +00:00
|
|
|
if strings.HasPrefix(name, "/") {
|
|
|
|
// Names are folder relative, not absolute.
|
|
|
|
return errInvalidFilename
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(name, "../") {
|
|
|
|
// Starting with a dotdot is not allowed. Any other dotdots would
|
|
|
|
// have been handled by the Clean() call at the top.
|
|
|
|
return errInvalidFilename
|
|
|
|
}
|
|
|
|
return nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) handleRequest(req Request) {
|
2018-11-13 07:53:55 +00:00
|
|
|
res, err := c.receiver.Request(c.id, req.Folder, req.Name, req.Size, req.Offset, req.Hash, req.WeakHash, req.FromTemporary)
|
2015-07-29 20:23:43 +00:00
|
|
|
if err != nil {
|
2016-07-04 10:40:29 +00:00
|
|
|
c.send(&Response{
|
|
|
|
ID: req.ID,
|
2015-07-29 20:23:43 +00:00
|
|
|
Code: errorToCode(err),
|
2018-11-13 07:53:55 +00:00
|
|
|
}, nil)
|
|
|
|
return
|
2015-07-29 20:23:43 +00:00
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
c.send(&Response{
|
|
|
|
ID: req.ID,
|
|
|
|
Data: res.Data(),
|
|
|
|
Code: errorToCode(nil),
|
|
|
|
}, done)
|
|
|
|
<-done
|
|
|
|
res.Close()
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) handleResponse(resp Response) {
|
2014-09-22 19:42:11 +00:00
|
|
|
c.awaitingMut.Lock()
|
2016-07-04 10:40:29 +00:00
|
|
|
if rc := c.awaiting[resp.ID]; rc != nil {
|
|
|
|
delete(c.awaiting, resp.ID)
|
2015-02-08 11:04:01 +00:00
|
|
|
rc <- asyncResult{resp.Data, codeToError(resp.Code)}
|
2014-09-22 19:42:11 +00:00
|
|
|
close(rc)
|
|
|
|
}
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) send(msg message, done chan struct{}) bool {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
2016-07-04 10:40:29 +00:00
|
|
|
case c.outbox <- asyncMessage{msg, done}:
|
2014-09-22 19:42:11 +00:00
|
|
|
return true
|
|
|
|
case <-c.closed:
|
2018-11-13 07:53:55 +00:00
|
|
|
if done != nil {
|
|
|
|
close(done)
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) writerLoop() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case hm := <-c.outbox:
|
2018-11-13 07:53:55 +00:00
|
|
|
err := c.writeMessage(hm)
|
|
|
|
if hm.done != nil {
|
|
|
|
close(hm.done)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-01-09 16:31:09 +00:00
|
|
|
c.internalClose(err)
|
2014-09-22 19:42:11 +00:00
|
|
|
return
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) writeMessage(hm asyncMessage) error {
|
|
|
|
if c.shouldCompressMessage(hm.msg) {
|
|
|
|
return c.writeCompressedMessage(hm)
|
|
|
|
}
|
|
|
|
return c.writeUncompressedMessage(hm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) writeCompressedMessage(hm asyncMessage) error {
|
|
|
|
size := hm.msg.ProtoSize()
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(size)
|
2016-07-04 10:40:29 +00:00
|
|
|
if _, err := hm.msg.MarshalTo(buf); err != nil {
|
|
|
|
return fmt.Errorf("marshalling message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
compressed, err := c.lz4Compress(buf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("compressing message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := Header{
|
|
|
|
Type: c.typeOf(hm.msg),
|
|
|
|
Compression: MessageCompressionLZ4,
|
|
|
|
}
|
|
|
|
hdrSize := hdr.ProtoSize()
|
|
|
|
if hdrSize > 1<<16-1 {
|
|
|
|
panic("impossibly large header")
|
|
|
|
}
|
|
|
|
|
|
|
|
totSize := 2 + hdrSize + 4 + len(compressed)
|
2018-11-13 07:53:55 +00:00
|
|
|
buf = BufferPool.Upgrade(buf, totSize)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
// Header length
|
|
|
|
binary.BigEndian.PutUint16(buf, uint16(hdrSize))
|
|
|
|
// Header
|
|
|
|
if _, err := hdr.MarshalTo(buf[2:]); err != nil {
|
|
|
|
return fmt.Errorf("marshalling header: %v", err)
|
|
|
|
}
|
|
|
|
// Message length
|
|
|
|
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(len(compressed)))
|
|
|
|
// Message
|
|
|
|
copy(buf[2+hdrSize+4:], compressed)
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(compressed)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
n, err := c.cw.Write(buf)
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message (%d uncompressed)), err=%v", n, hdrSize, len(compressed), size, err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing message: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) writeUncompressedMessage(hm asyncMessage) error {
|
|
|
|
size := hm.msg.ProtoSize()
|
|
|
|
|
|
|
|
hdr := Header{
|
|
|
|
Type: c.typeOf(hm.msg),
|
|
|
|
}
|
|
|
|
hdrSize := hdr.ProtoSize()
|
|
|
|
if hdrSize > 1<<16-1 {
|
|
|
|
panic("impossibly large header")
|
|
|
|
}
|
|
|
|
|
|
|
|
totSize := 2 + hdrSize + 4 + size
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(totSize)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
// Header length
|
|
|
|
binary.BigEndian.PutUint16(buf, uint16(hdrSize))
|
|
|
|
// Header
|
|
|
|
if _, err := hdr.MarshalTo(buf[2:]); err != nil {
|
|
|
|
return fmt.Errorf("marshalling header: %v", err)
|
|
|
|
}
|
|
|
|
// Message length
|
|
|
|
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(size))
|
|
|
|
// Message
|
|
|
|
if _, err := hm.msg.MarshalTo(buf[2+hdrSize+4:]); err != nil {
|
|
|
|
return fmt.Errorf("marshalling message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := c.cw.Write(buf[:totSize])
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
l.Debugf("wrote %d bytes on the wire (2 bytes length, %d bytes header, 4 bytes message length, %d bytes message), err=%v", n, hdrSize, size, err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing message: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) typeOf(msg message) MessageType {
|
|
|
|
switch msg.(type) {
|
|
|
|
case *ClusterConfig:
|
|
|
|
return messageTypeClusterConfig
|
|
|
|
case *Index:
|
|
|
|
return messageTypeIndex
|
|
|
|
case *IndexUpdate:
|
|
|
|
return messageTypeIndexUpdate
|
|
|
|
case *Request:
|
|
|
|
return messageTypeRequest
|
|
|
|
case *Response:
|
|
|
|
return messageTypeResponse
|
|
|
|
case *DownloadProgress:
|
|
|
|
return messageTypeDownloadProgress
|
|
|
|
case *Ping:
|
|
|
|
return messageTypePing
|
|
|
|
case *Close:
|
|
|
|
return messageTypeClose
|
|
|
|
default:
|
|
|
|
panic("bug: unknown message type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) newMessage(t MessageType) (message, error) {
|
|
|
|
switch t {
|
|
|
|
case messageTypeClusterConfig:
|
|
|
|
return new(ClusterConfig), nil
|
|
|
|
case messageTypeIndex:
|
|
|
|
return new(Index), nil
|
|
|
|
case messageTypeIndexUpdate:
|
|
|
|
return new(IndexUpdate), nil
|
|
|
|
case messageTypeRequest:
|
|
|
|
return new(Request), nil
|
|
|
|
case messageTypeResponse:
|
|
|
|
return new(Response), nil
|
|
|
|
case messageTypeDownloadProgress:
|
|
|
|
return new(DownloadProgress), nil
|
|
|
|
case messageTypePing:
|
|
|
|
return new(Ping), nil
|
|
|
|
case messageTypeClose:
|
|
|
|
return new(Close), nil
|
|
|
|
default:
|
|
|
|
return nil, errUnknownMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) shouldCompressMessage(msg message) bool {
|
|
|
|
switch c.compression {
|
|
|
|
case CompressNever:
|
|
|
|
return false
|
|
|
|
|
|
|
|
case CompressAlways:
|
|
|
|
// Use compression for large enough messages
|
|
|
|
return msg.ProtoSize() >= compressionThreshold
|
|
|
|
|
|
|
|
case CompressMetadata:
|
|
|
|
_, isResponse := msg.(*Response)
|
|
|
|
// Compress if it's large enough and not a response message
|
|
|
|
return !isResponse && msg.ProtoSize() >= compressionThreshold
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unknown compression setting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:31:09 +00:00
|
|
|
// Close is called when the connection is regularely closed and thus the Close
|
|
|
|
// BEP message is sent before terminating the actual connection. The error
|
|
|
|
// argument specifies the reason for closing the connection.
|
|
|
|
func (c *rawConnection) Close(err error) {
|
2019-01-14 07:32:37 +00:00
|
|
|
c.sendCloseOnce.Do(func() {
|
2019-01-09 16:31:09 +00:00
|
|
|
done := make(chan struct{})
|
2019-01-14 07:32:37 +00:00
|
|
|
c.send(&Close{err.Error()}, done)
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-c.closed:
|
|
|
|
}
|
2019-01-09 16:31:09 +00:00
|
|
|
})
|
2019-01-14 07:32:37 +00:00
|
|
|
|
|
|
|
// No more sends are necessary, therefore further steps to close the
|
|
|
|
// connection outside of this package can proceed immediately.
|
|
|
|
// And this prevents a potential deadlock due to calling c.receiver.Closed
|
|
|
|
go c.internalClose(err)
|
2019-01-09 16:31:09 +00:00
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2019-01-09 16:31:09 +00:00
|
|
|
// internalClose is called if there is an unexpected error during normal operation.
|
|
|
|
func (c *rawConnection) internalClose(err error) {
|
2019-01-14 07:32:37 +00:00
|
|
|
c.closeOnce.Do(func() {
|
|
|
|
l.Debugln("close due to", err)
|
|
|
|
close(c.closed)
|
|
|
|
|
|
|
|
c.awaitingMut.Lock()
|
|
|
|
for i, ch := range c.awaiting {
|
|
|
|
if ch != nil {
|
|
|
|
close(ch)
|
|
|
|
delete(c.awaiting, i)
|
|
|
|
}
|
2019-01-09 16:31:09 +00:00
|
|
|
}
|
2019-01-14 07:32:37 +00:00
|
|
|
c.awaitingMut.Unlock()
|
2019-01-09 16:31:09 +00:00
|
|
|
|
2019-01-14 07:32:37 +00:00
|
|
|
c.receiver.Closed(c, err)
|
|
|
|
})
|
2019-01-09 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:51:42 +00:00
|
|
|
// The pingSender makes sure that we've sent a message within the last
|
|
|
|
// PingSendInterval. If we already have something sent in the last
|
|
|
|
// PingSendInterval/2, we do nothing. Otherwise we send a ping message. This
|
|
|
|
// results in an effecting ping interval of somewhere between
|
|
|
|
// PingSendInterval/2 and PingSendInterval.
|
|
|
|
func (c *rawConnection) pingSender() {
|
2016-12-18 18:57:41 +00:00
|
|
|
ticker := time.NewTicker(PingSendInterval / 2)
|
|
|
|
defer ticker.Stop()
|
2015-09-21 06:51:42 +00:00
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
2016-12-18 18:57:41 +00:00
|
|
|
case <-ticker.C:
|
2015-09-21 06:51:42 +00:00
|
|
|
d := time.Since(c.cw.Last())
|
|
|
|
if d < PingSendInterval/2 {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping skipped after wr", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-09-21 06:51:42 +00:00
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping -> after", d)
|
2015-09-21 06:51:42 +00:00
|
|
|
c.ping()
|
|
|
|
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:54:22 +00:00
|
|
|
// The pingReceiver checks that we've received a message (any message will do,
|
2015-09-21 06:51:42 +00:00
|
|
|
// but we expect pings in the absence of other messages) within the last
|
|
|
|
// ReceiveTimeout. If not, we close the connection with an ErrTimeout.
|
|
|
|
func (c *rawConnection) pingReceiver() {
|
2016-12-18 18:57:41 +00:00
|
|
|
ticker := time.NewTicker(ReceiveTimeout / 2)
|
|
|
|
defer ticker.Stop()
|
2015-09-21 06:51:42 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2016-12-18 18:57:41 +00:00
|
|
|
case <-ticker.C:
|
2015-09-21 06:51:42 +00:00
|
|
|
d := time.Since(c.cr.Last())
|
|
|
|
if d > ReceiveTimeout {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping timeout", d)
|
2019-01-09 16:31:09 +00:00
|
|
|
c.internalClose(ErrTimeout)
|
2015-09-21 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "last read within", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Statistics struct {
|
|
|
|
At time.Time
|
2015-01-18 00:26:52 +00:00
|
|
|
InBytesTotal int64
|
|
|
|
OutBytesTotal int64
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) Statistics() Statistics {
|
|
|
|
return Statistics{
|
|
|
|
At: time.Now(),
|
|
|
|
InBytesTotal: c.cr.Tot(),
|
|
|
|
OutBytesTotal: c.cw.Tot(),
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
func (c *rawConnection) lz4Compress(src []byte) ([]byte, error) {
|
|
|
|
var err error
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(len(src))
|
2016-07-04 10:40:29 +00:00
|
|
|
buf, err = lz4.Encode(buf, src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(buf, binary.LittleEndian.Uint32(buf))
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) lz4Decompress(src []byte) ([]byte, error) {
|
|
|
|
size := binary.BigEndian.Uint32(src)
|
|
|
|
binary.LittleEndian.PutUint32(src, size)
|
|
|
|
var err error
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(int(size))
|
2016-07-04 10:40:29 +00:00
|
|
|
buf, err = lz4.Decode(buf, src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|