summaryrefslogtreecommitdiff
path: root/toxcore/group.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-15 22:23:10 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-18 21:05:42 +0000
commit7f6c681cfaab29aa976f346e9027af6eb731d73e (patch)
tree97d53d9f7e90889123e8f901c1586b63ece7ef3b /toxcore/group.c
parenta592f0fa87c96f1c2dea1406e4dcbfa5cde4f8f1 (diff)
Use enums for group packet types.
Also moved some macros up to the beginning of `group.c`. This change brings us closer to the PGC PR.
Diffstat (limited to 'toxcore/group.c')
-rw-r--r--toxcore/group.c67
1 files changed, 37 insertions, 30 deletions
diff --git a/toxcore/group.c b/toxcore/group.c
index 2ee81de8..95de1afb 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -33,6 +33,41 @@
33#include "mono_time.h" 33#include "mono_time.h"
34#include "util.h" 34#include "util.h"
35 35
36/**
37 * Packet type IDs as per the protocol specification.
38 */
39typedef enum Group_Message_Id {
40 GROUP_MESSAGE_PING_ID = 0,
41 GROUP_MESSAGE_NEW_PEER_ID = 16,
42 GROUP_MESSAGE_KILL_PEER_ID = 17,
43 GROUP_MESSAGE_NAME_ID = 48,
44 GROUP_MESSAGE_TITLE_ID = 49,
45} Group_Message_Id;
46
47#define GROUP_MESSAGE_NEW_PEER_LENGTH (sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2)
48#define GROUP_MESSAGE_KILL_PEER_LENGTH (sizeof(uint16_t))
49
50#define MAX_GROUP_MESSAGE_DATA_LEN (MAX_CRYPTO_DATA_SIZE - (1 + MIN_MESSAGE_PACKET_LEN))
51
52typedef enum Invite_Id {
53 INVITE_ID = 0,
54 INVITE_RESPONSE_ID = 1,
55} Invite_Id;
56
57#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
58#define INVITE_RESPONSE_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + GROUP_IDENTIFIER_LENGTH)
59
60#define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
61
62typedef enum Peer_Id {
63 PEER_KILL_ID = 1,
64 PEER_QUERY_ID = 8,
65 PEER_RESPONSE_ID = 9,
66 PEER_TITLE_ID = 10,
67} Peer_Id;
68
69#define MIN_MESSAGE_PACKET_LEN (sizeof(uint16_t) * 2 + sizeof(uint32_t) + 1)
70
36/* return false if the groupnumber is not valid. 71/* return false if the groupnumber is not valid.
37 * return true if the groupnumber is valid. 72 * return true if the groupnumber is valid.
38 */ 73 */
@@ -218,14 +253,12 @@ static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
218{ 253{
219 uint64_t cmp1 = 0, cmp2 = 0; 254 uint64_t cmp1 = 0, cmp2 = 0;
220 255
221 unsigned int i; 256 for (size_t i = 0; i < sizeof(uint64_t); ++i) {
222
223 for (i = 0; i < sizeof(uint64_t); ++i) {
224 cmp1 = (cmp1 << 8) + (uint64_t)pk1[i]; 257 cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
225 cmp2 = (cmp2 << 8) + (uint64_t)pk2[i]; 258 cmp2 = (cmp2 << 8) + (uint64_t)pk2[i];
226 } 259 }
227 260
228 return (cmp1 - cmp2); 261 return cmp1 - cmp2;
229} 262}
230 263
231typedef enum Groupchat_Closest { 264typedef enum Groupchat_Closest {
@@ -1024,12 +1057,6 @@ static unsigned int send_lossy_group_peer(Friend_Connections *fr_c, int friendco
1024 packet, SIZEOF_VLA(packet)) != -1; 1057 packet, SIZEOF_VLA(packet)) != -1;
1025} 1058}
1026 1059
1027#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
1028#define INVITE_ID 0
1029
1030#define INVITE_RESPONSE_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + GROUP_IDENTIFIER_LENGTH)
1031#define INVITE_RESPONSE_ID 1
1032
1033/* invite friendnumber to groupnumber. 1060/* invite friendnumber to groupnumber.
1034 * 1061 *
1035 * return 0 on success. 1062 * return 0 on success.
@@ -1254,7 +1281,6 @@ int callback_groupchat_delete(Group_Chats *g_c, uint32_t groupnumber, group_on_d
1254static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *data, 1281static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *data,
1255 uint16_t len); 1282 uint16_t len);
1256 1283
1257#define GROUP_MESSAGE_PING_ID 0
1258static int group_ping_send(const Group_Chats *g_c, uint32_t groupnumber) 1284static int group_ping_send(const Group_Chats *g_c, uint32_t groupnumber)
1259{ 1285{
1260 if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0) { 1286 if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0) {
@@ -1264,8 +1290,6 @@ static int group_ping_send(const Group_Chats *g_c, uint32_t groupnumber)
1264 return -1; 1290 return -1;
1265} 1291}
1266 1292
1267#define GROUP_MESSAGE_NEW_PEER_ID 16
1268#define GROUP_MESSAGE_NEW_PEER_LENGTH (sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2)
1269/* send a new_peer message 1293/* send a new_peer message
1270 * return 0 on success 1294 * return 0 on success
1271 * return -1 on failure 1295 * return -1 on failure
@@ -1287,9 +1311,6 @@ static int group_new_peer_send(const Group_Chats *g_c, uint32_t groupnumber, uin
1287 return -1; 1311 return -1;
1288} 1312}
1289 1313
1290#define GROUP_MESSAGE_KILL_PEER_ID 17
1291#define GROUP_MESSAGE_KILL_PEER_LENGTH (sizeof(uint16_t))
1292
1293/* send a kill_peer message 1314/* send a kill_peer message
1294 * return 0 on success 1315 * return 0 on success
1295 * return -1 on failure 1316 * return -1 on failure
@@ -1308,8 +1329,6 @@ static int group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, ui
1308 return -1; 1329 return -1;
1309} 1330}
1310 1331
1311#define GROUP_MESSAGE_NAME_ID 48
1312
1313/* send a name message 1332/* send a name message
1314 * return 0 on success 1333 * return 0 on success
1315 * return -1 on failure 1334 * return -1 on failure
@@ -1327,8 +1346,6 @@ static int group_name_send(const Group_Chats *g_c, uint32_t groupnumber, const u
1327 return -1; 1346 return -1;
1328} 1347}
1329 1348
1330#define GROUP_MESSAGE_TITLE_ID 49
1331
1332/* set the group's title, limited to MAX_NAME_LENGTH 1349/* set the group's title, limited to MAX_NAME_LENGTH
1333 * return 0 on success 1350 * return 0 on success
1334 * return -1 if groupnumber is invalid. 1351 * return -1 if groupnumber is invalid.
@@ -1536,8 +1553,6 @@ static unsigned int count_close_connected(Group_c *g)
1536 return count; 1553 return count;
1537} 1554}
1538 1555
1539#define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
1540
1541static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier) 1556static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier)
1542{ 1557{
1543 uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE]; 1558 uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE];
@@ -1609,10 +1624,6 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_
1609 return 0; 1624 return 0;
1610} 1625}
1611 1626
1612#define PEER_KILL_ID 1
1613#define PEER_QUERY_ID 8
1614#define PEER_RESPONSE_ID 9
1615#define PEER_TITLE_ID 10
1616// we could send title with invite, but then if it changes between sending and accepting inv, joinee won't see it 1627// we could send title with invite, but then if it changes between sending and accepting inv, joinee won't see it
1617 1628
1618/* return 1 on success. 1629/* return 1 on success.
@@ -1795,8 +1806,6 @@ static void handle_direct_packet(Group_Chats *g_c, uint32_t groupnumber, const u
1795 } 1806 }
1796} 1807}
1797 1808
1798#define MIN_MESSAGE_PACKET_LEN (sizeof(uint16_t) * 2 + sizeof(uint32_t) + 1)
1799
1800/* Send message to all close except receiver (if receiver isn't -1) 1809/* Send message to all close except receiver (if receiver isn't -1)
1801 * NOTE: this function appends the group chat number to the data passed to it. 1810 * NOTE: this function appends the group chat number to the data passed to it.
1802 * 1811 *
@@ -1920,8 +1929,6 @@ static unsigned int send_lossy_all_close(const Group_Chats *g_c, uint32_t groupn
1920 return sent; 1929 return sent;
1921} 1930}
1922 1931
1923#define MAX_GROUP_MESSAGE_DATA_LEN (MAX_CRYPTO_DATA_SIZE - (1 + MIN_MESSAGE_PACKET_LEN))
1924
1925/* Send data of len with message_id to groupnumber. 1932/* Send data of len with message_id to groupnumber.
1926 * 1933 *
1927 * return number of peers it was sent to on success. 1934 * return number of peers it was sent to on success.