summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-06 13:41:04 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-06 13:41:04 -0700
commit39a213a58c8a98384b0813eb1e0f035606a7a305 (patch)
tree50c553ee4bd454d01aa4ad12667b59b119c6aad1 /core
parentfc5a2f53df06bfba185ad9868970e1fecfa0fc06 (diff)
parent9e0ee5b7df3417b1ca6ae446aa3724106e447ed4 (diff)
Merge pull request #361 from plutooo/master
core: Moved handle ping packets to ping.c
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c62
-rw-r--r--core/DHT.h2
-rw-r--r--core/ping.c56
-rw-r--r--core/ping.h2
4 files changed, 63 insertions, 59 deletions
diff --git a/core/DHT.c b/core/DHT.c
index be3ac186..b5224b8f 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -352,7 +352,7 @@ static int replace_good( Client_data * list,
352/* Attempt to add client with ip_port and client_id to the friends client list 352/* Attempt to add client with ip_port and client_id to the friends client list
353 * and close_clientlist 353 * and close_clientlist
354 */ 354 */
355static void addto_lists(IP_Port ip_port, uint8_t * client_id) 355void addto_lists(IP_Port ip_port, uint8_t * client_id)
356{ 356{
357 uint32_t i; 357 uint32_t i;
358 358
@@ -554,62 +554,6 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
554 return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 554 return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
555} 555}
556 556
557/* Packet handling functions, one to handle each types of packets we receive
558 * Returns 0 if handled correctly, 1 if packet is bad.
559 */
560static int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
561{
562 uint64_t ping_id;
563 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
564 return 1;
565
566 /* check if packet is from ourself. */
567 if(id_equal(packet + 1, self_public_key))
568 return 1;
569
570 int len = decrypt_data( packet + 1,
571 self_secret_key,
572 packet + 1 + CLIENT_ID_SIZE,
573 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
574 sizeof(ping_id) + ENCRYPTION_PADDING,
575 (uint8_t *)&ping_id );
576
577 if(len != sizeof(ping_id))
578 return 1;
579
580 send_ping_response(source, (clientid_t*) (packet + 1), ping_id);
581 send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */
582
583 return 0;
584}
585
586static int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
587{
588 uint64_t ping_id;
589 if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING)
590 return 1;
591
592 /* check if packet is from ourself. */
593 if(id_equal(packet + 1, self_public_key))
594 return 1;
595
596 int len = decrypt_data( packet + 1,
597 self_secret_key,
598 packet + 1 + CLIENT_ID_SIZE,
599 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
600 sizeof(ping_id) + ENCRYPTION_PADDING,
601 (uint8_t *)&ping_id );
602
603 if(len != sizeof(ping_id))
604 return 1;
605
606 if(is_pinging(source, ping_id)) {
607 addto_lists(source, packet + 1);
608 return 0;
609 }
610 return 1;
611}
612
613static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) 557static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
614{ 558{
615 uint64_t ping_id; 559 uint64_t ping_id;
@@ -1134,10 +1078,10 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
1134{ 1078{
1135 switch (packet[0]) { 1079 switch (packet[0]) {
1136 case 0: 1080 case 0:
1137 return handle_pingreq(packet, length, source); 1081 return handle_ping_request(packet, length, source);
1138 1082
1139 case 1: 1083 case 1:
1140 return handle_pingres(packet, length, source); 1084 return handle_ping_response(packet, length, source);
1141 1085
1142 case 2: 1086 case 2:
1143 return handle_getnodes(packet, length, source); 1087 return handle_getnodes(packet, length, source);
diff --git a/core/DHT.h b/core/DHT.h
index 0edaebf3..2308abd8 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -104,6 +104,8 @@ int DHT_load(uint8_t *data, uint32_t size);
104 returns 1 if we are */ 104 returns 1 if we are */
105int DHT_isconnected(); 105int DHT_isconnected();
106 106
107void addto_lists(IP_Port ip_port, uint8_t * client_id);
108
107#ifdef __cplusplus 109#ifdef __cplusplus
108} 110}
109#endif 111#endif
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}
diff --git a/core/ping.h b/core/ping.h
index f2770a00..2cab7d59 100644
--- a/core/ping.h
+++ b/core/ping.h
@@ -12,3 +12,5 @@ uint64_t add_ping(IP_Port ipp);
12bool is_pinging(IP_Port ipp, uint64_t ping_id); 12bool is_pinging(IP_Port ipp, uint64_t ping_id);
13int send_ping_request(IP_Port ipp, clientid_t* client_id); 13int send_ping_request(IP_Port ipp, clientid_t* client_id);
14int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id); 14int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id);
15int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source);
16int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source);