-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
ctrl.c
1399 lines (1260 loc) · 44.3 KB
/
ctrl.c
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
// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#include <chiaki/ctrl.h>
#include <chiaki/session.h>
#include <chiaki/base64.h>
#include <chiaki/http.h>
#include <chiaki/time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#endif
#define SESSION_OSTYPE "Win10.0.0"
#define SESSION_CTRL_PORT 9295
#define CTRL_EXPECT_TIMEOUT 5000
typedef enum ctrl_message_type_t {
CTRL_MESSAGE_TYPE_SESSION_ID = 0x33,
CTRL_MESSAGE_TYPE_HEARTBEAT_REQ = 0xfe,
CTRL_MESSAGE_TYPE_HEARTBEAT_REP = 0x1fe,
CTRL_MESSAGE_TYPE_LOGIN_PIN_REQ = 0x4,
CTRL_MESSAGE_TYPE_LOGIN_PIN_REP = 0x8004,
CTRL_MESSAGE_TYPE_LOGIN = 0x5,
CTRL_MESSAGE_TYPE_GOTO_BED = 0x50,
CTRL_MESSAGE_TYPE_KEYBOARD_ENABLE = 0xd,
CTRL_MESSAGE_TYPE_KEYBOARD_ENABLE_TOGGLE = 0x20,
CTRL_MESSAGE_TYPE_KEYBOARD_OPEN = 0x21,
CTRL_MESSAGE_TYPE_KEYBOARD_CLOSE_REMOTE = 0x22,
CTRL_MESSAGE_TYPE_KEYBOARD_TEXT_CHANGE_REQ = 0x23,
CTRL_MESSAGE_TYPE_KEYBOARD_TEXT_CHANGE_RES = 0x24,
CTRL_MESSAGE_TYPE_KEYBOARD_CLOSE_REQ = 0x25,
CTRL_MESSAGE_TYPE_ENABLE_DUALSENSE_FEATURES = 0x13,
CTRL_MESSAGE_TYPE_GO_HOME = 0x14,
CTRL_MESSAGE_TYPE_DISPLAYA = 0x1,
CTRL_MESSAGE_TYPE_DISPLAYB = 0x16,
CTRL_MESSAGE_TYPE_MIC_CONNECT = 0x30,
CTRL_MESSAGE_TYPE_MIC_TOGGLE = 0x36,
CTRL_MESSAGE_TYPE_DISPLAY_DEVICES = 0x910,
CTRL_MESSAGE_TYPE_SWITCH_TO_STREAM_CONNECTION = 0x34
} CtrlMessageType;
typedef enum ctrl_login_state_t {
CTRL_LOGIN_STATE_SUCCESS = 0x0,
CTRL_LOGIN_STATE_PIN_INCORRECT = 0x1
} CtrlLoginState;
struct chiaki_ctrl_message_queue_t
{
ChiakiCtrlMessageQueue *next;
uint16_t type;
uint8_t *payload;
size_t payload_size;
};
typedef struct ctrl_keyboard_open_t
{
uint8_t unk[0x1C];
uint32_t text_length;
} CtrlKeyboardOpenMessage;
typedef struct ctrl_keyboard_text_request_t
{
uint32_t counter;
uint32_t text_length1;
uint8_t unk1[0x8];
uint8_t unk2[0x10];
uint32_t text_length2;
} CtrlKeyboardTextRequestMessage;
typedef struct ctrl_keyboard_text_response_t
{
uint32_t counter;
uint32_t unk;
uint32_t text_length1;
uint32_t unk2;
uint8_t unk3[0x10];
uint32_t unk4;
uint32_t text_length2;
} CtrlKeyboardTextResponseMessage;
/**
* @return The offset of the mac of size CHIAKI_GKCRYPT_GMAC_SIZE inside a packet of type or -1 if unknown.
*/
int rudp_packet_type_data_offset(uint8_t subtype)
{
switch(subtype)
{
case 0x12:
return 8;
case 0x26:
return 6;
default:
return 2;
}
}
void chiaki_session_send_event(ChiakiSession *session, ChiakiEvent *event);
static void *ctrl_thread_func(void *user);
static ChiakiErrorCode ctrl_message_send(ChiakiCtrl *ctrl, uint16_t type, const uint8_t *payload, size_t payload_size);
static void ctrl_message_received_session_id(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_heartbeat_req(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_login_pin_req(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_login(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_displaya(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_displayb(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_keyboard_open(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_keyboard_close(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_keyboard_text_change(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
static void ctrl_message_received_switch_to_stream_connection(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size);
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_init(ChiakiCtrl *ctrl, ChiakiSession *session)
{
ChiakiErrorCode err = chiaki_mutex_init(&ctrl->notif_mutex, false);
if(err != CHIAKI_ERR_SUCCESS)
return err;
chiaki_mutex_lock(&ctrl->notif_mutex);
ctrl->session = session;
ctrl->should_stop = false;
ctrl->login_pin_entered = false;
ctrl->login_pin_requested = false;
ctrl->login_pin = NULL;
ctrl->login_pin_size = 0;
ctrl->cant_displaya = false;
ctrl->cant_displayb = false;
ctrl->msg_queue = NULL;
ctrl->keyboard_text_counter = 0;
ctrl->sock = CHIAKI_INVALID_SOCKET;
err = chiaki_stop_pipe_init(&ctrl->notif_pipe);
if(err != CHIAKI_ERR_SUCCESS)
goto error_mutex;
chiaki_mutex_unlock(&ctrl->notif_mutex);
return err;
error_mutex:
chiaki_mutex_unlock(&ctrl->notif_mutex);
chiaki_mutex_fini(&ctrl->notif_mutex);
return err;
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_start(ChiakiCtrl *ctrl)
{
ChiakiErrorCode err = chiaki_thread_create(&ctrl->thread, ctrl_thread_func, ctrl);
if(err != CHIAKI_ERR_SUCCESS)
return err;
chiaki_thread_set_name(&ctrl->thread, "Chiaki Ctrl");
return err;
}
CHIAKI_EXPORT void chiaki_ctrl_stop(ChiakiCtrl *ctrl)
{
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->notif_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
ctrl->should_stop = true;
chiaki_stop_pipe_stop(&ctrl->notif_pipe);
chiaki_mutex_unlock(&ctrl->notif_mutex);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_join(ChiakiCtrl *ctrl)
{
return chiaki_thread_join(&ctrl->thread, NULL);
}
CHIAKI_EXPORT void chiaki_ctrl_fini(ChiakiCtrl *ctrl)
{
chiaki_stop_pipe_fini(&ctrl->notif_pipe);
chiaki_mutex_fini(&ctrl->notif_mutex);
free(ctrl->login_pin);
}
static void ctrl_message_queue_free(ChiakiCtrlMessageQueue *queue)
{
free(queue->payload);
free(queue);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_send_message(ChiakiCtrl *ctrl, uint16_t type, const uint8_t *payload, size_t payload_size)
{
ChiakiCtrlMessageQueue *queue = CHIAKI_NEW(ChiakiCtrlMessageQueue);
if(!queue)
return CHIAKI_ERR_MEMORY;
queue->next = NULL;
queue->type = type;
if(payload)
{
queue->payload = malloc(payload_size);
if(!queue->payload)
{
free(queue);
return CHIAKI_ERR_MEMORY;
}
memcpy(queue->payload, payload, payload_size);
queue->payload_size = payload_size;
}
else
{
queue->payload = NULL;
queue->payload_size = 0;
}
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->notif_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
if(!ctrl->msg_queue)
ctrl->msg_queue = queue;
else
{
ChiakiCtrlMessageQueue *c = ctrl->msg_queue;
while(c->next)
c = c->next;
c->next = queue;
}
chiaki_mutex_unlock(&ctrl->notif_mutex);
chiaki_stop_pipe_stop(&ctrl->notif_pipe);
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT void chiaki_ctrl_set_login_pin(ChiakiCtrl *ctrl, const uint8_t *pin, size_t pin_size)
{
uint8_t *buf = malloc(pin_size);
if(!buf)
return;
memcpy(buf, pin, pin_size);
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->notif_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
if(ctrl->login_pin_entered)
free(ctrl->login_pin);
ctrl->login_pin_entered = true;
ctrl->login_pin = buf;
ctrl->login_pin_size = pin_size;
chiaki_stop_pipe_stop(&ctrl->notif_pipe);
chiaki_mutex_unlock(&ctrl->notif_mutex);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_goto_bed(ChiakiCtrl *ctrl)
{
return chiaki_ctrl_send_message(ctrl, CTRL_MESSAGE_TYPE_GOTO_BED, NULL, 0);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_keyboard_set_text(ChiakiCtrl *ctrl, const char *text)
{
const uint32_t length = strlen(text);
const size_t payload_size = sizeof(CtrlKeyboardTextRequestMessage) + length;
uint8_t *payload = malloc(payload_size);
if(!payload)
return CHIAKI_ERR_MEMORY;
memset(payload, 0, payload_size);
memcpy(payload + sizeof(CtrlKeyboardTextRequestMessage), text, length);
CtrlKeyboardTextRequestMessage *msg = (CtrlKeyboardTextRequestMessage *)payload;
msg->counter = ntohl(++ctrl->keyboard_text_counter);
msg->text_length1 = ntohl(length);
msg->text_length2 = ntohl(length);
ChiakiErrorCode err;
err = chiaki_ctrl_send_message(ctrl, CTRL_MESSAGE_TYPE_KEYBOARD_TEXT_CHANGE_REQ, payload, payload_size);
free(payload);
return err;
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_keyboard_accept(ChiakiCtrl *ctrl)
{
const uint8_t accept[4] = { 0x00, 0x00, 0x00, 0x00 };
return chiaki_ctrl_send_message(ctrl, CTRL_MESSAGE_TYPE_KEYBOARD_CLOSE_REQ, accept, 4);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_keyboard_reject(ChiakiCtrl *ctrl)
{
const uint8_t reject[4] = { 0x00, 0x00, 0x00, 0x01 };
return chiaki_ctrl_send_message(ctrl, CTRL_MESSAGE_TYPE_KEYBOARD_CLOSE_REQ, reject, 4);
}
static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl);
static void ctrl_message_received(ChiakiCtrl *ctrl, uint16_t msg_type, uint8_t *payload, size_t payload_size);
static void ctrl_failed(ChiakiCtrl *ctrl, ChiakiQuitReason reason)
{
ChiakiErrorCode mutex_err = chiaki_mutex_lock(&ctrl->session->state_mutex);
assert(mutex_err == CHIAKI_ERR_SUCCESS);
ctrl->session->quit_reason = reason;
ctrl->session->ctrl_failed = true;
chiaki_mutex_unlock(&ctrl->session->state_mutex);
chiaki_cond_signal(&ctrl->session->state_cond);
}
static void *ctrl_thread_func(void *user)
{
ChiakiCtrl *ctrl = user;
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->notif_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
err = ctrl_connect(ctrl);
if(err != CHIAKI_ERR_SUCCESS)
{
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_CONNECT_FAILED);
chiaki_mutex_unlock(&ctrl->notif_mutex);
return NULL;
}
CHIAKI_LOGI(ctrl->session->log, "Ctrl connected");
while(true)
{
bool overflow = false;
while(ctrl->recv_buf_size >= 8)
{
uint32_t payload_size = *((uint32_t *)ctrl->recv_buf);
payload_size = ntohl(payload_size);
if(ctrl->recv_buf_size < 8 + payload_size)
{
if(8 + payload_size > sizeof(ctrl->recv_buf))
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl buffer overflow!");
overflow = true;
}
break;
}
uint16_t msg_type = *((chiaki_unaligned_uint16_t *)(ctrl->recv_buf + 4));
msg_type = ntohs(msg_type);
ctrl_message_received(ctrl, msg_type, ctrl->recv_buf + 8, (size_t)payload_size);
ctrl->recv_buf_size -= 8 + payload_size;
if(ctrl->recv_buf_size > 0)
memmove(ctrl->recv_buf, ctrl->recv_buf + 8 + payload_size, ctrl->recv_buf_size);
}
if(overflow)
{
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
break;
}
chiaki_mutex_unlock(&ctrl->notif_mutex);
if(ctrl->session->rudp)
err = chiaki_rudp_stop_pipe_select_single(ctrl->session->rudp, &ctrl->notif_pipe, UINT64_MAX);
else
err = chiaki_stop_pipe_select_single(&ctrl->notif_pipe, ctrl->sock, false, UINT64_MAX);
chiaki_mutex_lock(&ctrl->notif_mutex);
bool msg_queue_updated = false;
if(err == CHIAKI_ERR_CANCELED)
{
while(ctrl->msg_queue)
{
ctrl_message_send(ctrl, ctrl->msg_queue->type, ctrl->msg_queue->payload, ctrl->msg_queue->payload_size);
ChiakiCtrlMessageQueue *next = ctrl->msg_queue->next;
ctrl_message_queue_free(ctrl->msg_queue);
ctrl->msg_queue = next;
msg_queue_updated = true;
}
if(ctrl->login_pin_entered)
{
CHIAKI_LOGI(ctrl->session->log, "Ctrl received entered Login PIN, sending to console");
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_LOGIN_PIN_REP, ctrl->login_pin, ctrl->login_pin_size);
ctrl->login_pin_entered = false;
free(ctrl->login_pin);
ctrl->login_pin = NULL;
ctrl->login_pin_size = 0;
chiaki_stop_pipe_reset(&ctrl->notif_pipe);
continue;
}
if(ctrl->should_stop)
{
CHIAKI_LOGI(ctrl->session->log, "Ctrl requested to stop");
break;
}
if(msg_queue_updated)
chiaki_stop_pipe_reset(&ctrl->notif_pipe);
continue;
}
else if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl select error: %s", chiaki_error_string(err));
break;
}
CHIAKI_SSIZET_TYPE received = 0;
if(ctrl->session->rudp)
{
RudpMessage message;
uint16_t remote_counter = 0;
uint16_t ack_counter = 0;
err = chiaki_rudp_recv_only(ctrl->session->rudp, sizeof(ctrl->rudp_recv_buf) - ctrl->recv_buf_size, &message);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to receive Rudp ctrl packet");
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
break;
}
if(message.data_size < 4)
{
CHIAKI_LOGE(ctrl->session->log, "Rudp ctrl message response too small");
chiaki_rudp_print_message(ctrl->session->rudp, &message);
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
break;
}
remote_counter = message.remote_counter;
while(true)
{
switch(message.subtype) // wrong but works ...
{
case 0x12:
case 0x26:
case 0x36:
ack_counter = ntohs(*((chiaki_unaligned_uint16_t *)(message.data + 2)));
chiaki_rudp_ack_packet(ctrl->session->rudp, ack_counter);
case 0x02:
chiaki_rudp_send_ack_message(ctrl->session->rudp, remote_counter);
int offset = rudp_packet_type_data_offset(message.subtype);
// ctrl message header is 8 bytes
if((message.data_size - offset) < 8)
break;
// check if message is ctrl message by making sure the payload size (size of message - 8 byte header is correct)
uint32_t ctrl_payload_size = ntohl(*(uint32_t*)(message.data + offset));
if((message.data_size - offset - 8) == ctrl_payload_size)
{
memcpy(ctrl->recv_buf + ctrl->recv_buf_size, message.data + offset, message.data_size - offset);
ctrl->recv_buf_size += message.data_size - offset;
}
break;
case 0x24:
ack_counter = ntohs(*((chiaki_unaligned_uint16_t *)(message.data + 2)));
chiaki_rudp_ack_packet(ctrl->session->rudp, ack_counter);
break;
case 0xC0:
CHIAKI_LOGI(ctrl->session->log, "Received rudp finish message, stopping ctrl.");
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
break;
default:
CHIAKI_LOGI(ctrl->session->log, "Received message of unknown type: 0x%04x", message.type);
chiaki_rudp_ack_packet(ctrl->session->rudp, ack_counter);
chiaki_rudp_send_ack_message(ctrl->session->rudp, remote_counter);
// we already checked before if data size was at least 4
int offset2 = 4;
// ctrl message header is 8 bytes
if((message.data_size - offset2) < 8)
break;
uint32_t ctrl_payload_size2 = ntohl(*(uint32_t*)(message.data + offset2));
if((message.data_size - offset2 - 8) == ctrl_payload_size2)
{
memcpy(ctrl->recv_buf + ctrl->recv_buf_size, message.data + offset2, message.data_size - offset2);
ctrl->recv_buf_size += message.data_size - offset2;
}
break;
}
if(message.subMessage)
{
if(message.data)
{
free(message.data);
message.data = NULL;
}
RudpMessage *tmp = message.subMessage;
memcpy(&message, message.subMessage, sizeof(RudpMessage));
free(tmp);
}
else
{
chiaki_rudp_message_pointers_free(&message);
break;
}
}
}
else
{
received = recv(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)ctrl->recv_buf + ctrl->recv_buf_size, sizeof(ctrl->recv_buf) - ctrl->recv_buf_size, 0);
if(received <= 0)
{
if(received < 0)
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl failed to recv: " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
ctrl_failed(ctrl, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
}
break;
}
CHIAKI_LOGI(ctrl->session->log, "CTRL RECEIVED");
chiaki_log_hexdump(ctrl->session->log, CHIAKI_LOG_INFO, ctrl->recv_buf + ctrl->recv_buf_size, received);
}
ctrl->recv_buf_size += received;
}
chiaki_mutex_unlock(&ctrl->notif_mutex);
if(!ctrl->session->rudp)
{
if(!CHIAKI_SOCKET_IS_INVALID(ctrl->sock))
{
CHIAKI_SOCKET_CLOSE(ctrl->sock);
ctrl->sock = CHIAKI_INVALID_SOCKET;
}
}
return NULL;
}
static ChiakiErrorCode ctrl_message_send(ChiakiCtrl *ctrl, uint16_t type, const uint8_t *payload, size_t payload_size)
{
if(!(payload_size == 0 || payload))
return CHIAKI_ERR_INVALID_DATA;
CHIAKI_LOGV(ctrl->session->log, "Ctrl sending message type %x, size %llx\n",
(unsigned int)type, (unsigned long long)payload_size);
if(payload)
chiaki_log_hexdump(ctrl->session->log, CHIAKI_LOG_VERBOSE, payload, payload_size);
uint8_t *enc = NULL;
if(payload)
{
ChiakiErrorCode err;
enc = malloc(payload_size);
if(!enc)
return CHIAKI_ERR_MEMORY;
if(ctrl->session->rudp && type == CTRL_MESSAGE_TYPE_LOGIN_PIN_REP)
{
uint16_t local_counter = ctrl->crypt_counter_local++;
err = chiaki_rpcrypt_encrypt(&ctrl->session->rpcrypt, local_counter - 1, payload, enc, payload_size);
}
else
err = chiaki_rpcrypt_encrypt(&ctrl->session->rpcrypt, ctrl->crypt_counter_local++, payload, enc, payload_size);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl failed to encrypt payload");
free(enc);
return err;
}
}
#ifdef __GNUC__
__attribute__((aligned(__alignof__(uint32_t))))
#endif
uint8_t header[8];
*((uint32_t *)header) = htonl((uint32_t)payload_size);
*((uint16_t *)(header + 4)) = htons(type);
*((uint16_t *)(header + 6)) = 0;
if(ctrl->session->rudp)
{
uint8_t buf_size = 8 + payload_size;
uint8_t buf[buf_size];
memcpy(buf, header, 8);
if(enc)
memcpy(buf + 8, enc, payload_size);
ChiakiErrorCode err;
err = chiaki_rudp_send_ctrl_message(ctrl->session->rudp, buf, buf_size);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to send Ctrl Message");
return err;
}
}
else
{
int sent = send(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)header, sizeof(header), 0);
if(sent < 0)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to send Ctrl Message Header");
return CHIAKI_ERR_NETWORK;
}
if(enc)
{
sent = send(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)enc, payload_size, 0);
free(enc);
if(sent < 0)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to send Ctrl Message Payload");
return CHIAKI_ERR_NETWORK;
}
}
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode ctrl_message_go_home(ChiakiCtrl *ctrl)
{
CHIAKI_LOGV(ctrl->session->log, "Ctrl sending go to home screen message");
uint8_t home[0x10] = {0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ChiakiErrorCode err = ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_GO_HOME, home, 0x10);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to go to home screen");
return err;
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode ctrl_message_connect_microphone(ChiakiCtrl *ctrl)
{
CHIAKI_LOGV(ctrl->session->log, "Ctrl sending microphone connect message");
uint8_t connect[2] = {0x00, 0x00};
ChiakiErrorCode err = ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_MIC_CONNECT, connect, 0x2);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to connect mic");
return err;
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode ctrl_message_toggle_microphone(ChiakiCtrl *ctrl, bool muted)
{
CHIAKI_LOGV(ctrl->session->log, "Ctrl sending toggle microphone mute message: %s", muted ? "unmute": "mute");
uint8_t toggle[0x4] = {0, 1, 1, 89};
if(muted)
toggle[2] = 0;
ChiakiErrorCode err = ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_MIC_TOGGLE, toggle, 0x4);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to toggle mic mute");
return err;
}
return CHIAKI_ERR_SUCCESS;
}
CHIAKI_EXPORT ChiakiErrorCode ctrl_message_set_fallback_session_id(ChiakiCtrl *ctrl)
{
char fallback_session_id[80];
int64_t time_seconds = chiaki_time_now_monotonic_ms() / 1000;
int len = snprintf(fallback_session_id, 16, "%"PRId64, time_seconds);
if(len < 0)
{
CHIAKI_LOGI(ctrl->session->log, "Error writing time to fallback session id");
return CHIAKI_ERR_UNKNOWN;
}
uint8_t rand_bytes[48];
ChiakiErrorCode err = chiaki_random_bytes_crypt(rand_bytes, 48);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Couldn't generate random bytes to use for fallback session Id with error: %s.", chiaki_error_string(err));
return err;
}
err = chiaki_base64_encode(rand_bytes, sizeof(rand_bytes), fallback_session_id + len, 65);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Couldn't base64 encode rand_bytes for fallback session Id with error: %s", chiaki_error_string(err));
return err;
}
chiaki_mutex_lock(&ctrl->session->state_mutex);
if(ctrl->session->ctrl_session_id_received)
{
CHIAKI_LOGW(ctrl->session->log, "Aleady received session Id don't need fallback.");
chiaki_mutex_unlock(&ctrl->session->state_mutex);
return err;
}
memcpy(ctrl->session->session_id, fallback_session_id, sizeof(fallback_session_id));
CHIAKI_LOGI(ctrl->session->log, "Ctrl set fallback session Id %s", ctrl->session->session_id);
ctrl->session->ctrl_session_id_received = true;
chiaki_mutex_unlock(&ctrl->session->state_mutex);
chiaki_cond_signal(&ctrl->session->state_cond);
return err;
}
static void ctrl_message_received(ChiakiCtrl *ctrl, uint16_t msg_type, uint8_t *payload, size_t payload_size)
{
if(payload_size > 0)
{
ChiakiErrorCode err = chiaki_rpcrypt_decrypt(&ctrl->session->rpcrypt, ctrl->crypt_counter_remote++, payload, payload, payload_size);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to decrypt payload for Ctrl Message type %#x", msg_type);
return;
}
}
CHIAKI_LOGV(ctrl->session->log, "Ctrl received message of type %#x, size %#llx", (unsigned int)msg_type, (unsigned long long)payload_size);
if(payload_size > 0)
chiaki_log_hexdump(ctrl->session->log, CHIAKI_LOG_VERBOSE, payload, payload_size);
switch(msg_type)
{
case CTRL_MESSAGE_TYPE_SESSION_ID:
ctrl_message_received_session_id(ctrl, payload, payload_size);
ctrl_enable_features(ctrl);
break;
case CTRL_MESSAGE_TYPE_HEARTBEAT_REQ:
ctrl_message_received_heartbeat_req(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_LOGIN_PIN_REQ:
ctrl_message_received_login_pin_req(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_LOGIN:
ctrl_message_received_login(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_KEYBOARD_OPEN:
ctrl_message_received_keyboard_open(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_KEYBOARD_TEXT_CHANGE_RES:
ctrl_message_received_keyboard_text_change(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_KEYBOARD_CLOSE_REMOTE:
ctrl_message_received_keyboard_close(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_DISPLAYA:
ctrl_message_received_displaya(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_DISPLAYB:
ctrl_message_received_displayb(ctrl, payload, payload_size);
break;
case CTRL_MESSAGE_TYPE_SWITCH_TO_STREAM_CONNECTION:
ctrl_message_received_switch_to_stream_connection(ctrl, payload, payload_size);
break;
default:
// CHIAKI_LOGW(ctrl->session->log, "Received Ctrl Message with unknown type %#x", msg_type);
chiaki_log_hexdump(ctrl->session->log, CHIAKI_LOG_WARNING, payload, payload_size);
break;
}
}
CHIAKI_EXPORT void ctrl_enable_features(ChiakiCtrl *ctrl)
{
if(ctrl->session->connect_info.enable_dualsense)
{
CHIAKI_LOGI(ctrl->session->log, "Enabling DualSense features");
const uint8_t enable[3] = { 0x00, 0x40, 0x00 };
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_ENABLE_DUALSENSE_FEATURES, enable, 3);
const uint8_t connect[0x10] = { 0xa0, 0xab, 0x51, 0xbd, 0xd1, 0x7e, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 };
ctrl_message_send(ctrl, 0x11, connect, 0x10);
}
if(ctrl->session->connect_info.enable_keyboard)
{
CHIAKI_LOGI(ctrl->session->log, "Enabling Keyboard");
// TODO: Signature ?!
uint8_t enable = 1;
uint8_t signature[0x10] = { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0xAE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_KEYBOARD_ENABLE, signature, 0x10);
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_KEYBOARD_ENABLE_TOGGLE, &enable, 1);
}
ctrl_message_toggle_microphone(ctrl, false);
ctrl_message_toggle_microphone(ctrl, false);
uint8_t display[0x4] = { 0x00, 0x00, 0x00, 0x00 };
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_DISPLAY_DEVICES, display, 0x4);
}
static void ctrl_message_received_session_id(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
chiaki_mutex_lock(&ctrl->session->state_mutex);
if(ctrl->session->ctrl_session_id_received)
{
CHIAKI_LOGW(ctrl->session->log, "Received another Session Id Message");
chiaki_mutex_unlock(&ctrl->session->state_mutex);
return;
}
chiaki_mutex_unlock(&ctrl->session->state_mutex);
if(payload_size < 2)
{
CHIAKI_LOGE(ctrl->session->log, "Invalid Session Id \"%s\" received", payload);
ctrl_message_set_fallback_session_id(ctrl);
return;
}
if(payload[0] != 0x4a)
{
CHIAKI_LOGW(ctrl->session->log, "Received presumably invalid Session Id:");
chiaki_log_hexdump(ctrl->session->log, CHIAKI_LOG_WARNING, payload, payload_size);
}
// skip the size
payload++;
payload_size--;
if(payload_size >= CHIAKI_SESSION_ID_SIZE_MAX - 1)
{
CHIAKI_LOGE(ctrl->session->log, "Received Session Id is too long");
ctrl_message_set_fallback_session_id(ctrl);
return;
}
if(payload_size < 24)
{
CHIAKI_LOGE(ctrl->session->log, "Received Session Id is too short");
ctrl_message_set_fallback_session_id(ctrl);
return;
}
for(uint8_t *cur=payload; cur<payload+payload_size; cur++)
{
char c = *cur;
if(c >= 'a' && c <= 'z')
continue;
if(c >= 'A' && c <= 'Z')
continue;
if(c >= '0' && c <= '9')
continue;
CHIAKI_LOGE(ctrl->session->log, "Ctrl received Session Id contains invalid characters");
ctrl_message_set_fallback_session_id(ctrl);
return;
}
chiaki_mutex_lock(&ctrl->session->state_mutex);
memcpy(ctrl->session->session_id, payload, payload_size);
ctrl->session->session_id[payload_size] = '\0';
CHIAKI_LOGI(ctrl->session->log, "Ctrl received valid Session Id: %s", ctrl->session->session_id);
ctrl->session->ctrl_session_id_received = true;
chiaki_mutex_unlock(&ctrl->session->state_mutex);
chiaki_cond_signal(&ctrl->session->state_cond);
}
static void ctrl_message_received_heartbeat_req(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size != 0)
CHIAKI_LOGW(ctrl->session->log, "Ctrl received Heartbeat request with non-empty payload");
CHIAKI_LOGI(ctrl->session->log, "Ctrl received Heartbeat, sending reply");
ctrl_message_send(ctrl, CTRL_MESSAGE_TYPE_HEARTBEAT_REP, NULL, 0);
}
static void ctrl_message_received_switch_to_stream_connection(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size != 0)
CHIAKI_LOGW(ctrl->session->log, "Ctrl received Switch to Stream Connection Ack with non-empty payload");
if(!ctrl->session->stream_connection_switch_received)
{
chiaki_session_set_stream_connection_switch_received(ctrl->session);
}
else
CHIAKI_LOGI(ctrl->session->log, "Received an extra stream connection switch ACK, ignoring...");
}
static void ctrl_message_received_login_pin_req(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size != 0)
CHIAKI_LOGW(ctrl->session->log, "Ctrl received Login PIN request with non-empty payload");
CHIAKI_LOGI(ctrl->session->log, "Ctrl received Login PIN request");
ctrl->login_pin_requested = true;
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->session->state_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
ctrl->session->ctrl_login_pin_requested = true;
chiaki_mutex_unlock(&ctrl->session->state_mutex);
chiaki_cond_signal(&ctrl->session->state_cond);
}
static void ctrl_message_received_displaya(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload[0] == 0x1)
ctrl->cant_displaya = true;
else if (payload[0] == 0x0 && !ctrl->cant_displayb)
{
ctrl->cant_displaya = false;
CHIAKI_LOGI(ctrl->session->log, "Ctrl received message that the stream can now display.");
ctrl->session->display_sink.cantdisplay_cb(ctrl->session->display_sink.user, false);
}
}
static void ctrl_message_received_displayb(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(ctrl->cant_displaya == true)
{
if(payload[0] == 0x00 && payload[1] == 0x00 && !ctrl->cant_displayb)
{
ctrl->session->display_sink.cantdisplay_cb(ctrl->session->display_sink.user, true);
CHIAKI_LOGI(ctrl->session->log, "Ctrl received message that the stream can't display due to displaying some content that can't be streamed.");
ctrl->cant_displayb = true;
}
}
if(ctrl->cant_displayb && payload[0] == 0x01 && payload[1] == 0xff)
ctrl->cant_displayb = false;
}
static void ctrl_message_received_login(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size != 1)
{
CHIAKI_LOGW(ctrl->session->log, "Ctrl received Login message with payload of size %#llx", (unsigned long long)payload_size);
if(payload_size < 1)
return;
}
CtrlLoginState state = payload[0];
switch(state)
{
case CTRL_LOGIN_STATE_SUCCESS:
CHIAKI_LOGI(ctrl->session->log, "Ctrl received Login message: success");
ctrl->login_pin_requested = false;
break;
case CTRL_LOGIN_STATE_PIN_INCORRECT:
CHIAKI_LOGI(ctrl->session->log, "Ctrl received Login message: PIN incorrect");
if(ctrl->login_pin_requested)
{
CHIAKI_LOGI(ctrl->session->log, "Ctrl requesting PIN from Session again");
ChiakiErrorCode err = chiaki_mutex_lock(&ctrl->session->state_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
ctrl->session->ctrl_login_pin_requested = true;
chiaki_mutex_unlock(&ctrl->session->state_mutex);
chiaki_cond_signal(&ctrl->session->state_cond);
}
else
CHIAKI_LOGW(ctrl->session->log, "Ctrl Login PIN incorrect message, but PIN was not requested");
break;
default:
CHIAKI_LOGI(ctrl->session->log, "Ctrl received Login message with state: %#x", state);
break;
}
}
static void ctrl_message_received_keyboard_open(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size < sizeof(CtrlKeyboardOpenMessage))
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl received invalid message keyboard open with payload size %zu while expected size is at least %zu", payload_size, sizeof(CtrlKeyboardOpenMessage));
return;
}
CtrlKeyboardOpenMessage *msg = (CtrlKeyboardOpenMessage *)payload;
msg->text_length = ntohl(msg->text_length);
assert(payload_size == sizeof(CtrlKeyboardOpenMessage) + msg->text_length);
uint8_t *buffer = msg->text_length > 0 ? malloc((size_t)msg->text_length + 1) : NULL;
if(buffer)
{
buffer[msg->text_length] = '\0';
memcpy(buffer, payload + sizeof(CtrlKeyboardOpenMessage), msg->text_length);
}
ChiakiEvent keyboard_event;
keyboard_event.type = CHIAKI_EVENT_KEYBOARD_OPEN;
keyboard_event.keyboard.text_str = (const char *)buffer;
chiaki_session_send_event(ctrl->session, &keyboard_event);
if(buffer)
free(buffer);
}
static void ctrl_message_received_keyboard_close(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
(void)payload;
(void)payload_size;
ChiakiEvent keyboard_event;
keyboard_event.type = CHIAKI_EVENT_KEYBOARD_REMOTE_CLOSE;
keyboard_event.keyboard.text_str = NULL;
chiaki_session_send_event(ctrl->session, &keyboard_event);
}
static void ctrl_message_received_keyboard_text_change(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
{
if(payload_size < sizeof(CtrlKeyboardTextResponseMessage))
{
CHIAKI_LOGE(ctrl->session->log, "Ctrl received invalid message keyboard text change with payload size %zu while expected size is at least %zu", payload_size, sizeof(CtrlKeyboardTextResponseMessage));
return;
}
CtrlKeyboardTextResponseMessage *msg = (CtrlKeyboardTextResponseMessage *)payload;
msg->text_length1 = ntohl(msg->text_length1);
assert(payload_size == sizeof(CtrlKeyboardTextResponseMessage) + msg->text_length1);
uint8_t *buffer = msg->text_length1 > 0 ? malloc((size_t)msg->text_length1 + 1) : NULL;
if(buffer)
{
buffer[msg->text_length1] = '\0';
memcpy(buffer, payload + sizeof(CtrlKeyboardTextResponseMessage), msg->text_length1);
}
ChiakiEvent keyboard_event;
keyboard_event.type = CHIAKI_EVENT_KEYBOARD_TEXT_CHANGE;
keyboard_event.keyboard.text_str = (const char *)buffer;
chiaki_session_send_event(ctrl->session, &keyboard_event);
if(buffer)
free(buffer);
}
typedef struct ctrl_response_t
{
bool server_type_valid;
uint8_t rp_server_type[0x10];