summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-06-04 23:47:18 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 01:51:11 +0300
commitb3174bb6c4cf1f98d51d45a744b6cfbd3fe61ce0 (patch)
tree21ec50b69b5a4b255a65ca7ba831901ff5eead6d /toxcore/DHT.c
parentc12ef2213800d34df768bf5308d6ac79ecee575a (diff)
Extract SharedKey struct and use it as var instead of indexing
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 93f1a52c..dbe7c6e7 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -114,21 +114,22 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
114 114
115 for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { 115 for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) {
116 int index = public_key[30] * MAX_KEYS_PER_SLOT + i; 116 int index = public_key[30] * MAX_KEYS_PER_SLOT + i;
117 Shared_Key *key = &shared_keys->keys[index];
117 118
118 if (shared_keys->keys[index].stored) { 119 if (key->stored) {
119 if (public_key_cmp(public_key, shared_keys->keys[index].public_key) == 0) { 120 if (public_key_cmp(public_key, key->public_key) == 0) {
120 memcpy(shared_key, shared_keys->keys[index].shared_key, CRYPTO_SHARED_KEY_SIZE); 121 memcpy(shared_key, key->shared_key, CRYPTO_SHARED_KEY_SIZE);
121 ++shared_keys->keys[index].times_requested; 122 ++key->times_requested;
122 shared_keys->keys[index].time_last_requested = unix_time(); 123 key->time_last_requested = unix_time();
123 return; 124 return;
124 } 125 }
125 126
126 if (num != 0) { 127 if (num != 0) {
127 if (is_timeout(shared_keys->keys[index].time_last_requested, KEYS_TIMEOUT)) { 128 if (is_timeout(key->time_last_requested, KEYS_TIMEOUT)) {
128 num = 0; 129 num = 0;
129 curr = index; 130 curr = index;
130 } else if (num > shared_keys->keys[index].times_requested) { 131 } else if (num > key->times_requested) {
131 num = shared_keys->keys[index].times_requested; 132 num = key->times_requested;
132 curr = index; 133 curr = index;
133 } 134 }
134 } 135 }
@@ -143,11 +144,12 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
143 encrypt_precompute(public_key, secret_key, shared_key); 144 encrypt_precompute(public_key, secret_key, shared_key);
144 145
145 if (num != (uint32_t)~0) { 146 if (num != (uint32_t)~0) {
146 shared_keys->keys[curr].stored = 1; 147 Shared_Key *key = &shared_keys->keys[curr];
147 shared_keys->keys[curr].times_requested = 1; 148 key->stored = 1;
148 memcpy(shared_keys->keys[curr].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); 149 key->times_requested = 1;
149 memcpy(shared_keys->keys[curr].shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE); 150 memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
150 shared_keys->keys[curr].time_last_requested = unix_time(); 151 memcpy(key->shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE);
152 key->time_last_requested = unix_time();
151 } 153 }
152} 154}
153 155