2013-07-07 10:28:15 +08:00
|
|
|
/* Messenger.c
|
|
|
|
*
|
|
|
|
* An implementation of a simple text chat only messenger on the tox network core.
|
|
|
|
*
|
2013-07-11 00:54:39 +08:00
|
|
|
|
|
|
|
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-07 10:28:15 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Messenger.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
2013-07-09 08:50:25 +08:00
|
|
|
int crypt_connection_id;
|
|
|
|
int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend.
|
2013-07-10 01:42:28 +08:00
|
|
|
uint8_t status;//0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online.
|
2013-07-11 05:31:58 +08:00
|
|
|
uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do
|
|
|
|
uint16_t info_size; //length of the info
|
2013-07-07 10:28:15 +08:00
|
|
|
}Friend;
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
|
2013-07-17 07:02:44 +08:00
|
|
|
|
|
|
|
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
#define MAX_NUM_FRIENDS 256
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static Friend friendlist[MAX_NUM_FRIENDS];
|
2013-07-09 08:50:25 +08:00
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static uint32_t numfriends;
|
2013-07-17 07:02:44 +08:00
|
|
|
|
|
|
|
//1 if we are online
|
|
|
|
//0 if we are offline
|
|
|
|
//static uint8_t online;
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
|
|
|
|
//return the friend id associated to that public key.
|
|
|
|
//return -1 if no such friend
|
|
|
|
int getfriend_id(uint8_t * client_id)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < numfriends; i++)
|
|
|
|
{
|
|
|
|
if(friendlist[i].status > 0)
|
|
|
|
{
|
|
|
|
if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-15 22:42:50 +08:00
|
|
|
|
2013-07-14 05:44:43 +08:00
|
|
|
//copies the public key associated to that friend id into client_id buffer.
|
|
|
|
//make sure that client_id is of size CLIENT_ID_SIZE.
|
|
|
|
//returns 0 if success
|
|
|
|
//return -1 if failure.
|
|
|
|
int getclient_id(int friend_id, uint8_t * client_id)
|
|
|
|
{
|
2013-07-14 06:20:58 +08:00
|
|
|
if(friend_id >= numfriends || friend_id < 0)
|
2013-07-14 05:44:43 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(friendlist[friend_id].status > 0)
|
|
|
|
{
|
|
|
|
memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
//add a friend
|
2013-07-11 05:31:58 +08:00
|
|
|
//set the data that will be sent along with friend request
|
|
|
|
//client_id is the client id of the friend
|
|
|
|
//data is the data and length is the length
|
2013-07-07 10:28:15 +08:00
|
|
|
//returns the friend number if success
|
|
|
|
//return -1 if failure.
|
2013-07-11 05:31:58 +08:00
|
|
|
int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-07-15 22:42:50 +08:00
|
|
|
if(length == 0 || length >=
|
|
|
|
(MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES))
|
2013-07-11 05:31:58 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-17 07:02:44 +08:00
|
|
|
if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-10 08:25:52 +08:00
|
|
|
if(getfriend_id(client_id) != -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t i;
|
2013-07-15 22:42:50 +08:00
|
|
|
for(i = 0; i <= numfriends; i++)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
|
|
|
if(friendlist[i].status == 0)
|
|
|
|
{
|
|
|
|
DHT_addfriend(client_id);
|
|
|
|
friendlist[i].status = 1;
|
2013-07-17 07:02:44 +08:00
|
|
|
friendlist[i].crypt_connection_id = -1;
|
2013-07-10 08:25:52 +08:00
|
|
|
friendlist[i].friend_request_id = -1;
|
|
|
|
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
2013-07-11 05:31:58 +08:00
|
|
|
|
|
|
|
memcpy(friendlist[i].info, data, length);
|
|
|
|
friendlist[i].info_size = length;
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
numfriends++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:20:48 +08:00
|
|
|
int m_addfriend_norequest(uint8_t * client_id)
|
|
|
|
{
|
2013-07-10 08:25:52 +08:00
|
|
|
if(getfriend_id(client_id) != -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t i;
|
2013-07-15 22:42:50 +08:00
|
|
|
for(i = 0; i <= numfriends; i++)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
|
|
|
if(friendlist[i].status == 0)
|
|
|
|
{
|
|
|
|
DHT_addfriend(client_id);
|
|
|
|
friendlist[i].status = 2;
|
2013-07-17 07:02:44 +08:00
|
|
|
friendlist[i].crypt_connection_id = -1;
|
2013-07-10 08:25:52 +08:00
|
|
|
friendlist[i].friend_request_id = -1;
|
|
|
|
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
numfriends++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
//remove a friend
|
2013-07-10 08:25:52 +08:00
|
|
|
//returns 0 if success
|
|
|
|
//return -1 if failure.
|
2013-07-07 10:28:15 +08:00
|
|
|
int m_delfriend(int friendnumber)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
|
|
|
if(friendnumber >= numfriends || friendnumber < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
DHT_delfriend(friendlist[friendnumber].client_id);
|
2013-07-14 23:52:32 +08:00
|
|
|
crypto_kill(friendlist[friendnumber].crypt_connection_id);
|
2013-07-10 08:25:52 +08:00
|
|
|
memset(&friendlist[friendnumber], 0, sizeof(Friend));
|
|
|
|
uint32_t i;
|
|
|
|
for(i = numfriends; i != 0; i--)
|
|
|
|
{
|
|
|
|
if(friendlist[i].status != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
numfriends = i;
|
|
|
|
return 0;
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//return 4 if friend is online
|
|
|
|
//return 3 if friend is confirmed
|
2013-07-10 01:42:28 +08:00
|
|
|
//return 2 if the friend request was sent
|
2013-07-09 08:50:25 +08:00
|
|
|
//return 1 if the friend was added
|
|
|
|
//return 0 if there is no friend with that number.
|
|
|
|
int m_friendstatus(int friendnumber)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-07-09 08:50:25 +08:00
|
|
|
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return friendlist[friendnumber].status;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
//send a text chat message to an online friend.
|
|
|
|
//returns 1 if packet was successfully put into the send queue
|
|
|
|
//return 0 if it was not.
|
|
|
|
int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-07-10 01:20:48 +08:00
|
|
|
if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
|
2013-07-09 08:50:25 +08:00
|
|
|
//this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less.
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
|
|
|
temp[0] = 64;
|
|
|
|
memcpy(temp + 1, message, length);
|
|
|
|
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
|
2013-07-09 08:50:25 +08:00
|
|
|
|
|
|
|
//set the function that will be executed when a friend request is received.
|
2013-07-10 01:20:48 +08:00
|
|
|
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-07-09 08:50:25 +08:00
|
|
|
friend_request = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static void (*friend_message)(int, uint8_t *, uint16_t);
|
2013-07-09 08:50:25 +08:00
|
|
|
|
|
|
|
//set the function that will be executed when a message from a friend is received.
|
2013-07-10 01:20:48 +08:00
|
|
|
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
|
|
|
friend_message = function;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define PORT 33445
|
|
|
|
//run this at startup
|
2013-07-09 08:50:25 +08:00
|
|
|
void initMessenger()
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
|
|
|
new_keys();
|
2013-07-09 08:50:25 +08:00
|
|
|
initNetCrypto();
|
2013-07-07 10:28:15 +08:00
|
|
|
IP ip;
|
|
|
|
ip.i = 0;
|
|
|
|
init_networking(ip, PORT);
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static void doFriends()
|
2013-07-09 08:50:25 +08:00
|
|
|
{//TODO: add incoming connections and some other stuff.
|
|
|
|
uint32_t i;
|
|
|
|
int len;
|
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
|
|
|
for(i = 0; i < numfriends; i++)
|
|
|
|
{
|
|
|
|
if(friendlist[i].status == 1)
|
|
|
|
{
|
|
|
|
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
|
|
|
|
int request = check_friendrequest(friendlist[i].friend_request_id);
|
|
|
|
//printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id);
|
|
|
|
if(friendip.ip.i > 1 && request == -1)
|
|
|
|
{
|
2013-07-11 05:31:58 +08:00
|
|
|
friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id,
|
|
|
|
friendip, friendlist[i].info, friendlist[i].info_size);
|
2013-07-09 08:50:25 +08:00
|
|
|
friendlist[i].status = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(friendlist[i].status == 2 || friendlist[i].status == 3)
|
|
|
|
{
|
2013-07-10 01:20:48 +08:00
|
|
|
check_friendrequest(friendlist[i].friend_request_id);//for now this is used to kill the friend request
|
2013-07-09 08:50:25 +08:00
|
|
|
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
|
2013-07-15 22:42:50 +08:00
|
|
|
switch(is_cryptoconnected(friendlist[i].crypt_connection_id))
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
2013-07-15 22:42:50 +08:00
|
|
|
case 0:
|
|
|
|
if (friendip.ip.i > 1)
|
|
|
|
friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
|
|
|
|
break;
|
|
|
|
case 3: // Connection is established
|
|
|
|
friendlist[i].status = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
crypto_kill(friendlist[i].crypt_connection_id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
while(friendlist[i].status == 4)
|
|
|
|
{
|
|
|
|
len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
|
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
if(temp[0] == 64)
|
|
|
|
{
|
2013-07-14 10:28:25 +08:00
|
|
|
(*friend_message)(i, temp + 1, len - 1);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it
|
|
|
|
{
|
|
|
|
crypto_kill(friendlist[i].crypt_connection_id);
|
|
|
|
friendlist[i].status = 3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static void doFriendRequest()
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
|
|
|
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
|
|
|
|
|
|
|
int len = handle_friendrequest(public_key, temp);
|
|
|
|
if(len >= 0)
|
|
|
|
{
|
|
|
|
(*friend_request)(public_key, temp, len);
|
|
|
|
}
|
|
|
|
}
|
2013-07-10 01:20:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-13 04:27:19 +08:00
|
|
|
static void doInbound()
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
|
|
|
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
|
|
|
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
|
|
|
if(inconnection != -1)
|
|
|
|
{
|
|
|
|
int friend_id = getfriend_id(public_key);
|
|
|
|
if(friend_id != -1)
|
|
|
|
{
|
|
|
|
friendlist[friend_id].crypt_connection_id =
|
|
|
|
accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
|
|
|
|
|
|
|
|
friendlist[friend_id].status = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
//the main loop that needs to be run at least 200 times per second.
|
2013-07-09 08:50:25 +08:00
|
|
|
void doMessenger()
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
|
|
|
IP_Port ip_port;
|
|
|
|
uint8_t data[MAX_UDP_PACKET_SIZE];
|
|
|
|
uint32_t length;
|
2013-07-09 08:50:25 +08:00
|
|
|
while(receivepacket(&ip_port, data, &length) != -1)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-07-14 23:40:42 +08:00
|
|
|
#ifdef DEBUG
|
2013-07-07 10:28:15 +08:00
|
|
|
//if(rand() % 3 != 1)//simulate packet loss
|
|
|
|
//{
|
|
|
|
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
|
|
|
|
{
|
|
|
|
//if packet is discarded
|
2013-07-14 23:40:42 +08:00
|
|
|
printf("Received unhandled packet with length: %u\n", length);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-14 23:40:42 +08:00
|
|
|
printf("Received handled packet with length: %u\n", length);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
//}
|
2013-07-17 07:02:44 +08:00
|
|
|
printf("Status: %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id) );
|
2013-07-14 23:40:42 +08:00
|
|
|
#else
|
|
|
|
DHT_handlepacket(data, length, ip_port);
|
|
|
|
LosslessUDP_handlepacket(data, length, ip_port);
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
doDHT();
|
|
|
|
doLossless_UDP();
|
|
|
|
doNetCrypto();
|
2013-07-10 01:20:48 +08:00
|
|
|
doInbound();
|
2013-07-09 08:50:25 +08:00
|
|
|
doFriendRequest();
|
|
|
|
doFriends();
|
|
|
|
}
|
2013-07-17 07:02:44 +08:00
|
|
|
|