forked from beito123/go-raknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
267 lines (220 loc) · 5.71 KB
/
type.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
/**
* SpecterGO Copyright (C) 2018 SpecterTeam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package raknet
import (
"net"
"strconv"
"github.com/satori/go.uuid"
)
/*
RaknetProtocol Interface
*/
// Packet is a basic packet
type Packet interface {
ID() byte
Encode() error
Decode() error
Bytes() []byte
SetBytes([]byte)
New() Packet
}
// Protocol is a packet protocol
type Protocol interface {
RegisterPackets()
Packet(id byte) Packet
Packets() []Packet
}
/*
ConnectionType
*/
const MaxMetadataValues = 0xff
// Metadata is metadata for ConnectionType
type Metadata map[string]string
// ConnectionTypeMagic is the magic for ConnectionType.
var ConnctionTypeMagic = []byte{0x03, 0x08, 0x05, 0x0b, 0x43, 0x54, 0x49}
// ConnectionVanilla is a connection from a vanilla client or an unknown implementation
var ConnectionVanilla = ConnectionType{
Name: "Vanilla",
IsVanilla: true,
}
// ConnectionGoRaknet is a connection from go-raknet.
var ConnectionGoRaknet = &ConnectionType{
UUID: uuid.FromStringOrNil("495248b9-d485-4389-acd0-175fdb2233cf"),
Name: "GoRaknet",
Lang: "Go",
Version: "2.9.2",
}
// ConnectionType used to signify which implementation of the Raknet protocol
type ConnectionType struct {
UUID uuid.UUID
Name string
Lang string
Version string
Metadata Metadata
IsVanilla bool
}
/*
SystemAddress
*/
// NewSystemAddress returns a new SystemAddress from string
func NewSystemAddress(addr string, port uint16) *SystemAddress {
return &SystemAddress{
IP: net.ParseIP(addr),
Port: port,
}
}
// NewSystemAddress returns a new SystemAddress from bytes
func NewSystemAddressBytes(addr []byte, port uint16) *SystemAddress {
return &SystemAddress{
IP: net.IP(addr).To16(),
Port: port,
}
}
// SystemAddress is internal address for Raknet
type SystemAddress struct {
IP net.IP
Port uint16
}
// SetLoopback sets loopback address
func (addr *SystemAddress) SetLoopback() {
if len(addr.IP) == net.IPv4len {
addr.IP = net.ParseIP("127.0.0.1")
} else {
addr.IP = net.IPv6loopback // "::1"
}
}
// IsLoopback returns whether this is loopback address
func (addr *SystemAddress) IsLoopback() bool {
return addr.IP.IsLoopback()
}
// Version returns the ip address version (4 or 6)
func (addr *SystemAddress) Version() int {
if len(addr.IP) == net.IPv6len {
return 6
}
return 4
}
// Equal returns whether sub is the same address
func (addr *SystemAddress) Equal(sub *SystemAddress) bool {
return addr.IP.Equal(sub.IP) && addr.Port == sub.Port
}
// String returns as string
// Format: 192.168.11.1:8080, [fc00::]:8080
func (addr *SystemAddress) String() string {
if len(addr.IP) == net.IPv6len {
return "[" + addr.IP.String() + "]:" + strconv.Itoa(int(addr.Port))
}
return addr.IP.String() + ":" + strconv.Itoa(int(addr.Port))
}
/*
Reliability
*/
// Reliability decides reliable and ordered of packet when sending
// Thanks: http://www.jenkinssoftware.com/raknet/manual/Doxygen/PacketPriority_8h.html#e41fa01235e99dced384d137fa874a7e
type Reliability int
const (
// Unreliable is normal UDP packet.
Unreliable Reliability = iota
// UnreliableSequenced is the same as Unreliable. but it has Sequenced.
UnreliableSequenced
Reliable
ReliableOrdered
ReliableSequenced
UnreliableWithACKReceipt
ReliableWithACKReceipt
ReliableOrderedWithACKReceipt
)
// IsReliable returns whether reliability has reliable
func (r Reliability) IsReliable() bool {
return (r & 0x04) > 0
}
// IsOrdered returns whether reliability has ordered
func (r Reliability) IsOrdered() bool {
return (r & 0x02) > 0
}
// IsSequenced returns whether reliability has sequenced
func (r Reliability) IsSequenced() bool {
return (r & 0x01) > 0
}
// IsNeededACK returns whether reliability need ack
func (r Reliability) IsNeededACK() bool {
return r == UnreliableWithACKReceipt ||
r == ReliableWithACKReceipt ||
r == ReliableOrderedWithACKReceipt
}
// ToBinary encode reliability to bytes
func (r Reliability) ToBinary() byte {
var b byte
if r.IsReliable() {
b |= 1 << 2
}
if r.IsOrdered() {
b |= 1 << 1
}
if r.IsSequenced() {
b |= 1
}
return b
}
// ReliabilityBinary returns Reliability from binary
func ReliabilityBinary(b byte) Reliability {
if (b & 0x04) > 0 { // Reliable
if (b & 0x02) > 0 { // Ordered
return ReliableOrdered
} else if (b & 0x01) > 0 { // Sequenced
return ReliableSequenced
} else {
return Reliable
}
} else { // Unreliable
if (b & 0x01) > 0 { // Sequenced
return UnreliableSequenced
}
}
return Unreliable
}
/*
Records
*/
// Record is ack numbers container for acknowledge packet
type Record struct {
Index int
EndIndex int
}
// IsRanged returns whether the record is range
func (rec *Record) IsRanged() bool {
return rec.Index < rec.EndIndex
}
// Count returns the number of Record
func (rec *Record) Count() int {
if !rec.IsRanged() {
return 1
}
return (rec.EndIndex - rec.Index) + 1
}
// Numbers returns numbers recorded in Record as array
func (rec *Record) Numbers() []int {
count := rec.Count()
if count == 1 {
return []int{rec.Index}
}
numbers := make([]int, count)
for i := 0; i < count; i++ {
numbers[i] = rec.Index + i
}
return numbers
}