Skip to content

Commit 89b5981

Browse files
committed
Use cached session to avoid use expensive crypto_box_open_easy on every incoming session packet
1 parent 451c5c1 commit 89b5981

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/rx.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ Aggregator::Aggregator(const string &keypair, uint64_t epoch, uint32_t channel_i
274274
last_known_block((uint64_t)-1), epoch(epoch), channel_id(channel_id)
275275
{
276276
memset(session_key, '\0', sizeof(session_key));
277+
memset(session_hash, '\0', sizeof(session_hash));
277278

278279
FILE *fp;
279280
if((fp = fopen(keypair.c_str(), "r")) == NULL)
@@ -571,6 +572,8 @@ void Aggregator::process_packet(const uint8_t *buf, size_t size, uint8_t wlan_id
571572
uint8_t bandwidth, sockaddr_in *sockaddr)
572573
{
573574
uint8_t session_tmp[MAX_SESSION_PACKET_SIZE - crypto_box_MACBYTES - sizeof(wsession_hdr_t)];
575+
uint8_t new_session_hash[sizeof(session_hash)];
576+
574577
wsession_data_t* new_session_data = NULL;
575578
//size_t new_session_tags_size = 0;
576579

@@ -608,6 +611,24 @@ void Aggregator::process_packet(const uint8_t *buf, size_t size, uint8_t wlan_id
608611
return;
609612
}
610613

614+
if(crypto_generichash(new_session_hash,
615+
sizeof(new_session_hash),
616+
buf + sizeof(wsession_hdr_t),
617+
size - sizeof(wsession_hdr_t),
618+
((wsession_hdr_t*)buf)->session_nonce,
619+
sizeof(((wsession_hdr_t*)buf)->session_nonce)) != 0)
620+
{
621+
// Should newer happened
622+
assert(0);
623+
}
624+
625+
if (memcmp(session_hash, new_session_hash, sizeof(session_hash)) == 0)
626+
{
627+
// Session is equal to current so we can ignore it
628+
count_p_session += 1;
629+
return;
630+
}
631+
611632
if(crypto_box_open_easy((uint8_t*)session_tmp,
612633
buf + sizeof(wsession_hdr_t),
613634
size - sizeof(wsession_hdr_t),
@@ -678,6 +699,9 @@ void Aggregator::process_packet(const uint8_t *buf, size_t size, uint8_t wlan_id
678699
IPC_MSG_SEND();
679700
}
680701

702+
// Cache already processed session session
703+
memcpy(session_hash, new_session_hash, sizeof(session_hash));
704+
681705
return;
682706

683707
default:

src/rx.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class Aggregator : public BaseAggregator
221221
fec_t* fec_p;
222222
int fec_k; // RS number of primary fragments in block
223223
int fec_n; // RS total number of fragments in block
224+
uint8_t session_hash[crypto_generichash_BYTES];
224225

225226
uint32_t seq;
226227
rx_ring_item_t rx_ring[RX_RING_SIZE];

0 commit comments

Comments
 (0)