summaryrefslogtreecommitdiff
path: root/core/ping.c
diff options
context:
space:
mode:
authorplutooo <tfy12vbr@student.lu.se>2013-08-06 13:09:14 -0700
committerplutooo <tfy12vbr@student.lu.se>2013-08-06 13:09:14 -0700
commit9e0ee5b7df3417b1ca6ae446aa3724106e447ed4 (patch)
tree23e6959d9c06249301956709bb39de86075beaf2 /core/ping.c
parent6e610749ebfc0bfe153ab88bcf76f4f9b24ff3fa (diff)
core: Moved handle ping packets to ping.c
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 d1cc182f..3f70b26d 100644
--- a/core/ping.c
+++ b/core/ping.c
@@ -163,3 +163,59 @@ int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
163 163
164 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); 164 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
165} 165}
166
167int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source)
168{
169 pingreq_t* p = (pingreq_t*) packet;
170 int rc;
171 uint64_t ping_id;
172
173 if (length != sizeof(pingreq_t) || id_eq(&p->client_id, self_id))
174 return 1;
175
176 // Decrypt ping_id
177 rc = decrypt_data((uint8_t*) &p->client_id,
178 self_secret_key,
179 (uint8_t*) &p->nonce,
180 (uint8_t*) &p->ping_id,
181 sizeof(ping_id) + ENCRYPTION_PADDING,
182 (uint8_t*) &ping_id);
183
184 if (rc != sizeof(ping_id))
185 return 1;
186
187 // Send response
188 send_ping_response(source, &p->client_id, ping_id);
189 send_ping_request(source, &p->client_id); // Make this smarter?
190
191 return 0;
192}
193
194int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source)
195{
196 pingres_t* p = (pingres_t*) packet;
197 int rc;
198 uint64_t ping_id;
199
200 if (length != sizeof(pingres_t) || id_eq(&p->client_id, self_id))
201 return 1;
202
203 // Decrypt ping_id
204 rc = decrypt_data((uint8_t*) &p->client_id,
205 self_secret_key,
206 (uint8_t*) &p->nonce,
207 (uint8_t*) &p->ping_id,
208 sizeof(ping_id) + ENCRYPTION_PADDING,
209 (uint8_t*) &ping_id);
210
211 if (rc != sizeof(ping_id))
212 return 1;
213
214 // Make sure ping_id is correct
215 if(!is_pinging(source, ping_id))
216 return 1;
217
218 // Associate source ip with client_id
219 addto_lists(source, (uint8_t*) &p->client_id);
220 return 0;
221}