diff options
-rw-r--r-- | toxcore/group.c | 67 | ||||
-rw-r--r-- | toxcore/group.h | 11 | ||||
-rw-r--r-- | toxcore/tox.c | 4 |
3 files changed, 46 insertions, 36 deletions
diff --git a/toxcore/group.c b/toxcore/group.c index f62684ed..b42c6b24 100644 --- a/toxcore/group.c +++ b/toxcore/group.c | |||
@@ -54,10 +54,10 @@ typedef enum Invite_Id { | |||
54 | INVITE_RESPONSE_ID = 1, | 54 | INVITE_RESPONSE_ID = 1, |
55 | } Invite_Id; | 55 | } Invite_Id; |
56 | 56 | ||
57 | #define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) | 57 | #define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + 1 + GROUP_ID_LENGTH) |
58 | #define INVITE_RESPONSE_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + GROUP_IDENTIFIER_LENGTH) | 58 | #define INVITE_RESPONSE_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH) |
59 | 59 | ||
60 | #define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) | 60 | #define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + 1 + GROUP_ID_LENGTH) |
61 | 61 | ||
62 | typedef enum Peer_Id { | 62 | typedef enum Peer_Id { |
63 | PEER_KILL_ID = 1, | 63 | PEER_KILL_ID = 1, |
@@ -200,17 +200,17 @@ static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk) | |||
200 | } | 200 | } |
201 | 201 | ||
202 | /* | 202 | /* |
203 | * check if group with identifier is in group array. | 203 | * check if group with the given type and id is in group array. |
204 | * | 204 | * |
205 | * return group number if peer is in list. | 205 | * return group number if peer is in list. |
206 | * return -1 if group is not in list. | 206 | * return -1 if group is not in list. |
207 | * | 207 | * |
208 | * TODO(irungentoo): make this more efficient and maybe use constant time comparisons? | 208 | * TODO(irungentoo): make this more efficient and maybe use constant time comparisons? |
209 | */ | 209 | */ |
210 | static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier) | 210 | static int32_t get_group_num(const Group_Chats *g_c, const uint8_t type, const uint8_t *id) |
211 | { | 211 | { |
212 | for (uint16_t i = 0; i < g_c->num_chats; ++i) { | 212 | for (uint16_t i = 0; i < g_c->num_chats; ++i) { |
213 | if (crypto_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) { | 213 | if (g_c->chats[i].type == type && crypto_memcmp(g_c->chats[i].id, id, GROUP_ID_LENGTH) == 0) { |
214 | return i; | 214 | return i; |
215 | } | 215 | } |
216 | } | 216 | } |
@@ -218,10 +218,10 @@ static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier) | |||
218 | return -1; | 218 | return -1; |
219 | } | 219 | } |
220 | 220 | ||
221 | int32_t conference_by_uid(const Group_Chats *g_c, const uint8_t *uid) | 221 | int32_t conference_by_id(const Group_Chats *g_c, const uint8_t *id) |
222 | { | 222 | { |
223 | for (uint16_t i = 0; i < g_c->num_chats; ++i) { | 223 | for (uint16_t i = 0; i < g_c->num_chats; ++i) { |
224 | if (crypto_memcmp(g_c->chats[i].identifier + 1, uid, GROUP_IDENTIFIER_LENGTH - 1) == 0) { | 224 | if (crypto_memcmp(g_c->chats[i].id, id, GROUP_ID_LENGTH) == 0) { |
225 | return i; | 225 | return i; |
226 | } | 226 | } |
227 | } | 227 | } |
@@ -371,7 +371,8 @@ static unsigned int pk_in_closest_peers(Group_c *g, uint8_t *real_pk) | |||
371 | return 0; | 371 | return 0; |
372 | } | 372 | } |
373 | 373 | ||
374 | static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier); | 374 | static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t type, |
375 | uint8_t *id); | ||
375 | 376 | ||
376 | static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *userdata) | 377 | static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *userdata) |
377 | { | 378 | { |
@@ -437,7 +438,7 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user | |||
437 | add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 1, lock); | 438 | add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 1, lock); |
438 | 439 | ||
439 | if (friend_con_connected(g_c->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) { | 440 | if (friend_con_connected(g_c->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) { |
440 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier); | 441 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id); |
441 | } | 442 | } |
442 | } | 443 | } |
443 | 444 | ||
@@ -692,7 +693,7 @@ static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int fri | |||
692 | } | 693 | } |
693 | 694 | ||
694 | if (type == GROUPCHAT_CLOSE_ONLINE) { | 695 | if (type == GROUPCHAT_CLOSE_ONLINE) { |
695 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier); | 696 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id); |
696 | } else { | 697 | } else { |
697 | g->close[i].type = type; | 698 | g->close[i].type = type; |
698 | } | 699 | } |
@@ -790,8 +791,8 @@ int add_groupchat(Group_Chats *g_c, uint8_t type) | |||
790 | 791 | ||
791 | g->status = GROUPCHAT_STATUS_CONNECTED; | 792 | g->status = GROUPCHAT_STATUS_CONNECTED; |
792 | g->number_joined = -1; | 793 | g->number_joined = -1; |
793 | new_symmetric_key(g->identifier + 1); | 794 | g->type = type; |
794 | g->identifier[0] = type; | 795 | new_symmetric_key(g->id); |
795 | g->peer_number = 0; /* Founder is peer 0. */ | 796 | g->peer_number = 0; /* Founder is peer 0. */ |
796 | memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); | 797 | memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); |
797 | int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), 0, nullptr, false); | 798 | int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), 0, nullptr, false); |
@@ -1000,15 +1001,15 @@ int group_get_type(const Group_Chats *g_c, uint32_t groupnumber) | |||
1000 | return -1; | 1001 | return -1; |
1001 | } | 1002 | } |
1002 | 1003 | ||
1003 | return g->identifier[0]; | 1004 | return g->type; |
1004 | } | 1005 | } |
1005 | 1006 | ||
1006 | /* Copies the unique id of group_chat[groupnumber] into uid. | 1007 | /* Copies the unique id of group_chat[groupnumber] into id. |
1007 | * | 1008 | * |
1008 | * return false on failure. | 1009 | * return false on failure. |
1009 | * return true on success. | 1010 | * return true on success. |
1010 | */ | 1011 | */ |
1011 | bool conference_get_uid(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *uid) | 1012 | bool conference_get_id(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *id) |
1012 | { | 1013 | { |
1013 | const Group_c *g = get_group_c(g_c, groupnumber); | 1014 | const Group_c *g = get_group_c(g_c, groupnumber); |
1014 | 1015 | ||
@@ -1016,8 +1017,8 @@ bool conference_get_uid(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *u | |||
1016 | return false; | 1017 | return false; |
1017 | } | 1018 | } |
1018 | 1019 | ||
1019 | if (uid != nullptr) { | 1020 | if (id != nullptr) { |
1020 | memcpy(uid, g->identifier + 1, sizeof(g->identifier) - 1); | 1021 | memcpy(id, g->id, sizeof(g->id)); |
1021 | } | 1022 | } |
1022 | 1023 | ||
1023 | return true; | 1024 | return true; |
@@ -1088,7 +1089,8 @@ int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) | |||
1088 | invite[0] = INVITE_ID; | 1089 | invite[0] = INVITE_ID; |
1089 | uint16_t groupchat_num = net_htons((uint16_t)groupnumber); | 1090 | uint16_t groupchat_num = net_htons((uint16_t)groupnumber); |
1090 | memcpy(invite + 1, &groupchat_num, sizeof(groupchat_num)); | 1091 | memcpy(invite + 1, &groupchat_num, sizeof(groupchat_num)); |
1091 | memcpy(invite + 1 + sizeof(groupchat_num), g->identifier, GROUP_IDENTIFIER_LENGTH); | 1092 | invite[1 + sizeof(groupchat_num)] = g->type; |
1093 | memcpy(invite + 1 + sizeof(groupchat_num) + 1, g->id, GROUP_ID_LENGTH); | ||
1092 | 1094 | ||
1093 | if (send_conference_invite_packet(g_c->m, friendnumber, invite, sizeof(invite))) { | 1095 | if (send_conference_invite_packet(g_c->m, friendnumber, invite, sizeof(invite))) { |
1094 | return 0; | 1096 | return 0; |
@@ -1113,7 +1115,7 @@ static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t | |||
1113 | */ | 1115 | */ |
1114 | int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length) | 1116 | int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length) |
1115 | { | 1117 | { |
1116 | if (length != sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) { | 1118 | if (length != sizeof(uint16_t) + 1 + GROUP_ID_LENGTH) { |
1117 | return -1; | 1119 | return -1; |
1118 | } | 1120 | } |
1119 | 1121 | ||
@@ -1127,7 +1129,7 @@ int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_typ | |||
1127 | return -3; | 1129 | return -3; |
1128 | } | 1130 | } |
1129 | 1131 | ||
1130 | if (get_group_num(g_c, data + sizeof(uint16_t)) != -1) { | 1132 | if (get_group_num(g_c, data[sizeof(uint16_t)], data + sizeof(uint16_t) + 1) != -1) { |
1131 | return -4; | 1133 | return -4; |
1132 | } | 1134 | } |
1133 | 1135 | ||
@@ -1147,13 +1149,14 @@ int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_typ | |||
1147 | uint8_t response[INVITE_RESPONSE_PACKET_SIZE]; | 1149 | uint8_t response[INVITE_RESPONSE_PACKET_SIZE]; |
1148 | response[0] = INVITE_RESPONSE_ID; | 1150 | response[0] = INVITE_RESPONSE_ID; |
1149 | memcpy(response + 1, &group_num, sizeof(uint16_t)); | 1151 | memcpy(response + 1, &group_num, sizeof(uint16_t)); |
1150 | memcpy(response + 1 + sizeof(uint16_t), data, sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH); | 1152 | memcpy(response + 1 + sizeof(uint16_t), data, sizeof(uint16_t) + 1 + GROUP_ID_LENGTH); |
1151 | 1153 | ||
1152 | if (send_conference_invite_packet(g_c->m, friendnumber, response, sizeof(response))) { | 1154 | if (send_conference_invite_packet(g_c->m, friendnumber, response, sizeof(response))) { |
1153 | uint16_t other_groupnum; | 1155 | uint16_t other_groupnum; |
1154 | memcpy(&other_groupnum, data, sizeof(other_groupnum)); | 1156 | memcpy(&other_groupnum, data, sizeof(other_groupnum)); |
1155 | other_groupnum = net_ntohs(other_groupnum); | 1157 | other_groupnum = net_ntohs(other_groupnum); |
1156 | memcpy(g->identifier, data + sizeof(uint16_t), GROUP_IDENTIFIER_LENGTH); | 1158 | g->type = data[sizeof(uint16_t)]; |
1159 | memcpy(g->id, data + sizeof(uint16_t) + 1, GROUP_ID_LENGTH); | ||
1157 | int close_index = add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 0, 1); | 1160 | int close_index = add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 0, 1); |
1158 | 1161 | ||
1159 | if (close_index != -1) { | 1162 | if (close_index != -1) { |
@@ -1456,7 +1459,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con | |||
1456 | return; | 1459 | return; |
1457 | } | 1460 | } |
1458 | 1461 | ||
1459 | int groupnumber = get_group_num(g_c, data + 1 + sizeof(uint16_t)); | 1462 | int groupnumber = get_group_num(g_c, data[1 + sizeof(uint16_t)], data + 1 + sizeof(uint16_t) + 1); |
1460 | 1463 | ||
1461 | if (groupnumber == -1) { | 1464 | if (groupnumber == -1) { |
1462 | if (g_c->invite_callback) { | 1465 | if (g_c->invite_callback) { |
@@ -1484,7 +1487,11 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con | |||
1484 | return; | 1487 | return; |
1485 | } | 1488 | } |
1486 | 1489 | ||
1487 | if (crypto_memcmp(data + 1 + sizeof(uint16_t) * 2, g->identifier, GROUP_IDENTIFIER_LENGTH) != 0) { | 1490 | if (data[1 + sizeof(uint16_t) * 2] != g->type) { |
1491 | return; | ||
1492 | } | ||
1493 | |||
1494 | if (crypto_memcmp(data + 1 + sizeof(uint16_t) * 2 + 1, g->id, GROUP_ID_LENGTH) != 0) { | ||
1488 | return; | 1495 | return; |
1489 | } | 1496 | } |
1490 | 1497 | ||
@@ -1567,13 +1574,15 @@ static unsigned int count_close_connected(Group_c *g) | |||
1567 | return count; | 1574 | return count; |
1568 | } | 1575 | } |
1569 | 1576 | ||
1570 | static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier) | 1577 | static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t type, |
1578 | uint8_t *id) | ||
1571 | { | 1579 | { |
1572 | uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE]; | 1580 | uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE]; |
1573 | group_num = net_htons(group_num); | 1581 | group_num = net_htons(group_num); |
1574 | packet[0] = PACKET_ID_ONLINE_PACKET; | 1582 | packet[0] = PACKET_ID_ONLINE_PACKET; |
1575 | memcpy(packet + 1, &group_num, sizeof(uint16_t)); | 1583 | memcpy(packet + 1, &group_num, sizeof(uint16_t)); |
1576 | memcpy(packet + 1 + sizeof(uint16_t), identifier, GROUP_IDENTIFIER_LENGTH); | 1584 | packet[1 + sizeof(uint16_t)] = type; |
1585 | memcpy(packet + 1 + sizeof(uint16_t) + 1, id, GROUP_ID_LENGTH); | ||
1577 | return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id), packet, | 1586 | return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id), packet, |
1578 | sizeof(packet), 0) != -1; | 1587 | sizeof(packet), 0) != -1; |
1579 | } | 1588 | } |
@@ -1586,7 +1595,7 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_ | |||
1586 | return -1; | 1595 | return -1; |
1587 | } | 1596 | } |
1588 | 1597 | ||
1589 | int groupnumber = get_group_num(g_c, data + sizeof(uint16_t)); | 1598 | int groupnumber = get_group_num(g_c, data[sizeof(uint16_t)], data + sizeof(uint16_t) + 1); |
1590 | 1599 | ||
1591 | if (groupnumber == -1) { | 1600 | if (groupnumber == -1) { |
1592 | return -1; | 1601 | return -1; |
@@ -1618,7 +1627,7 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_ | |||
1618 | 1627 | ||
1619 | g->close[index].group_number = other_groupnum; | 1628 | g->close[index].group_number = other_groupnum; |
1620 | g->close[index].type = GROUPCHAT_CLOSE_ONLINE; | 1629 | g->close[index].type = GROUPCHAT_CLOSE_ONLINE; |
1621 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier); | 1630 | send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id); |
1622 | 1631 | ||
1623 | return 0; | 1632 | return 0; |
1624 | } | 1633 | } |
diff --git a/toxcore/group.h b/toxcore/group.h index 970cf7fb..31e0e7e3 100644 --- a/toxcore/group.h +++ b/toxcore/group.h | |||
@@ -71,7 +71,7 @@ typedef struct Group_Peer { | |||
71 | 71 | ||
72 | #define DESIRED_CLOSE_CONNECTIONS 4 | 72 | #define DESIRED_CLOSE_CONNECTIONS 4 |
73 | #define MAX_GROUP_CONNECTIONS 16 | 73 | #define MAX_GROUP_CONNECTIONS 16 |
74 | #define GROUP_IDENTIFIER_LENGTH (1 + CRYPTO_SYMMETRIC_KEY_SIZE) // type + CRYPTO_SYMMETRIC_KEY_SIZE so we can use new_symmetric_key(...) to fill it | 74 | #define GROUP_ID_LENGTH CRYPTO_SYMMETRIC_KEY_SIZE |
75 | 75 | ||
76 | typedef enum Groupchat_Close_Type { | 76 | typedef enum Groupchat_Close_Type { |
77 | GROUPCHAT_CLOSE_NONE, | 77 | GROUPCHAT_CLOSE_NONE, |
@@ -111,7 +111,8 @@ typedef struct Group_c { | |||
111 | Groupchat_Close_Connection closest_peers[DESIRED_CLOSE_CONNECTIONS]; | 111 | Groupchat_Close_Connection closest_peers[DESIRED_CLOSE_CONNECTIONS]; |
112 | uint8_t changed; | 112 | uint8_t changed; |
113 | 113 | ||
114 | uint8_t identifier[GROUP_IDENTIFIER_LENGTH]; | 114 | uint8_t type; |
115 | uint8_t id[GROUP_ID_LENGTH]; | ||
115 | 116 | ||
116 | uint8_t title[MAX_NAME_LENGTH]; | 117 | uint8_t title[MAX_NAME_LENGTH]; |
117 | uint8_t title_len; | 118 | uint8_t title_len; |
@@ -361,14 +362,14 @@ uint32_t copy_chatlist(const Group_Chats *g_c, uint32_t *out_list, uint32_t list | |||
361 | */ | 362 | */ |
362 | int group_get_type(const Group_Chats *g_c, uint32_t groupnumber); | 363 | int group_get_type(const Group_Chats *g_c, uint32_t groupnumber); |
363 | 364 | ||
364 | /* Copies the unique id of group_chat[groupnumber] into uid. | 365 | /* Copies the unique id of group_chat[groupnumber] into id. |
365 | * | 366 | * |
366 | * return false on failure. | 367 | * return false on failure. |
367 | * return true on success. | 368 | * return true on success. |
368 | */ | 369 | */ |
369 | bool conference_get_uid(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *uid); | 370 | bool conference_get_id(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *id); |
370 | 371 | ||
371 | int32_t conference_by_uid(const Group_Chats *g_c, const uint8_t *uid); | 372 | int32_t conference_by_id(const Group_Chats *g_c, const uint8_t *id); |
372 | 373 | ||
373 | /* Send current name (set in messenger) to all online groups. | 374 | /* Send current name (set in messenger) to all online groups. |
374 | */ | 375 | */ |
diff --git a/toxcore/tox.c b/toxcore/tox.c index bf84f022..f7bdba79 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -1719,7 +1719,7 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ | |||
1719 | 1719 | ||
1720 | bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id /* TOX_CONFERENCE_ID_SIZE bytes */) | 1720 | bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id /* TOX_CONFERENCE_ID_SIZE bytes */) |
1721 | { | 1721 | { |
1722 | return conference_get_uid((Group_Chats *)tox->m->conferences_object, conference_number, id); | 1722 | return conference_get_id((Group_Chats *)tox->m->conferences_object, conference_number, id); |
1723 | } | 1723 | } |
1724 | 1724 | ||
1725 | // TODO(iphydf): Delete in 0.3.0. | 1725 | // TODO(iphydf): Delete in 0.3.0. |
@@ -1735,7 +1735,7 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Confere | |||
1735 | return UINT32_MAX; | 1735 | return UINT32_MAX; |
1736 | } | 1736 | } |
1737 | 1737 | ||
1738 | int32_t ret = conference_by_uid((Group_Chats *)tox->m->conferences_object, id); | 1738 | int32_t ret = conference_by_id((Group_Chats *)tox->m->conferences_object, id); |
1739 | 1739 | ||
1740 | if (ret == -1) { | 1740 | if (ret == -1) { |
1741 | SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND); | 1741 | SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND); |