summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-08-13 20:29:02 -0400
committerirungentoo <irungentoo@gmail.com>2014-08-13 20:29:02 -0400
commit450ad2dd34072acf1a44192576c89b1373edf1f9 (patch)
tree61bb75a999ea71870c9f333e003903e185f0303f
parent6311bf8747914a25dfd323c75578fca3e264619f (diff)
Tox now closer to working on TCP only networks.
Use TCP paths when UDP is not connected. Made some code depend less on the UDP only DHT.
-rw-r--r--toxcore/onion_client.c73
1 files changed, 54 insertions, 19 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 3036df8c..0a519154 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -64,13 +64,30 @@ static int add_path_node(Onion_Client *onion_c, Node_format *node)
64 */ 64 */
65static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num) 65static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num)
66{ 66{
67 if (onion_c->path_nodes_index < 3) 67 unsigned int i;
68 return random_nodes_path(onion_c->dht, nodes, max_num); 68
69 if (!max_num)
70 return 0;
69 71
70 unsigned int i, num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; 72 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
71 73
72 for (i = 0; i < max_num; ++i) { 74 //if (DHT_non_lan_connected(onion_c->dht)) {
73 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 75 if (DHT_isconnected(onion_c->dht)) {
76 if (num_nodes < 3)
77 return random_nodes_path(onion_c->dht, nodes, max_num);
78
79 for (i = 0; i < max_num; ++i) {
80 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
81 }
82 } else {
83 if (num_nodes == 0)
84 return 0;
85
86 nodes[0].ip_port.ip.family = TCP_FAMILY;
87
88 for (i = 1; i < max_num; ++i) {
89 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
90 }
74 } 91 }
75 92
76 return max_num; 93 return max_num;
@@ -93,7 +110,7 @@ static int is_path_used(const Onion_Client_Paths *onion_paths, const Node_format
93 continue; 110 continue;
94 } 111 }
95 112
96 if (ipport_equal(&onion_paths->paths[i].ip_port1, &nodes[0].ip_port)) { 113 if (ipport_equal(&onion_paths->paths[i].ip_port1, &nodes[2].ip_port)) {
97 return i; 114 return i;
98 } 115 }
99 } 116 }
@@ -1050,9 +1067,25 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on
1050 return 0; 1067 return 0;
1051} 1068}
1052 1069
1070static void populate_path_nodes(Onion_Client *onion_c)
1071{
1072 if (onion_c->path_nodes_index < MAX_PATH_NODES) {
1073 Node_format nodes_list[MAX_SENT_NODES];
1074 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
1075 uint32_t random_num = rand();
1076 memcpy(client_id, &random_num, sizeof(random_num));
1053 1077
1054#define ANNOUNCE_FRIEND (ONION_NODE_PING_INTERVAL * 3) 1078 uint32_t num_nodes = get_close_nodes(onion_c->dht, client_id, nodes_list, (rand() % 2) ? AF_INET : AF_INET6, 1, 0);
1055#define FRIEND_ONION_NODE_TIMEOUT (ONION_NODE_TIMEOUT * 3) 1079 unsigned int i;
1080
1081 for (i = 0; i < num_nodes; ++i) {
1082 add_path_node(onion_c, nodes_list + i);
1083 }
1084 }
1085}
1086
1087#define ANNOUNCE_FRIEND (ONION_NODE_PING_INTERVAL * 2)
1088#define FRIEND_ONION_NODE_TIMEOUT (ONION_NODE_TIMEOUT * 2)
1056 1089
1057static void do_friend(Onion_Client *onion_c, uint16_t friendnum) 1090static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1058{ 1091{
@@ -1086,12 +1119,13 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1086 1119
1087 if (count != MAX_ONION_CLIENTS) { 1120 if (count != MAX_ONION_CLIENTS) {
1088 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) { 1121 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) {
1089 Node_format nodes_list[MAX_SENT_NODES]; 1122 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
1090 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[friendnum].real_client_id, nodes_list,
1091 (rand() % 2) ? AF_INET : AF_INET6, 1, 0);
1092 1123
1093 for (i = 0; i < num_nodes; ++i) 1124 if (num_nodes != 0) {
1094 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0, ~0); 1125 unsigned int num = rand() % num_nodes;
1126 client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port,
1127 onion_c->path_nodes[num].client_id, 0, ~0);
1128 }
1095 } 1129 }
1096 } 1130 }
1097 1131
@@ -1132,7 +1166,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
1132 onion_c->Onion_Data_Handlers[byte].object = object; 1166 onion_c->Onion_Data_Handlers[byte].object = object;
1133} 1167}
1134 1168
1135#define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10 1169#define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 5
1136#define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL 1170#define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL
1137 1171
1138static void do_announce(Onion_Client *onion_c) 1172static void do_announce(Onion_Client *onion_c)
@@ -1168,12 +1202,12 @@ static void do_announce(Onion_Client *onion_c)
1168 1202
1169 if (count != MAX_ONION_CLIENTS) { 1203 if (count != MAX_ONION_CLIENTS) {
1170 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) { 1204 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) {
1171 Node_format nodes_list[MAX_SENT_NODES];
1172 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->c->self_public_key, nodes_list,
1173 (rand() % 2) ? AF_INET : AF_INET6, 1, 0);
1174 1205
1175 for (i = 0; i < num_nodes; ++i) { 1206 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
1176 client_send_announce_request(onion_c, 0, nodes_list[i].ip_port, nodes_list[i].client_id, 0, ~0); 1207
1208 if (num_nodes != 0) {
1209 unsigned int num = rand() % num_nodes;
1210 client_send_announce_request(onion_c, 0, onion_c->path_nodes[num].ip_port, onion_c->path_nodes[num].client_id, 0, ~0);
1177 } 1211 }
1178 } 1212 }
1179 } 1213 }
@@ -1186,6 +1220,7 @@ void do_onion_client(Onion_Client *onion_c)
1186 if (onion_c->last_run == unix_time()) 1220 if (onion_c->last_run == unix_time())
1187 return; 1221 return;
1188 1222
1223 populate_path_nodes(onion_c);
1189 do_announce(onion_c); 1224 do_announce(onion_c);
1190 1225
1191 for (i = 0; i < onion_c->num_friends; ++i) { 1226 for (i = 0; i < onion_c->num_friends; ++i) {