mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Some CPU optimizations and fixes to onion_announce_request.
This commit is contained in:
parent
c0c660c99a
commit
325395820d
|
@ -216,9 +216,12 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet
|
||||||
if (length != ANNOUNCE_REQUEST_SIZE_RECV)
|
if (length != ANNOUNCE_REQUEST_SIZE_RECV)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
uint8_t *packet_public_key = packet + 1 + crypto_box_NONCEBYTES;
|
||||||
|
uint8_t shared_key[crypto_box_BEFORENMBYTES];
|
||||||
|
encrypt_precompute(packet_public_key, onion_a->dht->self_secret_key, shared_key);
|
||||||
|
|
||||||
uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
|
uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
|
||||||
int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, packet + 1,
|
int len = decrypt_data_fast(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
|
||||||
packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
|
|
||||||
ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH +
|
ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH +
|
||||||
crypto_box_MACBYTES, plain);
|
crypto_box_MACBYTES, plain);
|
||||||
|
|
||||||
|
@ -226,16 +229,17 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
uint8_t ping_id1[ONION_PING_ID_SIZE];
|
uint8_t ping_id1[ONION_PING_ID_SIZE];
|
||||||
generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES, source, ping_id1);
|
generate_ping_id(onion_a, unix_time(), packet_public_key, source, ping_id1);
|
||||||
|
|
||||||
uint8_t ping_id2[ONION_PING_ID_SIZE];
|
uint8_t ping_id2[ONION_PING_ID_SIZE];
|
||||||
generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES, source, ping_id2);
|
generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet_public_key, source, ping_id2);
|
||||||
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
|
|
||||||
|
uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES;
|
||||||
|
|
||||||
if (memcmp(ping_id1, plain, ONION_PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, ONION_PING_ID_SIZE) == 0) {
|
if (memcmp(ping_id1, plain, ONION_PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, ONION_PING_ID_SIZE) == 0) {
|
||||||
index = add_to_entries(onion_a, source, packet + 1 + crypto_box_NONCEBYTES,
|
index = add_to_entries(onion_a, source, packet_public_key, data_public_key,
|
||||||
plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES,
|
|
||||||
packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3));
|
packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3));
|
||||||
} else {
|
} else {
|
||||||
index = in_entries(onion_a, plain + ONION_PING_ID_SIZE);
|
index = in_entries(onion_a, plain + ONION_PING_ID_SIZE);
|
||||||
|
@ -259,16 +263,21 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
pl[0] = 0;
|
pl[0] = 0;
|
||||||
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
||||||
|
} else {
|
||||||
|
if (memcmp(onion_a->entries[index].public_key, packet_public_key, crypto_box_PUBLICKEYBYTES) == 0
|
||||||
|
&& memcmp(onion_a->entries[index].data_public_key, data_public_key, crypto_box_PUBLICKEYBYTES) != 0) {
|
||||||
|
pl[0] = 0;
|
||||||
|
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
||||||
} else {
|
} else {
|
||||||
pl[0] = 1;
|
pl[0] = 1;
|
||||||
memcpy(pl + 1, onion_a->entries[index].data_public_key, crypto_box_PUBLICKEYBYTES);
|
memcpy(pl + 1, onion_a->entries[index].data_public_key, crypto_box_PUBLICKEYBYTES);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(pl + 1 + ONION_PING_ID_SIZE, nodes_list, num_nodes * sizeof(Node_format));
|
memcpy(pl + 1 + ONION_PING_ID_SIZE, nodes_list, num_nodes * sizeof(Node_format));
|
||||||
|
|
||||||
uint8_t data[ONION_ANNOUNCE_RESPONSE_MAX_SIZE];
|
uint8_t data[ONION_ANNOUNCE_RESPONSE_MAX_SIZE];
|
||||||
len = encrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, nonce, pl,
|
len = encrypt_data_fast(shared_key, nonce, pl, 1 + ONION_PING_ID_SIZE + num_nodes * sizeof(Node_format),
|
||||||
1 + ONION_PING_ID_SIZE + num_nodes * sizeof(Node_format),
|
|
||||||
data + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES);
|
data + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES);
|
||||||
|
|
||||||
if ((uint32_t)len != 1 + ONION_PING_ID_SIZE + num_nodes * sizeof(Node_format) + crypto_box_MACBYTES)
|
if ((uint32_t)len != 1 + ONION_PING_ID_SIZE + num_nodes * sizeof(Node_format) + crypto_box_MACBYTES)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user