summaryrefslogtreecommitdiff
path: root/core/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/ping.c')
-rw-r--r--core/ping.c68
1 files changed, 34 insertions, 34 deletions
diff --git a/core/ping.c b/core/ping.c
index ee05d9fa..47d6e163 100644
--- a/core/ping.c
+++ b/core/ping.c
@@ -26,7 +26,7 @@ typedef struct {
26static pinged_t pings[PING_NUM_MAX]; 26static pinged_t pings[PING_NUM_MAX];
27static size_t num_pings; 27static size_t num_pings;
28static size_t pos_pings; 28static size_t pos_pings;
29static clientid_t* self_id = (clientid_t*) &self_public_key; 29static clientid_t *self_id = (clientid_t *) &self_public_key;
30 30
31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c 31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c
32 32
@@ -48,10 +48,10 @@ static void remove_timeouts() // O(n)
48 size_t new_num = num_pings; 48 size_t new_num = num_pings;
49 49
50 // Loop through buffer, oldest first 50 // Loop through buffer, oldest first
51 for (i=0; i<num_pings; i++) { 51 for (i = 0; i < num_pings; i++) {
52 id = (pos_pings + i) % PING_NUM_MAX; 52 id = (pos_pings + i) % PING_NUM_MAX;
53 53
54 if(is_timeout(pings[id].timestamp)) { 54 if (is_timeout(pings[id].timestamp)) {
55 new_pos++; 55 new_pos++;
56 new_num--; 56 new_num--;
57 } 57 }
@@ -92,12 +92,12 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
92{ 92{
93 if (ipp.ip.i == 0 && ping_id == 0) 93 if (ipp.ip.i == 0 && ping_id == 0)
94 return false; 94 return false;
95 95
96 size_t i, id; 96 size_t i, id;
97 97
98 remove_timeouts(); 98 remove_timeouts();
99 99
100 for (i=0; i<num_pings; i++) { 100 for (i = 0; i < num_pings; i++) {
101 id = (pos_pings + i) % PING_NUM_MAX; 101 id = (pos_pings + i) % PING_NUM_MAX;
102 102
103 // ping_id = 0 means match any id 103 // ping_id = 0 means match any id
@@ -109,7 +109,7 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with
109 return false; 109 return false;
110} 110}
111 111
112int send_ping_request(IP_Port ipp, clientid_t* client_id) 112int send_ping_request(IP_Port ipp, clientid_t *client_id)
113{ 113{
114 pingreq_t pk; 114 pingreq_t pk;
115 int rc; 115 int rc;
@@ -123,22 +123,22 @@ int send_ping_request(IP_Port ipp, clientid_t* client_id)
123 123
124 pk.magic = PACKET_PING_REQ; 124 pk.magic = PACKET_PING_REQ;
125 id_cpy(&pk.client_id, self_id); // Our pubkey 125 id_cpy(&pk.client_id, self_id); // Our pubkey
126 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce 126 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
127 127
128 // Encrypt ping_id using recipient privkey 128 // Encrypt ping_id using recipient privkey
129 rc = encrypt_data((uint8_t*) client_id, 129 rc = encrypt_data((uint8_t *) client_id,
130 self_secret_key, 130 self_secret_key,
131 (uint8_t*) &pk.nonce, 131 (uint8_t *) &pk.nonce,
132 (uint8_t*) &ping_id, sizeof(ping_id), 132 (uint8_t *) &ping_id, sizeof(ping_id),
133 (uint8_t*) &pk.ping_id); 133 (uint8_t *) &pk.ping_id);
134 134
135 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) 135 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
136 return 1; 136 return 1;
137 137
138 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); 138 return sendpacket(ipp, (uint8_t *) &pk, sizeof(pk));
139} 139}
140 140
141int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id) 141int send_ping_response(IP_Port ipp, clientid_t *client_id, uint64_t ping_id)
142{ 142{
143 pingres_t pk; 143 pingres_t pk;
144 int rc; 144 int rc;
@@ -148,24 +148,24 @@ int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
148 148
149 pk.magic = PACKET_PING_RES; 149 pk.magic = PACKET_PING_RES;
150 id_cpy(&pk.client_id, self_id); // Our pubkey 150 id_cpy(&pk.client_id, self_id); // Our pubkey
151 random_nonce((uint8_t*) &pk.nonce); // Generate random nonce 151 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce
152 152
153 // Encrypt ping_id using recipient privkey 153 // Encrypt ping_id using recipient privkey
154 rc = encrypt_data((uint8_t*) client_id, 154 rc = encrypt_data((uint8_t *) client_id,
155 self_secret_key, 155 self_secret_key,
156 (uint8_t*) &pk.nonce, 156 (uint8_t *) &pk.nonce,
157 (uint8_t*) &ping_id, sizeof(ping_id), 157 (uint8_t *) &ping_id, sizeof(ping_id),
158 (uint8_t*) &pk.ping_id); 158 (uint8_t *) &pk.ping_id);
159 159
160 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) 160 if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
161 return 1; 161 return 1;
162 162
163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); 163 return sendpacket(ipp, (uint8_t *) &pk, sizeof(pk));
164} 164}
165 165
166int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length) 166int handle_ping_request(IP_Port source, uint8_t *packet, uint32_t length)
167{ 167{
168 pingreq_t* p = (pingreq_t*) packet; 168 pingreq_t *p = (pingreq_t *) packet;
169 int rc; 169 int rc;
170 uint64_t ping_id; 170 uint64_t ping_id;
171 171
@@ -173,26 +173,26 @@ int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length)
173 return 1; 173 return 1;
174 174
175 // Decrypt ping_id 175 // Decrypt ping_id
176 rc = decrypt_data((uint8_t*) &p->client_id, 176 rc = decrypt_data((uint8_t *) &p->client_id,
177 self_secret_key, 177 self_secret_key,
178 (uint8_t*) &p->nonce, 178 (uint8_t *) &p->nonce,
179 (uint8_t*) &p->ping_id, 179 (uint8_t *) &p->ping_id,
180 sizeof(ping_id) + ENCRYPTION_PADDING, 180 sizeof(ping_id) + ENCRYPTION_PADDING,
181 (uint8_t*) &ping_id); 181 (uint8_t *) &ping_id);
182 182
183 if (rc != sizeof(ping_id)) 183 if (rc != sizeof(ping_id))
184 return 1; 184 return 1;
185 185
186 // Send response 186 // Send response
187 send_ping_response(source, &p->client_id, ping_id); 187 send_ping_response(source, &p->client_id, ping_id);
188 add_toping((uint8_t*) &p->client_id, source); 188 add_toping((uint8_t *) &p->client_id, source);
189 189
190 return 0; 190 return 0;
191} 191}
192 192
193int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length) 193int handle_ping_response(IP_Port source, uint8_t *packet, uint32_t length)
194{ 194{
195 pingres_t* p = (pingres_t*) packet; 195 pingres_t *p = (pingres_t *) packet;
196 int rc; 196 int rc;
197 uint64_t ping_id; 197 uint64_t ping_id;
198 198
@@ -200,21 +200,21 @@ int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length)
200 return 1; 200 return 1;
201 201
202 // Decrypt ping_id 202 // Decrypt ping_id
203 rc = decrypt_data((uint8_t*) &p->client_id, 203 rc = decrypt_data((uint8_t *) &p->client_id,
204 self_secret_key, 204 self_secret_key,
205 (uint8_t*) &p->nonce, 205 (uint8_t *) &p->nonce,
206 (uint8_t*) &p->ping_id, 206 (uint8_t *) &p->ping_id,
207 sizeof(ping_id) + ENCRYPTION_PADDING, 207 sizeof(ping_id) + ENCRYPTION_PADDING,
208 (uint8_t*) &ping_id); 208 (uint8_t *) &ping_id);
209 209
210 if (rc != sizeof(ping_id)) 210 if (rc != sizeof(ping_id))
211 return 1; 211 return 1;
212 212
213 // Make sure ping_id is correct 213 // Make sure ping_id is correct
214 if(!is_pinging(source, ping_id)) 214 if (!is_pinging(source, ping_id))
215 return 1; 215 return 1;
216 216
217 // Associate source ip with client_id 217 // Associate source ip with client_id
218 addto_lists(source, (uint8_t*) &p->client_id); 218 addto_lists(source, (uint8_t *) &p->client_id);
219 return 0; 219 return 0;
220} 220}