-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhandshake.go
427 lines (393 loc) · 10.5 KB
/
handshake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2018 Axel Wagner
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nbd
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
)
// Export specifies the data needed for the NBD network protocol.
type Export struct {
Name string
Description string
Size uint64
Flags uint16 // TODO: Determine Flags from Device.
BlockSizes *BlockSizeConstraints
Device Device
}
// BlockSizeConstraints optionally specifies possible block sizes for a given
// export.
type BlockSizeConstraints struct {
Min uint32
Preferred uint32
Max uint32
}
var defaultBlockSizes = BlockSizeConstraints{1, 4096, 0xffffffff}
type connParameters struct {
Export Export
BlockSizes BlockSizeConstraints
}
func serverHandshake(rw io.ReadWriter, exp []Export) (connParameters, error) {
parms := connParameters{
BlockSizes: defaultBlockSizes,
}
return parms, do(rw, func(e *encoder) {
e.writeUint64(nbdMagic)
e.writeUint64(optMagic)
e.writeUint16(flagDefaults)
clientFlags := e.uint32()
if clientFlags & ^uint32(flagDefaults) != 0 {
e.check(fmt.Errorf("handshake aborted due to unknown handshake flags 0x%d", clientFlags & ^uint32(flagDefaults)))
}
if clientFlags != flagDefaults {
e.check(fmt.Errorf("refusing deprecated handshake flags 0x%x", clientFlags))
}
for {
code, o, err := decodeOption(e)
if err != 0 {
encodeReply(e, code, &repError{err, ""})
continue
}
switch o := o.(type) {
case *optExportName:
var ok bool
parms.Export, ok = findExport(o.name, exp)
if !ok {
encodeReply(e, code, &repError{errUnknown, ""})
continue
}
e.writeUint64(parms.Export.Size)
e.writeUint16(parms.Export.Flags)
return
case *optAbort:
encodeReply(e, code, &repAck{})
e.check(errors.New("client aborted negotiation"))
case *optList:
for _, ex := range exp {
encodeReply(e, code, &repServer{ex.Name, ""})
}
encodeReply(e, code, &repAck{})
case *optInfo:
var ok bool
parms.Export, ok = findExport(o.name, exp)
if !ok {
encodeReply(e, code, &repError{errUnknown, ""})
continue
}
encodeReply(e, code, &infoExport{parms.Export.Size, parms.Export.Flags})
for _, r := range o.reqs {
switch r {
case cInfoExport:
// already sent
case cInfoName:
encodeReply(e, code, &infoName{parms.Export.Name})
case cInfoDescription:
encodeReply(e, code, &infoDescription{parms.Export.Description})
case cInfoBlockSize:
if parms.Export.BlockSizes == nil {
break
}
if o.done {
parms.BlockSizes = *parms.Export.BlockSizes
}
encodeReply(e, code, &infoBlockSize{
parms.BlockSizes.Min,
parms.BlockSizes.Preferred,
parms.BlockSizes.Max,
})
}
}
encodeReply(e, code, &repAck{})
if o.done {
return
}
}
}
})
}
// Client performs the client-side of the NBD network protocol handshake and
// can be used to query information about the exports from a server.
type Client struct {
rw io.ReadWriteCloser
closed bool
}
// ClientHandshake starts the client-side of the NBD handshake over c.
func ClientHandshake(ctx context.Context, c net.Conn) (*Client, error) {
rw := wrapConn(ctx, c)
cl := &Client{rw, false}
return cl, do(rw, func(e *encoder) {
if e.uint64() != nbdMagic {
e.check(errors.New("invalid magic from server"))
}
if e.uint64() != optMagic {
e.check(errors.New("invalid magic from server"))
}
serverFlags := e.uint16()
if serverFlags&flagDefaults != flagDefaults {
e.check(errors.New("refusing deprecated handshake flags"))
}
e.writeUint32(flagDefaults)
})
}
func (c *Client) checkClosed(e *encoder) {
if c.closed {
e.check(errors.New("use of closed client"))
}
}
// send sends an option request to the server.
func (c *Client) send(e *encoder, o optionRequest) {
c.checkClosed(e)
e.writeUint64(optMagic)
e.writeUint32(o.code())
e.buf = []byte{}
o.encode(e)
buf := e.buf
e.buf = nil
e.writeUint32(uint32(len(buf)))
e.write(buf)
}
// recv receives an option reply from the server.
func (c *Client) recv(e *encoder, code uint32) optionReply {
c.checkClosed(e)
if e.uint64() != repMagic {
e.check(errors.New("invalid reply magic from server"))
}
if e.uint32() != code {
e.check(errors.New("server responded to wrong option"))
}
code = e.uint32()
length := e.uint32()
var rep optionReply
switch code {
case cRepAck:
rep = new(repAck)
case cRepServer:
rep = new(repServer)
case cRepInfo:
return decodeInfo(e, length)
default:
if code&(1<<31) != 0 {
rep = &repError{errno: errno(code)}
rep.decode(e, length)
e.check(rep.(error))
} else {
e.check(fmt.Errorf("unknown response code 0x%x", code))
}
return nil
}
rep.decode(e, length)
return rep
}
// Abort aborts the handshake. c should not be used after Abort returns.
func (c *Client) Abort() error {
return do(c.rw, func(e *encoder) {
c.send(e, &optAbort{})
rep := c.recv(e, cOptAbort)
c.close()
switch rep.(type) {
case *repAck:
default:
e.check(errors.New("invalid response to abort request"))
}
})
}
// List returns the names of exports the server is providing.
func (c *Client) List() ([]string, error) {
var list []string
err := do(c.rw, func(e *encoder) {
c.send(e, &optList{})
for {
rep := c.recv(e, cOptList)
switch rep := rep.(type) {
case *repAck:
return
case *repServer:
list = append(list, rep.name)
default:
e.check(errors.New("invalid response to list request"))
}
}
})
return list, err
}
// into sends an NBD_OPT_INFO (if done == false) or NBD_OPT_GO (if done ==
// true) request and returns the export data returned by the server.
func (c *Client) info(exportName string, done bool) (Export, error) {
var ex Export
err := do(c.rw, func(e *encoder) {
reqs := []uint16{cInfoExport, cInfoName, cInfoDescription, cInfoBlockSize}
c.send(e, &optInfo{done, exportName, reqs})
code := uint32(cOptInfo)
if done {
code = cOptGo
}
for {
rep := c.recv(e, code)
switch rep := rep.(type) {
case *repAck:
return
case *infoExport:
ex.Size = rep.size
ex.Flags = rep.flags
case *infoName:
ex.Name = rep.name
case *infoDescription:
ex.Description = rep.description
case *infoBlockSize:
ex.BlockSizes = &BlockSizeConstraints{
Min: rep.min,
Preferred: rep.preferred,
Max: rep.max,
}
default:
e.check(errors.New("invalid response to info request"))
}
}
})
return ex, err
}
// Info requests information about the export identified by exportName. If
// exportName is the empty string, the default export will be queried.
func (c *Client) Info(exportName string) (Export, error) {
return c.info(exportName, false)
}
// Go terminates the handshake phase of the NBD protocol, opening the export
// identified by exportName. If exportName is the empty string, the default
// export will be used. c should not be used after Go returns.
func (c *Client) Go(exportName string) (Export, error) {
ex, err := c.info(exportName, true)
c.close()
return ex, err
}
// close marks the Client closed, preventing it from further use, and cleans up
// the connection wrapper. It does not close the wrapped connection.
func (c *Client) close() error {
c.closed = true
return c.rw.Close()
}
// findExport searches the list of exports for one with the given name. If name
// is empty, it returns the first export. findExport performs a linear search,
// so it doesn't scale to a large number of exports, but we assume for now that
// that's not a practical problem.
func findExport(name string, exp []Export) (Export, bool) {
if len(exp) > 0 && name == "" {
return exp[0], true
}
for _, e := range exp {
if e.Name == name {
return e, true
}
}
return Export{}, false
}
// do wraps rw for easy en-/decoding of binary data. It creates an *encoder and
// calls f with that. The process uses panic/recover for error handling, so e
// should never be passed to a different goroutine.
func do(rw io.ReadWriter, f func(e *encoder)) (err error) {
sentinel := new(uint8)
defer func() {
if v := recover(); v != nil && v != sentinel {
panic(v)
}
}()
check := func(e error) {
if e != nil {
err = e
panic(sentinel)
}
}
f(&encoder{rw, nil, check})
return err
}
// encoder provides helper methods for easy de-/encoding of binary data.
// If an error occurs, it calls check, which is expected to panic if its
// non-nil. If buf is non-nil, the encoder won't write to rw directly, but
// append to buf. That way, nested messages can be buffered before writing them
// out, to determine their length.
type encoder struct {
rw io.ReadWriter
buf []byte
check func(error)
}
func (e *encoder) write(b []byte) {
if e.buf != nil {
e.buf = append(e.buf, b...)
return
}
_, err := e.rw.Write(b)
e.check(err)
}
func (e *encoder) writeString(s string) {
if e.buf != nil {
e.buf = append(e.buf, s...)
return
}
var err error
if sw, ok := e.rw.(interface{ WriteString(string) (int, error) }); ok {
_, err = sw.WriteString(s)
} else {
_, err = e.rw.Write([]byte(s))
}
e.check(err)
}
func (e *encoder) read(b []byte) {
_, err := io.ReadFull(e.rw, b)
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
e.check(err)
}
func (e *encoder) discard(n uint32) {
buf := make([]byte, 512)
for n > 0 {
if n > uint32(len(buf)) {
buf = buf[:n]
}
e.read(buf)
n -= uint32(len(buf))
}
}
func (e *encoder) uint16() uint16 {
var b [2]byte
e.read(b[:])
return binary.BigEndian.Uint16(b[:])
}
func (e *encoder) uint32() uint32 {
var b [4]byte
e.read(b[:])
return binary.BigEndian.Uint32(b[:])
}
func (e *encoder) uint64() uint64 {
var b [8]byte
e.read(b[:])
return binary.BigEndian.Uint64(b[:])
}
func (e *encoder) writeUint16(v uint16) {
var b [2]byte
binary.BigEndian.PutUint16(b[:], v)
e.write(b[:])
}
func (e *encoder) writeUint32(v uint32) {
var b [4]byte
binary.BigEndian.PutUint32(b[:], v)
e.write(b[:])
}
func (e *encoder) writeUint64(v uint64) {
var b [8]byte
binary.BigEndian.PutUint64(b[:], v)
e.write(b[:])
}