From adac62560df40f4a68c6c517d28356aa38de48ba Mon Sep 17 00:00:00 2001 From: loadletter Date: Sun, 18 Aug 2013 23:16:39 +0200 Subject: Use configdir.c instead of hardcoded paths for the list of DHT servers. --- testing/toxic/configdir.h | 4 ++-- testing/toxic/main.c | 35 ++++++++++++----------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h index 17d95107..e886e53a 100644 --- a/testing/toxic/configdir.h +++ b/testing/toxic/configdir.h @@ -19,9 +19,9 @@ */ #ifdef _win32 -#define CONFIGDIR "\\toxic\\" +#define CONFIGDIR "\\tox\\" #else -#define CONFIGDIR "/toxic/" +#define CONFIGDIR "/tox/" #endif #ifndef S_ISDIR diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 72d8633b..3ed2fa5e 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -27,7 +27,7 @@ /* Export for use in Callbacks */ char *DATA_FILE = NULL; -char dir[256]; +char *SRVLIST_FILE = NULL; void on_window_resize(int sig) { @@ -36,17 +36,6 @@ void on_window_resize(int sig) clear(); } -void setdir() -{ -#ifdef WIN32 - strcpy(dir, "%appdata%/.tox/"); -#elif defined(MAC_OSX) - strcpy(dir, "~/Library/Application Support/.tox/"); -#elif defined(linux) - strcpy(dir, "~/.tox/"); -#endif -} - static void init_term() { /* Setup terminal */ @@ -103,16 +92,12 @@ static Messenger *init_tox() /* Connects to a random DHT server listed in the DHTservers file */ int init_connection(void) { + FILE *fp = NULL; + if (DHT_isconnected()) return 0; -#if WIN32 - FILE *fp = fopen("%appdata%/.tox/DHTservers", "r"); -#elif MAC_OSX - FILE *fp = fopen("~/Library/Application Support/.tox/DHTservers", "r"); -#else - FILE *fp = fopen("~/.tox/DHTservers", "r"); -#endif + fp = fopen(SRVLIST_FILE, "r"); if (!fp) return 1; @@ -279,7 +264,6 @@ static void load_data(Messenger *m, char *path) int main(int argc, char *argv[]) { - setdir(); char *user_config_dir = get_user_config_dir(); int config_err = 0; @@ -306,16 +290,20 @@ int main(int argc, char *argv[]) config_err = create_user_config_dir(user_config_dir); if (config_err) { - strcat(DATA_FILE, dir); DATA_FILE = strdup("data"); + SRVLIST_FILE = strdup("../../other/DHTservers"); } else { DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); strcpy(DATA_FILE, user_config_dir); strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, dir); - DATA_FILE = strdup("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"); } } @@ -352,5 +340,6 @@ int main(int argc, char *argv[]) cleanupMessenger(m); free(DATA_FILE); + free(SRVLIST_FILE); return 0; } -- cgit v1.2.3 From e8b8dfebb3fce4a7e2dc6cb362351dd0a21c2af4 Mon Sep 17 00:00:00 2001 From: loadletter Date: Sun, 18 Aug 2013 23:20:34 +0200 Subject: Modified cmakefile to move the serverlist to the proper directory. Tested on GNU/Linux. --- other/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/other/CMakeLists.txt b/other/CMakeLists.txt index eaf677ed..871a9e92 100644 --- a/other/CMakeLists.txt +++ b/other/CMakeLists.txt @@ -6,16 +6,16 @@ SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake) if(WIN32) - file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.tox") - file(INSTALL DHTservers DESTINATION "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/.config/tox") else() set(HOME "$ENV{HOME}") if(APPLE) - file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.tox) - file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.tox) + file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.config/tox) + file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.config/tox) else() - file(MAKE_DIRECTORY ${HOME}/.tox) - file(INSTALL DHTservers DESTINATION ${HOME}/.tox) + file(MAKE_DIRECTORY ${HOME}/.config/tox) + file(INSTALL DHTservers DESTINATION ${HOME}/.config/tox) endif() endif() -- cgit v1.2.3 From 29b61c574b91334668b9ebebf854ad65af568f2c Mon Sep 17 00:00:00 2001 From: Frederik Holden Date: Mon, 19 Aug 2013 01:21:36 +0200 Subject: Made configdir check XDG_CONFIG_HOME for config dir --- testing/toxic/configdir.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index 1a62e8ed..a43dd1de 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c @@ -98,14 +98,18 @@ char *get_user_config_dir(void) snprintf(user_config_dir, len, "%s/Library/Application Support", home); # else /* __APPLE__ */ - len = strlen(home) + strlen("/.config") + 1; - user_config_dir = malloc(len); - if (user_config_dir == NULL) { - return NULL; + if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) { + 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__ */ return user_config_dir; -- cgit v1.2.3 From 88986f793a02921e8a23e540a80275e91c87a82a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 19 Aug 2013 07:00:59 -0400 Subject: Prevented possible MITM attack. Friends must now send to themselves at least one encrypted ping packet every 5 seconds. If no encrypted ping packets are recieved from a friend within 10 seconds the connection is killed. --- core/Messenger.c | 33 ++++++++++++++++++++++++++++++--- core/Messenger.h | 9 +++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 66ae4f41..7fd6a569 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -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)); } +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) { 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]; 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); } @@ -659,6 +668,7 @@ void doFriends(Messenger *m) uint32_t i; int len; uint8_t temp[MAX_DATA_SIZE]; + uint64_t temp_time = unix_time(); for (i = 0; i < m->numfriends; ++i) { if (m->friendlist[i].status == FRIEND_ADDED) { @@ -667,7 +677,7 @@ void doFriends(Messenger *m) if (fr >= 0) { 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 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.*/ - 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); /* Double the default timeout everytime if friendrequest is assumed to have been sent unsuccessfully. */ @@ -698,6 +708,7 @@ void doFriends(Messenger *m) m->friendlist[i].name_sent = 0; m->friendlist[i].userstatus_sent = 0; m->friendlist[i].statusmessage_sent = 0; + m->friendlist[i].ping_lastrecv = temp_time; break; case 4: @@ -726,6 +737,10 @@ void doFriends(Messenger *m) 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); uint8_t packet_id = temp[0]; uint8_t *data = temp + 1; @@ -733,6 +748,11 @@ void doFriends(Messenger *m) if (len > 0) { switch (packet_id) { + case PACKET_ID_PING: { + m->friendlist[i].ping_lastrecv = temp_time; + break; + } + case PACKET_ID_NICKNAME: { if (data_length >= MAX_NAME_LENGTH || data_length == 0) break; @@ -821,6 +841,13 @@ void doFriends(Messenger *m) 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); + } } } } diff --git a/core/Messenger.h b/core/Messenger.h index 20ea33fb..9016be93 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -40,6 +40,7 @@ extern "C" { #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_STATUSMESSAGE 49 #define PACKET_ID_USERSTATUS 50 @@ -71,6 +72,12 @@ extern "C" { /* Default start timeout in seconds between friend requests */ #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 * Represents userstatuses someone can have. */ @@ -100,6 +107,8 @@ typedef struct { 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? */ uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/ + uint64_t ping_lastrecv; + uint64_t ping_lastsent; } Friend; typedef struct Messenger { -- cgit v1.2.3 From 17890b1f61da9b0e8ea94ccfe32a9d0f6ebf3111 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 19 Aug 2013 07:15:56 -0400 Subject: Realloc apparently doesn't always behave like free() if size is zero. --- core/DHT.c | 6 ++++++ core/Lossless_UDP.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/core/DHT.c b/core/DHT.c index 55f34994..924e3216 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -704,6 +704,12 @@ int DHT_delfriend(uint8_t *client_id) CLIENT_ID_SIZE ); } + if (num_friends == 0) { + free(friends_list); + friends_list = NULL; + return 0; + } + temp = realloc(friends_list, sizeof(Friend) * (num_friends)); if (temp == NULL) diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index fa5f9144..2210f7d3 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -319,6 +319,12 @@ static void free_connections(void) if (connections_length == i) return; + if (i == 0) { + free(connections); + connections = NULL; + return; + } + Connection *temp; temp = realloc(connections, sizeof(Connection) * i); -- cgit v1.2.3 From 4b480381ee7e1b5bbf3c253c0c4bf045e43f0a0e Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 19 Aug 2013 07:18:24 -0400 Subject: Fixed segfault. --- core/Lossless_UDP.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 2210f7d3..506a06eb 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -322,6 +322,7 @@ static void free_connections(void) if (i == 0) { free(connections); connections = NULL; + connections_length = i; return; } -- cgit v1.2.3