Merge with upstream

This commit is contained in:
Michael Rose 2013-08-19 14:40:15 +02:00
commit 0aabb5bc49
8 changed files with 80 additions and 36 deletions

View File

@ -704,6 +704,12 @@ int DHT_delfriend(uint8_t *client_id)
CLIENT_ID_SIZE ); CLIENT_ID_SIZE );
} }
if (num_friends == 0) {
free(friends_list);
friends_list = NULL;
return 0;
}
temp = realloc(friends_list, sizeof(Friend) * (num_friends)); temp = realloc(friends_list, sizeof(Friend) * (num_friends));
if (temp == NULL) if (temp == NULL)

View File

@ -319,6 +319,13 @@ static void free_connections(void)
if (connections_length == i) if (connections_length == i)
return; return;
if (i == 0) {
free(connections);
connections = NULL;
connections_length = i;
return;
}
Connection *temp; Connection *temp;
temp = realloc(connections, sizeof(Connection) * i); temp = realloc(connections, sizeof(Connection) * i);

View File

@ -482,6 +482,12 @@ static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
} }
static int send_ping(Messenger *m, int friendnumber)
{
m->friendlist[friendnumber].ping_lastsent = unix_time();
return write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
}
static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
{ {
if (friendnumber >= m->numfriends || friendnumber < 0) if (friendnumber >= m->numfriends || friendnumber < 0)
@ -596,7 +602,10 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
uint8_t packet[length + 1]; uint8_t packet[length + 1];
packet[0] = packet_id; packet[0] = packet_id;
memcpy(packet + 1, data, length);
if (length != 0)
memcpy(packet + 1, data, length);
return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
} }
@ -659,6 +668,7 @@ void doFriends(Messenger *m)
uint32_t i; uint32_t i;
int len; int len;
uint8_t temp[MAX_DATA_SIZE]; uint8_t temp[MAX_DATA_SIZE];
uint64_t temp_time = unix_time();
for (i = 0; i < m->numfriends; ++i) { for (i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status == FRIEND_ADDED) { if (m->friendlist[i].status == FRIEND_ADDED) {
@ -667,7 +677,7 @@ void doFriends(Messenger *m)
if (fr >= 0) { if (fr >= 0) {
set_friend_status(m, i, FRIEND_REQUESTED); set_friend_status(m, i, FRIEND_REQUESTED);
m->friendlist[i].friendrequest_lastsent = unix_time(); m->friendlist[i].friendrequest_lastsent = temp_time;
} }
} }
@ -676,7 +686,7 @@ void doFriends(Messenger *m)
if (m->friendlist[i].status == FRIEND_REQUESTED) { 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 /* 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.*/ unsuccessful so we set the status back to FRIEND_ADDED and try again.*/
if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < unix_time()) { if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) {
set_friend_status(m, i, FRIEND_ADDED); set_friend_status(m, i, FRIEND_ADDED);
/* Double the default timeout everytime if friendrequest is assumed to have been /* Double the default timeout everytime if friendrequest is assumed to have been
sent unsuccessfully. */ sent unsuccessfully. */
@ -698,6 +708,7 @@ void doFriends(Messenger *m)
m->friendlist[i].name_sent = 0; m->friendlist[i].name_sent = 0;
m->friendlist[i].userstatus_sent = 0; m->friendlist[i].userstatus_sent = 0;
m->friendlist[i].statusmessage_sent = 0; m->friendlist[i].statusmessage_sent = 0;
m->friendlist[i].ping_lastrecv = temp_time;
break; break;
case 4: case 4:
@ -726,6 +737,10 @@ void doFriends(Messenger *m)
m->friendlist[i].userstatus_sent = 1; m->friendlist[i].userstatus_sent = 1;
} }
if (m->friendlist[i].ping_lastsent + FRIEND_PING_INTERVAL < temp_time) {
send_ping(m, i);
}
len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp); len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp);
uint8_t packet_id = temp[0]; uint8_t packet_id = temp[0];
uint8_t *data = temp + 1; uint8_t *data = temp + 1;
@ -733,6 +748,11 @@ void doFriends(Messenger *m)
if (len > 0) { if (len > 0) {
switch (packet_id) { switch (packet_id) {
case PACKET_ID_PING: {
m->friendlist[i].ping_lastrecv = temp_time;
break;
}
case PACKET_ID_NICKNAME: { case PACKET_ID_NICKNAME: {
if (data_length >= MAX_NAME_LENGTH || data_length == 0) if (data_length >= MAX_NAME_LENGTH || data_length == 0)
break; break;
@ -821,6 +841,13 @@ void doFriends(Messenger *m)
break; break;
} }
if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
/* if we stopped recieving ping packets kill it */
crypto_kill(m->friendlist[i].crypt_connection_id);
m->friendlist[i].crypt_connection_id = -1;
set_friend_status(m, i, FRIEND_CONFIRMED);
}
} }
} }
} }

View File

