From 76af0cf07b5fb1fc5606c66f7d5f978821af5034 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 17 Jan 2019 20:48:43 +0100 Subject: [PATCH] lib/protocol: Remove support for v0.13 hello messages (#5461) --- lib/protocol/bep_extensions.go | 5 +- lib/protocol/hello.go | 41 +++------- lib/protocol/hello_test.go | 137 ++++++-------------------------- lib/protocol/hello_v0.13.go | 24 ------ lib/protocol/hello_v0.13_xdr.go | 85 -------------------- 5 files changed, 37 insertions(+), 255 deletions(-) delete mode 100644 lib/protocol/hello_v0.13.go delete mode 100644 lib/protocol/hello_v0.13_xdr.go diff --git a/lib/protocol/bep_extensions.go b/lib/protocol/bep_extensions.go index eee6530f..550d47fb 100644 --- a/lib/protocol/bep_extensions.go +++ b/lib/protocol/bep_extensions.go @@ -17,8 +17,9 @@ import ( ) const ( - SyntheticDirectorySize = 128 - HelloMessageMagic = uint32(0x2EA7D90B) + SyntheticDirectorySize = 128 + HelloMessageMagic uint32 = 0x2EA7D90B + Version13HelloMagic uint32 = 0x9F79BC40 // old ) func (m Hello) Magic() uint32 { diff --git a/lib/protocol/hello.go b/lib/protocol/hello.go index 0e9cb2f0..debae32d 100644 --- a/lib/protocol/hello.go +++ b/lib/protocol/hello.go @@ -26,12 +26,9 @@ type HelloResult struct { } var ( - // ErrTooOldVersion12 is returned by ExchangeHello when the other side - // speaks the older, incompatible version 0.12 of the protocol. - ErrTooOldVersion12 = errors.New("the remote device speaks an older version of the protocol (v0.12) not compatible with this version") - // ErrTooOldVersion13 is returned by ExchangeHello when the other side - // speaks the older, incompatible version 0.12 of the protocol. - ErrTooOldVersion13 = errors.New("the remote device speaks an older version of the protocol (v0.13) not compatible with this version") + // ErrTooOldVersion is returned by ExchangeHello when the other side + // speaks an older, incompatible version of the protocol. + ErrTooOldVersion = errors.New("the remote device speaks an older version of the protocol not compatible with this version") // ErrUnknownMagic is returned by ExchangeHellow when the other side // speaks something entirely unknown. ErrUnknownMagic = errors.New("the remote device speaks an unknown (newer?) version of the protocol") @@ -48,7 +45,7 @@ func ExchangeHello(c io.ReadWriter, h HelloIntf) (HelloResult, error) { // version mismatch that we might want to alert the user about. func IsVersionMismatch(err error) bool { switch err { - case ErrTooOldVersion12, ErrTooOldVersion13, ErrUnknownMagic: + case ErrTooOldVersion, ErrUnknownMagic: return true default: return false @@ -87,31 +84,11 @@ func readHello(c io.Reader) (HelloResult, error) { } return res, nil - case Version13HelloMagic: - // This is a v0.13 Hello message in XDR format - if _, err := io.ReadFull(c, header[:4]); err != nil { - return HelloResult{}, err - } - msgSize := binary.BigEndian.Uint32(header[:4]) - if msgSize > 1024 { - return HelloResult{}, fmt.Errorf("hello message too big") - } - buf := make([]byte, msgSize) - if _, err := io.ReadFull(c, buf); err != nil { - return HelloResult{}, err - } - - var hello Version13HelloMessage - if err := hello.UnmarshalXDR(buf); err != nil { - return HelloResult{}, err - } - res := HelloResult(hello) - return res, ErrTooOldVersion13 - - case 0x00010001, 0x00010000: - // This is the first word of a v0.12 cluster config message. - // (Version 0, message ID 1, message type 0, compression enabled or disabled) - return HelloResult{}, ErrTooOldVersion12 + case 0x00010001, 0x00010000, Version13HelloMagic: + // This is the first word of an older cluster config message or an + // old magic number. (Version 0, message ID 1, message type 0, + // compression enabled or disabled) + return HelloResult{}, ErrTooOldVersion } return HelloResult{}, ErrUnknownMagic diff --git a/lib/protocol/hello_test.go b/lib/protocol/hello_test.go index bda51693..4ef5d993 100644 --- a/lib/protocol/hello_test.go +++ b/lib/protocol/hello_test.go @@ -60,126 +60,39 @@ func TestVersion14Hello(t *testing.T) { } } -func TestVersion13Hello(t *testing.T) { - // Tests that we can send and receive a version 0.13 hello message. +func TestOldHelloMsgs(t *testing.T) { + // Tests that we can correctly identify old/missing/unknown hello + // messages. - expected := Version13HelloMessage{ - DeviceName: "test device", - ClientName: "syncthing", - ClientVersion: "v0.13.5", - } - msgBuf := expected.MustMarshalXDR() - - hdrBuf := make([]byte, 8) - binary.BigEndian.PutUint32(hdrBuf, Version13HelloMagic) - binary.BigEndian.PutUint32(hdrBuf[4:], uint32(len(msgBuf))) - - outBuf := new(bytes.Buffer) - outBuf.Write(hdrBuf) - outBuf.Write(msgBuf) - - inBuf := new(bytes.Buffer) - - conn := &readWriter{outBuf, inBuf} - - send := Version13HelloMessage{ - DeviceName: "this device", - ClientName: "other client", - ClientVersion: "v0.13.6", + cases := []struct { + msg string + err error + }{ + {"00010001", ErrTooOldVersion}, // v12 + {"9F79BC40", ErrTooOldVersion}, // v13 + {"12345678", ErrUnknownMagic}, } - res, err := ExchangeHello(conn, send) - if err != ErrTooOldVersion13 { - t.Errorf("unexpected error %v != ErrTooOldVersion13", err) - } + for _, tc := range cases { + msg, _ := hex.DecodeString(tc.msg) - if res.ClientName != expected.ClientName { - t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName) - } - if res.ClientVersion != expected.ClientVersion { - t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion) - } - if res.DeviceName != expected.DeviceName { - t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName) - } -} + outBuf := new(bytes.Buffer) + outBuf.Write(msg) -func TestVersion12Hello(t *testing.T) { - // Tests that we can correctly interpret the lack of a hello message - // from a v0.12 client. + inBuf := new(bytes.Buffer) - // This is the typical v0.12 connection start - our message header for a - // ClusterConfig message and then the cluster config message data. Taken - // from a protocol dump of a recent v0.12 client. - msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(` - 00010001 - 0000014a - 7802000070000000027332000100a00973796e637468696e670e00b000000876 - 302e31322e32352400b00000000764656661756c741e00f01603000000204794 - 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800 - 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230 - 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb - aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f - 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf - a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400 - 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d - c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8 - 90000000000000000000`, ``)) + conn := &readWriter{outBuf, inBuf} - outBuf := new(bytes.Buffer) - outBuf.Write(msg) + send := &Hello{ + DeviceName: "this device", + ClientName: "other client", + ClientVersion: "v1.0.0", + } - inBuf := new(bytes.Buffer) - - conn := &readWriter{outBuf, inBuf} - - send := Version13HelloMessage{ - DeviceName: "this device", - ClientName: "other client", - ClientVersion: "v0.13.6", - } - - _, err := ExchangeHello(conn, send) - if err != ErrTooOldVersion12 { - t.Errorf("unexpected error %v != ErrTooOldVersion12", err) - } -} - -func TestUnknownHello(t *testing.T) { - // Tests that we react correctly to a completely unknown magic number. - - // This is an unknown magic follow byte some message data. - msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(` - 12345678 - 0000014a - 7802000070000000027332000100a00973796e637468696e670e00b000000876 - 302e31322e32352400b00000000764656661756c741e00f01603000000204794 - 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800 - 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230 - 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb - aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f - 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf - a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400 - 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d - c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8 - 90000000000000000000`, ``)) - - outBuf := new(bytes.Buffer) - outBuf.Write(msg) - - inBuf := new(bytes.Buffer) - - conn := &readWriter{outBuf, inBuf} - - send := Version13HelloMessage{ - DeviceName: "this device", - ClientName: "other client", - ClientVersion: "v0.13.6", - } - - _, err := ExchangeHello(conn, send) - if err != ErrUnknownMagic { - t.Errorf("unexpected error %v != ErrUnknownMagic", err) + _, err := ExchangeHello(conn, send) + if err != tc.err { + t.Errorf("unexpected error %v != %v", err, tc.err) + } } } diff --git a/lib/protocol/hello_v0.13.go b/lib/protocol/hello_v0.13.go deleted file mode 100644 index ec85560f..00000000 --- a/lib/protocol/hello_v0.13.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2016 The Protocol Authors. - -//go:generate -command genxdr go run ../../repos/xdr/cmd/genxdr/main.go -//go:generate genxdr -o hello_v0.13_xdr.go hello_v0.13.go - -package protocol - -var ( - Version13HelloMagic uint32 = 0x9F79BC40 -) - -type Version13HelloMessage struct { - DeviceName string // max:64 - ClientName string // max:64 - ClientVersion string // max:64 -} - -func (m Version13HelloMessage) Magic() uint32 { - return Version13HelloMagic -} - -func (m Version13HelloMessage) Marshal() ([]byte, error) { - return m.MarshalXDR() -} diff --git a/lib/protocol/hello_v0.13_xdr.go b/lib/protocol/hello_v0.13_xdr.go deleted file mode 100644 index ecf14967..00000000 --- a/lib/protocol/hello_v0.13_xdr.go +++ /dev/null @@ -1,85 +0,0 @@ -// ************************************************************ -// This file is automatically generated by genxdr. Do not edit. -// ************************************************************ - -package protocol - -import ( - "github.com/calmh/xdr" -) - -/* - -Version13HelloMessage 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 Version13HelloMessage { - string DeviceName<64>; - string ClientName<64>; - string ClientVersion<64>; -} - -*/ - -func (o Version13HelloMessage) 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 Version13HelloMessage) MarshalXDR() ([]byte, error) { - buf := make([]byte, o.XDRSize()) - m := &xdr.Marshaller{Data: buf} - return buf, o.MarshalXDRInto(m) -} - -func (o Version13HelloMessage) MustMarshalXDR() []byte { - bs, err := o.MarshalXDR() - if err != nil { - panic(err) - } - return bs -} - -func (o Version13HelloMessage) 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 *Version13HelloMessage) UnmarshalXDR(bs []byte) error { - u := &xdr.Unmarshaller{Data: bs} - return o.UnmarshalXDRFrom(u) -} -func (o *Version13HelloMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { - o.DeviceName = u.UnmarshalStringMax(64) - o.ClientName = u.UnmarshalStringMax(64) - o.ClientVersion = u.UnmarshalStringMax(64) - return u.Error -}