mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge branch 'master' of git://github.com/irungentoo/ProjectTox-Core
This commit is contained in:
commit
95a3f69580
@ -8,7 +8,9 @@ set(core_sources
|
|||||||
net_crypto.c
|
net_crypto.c
|
||||||
friend_requests.c
|
friend_requests.c
|
||||||
LAN_discovery.c
|
LAN_discovery.c
|
||||||
Messenger.c)
|
Messenger.c
|
||||||
|
util.c
|
||||||
|
ping.c)
|
||||||
|
|
||||||
if(SHARED_TOXCORE)
|
if(SHARED_TOXCORE)
|
||||||
add_library(toxcore SHARED ${core_sources})
|
add_library(toxcore SHARED ${core_sources})
|
||||||
|
63
core/DHT.c
63
core/DHT.c
@ -24,6 +24,7 @@
|
|||||||
/*----------------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
#include "ping.h"
|
||||||
|
|
||||||
/* maximum number of clients stored per friend. */
|
/* maximum number of clients stored per friend. */
|
||||||
#define MAX_FRIEND_CLIENTS 8
|
#define MAX_FRIEND_CLIENTS 8
|
||||||
@ -109,7 +110,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
|||||||
static Client_data close_clientlist[LCLIENT_LIST];
|
static Client_data close_clientlist[LCLIENT_LIST];
|
||||||
static Friend * friends_list;
|
static Friend * friends_list;
|
||||||
static uint16_t num_friends;
|
static uint16_t num_friends;
|
||||||
static Pinged pings[LPING_ARRAY];
|
|
||||||
static Pinged send_nodes[LSEND_NODES_ARRAY];
|
static Pinged send_nodes[LSEND_NODES_ARRAY];
|
||||||
|
|
||||||
/*----------------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------------*/
|
||||||
@ -426,35 +426,6 @@ static void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check if we are currently pinging an ip_port and/or a ping_id variables with
|
|
||||||
* values of zero will not be checked. If we are already, return 1 else return 0
|
|
||||||
*
|
|
||||||
* TODO: optimize this
|
|
||||||
*/
|
|
||||||
static int is_pinging(IP_Port ip_port, uint64_t ping_id)
|
|
||||||
{
|
|
||||||
uint32_t i;
|
|
||||||
uint8_t pinging;
|
|
||||||
uint64_t temp_time = unix_time();
|
|
||||||
|
|
||||||
for (i = 0; i < LPING_ARRAY; ++i ) {
|
|
||||||
if (!is_timeout(temp_time, pings[i].timestamp, PING_TIMEOUT)) {
|
|
||||||
pinging = 0;
|
|
||||||
|
|
||||||
if (ip_port.ip.i != 0 && ipport_equal(pings[i].ip_port, ip_port))
|
|
||||||
++pinging;
|
|
||||||
|
|
||||||
if (ping_id != 0 && pings[i].ping_id == ping_id)
|
|
||||||
++pinging;
|
|
||||||
|
|
||||||
if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Same as last function but for get_node requests. */
|
/* Same as last function but for get_node requests. */
|
||||||
static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
|
static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
|
||||||
{
|
{
|
||||||
@ -480,32 +451,6 @@ static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add a new ping request to the list of ping requests
|
|
||||||
* returns the ping_id to put in the ping request
|
|
||||||
* returns 0 if problem.
|
|
||||||
*
|
|
||||||
* TODO: optimize this
|
|
||||||
*/
|
|
||||||
static uint64_t add_pinging(IP_Port ip_port)
|
|
||||||
{
|
|
||||||
uint32_t i, j;
|
|
||||||
uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
|
|
||||||
uint64_t temp_time = unix_time();
|
|
||||||
|
|
||||||
for(i = 0; i < PING_TIMEOUT; ++i ) {
|
|
||||||
for(j = 0; j < LPING_ARRAY; ++j ) {
|
|
||||||
if(is_timeout(temp_time, pings[j].timestamp, PING_TIMEOUT - i)) {
|
|
||||||
pings[j].timestamp = temp_time;
|
|
||||||
pings[j].ip_port = ip_port;
|
|
||||||
pings[j].ping_id = ping_id;
|
|
||||||
return ping_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Same but for get node requests */
|
/* Same but for get node requests */
|
||||||
static uint64_t add_gettingnodes(IP_Port ip_port)
|
static uint64_t add_gettingnodes(IP_Port ip_port)
|
||||||
{
|
{
|
||||||
@ -536,7 +481,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
|
|||||||
if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
|
if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
uint64_t ping_id = add_pinging(ip_port);
|
uint64_t ping_id = add_ping(ip_port);
|
||||||
if(ping_id == 0)
|
if(ping_id == 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
@ -1108,7 +1053,7 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
|
|||||||
static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
|
static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
|
if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
|
||||||
&& length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
|| length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* check if request is for us. */
|
/* check if request is for us. */
|
||||||
@ -1301,6 +1246,8 @@ void DHT_save(uint8_t * data)
|
|||||||
*/
|
*/
|
||||||
int DHT_load(uint8_t * data, uint32_t size)
|
int DHT_load(uint8_t * data, uint32_t size)
|
||||||
{
|
{
|
||||||
|
init_ping();
|
||||||
|
|
||||||
if(size < sizeof(close_clientlist))
|
if(size < sizeof(close_clientlist))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ typedef struct {
|
|||||||
uint8_t *userstatus;
|
uint8_t *userstatus;
|
||||||
uint16_t userstatus_length;
|
uint16_t userstatus_length;
|
||||||
uint8_t userstatus_sent;
|
uint8_t userstatus_sent;
|
||||||
|
USERSTATUS_KIND userstatus_kind;
|
||||||
uint16_t info_size; /* length of the info */
|
uint16_t info_size; /* length of the info */
|
||||||
} Friend;
|
} Friend;
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ static uint16_t self_name_length;
|
|||||||
|
|
||||||
static uint8_t *self_userstatus;
|
static uint8_t *self_userstatus;
|
||||||
static uint16_t self_userstatus_len;
|
static uint16_t self_userstatus_len;
|
||||||
|
static USERSTATUS_KIND self_userstatus_kind;
|
||||||
|
|
||||||
#define MAX_NUM_FRIENDS 256
|
#define MAX_NUM_FRIENDS 256
|
||||||
|
|
||||||
@ -123,6 +125,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
|||||||
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||||
friendlist[i].userstatus = calloc(1, 1);
|
friendlist[i].userstatus = calloc(1, 1);
|
||||||
friendlist[i].userstatus_length = 1;
|
friendlist[i].userstatus_length = 1;
|
||||||
|
friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE;
|
||||||
memcpy(friendlist[i].info, data, length);
|
memcpy(friendlist[i].info, data, length);
|
||||||
friendlist[i].info_size = length;
|
friendlist[i].info_size = length;
|
||||||
|
|
||||||
@ -269,10 +272,13 @@ int getname(int friendnumber, uint8_t * name)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int m_set_userstatus(uint8_t *status, uint16_t length)
|
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length)
|
||||||
{
|
{
|
||||||
if (length > MAX_USERSTATUS_LENGTH)
|
if (length > MAX_USERSTATUS_LENGTH)
|
||||||
return -1;
|
return -1;
|
||||||
|
if (kind != USERSTATUS_KIND_RETAIN) {
|
||||||
|
self_userstatus_kind = kind;
|
||||||
|
}
|
||||||
uint8_t *newstatus = calloc(length, 1);
|
uint8_t *newstatus = calloc(length, 1);
|
||||||
memcpy(newstatus, status, length);
|
memcpy(newstatus, status, length);
|
||||||
free(self_userstatus);
|
free(self_userstatus);
|
||||||
@ -285,6 +291,20 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int m_set_userstatus_kind(USERSTATUS_KIND kind) {
|
||||||
|
if (kind >= USERSTATUS_KIND_INVALID) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (kind == USERSTATUS_KIND_RETAIN) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
self_userstatus_kind = kind;
|
||||||
|
uint32_t i;
|
||||||
|
for (i = 0; i < numfriends; ++i)
|
||||||
|
friendlist[i].userstatus_sent = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* return the size of friendnumber's user status
|
/* return the size of friendnumber's user status
|
||||||
guaranteed to be at most MAX_USERSTATUS_LENGTH */
|
guaranteed to be at most MAX_USERSTATUS_LENGTH */
|
||||||
int m_get_userstatus_size(int friendnumber)
|
int m_get_userstatus_size(int friendnumber)
|
||||||
@ -305,11 +325,33 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int m_copy_self_userstatus(uint8_t * buf, uint32_t maxlen)
|
||||||
|
{
|
||||||
|
memset(buf, 0, maxlen);
|
||||||
|
memcpy(buf, self_userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber) {
|
||||||
|
if (friendnumber >= numfriends || friendnumber < 0)
|
||||||
|
return USERSTATUS_KIND_INVALID;
|
||||||
|
USERSTATUS_KIND uk = friendlist[friendnumber].userstatus_kind;
|
||||||
|
if (uk >= USERSTATUS_KIND_INVALID) {
|
||||||
|
uk = USERSTATUS_KIND_ONLINE;
|
||||||
|
}
|
||||||
|
return uk;
|
||||||
|
}
|
||||||
|
|
||||||
|
USERSTATUS_KIND m_get_self_userstatus_kind(void) {
|
||||||
|
return self_userstatus_kind;
|
||||||
|
}
|
||||||
|
|
||||||
static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
||||||
{
|
{
|
||||||
uint8_t *thepacket = malloc(length + 1);
|
uint8_t *thepacket = malloc(length + 2);
|
||||||
memcpy(thepacket + 1, status, length);
|
memcpy(thepacket + 2, status, length);
|
||||||
thepacket[0] = PACKET_ID_USERSTATUS;
|
thepacket[0] = PACKET_ID_USERSTATUS;
|
||||||
|
thepacket[1] = self_userstatus_kind;
|
||||||
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
|
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
|
||||||
free(thepacket);
|
free(thepacket);
|
||||||
return written;
|
return written;
|
||||||
@ -327,6 +369,11 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_friend_userstatus_kind(int friendnumber, USERSTATUS_KIND k)
|
||||||
|
{
|
||||||
|
friendlist[friendnumber].userstatus_kind = k;
|
||||||
|
}
|
||||||
|
|
||||||
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
|
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
|
||||||
static uint8_t friend_request_isset = 0; */
|
static uint8_t friend_request_isset = 0; */
|
||||||
/* set the function that will be executed when a friend request is received. */
|
/* set the function that will be executed when a friend request is received. */
|
||||||
@ -353,9 +400,9 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
|
|||||||
friend_namechange_isset = 1;
|
friend_namechange_isset = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void (*friend_statuschange)(int, uint8_t *, uint16_t);
|
static void (*friend_statuschange)(int, USERSTATUS_KIND, uint8_t *, uint16_t);
|
||||||
static uint8_t friend_statuschange_isset = 0;
|
static uint8_t friend_statuschange_isset = 0;
|
||||||
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
|
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t))
|
||||||
{
|
{
|
||||||
friend_statuschange = function;
|
friend_statuschange = function;
|
||||||
friend_statuschange_isset = 1;
|
friend_statuschange_isset = 1;
|
||||||
@ -366,7 +413,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
|
|||||||
int initMessenger(void)
|
int initMessenger(void)
|
||||||
{
|
{
|
||||||
new_keys();
|
new_keys();
|
||||||
m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
|
m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online"));
|
||||||
initNetCrypto();
|
initNetCrypto();
|
||||||
IP ip;
|
IP ip;
|
||||||
ip.i = 0;
|
ip.i = 0;
|
||||||
@ -438,12 +485,17 @@ static void doFriends(void)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PACKET_ID_USERSTATUS: {
|
case PACKET_ID_USERSTATUS: {
|
||||||
uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
|
if (len > 2) {
|
||||||
memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
uint8_t *status = calloc(MIN(len - 2, MAX_USERSTATUS_LENGTH), 1);
|
||||||
|
memcpy(status, temp + 2, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||||
if (friend_statuschange_isset)
|
if (friend_statuschange_isset)
|
||||||
friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||||
set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||||
free(status);
|
free(status);
|
||||||
|
} else if (friend_statuschange_isset) {
|
||||||
|
friend_statuschange(i, temp[1], friendlist[i].userstatus, friendlist[i].userstatus_length);
|
||||||
|
}
|
||||||
|
set_friend_userstatus_kind(i, temp[1]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PACKET_ID_MESSAGE: {
|
case PACKET_ID_MESSAGE: {
|
||||||
|
@ -60,6 +60,22 @@ extern "C" {
|
|||||||
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
||||||
to an absurdly large number later */
|
to an absurdly large number later */
|
||||||
|
|
||||||
|
/* USERSTATUS_KIND
|
||||||
|
* Represents the different kinds of userstatus
|
||||||
|
* someone can have.
|
||||||
|
* More on this later... */
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
USERSTATUS_KIND_RETAIN = (uint8_t)0, /* This is a special value that must not be returned by
|
||||||
|
* m_get_userstatus_kind. You can pass it into m_set_userstatus
|
||||||
|
* to keep the current USERSTATUS_KIND. */
|
||||||
|
USERSTATUS_KIND_ONLINE, /* Recommended representation: Green. */
|
||||||
|
USERSTATUS_KIND_AWAY, /* Recommended representation: Orange, or yellow. */
|
||||||
|
USERSTATUS_KIND_BUSY, /* Recommended representation: Red. */
|
||||||
|
USERSTATUS_KIND_OFFLINE, /* Recommended representation: Grey, semi-transparent. */
|
||||||
|
USERSTATUS_KIND_INVALID,
|
||||||
|
} USERSTATUS_KIND;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* add a friend
|
* add a friend
|
||||||
* set the data that will be sent along with friend request
|
* set the data that will be sent along with friend request
|
||||||
@ -128,7 +144,8 @@ int getname(int friendnumber, uint8_t *name);
|
|||||||
/* set our user status
|
/* set our user status
|
||||||
you are responsible for freeing status after
|
you are responsible for freeing status after
|
||||||
returns 0 on success, -1 on failure */
|
returns 0 on success, -1 on failure */
|
||||||
int m_set_userstatus(uint8_t *status, uint16_t length);
|
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length);
|
||||||
|
int m_set_userstatus_kind(USERSTATUS_KIND kind);
|
||||||
|
|
||||||
/* return the length of friendnumber's user status,
|
/* return the length of friendnumber's user status,
|
||||||
including null
|
including null
|
||||||
@ -136,8 +153,17 @@ int m_set_userstatus(uint8_t *status, uint16_t length);
|
|||||||
int m_get_userstatus_size(int friendnumber);
|
int m_get_userstatus_size(int friendnumber);
|
||||||
|
|
||||||
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
||||||
get the size you need to allocate from m_get_userstatus_size */
|
get the size you need to allocate from m_get_userstatus_size
|
||||||
|
The self variant will copy our own userstatus. */
|
||||||
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||||
|
int m_copy_self_userstatus(uint8_t *buf, uint32_t maxlen);
|
||||||
|
|
||||||
|
/* Return one of USERSTATUS_KIND values, except USERSTATUS_KIND_RETAIN.
|
||||||
|
* Values unknown to your application should be represented as USERSTATUS_KIND_ONLINE.
|
||||||
|
* As above, the self variant will return our own USERSTATUS_KIND.
|
||||||
|
* If friendnumber is invalid, this shall return USERSTATUS_KIND_INVALID. */
|
||||||
|
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber);
|
||||||
|
USERSTATUS_KIND m_get_self_userstatus_kind(void);
|
||||||
|
|
||||||
/* set the function that will be executed when a friend request is received.
|
/* set the function that will be executed when a friend request is received.
|
||||||
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
||||||
@ -153,9 +179,9 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
|||||||
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
|
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
|
||||||
|
|
||||||
/* set the callback for user status changes
|
/* set the callback for user status changes
|
||||||
function(int friendnumber, uint8_t *newstatus, uint16_t length)
|
function(int friendnumber, USERSTATUS_KIND kind, uint8_t *newstatus, uint16_t length)
|
||||||
you are not responsible for freeing newstatus */
|
you are not responsible for freeing newstatus */
|
||||||
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
|
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t));
|
||||||
|
|
||||||
/* run this at startup
|
/* run this at startup
|
||||||
returns 0 if no connection problems
|
returns 0 if no connection problems
|
||||||
|
@ -104,7 +104,7 @@ static int request_recieved(uint8_t * client_id)
|
|||||||
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
if (packet[0] == 32) {
|
if (packet[0] == 32) {
|
||||||
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
|
||||||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||||
return 1;
|
return 1;
|
||||||
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
|
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
|
||||||
|
103
core/ping.c
Normal file
103
core/ping.c
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* ping.c -- Buffered pinging using cyclic arrays.
|
||||||
|
*
|
||||||
|
* This file is donated to the Tox Project.
|
||||||
|
* Copyright 2013 plutooo
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "network.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#define PING_NUM_MAX 256
|
||||||
|
#define PING_TIMEOUT 5 // 5s
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
IP_Port ipp;
|
||||||
|
uint64_t id;
|
||||||
|
uint64_t timestamp;
|
||||||
|
} pinged_t;
|
||||||
|
|
||||||
|
static pinged_t pings[PING_NUM_MAX];
|
||||||
|
static size_t num_pings;
|
||||||
|
static size_t pos_pings;
|
||||||
|
|
||||||
|
|
||||||
|
void init_ping()
|
||||||
|
{
|
||||||
|
num_pings = 0;
|
||||||
|
pos_pings = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool is_timeout(uint64_t time)
|
||||||
|
{
|
||||||
|
return (time + PING_TIMEOUT) < now();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void remove_timeouts() // O(n)
|
||||||
|
{
|
||||||
|
size_t i, id;
|
||||||
|
size_t new_pos = pos_pings;
|
||||||
|
size_t new_num = num_pings;
|
||||||
|
|
||||||
|
// Loop through buffer, oldest first
|
||||||
|
for (i=0; i<num_pings; i++) {
|
||||||
|
id = (pos_pings + i) % PING_NUM_MAX;
|
||||||
|
|
||||||
|
if(is_timeout(pings[id].timestamp)) {
|
||||||
|
new_pos++;
|
||||||
|
new_num--;
|
||||||
|
}
|
||||||
|
// Break here because list is sorted.
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
num_pings = new_num;
|
||||||
|
pos_pings = new_pos % PING_NUM_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t add_ping(IP_Port ipp) // O(n)
|
||||||
|
{
|
||||||
|
size_t p;
|
||||||
|
|
||||||
|
remove_timeouts();
|
||||||
|
|
||||||
|
// Remove oldest ping if full buffer
|
||||||
|
if (num_pings == PING_NUM_MAX) {
|
||||||
|
num_pings--;
|
||||||
|
pos_pings = (pos_pings + 1) % PING_NUM_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert new ping at end of list
|
||||||
|
p = (pos_pings + num_pings) % PING_NUM_MAX;
|
||||||
|
|
||||||
|
pings[p].ipp = ipp;
|
||||||
|
pings[p].timestamp = now();
|
||||||
|
pings[p].id = random_64b();
|
||||||
|
|
||||||
|
num_pings++;
|
||||||
|
return pings[p].id;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else.
|
||||||
|
{
|
||||||
|
if (ipp.ip.i == 0 && ping_id == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t i, id;
|
||||||
|
|
||||||
|
remove_timeouts();
|
||||||
|
|
||||||
|
for (i=0; i<num_pings; i++) {
|
||||||
|
id = (pos_pings + i) % PING_NUM_MAX;
|
||||||
|
|
||||||
|
if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
13
core/ping.h
Normal file
13
core/ping.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* ping.h -- Buffered pinging using cyclic arrays.
|
||||||
|
*
|
||||||
|
* This file is donated to the Tox Project.
|
||||||
|
* Copyright 2013 plutooo
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
void init_ping();
|
||||||
|
uint64_t add_ping(IP_Port ipp);
|
||||||
|
bool is_pinging(IP_Port ipp, uint64_t ping_id);
|
||||||
|
|
34
core/util.c
Normal file
34
core/util.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* util.c -- Utilities.
|
||||||
|
*
|
||||||
|
* This file is donated to the Tox Project.
|
||||||
|
* Copyright 2013 plutooo
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "network.h"
|
||||||
|
|
||||||
|
uint64_t now()
|
||||||
|
{
|
||||||
|
return time(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t random_64b()
|
||||||
|
{
|
||||||
|
uint64_t r;
|
||||||
|
|
||||||
|
// This is probably not random enough?
|
||||||
|
r = random_int();
|
||||||
|
r <<= 32;
|
||||||
|
r |= random_int();
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ipp_eq(IP_Port a, IP_Port b)
|
||||||
|
{
|
||||||
|
return (a.ip.i == b.ip.i) && (a.port == b.port);
|
||||||
|
}
|
10
core/util.h
Normal file
10
core/util.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* util.h -- Utilities.
|
||||||
|
*
|
||||||
|
* This file is donated to the Tox Project.
|
||||||
|
* Copyright 2013 plutooo
|
||||||
|
*/
|
||||||
|
|
||||||
|
uint64_t now();
|
||||||
|
uint64_t random_64b();
|
||||||
|
bool ipp_eq(IP_Port a, IP_Port b);
|
@ -6,7 +6,7 @@ Linux
|
|||||||
|
|
||||||
First, install the build dependencies ::
|
First, install the build dependencies ::
|
||||||
|
|
||||||
bash apt-get install build-essential libtool autotools-dev automake libconfig-dev ncurses-dev cmake checkinstall
|
sudo apt-get install build-essential libtool autotools-dev automake libconfig-dev ncurses-dev cmake checkinstall
|
||||||
|
|
||||||
.. note :: ``libconfig-dev`` should be >= 1.4.
|
.. note :: ``libconfig-dev`` should be >= 1.4.
|
||||||
|
|
||||||
|
@ -228,7 +228,7 @@ void line_eval(char *line)
|
|||||||
status[i-3] = line[i];
|
status[i-3] = line[i];
|
||||||
}
|
}
|
||||||
status[i-3] = 0;
|
status[i-3] = 0;
|
||||||
m_set_userstatus(status, strlen((char*)status) + 1);
|
m_set_userstatus(USERSTATUS_KIND_ONLINE, status, strlen((char*)status) + 1);
|
||||||
char numstring[100];
|
char numstring[100];
|
||||||
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
||||||
new_lines(numstring);
|
new_lines(numstring);
|
||||||
@ -364,7 +364,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
|
void print_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
|
||||||
{
|
{
|
||||||
char name[MAX_NAME_LENGTH];
|
char name[MAX_NAME_LENGTH];
|
||||||
if(getname(friendnumber, (uint8_t*)name) != -1) {
|
if(getname(friendnumber, (uint8_t*)name) != -1) {
|
||||||
|
@ -86,7 +86,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
|||||||
printf(msg);
|
printf(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
|
void print_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
|
||||||
{
|
{
|
||||||
char name[MAX_NAME_LENGTH];
|
char name[MAX_NAME_LENGTH];
|
||||||
getname(friendnumber, (uint8_t*)name);
|
getname(friendnumber, (uint8_t*)name);
|
||||||
|
@ -58,6 +58,8 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
|
|||||||
wprintw(ctx->history, "%s\n", msg);
|
wprintw(ctx->history, "%s\n", msg);
|
||||||
|
|
||||||
self->blink = true;
|
self->blink = true;
|
||||||
|
beep();
|
||||||
|
flash();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
|
static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
|
||||||
@ -161,7 +163,7 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
msg++;
|
msg++;
|
||||||
m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
|
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
|
||||||
wprintw(ctx->history, "Status set to: %s\n", msg);
|
wprintw(ctx->history, "Status set to: %s\n", msg);
|
||||||
}
|
}
|
||||||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||||
|
@ -116,7 +116,7 @@ static void friendlist_onDraw(ToxWindow* self) {
|
|||||||
curs_set(0);
|
curs_set(0);
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
wclear(self->window);
|
werase(self->window);
|
||||||
|
|
||||||
if(num_friends == 0) {
|
if(num_friends == 0) {
|
||||||
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
||||||
|
@ -69,10 +69,10 @@ void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
|
void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t* string, uint16_t length) {
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
|
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
|
||||||
|
|
||||||
for(i=0; i<w_num; i++) {
|
for(i=0; i<w_num; i++) {
|
||||||
if(windows[i].onStatusChange != NULL)
|
if(windows[i].onStatusChange != NULL)
|
||||||
|
@ -174,7 +174,7 @@ static void execute(ToxWindow* self, char* cmd) {
|
|||||||
}
|
}
|
||||||
msg++;
|
msg++;
|
||||||
|
|
||||||
m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
|
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
|
||||||
wprintw(self->window, "Status set to: %s\n", msg);
|
wprintw(self->window, "Status set to: %s\n", msg);
|
||||||
}
|
}
|
||||||
else if(!strncmp(cmd, "nick ", strlen("nick "))) {
|
else if(!strncmp(cmd, "nick ", strlen("nick "))) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user