summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c137
-rw-r--r--toxcore/Messenger.h14
2 files changed, 107 insertions, 44 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 6035fcb7..ac79cde1 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -948,10 +948,11 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
948 948
949/* Set the callback for file control requests. 949/* Set the callback for file control requests.
950 * 950 *
951 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 951 * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
952 * 952 *
953 */ 953 */
954void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t, 954void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
955 uint16_t,
955 void *), void *userdata) 956 void *), void *userdata)
956{ 957{
957 m->file_filecontrol = function; 958 m->file_filecontrol = function;
@@ -1026,13 +1027,15 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f
1026} 1027}
1027 1028
1028/* Send a file control request. 1029/* Send a file control request.
1030 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
1029 * 1031 *
1030 * return 1 on success 1032 * return 1 on success
1031 * return 0 on failure 1033 * return 0 on failure
1032 */ 1034 */
1033int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) 1035int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
1036 uint8_t *data, uint16_t length)
1034{ 1037{
1035 if (length > MAX_DATA_SIZE - 2) 1038 if (length > MAX_DATA_SIZE - 3)
1036 return 0; 1039 return 0;
1037 1040
1038 if (friend_not_valid(m, friendnumber)) 1041 if (friend_not_valid(m, friendnumber))
@@ -1041,26 +1044,46 @@ int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t mes
1041 if (m->friendlist[friendnumber].file_receiving[filenumber].status == 0) 1044 if (m->friendlist[friendnumber].file_receiving[filenumber].status == 0)
1042 return 0; 1045 return 0;
1043 1046
1047 if (send_receive > 1)
1048 return 0;
1049
1044 uint8_t packet[MAX_DATA_SIZE]; 1050 uint8_t packet[MAX_DATA_SIZE];
1045 packet[0] = filenumber; 1051 packet[0] = send_receive;
1046 packet[1] = message_id; 1052 packet[1] = filenumber;
1047 memcpy(packet + 2, data, length); 1053 packet[2] = message_id;
1054 memcpy(packet + 3, data, length);
1055
1056 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, length + 3)) {
1057 if (send_receive == 1)
1058 switch (message_id) {
1059 case FILECONTROL_ACCEPT:
1060 m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
1061 break;
1048 1062
1049 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, length + 2)) { 1063 case FILECONTROL_PAUSE:
1050 switch (message_id) { 1064 m->friendlist[friendnumber].file_receiving[filenumber].status = 5;
1051 case FILECONTROL_ACCEPT: 1065 break;
1052 m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
1053 break;
1054 1066
1055 case FILECONTROL_PAUSE: 1067 case FILECONTROL_KILL:
1056 m->friendlist[friendnumber].file_receiving[filenumber].status = 2; 1068 case FILECONTROL_FINISHED:
1057 break; 1069 m->friendlist[friendnumber].file_receiving[filenumber].status = 0;
1070 break;
1071 }
1072 else
1073 switch (message_id) {
1074 case FILECONTROL_ACCEPT:
1075 m->friendlist[friendnumber].file_sending[filenumber].status = 3;
1076 break;
1058 1077
1059 case FILECONTROL_KILL: 1078 case FILECONTROL_PAUSE:
1060 case FILECONTROL_FINISHED: 1079 m->friendlist[friendnumber].file_sending[filenumber].status = 5;
1061 m->friendlist[friendnumber].file_receiving[filenumber].status = 0; 1080 break;
1062 break; 1081
1063 } 1082 case FILECONTROL_KILL:
1083 case FILECONTROL_FINISHED:
1084 m->friendlist[friendnumber].file_sending[filenumber].status = 0;
1085 break;
1086 }
1064 1087
1065 return 1; 1088 return 1;
1066 } else { 1089 } else {
@@ -1141,25 +1164,61 @@ static void break_files(Messenger *m, int friendnumber)
1141 } 1164 }
1142} 1165}
1143 1166
1144static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, 1167static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber,
1168 uint8_t message_id, uint8_t *data,
1145 uint16_t length) 1169 uint16_t length)
1146{ 1170{
1147 if (m->friendlist[friendnumber].file_sending[filenumber].status == 0) 1171 if (send_receive > 1)
1148 return -1; 1172 return -1;
1149 1173
1150 switch (message_id) { 1174 if (send_receive == 0) {
1151 case FILECONTROL_ACCEPT: 1175 if (m->friendlist[friendnumber].file_receiving[filenumber].status == 0)
1152 m->friendlist[friendnumber].file_sending[filenumber].status = 3; 1176 return -1;
1153 return 0;
1154 1177
1155 case FILECONTROL_PAUSE: 1178 switch (message_id) {
1156 m->friendlist[friendnumber].file_sending[filenumber].status = 2; 1179 case FILECONTROL_ACCEPT:
1157 return 0; 1180 if (m->friendlist[friendnumber].file_receiving[filenumber].status != 5) {
1181 m->friendlist[friendnumber].file_receiving[filenumber].status = 3;
1182 return 0;
1183 }
1158 1184
1159 case FILECONTROL_KILL: 1185 return -1;
1160 case FILECONTROL_FINISHED: 1186
1161 m->friendlist[friendnumber].file_sending[filenumber].status = 0; 1187 case FILECONTROL_PAUSE:
1162 return 0; 1188 if (m->friendlist[friendnumber].file_receiving[filenumber].status != 5) {
1189 m->friendlist[friendnumber].file_receiving[filenumber].status = 2;
1190 return 0;
1191 }
1192
1193 return -1;
1194
1195 case FILECONTROL_KILL:
1196 case FILECONTROL_FINISHED:
1197 m->friendlist[friendnumber].file_receiving[filenumber].status = 0;
1198 return 0;
1199 }
1200 } else {
1201 if (m->friendlist[friendnumber].file_sending[filenumber].status == 0)
1202 return -1;
1203
1204 switch (message_id) {
1205 case FILECONTROL_ACCEPT:
1206 if (m->friendlist[friendnumber].file_sending[filenumber].status != 5) {
1207 m->friendlist[friendnumber].file_sending[filenumber].status = 3;
1208 return 0;
1209 }
1210
1211 return -1;
1212
1213 case FILECONTROL_PAUSE:
1214 m->friendlist[friendnumber].file_sending[filenumber].status = 2;
1215 return 0;
1216
1217 case FILECONTROL_KILL:
1218 case FILECONTROL_FINISHED:
1219 m->friendlist[friendnumber].file_sending[filenumber].status = 0;
1220 return 0;
1221 }
1163 } 1222 }
1164 1223
1165 return -1; 1224 return -1;
@@ -1468,17 +1527,19 @@ void doFriends(Messenger *m)
1468 } 1527 }
1469 1528
1470 case PACKET_ID_FILE_CONTROL: { 1529 case PACKET_ID_FILE_CONTROL: {
1471 if (data_length < 2) 1530 if (data_length < 3)
1472 break; 1531 break;
1473 1532
1474 uint8_t filenumber = data[0]; 1533 uint8_t send_receive = data[0];
1475 uint8_t control_type = data[1]; 1534 uint8_t filenumber = data[1];
1535 uint8_t control_type = data[2];
1476 1536
1477 if (handle_filecontrol(m, i, filenumber, control_type, data + 2, data_length - 2) == -1) 1537 if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3) == -1)
1478 break; 1538 break;
1479 1539
1480 if (m->file_filecontrol) 1540 if (m->file_filecontrol)
1481 (*m->file_filecontrol)(m, i, filenumber, control_type, data + 2, data_length - 2, m->file_filecontrol_userdata); 1541 (*m->file_filecontrol)(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3,
1542 m->file_filecontrol_userdata);
1482 1543
1483 break; 1544 break;
1484 } 1545 }
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 8e635afe..40b2fac2 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -102,7 +102,7 @@ USERSTATUS;
102struct File_Transfers { 102struct File_Transfers {
103 uint64_t size; 103 uint64_t size;
104 uint64_t transferred; 104 uint64_t transferred;
105 uint8_t status; /* 0 == no transfer, 1 = not accepted, 2 = paused, 3 = transferring, 4 = broken*/ 105 uint8_t status; /* 0 == no transfer, 1 = not accepted, 2 = paused by the other, 3 = transferring, 4 = broken, 5 = paused by us */
106}; 106};
107 107
108/* This cannot be bigger than 256 */ 108/* This cannot be bigger than 256 */
@@ -186,7 +186,7 @@ typedef struct Messenger {
186 186
187 void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *); 187 void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *);
188 void *file_sendrequest_userdata; 188 void *file_sendrequest_userdata;
189 void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t, void *); 189 void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *);
190 void *file_filecontrol_userdata; 190 void *file_filecontrol_userdata;
191 void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *); 191 void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *);
192 void *file_filedata_userdata; 192 void *file_filedata_userdata;
@@ -480,10 +480,11 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int,
480 480
481/* Set the callback for file control requests. 481/* Set the callback for file control requests.
482 * 482 *
483 * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 483 * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
484 * 484 *
485 */ 485 */
486void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t, 486void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *,
487 uint16_t,
487 void *), void *userdata); 488 void *), void *userdata);
488 489
489/* Set the callback for file data. 490/* Set the callback for file data.
@@ -510,12 +511,13 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_
510int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); 511int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length);
511 512
512/* Send a file control request. 513/* Send a file control request.
514 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
513 * 515 *
514 * return 1 on success 516 * return 1 on success
515 * return 0 on failure 517 * return 0 on failure
516 */ 518 */
517int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, 519int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
518 uint16_t length); 520 uint8_t *data, uint16_t length);
519 521
520/* Send file data. 522/* Send file data.
521 * 523 *