protocol: Add "Hello" message at connection start, also for unauthed peers
This commit is contained in:
@@ -11,7 +11,16 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
|
||||
var (
|
||||
sha256OfEmptyBlock = sha256.Sum256(make([]byte, BlockSize))
|
||||
HelloMessageMagic uint32 = 0x9F79BC40
|
||||
)
|
||||
|
||||
type HelloMessage struct {
|
||||
DeviceName string // max:64
|
||||
ClientName string // max:64
|
||||
ClientVersion string // max:64
|
||||
}
|
||||
|
||||
type IndexMessage struct {
|
||||
Folder string // max:256
|
||||
@@ -125,11 +134,8 @@ type ResponseMessage struct {
|
||||
}
|
||||
|
||||
type ClusterConfigMessage struct {
|
||||
DeviceName string // max:64
|
||||
ClientName string // max:64
|
||||
ClientVersion string // max:64
|
||||
Folders []Folder // max:1000000
|
||||
Options []Option // max:64
|
||||
Folders []Folder // max:1000000
|
||||
Options []Option // max:64
|
||||
}
|
||||
|
||||
func (o *ClusterConfigMessage) GetOption(key string) string {
|
||||
|
||||
@@ -10,6 +10,82 @@ 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
|
||||
@@ -506,18 +582,6 @@ ClusterConfigMessage 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) \
|
||||
/ /
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
| Number of Folders |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
/ /
|
||||
@@ -533,9 +597,6 @@ ClusterConfigMessage Structure:
|
||||
|
||||
|
||||
struct ClusterConfigMessage {
|
||||
string DeviceName<64>;
|
||||
string ClientName<64>;
|
||||
string ClientVersion<64>;
|
||||
Folder Folders<1000000>;
|
||||
Option Options<64>;
|
||||
}
|
||||
@@ -543,10 +604,7 @@ struct ClusterConfigMessage {
|
||||
*/
|
||||
|
||||
func (o ClusterConfigMessage) 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)) +
|
||||
4 + xdr.SizeOfSlice(o.Folders) +
|
||||
return 4 + xdr.SizeOfSlice(o.Folders) +
|
||||
4 + xdr.SizeOfSlice(o.Options)
|
||||
}
|
||||
|
||||
@@ -565,18 +623,6 @@ func (o ClusterConfigMessage) MustMarshalXDR() []byte {
|
||||
}
|
||||
|
||||
func (o ClusterConfigMessage) 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)
|
||||
if l := len(o.Folders); l > 1000000 {
|
||||
return xdr.ElementSizeExceeded("Folders", l, 1000000)
|
||||
}
|
||||
@@ -603,9 +649,6 @@ func (o *ClusterConfigMessage) UnmarshalXDR(bs []byte) error {
|
||||
return o.UnmarshalXDRFrom(u)
|
||||
}
|
||||
func (o *ClusterConfigMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
|
||||
o.DeviceName = u.UnmarshalStringMax(64)
|
||||
o.ClientName = u.UnmarshalStringMax(64)
|
||||
o.ClientVersion = u.UnmarshalStringMax(64)
|
||||
_FoldersSize := int(u.UnmarshalUint32())
|
||||
if _FoldersSize < 0 {
|
||||
return xdr.ElementSizeExceeded("Folders", _FoldersSize, 1000000)
|
||||
|
||||
@@ -189,12 +189,12 @@ func TestClose(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestElementSizeExceededNested(t *testing.T) {
|
||||
m := ClusterConfigMessage{
|
||||
m := HelloMessage{
|
||||
ClientName: "longstringlongstringlongstringinglongstringlongstringlonlongstringlongstringlon",
|
||||
}
|
||||
_, err := m.MarshalXDR()
|
||||
if err == nil {
|
||||
t.Errorf("ID length %d > max 64, but no error", len(m.Folders[0].ID))
|
||||
t.Errorf("ID length %d > max 64, but no error", len(m.ClientName))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user