-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
374 lines (338 loc) · 8.67 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
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
package ntgo
import (
"io"
"errors"
)
const (
MessageTypeKeepAlive MessageType = 0x00
MessageTypeClientHello = 0x01
MessageTypeProtocVersionUnsupported = 0x02
MessageTypeServerHelloComplete = 0x03
MessageTypeServerHello = 0x04
MessageTypeClientHelloComplete = 0x05
MessageTypeEntryAssignment = 0x10
MessageTypeEntryUpdate = 0x11
MessageTypeEntryFlagsUpdate = 0x12
MessageTypeEntryDelete = 0x13
MessageTypeClearAll = 0x14
MessageTypeRPCExecute = 0x20
MessageTypeRPCResponse = 0x21
MessageTypeUndef = 0xFF
)
const (
FlagMessageClientNew MessageFlag = 0x00
FlagMessageClientSeen = 0x01
FlagMessageClientReserved = 0xFF
)
var (
ErrMessageNoSuchType = errors.New("message: no such message type")
ErrMessageFlagNoSuchType = errors.New("messageflag: no such flag")
)
var (
// DangerousMagic is a 4 byte sequence specified by the protocol documentation
// that should be sent as the message payload for a ClearAll message.
DangerousMagic = [4]byte{0xD0, 0x6C, 0xB2, 0x7A}
)
type ProtocolRevision [2]byte
type Message struct {
Type MessageType
Data MessageData
}
func DecodeMessage(r io.Reader) (*Message, error) {
messageType, typeErr := DecodeMessageType(r)
if typeErr != nil {
return nil, typeErr
}
message := &Message{
Type: messageType,
}
var messageData MessageData
var dataErr error = nil
switch messageType {
case MessageTypeKeepAlive:
messageData = &MessageDataKeepAlive{}
case MessageTypeClientHello:
messageData, dataErr = DecodeDataClientHello(r)
case MessageTypeProtocVersionUnsupported:
messageData, dataErr = DecodeDataProtocVersionUnsupported(r)
case MessageTypeServerHelloComplete:
messageData = &MessageDataServerHelloComplete{}
case MessageTypeServerHello:
messageData, dataErr = DecodeDataServerHello(r)
case MessageTypeClientHelloComplete:
messageData = &MessageDataClientHelloComplete{}
case MessageTypeEntryAssignment:
messageData, dataErr = DecodeDataEntryAssignment(r)
case MessageTypeEntryUpdate:
messageData, dataErr = DecodeDataEntryUpdate(r)
case MessageTypeEntryFlagsUpdate:
messageData, dataErr = DecodeDataEntryFlagsUpdate(r)
case MessageTypeClearAll:
messageData, dataErr = DecodeDataClearAll(r)
default:
dataErr = ErrMessageNoSuchType
}
message.Data = messageData
return message, dataErr
}
type MessageFlag byte
func DecodeMessageFlag(r io.Reader) (MessageFlag, error) {
flagRaw := make([]byte, 1)
_, flagErr := r.Read(flagRaw)
if flagErr != nil {
return FlagMessageClientReserved, flagErr
}
switch MessageFlag(flagRaw[0]) {
case FlagMessageClientNew:
return FlagMessageClientNew, nil
case FlagMessageClientSeen:
return FlagMessageClientSeen, nil
default:
return FlagMessageClientReserved, ErrMessageFlagNoSuchType
}
}
type MessageType byte
func DecodeMessageType(r io.Reader) (MessageType, error) {
typeRaw := make([]byte, 1)
_, typeErr := r.Read(typeRaw)
if typeErr != nil {
return EntryTypeUndef, typeErr
}
return MessageType(typeRaw[0]), nil
}
type MessageData interface{}
type MessageDataKeepAlive struct{}
type MessageDataClientHello struct {
ProtocVersion ProtocolRevision
Identity *ValueString
}
func DecodeDataClientHello(r io.Reader) (*MessageDataClientHello, error) {
protocRaw := [2]byte{}
_, protocErr := r.Read(protocRaw[:])
if protocErr != nil {
return nil, protocErr
}
identity, identityErr := DecodeString(r)
if identityErr != nil {
return nil, identityErr
}
return &MessageDataClientHello{
ProtocVersion: ProtocolRevision(protocRaw),
Identity: identity,
}, nil
}
type MessageDataProtocVersionUnsupported struct {
SupportedProtoc ProtocolRevision
}
func DecodeDataProtocVersionUnsupported(r io.Reader) (*MessageDataProtocVersionUnsupported, error) {
protocRaw := [2]byte{}
_, protocErr := r.Read(protocRaw[:])
if protocErr != nil {
return nil, protocErr
}
return &MessageDataProtocVersionUnsupported{
SupportedProtoc: protocRaw,
}, nil
}
type MessageDataServerHelloComplete struct{}
type MessageDataServerHello struct {
Flags MessageFlag
Identity *ValueString
}
func DecodeDataServerHello(r io.Reader) (*MessageDataServerHello, error) {
flag, flagErr := DecodeMessageFlag(r)
if flagErr != nil {
return nil, flagErr
}
identity, identityErr := DecodeString(r)
if identityErr != nil {
return nil, identityErr
}
return &MessageDataServerHello{
Flags: flag,
Identity: identity,
}, nil
}
type MessageDataClientHelloComplete struct{}
type MessageDataEntryAssignment struct {
Entry *Entry
}
func DecodeDataEntryAssignment(r io.Reader) (*MessageDataEntryAssignment, error) {
name, nameErr := DecodeString(r)
if nameErr != nil {
return nil, nameErr
}
entryType, entryErr := DecodeEntryType(r)
if entryErr != nil {
return nil, entryErr
}
idRaw := [2]byte{}
_, idErr := r.Read(idRaw[:])
if idErr != nil {
return nil, idErr
}
seqRaw := [2]byte{}
_, seqErr := r.Read(seqRaw[:])
if seqErr != nil {
return nil, idErr
}
flag, flagErr := DecodeEntryFlag(r)
if flagErr != nil {
return nil, flagErr
}
value, valueErr := DecodeEntryValue(r, entryType)
if valueErr != nil {
return nil, valueErr
}
return &MessageDataEntryAssignment{
Entry: &Entry{
Name: name,
Type: entryType,
ID: idRaw,
Sequence: seqRaw,
Flags: flag,
Value: value,
},
}, nil
}
type MessageDataEntryUpdate struct {
Entry *Entry
}
func DecodeDataEntryUpdate(r io.Reader) (*MessageDataEntryUpdate, error) {
idRaw := [2]byte{}
_, idErr := r.Read(idRaw[:])
if idErr != nil {
return nil, idErr
}
seqRaw := [2]byte{}
_, seqErr := r.Read(seqRaw[:])
if seqErr != nil {
return nil, idErr
}
value, entryType, valueErr := DecodeEntryValueAndType(r)
if valueErr != nil {
return nil, valueErr
}
return &MessageDataEntryUpdate{
Entry: &Entry{
ID: idRaw,
Sequence: seqRaw,
Type: entryType,
Value: value,
},
}, nil
}
type MessageDataEntryFlagsUpdate struct {
Entry *Entry
}
func DecodeDataEntryFlagsUpdate(r io.Reader) (*MessageDataEntryFlagsUpdate, error) {
idRaw := [2]byte{}
_, idErr := r.Read(idRaw[:])
if idErr != nil {
return nil, idErr
}
flag, flagErr := DecodeEntryFlag(r)
if flagErr != nil {
return nil, flagErr
}
return &MessageDataEntryFlagsUpdate{
Entry: &Entry{
ID: idRaw,
Flags: flag,
},
}, nil
}
type MessageDataEntryDelete struct {
Entry *Entry
}
func DecodeDataEntryDelete(r io.Reader) (*MessageDataEntryDelete, error) {
idRaw := [2]byte{}
_, idErr := r.Read(idRaw[:])
if idErr != nil {
return nil, idErr
}
return &MessageDataEntryDelete{
Entry: &Entry{
ID: idRaw,
},
}, nil
}
type MessageDataClearAll struct {
PotentialMagic [4]byte
}
func DecodeDataClearAll(r io.Reader) (*MessageDataClearAll, error) {
magicRaw := [4]byte{}
_, magicErr := r.Read(magicRaw[:])
if magicErr != nil {
return nil, magicErr
}
return &MessageDataClearAll{
PotentialMagic: magicRaw,
}, nil
}
type MessageDataRPCExecute struct {
EntryID [2]byte
UniqueID [2]byte
ParamLength uint32
Params []byte
}
func DecodeDataRPCExecute(r io.Reader) (*MessageDataRPCExecute, error) {
entryIDRaw := [2]byte{}
_, entryIDErr := r.Read(entryIDRaw[:])
if entryIDErr != nil {
return nil, entryIDErr
}
uniqueIDRaw := [2]byte{}
_, uniqueIDErr := r.Read(uniqueIDRaw[:])
if uniqueIDErr != nil {
return nil, uniqueIDErr
}
paramsSize, ulebErr := DecodeULEB128(r)
if ulebErr != nil {
return nil, ulebErr
}
params := make([]byte, paramsSize)
_, paramsErr := r.Read(params)
if paramsErr != nil {
return nil, paramsErr
}
return &MessageDataRPCExecute{
EntryID: entryIDRaw,
UniqueID: uniqueIDRaw,
ParamLength: paramsSize,
Params: params,
}, nil
}
type MessageDataRPCResponse struct {
EntryID [2]byte
UniqueID [2]byte
ResultLength uint32
Results []byte
}
func DecodeDataRPCReseponse(r io.Reader) (*MessageDataRPCResponse, error) {
entryIDRaw := [2]byte{}
_, entryIDErr := r.Read(entryIDRaw[:])
if entryIDErr != nil {
return nil, entryIDErr
}
uniqueIDRaw := [2]byte{}
_, uniqueIDErr := r.Read(uniqueIDRaw[:])
if uniqueIDErr != nil {
return nil, uniqueIDErr
}
resultsSize, ulebErr := DecodeULEB128(r)
if ulebErr != nil {
return nil, ulebErr
}
results := make([]byte, resultsSize)
_, resultsErr := r.Read(results)
if resultsErr != nil {
return nil, resultsErr
}
return &MessageDataRPCResponse{
EntryID: entryIDRaw,
UniqueID: uniqueIDRaw,
ResultLength: resultsSize,
Results: results,
}, nil
}