diff options
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 137 |
1 files changed, 99 insertions, 38 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 49be4a0a..6ad3fd00 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 | */ |
954 | void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t *, uint16_t, | 954 | void 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 | */ |
1033 | int file_control(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) | 1035 | int 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 | ||
1144 | static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t message_id, uint8_t *data, | 1167 | static 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 | } |