int m_addfriend_norequest(uint8_t * client_id) { if (getfriend_id(client_id) != -1) return -1; /* resize the friend list if necessary */ realloc_friendlist(numfriends + 1); uint32_t i; for (i = 0; i <= numfriends; ++i) { if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_REQUESTED; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].statusmessage = calloc(1, 1); friendlist[i].statusmessage_length = 1; friendlist[i].userstatus = USERSTATUS_NONE; friendlist[i].message_id = 0; friendlist[i].receives_read_receipts = 1; /* default: YES */ ++numfriends; return i; } } return -1; }
/* add a friend set the data that will be sent along with friend request client_id is the client id of the friend data is the data and length is the length returns the friend number if success return -1 if failure. */ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) { if (length == 0 || length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) return -1; if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) return -1; if (getfriend_id(client_id) != -1) return -1; uint32_t i; for (i = 0; i <= numfriends; ++i) { if(friendlist[i].status == 0) { DHT_addfriend(client_id); friendlist[i].status = 1; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].userstatus = calloc(1, 1); friendlist[i].userstatus_length = 1; memcpy(friendlist[i].info, data, length); friendlist[i].info_size = length; ++numfriends; return i; } } return -1; }
int main(int argc, char *argv[]) { Suite *messenger = messenger_suite(); SRunner *test_runner = srunner_create(messenger); int number_failed = 0; friend_id = hex_string_to_bin(friend_id_str); good_id_a = hex_string_to_bin(good_id_a_str); good_id_b = hex_string_to_bin(good_id_b_str); bad_id = hex_string_to_bin(bad_id_str); /* setup a default friend and friendnum */ if(m_addfriend_norequest((uint8_t *)friend_id) < 0) fputs("m_addfriend_norequest() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0) fputs("getfriend_id() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner); srunner_free(test_runner); free(friend_id); free(good_id_a); free(good_id_b); free(bad_id); return number_failed; }
int m_addfriend_norequest(Messenger *m, uint8_t *client_id) { if (getfriend_id(m, client_id) != -1) return -1; /* resize the friend list if necessary */ if (realloc_friendlist(m, m->numfriends + 1) != 0) return FAERR_NOMEM; memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); uint32_t i; for (i = 0; i <= m->numfriends; ++i) { if (m->friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); m->friendlist[i].status = FRIEND_CONFIRMED; m->friendlist[i].crypt_connection_id = -1; m->friendlist[i].friendrequest_lastsent = 0; memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); m->friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage_length = 1; m->friendlist[i].userstatus = USERSTATUS_NONE; m->friendlist[i].message_id = 0; m->friendlist[i].receives_read_receipts = 1; /* default: YES */ if (m->numfriends == i) ++ m->numfriends; return i; } } return -1; }
/* * add a friend * set the data that will be sent along with friend request * client_id is the client id of the friend * data is the data and length is the length * returns the friend number if success * return FA_TOOLONG if message length is too long * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) * return FAERR_OWNKEY if user's own key * return FAERR_ALREADYSENT if friend request already sent or already a friend * return FAERR_UNKNOWN for unknown error */ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) { if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) return FAERR_TOOLONG; if (length < 1) return FAERR_NOMESSAGE; if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) return FAERR_OWNKEY; if (getfriend_id(client_id) != -1) return FAERR_ALREADYSENT; uint32_t i; for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_ADDED; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].userstatus = calloc(1, 1); friendlist[i].userstatus_length = 1; friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE; memcpy(friendlist[i].info, data, length); friendlist[i].info_size = length; ++numfriends; return i; } } return FAERR_UNKNOWN; }
static void doInbound(void) { uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES]; int inconnection = crypto_inbound(public_key, secret_nonce, session_key); if (inconnection != -1) { int friend_id = getfriend_id(public_key); if (friend_id != -1) { crypto_kill(friendlist[friend_id].crypt_connection_id); friendlist[friend_id].crypt_connection_id = accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); set_friend_status(friend_id, FRIEND_CONFIRMED); } } }
int m_addfriend_norequest(uint8_t * client_id) { if (getfriend_id(client_id) != -1) return -1; uint32_t i; for (i = 0; i <= numfriends; ++i) { if(friendlist[i].status == 0) { DHT_addfriend(client_id); friendlist[i].status = 2; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].userstatus = calloc(1, 1); friendlist[i].userstatus_length = 1; numfriends++; return i; } } return -1; }
int m_addfriend_norequest(uint8_t * client_id) { if (getfriend_id(client_id) != -1) return -1; uint32_t i; for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ if(friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_REQUESTED; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].userstatus = calloc(1, 1); friendlist[i].userstatus_length = 1; numfriends++; return i; } } return -1; }
int main(int argc, char *argv[]) { Suite *messenger = messenger_suite(); SRunner *test_runner = srunner_create(messenger); int number_failed = 0; friend_id = hex_string_to_bin(friend_id_str); good_id_a = hex_string_to_bin(good_id_a_str); good_id_b = hex_string_to_bin(good_id_b_str); bad_id = hex_string_to_bin(bad_id_str); /* IPv6 status from global define */ Messenger_Options options = {0}; options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; m = new_messenger(&options); /* setup a default friend and friendnum */ if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) fputs("m_addfriend_norequest() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); if ((friend_id_num = getfriend_id(m, (uint8_t *)friend_id)) < 0) fputs("getfriend_id() failed on a valid ID!\n" "this was CRITICAL to the test, and the build WILL fail.\n" "the tests will continue now...\n\n", stderr); srunner_run_all(test_runner, CK_NORMAL); number_failed = srunner_ntests_failed(test_runner); srunner_free(test_runner); free(friend_id); free(good_id_a); free(good_id_b); free(bad_id); kill_messenger(m); return number_failed; }
/* * add a friend * set the data that will be sent along with friend request * client_id is the client id of the friend * data is the data and length is the length * returns the friend number if success * return FA_TOOLONG if message length is too long * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) * return FAERR_OWNKEY if user's own key * return FAERR_ALREADYSENT if friend request already sent or already a friend * return FAERR_UNKNOWN for unknown error */ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) { if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) return FAERR_TOOLONG; if (length < 1) return FAERR_NOMESSAGE; if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) return FAERR_OWNKEY; if (getfriend_id(client_id) != -1) return FAERR_ALREADYSENT; /* resize the friend list if necessary */ realloc_friendlist(numfriends + 1); uint32_t i; for (i = 0; i <= numfriends; ++i) { if (friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); friendlist[i].status = FRIEND_ADDED; friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); friendlist[i].statusmessage = calloc(1, 1); friendlist[i].statusmessage_length = 1; friendlist[i].userstatus = USERSTATUS_NONE; memcpy(friendlist[i].info, data, length); friendlist[i].info_size = length; friendlist[i].message_id = 0; friendlist[i].receives_read_receipts = 1; /* default: YES */ ++numfriends; return i; } } return FAERR_UNKNOWN; }
/* return the friend id associated to that client id. * return -1 if no such friend. */ int tox_getfriend_id(void *tox, uint8_t *client_id) { Messenger *m = tox; return getfriend_id(m, client_id); }
/* return the friend number associated to that client id. * return -1 if no such friend. */ int32_t tox_get_friend_number(const Tox *tox, const uint8_t *client_id) { const Messenger *m = tox; return getfriend_id(m, client_id); }
/* * add a friend * set the data that will be sent along with friend request * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length * returns the friend number if success * return FA_TOOLONG if message length is too long * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) * return FAERR_OWNKEY if user's own key * return FAERR_ALREADYSENT if friend request already sent or already a friend * return FAERR_UNKNOWN for unknown error * return FAERR_BADCHECKSUM if bad checksum in address * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different * (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) { if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) return FAERR_TOOLONG; uint8_t client_id[crypto_box_PUBLICKEYBYTES]; memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); if (check != checksum) return FAERR_BADCHECKSUM; if (length < 1) return FAERR_NOMESSAGE; if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) return FAERR_OWNKEY; int friend_id = getfriend_id(m, client_id); if (friend_id != -1) { uint32_t nospam; memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(nospam)); if (m->friendlist[friend_id].friendrequest_nospam == nospam) return FAERR_ALREADYSENT; m->friendlist[friend_id].friendrequest_nospam = nospam; return FAERR_SETNEWNOSPAM; } /* resize the friend list if necessary */ if (realloc_friendlist(m, m->numfriends + 1) != 0) return FAERR_NOMEM; memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); uint32_t i; for (i = 0; i <= m->numfriends; ++i) { if (m->friendlist[i].status == NOFRIEND) { DHT_addfriend(client_id); m->friendlist[i].status = FRIEND_ADDED; m->friendlist[i].crypt_connection_id = -1; m->friendlist[i].friendrequest_lastsent = 0; m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT; memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); m->friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage_length = 1; m->friendlist[i].userstatus = USERSTATUS_NONE; memcpy(m->friendlist[i].info, data, length); m->friendlist[i].info_size = length; m->friendlist[i].message_id = 0; m->friendlist[i].receives_read_receipts = 1; /* default: YES */ memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); if (m->numfriends == i) ++ m->numfriends; return i; } } return FAERR_UNKNOWN; }
/* return the friend number associated to that client id. * return -1 if no such friend. */ int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id) { Messenger *m = tox; return getfriend_id(m, client_id); }
/* return the friend id associated to that client id. * return -1 if no such friend. */ int tox_get_friend_id(Tox *tox, size_t *client_id) { Messenger *m = tox; return getfriend_id(m, client_id); }