summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Messenger.c48
-rw-r--r--toxcore/Messenger.h12
-rw-r--r--toxcore/net_crypto.c2
-rw-r--r--toxcore/tox.c24
-rw-r--r--toxcore/tox.h18
5 files changed, 87 insertions, 17 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index cb588adf..32e81389 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1653,6 +1653,54 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1653 return Messenger_load_old(m, data, length); 1653 return Messenger_load_old(m, data, length);
1654} 1654}
1655 1655
1656/* Return the number of friends in the instance m.
1657 * You should use this to determine how much memory to allocate
1658 * for copy_friendlist. */
1659uint32_t count_friendlist(Messenger *m)
1660{
1661 uint32_t ret = 0;
1662 uint32_t i;
1663
1664 for (i = 0; i < m->numfriends; i++) {
1665 if (m->friendlist[i].status > 0) {
1666 ret++;
1667 }
1668 }
1669
1670 return ret;
1671}
1672
1673/* Copy a list of valid friend IDs into the array out_list.
1674 * If out_list is NULL, returns 0.
1675 * Otherwise, returns the number of elements copied.
1676 * If the array was too small, the contents
1677 * of out_list will be truncated to list_size. */
1678uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size)
1679{
1680 if (!out_list)
1681 return 0;
1682
1683 if (m->numfriends == 0) {
1684 return 0;
1685 }
1686
1687 uint32_t i;
1688 uint32_t ret = 0;
1689
1690 for (i = 0; i < m->numfriends; i++) {
1691 if (i >= list_size) {
1692 break; /* Abandon ship */
1693 }
1694
1695 if (m->friendlist[i].status > 0) {
1696 out_list[i] = i;
1697 ret++;
1698 }
1699 }
1700
1701 return ret;
1702}
1703
1656/* Allocate and return a list of valid friend id's. List must be freed by the 1704/* Allocate and return a list of valid friend id's. List must be freed by the
1657 * caller. 1705 * caller.
1658 * 1706 *
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 78580dc0..2b29896c 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -456,6 +456,18 @@ void Messenger_save(Messenger *m, uint8_t *data);
456/* Load the messenger from data of size length. */ 456/* Load the messenger from data of size length. */
457int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); 457int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
458 458
459/* Return the number of friends in the instance m.
460 * You should use this to determine how much memory to allocate
461 * for copy_friendlist. */
462uint32_t count_friendlist(Messenger *m);
463
464/* Copy a list of valid friend IDs into the array out_list.
465 * If out_list is NULL, returns 0.
466 * Otherwise, returns the number of elements copied.
467 * If the array was too small, the contents
468 * of out_list will be truncated to list_size. */
469uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size);
470
459/* Allocate and return a list of valid friend id's. List must be freed by the 471/* Allocate and return a list of valid friend id's. List must be freed by the
460 * caller. 472 * caller.
461 * 473 *
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 2da2ba3b..98571ce7 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -503,8 +503,6 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
503 */ 503 */
504int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) 504int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
505{ 505{
506 uint32_t i, j;
507
508 while (1) { 506 while (1) {
509 int incoming_con = incoming_connection(c->lossless_udp, 1); 507 int incoming_con = incoming_connection(c->lossless_udp, 1);
510 508
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 4fba360b..fa53e693 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -262,16 +262,24 @@ void tox_set_sends_receipts(void *tox, int friendnumber, int yesno)
262 m_set_sends_receipts(m, friendnumber, yesno); 262 m_set_sends_receipts(m, friendnumber, yesno);
263} 263}
264 264
265/* Allocate and return a list of valid friend id's. List must be freed by the 265/* Return the number of friends in the instance m.
266 * caller. 266 * You should use this to determine how much memory to allocate
267 * 267 * for copy_friendlist. */
268 * retun 0 if success. 268uint32_t tox_count_friendlist(void *tox)
269 * return -1 if failure. 269{
270 */ 270 Messenger *m = tox;
271int tox_get_friendlist(void *tox, int **out_list, uint32_t *out_list_length) 271 return count_friendlist(m);
272}
273
274/* Copy a list of valid friend IDs into the array out_list.
275 * If out_list is NULL, returns 0.
276 * Otherwise, returns the number of elements copied.
277 * If the array was too small, the contents
278 * of out_list will be truncated to list_size. */
279uint32_t tox_copy_friendlist(void *tox, int *out_list, uint32_t list_size)
272{ 280{
273 Messenger *m = tox; 281 Messenger *m = tox;
274 return get_friendlist(m, out_list, out_list_length); 282 return copy_friendlist(m, out_list, list_size);
275} 283}
276 284
277/* Set the function that will be executed when a friend request is received. 285/* Set the function that will be executed when a friend request is received.
diff --git a/toxcore/tox.h b/toxcore/tox.h
index b39008fe..9c810418 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -280,13 +280,17 @@ TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox);
280 */ 280 */
281void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); 281void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno);
282 282
283/* Allocate and return a list of valid friend id's. List must be freed by the 283/* Return the number of friends in the instance m.
284 * caller. 284 * You should use this to determine how much memory to allocate
285 * 285 * for copy_friendlist. */
286 * retun 0 if success. 286uint32_t tox_count_friendlist(void *tox);
287 * return -1 if failure. 287
288 */ 288/* Copy a list of valid friend IDs into the array out_list.
289int tox_get_friendlist(void *tox, int **out_list, uint32_t *out_list_length); 289 * If out_list is NULL, returns 0.
290 * Otherwise, returns the number of elements copied.
291 * If the array was too small, the contents
292 * of out_list will be truncated to list_size. */
293uint32_t tox_copy_friendlist(void *tox, int *out_list, uint32_t list_size);
290 294
291/* Set the function that will be executed when a friend request is received. 295/* Set the function that will be executed when a friend request is received.
292 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 296 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)