-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathpacket.go
353 lines (301 loc) · 7.54 KB
/
packet.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
package flatend
import (
"errors"
"fmt"
"github.com/lithdew/bytesutil"
"github.com/lithdew/kademlia"
"io"
"math"
"net"
"strconv"
"unicode/utf8"
"unsafe"
)
type Opcode = uint8
const (
OpcodeHandshake Opcode = iota
OpcodeServiceRequest
OpcodeServiceResponse
OpcodeData
OpcodeFindNodeRequest
OpcodeFindNodeResponse
)
type ServiceRequestPacket struct {
ID uint32 // stream id
Services []string // services this packet may be processed through
Headers map[string]string // headers for this packet
}
func (p ServiceRequestPacket) AppendTo(dst []byte) []byte {
dst = bytesutil.AppendUint32BE(dst, p.ID)
dst = append(dst, uint8(len(p.Services)))
for _, service := range p.Services {
dst = append(dst, uint8(len(service)))
dst = append(dst, service...)
}
if p.Headers != nil {
dst = bytesutil.AppendUint16BE(dst, uint16(len(p.Headers)))
for name, value := range p.Headers {
dst = append(dst, byte(len(name)))
dst = append(dst, name...)
dst = bytesutil.AppendUint16BE(dst, uint16(len(value)))
dst = append(dst, value...)
}
} else {
dst = bytesutil.AppendUint16BE(dst, 0)
}
return dst
}
func UnmarshalServiceRequestPacket(buf []byte) (ServiceRequestPacket, error) {
var packet ServiceRequestPacket
{
if len(buf) < 4 {
return packet, io.ErrUnexpectedEOF
}
packet.ID, buf = bytesutil.Uint32BE(buf[:4]), buf[4:]
}
{
var size uint8
size, buf = buf[0], buf[1:]
packet.Services = make([]string, size)
for i := 0; i < len(packet.Services); i++ {
if len(buf) < 1 {
return packet, io.ErrUnexpectedEOF
}
size, buf = buf[0], buf[1:]
if len(buf) < int(size) {
return packet, io.ErrUnexpectedEOF
}
packet.Services[i] = string(buf[:size])
buf = buf[size:]
}
}
{
if len(buf) < 2 {
return packet, io.ErrUnexpectedEOF
}
var size uint16
size, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
packet.Headers = make(map[string]string, size)
for i := uint16(0); i < size; i++ {
{
if len(buf) < 1 {
return packet, io.ErrUnexpectedEOF
}
var nameSize uint8
nameSize, buf = buf[0], buf[1:]
if len(buf) < int(nameSize) {
return packet, io.ErrUnexpectedEOF
}
var name string
name, buf = string(buf[:nameSize]), buf[nameSize:]
if len(buf) < 2 {
return packet, io.ErrUnexpectedEOF
}
var valueSize uint16
valueSize, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
if len(buf) < int(valueSize) {
return packet, io.ErrUnexpectedEOF
}
var value string
value, buf = string(buf[:valueSize]), buf[valueSize:]
packet.Headers[name] = value
}
}
}
return packet, nil
}
type ServiceResponsePacket struct {
ID uint32 // stream id
Handled bool // whether or not the service was handled
Headers map[string]string // headers for this packet
}
func (p ServiceResponsePacket) AppendTo(dst []byte) []byte {
dst = bytesutil.AppendUint32BE(dst, p.ID)
if p.Handled {
dst = append(dst, 1)
} else {
dst = append(dst, 0)
}
if p.Headers != nil {
dst = bytesutil.AppendUint16BE(dst, uint16(len(p.Headers)))
for name, value := range p.Headers {
dst = append(dst, byte(len(name)))
dst = append(dst, name...)
dst = bytesutil.AppendUint16BE(dst, uint16(len(value)))
dst = append(dst, value...)
}
} else {
dst = bytesutil.AppendUint16BE(dst, 0)
}
return dst
}
func UnmarshalServiceResponsePacket(buf []byte) (ServiceResponsePacket, error) {
var packet ServiceResponsePacket
{
if len(buf) < 5 {
return packet, io.ErrUnexpectedEOF
}
packet.ID, buf = bytesutil.Uint32BE(buf[:4]), buf[4:]
packet.Handled, buf = buf[0] == 1, buf[1:]
}
{
if len(buf) < 2 {
return packet, io.ErrUnexpectedEOF
}
var size uint16
size, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
packet.Headers = make(map[string]string, size)
for i := uint16(0); i < size; i++ {
{
if len(buf) < 1 {
return packet, io.ErrUnexpectedEOF
}
var nameSize uint8
nameSize, buf = buf[0], buf[1:]
if len(buf) < int(nameSize) {
return packet, io.ErrUnexpectedEOF
}
var name string
name, buf = string(buf[:nameSize]), buf[nameSize:]
if len(buf) < 2 {
return packet, io.ErrUnexpectedEOF
}
var valueSize uint16
valueSize, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
if len(buf) < int(valueSize) {
return packet, io.ErrUnexpectedEOF
}
var value string
value, buf = string(buf[:valueSize]), buf[valueSize:]
packet.Headers[name] = value
}
}
}
return packet, nil
}
type DataPacket struct {
ID uint32
Data []byte
}
func (p DataPacket) AppendTo(dst []byte) []byte {
dst = bytesutil.AppendUint32BE(dst, p.ID)
dst = bytesutil.AppendUint16BE(dst, uint16(len(p.Data)))
dst = append(dst, p.Data...)
return dst
}
func UnmarshalDataPacket(buf []byte) (DataPacket, error) {
var packet DataPacket
if len(buf) < 4+2 {
return packet, io.ErrUnexpectedEOF
}
packet.ID, buf = bytesutil.Uint32BE(buf[:4]), buf[4:]
var size uint16
size, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
if uint16(len(buf)) < size {
return packet, io.ErrUnexpectedEOF
}
packet.Data, buf = buf[:size], buf[size:]
return packet, nil
}
type HandshakePacket struct {
ID *kademlia.ID
Services []string
Signature kademlia.Signature
}
func (h HandshakePacket) AppendPayloadTo(dst []byte) []byte {
dst = h.ID.AppendTo(dst)
for _, service := range h.Services {
dst = append(dst, service...)
}
return dst
}
func (h HandshakePacket) AppendTo(dst []byte) []byte {
if h.ID != nil {
dst = append(dst, 1)
dst = h.ID.AppendTo(dst)
} else {
dst = append(dst, 0)
}
dst = append(dst, uint8(len(h.Services)))
for _, service := range h.Services {
dst = append(dst, uint8(len(service)))
dst = append(dst, service...)
}
if h.ID != nil {
dst = append(dst, h.Signature[:]...)
}
return dst
}
func UnmarshalHandshakePacket(buf []byte) (HandshakePacket, error) {
var pkt HandshakePacket
if len(buf) < 1 {
return pkt, io.ErrUnexpectedEOF
}
hasID := buf[0] == 1
buf = buf[1:]
if hasID {
id, leftover, err := kademlia.UnmarshalID(buf)
if err != nil {
return pkt, err
}
pkt.ID = &id
buf = leftover
}
if len(buf) < 1 {
return pkt, io.ErrUnexpectedEOF
}
var size uint8
size, buf = buf[0], buf[1:]
if len(buf) < int(size) {
return pkt, io.ErrUnexpectedEOF
}
pkt.Services = make([]string, size)
for i := 0; i < len(pkt.Services); i++ {
if len(buf) < 1 {
return pkt, io.ErrUnexpectedEOF
}
size, buf = buf[0], buf[1:]
if len(buf) < int(size) {
return pkt, io.ErrUnexpectedEOF
}
pkt.Services[i] = string(buf[:size])
buf = buf[size:]
}
if hasID {
if len(buf) < kademlia.SizeSignature {
return pkt, io.ErrUnexpectedEOF
}
pkt.Signature, buf = *(*kademlia.Signature)(unsafe.Pointer(&((buf[:kademlia.SizeSignature])[0]))),
buf[kademlia.SizeSignature:]
}
return pkt, nil
}
func (h HandshakePacket) Validate(dst []byte) error {
if h.ID != nil {
err := h.ID.Validate()
if err != nil {
return err
}
}
for _, service := range h.Services {
if !utf8.ValidString(service) {
return fmt.Errorf("service '%s' in hello packet is not valid utf8", service)
}
if len(service) > math.MaxUint8 {
return fmt.Errorf("service '%s' in hello packet is too large - must <= %d bytes",
service, math.MaxUint8)
}
}
if h.ID != nil && !h.Signature.Verify(h.ID.Pub, h.AppendPayloadTo(dst)) {
return errors.New("signature is malformed")
}
return nil
}
func Addr(host net.IP, port uint16) string {
h := ""
if len(host) > 0 {
h = host.String()
}
p := strconv.FormatUint(uint64(port), 10)
return net.JoinHostPort(h, p)
}