-
Notifications
You must be signed in to change notification settings - Fork 12
/
packet.h
468 lines (409 loc) · 16.1 KB
/
packet.h
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#ifndef PACKET_H
#define PACKET_H
#include <QObject>
#include <QHostInfo>
#include <QDebug>
#include <QUuid>
#include "hmacauth.h"
class PacketTypeEnum {
public:
// If adding a new packet packetType, you can replace one marked usable or add at the end.
// This enum must hold 256 or fewer packet types (so the value is <= 255) since it is statically typed as a uint8_t
enum class Value : uint8_t {
Unknown,
StunResponse,
DomainList,
Ping,
PingReply,
KillAvatar,
AvatarData,
InjectAudio,
MixedAudio,
MicrophoneAudioNoEcho,
MicrophoneAudioWithEcho,
BulkAvatarData,
SilentAudioFrame,
DomainListRequest,
RequestAssignment,
CreateAssignment,
DomainConnectionDenied,
MuteEnvironment,
AudioStreamStats,
DomainServerPathQuery,
DomainServerPathResponse,
DomainServerAddedNode,
ICEServerPeerInformation,
ICEServerQuery,
OctreeStats,
SetAvatarTraits,
AvatarIdentityRequest,
AssignmentClientStatus,
NoisyMute,
AvatarIdentity,
NodeIgnoreRequest,
DomainConnectRequest,
DomainServerRequireDTLS,
NodeJsonStats,
OctreeDataNack,
StopNode,
AudioEnvironment,
EntityEditNack,
ICEServerHeartbeat,
ICEPing,
ICEPingReply,
EntityData,
EntityQuery,
EntityAdd,
EntityErase,
EntityEdit,
DomainServerConnectionToken,
DomainSettingsRequest,
DomainSettings,
AssetGet,
AssetGetReply,
AssetUpload,
AssetUploadReply,
AssetGetInfo,
AssetGetInfoReply,
DomainDisconnectRequest,
DomainServerRemovedNode,
MessagesData,
MessagesSubscribe,
MessagesUnsubscribe,
ICEServerHeartbeatDenied,
AssetMappingOperation,
AssetMappingOperationReply,
ICEServerHeartbeatACK,
NegotiateAudioFormat,
SelectedAudioFormat,
MoreEntityShapes,
NodeKickRequest,
NodeMuteRequest,
RadiusIgnoreRequest,
UsernameFromIDRequest,
UsernameFromIDReply,
AvatarQuery,
RequestsDomainListData,
PerAvatarGainSet,
EntityScriptGetStatus,
EntityScriptGetStatusReply,
ReloadEntityServerScript,
EntityPhysics,
EntityServerScriptLog,
AdjustAvatarSorting,
OctreeFileReplacement,
CollisionEventChanges,
ReplicatedMicrophoneAudioNoEcho,
ReplicatedMicrophoneAudioWithEcho,
ReplicatedInjectAudio,
ReplicatedSilentAudioFrame,
ReplicatedAvatarIdentity,
ReplicatedKillAvatar,
ReplicatedBulkAvatarData,
DomainContentReplacementFromUrl,
ChallengeOwnership,
EntityScriptCallMethod,
ChallengeOwnershipRequest,
ChallengeOwnershipReply,
OctreeDataFileRequest,
OctreeDataFileReply,
OctreeDataPersist,
EntityClone,
EntityQueryInitialResultsComplete,
BulkAvatarTraits,
AudioSoloRequest,
ProxiedICEPing,
ProxiedICEPingReply,
ProxiedDomainListRequest,
NUM_PACKET_TYPE
};
const static QHash<PacketTypeEnum::Value, PacketTypeEnum::Value> GetReplicatedPacketMapping() {
const static QHash<PacketTypeEnum::Value, PacketTypeEnum::Value> REPLICATED_PACKET_MAPPING {
{ PacketTypeEnum::Value::MicrophoneAudioNoEcho, PacketTypeEnum::Value::ReplicatedMicrophoneAudioNoEcho },
{ PacketTypeEnum::Value::MicrophoneAudioWithEcho, PacketTypeEnum::Value::ReplicatedMicrophoneAudioWithEcho },
{ PacketTypeEnum::Value::InjectAudio, PacketTypeEnum::Value::ReplicatedInjectAudio },
{ PacketTypeEnum::Value::SilentAudioFrame, PacketTypeEnum::Value::ReplicatedSilentAudioFrame },
{ PacketTypeEnum::Value::AvatarIdentity, PacketTypeEnum::Value::ReplicatedAvatarIdentity },
{ PacketTypeEnum::Value::KillAvatar, PacketTypeEnum::Value::ReplicatedKillAvatar },
{ PacketTypeEnum::Value::BulkAvatarData, PacketTypeEnum::Value::ReplicatedBulkAvatarData }
};
return REPLICATED_PACKET_MAPPING;
}
const static QSet<PacketTypeEnum::Value> GetProxiedPackets() {
const static QSet<PacketTypeEnum::Value> PROXIED_PACKETS = QSet<PacketTypeEnum::Value>()
<< PacketTypeEnum::Value::ProxiedICEPing
<< PacketTypeEnum::Value::ProxiedICEPingReply
<< PacketTypeEnum::Value::ProxiedDomainListRequest;
return PROXIED_PACKETS;
}
const static QSet<PacketTypeEnum::Value> GetNonVerifiedPackets() {
const static QSet<PacketTypeEnum::Value> NON_VERIFIED_PACKETS = QSet<PacketTypeEnum::Value>()
<< PacketTypeEnum::Value::NodeJsonStats
<< PacketTypeEnum::Value::EntityQuery
<< PacketTypeEnum::Value::OctreeDataNack
<< PacketTypeEnum::Value::EntityEditNack
<< PacketTypeEnum::Value::DomainListRequest
<< PacketTypeEnum::Value::StopNode
<< PacketTypeEnum::Value::DomainDisconnectRequest
<< PacketTypeEnum::Value::UsernameFromIDRequest
<< PacketTypeEnum::Value::NodeKickRequest
<< PacketTypeEnum::Value::NodeMuteRequest;
return NON_VERIFIED_PACKETS;
}
const static QSet<PacketTypeEnum::Value> GetNonSourcedPackets() {
const static QSet<PacketTypeEnum::Value> NON_SOURCED_PACKETS = QSet<PacketTypeEnum::Value>()
<< PacketTypeEnum::Value::StunResponse << PacketTypeEnum::Value::CreateAssignment
<< PacketTypeEnum::Value::RequestAssignment << PacketTypeEnum::Value::DomainServerRequireDTLS
<< PacketTypeEnum::Value::DomainConnectRequest << PacketTypeEnum::Value::DomainList
<< PacketTypeEnum::Value::DomainConnectionDenied << PacketTypeEnum::Value::DomainServerPathQuery
<< PacketTypeEnum::Value::DomainServerPathResponse << PacketTypeEnum::Value::DomainServerAddedNode
<< PacketTypeEnum::Value::DomainServerConnectionToken << PacketTypeEnum::Value::DomainSettingsRequest
<< PacketTypeEnum::Value::OctreeDataFileRequest << PacketTypeEnum::Value::OctreeDataFileReply
<< PacketTypeEnum::Value::OctreeDataPersist << PacketTypeEnum::Value::DomainContentReplacementFromUrl
<< PacketTypeEnum::Value::DomainSettings << PacketTypeEnum::Value::ICEServerPeerInformation
<< PacketTypeEnum::Value::ICEServerQuery << PacketTypeEnum::Value::ICEServerHeartbeat
<< PacketTypeEnum::Value::ICEServerHeartbeatACK << PacketTypeEnum::Value::ICEPing
<< PacketTypeEnum::Value::ICEPingReply << PacketTypeEnum::Value::ICEServerHeartbeatDenied
<< PacketTypeEnum::Value::AssignmentClientStatus << PacketTypeEnum::Value::StopNode
<< PacketTypeEnum::Value::DomainServerRemovedNode << PacketTypeEnum::Value::UsernameFromIDReply
<< PacketTypeEnum::Value::OctreeFileReplacement << PacketTypeEnum::Value::ReplicatedMicrophoneAudioNoEcho
<< PacketTypeEnum::Value::ReplicatedMicrophoneAudioWithEcho << PacketTypeEnum::Value::ReplicatedInjectAudio
<< PacketTypeEnum::Value::ReplicatedSilentAudioFrame << PacketTypeEnum::Value::ReplicatedAvatarIdentity
<< PacketTypeEnum::Value::ReplicatedKillAvatar << PacketTypeEnum::Value::ReplicatedBulkAvatarData;
return NON_SOURCED_PACKETS;
}
const static QSet<PacketTypeEnum::Value> GetDomainSourcedPackets() {
const static QSet<PacketTypeEnum::Value> DOMAIN_SOURCED_PACKETS = QSet<PacketTypeEnum::Value>()
<< PacketTypeEnum::Value::AssetMappingOperation
<< PacketTypeEnum::Value::AssetGet
<< PacketTypeEnum::Value::AssetUpload;
return DOMAIN_SOURCED_PACKETS;
}
const static QSet<PacketTypeEnum::Value> GetDomainIgnoredVerificationPackets() {
const static QSet<PacketTypeEnum::Value> DOMAIN_IGNORED_VERIFICATION_PACKETS = QSet<PacketTypeEnum::Value>()
<< PacketTypeEnum::Value::AssetMappingOperationReply
<< PacketTypeEnum::Value::AssetGetReply
<< PacketTypeEnum::Value::AssetUploadReply;
return DOMAIN_IGNORED_VERIFICATION_PACKETS;
}
};
using PacketType = PacketTypeEnum::Value;
typedef char PacketVersion;
PacketVersion VersionForPacketType(PacketType packetType);
uint qHash(const PacketType& key, uint seed);
// Due to the different legacy behaviour, we need special processing for domains that were created before
// the zone inheritance modes were added. These have version numbers up to 80
enum class EntityVersion : PacketVersion {
StrokeColorProperty = 0,
HasDynamicOwnershipTests,
HazeEffect,
StaticCertJsonVersionOne,
OwnershipChallengeFix,
ZoneLightInheritModes = 82,
ZoneStageRemoved,
SoftEntities,
MaterialEntities,
ShadowControl,
MaterialData,
CloneableData,
CollisionMask16Bytes,
YieldSimulationOwnership,
ParticleEntityFix,
ParticleSpin,
BloomEffect,
GrabProperties,
ScriptGlmVectors,
FixedLightSerialization
};
enum class EntityScriptCallMethodVersion : PacketVersion {
ServerCallable = 18,
ClientCallable = 19
};
enum class EntityQueryPacketVersion: PacketVersion {
JSONFilter = 18,
JSONFilterWithFamilyTree = 19,
ConnectionIdentifier = 20,
RemovedJurisdictions = 21,
MultiFrustumQuery = 22,
ConicalFrustums = 23
};
enum class AssetServerPacketVersion: PacketVersion {
VegasCongestionControl = 19,
RangeRequestSupport,
RedirectedMappings,
BakingTextureMeta
};
enum class AvatarMixerPacketVersion : PacketVersion {
TranslationSupport = 17,
SoftAttachmentSupport,
AvatarEntities,
AbsoluteSixByteRotations,
SensorToWorldMat,
HandControllerJoints,
HasKillAvatarReason,
SessionDisplayName,
Unignore,
ImmediateSessionDisplayNameUpdates,
VariableAvatarData,
AvatarAsChildFixes,
StickAndBallDefaultAvatar,
IdentityPacketsIncludeUpdateTime,
AvatarIdentitySequenceId,
MannequinDefaultAvatar,
AvatarIdentitySequenceFront,
IsReplicatedInAvatarIdentity,
AvatarIdentityLookAtSnapping,
UpdatedMannequinDefaultAvatar,
AvatarJointDefaultPoseFlags,
FBXReaderNodeReparenting,
FixMannequinDefaultAvatarFeet,
ProceduralFaceMovementFlagsAndBlendshapes,
FarGrabJoints,
MigrateSkeletonURLToTraits,
MigrateAvatarEntitiesToTraits,
FarGrabJointsRedux,
JointTransScaled
};
enum class DomainConnectRequestVersion : PacketVersion {
NoHostname = 17,
HasHostname,
HasProtocolVersions,
HasMACAddress,
HasMachineFingerprint,
AlwaysHasMachineFingerprint
};
enum class DomainConnectionDeniedVersion : PacketVersion {
ReasonMessageOnly = 17,
IncludesReasonCode,
IncludesExtraInfo
};
enum class DomainServerAddedNodeVersion : PacketVersion {
PrePermissionsGrid = 17,
PermissionsGrid
};
enum class DomainListVersion : PacketVersion {
PrePermissionsGrid = 18,
PermissionsGrid,
GetUsernameFromUUIDSupport,
GetMachineFingerprintFromUUIDSupport,
AuthenticationOptional
};
enum class AudioVersion : PacketVersion {
HasCompressedAudio = 17,
CodecNameInAudioPackets,
Exactly10msAudioPackets,
TerminatingStreamStats,
SpaceBubbleChanges,
HasPersonalMute,
HighDynamicRangeVolume,
};
enum class MessageDataVersion : PacketVersion {
TextOrBinaryData = 18
};
enum class IcePingVersion : PacketVersion {
SendICEPeerID = 18
};
enum class PingVersion : PacketVersion {
IncludeConnectionID = 18
};
enum class AvatarQueryVersion : PacketVersion {
SendMultipleFrustums = 21,
ConicalFrustums = 22
};
using ControlBitAndType = uint32_t;
enum ControlType : uint16_t {
ACK,
Handshake,
HandshakeACK,
HandshakeRequest
};
const int UDP_IPV4_HEADER_SIZE = 28;
const int MAX_PACKET_SIZE_WITH_UDP_HEADER = 1492;
const int MAX_PACKET_SIZE = MAX_PACKET_SIZE_WITH_UDP_HEADER - UDP_IPV4_HEADER_SIZE;
const int CONTROL_BIT_SIZE = 1;
const int RELIABILITY_BIT_SIZE = 1;
const int MESSAGE_BIT_SIZE = 1;
const int OBFUSCATION_LEVEL_SIZE = 2;
const int SEQUENCE_NUMBER_SIZE= 27;
const int PACKET_POSITION_SIZE = 2;
const int MESSAGE_NUMBER_SIZE = 30;
const int MESSAGE_PART_NUMBER_SIZE = 32;
const int SEQUENCE_NUMBER_OFFSET = 0;
const int OBFUSCATION_LEVEL_OFFSET = SEQUENCE_NUMBER_OFFSET + SEQUENCE_NUMBER_SIZE;
const int MESSAGE_BIT_OFFSET = OBFUSCATION_LEVEL_OFFSET + OBFUSCATION_LEVEL_SIZE;
const int RELIABILITY_BIT_OFFSET = MESSAGE_BIT_OFFSET + MESSAGE_BIT_SIZE;
const int CONTROL_BIT_OFFSET = RELIABILITY_BIT_OFFSET + RELIABILITY_BIT_SIZE;
const int MESSAGE_NUMBER_OFFSET = 0;
const int PACKET_POSITION_OFFSET = MESSAGE_NUMBER_OFFSET + MESSAGE_NUMBER_SIZE;
const int MESSAGE_PART_NUMBER_OFFSET = 0;
const uint32_t CONTROL_BIT_MASK = uint32_t(1) << CONTROL_BIT_OFFSET;
const uint32_t RELIABILITY_BIT_MASK = uint32_t(1) << RELIABILITY_BIT_OFFSET;
const uint32_t MESSAGE_BIT_MASK = uint32_t(1) << MESSAGE_BIT_OFFSET;
const uint32_t OBFUSCATION_LEVEL_MASK = uint32_t(3) << OBFUSCATION_LEVEL_OFFSET;
const uint32_t BIT_FIELD_MASK = CONTROL_BIT_MASK | RELIABILITY_BIT_MASK | MESSAGE_BIT_MASK | OBFUSCATION_LEVEL_MASK;
const uint32_t SEQUENCE_NUMBER_MASK = ~BIT_FIELD_MASK;
const uint32_t PACKET_POSITION_MASK = uint32_t(3) << PACKET_POSITION_OFFSET;
const uint32_t MESSAGE_NUMBER_MASK = ~PACKET_POSITION_MASK;
static const uint32_t MESSAGE_PART_NUMBER_MASK = ~uint32_t(0);
class Packet : public QIODevice
{
public:
// Use same size as SequenceNumberAndBitField so we can use the enum with bitwise operations
enum ObfuscationLevel : uint32_t {
NoObfuscation = 0x0, // 00
ObfuscationL1 = 0x1, // 01
ObfuscationL2 = 0x2, // 10
ObfuscationL3 = 0x3, // 11
};
Packet(uint32_t sequence, PacketType t, qint64 size = MAX_PACKET_SIZE, bool reliable = false, bool part_of_message = false);
Packet(uint32_t sequence, ControlType t, qint64 size = MAX_PACKET_SIZE);
Packet(char * data, qint64 size);
static int HeaderSize(bool is_part_of_message);
static int LocalHeaderSize(PacketType type);
static int LocalControlHeaderSize();
int TotalHeaderSize();
static std::unique_ptr<Packet> Create(uint32_t sequence, PacketType t, qint64 size = -1);
static std::unique_ptr<Packet> FromReceivedPacket(char * data, qint64 size);
static std::unique_ptr<Packet> CreateControl(uint32_t sequence, ControlType t, qint64 size = -1);
static std::unique_ptr<Packet> FromReceivedControlPacket(char * data, qint64 size);
void Obfuscate(ObfuscationLevel level);
void WriteControlType();
void ReadControlType();
void WriteSourceID(quint16 s);
void WriteVerificationHash(HMACAuth * h);
void AdjustPayloadStartAndCapacity(int header_size, bool should_decrease_payload_size = false);
qint64 BytesLeftToRead() const { return payload_size - pos();}
qint64 BytesAvailableForWrite() const { return payload_capacity - pos();}
bool reset();
qint64 writeData(const char* data, qint64 max_size);
qint64 readData(char* dest, qint64 max_size);
qint64 WriteString(const QString& string);
QString ReadString();
char* GetData() { return packet.get(); }
const char* GetData() const { return packet.get(); }
qint64 GetDataSize() const { return (payload_start - packet.get()) + payload_size; }
PacketType GetType() {return type;}
ControlType GetControlType() {return control_type;}
bool GetIsReliable() const {return is_reliable;}
bool GetIsPartOfMessage() const {return is_part_of_message;}
uint32_t GetSequenceNumber() {return sequence_number;}
static QByteArray HashForPacketAndHMAC(const Packet& packet, HMACAuth * hash);
private:
PacketType type;
ControlType control_type;
PacketVersion version;
qint64 packet_size;
qint64 payload_size;
char * payload_start;
qint64 payload_capacity;
uint32_t obfuscation_level;
bool is_part_of_message;
bool is_reliable;
uint32_t message_number;
short packet_position;
uint32_t * message_part_number;
uint32_t sequence_number;
quint16 source_id;
std::unique_ptr<char[]> packet;
};
#endif // PACKET_H