forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_transfers.c
1094 lines (940 loc) · 44 KB
/
file_transfers.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
#include "main.h"
//static FILE_TRANSFER *file_t[256], **file_tend = file_t;
static FILE_TRANSFER outgoing_transfer[MAX_NUM_FRIENDS][MAX_FILE_TRANSFERS];
static FILE_TRANSFER incoming_transfer[MAX_NUM_FRIENDS][MAX_FILE_TRANSFERS];
static BROKEN_TRANSFER broken_list[MAX_FILE_TRANSFERS] = {{0}};
// Remove if supported by core
#define TOX_FILE_KIND_EXISTING 3
FILE_TRANSFER *get_file_transfer(uint32_t friend_number, uint32_t file_number){
_Bool incoming = 0;
if (file_number >= (1 << 16)) {
file_number = (file_number >> 16) - 1;
incoming = 1;
}
if (file_number >= MAX_FILE_TRANSFERS)
return NULL;
FILE_TRANSFER *ft;
if (incoming) {
ft = &incoming_transfer[friend_number][file_number];
ft->incoming = 1;
} else {
ft = &outgoing_transfer[friend_number][file_number];
ft->incoming = 0;
}
return ft;
}
/* The following are internal file status helper functions */
static void utox_update_user_file(FILE_TRANSFER *file){
FILE_TRANSFER *file_copy = malloc(sizeof(FILE_TRANSFER));
memcpy(file_copy, file, sizeof(FILE_TRANSFER));
postmessage(FRIEND_FILE_UPDATE, 0, 0, file_copy);
}
static void calculate_speed(FILE_TRANSFER *file){
if ((file->speed) > file->num_packets * 20 * 1371) {
++file->num_packets;
return;
}
file->num_packets = 0;
uint64_t time = get_time();
if (!file->last_check_time) {
file->last_check_time = time;
return;
}
if(time - file->last_check_time >= 1000 * 1000 * 100) {
file->speed = (((double)(file->size_transferred - file->last_check_transferred) * 1000.0 * 1000.0 * 1000.0) / (double)(time - file->last_check_time)) + 0.5;
file->last_check_time = time;
file->last_check_transferred = file->size_transferred;
}
utox_update_user_file(file);
}
static int utox_file_alloc_resume(Tox *tox, FILE_TRANSFER *file){
TOX_ERR_FILE_GET error;
int i = 0;
int resume = 0;
resume = tox_file_get_file_id(tox, file->friend_number, file->file_number, file->file_id, &error);
if (resume){
file->resume = 0;
for(i = 1; i < MAX_FILE_TRANSFERS; i++){
if(!broken_list[i].used){
// TODO set an expire time for this.
broken_list[i].used = 1;
broken_list[i].incoming = file->incoming;
broken_list[i].friend_number = file->friend_number;
broken_list[i].file_number = file->file_number;
memcpy(broken_list[i].file_id, file->file_id, sizeof(uint8_t) * TOX_FILE_ID_LENGTH);
if(file->path){
memcpy(broken_list[i].file_path, file->path, file->path_length);
}
FILE_TRANSFER *data = malloc(sizeof(FILE_TRANSFER));
memcpy(data, file, sizeof(FILE_TRANSFER));
broken_list[i].data = data;
break;
}
}
if(i >= 1 && i <= MAX_FILE_TRANSFERS){
debug("FileTransfer:\tBroken transfer #%u set; ready to resume file %.*s\n", i, (uint32_t)file->name_length, file->name);
return i;
} else {
debug("FileTransfer:\tUnable to set resume number; number was %i, name was %.*s\n", i, (uint32_t)file->name_length, file->name);
return 0;
}
} else {
debug("FileTransfer:\tUnable to get file id for incoming file... uTox can't resume file %.*s\n", (uint32_t)file->name_length, file->name);
return 0;
}
}
static void utox_file_free_resume(uint8_t i){
if(i >= 1 && i < MAX_FILE_TRANSFERS){
memset(&broken_list[i], 0, sizeof(BROKEN_TRANSFER));
// TODO recurse and free needed allocs
debug("FileTransfer:\tBroken transfer #%u reset!\n",i);
return;
}
if ( i != 0 || broken_list[i].used) { // We shouldn't be called for a 0; but either way no error needed!
debug("FileTransfer:\tBroken transfer #%u unable to be reset! This is bad!\n",i);
}
}
static void utox_build_file_transfer(FILE_TRANSFER *ft, uint32_t friend_number, uint32_t file_number,
uint64_t file_size, _Bool incoming, _Bool in_memory, _Bool is_avatar, uint8_t kind, const uint8_t *name,
size_t name_length, const uint8_t *path, size_t path_length, const uint8_t *file_id, Tox *tox);
static FILE_TRANSFER* utox_file_find_existing(uint8_t *file_id){
if(!file_id){
return NULL;
}
for(int i = 1; i < MAX_FILE_TRANSFERS; i++){
if(broken_list[i].used && memcmp(broken_list[i].file_id, file_id, TOX_FILE_ID_LENGTH) == 0){
if(broken_list[i].data){
debug("FileTransfer:\tExisting found, list number %u\n", i);
return broken_list[i].data;
} else {
debug("FileTransfer:\tExisting found (post-restart), list number %u\n", i);
FILE_TRANSFER *tmp = malloc(sizeof(*tmp));
utox_build_file_transfer(tmp, broken_list[i].friend_number, broken_list[i].file_number, 0,
broken_list[i].incoming, 0, 0, 3, NULL, 0, broken_list[i].file_path,
strlen((const char*)broken_list[i].file_path), broken_list[i].file_id, NULL);
tmp->resume = i;
debug("path:: %s\n", tmp->path);
return tmp;
}
}
}
return NULL;
}
static void utox_kill_file(FILE_TRANSFER *file, uint8_t us){
if (file->status == FILE_TRANSFER_STATUS_KILLED) {
debug("File already killed.\n");
return;
} else if(file->status == FILE_TRANSFER_STATUS_COMPLETED){
debug("File already completed.\n");
return;
} else {
file->status = FILE_TRANSFER_STATUS_KILLED;
}
utox_update_user_file(file);
if (!file->incoming && friend[file->friend_number].transfer_count) {
--friend[file->friend_number].transfer_count;
}
utox_cleanup_file_transfers(file->friend_number, file->file_number);
utox_file_free_resume(file->resume);
utox_file_save_active();
}
static void utox_break_file(FILE_TRANSFER *file){
if(file->status == FILE_TRANSFER_STATUS_NONE && file->resume){
return utox_kill_file(file, 1); /* We don't save unstarted files */
} else if(file->status == FILE_TRANSFER_STATUS_COMPLETED || file->status == FILE_TRANSFER_STATUS_KILLED) {
/* We don't touch these files! */
return;
}
file->status = FILE_TRANSFER_STATUS_BROKEN;
utox_update_user_file(file);
}
static void utox_pause_file(FILE_TRANSFER *file, uint8_t us){
switch(file->status){
case FILE_TRANSFER_STATUS_NONE:{
if(!file->incoming){
// New transfers start as paused them
file->status = FILE_TRANSFER_STATUS_PAUSED_THEM;
} else {
debug("FileTransfer:\tWe can't pause an unaccepted file!\n");
}
break;
}
case FILE_TRANSFER_STATUS_ACTIVE:{
if(us){
debug("FileTransfer:\tFile now paused by us.\n");
file->status = FILE_TRANSFER_STATUS_PAUSED_US;
} else {
debug("FileTransfer:\tFile now paused by them.\n");
file->status = FILE_TRANSFER_STATUS_PAUSED_THEM;
}
break;
}
case FILE_TRANSFER_STATUS_PAUSED_US:
case FILE_TRANSFER_STATUS_PAUSED_BOTH:
case FILE_TRANSFER_STATUS_PAUSED_THEM:{
if(us){
if(file->status == FILE_TRANSFER_STATUS_PAUSED_US){
debug("FileTransfer:\tFile already paused by us!\n");
} else if (file->status == FILE_TRANSFER_STATUS_PAUSED_THEM) {
file->status = FILE_TRANSFER_STATUS_PAUSED_BOTH;
debug("FileTransfer:\tFile now paused by both!\n");
} else if (file->status == FILE_TRANSFER_STATUS_PAUSED_BOTH) {
debug("FileTransfer:\tFile already paused by both!\n");
} else {
file->status = FILE_TRANSFER_STATUS_PAUSED_US;
}
} else {
if(file->status == FILE_TRANSFER_STATUS_PAUSED_US){
file->status = FILE_TRANSFER_STATUS_PAUSED_BOTH;
debug("FileTransfer:\tFile now paused by both!\n");
} else if (file->status == FILE_TRANSFER_STATUS_PAUSED_THEM) {
debug("FileTransfer:\tFile was already paused by them!\n");
} else if (file->status == FILE_TRANSFER_STATUS_PAUSED_BOTH) {
debug("FileTransfer:\tFile already paused by both!\n");
} else {
file->status = FILE_TRANSFER_STATUS_PAUSED_THEM;
}
}
break;
}
case FILE_TRANSFER_STATUS_BROKEN:{
debug("FileTransfer:\tCan't pause a broken file;\n");
break;
}
case FILE_TRANSFER_STATUS_COMPLETED:{
debug("FileTransfer:\tCan't pause a completed file;\n");
break;
}
case FILE_TRANSFER_STATUS_KILLED:{
debug("FileTransfer:\tCan't pause a killed file;\n");
break;
}
}
utox_update_user_file(file);
//TODO free not freed data.
}
static void utox_run_file(FILE_TRANSFER *file, uint8_t us){
if(file->status == FILE_TRANSFER_STATUS_ACTIVE){
return;
}
if(us){
if(file->status == FILE_TRANSFER_STATUS_NONE){
file->status = FILE_TRANSFER_STATUS_ACTIVE;
} else if(file->status == FILE_TRANSFER_STATUS_PAUSED_US){
file->status = FILE_TRANSFER_STATUS_ACTIVE;
} else if(file->status == FILE_TRANSFER_STATUS_PAUSED_BOTH) {
file->status = FILE_TRANSFER_STATUS_PAUSED_THEM;
} else if(file->status == FILE_TRANSFER_STATUS_PAUSED_THEM) {
// Do nothing;
} else {
debug("FileTransfer:\tTried to run outgoing file from an unknown state! (%u)\n", file->status);
}
} else {
if(file->status == FILE_TRANSFER_STATUS_PAUSED_US){
// Do nothing;
} else if(file->status == FILE_TRANSFER_STATUS_PAUSED_BOTH) {
file->status = FILE_TRANSFER_STATUS_PAUSED_US;
} else if(file->status == FILE_TRANSFER_STATUS_PAUSED_THEM) {
file->status = FILE_TRANSFER_STATUS_ACTIVE;
} else {
debug("FileTransfer:\tTried to run incoming file from an unknown state! (%u)\n", file->status);
}
}
utox_update_user_file(file);
debug("utox_run_file\n");
}
static void utox_complete_file(FILE_TRANSFER *file){
if(file->status == FILE_TRANSFER_STATUS_ACTIVE){
if(file->incoming){
if(file->in_memory){
if(file->is_avatar){
utox_incoming_avatar(file->friend_number, file->avatar, file->size);
file->avatar = NULL;
file->size = 0;
} else {
friend_recvimage(&friend[file->friend_number], (UTOX_PNG_IMAGE)file->memory, file->size);
}
} else { // Is a file
fclose(file->file);
file->ui_data->path = file->path;
}
} else {
if(file->in_memory){
// TODO, might want to do something here.
} else { // Is a file
fclose(file->file);
file->ui_data->path = file->path;
}
if(friend[file->friend_number].transfer_count){
/* Decrement if > 0 this might be better held inside local_control */
--friend[file->friend_number].transfer_count;
}
}
file->status = FILE_TRANSFER_STATUS_COMPLETED;
utox_update_user_file(file);
} else {
debug("FileTransfer:\tUnable to complete file in non-active state (file:%u)\n", file->file_number);
}
utox_file_free_resume(file->resume);
utox_cleanup_file_transfers(file->friend_number, file->file_number);
utox_file_save_active();
file->resume = 0; // We don't need to always be resetting this broken number anymore
}
static void utox_restart_file(Tox *tox, BROKEN_TRANSFER broken, uint8_t broken_number){
debug("FileTransfer:\tRestarting File\n");
if(broken.data){
outgoing_file_send_existing(tox, broken.data, broken_number);
} else {
debug("FileTransfer:\tWe are post-restart, rebuilding file from %u!\n", broken_number);
FILE_TRANSFER *tmp = malloc(sizeof(*tmp));
utox_build_file_transfer(tmp, broken.friend_number, broken.file_number, 0, 0, 0, 0, 3, NULL, 0,
broken.file_path, strlen((const char*)broken.file_path), broken.file_id, NULL);
tmp->resume = broken_number;
debug("path:: %s\n", tmp->path);
outgoing_file_send_existing(tox, tmp, broken_number);
}
}
void ft_friend_online(Tox *tox, uint32_t friend_number){
for(int i = 1; i < MAX_FILE_TRANSFERS; i++){
if( broken_list[i].used &&
broken_list[i].friend_number == friend_number &&
broken_list[i].incoming == 0){
utox_restart_file(tox, broken_list[i], i);
}
}
}
void ft_friend_offline(Tox *tox, uint32_t friend_number){
unsigned int i;
for (i = 0; i < MAX_FILE_TRANSFERS; ++i) {
utox_break_file(&incoming_transfer[friend_number][i]);
utox_break_file(&outgoing_transfer[friend_number][i]);
}
}
void file_transfer_local_control(Tox *tox, uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control){
TOX_ERR_FILE_CONTROL error = 0;
FILE_TRANSFER *info = get_file_transfer(friend_number, file_number);
switch(control){
case TOX_FILE_CONTROL_RESUME:
if(info->status != FILE_TRANSFER_STATUS_ACTIVE){
if(tox_file_control(tox, friend_number, file_number, control, &error)){
debug("FileTransfer:\tWe just resumed file (%u & %u)\n", friend_number, file_number);
} else {
debug("FileTransfer:\tToxcore doesn't like us! (%u & %u)\n", friend_number, file_number);
}
} else {
debug("FileTransfer:\tFile already active (%u & %u)\n", friend_number, file_number);
}
utox_run_file(info, 1);
break;
case TOX_FILE_CONTROL_PAUSE:
debug("FileTransfer:\tWe just paused file (%u & %u)\n", friend_number, file_number);
utox_pause_file(info, 1);
tox_file_control(tox, friend_number, file_number, control, &error);
break;
case TOX_FILE_CONTROL_CANCEL:
debug("FileTransfer:\tWe just killed file (%u & %u)\n", friend_number, file_number);
tox_file_control(tox, friend_number, file_number, control, &error);
utox_kill_file(info, 1);
break;
}
if(error){
if(error == TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED){
debug("FileTransfer:\tUnable to send command, Friend (%u) offline!\n", info->friend_number);
} else {
debug("FileTransfer:\tThere was an error(%u) sending the command, you probably want to see to that!\n", error);
}
debug("FileTransfer:\t\tFriend %u, and File %u. \n", friend_number, file_number);
} else {
utox_update_user_file(info);
}
}
static void file_transfer_callback_control(Tox *UNUSED(tox), uint32_t friend_number, uint32_t file_number, TOX_FILE_CONTROL control, void *UNUSED(userdata)){
FILE_TRANSFER *info = get_file_transfer(friend_number, file_number);
switch(control){
case TOX_FILE_CONTROL_RESUME:{
debug("FileTransfer:\tFriend (%i) has resumed file (%i)\n", friend_number, file_number);
utox_run_file(info, 0);
break;
}
case TOX_FILE_CONTROL_PAUSE:{
debug("FileTransfer:\tFriend (%i) has paused file (%i)\n", friend_number, file_number);
utox_pause_file(info, 0);
break;
}
case TOX_FILE_CONTROL_CANCEL:{
if(info->avatar){
debug("FileTransfer:\tFriend (%i) rejected avatar\n", friend_number);
} else {
debug("FileTransfer:\tFriend (%i) has canceled file (%i)\n", friend_number, file_number);
}
utox_kill_file(info, 0);
break;
}
}
}
static void utox_build_file_transfer(FILE_TRANSFER *ft, uint32_t friend_number, uint32_t file_number,
uint64_t file_size, _Bool incoming, _Bool in_memory, _Bool is_avatar, uint8_t kind, const uint8_t *name,
size_t name_length, const uint8_t *path, size_t path_length, const uint8_t *file_id, Tox *tox){
FILE_TRANSFER *file = ft;
memset(file, 0, sizeof(FILE_TRANSFER));
file->friend_number = friend_number;
file->file_number = file_number;
file->size = file_size;
file->incoming = incoming;
file->in_memory = in_memory;
file->is_avatar = is_avatar;
file->kind = kind;
if(name){
file->name = malloc(name_length + 1);
memcpy(file->name, name, name_length);
file->name_length = name_length;
file->name[file->name_length] = 0;
} else {
file->name = NULL;
file->name_length = 0;
}
if(path){
file->path = malloc(path_length + 1);
memcpy(file->path, path, path_length);
file->path_length = path_length;
file->path[file->path_length] = 0;
} else {
file->path = NULL;
file->path_length = 0;
}
if(file_id){
memcpy(file->file_id, file_id, TOX_FILE_ID_LENGTH);
} else {
tox_file_get_file_id(tox, friend_number, file_number, file->file_id, 0);
}
// TODO size correction error checking for this...
if(in_memory){
if (is_avatar){
file->avatar = calloc(file_size, sizeof(uint8_t));
} else {
file->memory = calloc(file_size, sizeof(uint8_t));
}
}
if(!incoming){
/* Outgoing file */
utox_pause_file(file, 1);
}
}
/* Function called by core with a new incoming file. */
static void incoming_file_avatar(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, void *UNUSED(user_data)){
debug("FileTransfer:\tNew Avatar from friend (%u)\n", friend_number);
/* If 0 unset the avatar... TODO! */
if(file_size == 0){
utox_incoming_avatar(friend_number, NULL, 0);
debug("FileTransfer:\tAvatar Unset from friend(%u).\n", friend_number);
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
return;
/* If incoming avatar is too big, cancel */
} else if (file_size > UTOX_AVATAR_MAX_DATA_LENGTH){
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
debug("FileTransfer:\tAvatar from friend(%u) rejected, TOO LARGE (%u)\n", friend_number, (uint32_t)file_size);
return;
}
uint8_t file_id[TOX_FILE_ID_LENGTH] = {0};
tox_file_get_file_id(tox, friend_number, file_number, file_id, 0);
if((friend[friend_number].avatar.format > 0) && memcmp(friend[friend_number].avatar.hash, file_id, TOX_HASH_LENGTH) == 0) {
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
debug("FileTransfer:\tAvatar from friend (%u) rejected: Same as Current\n", friend_number);
return;
}
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
if (!file_handle) {
tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
return;
}
utox_build_file_transfer(file_handle, friend_number, file_number, file_size, 1, 1, 1, TOX_FILE_KIND_AVATAR, NULL, 0, NULL, 0, file_id, tox);
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME);
}
static void incoming_file_existing(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t size, const uint8_t *filename, size_t filename_length, void *UNUSED(user_data)){
//new incoming file
debug("FileTransfer:\tIncoming Existing file from friend (%u) \n", friend_number);
uint8_t new_id[TOX_FILE_ID_LENGTH] = {0};
tox_file_get_file_id(tox, friend_number, file_number, new_id, 0);
FILE_TRANSFER *file_new = get_file_transfer(friend_number, file_number);
FILE_TRANSFER *file_existing = utox_file_find_existing(new_id);
FILE *file;
TOX_ERR_FILE_SEEK error = 0;
if (!file_new) {
tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
return;
}
if(file_existing){
uint8_t old_broken_number = file_existing->resume;
if( file_existing->friend_number == friend_number &&
file_existing->size == size &&
memcmp(file_existing->name, filename, filename_length) == 0 ){
// DO STUFF
utox_build_file_transfer(file_new, friend_number, file_number, size, 1, 0, 0, TOX_FILE_KIND_EXISTING,
filename, filename_length, NULL, 0, new_id, tox);
file_new->size_transferred = file_existing->size_transferred;
file_new->file = file_existing->file;
file_new->ui_data = file_existing->ui_data;
utox_file_free_resume(old_broken_number);
file_new->resume = utox_file_alloc_resume(tox, file_new);
tox_file_seek(tox, friend_number, file_number, file_existing->size_transferred, &error);
if(error){
debug("FileTransfer:\tError seeking new file.");
}
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME);
} else {
debug("FileTransfer:\tSomething didn't match, does that file still exist?\n");
file_new->path = file_existing->path;
file = fopen((char*)broken_list[old_broken_number].file_path, "rb");
uint64_t file_size = 0;
if(file){
debug("FileTransfer:\tCool file exists, let try to restart it.\n");
fseeko(file, 0, SEEK_END);
file_size = ftello(file);
fseeko(file, 0, SEEK_SET);
fclose(file);
if(file_size >= size){
// This is a case we don't want to touch... Just abort.
debug("FileTransfer:\tResume size mismatch, aborting!");
} else {
file = fopen((const char*)file_new->path, "ab");
if(file){
utox_build_file_transfer(file_new, friend_number, file_number, size, 1, 0, 0,
TOX_FILE_KIND_DATA, filename, filename_length, file_new->path, file_existing->path_length, new_id, tox);
file_new->file = file;
file_new->size_transferred = file_size;
file_new->ui_data = message_add_type_file(file_new);
file_new->resume = utox_file_alloc_resume(tox, file_new);
debug("Resume %i\n", file_new->resume);
tox_file_seek(tox, friend_number, file_number, file_new->size_transferred, &error);
postmessage(FRIEND_FILE_NEW, 0, 0, file_new);
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME);
} else {
debug("FileTransfer:\tFile opened for reading, but unable to write.\n");
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
}
}
} else {
debug("FileTransfer:\tUnable to open that file; treating as new.\n");
utox_build_file_transfer(file_new, friend_number, file_number, size, 1, 0, 0, TOX_FILE_KIND_DATA,
filename, filename_length, NULL, 0, new_id, tox);
file_new->ui_data = message_add_type_file(file_new);
file_new->resume = utox_file_alloc_resume(tox, file_new);
postmessage(FRIEND_FILE_NEW, 0, 0, file_new);
}
utox_file_free_resume(old_broken_number);
}
} else {
debug("FileTransfer:\tWe don't know anything about this file, so for safety we're just going to reject it!.\n");
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
}
}
/* Function called by core with a new incoming file. */
static void incoming_file_callback_request(Tox *tox, 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){
if(kind == TOX_FILE_KIND_AVATAR){
incoming_file_avatar(tox, friend_number, file_number, kind, file_size, filename, filename_length, user_data);
return;
} else if (kind == TOX_FILE_KIND_EXISTING){
incoming_file_existing(tox, friend_number, file_number, kind, file_size, filename, filename_length, user_data);
return;
} else {
debug("FileTransfer:\tNew incoming file from friend (%u) file number (%u)\nFileTransfer:\t\tfilename: %s\n", friend_number, file_number, filename);
}
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
if (!file_handle) {
tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL, 0);
return;
}
if(file_size < 1024 * 1024 * 4 && filename_length == (sizeof("utox-inline.png") - 1) &&
memcmp(filename, "utox-inline.png", filename_length) == 0) {
utox_build_file_transfer(file_handle, friend_number, file_number, file_size, 1, 1, 0, TOX_FILE_KIND_DATA,
filename, filename_length, NULL, 0, NULL, tox);
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME);
} else {
utox_build_file_transfer(file_handle, friend_number, file_number, file_size, 1, 0, 0, TOX_FILE_KIND_DATA,
filename, filename_length, NULL, 0, NULL, tox);
file_handle->ui_data = message_add_type_file(file_handle);
file_handle->resume = utox_file_alloc_resume(tox, file_handle);
postmessage(FRIEND_FILE_NEW, 0, 0, file_handle);
}
}
static void incoming_file_callback_chunk(Tox *UNUSED(tox), uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, void *UNUSED(user_data)){
// debug("FileTransfer:\tIncoming chunk friend(%u), file(%u), start(%u), end(%u), \n", friend_number, file_number, position, length);
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
if(length == 0){
debug("\nFileTransfer:\tIncoming transfer is done (%u & %u)\n", friend_number, file_number);
utox_complete_file(file_handle);
return;
}
if(file_handle->in_memory) {
if(file_handle->is_avatar){
memcpy(file_handle->avatar + position, data, length);
} else {
memcpy(file_handle->memory + position, data, length);
}
} else {
// Removed until we can better implement temp files
// if(file_handle->in_tmp_loc){
// fseeko(file_handle->tmp_file, position, SEEK_SET);
// size_t write_size = fwrite(data, 1, length, file_handle->tmp_file);
// if(write_size != length){
// debug("\n\nFileTransfer:\tERROR WRITING DATA TO TEMP FILE! (%u & %u)\n\n", friend_number, file_number);
// tox_postmessage(TOX_FILE_INCOMING_CANCEL, friend_number, file_number, NULL);
// return;
// }
// }
if(file_handle->file) {
while(!file_lock(file_handle->file, position, length)){
debug("FileTransfer:\tCan't get lock, sleeping...\n");
yieldcpu(10);
// If you get a bug report about this hanging utox, just disable it, it's unlikely to be needed!
}
fseeko(file_handle->file, position, SEEK_SET);
size_t write_size = fwrite(data, 1, length, file_handle->file);
fflush(file_handle->file);
file_unlock(file_handle->file, position, length);
if(write_size != length){
debug("\n\nFileTransfer:\tERROR WRITING DATA TO FILE! (%u & %u)\n\n", friend_number, file_number);
tox_postmessage(TOX_FILE_INCOMING_CANCEL, friend_number, file_number, NULL);
return;
}
} else {
debug("FileTransfer:\tFile Handle failed!\n");
tox_postmessage(TOX_FILE_INCOMING_CANCEL, friend_number, file_number, NULL);
return;
}
}
file_handle->size_transferred += length;
// TODO dirty hack, this needs to be replaced
if(file_handle->resume){
broken_list[file_handle->resume].data->size_transferred = file_handle->size_transferred;
broken_list[file_handle->resume].data->file = file_handle->file;
}
calculate_speed(file_handle);
}
void outgoing_file_send_new(Tox *tox, uint32_t friend_number, uint8_t *path, const uint8_t *filename, size_t filename_length){
debug("FileTransfer:\tStarting outgoing file to friend %u. (filename, %s)\n", friend_number, filename);
// FILE_TRANSFER *file_handle = active_transfer[friend_number][file_number]; TODO
if(friend[friend_number].transfer_count >= MAX_FILE_TRANSFERS) {
debug("FileTransfer:\tMaximum outgoing file sending limit reached(%u/%u) for friend(%u). ABORTING!\n",
friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
return;
}
FILE *file = fopen((char*)path, "rb");
if(!file) {
debug("FileTransfer:\tUnable to open file for reading!\n");
return;
}
TOX_ERR_FILE_SEND error;
const uint8_t *file_id = NULL;
uint64_t file_size = 0;
fseeko(file, 0, SEEK_END);
file_size = ftello(file);
fseeko(file, 0, SEEK_SET);
int file_number = tox_file_send(tox, friend_number, TOX_FILE_KIND_DATA, file_size, file_id, filename, filename_length, &error);
if(file_number != -1) {
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
utox_build_file_transfer(file_handle, friend_number, file_number, file_size, 0, 0, 0, TOX_FILE_KIND_DATA,
filename, filename_length, path, strlen((char*)path), NULL, tox);
file_handle->file = file;
if(!file_handle->file) {
debug("FileTransfer:\tUnable to regain file for reading!\n");
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
return;
}
file_handle->ui_data = message_add_type_file(file_handle);
file_handle->resume = utox_file_alloc_resume(tox, file_handle);
// TODO move this to utox_file_run(ish)
++friend[friend_number].transfer_count;
debug("Sending file %d of %d(max) to friend(%d).\n", friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
// Create a new msg for the UI and save it's pointer
postmessage(FRIEND_FILE_NEW, 0, 0, file_handle);
} else {
debug("tox_file_send() failed\n");
}
}
void outgoing_file_send_existing(Tox *tox, FILE_TRANSFER *broken_data, uint8_t broken_number){
debug("FileTransfer:\tRestarting outgoing file to friend %u. (filename, %s)\n", broken_data->friend_number, broken_data->path);
if(friend[broken_data->friend_number].transfer_count >= MAX_FILE_TRANSFERS) {
debug("FileTransfer:\tMaximum outgoing file sending limit reached(%u/%u) for friend(%u). ABORTING!\n",
friend[broken_data->friend_number].transfer_count, MAX_FILE_TRANSFERS, broken_data->friend_number);
utox_file_free_resume(broken_data->resume);
return;
}
if(!broken_data->file) {
debug("FileTransfer:\tUnable to open file for reading, trying to re-access!\n");
if(broken_data->path_length){
FILE *file = fopen((const char*)broken_data->path, "rb");
if(file){
broken_data->file = file;
fseeko(file, 0, SEEK_END);
broken_data->size = ftello(file);
fseeko(file, 0, SEEK_SET);
} else {
debug("FileTransfer:\tUnable to re-access file, must fail!\n");
file_transfer_local_control(tox, broken_data->friend_number, broken_data->file_number, TOX_FILE_CONTROL_CANCEL);
return;
}
}
}
TOX_ERR_FILE_SEND error;
const uint8_t *file_id = broken_data->file_id;
uint8_t *p = broken_data->path, *name = broken_data->path;
while(*p != '\0') {
if(*p == '/' || *p == '\\') {
name = p + 1;
}
p++;
}
int new_file_number = tox_file_send(tox, broken_data->friend_number, TOX_FILE_KIND_EXISTING, broken_data->size, file_id, name, p - name, &error);
if(new_file_number != -1) {
FILE_TRANSFER *file_handle = get_file_transfer(broken_data->friend_number, new_file_number);
utox_build_file_transfer(file_handle, broken_data->friend_number, new_file_number, broken_data->size, 0, 0, 0,
TOX_FILE_KIND_EXISTING, name, p - name, broken_data->path, broken_data->path_length, file_id, tox);
file_handle->file = broken_data->file;
if(broken_data->ui_data){
file_handle->ui_data = broken_data->ui_data;
} else {
file_handle->ui_data = message_add_type_file(file_handle);
}
utox_file_free_resume(broken_number);
file_handle->resume = utox_file_alloc_resume(tox, file_handle);
++friend[file_handle->friend_number].transfer_count;
debug("Resending file %d of %d(max) to friend(%d).\n", friend[file_handle->friend_number].transfer_count, MAX_FILE_TRANSFERS, file_handle->friend_number);
utox_update_user_file(file_handle);
} else {
debug("tox_file_send() failed\n");
}
free(broken_data);
}
void outgoing_file_send_inline(Tox *tox, uint32_t friend_number, uint8_t *image, size_t image_size){
debug("FileTransfer:\tStarting outgoing inline to friend %u.\n", friend_number);
if(friend[friend_number].transfer_count >= MAX_FILE_TRANSFERS) {
debug("FileTransfer:\tMaximum outgoing file limit reached(%u/%u) for friend(%u). ABORTING!\n", friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
return;
}
if(!image) {
debug("FileTransfer:\tUnable to use *image!\n");
return;
}
TOX_ERR_FILE_SEND error;
uint8_t file_id[TOX_FILE_ID_LENGTH] = {0};
uint8_t *filename;
size_t filename_length = 0;
if(!tox_hash(file_id, image, image_size)){
debug("FileTransfer:\tUnable to get hash for image!\n");
return;
}
if( image_size < 1024 * 1024 * 4 ) {
filename_length = sizeof("utox-inline.png") - 1;
filename = malloc(filename_length + 1);
memcpy(filename, "utox-inline.png", filename_length);
} else {
filename_length = sizeof("utox-image.png") - 1;
filename = malloc(filename_length + 1);
memcpy(filename, "utox-image.png", filename_length);
}
filename[filename_length] = 0; // null term for the string // TODO WRAP THIS INTO BUILD
uint32_t file_number = tox_file_send(tox, friend_number, TOX_FILE_KIND_DATA, image_size, file_id, (const uint8_t*)filename, filename_length, &error);
if(file_number != -1) {
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
utox_build_file_transfer(file_handle, friend_number, file_number, image_size, 0, 1, 0, TOX_FILE_KIND_DATA,
file_id, TOX_FILE_ID_LENGTH, NULL, 0, file_id, tox);
file_handle->kind = TOX_FILE_KIND_DATA;
file_handle->resume = 0;
memcpy(file_handle->memory, image, image_size);
utox_pause_file(file_handle, 1);
++friend[friend_number].transfer_count;
debug("FileTransfer:\tSending image file %d of %d(max) to friend(%d).\n", friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
// Create a new msg for the UI and save it's pointer
} else {
free(image);
free(filename);
debug("tox_file_send() failed for image\n");
return;
}
free(filename);
return;
}
int outgoing_file_send_avatar(Tox *tox, uint32_t friend_number, uint8_t *avatar, size_t avatar_size){
debug("FileTransfer:\tStarting avatar to friend %u.\n", friend_number);
if(friend[friend_number].transfer_count >= MAX_FILE_TRANSFERS) {
debug("FileTransfer:\tMaximum outgoing file sending limit reached(%u/%u) for friend(%u). ABORTING!\n",
friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
return 1;
}
if(!avatar && avatar_size) {
debug("FileTransfer:\tUnable to use *avatar!\n");
return 1;
}
TOX_ERR_FILE_SEND error;
uint8_t file_id[TOX_HASH_LENGTH] = {0};
if(!tox_hash(file_id, avatar, avatar_size)){
debug("FileTransfer:\tUnable to get hash for avatar!\n");
return 1;
}
int file_number = tox_file_send(tox, friend_number, TOX_FILE_KIND_AVATAR, avatar_size, file_id, NULL, 0, &error);
if(file_number != -1) {
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
utox_build_file_transfer(file_handle, friend_number, file_number, avatar_size, 0, 1, 1, TOX_FILE_KIND_AVATAR,
NULL, 0, NULL, 0, (const uint8_t*)file_id, tox);
file_handle->kind = TOX_FILE_KIND_AVATAR;
file_handle->status = FILE_TRANSFER_STATUS_PAUSED_THEM;
file_handle->resume = 0;
memcpy(file_handle->avatar, avatar, avatar_size);
++friend[friend_number].transfer_count;
// Create a new msg for the UI and save it's pointer
} else {
debug("tox_file_send() failed for avatar\n");
return 1;
}
return 0;
}
static void outgoing_file_callback_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, void *UNUSED(user_data)){
// debug("FileTransfer:\tChunk requested for friend_id (%u), and file_id (%u). Start (%lu), End (%zu).\r", friend_number, file_number, position, length);
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
if(length == 0){
debug("FileTransfer:\tOutgoing transfer is done (%u & %u)\n", friend_number, file_number);
utox_complete_file(file_handle);
return;
}
uint8_t buffer[length];
size_t read_size = 0;
if(file_handle->in_memory){
// Memory
if(file_handle->is_avatar){
if(file_handle->size < length){
debug("FileTransfer:\tAvatar size mismatch!\n");
return;
}
memcpy(buffer, file_handle->avatar + position, length);
read_size = length; /* We hope!! */
} else {
if(file_handle->size < length){
debug("FileTransfer:\tMemory size mismatch!\n");
return;
}
memcpy(buffer, file_handle->memory + position, length);
read_size = length; /* We hope!! */
}
} else {
// File
FILE *file = file_handle->file;
if(file){
fseeko(file, position, SEEK_SET);
read_size = fread(buffer, 1, length, file);
// debug("File okay\n");
}
}
if(read_size != length){
debug("FileTransfer:\tERROR READING FILE! (%u & %u)\n", friend_number, file_number);
debug("FileTransfer:\t\tSize (%lu), Position (%lu), Length(%zu), Read_size (%zu), Size_transferred (%zu).\n",
file_handle->size, position, length, read_size, file_handle->size_transferred);
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_CANCEL);
return;
}
TOX_ERR_FILE_SEND_CHUNK error;
tox_file_send_chunk(tox, friend_number, file_number, position, buffer, length, &error);
file_handle->size_transferred += length;
calculate_speed(file_handle);
}
int utox_file_start_write(uint32_t friend_number, uint32_t file_number, void *filepath){
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
file_handle->file = fopen(filepath, "wb");
if(!file_handle->file) {
debug("FileTransfer:\tThe file we're supposed to write to couldn't be opened\n");
free(filepath);
file_handle->status = FILE_TRANSFER_STATUS_BROKEN;
return -1;
}
file_handle->path = filepath;
file_handle->path_length = strlen(filepath);
memcpy(broken_list[file_handle->resume].file_path, file_handle->path, file_handle->path_length);
// Removed until we can find a better way of working this in;
// if(file_handle->in_tmp_loc){
// fseeko(file_handle->tmp_file, 0, SEEK_SET);
// fseeko(file_handle->file, 0, SEEK_SET);
// fwrite(file_handle->tmp_file, 1, file_handle->size_transferred, file_handle->file);
// fclose(file_handle->tmp_file);
// file_handle->in_tmp_loc = 0;
// // free(file_handle->tmp_path); // Freed by xlib probably...
// // TODO unlink();
// debug("FileTransfer: Data copied from tmp_file to save_file\n");
// }
return 0;