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
|
||||
friend_requests.c
|
||||
LAN_discovery.c
|
||||
Messenger.c)
|
||||
Messenger.c
|
||||
util.c
|
||||
ping.c)
|
||||
|
||||
if(SHARED_TOXCORE)
|
||||
add_library(toxcore SHARED ${core_sources})
|
||||
|
|
63
core/DHT.c
63
core/DHT.c
|
@ -24,6 +24,7 @@
|
|||
/*----------------------------------------------------------------------------------*/
|
||||
|
||||
#include "DHT.h"
|
||||
#include "ping.h"
|
||||
|
||||
/* maximum number of clients stored per friend. */
|
||||
#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 Friend * friends_list;
|
||||
static uint16_t num_friends;
|
||||
static Pinged pings[LPING_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. */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
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))
|
||||
return 1;
|
||||
|
||||
uint64_t ping_id = add_pinging(ip_port);
|
||||
uint64_t ping_id = add_ping(ip_port);
|
||||
if(ping_id == 0)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
/* 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)
|
||||
{
|
||||
init_ping();
|
||||
|
||||
if(size < sizeof(close_clientlist))
|
||||
return -1;
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ typedef struct {
|
|||
uint8_t *userstatus;
|
||||
uint16_t userstatus_length;
|
||||
uint8_t userstatus_sent;
|
||||
USERSTATUS_KIND userstatus_kind;
|
||||
uint16_t info_size; /* length of the info */
|
||||
} Friend;
|
||||
|
||||
|
@ -45,6 +46,7 @@ static uint16_t self_name_length;
|
|||
|
||||
static uint8_t *self_userstatus;
|
||||
static uint16_t self_userstatus_len;
|
||||
static USERSTATUS_KIND self_userstatus_kind;
|
||||
|
||||
#define MAX_NUM_FRIENDS 256
|
||||
|
||||
|
@ -98,12 +100,12 @@ int getclient_id(int friend_id, uint8_t *client_id)
|
|||
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte)
|
||||
* return FAERR_OWNKEY if user's own key
|
||||
* return FAERR_ALREADYSENT if friend request already sent or already a friend
|
||||
* return FAERR_UNKNOWN for unknown error
|
||||
* return FAERR_UNKNOWN for unknown error
|
||||
*/
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
||||
{
|
||||
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
|
||||
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
||||
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
|
||||
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
||||
+ crypto_box_ZEROBYTES))
|
||||
return FAERR_TOOLONG;
|
||||
if (length < 1)
|
||||
|
@ -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);
|
||||
friendlist[i].userstatus = calloc(1, 1);
|
||||
friendlist[i].userstatus_length = 1;
|
||||
friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE;
|
||||
memcpy(friendlist[i].info, data, length);
|
||||
friendlist[i].info_size = length;
|
||||
|
||||
|
@ -205,7 +208,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
|
|||
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
|
||||
}
|
||||
|
||||
/* send a name packet to friendnumber
|
||||
/* send a name packet to friendnumber
|
||||
length is the length with the NULL terminator*/
|
||||
static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
|
||||
{
|
||||
|
@ -247,14 +250,14 @@ int setname(uint8_t * name, uint16_t length)
|
|||
}
|
||||
|
||||
/* get our nickname
|
||||
put it in name
|
||||
put it in name
|
||||
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
||||
return the length of the name */
|
||||
uint16_t getself_name(uint8_t *name)
|
||||
{
|
||||
memcpy(name, self_name, self_name_length);
|
||||
return self_name_length;
|
||||
}
|
||||
}
|
||||
|
||||
/* get name of friendnumber
|
||||
put it in name
|
||||
|
@ -269,10 +272,13 @@ int getname(int friendnumber, uint8_t * name)
|
|||
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)
|
||||
return -1;
|
||||
if (kind != USERSTATUS_KIND_RETAIN) {
|
||||
self_userstatus_kind = kind;
|
||||
}
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
free(self_userstatus);
|
||||
|
@ -285,6 +291,20 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
|
|||
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
|
||||
guaranteed to be at most MAX_USERSTATUS_LENGTH */
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
uint8_t *thepacket = malloc(length + 1);
|
||||
memcpy(thepacket + 1, status, length);
|
||||
uint8_t *thepacket = malloc(length + 2);
|
||||
memcpy(thepacket + 2, status, length);
|
||||
thepacket[0] = PACKET_ID_USERSTATUS;
|
||||
thepacket[1] = self_userstatus_kind;
|
||||
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
|
||||
free(thepacket);
|
||||
return written;
|
||||
|
@ -327,6 +369,11 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
|
|||
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 uint8_t friend_request_isset = 0; */
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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;
|
||||
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_isset = 1;
|
||||
|
@ -366,7 +413,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
|
|||
int initMessenger(void)
|
||||
{
|
||||
new_keys();
|
||||
m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
|
||||
m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online"));
|
||||
initNetCrypto();
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
|
@ -438,12 +485,17 @@ static void doFriends(void)
|
|||
break;
|
||||
}
|
||||
case PACKET_ID_USERSTATUS: {
|
||||
uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
|
||||
memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
if (friend_statuschange_isset)
|
||||
friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
|
||||
free(status);
|
||||
if (len > 2) {
|
||||
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)
|
||||
friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||
set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||
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;
|
||||
}
|
||||
case PACKET_ID_MESSAGE: {
|
||||
|
|
|
@ -60,6 +60,22 @@ extern "C" {
|
|||
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
||||
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
|
||||
* set the data that will be sent along with friend request
|
||||
|
@ -70,7 +86,7 @@ extern "C" {
|
|||
* return -2 if no message (message length must be >= 1 byte)
|
||||
* return -3 if user's own key
|
||||
* return -4 if friend request already sent or already a friend
|
||||
* return -5 for unknown error
|
||||
* return -5 for unknown error
|
||||
*/
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
|
||||
|
||||
|
@ -114,7 +130,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
|||
int setname(uint8_t *name, uint16_t length);
|
||||
|
||||
/* get our nickname
|
||||
put it in name
|
||||
put it in name
|
||||
return the length of the name*/
|
||||
uint16_t getself_name(uint8_t *name);
|
||||
|
||||
|
@ -128,7 +144,8 @@ int getname(int friendnumber, uint8_t *name);
|
|||
/* set our user status
|
||||
you are responsible for freeing status after
|
||||
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,
|
||||
including null
|
||||
|
@ -136,8 +153,17 @@ int m_set_userstatus(uint8_t *status, uint16_t length);
|
|||
int m_get_userstatus_size(int friendnumber);
|
||||
|
||||
/* 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_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.
|
||||
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));
|
||||
|
||||
/* 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 */
|
||||
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
|
||||
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)
|
||||
{
|
||||
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)
|
||||
return 1;
|
||||
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 ::
|
||||
|
||||
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.
|
||||
|
||||
|
|
|
@ -228,7 +228,7 @@ void line_eval(char *line)
|
|||
status[i-3] = line[i];
|
||||
}
|
||||
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];
|
||||
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
||||
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];
|
||||
if(getname(friendnumber, (uint8_t*)name) != -1) {
|
||||
|
@ -392,7 +392,7 @@ void load_key(char *path)
|
|||
}
|
||||
Messenger_load(data, size);
|
||||
|
||||
} else {
|
||||
} else {
|
||||
//else save new keys
|
||||
int size = Messenger_size();
|
||||
uint8_t data[size];
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nTox_win32.h"
|
||||
|
@ -86,7 +86,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
|||
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];
|
||||
getname(friendnumber, (uint8_t*)name);
|
||||
|
@ -95,7 +95,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
|
|||
printf(msg);
|
||||
}
|
||||
|
||||
void load_key()
|
||||
void load_key()
|
||||
{
|
||||
FILE *data_file = NULL;
|
||||
data_file = fopen("data","r");
|
||||
|
@ -130,7 +130,7 @@ void add_friend()
|
|||
int i;
|
||||
char temp_id[128];
|
||||
|
||||
for (i = 0; i < 128; i++)
|
||||
for (i = 0; i < 128; i++)
|
||||
temp_id[i] = line[i+3];
|
||||
|
||||
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
|
@ -141,7 +141,7 @@ void add_friend()
|
|||
printf(numstring);
|
||||
++maxnumfriends;
|
||||
}
|
||||
else if (num == -1)
|
||||
else if (num == -1)
|
||||
printf("\n[i] Message is too long.\n\n");
|
||||
|
||||
else if (num == -2)
|
||||
|
@ -180,7 +180,7 @@ void list_friends()
|
|||
char name[MAX_NAME_LENGTH];
|
||||
getname(i, (uint8_t*)name);
|
||||
|
||||
if (m_friendstatus(i) == 4)
|
||||
if (m_friendstatus(i) == 4)
|
||||
printf("[%d] %s\n", i, (uint8_t*)name);
|
||||
}
|
||||
|
||||
|
@ -213,7 +213,7 @@ void message_friend()
|
|||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
if (line[i+3] != ' ')
|
||||
if (line[i+3] != ' ')
|
||||
numstring[i] = line[i+3];
|
||||
|
||||
else {
|
||||
|
@ -243,7 +243,7 @@ void change_nickname()
|
|||
|
||||
for (i = 3; i < len; i++) {
|
||||
|
||||
if (line[i] == 0 || line[i] == '\n')
|
||||
if (line[i] == 0 || line[i] == '\n')
|
||||
break;
|
||||
|
||||
name[i-3] = line[i];
|
||||
|
@ -268,7 +268,7 @@ void change_status(int savetofile)
|
|||
size_t len = strlen(line);
|
||||
|
||||
for (i = 3; i < len; i++) {
|
||||
if (line[i] == 0 || line[i] == '\n')
|
||||
if (line[i] == 0 || line[i] == '\n')
|
||||
break;
|
||||
|
||||
status[i-3] = line[i];
|
||||
|
@ -350,7 +350,7 @@ void line_eval(char* line)
|
|||
accept_friend_request(line);
|
||||
}
|
||||
/* EXIT */
|
||||
else if (inpt_command == 'q') {
|
||||
else if (inpt_command == 'q') {
|
||||
strcpy(line, "---Offline");
|
||||
change_status(0);
|
||||
exit(EXIT_SUCCESS);
|
||||
|
@ -398,7 +398,7 @@ int main(int argc, char *argv[])
|
|||
nameloaded = 1;
|
||||
printf("%s\n", name);
|
||||
fclose(name_file);
|
||||
}
|
||||
}
|
||||
|
||||
FILE* status_file = NULL;
|
||||
status_file = fopen("statusfile.txt", "r");
|
||||
|
@ -424,7 +424,7 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
if(self_public_key[i] < (PUB_KEY_BYTES/2))
|
||||
strcpy(idstring1[i],"0");
|
||||
else
|
||||
else
|
||||
strcpy(idstring1[i], "");
|
||||
sprintf(idstring2[i], "%hhX",self_public_key[i]);
|
||||
}
|
||||
|
@ -442,7 +442,7 @@ int main(int argc, char *argv[])
|
|||
int resolved_address = resolve_addr(argv[1]);
|
||||
if (resolved_address != 0)
|
||||
bootstrap_ip_port.ip.i = resolved_address;
|
||||
else
|
||||
else
|
||||
exit(1);
|
||||
|
||||
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
||||
|
|
|
@ -32,7 +32,7 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
|
|||
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
|
||||
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
|
@ -58,6 +58,8 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
|
|||
wprintw(ctx->history, "%s\n", msg);
|
||||
|
||||
self->blink = true;
|
||||
beep();
|
||||
flash();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||
|
|
|
@ -116,7 +116,7 @@ static void friendlist_onDraw(ToxWindow* self) {
|
|||
curs_set(0);
|
||||
size_t i;
|
||||
|
||||
wclear(self->window);
|
||||
werase(self->window);
|
||||
|
||||
if(num_friends == 0) {
|
||||
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;
|
||||
|
||||
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++) {
|
||||
if(windows[i].onStatusChange != NULL)
|
||||
|
@ -201,7 +201,7 @@ static void load_data(char *path) {
|
|||
|
||||
Messenger_load(buf, len);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
len = Messenger_size();
|
||||
buf = malloc(len);
|
||||
|
||||
|
@ -289,7 +289,7 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
for(i = 0; i < argc; i++) {
|
||||
if (argv[i] == NULL){
|
||||
break;
|
||||
break;
|
||||
} else if(argv[i][0] == '-') {
|
||||
if(argv[i][1] == 'f') {
|
||||
if(argv[i + 1] != NULL)
|
||||
|
|
|
@ -91,7 +91,7 @@ static void execute(ToxWindow* self, char* cmd) {
|
|||
dht.ip.i = resolved_address;
|
||||
unsigned char *binary_string = hex_string_to_bin(key);
|
||||
DHT_bootstrap(dht, binary_string);
|
||||
free(binary_string);
|
||||
free(binary_string);
|
||||
}
|
||||
else if(!strncmp(cmd, "add ", strlen("add "))) {
|
||||
uint8_t id_bin[32];
|
||||
|
@ -137,7 +137,7 @@ static void execute(ToxWindow* self, char* cmd) {
|
|||
|
||||
num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
|
||||
switch (num) {
|
||||
case -1:
|
||||
case -1:
|
||||
wprintw(self->window, "Message is too long.\n");
|
||||
break;
|
||||
case -2:
|
||||
|
@ -151,7 +151,7 @@ static void execute(ToxWindow* self, char* cmd) {
|
|||
break;
|
||||
case -5:
|
||||
wprintw(self->window, "Undefined error when adding friend.\n");
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
wprintw(self->window, "Friend added as %d.\n", num);
|
||||
on_friendadded(num);
|
||||
|
@ -174,7 +174,7 @@ static void execute(ToxWindow* self, char* cmd) {
|
|||
}
|
||||
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);
|
||||
}
|
||||
else if(!strncmp(cmd, "nick ", strlen("nick "))) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user