diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 62 |
1 files changed, 3 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); |