forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tox_transfer.c
364 lines (292 loc) · 10.9 KB
/
tox_transfer.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
#include "main.h"
static FILE_T *file_t[256], **file_tend = file_t;
static void fillbuffer(FILE_T *ft)
{
if(ft->inline_png) {
ft->buffer += ft->sendsize;
if(ft->total - ft->bytes <= ft->sendsize) {
ft->buffer_bytes = ft->total - ft->bytes;
ft->bytes = ft->total;
ft->finish = 1;
return;
}
ft->buffer_bytes = ft->sendsize;
ft->bytes += ft->sendsize;
return;
}
ft->buffer_bytes = fread(ft->buffer, 1, ft->sendsize, ft->data);
ft->bytes += ft->buffer_bytes;
ft->finish = (ft->bytes >= ft->total) || (feof((FILE*)ft->data) != 0);
}
// Outgoing transfer functions
void utox_transfer_start_file(Tox *tox, uint32_t fid, uint8_t *path, uint8_t *name, uint16_t name_length)
{
debug("Sending: %s\n", path);
if (friend[fid].count_outgoing >= MAX_FILE_TRANSFERS || (file_tend - file_t) >= countof(file_t)) {
debug("Maximum outgoing file sending limit reached.\n");
return;
}
FILE *file = fopen((char*)path, "rb");
if(!file) {
return;
}
uint64_t size;
fseeko(file, 0, SEEK_END);
size = ftello(file);
fseeko(file, 0, SEEK_SET);
int filenumber = tox_new_file_sender(tox, fid, size, name, name_length);
if(filenumber != -1) {
FILE_T *ft = &friend[fid].outgoing[filenumber];
memset(ft, 0, sizeof(FILE_T));
*file_tend++ = ft;
ft->fid = fid;
ft->filenumber = filenumber;
name_length = name_length > sizeof(ft->name) ? sizeof(ft->name) : name_length;
ft->name_length = name_length;
ft->status = FT_PENDING;
ft->sendsize = tox_file_data_size(tox, fid);
memcpy(ft->name, name, name_length);
ft->total = size;
ft->path = (uint8_t*)strdup((char*)path);
ft->data = file;
ft->buffer = malloc(ft->sendsize);
fillbuffer(ft);
postmessage(FRIEND_FILE_OUT_NEW, fid, filenumber, NULL);
++friend[fid].count_outgoing;
} else {
fclose(file);
debug("tox_new_file_sender() failed\n");
}
}
void utox_transfer_start_memory(Tox *tox, uint16_t fid, void *pngdata, size_t size)
{
if (friend[fid].count_outgoing >= MAX_FILE_TRANSFERS || (file_tend - file_t) >= countof(file_t)) {
debug("Maximum outgoing file sending limit reached.\n");
return;
}
int filenumber = tox_new_file_sender(tox, fid, size, (uint8_t*)"inline.png", sizeof("inline.png") - 1);
if(filenumber != -1) {
FILE_T *ft = &friend[fid].outgoing[filenumber];
memset(ft, 0, sizeof(FILE_T));
*file_tend++ = ft;
ft->fid = fid;
ft->filenumber = filenumber;
ft->inline_png = 1;
ft->status = FT_PENDING;
ft->sendsize = tox_file_data_size(tox, fid);
memcpy(ft->name, "inline.png", sizeof("inline.png") - 1);
ft->name_length = sizeof("inline.png") - 1;
ft->total = size;
ft->data = pngdata;
ft->buffer = ft->data;
if(ft->total <= ft->sendsize) {
ft->bytes = ft->total;
ft->buffer_bytes = ft->total;
ft->finish = 1;
} else {
ft->bytes = ft->sendsize;
ft->buffer_bytes = ft->sendsize;
}
postmessage(FRIEND_FILE_OUT_NEW_INLINE, fid, filenumber, NULL);
++friend[fid].count_outgoing;
} else {
free(pngdata);
}
}
static void resetft(Tox *UNUSED(tox), FILE_T *ft, uint64_t start)
{
if(start >= ft->total) {
return;
}
if(ft->inline_png) {
ft->buffer = ft->data;
} else {
fseek(ft->data, start, SEEK_SET);
fillbuffer(ft);
}
if(ft->status == FT_NONE) {
*file_tend++ = ft;
}
ft->bytes = start;
ft->status = FT_SEND;
}
// Tox file callbacks
static void callback_file_send_request(Tox *tox, int32_t fid, uint8_t filenumber, uint64_t filesize, const uint8_t *filename, uint16_t filename_length, void *UNUSED(userdata))
{
if(filenumber > countof(friend[0].incoming)) {
tox_file_send_control(tox, fid, 1, filenumber, TOX_FILECONTROL_KILL, NULL, 0);
return;
}
FILE_T *ft = &friend[fid].incoming[filenumber];
memset(ft, 0, sizeof(FILE_T));
ft->fid = fid;
ft->filenumber = filenumber;
ft->total = filesize;
filename_length = filename_length > sizeof(ft->name) ? sizeof(ft->name) : filename_length;
filename_length = utf8_validate(filename, filename_length);
ft->name_length = filename_length;
memcpy(ft->name, filename, filename_length);
debug("File Request %.*s\n", filename_length, filename);
if(filesize < 1024 * 1024 *4 && filename_length == sizeof("inline.png") - 1 && memcmp(filename, "inline.png", filename_length) == 0) {
ft->inline_png = 1;
ft->status = FT_SEND;
ft->data = malloc(filesize);
tox_file_send_control(tox, fid, 1, filenumber, TOX_FILECONTROL_ACCEPT, NULL, 0);
//postmessage(FRIEND_FILE_IN_STATUS, fid, filenumber, (void*)FILE_OK);
postmessage(FRIEND_FILE_IN_NEW_INLINE, fid, filenumber, NULL);
} else {
postmessage(FRIEND_FILE_IN_NEW, fid, filenumber, NULL);
}
}
static void callback_file_control(Tox *tox, int32_t fid, uint8_t receive_send, uint8_t filenumber, uint8_t control, const uint8_t *data, uint16_t length, void *UNUSED(userdata))
{
FILE_T *ft = (receive_send) ? &friend[fid].outgoing[filenumber] : &friend[fid].incoming[filenumber];
switch(control) {
case TOX_FILECONTROL_ACCEPT: {
ft->status = FT_SEND;
debug("FileAccepted %u\n", filenumber);
postmessage(FRIEND_FILE_IN_STATUS + receive_send, fid, filenumber, (void*)FILE_OK);
break;
}
case TOX_FILECONTROL_KILL: {
ft->status = receive_send ? FT_KILL : FT_NONE;
if(!receive_send) {
if(ft->data) {
if(ft->inline_png) {
free(ft->data);
} else {
fclose(ft->data);
free(ft->path);
}
}
postmessage(FRIEND_FILE_IN_STATUS, fid, filenumber, (void*)FILE_KILLED);
}
break;
}
case TOX_FILECONTROL_PAUSE: {
if(ft->status == FT_SEND) {
ft->status = FT_PAUSE;
postmessage(FRIEND_FILE_IN_STATUS + receive_send, fid, filenumber, (void*)FILE_PAUSED_OTHER);
} else if(ft->status == FT_PAUSE) {
ft->status = FT_SEND;
postmessage(FRIEND_FILE_IN_STATUS + receive_send, fid, filenumber, (void*)FILE_OK);
}
break;
}
case TOX_FILECONTROL_FINISHED: {
if(!receive_send) {
ft->status = FT_NONE;
if(ft->inline_png) {
postmessage(FRIEND_FILE_IN_DONE_INLINE, fid, filenumber, ft->data);
} else {
fclose(ft->data);
postmessage(FRIEND_FILE_IN_DONE, fid, filenumber, ft->path);
}
tox_file_send_control(tox, fid, 1, filenumber, TOX_FILECONTROL_FINISHED, NULL, 0);
} else {
if(ft->status == FT_NONE) {
if(!ft->inline_png) {
fclose(ft->data);
free(ft->buffer);
postmessage(FRIEND_FILE_OUT_DONE, fid, filenumber, ft->path);
} else {
postmessage(FRIEND_FILE_OUT_DONE, fid, filenumber, ft->data);
}
}
}
break;
}
case TOX_FILECONTROL_RESUME_BROKEN: {
if(receive_send && length == 8) {
resetft(tox, ft, *(uint64_t*)data);
tox_file_send_control(tox, fid, 0, filenumber, TOX_FILECONTROL_ACCEPT, NULL, 0);
postmessage(FRIEND_FILE_IN_STATUS + receive_send, fid, filenumber, (void*)FILE_OK);
}
break;
}
}
debug("File Control\n");
}
static void callback_file_data(Tox *UNUSED(tox), int32_t fid, uint8_t filenumber, const uint8_t *data, uint16_t length, void *UNUSED(userdata))
{
FILE_T *ft = &friend[fid].incoming[filenumber];
if(ft->inline_png) {
memcpy(ft->data + ft->bytes, data, length);
} else {
fwrite(data, 1, length, ft->data);
}
ft->bytes += length;
uint64_t time = get_time();
if(time - ft->lastupdate >= 1000 * 1000 * 50) {
FILE_PROGRESS *p = malloc(sizeof(FILE_PROGRESS));
p->bytes = ft->bytes;
p->speed = ((ft->bytes - ft->lastprogress) * 1000 * 1000 * 1000) / (time - ft->lastupdate);
postmessage(FRIEND_FILE_IN_PROGRESS, fid, filenumber, p);
ft->lastupdate = time;
ft->lastprogress = ft->bytes;
}
}
void utox_set_callbacks_for_transfer(Tox *tox)
{
tox_callback_file_send_request(tox, callback_file_send_request, NULL);
tox_callback_file_control(tox, callback_file_control, NULL);
tox_callback_file_data(tox, callback_file_data, NULL);
}
// Called from tox_thread main loop to do transfer work.
// Tox API can be called directly.
void utox_thread_work_for_transfers(Tox *tox, uint64_t time)
{
FILE_T **p;
for(p = file_t; p < file_tend; p++) {
FILE_T *ft = *p;
if(!ft) {
continue;
}
switch(ft->status) {
case FT_SEND: {
while(tox_file_send_data(tox, ft->fid, ft->filenumber, ft->buffer, ft->buffer_bytes) != -1) {
if(time - ft->lastupdate >= 1000 * 1000 * 50 || ft->bytes - ft->lastprogress >= 1024 * 1024) {
FILE_PROGRESS *fp = malloc(sizeof(FILE_PROGRESS));
fp->bytes = ft->bytes;
fp->speed = (time - ft->lastupdate) == 0 ? 0 : ((ft->bytes - ft->lastprogress) * 1000 * 1000 * 1000) / (time - ft->lastupdate);
postmessage(FRIEND_FILE_OUT_PROGRESS, ft->fid, ft->filenumber, fp);
ft->lastupdate = time;
ft->lastprogress = ft->bytes;
}
if(ft->finish) {
tox_file_send_control(tox, ft->fid, 0, ft->filenumber, TOX_FILECONTROL_FINISHED, NULL, 0);
memmove(p, p + 1, ((void*)file_tend - (void*)(p + 1)));
p--;
file_tend--;
--friend[ft->fid].count_outgoing;
ft->status = FT_NONE;
break;
}
fillbuffer(ft);
}
break;
}
case FT_KILL: {
if(ft->inline_png) {
free(ft->data);
} else {
fclose(ft->data);
free(ft->buffer);
}
memmove(p, p + 1, ((void*)file_tend - (void*)(p + 1)));
p--;
file_tend--;
--friend[ft->fid].count_outgoing;
postmessage(FRIEND_FILE_OUT_STATUS, ft->fid, ft->filenumber, (void*)FILE_KILLED);
break;
}
case FT_NONE: {
memmove(p, p + 1, ((void*)file_tend - (void*)(p + 1)));
p--;
file_tend--;
--friend[ft->fid].count_outgoing;
break;
}
}
}
}