Skip to content

Add toxcore capabilities to "online" packet sent to friends #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,30 @@ void getaddress(const Messenger *m, uint8_t *address)
memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, sizeof(checksum));
}

/*
* -- CAPABILITIES --
*/
static int send_online_packet(Messenger *m, int32_t friendnumber)
{
if (friend_not_valid(m, friendnumber)) {
return 0;
}

uint8_t buf[TOX_CAPABILITIES_SIZE + 1];
buf[0] = PACKET_ID_ONLINE;
net_pack_u64(buf + 1, TOX_CAPABILITIES_CURRENT);

write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), buf, TOX_CAPABILITIES_SIZE + 1, 0);
// TODO: what to do if res == -1 ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably return -1.


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;
}
/*
* -- CAPABILITIES --
*/

static int send_offline_packet(Messenger *m, int friendcon_id)
{
Expand All @@ -183,6 +197,7 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta
}

memset(&m->friendlist[m->numfriends], 0, sizeof(Friend));
m->friendlist[m->numfriends].toxcore_capabilities = TOX_CAPABILITY_BASIC;

int friendcon_id = new_friend_connection(m->fr_c, real_pk);

Expand All @@ -202,6 +217,7 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta
m->friendlist[i].userstatus = USERSTATUS_NONE;
m->friendlist[i].is_typing = 0;
m->friendlist[i].message_id = 0;
m->friendlist[i].toxcore_capabilities = TOX_CAPABILITY_BASIC;
friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet,
&m_handle_lossy_packet, m, i);

Expand Down Expand Up @@ -2157,6 +2173,23 @@ static int m_handle_status(void *object, int i, uint8_t status, void *userdata)
return 0;
}

/* get capabilities of friend's toxcore
* return TOX_CAPABILITY_BASIC on any error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a surprising error behaviour. If the friend doesn't exist, you say that they have basic capabilities. In fact they have no capabilities at all.

*/
uint64_t m_get_friend_toxcore_capabilities(const Messenger *m, int32_t friendnumber)
{
if (friend_not_valid(m, friendnumber)) {
return TOX_CAPABILITY_BASIC;
}

// TODO: maybe also return the value for OFFLINE friends?
if (m->friendlist[friendnumber].status == FRIEND_ONLINE) {
return m->friendlist[friendnumber].toxcore_capabilities;
}

return TOX_CAPABILITY_BASIC;
}

static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
{
if (len == 0) {
Expand All @@ -2169,7 +2202,13 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
uint32_t data_length = len - 1;

if (m->friendlist[i].status != FRIEND_ONLINE) {
if (packet_id == PACKET_ID_ONLINE && len == 1) {
if (packet_id == PACKET_ID_ONLINE) {
if (len == TOX_CAPABILITIES_SIZE) {
uint64_t received_caps = TOX_CAPABILITY_BASIC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead store.

net_unpack_u64(data, &received_caps);
m->friendlist[i].toxcore_capabilities = received_caps;
}

set_friend_status(m, i, FRIEND_ONLINE, userdata);
send_online_packet(m, i);
} else {
Expand Down
17 changes: 17 additions & 0 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ typedef struct Messenger_Options {
} Messenger_Options;


enum ToxCapabilitiesFlags {
TOX_CAPABILITY_BASIC = 1 << 0,
TOX_CAPABILITY_PGC = 1 << 1,
TOX_CAPABILITY_H264 = 1 << 2,
TOX_CAPABILITY_MESSAGE_V2 = 1 << 3,
};

#define TOX_CAPABILITIES_CURRENT (uint64_t)(TOX_CAPABILITY_BASIC)
#define TOX_CAPABILITIES_SIZE sizeof(uint64_t)


struct Receipts {
uint32_t packet_num;
uint32_t msg_id;
Expand Down Expand Up @@ -228,6 +239,7 @@ typedef struct Friend {

struct Receipts *receipts_start;
struct Receipts *receipts_end;
uint64_t toxcore_capabilities;
} Friend;

struct Messenger {
Expand Down Expand Up @@ -460,6 +472,11 @@ uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber);
uint8_t m_get_self_userstatus(const Messenger *m);


/* get capabilities of friend's toxcore
* return TOX_CAPABILITY_BASIC on any error
*/
uint64_t m_get_friend_toxcore_capabilities(const Messenger *m, int32_t friendnumber);

/* returns timestamp of last time friendnumber was seen online or 0 if never seen.
* if friendnumber is invalid this function will return UINT64_MAX.
*/
Expand Down