summaryrefslogtreecommitdiff
path: root/toxcore/friend_connection.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-04-18 22:14:37 -0400
committerirungentoo <irungentoo@gmail.com>2015-04-18 22:14:37 -0400
commit1a2fa1b7e639e5e94612c774894baa50b3514265 (patch)
tree0868dafc7bc88ecb38fe401990123b0ec8078818 /toxcore/friend_connection.c
parentb4fc0809a7b42ba5d104793e909aecf85951f100 (diff)
Some changes to net crypto.
Should fix certain connection issues that sometimes happen. The dht public key of the peer must be known to create the connection. If the dht pk of the peer changes when a connection is active, it is killed to make way for the new one.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c60
1 files changed, 41 insertions, 19 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index d38ad73a..0f171100 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -160,7 +160,7 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
160 160
161 unsigned int i; 161 unsigned int i;
162 162
163 uint16_t index = friend_con->tcp_relay_counter; 163 uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS;
164 164
165 for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { 165 for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) {
166 if (friend_con->tcp_relays[i].ip_port.ip.family != 0 166 if (friend_con->tcp_relays[i].ip_port.ip.family != 0
@@ -169,8 +169,8 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
169 } 169 }
170 } 170 }
171 171
172 friend_con->tcp_relays[index % FRIEND_MAX_STORED_TCP_RELAYS].ip_port = ip_port; 172 friend_con->tcp_relays[index].ip_port = ip_port;
173 memcpy(friend_con->tcp_relays[index % FRIEND_MAX_STORED_TCP_RELAYS].public_key, public_key, crypto_box_PUBLICKEYBYTES); 173 memcpy(friend_con->tcp_relays[index].public_key, public_key, crypto_box_PUBLICKEYBYTES);
174 ++friend_con->tcp_relay_counter; 174 ++friend_con->tcp_relay_counter;
175 175
176 return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, public_key); 176 return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, public_key);
@@ -233,20 +233,15 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
233 friend_con->dht_ip_port_lastrecv = unix_time(); 233 friend_con->dht_ip_port_lastrecv = unix_time();
234} 234}
235 235
236/* Callback for dht public key changes. */ 236static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key)
237static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key)
238{ 237{
239 Friend_Connections *fr_c = object; 238 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
240 Friend_Conn *friend_con = get_conn(fr_c, number);
241 239
242 if (!friend_con) 240 if (!friend_con)
243 return; 241 return;
244 242
245 friend_con->dht_pk_lastrecv = unix_time(); 243 friend_con->dht_pk_lastrecv = unix_time();
246 244
247 if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0)
248 return;
249
250 if (friend_con->dht_lock) { 245 if (friend_con->dht_lock) {
251 if (DHT_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) { 246 if (DHT_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) {
252 printf("a. Could not delete dht peer. Please report this.\n"); 247 printf("a. Could not delete dht peer. Please report this.\n");
@@ -256,16 +251,32 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub
256 friend_con->dht_lock = 0; 251 friend_con->dht_lock = 0;
257 } 252 }
258 253
259 DHT_addfriend(fr_c->dht, dht_public_key, dht_ip_callback, object, number, &friend_con->dht_lock); 254 DHT_addfriend(fr_c->dht, dht_public_key, dht_ip_callback, fr_c, friendcon_id, &friend_con->dht_lock);
255 memcpy(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES);
256}
260 257
261 if (friend_con->crypt_connection_id == -1) { 258/* Callback for dht public key changes. */
262 friend_new_connection(fr_c, number); 259static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key)
260{
261 Friend_Connections *fr_c = object;
262 Friend_Conn *friend_con = get_conn(fr_c, number);
263
264 if (!friend_con)
265 return;
266
267 if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0)
268 return;
269
270 change_dht_pk(fr_c, number, dht_public_key);
271
272 /* if pk changed, create a new connection.*/
273 if (friend_con->crypt_connection_id != -1) {
274 crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id);
275 friend_con->crypt_connection_id = -1;
263 } 276 }
264 277
265 set_connection_dht_public_key(fr_c->net_crypto, friend_con->crypt_connection_id, dht_public_key); 278 friend_new_connection(fr_c, number);
266 onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key); 279 onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key);
267
268 memcpy(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES);
269} 280}
270 281
271static int handle_status(void *object, int number, uint8_t status) 282static int handle_status(void *object, int number, uint8_t status)
@@ -385,6 +396,11 @@ static int handle_new_connections(void *object, New_Connection *n_c)
385 return -1; 396 return -1;
386 397
387 int id = accept_crypto_connection(fr_c->net_crypto, n_c); 398 int id = accept_crypto_connection(fr_c->net_crypto, n_c);
399
400 if (id == -1) {
401 return -1;
402 }
403
388 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); 404 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id);
389 connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); 405 connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id);
390 connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); 406 connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id);
@@ -397,7 +413,9 @@ static int handle_new_connections(void *object, New_Connection *n_c)
397 friend_con->dht_ip_port_lastrecv = unix_time(); 413 friend_con->dht_ip_port_lastrecv = unix_time();
398 } 414 }
399 415
400 dht_pk_callback(fr_c, friendcon_id, n_c->dht_public_key); 416 if (memcmp(friend_con->dht_temp_pk, n_c->dht_public_key, crypto_box_PUBLICKEYBYTES) != 0) {
417 change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key);
418 }
401 419
402 nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); 420 nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id);
403 return 0; 421 return 0;
@@ -417,7 +435,12 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
417 return -1; 435 return -1;
418 } 436 }
419 437
420 int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key); 438 /* If dht_temp_pk does not contains a pk. */
439 if (!friend_con->dht_lock) {
440 return -1;
441 }
442
443 int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk);
421 444
422 if (id == -1) 445 if (id == -1)
423 return -1; 446 return -1;
@@ -712,7 +735,6 @@ void do_friend_connections(Friend_Connections *fr_c)
712 735
713 if (friend_con->dht_lock) { 736 if (friend_con->dht_lock) {
714 if (friend_new_connection(fr_c, i) == 0) { 737 if (friend_new_connection(fr_c, i) == 0) {
715 set_connection_dht_public_key(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_temp_pk);
716 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port); 738 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port);
717 connect_to_saved_tcp_relays(fr_c, i, (MAX_FRIEND_TCP_CONNECTIONS / 2)); /* Only fill it half up. */ 739 connect_to_saved_tcp_relays(fr_c, i, (MAX_FRIEND_TCP_CONNECTIONS / 2)); /* Only fill it half up. */
718 } 740 }