-
Notifications
You must be signed in to change notification settings - Fork 38
/
packet.go
222 lines (199 loc) · 5.6 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
package raknet
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
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.
type packet struct {
reliability byte
messageIndex uint24
sequenceIndex uint24
orderIndex uint24
content []byte
split bool
splitCount uint32
splitIndex uint32
splitID uint16
}
// write writes the packet and its content to the buffer passed.
func (pk *packet) write(buf *bytes.Buffer) {
header := pk.reliability << 5
if pk.split {
header |= splitFlag
}
buf.WriteByte(header)
writeUint16(buf, uint16(len(pk.content))<<3)
if pk.reliable() {
writeUint24(buf, pk.messageIndex)
}
if pk.sequenced() {
writeUint24(buf, pk.sequenceIndex)
}
if pk.sequencedOrOrdered() {
writeUint24(buf, pk.orderIndex)
// Order channel, we don't care about this.
buf.WriteByte(0)
}
if pk.split {
writeUint32(buf, pk.splitCount)
writeUint16(buf, pk.splitID)
writeUint32(buf, pk.splitIndex)
}
buf.Write(pk.content)
}
// read reads a packet and its content from the buffer passed.
func (pk *packet) read(b []byte) (int, error) {
if len(b) < 3 {
return 0, io.ErrUnexpectedEOF
}
header := b[0]
pk.split = (header & splitFlag) != 0
pk.reliability = (header & 224) >> 5
n := binary.BigEndian.Uint16(b[1:]) >> 3
if n == 0 {
return 0, errors.New("invalid packet length: cannot be 0")
}
offset := 3
if pk.reliable() {
if len(b)-offset < 3 {
return 0, io.ErrUnexpectedEOF
}
pk.messageIndex = loadUint24(b[offset:])
offset += 3
}
if pk.sequenced() {
if len(b)-offset < 3 {
return 0, io.ErrUnexpectedEOF
}
pk.sequenceIndex = loadUint24(b[offset:])
offset += 3
}
if pk.sequencedOrOrdered() {
if len(b)-offset < 4 {
return 0, io.ErrUnexpectedEOF
}
pk.orderIndex = loadUint24(b[offset:])
// Order channel (byte)
offset += 4
}
if pk.split {
if len(b)-offset < 10 {
return 0, io.ErrUnexpectedEOF
}
pk.splitCount = binary.BigEndian.Uint32(b[offset:])
pk.splitID = binary.BigEndian.Uint16(b[offset+4:])
pk.splitIndex = binary.BigEndian.Uint32(b[offset+6:])
offset += 10
}
pk.content = make([]byte, n)
if got := copy(pk.content, b[offset:]); got != int(n) {
return 0, io.ErrUnexpectedEOF
}
return offset + int(n), nil
}
func (pk *packet) reliable() bool {
switch pk.reliability {
case reliabilityReliable,
reliabilityReliableOrdered,
reliabilityReliableSequenced:
return true
default:
return false
}
}
func (pk *packet) sequencedOrOrdered() bool {
switch pk.reliability {
case reliabilityUnreliableSequenced,
reliabilityReliableOrdered,
reliabilityReliableSequenced:
return true
default:
return false
}
}
func (pk *packet) sequenced() bool {
switch pk.reliability {
case reliabilityUnreliableSequenced,
reliabilityReliableSequenced:
return true
default:
return false
}
}
const (
// Datagram header +
// Datagram sequence number +
// Packet header +
// Packet content length +
// Packet message index +
// Packet order index +
// Packet order channel
packetAdditionalSize = 1 + 3 + 1 + 2 + 3 + 3 + 1
// Packet split count +
// Packet split ID +
// Packet split index
splitAdditionalSize = 4 + 2 + 4
)
// split splits a content buffer in smaller buffers so that they do not exceed
// the MTU size that the connection holds.
func split(b []byte, mtu uint16) [][]byte {
n := len(b)
maxSize := int(mtu - packetAdditionalSize)
if n > maxSize {
// If the content size is bigger than the maximum size here, it means
// the packet will get split. This means that the packet will get even
// bigger because a split packet uses 4 + 2 + 4 more bytes.
maxSize -= splitAdditionalSize
}
// If the content length can't be divided by maxSize perfectly, we need
// to reserve another fragment for the last bit of the packet.
fragmentCount := n/maxSize + min(n%maxSize, 1)
fragments := make([][]byte, fragmentCount)
for i := range fragmentCount - 1 {
fragments[i] = b[:maxSize]
b = b[maxSize:]
}
fragments[len(fragments)-1] = b
return fragments
}