lib/protocol: Understand older/newer Hello messages (fixes #3287)
This is in preparation for future changes, but also improves the
handling when talking to pre-v0.13 clients. It breaks out the Hello
message and magic from the rest of the protocol implementation, with the
intention that this small part of the protocol will survive future
changes.
To enable this, and future testing, the new ExchangeHello function takes
an interface that can be implemented by future Hello versions and
returns a version indendent result type. It correctly detects pre-v0.13
protocols and returns a "too old" error message which gets logged to the
user at warning level:
[I6KAH] 09:21:36 WARNING: Connecting to [...]:
the remote device speaks an older version of the protocol (v0.12) not
compatible with this version
Conversely, something entirely unknown will generate:
[I6KAH] 09:40:27 WARNING: Connecting to [...]:
the remote device speaks an unknown (newer?) version of the protocol
The intention is that in future iterations the Hello exchange will
succeed on at least one side and ExchangeHello will return the actual
data from the Hello together with ErrTooOld and an even more precise
message can be generated.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3289
This commit is contained in:
committed by
Audrius Butkevicius
parent
9a25df01fe
commit
d507126101
@@ -10,82 +10,6 @@ import (
|
||||
|
||||
/*
|
||||
|
||||
HelloMessage Structure:
|
||||
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
/ /
|
||||
\ Device Name (length + padded data) \
|
||||
/ /
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
/ /
|
||||
\ Client Name (length + padded data) \
|
||||
/ /
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
/ /
|
||||
\ Client Version (length + padded data) \
|
||||
/ /
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
|
||||
struct HelloMessage {
|
||||
string DeviceName<64>;
|
||||
string ClientName<64>;
|
||||
string ClientVersion<64>;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
func (o HelloMessage) XDRSize() int {
|
||||
return 4 + len(o.DeviceName) + xdr.Padding(len(o.DeviceName)) +
|
||||
4 + len(o.ClientName) + xdr.Padding(len(o.ClientName)) +
|
||||
4 + len(o.ClientVersion) + xdr.Padding(len(o.ClientVersion))
|
||||
}
|
||||
|
||||
func (o HelloMessage) MarshalXDR() ([]byte, error) {
|
||||
buf := make([]byte, o.XDRSize())
|
||||
m := &xdr.Marshaller{Data: buf}
|
||||
return buf, o.MarshalXDRInto(m)
|
||||
}
|
||||
|
||||
func (o HelloMessage) MustMarshalXDR() []byte {
|
||||
bs, err := o.MarshalXDR()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
func (o HelloMessage) MarshalXDRInto(m *xdr.Marshaller) error {
|
||||
if l := len(o.DeviceName); l > 64 {
|
||||
return xdr.ElementSizeExceeded("DeviceName", l, 64)
|
||||
}
|
||||
m.MarshalString(o.DeviceName)
|
||||
if l := len(o.ClientName); l > 64 {
|
||||
return xdr.ElementSizeExceeded("ClientName", l, 64)
|
||||
}
|
||||
m.MarshalString(o.ClientName)
|
||||
if l := len(o.ClientVersion); l > 64 {
|
||||
return xdr.ElementSizeExceeded("ClientVersion", l, 64)
|
||||
}
|
||||
m.MarshalString(o.ClientVersion)
|
||||
return m.Error
|
||||
}
|
||||
|
||||
func (o *HelloMessage) UnmarshalXDR(bs []byte) error {
|
||||
u := &xdr.Unmarshaller{Data: bs}
|
||||
return o.UnmarshalXDRFrom(u)
|
||||
}
|
||||
func (o *HelloMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
|
||||
o.DeviceName = u.UnmarshalStringMax(64)
|
||||
o.ClientName = u.UnmarshalStringMax(64)
|
||||
o.ClientVersion = u.UnmarshalStringMax(64)
|
||||
return u.Error
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
IndexMessage Structure:
|
||||
|
||||
0 1 2 3
|
||||
|
||||
Reference in New Issue
Block a user