/
packet.go
330 lines (304 loc) · 9.4 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
package raknet
import (
"bytes"
"encoding/binary"
"fmt"
"sort"
)
const (
// bitFlagDatagram is set for every valid datagram. It is used to identify
// packets that are datagrams.
bitFlagDatagram = 0x80
// bitFlagACK is set for every ACK packet.
bitFlagACK = 0x40
// bitFlagNACK is set for every NACK packet.
bitFlagNACK = 0x20
// bitFlagNeedsBAndAS is set for every datagram with packet data, but is not
// actually used.
bitFlagNeedsBAndAS = 0x04
)
// noinspection GoUnusedConst
const (
// reliabilityUnreliable means that the packet sent could arrive out of
// order, be duplicated, or just not arrive at all. It is usually used for
// high frequency packets of which the order does not matter.
//lint:ignore U1000 While this constant is unused, it is here for the sake
// of having all reliabilities documented.
reliabilityUnreliable byte = iota
// reliabilityUnreliableSequenced means that the packet sent could be
// duplicated or not arrive at all, but ensures that it is always handled in
// the right order.
reliabilityUnreliableSequenced
// reliabilityReliable means that the packet sent could not arrive, or
// arrive out of order, but ensures that the packet is not duplicated.
reliabilityReliable
// reliabilityReliableOrdered means that every packet sent arrives, arrives
// in the right order and is not duplicated.
reliabilityReliableOrdered
// reliabilityReliableSequenced means that the packet sent could not arrive,
// but ensures that the packet will be in the right order and not be
// duplicated.
reliabilityReliableSequenced
// splitFlag is set in the header if the packet was split. If so, the
// encapsulation contains additional data about the fragment.
splitFlag = 0x10
)
// packet is an encapsulation around every packet sent after the connection is
// established. It is
type packet struct {
reliability byte
content []byte
messageIndex uint24
sequenceIndex uint24
orderIndex uint24
split bool
splitCount uint32
splitIndex uint32
splitID uint16
}
// write writes the packet and its content to the buffer passed.
func (packet *packet) write(b *bytes.Buffer) {
header := packet.reliability << 5
if packet.split {
header |= splitFlag
}
b.WriteByte(header)
_ = binary.Write(b, binary.BigEndian, uint16(len(packet.content))<<3)
if packet.reliable() {
writeUint24(b, packet.messageIndex)
}
if packet.sequenced() {
writeUint24(b, packet.sequenceIndex)
}
if packet.sequencedOrOrdered() {
writeUint24(b, packet.orderIndex)
// Order channel, we don't care about this.
b.WriteByte(0)
}
if packet.split {
_ = binary.Write(b, binary.BigEndian, packet.splitCount)
_ = binary.Write(b, binary.BigEndian, packet.splitID)
_ = binary.Write(b, binary.BigEndian, packet.splitIndex)
}
b.Write(packet.content)
}
// read reads a packet and its content from the buffer passed.
func (packet *packet) read(b *bytes.Buffer) error {
header, err := b.ReadByte()
if err != nil {
return fmt.Errorf("error reading packet header: %v", err)
}
packet.split = (header & splitFlag) != 0
packet.reliability = (header & 224) >> 5
var packetLength uint16
if err := binary.Read(b, binary.BigEndian, &packetLength); err != nil {
return fmt.Errorf("error reading packet length: %v", err)
}
packetLength >>= 3
if packetLength == 0 {
return fmt.Errorf("invalid packet length: cannot be 0")
}
if packet.reliable() {
packet.messageIndex, err = readUint24(b)
if err != nil {
return fmt.Errorf("error reading packet message index: %v", err)
}
}
if packet.sequenced() {
packet.sequenceIndex, err = readUint24(b)
if err != nil {
return fmt.Errorf("error reading packet sequence index: %v", err)
}
}
if packet.sequencedOrOrdered() {
packet.orderIndex, err = readUint24(b)
if err != nil {
return fmt.Errorf("error reading packet order index: %v", err)
}
// Order channel (byte), we don't care about this.
b.Next(1)
}
if packet.split {
if err := binary.Read(b, binary.BigEndian, &packet.splitCount); err != nil {
return fmt.Errorf("error reading packet split count: %v", err)
}
if err := binary.Read(b, binary.BigEndian, &packet.splitID); err != nil {
return fmt.Errorf("error reading packet split ID: %v", err)
}
if err := binary.Read(b, binary.BigEndian, &packet.splitIndex); err != nil {
return fmt.Errorf("error reading packet split index: %v", err)
}
}
packet.content = make([]byte, packetLength)
if n, err := b.Read(packet.content); err != nil || n != int(packetLength) {
return fmt.Errorf("not enough data in packet: %v bytes read but need %v", n, packetLength)
}
return nil
}
func (packet *packet) reliable() bool {
switch packet.reliability {
case reliabilityReliable,
reliabilityReliableOrdered,
reliabilityReliableSequenced:
return true
}
return false
}
func (packet *packet) sequencedOrOrdered() bool {
switch packet.reliability {
case reliabilityUnreliableSequenced,
reliabilityReliableOrdered,
reliabilityReliableSequenced:
return true
}
return false
}
func (packet *packet) sequenced() bool {
switch packet.reliability {
case reliabilityUnreliableSequenced,
reliabilityReliableSequenced:
return true
}
return false
}
const (
// packetRange indicates a range of packets, followed by the first and the
// last packet in the range.
packetRange = iota
// packetSingle indicates a single packet, followed by its sequence number.
packetSingle
)
// acknowledgement is an acknowledgement packet that may either be an ACK or a
// NACK, depending on the purpose that it is sent with.
type acknowledgement struct {
packets []uint24
}
// write encodes an acknowledgement packet and returns an error if not
// successful.
func (ack *acknowledgement) write(b *bytes.Buffer, mtu uint16) (n int, err error) {
packets := ack.packets
if len(packets) == 0 {
return 0, binary.Write(b, binary.BigEndian, int16(0))
}
buffer := bytes.NewBuffer(nil)
// Sort packets before encoding to ensure packets are encoded correctly.
sort.Slice(packets, func(i, j int) bool {
return packets[i] < packets[j]
})
var firstPacketInRange uint24
var lastPacketInRange uint24
var recordCount int16
for index, packet := range packets {
if buffer.Len() >= int(mtu-10) {
// We must make sure the final packet length doesn't exceed the MTU
// size.
break
}
n++
if index == 0 {
// The first packet, set the first and last packet to it.
firstPacketInRange = packet
lastPacketInRange = packet
continue
}
if packet == lastPacketInRange+1 {
// Packet is still part of the current range, as it's sequenced
// properly with the last packet. Set the last packet in range to
// the packet and continue to the next packet.
lastPacketInRange = packet
continue
} else {
// We got to the end of a range/single packet. We need to write
// those down now.
if firstPacketInRange == lastPacketInRange {
// First packet equals last packet, so we have a single packet
// record. Write down the packet, and set the first and last
// packet to the current packet.
if err := buffer.WriteByte(packetSingle); err != nil {
return 0, err
}
writeUint24(buffer, firstPacketInRange)
firstPacketInRange = packet
lastPacketInRange = packet
} else {
// There's a gap between the first and last packet, so we have a
// range of packets. Write the first and last packet of the
// range and set both to the current packet.
if err := buffer.WriteByte(packetRange); err != nil {
return 0, err
}
writeUint24(buffer, firstPacketInRange)
writeUint24(buffer, lastPacketInRange)
firstPacketInRange = packet
lastPacketInRange = packet
}
// Keep track of the amount of records as we need to write that
// first.
recordCount++
}
}
// Make sure the last single packet/range is written, as we always need to
// know one packet ahead to know how we should write the current.
if firstPacketInRange == lastPacketInRange {
if err := buffer.WriteByte(packetSingle); err != nil {
return 0, err
}
writeUint24(buffer, firstPacketInRange)
} else {
if err := buffer.WriteByte(packetRange); err != nil {
return 0, err
}
writeUint24(buffer, firstPacketInRange)
writeUint24(buffer, lastPacketInRange)
}
recordCount++
if err := binary.Write(b, binary.BigEndian, recordCount); err != nil {
return 0, err
}
if _, err := b.Write(buffer.Bytes()); err != nil {
return 0, err
}
return n, nil
}
// read decodes an acknowledgement packet and returns an error if not
// successful.
func (ack *acknowledgement) read(b *bytes.Buffer) error {
const maxAcknowledgementPackets = 8192
var recordCount int16
if err := binary.Read(b, binary.BigEndian, &recordCount); err != nil {
return err
}
for i := int16(0); i < recordCount; i++ {
recordType, err := b.ReadByte()
if err != nil {
return err
}
switch recordType {
case packetRange:
start, err := readUint24(b)
if err != nil {
return err
}
end, err := readUint24(b)
if err != nil {
return err
}
for pack := start; pack <= end; pack++ {
ack.packets = append(ack.packets, pack)
if len(ack.packets) > maxAcknowledgementPackets {
return fmt.Errorf("maximum amount of packets in acknowledgement exceeded")
}
}
case packetSingle:
packet, err := readUint24(b)
if err != nil {
return err
}
ack.packets = append(ack.packets, packet)
if len(ack.packets) > maxAcknowledgementPackets {
return fmt.Errorf("maximum amount of packets in acknowledgement exceeded")
}
}
}
return nil
}