diff options
author | irungentoo <irungentoo@gmail.com> | 2015-04-21 15:30:41 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-04-21 15:30:41 -0400 |
commit | e1a98987ffeea04dd075be760af7621824fdc30a (patch) | |
tree | c6482a9e3e8404c41ad5ab2a4e166d4e21419a45 /toxcore/TCP_connection.c | |
parent | 0a0ed452026129fc9055c35ab75cfadab6f0dc06 (diff) |
Only allocate some TCP connections for onion use when needed.
Added a function to enable and disable TCP onion connections.
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r-- | toxcore/TCP_connection.c | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index d2aa3d96..08cb728c 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c | |||
@@ -997,7 +997,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe | |||
997 | tcp_con->connected_time = 0; | 997 | tcp_con->connected_time = 0; |
998 | } | 998 | } |
999 | 999 | ||
1000 | if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { | 1000 | if (tcp_c->onion_status && tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { |
1001 | tcp_con->onion = 1; | 1001 | tcp_con->onion = 1; |
1002 | ++tcp_c->onion_num_conns; | 1002 | ++tcp_c->onion_num_conns; |
1003 | } | 1003 | } |
@@ -1156,6 +1156,73 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_ | |||
1156 | return copied; | 1156 | return copied; |
1157 | } | 1157 | } |
1158 | 1158 | ||
1159 | /* Set if we want TCP_connection to allocate some connection for onion use. | ||
1160 | * | ||
1161 | * If status is 1, allocate some connections. if status is 0, don't. | ||
1162 | * | ||
1163 | * return 0 on success. | ||
1164 | * return -1 on failure. | ||
1165 | */ | ||
1166 | int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status) | ||
1167 | { | ||
1168 | if (tcp_c->onion_status == status) | ||
1169 | return -1; | ||
1170 | |||
1171 | if (status) { | ||
1172 | unsigned int i; | ||
1173 | |||
1174 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | ||
1175 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); | ||
1176 | |||
1177 | if (tcp_con) { | ||
1178 | if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion) { | ||
1179 | ++tcp_c->onion_num_conns; | ||
1180 | tcp_con->onion = 1; | ||
1181 | } | ||
1182 | } | ||
1183 | |||
1184 | if (tcp_c->onion_num_conns >= NUM_ONION_TCP_CONNECTIONS) | ||
1185 | break; | ||
1186 | } | ||
1187 | |||
1188 | if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { | ||
1189 | unsigned int wakeup = NUM_ONION_TCP_CONNECTIONS - tcp_c->onion_num_conns; | ||
1190 | |||
1191 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | ||
1192 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); | ||
1193 | |||
1194 | if (tcp_con) { | ||
1195 | if (tcp_con->status == TCP_CONN_SLEEPING) { | ||
1196 | tcp_con->unsleep = 1; | ||
1197 | } | ||
1198 | } | ||
1199 | |||
1200 | if (!wakeup) | ||
1201 | break; | ||
1202 | } | ||
1203 | } | ||
1204 | |||
1205 | tcp_c->onion_status = 1; | ||
1206 | } else { | ||
1207 | unsigned int i; | ||
1208 | |||
1209 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | ||
1210 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); | ||
1211 | |||
1212 | if (tcp_con) { | ||
1213 | if (tcp_con->onion) { | ||
1214 | --tcp_c->onion_num_conns; | ||
1215 | tcp_con->onion = 0; | ||
1216 | } | ||
1217 | } | ||
1218 | } | ||
1219 | |||
1220 | tcp_c->onion_status = 0; | ||
1221 | } | ||
1222 | |||
1223 | return 0; | ||
1224 | } | ||
1225 | |||
1159 | TCP_Connections *new_tcp_connections(DHT *dht, TCP_Proxy_Info *proxy_info) | 1226 | TCP_Connections *new_tcp_connections(DHT *dht, TCP_Proxy_Info *proxy_info) |
1160 | { | 1227 | { |
1161 | if (dht == NULL) | 1228 | if (dht == NULL) |