diff options
author | plutooo <tfy12vbr@student.lu.se> | 2013-08-06 13:09:14 -0700 |
---|---|---|
committer | plutooo <tfy12vbr@student.lu.se> | 2013-08-06 13:09:14 -0700 |
commit | 9e0ee5b7df3417b1ca6ae446aa3724106e447ed4 (patch) | |
tree | 23e6959d9c06249301956709bb39de86075beaf2 /core | |
parent | 6e610749ebfc0bfe153ab88bcf76f4f9b24ff3fa (diff) |
core: Moved handle ping packets to ping.c
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 62 | ||||
-rw-r--r-- | core/DHT.h | 2 | ||||
-rw-r--r-- | core/ping.c | 56 | ||||
-rw-r--r-- | core/ping.h | 2 |
4 files changed, 63 insertions, 59 deletions
@@ -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 | */ |
355 | static void addto_lists(IP_Port ip_port, uint8_t * client_id) | 355 | void 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 | */ | ||
560 | static 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 | |||
586 | static 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 | |||
613 | static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 557 | static 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); |
@@ -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 */ |
105 | int DHT_isconnected(); | 105 | int DHT_isconnected(); |
106 | 106 | ||
107 | void 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 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 | |||
167 | int 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 | |||
194 | int 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 | } | ||
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); | |||
12 | bool is_pinging(IP_Port ipp, uint64_t ping_id); | 12 | bool is_pinging(IP_Port ipp, uint64_t ping_id); |
13 | int send_ping_request(IP_Port ipp, clientid_t* client_id); | 13 | int send_ping_request(IP_Port ipp, clientid_t* client_id); |
14 | int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id); | 14 | int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id); |
15 | int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source); | ||
16 | int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source); | ||