2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2021-05-08 10:52:06 +00:00
|
|
|
//go:generate -command counterfeiter go run github.com/maxbrunsfeld/counterfeiter/v6
|
|
|
|
|
2021-03-03 07:53:50 +00:00
|
|
|
// Prevents import loop, for internal testing
|
|
|
|
//go:generate counterfeiter -o mocked_connection_info_test.go --fake-name mockedConnectionInfo . ConnectionInfo
|
|
|
|
//go:generate go run ../../script/prune_mocks.go -t mocked_connection_info_test.go
|
|
|
|
|
|
|
|
//go:generate counterfeiter -o mocks/connection_info.go --fake-name ConnectionInfo . ConnectionInfo
|
|
|
|
//go:generate counterfeiter -o mocks/connection.go --fake-name Connection . Connection
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2019-11-19 08:56:53 +00:00
|
|
|
"context"
|
2018-04-16 18:08:50 +00:00
|
|
|
"crypto/sha256"
|
2014-09-22 19:42:11 +00:00
|
|
|
"encoding/binary"
|
2022-08-16 08:01:49 +00:00
|
|
|
"errors"
|
2014-09-22 19:42:11 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-12-21 10:40:51 +00:00
|
|
|
"net"
|
2016-12-01 12:35:32 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2014-09-22 19:42:11 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-01-17 17:52:43 +00:00
|
|
|
lz4 "github.com/pierrec/lz4/v4"
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2019-03-27 19:20:30 +00:00
|
|
|
var sha256OfEmptyBlock = map[int][sha256.Size]byte{
|
|
|
|
128 << KiB: {0xfa, 0x43, 0x23, 0x9b, 0xce, 0xe7, 0xb9, 0x7c, 0xa6, 0x2f, 0x0, 0x7c, 0xc6, 0x84, 0x87, 0x56, 0xa, 0x39, 0xe1, 0x9f, 0x74, 0xf3, 0xdd, 0xe7, 0x48, 0x6d, 0xb3, 0xf9, 0x8d, 0xf8, 0xe4, 0x71},
|
|
|
|
256 << KiB: {0x8a, 0x39, 0xd2, 0xab, 0xd3, 0x99, 0x9a, 0xb7, 0x3c, 0x34, 0xdb, 0x24, 0x76, 0x84, 0x9c, 0xdd, 0xf3, 0x3, 0xce, 0x38, 0x9b, 0x35, 0x82, 0x68, 0x50, 0xf9, 0xa7, 0x0, 0x58, 0x9b, 0x4a, 0x90},
|
|
|
|
512 << KiB: {0x7, 0x85, 0x4d, 0x2f, 0xef, 0x29, 0x7a, 0x6, 0xba, 0x81, 0x68, 0x5e, 0x66, 0xc, 0x33, 0x2d, 0xe3, 0x6d, 0x5d, 0x18, 0xd5, 0x46, 0x92, 0x7d, 0x30, 0xda, 0xad, 0x6d, 0x7f, 0xda, 0x15, 0x41},
|
|
|
|
1 << MiB: {0x30, 0xe1, 0x49, 0x55, 0xeb, 0xf1, 0x35, 0x22, 0x66, 0xdc, 0x2f, 0xf8, 0x6, 0x7e, 0x68, 0x10, 0x46, 0x7, 0xe7, 0x50, 0xab, 0xb9, 0xd3, 0xb3, 0x65, 0x82, 0xb8, 0xaf, 0x90, 0x9f, 0xcb, 0x58},
|
|
|
|
2 << MiB: {0x56, 0x47, 0xf0, 0x5e, 0xc1, 0x89, 0x58, 0x94, 0x7d, 0x32, 0x87, 0x4e, 0xeb, 0x78, 0x8f, 0xa3, 0x96, 0xa0, 0x5d, 0xb, 0xab, 0x7c, 0x1b, 0x71, 0xf1, 0x12, 0xce, 0xb7, 0xe9, 0xb3, 0x1e, 0xee},
|
|
|
|
4 << MiB: {0xbb, 0x9f, 0x8d, 0xf6, 0x14, 0x74, 0xd2, 0x5e, 0x71, 0xfa, 0x0, 0x72, 0x23, 0x18, 0xcd, 0x38, 0x73, 0x96, 0xca, 0x17, 0x36, 0x60, 0x5e, 0x12, 0x48, 0x82, 0x1c, 0xc0, 0xde, 0x3d, 0x3a, 0xf8},
|
|
|
|
8 << MiB: {0x2d, 0xae, 0xb1, 0xf3, 0x60, 0x95, 0xb4, 0x4b, 0x31, 0x84, 0x10, 0xb3, 0xf4, 0xe8, 0xb5, 0xd9, 0x89, 0xdc, 0xc7, 0xbb, 0x2, 0x3d, 0x14, 0x26, 0xc4, 0x92, 0xda, 0xb0, 0xa3, 0x5, 0x3e, 0x74},
|
|
|
|
16 << MiB: {0x8, 0xa, 0xcf, 0x35, 0xa5, 0x7, 0xac, 0x98, 0x49, 0xcf, 0xcb, 0xa4, 0x7d, 0xc2, 0xad, 0x83, 0xe0, 0x1b, 0x75, 0x66, 0x3a, 0x51, 0x62, 0x79, 0xc8, 0xb9, 0xd2, 0x43, 0xb7, 0x19, 0x64, 0x3e},
|
|
|
|
}
|
2018-04-16 18:08:50 +00:00
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
var errNotCompressible = errors.New("not compressible")
|
2022-01-17 17:52:43 +00:00
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
func init() {
|
|
|
|
for blockSize := MinBlockSize; blockSize <= MaxBlockSize; blockSize *= 2 {
|
|
|
|
BlockSizes = append(BlockSizes, blockSize)
|
2019-03-27 19:20:30 +00:00
|
|
|
if _, ok := sha256OfEmptyBlock[blockSize]; !ok {
|
|
|
|
panic("missing hard coded value for sha256 of empty block")
|
|
|
|
}
|
2018-04-16 18:08:50 +00:00
|
|
|
}
|
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
|
|
|
)
|
|
|
|
|
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 (
|
2019-11-06 06:09:58 +00:00
|
|
|
ErrClosed = errors.New("connection closed")
|
|
|
|
ErrTimeout = errors.New("read timeout")
|
|
|
|
errUnknownMessage = errors.New("unknown message")
|
|
|
|
errInvalidFilename = errors.New("filename is invalid")
|
|
|
|
errUncleanFilename = errors.New("filename not in canonical format")
|
|
|
|
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
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
Index(conn Connection, idx *Index) error
|
2014-09-28 11:00:38 +00:00
|
|
|
// An index update was received from the peer device
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
IndexUpdate(conn Connection, idxUp *IndexUpdate) error
|
2014-09-28 11:00:38 +00:00
|
|
|
// A request was made by the peer device
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
Request(conn Connection, req *Request) (RequestResponse, error)
|
2014-09-22 19:42:11 +00:00
|
|
|
// A cluster configuration message was received
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
ClusterConfig(conn Connection, config *ClusterConfig) error
|
2021-03-22 20:50:19 +00:00
|
|
|
// The peer device closed the connection or an error occurred
|
2023-07-29 08:24:44 +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
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
DownloadProgress(conn Connection, p *DownloadProgress) error
|
2023-07-29 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
// rawModel is the Model interface, but without the initial Connection
|
|
|
|
// parameter. Internal use only.
|
|
|
|
type rawModel interface {
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
Index(*Index) error
|
|
|
|
IndexUpdate(*IndexUpdate) error
|
|
|
|
Request(*Request) (RequestResponse, error)
|
|
|
|
ClusterConfig(*ClusterConfig) error
|
2023-07-29 08:24:44 +00:00
|
|
|
Closed(err error)
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
DownloadProgress(*DownloadProgress) error
|
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()
|
2021-03-22 20:50:19 +00:00
|
|
|
SetFolderPasswords(passwords map[string]string)
|
2019-01-09 16:31:09 +00:00
|
|
|
Close(err error)
|
2023-07-29 08:24:44 +00:00
|
|
|
DeviceID() DeviceID
|
2019-11-25 10:07:36 +00:00
|
|
|
Index(ctx context.Context, folder string, files []FileInfo) error
|
|
|
|
IndexUpdate(ctx context.Context, folder string, files []FileInfo) error
|
2020-11-09 14:33:32 +00:00
|
|
|
Request(ctx context.Context, folder string, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error)
|
2016-07-04 10:40:29 +00:00
|
|
|
ClusterConfig(config ClusterConfig)
|
2019-11-25 10:07:36 +00:00
|
|
|
DownloadProgress(ctx context.Context, folder string, updates []FileDownloadProgressUpdate)
|
2014-09-22 19:42:11 +00:00
|
|
|
Statistics() Statistics
|
2021-06-17 11:57:44 +00:00
|
|
|
Closed() <-chan struct{}
|
2020-12-21 10:40:51 +00:00
|
|
|
ConnectionInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionInfo interface {
|
|
|
|
Type() string
|
|
|
|
Transport() string
|
2022-11-28 08:28:33 +00:00
|
|
|
IsLocal() bool
|
2020-12-21 10:40:51 +00:00
|
|
|
RemoteAddr() net.Addr
|
|
|
|
Priority() int
|
|
|
|
String() string
|
|
|
|
Crypto() string
|
2021-01-05 16:45:07 +00:00
|
|
|
EstablishedAt() time.Time
|
2023-09-06 10:52:01 +00:00
|
|
|
ConnectionID() string
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rawConnection struct {
|
2020-12-21 10:40:51 +00:00
|
|
|
ConnectionInfo
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
deviceID DeviceID
|
2023-08-04 17:57:30 +00:00
|
|
|
idString string
|
2023-09-06 10:52:01 +00:00
|
|
|
model rawModel
|
2020-04-20 06:23:38 +00:00
|
|
|
startTime time.Time
|
2023-09-06 10:52:01 +00:00
|
|
|
started chan struct{}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2020-12-21 10:40:51 +00:00
|
|
|
cr *countingReader
|
|
|
|
cw *countingWriter
|
|
|
|
closer io.Closer // Closing the underlying connection and thus cr and cw
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2022-02-28 08:13:30 +00:00
|
|
|
awaitingMut sync.Mutex // Protects awaiting and nextID.
|
2020-10-02 06:07:05 +00:00
|
|
|
awaiting map[int]chan asyncResult
|
2022-02-28 08:13:30 +00:00
|
|
|
nextID int
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
idxMut sync.Mutex // ensures serialization of Index calls
|
|
|
|
|
2019-05-25 19:08:07 +00:00
|
|
|
inbox chan message
|
|
|
|
outbox chan asyncMessage
|
2019-06-05 06:01:59 +00:00
|
|
|
closeBox chan asyncMessage
|
2019-05-27 10:15:34 +00:00
|
|
|
clusterConfigBox chan *ClusterConfig
|
2019-05-25 19:08:07 +00:00
|
|
|
dispatcherLoopStopped chan struct{}
|
|
|
|
closed chan struct{}
|
|
|
|
closeOnce sync.Once
|
|
|
|
sendCloseOnce sync.Once
|
|
|
|
compression Compression
|
2023-09-12 12:48:15 +00:00
|
|
|
startStopMut sync.Mutex // start and stop must be serialized
|
2020-11-27 10:31:20 +00:00
|
|
|
|
|
|
|
loopWG sync.WaitGroup // Need to ensure no leftover routines in testing
|
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
|
|
|
)
|
|
|
|
|
2019-06-05 06:01:59 +00:00
|
|
|
// CloseTimeout is the longest we'll wait when trying to send the close
|
|
|
|
// message before just closing the connection.
|
|
|
|
// Should not be modified in production code, just for testing.
|
|
|
|
var CloseTimeout = 10 * time.Second
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, closer io.Closer, model Model, connInfo ConnectionInfo, compress Compression, passwords map[string]string, keyGen *KeyGenerator) Connection {
|
|
|
|
// We create the wrapper for the model first, as it needs to be passed
|
|
|
|
// in at the lowest level in the stack. At the end of construction,
|
|
|
|
// before returning, we add the connection to cwm so that it can be used
|
|
|
|
// by the model.
|
|
|
|
cwm := &connectionWrappingModel{model: model}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
// Encryption / decryption is first (outermost) before conversion to
|
|
|
|
// native path formats.
|
2023-07-29 08:24:44 +00:00
|
|
|
nm := makeNative(cwm)
|
2023-03-12 19:06:59 +00:00
|
|
|
em := newEncryptedModel(nm, newFolderKeyRegistry(keyGen, passwords), keyGen)
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
// We do the wire format conversion first (outermost) so that the
|
|
|
|
// metadata is in wire format when it reaches the encryption step.
|
2020-12-21 10:40:51 +00:00
|
|
|
rc := newRawConnection(deviceID, reader, writer, closer, em, connInfo, compress)
|
2023-03-12 19:06:59 +00:00
|
|
|
ec := newEncryptedConnection(rc, rc, em.folderKeys, keyGen)
|
2020-11-09 14:33:32 +00:00
|
|
|
wc := wireFormatConnection{ec}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
cwm.conn = wc
|
2020-11-09 14:33:32 +00:00
|
|
|
return wc
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
func newRawConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, closer io.Closer, receiver rawModel, connInfo ConnectionInfo, compress Compression) *rawConnection {
|
2023-08-04 17:57:30 +00:00
|
|
|
idString := deviceID.String()
|
|
|
|
cr := &countingReader{Reader: reader, idString: idString}
|
|
|
|
cw := &countingWriter{Writer: writer, idString: idString}
|
|
|
|
registerDeviceMetrics(idString)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
return &rawConnection{
|
2020-12-21 10:40:51 +00:00
|
|
|
ConnectionInfo: connInfo,
|
2023-07-29 08:24:44 +00:00
|
|
|
deviceID: deviceID,
|
2023-08-04 17:57:30 +00:00
|
|
|
idString: deviceID.String(),
|
2023-07-29 08:24:44 +00:00
|
|
|
model: receiver,
|
2023-09-06 10:52:01 +00:00
|
|
|
started: make(chan struct{}),
|
2019-05-25 19:08:07 +00:00
|
|
|
cr: cr,
|
|
|
|
cw: cw,
|
2020-12-21 10:40:51 +00:00
|
|
|
closer: closer,
|
2020-10-02 06:07:05 +00:00
|
|
|
awaiting: make(map[int]chan asyncResult),
|
2019-05-25 19:08:07 +00:00
|
|
|
inbox: make(chan message),
|
|
|
|
outbox: make(chan asyncMessage),
|
2019-06-05 06:01:59 +00:00
|
|
|
closeBox: make(chan asyncMessage),
|
2019-05-27 10:15:34 +00:00
|
|
|
clusterConfigBox: make(chan *ClusterConfig),
|
2019-05-25 19:08:07 +00:00
|
|
|
dispatcherLoopStopped: make(chan struct{}),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
compression: compress,
|
2020-11-27 10:31:20 +00:00
|
|
|
loopWG: sync.WaitGroup{},
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2015-07-10 06:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates the goroutines for sending and receiving of messages. It must
|
|
|
|
// be called exactly once after creating a connection.
|
|
|
|
func (c *rawConnection) Start() {
|
2023-09-12 12:48:15 +00:00
|
|
|
c.startStopMut.Lock()
|
|
|
|
defer c.startStopMut.Unlock()
|
2020-11-27 10:31:20 +00:00
|
|
|
c.loopWG.Add(5)
|
|
|
|
go func() {
|
|
|
|
c.readerLoop()
|
|
|
|
c.loopWG.Done()
|
|
|
|
}()
|
2019-02-02 10:45:17 +00:00
|
|
|
go func() {
|
2019-05-25 19:08:07 +00:00
|
|
|
err := c.dispatcherLoop()
|
2020-11-27 10:31:20 +00:00
|
|
|
c.Close(err)
|
|
|
|
c.loopWG.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
c.writerLoop()
|
|
|
|
c.loopWG.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
c.pingSender()
|
|
|
|
c.loopWG.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
c.pingReceiver()
|
|
|
|
c.loopWG.Done()
|
2019-02-02 10:45:17 +00:00
|
|
|
}()
|
2021-03-12 09:35:10 +00:00
|
|
|
c.startTime = time.Now().Truncate(time.Second)
|
2023-09-06 10:52:01 +00:00
|
|
|
close(c.started)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (c *rawConnection) DeviceID() DeviceID {
|
|
|
|
return c.deviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// Index writes the list of file information to the connected peer device
|
2019-11-25 10:07:36 +00:00
|
|
|
func (c *rawConnection) Index(ctx context.Context, folder string, idx []FileInfo) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
c.send(ctx, &Index{
|
2016-07-04 10:40:29 +00:00
|
|
|
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
|
2019-11-25 10:07:36 +00:00
|
|
|
func (c *rawConnection) IndexUpdate(ctx context.Context, folder string, idx []FileInfo) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
c.send(ctx, &IndexUpdate{
|
2016-07-04 10:40:29 +00:00
|
|
|
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.
|
2020-11-09 14:33:32 +00:00
|
|
|
func (c *rawConnection) Request(ctx context.Context, folder string, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
|
2022-02-28 08:13:30 +00:00
|
|
|
rc := make(chan asyncResult, 1)
|
2016-04-15 10:59:41 +00:00
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
c.awaitingMut.Lock()
|
2022-02-28 08:13:30 +00:00
|
|
|
id := c.nextID
|
|
|
|
c.nextID++
|
2016-07-04 10:40:29 +00:00
|
|
|
if _, ok := c.awaiting[id]; ok {
|
2020-04-20 12:52:16 +00:00
|
|
|
c.awaitingMut.Unlock()
|
2014-09-22 19:42:11 +00:00
|
|
|
panic("id taken")
|
|
|
|
}
|
|
|
|
c.awaiting[id] = rc
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
|
2019-11-19 08:56:53 +00:00
|
|
|
ok := c.send(ctx, &Request{
|
2016-07-04 10:40:29 +00:00
|
|
|
ID: id,
|
|
|
|
Folder: folder,
|
|
|
|
Name: name,
|
|
|
|
Offset: offset,
|
2020-10-02 06:07:05 +00:00
|
|
|
Size: size,
|
2020-11-09 14:33:32 +00:00
|
|
|
BlockNo: blockNo,
|
2016-07-04 10:40:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-19 08:56:53 +00:00
|
|
|
select {
|
|
|
|
case res, ok := <-rc:
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
return res.val, res.err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 10:15:34 +00:00
|
|
|
// ClusterConfig sends the cluster configuration message to the peer.
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *rawConnection) ClusterConfig(config ClusterConfig) {
|
2019-05-29 10:14:00 +00:00
|
|
|
select {
|
|
|
|
case c.clusterConfigBox <- &config:
|
|
|
|
case <-c.closed:
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:57:44 +00:00
|
|
|
func (c *rawConnection) Closed() <-chan struct{} {
|
|
|
|
return c.closed
|
2016-01-11 15:49:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
// DownloadProgress sends the progress updates for the files that are currently being downloaded.
|
2019-11-25 10:07:36 +00:00
|
|
|
func (c *rawConnection) DownloadProgress(ctx context.Context, folder string, updates []FileDownloadProgressUpdate) {
|
|
|
|
c.send(ctx, &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 {
|
2019-11-19 08:56:53 +00:00
|
|
|
return c.send(context.Background(), &Ping{}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:08:07 +00:00
|
|
|
func (c *rawConnection) readerLoop() {
|
2018-11-13 07:53:55 +00:00
|
|
|
fourByteBuf := make([]byte, 4)
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
2019-05-25 19:08:07 +00:00
|
|
|
msg, err := c.readMessage(fourByteBuf)
|
|
|
|
if err != nil {
|
|
|
|
if err == errUnknownMessage {
|
|
|
|
// Unknown message types are skipped, for future extensibility.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.internalClose(err)
|
|
|
|
return
|
|
|
|
}
|
2019-05-02 08:21:07 +00:00
|
|
|
select {
|
2019-05-25 19:08:07 +00:00
|
|
|
case c.inbox <- msg:
|
2019-05-02 08:21:07 +00:00
|
|
|
case <-c.closed:
|
2019-05-25 19:08:07 +00:00
|
|
|
return
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2019-05-08 06:08:26 +00:00
|
|
|
|
2019-05-25 19:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2019-05-25 19:08:07 +00:00
|
|
|
func (c *rawConnection) dispatcherLoop() (err error) {
|
|
|
|
defer close(c.dispatcherLoopStopped)
|
|
|
|
var msg message
|
|
|
|
state := stateInitial
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg = <-c.inbox:
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
}
|
2021-03-15 18:14:09 +00:00
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
metricDeviceRecvMessages.WithLabelValues(c.idString).Inc()
|
|
|
|
|
2021-03-15 18:14:09 +00:00
|
|
|
msgContext, err := messageContext(msg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("protocol error: %w", err)
|
|
|
|
}
|
|
|
|
l.Debugf("handle %v message", msgContext)
|
|
|
|
|
2019-05-08 06:08:26 +00:00
|
|
|
switch msg := msg.(type) {
|
2016-07-04 10:40:29 +00:00
|
|
|
case *ClusterConfig:
|
2020-10-02 09:49:51 +00:00
|
|
|
if state == stateInitial {
|
|
|
|
state = stateReady
|
2015-06-26 13:38:56 +00:00
|
|
|
}
|
2021-03-15 18:14:09 +00:00
|
|
|
case *Close:
|
|
|
|
return fmt.Errorf("closed by remote: %v", msg.Reason)
|
|
|
|
default:
|
|
|
|
if state != stateReady {
|
|
|
|
return newProtocolError(fmt.Errorf("invalid state %d", state), msgContext)
|
2019-12-04 09:46:55 +00:00
|
|
|
}
|
2021-03-15 18:14:09 +00:00
|
|
|
}
|
2015-06-26 13:38:56 +00:00
|
|
|
|
2021-03-15 18:14:09 +00:00
|
|
|
switch msg := msg.(type) {
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Index:
|
2021-03-15 18:14:09 +00:00
|
|
|
err = checkIndexConsistency(msg.Files)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
case *IndexUpdate:
|
2021-03-15 18:14:09 +00:00
|
|
|
err = checkIndexConsistency(msg.Files)
|
|
|
|
|
|
|
|
case *Request:
|
|
|
|
err = checkFilename(msg.Name)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return newProtocolError(err, msgContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *ClusterConfig:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
err = c.model.ClusterConfig(msg)
|
2021-03-15 18:14:09 +00:00
|
|
|
|
|
|
|
case *Index:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
err = c.handleIndex(msg)
|
2021-03-15 18:14:09 +00:00
|
|
|
|
|
|
|
case *IndexUpdate:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
err = c.handleIndexUpdate(msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Request:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
go c.handleRequest(msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Response:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
c.handleResponse(msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case *DownloadProgress:
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
err = c.model.DownloadProgress(msg)
|
2021-03-15 18:14:09 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return newHandleError(err, msgContext)
|
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 {
|
2022-08-16 08:01:49 +00:00
|
|
|
return nil, fmt.Errorf("reading message length: %w", 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)
|
2019-11-06 06:09:58 +00:00
|
|
|
} else if msgLen > MaxMessageLen {
|
|
|
|
return nil, fmt.Errorf("message length %d exceeds maximum %d", msgLen, MaxMessageLen)
|
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 {
|
2021-02-27 07:55:51 +00:00
|
|
|
BufferPool.Put(buf)
|
2022-08-16 08:01:49 +00:00
|
|
|
return nil, fmt.Errorf("reading message: %w", 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:
|
2021-06-27 15:59:30 +00:00
|
|
|
decomp, err := lz4Decompress(buf)
|
2018-11-13 07:53:55 +00:00
|
|
|
BufferPool.Put(buf)
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return nil, fmt.Errorf("decompressing message: %w", 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
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
metricDeviceRecvDecompressedBytes.WithLabelValues(c.idString).Add(float64(4 + len(buf)))
|
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
msg, err := newMessage(hdr.Type)
|
2016-07-04 10:40:29 +00:00
|
|
|
if err != nil {
|
2021-02-27 07:55:51 +00:00
|
|
|
BufferPool.Put(buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := msg.Unmarshal(buf); err != nil {
|
2021-02-27 07:55:51 +00:00
|
|
|
BufferPool.Put(buf)
|
2022-08-16 08:01:49 +00:00
|
|
|
return nil, fmt.Errorf("unmarshalling message: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
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 {
|
2022-08-16 08:01:49 +00:00
|
|
|
return Header{}, fmt.Errorf("reading length: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
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 {
|
2021-02-27 07:55:51 +00:00
|
|
|
BufferPool.Put(buf)
|
2022-08-16 08:01:49 +00:00
|
|
|
return Header{}, fmt.Errorf("reading header: %w", err)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
var hdr Header
|
2021-02-27 07:55:51 +00:00
|
|
|
err := hdr.Unmarshal(buf)
|
|
|
|
BufferPool.Put(buf)
|
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return Header{}, fmt.Errorf("unmarshalling header: %w", err)
|
2016-02-01 08:10:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
metricDeviceRecvDecompressedBytes.WithLabelValues(c.idString).Add(float64(2 + len(buf)))
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
return hdr, nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *rawConnection) handleIndex(im *Index) error {
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugf("Index(%v, %v, %d file)", c.deviceID, im.Folder, len(im.Files))
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return c.model.Index(im)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *rawConnection) handleIndexUpdate(im *IndexUpdate) error {
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugf("queueing IndexUpdate(%v, %v, %d files)", c.deviceID, im.Folder, len(im.Files))
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return c.model.IndexUpdate(im)
|
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 {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("%q: %w", 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
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *rawConnection) handleRequest(req *Request) {
|
|
|
|
res, err := c.model.Request(req)
|
2015-07-29 20:23:43 +00:00
|
|
|
if err != nil {
|
2019-11-19 08:56:53 +00:00
|
|
|
c.send(context.Background(), &Response{
|
2016-07-04 10:40:29 +00:00
|
|
|
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{})
|
2019-11-19 08:56:53 +00:00
|
|
|
c.send(context.Background(), &Response{
|
2018-11-13 07:53:55 +00:00
|
|
|
ID: req.ID,
|
|
|
|
Data: res.Data(),
|
|
|
|
Code: errorToCode(nil),
|
|
|
|
}, done)
|
|
|
|
<-done
|
|
|
|
res.Close()
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +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()
|
|
|
|
}
|
|
|
|
|
2019-11-19 08:56:53 +00:00
|
|
|
func (c *rawConnection) send(ctx context.Context, 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:
|
2019-11-19 08:56:53 +00:00
|
|
|
case <-ctx.Done():
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2019-07-14 09:03:55 +00:00
|
|
|
if done != nil {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
return false
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 06:08:26 +00:00
|
|
|
func (c *rawConnection) writerLoop() {
|
2019-05-27 10:15:34 +00:00
|
|
|
select {
|
|
|
|
case cc := <-c.clusterConfigBox:
|
|
|
|
err := c.writeMessage(cc)
|
|
|
|
if err != nil {
|
|
|
|
c.internalClose(err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-05 06:01:59 +00:00
|
|
|
case hm := <-c.closeBox:
|
|
|
|
_ = c.writeMessage(hm.msg)
|
|
|
|
close(hm.done)
|
|
|
|
return
|
2019-05-27 10:15:34 +00:00
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-10-02 09:49:51 +00:00
|
|
|
case cc := <-c.clusterConfigBox:
|
|
|
|
err := c.writeMessage(cc)
|
|
|
|
if err != nil {
|
|
|
|
c.internalClose(err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
case hm := <-c.outbox:
|
2019-05-02 12:09:19 +00:00
|
|
|
err := c.writeMessage(hm.msg)
|
2018-11-13 07:53:55 +00:00
|
|
|
if hm.done != nil {
|
|
|
|
close(hm.done)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-05-08 06:08:26 +00:00
|
|
|
c.internalClose(err)
|
|
|
|
return
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2019-06-05 06:01:59 +00:00
|
|
|
case hm := <-c.closeBox:
|
|
|
|
_ = c.writeMessage(hm.msg)
|
|
|
|
close(hm.done)
|
|
|
|
return
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
case <-c.closed:
|
2019-05-08 06:08:26 +00:00
|
|
|
return
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 12:09:19 +00:00
|
|
|
func (c *rawConnection) writeMessage(msg message) error {
|
2021-03-22 20:50:19 +00:00
|
|
|
msgContext, _ := messageContext(msg)
|
|
|
|
l.Debugf("Writing %v", msgContext)
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
defer func() {
|
|
|
|
metricDeviceSentMessages.WithLabelValues(c.idString).Inc()
|
|
|
|
}()
|
|
|
|
|
2019-05-02 12:09:19 +00:00
|
|
|
size := msg.ProtoSize()
|
2016-07-04 10:40:29 +00:00
|
|
|
hdr := Header{
|
2022-01-24 18:36:58 +00:00
|
|
|
Type: typeOf(msg),
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
hdrSize := hdr.ProtoSize()
|
|
|
|
if hdrSize > 1<<16-1 {
|
|
|
|
panic("impossibly large header")
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:59:30 +00:00
|
|
|
overhead := 2 + hdrSize + 4
|
|
|
|
totSize := overhead + size
|
|
|
|
buf := BufferPool.Get(totSize)
|
|
|
|
defer BufferPool.Put(buf)
|
|
|
|
|
|
|
|
// Message
|
|
|
|
if _, err := msg.MarshalTo(buf[2+hdrSize+4:]); err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("marshalling message: %w", err)
|
2021-06-27 15:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.shouldCompressMessage(msg) {
|
2022-01-24 18:36:58 +00:00
|
|
|
ok, err := c.writeCompressedMessage(msg, buf[overhead:])
|
2021-06-27 15:59:30 +00:00
|
|
|
if ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
metricDeviceSentUncompressedBytes.WithLabelValues(c.idString).Add(float64(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 {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("marshalling header: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
// Message length
|
2021-06-27 15:59:30 +00:00
|
|
|
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(size))
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
n, err := c.cw.Write(buf)
|
|
|
|
|
2021-06-27 15:59:30 +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)
|
2016-07-04 10:40:29 +00:00
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("writing message: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
// Write msg out compressed, given its uncompressed marshaled payload.
|
2021-06-27 15:59:30 +00:00
|
|
|
//
|
|
|
|
// The first return value indicates whether compression succeeded.
|
|
|
|
// If not, the caller should retry without compression.
|
2022-01-24 18:36:58 +00:00
|
|
|
func (c *rawConnection) writeCompressedMessage(msg message, marshaled []byte) (ok bool, err error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
hdr := Header{
|
2022-01-24 18:36:58 +00:00
|
|
|
Type: typeOf(msg),
|
2021-06-27 15:59:30 +00:00
|
|
|
Compression: MessageCompressionLZ4,
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
hdrSize := hdr.ProtoSize()
|
|
|
|
if hdrSize > 1<<16-1 {
|
|
|
|
panic("impossibly large header")
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:59:30 +00:00
|
|
|
cOverhead := 2 + hdrSize + 4
|
2023-08-04 17:57:30 +00:00
|
|
|
|
|
|
|
metricDeviceSentUncompressedBytes.WithLabelValues(c.idString).Add(float64(cOverhead + len(marshaled)))
|
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
// The compressed size may be at most n-n/32 = .96875*n bytes,
|
|
|
|
// I.e., if we can't save at least 3.125% bandwidth, we forgo compression.
|
|
|
|
// This number is arbitrary but cheap to compute.
|
|
|
|
maxCompressed := cOverhead + len(marshaled) - len(marshaled)/32
|
2021-06-27 15:59:30 +00:00
|
|
|
buf := BufferPool.Get(maxCompressed)
|
|
|
|
defer BufferPool.Put(buf)
|
|
|
|
|
|
|
|
compressedSize, err := lz4Compress(marshaled, buf[cOverhead:])
|
|
|
|
totSize := compressedSize + cOverhead
|
2022-01-24 18:36:58 +00:00
|
|
|
if err != nil {
|
2021-06-27 15:59:30 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
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 {
|
2022-08-16 08:01:49 +00:00
|
|
|
return true, fmt.Errorf("marshalling header: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
// Message length
|
2021-06-27 15:59:30 +00:00
|
|
|
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(compressedSize))
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
n, err := c.cw.Write(buf[:totSize])
|
2021-06-27 15:59:30 +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, compressedSize, len(marshaled), err)
|
2016-07-04 10:40:29 +00:00
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return true, fmt.Errorf("writing message: %w", err)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2021-06-27 15:59:30 +00:00
|
|
|
return true, nil
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
func typeOf(msg message) MessageType {
|
2016-07-04 10:40:29 +00:00
|
|
|
switch msg.(type) {
|
|
|
|
case *ClusterConfig:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeClusterConfig
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Index:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeIndex
|
2016-07-04 10:40:29 +00:00
|
|
|
case *IndexUpdate:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeIndexUpdate
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Request:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeRequest
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Response:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeResponse
|
2016-07-04 10:40:29 +00:00
|
|
|
case *DownloadProgress:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeDownloadProgress
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Ping:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypePing
|
2016-07-04 10:40:29 +00:00
|
|
|
case *Close:
|
2020-10-02 06:07:05 +00:00
|
|
|
return MessageTypeClose
|
2016-07-04 10:40:29 +00:00
|
|
|
default:
|
|
|
|
panic("bug: unknown message type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
func newMessage(t MessageType) (message, error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
switch t {
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeClusterConfig:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(ClusterConfig), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeIndex:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(Index), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeIndexUpdate:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(IndexUpdate), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeRequest:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(Request), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeResponse:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(Response), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeDownloadProgress:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(DownloadProgress), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypePing:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(Ping), nil
|
2020-10-02 06:07:05 +00:00
|
|
|
case MessageTypeClose:
|
2016-07-04 10:40:29 +00:00
|
|
|
return new(Close), nil
|
|
|
|
default:
|
|
|
|
return nil, errUnknownMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) shouldCompressMessage(msg message) bool {
|
|
|
|
switch c.compression {
|
2020-10-02 06:07:05 +00:00
|
|
|
case CompressionNever:
|
2016-07-04 10:40:29 +00:00
|
|
|
return false
|
|
|
|
|
2020-10-02 06:07:05 +00:00
|
|
|
case CompressionAlways:
|
2016-07-04 10:40:29 +00:00
|
|
|
// Use compression for large enough messages
|
|
|
|
return msg.ProtoSize() >= compressionThreshold
|
|
|
|
|
2020-10-02 06:07:05 +00:00
|
|
|
case CompressionMetadata:
|
2016-07-04 10:40:29 +00:00
|
|
|
_, 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-06-05 06:01:59 +00:00
|
|
|
timeout := time.NewTimer(CloseTimeout)
|
2019-01-14 07:32:37 +00:00
|
|
|
select {
|
2019-06-05 06:01:59 +00:00
|
|
|
case c.closeBox <- asyncMessage{&Close{err.Error()}, done}:
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-timeout.C:
|
|
|
|
case <-c.closed:
|
|
|
|
}
|
|
|
|
case <-timeout.C:
|
2019-01-14 07:32:37 +00:00
|
|
|
case <-c.closed:
|
|
|
|
}
|
2019-01-09 16:31:09 +00:00
|
|
|
})
|
2019-01-14 07:32:37 +00:00
|
|
|
|
2019-06-14 17:04:41 +00:00
|
|
|
// Close might be called from a method that is called from within
|
|
|
|
// dispatcherLoop, resulting in a deadlock.
|
|
|
|
// The sending above must happen before spawning the routine, to prevent
|
|
|
|
// the underlying connection from terminating before sending the close msg.
|
|
|
|
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) {
|
2023-09-12 12:48:15 +00:00
|
|
|
c.startStopMut.Lock()
|
|
|
|
defer c.startStopMut.Unlock()
|
2019-01-14 07:32:37 +00:00
|
|
|
c.closeOnce.Do(func() {
|
2023-09-06 10:52:01 +00:00
|
|
|
l.Debugf("close connection to %s at %s due to %v", c.deviceID.Short(), c.ConnectionInfo, err)
|
2020-12-21 10:40:51 +00:00
|
|
|
if cerr := c.closer.Close(); cerr != nil {
|
2023-09-06 10:52:01 +00:00
|
|
|
l.Debugf("failed to close underlying conn %s at %s %v:", c.deviceID.Short(), c.ConnectionInfo, cerr)
|
2020-12-21 10:40:51 +00:00
|
|
|
}
|
2019-01-14 07:32:37 +00:00
|
|
|
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
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
if !c.startTime.IsZero() {
|
|
|
|
// Wait for the dispatcher loop to exit, if it was started to
|
|
|
|
// begin with.
|
|
|
|
<-c.dispatcherLoopStopped
|
|
|
|
}
|
2019-05-25 19:08:07 +00:00
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
c.model.Closed(err)
|
2019-01-14 07:32:37 +00:00
|
|
|
})
|
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.
|
2019-05-08 06:08:26 +00:00
|
|
|
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 {
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugln(c.deviceID, "ping skipped after wr", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-09-21 06:51:42 +00:00
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugln(c.deviceID, "ping -> after", d)
|
2015-09-21 06:51:42 +00:00
|
|
|
c.ping()
|
|
|
|
|
|
|
|
case <-c.closed:
|
2019-05-08 06:08:26 +00:00
|
|
|
return
|
2015-09-21 06:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2019-05-08 06:08:26 +00:00
|
|
|
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 {
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugln(c.deviceID, "ping timeout", d)
|
2019-05-08 06:08:26 +00:00
|
|
|
c.internalClose(ErrTimeout)
|
2015-09-21 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
l.Debugln(c.deviceID, "last read within", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
case <-c.closed:
|
2019-05-08 06:08:26 +00:00
|
|
|
return
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Statistics struct {
|
2022-01-10 09:26:45 +00:00
|
|
|
At time.Time `json:"at"`
|
|
|
|
InBytesTotal int64 `json:"inBytesTotal"`
|
|
|
|
OutBytesTotal int64 `json:"outBytesTotal"`
|
|
|
|
StartedAt time.Time `json:"startedAt"`
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) Statistics() Statistics {
|
|
|
|
return Statistics{
|
2021-03-12 09:35:10 +00:00
|
|
|
At: time.Now().Truncate(time.Second),
|
2014-09-22 19:42:11 +00:00
|
|
|
InBytesTotal: c.cr.Tot(),
|
|
|
|
OutBytesTotal: c.cw.Tot(),
|
2020-04-20 06:23:38 +00:00
|
|
|
StartedAt: c.startTime,
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2021-06-27 15:59:30 +00:00
|
|
|
func lz4Compress(src, buf []byte) (int, error) {
|
2022-01-17 17:52:43 +00:00
|
|
|
n, err := lz4.CompressBlock(src, buf[4:], nil)
|
2016-07-04 10:40:29 +00:00
|
|
|
if err != nil {
|
2021-06-27 15:59:30 +00:00
|
|
|
return -1, err
|
2022-01-24 18:36:58 +00:00
|
|
|
} else if n == 0 {
|
2022-01-17 17:52:43 +00:00
|
|
|
return -1, errNotCompressible
|
2019-11-11 08:36:31 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2022-01-24 18:36:58 +00:00
|
|
|
// The compressed block is prefixed by the size of the uncompressed data.
|
|
|
|
binary.BigEndian.PutUint32(buf, uint32(len(src)))
|
|
|
|
|
2022-01-17 17:52:43 +00:00
|
|
|
return n + 4, nil
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:59:30 +00:00
|
|
|
func lz4Decompress(src []byte) ([]byte, error) {
|
2016-07-04 10:40:29 +00:00
|
|
|
size := binary.BigEndian.Uint32(src)
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := BufferPool.Get(int(size))
|
2022-01-17 17:52:43 +00:00
|
|
|
|
|
|
|
n, err := lz4.UncompressBlock(src[4:], buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
if err != nil {
|
2021-02-27 07:55:51 +00:00
|
|
|
BufferPool.Put(buf)
|
2016-07-04 10:40:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-17 17:52:43 +00:00
|
|
|
|
|
|
|
return buf[:n], nil
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2021-03-15 18:14:09 +00:00
|
|
|
|
|
|
|
func newProtocolError(err error, msgContext string) error {
|
|
|
|
return fmt.Errorf("protocol error on %v: %w", msgContext, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHandleError(err error, msgContext string) error {
|
|
|
|
return fmt.Errorf("handling %v: %w", msgContext, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func messageContext(msg message) (string, error) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *ClusterConfig:
|
|
|
|
return "cluster-config", nil
|
|
|
|
case *Index:
|
|
|
|
return fmt.Sprintf("index for %v", msg.Folder), nil
|
|
|
|
case *IndexUpdate:
|
|
|
|
return fmt.Sprintf("index-update for %v", msg.Folder), nil
|
|
|
|
case *Request:
|
|
|
|
return fmt.Sprintf(`request for "%v" in %v`, msg.Name, msg.Folder), nil
|
|
|
|
case *Response:
|
|
|
|
return "response", nil
|
|
|
|
case *DownloadProgress:
|
|
|
|
return fmt.Sprintf("download-progress for %v", msg.Folder), nil
|
|
|
|
case *Ping:
|
|
|
|
return "ping", nil
|
|
|
|
case *Close:
|
|
|
|
return "close", nil
|
|
|
|
default:
|
|
|
|
return "", errors.New("unknown or empty message")
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 08:24:44 +00:00
|
|
|
|
|
|
|
// connectionWrappingModel takes the Model interface from the model package,
|
|
|
|
// which expects the Connection as the first parameter in all methods, and
|
2023-09-06 10:52:01 +00:00
|
|
|
// wraps it to conform to the rawModel interface.
|
2023-07-29 08:24:44 +00:00
|
|
|
type connectionWrappingModel struct {
|
|
|
|
conn Connection
|
|
|
|
model Model
|
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *connectionWrappingModel) Index(m *Index) error {
|
|
|
|
return c.model.Index(c.conn, m)
|
2023-07-29 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *connectionWrappingModel) IndexUpdate(idxUp *IndexUpdate) error {
|
|
|
|
return c.model.IndexUpdate(c.conn, idxUp)
|
2023-07-29 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *connectionWrappingModel) Request(req *Request) (RequestResponse, error) {
|
|
|
|
return c.model.Request(c.conn, req)
|
2023-07-29 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *connectionWrappingModel) ClusterConfig(config *ClusterConfig) error {
|
2023-07-29 08:24:44 +00:00
|
|
|
return c.model.ClusterConfig(c.conn, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connectionWrappingModel) Closed(err error) {
|
|
|
|
c.model.Closed(c.conn, err)
|
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (c *connectionWrappingModel) DownloadProgress(p *DownloadProgress) error {
|
|
|
|
return c.model.DownloadProgress(c.conn, p)
|
2023-07-29 08:24:44 +00:00
|
|
|
}
|