2013-07-07 10:28:15 +08:00
|
|
|
/* Messenger.c
|
2013-07-26 09:45:56 +08:00
|
|
|
*
|
|
|
|
* An implementation of a simple text chat only messenger on the tox network core.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
*/
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2013-09-05 18:02:26 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-06-19 05:26:02 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
#include "logger.h"
|
2013-07-07 10:28:15 +08:00
|
|
|
#include "Messenger.h"
|
2013-11-15 02:05:36 +08:00
|
|
|
#include "assoc.h"
|
2013-10-02 22:38:54 +08:00
|
|
|
#include "network.h"
|
2013-09-13 14:50:46 +08:00
|
|
|
#include "util.h"
|
2013-08-12 21:19:25 +08:00
|
|
|
|
2013-11-15 02:05:36 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status);
|
2014-07-03 03:41:59 +08:00
|
|
|
static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
|
2014-08-30 09:32:05 +08:00
|
|
|
uint32_t length, uint8_t congestion_control);
|
2013-08-08 13:46:27 +08:00
|
|
|
|
2013-09-02 21:55:37 +08:00
|
|
|
// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
|
2014-06-18 03:34:45 +08:00
|
|
|
static uint8_t friend_not_valid(const Messenger *m, int32_t friendnumber)
|
2013-09-02 21:55:37 +08:00
|
|
|
{
|
2015-02-18 04:46:14 +08:00
|
|
|
if ((unsigned int)friendnumber < m->numfriends) {
|
|
|
|
if (m->friendlist[friendnumber].status != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2013-09-02 21:55:37 +08:00
|
|
|
}
|
2013-09-02 10:42:59 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int add_online_friend(Messenger *m, int32_t friendnumber)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
++m->numonline_friends;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int remove_online_friend(Messenger *m, int32_t friendnumber)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
2014-05-27 01:22:53 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2014-02-15 10:16:31 +08:00
|
|
|
|
2014-05-27 01:22:53 +08:00
|
|
|
--m->numonline_friends;
|
|
|
|
return 0;
|
2014-02-15 10:16:31 +08:00
|
|
|
}
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Set the size of the friend list to numfriends.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return -1 if realloc fails.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2013-08-17 01:11:09 +08:00
|
|
|
int realloc_friendlist(Messenger *m, uint32_t num)
|
|
|
|
{
|
2013-08-18 22:27:03 +08:00
|
|
|
if (num == 0) {
|
|
|
|
free(m->friendlist);
|
|
|
|
m->friendlist = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-18 16:48:36 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
Friend *newfriendlist = realloc(m->friendlist, num * sizeof(Friend));
|
2013-08-18 16:48:36 +08:00
|
|
|
|
2013-08-18 22:27:03 +08:00
|
|
|
if (newfriendlist == NULL)
|
2013-08-08 22:49:36 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist = newfriendlist;
|
2013-08-08 22:49:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-03 00:12:02 +08:00
|
|
|
/* return the friend id associated to that public key.
|
|
|
|
* return -1 if no such friend.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2015-01-28 09:56:39 +08:00
|
|
|
int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-07-27 06:17:24 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status > 0)
|
2015-01-28 09:56:39 +08:00
|
|
|
if (id_equal(real_pk, m->friendlist[i].real_pk))
|
2013-07-10 08:25:52 +08:00
|
|
|
return i;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-07-27 06:17:24 +08:00
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
/* Copies the public key associated to that friend id into real_pk buffer.
|
|
|
|
* Make sure that real_pk is of size crypto_box_PUBLICKEYBYTES.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2015-01-28 09:56:39 +08:00
|
|
|
int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk)
|
2013-07-14 05:44:43 +08:00
|
|
|
{
|
2014-02-22 00:38:04 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-14 05:44:43 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-02-18 04:46:14 +08:00
|
|
|
memcpy(real_pk, m->friendlist[friendnumber].real_pk, crypto_box_PUBLICKEYBYTES);
|
|
|
|
return 0;
|
2013-07-14 05:44:43 +08:00
|
|
|
}
|
2014-09-29 01:25:29 +08:00
|
|
|
|
|
|
|
/* return friend connection id on success.
|
|
|
|
* return -1 if failure.
|
|
|
|
*/
|
|
|
|
int getfriendcon_id(const Messenger *m, int32_t friendnumber)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].friendcon_id;
|
|
|
|
}
|
|
|
|
|
2015-03-05 09:28:17 +08:00
|
|
|
/*
|
2013-09-03 00:12:02 +08:00
|
|
|
* return a uint16_t that represents the checksum of address of length len.
|
2013-08-14 07:07:59 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
static uint16_t address_checksum(const uint8_t *address, uint32_t len)
|
2013-08-14 07:07:59 +08:00
|
|
|
{
|
|
|
|
uint8_t checksum[2] = {0};
|
|
|
|
uint16_t check;
|
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
2013-08-14 07:07:59 +08:00
|
|
|
checksum[i % 2] ^= address[i];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 07:07:59 +08:00
|
|
|
memcpy(&check, checksum, sizeof(check));
|
|
|
|
return check;
|
|
|
|
}
|
2013-07-14 05:44:43 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
/* Format: [real_pk (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
|
2013-08-14 13:27:10 +08:00
|
|
|
*
|
2013-09-03 00:12:02 +08:00
|
|
|
* return FRIEND_ADDRESS_SIZE byte address to give to others.
|
2013-08-13 21:32:31 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
void getaddress(const Messenger *m, uint8_t *address)
|
2013-08-13 21:32:31 +08:00
|
|
|
{
|
2013-10-25 04:32:28 +08:00
|
|
|
id_copy(address, m->net_crypto->self_public_key);
|
2013-08-21 00:08:55 +08:00
|
|
|
uint32_t nospam = get_nospam(&(m->fr));
|
2013-08-13 21:32:31 +08:00
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam));
|
2013-08-14 07:07:59 +08:00
|
|
|
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(nospam), &checksum, sizeof(checksum));
|
2013-08-13 21:32:31 +08:00
|
|
|
}
|
|
|
|
|
2014-10-03 05:33:54 +08:00
|
|
|
static int send_online_packet(Messenger *m, int32_t friendnumber)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t packet = PACKET_ID_ONLINE;
|
|
|
|
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), &packet, sizeof(packet), 0) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_offine_packet(Messenger *m, int friendcon_id)
|
|
|
|
{
|
|
|
|
uint8_t packet = PACKET_ID_OFFLINE;
|
|
|
|
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
|
|
|
|
sizeof(packet), 0) != -1;
|
|
|
|
}
|
|
|
|
|
2014-09-28 06:25:03 +08:00
|
|
|
static int handle_status(void *object, int i, uint8_t status);
|
|
|
|
static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len);
|
|
|
|
static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length);
|
2014-09-27 08:32:38 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t status)
|
2015-01-24 09:56:46 +08:00
|
|
|
{
|
|
|
|
/* 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));
|
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
int friendcon_id = new_friend_connection(m->fr_c, real_pk);
|
2015-01-24 09:56:46 +08:00
|
|
|
|
|
|
|
if (friendcon_id == -1)
|
2015-02-17 23:29:18 +08:00
|
|
|
return FAERR_NOMEM;
|
2015-01-24 09:56:46 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i <= m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status == NOFRIEND) {
|
|
|
|
m->friendlist[i].status = status;
|
|
|
|
m->friendlist[i].friendcon_id = friendcon_id;
|
|
|
|
m->friendlist[i].friendrequest_lastsent = 0;
|
2015-01-28 09:56:39 +08:00
|
|
|
id_copy(m->friendlist[i].real_pk, real_pk);
|
2015-02-17 09:25:44 +08:00
|
|
|
m->friendlist[i].statusmessage_length = 0;
|
2015-01-24 09:56:46 +08:00
|
|
|
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
|
|
|
m->friendlist[i].is_typing = 0;
|
|
|
|
m->friendlist[i].message_id = 0;
|
|
|
|
friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &handle_status, &handle_packet,
|
|
|
|
&handle_custom_lossy_packet, m, i);
|
|
|
|
|
|
|
|
if (m->numfriends == i)
|
|
|
|
++m->numfriends;
|
|
|
|
|
|
|
|
if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
|
|
|
|
send_online_packet(m, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 23:29:18 +08:00
|
|
|
return FAERR_NOMEM;
|
2015-01-24 09:56:46 +08:00
|
|
|
}
|
|
|
|
|
2013-08-02 03:27:08 +08:00
|
|
|
/*
|
2013-08-30 05:17:51 +08:00
|
|
|
* 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.
|
|
|
|
* data is the data and length is the length.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 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_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.
|
2013-08-02 03:27:08 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2014-04-23 23:35:40 +08:00
|
|
|
if (length > MAX_FRIEND_REQUEST_DATA_SIZE)
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_TOOLONG;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
uint8_t real_pk[crypto_box_PUBLICKEYBYTES];
|
|
|
|
id_copy(real_pk, address);
|
2014-06-17 07:31:39 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
if (!public_key_valid(real_pk))
|
2014-06-17 07:31:39 +08:00
|
|
|
return FAERR_BADCHECKSUM;
|
|
|
|
|
2013-08-14 07:07:59 +08:00
|
|
|
uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
|
|
|
memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check));
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 07:07:59 +08:00
|
|
|
if (check != checksum)
|
|
|
|
return FAERR_BADCHECKSUM;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-02 03:27:08 +08:00
|
|
|
if (length < 1)
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_NOMESSAGE;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
if (id_equal(real_pk, m->net_crypto->self_public_key))
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_OWNKEY;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
int32_t friend_id = getfriend_id(m, real_pk);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
if (friend_id != -1) {
|
2014-04-02 04:36:33 +08:00
|
|
|
if (m->friendlist[friend_id].status >= FRIEND_CONFIRMED)
|
|
|
|
return FAERR_ALREADYSENT;
|
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
uint32_t nospam;
|
2013-08-14 07:07:59 +08:00
|
|
|
memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(nospam));
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (m->friendlist[friend_id].friendrequest_nospam == nospam)
|
2013-08-14 05:25:52 +08:00
|
|
|
return FAERR_ALREADYSENT;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
m->friendlist[friend_id].friendrequest_nospam = nospam;
|
2013-08-14 07:07:59 +08:00
|
|
|
return FAERR_SETNEWNOSPAM;
|
2013-08-14 05:25:52 +08:00
|
|
|
}
|
2013-08-02 03:27:08 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
int32_t ret = init_new_friend(m, real_pk, FRIEND_ADDED);
|
2013-08-08 22:49:36 +08:00
|
|
|
|
2015-01-24 09:56:46 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2013-07-10 08:25:52 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-01-24 09:56:46 +08:00
|
|
|
m->friendlist[ret].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
|
|
|
|
memcpy(m->friendlist[ret].info, data, length);
|
|
|
|
m->friendlist[ret].info_size = length;
|
|
|
|
memcpy(&(m->friendlist[ret].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
|
|
|
|
|
|
|
|
return ret;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk)
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
2015-01-28 09:56:39 +08:00
|
|
|
if (getfriend_id(m, real_pk) != -1)
|
2015-02-17 23:29:18 +08:00
|
|
|
return FAERR_ALREADYSENT;
|
2013-08-08 22:49:36 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
if (!public_key_valid(real_pk))
|
2015-02-17 23:29:18 +08:00
|
|
|
return FAERR_BADCHECKSUM;
|
2014-06-17 07:31:39 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
if (id_equal(real_pk, m->net_crypto->self_public_key))
|
2015-02-17 23:29:18 +08:00
|
|
|
return FAERR_OWNKEY;
|
2013-08-17 23:16:08 +08:00
|
|
|
|
2015-02-17 23:29:18 +08:00
|
|
|
return init_new_friend(m, real_pk, FRIEND_CONFIRMED);
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
static int clear_receipts(Messenger *m, int32_t friendnumber)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2014-08-28 03:13:44 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
while (receipts) {
|
|
|
|
struct Receipts *temp_r = receipts->next;
|
|
|
|
free(receipts);
|
|
|
|
receipts = temp_r;
|
2013-08-11 20:15:07 +08:00
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
m->friendlist[friendnumber].receipts_start = NULL;
|
|
|
|
m->friendlist[friendnumber].receipts_end = NULL;
|
2013-08-11 20:15:07 +08:00
|
|
|
return 0;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, uint32_t msg_id)
|
2013-08-08 00:53:52 +08:00
|
|
|
{
|
2014-08-28 03:13:44 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
struct Receipts *new = calloc(1, sizeof(struct Receipts));
|
|
|
|
|
|
|
|
if (!new)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
new->packet_num = packet_num;
|
|
|
|
new->msg_id = msg_id;
|
|
|
|
|
|
|
|
if (!m->friendlist[friendnumber].receipts_start) {
|
|
|
|
m->friendlist[friendnumber].receipts_start = new;
|
|
|
|
} else {
|
|
|
|
m->friendlist[friendnumber].receipts_end->next = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->friendlist[friendnumber].receipts_end = new;
|
|
|
|
new->next = NULL;
|
|
|
|
return 0;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
/*
|
|
|
|
* return -1 on failure.
|
|
|
|
* return 0 if packet was received.
|
|
|
|
*/
|
|
|
|
static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint32_t number)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), number);
|
|
|
|
}
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
static int do_receipts(Messenger *m, int32_t friendnumber)
|
2013-10-11 10:27:51 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
2014-08-28 03:13:44 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
|
2013-10-11 10:27:51 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
while (receipts) {
|
|
|
|
struct Receipts *temp_r = receipts->next;
|
2013-10-11 10:27:51 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (friend_received_packet(m, friendnumber, receipts->packet_num) == -1)
|
2014-08-28 03:13:44 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (m->read_receipt)
|
|
|
|
(*m->read_receipt)(m, friendnumber, receipts->msg_id, m->read_receipt_userdata);
|
2013-10-11 10:27:51 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
free(receipts);
|
|
|
|
m->friendlist[friendnumber].receipts_start = temp_r;
|
|
|
|
receipts = temp_r;
|
2013-10-11 10:27:51 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
if (!m->friendlist[friendnumber].receipts_start)
|
|
|
|
m->friendlist[friendnumber].receipts_end = NULL;
|
|
|
|
|
2013-10-11 10:27:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Remove a friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
2013-08-30 05:17:51 +08:00
|
|
|
* return 0 if success.
|
2013-08-30 05:55:58 +08:00
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_delfriend(Messenger *m, int32_t friendnumber)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
|
|
|
|
2014-02-08 09:11:36 +08:00
|
|
|
if (m->friendlist[friendnumber].status == FRIEND_ONLINE)
|
2014-02-15 10:16:31 +08:00
|
|
|
remove_online_friend(m, friendnumber);
|
2014-02-08 09:11:36 +08:00
|
|
|
|
2015-02-21 05:39:33 +08:00
|
|
|
clear_receipts(m, friendnumber);
|
2015-01-28 09:56:39 +08:00
|
|
|
remove_request_received(&(m->fr), m->friendlist[friendnumber].real_pk);
|
2014-09-28 06:25:03 +08:00
|
|
|
friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0);
|
|
|
|
kill_friend_connection(m->fr_c, m->friendlist[friendnumber].friendcon_id);
|
2014-10-03 05:33:54 +08:00
|
|
|
|
|
|
|
if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
|
|
|
|
send_offine_packet(m, m->friendlist[friendnumber].friendcon_id);
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
|
2013-07-10 08:25:52 +08:00
|
|
|
uint32_t i;
|
2013-08-01 06:01:59 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = m->numfriends; i != 0; --i) {
|
2013-08-17 01:11:09 +08:00
|
|
|
if (m->friendlist[i - 1].status != NOFRIEND)
|
2013-07-10 08:25:52 +08:00
|
|
|
break;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->numfriends = i;
|
2013-08-15 21:34:27 +08:00
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
if (realloc_friendlist(m, m->numfriends) != 0)
|
2013-08-15 21:34:27 +08:00
|
|
|
return FAERR_NOMEM;
|
2013-08-01 06:01:59 +08:00
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return 0;
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
|
2013-09-08 04:05:16 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
2015-02-20 09:51:19 +08:00
|
|
|
if (m->friendlist[friendnumber].status == FRIEND_ONLINE) {
|
|
|
|
uint8_t direct_connected = 0;
|
|
|
|
crypto_connection_status(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), &direct_connected);
|
|
|
|
|
|
|
|
if (direct_connected) {
|
|
|
|
return CONNECTION_UDP;
|
|
|
|
} else {
|
|
|
|
return CONNECTION_TCP;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return CONNECTION_NONE;
|
|
|
|
}
|
2013-09-08 04:05:16 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_friend_exists(const Messenger *m, int32_t friendnumber)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-09-08 04:05:16 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-18 04:46:14 +08:00
|
|
|
return 1;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 06:24:24 +08:00
|
|
|
/* Send a packet_id message.
|
|
|
|
*
|
|
|
|
* return -1 if friend not valid.
|
|
|
|
* return -2 if too large.
|
|
|
|
* return -3 if friend not online.
|
|
|
|
* return -4 if send failed (because queue is full).
|
|
|
|
* return 0 if success.
|
|
|
|
*/
|
|
|
|
static int send_message_generic(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length,
|
|
|
|
uint8_t packet_id, uint32_t *message_id)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2015-02-21 06:24:24 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-21 06:24:24 +08:00
|
|
|
if (length >= MAX_CRYPTO_DATA_SIZE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -3;
|
2014-08-28 03:13:44 +08:00
|
|
|
|
|
|
|
uint8_t packet[length + 1];
|
|
|
|
packet[0] = packet_id;
|
|
|
|
|
|
|
|
if (length != 0)
|
|
|
|
memcpy(packet + 1, message, length);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-21 06:24:24 +08:00
|
|
|
int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), packet, length + 1, 0);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
if (packet_num == -1)
|
2015-02-21 06:24:24 +08:00
|
|
|
return -4;
|
2014-08-28 03:13:44 +08:00
|
|
|
|
|
|
|
uint32_t msg_id = ++m->friendlist[friendnumber].message_id;
|
|
|
|
|
|
|
|
if (msg_id == 0) {
|
|
|
|
msg_id = ++m->friendlist[friendnumber].message_id; // Otherwise, false error
|
2013-08-11 20:15:07 +08:00
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
add_receipt(m, friendnumber, packet_num, msg_id);
|
2015-02-21 06:24:24 +08:00
|
|
|
|
|
|
|
if (message_id)
|
|
|
|
*message_id = msg_id;
|
|
|
|
|
|
|
|
return 0;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 06:24:24 +08:00
|
|
|
int m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, uint32_t *message_id)
|
2013-08-08 00:53:52 +08:00
|
|
|
{
|
2015-02-21 06:24:24 +08:00
|
|
|
return send_message_generic(m, friendnumber, message, length, PACKET_ID_MESSAGE, message_id);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 06:24:24 +08:00
|
|
|
int m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length, uint32_t *message_id)
|
2013-10-11 10:27:51 +08:00
|
|
|
{
|
2015-02-21 06:24:24 +08:00
|
|
|
return send_message_generic(m, friendnumber, action, length, PACKET_ID_ACTION, message_id);
|
2013-08-09 03:00:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Send a name packet to friendnumber.
|
|
|
|
* length is the length with the NULL terminator.
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2015-02-17 09:25:44 +08:00
|
|
|
if (length > MAX_NAME_LENGTH)
|
2013-07-28 09:08:06 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-30 09:32:05 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length, 0);
|
2013-07-18 23:47:27 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 18:41:33 +08:00
|
|
|
/* Set the name and name_length of a friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-24 03:40:25 +08:00
|
|
|
if (length > MAX_NAME_LENGTH || length == 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-09-23 16:30:24 +08:00
|
|
|
m->friendlist[friendnumber].name_length = length;
|
|
|
|
memcpy(m->friendlist[friendnumber].name, name, length);
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Set our nickname
|
2013-08-30 05:17:51 +08:00
|
|
|
* name must be a string of maximum MAX_NAME_LENGTH length.
|
|
|
|
* length must be at least 1 byte.
|
|
|
|
* length is the length of name with the NULL terminator.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-06-11 02:54:48 +08:00
|
|
|
int setname(Messenger *m, const uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2015-02-17 09:25:44 +08:00
|
|
|
if (length > MAX_NAME_LENGTH)
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-17 09:25:44 +08:00
|
|
|
if (m->name_length == length && (length == 0 || memcmp(name, m->name, length) == 0))
|
2015-01-31 09:29:33 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-02-17 09:25:44 +08:00
|
|
|
if (length)
|
|
|
|
memcpy(m->name, name, length);
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->name_length = length;
|
2013-07-18 23:47:27 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].name_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Get our nickname and put it in name.
|
2013-09-03 00:12:02 +08:00
|
|
|
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
|
|
|
*
|
|
|
|
* return the length of the name.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
uint16_t getself_name(const Messenger *m, uint8_t *name)
|
2013-07-30 23:09:09 +08:00
|
|
|
{
|
2014-02-22 00:38:04 +08:00
|
|
|
if (name == NULL) {
|
2013-08-11 21:24:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
memcpy(name, m->name, m->name_length);
|
2013-08-11 21:24:47 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
return m->name_length;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
2013-07-30 23:09:09 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Get name of friendnumber and put it in name.
|
2013-09-03 00:12:02 +08:00
|
|
|
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
|
|
|
*
|
2013-09-06 21:45:39 +08:00
|
|
|
* return length of name if success.
|
2013-09-03 00:12:02 +08:00
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int getname(const Messenger *m, int32_t friendnumber, uint8_t *name)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-06 21:45:39 +08:00
|
|
|
memcpy(name, m->friendlist[friendnumber].name, m->friendlist[friendnumber].name_length);
|
|
|
|
return m->friendlist[friendnumber].name_length;
|
2013-07-18 23:47:27 +08:00
|
|
|
}
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_get_name_size(const Messenger *m, int32_t friendnumber)
|
2014-02-22 09:30:38 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].name_length;
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_get_self_name_size(const Messenger *m)
|
2014-02-22 09:30:38 +08:00
|
|
|
{
|
|
|
|
return m->name_length;
|
|
|
|
}
|
|
|
|
|
2014-06-11 02:54:48 +08:00
|
|
|
int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-08 05:01:31 +08:00
|
|
|
if (length > MAX_STATUSMESSAGE_LENGTH)
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-17 09:25:44 +08:00
|
|
|
if (m->statusmessage_length == length && (length == 0 || memcmp(m->statusmessage, status, length) == 0))
|
2015-01-31 09:29:33 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-02-17 09:25:44 +08:00
|
|
|
if (length)
|
|
|
|
memcpy(m->statusmessage, status, length);
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->statusmessage_length = length;
|
2013-07-19 01:56:50 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].statusmessage_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
int m_set_userstatus(Messenger *m, uint8_t status)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2015-01-31 09:29:33 +08:00
|
|
|
if (status >= USERSTATUS_INVALID)
|
2013-08-06 03:30:07 +08:00
|
|
|
return -1;
|
2015-01-31 09:29:33 +08:00
|
|
|
|
|
|
|
if (m->userstatus == status)
|
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->userstatus = status;
|
2013-08-06 03:30:07 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].userstatus_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-06 03:30:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* return the size of friendnumber's user status.
|
|
|
|
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m->friendlist[friendnumber].statusmessage_length;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-06-19 10:38:25 +08:00
|
|
|
int msglen = MIN(maxlen, m->friendlist[friendnumber].statusmessage_length);
|
2014-06-20 22:05:35 +08:00
|
|
|
|
2014-06-19 10:38:25 +08:00
|
|
|
memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
|
2014-06-20 22:05:35 +08:00
|
|
|
memset(buf + msglen, 0, maxlen - msglen);
|
2014-06-19 10:38:25 +08:00
|
|
|
return msglen;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 09:30:38 +08:00
|
|
|
/* return the size of friendnumber's user status.
|
|
|
|
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_get_self_statusmessage_size(const Messenger *m)
|
2014-02-22 09:30:38 +08:00
|
|
|
{
|
|
|
|
return m->statusmessage_length;
|
|
|
|
}
|
|
|
|
|
2015-02-17 09:57:44 +08:00
|
|
|
int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf)
|
2013-08-06 03:30:07 +08:00
|
|
|
{
|
2015-02-17 09:57:44 +08:00
|
|
|
memcpy(buf, m->statusmessage, m->statusmessage_length);
|
|
|
|
return m->statusmessage_length;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-08-08 05:01:31 +08:00
|
|
|
return USERSTATUS_INVALID;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
uint8_t status = m->friendlist[friendnumber].userstatus;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 05:01:31 +08:00
|
|
|
if (status >= USERSTATUS_INVALID) {
|
|
|
|
status = USERSTATUS_NONE;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 05:01:31 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
uint8_t m_get_self_userstatus(const Messenger *m)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
return m->userstatus;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber)
|
2014-03-14 08:20:48 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].ping_lastrecv;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-03-19 08:02:50 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
2015-01-31 09:29:33 +08:00
|
|
|
if (is_typing != 0 && is_typing != 1)
|
2014-02-17 05:33:37 +08:00
|
|
|
return -1;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2015-01-31 09:29:33 +08:00
|
|
|
if (m->friendlist[friendnumber].user_istyping == is_typing)
|
|
|
|
return 0;
|
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
m->friendlist[friendnumber].user_istyping = is_typing;
|
|
|
|
m->friendlist[friendnumber].user_istyping_sent = 0;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-20 05:54:49 +08:00
|
|
|
int m_get_istyping(const Messenger *m, int32_t friendnumber)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
return m->friendlist[friendnumber].is_typing;
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int send_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2014-08-30 09:32:05 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length, 0);
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int send_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2014-08-30 09:32:05 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status), 0);
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
uint8_t typing = is_typing;
|
2014-08-30 09:32:05 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0);
|
2014-02-17 05:33:37 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int send_relays(const Messenger *m, int32_t friendnumber)
|
2014-05-20 05:39:50 +08:00
|
|
|
{
|
2014-05-20 07:03:52 +08:00
|
|
|
Node_format nodes[MAX_SHARED_RELAYS];
|
2014-05-20 05:39:50 +08:00
|
|
|
uint8_t data[1024];
|
|
|
|
int n, length;
|
|
|
|
|
2014-05-20 07:03:52 +08:00
|
|
|
n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS);
|
2014-05-20 05:39:50 +08:00
|
|
|
length = pack_nodes(data, sizeof(data), nodes, n);
|
|
|
|
|
2014-08-30 09:32:05 +08:00
|
|
|
int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0);
|
2014-05-20 05:39:50 +08:00
|
|
|
|
|
|
|
if (ret == 1)
|
|
|
|
m->friendlist[friendnumber].share_relays_lastsent = unix_time();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2015-02-17 09:25:44 +08:00
|
|
|
if (length > MAX_STATUSMESSAGE_LENGTH)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
memcpy(m->friendlist[friendnumber].statusmessage, status, length);
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[friendnumber].statusmessage_length = length;
|
2013-07-19 01:56:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-08-06 03:30:07 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[friendnumber].userstatus = status;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
m->friendlist[friendnumber].is_typing = is_typing;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Set the function that will be executed when a friend request is received. */
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t,
|
2014-06-11 06:35:55 +08:00
|
|
|
void *), void *userdata)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2015-02-18 04:49:22 +08:00
|
|
|
void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, size_t, void *) = (void *)function;
|
2014-03-17 01:24:39 +08:00
|
|
|
callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Set the function that will be executed when a message from a friend is received. */
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
void *userdata)
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_message = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_message_userdata = userdata;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_action(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *),
|
2014-02-22 00:38:04 +08:00
|
|
|
void *userdata)
|
2013-08-09 03:00:30 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_action = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_action_userdata = userdata;
|
2013-08-09 03:00:30 +08:00
|
|
|
}
|
|
|
|
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
void *userdata)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_namechange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_namechange_userdata = userdata;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
void *userdata)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_statusmessagechange = function;
|
2015-03-06 01:30:50 +08:00
|
|
|
m->friend_statusmessagechange_userdata = userdata;
|
2013-08-08 05:01:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-06 01:49:38 +08:00
|
|
|
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, void *), void *userdata)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_userstatuschange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_userstatuschange_userdata = userdata;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 07:29:32 +08:00
|
|
|
void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, uint32_t, _Bool, void *), void *userdata)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
m->friend_typingchange = function;
|
|
|
|
m->friend_typingchange_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, void *), void *userdata)
|
2013-08-08 00:53:52 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->read_receipt = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->read_receipt_userdata = userdata;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
|
|
|
|
2015-03-06 01:49:38 +08:00
|
|
|
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, void *),
|
2015-02-18 04:49:22 +08:00
|
|
|
void *userdata)
|
2013-08-08 13:46:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_connectionstatuschange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_connectionstatuschange_userdata = userdata;
|
2013-08-12 02:57:47 +08:00
|
|
|
}
|
2014-02-16 02:10:53 +08:00
|
|
|
|
2015-03-12 03:37:25 +08:00
|
|
|
void m_callback_core_connection(Messenger *m, void (*function)(Messenger *m, unsigned int, void *), void *userdata)
|
|
|
|
{
|
|
|
|
m->core_connection_change = function;
|
|
|
|
m->core_connection_change_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, uint32_t, uint8_t, void *),
|
2014-02-16 02:10:53 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->friend_connectionstatuschange_internal = function;
|
|
|
|
m->friend_connectionstatuschange_internal_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:51:19 +08:00
|
|
|
static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber)
|
|
|
|
{
|
|
|
|
int last_connection_udp_tcp = m->friendlist[friendnumber].last_connection_udp_tcp;
|
|
|
|
|
|
|
|
int ret = m_get_friend_connectionstatus(m, friendnumber);
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (last_connection_udp_tcp != ret) {
|
|
|
|
if (m->friend_connectionstatuschange)
|
|
|
|
m->friend_connectionstatuschange(m, friendnumber, ret, m->friend_connectionstatuschange_userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
m->friendlist[friendnumber].last_connection_udp_tcp = ret;
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static void break_files(const Messenger *m, int32_t friendnumber);
|
2014-02-22 00:38:04 +08:00
|
|
|
static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-08-12 02:57:47 +08:00
|
|
|
{
|
|
|
|
if (status == NOFRIEND)
|
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-27 17:24:33 +08:00
|
|
|
const uint8_t was_online = m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
|
|
|
const uint8_t is_online = status == FRIEND_ONLINE;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
if (is_online != was_online) {
|
2014-02-08 08:43:21 +08:00
|
|
|
if (was_online) {
|
2013-10-02 04:48:32 +08:00
|
|
|
break_files(m, friendnumber);
|
2014-02-15 10:16:31 +08:00
|
|
|
remove_online_friend(m, friendnumber);
|
2015-02-21 05:39:33 +08:00
|
|
|
clear_receipts(m, friendnumber);
|
2014-02-08 08:43:21 +08:00
|
|
|
} else {
|
2014-02-15 10:16:31 +08:00
|
|
|
add_online_friend(m, friendnumber);
|
2014-02-08 08:43:21 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2014-05-27 01:15:00 +08:00
|
|
|
m->friendlist[friendnumber].status = status;
|
|
|
|
|
2015-02-20 09:51:19 +08:00
|
|
|
check_friend_tcp_udp(m, friendnumber);
|
2014-02-16 02:10:53 +08:00
|
|
|
|
|
|
|
if (m->friend_connectionstatuschange_internal)
|
|
|
|
m->friend_connectionstatuschange_internal(m, friendnumber, is_online,
|
|
|
|
m->friend_connectionstatuschange_internal_userdata);
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
2013-08-08 13:46:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-08-08 13:46:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
check_friend_connectionstatus(m, friendnumber, status);
|
|
|
|
m->friendlist[friendnumber].status = status;
|
2013-08-08 13:46:27 +08:00
|
|
|
}
|
|
|
|
|
2014-08-30 09:32:05 +08:00
|
|
|
static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
|
|
|
|
uint32_t length, uint8_t congestion_control)
|
2013-08-10 03:58:08 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-08-10 04:28:12 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-02 22:36:48 +08:00
|
|
|
if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
2013-08-10 04:28:12 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-10 03:58:08 +08:00
|
|
|
uint8_t packet[length + 1];
|
|
|
|
packet[0] = packet_id;
|
2013-08-19 19:00:59 +08:00
|
|
|
|
|
|
|
if (length != 0)
|
|
|
|
memcpy(packet + 1, data, length);
|
|
|
|
|
2014-09-28 06:25:03 +08:00
|
|
|
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), packet, length + 1, congestion_control) != -1;
|
2013-08-10 03:58:08 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
/**********GROUP CHATS************/
|
|
|
|
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
/* Set the callback for group invites.
|
|
|
|
*
|
2015-02-18 04:49:22 +08:00
|
|
|
* Function(Messenger *m, uint32_t friendnumber, uint8_t *data, uint16_t length)
|
2013-09-13 07:26:30 +08:00
|
|
|
*/
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, uint16_t))
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
|
|
|
m->group_invite = function;
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
|
2014-09-26 04:48:18 +08:00
|
|
|
/* Send a group invite packet.
|
2013-12-14 09:27:25 +08:00
|
|
|
*
|
2014-09-26 04:48:18 +08:00
|
|
|
* return 1 on success
|
|
|
|
* return 0 on failure
|
2013-11-23 12:31:55 +08:00
|
|
|
*/
|
2014-09-26 04:48:18 +08:00
|
|
|
int send_group_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length)
|
2013-11-23 12:31:55 +08:00
|
|
|
{
|
2014-09-26 04:48:18 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, data, length, 0);
|
2013-11-23 12:31:55 +08:00
|
|
|
}
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
/****************FILE SENDING*****************/
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the callback for file send requests.
|
|
|
|
*
|
2015-03-14 01:00:35 +08:00
|
|
|
* Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint32_t filetype, uint64_t filesize, uint8_t *filename, size_t filename_length, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2015-03-14 01:00:35 +08:00
|
|
|
void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint32_t, uint64_t,
|
2015-03-07 11:06:58 +08:00
|
|
|
const uint8_t *, size_t, void *), void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
|
|
|
m->file_sendrequest = function;
|
|
|
|
m->file_sendrequest_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the callback for file control requests.
|
|
|
|
*
|
2015-03-11 05:31:50 +08:00
|
|
|
* Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, unsigned int control_type, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*
|
|
|
|
*/
|
2015-03-11 05:31:50 +08:00
|
|
|
void callback_file_control(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, unsigned int, void *),
|
|
|
|
void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
|
|
|
m->file_filecontrol = function;
|
|
|
|
m->file_filecontrol_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the callback for file data.
|
|
|
|
*
|
2015-03-11 05:31:50 +08:00
|
|
|
* Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, uint8_t *data, size_t length, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*
|
|
|
|
*/
|
2015-03-11 05:31:50 +08:00
|
|
|
void callback_file_data(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint64_t, const uint8_t *,
|
|
|
|
size_t, void *), void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
|
|
|
m->file_filedata = function;
|
|
|
|
m->file_filedata_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
/* Set the callback for file request chunk.
|
|
|
|
*
|
|
|
|
* Function(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, size_t length, void *userdata)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void callback_file_reqchunk(Messenger *m, void (*function)(Messenger *m, uint32_t, uint32_t, uint64_t, size_t, void *),
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->file_reqchunk = function;
|
|
|
|
m->file_reqchunk_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2013-10-02 22:28:08 +08:00
|
|
|
#define MAX_FILENAME_LENGTH 255
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2015-03-18 01:44:48 +08:00
|
|
|
/* Copy the file transfer file id to file_id
|
|
|
|
*
|
|
|
|
* return 0 on success.
|
|
|
|
* return -1 if friend not valid.
|
|
|
|
* return -2 if filenumber not valid
|
|
|
|
*/
|
|
|
|
int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint8_t *file_id)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
uint32_t temp_filenum;
|
|
|
|
uint8_t send_receive, file_number;
|
|
|
|
|
|
|
|
if (filenumber >= (1 << 16)) {
|
|
|
|
send_receive = 1;
|
|
|
|
temp_filenum = (filenumber >> 16) - 1;
|
|
|
|
} else {
|
|
|
|
send_receive = 0;
|
|
|
|
temp_filenum = filenumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
file_number = temp_filenum;
|
|
|
|
|
|
|
|
struct File_Transfers *ft;
|
|
|
|
|
|
|
|
if (send_receive) {
|
|
|
|
ft = &m->friendlist[friendnumber].file_receiving[file_number];
|
|
|
|
} else {
|
|
|
|
ft = &m->friendlist[friendnumber].file_sending[file_number];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ft->status == FILESTATUS_NONE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
memcpy(file_id, ft->id, FILE_ID_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
/* Send a file send request.
|
2013-10-02 22:28:08 +08:00
|
|
|
* Maximum filename length is 255 bytes.
|
2013-09-30 21:35:17 +08:00
|
|
|
* return 1 on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2015-03-07 11:06:58 +08:00
|
|
|
static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint32_t file_type,
|
2015-03-18 01:44:48 +08:00
|
|
|
uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
if (filename_length > MAX_FILENAME_LENGTH)
|
|
|
|
return 0;
|
|
|
|
|
2015-03-18 01:44:48 +08:00
|
|
|
uint8_t packet[1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length];
|
2013-09-30 21:35:17 +08:00
|
|
|
packet[0] = filenumber;
|
2015-03-07 11:06:58 +08:00
|
|
|
file_type = htonl(file_type);
|
|
|
|
memcpy(packet + 1, &file_type, sizeof(file_type));
|
2013-10-16 05:23:00 +08:00
|
|
|
host_to_net((uint8_t *)&filesize, sizeof(filesize));
|
2015-03-07 11:06:58 +08:00
|
|
|
memcpy(packet + 1 + sizeof(file_type), &filesize, sizeof(filesize));
|
2015-03-18 01:44:48 +08:00
|
|
|
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH);
|
|
|
|
|
|
|
|
if (filename_length) {
|
|
|
|
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
|
|
|
|
}
|
|
|
|
|
2015-03-07 11:06:58 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, sizeof(packet), 0);
|
2013-09-30 21:35:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
/* Send a file send request.
|
2013-10-02 22:28:08 +08:00
|
|
|
* Maximum filename length is 255 bytes.
|
2013-10-02 04:48:32 +08:00
|
|
|
* return file number on success
|
2015-03-10 07:21:51 +08:00
|
|
|
* return -1 if friend not found.
|
2015-03-16 06:35:22 +08:00
|
|
|
* return -2 if filename length invalid.
|
2015-03-10 07:21:51 +08:00
|
|
|
* return -3 if no more file sending slots left.
|
|
|
|
* return -4 if could not send packet (friend offline).
|
|
|
|
*
|
2013-10-02 04:48:32 +08:00
|
|
|
*/
|
2015-03-10 07:21:51 +08:00
|
|
|
long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize,
|
2015-03-18 01:44:48 +08:00
|
|
|
const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
|
2013-10-02 04:48:32 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 23:03:05 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-10 07:21:51 +08:00
|
|
|
if (filename_length > MAX_FILENAME_LENGTH)
|
|
|
|
return -2;
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[i].status == FILESTATUS_NONE)
|
2013-10-02 04:48:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == MAX_CONCURRENT_FILE_PIPES)
|
2015-03-10 07:21:51 +08:00
|
|
|
return -3;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-18 01:44:48 +08:00
|
|
|
if (file_sendrequest(m, friendnumber, i, file_type, filesize, file_id, filename, filename_length) == 0)
|
2015-03-10 07:21:51 +08:00
|
|
|
return -4;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-13 04:05:18 +08:00
|
|
|
struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
|
|
|
|
ft->status = FILESTATUS_NOT_ACCEPTED;
|
|
|
|
ft->size = filesize;
|
|
|
|
ft->transferred = 0;
|
|
|
|
ft->requested = 0;
|
|
|
|
ft->slots_allocated = 0;
|
|
|
|
ft->paused = FILE_PAUSE_NOT;
|
2015-03-18 01:44:48 +08:00
|
|
|
memcpy(ft->id, file_id, FILE_ID_LENGTH);
|
2015-03-13 04:05:18 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
++m->friendlist[friendnumber].num_sending_files;
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
int send_file_control_packet(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber,
|
|
|
|
uint8_t control_type, uint8_t *data, uint16_t data_length)
|
|
|
|
{
|
|
|
|
if (1 + 3 + data_length > MAX_CRYPTO_DATA_SIZE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint8_t packet[3 + data_length];
|
|
|
|
|
|
|
|
packet[0] = send_receive;
|
|
|
|
packet[1] = filenumber;
|
|
|
|
packet[2] = control_type;
|
|
|
|
|
|
|
|
if (data_length) {
|
2015-03-18 03:05:17 +08:00
|
|
|
memcpy(packet + 3, data, data_length);
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, sizeof(packet), 0);
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
/* Send a file control request.
|
|
|
|
*
|
2013-11-29 18:26:09 +08:00
|
|
|
* return 0 on success
|
2015-03-11 05:31:50 +08:00
|
|
|
* return -1 if friend not valid.
|
|
|
|
* return -2 if friend not online.
|
|
|
|
* return -3 if file number invalid.
|
|
|
|
* return -4 if file control is bad.
|
|
|
|
* return -5 if file already paused.
|
|
|
|
* return -6 if resume file failed because it was only paused by the other.
|
|
|
|
* return -7 if resume file failed because it wasn't paused.
|
|
|
|
* return -8 if packet failed to send.
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2015-03-11 05:31:50 +08:00
|
|
|
int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
uint32_t temp_filenum;
|
|
|
|
uint8_t send_receive, file_number;
|
|
|
|
|
|
|
|
if (filenumber >= (1 << 16)) {
|
|
|
|
send_receive = 1;
|
|
|
|
temp_filenum = (filenumber >> 16) - 1;
|
2013-10-05 22:55:28 +08:00
|
|
|
} else {
|
2015-03-11 05:31:50 +08:00
|
|
|
send_receive = 0;
|
|
|
|
temp_filenum = filenumber;
|
2013-10-05 22:55:28 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES)
|
|
|
|
return -3;
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
file_number = temp_filenum;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
struct File_Transfers *ft;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (send_receive) {
|
|
|
|
ft = &m->friendlist[friendnumber].file_receiving[file_number];
|
2013-10-16 05:23:00 +08:00
|
|
|
} else {
|
2015-03-11 05:31:50 +08:00
|
|
|
ft = &m->friendlist[friendnumber].file_sending[file_number];
|
2013-10-16 05:23:00 +08:00
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (ft->status == FILESTATUS_NONE)
|
|
|
|
return -3;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (control > FILECONTROL_KILL)
|
|
|
|
return -4;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (control == FILECONTROL_PAUSE && (ft->paused & FILE_PAUSE_US))
|
|
|
|
return -5;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (control == FILECONTROL_ACCEPT && ft->status == FILESTATUS_TRANSFERRING) {
|
|
|
|
if (!(ft->paused & FILE_PAUSE_US)) {
|
|
|
|
if (ft->paused & FILE_PAUSE_OTHER) {
|
|
|
|
return -6;
|
|
|
|
} else {
|
|
|
|
return -7;
|
2013-10-03 08:08:14 +08:00
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, 0, 0)) {
|
|
|
|
if (control == FILECONTROL_KILL) {
|
|
|
|
ft->status = FILESTATUS_NONE;
|
2014-08-05 03:18:18 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (send_receive == 0) {
|
|
|
|
--m->friendlist[friendnumber].num_sending_files;
|
2013-10-03 08:08:14 +08:00
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
} else if (control == FILECONTROL_PAUSE) {
|
|
|
|
ft->paused |= FILE_PAUSE_US;
|
|
|
|
} else if (control == FILECONTROL_ACCEPT) {
|
|
|
|
ft->status = FILESTATUS_TRANSFERRING;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (ft->paused & FILE_PAUSE_US) {
|
|
|
|
ft->paused ^= FILE_PAUSE_US;
|
|
|
|
}
|
|
|
|
}
|
2013-11-29 18:26:09 +08:00
|
|
|
} else {
|
2015-03-11 05:31:50 +08:00
|
|
|
return -8;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
|
|
|
|
return 0;
|
2013-09-30 21:35:17 +08:00
|
|
|
}
|
|
|
|
|
2015-03-18 03:05:17 +08:00
|
|
|
/* Send a seek file control request.
|
|
|
|
*
|
|
|
|
* return 0 on success
|
|
|
|
* return -1 if friend not valid.
|
|
|
|
* return -2 if friend not online.
|
|
|
|
* return -3 if file number invalid.
|
|
|
|
* return -4 if not receiving file.
|
|
|
|
* return -5 if file status wrong.
|
|
|
|
* return -6 if position bad.
|
|
|
|
* return -8 if packet failed to send.
|
|
|
|
*/
|
|
|
|
int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
uint32_t temp_filenum;
|
|
|
|
uint8_t send_receive, file_number;
|
|
|
|
|
|
|
|
if (filenumber >= (1 << 16)) {
|
|
|
|
send_receive = 1;
|
|
|
|
temp_filenum = (filenumber >> 16) - 1;
|
|
|
|
} else {
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
file_number = temp_filenum;
|
|
|
|
|
|
|
|
struct File_Transfers *ft;
|
|
|
|
|
|
|
|
if (send_receive) {
|
|
|
|
ft = &m->friendlist[friendnumber].file_receiving[file_number];
|
|
|
|
} else {
|
|
|
|
ft = &m->friendlist[friendnumber].file_sending[file_number];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ft->status == FILESTATUS_NONE)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (ft->status != FILESTATUS_NOT_ACCEPTED)
|
|
|
|
return -5;
|
|
|
|
|
|
|
|
if (ft->size && position > ft->size) {
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sending_pos = position;
|
|
|
|
host_to_net((uint8_t *)&sending_pos, sizeof(sending_pos));
|
|
|
|
|
|
|
|
if (send_file_control_packet(m, friendnumber, send_receive, file_number, FILECONTROL_SEEK, (uint8_t *)&sending_pos,
|
|
|
|
sizeof(sending_pos))) {
|
|
|
|
ft->transferred = position;
|
|
|
|
} else {
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
/* return packet number on success.
|
|
|
|
* return -1 on failure.
|
|
|
|
*/
|
|
|
|
static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, uint8_t filenumber, const uint8_t *data,
|
|
|
|
uint16_t length)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint8_t packet[2 + length];
|
|
|
|
packet[0] = PACKET_ID_FILE_DATA;
|
|
|
|
packet[1] = filenumber;
|
|
|
|
|
|
|
|
if (length) {
|
|
|
|
memcpy(packet + 2, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), packet, sizeof(packet), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
|
2015-03-13 06:01:08 +08:00
|
|
|
#define MIN_SLOTS_FREE (CRYPTO_MIN_QUEUE_LENGTH / 4)
|
2013-09-30 21:35:17 +08:00
|
|
|
/* Send file data.
|
|
|
|
*
|
2013-11-29 18:26:09 +08:00
|
|
|
* return 0 on success
|
2015-03-11 05:31:50 +08:00
|
|
|
* return -1 if friend not valid.
|
|
|
|
* return -2 if friend not online.
|
|
|
|
* return -3 if filenumber invalid.
|
|
|
|
* return -4 if file transfer not transferring.
|
2015-03-14 00:40:21 +08:00
|
|
|
* return -5 if bad data size.
|
2015-03-11 05:31:50 +08:00
|
|
|
* return -6 if packet queue full.
|
2015-03-13 03:17:34 +08:00
|
|
|
* return -7 if wrong position.
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2015-03-13 03:17:34 +08:00
|
|
|
int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data,
|
|
|
|
uint16_t length)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (filenumber > MAX_CONCURRENT_FILE_PIPES)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[filenumber];
|
|
|
|
|
|
|
|
if (ft->status != FILESTATUS_TRANSFERRING)
|
|
|
|
return -4;
|
|
|
|
|
|
|
|
if (ft->paused != FILE_PAUSE_NOT)
|
|
|
|
return -4;
|
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
if (length > MAX_FILE_DATA_SIZE)
|
2015-03-11 05:31:50 +08:00
|
|
|
return -5;
|
|
|
|
|
|
|
|
if (ft->size) {
|
|
|
|
if (ft->size - ft->transferred < length) {
|
|
|
|
return -5;
|
|
|
|
}
|
2015-03-14 00:40:21 +08:00
|
|
|
|
|
|
|
if (length != MAX_FILE_DATA_SIZE && (ft->transferred + length) != ft->size) {
|
|
|
|
return -5;
|
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-13 03:17:34 +08:00
|
|
|
if (position != ft->transferred) {
|
|
|
|
return -7;
|
|
|
|
}
|
|
|
|
|
2014-09-28 06:25:03 +08:00
|
|
|
/* Prevent file sending from filling up the entire buffer preventing messages from being sent. TODO: remove */
|
2014-09-28 08:01:41 +08:00
|
|
|
if (crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id)) < MIN_SLOTS_FREE)
|
2015-03-11 05:31:50 +08:00
|
|
|
return -6;
|
2013-10-04 23:20:47 +08:00
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
int64_t ret = send_file_data_packet(m, friendnumber, filenumber, data, length);
|
2015-03-11 05:31:50 +08:00
|
|
|
|
|
|
|
if (ret != -1) {
|
|
|
|
//TODO record packet ids to check if other received complete file.
|
|
|
|
ft->transferred += length;
|
|
|
|
|
2015-03-13 04:05:18 +08:00
|
|
|
if (ft->slots_allocated) {
|
|
|
|
--ft->slots_allocated;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
if (length != MAX_FILE_DATA_SIZE || ft->size == ft->transferred) {
|
2015-03-11 05:31:50 +08:00
|
|
|
ft->status = FILESTATUS_FINISHED;
|
|
|
|
ft->last_packet_number = ret;
|
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-11-29 18:26:09 +08:00
|
|
|
return 0;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
return -6;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
|
|
|
}
|
2013-10-02 06:59:33 +08:00
|
|
|
|
|
|
|
/* Give the number of bytes left to be sent/received.
|
|
|
|
*
|
|
|
|
* send_receive is 0 if we want the sending files, 1 if we want the receiving.
|
|
|
|
*
|
|
|
|
* return number of bytes remaining to be sent/received on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
|
2013-10-02 06:59:33 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (send_receive == 0) {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE)
|
2013-10-02 06:59:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].file_sending[filenumber].size -
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].transferred;
|
|
|
|
} else {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE)
|
2013-10-02 06:59:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].file_receiving[filenumber].size -
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].transferred;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
|
|
|
|
{
|
|
|
|
if (!m->friendlist[friendnumber].num_sending_files)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int free_slots = crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id));
|
|
|
|
|
2015-03-13 06:01:08 +08:00
|
|
|
if (free_slots < MIN_SLOTS_FREE) {
|
|
|
|
free_slots = 0;
|
|
|
|
} else {
|
|
|
|
free_slots -= MIN_SLOTS_FREE;
|
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
|
|
|
|
unsigned int i, num = m->friendlist[friendnumber].num_sending_files;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
|
|
|
|
struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
|
|
|
|
|
|
|
|
if (ft->status != FILESTATUS_NONE) {
|
|
|
|
--num;
|
|
|
|
|
|
|
|
if (ft->status == FILESTATUS_FINISHED) {
|
|
|
|
/* Check if file was entirely sent. */
|
|
|
|
if (friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
|
|
|
|
if (m->file_reqchunk)
|
|
|
|
(*m->file_reqchunk)(m, friendnumber, i, ft->transferred, 0, m->file_reqchunk_userdata);
|
|
|
|
|
|
|
|
ft->status = FILESTATUS_NONE;
|
|
|
|
--m->friendlist[friendnumber].num_sending_files;
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 04:05:18 +08:00
|
|
|
|
|
|
|
/* TODO: if file is too slow, switch to the next. */
|
|
|
|
if (ft->slots_allocated > free_slots) {
|
|
|
|
free_slots = 0;
|
|
|
|
} else {
|
|
|
|
free_slots -= ft->slots_allocated;
|
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (ft->status == FILESTATUS_TRANSFERRING && (ft->paused == FILE_PAUSE_NOT)) {
|
|
|
|
if (free_slots == 0)
|
|
|
|
break;
|
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
uint16_t length = MAX_FILE_DATA_SIZE;
|
2015-03-11 05:31:50 +08:00
|
|
|
|
|
|
|
if (ft->size) {
|
2015-03-13 04:05:18 +08:00
|
|
|
if (ft->size == ft->requested) {
|
2015-03-11 05:31:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-13 04:05:18 +08:00
|
|
|
if (ft->size - ft->requested < length) {
|
|
|
|
length = ft->size - ft->requested;
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 06:01:08 +08:00
|
|
|
++ft->slots_allocated;
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->file_reqchunk)
|
2015-03-13 04:05:18 +08:00
|
|
|
(*m->file_reqchunk)(m, friendnumber, i, ft->requested, length, m->file_reqchunk_userdata);
|
|
|
|
|
|
|
|
ft->requested += length;
|
2015-03-11 05:31:50 +08:00
|
|
|
|
|
|
|
--free_slots;
|
2015-03-14 04:51:41 +08:00
|
|
|
|
|
|
|
if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id))) {
|
|
|
|
free_slots = 0;
|
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
/* Run this when the friend disconnects.
|
|
|
|
* Sets all current file transfers to broken.
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
static void break_files(const Messenger *m, int32_t friendnumber)
|
2013-10-02 04:48:32 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2015-03-11 05:31:50 +08:00
|
|
|
/* TODO
|
|
|
|
for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
|
|
|
|
if (m->friendlist[friendnumber].file_sending[i].status != FILESTATUS_NONE)
|
|
|
|
m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_BROKEN;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[i].status != FILESTATUS_NONE)
|
|
|
|
m->friendlist[friendnumber].file_receiving[i].status = FILESTATUS_BROKEN;
|
|
|
|
}*/
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
/* return -1 on failure, 0 on success.
|
|
|
|
*/
|
|
|
|
static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber,
|
|
|
|
uint8_t control_type, uint8_t *data, uint16_t length)
|
2013-10-02 04:48:32 +08:00
|
|
|
{
|
2013-10-16 05:23:00 +08:00
|
|
|
if (receive_send > 1)
|
2013-10-02 04:48:32 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-03-18 03:05:17 +08:00
|
|
|
if (control_type > FILECONTROL_SEEK)
|
2015-03-11 05:31:50 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
uint32_t real_filenumber = filenumber;
|
|
|
|
struct File_Transfers *ft;
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (receive_send == 0) {
|
|
|
|
real_filenumber += 1;
|
|
|
|
real_filenumber <<= 16;
|
|
|
|
ft = &m->friendlist[friendnumber].file_receiving[filenumber];
|
|
|
|
} else {
|
|
|
|
ft = &m->friendlist[friendnumber].file_sending[filenumber];
|
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (ft->status == FILESTATUS_NONE) {
|
|
|
|
/* File transfer doesn't exist, tell the other to kill it. */
|
|
|
|
send_file_control_packet(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, 0, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (control_type == FILECONTROL_ACCEPT) {
|
|
|
|
ft->status = FILESTATUS_TRANSFERRING;
|
2014-08-05 03:18:18 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (ft->paused & FILE_PAUSE_OTHER) {
|
|
|
|
ft->paused ^= FILE_PAUSE_OTHER;
|
2013-10-16 05:23:00 +08:00
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->file_filecontrol)
|
|
|
|
(*m->file_filecontrol)(m, friendnumber, real_filenumber, control_type, m->file_filecontrol_userdata);
|
|
|
|
} else if (control_type == FILECONTROL_PAUSE) {
|
|
|
|
ft->paused |= FILE_PAUSE_OTHER;
|
2013-10-04 00:42:29 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->file_filecontrol)
|
|
|
|
(*m->file_filecontrol)(m, friendnumber, real_filenumber, control_type, m->file_filecontrol_userdata);
|
|
|
|
} else if (control_type == FILECONTROL_KILL) {
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (m->file_filecontrol)
|
|
|
|
(*m->file_filecontrol)(m, friendnumber, real_filenumber, control_type, m->file_filecontrol_userdata);
|
2013-10-16 05:23:00 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
ft->status = FILESTATUS_NONE;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (receive_send) {
|
|
|
|
--m->friendlist[friendnumber].num_sending_files;
|
2013-10-03 08:08:14 +08:00
|
|
|
}
|
2015-03-11 05:31:50 +08:00
|
|
|
|
2015-03-18 03:05:17 +08:00
|
|
|
} else if (control_type == FILECONTROL_SEEK) {
|
|
|
|
uint64_t position;
|
|
|
|
|
|
|
|
if (length != sizeof(position)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek can only be sent by the receiver to seek before resuming broken tranfers. */
|
|
|
|
if (ft->status != FILESTATUS_NOT_ACCEPTED || !receive_send) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&position, data, sizeof(position));
|
|
|
|
net_to_host((uint8_t *) &position, sizeof(position));
|
|
|
|
|
|
|
|
if (ft->size && position > ft->size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ft->transferred = ft->requested = position;
|
2015-03-11 05:31:50 +08:00
|
|
|
} else {
|
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
return 0;
|
2013-09-30 21:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************/
|
|
|
|
|
2013-10-14 10:07:30 +08:00
|
|
|
/* Set the callback for msi packets.
|
|
|
|
*
|
|
|
|
* Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata)
|
|
|
|
*/
|
2015-02-18 04:49:22 +08:00
|
|
|
void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, uint16_t, void *),
|
2013-10-14 10:07:30 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->msi_packet = function;
|
|
|
|
m->msi_packet_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an msi packet.
|
|
|
|
*
|
|
|
|
* return 1 on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2014-06-18 03:34:45 +08:00
|
|
|
int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length)
|
2013-10-14 10:07:30 +08:00
|
|
|
{
|
2014-08-30 09:32:05 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, 0);
|
2013-10-14 10:07:30 +08:00
|
|
|
}
|
2013-09-12 08:42:25 +08:00
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
Messenger *m = object;
|
|
|
|
|
2014-05-22 03:28:14 +08:00
|
|
|
if (friend_not_valid(m, friend_num))
|
2014-02-15 10:16:31 +08:00
|
|
|
return 1;
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
if (packet[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED)) {
|
|
|
|
if (m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function)
|
|
|
|
return m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_LOSSY_AV_RESERVED].function(
|
|
|
|
m, friend_num, packet, length, m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] %
|
|
|
|
PACKET_LOSSY_AV_RESERVED].object);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->lossy_packethandler)
|
|
|
|
m->lossy_packethandler(m, friend_num, packet, length, m->lossy_packethandler_userdata);
|
2014-02-15 10:16:31 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
void custom_lossy_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m,
|
|
|
|
uint32_t friendnumber, const uint8_t *data, size_t len, void *object), void *object)
|
|
|
|
{
|
|
|
|
m->lossy_packethandler = packet_handler_callback;
|
|
|
|
m->lossy_packethandler_userdata = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*packet_handler_callback)(Messenger *m,
|
|
|
|
uint32_t friendnumber, const uint8_t *data, uint16_t len, void *object), void *object)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
2014-05-22 03:28:14 +08:00
|
|
|
if (byte < PACKET_ID_LOSSY_RANGE_START)
|
|
|
|
return -1;
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_LOSSY_AV_RESERVED))
|
2014-02-15 10:16:31 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function =
|
|
|
|
packet_handler_callback;
|
|
|
|
m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].object = object;
|
2014-02-15 10:16:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
2014-03-09 06:21:39 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
2015-02-27 02:13:26 +08:00
|
|
|
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (data[0] < PACKET_ID_LOSSY_RANGE_START)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
|
|
|
|
return -3;
|
|
|
|
|
2014-03-09 06:21:39 +08:00
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
2015-02-27 02:13:26 +08:00
|
|
|
return -4;
|
2014-03-09 06:21:39 +08:00
|
|
|
|
2015-02-27 02:13:26 +08:00
|
|
|
if (send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), data, length) == -1) {
|
|
|
|
return -5;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-15 10:16:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static int handle_custom_lossless_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length)
|
2014-06-12 08:01:07 +08:00
|
|
|
{
|
|
|
|
Messenger *m = object;
|
|
|
|
|
|
|
|
if (friend_not_valid(m, friend_num))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (packet[0] < PACKET_ID_LOSSLESS_RANGE_START)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (packet[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE))
|
|
|
|
return -1;
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
if (m->lossless_packethandler)
|
|
|
|
m->lossless_packethandler(m, friend_num, packet, length, m->lossless_packethandler_userdata);
|
2014-06-12 08:01:07 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:05:57 +08:00
|
|
|
void custom_lossless_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m,
|
|
|
|
uint32_t friendnumber, const uint8_t *data, size_t len, void *object), void *object)
|
2014-06-12 08:01:07 +08:00
|
|
|
{
|
2015-02-26 21:05:57 +08:00
|
|
|
m->lossless_packethandler = packet_handler_callback;
|
|
|
|
m->lossless_packethandler_userdata = object;
|
2014-06-12 08:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
|
2014-06-12 08:01:07 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
2015-02-27 02:13:26 +08:00
|
|
|
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
|
|
|
|
return -2;
|
2014-09-06 09:31:35 +08:00
|
|
|
|
|
|
|
if (data[0] < PACKET_ID_LOSSLESS_RANGE_START)
|
2015-02-27 02:13:26 +08:00
|
|
|
return -3;
|
2014-09-06 09:31:35 +08:00
|
|
|
|
|
|
|
if (data[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE))
|
2015-02-27 02:13:26 +08:00
|
|
|
return -3;
|
2014-09-06 09:31:35 +08:00
|
|
|
|
2014-06-12 08:01:07 +08:00
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
2015-02-27 02:13:26 +08:00
|
|
|
return -4;
|
2014-06-12 08:01:07 +08:00
|
|
|
|
2014-09-28 06:25:03 +08:00
|
|
|
if (write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
|
|
|
m->friendlist[friendnumber].friendcon_id), data, length, 1) == -1) {
|
2015-02-27 02:13:26 +08:00
|
|
|
return -5;
|
2014-09-09 00:20:44 +08:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-12 08:01:07 +08:00
|
|
|
}
|
2014-02-15 10:16:31 +08:00
|
|
|
|
2013-11-28 03:18:39 +08:00
|
|
|
/* Function to filter out some friend requests*/
|
2015-01-28 09:56:39 +08:00
|
|
|
static int friend_already_added(const uint8_t *real_pk, void *data)
|
2013-11-28 03:18:39 +08:00
|
|
|
{
|
2014-06-18 03:34:45 +08:00
|
|
|
const Messenger *m = data;
|
2013-11-28 03:18:39 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
if (getfriend_id(m, real_pk) == -1)
|
2013-11-28 03:18:39 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
|
2013-08-21 02:47:32 +08:00
|
|
|
static void LANdiscovery(Messenger *m)
|
2013-08-13 06:47:15 +08:00
|
|
|
{
|
2013-08-21 02:47:32 +08:00
|
|
|
if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
|
2014-01-18 02:35:40 +08:00
|
|
|
send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->dht);
|
2013-08-21 02:47:32 +08:00
|
|
|
m->last_LANdiscovery = unix_time();
|
|
|
|
}
|
2013-08-13 06:47:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Run this at startup. */
|
2015-03-13 01:03:14 +08:00
|
|
|
Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
Messenger *m = calloc(1, sizeof(Messenger));
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2015-03-13 01:03:14 +08:00
|
|
|
if (error)
|
|
|
|
*error = MESSENGER_ERROR_OTHER;
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if ( ! m )
|
2013-08-21 03:53:15 +08:00
|
|
|
return NULL;
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2015-03-13 01:03:14 +08:00
|
|
|
unsigned int net_err = 0;
|
|
|
|
|
2014-08-15 03:27:34 +08:00
|
|
|
if (options->udp_disabled) {
|
|
|
|
/* this is the easiest way to completely disable UDP without changing too much code. */
|
|
|
|
m->net = calloc(1, sizeof(Networking_Core));
|
|
|
|
} else {
|
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, options->ipv6enabled);
|
2015-03-13 01:03:14 +08:00
|
|
|
m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1], &net_err);
|
2014-08-15 03:27:34 +08:00
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (m->net == NULL) {
|
|
|
|
free(m);
|
2015-03-13 01:03:14 +08:00
|
|
|
|
|
|
|
if (error && net_err == 1) {
|
|
|
|
*error = MESSENGER_ERROR_PORT;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2014-04-23 23:35:40 +08:00
|
|
|
m->dht = new_DHT(m->net);
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2014-04-23 23:35:40 +08:00
|
|
|
if (m->dht == NULL) {
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_networking(m->net);
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-15 03:27:34 +08:00
|
|
|
|
2014-12-22 08:59:00 +08:00
|
|
|
m->net_crypto = new_net_crypto(m->dht, &options->proxy_info);
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2014-04-23 23:35:40 +08:00
|
|
|
if (m->net_crypto == NULL) {
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_networking(m->net);
|
2014-04-23 23:35:40 +08:00
|
|
|
kill_DHT(m->dht);
|
2013-08-20 11:54:28 +08:00
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
m->onion = new_onion(m->dht);
|
|
|
|
m->onion_a = new_onion_announce(m->dht);
|
2014-04-23 23:35:40 +08:00
|
|
|
m->onion_c = new_onion_client(m->net_crypto);
|
2014-09-28 06:25:03 +08:00
|
|
|
m->fr_c = new_friend_connections(m->onion_c);
|
2014-01-18 02:35:40 +08:00
|
|
|
|
|
|
|
if (!(m->onion && m->onion_a && m->onion_c)) {
|
2014-09-28 06:25:03 +08:00
|
|
|
kill_friend_connections(m->fr_c);
|
2014-01-18 02:35:40 +08:00
|
|
|
kill_onion(m->onion);
|
|
|
|
kill_onion_announce(m->onion_a);
|
|
|
|
kill_onion_client(m->onion_c);
|
|
|
|
kill_DHT(m->dht);
|
|
|
|
kill_net_crypto(m->net_crypto);
|
|
|
|
kill_networking(m->net);
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-15 03:27:34 +08:00
|
|
|
m->options = *options;
|
2014-10-05 04:49:17 +08:00
|
|
|
friendreq_init(&(m->fr), m->fr_c);
|
2013-08-21 00:08:55 +08:00
|
|
|
LANdiscovery_init(m->dht);
|
|
|
|
set_nospam(&(m->fr), random_int());
|
2013-11-28 03:18:39 +08:00
|
|
|
set_filter_function(&(m->fr), &friend_already_added, m);
|
|
|
|
|
2015-03-13 01:03:14 +08:00
|
|
|
if (error)
|
|
|
|
*error = MESSENGER_ERROR_NONE;
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Run this before closing shop. */
|
2013-10-24 02:42:39 +08:00
|
|
|
void kill_messenger(Messenger *m)
|
2013-08-17 01:11:09 +08:00
|
|
|
{
|
2014-09-30 01:45:51 +08:00
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
2014-09-26 04:48:18 +08:00
|
|
|
uint32_t i;
|
2013-11-24 11:17:36 +08:00
|
|
|
|
2014-09-28 06:25:03 +08:00
|
|
|
kill_friend_connections(m->fr_c);
|
2014-01-21 10:01:56 +08:00
|
|
|
kill_onion(m->onion);
|
|
|
|
kill_onion_announce(m->onion_a);
|
|
|
|
kill_onion_client(m->onion_c);
|
2013-08-20 11:54:28 +08:00
|
|
|
kill_net_crypto(m->net_crypto);
|
2014-05-03 09:44:09 +08:00
|
|
|
kill_DHT(m->dht);
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_networking(m->net);
|
2014-05-10 09:46:37 +08:00
|
|
|
|
2014-04-24 08:29:50 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i) {
|
2014-08-28 03:13:44 +08:00
|
|
|
clear_receipts(m, i);
|
2014-04-24 08:29:50 +08:00
|
|
|
}
|
2014-05-10 09:46:37 +08:00
|
|
|
|
2013-08-14 13:35:15 +08:00
|
|
|
free(m->friendlist);
|
2013-08-11 11:24:11 +08:00
|
|
|
free(m);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-02 19:16:01 +08:00
|
|
|
/* Check for and handle a timed-out friend request. If the request has
|
|
|
|
* timed-out then the friend status is set back to FRIEND_ADDED.
|
|
|
|
* i: friendlist index of the timed-out friend
|
|
|
|
* t: time
|
|
|
|
*/
|
|
|
|
static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t)
|
|
|
|
{
|
|
|
|
Friend *f = &m->friendlist[i];
|
|
|
|
|
|
|
|
if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
|
|
|
|
set_friend_status(m, i, FRIEND_ADDED);
|
2014-09-10 08:31:37 +08:00
|
|
|
/* Double the default timeout every time if friendrequest is assumed
|
2013-12-02 19:16:01 +08:00
|
|
|
* to have been sent unsuccessfully.
|
|
|
|
*/
|
|
|
|
f->friendrequest_timeout *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
static int handle_status(void *object, int i, uint8_t status)
|
2013-07-27 20:43:36 +08:00
|
|
|
{
|
2013-08-19 19:00:59 +08:00
|
|
|
uint64_t temp_time = unix_time();
|
2014-05-03 09:25:23 +08:00
|
|
|
Messenger *m = object;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (status) { /* Went online. */
|
2014-10-03 05:33:54 +08:00
|
|
|
send_online_packet(m, i);
|
2014-05-03 09:25:23 +08:00
|
|
|
m->friendlist[i].name_sent = 0;
|
|
|
|
m->friendlist[i].userstatus_sent = 0;
|
|
|
|
m->friendlist[i].statusmessage_sent = 0;
|
2015-01-31 09:29:33 +08:00
|
|
|
m->friendlist[i].user_istyping_sent = 0;
|
2014-05-03 09:25:23 +08:00
|
|
|
m->friendlist[i].ping_lastrecv = temp_time;
|
|
|
|
} else { /* Went offline. */
|
|
|
|
if (m->friendlist[i].status == FRIEND_ONLINE) {
|
|
|
|
set_friend_status(m, i, FRIEND_CONFIRMED);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2014-05-03 09:25:23 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
|
|
|
|
{
|
|
|
|
if (len == 0)
|
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
Messenger *m = object;
|
|
|
|
uint8_t packet_id = temp[0];
|
|
|
|
uint8_t *data = temp + 1;
|
|
|
|
uint32_t data_length = len - 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-10-03 05:33:54 +08:00
|
|
|
if (m->friendlist[i].status != FRIEND_ONLINE) {
|
|
|
|
if (packet_id == PACKET_ID_ONLINE && len == 1) {
|
|
|
|
set_friend_status(m, i, FRIEND_ONLINE);
|
|
|
|
send_online_packet(m, i);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
switch (packet_id) {
|
2014-10-03 05:33:54 +08:00
|
|
|
case PACKET_ID_OFFLINE: {
|
|
|
|
if (data_length != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
set_friend_status(m, i, FRIEND_CONFIRMED);
|
|
|
|
}
|
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_NICKNAME: {
|
2015-02-17 09:25:44 +08:00
|
|
|
if (data_length > MAX_NAME_LENGTH)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* inform of namechange before we overwrite the old name */
|
|
|
|
if (m->friend_namechange)
|
|
|
|
m->friend_namechange(m, i, data_terminated, data_length, m->friend_namechange_userdata);
|
|
|
|
|
|
|
|
memcpy(m->friendlist[i].name, data_terminated, data_length);
|
|
|
|
m->friendlist[i].name_length = data_length;
|
|
|
|
|
|
|
|
break;
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_STATUSMESSAGE: {
|
2015-02-17 09:25:44 +08:00
|
|
|
if (data_length > MAX_STATUSMESSAGE_LENGTH)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
/* 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;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friend_statusmessagechange)
|
|
|
|
m->friend_statusmessagechange(m, i, data_terminated, data_length,
|
2015-03-06 01:30:50 +08:00
|
|
|
m->friend_statusmessagechange_userdata);
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
set_friend_statusmessage(m, i, data_terminated, data_length);
|
|
|
|
break;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_USERSTATUS: {
|
|
|
|
if (data_length != 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
USERSTATUS status = data[0];
|
|
|
|
|
|
|
|
if (status >= USERSTATUS_INVALID)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (m->friend_userstatuschange)
|
|
|
|
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
|
|
|
|
|
|
|
|
set_friend_userstatus(m, i, status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_TYPING: {
|
|
|
|
if (data_length != 1)
|
|
|
|
break;
|
2013-08-19 19:00:59 +08:00
|
|
|
|
2015-02-19 07:29:32 +08:00
|
|
|
_Bool typing = !!data[0];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
set_friend_typing(m, i, typing);
|
2013-10-14 10:07:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friend_typingchange)
|
|
|
|
m->friend_typingchange(m, i, typing, m->friend_typingchange_userdata);
|
2013-08-19 19:00:59 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_MESSAGE: {
|
2014-06-18 03:34:45 +08:00
|
|
|
const uint8_t *message_id = data;
|
2013-09-06 21:45:39 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
if (data_length == 0)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-11-11 22:25:43 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
const uint8_t *message = data;
|
|
|
|
uint16_t message_length = data_length;
|
2013-09-06 21:45:39 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
/* 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;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friend_message)
|
|
|
|
(*m->friend_message)(m, i, message_terminated, message_length, m->friend_message_userdata);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_ACTION: {
|
2014-06-18 03:34:45 +08:00
|
|
|
const uint8_t *message_id = data;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
if (data_length == 0)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
|
2014-08-28 03:13:44 +08:00
|
|
|
const uint8_t *action = data;
|
|
|
|
uint16_t action_length = data_length;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
/* 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;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friend_action)
|
|
|
|
(*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata);
|
|
|
|
|
2014-05-20 06:27:02 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_INVITE_GROUPCHAT: {
|
2014-09-26 04:48:18 +08:00
|
|
|
if (data_length == 0)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->group_invite)
|
2014-09-26 09:05:17 +08:00
|
|
|
(*m->group_invite)(m, i, data, data_length);
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_FILE_SENDREQUEST: {
|
2015-03-18 01:44:48 +08:00
|
|
|
const unsigned int head_length = 1 + sizeof(uint32_t) + sizeof(uint64_t) + FILE_ID_LENGTH;
|
2015-03-07 11:06:58 +08:00
|
|
|
|
2015-03-18 01:44:48 +08:00
|
|
|
if (data_length < head_length)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-10-11 10:27:51 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
uint8_t filenumber = data[0];
|
|
|
|
uint64_t filesize;
|
2015-03-07 11:06:58 +08:00
|
|
|
uint32_t file_type;
|
2015-03-16 06:35:22 +08:00
|
|
|
uint16_t filename_length = data_length - head_length;
|
2015-03-07 11:06:58 +08:00
|
|
|
memcpy(&file_type, data + 1, sizeof(file_type));
|
|
|
|
file_type = ntohl(file_type);
|
|
|
|
|
|
|
|
memcpy(&filesize, data + 1 + sizeof(uint32_t), sizeof(filesize));
|
2014-06-18 03:34:45 +08:00
|
|
|
net_to_host((uint8_t *) &filesize, sizeof(filesize));
|
2015-03-18 01:44:48 +08:00
|
|
|
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
|
|
|
|
|
|
|
|
ft->status = FILESTATUS_NOT_ACCEPTED;
|
|
|
|
ft->size = filesize;
|
|
|
|
ft->transferred = 0;
|
|
|
|
ft->paused = FILE_PAUSE_NOT;
|
|
|
|
memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2015-03-16 06:35:22 +08:00
|
|
|
uint8_t filename_terminated[filename_length + 1];
|
2015-03-18 01:47:09 +08:00
|
|
|
uint8_t *filename = NULL;
|
|
|
|
|
|
|
|
if (filename_length) {
|
|
|
|
/* Force NULL terminate file name. */
|
|
|
|
memcpy(filename_terminated, data + head_length, filename_length);
|
|
|
|
filename_terminated[filename_length] = 0;
|
|
|
|
filename = filename_terminated;
|
|
|
|
}
|
2015-03-07 11:06:58 +08:00
|
|
|
|
|
|
|
uint32_t real_filenumber = filenumber;
|
2015-03-10 02:32:37 +08:00
|
|
|
real_filenumber += 1;
|
2015-03-07 11:06:58 +08:00
|
|
|
real_filenumber <<= 16;
|
2013-10-11 10:27:51 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->file_sendrequest)
|
2015-03-18 01:44:48 +08:00
|
|
|
(*m->file_sendrequest)(m, i, real_filenumber, file_type, filesize, filename, filename_length,
|
2014-05-03 09:25:23 +08:00
|
|
|
m->file_sendrequest_userdata);
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-10-14 10:07:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_FILE_CONTROL: {
|
|
|
|
if (data_length < 3)
|
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
uint8_t send_receive = data[0];
|
|
|
|
uint8_t filenumber = data[1];
|
|
|
|
uint8_t control_type = data[2];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3) == -1)
|
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_FILE_DATA: {
|
2015-03-11 05:31:50 +08:00
|
|
|
if (data_length <= 1)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
uint8_t filenumber = data[0];
|
2015-03-11 05:31:50 +08:00
|
|
|
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
if (ft->status == FILESTATUS_NONE)
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2015-03-11 05:31:50 +08:00
|
|
|
uint64_t position = ft->transferred;
|
|
|
|
uint32_t real_filenumber = filenumber;
|
|
|
|
real_filenumber += 1;
|
|
|
|
real_filenumber <<= 16;
|
|
|
|
uint16_t file_data_length = (data_length - 1);
|
|
|
|
uint8_t *file_data;
|
|
|
|
|
|
|
|
if (file_data_length == 0) {
|
|
|
|
file_data = NULL;
|
|
|
|
} else {
|
|
|
|
file_data = data + 1;
|
|
|
|
}
|
2013-09-28 10:03:38 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->file_filedata)
|
2015-03-11 05:31:50 +08:00
|
|
|
(*m->file_filedata)(m, i, real_filenumber, position, file_data, file_data_length, m->file_filedata_userdata);
|
|
|
|
|
|
|
|
ft->transferred += file_data_length;
|
|
|
|
|
2015-03-14 00:40:21 +08:00
|
|
|
if ((ft->size && ft->transferred >= ft->size) || file_data_length != MAX_FILE_DATA_SIZE) {
|
2015-03-11 05:31:50 +08:00
|
|
|
file_data_length = 0;
|
|
|
|
file_data = NULL;
|
|
|
|
position = ft->transferred;
|
|
|
|
|
|
|
|
/* Full file received. */
|
|
|
|
if (m->file_filedata)
|
|
|
|
(*m->file_filedata)(m, i, real_filenumber, position, file_data, file_data_length, m->file_filedata_userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data is zero, filetransfer is over. */
|
|
|
|
if (file_data_length == 0) {
|
|
|
|
ft->status = FILESTATUS_NONE;
|
|
|
|
}
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
case PACKET_ID_MSI: {
|
|
|
|
if (data_length == 0)
|
|
|
|
break;
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->msi_packet)
|
|
|
|
(*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata);
|
2014-06-12 08:01:07 +08:00
|
|
|
|
|
|
|
break;
|
2014-05-03 09:25:23 +08:00
|
|
|
}
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-05-20 05:39:50 +08:00
|
|
|
case PACKET_ID_SHARE_RELAYS: {
|
2014-05-20 07:03:52 +08:00
|
|
|
Node_format nodes[MAX_SHARED_RELAYS];
|
2014-05-20 05:39:50 +08:00
|
|
|
int n;
|
|
|
|
|
2014-05-20 07:28:20 +08:00
|
|
|
if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1)
|
2014-05-20 05:39:50 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
int i;
|
2014-05-20 07:03:52 +08:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2015-01-30 08:38:44 +08:00
|
|
|
add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].public_key);
|
2014-05-20 05:39:50 +08:00
|
|
|
}
|
2014-06-12 23:15:20 +08:00
|
|
|
|
2014-06-12 08:01:07 +08:00
|
|
|
break;
|
2014-05-20 05:39:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
default: {
|
2014-06-12 08:01:07 +08:00
|
|
|
handle_custom_lossless_packet(object, i, temp, len);
|
2014-05-03 09:25:23 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-28 10:03:38 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
void do_friends(Messenger *m)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint64_t temp_time = unix_time();
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status == FRIEND_ADDED) {
|
2014-10-05 04:49:17 +08:00
|
|
|
int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
|
|
|
|
m->friendlist[i].info,
|
|
|
|
m->friendlist[i].info_size);
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (fr >= 0) {
|
|
|
|
set_friend_status(m, i, FRIEND_REQUESTED);
|
|
|
|
m->friendlist[i].friendrequest_lastsent = temp_time;
|
|
|
|
}
|
|
|
|
}
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friendlist[i].status == FRIEND_REQUESTED
|
|
|
|
|| m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online. */
|
|
|
|
if (m->friendlist[i].status == FRIEND_REQUESTED) {
|
|
|
|
/* If we didn't connect to friend after successfully sending him a friend request the request is deemed
|
|
|
|
* unsuccessful so we set the status back to FRIEND_ADDED and try again.
|
|
|
|
*/
|
|
|
|
check_friend_request_timed_out(m, i, temp_time);
|
2014-05-18 02:56:20 +08:00
|
|
|
}
|
2014-05-03 09:25:23 +08:00
|
|
|
}
|
2013-10-02 06:59:33 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */
|
|
|
|
if (m->friendlist[i].name_sent == 0) {
|
|
|
|
if (m_sendname(m, i, m->name, m->name_length))
|
|
|
|
m->friendlist[i].name_sent = 1;
|
|
|
|
}
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friendlist[i].statusmessage_sent == 0) {
|
|
|
|
if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
|
|
|
|
m->friendlist[i].statusmessage_sent = 1;
|
|
|
|
}
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friendlist[i].userstatus_sent == 0) {
|
|
|
|
if (send_userstatus(m, i, m->userstatus))
|
|
|
|
m->friendlist[i].userstatus_sent = 1;
|
|
|
|
}
|
2013-10-14 10:07:30 +08:00
|
|
|
|
2014-05-03 09:25:23 +08:00
|
|
|
if (m->friendlist[i].user_istyping_sent == 0) {
|
|
|
|
if (send_user_istyping(m, i, m->friendlist[i].user_istyping))
|
|
|
|
m->friendlist[i].user_istyping_sent = 1;
|
|
|
|
}
|
2013-10-14 10:07:30 +08:00
|
|
|
|
2014-05-20 05:39:50 +08:00
|
|
|
if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) {
|
|
|
|
send_relays(m, i);
|
|
|
|
}
|
2015-02-20 09:51:19 +08:00
|
|
|
|
|
|
|
check_friend_tcp_udp(m, i);
|
2014-08-28 03:13:44 +08:00
|
|
|
do_receipts(m, i);
|
2015-03-11 05:31:50 +08:00
|
|
|
do_reqchunk_filecb(m, i);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 03:37:25 +08:00
|
|
|
static void connection_status_cb(Messenger *m)
|
|
|
|
{
|
|
|
|
unsigned int conn_status = onion_connection_status(m->onion_c);
|
2013-09-22 07:27:53 +08:00
|
|
|
|
2015-03-12 03:37:25 +08:00
|
|
|
if (conn_status != m->last_connection_status) {
|
|
|
|
if (m->core_connection_change)
|
|
|
|
(*m->core_connection_change)(m, conn_status, m->core_connection_change_userdata);
|
|
|
|
|
|
|
|
m->last_connection_status = conn_status;
|
|
|
|
}
|
|
|
|
}
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-07-10 01:20:48 +08:00
|
|
|
|
2013-09-13 14:50:46 +08:00
|
|
|
#ifdef LOGGING
|
2013-09-16 16:37:22 +08:00
|
|
|
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
|
2013-09-13 14:50:46 +08:00
|
|
|
static time_t lastdump = 0;
|
2015-01-28 09:56:39 +08:00
|
|
|
static char IDString[crypto_box_PUBLICKEYBYTES * 2 + 1];
|
|
|
|
static char *ID2String(const uint8_t *pk)
|
2013-09-13 14:50:46 +08:00
|
|
|
{
|
2013-09-15 00:42:17 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++)
|
|
|
|
sprintf(&IDString[i * 2], "%02X", pk[i]);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2015-01-28 09:56:39 +08:00
|
|
|
IDString[crypto_box_PUBLICKEYBYTES * 2] = 0;
|
2013-09-15 00:42:17 +08:00
|
|
|
return IDString;
|
2013-09-13 14:50:46 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-29 10:29:39 +08:00
|
|
|
/* Minimum messenger run interval in ms
|
2014-06-28 03:28:02 +08:00
|
|
|
TODO: A/V */
|
|
|
|
#define MIN_RUN_INTERVAL 50
|
2014-05-31 11:01:17 +08:00
|
|
|
|
|
|
|
/* Return the time in milliseconds before do_messenger() should be called again
|
|
|
|
* for optimal performance.
|
|
|
|
*
|
|
|
|
* returns time (in ms) before the next do_messenger() needs to be run on success.
|
|
|
|
*/
|
2015-02-16 09:20:06 +08:00
|
|
|
uint32_t messenger_run_interval(const Messenger *m)
|
2014-05-31 11:01:17 +08:00
|
|
|
{
|
|
|
|
uint32_t crypto_interval = crypto_run_interval(m->net_crypto);
|
|
|
|
|
|
|
|
if (crypto_interval > MIN_RUN_INTERVAL) {
|
|
|
|
return MIN_RUN_INTERVAL;
|
|
|
|
} else {
|
|
|
|
return crypto_interval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* The main loop that needs to be run at least 20 times per second. */
|
2013-10-24 02:42:39 +08:00
|
|
|
void do_messenger(Messenger *m)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2014-09-13 01:21:17 +08:00
|
|
|
// Add the TCP relays, but only if this is the first time calling do_messenger
|
2014-09-13 05:07:27 +08:00
|
|
|
if (m->has_added_relays == 0) {
|
2014-09-13 01:21:17 +08:00
|
|
|
m->has_added_relays = 1;
|
|
|
|
|
|
|
|
int i;
|
2014-09-13 05:07:27 +08:00
|
|
|
|
2014-09-13 01:21:17 +08:00
|
|
|
for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) {
|
2015-01-30 08:38:44 +08:00
|
|
|
add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].public_key);
|
2014-09-13 01:21:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 04:32:28 +08:00
|
|
|
unix_time_update();
|
|
|
|
|
2014-08-15 03:27:34 +08:00
|
|
|
if (!m->options.udp_disabled) {
|
|
|
|
networking_poll(m->net);
|
|
|
|
do_DHT(m->dht);
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
do_net_crypto(m->net_crypto);
|
2014-01-18 02:35:40 +08:00
|
|
|
do_onion_client(m->onion_c);
|
2014-09-28 06:25:03 +08:00
|
|
|
do_friend_connections(m->fr_c);
|
2013-10-24 02:42:39 +08:00
|
|
|
do_friends(m);
|
2013-08-21 02:47:32 +08:00
|
|
|
LANdiscovery(m);
|
2015-03-12 03:37:25 +08:00
|
|
|
connection_status_cb(m);
|
2013-09-13 14:50:46 +08:00
|
|
|
|
|
|
|
#ifdef LOGGING
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-11-06 22:25:23 +08:00
|
|
|
if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-03 06:02:53 +08:00
|
|
|
#ifdef ENABLE_ASSOC_DHT
|
2013-11-17 08:04:49 +08:00
|
|
|
Assoc_status(m->dht->assoc);
|
2014-04-03 06:02:53 +08:00
|
|
|
#endif
|
2013-11-17 08:04:49 +08:00
|
|
|
|
2013-11-06 22:25:23 +08:00
|
|
|
lastdump = unix_time();
|
2013-09-15 00:42:17 +08:00
|
|
|
uint32_t client, last_pinged;
|
|
|
|
|
|
|
|
for (client = 0; client < LCLIENT_LIST; client++) {
|
|
|
|
Client_data *cptr = &m->dht->close_clientlist[client];
|
2013-09-27 09:27:52 +08:00
|
|
|
IPPTsPng *assoc = NULL;
|
2013-09-28 01:50:42 +08:00
|
|
|
uint32_t a;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-28 01:50:42 +08:00
|
|
|
for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
|
2013-10-20 22:56:12 +08:00
|
|
|
if (ip_isset(&assoc->ip_port.ip)) {
|
|
|
|
last_pinged = lastdump - assoc->last_pinged;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
if (last_pinged > 999)
|
|
|
|
last_pinged = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2015-01-11 06:09:51 +08:00
|
|
|
LOGGER_TRACE("C[%2u] %s:%u [%3u] %s",
|
2015-01-11 10:39:40 +08:00
|
|
|
client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
|
|
|
|
last_pinged, ID2String(cptr->client_id));
|
2013-10-20 22:56:12 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
uint32_t friend, dhtfriend;
|
|
|
|
|
|
|
|
/* dht contains additional "friends" (requests) */
|
|
|
|
uint32_t num_dhtfriends = m->dht->num_friends;
|
|
|
|
int32_t m2dht[num_dhtfriends];
|
|
|
|
int32_t dht2m[num_dhtfriends];
|
|
|
|
|
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++) {
|
|
|
|
m2dht[friend] = -1;
|
|
|
|
dht2m[friend] = -1;
|
|
|
|
|
|
|
|
if (friend >= m->numfriends)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++)
|
2015-01-28 09:56:39 +08:00
|
|
|
if (id_equal(m->friendlist[friend].real_pk, m->dht->friends_list[dhtfriend].client_id)) {
|
2013-12-03 01:20:37 +08:00
|
|
|
m2dht[friend] = dhtfriend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++)
|
|
|
|
if (m2dht[friend] >= 0)
|
|
|
|
dht2m[m2dht[friend]] = friend;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
|
|
|
if (m->numfriends != m->dht->num_friends) {
|
2015-01-11 06:09:51 +08:00
|
|
|
LOGGER_TRACE("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends);
|
2013-09-15 00:42:17 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
uint32_t ping_lastrecv;
|
|
|
|
Friend *msgfptr;
|
|
|
|
DHT_Friend *dhtfptr;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++) {
|
|
|
|
if (dht2m[friend] >= 0)
|
|
|
|
msgfptr = &m->friendlist[dht2m[friend]];
|
|
|
|
else
|
|
|
|
msgfptr = NULL;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
dhtfptr = &m->dht->friends_list[friend];
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
if (msgfptr) {
|
|
|
|
ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
if (ping_lastrecv > 999)
|
|
|
|
ping_lastrecv = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2015-01-11 06:09:51 +08:00
|
|
|
LOGGER_TRACE("F[%2u:%2u] <%s> [%03u] %s",
|
2015-01-11 10:39:40 +08:00
|
|
|
dht2m[friend], friend, msgfptr->name,
|
2015-01-28 09:56:39 +08:00
|
|
|
ping_lastrecv, ID2String(msgfptr->real_pk));
|
2013-12-03 01:20:37 +08:00
|
|
|
} else {
|
2015-01-11 06:09:51 +08:00
|
|
|
LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id));
|
2013-12-03 01:20:37 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
|
|
|
|
for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
|
|
|
|
Client_data *cptr = &dhtfptr->client_list[client];
|
2013-09-27 09:27:52 +08:00
|
|
|
IPPTsPng *assoc = NULL;
|
2013-09-28 01:50:42 +08:00
|
|
|
uint32_t a;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-28 01:50:42 +08:00
|
|
|
for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
|
2013-10-20 22:56:12 +08:00
|
|
|
if (ip_isset(&assoc->ip_port.ip)) {
|
|
|
|
last_pinged = lastdump - assoc->last_pinged;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
if (last_pinged > 999)
|
|
|
|
last_pinged = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2015-01-11 06:09:51 +08:00
|
|
|
LOGGER_TRACE("F[%2u] => C[%2u] %s:%u [%3u] %s",
|
2015-01-11 10:39:40 +08:00
|
|
|
friend, client, ip_ntoa(&assoc->ip_port.ip),
|
|
|
|
ntohs(assoc->ip_port.port), last_pinged,
|
|
|
|
ID2String(cptr->client_id));
|
2013-10-20 22:56:12 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
#endif /* LOGGING */
|
2013-08-10 07:30:18 +08:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* new messenger format for load/save, more robust and forward compatible */
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
#define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1f
|
2013-09-14 00:05:11 +08:00
|
|
|
|
|
|
|
#define MESSENGER_STATE_COOKIE_TYPE 0x01ce
|
2014-03-07 04:35:52 +08:00
|
|
|
#define MESSENGER_STATE_TYPE_NOSPAMKEYS 1
|
|
|
|
#define MESSENGER_STATE_TYPE_DHT 2
|
|
|
|
#define MESSENGER_STATE_TYPE_FRIENDS 3
|
|
|
|
#define MESSENGER_STATE_TYPE_NAME 4
|
|
|
|
#define MESSENGER_STATE_TYPE_STATUSMESSAGE 5
|
|
|
|
#define MESSENGER_STATE_TYPE_STATUS 6
|
2014-05-19 01:45:33 +08:00
|
|
|
#define MESSENGER_STATE_TYPE_TCP_RELAY 10
|
2014-08-15 04:56:29 +08:00
|
|
|
#define MESSENGER_STATE_TYPE_PATH_NODE 11
|
2013-09-14 00:05:11 +08:00
|
|
|
|
2014-04-23 23:35:40 +08:00
|
|
|
#define SAVED_FRIEND_REQUEST_SIZE 1024
|
2014-08-15 04:56:29 +08:00
|
|
|
#define NUM_SAVED_PATH_NODES 8
|
2014-03-06 22:55:50 +08:00
|
|
|
struct SAVED_FRIEND {
|
|
|
|
uint8_t status;
|
2015-01-28 09:56:39 +08:00
|
|
|
uint8_t real_pk[crypto_box_PUBLICKEYBYTES];
|
2014-04-23 23:35:40 +08:00
|
|
|
uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do.
|
2014-03-06 22:55:50 +08:00
|
|
|
uint16_t info_size; // Length of the info.
|
|
|
|
uint8_t name[MAX_NAME_LENGTH];
|
|
|
|
uint16_t name_length;
|
|
|
|
uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
|
|
|
|
uint16_t statusmessage_length;
|
|
|
|
uint8_t userstatus;
|
|
|
|
uint32_t friendrequest_nospam;
|
2014-03-14 08:20:48 +08:00
|
|
|
uint64_t ping_lastrecv;
|
2014-03-06 22:55:50 +08:00
|
|
|
};
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static uint32_t saved_friendslist_size(const Messenger *m)
|
2014-03-06 22:55:50 +08:00
|
|
|
{
|
|
|
|
return count_friendlist(m) * sizeof(struct SAVED_FRIEND);
|
|
|
|
}
|
|
|
|
|
2014-06-18 03:34:45 +08:00
|
|
|
static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
|
2014-03-06 22:55:50 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t num = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
|
|
|
if (m->friendlist[i].status > 0) {
|
|
|
|
struct SAVED_FRIEND temp;
|
|
|
|
memset(&temp, 0, sizeof(struct SAVED_FRIEND));
|
|
|
|
temp.status = m->friendlist[i].status;
|
2015-01-28 09:56:39 +08:00
|
|
|
memcpy(temp.real_pk, m->friendlist[i].real_pk, crypto_box_PUBLICKEYBYTES);
|
2014-03-06 22:55:50 +08:00
|
|
|
|
|
|
|
if (temp.status < 3) {
|
2014-04-23 23:35:40 +08:00
|
|
|
if (m->friendlist[i].info_size > SAVED_FRIEND_REQUEST_SIZE) {
|
|
|
|
memcpy(temp.info, m->friendlist[i].info, SAVED_FRIEND_REQUEST_SIZE);
|
|
|
|
} else {
|
|
|
|
memcpy(temp.info, m->friendlist[i].info, m->friendlist[i].info_size);
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
temp.info_size = htons(m->friendlist[i].info_size);
|
|
|
|
temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam;
|
|
|
|
} else {
|
|
|
|
memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length);
|
|
|
|
temp.name_length = htons(m->friendlist[i].name_length);
|
|
|
|
memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length);
|
|
|
|
temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length);
|
|
|
|
temp.userstatus = m->friendlist[i].userstatus;
|
2014-03-14 08:20:48 +08:00
|
|
|
|
|
|
|
uint8_t lastonline[sizeof(uint64_t)];
|
|
|
|
memcpy(lastonline, &m->friendlist[i].ping_lastrecv, sizeof(uint64_t));
|
|
|
|
host_to_net(lastonline, sizeof(uint64_t));
|
|
|
|
memcpy(&temp.ping_lastrecv, lastonline, sizeof(uint64_t));
|
2014-03-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND));
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num * sizeof(struct SAVED_FRIEND);
|
|
|
|
}
|
|
|
|
|
2014-06-11 02:54:48 +08:00
|
|
|
static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
|
2014-03-06 22:55:50 +08:00
|
|
|
{
|
2014-03-16 17:09:36 +08:00
|
|
|
if (length % sizeof(struct SAVED_FRIEND) != 0) {
|
2014-06-10 08:42:13 +08:00
|
|
|
return -1;
|
2014-03-16 17:09:36 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 08:42:13 +08:00
|
|
|
uint32_t num = length / sizeof(struct SAVED_FRIEND);
|
2014-03-06 22:55:50 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; ++i) {
|
|
|
|
struct SAVED_FRIEND temp;
|
2014-06-10 08:42:13 +08:00
|
|
|
memcpy(&temp, data + i * sizeof(struct SAVED_FRIEND), sizeof(struct SAVED_FRIEND));
|
2014-03-06 22:55:50 +08:00
|
|
|
|
|
|
|
if (temp.status >= 3) {
|
2015-01-28 09:56:39 +08:00
|
|
|
int fnum = m_addfriend_norequest(m, temp.real_pk);
|
2014-07-06 00:46:58 +08:00
|
|
|
|
|
|
|
if (fnum < 0)
|
|
|
|
continue;
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
setfriendname(m, fnum, temp.name, ntohs(temp.name_length));
|
|
|
|
set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length));
|
|
|
|
set_friend_userstatus(m, fnum, temp.userstatus);
|
2014-06-10 08:42:13 +08:00
|
|
|
uint8_t lastonline[sizeof(uint64_t)];
|
|
|
|
memcpy(lastonline, &temp.ping_lastrecv, sizeof(uint64_t));
|
|
|
|
net_to_host(lastonline, sizeof(uint64_t));
|
|
|
|
memcpy(&m->friendlist[fnum].ping_lastrecv, lastonline, sizeof(uint64_t));
|
2014-03-06 22:55:50 +08:00
|
|
|
} else if (temp.status != 0) {
|
|
|
|
/* TODO: This is not a good way to do this. */
|
|
|
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
2015-01-28 09:56:39 +08:00
|
|
|
id_copy(address, temp.real_pk);
|
2014-03-06 22:55:50 +08:00
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp.friendrequest_nospam), sizeof(uint32_t));
|
|
|
|
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
|
2014-03-14 03:10:59 +08:00
|
|
|
m_addfriend(m, address, temp.info, ntohs(temp.info_size));
|
2014-03-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* return size of the messenger data (for saving) */
|
2014-06-18 03:34:45 +08:00
|
|
|
uint32_t messenger_size(const Messenger *m)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
|
2013-09-14 00:05:11 +08:00
|
|
|
return size32 * 2 // global cookie
|
2013-09-17 07:08:57 +08:00
|
|
|
+ sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
2013-09-16 23:22:01 +08:00
|
|
|
+ sizesubhead + DHT_size(m->dht) // DHT
|
2014-03-06 22:55:50 +08:00
|
|
|
+ sizesubhead + saved_friendslist_size(m) // Friendlist itself.
|
2013-09-16 23:22:01 +08:00
|
|
|
+ sizesubhead + m->name_length // Own nickname.
|
2014-03-07 04:35:52 +08:00
|
|
|
+ sizesubhead + m->statusmessage_length // status message
|
|
|
|
+ sizesubhead + 1 // status
|
2014-05-19 01:45:33 +08:00
|
|
|
+ sizesubhead + NUM_SAVED_TCP_RELAYS * sizeof(Node_format) //TCP relays
|
2014-08-15 04:56:29 +08:00
|
|
|
+ sizesubhead + NUM_SAVED_PATH_NODES * sizeof(Node_format) //saved path nodes
|
2013-09-16 23:22:01 +08:00
|
|
|
;
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2014-10-25 09:04:27 +08:00
|
|
|
host_to_lendian32(data, len);
|
2014-10-22 04:15:49 +08:00
|
|
|
data += sizeof(uint32_t);
|
2014-10-25 09:04:27 +08:00
|
|
|
host_to_lendian32(data, (host_tolendian16(MESSENGER_STATE_COOKIE_TYPE) << 16) | host_tolendian16(type));
|
2014-10-22 04:15:49 +08:00
|
|
|
data += sizeof(uint32_t);
|
2013-09-14 00:05:11 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the messenger in data of size Messenger_size(). */
|
2014-06-18 03:34:45 +08:00
|
|
|
void messenger_save(const Messenger *m, uint8_t *data)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t len;
|
2013-09-14 00:05:11 +08:00
|
|
|
uint16_t type;
|
|
|
|
uint32_t *data32, size32 = sizeof(uint32_t);
|
|
|
|
|
|
|
|
data32 = (uint32_t *)data;
|
|
|
|
data32[0] = 0;
|
|
|
|
data32[1] = MESSENGER_STATE_COOKIE_GLOBAL;
|
|
|
|
data += size32 * 2;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t));
|
|
|
|
#endif
|
|
|
|
len = size32 + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
|
|
|
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
*(uint32_t *)data = get_nospam(&(m->fr));
|
|
|
|
save_keys(m->net_crypto, data + size32);
|
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = DHT_size(m->dht);
|
|
|
|
type = MESSENGER_STATE_TYPE_DHT;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
DHT_save(m->dht, data);
|
|
|
|
data += len;
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
len = saved_friendslist_size(m);
|
2013-09-14 00:05:11 +08:00
|
|
|
type = MESSENGER_STATE_TYPE_FRIENDS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
2014-03-06 22:55:50 +08:00
|
|
|
friends_list_save(m, data);
|
2013-09-14 00:05:11 +08:00
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = m->name_length;
|
|
|
|
type = MESSENGER_STATE_TYPE_NAME;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memcpy(data, m->name, len);
|
|
|
|
data += len;
|
2014-03-07 04:35:52 +08:00
|
|
|
|
|
|
|
len = m->statusmessage_length;
|
|
|
|
type = MESSENGER_STATE_TYPE_STATUSMESSAGE;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memcpy(data, m->statusmessage, len);
|
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = 1;
|
|
|
|
type = MESSENGER_STATE_TYPE_STATUS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
*data = m->userstatus;
|
|
|
|
data += len;
|
2013-11-15 02:05:36 +08:00
|
|
|
|
2014-05-19 01:45:33 +08:00
|
|
|
Node_format relays[NUM_SAVED_TCP_RELAYS];
|
|
|
|
len = sizeof(relays);
|
|
|
|
type = MESSENGER_STATE_TYPE_TCP_RELAY;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memset(relays, 0, len);
|
|
|
|
copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS);
|
|
|
|
memcpy(data, relays, len);
|
2014-08-15 04:56:29 +08:00
|
|
|
data += len;
|
|
|
|
|
|
|
|
Node_format nodes[NUM_SAVED_PATH_NODES];
|
|
|
|
len = sizeof(nodes);
|
|
|
|
type = MESSENGER_STATE_TYPE_PATH_NODE;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memset(nodes, 0, len);
|
|
|
|
onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
|
|
|
|
memcpy(data, nodes, len);
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2014-06-11 02:54:48 +08:00
|
|
|
static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
|
2014-03-06 22:55:50 +08:00
|
|
|
{
|
|
|
|
Messenger *m = outer;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MESSENGER_STATE_TYPE_NOSPAMKEYS:
|
|
|
|
if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) {
|
|
|
|
set_nospam(&(m->fr), *(uint32_t *)data);
|
|
|
|
load_keys(m->net_crypto, &data[sizeof(uint32_t)]);
|
|
|
|
#ifdef ENABLE_ASSOC_DHT
|
|
|
|
|
|
|
|
if (m->dht->assoc)
|
|
|
|
Assoc_self_client_id_changed(m->dht->assoc, m->net_crypto->self_public_key);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
return -1; /* critical */
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_DHT:
|
|
|
|
DHT_load(m->dht, data, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_FRIENDS:
|
|
|
|
friends_list_load(m, data, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_NAME:
|
2014-12-22 11:12:12 +08:00
|
|
|
if ((length > 0) && (length <= MAX_NAME_LENGTH)) {
|
2014-03-06 22:55:50 +08:00
|
|
|
setname(m, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-03-07 04:35:52 +08:00
|
|
|
case MESSENGER_STATE_TYPE_STATUSMESSAGE:
|
|
|
|
if ((length > 0) && (length < MAX_STATUSMESSAGE_LENGTH)) {
|
|
|
|
m_set_statusmessage(m, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_STATUS:
|
|
|
|
if (length == 1) {
|
|
|
|
m_set_userstatus(m, *data);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-05-19 01:45:33 +08:00
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_TCP_RELAY: {
|
2014-09-10 00:34:39 +08:00
|
|
|
if (length != sizeof(m->loaded_relays)) {
|
2014-05-19 01:45:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-10 00:34:39 +08:00
|
|
|
memcpy(m->loaded_relays, data, length);
|
2014-09-13 05:40:09 +08:00
|
|
|
m->has_added_relays = 0;
|
2014-08-15 04:56:29 +08:00
|
|
|
|
|
|
|
break;
|
2014-05-19 01:45:33 +08:00
|
|
|
}
|
2014-08-15 04:56:29 +08:00
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_PATH_NODE: {
|
|
|
|
Node_format nodes[NUM_SAVED_PATH_NODES];
|
|
|
|
|
|
|
|
if (length != sizeof(nodes)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(nodes, data, length);
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_SAVED_PATH_NODES; ++i) {
|
2015-01-30 08:38:44 +08:00
|
|
|
onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].public_key);
|
2014-08-15 04:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
|
|
|
|
length, type);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* Load the messenger from data of size length. */
|
2014-06-18 03:34:45 +08:00
|
|
|
int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2014-04-19 23:28:46 +08:00
|
|
|
uint32_t data32[2];
|
|
|
|
uint32_t cookie_len = sizeof(data32);
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
if (length < cookie_len)
|
|
|
|
return -1;
|
2013-09-14 00:05:11 +08:00
|
|
|
|
2014-04-19 23:28:46 +08:00
|
|
|
memcpy(data32, data, sizeof(data32));
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL))
|
|
|
|
return load_state(messenger_load_state_callback, m, data + cookie_len,
|
|
|
|
length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
|
2014-05-19 01:45:33 +08:00
|
|
|
else
|
2013-10-28 08:25:31 +08:00
|
|
|
return -1;
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
/* Return the number of friends in the instance m.
|
|
|
|
* You should use this to determine how much memory to allocate
|
|
|
|
* for copy_friendlist. */
|
2014-06-18 03:34:45 +08:00
|
|
|
uint32_t count_friendlist(const Messenger *m)
|
2013-09-22 05:47:30 +08:00
|
|
|
{
|
2013-09-23 02:24:38 +08:00
|
|
|
uint32_t ret = 0;
|
2013-09-22 05:47:30 +08:00
|
|
|
uint32_t i;
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
|
|
|
if (m->friendlist[i].status > 0) {
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-08 08:43:21 +08:00
|
|
|
/* Return the number of online friends in the instance m. */
|
2014-06-18 03:34:45 +08:00
|
|
|
uint32_t get_num_online_friends(const Messenger *m)
|
2014-02-08 08:43:21 +08:00
|
|
|
{
|
|
|
|
return m->numonline_friends;
|
|
|
|
}
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
/* Copy a list of valid friend IDs into the array out_list.
|
2013-09-23 02:24:38 +08:00
|
|
|
* If out_list is NULL, returns 0.
|
2013-09-22 05:47:30 +08:00
|
|
|
* Otherwise, returns the number of elements copied.
|
|
|
|
* If the array was too small, the contents
|
|
|
|
* of out_list will be truncated to list_size. */
|
2015-02-18 04:49:22 +08:00
|
|
|
uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_size)
|
2013-09-22 05:47:30 +08:00
|
|
|
{
|
|
|
|
if (!out_list)
|
2013-09-23 02:24:38 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
if (m->numfriends == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
uint32_t i;
|
2013-09-23 02:24:38 +08:00
|
|
|
uint32_t ret = 0;
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
2013-11-20 08:45:49 +08:00
|
|
|
if (ret >= list_size) {
|
2013-09-22 05:47:30 +08:00
|
|
|
break; /* Abandon ship */
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
if (m->friendlist[i].status > 0) {
|
2013-11-20 08:45:49 +08:00
|
|
|
out_list[ret] = i;
|
2013-09-22 05:47:30 +08:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|