Lowered CPU usage.

Check if onion data will be sent before encrypting it, not after.
This commit is contained in:
irungentoo 2015-02-05 14:10:39 -05:00
parent 1302981d55
commit 66b27fc538
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98

View File

@ -778,6 +778,24 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
if (length == 0)
return -1;
unsigned int i, good_nodes[MAX_ONION_CLIENTS], num_good = 0, num_nodes = 0;
Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;
for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT))
continue;
++num_nodes;
if (list_nodes[i].is_stored) {
good_nodes[num_good] = i;
++num_good;
}
}
if (num_good < (num_nodes / 4) + 1)
return -1;
uint8_t nonce[crypto_box_NONCEBYTES];
random_nonce(nonce);
@ -789,31 +807,14 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
if ((uint32_t)len + crypto_box_PUBLICKEYBYTES != sizeof(packet))
return -1;
unsigned int i, good_nodes[MAX_ONION_CLIENTS], num_good = 0, num_nodes = 0;
Onion_Path path[MAX_ONION_CLIENTS];
Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;
for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT))
continue;
++num_nodes;
if (list_nodes[i].is_stored) {
if (random_path(onion_c, &onion_c->onion_paths_friends, ~0, &path[num_good]) == -1)
continue;
good_nodes[num_good] = i;
++num_good;
}
}
if (num_good < (num_nodes / 4) + 1)
return -1;
unsigned int good = 0;
for (i = 0; i < num_good; ++i) {
Onion_Path path;
if (random_path(onion_c, &onion_c->onion_paths_friends, ~0, &path) == -1)
continue;
uint8_t o_packet[ONION_MAX_PACKET_SIZE];
len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_client_id,
list_nodes[good_nodes[i]].data_public_key, nonce, packet, sizeof(packet));
@ -821,7 +822,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
if (len == -1)
continue;
if (send_onion_packet_tcp_udp(onion_c, &path[i], list_nodes[good_nodes[i]].ip_port, o_packet, len) == 0)
if (send_onion_packet_tcp_udp(onion_c, &path, list_nodes[good_nodes[i]].ip_port, o_packet, len) == 0)
++good;
}