2016-06-09 10:50:14 +00:00
|
|
|
// Copyright (C) 2016 The Protocol Authors.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The HelloResult is the non version specific interpretation of the other
|
|
|
|
// side's Hello message.
|
|
|
|
type HelloResult struct {
|
|
|
|
DeviceName string
|
|
|
|
ClientName string
|
|
|
|
ClientVersion string
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2016-07-04 10:40:29 +00:00
|
|
|
// 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")
|
2016-06-09 10:50:14 +00:00
|
|
|
// 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")
|
|
|
|
)
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func ExchangeHello(c io.ReadWriter, h HelloIntf) (HelloResult, error) {
|
2016-06-09 10:50:14 +00:00
|
|
|
if err := writeHello(c, h); err != nil {
|
|
|
|
return HelloResult{}, err
|
|
|
|
}
|
|
|
|
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 {
|
2016-07-04 10:40:29 +00:00
|
|
|
case ErrTooOldVersion12, ErrTooOldVersion13, ErrUnknownMagic:
|
2016-06-09 10:50:14 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readHello(c io.Reader) (HelloResult, 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 {
|
|
|
|
return HelloResult{}, err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return HelloResult{}, err
|
|
|
|
}
|
|
|
|
msgSize := binary.BigEndian.Uint16(header[:2])
|
|
|
|
if msgSize > 32767 {
|
2016-07-04 10:40:29 +00:00
|
|
|
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 Hello
|
|
|
|
if err := hello.Unmarshal(buf); err != nil {
|
|
|
|
return HelloResult{}, err
|
|
|
|
}
|
|
|
|
res := HelloResult{
|
|
|
|
DeviceName: hello.DeviceName,
|
|
|
|
ClientName: hello.ClientName,
|
|
|
|
ClientVersion: hello.ClientVersion,
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
|
2016-06-09 10:50:14 +00:00
|
|
|
case Version13HelloMagic:
|
|
|
|
// This is a v0.13 Hello message in XDR format
|
2016-07-17 21:41:20 +00:00
|
|
|
if _, err := io.ReadFull(c, header[:4]); err != nil {
|
|
|
|
return HelloResult{}, err
|
|
|
|
}
|
|
|
|
msgSize := binary.BigEndian.Uint32(header[:4])
|
2016-06-09 10:50:14 +00:00
|
|
|
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{
|
|
|
|
DeviceName: hello.DeviceName,
|
|
|
|
ClientName: hello.ClientName,
|
|
|
|
ClientVersion: hello.ClientVersion,
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
return res, ErrTooOldVersion13
|
2016-06-09 10:50:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return HelloResult{}, ErrUnknownMagic
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-07-17 21:41:20 +00:00
|
|
|
header := make([]byte, 6)
|
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
|
|
|
|
}
|