2016-06-09 10:50:14 +00:00
|
|
|
// Copyright (C) 2016 The Protocol Authors.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// The HelloIntf interface is implemented by the version specific hello
|
2016-06-09 10:50:14 +00:00
|
|
|
// message. It knows its magic number and how to serialize itself to a byte
|
|
|
|
// buffer.
|
2016-07-04 10:40:29 +00:00
|
|
|
type HelloIntf interface {
|
2016-06-09 10:50:14 +00:00
|
|
|
Magic() uint32
|
|
|
|
Marshal() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-01-17 19:48:43 +00:00
|
|
|
// 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")
|
2023-05-09 06:54:02 +00:00
|
|
|
// ErrUnknownMagic is returned by ExchangeHello when the other side
|
2016-06-09 10:50:14 +00:00
|
|
|
// speaks something entirely unknown.
|
|
|
|
ErrUnknownMagic = errors.New("the remote device speaks an unknown (newer?) version of the protocol")
|
|
|
|
)
|
|
|
|
|
2020-09-29 11:17:38 +00:00
|
|
|
func ExchangeHello(c io.ReadWriter, h HelloIntf) (Hello, error) {
|
2016-06-09 10:50:14 +00:00
|
|
|
if err := writeHello(c, h); err != nil {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, err
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
return readHello(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsVersionMismatch returns true if the error is a reliable indication of a
|
|
|
|
// version mismatch that we might want to alert the user about.
|
|
|
|
func IsVersionMismatch(err error) bool {
|
|
|
|
switch err {
|
2019-01-17 19:48:43 +00:00
|
|
|
case ErrTooOldVersion, ErrUnknownMagic:
|
2016-06-09 10:50:14 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:17:38 +00:00
|
|
|
func readHello(c io.Reader) (Hello, error) {
|
2016-07-17 21:41:20 +00:00
|
|
|
header := make([]byte, 4)
|
2016-06-09 10:50:14 +00:00
|
|
|
if _, err := io.ReadFull(c, header); err != nil {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, err
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 21:41:20 +00:00
|
|
|
switch binary.BigEndian.Uint32(header) {
|
2016-07-04 10:40:29 +00:00
|
|
|
case HelloMessageMagic:
|
|
|
|
// This is a v0.14 Hello message in proto format
|
2016-07-17 21:41:20 +00:00
|
|
|
if _, err := io.ReadFull(c, header[:2]); err != nil {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, err
|
2016-07-17 21:41:20 +00:00
|
|
|
}
|
|
|
|
msgSize := binary.BigEndian.Uint16(header[:2])
|
|
|
|
if msgSize > 32767 {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, errors.New("hello message too big")
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, msgSize)
|
|
|
|
if _, err := io.ReadFull(c, buf); err != nil {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, err
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hello Hello
|
|
|
|
if err := hello.Unmarshal(buf); err != nil {
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, err
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello(hello), nil
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
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)
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, ErrTooOldVersion
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 11:17:38 +00:00
|
|
|
return Hello{}, ErrUnknownMagic
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func writeHello(c io.Writer, h HelloIntf) error {
|
2016-06-09 10:50:14 +00:00
|
|
|
msg, err := h.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-17 21:41:20 +00:00
|
|
|
if len(msg) > 32767 {
|
|
|
|
// The header length must be a positive signed int16
|
|
|
|
panic("bug: attempting to serialize too large hello message")
|
|
|
|
}
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2021-09-05 15:20:04 +00:00
|
|
|
header := make([]byte, 6, 6+len(msg))
|
2016-06-09 10:50:14 +00:00
|
|
|
binary.BigEndian.PutUint32(header[:4], h.Magic())
|
2016-07-17 21:41:20 +00:00
|
|
|
binary.BigEndian.PutUint16(header[4:], uint16(len(msg)))
|
2016-06-09 10:50:14 +00:00
|
|
|
|
|
|
|
_, err = c.Write(append(header, msg...))
|
|
|
|
return err
|
|
|
|
}
|