-
Notifications
You must be signed in to change notification settings - Fork 12
/
hificonnection.cpp
1205 lines (978 loc) · 51.5 KB
/
hificonnection.cpp
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <random>
#include "hificonnection.h"
HifiConnection::HifiConnection(QWebSocket * s)
{
username = "";
password = "";
keypair_generator = new RSAKeypairGenerator();
waiting_for_keypair = false;
token = "";
domain_name = "";
domain_place_name = "";
domain_id = QUuid();
finished_domain_id_request = false;
stun_server_hostname = "stun.highfidelity.io";
stun_server_address = QHostAddress();
stun_server_port = 3478;
ice_server_hostname = "ice.highfidelity.com"; //"dev-ice.highfidelity.com";
qDebug() << "HifiConnection::HifiConnection() - Synchronously looking up IP address for hostname" << stun_server_hostname;
QHostInfo result_stun = QHostInfo::fromName(stun_server_hostname);
HandleLookupResult(result_stun, "stun");
//qDebug() << "HifiConnection::HifiConnection() - STUN server IP address: " << stun_server_hostname;
qDebug() << "HifiConnection::HifiConnection() - Synchronously looking up IP address for hostname" << ice_server_hostname;
QHostInfo result_ice = QHostInfo::fromName(ice_server_hostname);
HandleLookupResult(result_ice, "ice");
//qDebug() << "HifiConnection::HifiConnection() - ICE server IP address: " << ice_server_hostname;
ice_server_address = Utils::GetDefaultIceServerAddress();
ice_server_port = Utils::GetDefaultIceServerPort();
has_tcp_checked_local_socket = false;
UpdateLocalSocket();
connect(this, SIGNAL(WebRTCConnectionReady()), this, SLOT(StartStun()));
connect(this, SIGNAL(StunFinished()), this, SLOT(StartIce()));
connect(this, SIGNAL(IceFinished()), this, SLOT(StartDomainConnect()));
ice_client_id = QUuid::createUuid();
started_domain_connect = false;
owner_type = NodeType::Agent;
node_types_of_interest = NodeSet() << NodeType::AudioMixer << NodeType::AvatarMixer << NodeType::EntityServer << NodeType::AssetServer << NodeType::MessagesMixer << NodeType::EntityScriptServer;
domain_connected = false;
asset_server = nullptr;
audio_mixer = nullptr;
avatar_mixer = nullptr;
messages_mixer = nullptr;
entity_server = nullptr;
entity_script_server = nullptr;
data_channel = nullptr;
stun_response_timer = new QTimer { this };
connect(stun_response_timer, &QTimer::timeout, this, &HifiConnection::SendStunRequest);
stun_response_timer->setInterval(HIFI_INITIAL_UPDATE_INTERVAL_MSEC); // 250ms, Qt::CoarseTimer acceptable
ice_response_timer = new QTimer { this };
connect(ice_response_timer, &QTimer::timeout, this, &HifiConnection::SendIceRequest);
ice_response_timer->setInterval(HIFI_INITIAL_UPDATE_INTERVAL_MSEC); // 250ms, Qt::CoarseTimer acceptable
hifi_response_timer = new QTimer { this };
connect(hifi_response_timer, &QTimer::timeout, this, &HifiConnection::SendDomainCheckIn);
hifi_response_timer->setInterval(HIFI_INITIAL_UPDATE_INTERVAL_MSEC); // 250ms, Qt::CoarseTimer acceptable
qDebug() << "HifiConnection::Connect() - New client" << s << ice_client_id << s->peerAddress() << s->peerPort();
client_socket = s;
connect(client_socket, &QWebSocket::textMessageReceived, this, &HifiConnection::ClientMessageReceived);
started_hifi_connect = false;
hifi_socket = new QUdpSocket(this);
connect(hifi_socket, SIGNAL(readyRead()), this, SLOT(ParseHifiResponse()));
QJsonObject connected_object;
connected_object.insert("type", QJsonValue::fromVariant("connected"));
connected_object.insert("id", QJsonValue::fromVariant(ice_client_id.toString()));
QJsonDocument connectedDoc(connected_object);
client_socket->sendTextMessage(QString::fromStdString(connectedDoc.toJson(QJsonDocument::Compact).toStdString()));
client_timestamp = Utils::GetTimestamp();
server_timestamp = client_timestamp;
timeout_timer = new QTimer { this };
connect(timeout_timer, &QTimer::timeout, this, &HifiConnection::Timeout);
timeout_timer->setInterval(HIFI_TIMEOUT_MSEC/4);
timeout_timer->start();
}
HifiConnection::~HifiConnection()
{
}
void HifiConnection::HandleLookupResult(const QHostInfo& hostInfo, QString addr_type)
{
if (hostInfo.error() != QHostInfo::NoError) {
qDebug() << "Task::handleLookupResult() - Lookup failed for" << hostInfo.lookupId() << ":" << hostInfo.errorString();
} else {
for (int i = 0; i < hostInfo.addresses().size(); i++) {
// just take the first IPv4 address
QHostAddress address = hostInfo.addresses()[i];
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
if (addr_type == "stun") stun_server_address = address;
else if (addr_type == "ice") ice_server_address = address;
qDebug() << "Task::handleLookupResult() - QHostInfo lookup result for"
<< hostInfo.hostName() << "with lookup ID" << hostInfo.lookupId() << "is" << address.toString();
break;
}
}
}
}
void HifiConnection::UpdateLocalSocket()
{
// attempt to use Google's DNS to confirm that local IP
const QHostAddress RELIABLE_LOCAL_IP_CHECK_HOST = QHostAddress { "8.8.8.8" };
const int RELIABLE_LOCAL_IP_CHECK_PORT = 53;
QTcpSocket* localIPTestSocket = new QTcpSocket;
connect(localIPTestSocket, &QTcpSocket::connected, this, &HifiConnection::ConnectedForLocalSocketTest);
connect(localIPTestSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(ErrorTestingLocalSocket()));
// attempt to connect to our reliable host
localIPTestSocket->connectToHost(RELIABLE_LOCAL_IP_CHECK_HOST, RELIABLE_LOCAL_IP_CHECK_PORT);
}
void HifiConnection::ConnectedForLocalSocketTest()
{
auto local_ip_test_socket = qobject_cast<QTcpSocket*>(sender());
if (local_ip_test_socket) {
auto local_host_address = local_ip_test_socket->localAddress();
if (local_host_address.protocol() == QAbstractSocket::IPv4Protocol) {
local_address = local_host_address;
//qDebug() << "HifiConnection::connectedForLocalSocketTest() - Local address: " << local_address;
has_tcp_checked_local_socket = true;
}
local_ip_test_socket->deleteLater();
}
}
void HifiConnection::ErrorTestingLocalSocket()
{
auto local_ip_test_socket = qobject_cast<QTcpSocket*>(sender());
if (local_ip_test_socket) {
// error connecting to the test socket - if we've never set our local socket using this test socket
// then use our possibly updated guessed local address as fallback
if (!has_tcp_checked_local_socket) {
local_address = GetGuessedLocalAddress();
//qDebug() << "HifiConnection::errorTestingLocalSocket() - Local address: " << local_address;
has_tcp_checked_local_socket = true;
}
local_ip_test_socket->deleteLater();
}
}
QHostAddress HifiConnection::GetGuessedLocalAddress()
{
QHostAddress address;
for (int i= 0; i < QNetworkInterface::allInterfaces().size(); i++) {
QNetworkInterface network_interface = QNetworkInterface::allInterfaces()[i];
if (network_interface.flags() & QNetworkInterface::IsUp
&& network_interface.flags() & QNetworkInterface::IsRunning
&& network_interface.flags() & ~QNetworkInterface::IsLoopBack) {
// we've decided that this is the active NIC
// enumerate it's addresses to grab the IPv4 address
for (int j = 0; network_interface.addressEntries().size(); j++) {
// make sure it's an IPv4 address that isn't the loopback
QNetworkAddressEntry entry = network_interface.addressEntries()[j];
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol && !entry.ip().isLoopback()) {
// set our localAddress and break out
address = entry.ip();
//qDebug() << "HifiConnection::getGuessedLocalAddress() - " << address;
break;
}
}
}
if (!address.isNull()) {
break;
}
}
has_tcp_checked_local_socket = true;
// return the looked up local address
return address;
}
void HifiConnection::Stop()
{
if (asset_server) {
delete asset_server;
asset_server = nullptr;
}
if (audio_mixer) {
delete audio_mixer;
audio_mixer = nullptr;
}
if (messages_mixer) {
delete messages_mixer;
messages_mixer = nullptr;
}
if (avatar_mixer) {
delete avatar_mixer;
avatar_mixer = nullptr;
}
if (entity_script_server) {
delete entity_script_server;
entity_script_server = nullptr;
}
if (entity_server) {
delete entity_server;
entity_server = nullptr;
}
if (data_channel) {
ClearDataChannel();
}
if (timeout_timer) {
delete timeout_timer;
timeout_timer = nullptr;
}
if (ice_response_timer) {
delete ice_response_timer;
ice_response_timer = nullptr;
}
if (stun_response_timer) {
delete stun_response_timer;
stun_response_timer = nullptr;
}
if (hifi_response_timer) {
delete hifi_response_timer;
hifi_response_timer = nullptr;
}
if (client_socket) {
delete client_socket;
client_socket = nullptr;
}
if (hifi_socket) {
delete hifi_socket;
hifi_socket = nullptr;
}
}
void HifiConnection::DomainRequestFinished()
{
QNetworkReply *domain_reply = qobject_cast<QNetworkReply *>(sender());
if (domain_reply) {
QByteArray domain_reply_contents;
if (domain_reply->error() == QNetworkReply::NoError && domain_reply->isOpen()) {
domain_reply_contents += domain_reply->readAll();
//qDebug() << domain_reply_contents;
QJsonDocument doc;
doc = QJsonDocument::fromJson(domain_reply_contents);
QJsonObject obj = doc.object();
QJsonObject data = obj["data"].toObject();
QJsonObject place = data["place"].toObject();
QJsonObject domain = place["domain"].toObject();
domain_id = QUuid(domain["id"].toString());
domain_place_name = domain["default_place_name"].toString();
if (domain.contains("ice_server_address")) {
ice_server_address = QHostAddress(domain["ice_server_address"].toString());
}
}
domain_reply->close();
domain_reply->deleteLater();
}
qDebug() << "HifiConnection::domainRequestFinished() - Domain name" << domain_name;
qDebug() << "HifiConnection::domainRequestFinished() - Domain place name" << domain_place_name;
qDebug() << "HifiConnection::domainRequestFinished() - Domain ID" << domain_id;
finished_domain_id_request = true;
if (data_channel && finished_domain_id_request && !started_hifi_connect) {
started_hifi_connect = true;
Q_EMIT WebRTCConnectionReady();
}
}
void HifiConnection::RequestAccessTokenFinished() {
QNetworkReply* reply = reinterpret_cast<QNetworkReply*>(sender());
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
const QJsonObject& root_object = response.object();
if (!root_object.contains("error")) {
// construct an OAuthAccessToken from the json object
if (!root_object.contains("access_token") || !root_object.contains("expires_in")
|| !root_object.contains("token_type")) {
qDebug() << "Received a response for password grant that is missing one or more expected values.";
} else {
// clear the path from the response URL so we have the right root URL for this access token
QUrl url = reply->url();
url.setPath("");
qDebug() << "Storing an account with access-token for" << url.toString();
token = root_object["access_token"].toString();
refreshToken = root_object["refresh_token"].toString();
expiryTimestamp = QDateTime::currentMSecsSinceEpoch() + (root_object["expires_in"].toDouble() * 1000);
tokenType = root_object["token_type"].toString();
}
} else {
qDebug() << "Error in response for password grant -" << root_object["error_description"].toString();
}
}
void HifiConnection::RequestAccessTokenError(QNetworkReply::NetworkError error) {
qDebug() << "HifiConnection: failed to fetch access token - " << error;
}
void HifiConnection::StartStun()
{
// Register Domain Server DC callbacks here
std::function<void(std::string)> onErrorCallback = [this](std::string message) {
qDebug() << "HifiConnection::onError() - Data channel error" << QString::fromStdString(message);
};
data_channel->SetOnErrorCallback(onErrorCallback);
std::function<void(rtcdcpp::ChunkPtr)> onBinaryMessageCallback = [this](rtcdcpp::ChunkPtr message) {
this->client_timestamp = Utils::GetTimestamp();
NodeType_t server = (NodeType_t) message->Data()[0];
QByteArray packet = QByteArray((char *) (message->Data() + sizeof(NodeType_t)), message->Length() - 1);
//qDebug() << "HifiConnection::onMessage() - " << (char) server << packet << packet.size();
if (server == NodeType::DomainServer) {
//qDebug() << "domain";
bool is_control_packet = *reinterpret_cast<uint32_t*>(packet.data()) & CONTROL_BIT_MASK;
if (is_control_packet) {
this->SendServerMessage(packet, domain_public_address, domain_public_port);
}
else {
std::unique_ptr<Packet> response_packet = Packet::FromReceivedPacket(packet.data(), (qint64) packet.size());// check if this was a control packet or a data packet
if (response_packet->GetType() == PacketType::ProxiedICEPing) {
uint8_t ping_type = 2; //Default to public
response_packet->read(reinterpret_cast<char*>(&ping_type), sizeof(uint8_t));
//qDebug() << "proxiediceping" << ping_type;
SendIcePing(response_packet->GetSequenceNumber(), ping_type);
}
else if (response_packet->GetType() == PacketType::ProxiedICEPingReply) {
uint8_t ping_type = 2; //Default to public
response_packet->read(reinterpret_cast<char*>(&ping_type), sizeof(uint8_t));
//qDebug() << "proxiedicepingreply" << ping_type;
SendIcePingReply(response_packet->GetSequenceNumber(), ping_type);
}
else if (response_packet->GetType() == PacketType::ProxiedDomainListRequest) {
//qDebug() << "proxieddomainlistrequest";
SendDomainCheckInRequest(response_packet->GetSequenceNumber());
}
else {
this->SendServerMessage(packet, domain_public_address, domain_public_port);
}
}
}
else if (server == NodeType::AssetServer) {
//qDebug() << "asset";
if (this->asset_server) SendServerMessage(packet, asset_server->GetPublicAddress(), asset_server->GetPublicPort());
}
else if (server == NodeType::AudioMixer) {
//qDebug() << "audio";
if (this->audio_mixer) SendServerMessage(packet, audio_mixer->GetPublicAddress(), audio_mixer->GetPublicPort());
}
else if (server == NodeType::AvatarMixer) {
//qDebug() << "avatar";
if (this->avatar_mixer) SendServerMessage(packet, avatar_mixer->GetPublicAddress(), avatar_mixer->GetPublicPort());
}
else if (server == NodeType::MessagesMixer) {
//qDebug() << "messages";
if (this->messages_mixer) SendServerMessage(packet, messages_mixer->GetPublicAddress(), messages_mixer->GetPublicPort());
}
else if (server == NodeType::EntityServer) {
//qDebug() << "entity";
if (this->entity_server) SendServerMessage(packet, entity_server->GetPublicAddress(), entity_server->GetPublicPort());
}
else if (server == NodeType::EntityScriptServer) {
//qDebug() << "entityscript";
if (this->entity_script_server) SendServerMessage(packet, entity_script_server->GetPublicAddress(), entity_script_server->GetPublicPort());
}
};
data_channel->SetOnBinaryMsgCallback(onBinaryMessageCallback);
std::function<void()> onClosed = [this]() {
qDebug() << "HifiConnection::onClosed() - Domain Server data channel closed";
ClearDataChannel();
Q_EMIT Disconnected();
};
data_channel->SetOnClosedCallback(onClosed);
num_requests = 0;
has_completed_current_request = false;
stun_response_timer->start();
}
void HifiConnection::Timeout()
{
quint64 timestamp = Utils::GetTimestamp();
//qDebug() << "Timeout" << timestamp / 1000 << client_timestamp / 1000 << server_timestamp / 1000;
if (timestamp > client_timestamp && (timestamp - client_timestamp) / 1000 > HIFI_TIMEOUT_MSEC) { // Convert to millisecond
qDebug() << "HifiConnection::Timeout() - Client connection timed out. Disconnecting...";
if (timeout_timer) {
delete timeout_timer;
timeout_timer = nullptr;
}
Q_EMIT Disconnected();
return;
}
else if (timestamp > server_timestamp && (timestamp - server_timestamp) / 1000000 > HIFI_TIMEOUT_MSEC) { // Convert to millisecond
qDebug() << "HifiConnection::Timeout() - Server connection timed out. Disconnecting...";
if (timeout_timer) {
delete timeout_timer;
timeout_timer = nullptr;
}
Q_EMIT Disconnected();
return;
}
}
void HifiConnection::StartIce()
{
num_requests = 0;
has_completed_current_request = false;
ice_response_timer->start();
}
void HifiConnection::StartDomainConnect()
{
connect(hifi_socket, SIGNAL(disconnected()), this, SLOT(ServerDisconnected()));
num_requests = 0;
has_completed_current_request = false;
hifi_response_timer->start();
}
void HifiConnection::ParseHifiResponse()
{
while (hifi_socket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(hifi_socket->pendingDatagramSize());
QHostAddress sender;
quint16 sender_port;
hifi_socket->readDatagram(datagram.data(), datagram.size(), &sender, &sender_port);
server_timestamp = Utils::GetTimestamp();
//Stun Server response;
if (sender.toIPv4Address() == stun_server_address.toIPv4Address() && sender_port == stun_server_port) {
//qDebug() << "HifiConnection::ParseHifiResponse() - read packet from " << sender << ":" << sender_port << " of size " << datagram.size() << " bytes";
// check the cookie to make sure this is actually a STUN response
// and read the first attribute and make sure it is a XOR_MAPPED_ADDRESS
const int NUM_BYTES_MESSAGE_TYPE_AND_LENGTH = 4;
const uint16_t XOR_MAPPED_ADDRESS_TYPE = htons(0x0020);
const uint32_t RFC_5389_MAGIC_COOKIE_NETWORK_ORDER = htonl(RFC_5389_MAGIC_COOKIE);
int attribute_start_index = NUM_BYTES_STUN_HEADER;
if (memcmp(datagram.data() + NUM_BYTES_MESSAGE_TYPE_AND_LENGTH,
&RFC_5389_MAGIC_COOKIE_NETWORK_ORDER,
sizeof(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER)) != 0) {
qDebug() << "HifiConnection::ParseHifiResponse() - STUN response cannot be parsed, magic cookie is invalid";
Q_EMIT Disconnected();
return;
}
// enumerate the attributes to find XOR_MAPPED_ADDRESS_TYPE
while (attribute_start_index < datagram.size()) {
if (memcmp(datagram.data() + attribute_start_index, &XOR_MAPPED_ADDRESS_TYPE, sizeof(XOR_MAPPED_ADDRESS_TYPE)) == 0) {
const int NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH = 4;
const int NUM_BYTES_FAMILY_ALIGN = 1;
const uint8_t IPV4_FAMILY_NETWORK_ORDER = htons(0x01) >> 8;
int byte_index = attribute_start_index + NUM_BYTES_STUN_ATTR_TYPE_AND_LENGTH + NUM_BYTES_FAMILY_ALIGN;
uint8_t address_family = 0;
memcpy(&address_family, datagram.data() + byte_index, sizeof(address_family));
byte_index += sizeof(address_family);
if (address_family == IPV4_FAMILY_NETWORK_ORDER) {
// grab the X-Port
uint16_t xor_mapped_port = 0;
memcpy(&xor_mapped_port, datagram.data() + byte_index, sizeof(xor_mapped_port));
public_port = ntohs(xor_mapped_port) ^ (ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER) >> 16);
byte_index += sizeof(xor_mapped_port);
// grab the X-Address
uint32_t xor_mapped_address = 0;
memcpy(&xor_mapped_address, datagram.data() + byte_index, sizeof(xor_mapped_address));
uint32_t stun_address = ntohl(xor_mapped_address) ^ ntohl(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER);
// QHostAddress newPublicAddress(stun_address);
public_address = QHostAddress(stun_address);
qDebug() << "HifiConnection::ParseHifiResponse() - Public address: " << public_address;
qDebug() << "HifiConnection::ParseHifiResponse() - Public port: " << public_port;
local_port = hifi_socket->localPort();
qDebug() << "HifiConnection::ParseHifiResponse() - Local address: " << local_address;
qDebug() << "HifiConnection::ParseHifiResponse() - Local port: " << local_port;
has_completed_current_request = true;
stun_response_timer->stop();
SendClientMessageFromNode(NodeType::DomainServer, datagram);
Q_EMIT StunFinished();
break;
}
}
else {
// push forward attribute_start_index by the length of this attribute
const int NUM_BYTES_ATTRIBUTE_TYPE = 2;
uint16_t attribute_length = 0;
memcpy(&attribute_length, datagram.data() + attribute_start_index + NUM_BYTES_ATTRIBUTE_TYPE,
sizeof(attribute_length));
attribute_length = ntohs(attribute_length);
attribute_start_index += NUM_BYTES_MESSAGE_TYPE_AND_LENGTH + attribute_length;
}
}
continue;
}
bool is_control_packet = *reinterpret_cast<uint32_t*>(datagram.data()) & CONTROL_BIT_MASK;
if (!is_control_packet) {
ParseDatagram(datagram);
}
Node * node = GetNodeFromAddress(sender, sender_port);
if (node) {
SendClientMessageFromNode(node->GetNodeType(), datagram);
}
else {
SendClientMessageFromNode(NodeType::DomainServer, datagram);
}
}
}
void HifiConnection::ParseDatagram(QByteArray datagram)
{
std::unique_ptr<Packet> response_packet = Packet::FromReceivedPacket(datagram.data(), (qint64) datagram.size());// check if this was a control packet or a data packet
//qDebug() << "HifiConnection::ParseHifiResponse() - Packet type" << (int) response_packet->GetType();
//ICE response
if (response_packet->GetType() == PacketType::ICEServerPeerInformation)
{
QDataStream ice_response_stream(response_packet.get()->readAll());
QUuid domain_uuid;
ice_response_stream >> domain_uuid >> domain_public_address >> domain_public_port >> domain_local_address >> domain_local_port;
if (domain_uuid != domain_id){
qDebug() << "HifiConnection::ParseHifiResponse() - Error: Domain ID's do not match " << domain_uuid << domain_id;
ice_response_timer->stop();
Q_EMIT Disconnected();
}
qDebug() << "HifiConnection::ParseHifiResponse() - Domain ID: " << domain_uuid << "Domain Public Address: " << domain_public_address << "Domain Public Port: " << domain_public_port << "Domain Local Address: " << domain_local_address << "Domain Local Port: " << domain_local_port;
has_completed_current_request = true;
if (!started_domain_connect)
{
ice_response_timer->stop();
Q_EMIT IceFinished();
}
}
else if (response_packet->GetType() == PacketType::DomainServerConnectionToken) {
num_requests = 0; // Reset number of requests so we can keep sending DomainConnectRequests
QByteArray token(response_packet->readAll().constData(), NUM_BYTES_RFC4122_UUID);
domain_connection_token = QUuid::fromRfc4122(token);
qDebug() << "HifiConnection::ParseHifiResponse() - Domain connection token: " << domain_connection_token;
}
else if (response_packet->GetType() == PacketType::DomainList && !domain_connected) {
qDebug() << "HifiConnection::ParseHifiResponse() - Process domain list";
QDataStream packet_stream(response_packet->readAll());
// grab the domain's ID from the beginning of the packet
QUuid domain_uuid;
packet_stream >> domain_uuid;
if (domain_id != domain_uuid) {
// Recieved packet from different domain.
qDebug() << "HifiConnection::ParseHifiResponse() - Received packet from different domain";
return;
}
quint16 domain_local_id;
packet_stream >> domain_local_id;
// pull our owner (ie. session) UUID from the packet, it's always the first thing
// The short (16 bit) ID comes next.
packet_stream >> session_id;
packet_stream >> local_id;
// if this was the first domain-server list from this domain, we've now connected
hifi_response_timer->stop();
domain_connected = true;
// pull the permissions/right/privileges for this node out of the stream
uint new_permissions;
packet_stream >> new_permissions;
permissions = (Permissions) new_permissions;
// Is packet authentication enabled?
bool is_authenticated;
packet_stream >> is_authenticated; //TODO: handle authentication of packets
//qDebug() << permissions << is_authenticated;
//qDebug() << domain_uuid << domain_local_id << session_id << local_id << new_permissions << is_authenticated;
// pull each node in the packet
while (packet_stream.device()->pos() < response_packet->GetDataSize() - response_packet->TotalHeaderSize()) {
ParseNodeFromPacketStream(packet_stream);
}
}
else if (response_packet->GetType() == PacketType::DomainConnectionDenied) {
uint8_t reasonCode;
//uint8_t reasonSize;
response_packet->read((char *) &reasonCode, sizeof(uint8_t));
/*response_packet->read((char *) &reasonSize, sizeof(uint8_t));
QByteArray utfReason;
utfReason.resize(reasonSize);
response_packet->read(utfReason.data(), reasonSize);
QString reason = QString::fromUtf8(utfReason.constData(), reasonSize);*/
qDebug() << "HifiConnection::ParseHifiResponse() - DomainConnectionDenied - Code: " << reasonCode; //"Reason: "<< reason;
if (reasonCode == 2) {
username = "";
}
}
}
void HifiConnection::ParseNodeFromPacketStream(QDataStream& packet_stream)
{
// setup variables to read into from QDataStream
NodeType_t node_type;
QUuid node_uuid, connection_secret_uuid;
QHostAddress node_public_address, node_local_address;
quint16 node_public_port, node_local_port;
uint node_permissions;
bool is_replicated;
quint16 session_local_id;
packet_stream >> node_type >> node_uuid >> node_public_address >> node_public_port >> node_local_address >> node_local_port >> node_permissions
>> is_replicated >> session_local_id >> connection_secret_uuid;
// if the public socket address is 0 then it's reachable at the same IP
// as the domain server
if (node_public_address.isNull()) {
node_public_address = public_address;
}
//qDebug() << (char) node_type << node_uuid << node_public_address << node_public_port << node_local_address << node_local_port << node_permissions
// << is_replicated << session_local_id << connection_secret_uuid;
Node * node = new Node();
node->SetNodeID(node_uuid);
node->SetNodeType(node_type);
node->SetPublicAddress(node_public_address, node_public_port);
node->SetLocalAddress(node_local_address, node_local_port);
node->SetSessionLocalID(session_local_id);
node->SetDomainSessionLocalID(local_id);
node->SetIsReplicated(is_replicated);
node->SetConnectionSecret(connection_secret_uuid);
node->SetPermissions((Permissions) node_permissions);
switch (node_type) {
case NodeType::AssetServer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering asset server" << node_public_address << node_public_port;
asset_server = node;
break;
}
case NodeType::AudioMixer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering audio mixer" << node_public_address << node_public_port;
audio_mixer = node;
break;
}
case NodeType::AvatarMixer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering avatar mixer" << node_public_address << node_public_port;
avatar_mixer = node;
break;
}
case NodeType::MessagesMixer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering messages mixer" << node_public_address << node_public_port;
messages_mixer = node;
break;
}
case NodeType::EntityServer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering entity server" << node_public_address << node_public_port;
entity_server = node;
break;
}
case NodeType::EntityScriptServer : {
qDebug() << "HifiConnection::ParseNodeFromPacketStream() - Registering entity script server" << node_public_address << node_public_port;
entity_script_server = node;
break;
}
default: {
break;
}
}
}
void HifiConnection::SendStunRequest()
{
if (!finished_domain_id_request || !has_tcp_checked_local_socket) {
return;
}
if (num_requests == HIFI_NUM_INITIAL_REQUESTS_BEFORE_FAIL) {
qDebug() << "HifiConnection::SendStunRequest() - Stopping stun requests to" << stun_server_hostname << stun_server_port;
stun_response_timer->stop();
Q_EMIT Disconnected();
//disconnect(stun_response_timer, &QTimer::timeout, this, &HifiConnection::SendStunRequest);
//stun_response_timer->deleteLater();
return;
}
if (!has_completed_current_request) {
qDebug() << "HifiConnection::SendStunRequest() - Sending initial stun request to" << stun_server_hostname << stun_server_port;
++num_requests;
}
else {
//qDebug() << "HifiConnection::SendStunRequest() - Completed stun request";
return;
}
char * stun_request_packet = (char *) malloc(NUM_BYTES_STUN_HEADER);
int packet_index = 0;
const uint32_t RFC_5389_MAGIC_COOKIE_NETWORK_ORDER = htonl(RFC_5389_MAGIC_COOKIE);
// leading zeros + message type
const uint16_t REQUEST_MESSAGE_TYPE = htons(0x0001);
memcpy(stun_request_packet + packet_index, &REQUEST_MESSAGE_TYPE, sizeof(REQUEST_MESSAGE_TYPE));
packet_index += sizeof(REQUEST_MESSAGE_TYPE);
// message length (no additional attributes are included)
uint16_t message_length = 0;
memcpy(stun_request_packet + packet_index, &message_length, sizeof(message_length));
packet_index += sizeof(message_length);
memcpy(stun_request_packet + packet_index, &RFC_5389_MAGIC_COOKIE_NETWORK_ORDER, sizeof(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER));
packet_index += sizeof(RFC_5389_MAGIC_COOKIE_NETWORK_ORDER);
// transaction ID (random 12-byte unsigned integer)
const uint NUM_TRANSACTION_ID_BYTES = 12;
QUuid randomUUID = QUuid::createUuid();
memcpy(stun_request_packet + packet_index, randomUUID.toRfc4122().data(), NUM_TRANSACTION_ID_BYTES);
qDebug () << "HifiConnection::SendStunRequest() - STUN address:" << stun_server_address << "STUN port:" << stun_server_port;
SendServerMessage(stun_request_packet, NUM_BYTES_STUN_HEADER, stun_server_address, stun_server_port);
}
void HifiConnection::SendIceRequest()
{
if (num_requests == HIFI_NUM_INITIAL_REQUESTS_BEFORE_FAIL) {
qDebug() << "HifiConnection::SendIceRequest() - Stopping ice requests to" << ice_server_address << ice_server_port;
ice_response_timer->stop();
Q_EMIT Disconnected();
//disconnect(ice_response_timer, &QTimer::timeout, this, &HifiConnection::SendIceRequest);
//ice_response_timer->deleteLater();
return;
}
if (!has_completed_current_request) {
qDebug() << "HifiConnection::SendIceRequest() - Sending intial ice request to" << ice_server_address << ice_server_port;
++num_requests;
}
else {
//qDebug() << "HifiConnection::SendIceRequest() - Completed ice request";
//ice_response_timer->stop();
//disconnect(ice_response_timer, &QTimer::timeout, this, &HifiConnection::SendIceRequest);
//ice_response_timer->deleteLater();
return;
}
PacketType packetType = PacketType::ICEServerQuery;
//PacketVersion version = versionForPacketType(packetType);
std::unique_ptr<Packet> ice_request_packet = Packet::Create(0,packetType);
QDataStream ice_data_stream(ice_request_packet.get());
ice_data_stream << ice_client_id << public_address << public_port << local_address << local_port << domain_id;
//qDebug () << "HifiConnection::SendIceRequest() - ICE address:" << hifi_socket->peerAddress() << "ICE port:" << hifi_socket->peerPort();
//qDebug() << "ICE packet values" << sequence_number << (uint8_t)packetType << (int)versionForPacketType(packetType) << ice_client_id << public_address << public_port << local_address << local_port << domain_id;
SendServerMessage(ice_request_packet->GetData(), ice_request_packet->GetDataSize(), ice_server_address, ice_server_port);
}
void HifiConnection::SendDomainCheckIn()
{
if (!finished_domain_id_request) {
return;
}
if (num_requests == HIFI_NUM_INITIAL_REQUESTS_BEFORE_FAIL) {
qDebug() << "HifiConnection::SendDomainConnectRequest() - Stopping domain requests to" << domain_place_name;
hifi_response_timer->stop();
Q_EMIT Disconnected();
//hifi_response_timer->deleteLater();
return;
}
if (!domain_connected) {
qDebug() << "HifiConnection::SendDomainConnectRequest() - Sending initial domain connect request to" << domain_public_address << domain_public_port;
++num_requests;
}
else {
//qDebug() << "HifiConnection::SendDomainConnectRequest() - Completed domain connect request";
//hifi_response_timer->stop();
//hifi_response_timer->deleteLater();
return;
}
SendDomainCheckInRequest();
}
void HifiConnection::KeypairRequestFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (reply) {
// double check if the finished network reply had a session ID in the header and make
// sure that our session ID matches that value if so
/*if (reply->hasRawHeader(METAVERSE_SESSION_ID_HEADER)) {
session_id = networkReply->rawHeader(METAVERSE_SESSION_ID_HEADER);
}*/
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "Uploaded public key to Metaverse API. RSA keypair generation is completed.";
waiting_for_keypair = false;
//qDebug(networking) << "Received JSON response from metaverse API that has no matching callback.";
//qDebug(networking) << QJsonDocument::fromJson(networkReply->readAll());
} else {
qWarning() << "Public key upload failed from AccountManager" << reply->errorString();
// we aren't waiting for a response any longer
waiting_for_keypair = false;
username = ""; // Reset username so user can still log in
//qDebug(networking) << "Received error response from metaverse API that has no matching callback.";
//qDebug(networking) << "Error" << networkReply->error() << "-" << networkReply->errorString();
//qDebug(networking) << networkReply->readAll();
}
reply->close();
reply->deleteLater();
}
}
void HifiConnection::SendDomainCheckInRequest(uint32_t s)
{
if (!finished_domain_id_request) {
return;
}
bool requires_username_signature = !domain_connected && !domain_connection_token.isNull();
//Generate keypair
if (requires_username_signature && keypair_generator->GetPrivateKey().isEmpty()) {
qDebug() << "HifiConnection::SendDomainCheckInRequest() - Generating keypair for username signature";
num_requests = 0;
username_signature = QByteArray();
keypair_generator->GenerateKeypair();
if (!waiting_for_keypair) {
waiting_for_keypair = true;
// upload the public key so data-web has an up-to-date key
// setup a multipart upload to send up the public key
QHttpMultiPart* request_multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart public_key_part;
public_key_part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
public_key_part.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"public_key\"; filename=\"public_key\""));
public_key_part.setBody(keypair_generator->GetPublicKey());
request_multipart->append(public_key_part);
const QByteArray HIGH_FIDELITY_USER_AGENT = "Mozilla/5.0 (HighFidelityInterface)";
const auto METAVERSE_SESSION_ID_HEADER = QString("HFM-SessionID").toLocal8Bit();
const QByteArray ACCESS_TOKEN_AUTHORIZATION_HEADER = "Authorization";
QNetworkAccessManager * nam = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
request.setRawHeader(METAVERSE_SESSION_ID_HEADER, uuidStringWithoutCurlyBraces(session_id).toLocal8Bit());
if (token != "") {
request.setRawHeader(ACCESS_TOKEN_AUTHORIZATION_HEADER, QString("Bearer %1").arg(token).toUtf8());
}
request.setUrl(QUrl("https://metaverse.highfidelity.com/api/v1/user/public_key"));
QNetworkReply* reply = NULL;
reply = nam->put(request, request_multipart);
connect(reply, SIGNAL(finished()), this, SLOT(KeypairRequestFinished()));
}
return;
}
if (waiting_for_keypair) return;
PacketType packet_type = (domain_connected) ? PacketType::DomainListRequest : PacketType::DomainConnectRequest;
std::unique_ptr<Packet> domain_checkin_request_packet = Packet::Create(s,packet_type);
QDataStream domain_checkin_data_stream(domain_checkin_request_packet.get());
if (packet_type == PacketType::DomainConnectRequest) {
domain_checkin_data_stream << ice_client_id;
QByteArray protocol_version_sig = Utils::GetProtocolVersionSignature();
domain_checkin_data_stream.writeBytes(protocol_version_sig.constData(), protocol_version_sig.size());
domain_checkin_data_stream << Utils::GetHardwareAddress(local_address) << Utils::GetMachineFingerprint();
//qDebug() << ice_client_id << protocol_version_sig << Utils::GetHardwareAddress(hifi_socket->localAddress()) << Utils::GetMachineFingerprint() << (char)owner_type.load()
// << public_address << public_port << local_address << local_port << node_types_of_interest << domain_place_name;
}
//qDebug() << "list request" << (char)owner_type.load() << public_address << public_port << local_address << local_port << node_types_of_interest << domain_place_name;
domain_checkin_data_stream << owner_type.load() << public_address << public_port <<local_address << local_port
<< node_types_of_interest.toList() << domain_place_name;
if (!domain_connected && username != "") {
domain_checkin_data_stream << username;
// if this is a connect request, and we can present a username signature, send it along
if (!keypair_generator->GetPrivateKey().isEmpty()) {
QByteArray plaintext = username.toLower().toUtf8().append(domain_connection_token.toRfc4122());
//Sign plaintext
if (!keypair_generator->GetPrivateKey().isEmpty()) {
const char* private_key_data = keypair_generator->GetPrivateKey().constData();
RSA* rsa_private_key = d2i_RSAPrivateKey(NULL,
reinterpret_cast<const unsigned char**>(&private_key_data),
keypair_generator->GetPrivateKey().size());
if (rsa_private_key) {
QByteArray signature(RSA_size(rsa_private_key), 0);
unsigned int signature_bytes = 0;
QByteArray hashed_plaintext = QCryptographicHash::hash(plaintext, QCryptographicHash::Sha256);
int encrypt_return = RSA_sign(NID_sha256,
reinterpret_cast<const unsigned char*>(hashed_plaintext.constData()),
hashed_plaintext.size(),
reinterpret_cast<unsigned char*>(signature.data()),
&signature_bytes,