forked from zeromq/gomq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
600 lines (496 loc) · 16.1 KB
/
conn.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package zmtp
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
)
// Connection is a ZMTP level connection
type Connection struct {
rw io.ReadWriter
metadata map[string]string
securityMechanism SecurityMechanism
socket Socket
isPrepared bool
isDowngraded bool
asServer, otherEndAsServer bool
}
// SocketType is a ZMTP socket type
type SocketType string
// SocketIdentity is the ZMTP metadata socket identity.
// See:
// https://rfc.zeromq.org/spec:23/ZMTP/.
type SocketIdentity []byte
func (id SocketIdentity) String() string {
n := len(id)
if n > 255 { // ZMTP identities are: 0*255OCTET
n = 255
}
return string(id[:n])
}
const (
ClientSocketType SocketType = "CLIENT" // a ZMQ_CLIENT socket
ServerSocketType SocketType = "SERVER" // a ZMQ_SERVER socket
PullSocketType SocketType = "PULL" // a ZMQ_PULL socket
PushSocketType SocketType = "PUSH" // a ZMQ_PUSH socket
DealerSocketType SocketType = "DEALER" // a ZMQ_DEALER socket
RouterSocketType SocketType = "ROUTER" // a ZMQ_ROUTER socket
ReqSocketType SocketType = "REQ" // a ZMQ_REQ socket
RepSocketType SocketType = "REP" // a ZMQ_REP socket
PubSocketType SocketType = "PUB" // a ZMQ_PUB socket
SubSocketType SocketType = "SUB" // a ZMQ_SUB socket
XPubSocketType SocketType = "XPUB" // a ZMQ_XPUB socket
XSubSocketType SocketType = "XSUB" // a ZMQ_XSUB socket
)
// NewConnection accepts an io.ReadWriter and creates a new ZMTP connection
func NewConnection(rw io.ReadWriter) *Connection {
return &Connection{
rw: rw,
metadata: make(map[string]string),
}
}
// Prepare performs a ZMTP handshake over a Connection's readWriter
func (c *Connection) Prepare(mechanism SecurityMechanism, socketType SocketType, socketID SocketIdentity, asServer bool, applicationMetadata map[string]string) (map[string]string, error) {
if c.isPrepared {
return nil, errors.New("Connection was already prepared")
}
c.isPrepared = true
c.securityMechanism = mechanism
var err error
if c.socket, err = NewSocket(socketType); err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while creating socket: %v", err)
}
// Send/recv greeting
if err := c.sendGreeting(asServer); err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while sending greeting: %v", err)
}
if err := c.recvGreeting(asServer); err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while receiving greeting: %v", err)
}
// Do security handshake
if err := mechanism.Handshake(); err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while running the security handshake: %v", err)
}
// Send/recv metadata
if err := c.sendMetadata(socketType, socketID, applicationMetadata); err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while sending metadata: %v", err)
}
otherEndApplicationMetaData, err := c.recvMetadata()
if err != nil {
return nil, fmt.Errorf("gomq/zmtp: Got error while receiving metadata: %v", err)
}
return otherEndApplicationMetaData, nil
}
func (c *Connection) sendGreeting(asServer bool) error {
greeting := greeting{
SignaturePrefix: signaturePrefix,
SignatureSuffix: signatureSuffix,
Version: version,
}
toNullPaddedString(string(c.securityMechanism.Type()), greeting.Mechanism[:])
if err := greeting.marshal(c.rw); err != nil {
return err
}
return nil
}
func (c *Connection) recvGreeting(asServer bool) error {
var greeting greeting
if err := greeting.unmarshal(c.rw); err != nil {
return fmt.Errorf("Error while reading: %v", err)
}
if greeting.SignaturePrefix != signaturePrefix {
return fmt.Errorf("Signature prefix received does not correspond with expected signature. Received: %#v. Expected: %#v.", greeting.SignaturePrefix, signaturePrefix)
}
if greeting.SignatureSuffix != signatureSuffix {
return fmt.Errorf("Signature prefix received does not correspond with expected signature. Received: %#v. Expected: %#v.", greeting.SignatureSuffix, signatureSuffix)
}
if greeting.Version != version {
if greeting.Version[0] == version[0] && greeting.Version[1] < version[1] {
c.isDowngraded = true
} else {
return fmt.Errorf("Version %v.%v received does match expected version %v.%v", int(greeting.Version[0]), int(greeting.Version[1]), int(majorVersion), int(minorVersion))
}
}
var otherMechanism = fromNullPaddedString(greeting.Mechanism[:])
var thisMechanism = string(c.securityMechanism.Type())
if thisMechanism != otherMechanism {
return fmt.Errorf("Encryption mechanism on other side %q does not match this side's %q", otherMechanism, thisMechanism)
}
otherEndAsServer, err := fromByteBool(greeting.ServerFlag)
if err != nil {
return err
}
c.otherEndAsServer = otherEndAsServer
return nil
}
func (c *Connection) sendMetadata(socketType SocketType, socketID SocketIdentity, applicationMetadata map[string]string) error {
buffer := new(bytes.Buffer)
var usedKeys map[string]struct{}
for k, v := range applicationMetadata {
if len(k) == 0 {
return errors.New("Cannot send empty application metadata key")
}
lowerCaseKey := strings.ToLower(k)
if _, alreadyPresent := usedKeys[lowerCaseKey]; alreadyPresent {
return fmt.Errorf("Key %q is specified multiple times with different casing", lowerCaseKey)
}
usedKeys[lowerCaseKey] = struct{}{}
c.writeMetadata(buffer, "x-"+lowerCaseKey, v)
}
c.writeMetadata(buffer, "socket-type", string(socketType))
c.writeMetadata(buffer, "Identity", socketID.String())
return c.SendCommand("READY", buffer.Bytes())
}
func (c *Connection) writeMetadata(buffer *bytes.Buffer, name string, value string) {
var (
p = 0
nameLen = len(name)
valueLen = len(value)
buf = make([]byte, 1+nameLen+4+valueLen)
)
buf[p] = byte(nameLen)
p++
p += copy(buf[p:], name)
byteOrder.PutUint32(buf[p:p+4], uint32(valueLen))
p += 4
copy(buf[p:], value)
_, err := buffer.Write(buf)
if err != nil {
panic(err)
}
}
func (c *Connection) recvMetadata() (map[string]string, error) {
isCommand, body, err := c.read()
if err != nil {
return nil, err
}
if !isCommand {
return nil, errors.New("Got a message frame for metadata, expected a command frame")
}
command, err := c.parseCommand(body)
if err != nil {
return nil, err
}
if command.Name != "READY" {
return nil, fmt.Errorf("Got a %v command for metadata instead of the expected READY command frame", command.Name)
}
applicationMetadata := make(map[string]string)
i := 0
for i < len(command.Body) {
// Key length
keyLength := int(command.Body[i])
if i+keyLength >= len(command.Body) {
return nil, fmt.Errorf("metadata key of length %v overflows body of length %v at position %v", keyLength, len(command.Body), i)
}
i++
// Key
key := strings.ToLower(string(command.Body[i : i+keyLength]))
i += keyLength
// Value length
rawValueLength := byteOrder.Uint32(command.Body[i : i+4])
if uint64(rawValueLength) > uint64(maxInt) {
return nil, fmt.Errorf("Length of value %v overflows integer max length %v on this platform", rawValueLength, maxInt)
}
valueLength := int(rawValueLength)
if i+valueLength >= len(command.Body) {
return nil, fmt.Errorf("metadata value of length %v overflows body of length %v at position %v", valueLength, len(command.Body), i)
}
i += 4
// Value
value := string(command.Body[i : i+valueLength])
i += valueLength
if strings.HasPrefix(key, "x-") {
applicationMetadata[key[2:]] = value
} else {
c.metadata[key] = value
}
}
socketType := c.metadata["socket-type"]
if !c.socket.IsSocketTypeCompatible(SocketType(socketType)) {
return nil, fmt.Errorf("Socket type %v is not compatible with %v", c.socket.Type(), socketType)
}
return applicationMetadata, nil
}
// GetIdentity get connection's identity
func (c *Connection) GetIdentity() (string, error) {
if identity, ok := c.metadata["identity"]; ok {
return identity, nil
}
return "", fmt.Errorf("Indentiy not exist")
}
// SendCommand sends a ZMTP command over a Connection
func (c *Connection) SendCommand(commandName string, body []byte) error {
cmdLen := len(commandName)
if cmdLen > 255 {
return errors.New("Command names may not be longer than 255 characters")
}
bodyLen := len(body)
buf := make([]byte, 1+cmdLen+bodyLen) // FIXME(sbinet): maybe use a pool of []byte ?
buf[0] = byte(cmdLen)
copy(buf[1:], []byte(commandName))
copy(buf[1+cmdLen:], body)
return c.send(true, buf)
}
// SendFrame sends a ZMTP frame over a Connection
func (c *Connection) SendFrame(body []byte) error {
return c.send(false, body)
}
func (c *Connection) send(isCommand bool, body []byte) error {
// Compute total body length
length := len(body)
var bitFlags byte
// More flag: Unused, we don't support multiframe messages
// Long flag
isLong := length > 255
if isLong {
bitFlags ^= isLongBitFlag
}
// Command flag
if isCommand {
bitFlags ^= isCommandBitFlag
}
// Write out the message itself
if _, err := c.rw.Write([]byte{bitFlags}); err != nil {
return err
}
if isLong {
var buf [8]byte
byteOrder.PutUint64(buf[:], uint64(len(body)))
if _, err := c.rw.Write(buf[:]); err != nil {
return err
}
} else {
if _, err := c.rw.Write([]byte{uint8(len(body))}); err != nil {
return err
}
}
if _, err := c.rw.Write(c.securityMechanism.Encrypt(body)); err != nil {
return err
}
return nil
}
// Recv starts listening to the ReadWriter and passes *Message to a channel
func (c *Connection) Recv(messageOut chan<- *Message) {
go func() {
for {
// Actually read out the body and send it over the channel now
isCommand, body, err := c.read()
if err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
if !isCommand {
// Data frame
frames := [][]byte{body}
messageOut <- &Message{Body: frames, MessageType: UserMessage}
} else {
command, err := c.parseCommand(body)
if err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
// Check what type of command we got
// Certain commands we deal with directly, the rest we send over to the application
switch command.Name {
case "PING":
// When we get a ping, we want to send back a pong, we don't really care about the contents right now
if err := c.SendCommand("PONG", nil); err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
default:
frames := [][]byte{command.Body}
messageOut <- &Message{Name: command.Name, Body: frames, MessageType: ErrorMessage}
}
}
}
}()
}
// read returns the isCommand flag, the body of the message, and optionally an error
func (c *Connection) read() (bool, []byte, error) {
var header [2]byte
var longLength [8]byte
// Read out the header
_, err := io.ReadFull(c.rw, header[:])
if err != nil {
return false, nil, err
}
bitFlags := header[0]
// Read all the flags
hasMore := bitFlags&hasMoreBitFlag == hasMoreBitFlag
isLong := bitFlags&isLongBitFlag == isLongBitFlag
isCommand := bitFlags&isCommandBitFlag == isCommandBitFlag
// Error out in case get a more flag set to true
if hasMore {
return false, nil, errors.New("Received a packet with the MORE flag set to true, we don't support more")
}
// Determine the actual length of the body
bodyLength := uint64(0)
if isLong {
// We read 2 bytes of the header already
// In case of a long message, the length is bytes 2-8 of the header
// We already have the first byte, so assign it, and then read the rest
longLength[0] = header[1]
_, err := io.ReadFull(c.rw, longLength[1:])
if err != nil {
return false, nil, err
}
bodyLength = byteOrder.Uint64(longLength[:])
} else {
// Short message length is just 1 byte, read it
bodyLength = uint64(header[1])
}
if bodyLength > uint64(maxInt64) {
return false, nil, fmt.Errorf("Body length %v overflows max int64 value %v", bodyLength, maxInt64)
}
buf := make([]byte, bodyLength)
_, err = io.ReadFull(c.rw, buf)
if err != nil {
return false, nil, err
}
return isCommand, buf, nil
}
func (c *Connection) parseCommand(body []byte) (*Command, error) {
// Sanity check
if len(body) == 0 {
return nil, errors.New("Got empty command frame body")
}
// Read out the command length
commandNameLength := int(body[0])
if commandNameLength > len(body)-1 {
return nil, fmt.Errorf("Got command name length %v, which is too long for a body of length %v", commandNameLength, len(body))
}
command := &Command{
Name: string(body[1 : commandNameLength+1]),
Body: body[1+commandNameLength:],
}
return command, nil
}
func (c *Connection) SendMultipart(bs [][]byte) error {
const cmd = false
return c.sendMultipart(cmd, bs)
}
func (c *Connection) sendMultipart(isCommand bool, bs [][]byte) error {
for i, part := range bs {
// Compute total body length
length := len(part)
var bitFlags byte
// More flag:
if i < len(bs)-1 {
bitFlags ^= hasMoreBitFlag
}
// Long flag
isLong := length > 255
if isLong {
bitFlags ^= isLongBitFlag
}
// Command flag
if isCommand {
bitFlags ^= isCommandBitFlag
}
// Write out the message itself
if _, err := c.rw.Write([]byte{bitFlags}); err != nil {
return err
}
if isLong {
var buf [8]byte
byteOrder.PutUint64(buf[:], uint64(len(part)))
if _, err := c.rw.Write(buf[:]); err != nil {
return err
}
} else {
if _, err := c.rw.Write([]byte{uint8(len(part))}); err != nil {
return err
}
}
if _, err := c.rw.Write(c.securityMechanism.Encrypt(part)); err != nil {
return err
}
}
return nil
}
// RecvMultipart starts listening to the ReadWriter and passes *Message to a channel
func (c *Connection) RecvMultipart(messageOut chan<- *Message) {
go func() {
for {
// Actually read out the body and send it over the channel now
isCommand, body, err := c.readMultipart()
if err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
if !isCommand {
// Data frame
messageOut <- &Message{Body: body, MessageType: UserMessage}
} else {
command, err := c.parseCommand(body[0])
if err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
// Check what type of command we got
// Certain commands we deal with directly, the rest we send over to the application
switch command.Name {
case "PING":
// When we get a ping, we want to send back a pong, we don't really care about the contents right now
if err := c.SendCommand("PONG", nil); err != nil {
messageOut <- &Message{Err: err, MessageType: ErrorMessage}
return
}
default:
frames := [][]byte{command.Body}
messageOut <- &Message{Name: command.Name, Body: frames, MessageType: ErrorMessage}
}
}
}
}()
}
// readMultipart returns the isCommand flag, the body of the message, and optionally an error
func (c *Connection) readMultipart() (bool, [][]byte, error) {
var (
header [2]byte
longLength [8]byte
frames [][]byte
hasMore = true
isCommand = false
)
for hasMore {
// Read out the header
_, err := io.ReadFull(c.rw, header[:])
if err != nil {
return false, nil, err
}
bitFlags := header[0]
// Read all the flags
hasMore = bitFlags&hasMoreBitFlag == hasMoreBitFlag
isLong := bitFlags&isLongBitFlag == isLongBitFlag
isCommand = isCommand || (bitFlags&isCommandBitFlag == isCommandBitFlag)
// Determine the actual length of the body
bodyLength := uint64(0)
if isLong {
// We read 2 bytes of the header already
// In case of a long message, the length is bytes 2-8 of the header
// We already have the first byte, so assign it, and then read the rest
longLength[0] = header[1]
_, err := io.ReadFull(c.rw, longLength[1:])
if err != nil {
return false, nil, err
}
bodyLength = byteOrder.Uint64(longLength[:])
} else {
// Short message length is just 1 byte, read it
bodyLength = uint64(header[1])
}
if bodyLength > uint64(maxInt64) {
return false, nil, fmt.Errorf("Body length %v overflows max int64 value %v", bodyLength, maxInt64)
}
buf := make([]byte, bodyLength)
_, err = io.ReadFull(c.rw, buf)
if err != nil {
return false, nil, err
}
frames = append(frames, buf)
}
return isCommand, frames, nil
}