-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
75 lines (64 loc) · 1.68 KB
/
message.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
package gogossip
import (
"net"
)
type packet interface {
Kind() byte
// 'packet' handler returns packet list if need respond. and basically
// it is used to divide and transmit a large response. Add 'to' to
// the packet itself, as there may be times when need to send a
// response to multiple peers later.
//
// It is removed in marshalling and cannot be used on the receiver side.
To() *net.UDPAddr
}
const (
pullRequestType = 0x01
pullResponseType = 0x02
)
type packetType byte
func (p packetType) String() string {
switch p {
case pullRequestType:
return "PullRequest"
case pullResponseType:
return "PullResponse"
}
return ""
}
type (
pullRequest struct {
}
pullResponse struct {
to *net.UDPAddr
Keys [][8]byte
Values [][]byte
}
)
func marshalWithEncryption(p packet, encType EncryptType, passphrase string) ([]byte, error) {
cipher, err := encryptPacket(encType, passphrase, p)
if err != nil {
return nil, err
}
return bytesToLabel([]byte{p.Kind(), byte(encType)}).combine(cipher)
}
func unmarshalWithDecryption(buf []byte, passphrase string) (*label, []byte, error) {
label, payload, err := splitLabel(buf)
if err != nil {
return nil, nil, err
}
plain, err := decryptPayload(payload, EncryptType(label.encryptType), passphrase)
if err != nil {
return nil, nil, err
}
return label, plain, err
}
func (req *pullRequest) Kind() byte { return pullRequestType }
func (req *pullRequest) To() *net.UDPAddr { panic("not supported") }
func (res *pullResponse) Kind() byte { return pullResponseType }
func (res *pullResponse) To() *net.UDPAddr {
if res.to == nil {
panic("'to' is empty (hint: maybe you are the recipient)")
}
return res.to
}