From f2a313359e14ac3f4cf00421ad4ff0e4954c5a07 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 26 Sep 2014 20:32:38 -0400 Subject: Added callbacks to onion_client and net_crypto for the temp dht key. Better than the polling mess. Moved DHT to Messenger from onion_client (still needs some cleanups). --- toxcore/net_crypto.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'toxcore/net_crypto.h') diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 5e5df499..8c77a7a1 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -161,6 +161,10 @@ typedef struct { uint8_t maximum_speed_reached; pthread_mutex_t mutex; + + void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key); + void *dht_pk_callback_object; + uint32_t dht_pk_callback_number; } Crypto_Connection; typedef struct { @@ -294,6 +298,16 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, int id); +/* Set the function for this friend that will be callbacked with object and number + * when that friend gives us his DHT temporary public key. + * + * object and number will be passed as argument to this function. + * + * return -1 on failure. + * return 0 on success. + */ +int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, const uint8_t *dht_public_key), void *object, uint32_t number); + /* returns the number of packet slots left in the sendbuffer. * return 0 if failure. */ -- cgit v1.2.3 From c618263acd7e2ed76befe3c4b6211f8ba15ee79c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 26 Sep 2014 20:43:59 -0400 Subject: Removed useless code from onion_client. Astyled. --- toxcore/Messenger.c | 9 ++++++--- toxcore/net_crypto.c | 3 ++- toxcore/net_crypto.h | 3 ++- toxcore/onion_client.c | 29 +++++++++++------------------ toxcore/onion_client.h | 10 +++++----- 5 files changed, 26 insertions(+), 28 deletions(-) (limited to 'toxcore/net_crypto.h') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 8229c270..c98b9d95 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -211,8 +211,9 @@ static void dht_pk_callback(void *data, int32_t number, const uint8_t *dht_publi friend_new_connection(m, number, m->friendlist[number].client_id); } - set_connection_dht_public_key(m->net_crypto, m->friendlist[number].crypt_connection_id, dht_public_key, current_time_monotonic()); - onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[number].onion_friendnum, dht_public_key, current_time_monotonic()); + set_connection_dht_public_key(m->net_crypto, m->friendlist[number].crypt_connection_id, dht_public_key, + current_time_monotonic()); + onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[number].onion_friendnum, dht_public_key); memcpy(m->friendlist[number].dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES); } @@ -389,6 +390,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber) remove_online_friend(m, friendnumber); onion_delfriend(m->onion_c, m->friendlist[friendnumber].onion_friendnum); + if (m->friendlist[friendnumber].dht_lock) { DHT_delfriend(m->dht, m->friendlist[friendnumber].dht_temp_pk, m->friendlist[friendnumber].dht_lock); } @@ -2399,7 +2401,8 @@ void do_friends(Messenger *m) check_friend_request_timed_out(m, i, temp_time); if (m->friendlist[i].dht_lock) - set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk, current_time_monotonic()); + set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk, + current_time_monotonic()); set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_ip_port); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1d93bc6a..a286df2a 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2260,7 +2260,8 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, * return -1 on failure. * return 0 on success. */ -int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, const uint8_t *dht_public_key), void *object, uint32_t number) +int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, + const uint8_t *dht_public_key), void *object, uint32_t number) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 8c77a7a1..cfa7c576 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -306,7 +306,8 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, * return -1 on failure. * return 0 on success. */ -int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, const uint8_t *dht_public_key), void *object, uint32_t number); +int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, + const uint8_t *dht_public_key), void *object, uint32_t number); /* returns the number of packet slots left in the sendbuffer. * return 0 if failure. diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 56c79cc5..97afeb79 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -638,9 +638,12 @@ static int handle_fakeid_announce(void *object, const uint8_t *source_pubkey, co return 1; onion_c->friends_list[friend_num].last_noreplay = no_replay; + if (onion_c->friends_list[friend_num].dht_pk_callback) - onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object, onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t)); - //onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t), current_time_monotonic()); + onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object, + onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t)); + + onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t)); onion_c->friends_list[friend_num].last_seen = unix_time(); uint16_t len_nodes = length - FAKEID_DATA_MIN_LENGTH; @@ -1006,7 +1009,8 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela * return -1 on failure. * return 0 on success. */ -int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, const uint8_t *dht_public_key), void *object, uint32_t number) +int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, + const uint8_t *dht_public_key), void *object, uint32_t number) { if ((uint32_t)friend_num >= onion_c->num_friends) return -1; @@ -1018,13 +1022,11 @@ int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function } /* Set a friends DHT public key. - * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to - * the other peer. * * return -1 on failure. * return 0 on success. */ -int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key, uint64_t timestamp) +int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key) { if ((uint32_t)friend_num >= onion_c->num_friends) return -1; @@ -1032,25 +1034,16 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin if (onion_c->friends_list[friend_num].status == 0) return -1; - if (onion_c->friends_list[friend_num].fake_client_id_timestamp >= timestamp) - return -1; - if (onion_c->friends_list[friend_num].is_fake_clientid) { if (memcmp(dht_key, onion_c->friends_list[friend_num].fake_client_id, crypto_box_PUBLICKEYBYTES) == 0) { return -1; } - //DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id, 0); onion_c->friends_list[friend_num].is_fake_clientid = 0; } - //if (DHT_addfriend(onion_c->dht, dht_key, 0, 0, 0, 0) == -1) { - // return -1; - //} - onion_c->friends_list[friend_num].last_seen = unix_time(); onion_c->friends_list[friend_num].is_fake_clientid = 1; - onion_c->friends_list[friend_num].fake_client_id_timestamp = timestamp; memcpy(onion_c->friends_list[friend_num].fake_client_id, dht_key, crypto_box_PUBLICKEYBYTES); return 0; @@ -1059,9 +1052,9 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin /* Copy friends DHT public key into dht_key. * * return 0 on failure (no key copied). - * return timestamp on success (key copied). + * return 1 on success (key copied). */ -uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key) +unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key) { if ((uint32_t)friend_num >= onion_c->num_friends) return 0; @@ -1073,7 +1066,7 @@ uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, return 0; memcpy(dht_key, onion_c->friends_list[friend_num].fake_client_id, crypto_box_PUBLICKEYBYTES); - return onion_c->friends_list[friend_num].fake_client_id_timestamp; + return 1; } /* Get the ip of friend friendnum and put it in ip_port diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 5b89a8d9..3143f93a 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -79,7 +79,6 @@ typedef struct { uint8_t is_online; /* Set by the onion_set_friend_status function. */ uint8_t is_fake_clientid; /* 0 if we don't know the fake client id of the other 1 if we do. */ - uint64_t fake_client_id_timestamp; uint8_t fake_client_id[crypto_box_PUBLICKEYBYTES]; uint8_t real_client_id[crypto_box_PUBLICKEYBYTES]; @@ -218,7 +217,8 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela * return -1 on failure. * return 0 on success. */ -int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, const uint8_t *dht_public_key), void *object, uint32_t number); +int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, + const uint8_t *dht_public_key), void *object, uint32_t number); /* Set a friends DHT public key. * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to @@ -227,14 +227,14 @@ int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function * return -1 on failure. * return 0 on success. */ -int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key, uint64_t timestamp); +int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key); /* Copy friends DHT public key into dht_key. * * return 0 on failure (no key copied). - * return timestamp on success (key copied). + * return 1 on success (key copied). */ -uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key); +unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key); #define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) #define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE) -- cgit v1.2.3 From 03d6f9571345cb79e5a2b78d598a928ddea2a5bc Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 26 Sep 2014 21:22:07 -0400 Subject: Remove timestamp from set_connection_dht_public_key(). --- toxcore/Messenger.c | 6 ++---- toxcore/net_crypto.c | 21 +++++++-------------- toxcore/net_crypto.h | 10 +++------- 3 files changed, 12 insertions(+), 25 deletions(-) (limited to 'toxcore/net_crypto.h') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 9383af68..df736ebe 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -213,8 +213,7 @@ static void dht_pk_callback(void *data, int32_t number, const uint8_t *dht_publi friend_new_connection(m, number, m->friendlist[number].client_id); } - set_connection_dht_public_key(m->net_crypto, m->friendlist[number].crypt_connection_id, dht_public_key, - current_time_monotonic()); + set_connection_dht_public_key(m->net_crypto, m->friendlist[number].crypt_connection_id, dht_public_key); onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[number].onion_friendnum, dht_public_key); memcpy(m->friendlist[number].dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES); @@ -2415,8 +2414,7 @@ void do_friends(Messenger *m) if (friend_new_connection(m, i, m->friendlist[i].client_id) == 0) { if (m->friendlist[i].dht_lock) - set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk, - current_time_monotonic()); + set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk); set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_ip_port); } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 59f6ee9a..63012ce6 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1238,7 +1238,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons conn->status = CRYPTO_CONN_NOT_CONFIRMED; /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ - set_connection_dht_public_key(c, crypt_connection_id, dht_public_key, current_time_monotonic()); + set_connection_dht_public_key(c, crypt_connection_id, dht_public_key); if (conn->dht_pk_callback) conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key); @@ -1477,7 +1477,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const if (create_send_handshake(c, crypt_connection_id, n_c.cookie, n_c.dht_public_key) == 0) { conn->status = CRYPTO_CONN_NOT_CONFIRMED; /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ - set_connection_dht_public_key(c, crypt_connection_id, n_c.dht_public_key, current_time_monotonic()); + set_connection_dht_public_key(c, crypt_connection_id, n_c.dht_public_key); if (conn->dht_pk_callback) conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, n_c.dht_public_key); @@ -1530,7 +1530,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) conn->status = CRYPTO_CONN_NOT_CONFIRMED; /* Status needs to be CRYPTO_CONN_NOT_CONFIRMED for this to work. */ - set_connection_dht_public_key(c, crypt_connection_id, n_c->dht_public_key, current_time_monotonic()); + set_connection_dht_public_key(c, crypt_connection_id, n_c->dht_public_key); conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; crypto_connection_add_source(c, crypt_connection_id, n_c->source); @@ -1626,9 +1626,9 @@ static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id) /* Copy friends DHT public key into dht_key. * * return 0 on failure (no key copied). - * return timestamp on success (key copied). + * return 1 on success (key copied). */ -uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) +unsigned int get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1639,28 +1639,22 @@ uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, ui return 0; memcpy(dht_public_key, conn->dht_public_key, crypto_box_PUBLICKEYBYTES); - return conn->dht_public_key_timestamp; + return 1; } /* Set the DHT public key of the crypto connection. - * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to - * the other peer. * * return -1 on failure. * return 0 on success. */ -int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, - uint64_t timestamp) +int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == 0) return -1; - if (timestamp <= conn->dht_public_key_timestamp) - return -1; - if (conn->dht_public_key_set == 1 && memcmp(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) return -1; @@ -1670,7 +1664,6 @@ int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); conn->dht_public_key_set = 1; - conn->dht_public_key_timestamp = timestamp; if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) { conn->cookie_request_number = random_64b(); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index cfa7c576..3245b6a2 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -111,7 +111,6 @@ typedef struct { uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ uint8_t dht_public_key_set; /* True if the dht public key is set, false if it isn't. */ - uint64_t dht_public_key_timestamp; /* Timestamp of the last time we confirmed the key was correct. */ uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ uint16_t temp_packet_length; @@ -240,19 +239,16 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key); /* Copy friends DHT public key into dht_key. * * return 0 on failure (no key copied). - * return timestamp on success (key copied). + * return 1 on success (key copied). */ -uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key); +unsigned int get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key); /* Set the DHT public key of the crypto connection. - * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to - * the other peer. * * return -1 on failure. * return 0 on success. */ -int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, - uint64_t timestamp); +int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key); /* Set the direct ip of the crypto connection. * -- cgit v1.2.3