summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Rose <michael_rose@gmx.de>2013-08-19 14:40:15 +0200
committerMichael Rose <michael_rose@gmx.de>2013-08-19 14:40:15 +0200
commit0aabb5bc49eb86c4168bbf4f369f72fa97d50c53 (patch)
tree7f68fd93e728f7de5f350ad1bc9e4f6a467cd08a
parentd6ae8dce983925a3f1462d111b5243fff3be31ec (diff)
parent4b480381ee7e1b5bbf3c253c0c4bf045e43f0a0e (diff)
Merge with upstream
-rw-r--r--core/DHT.c6
-rw-r--r--core/Lossless_UDP.c7
-rw-r--r--core/Messenger.c33
-rw-r--r--core/Messenger.h9
-rw-r--r--other/CMakeLists.txt12
-rw-r--r--testing/toxic/configdir.c14
-rw-r--r--testing/toxic/configdir.h4
-rw-r--r--testing/toxic/main.c31
8 files changed, 80 insertions, 36 deletions
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)
704 CLIENT_ID_SIZE ); 704 CLIENT_ID_SIZE );
705 } 705 }
706 706
707 if (num_friends == 0) {
708 free(friends_list);
709 friends_list = NULL;
710 return 0;
711 }
712
707 temp = realloc(friends_list, sizeof(Friend) * (num_friends)); 713 temp = realloc(friends_list, sizeof(Friend) * (num_friends));
708 714
709 if (temp == NULL) 715 if (temp == NULL)
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index fa5f9144..506a06eb 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -319,6 +319,13 @@ static void free_connections(void)
319 if (connections_length == i) 319 if (connections_length == i)
320 return; 320 return;
321 321
322 if (i == 0) {
323 free(connections);
324 connections = NULL;
325 connections_length = i;
326 return;
327 }
328
322 Connection *temp; 329 Connection *temp;
323 temp = realloc(connections, sizeof(Connection) * i); 330 temp = realloc(connections, sizeof(Connection) * i);
324 331
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)
482 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); 482 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
483} 483}
484 484
485static int send_ping(Messenger *m, int friendnumber)
486{
487 m->friendlist[friendnumber].ping_lastsent = unix_time();
488 return write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
489}
490
485static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 491static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
486{ 492{
487 if (friendnumber >= m->numfriends || friendnumber < 0) 493 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -596,7 +602,10 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
596 602
597 uint8_t packet[length + 1]; 603 uint8_t packet[length + 1];
598 packet[0] = packet_id; 604 packet[0] = packet_id;
599 memcpy(packet + 1, data, length); 605
606 if (length != 0)
607 memcpy(packet + 1, data, length);
608
600 return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 609 return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
601} 610}
602 611
@@ -659,6 +668,7 @@ void doFriends(Messenger *m)
659 uint32_t i; 668 uint32_t i;
660 int len; 669 int len;
661 uint8_t temp[MAX_DATA_SIZE]; 670 uint8_t temp[MAX_DATA_SIZE];
671 uint64_t temp_time = unix_time();
662 672
663 for (i = 0; i < m->numfriends; ++i) { 673 for (i = 0; i < m->numfriends; ++i) {
664 if (m->friendlist[i].status == FRIEND_ADDED) { 674 if (m->friendlist[i].status == FRIEND_ADDED) {
@@ -667,7 +677,7 @@ void doFriends(Messenger *m)
667 677
668 if (fr >= 0) { 678 if (fr >= 0) {
669 set_friend_status(m, i, FRIEND_REQUESTED); 679 set_friend_status(m, i, FRIEND_REQUESTED);
670 m->friendlist[i].friendrequest_lastsent = unix_time(); 680 m->friendlist[i].friendrequest_lastsent = temp_time;
671 } 681 }
672 } 682 }
673 683
@@ -676,7 +686,7 @@ void doFriends(Messenger *m)
676 if (m->friendlist[i].status == FRIEND_REQUESTED) { 686 if (m->friendlist[i].status == FRIEND_REQUESTED) {
677 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed 687 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed
678 unsuccessful so we set the status back to FRIEND_ADDED and try again.*/ 688 unsuccessful so we set the status back to FRIEND_ADDED and try again.*/
679 if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < unix_time()) { 689 if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) {
680 set_friend_status(m, i, FRIEND_ADDED); 690 set_friend_status(m, i, FRIEND_ADDED);
681 /* Double the default timeout everytime if friendrequest is assumed to have been 691 /* Double the default timeout everytime if friendrequest is assumed to have been
682 sent unsuccessfully. */ 692 sent unsuccessfully. */
@@ -698,6 +708,7 @@ void doFriends(Messenger *m)
698 m->friendlist[i].name_sent = 0; 708 m->friendlist[i].name_sent = 0;
699 m->friendlist[i].userstatus_sent = 0; 709 m->friendlist[i].userstatus_sent = 0;
700 m->friendlist[i].statusmessage_sent = 0; 710 m->friendlist[i].statusmessage_sent = 0;
711 m->friendlist[i].ping_lastrecv = temp_time;
701 break; 712 break;
702 713
703 case 4: 714 case 4:
@@ -726,6 +737,10 @@ void doFriends(Messenger *m)
726 m->friendlist[i].userstatus_sent = 1; 737 m->friendlist[i].userstatus_sent = 1;
727 } 738 }
728 739
740 if (m->friendlist[i].ping_lastsent + FRIEND_PING_INTERVAL < temp_time) {
741 send_ping(m, i);
742 }
743
729 len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp); 744 len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp);
730 uint8_t packet_id = temp[0]; 745 uint8_t packet_id = temp[0];
731 uint8_t *data = temp + 1; 746 uint8_t *data = temp + 1;
@@ -733,6 +748,11 @@ void doFriends(Messenger *m)
733 748
734 if (len > 0) { 749 if (len > 0) {
735 switch (packet_id) { 750 switch (packet_id) {
751 case PACKET_ID_PING: {
752 m->friendlist[i].ping_lastrecv = temp_time;
753 break;
754 }
755
736 case PACKET_ID_NICKNAME: { 756 case PACKET_ID_NICKNAME: {
737 if (data_length >= MAX_NAME_LENGTH || data_length == 0) 757 if (data_length >= MAX_NAME_LENGTH || data_length == 0)
738 break; 758 break;
@@ -821,6 +841,13 @@ void doFriends(Messenger *m)
821 841
822 break; 842 break;
823 } 843 }
844
845 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
846 /* if we stopped recieving ping packets kill it */
847 crypto_kill(m->friendlist[i].crypt_connection_id);
848 m->friendlist[i].crypt_connection_id = -1;
849 set_friend_status(m, i, FRIEND_CONFIRMED);
850 }
824 } 851 }
825 } 852 }
826} 853}
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" {
40 40
41#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) 41#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t))
42 42
43#define PACKET_ID_PING 0
43#define PACKET_ID_NICKNAME 48 44#define PACKET_ID_NICKNAME 48
44#define PACKET_ID_STATUSMESSAGE 49 45#define PACKET_ID_STATUSMESSAGE 49
45#define PACKET_ID_USERSTATUS 50 46#define PACKET_ID_USERSTATUS 50
@@ -71,6 +72,12 @@ extern "C" {
71/* Default start timeout in seconds between friend requests */ 72/* Default start timeout in seconds between friend requests */
72#define FRIENDREQUEST_TIMEOUT 5; 73#define FRIENDREQUEST_TIMEOUT 5;
73 74
75/* interval between the sending of ping packets.*/
76#define FRIEND_PING_INTERVAL 5
77
78/* If no packets are recieved from friend in this time interval, kill the connection.*/
79#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2)
80
74/* USERSTATUS 81/* USERSTATUS
75 * Represents userstatuses someone can have. */ 82 * Represents userstatuses someone can have. */
76 83
@@ -100,6 +107,8 @@ typedef struct {
100 uint32_t message_id; /* a semi-unique id used in read receipts */ 107 uint32_t message_id; /* a semi-unique id used in read receipts */
101 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ 108 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
102 uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/ 109 uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/
110 uint64_t ping_lastrecv;
111 uint64_t ping_lastsent;
103} Friend; 112} Friend;
104 113
105typedef struct Messenger { 114typedef struct Messenger {
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)
6include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake) 6include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake)
7 7
8if(WIN32) 8if(WIN32)
9 file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.tox") 9 file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox")
10 file(INSTALL DHTservers DESTINATION "C:/Users/${USER_NAME}/AppData/Roaming/.tox") 10 file(INSTALL DHTservers DESTINATION "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox")
11else() 11else()
12 set(HOME "$ENV{HOME}") 12 set(HOME "$ENV{HOME}")
13 if(APPLE) 13 if(APPLE)
14 file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.tox) 14 file(MAKE_DIRECTORY ${HOME}/Library/Application\ Support/.config/tox)
15 file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.tox) 15 file(INSTALL DHTservers DESTINATION ${HOME}/Library/Application\ Support/.config/tox)
16 else() 16 else()
17 file(MAKE_DIRECTORY ${HOME}/.tox) 17 file(MAKE_DIRECTORY ${HOME}/.config/tox)
18 file(INSTALL DHTservers DESTINATION ${HOME}/.tox) 18 file(INSTALL DHTservers DESTINATION ${HOME}/.config/tox)
19 endif() 19 endif()
20endif() 20endif()
21 21
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)
98 98
99 snprintf(user_config_dir, len, "%s/Library/Application Support", home); 99 snprintf(user_config_dir, len, "%s/Library/Application Support", home);
100# else /* __APPLE__ */ 100# else /* __APPLE__ */
101 len = strlen(home) + strlen("/.config") + 1;
102 user_config_dir = malloc(len);
103 101
104 if (user_config_dir == NULL) { 102 if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) {
105 return NULL; 103 len = strlen(home) + strlen("/.config") + 1;
104 user_config_dir = malloc(len);
105
106 if (user_config_dir == NULL) {
107 return NULL;
108 }
109
110 snprintf(user_config_dir, len, "%s/.config", home);
106 } 111 }
107 112
108 snprintf(user_config_dir, len, "%s/.config", home);
109# endif /* __APPLE__ */ 113# endif /* __APPLE__ */
110 114
111 return user_config_dir; 115 return user_config_dir;
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 @@
19 */ 19 */
20 20
21#ifdef _win32 21#ifdef _win32
22#define CONFIGDIR "\\toxic\\" 22#define CONFIGDIR "\\tox\\"
23#else 23#else
24#define CONFIGDIR "/toxic/" 24#define CONFIGDIR "/tox/"
25#endif 25#endif
26 26
27#ifndef S_ISDIR 27#ifndef S_ISDIR
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index c4c84eaf..9abe8de4 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -27,7 +27,7 @@
27 27
28/* Export for use in Callbacks */ 28/* Export for use in Callbacks */
29char *DATA_FILE = NULL; 29char *DATA_FILE = NULL;
30char dir[256]; 30char *SRVLIST_FILE = NULL;
31 31
32void on_window_resize(int sig) 32void on_window_resize(int sig)
33{ 33{
@@ -36,17 +36,6 @@ void on_window_resize(int sig)
36 clear(); 36 clear();
37} 37}
38 38
39void setdir()
40{
41#ifdef WIN32
42 strcpy(dir, "%appdata%/.tox/");
43#elif defined(__APPLE__)
44 strcpy(dir, "~/Library/Application Support/.tox/");
45#elif defined(linux)
46 strcpy(dir, "~/.tox/");
47#endif
48}
49
50static void init_term() 39static void init_term()
51{ 40{
52 /* Setup terminal */ 41 /* Setup terminal */
@@ -103,16 +92,12 @@ static Messenger *init_tox()
103/* Connects to a random DHT server listed in the DHTservers file */ 92/* Connects to a random DHT server listed in the DHTservers file */
104int init_connection(void) 93int init_connection(void)
105{ 94{
95 FILE *fp = NULL;
96
106 if (DHT_isconnected()) 97 if (DHT_isconnected())
107 return 0; 98 return 0;
108 99
109#if WIN32 100 fp = fopen(SRVLIST_FILE, "r");
110 FILE *fp = fopen("%appdata%/.tox/DHTservers", "r");
111#elif defined(__APPLE__)
112 FILE *fp = fopen("~/Library/Application Support/.tox/DHTservers", "r");
113#else
114 FILE *fp = fopen("~/.tox/DHTservers", "r");
115#endif
116 101
117 if (!fp) 102 if (!fp)
118 return 1; 103 return 1;
@@ -279,7 +264,6 @@ static void load_data(Messenger *m, char *path)
279 264
280int main(int argc, char *argv[]) 265int main(int argc, char *argv[])
281{ 266{
282 setdir();
283 char *user_config_dir = get_user_config_dir(); 267 char *user_config_dir = get_user_config_dir();
284 int config_err = 0; 268 int config_err = 0;
285 269
@@ -307,11 +291,17 @@ int main(int argc, char *argv[])
307 291
308 if (config_err) { 292 if (config_err) {
309 DATA_FILE = strdup("data"); 293 DATA_FILE = strdup("data");
294 SRVLIST_FILE = strdup("../../other/DHTservers");
310 } else { 295 } else {
311 DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); 296 DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1);
312 strcpy(DATA_FILE, user_config_dir); 297 strcpy(DATA_FILE, user_config_dir);
313 strcat(DATA_FILE, CONFIGDIR); 298 strcat(DATA_FILE, CONFIGDIR);
314 strcat(DATA_FILE, "data"); 299 strcat(DATA_FILE, "data");
300
301 SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1);
302 strcpy(SRVLIST_FILE, user_config_dir);
303 strcat(SRVLIST_FILE, CONFIGDIR);
304 strcat(SRVLIST_FILE, "DHTservers");
315 } 305 }
316 } 306 }
317 307
@@ -348,5 +338,6 @@ int main(int argc, char *argv[])
348 338
349 cleanupMessenger(m); 339 cleanupMessenger(m);
350 free(DATA_FILE); 340 free(DATA_FILE);
341 free(SRVLIST_FILE);
351 return 0; 342 return 0;
352} 343}