summaryrefslogtreecommitdiff
path: root/core/ping.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/ping.c')
-rw-r--r--core/ping.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/core/ping.c b/core/ping.c
index a687f2fb..6a1fbb7e 100644
--- a/core/ping.c
+++ b/core/ping.c
@@ -162,3 +162,59 @@ int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
162 162
163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); 163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
164} 164}
165
166int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source)
167{
168 pingreq_t* p = (pingreq_t*) packet;
169 int rc;
170 uint64_t ping_id;
171
172 if (length != sizeof(pingreq_t) || id_eq(&p->client_id, self_id))
173 return 1;
174
175 // Decrypt ping_id
176 rc = decrypt_data((uint8_t*) &p->client_id,
177 self_secret_key,
178 (uint8_t*) &p->nonce,
179 (uint8_t*) &p->ping_id,
180 sizeof(ping_id) + ENCRYPTION_PADDING,
181 (uint8_t*) &ping_id);
182
183 if (rc != sizeof(ping_id))
184 return 1;
185
186 // Send response
187 send_ping_response(source, &p->client_id, ping_id);
188 send_ping_request(source, &p->client_id); // Make this smarter?
189
190 return 0;
191}
192
193int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source)
194{
195 pingres_t* p = (pingres_t*) packet;
196 int rc;
197 uint64_t ping_id;
198
199 if (length != sizeof(pingres_t) || id_eq(&p->client_id, self_id))
200 return 1;
201
202 // Decrypt ping_id
203 rc = decrypt_data((uint8_t*) &p->client_id,
204 self_secret_key,
205 (uint8_t*) &p->nonce,
206 (uint8_t*) &p->ping_id,
207 sizeof(ping_id) + ENCRYPTION_PADDING,
208 (uint8_t*) &ping_id);
209
210 if (rc != sizeof(ping_id))
211 return 1;
212
213 // Make sure ping_id is correct
214 if(!is_pinging(source, ping_id))
215 return 1;
216
217 // Associate source ip with client_id
218 addto_lists(source, (uint8_t*) &p->client_id);
219 return 0;
220}