forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathtox.c
2884 lines (2317 loc) · 81.9 KB
/
tox.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: GPL-3.0-or-later
* Copyright © 2016-2018 The TokTok team.
* Copyright © 2013 Tox project.
*/
/*
* The Tox public API.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#include "tox.h"
#include "tox_private.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Messenger.h"
#include "group.h"
#include "logger.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "../toxencryptsave/defines.h"
#include <errno.h>
#if !defined(HAVE_LIBEV) && !defined(HAVE_LIBEVENT)
#if defined (WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <winsock2.h>
#else
#include <sys/select.h>
#endif // WIN32 || _WIN32 || __WIN32__
#endif // !HAVE_LIBEV && !HAVE_LIBEVENT
#define SET_ERROR_PARAMETER(param, x) \
do { \
if (param) { \
*param = x; \
} \
} while (0)
static_assert(TOX_HASH_LENGTH == CRYPTO_SHA256_SIZE,
"TOX_HASH_LENGTH is assumed to be equal to CRYPTO_SHA256_SIZE");
static_assert(FILE_ID_LENGTH == CRYPTO_SYMMETRIC_KEY_SIZE,
"FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE");
static_assert(TOX_FILE_ID_LENGTH == CRYPTO_SYMMETRIC_KEY_SIZE,
"TOX_FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE");
static_assert(TOX_FILE_ID_LENGTH == TOX_HASH_LENGTH,
"TOX_FILE_ID_LENGTH is assumed to be equal to TOX_HASH_LENGTH");
static_assert(TOX_PUBLIC_KEY_SIZE == CRYPTO_PUBLIC_KEY_SIZE,
"TOX_PUBLIC_KEY_SIZE is assumed to be equal to CRYPTO_PUBLIC_KEY_SIZE");
static_assert(TOX_SECRET_KEY_SIZE == CRYPTO_SECRET_KEY_SIZE,
"TOX_SECRET_KEY_SIZE is assumed to be equal to CRYPTO_SECRET_KEY_SIZE");
static_assert(TOX_MAX_NAME_LENGTH == MAX_NAME_LENGTH,
"TOX_MAX_NAME_LENGTH is assumed to be equal to MAX_NAME_LENGTH");
static_assert(TOX_MAX_STATUS_MESSAGE_LENGTH == MAX_STATUSMESSAGE_LENGTH,
"TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH");
#if defined(HAVE_LIBEV) || defined(HAVE_LIBEVENT)
typedef struct {
Tox *tox;
void *user_data;
} Event_Arg;
#endif
struct Tox {
// XXX: Messenger *must* be the first member, because toxav casts its
// `Tox *` to `Messenger **`.
Messenger *m;
Mono_Time *mono_time;
pthread_mutex_t *mutex;
tox_self_connection_status_cb *self_connection_status_callback;
tox_friend_name_cb *friend_name_callback;
tox_friend_status_message_cb *friend_status_message_callback;
tox_friend_status_cb *friend_status_callback;
tox_friend_connection_status_cb *friend_connection_status_callback;
tox_friend_typing_cb *friend_typing_callback;
tox_friend_read_receipt_cb *friend_read_receipt_callback;
tox_friend_request_cb *friend_request_callback;
tox_friend_message_cb *friend_message_callback;
tox_file_recv_control_cb *file_recv_control_callback;
tox_file_chunk_request_cb *file_chunk_request_callback;
tox_file_recv_cb *file_recv_callback;
tox_file_recv_chunk_cb *file_recv_chunk_callback;
tox_conference_invite_cb *conference_invite_callback;
tox_conference_connected_cb *conference_connected_callback;
tox_conference_message_cb *conference_message_callback;
tox_conference_title_cb *conference_title_callback;
tox_conference_peer_name_cb *conference_peer_name_callback;
tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback;
tox_friend_lossy_packet_cb *friend_lossy_packet_callback_per_pktid[UINT8_MAX + 1];
tox_friend_lossless_packet_cb *friend_lossless_packet_callback_per_pktid[UINT8_MAX + 1];
tox_loop_begin_cb *loop_begin_cb;
tox_loop_end_cb *loop_end_cb;
void *toxav_object; // workaround to store a ToxAV object (setter and getter functions are available)
};
static void lock(const Tox *tox)
{
if (tox->mutex != nullptr) {
pthread_mutex_lock(tox->mutex);
}
}
static void unlock(const Tox *tox)
{
if (tox->mutex != nullptr) {
pthread_mutex_unlock(tox->mutex);
}
}
struct Tox_Userdata {
Tox *tox;
void *user_data;
};
static void tox_self_connection_status_handler(Messenger *m, unsigned int connection_status, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->self_connection_status_callback != nullptr) {
tox_data->tox->self_connection_status_callback(tox_data->tox, (Tox_Connection)connection_status, tox_data->user_data);
}
}
static void tox_friend_name_handler(Messenger *m, uint32_t friend_number, const uint8_t *name, size_t length,
void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_name_callback != nullptr) {
tox_data->tox->friend_name_callback(tox_data->tox, friend_number, name, length, tox_data->user_data);
}
}
static void tox_friend_status_message_handler(Messenger *m, uint32_t friend_number, const uint8_t *message,
size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_status_message_callback != nullptr) {
tox_data->tox->friend_status_message_callback(tox_data->tox, friend_number, message, length, tox_data->user_data);
}
}
static void tox_friend_status_handler(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_status_callback != nullptr) {
tox_data->tox->friend_status_callback(tox_data->tox, friend_number, (Tox_User_Status)status, tox_data->user_data);
}
}
static void tox_friend_connection_status_handler(Messenger *m, uint32_t friend_number, unsigned int connection_status,
void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_connection_status_callback != nullptr) {
tox_data->tox->friend_connection_status_callback(tox_data->tox, friend_number, (Tox_Connection)connection_status,
tox_data->user_data);
}
}
static void tox_friend_typing_handler(Messenger *m, uint32_t friend_number, bool is_typing, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_typing_callback != nullptr) {
tox_data->tox->friend_typing_callback(tox_data->tox, friend_number, is_typing, tox_data->user_data);
}
}
static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number, uint32_t message_id, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_read_receipt_callback != nullptr) {
tox_data->tox->friend_read_receipt_callback(tox_data->tox, friend_number, message_id, tox_data->user_data);
}
}
static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length,
void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_request_callback != nullptr) {
tox_data->tox->friend_request_callback(tox_data->tox, public_key, message, length, tox_data->user_data);
}
}
static void tox_friend_message_handler(Messenger *m, uint32_t friend_number, unsigned int type, const uint8_t *message,
size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_message_callback != nullptr) {
tox_data->tox->friend_message_callback(tox_data->tox, friend_number, (Tox_Message_Type)type, message, length,
tox_data->user_data);
}
}
static void tox_file_recv_control_handler(Messenger *m, uint32_t friend_number, uint32_t file_number,
unsigned int control, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->file_recv_control_callback != nullptr) {
tox_data->tox->file_recv_control_callback(tox_data->tox, friend_number, file_number, (Tox_File_Control)control,
tox_data->user_data);
}
}
static void tox_file_chunk_request_handler(Messenger *m, uint32_t friend_number, uint32_t file_number,
uint64_t position, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->file_chunk_request_callback != nullptr) {
tox_data->tox->file_chunk_request_callback(tox_data->tox, friend_number, file_number, position, length,
tox_data->user_data);
}
}
static void tox_file_recv_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint32_t kind,
uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->file_recv_callback != nullptr) {
tox_data->tox->file_recv_callback(tox_data->tox, friend_number, file_number, kind, file_size, filename, filename_length,
tox_data->user_data);
}
}
static void tox_file_recv_chunk_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position,
const uint8_t *data, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->file_recv_chunk_callback != nullptr) {
tox_data->tox->file_recv_chunk_callback(tox_data->tox, friend_number, file_number, position, data, length,
tox_data->user_data);
}
}
static void tox_conference_invite_handler(Messenger *m, uint32_t friend_number, int type, const uint8_t *cookie,
size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_invite_callback != nullptr) {
tox_data->tox->conference_invite_callback(tox_data->tox, friend_number, (Tox_Conference_Type)type, cookie, length,
tox_data->user_data);
}
}
static void tox_conference_connected_handler(Messenger *m, uint32_t conference_number, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_connected_callback != nullptr) {
tox_data->tox->conference_connected_callback(tox_data->tox, conference_number, tox_data->user_data);
}
}
static void tox_conference_message_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, int type,
const uint8_t *message, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_message_callback != nullptr) {
tox_data->tox->conference_message_callback(tox_data->tox, conference_number, peer_number, (Tox_Message_Type)type,
message, length, tox_data->user_data);
}
}
static void tox_conference_title_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number,
const uint8_t *title, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_title_callback != nullptr) {
tox_data->tox->conference_title_callback(tox_data->tox, conference_number, peer_number, title, length,
tox_data->user_data);
}
}
static void tox_conference_peer_name_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number,
const uint8_t *name, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_peer_name_callback != nullptr) {
tox_data->tox->conference_peer_name_callback(tox_data->tox, conference_number, peer_number, name, length,
tox_data->user_data);
}
}
static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conference_number, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->conference_peer_list_changed_callback != nullptr) {
tox_data->tox->conference_peer_list_changed_callback(tox_data->tox, conference_number, tox_data->user_data);
}
}
static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
const uint8_t *data, size_t length, void *user_data)
{
assert(data != nullptr);
assert(length > 0);
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id] != nullptr) {
tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
tox_data->user_data);
}
}
static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
const uint8_t *data, size_t length, void *user_data)
{
assert(data != nullptr);
assert(length > 0);
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id] != nullptr) {
tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
tox_data->user_data);
}
}
bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch)
{
return TOX_VERSION_IS_API_COMPATIBLE(major, minor, patch);
}
static State_Load_Status state_load_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
{
const Tox *tox = (const Tox *)outer;
State_Load_Status status = STATE_LOAD_STATUS_CONTINUE;
if (messenger_load_state_section(tox->m, data, length, type, &status)
|| conferences_load_state_section(tox->m->conferences_object, data, length, type, &status)) {
return status;
}
if (type == STATE_TYPE_END) {
if (length != 0) {
return STATE_LOAD_STATUS_ERROR;
}
return STATE_LOAD_STATUS_END;
}
LOGGER_ERROR(tox->m->log, "Load state: contains unrecognized part (len %u, type %u)",
length, type);
return STATE_LOAD_STATUS_CONTINUE;
}
/* Load tox from data of size length. */
static int tox_load(Tox *tox, const uint8_t *data, uint32_t length)
{
uint32_t data32[2];
const uint32_t cookie_len = sizeof(data32);
if (length < cookie_len) {
return -1;
}
memcpy(data32, data, sizeof(uint32_t));
lendian_bytes_to_host32(data32 + 1, data + sizeof(uint32_t));
if (data32[0] != 0 || data32[1] != STATE_COOKIE_GLOBAL) {
return -1;
}
return state_load(tox->m->log, state_load_callback, tox, data + cookie_len,
length - cookie_len, STATE_COOKIE_TYPE);
}
Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
{
Tox *tox = (Tox *)calloc(1, sizeof(Tox));
if (tox == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
return nullptr;
}
Messenger_Options m_options = {0};
bool load_savedata_sk = false;
bool load_savedata_tox = false;
struct Tox_Options *default_options = nullptr;
if (options == nullptr) {
Tox_Err_Options_New err;
default_options = tox_options_new(&err);
switch (err) {
case TOX_ERR_OPTIONS_NEW_OK: {
break;
}
case TOX_ERR_OPTIONS_NEW_MALLOC: {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
free(tox);
return nullptr;
}
}
}
const struct Tox_Options *const opts = options != nullptr ? options : default_options;
assert(opts != nullptr);
if (tox_options_get_savedata_type(opts) != TOX_SAVEDATA_TYPE_NONE) {
if (tox_options_get_savedata_data(opts) == nullptr || tox_options_get_savedata_length(opts) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
tox_options_free(default_options);
free(tox);
return nullptr;
}
}
if (tox_options_get_savedata_type(opts) == TOX_SAVEDATA_TYPE_SECRET_KEY) {
if (tox_options_get_savedata_length(opts) != TOX_SECRET_KEY_SIZE) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
tox_options_free(default_options);
free(tox);
return nullptr;
}
load_savedata_sk = true;
} else if (tox_options_get_savedata_type(opts) == TOX_SAVEDATA_TYPE_TOX_SAVE) {
if (tox_options_get_savedata_length(opts) < TOX_ENC_SAVE_MAGIC_LENGTH) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
tox_options_free(default_options);
free(tox);
return nullptr;
}
if (memcmp(tox_options_get_savedata_data(opts), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED);
tox_options_free(default_options);
free(tox);
return nullptr;
}
load_savedata_tox = true;
}
m_options.ipv6enabled = tox_options_get_ipv6_enabled(opts);
m_options.udp_disabled = !tox_options_get_udp_enabled(opts);
m_options.port_range[0] = tox_options_get_start_port(opts);
m_options.port_range[1] = tox_options_get_end_port(opts);
m_options.tcp_server_port = tox_options_get_tcp_port(opts);
m_options.hole_punching_enabled = tox_options_get_hole_punching_enabled(opts);
m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(opts);
m_options.log_callback = (logger_cb *)tox_options_get_log_callback(opts);
m_options.log_context = tox;
m_options.log_user_data = tox_options_get_log_user_data(opts);
switch (tox_options_get_proxy_type(opts)) {
case TOX_PROXY_TYPE_HTTP: {
m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
break;
}
case TOX_PROXY_TYPE_SOCKS5: {
m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
break;
}
case TOX_PROXY_TYPE_NONE: {
m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
break;
}
default: {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE);
tox_options_free(default_options);
free(tox);
return nullptr;
}
}
if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) {
if (tox_options_get_proxy_port(opts) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT);
tox_options_free(default_options);
free(tox);
return nullptr;
}
ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled);
if (m_options.ipv6enabled) {
m_options.proxy_info.ip_port.ip.family = net_family_unspec;
}
if (addr_resolve_or_parse_ip(tox_options_get_proxy_host(opts), &m_options.proxy_info.ip_port.ip, nullptr) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST);
// TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain.
tox_options_free(default_options);
free(tox);
return nullptr;
}
m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(opts));
}
tox->mono_time = mono_time_new();
if (tox->mono_time == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
tox_options_free(default_options);
free(tox);
return nullptr;
}
if (tox_options_get_experimental_thread_safety(opts)) {
tox->mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (tox->mutex == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
tox_options_free(default_options);
free(tox);
return nullptr;
}
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(tox->mutex, &attr);
} else {
tox->mutex = nullptr;
}
lock(tox);
unsigned int m_error;
tox->m = new_messenger(tox->mono_time, &m_options, &m_error);
// TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so
// new_groupchats can assume m is non-NULL.
if (!new_groupchats(tox->mono_time, tox->m)) {
kill_messenger(tox->m);
if (m_error == MESSENGER_ERROR_PORT) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC);
} else if (m_error == MESSENGER_ERROR_TCP_SERVER) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC);
} else {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
}
mono_time_free(tox->mono_time);
tox_options_free(default_options);
unlock(tox);
if (tox->mutex != nullptr) {
pthread_mutex_destroy(tox->mutex);
}
free(tox->mutex);
free(tox);
return nullptr;
}
if (load_savedata_tox
&& tox_load(tox, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
} else if (load_savedata_sk) {
load_secret_key(tox->m->net_crypto, tox_options_get_savedata_data(opts));
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
} else {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
}
m_callback_namechange(tox->m, tox_friend_name_handler);
m_callback_core_connection(tox->m, tox_self_connection_status_handler);
m_callback_statusmessage(tox->m, tox_friend_status_message_handler);
m_callback_userstatus(tox->m, tox_friend_status_handler);
m_callback_connectionstatus(tox->m, tox_friend_connection_status_handler);
m_callback_typingchange(tox->m, tox_friend_typing_handler);
m_callback_read_receipt(tox->m, tox_friend_read_receipt_handler);
m_callback_friendrequest(tox->m, tox_friend_request_handler);
m_callback_friendmessage(tox->m, tox_friend_message_handler);
callback_file_control(tox->m, tox_file_recv_control_handler);
callback_file_reqchunk(tox->m, tox_file_chunk_request_handler);
callback_file_sendrequest(tox->m, tox_file_recv_handler);
callback_file_data(tox->m, tox_file_recv_chunk_handler);
g_callback_group_invite(tox->m->conferences_object, tox_conference_invite_handler);
g_callback_group_connected(tox->m->conferences_object, tox_conference_connected_handler);
g_callback_group_message(tox->m->conferences_object, tox_conference_message_handler);
g_callback_group_title(tox->m->conferences_object, tox_conference_title_handler);
g_callback_peer_name(tox->m->conferences_object, tox_conference_peer_name_handler);
g_callback_peer_list_changed(tox->m->conferences_object, tox_conference_peer_list_changed_handler);
custom_lossy_packet_registerhandler(tox->m, tox_friend_lossy_packet_handler);
custom_lossless_packet_registerhandler(tox->m, tox_friend_lossless_packet_handler);
tox_options_free(default_options);
unlock(tox);
return tox;
}
void tox_kill(Tox *tox)
{
if (tox == nullptr) {
return;
}
lock(tox);
LOGGER_ASSERT(tox->m->log, tox->m->msi_packet == nullptr, "Attempted to kill tox while toxav is still alive");
tox_loop_stop(tox);
kill_groupchats(tox->m->conferences_object);
kill_messenger(tox->m);
mono_time_free(tox->mono_time);
unlock(tox);
if (tox->mutex != nullptr) {
pthread_mutex_destroy(tox->mutex);
free(tox->mutex);
}
free(tox);
}
static uint32_t end_size(void)
{
return 2 * sizeof(uint32_t);
}
static void end_save(uint8_t *data)
{
state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_END);
}
size_t tox_get_savedata_size(const Tox *tox)
{
assert(tox != nullptr);
lock(tox);
size_t ret = 2 * sizeof(uint32_t)
+ messenger_size(tox->m)
+ conferences_size(tox->m->conferences_object)
+ end_size();
unlock(tox);
return ret;
}
void tox_get_savedata(const Tox *tox, uint8_t *savedata)
{
assert(tox != nullptr);
if (savedata == nullptr) {
return;
}
memset(savedata, 0, tox_get_savedata_size(tox));
lock(tox);
const uint32_t size32 = sizeof(uint32_t);
// write cookie
memset(savedata, 0, size32);
savedata += size32;
host_to_lendian_bytes32(savedata, STATE_COOKIE_GLOBAL);
savedata += size32;
savedata = messenger_save(tox->m, savedata);
savedata = conferences_save(tox->m->conferences_object, savedata);
end_save(savedata);
unlock(tox);
}
bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, Tox_Err_Bootstrap *error)
{
assert(tox != nullptr);
if (!host || !public_key) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL);
return 0;
}
if (port == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT);
return 0;
}
IP_Port *root;
const int32_t count = net_getipport(host, &root, TOX_SOCK_DGRAM);
if (count == -1) {
net_freeipport(root);
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0;
}
lock(tox);
for (int32_t i = 0; i < count; ++i) {
root[i].port = net_htons(port);
onion_add_bs_path_node(tox->m->onion_c, root[i], public_key);
if (!tox->m->options.udp_disabled) {
dht_bootstrap(tox->m->dht, root[i], public_key);
}
}
unlock(tox);
net_freeipport(root);
if (count) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
return 1;
}
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0;
}
bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key,
Tox_Err_Bootstrap *error)
{
assert(tox != nullptr);
if (!host || !public_key) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL);
return 0;
}
if (port == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT);
return 0;
}
IP_Port *root;
const int32_t count = net_getipport(host, &root, TOX_SOCK_STREAM);
if (count == -1) {
net_freeipport(root);
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0;
}
lock(tox);
for (int32_t i = 0; i < count; ++i) {
root[i].port = net_htons(port);
add_tcp_relay(tox->m->net_crypto, root[i], public_key);
}
unlock(tox);
net_freeipport(root);
if (count) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
return 1;
}
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0;
}
Tox_Connection tox_self_get_connection_status(const Tox *tox)
{
assert(tox != nullptr);
lock(tox);
const unsigned int ret = onion_connection_status(tox->m->onion_c);
unlock(tox);
if (ret == 2) {
return TOX_CONNECTION_UDP;
}
if (ret == 1) {
return TOX_CONNECTION_TCP;
}
return TOX_CONNECTION_NONE;
}
void tox_callback_self_connection_status(Tox *tox, tox_self_connection_status_cb *callback)
{
assert(tox != nullptr);
tox->self_connection_status_callback = callback;
}
uint32_t tox_iteration_interval(const Tox *tox)
{
assert(tox != nullptr);
lock(tox);
uint32_t ret = messenger_run_interval(tox->m);
unlock(tox);
return ret;
}
void tox_iterate(Tox *tox, void *user_data)
{
assert(tox != nullptr);
lock(tox);
mono_time_update(tox->mono_time);
struct Tox_Userdata tox_data = { tox, user_data };
do_messenger(tox->m, &tox_data);
do_groupchats(tox->m->conferences_object, &tox_data);
unlock(tox);
}
void tox_callback_loop_begin(Tox *tox, tox_loop_begin_cb *callback)
{
if (tox == NULL) {
return;
}
tox->loop_begin_cb = callback;
}
void tox_callback_loop_end(Tox *tox, tox_loop_end_cb *callback)
{
if (tox == NULL) {
return;
}
tox->loop_end_cb = callback;
}
#ifdef HAVE_LIBEV
static void tox_stop_loop_cb(struct ev_loop *dispatcher, ev_async *listener, int events)
{
if (dispatcher == NULL || listener == NULL) {
return;
}
Event_Arg *tmp = (Event_Arg *) listener->data;
Messenger *m = tmp->tox;
if (ev_is_active(&m->net->sock_listener.listener) || ev_is_pending(&m->net->sock_listener.listener)) {
ev_io_stop(dispatcher, &m->net->sock_listener.listener);
}
uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto));
for (uint32_t i = 0; i < len; i++) {
const TCP_con *conn = tcp_connections_connection_at(nc_get_tcp_c(m->net_crypto), i);
if (ev_is_active(&conn->connection->sock_listener.listener)
|| ev_is_pending(&conn->connection->sock_listener.listener)) {
ev_io_stop(dispatcher, &conn->connection->sock_listener.listener);
}
}
ev_async_stop(dispatcher, listener);
ev_break(dispatcher, EVBREAK_ALL);
}
static void tox_do_iterate(struct ev_loop *dispatcher, ev_io *sock_listener, int events)
{
if (dispatcher == NULL || sock_listener == NULL) {
return;
}
Event_Arg *tmp = (Event_Arg *) sock_listener->data;
Messenger *m = tmp->tox->m;
if (tmp->tox->loop_begin_cb) {
tmp->tox->loop_begin_cb(tmp->tox, tmp->user_data);
}
tox_iterate(tmp->tox, tmp->user_data);
if (!ev_is_active(&m->net->sock_listener.listener) && !ev_is_pending(&m->net->sock_listener.listener)) {
m->net->sock_listener.dispatcher = dispatcher;
ev_io_init(&m->net->sock_listener.listener, tox_do_iterate, net_sock(m->net), EV_READ);
m->net->sock_listener.listener.data = sock_listener->data;
ev_io_start(dispatcher, &m->net->sock_listener.listener);
}
uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto));
for (uint32_t i = 0; i < len; i++) {
const TCP_con *conn = tcp_connections_connection_at(nc_get_tcp_c(m->net_crypto), i);
if (!ev_is_active(&conn->connection->sock_listener.listener)
&& !ev_is_pending(&conn->connection->sock_listener.listener)) {
conn->connection->sock_listener.dispatcher = dispatcher;
ev_io_init(&conn->connection->sock_listener.listener, tox_do_iterate, tcp_con_sock(conn->connection), EV_READ);
conn->connection->sock_listener.listener.data = sock_listener->data;
ev_io_start(m->dispatcher, &conn->connection->sock_listener.listener);
}
}
if (m->loop_end_cb) {
m->loop_end_cb(m, tmp->user_data);
}
}
#elif defined(HAVE_LIBEVENT)
static void tox_do_iterate(evutil_socket_t fd, short events, void *arg)
{
if (arg == NULL) {
return;
}
Event_Arg *tmp = (Event_Arg *) arg;
Messenger *m = tmp->tox;
struct timeval timeout;
if (m->loop_begin_cb) {
m->loop_begin_cb(m, tmp->user_data);
}
tox_iterate(tmp->tox, tmp->user_data);
timeout.tv_sec = 0;
// TODO(cleverca22): use a longer timeout.
timeout.tv_usec = tox_iteration_interval(tmp->tox) * 1000 * 2;
if (!m->net->sock_listener) {
m->net->sock_listener = event_new(m->dispatcher, net_sock(m->net), EV_READ | EV_PERSIST, tox_do_iterate, arg);
}
event_add(m->net->sock_listener, &timeout);
uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto));
for (uint32_t i = 0; i < len; i++) {
const TCP_con *conn = tcp_connections_connection_at(nc_get_tcp_c(m->net_crypto), i);
if (!conn->connection->sock_listener) {
conn->connection->sock_listener = event_new(m->dispatcher, tcp_con_sock(conn->connection), EV_READ | EV_PERSIST,
tox_do_iterate, arg);
}
event_add(conn->connection->sock_listener, NULL);
}
if (m->loop_end_cb) {
m->loop_end_cb(m, tmp->user_data);
}
}
#else
/**
* Gathers a list of every network file descriptor,
* where an activity is expected on.
*
* @param sockets a pointer to an array (the pointed array can be NULL).
* @param sockets_num the number of current known sockets (will be updated by the funciton).
*
* @return false if errors occurred, true otherwise.
*/
static bool tox_fds(Messenger *m, Socket **sockets, uint32_t *sockets_num)
{
if (m == NULL || sockets == NULL || sockets_num == NULL) {
return false;
}
uint32_t len = tcp_connections_length(nc_get_tcp_c(m->net_crypto));
uint32_t fdcount = 1 + len;
if (fdcount != *sockets_num || *sockets == NULL) {
Socket *tmp_sockets = (Socket *)realloc(*sockets, fdcount * sizeof(Socket));
if (tmp_sockets == NULL) {
return false;
}
*sockets = tmp_sockets;