| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"sync" |
| 10 |
) |
| 11 |
|
| 12 |
// Protocol handles the casync protocol when using remote stores via SSH |
| 13 |
type Protocol struct { |
| 14 |
r io.Reader |
| 15 |
w io.Writer |
| 16 |
initialized bool |
| 17 |
} |
| 18 |
|
| 19 |
// Message represents a command sent to, or received from the communication partner. |
| 20 |
type Message struct { |
| 21 |
Type uint64 |
| 22 |
Body []byte |
| 23 |
} |
| 24 |
|
| 25 |
// NewProtocol creates a new casync protocol handler |
| 26 |
func NewProtocol(r io.Reader, w io.Writer) *Protocol { |
| 27 |
return &Protocol{r: r, w: w} |
| 28 |
} |
| 29 |
|
| 30 |
// Initialize exchanges HELLOs with the other side to start a protocol session. |
| 31 |
// Returns the (capability) flags provided by the other party. |
| 32 |
func (p *Protocol) Initialize(flags uint64) (uint64, error) { |
| 33 |
var ( |
| 34 |
wg sync.WaitGroup |
| 35 |
sendErr, recvErr error |
| 36 |
outFlags uint64 |
| 37 |
) |
| 38 |
wg.Add(2) |
| 39 |
go func() { sendErr = p.SendHello(flags); wg.Done() }() |
| 40 |
go func() { outFlags, recvErr = p.RecvHello(); wg.Done() }() |
| 41 |
wg.Wait() |
| 42 |
if sendErr != nil { |
| 43 |
return 0, sendErr |
| 44 |
} |
| 45 |
if recvErr != nil { |
| 46 |
return 0, recvErr |
| 47 |
} |
| 48 |
p.initialized = true |
| 49 |
return outFlags, nil |
| 50 |
} |
| 51 |
|
| 52 |
// SendHello sends a HELLO message to the server, with the flags signaling which |
| 53 |
// service is being requested from it. |
| 54 |
func (p *Protocol) SendHello(flags uint64) error { |
| 55 |
f := make([]byte, 8) |
| 56 |
binary.LittleEndian.PutUint64(f, flags) |
| 57 |
m := Message{Type: CaProtocolHello, Body: f} |
| 58 |
return p.WriteMessage(m) |
| 59 |
} |
| 60 |
|
| 61 |
// RecvHello waits for the server to send a HELLO, fails if anything else is |
| 62 |
// received. Returns the flags provided by the server. |
| 63 |
func (p *Protocol) RecvHello() (uint64, error) { |
| 64 |
m, err := p.ReadMessage() |
| 65 |
if err != nil { |
| 66 |
return 0, err |
| 67 |
} |
| 68 |
if m.Type != CaProtocolHello { |
| 69 |
return 0, fmt.Errorf("expected protocol hello, got %x", m.Type) |
| 70 |
} |
| 71 |
if len(m.Body) != 8 { |
| 72 |
return 0, fmt.Errorf("unexpected length of hello msg, got %d, expected 8", len(m.Body)) |
| 73 |
} |
| 74 |
return binary.LittleEndian.Uint64(m.Body), nil |
| 75 |
} |
| 76 |
|
| 77 |
// SendProtocolRequest requests a chunk from a server |
| 78 |
func (p *Protocol) SendProtocolRequest(id ChunkID, flags uint64) error { |
| 79 |
if !p.initialized { |
| 80 |
return errors.New("protocol not initialized") |
| 81 |
} |
| 82 |
// prepare the body |
| 83 |
b := make([]byte, 40) |
| 84 |
|
| 85 |
// write the flags into it |
| 86 |
binary.LittleEndian.PutUint64(b[0:8], flags) |
| 87 |
|
| 88 |
// and the chunk id |
| 89 |
copy(b[8:], id[:]) |
| 90 |
|
| 91 |
m := Message{Type: CaProtocolRequest, Body: b} |
| 92 |
return p.WriteMessage(m) |
| 93 |
} |
| 94 |
|
| 95 |
// SendProtocolChunk responds to a request with the content of a chunk |
| 96 |
func (p *Protocol) SendProtocolChunk(id ChunkID, flags uint64, chunk []byte) error { |
| 97 |
if !p.initialized { |
| 98 |
return errors.New("protocol not initialized") |
| 99 |
} |
| 100 |
// prepare the body |
| 101 |
b := make([]byte, len(chunk)+40) |
| 102 |
|
| 103 |
// write the flags into it |
| 104 |
binary.LittleEndian.PutUint64(b[0:8], flags) |
| 105 |
|
| 106 |
// then the chunk id |
| 107 |
copy(b[8:], id[:]) |
| 108 |
|
| 109 |
// then the chunk itself |
| 110 |
copy(b[40:], chunk) |
| 111 |
|
| 112 |
m := Message{Type: CaProtocolChunk, Body: b} |
| 113 |
return p.WriteMessage(m) |
| 114 |
} |
| 115 |
|
| 116 |
// SendMissing tells the client that the requested chunk is not available |
| 117 |
func (p *Protocol) SendMissing(id ChunkID) error { |
| 118 |
if !p.initialized { |
| 119 |
return errors.New("protocol not initialized") |
| 120 |
} |
| 121 |
m := Message{Type: CaProtocolMissing, Body: id[:]} |
| 122 |
return p.WriteMessage(m) |
| 123 |
} |
| 124 |
|
| 125 |
// SendGoodbye tells the other side to terminate gracefully |
| 126 |
func (p *Protocol) SendGoodbye() error { |
| 127 |
if !p.initialized { |
| 128 |
return errors.New("protocol not initialized") |
| 129 |
} |
| 130 |
m := Message{Type: CaProtocolGoodbye, Body: nil} |
| 131 |
return p.WriteMessage(m) |
| 132 |
} |
| 133 |
|
| 134 |
// RequestChunk sends a request for a specific chunk to the server, waits for |
| 135 |
// the response and returns the bytes in the chunk. Returns an error if the |
| 136 |
// server reports the chunk as missing |
| 137 |
func (p *Protocol) RequestChunk(id ChunkID) (*Chunk, error) { |
| 138 |
if !p.initialized { |
| 139 |
return nil, errors.New("protocol not initialized") |
| 140 |
} |
| 141 |
if err := p.SendProtocolRequest(id, CaProtocolRequestHighPriority); err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
m, err := p.ReadMessage() |
| 145 |
if err != nil { |
| 146 |
return nil, err |
| 147 |
} |
| 148 |
switch m.Type { // TODO: deal with ABORT messages |
| 149 |
case CaProtocolMissing: |
| 150 |
return nil, ChunkMissing{id} |
| 151 |
case CaProtocolChunk: |
| 152 |
// The body comes with flags... do we need them? Ignore for now |
| 153 |
if len(m.Body) < 40 { |
| 154 |
return nil, errors.New("received chunk too small") |
| 155 |
} |
| 156 |
// The rest should be the (compressed) chunk data |
| 157 |
return NewChunkFromStorage(id, m.Body[40:], []converter{Compressor{}}, false) |
| 158 |
default: |
| 159 |
return nil, fmt.Errorf("unexpected protocol message type %x", m.Type) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// ReadMessage reads a generic message from the other end, verifies the length, |
| 164 |
// extracts the type and returns the message body as byte slice |
| 165 |
func (p *Protocol) ReadMessage() (Message, error) { |
| 166 |
r := reader{p.r} |
| 167 |
|
| 168 |
// Get the length of the message |
| 169 |
len, err := r.ReadUint64() |
| 170 |
if err != nil { |
| 171 |
return Message{}, err |
| 172 |
} |
| 173 |
|
| 174 |
// Got to have at least a type following the length |
| 175 |
if len < 16 { |
| 176 |
return Message{}, errors.New("message length too short") |
| 177 |
} |
| 178 |
|
| 179 |
// Read the remaining message body |
| 180 |
b, err := r.ReadN(len - 8) |
| 181 |
if err != nil { |
| 182 |
return Message{}, err |
| 183 |
} |
| 184 |
|
| 185 |
// Get the message type and strip it off the remaining message data |
| 186 |
typ := binary.LittleEndian.Uint64(b[0:8]) |
| 187 |
b = b[8:] |
| 188 |
|
| 189 |
return Message{Type: typ, Body: b}, nil |
| 190 |
} |
| 191 |
|
| 192 |
// WriteMessage sends a generic message to the server |
| 193 |
func (p *Protocol) WriteMessage(m Message) error { |
| 194 |
len := 16 + len(m.Body) |
| 195 |
h := make([]byte, 16) |
| 196 |
binary.LittleEndian.PutUint64(h[0:8], uint64(len)) |
| 197 |
binary.LittleEndian.PutUint64(h[8:16], uint64(m.Type)) |
| 198 |
r := io.MultiReader(bytes.NewReader(h), bytes.NewReader(m.Body)) |
| 199 |
_, err := io.Copy(p.w, r) |
| 200 |
return err |
| 201 |
} |