From 22db2b9fe581a35300b66126604d12e83c2eafb1 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 15 Jan 2018 00:29:51 +0000 Subject: [PATCH] Make Net_Crypto a module-private type. --- auto_tests/onion_test.c | 4 +- toxcore/Messenger.c | 8 +-- toxcore/group.c | 6 +- toxcore/net_crypto.c | 140 ++++++++++++++++++++++++++++++++++++++++ toxcore/net_crypto.h | 127 ++---------------------------------- toxcore/onion_client.c | 48 ++++++++------ toxcore/tox.c | 4 +- 7 files changed, 184 insertions(+), 153 deletions(-) diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 61b358d6..28fcbb40 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -479,9 +479,9 @@ START_TEST(test_announce) printf("adding friend\n"); int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c, - onion_get_net_crypto(onions[NUM_LAST]->onion_c)->self_public_key); + nc_get_self_public_key(onion_get_net_crypto(onions[NUM_LAST]->onion_c))); int frnum = onion_addfriend(onions[NUM_LAST]->onion_c, - onion_get_net_crypto(onions[NUM_FIRST]->onion_c)->self_public_key); + nc_get_self_public_key(onion_get_net_crypto(onions[NUM_FIRST]->onion_c))); onion_dht_pk_callback(onions[NUM_FIRST]->onion_c, frnum_f, &dht_pk_callback, onions[NUM_FIRST], NUM_FIRST); onion_dht_pk_callback(onions[NUM_LAST]->onion_c, frnum, &dht_pk_callback, onions[NUM_LAST], NUM_LAST); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 36dadbcf..fcee455c 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -140,7 +140,7 @@ static uint16_t address_checksum(const uint8_t *address, uint32_t len) */ void getaddress(const Messenger *m, uint8_t *address) { - id_copy(address, m->net_crypto->self_public_key); + id_copy(address, nc_get_self_public_key(m->net_crypto)); uint32_t nospam = get_nospam(m->fr); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); @@ -255,7 +255,7 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u return FAERR_NOMESSAGE; } - if (id_equal(real_pk, m->net_crypto->self_public_key)) { + if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { return FAERR_OWNKEY; } @@ -301,7 +301,7 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk) return FAERR_BADCHECKSUM; } - if (id_equal(real_pk, m->net_crypto->self_public_key)) { + if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { return FAERR_OWNKEY; } @@ -3018,7 +3018,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 set_nospam(m->fr, *(const uint32_t *)data); load_secret_key(m->net_crypto, (&data[sizeof(uint32_t)]) + CRYPTO_PUBLIC_KEY_SIZE); - if (public_key_cmp((&data[sizeof(uint32_t)]), m->net_crypto->self_public_key) != 0) { + if (public_key_cmp((&data[sizeof(uint32_t)]), nc_get_self_public_key(m->net_crypto)) != 0) { return -1; } } else { diff --git a/toxcore/group.c b/toxcore/group.c index d0a1154e..30f241ad 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -743,7 +743,7 @@ int add_groupchat(Group_Chats *g_c, uint8_t type) new_symmetric_key(g->identifier + 1); g->identifier[0] = type; g->peer_number = 0; /* Founder is peer 0. */ - memcpy(g->real_pk, g_c->m->net_crypto->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); int peer_index = addpeer(g_c, groupnumber, g->real_pk, g_c->m->dht->self_public_key, 0, NULL, false); if (peer_index == -1) { @@ -1077,7 +1077,7 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type uint16_t group_num = net_htons(groupnumber); g->status = GROUPCHAT_STATUS_VALID; g->number_joined = -1; - memcpy(g->real_pk, g_c->m->net_crypto->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); uint8_t response[INVITE_RESPONSE_PACKET_SIZE]; response[0] = INVITE_RESPONSE_ID; @@ -1681,7 +1681,7 @@ static int handle_send_peers(Group_Chats *g_c, int groupnumber, const uint8_t *d } if (g->status == GROUPCHAT_STATUS_VALID - && public_key_cmp(d, g_c->m->net_crypto->self_public_key) == 0) { + && public_key_cmp(d, nc_get_self_public_key(g_c->m->net_crypto)) == 0) { g->peer_number = peer_num; g->status = GROUPCHAT_STATUS_CONNECTED; group_name_send(g_c, groupnumber, g_c->m->name, g_c->m->name_length); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 3150392e..521dad2f 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -33,6 +33,146 @@ #include +typedef struct { + uint64_t sent_time; + uint16_t length; + uint8_t data[MAX_CRYPTO_DATA_SIZE]; +} Packet_Data; + +typedef struct { + Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; + uint32_t buffer_start; + uint32_t buffer_end; /* packet numbers in array: {buffer_start, buffer_end) */ +} Packets_Array; + +typedef struct { + uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ + uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ + uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ + uint8_t sessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* Our public key for this session. */ + uint8_t sessionsecret_key[CRYPTO_SECRET_KEY_SIZE]; /* Our private key for this session. */ + uint8_t peersessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The public key of the peer. */ + uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; /* The precomputed shared key from encrypt_precompute. */ + uint8_t status; /* 0 if no connection, 1 we are sending cookie request packets, + * 2 if we are sending handshake packets + * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), + * 4 if the connection is established. + */ + uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ + uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer */ + + uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ + uint16_t temp_packet_length; + uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */ + uint32_t temp_packet_num_sent; + + IP_Port ip_portv4; /* The ip and port to contact this guy directly.*/ + IP_Port ip_portv6; + uint64_t direct_lastrecv_timev4; /* The Time at which we last received a direct packet in ms. */ + uint64_t direct_lastrecv_timev6; + + uint64_t last_tcp_sent; /* Time the last TCP packet was sent. */ + + Packets_Array send_array; + Packets_Array recv_array; + + int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata); + void *connection_status_callback_object; + int connection_status_callback_id; + + int (*connection_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); + void *connection_data_callback_object; + int connection_data_callback_id; + + int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); + void *connection_lossy_data_callback_object; + int connection_lossy_data_callback_id; + + uint64_t last_request_packet_sent; + uint64_t direct_send_attempt_time; + + uint32_t packet_counter; + double packet_recv_rate; + uint64_t packet_counter_set; + + double packet_send_rate; + uint32_t packets_left; + uint64_t last_packets_left_set; + double last_packets_left_rem; + + double packet_send_rate_requested; + uint32_t packets_left_requested; + uint64_t last_packets_left_requested_set; + double last_packets_left_requested_rem; + + uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; + long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE], + last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE]; + uint32_t packets_sent, packets_resent; + uint64_t last_congestion_event; + uint64_t rtt_time; + + /* TCP_connection connection_number */ + unsigned int connection_number_tcp; + + 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 *userdata); + void *dht_pk_callback_object; + uint32_t dht_pk_callback_number; +} Crypto_Connection; + +struct Net_Crypto { + Logger *log; + + DHT *dht; + TCP_Connections *tcp_c; + + Crypto_Connection *crypto_connections; + pthread_mutex_t tcp_mutex; + + pthread_mutex_t connections_mutex; + unsigned int connection_use_counter; + + uint32_t crypto_connections_length; /* Length of connections array. */ + + /* Our public and secret keys. */ + uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; + + /* The secret key used for cookies */ + uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; + + int (*new_connection_callback)(void *object, New_Connection *n_c); + void *new_connection_callback_object; + + /* The current optimal sleep time */ + uint32_t current_sleep_time; + + BS_LIST ip_port_list; +}; + +const uint8_t *nc_get_self_public_key(const Net_Crypto *c) +{ + return c->self_public_key; +} + +const uint8_t *nc_get_self_secret_key(const Net_Crypto *c) +{ + return c->self_secret_key; +} + +TCP_Connections *nc_get_tcp_c(const Net_Crypto *c) +{ + return c->tcp_c; +} + +DHT *nc_get_dht(const Net_Crypto *c) +{ + return c->dht; +} static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id) { diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 5ec5ca94..bd00a3fe 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -89,98 +89,14 @@ #define DEFAULT_PING_CONNECTION 1000 #define DEFAULT_TCP_PING_CONNECTION 500 -typedef struct { - uint64_t sent_time; - uint16_t length; - uint8_t data[MAX_CRYPTO_DATA_SIZE]; -} Packet_Data; +typedef struct Net_Crypto Net_Crypto; -typedef struct { - Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; - uint32_t buffer_start; - uint32_t buffer_end; /* packet numbers in array: {buffer_start, buffer_end) */ -} Packets_Array; +const uint8_t *nc_get_self_public_key(const Net_Crypto *c); +const uint8_t *nc_get_self_secret_key(const Net_Crypto *c); +TCP_Connections *nc_get_tcp_c(const Net_Crypto *c); +DHT *nc_get_dht(const Net_Crypto *c); -typedef struct { - uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ - uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ - uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ - uint8_t sessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* Our public key for this session. */ - uint8_t sessionsecret_key[CRYPTO_SECRET_KEY_SIZE]; /* Our private key for this session. */ - uint8_t peersessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The public key of the peer. */ - uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; /* The precomputed shared key from encrypt_precompute. */ - uint8_t status; /* 0 if no connection, 1 we are sending cookie request packets, - * 2 if we are sending handshake packets - * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), - * 4 if the connection is established. - */ - uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ - uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer */ - - uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ - uint16_t temp_packet_length; - uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */ - uint32_t temp_packet_num_sent; - - IP_Port ip_portv4; /* The ip and port to contact this guy directly.*/ - IP_Port ip_portv6; - uint64_t direct_lastrecv_timev4; /* The Time at which we last received a direct packet in ms. */ - uint64_t direct_lastrecv_timev6; - - uint64_t last_tcp_sent; /* Time the last TCP packet was sent. */ - - Packets_Array send_array; - Packets_Array recv_array; - - int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata); - void *connection_status_callback_object; - int connection_status_callback_id; - - int (*connection_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); - void *connection_data_callback_object; - int connection_data_callback_id; - - int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); - void *connection_lossy_data_callback_object; - int connection_lossy_data_callback_id; - - uint64_t last_request_packet_sent; - uint64_t direct_send_attempt_time; - - uint32_t packet_counter; - double packet_recv_rate; - uint64_t packet_counter_set; - - double packet_send_rate; - uint32_t packets_left; - uint64_t last_packets_left_set; - double last_packets_left_rem; - - double packet_send_rate_requested; - uint32_t packets_left_requested; - uint64_t last_packets_left_requested_set; - double last_packets_left_requested_rem; - - uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; - long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE], - last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE]; - uint32_t packets_sent, packets_resent; - uint64_t last_congestion_event; - uint64_t rtt_time; - - /* TCP_connection connection_number */ - unsigned int connection_number_tcp; - - 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 *userdata); - void *dht_pk_callback_object; - uint32_t dht_pk_callback_number; -} Crypto_Connection; - -typedef struct { +typedef struct New_Connection { IP_Port source; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer. */ @@ -190,37 +106,6 @@ typedef struct { uint8_t cookie_length; } New_Connection; -typedef struct { - Logger *log; - - DHT *dht; - TCP_Connections *tcp_c; - - Crypto_Connection *crypto_connections; - pthread_mutex_t tcp_mutex; - - pthread_mutex_t connections_mutex; - unsigned int connection_use_counter; - - uint32_t crypto_connections_length; /* Length of connections array. */ - - /* Our public and secret keys. */ - uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; - - /* The secret key used for cookies */ - uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; - - int (*new_connection_callback)(void *object, New_Connection *n_c); - void *new_connection_callback_object; - - /* The current optimal sleep time */ - uint32_t current_sleep_time; - - BS_LIST ip_port_list; -} Net_Crypto; - - /* Set function to be called when someone requests a new connection to us. * * The set function should return -1 on failure and 0 on success. diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 50373330..f7ff41bd 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -587,12 +587,13 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ int len; if (num == 0) { - len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->c->self_public_key, - onion_c->c->self_secret_key, ping_id, onion_c->c->self_public_key, onion_c->temp_public_key, sendback); + len = create_announce_request(request, sizeof(request), dest_pubkey, nc_get_self_public_key(onion_c->c), + nc_get_self_secret_key(onion_c->c), ping_id, nc_get_self_public_key(onion_c->c), + onion_c->temp_public_key, sendback); } else { len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->friends_list[num - 1].temp_public_key, - onion_c->friends_list[num - 1].temp_secret_key, ping_id, onion_c->friends_list[num - 1].real_public_key, zero_ping_id, - sendback); + onion_c->friends_list[num - 1].temp_secret_key, ping_id, + onion_c->friends_list[num - 1].real_public_key, zero_ping_id, sendback); } if (len == -1) { @@ -670,12 +671,12 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t } Onion_Node *list_nodes = NULL; - uint8_t *reference_id = NULL; + const uint8_t *reference_id = NULL; unsigned int list_length; if (num == 0) { list_nodes = onion_c->clients_announce_list; - reference_id = onion_c->c->self_public_key; + reference_id = nc_get_self_public_key(onion_c->c); list_length = MAX_ONION_CLIENTS_ANNOUNCE; if (is_stored == 1 && public_key_cmp(pingid_or_key, onion_c->temp_public_key) != 0) { @@ -772,7 +773,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for } Onion_Node *list_nodes = NULL; - uint8_t *reference_id = NULL; + const uint8_t *reference_id = NULL; unsigned int list_length; Last_Pinged *last_pinged = NULL; @@ -780,7 +781,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for if (num == 0) { list_nodes = onion_c->clients_announce_list; - reference_id = onion_c->c->self_public_key; + reference_id = nc_get_self_public_key(onion_c->c); list_length = MAX_ONION_CLIENTS_ANNOUNCE; last_pinged = onion_c->last_pinged; last_pinged_index = &onion_c->last_pinged_index; @@ -847,7 +848,8 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t int len = -1; if (num == 0) { - len = decrypt_data(public_key, onion_c->c->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, + len = decrypt_data(public_key, nc_get_self_secret_key(onion_c->c), + packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE, length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE), plain); } else { @@ -913,7 +915,8 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac } VLA(uint8_t, plain, SIZEOF_VLA(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE); - len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + CRYPTO_PUBLIC_KEY_SIZE, + len = decrypt_data(temp_plain, nc_get_self_secret_key(onion_c->c), + packet + 1, temp_plain + CRYPTO_PUBLIC_KEY_SIZE, SIZEOF_VLA(temp_plain) - CRYPTO_PUBLIC_KEY_SIZE, plain); if ((uint32_t)len != SIZEOF_VLA(plain)) { @@ -1064,8 +1067,9 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, random_nonce(nonce); VLA(uint8_t, packet, DATA_IN_RESPONSE_MIN_SIZE + length); - memcpy(packet, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); - int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, + memcpy(packet, nc_get_self_public_key(onion_c->c), CRYPTO_PUBLIC_KEY_SIZE); + int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, + nc_get_self_secret_key(onion_c->c), nonce, data, length, packet + CRYPTO_PUBLIC_KEY_SIZE); if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE != SIZEOF_VLA(packet)) { @@ -1118,9 +1122,10 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin random_nonce(nonce); VLA(uint8_t, temp, DATA_IN_RESPONSE_MIN_SIZE + CRYPTO_NONCE_SIZE + length); - memcpy(temp, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(temp, nc_get_self_public_key(onion_c->c), CRYPTO_PUBLIC_KEY_SIZE); memcpy(temp + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); - int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, + int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, + nc_get_self_secret_key(onion_c->c), nonce, data, length, temp + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE != SIZEOF_VLA(temp)) { @@ -1152,7 +1157,8 @@ static int handle_dht_dhtpk(void *object, IP_Port source, const uint8_t *source_ } uint8_t plain[DHTPK_DATA_MAX_LENGTH]; - int len = decrypt_data(packet, onion_c->c->self_secret_key, packet + CRYPTO_PUBLIC_KEY_SIZE, + int len = decrypt_data(packet, nc_get_self_secret_key(onion_c->c), + packet + CRYPTO_PUBLIC_KEY_SIZE, packet + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, length - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), plain); @@ -1819,11 +1825,11 @@ void do_onion_client(Onion_Client *onion_c) bool UDP_connected = DHT_non_lan_connected(onion_c->dht); if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) { - set_tcp_onion_status(onion_c->c->tcp_c, !UDP_connected); + set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !UDP_connected); } onion_c->UDP_connected = UDP_connected - || get_random_tcp_onion_conn_number(onion_c->c->tcp_c) == -1; /* Check if connected to any TCP relays. */ + || get_random_tcp_onion_conn_number(nc_get_tcp_c(onion_c->c)) == -1; /* Check if connected to any TCP relays. */ if (onion_connection_status(onion_c)) { for (i = 0; i < onion_c->num_friends; ++i) { @@ -1857,8 +1863,8 @@ Onion_Client *new_onion_client(Net_Crypto *c) return NULL; } - onion_c->dht = c->dht; - onion_c->net = c->dht->net; + onion_c->dht = nc_get_dht(c); + onion_c->net = onion_c->dht->net; onion_c->c = c; new_symmetric_key(onion_c->secret_symmetric_key); crypto_new_keypair(onion_c->temp_public_key, onion_c->temp_secret_key); @@ -1866,7 +1872,7 @@ Onion_Client *new_onion_client(Net_Crypto *c) networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c); oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, &handle_dhtpk_announce, onion_c); cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, &handle_dht_dhtpk, onion_c); - set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, &handle_tcp_onion, onion_c); + set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), &handle_tcp_onion, onion_c); return onion_c; } @@ -1883,7 +1889,7 @@ void kill_onion_client(Onion_Client *onion_c) networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL); cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL); - set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, NULL, NULL); + set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), NULL, NULL); crypto_memzero(onion_c, sizeof(Onion_Client)); free(onion_c); } diff --git a/toxcore/tox.c b/toxcore/tox.c index 3201f74e..2fd478dc 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -369,7 +369,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) const Messenger *m = tox; if (public_key) { - memcpy(public_key, m->net_crypto->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(public_key, nc_get_self_public_key(m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); } } @@ -378,7 +378,7 @@ void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key) const Messenger *m = tox; if (secret_key) { - memcpy(secret_key, m->net_crypto->self_secret_key, CRYPTO_SECRET_KEY_SIZE); + memcpy(secret_key, nc_get_self_secret_key(m->net_crypto), CRYPTO_SECRET_KEY_SIZE); } }