From 554afe11d7c3e286b6f286405a1a1857b9f55c81 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 21 Feb 2014 11:38:04 -0500 Subject: [PATCH 1/8] Some api changes. --- testing/nTox.c | 2 +- toxcore/Messenger.c | 174 +++++++++++++++++++------------------- toxcore/Messenger.h | 163 ++++++++++++++++++------------------ toxcore/tox.c | 115 +++++++++++++------------- toxcore/tox.h | 197 +++++++++++++++++++++++--------------------- 5 files changed, 334 insertions(+), 317 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 8f7dc7ea..343db236 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1243,7 +1243,7 @@ int main(int argc, char *argv[]) new_lines("[i] change username with /n"); uint8_t name[TOX_MAX_NAME_LENGTH + 1]; - uint16_t namelen = tox_get_self_name(m, name, sizeof(name)); + uint16_t namelen = tox_get_self_name(m, name); name[namelen] = 0; if (namelen > 0) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 69bc845a..9f75f89f 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -34,16 +34,16 @@ #define MIN(a,b) (((a)<(b))?(a):(b)) -static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); -static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); +static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status); +static int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); // friend_not_valid determines if the friendnumber passed is valid in the Messenger object -static uint8_t friend_not_valid(Messenger *m, int friendnumber) +static uint8_t friend_not_valid(Messenger *m, int32_t friendnumber) { return (unsigned int)friendnumber >= m->numfriends; } -static int add_online_friend(Messenger *m, int friendnumber) +static int add_online_friend(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return -1; @@ -74,7 +74,7 @@ static int add_online_friend(Messenger *m, int friendnumber) } -static int remove_online_friend(Messenger *m, int friendnumber) +static int remove_online_friend(Messenger *m, int32_t friendnumber) { uint32_t i; Online_Friend *temp; @@ -132,7 +132,7 @@ int realloc_friendlist(Messenger *m, uint32_t num) /* return the friend id associated to that public key. * return -1 if no such friend. */ -int getfriend_id(Messenger *m, uint8_t *client_id) +int32_t getfriend_id(Messenger *m, uint8_t *client_id) { uint32_t i; @@ -151,13 +151,13 @@ int getfriend_id(Messenger *m, uint8_t *client_id) * return 0 if success. * return -1 if failure. */ -int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) +int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id) { - if (friend_not_valid(m, friend_id)) + if (friend_not_valid(m, friendnumber)) return -1; - if (m->friendlist[friend_id].status > 0) { - memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE); + if (m->friendlist[friendnumber].status > 0) { + memcpy(client_id, m->friendlist[friendnumber].client_id, CLIENT_ID_SIZE); return 0; } @@ -210,7 +210,7 @@ void getaddress(Messenger *m, uint8_t *address) * (the nospam for that friend was set to the new one). * return FAERR_NOMEM if increasing the friend list size fails. */ -int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) +int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) { if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES @@ -231,7 +231,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) if (id_equal(client_id, m->net_crypto->self_public_key)) return FAERR_OWNKEY; - int friend_id = getfriend_id(m, client_id); + int32_t friend_id = getfriend_id(m, client_id); if (friend_id != -1) { uint32_t nospam; @@ -250,7 +250,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); - int onion_friendnum = onion_addfriend(m->onion_c, client_id); + int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id); if (onion_friendnum == -1) return FAERR_UNKNOWN; @@ -285,7 +285,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) return FAERR_UNKNOWN; } -int m_addfriend_norequest(Messenger *m, uint8_t *client_id) +int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id) { if (getfriend_id(m, client_id) != -1) return -1; @@ -299,7 +299,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); - int onion_friendnum = onion_addfriend(m->onion_c, client_id); + int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id); if (onion_friendnum == -1) return FAERR_UNKNOWN; @@ -335,7 +335,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) * return 0 if success. * return -1 if failure. */ -int m_delfriend(Messenger *m, int friendnumber) +int m_delfriend(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return -1; @@ -362,7 +362,7 @@ int m_delfriend(Messenger *m, int friendnumber) return 0; } -int m_get_friend_connectionstatus(Messenger *m, int friendnumber) +int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return -1; @@ -370,7 +370,7 @@ int m_get_friend_connectionstatus(Messenger *m, int friendnumber) return m->friendlist[friendnumber].status == FRIEND_ONLINE; } -int m_friend_exists(Messenger *m, int friendnumber) +int m_friend_exists(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return 0; @@ -383,7 +383,7 @@ int m_friend_exists(Messenger *m, int friendnumber) * return the message id if packet was successfully put into the send queue. * return 0 if it was not. */ -uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) +uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length) { if (friend_not_valid(m, friendnumber)) return 0; @@ -400,7 +400,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_ return 0; } -uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) +uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) { if (length >= (MAX_DATA_SIZE - sizeof(theid))) return 0; @@ -417,7 +417,7 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui * return the message id if packet was successfully put into the send queue. * return 0 if it was not. */ -uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) +uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length) { if (friend_not_valid(m, friendnumber)) return 0; @@ -434,7 +434,7 @@ uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t return 0; } -uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) +uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) { if (length >= (MAX_DATA_SIZE - sizeof(theid))) return 0; @@ -449,7 +449,7 @@ uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uin /* Send a name packet to friendnumber. * length is the length with the NULL terminator. */ -static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) +static int m_sendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length) { if (length > MAX_NAME_LENGTH || length == 0) return 0; @@ -462,7 +462,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le * return 0 if success. * return -1 if failure. */ -int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) +int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length) { if (friend_not_valid(m, friendnumber)) return -1; @@ -507,18 +507,15 @@ int setname(Messenger *m, uint8_t *name, uint16_t length) * * return the length of the name. */ -uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) +uint16_t getself_name(Messenger *m, uint8_t *name) { - uint16_t len; - - if (name == NULL || nlen == 0) { + if (name == NULL) { return 0; } - len = MIN(nlen, m->name_length); - memcpy(name, m->name, len); + memcpy(name, m->name, m->name_length); - return len; + return m->name_length; } /* Get name of friendnumber and put it in name. @@ -527,7 +524,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) * return length of name if success. * return -1 if failure. */ -int getname(Messenger *m, int friendnumber, uint8_t *name) +int getname(Messenger *m, int32_t friendnumber, uint8_t *name) { if (friend_not_valid(m, friendnumber)) return -1; @@ -570,7 +567,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status) /* return the size of friendnumber's user status. * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. */ -int m_get_statusmessage_size(Messenger *m, int friendnumber) +int m_get_statusmessage_size(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return -1; @@ -581,7 +578,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber) /* Copy the user status of friendnumber into buf, truncating if needed to maxlen * bytes, use m_get_statusmessage_size to find out how much you need to allocate. */ -int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) +int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) { if (friend_not_valid(m, friendnumber)) return -1; @@ -598,7 +595,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) return MIN(maxlen, m->statusmessage_length); } -USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) +USERSTATUS m_get_userstatus(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return USERSTATUS_INVALID; @@ -617,7 +614,7 @@ USERSTATUS m_get_self_userstatus(Messenger *m) return m->userstatus; } -int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing) +int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) { if (is_typing != 0 && is_typing != 1) { return -1; @@ -632,7 +629,7 @@ int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing) return 0; } -int m_get_istyping(Messenger *m, int friendnumber) +int m_get_istyping(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return -1; @@ -640,24 +637,24 @@ int m_get_istyping(Messenger *m, int friendnumber) return m->friendlist[friendnumber].is_typing; } -static int send_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) +static int send_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) { return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); } -static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status) +static int send_userstatus(Messenger *m, int32_t friendnumber, USERSTATUS status) { uint8_t stat = status; return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); } -static int send_user_istyping(Messenger *m, int friendnumber, uint8_t is_typing) +static int send_user_istyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) { uint8_t typing = is_typing; return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing)); } -static int send_ping(Messenger *m, int friendnumber) +static int send_ping(Messenger *m, int32_t friendnumber) { int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0); @@ -667,7 +664,7 @@ static int send_ping(Messenger *m, int friendnumber) return ret; } -static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) +static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) { if (friend_not_valid(m, friendnumber)) return -1; @@ -680,18 +677,18 @@ static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *sta return 0; } -static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status) +static void set_friend_userstatus(Messenger *m, int32_t friendnumber, USERSTATUS status) { m->friendlist[friendnumber].userstatus = status; } -static void set_friend_typing(Messenger *m, int friendnumber, uint8_t is_typing) +static void set_friend_typing(Messenger *m, int32_t friendnumber, uint8_t is_typing) { m->friendlist[friendnumber].is_typing = is_typing; } /* Sets whether we send read receipts for friendnumber. */ -void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno) +void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno) { if (yesno != 0 && yesno != 1) return; @@ -710,66 +707,67 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t } /* Set the function that will be executed when a message from a friend is received. */ -void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata) { m->friend_message = function; m->friend_message_userdata = userdata; } -void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata) +void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), + void *userdata) { m->friend_action = function; m->friend_action_userdata = userdata; } -void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata) { m->friend_namechange = function; m->friend_namechange_userdata = userdata; } -void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata) { m->friend_statusmessagechange = function; m->friend_statuschange_userdata = userdata; } -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata) +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, USERSTATUS, void *), void *userdata) { m->friend_userstatuschange = function; m->friend_userstatuschange_userdata = userdata; } -void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, int, void *), void *userdata) +void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int32_t, int, void *), void *userdata) { m->friend_typingchange = function; m->friend_typingchange_userdata = userdata; } -void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata) +void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int32_t, uint32_t, void *), void *userdata) { m->read_receipt = function; m->read_receipt_userdata = userdata; } -void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata) +void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata) { m->friend_connectionstatuschange = function; m->friend_connectionstatuschange_userdata = userdata; } -void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), +void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata) { m->friend_connectionstatuschange_internal = function; m->friend_connectionstatuschange_internal_userdata = userdata; } -static void break_files(Messenger *m, int friendnumber); -static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status) +static void break_files(Messenger *m, int32_t friendnumber); +static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status) { if (status == NOFRIEND) return; @@ -796,13 +794,13 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_ } } -void set_friend_status(Messenger *m, int friendnumber, uint8_t status) +void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status) { check_friend_connectionstatus(m, friendnumber, status); m->friendlist[friendnumber].status = status; } -int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) +int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) { if (friend_not_valid(m, friendnumber)) return 0; @@ -842,7 +840,7 @@ static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber) /* returns valid ip port of connected friend on success * returns zeroed out IP_Port on failure */ -IP_Port get_friend_ipport(Messenger *m, int friendnumber) +IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber) { IP_Port zero; memset(&zero, 0, sizeof(zero)); @@ -876,9 +874,9 @@ static int group_num(Messenger *m, uint8_t *group_public_key) /* Set the callback for group invites. * - * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata) + * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata) */ -void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata) +void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata) { m->group_invite = function; m->group_invite_userdata = userdata; @@ -1081,7 +1079,7 @@ int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *nam /* Store the fact that we invited a specific friend. */ -static void group_store_friendinvite(Messenger *m, int friendnumber, int groupnumber) +static void group_store_friendinvite(Messenger *m, int32_t friendnumber, int groupnumber) { /* Add 1 to the groupchat number because 0 (default value in invited_groups) is a valid groupchat number */ m->friendlist[friendnumber].invited_groups[m->friendlist[friendnumber].invited_groups_num % MAX_INVITED_GROUPS] = @@ -1092,7 +1090,7 @@ static void group_store_friendinvite(Messenger *m, int friendnumber, int groupnu /* return 1 if that friend was invited to the group * return 0 if the friend was not or error. */ -static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber) +static uint8_t group_invited(Messenger *m, int32_t friendnumber, int groupnumber) { uint32_t i; @@ -1114,7 +1112,7 @@ static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber) * return 0 on success * return -1 on failure */ -int invite_friend(Messenger *m, int friendnumber, int groupnumber) +int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber) { if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats) return -1; @@ -1140,7 +1138,7 @@ int invite_friend(Messenger *m, int friendnumber, int groupnumber) * returns group number on success * returns -1 on failure. */ -int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) +int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key) { if (friend_not_valid(m, friendnumber)) return -1; @@ -1257,9 +1255,10 @@ static void do_allgroupchats(Messenger *m) /* Set the callback for file send requests. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) */ -void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, +void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, + uint16_t, void *), void *userdata) { m->file_sendrequest = function; @@ -1268,10 +1267,10 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, /* Set the callback for file control requests. * - * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) * */ -void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, +void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *), void *userdata) { @@ -1281,10 +1280,11 @@ void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uin /* Set the callback for file data. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) * */ -void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *), +void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, + void *), void *userdata) { m->file_filedata = function; @@ -1298,7 +1298,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_ * return 1 on success * return 0 on failure */ -int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, +int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) { if (friend_not_valid(m, friendnumber)) @@ -1321,7 +1321,7 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_ * return file number on success * return -1 on failure */ -int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) +int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) { if (friend_not_valid(m, friendnumber)) return -1; @@ -1351,7 +1351,7 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f * return 0 on success * return -1 on failure */ -int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, +int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) { if (length > MAX_DATA_SIZE - 3) @@ -1439,7 +1439,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f * return 0 on success * return -1 on failure */ -int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) +int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) { if (length > MAX_DATA_SIZE - 1) return -1; @@ -1474,7 +1474,7 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, * return number of bytes remaining to be sent/received on success * return 0 on failure */ -uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive) +uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) { if (friend_not_valid(m, friendnumber)) return 0; @@ -1497,7 +1497,7 @@ uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, /* Run this when the friend disconnects. * Sets all current file transfers to broken. */ -static void break_files(Messenger *m, int friendnumber) +static void break_files(Messenger *m, int32_t friendnumber) { uint32_t i; @@ -1510,7 +1510,7 @@ static void break_files(Messenger *m, int friendnumber) } } -static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, +static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) { @@ -1600,7 +1600,7 @@ static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t receive_se * * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) */ -void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata) { m->msi_packet = function; @@ -1612,12 +1612,12 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uin * return 1 on success * return 0 on failure */ -int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length) +int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length) { return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); } -static int friendnum_from_ip_port(Messenger *m, IP_Port ip_port) +static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port) { uint32_t i; @@ -1632,7 +1632,7 @@ static int friendnum_from_ip_port(Messenger *m, IP_Port ip_port) static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length) { Messenger *m = object; - int friend_num = friendnum_from_ip_port(m, source); + int32_t friend_num = friendnum_from_ip_port(m, source); if (friend_num == -1) return 1; @@ -1645,7 +1645,7 @@ static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *pack } -int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t byte, packet_handler_callback cb, +int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, void *object) { if (friend_not_valid(m, friendnumber)) @@ -1660,7 +1660,7 @@ int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t b return 0; } -int send_custom_user_packet(Messenger *m, int friendnumber, uint8_t *data, uint32_t length) +int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length) { IP_Port ip_port = get_friend_ipport(m, friendnumber); @@ -2573,7 +2573,7 @@ uint32_t get_num_online_friends(Messenger *m) * Otherwise, returns the number of elements copied. * If the array was too small, the contents * of out_list will be truncated to list_size. */ -uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size) +uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size) { if (!out_list) return 0; @@ -2605,7 +2605,7 @@ uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size) * retun 0 if success. * return -1 if failure. */ -int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length) +int get_friendlist(Messenger *m, int32_t **out_list, uint32_t *out_list_length) { uint32_t i; @@ -2616,7 +2616,7 @@ int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length) return 0; } - *out_list = malloc(m->numfriends * sizeof(int)); + *out_list = malloc(m->numfriends * sizeof(int32_t)); if (*out_list == NULL) { return -1; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e6851800..8474bbe7 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -198,28 +198,28 @@ typedef struct Messenger { uint64_t last_LANdiscovery; - void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*friend_message)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *friend_message_userdata; - void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*friend_action)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *friend_action_userdata; - void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*friend_namechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *friend_namechange_userdata; - void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*friend_statusmessagechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *friend_statusmessagechange_userdata; - void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void *); + void (*friend_userstatuschange)(struct Messenger *m, int32_t, USERSTATUS, void *); void *friend_userstatuschange_userdata; - void (*friend_typingchange)(struct Messenger *m, int, int, void *); + void (*friend_typingchange)(struct Messenger *m, int32_t, int, void *); void *friend_typingchange_userdata; - void (*read_receipt)(struct Messenger *m, int, uint32_t, void *); + void (*read_receipt)(struct Messenger *m, int32_t, uint32_t, void *); void *read_receipt_userdata; - void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void *); + void (*friend_statuschange)(struct Messenger *m, int32_t, uint8_t, void *); void *friend_statuschange_userdata; - void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *); + void (*friend_connectionstatuschange)(struct Messenger *m, int32_t, uint8_t, void *); void *friend_connectionstatuschange_userdata; - void (*friend_connectionstatuschange_internal)(struct Messenger *m, int, uint8_t, void *); + void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *); void *friend_connectionstatuschange_internal_userdata; - void (*group_invite)(struct Messenger *m, int, uint8_t *, void *); + void (*group_invite)(struct Messenger *m, int32_t, uint8_t *, void *); void *group_invite_userdata; void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); void *group_message_userdata; @@ -228,14 +228,14 @@ typedef struct Messenger { void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *); void *group_namelistchange_userdata; - void (*file_sendrequest)(struct Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, void *); + void (*file_sendrequest)(struct Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, void *); void *file_sendrequest_userdata; - void (*file_filecontrol)(struct Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *); + void (*file_filecontrol)(struct Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *); void *file_filecontrol_userdata; - void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *); + void (*file_filedata)(struct Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *); void *file_filedata_userdata; - void (*msi_packet)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*msi_packet)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *msi_packet_userdata; } Messenger; @@ -262,19 +262,19 @@ void getaddress(Messenger *m, uint8_t *address); * (the nospam for that friend was set to the new one). * return -8 if increasing the friend list size fails. */ -int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); +int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); /* Add a friend without sending a friendrequest. * return the friend number if success. * return -1 if failure. */ -int m_addfriend_norequest(Messenger *m, uint8_t *client_id); +int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id); -/* return the friend id associated to that client id. +/* return the friend number associated to that client id. * return -1 if no such friend. */ -int getfriend_id(Messenger *m, uint8_t *client_id); +int32_t getfriend_id(Messenger *m, uint8_t *client_id); /* Copies the public key associated to that friend id into client_id buffer. * Make sure that client_id is of size CLIENT_ID_SIZE. @@ -282,10 +282,14 @@ int getfriend_id(Messenger *m, uint8_t *client_id); * return 0 if success * return -1 if failure */ -int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); +int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id); -/* Remove a friend. */ -int m_delfriend(Messenger *m, int friendnumber); +/* Remove a friend. + * + * return 0 if success + * return -1 if failure + */ +int m_delfriend(Messenger *m, int32_t friendnumber); /* Checks friend's connecting status. * @@ -293,14 +297,14 @@ int m_delfriend(Messenger *m, int friendnumber); * return 0 if friend is not connected to us (Offline). * return -1 on failure. */ -int m_get_friend_connectionstatus(Messenger *m, int friendnumber); +int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber); /* Checks if there exists a friend with given friendnumber. * * return 1 if friend exists. * return 0 if friend doesn't exist. */ -int m_friend_exists(Messenger *m, int friendnumber); +int m_friend_exists(Messenger *m, int32_t friendnumber); /* Send a text chat message to an online friend. * @@ -312,8 +316,8 @@ int m_friend_exists(Messenger *m, int friendnumber); * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); -uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); +uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length); +uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length); /* Send an action to an online friend. * @@ -325,8 +329,8 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui * m_sendaction_withid will send an action message with the id of your choosing, * however we can generate an id for you by calling plain m_sendaction. */ -uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); -uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); +uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length); +uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length); /* Set the name and name_length of a friend. * name must be a string of maximum MAX_NAME_LENGTH length. @@ -336,7 +340,7 @@ uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uin * return 0 if success. * return -1 if failure. */ -int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length); +int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length); /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. @@ -351,13 +355,12 @@ int setname(Messenger *m, uint8_t *name, uint16_t length); /* * Get your nickname. * m - The messanger context to use. - * name - Pointer to a string for the name. - * nlen - The length of the string buffer. + * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. * * return length of the name. * return 0 on error. */ -uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); +uint16_t getself_name(Messenger *m, uint8_t *name); /* Get name of friendnumber and put it in name. * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. @@ -365,12 +368,12 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); * return length of name if success. * return -1 if failure. */ -int getname(Messenger *m, int friendnumber, uint8_t *name); +int getname(Messenger *m, int32_t friendnumber, uint8_t *name); /* returns valid ip port of connected friend on success * returns zeroed out IP_Port on failure */ -IP_Port get_friend_ipport(Messenger *m, int friendnumber); +IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber); /* Set our user status. * You are responsible for freeing status after. @@ -384,7 +387,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status); /* return the length of friendnumber's status message, including null. * Pass it into malloc. */ -int m_get_statusmessage_size(Messenger *m, int friendnumber); +int m_get_statusmessage_size(Messenger *m, int32_t friendnumber); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. @@ -393,7 +396,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber); * returns the length of the copied data on success * retruns -1 on failure. */ -int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); +int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen); int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); /* return one of USERSTATUS values. @@ -401,7 +404,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); +USERSTATUS m_get_userstatus(Messenger *m, int32_t friendnumber); USERSTATUS m_get_self_userstatus(Messenger *m); /* Set our typing status for a friend. @@ -410,19 +413,19 @@ USERSTATUS m_get_self_userstatus(Messenger *m); * returns 0 on success. * returns -1 on failure. */ -int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing); +int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing); /* Get the typing status of a friend. * * returns 0 if friend is not typing. * returns 1 if friend is typing. */ -int m_get_istyping(Messenger *m, int friendnumber); +int m_get_istyping(Messenger *m, int32_t friendnumber); /* Sets whether we send read receipts for friendnumber. * This function is not lazy, and it will fail if yesno is not (0 or 1). */ -void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); +void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno); /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) @@ -430,43 +433,44 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when a message from a friend is received. - * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) + * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) */ -void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when an action from a friend is received. - * Function format is: function(int friendnumber, uint8_t * action, uint32_t length) + * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) */ -void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); +void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), + void *userdata); /* Set the callback for name changes. - * Function(int friendnumber, uint8_t *newname, uint16_t length) + * Function(int32_t friendnumber, uint8_t *newname, uint16_t length) * You are not responsible for freeing newname. */ -void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status message changes. - * Function(int friendnumber, uint8_t *newstatus, uint16_t length) + * Function(int32_t friendnumber, uint8_t *newstatus, uint16_t length) * * You are not responsible for freeing newstatus */ -void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status type changes. - * Function(int friendnumber, USERSTATUS kind) + * Function(int32_t friendnumber, USERSTATUS kind) */ -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, USERSTATUS, void *), void *userdata); /* Set the callback for typing changes. - * Function(int friendnumber, int is_typing) + * Function(int32_t friendnumber, int is_typing) */ -void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, int, void *), void *userdata); +void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int32_t, int, void *), void *userdata); /* Set the callback for read receipts. - * Function(int friendnumber, uint32_t receipt) + * Function(int32_t friendnumber, uint32_t receipt) * * If you are keeping a record of returns from m_sendmessage, * receipt might be one of those values, meaning the message @@ -474,10 +478,10 @@ void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int, in * Since core doesn't track ids for you, receipt may not correspond to any message. * In that case, you should discard it. */ -void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata); +void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int32_t, uint32_t, void *), void *userdata); /* Set the callback for connection status changes. - * function(int friendnumber, uint8_t status) + * function(int32_t friendnumber, uint8_t status) * * Status: * 0 -- friend went offline after being previously online. @@ -487,18 +491,19 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u * being previously online" part. * It's assumed that when adding friends, their connection status is offline. */ -void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); +void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), + void *userdata); /* Same as previous but for internal A/V core usage only */ -void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), +void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata); /**********GROUP CHATS************/ /* Set the callback for group invites. * - * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata) + * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata) */ -void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata); +void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata); /* Set the callback for group messages. * @@ -548,14 +553,14 @@ int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *nam * return 0 on success * return -1 on failure */ -int invite_friend(Messenger *m, int friendnumber, int groupnumber); +int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber); /* Join a group (you need to have been invited first.) * * returns group number on success * returns -1 on failure. */ -int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key); +int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key); /* send a group message * return 0 on success @@ -589,25 +594,27 @@ int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], /* Set the callback for file send requests. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) */ -void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, +void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, + uint16_t, void *), void *userdata); /* Set the callback for file control requests. * - * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) * */ -void callback_file_control(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, +void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for file data. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) * */ -void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *), +void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, + void *), void *userdata); /* Send a file send request. @@ -615,7 +622,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int, uint8_ * return 1 on success * return 0 on failure */ -int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, +int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); /* Send a file send request. @@ -623,7 +630,7 @@ int file_sendrequest(Messenger *m, int friendnumber, uint8_t filenumber, uint64_ * return file number on success * return -1 on failure */ -int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); +int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); /* Send a file control request. * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. @@ -631,7 +638,7 @@ int new_filesender(Messenger *m, int friendnumber, uint64_t filesize, uint8_t *f * return 1 on success * return 0 on failure */ -int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, +int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length); /* Send file data. @@ -639,7 +646,7 @@ int file_control(Messenger *m, int friendnumber, uint8_t send_receive, uint8_t f * return 1 on success * return 0 on failure */ -int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); +int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); /* Give the number of bytes left to be sent/received. * @@ -648,15 +655,15 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, * return number of bytes remaining to be sent/received on success * return 0 on failure */ -uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive); +uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive); /*************** A/V related ******************/ /* Set the callback for msi packets. * - * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) + * Function(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata) */ -void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Send an msi packet. @@ -664,7 +671,7 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uin * return 1 on success * return 0 on failure */ -int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length); +int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length); /**********************************************/ @@ -673,7 +680,7 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length) * return -1 on failure. * return 0 on success. */ -int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t byte, packet_handler_callback cb, +int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, void *object); /* High level function to send custom user packets. @@ -681,7 +688,7 @@ int custom_user_packet_registerhandler(Messenger *m, int friendnumber, uint8_t b * return -1 on failure. * return number of bytes sent on success. */ -int send_custom_user_packet(Messenger *m, int friendnumber, uint8_t *data, uint32_t length); +int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length); /**********************************************/ /* Run this at startup. @@ -747,7 +754,7 @@ uint32_t get_num_online_friends(Messenger *m); * Otherwise, returns the number of elements copied. * If the array was too small, the contents * of out_list will be truncated to list_size. */ -uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size); +uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size); /* Allocate and return a list of valid friend id's. List must be freed by the * caller. diff --git a/toxcore/tox.c b/toxcore/tox.c index 0115e827..ae824697 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -60,7 +60,7 @@ void tox_get_address(Tox *tox, uint8_t *address) * (the nospam for that friend was set to the new one). * return FAERR_NOMEM if increasing the friend list size fails. */ -int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length) +int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length) { Messenger *m = tox; return m_addfriend(m, address, data, length); @@ -71,16 +71,16 @@ int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length) * return the friend number if success. * return -1 if failure. */ -int tox_add_friend_norequest(Tox *tox, uint8_t *client_id) +int32_t tox_add_friend_norequest(Tox *tox, uint8_t *client_id) { Messenger *m = tox; return m_addfriend_norequest(m, client_id); } -/* return the friend id associated to that client id. +/* return the friend number associated to that client id. * return -1 if no such friend. */ -int tox_get_friend_id(Tox *tox, uint8_t *client_id) +int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id) { Messenger *m = tox; return getfriend_id(m, client_id); @@ -92,14 +92,14 @@ int tox_get_friend_id(Tox *tox, uint8_t *client_id) * return 0 if success. * return -1 if failure. */ -int tox_get_client_id(Tox *tox, int friend_id, uint8_t *client_id) +int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id) { Messenger *m = tox; - return getclient_id(m, friend_id, client_id); + return getclient_id(m, friendnumber, client_id); } /* Remove a friend. */ -int tox_del_friend(Tox *tox, int friendnumber) +int tox_del_friend(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return m_delfriend(m, friendnumber); @@ -111,7 +111,7 @@ int tox_del_friend(Tox *tox, int friendnumber) * return 0 if friend is not connected to us (Offline). * return -1 on failure. */ -int tox_get_friend_connection_status(Tox *tox, int friendnumber) +int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return m_get_friend_connectionstatus(m, friendnumber); @@ -122,7 +122,7 @@ int tox_get_friend_connection_status(Tox *tox, int friendnumber) * return 1 if friend exists. * return 0 if friend doesn't exist. */ -int tox_friend_exists(Tox *tox, int friendnumber) +int tox_friend_exists(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return m_friend_exists(m, friendnumber); @@ -137,13 +137,13 @@ int tox_friend_exists(Tox *tox, int friendnumber) * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t tox_send_message(Tox *tox, int friendnumber, uint8_t *message, uint32_t length) +uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length) { Messenger *m = tox; return m_sendmessage(m, friendnumber, message, length); } -uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) +uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) { Messenger *m = tox; return m_sendmessage_withid(m, friendnumber, theid, message, length); @@ -159,13 +159,13 @@ uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uin * m_sendaction_withid will send an action message with the id of your choosing, * however we can generate an id for you by calling plain m_sendaction. */ -uint32_t tox_send_action(Tox *tox, int friendnumber, uint8_t *action, uint32_t length) +uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length) { Messenger *m = tox; return m_sendaction(m, friendnumber, action, length); } -uint32_t tox_send_action_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) +uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) { Messenger *m = tox; return m_sendaction_withid(m, friendnumber, theid, action, length); @@ -187,16 +187,15 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length) /* Get your nickname. * m - The messanger context to use. - * name - Pointer to a string for the name. - * nlen - The length of the string buffer. + * name - Pointer to a string for the name. (must be at least MAX_NAME_LENGTH) * * return length of the name. * return 0 on error. */ -uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen) +uint16_t tox_get_self_name(Tox *tox, uint8_t *name) { Messenger *m = tox; - return getself_name(m, name, nlen); + return getself_name(m, name); } /* Get name of friendnumber and put it in name. @@ -205,7 +204,7 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen) * return length of name (with the NULL terminator) if success. * return -1 if failure. */ -int tox_get_name(Tox *tox, int friendnumber, uint8_t *name) +int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name) { Messenger *m = tox; return getname(m, friendnumber, name); @@ -225,13 +224,13 @@ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length) int tox_set_user_status(Tox *tox, TOX_USERSTATUS status) { Messenger *m = tox; - return m_set_userstatus(m, (USERSTATUS)status); + return m_set_userstatus(m, status); } /* return the length of friendnumber's status message, including null. * Pass it into malloc. */ -int tox_get_status_message_size(Tox *tox, int friendnumber) +int tox_get_status_message_size(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return m_get_statusmessage_size(m, friendnumber); @@ -241,7 +240,7 @@ int tox_get_status_message_size(Tox *tox, int friendnumber) * Get the size you need to allocate from m_get_statusmessage_size. * The self variant will copy our own status message. */ -int tox_get_status_message(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) +int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) { Messenger *m = tox; return m_copy_statusmessage(m, friendnumber, buf, maxlen); @@ -258,7 +257,7 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen) * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber) +TOX_USERSTATUS tox_get_user_status(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return (TOX_USERSTATUS)m_get_userstatus(m, friendnumber); @@ -276,7 +275,7 @@ TOX_USERSTATUS tox_get_self_user_status(Tox *tox) * returns 0 on success. * returns -1 on failure. */ -int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing) +int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing) { Messenger *m = tox; return m_set_usertyping(m, friendnumber, is_typing); @@ -287,7 +286,7 @@ int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing) * returns 0 if friend is not typing. * returns 1 if friend is typing. */ -int tox_get_is_typing(Tox *tox, int friendnumber) +int tox_get_is_typing(Tox *tox, int32_t friendnumber) { Messenger *m = tox; return m_get_istyping(m, friendnumber); @@ -297,7 +296,7 @@ int tox_get_is_typing(Tox *tox, int friendnumber) /* Sets whether we send read receipts for friendnumber. * This function is not lazy, and it will fail if yesno is not (0 or 1). */ -void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno) +void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno) { Messenger *m = tox; m_set_sends_receipts(m, friendnumber, yesno); @@ -324,7 +323,7 @@ uint32_t tox_get_num_online_friends(Tox *tox) * Otherwise, returns the number of elements copied. * If the array was too small, the contents * of out_list will be truncated to list_size. */ -uint32_t tox_get_friendlist(Tox *tox, int *out_list, uint32_t list_size) +uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size) { Messenger *m = tox; return copy_friendlist(m, out_list, list_size); @@ -341,9 +340,9 @@ void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t * /* Set the function that will be executed when a message from a friend is received. - * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) + * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) */ -void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -351,9 +350,9 @@ void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int, } /* Set the function that will be executed when an action from a friend is received. - * function format is: function(int friendnumber, uint8_t * action, uint32_t length) + * function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) */ -void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -361,10 +360,10 @@ void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int, } /* Set the callback for name changes. - * function(int friendnumber, uint8_t *newname, uint16_t length) + * function(int32_t friendnumber, uint8_t *newname, uint16_t length) * You are not responsible for freeing newname. */ -void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -372,10 +371,10 @@ void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int, ui } /* Set the callback for status message changes. - * function(int friendnumber, uint8_t *newstatus, uint16_t length) + * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length) * You are not responsible for freeing newstatus. */ -void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -383,25 +382,26 @@ void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int, } /* Set the callback for status type changes. - * function(int friendnumber, USERSTATUS kind) + * function(int32_t friendnumber, USERSTATUS kind) */ -void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int, TOX_USERSTATUS, void *), void *userdata) +void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int32_t, TOX_USERSTATUS, void *), + void *userdata) { Messenger *m = tox; m_callback_userstatus(m, function, userdata); } /* Set the callback for typing changes. - * function (int friendnumber, int is_typing) + * function (int32_t friendnumber, int is_typing) */ -void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int, int, void *), void *userdata) +void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int32_t, int, void *), void *userdata) { Messenger *m = tox; m_callback_typingchange(m, function, userdata); } /* Set the callback for read receipts. - * function(int friendnumber, uint32_t receipt) + * function(int32_t friendnumber, uint32_t receipt) * * If you are keeping a record of returns from m_sendmessage; * receipt might be one of those values, meaning the message @@ -409,14 +409,14 @@ void tox_callback_typing_change(Tox *tox, void (*function)(Messenger *tox, int, * Since core doesn't track ids for you, receipt may not correspond to any message. * in that case, you should discard it. */ -void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) +void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int32_t, uint32_t, void *), void *userdata) { Messenger *m = tox; m_callback_read_receipt(m, function, userdata); } /* Set the callback for connection status changes. - * function(int friendnumber, uint8_t status) + * function(int32_t friendnumber, uint8_t status) * * Status: * 0 -- friend went offline after being previously online @@ -426,7 +426,8 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int, u * being previously online" part. It's assumed that when adding friends, * their connection status is offline. */ -void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) +void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *), + void *userdata) { Messenger *m = tox; m_callback_connectionstatus(m, function, userdata); @@ -436,9 +437,9 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, i /* Set the callback for group invites. * - * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t *group_public_key, void *userdata) */ -void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, void *), void *userdata) +void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, void *), void *userdata) { Messenger *m = tox; m_callback_group_invite(m, function, userdata); @@ -513,7 +514,7 @@ int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name) * return 0 on success * return -1 on failure */ -int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber) +int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber) { Messenger *m = tox; return invite_friend(m, friendnumber, groupnumber); @@ -523,7 +524,7 @@ int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber) * returns group number on success * returns -1 on failure. */ -int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key) +int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key) { Messenger *m = tox; return join_groupchat(m, friendnumber, friend_group_public_key); @@ -598,9 +599,9 @@ uint32_t tox_get_chatlist(Tox *tox, int *out_list, uint32_t list_size) /* Set the callback for file send requests. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) */ -void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint64_t, uint8_t *, +void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, void *), void *userdata) { @@ -609,10 +610,10 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, i } /* Set the callback for file control requests. * - * Function(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) * */ -void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t, uint8_t, uint8_t *, +void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -620,10 +621,10 @@ void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int, u } /* Set the callback for file data. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) * */ -void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, uint8_t *, uint16_t length, +void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t *, uint16_t length, void *), void *userdata) @@ -636,7 +637,7 @@ void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int, uint * return file number on success * return -1 on failure */ -int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) +int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) { Messenger *m = tox; return new_filesender(m, friendnumber, filesize, filename, filename_length); @@ -647,7 +648,7 @@ int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t * * return 0 on success * return -1 on failure */ -int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, +int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length) { Messenger *m = tox; @@ -658,7 +659,7 @@ int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint * return 0 on success * return -1 on failure */ -int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) +int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) { Messenger *m = tox; return file_data(m, friendnumber, filenumber, data, length); @@ -669,7 +670,7 @@ int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t * * return size on success * return -1 on failure (currently will never return -1) */ -int tox_file_data_size(Tox *tox, int friendnumber) +int tox_file_data_size(Tox *tox, int32_t friendnumber) { return MAX_DATA_SIZE - crypto_box_MACBYTES - 3; } @@ -681,7 +682,7 @@ int tox_file_data_size(Tox *tox, int friendnumber) * return number of bytes remaining to be sent/received on success * return 0 on failure */ -uint64_t tox_file_data_remaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive) +uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) { Messenger *m = tox; return file_dataremaining(m, friendnumber, filenumber, send_receive); diff --git a/toxcore/tox.h b/toxcore/tox.h index 1db5c46e..348ef6e5 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -36,8 +36,6 @@ #include #include -/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ -typedef short sa_family_t; #ifndef true #define true 1 @@ -58,7 +56,7 @@ extern "C" { #endif #define TOX_MAX_NAME_LENGTH 128 -#define TOX_MAX_STATUSMESSAGE_LENGTH 128 +#define TOX_MAX_STATUSMESSAGE_LENGTH 1007 #define TOX_CLIENT_ID_SIZE 32 #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) @@ -67,37 +65,8 @@ extern "C" { #define TOX_PORTRANGE_TO 33545 #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM -typedef union { - uint8_t c[4]; - uint16_t s[2]; - uint32_t i; -} tox_IP4; - -typedef union { - uint8_t uint8[16]; - uint16_t uint16[8]; - uint32_t uint32[4]; - struct in6_addr in6_addr; -} tox_IP6; - -typedef struct { - sa_family_t family; - union { - tox_IP4 ip4; - tox_IP6 ip6; - }; -} tox_IP; - -/* will replace IP_Port as soon as the complete infrastructure is in place - * removed the unused union and padding also */ -typedef struct { - tox_IP ip; - uint16_t port; -} tox_IP_Port; - #define TOX_ENABLE_IPV6_DEFAULT 1 - /* Errors for m_addfriend * FAERR - Friend Add Error */ @@ -132,6 +101,8 @@ typedef struct Tox Tox; * * The length when passing those strings to the core includes that NULL character. * + * The length of all strings returned by the core include the NULL character. + * * If you send non NULL terminated strings Tox will force NULL terminates them when it receives them. */ @@ -156,28 +127,32 @@ void tox_get_address(Tox *tox, uint8_t *address); * (the nospam for that friend was set to the new one). * return TOX_FAERR_NOMEM if increasing the friend list size fails. */ -int tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); +int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); /* Add a friend without sending a friendrequest. * return the friend number if success. * return -1 if failure. */ -int tox_add_friend_norequest(Tox *tox, uint8_t *client_id); +int32_t tox_add_friend_norequest(Tox *tox, uint8_t *client_id); -/* return the friend id associated to that client id. +/* return the friend number associated to that client id. return -1 if no such friend */ -int tox_get_friend_id(Tox *tox, uint8_t *client_id); +int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id); /* Copies the public key associated to that friend id into client_id buffer. * Make sure that client_id is of size CLIENT_ID_SIZE. * return 0 if success. * return -1 if failure. */ -int tox_get_client_id(Tox *tox, int friend_id, uint8_t *client_id); +int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id); -/* Remove a friend. */ -int tox_del_friend(Tox *tox, int friendnumber); +/* Remove a friend. + * + * return 0 if success. + * return -1 if failure. + */ +int tox_del_friend(Tox *tox, int32_t friendnumber); /* Checks friend's connecting status. * @@ -185,14 +160,14 @@ int tox_del_friend(Tox *tox, int friendnumber); * return 0 if friend is not connected to us (Offline). * return -1 on failure. */ -int tox_get_friend_connection_status(Tox *tox, int friendnumber); +int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber); /* Checks if there exists a friend with given friendnumber. * * return 1 if friend exists. * return 0 if friend doesn't exist. */ -int tox_friend_exists(Tox *tox, int friendnumber); +int tox_friend_exists(Tox *tox, int32_t friendnumber); /* Send a text chat message to an online friend. * @@ -204,8 +179,8 @@ int tox_friend_exists(Tox *tox, int friendnumber); * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t tox_send_message(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); -uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); +uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length); +uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length); /* Send an action to an online friend. * @@ -217,8 +192,8 @@ uint32_t tox_send_message_withid(Tox *tox, int friendnumber, uint32_t theid, uin * m_sendaction_withid will send an action message with the id of your choosing, * however we can generate an id for you by calling plain m_sendaction. */ -uint32_t tox_send_action(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); -uint32_t tox_send_action_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); +uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length); +uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length); /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. @@ -233,35 +208,36 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length); /* * Get your nickname. * m - The messanger context to use. - * name - Pointer to a string for the name. - * nlen - The length of the string buffer. + * name - needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. * * return length of name. * return 0 on error. */ -uint16_t tox_get_self_name(Tox *tox, uint8_t *name, uint16_t nlen); +uint16_t tox_get_self_name(Tox *tox, uint8_t *name); /* Get name of friendnumber and put it in name. * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. * - * return length of name (with the NULL terminator) if success. + * return length of name if success. * return -1 if failure. */ -int tox_get_name(Tox *tox, int friendnumber, uint8_t *name); +int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); /* Set our user status. * You are responsible for freeing status after. * + * userstatus must be one of TOX_USERSTATUS values. + * * returns 0 on success. * returns -1 on failure. */ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); -int tox_set_user_status(Tox *tox, TOX_USERSTATUS status); +int tox_set_user_status(Tox *tox, TOX_USERSTATUS userstatus); -/* return the length of friendnumber's status message, including null. +/* return the length of friendnumber's status message. * Pass it into malloc */ -int tox_get_status_message_size(Tox *tox, int friendnumber); +int tox_get_status_message_size(Tox *tox, int32_t friendnumber); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. @@ -270,15 +246,15 @@ int tox_get_status_message_size(Tox *tox, int friendnumber); * returns the length of the copied data on success * retruns -1 on failure. */ -int tox_get_status_message(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); +int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen); int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen); -/* return one of USERSTATUS values. - * Values unknown to your application should be represented as USERSTATUS_NONE. - * As above, the self variant will return our own USERSTATUS. - * If friendnumber is invalid, this shall return USERSTATUS_INVALID. +/* return one of TOX_USERSTATUS values. + * Values unknown to your application should be represented as TOX_USERSTATUS_NONE. + * As above, the self variant will return our own TOX_USERSTATUS. + * If friendnumber is invalid, this shall return TOX_USERSTATUS_INVALID. */ -TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber); +TOX_USERSTATUS tox_get_user_status(Tox *tox, int32_t friendnumber); TOX_USERSTATUS tox_get_self_user_status(Tox *tox); /* Set our typing status for a friend. @@ -287,19 +263,19 @@ TOX_USERSTATUS tox_get_self_user_status(Tox *tox); * returns 0 on success. * returns -1 on failure. */ -int tox_set_user_is_typing(Tox *tox, int friendnumber, uint8_t is_typing); +int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing); /* Get the typing status of a friend. * * returns 0 if friend is not typing. * returns 1 if friend is typing. */ -int tox_get_is_typing(Tox *tox, int friendnumber); +int tox_get_is_typing(Tox *tox, int32_t friendnumber); /* Sets whether we send read receipts for friendnumber. * This function is not lazy, and it will fail if yesno is not (0 or 1). */ -void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); +void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno); /* Return the number of friends in the instance m. * You should use this to determine how much memory to allocate @@ -314,7 +290,7 @@ uint32_t tox_get_num_online_friends(Tox *tox); * Otherwise, returns the number of elements copied. * If the array was too small, the contents * of out_list will be truncated to list_size. */ -uint32_t tox_get_friendlist(Tox *tox, int *out_list, uint32_t list_size); +uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) @@ -328,36 +304,37 @@ void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int, uint8 void *userdata); /* Set the function that will be executed when an action from a friend is received. - * Function format is: function(int friendnumber, uint8_t * action, uint32_t length) + * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) */ -void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); +void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), + void *userdata); /* Set the callback for name changes. - * function(int friendnumber, uint8_t *newname, uint16_t length) + * function(int32_t friendnumber, uint8_t *newname, uint16_t length) * You are not responsible for freeing newname */ -void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status message changes. - * function(int friendnumber, uint8_t *newstatus, uint16_t length) + * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length) * You are not responsible for freeing newstatus. */ -void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status type changes. - * function(int friendnumber, USERSTATUS kind) + * function(int32_t friendnumber, USERSTATUS kind) */ -void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); +void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int32_t, TOX_USERSTATUS, void *), void *userdata); /* Set the callback for typing changes. - * function (int friendnumber, int is_typing) + * function (int32_t friendnumber, int is_typing) */ -void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int, int, void *), void *userdata); +void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int32_t, int, void *), void *userdata); /* Set the callback for read receipts. - * function(int friendnumber, uint32_t receipt) + * function(int32_t friendnumber, uint32_t receipt) * * If you are keeping a record of returns from m_sendmessage; * receipt might be one of those values, meaning the message @@ -365,10 +342,10 @@ void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int, int, v * Since core doesn't track ids for you, receipt may not correspond to any message. * In that case, you should discard it. */ -void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); +void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int32_t, uint32_t, void *), void *userdata); /* Set the callback for connection status changes. - * function(int friendnumber, uint8_t status) + * function(int32_t friendnumber, uint8_t status) * * Status: * 0 -- friend went offline after being previously online @@ -378,7 +355,7 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_ * being previously online" part. it's assumed that when adding friends, * their connection status is offline. */ -void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); +void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t, void *), void *userdata); /**********GROUP CHAT FUNCTIONS: WARNING WILL BREAK A LOT************/ @@ -386,7 +363,7 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int, ui * * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) */ -void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, void *), void *userdata); +void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, void *), void *userdata); /* Set the callback for group messages. * @@ -442,14 +419,14 @@ int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name) * return 0 on success * return -1 on failure */ -int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber); +int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber); /* Join a group (you need to have been invited first.) * * returns group number on success * returns -1 on failure. */ -int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key); +int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key); /* send a group message * return 0 on success @@ -528,9 +505,9 @@ enum { }; /* Set the callback for file send requests. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) */ -void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint64_t, uint8_t *, uint16_t, +void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for file control requests. @@ -538,18 +515,18 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int, uint * receive_send is 1 if the message is for a slot on which we are currently sending a file and 0 if the message * is for a slot on which we are receiving the file * - * Function(Tox *tox, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) * */ -void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t, uint8_t, uint8_t *, +void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for file data. * - * Function(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) + * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) * */ -void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uint8_t *, uint16_t length, void *), +void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *), void *userdata); @@ -558,7 +535,7 @@ void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int, uint8_t, uin * return file number on success * return -1 on failure */ -int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); +int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); /* Send a file control request. * @@ -568,7 +545,7 @@ int tox_new_file_sender(Tox *tox, int friendnumber, uint64_t filesize, uint8_t * * return 0 on success * return -1 on failure */ -int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, +int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, uint8_t *data, uint16_t length); /* Send file data. @@ -576,14 +553,14 @@ int tox_file_send_control(Tox *tox, int friendnumber, uint8_t send_receive, uint * return 0 on success * return -1 on failure */ -int tox_file_send_data(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); +int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); /* Returns the recommended/maximum size of the filedata you send with tox_file_send_data() * * return size on success * return -1 on failure (currently will never return -1) */ -int tox_file_data_size(Tox *tox, int friendnumber); +int tox_file_data_size(Tox *tox, int32_t friendnumber); /* Give the number of bytes left to be sent/received. * @@ -592,19 +569,51 @@ int tox_file_data_size(Tox *tox, int friendnumber); * return number of bytes remaining to be sent/received on success * return 0 on failure */ -uint64_t tox_file_data_remaining(Tox *tox, int friendnumber, uint8_t filenumber, uint8_t send_receive); +uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive); /***************END OF FILE SENDING FUNCTIONS******************/ +/* WARNING: DEPRECATED, DO NOT USE. */ +typedef union { + uint8_t c[4]; + uint16_t s[2]; + uint32_t i; +} tox_IP4; -/* - * Use these two functions to bootstrap the client. - */ +typedef union { + uint8_t uint8[16]; + uint16_t uint16[8]; + uint32_t uint32[4]; + struct in6_addr in6_addr; +} tox_IP6; + +typedef struct { + uint8_t family; + /* Not used for anything right now. */ + uint8_t padding[3]; + union { + tox_IP4 ip4; + tox_IP6 ip6; + }; +} tox_IP; + +/* will replace IP_Port as soon as the complete infrastructure is in place + * removed the unused union and padding also */ +typedef struct { + tox_IP ip; + uint16_t port; +} tox_IP_Port; +/* WARNING: DEPRECATED, DO NOT USE. */ /* Sends a "get nodes" request to the given node with ip, port and public_key * to setup connections */ void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); + +/* + * Use this function to bootstrap the client. + */ + /* Resolves address into an IP address. If successful, sends a "get nodes" * request to the given node with ip, port (in network byte order, HINT: use htons()) * and public_key to setup connections From 36adc48b781105e55790d0049fec1b190104e480 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Fri, 21 Feb 2014 20:30:38 -0500 Subject: [PATCH 2/8] Added size functions for names and status messages --- toxcore/Messenger.c | 21 +++++++++++++++++++++ toxcore/Messenger.h | 11 +++++++++-- toxcore/tox.c | 25 +++++++++++++++++++++++-- toxcore/tox.h | 11 +++++++++-- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 9f75f89f..e3dcf2a1 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -533,6 +533,19 @@ int getname(Messenger *m, int32_t friendnumber, uint8_t *name) return m->friendlist[friendnumber].name_length; } +int m_get_name_size(Messenger *m, int32_t friendnumber) +{ + if (friend_not_valid(m, friendnumber)) + return -1; + + return m->friendlist[friendnumber].name_length; +} + +int m_get_self_name_size(Messenger *m) +{ + return m->name_length; +} + int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) { if (length > MAX_STATUSMESSAGE_LENGTH) @@ -588,6 +601,14 @@ int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint3 return MIN(maxlen, m->friendlist[friendnumber].statusmessage_length); } +/* return the size of friendnumber's user status. + * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. + */ +int m_get_self_statusmessage_size(Messenger *m) +{ + return m->statusmessage_length; +} + int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) { memset(buf, 0, maxlen); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 8474bbe7..88e5e19d 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -370,6 +370,12 @@ uint16_t getself_name(Messenger *m, uint8_t *name); */ int getname(Messenger *m, int32_t friendnumber, uint8_t *name); +/* return the length of name, including null on success. + * return -1 on failure. + */ +int m_get_name_size(Messenger *m, int32_t friendnumber); +int m_get_self_name_size(Messenger *m); + /* returns valid ip port of connected friend on success * returns zeroed out IP_Port on failure */ @@ -384,10 +390,11 @@ IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber); int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); int m_set_userstatus(Messenger *m, USERSTATUS status); -/* return the length of friendnumber's status message, including null. - * Pass it into malloc. +/* return the length of friendnumber's status message, including null on success. + * return -1 on failure. */ int m_get_statusmessage_size(Messenger *m, int32_t friendnumber); +int m_get_self_statusmessage_size(Messenger *m); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. diff --git a/toxcore/tox.c b/toxcore/tox.c index ae824697..f91879ea 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -210,6 +210,21 @@ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name) return getname(m, friendnumber, name); } +/* returns the length of name on success. + * returns -1 on failure. + */ +int tox_get_name_size(Tox *tox, int32_t friendnumber) +{ + Messenger *m = tox; + return m_get_name_size(m, friendnumber); +} + +int tox_get_self_name_size(Tox *tox) +{ + Messenger *m = tox; + return m_get_self_name_size(m); +} + /* Set our user status; * you are responsible for freeing status after. * @@ -227,8 +242,8 @@ int tox_set_user_status(Tox *tox, TOX_USERSTATUS status) return m_set_userstatus(m, status); } -/* return the length of friendnumber's status message, including null. - * Pass it into malloc. +/* returns the length of status message on success. + * returns -1 on failure. */ int tox_get_status_message_size(Tox *tox, int32_t friendnumber) { @@ -236,6 +251,12 @@ int tox_get_status_message_size(Tox *tox, int32_t friendnumber) return m_get_statusmessage_size(m, friendnumber); } +int tox_get_self_status_message_size(Tox *tox) +{ + Messenger *m = tox; + return m_get_self_statusmessage_size(m, friendnumber); +} + /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. * The self variant will copy our own status message. diff --git a/toxcore/tox.h b/toxcore/tox.h index 348ef6e5..17bd4e1a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -223,6 +223,12 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name); */ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); +/* returns the length of name on success. + * returns -1 on failure. + */ +int tox_get_name_size(Tox *tox, int32_t friendnumber); +int tox_get_self_name_size(Tox *tox); + /* Set our user status. * You are responsible for freeing status after. * @@ -234,10 +240,11 @@ int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); int tox_set_user_status(Tox *tox, TOX_USERSTATUS userstatus); -/* return the length of friendnumber's status message. - * Pass it into malloc +/* returns the length of status message on success. + * returns -1 on failure. */ int tox_get_status_message_size(Tox *tox, int32_t friendnumber); +int tox_get_self_status_message_size(Tox *tox); /* Copy friendnumber's status message into buf, truncating if size is over maxlen. * Get the size you need to allocate from m_get_statusmessage_size. From 298f832feb3ae973191a0674654ac382e3d89cbf Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Sat, 22 Feb 2014 00:26:48 -0500 Subject: [PATCH 3/8] Fixed the size function for self status message --- toxcore/tox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/tox.c b/toxcore/tox.c index f91879ea..0b430a90 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -254,7 +254,7 @@ int tox_get_status_message_size(Tox *tox, int32_t friendnumber) int tox_get_self_status_message_size(Tox *tox) { Messenger *m = tox; - return m_get_self_statusmessage_size(m, friendnumber); + return m_get_self_statusmessage_size(m); } /* Copy friendnumber's status message into buf, truncating if size is over maxlen. From 50d2b45541923ecd6c911a2acb6288c7632cc8e7 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 22 Feb 2014 11:52:46 -0500 Subject: [PATCH 4/8] fixed build. --- auto_tests/messenger_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 0393e6d9..5e5f7b06 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -174,7 +174,7 @@ START_TEST(test_getself_name) char nick_check[len]; setname(m, (uint8_t *)nickname, len); - getself_name(m, (uint8_t *)nick_check, len); + getself_name(m, (uint8_t *)nick_check); ck_assert_msg((memcmp(nickname, nick_check, len) == 0), "getself_name failed to return the known name!\n" From f3becc62e84bb7c29b5d366523abdab6eeee8a31 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 23 Feb 2014 10:23:55 -0500 Subject: [PATCH 5/8] Strings now no longer need to be NULL terminated. --- toxcore/Messenger.c | 52 ++++++++++++++++++++++++++------------- toxcore/friend_requests.c | 6 +++-- toxcore/group_chats.c | 4 +-- toxcore/tox.h | 12 ++++----- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e3dcf2a1..da326b88 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -690,7 +690,7 @@ static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t if (friend_not_valid(m, friendnumber)) return -1; - uint8_t *newstatus = calloc(length, 1); + uint8_t *newstatus = calloc(length + 1, 1); memcpy(newstatus, status, length); free(m->friendlist[friendnumber].statusmessage); m->friendlist[friendnumber].statusmessage = newstatus; @@ -957,10 +957,12 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m if (i == -1) return; - message[length - 1] = 0; /* Force NULL terminator */ + uint8_t message_terminated[length + 1]; + memcpy(message_terminated, message, length); + message_terminated[length] = 0; /* Force NULL terminator */ if (m->group_message) - (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); + (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata); } static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) @@ -971,10 +973,12 @@ static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *ac if (i == -1) return; - action[length - 1] = 0; /* Force NULL terminator */ + uint8_t action_terminated[length + 1]; + memcpy(action_terminated, action, length); + action_terminated[length] = 0; /* Force NULL terminator */ if (m->group_action) - (*m->group_action)(m, i, peer_number, action, length, m->group_action_userdata); + (*m->group_action)(m, i, peer_number, action_terminated, length, m->group_action_userdata); } static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) @@ -1912,13 +1916,15 @@ void do_friends(Messenger *m) break; /* Make sure the NULL terminator is present. */ - data[data_length - 1] = 0; + uint8_t data_terminated[data_length + 1]; + memcpy(data_terminated, data, data_length); + data_terminated[data_length] = 0; /* inform of namechange before we overwrite the old name */ if (m->friend_namechange) - m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); + m->friend_namechange(m, i, data_terminated, data_length, m->friend_namechange_userdata); - memcpy(m->friendlist[i].name, data, data_length); + memcpy(m->friendlist[i].name, data_terminated, data_length + 1); m->friendlist[i].name_length = data_length; break; @@ -1928,13 +1934,16 @@ void do_friends(Messenger *m) if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH) break; - data[data_length - 1] = 0; /* Make sure the NULL terminator is present. */ + /* Make sure the NULL terminator is present. */ + uint8_t data_terminated[data_length + 1]; + memcpy(data_terminated, data, data_length); + data_terminated[data_length] = 0; if (m->friend_statusmessagechange) - m->friend_statusmessagechange(m, i, data, data_length, + m->friend_statusmessagechange(m, i, data_terminated, data_length, m->friend_statuschange_userdata); - set_friend_statusmessage(m, i, data, data_length); + set_friend_statusmessage(m, i, data_terminated, data_length); break; } @@ -1974,14 +1983,17 @@ void do_friends(Messenger *m) uint8_t *message = data + message_id_length; uint16_t message_length = data_length - message_id_length; - message[message_length - 1] = 0;/* Make sure the NULL terminator is present. */ + /* Make sure the NULL terminator is present. */ + uint8_t message_terminated[message_length + 1]; + memcpy(message_terminated, message, message_length); + message_terminated[message_length] = 0; if (m->friendlist[i].receives_read_receipts) { write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); } if (m->friend_message) - (*m->friend_message)(m, i, message, message_length, m->friend_message_userdata); + (*m->friend_message)(m, i, message_terminated, message_length, m->friend_message_userdata); break; } @@ -1996,14 +2008,17 @@ void do_friends(Messenger *m) uint8_t *action = data + message_id_length; uint16_t action_length = data_length - message_id_length; - action[action_length - 1] = 0;/* Make sure the NULL terminator is present. */ + /* Make sure the NULL terminator is present. */ + uint8_t action_terminated[action_length + 1]; + memcpy(action_terminated, action, action_length); + action_terminated[action_length] = 0; if (m->friendlist[i].receives_read_receipts) { write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); } if (m->friend_action) - (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata); + (*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata); break; } @@ -2062,10 +2077,13 @@ void do_friends(Messenger *m) m->friendlist[i].file_receiving[filenumber].size = filesize; m->friendlist[i].file_receiving[filenumber].transferred = 0; - data[data_length - 1] = 0; /* Force NULL terminate file name. */ + /* Force NULL terminate file name. */ + uint8_t filename_terminated[data_length - 1 - sizeof(uint64_t) + 1]; + memcpy(filename_terminated, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t)); + filename_terminated[data_length - 1 - sizeof(uint64_t)] = 0; if (m->file_sendrequest) - (*m->file_sendrequest)(m, i, filenumber, filesize, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t), + (*m->file_sendrequest)(m, i, filenumber, filesize, filename_terminated, data_length - 1 - sizeof(uint64_t), m->file_sendrequest_userdata); break; diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 9ac72097..987b1a4a 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -141,9 +141,11 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t addto_receivedlist(fr, source_pubkey); - packet[length - 1] = 0; /* Force NULL terminator. */ + uint8_t message[length - 4 + 1]; + memcpy(message, packet + 4, length - 4); + message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ - (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata); + (*fr->handle_friendrequest)(source_pubkey, message, length - 4, fr->handle_friendrequest_userdata); return 0; } diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 1ec8ede5..22cac286 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -335,7 +335,7 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name) static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) { - if (contents_len > MAX_NICK_BYTES || contents_len == 0) + if (contents_len >= MAX_NICK_BYTES || contents_len == 0) return; /* same name as already stored? */ @@ -345,7 +345,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c memcpy(chat->group[peernum].nick, contents, contents_len); /* Force null termination */ - chat->group[peernum].nick[contents_len - 1] = 0; + chat->group[peernum].nick[contents_len] = 0; chat->group[peernum].nick_len = contents_len; if (chat->peer_namelistchange != NULL) diff --git a/toxcore/tox.h b/toxcore/tox.h index 17bd4e1a..8fb03af3 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -97,13 +97,11 @@ TOX_USERSTATUS; typedef struct Tox Tox; #endif -/* NOTE: Strings in Tox are all UTF-8, also the last byte in all strings must be NULL (0). - * - * The length when passing those strings to the core includes that NULL character. - * - * The length of all strings returned by the core include the NULL character. - * - * If you send non NULL terminated strings Tox will force NULL terminates them when it receives them. +/* NOTE: Strings in Tox are all UTF-8, (This means that there is no terminating NULL character.) + * + * The exact buffer you send will be received at the other end without modification. + * + * Do not treat Tox strings as C strings. */ /* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others. From 199f083e0ae27288e9aba1f70e6690a821f35a78 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 23 Feb 2014 10:38:20 -0500 Subject: [PATCH 6/8] Userstatus function fixes. --- toxcore/Messenger.c | 17 ++++++++--------- toxcore/Messenger.h | 10 +++++----- toxcore/tox.c | 12 ++++++------ toxcore/tox.h | 26 +++++++++++++------------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index da326b88..81a347be 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -562,7 +562,7 @@ int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) return 0; } -int m_set_userstatus(Messenger *m, USERSTATUS status) +int m_set_userstatus(Messenger *m, uint8_t status) { if (status >= USERSTATUS_INVALID) { return -1; @@ -616,12 +616,12 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) return MIN(maxlen, m->statusmessage_length); } -USERSTATUS m_get_userstatus(Messenger *m, int32_t friendnumber) +uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) return USERSTATUS_INVALID; - USERSTATUS status = m->friendlist[friendnumber].userstatus; + uint8_t status = m->friendlist[friendnumber].userstatus; if (status >= USERSTATUS_INVALID) { status = USERSTATUS_NONE; @@ -630,7 +630,7 @@ USERSTATUS m_get_userstatus(Messenger *m, int32_t friendnumber) return status; } -USERSTATUS m_get_self_userstatus(Messenger *m) +uint8_t m_get_self_userstatus(Messenger *m) { return m->userstatus; } @@ -663,10 +663,9 @@ static int send_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *statu return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); } -static int send_userstatus(Messenger *m, int32_t friendnumber, USERSTATUS status) +static int send_userstatus(Messenger *m, int32_t friendnumber, uint8_t status) { - uint8_t stat = status; - return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); + return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status)); } static int send_user_istyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) @@ -698,7 +697,7 @@ static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t return 0; } -static void set_friend_userstatus(Messenger *m, int32_t friendnumber, USERSTATUS status) +static void set_friend_userstatus(Messenger *m, int32_t friendnumber, uint8_t status) { m->friendlist[friendnumber].userstatus = status; } @@ -756,7 +755,7 @@ void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32 m->friend_statuschange_userdata = userdata; } -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, USERSTATUS, void *), void *userdata) +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata) { m->friend_userstatuschange = function; m->friend_userstatuschange_userdata = userdata; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 88e5e19d..213f8586 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -206,7 +206,7 @@ typedef struct Messenger { void *friend_namechange_userdata; void (*friend_statusmessagechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); void *friend_statusmessagechange_userdata; - void (*friend_userstatuschange)(struct Messenger *m, int32_t, USERSTATUS, void *); + void (*friend_userstatuschange)(struct Messenger *m, int32_t, uint8_t, void *); void *friend_userstatuschange_userdata; void (*friend_typingchange)(struct Messenger *m, int32_t, int, void *); void *friend_typingchange_userdata; @@ -388,7 +388,7 @@ IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber); * returns -1 on failure. */ int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); -int m_set_userstatus(Messenger *m, USERSTATUS status); +int m_set_userstatus(Messenger *m, uint8_t status); /* return the length of friendnumber's status message, including null on success. * return -1 on failure. @@ -411,8 +411,8 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -USERSTATUS m_get_userstatus(Messenger *m, int32_t friendnumber); -USERSTATUS m_get_self_userstatus(Messenger *m); +uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber); +uint8_t m_get_self_userstatus(Messenger *m); /* Set our typing status for a friend. * You are responsible for turning it on or off. @@ -469,7 +469,7 @@ void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32 /* Set the callback for status type changes. * Function(int32_t friendnumber, USERSTATUS kind) */ -void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, USERSTATUS, void *), void *userdata); +void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), void *userdata); /* Set the callback for typing changes. * Function(int32_t friendnumber, int is_typing) diff --git a/toxcore/tox.c b/toxcore/tox.c index 0b430a90..d3e596ba 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -236,7 +236,7 @@ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length) return m_set_statusmessage(m, status, length); } -int tox_set_user_status(Tox *tox, TOX_USERSTATUS status) +int tox_set_user_status(Tox *tox, uint8_t status) { Messenger *m = tox; return m_set_userstatus(m, status); @@ -278,16 +278,16 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen) * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -TOX_USERSTATUS tox_get_user_status(Tox *tox, int32_t friendnumber) +uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber) { Messenger *m = tox; - return (TOX_USERSTATUS)m_get_userstatus(m, friendnumber); + return m_get_userstatus(m, friendnumber); } -TOX_USERSTATUS tox_get_self_user_status(Tox *tox) +uint8_t tox_get_self_user_status(Tox *tox) { Messenger *m = tox; - return (TOX_USERSTATUS)m_get_self_userstatus(m); + return m_get_self_userstatus(m); } /* Set our typing status for a friend. @@ -405,7 +405,7 @@ void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int3 /* Set the callback for status type changes. * function(int32_t friendnumber, USERSTATUS kind) */ -void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int32_t, TOX_USERSTATUS, void *), +void tox_callback_user_status(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, void *), void *userdata) { Messenger *m = tox; diff --git a/toxcore/tox.h b/toxcore/tox.h index 8fb03af3..5b619cd0 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -236,7 +236,7 @@ int tox_get_self_name_size(Tox *tox); * returns -1 on failure. */ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); -int tox_set_user_status(Tox *tox, TOX_USERSTATUS userstatus); +int tox_set_user_status(Tox *tox, uint8_t userstatus); /* returns the length of status message on success. * returns -1 on failure. @@ -259,8 +259,8 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen); * As above, the self variant will return our own TOX_USERSTATUS. * If friendnumber is invalid, this shall return TOX_USERSTATUS_INVALID. */ -TOX_USERSTATUS tox_get_user_status(Tox *tox, int32_t friendnumber); -TOX_USERSTATUS tox_get_self_user_status(Tox *tox); +uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber); +uint8_t tox_get_self_user_status(Tox *tox); /* Set our typing status for a friend. * You are responsible for turning it on or off. @@ -298,48 +298,48 @@ uint32_t tox_get_num_online_friends(Tox *tox); uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); /* Set the function that will be executed when a friend request is received. - * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) + * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata) */ void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when a message from a friend is received. - * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) + * Function format is: function(Tox *tox, int friendnumber, uint8_t * message, uint32_t length, void *userdata) */ void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when an action from a friend is received. - * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) + * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint32_t length, void *userdata) */ void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for name changes. - * function(int32_t friendnumber, uint8_t *newname, uint16_t length) + * function(Tox *tox, int32_t friendnumber, uint8_t *newname, uint16_t length, void *userdata) * You are not responsible for freeing newname */ void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status message changes. - * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length) + * function(Tox *tox, int32_t friendnumber, uint8_t *newstatus, uint16_t length, void *userdata) * You are not responsible for freeing newstatus. */ void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), void *userdata); /* Set the callback for status type changes. - * function(int32_t friendnumber, USERSTATUS kind) + * function(Tox *tox, int32_t friendnumber, uint8_t TOX_USERSTATUS, void *userdata) */ -void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int32_t, TOX_USERSTATUS, void *), void *userdata); +void tox_callback_user_status(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t, void *), void *userdata); /* Set the callback for typing changes. - * function (int32_t friendnumber, int is_typing) + * function (Tox *tox, int32_t friendnumber, int is_typing, void *userdata) */ void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int32_t, int, void *), void *userdata); /* Set the callback for read receipts. - * function(int32_t friendnumber, uint32_t receipt) + * function(Tox *tox, int32_t friendnumber, uint32_t receipt, void *userdata) * * If you are keeping a record of returns from m_sendmessage; * receipt might be one of those values, meaning the message @@ -350,7 +350,7 @@ void tox_callback_typing_change(Tox *tox, void (*function)(Tox *tox, int32_t, in void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int32_t, uint32_t, void *), void *userdata); /* Set the callback for connection status changes. - * function(int32_t friendnumber, uint8_t status) + * function(Tox *tox, int32_t friendnumber, uint8_t status, void *userdata) * * Status: * 0 -- friend went offline after being previously online From e1183194678bfd94bf15dfaaf1e153076a07937a Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Fri, 7 Mar 2014 21:12:46 -0500 Subject: [PATCH 7/8] Some tox_wait_* improvements --- toxcore/Messenger.c | 19 +++++++----- toxcore/Messenger.h | 7 +++-- toxcore/network.c | 71 ++++++++++++++++++++++++++++++++------------- toxcore/network.h | 7 +++-- toxcore/tox.c | 19 +++++++----- toxcore/tox.h | 40 ++++++++++++++----------- 6 files changed, 106 insertions(+), 57 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 81a347be..e48fa5fc 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2342,19 +2342,24 @@ void do_messenger(Messenger *m) /* * functions to avoid excessive polling */ -int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr) +size_t wait_data_size() { - return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data, lenptr); + return networking_wait_data_size(); } -int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds) +int wait_prepare_messenger(Messenger *m, uint8_t *data) { - return networking_wait_execute(data, len, milliseconds); -}; + return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data); +} -void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len) +int wait_execute_messenger(uint8_t *data, long seconds, long microseconds) { - networking_wait_cleanup(m->net, data, len); + return networking_wait_execute(data, seconds, microseconds); +} + +int wait_cleanup_messenger(Messenger *m, uint8_t *data) +{ + return networking_wait_cleanup(m->net, data); } /* new messenger format for load/save, more robust and forward compatible */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 213f8586..32b1f1b3 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -715,9 +715,10 @@ void do_messenger(Messenger *m); /* * functions to avoid excessive polling */ -int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr); -int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds); -void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len); +size_t wait_data_size(); +int wait_prepare_messenger(Messenger *m, uint8_t *data); +int wait_execute_messenger(uint8_t *data, long seconds, long microseconds); +int wait_cleanup_messenger(Messenger *m, uint8_t *data); /* SAVING AND LOADING FUNCTIONS: */ diff --git a/toxcore/network.c b/toxcore/network.c index 839618bf..35b55bf1 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -315,17 +315,17 @@ typedef struct { uint64_t send_fail_eagain; } select_info; -int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) +size_t networking_wait_data_size() { - if ((data == NULL) || !lenptr || (*lenptr < sizeof(select_info))) { - if (lenptr) { - *lenptr = sizeof(select_info); - return 0; - } else - return -1; + return sizeof(select_info); +} + +int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data) +{ + if (data == NULL) { + return 0; } - *lenptr = sizeof(select_info); select_info *s = (select_info *)data; s->sock = net->sock; s->sendqueue_length = sendqueue_length; @@ -335,25 +335,41 @@ int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uin return 1; } -int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) +/* *** Function MUSTN'T poll. *** +* The function mustn't modify anything at all, so it can be called completely +* asynchronously without any worry. +*/ +int networking_wait_execute(uint8_t *data, long seconds, long microseconds) { /* WIN32: supported since Win2K, but might need some adjustements */ /* UNIX: this should work for any remotely Unix'ish system */ + if (data == NULL) { + return 0; + } + select_info *s = (select_info *)data; /* add only if we had a failed write */ int writefds_add = 0; + /* if send_fail_eagain is set, that means that socket's buffer was full and couldn't fit data we tried to send, + * so this is the only case when we need to know when the socket becomes write-ready, i.e. socket's buffer gets + * some free space for us to put data to be sent in, but select will tell us that the socket is writable even + * if we can fit a small part of our data (say 1 byte), so we wait some time, in hope that large enough chunk + * of socket's buffer will be available (at least that's how I understand intentions of the previous author of + * that code) + */ if (s->send_fail_eagain != 0) { // current_time(): microseconds uint64_t now = current_time(); /* s->sendqueue_length: might be used to guess how long we keep checking */ - /* for now, threshold is hardcoded to 500ms, too long for a really really + /* for now, threshold is hardcoded to 250ms, too long for a really really * fast link, but too short for a sloooooow link... */ - if (now - s->send_fail_eagain < 500000) + if (now - s->send_fail_eagain < 250000) { writefds_add = 1; + } } int nfds = 1 + s->sock; @@ -366,27 +382,34 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) fd_set writefds; FD_ZERO(&writefds); - if (writefds_add) + if (writefds_add) { FD_SET(s->sock, &writefds); + } fd_set exceptfds; FD_ZERO(&exceptfds); FD_SET(s->sock, &exceptfds); struct timeval timeout; - timeout.tv_sec = 0; - timeout.tv_usec = milliseconds * 1000; + struct timeval *timeout_ptr = &timeout; + + if (seconds < 0 || microseconds < 0) { + timeout_ptr = NULL; + } else { + timeout.tv_sec = seconds; + timeout.tv_usec = microseconds; + } #ifdef LOGGING errno = 0; #endif /* returns -1 on error, 0 on timeout, the socket on activity */ - int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); + int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); #ifdef LOGGING /* only dump if not timeout */ if (res) { - sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, + sprintf(logbuffer, "select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), FD_ISSET(s->sock, &exceptfds)); loglog(logbuffer); @@ -394,18 +417,26 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) #endif - if (FD_ISSET(s->sock, &writefds)) + if (FD_ISSET(s->sock, &writefds)) { s->send_fail_reset = 1; + } - return res > 0 ? 1 : 0; + return res > 0 ? 2 : 1; } -void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len) +int networking_wait_cleanup(Networking_Core *net, uint8_t *data) { + if (data == NULL) { + return 0; + } + select_info *s = (select_info *)data; - if (s->send_fail_reset) + if (s->send_fail_reset) { net->send_fail_eagain = 0; + } + + return 1; } uint8_t at_startup_ran = 0; diff --git a/toxcore/network.h b/toxcore/network.h index aaf89f19..97a73014 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -312,9 +312,10 @@ void networking_poll(Networking_Core *net); /* * functions to avoid excessive polling */ -int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr); -int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds); -void networking_wait_cleanup(Networking_Core *net, uint8_t *data, uint16_t len); +size_t networking_wait_data_size(); +int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data); +int networking_wait_execute(uint8_t *data, long seconds, long microseconds); +int networking_wait_cleanup(Networking_Core *net, uint8_t *data); /* Initialize networking. * bind to ip and port. diff --git a/toxcore/tox.c b/toxcore/tox.c index d3e596ba..aec0d902 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -767,22 +767,27 @@ void tox_do(Tox *tox) /* * functions to avoid excessive polling */ -int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr) + +size_t tox_wait_data_size() { - Messenger *m = tox; - return wait_prepare_messenger(m, data, lenptr); + return wait_data_size(); } -int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds) +int tox_wait_prepare(Tox *tox, uint8_t *data) { Messenger *m = tox; - return wait_execute_messenger(m, data, len, milliseconds); + return wait_prepare_messenger(m, data); } -void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len) +int tox_wait_execute(uint8_t *data, long seconds, long microseconds) +{ + return wait_execute_messenger(data, seconds, microseconds); +} + +int tox_wait_cleanup(Tox *tox, uint8_t *data) { Messenger *m = tox; - wait_cleanup_messenger(m, data, len); + return wait_cleanup_messenger(m, data); } /* SAVING AND LOADING FUNCTIONS: */ diff --git a/toxcore/tox.h b/toxcore/tox.h index 5b619cd0..1c84c1cb 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -662,37 +662,43 @@ void tox_kill(Tox *tox); void tox_do(Tox *tox); /* - * tox_wait_prepare(): function should be called under lock + * tox_wait_data_size(): + * + * returns a size of data buffer to allocate. the size is constant. + * + * tox_wait_prepare(): function should be called under lock every time we want to call tox_wait_execute() * Prepares the data required to call tox_wait_execute() asynchronously * - * data[] is reserved and kept by the caller - * *lenptr is in/out: in = reserved data[], out = required data[] + * data[] should be of at least tox_wait_data_size() size and it's reserved and kept by the caller + * Use that data[] to call tox_wait_execute() * * returns 1 on success - * returns 0 if *lenptr is insufficient - * returns -1 if lenptr is NULL + * returns 0 if data was NULL * * * tox_wait_execute(): function can be called asynchronously - * Waits for something to happen on the socket for up to milliseconds milliseconds. - * *** Function MUSTN'T poll. *** - * The function mustn't modify anything at all, so it can be called completely - * asynchronously without any worry. + * Waits for something to happen on the socket for up to seconds seconds and mircoseconds microseconds. + * mircoseconds should be between 0 and 999999. + * If you set either or both seconds and microseconds to negatives, it will block indefinetly until there + * is an activity. * - * returns 1 if there is socket activity (i.e. tox_do() should be called) - * returns 0 if the timeout was reached - * returns -1 if data was NULL or len too short + * returns 2 if there is socket activity (i.e. tox_do() should be called) + * returns 1 if the timeout was reached (tox_do() should be called anyway. it's advised to call it at least + * once per second) + * returns 0 if data was NULL * * - * tox_wait_cleanup(): function should be called under lock + * tox_wait_cleanup(): function should be called under lock, every time tox_wait_execute() finishes * Stores results from tox_wait_execute(). * - * data[]/len shall be the exact same as given to tox_wait_execute() + * returns 1 on success + * returns 0 if data was NULL * */ -int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr); -int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds); -void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len); +size_t tox_wait_data_size(); +int tox_wait_prepare(Tox *tox, uint8_t *data); +int tox_wait_execute(uint8_t *data, long seconds, long microseconds); +int tox_wait_cleanup(Tox *tox, uint8_t *data); /* SAVING AND LOADING FUNCTIONS: */ From 5babb281c05a48c0b4bbfe3c6be1527f439beec2 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 16 Mar 2014 13:24:39 -0400 Subject: [PATCH 8/8] Friend request callback now contains the Tox object. --- auto_tests/friends_test.c | 2 +- auto_tests/tox_test.c | 15 +++++++++------ testing/Messenger_test.c | 2 +- testing/nTox.c | 2 +- toxav/phone.c | 2 +- toxcore/Messenger.c | 6 ++++-- toxcore/Messenger.h | 3 ++- toxcore/friend_requests.c | 8 +++++--- toxcore/friend_requests.h | 7 ++++--- toxcore/network.c | 2 +- toxcore/tox.c | 3 ++- toxcore/tox.h | 8 ++++---- 12 files changed, 35 insertions(+), 25 deletions(-) diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c index 5071319e..2448f97c 100644 --- a/auto_tests/friends_test.c +++ b/auto_tests/friends_test.c @@ -116,7 +116,7 @@ int parent_friend_request(DHT *dht) return 0; } -void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void child_got_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { fputs("OK\nsending status to parent", stdout); fflush(stdout); diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 7e74fba7..e8c10b7e 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -19,12 +19,13 @@ #define c_sleep(x) usleep(1000*x) #endif -void accept_friend_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void accept_friend_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { - Tox *t = userdata; + if (*((uint32_t *)userdata) != 974536) + return; if (length == 7 && memcmp("Gentoo", data, 7) == 0) { - tox_add_friend_norequest(t, public_key); + tox_add_friend_norequest(m, public_key); } } uint32_t messages_received; @@ -114,7 +115,8 @@ START_TEST(test_few_clients) Tox *tox2 = tox_new(TOX_ENABLE_IPV6_DEFAULT); Tox *tox3 = tox_new(TOX_ENABLE_IPV6_DEFAULT); ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); - tox_callback_friend_request(tox2, accept_friend_request, tox2); + uint32_t to_compare = 974536; + tox_callback_friend_request(tox2, accept_friend_request, &to_compare); uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(tox2, address); int test = tox_add_friend(tox3, address, (uint8_t *)"Gentoo", 7); @@ -140,7 +142,7 @@ START_TEST(test_few_clients) } printf("tox clients connected\n"); - uint32_t to_compare = 974536; + to_compare = 974536; tox_callback_friend_message(tox3, print_message, &to_compare); tox_send_message(tox2, 0, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); @@ -267,7 +269,8 @@ START_TEST(test_many_clients) for (i = 0; i < NUM_TOXES; ++i) { toxes[i] = tox_new(TOX_ENABLE_IPV6_DEFAULT); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); - tox_callback_friend_request(toxes[i], accept_friend_request, toxes[i]); + uint32_t to_comp = 974536; + tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); } struct { diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 895a23d9..b875f832 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -66,7 +66,7 @@ void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t len * networking_requesthandler and so cannot take a Messenger * */ static Messenger *m; -void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void print_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { printf("Friend request received from: \n"); printf("ClientID: "); diff --git a/testing/nTox.c b/testing/nTox.c index 343db236..dc9d5113 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -862,7 +862,7 @@ void do_refresh() refresh(); } -void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void print_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { new_lines("[i] received friend request with message:"); new_lines((char *)data); diff --git a/toxav/phone.c b/toxav/phone.c index 98e97873..bceaad9f 100644 --- a/toxav/phone.c +++ b/toxav/phone.c @@ -1064,7 +1064,7 @@ int av_terminate_session(av_session_t *_phone) /****** AV HELPER FUNCTIONS ******/ /* Auto accept friend request */ -void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata) +void av_friend_requ(Tox *_messenger, uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata) { av_session_t *_phone = _userdata; av_allocate_friend (_phone, -1, 0); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e48fa5fc..7ce6bb3c 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -721,9 +721,11 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno) /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ /* Set the function that will be executed when a friend request is received. */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata) { - callback_friendrequest(&(m->fr), function, userdata); + void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *) = function; + callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata); } /* Set the function that will be executed when a message from a friend is received. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 32b1f1b3..9e91ccee 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -437,7 +437,8 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno); /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata); /* Set the function that will be executed when a message from a friend is received. * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 987b1a4a..7574a881 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -72,11 +72,12 @@ uint32_t get_nospam(Friend_Requests *fr) /* Set the function that will be executed when a friend request is received. */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), - void *userdata) +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), + void *object, void *userdata) { fr->handle_friendrequest = function; fr->handle_friendrequest_isset = 1; + fr->handle_friendrequest_object = object; fr->handle_friendrequest_userdata = userdata; } /* Set the function used to check if a friend request should be displayed to the user or not. */ @@ -145,7 +146,8 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t memcpy(message, packet + 4, length - 4); message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ - (*fr->handle_friendrequest)(source_pubkey, message, length - 4, fr->handle_friendrequest_userdata); + (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, length - 4, + fr->handle_friendrequest_userdata); return 0; } diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 732dc4a2..722c7431 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -30,8 +30,9 @@ typedef struct { uint32_t nospam; - void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); + void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *); uint8_t handle_friendrequest_isset; + void *handle_friendrequest_object; void *handle_friendrequest_userdata; int (*filter_function)(uint8_t *, void *); @@ -57,8 +58,8 @@ uint32_t get_nospam(Friend_Requests *fr); /* Set the function that will be executed when a friend request for us is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata) */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), - void *userdata); +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), + void *object, void *userdata); /* Set the function used to check if a friend request should be displayed to the user or not. * Function format is int function(uint8_t * public_key, void * userdata) diff --git a/toxcore/network.c b/toxcore/network.c index 35b55bf1..e2e0bff4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -435,7 +435,7 @@ int networking_wait_cleanup(Networking_Core *net, uint8_t *data) if (s->send_fail_reset) { net->send_fail_eagain = 0; } - + return 1; } diff --git a/toxcore/tox.c b/toxcore/tox.c index aec0d902..9a5be0ad 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -353,7 +353,8 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size) /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata) { Messenger *m = tox; m_callback_friendrequest(m, function, userdata); diff --git a/toxcore/tox.h b/toxcore/tox.h index 1c84c1cb..41f868dd 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -98,9 +98,9 @@ typedef struct Tox Tox; #endif /* NOTE: Strings in Tox are all UTF-8, (This means that there is no terminating NULL character.) - * + * * The exact buffer you send will be received at the other end without modification. - * + * * Do not treat Tox strings as C strings. */ @@ -228,7 +228,6 @@ int tox_get_name_size(Tox *tox, int32_t friendnumber); int tox_get_self_name_size(Tox *tox); /* Set our user status. - * You are responsible for freeing status after. * * userstatus must be one of TOX_USERSTATUS values. * @@ -300,7 +299,8 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); /* Set the function that will be executed when a friend request is received. * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata) */ -void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata); /* Set the function that will be executed when a message from a friend is received. * Function format is: function(Tox *tox, int friendnumber, uint8_t * message, uint32_t length, void *userdata)