@ -40,6 +40,7 @@ extern "C" {
#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t))
#define PACKET_ID_PING 0
#define PACKET_ID_NICKNAME 48 #define PACKET_ID_NICKNAME 48
#define PACKET_ID_STATUSMESSAGE 49 #define PACKET_ID_STATUSMESSAGE 49
#define PACKET_ID_USERSTATUS 50 #define PACKET_ID_USERSTATUS 50
@ -71,6 +72,12 @@ extern "C" {
/* Default start timeout in seconds between friend requests */ /* Default start timeout in seconds between friend requests */
#define FRIENDREQUEST_TIMEOUT 5; #define FRIENDREQUEST_TIMEOUT 5;
/* interval between the sending of ping packets.*/
#define FRIEND_PING_INTERVAL 5
/* If no packets are recieved from friend in this time interval, kill the connection.*/
#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2)
/* USERSTATUS /* USERSTATUS
* Represents userstatuses someone can have. */ * Represents userstatuses someone can have. */
@ -100,6 +107,8 @@ typedef struct {
uint32_t message_id; /* a semi-unique id used in read receipts */ uint32_t message_id; /* a semi-unique id used in read receipts */
uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/ uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/
uint64_t ping_lastrecv;
uint64_t ping_lastsent;
} Friend; } Friend;
typedef struct Messenger { typedef struct Messenger {

View File

@ -6,16 +6,16 @@ SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake)
if(WIN32) if(WIN32)
file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.tox") file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox")
file(INSTALL DHTservers DESTINATION "C:/Users/${USER_NAME}/AppData/Roaming/.tox") file(INSTALL DHTservers DESTINATION "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox")
else() else()
set(HOME "$ENV{HOME}") set(HOME "$ENV{HOME}")
if(APPLE) if(APPLE)
file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.tox) file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.config/tox)
file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.tox) file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.config/tox)
else() else()
file(MAKE_DIRECTORY ${HOME}/.tox) file(MAKE_DIRECTORY ${HOME}/.config/tox)
file(INSTALL DHTservers DESTINATION ${HOME}/.tox) file(INSTALL DHTservers DESTINATION ${HOME}/.config/tox)
endif() endif()
endif() endif()

View File

@ -98,14 +98,18 @@ char *get_user_config_dir(void)
snprintf(user_config_dir, len, "%s/Library/Application Support", home); snprintf(user_config_dir, len, "%s/Library/Application Support", home);
# else /* __APPLE__ */ # else /* __APPLE__ */
len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) { if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) {
return NULL; len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) {
return NULL;
}
snprintf(user_config_dir, len, "%s/.config", home);
} }
snprintf(user_config_dir, len, "%s/.config", home);
# endif /* __APPLE__ */ # endif /* __APPLE__ */
return user_config_dir; return user_config_dir;

View File

@ -19,9 +19,9 @@
*/ */
#ifdef _win32 #ifdef _win32
#define CONFIGDIR "\\toxic\\" #define CONFIGDIR "\\tox\\"
#else #else
#define CONFIGDIR "/toxic/" #define CONFIGDIR "/tox/"
#endif #endif
#ifndef S_ISDIR #ifndef S_ISDIR

View File

@ -27,7 +27,7 @@
/* Export for use in Callbacks */ /* Export for use in Callbacks */
char *DATA_FILE = NULL; char *DATA_FILE = NULL;
char dir[256]; char *SRVLIST_FILE = NULL;
void on_window_resize(int sig) void on_window_resize(int sig)
{ {
@ -36,17 +36,6 @@ void on_window_resize(int sig)
clear(); clear();
} }
void setdir()
{
#ifdef WIN32
strcpy(dir, "%appdata%/.tox/");
#elif defined(__APPLE__)
strcpy(dir, "~/Library/Application Support/.tox/");
#elif defined(linux)
strcpy(dir, "~/.tox/");
#endif
}
static void init_term() static void init_term()
{ {
/* Setup terminal */ /* Setup terminal */
@ -103,16 +92,12 @@ static Messenger *init_tox()
/* Connects to a random DHT server listed in the DHTservers file */ /* Connects to a random DHT server listed in the DHTservers file */
int init_connection(void) int init_connection(void)
{ {
FILE *fp = NULL;
if (DHT_isconnected()) if (DHT_isconnected())
return 0; return 0;
#if WIN32 fp = fopen(SRVLIST_FILE, "r");
FILE *fp = fopen("%appdata%/.tox/DHTservers", "r");
#elif defined(__APPLE__)
FILE *fp = fopen("~/Library/Application Support/.tox/DHTservers", "r");
#else
FILE *fp = fopen("~/.tox/DHTservers", "r");
#endif
if (!fp) if (!fp)
return 1; return 1;
@ -279,7 +264,6 @@ static void load_data(Messenger *m, char *path)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
setdir();
char *user_config_dir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
int config_err = 0; int config_err = 0;
@ -307,11 +291,17 @@ int main(int argc, char *argv[])
if (config_err) { if (config_err) {
DATA_FILE = strdup("data"); DATA_FILE = strdup("data");
SRVLIST_FILE = strdup("../../other/DHTservers");
} else { } else {
DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
strcpy(DATA_FILE, user_config_dir); strcpy(DATA_FILE, user_config_dir);
strcat(DATA_FILE, CONFIGDIR); strcat(DATA_FILE, CONFIGDIR);
strcat(DATA_FILE, "data"); strcat(DATA_FILE, "data");
SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
strcpy(SRVLIST_FILE, user_config_dir);
strcat(SRVLIST_FILE, CONFIGDIR);
strcat(SRVLIST_FILE, "DHTservers");
} }
} }
@ -348,5 +338,6 @@ int main(int argc, char *argv[])
cleanupMessenger(m); cleanupMessenger(m);
free(DATA_FILE); free(DATA_FILE);
free(SRVLIST_FILE);
return 0; return 0;
} }