diff options
author | Jeffail <ash.jeffs@gmail.com> | 2013-08-02 10:47:32 +0100 |
---|---|---|
committer | Jeffail <ash.jeffs@gmail.com> | 2013-08-02 10:47:32 +0100 |
commit | cef8a3e4e58b7a1939c5b806f780f4e56ec3fac7 (patch) | |
tree | 6c1eed6910b42119fa5d38f50493d5f0f7939dfb /core/DHT.c | |
parent | a5a1f917ab77a865b7c34c9ebd674ac5b93b36ad (diff) |
Refactored DHT.c down to line 779
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 104 |
1 files changed, 64 insertions, 40 deletions
@@ -570,10 +570,8 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
570 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 570 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
571 | { | 571 | { |
572 | /* check if packet is gonna be sent to ourself */ | 572 | /* check if packet is gonna be sent to ourself */ |
573 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) | 573 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0 |
574 | return 1; | 574 | || is_gettingnodes(ip_port, 0)) |
575 | |||
576 | if(is_gettingnodes(ip_port, 0)) | ||
577 | return 1; | 575 | return 1; |
578 | 576 | ||
579 | uint64_t ping_id = add_gettingnodes(ip_port); | 577 | uint64_t ping_id = add_gettingnodes(ip_port); |
@@ -590,21 +588,29 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
590 | memcpy(plain, &ping_id, sizeof(ping_id)); | 588 | memcpy(plain, &ping_id, sizeof(ping_id)); |
591 | memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE); | 589 | memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE); |
592 | 590 | ||
593 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); | 591 | int len = encrypt_data( public_key, |
592 | self_secret_key, | ||
593 | nonce, | ||
594 | plain, | ||
595 | sizeof(ping_id) + CLIENT_ID_SIZE, | ||
596 | encrypt ); | ||
594 | 597 | ||
595 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 598 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
596 | return -1; | 599 | return -1; |
600 | |||
597 | data[0] = 2; | 601 | data[0] = 2; |
598 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 602 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
599 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 603 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
600 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 604 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
605 | |||
601 | return sendpacket(ip_port, data, sizeof(data)); | 606 | return sendpacket(ip_port, data, sizeof(data)); |
602 | } | 607 | } |
603 | 608 | ||
604 | /* send a send nodes response */ | 609 | /* send a send nodes response */ |
605 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 610 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) |
606 | { | 611 | { |
607 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 612 | /* check if packet is gonna be sent to ourself */ |
613 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) | ||
608 | return 1; | 614 | return 1; |
609 | 615 | ||
610 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 616 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
@@ -624,8 +630,12 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
624 | memcpy(plain, &ping_id, sizeof(ping_id)); | 630 | memcpy(plain, &ping_id, sizeof(ping_id)); |
625 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format)); | 631 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format)); |
626 | 632 | ||
627 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, | 633 | int len = encrypt_data( public_key, |
628 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); | 634 | self_secret_key, |
635 | nonce, | ||
636 | plain, | ||
637 | sizeof(ping_id) + num_nodes * sizeof(Node_format), | ||
638 | encrypt ); | ||
629 | 639 | ||
630 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) | 640 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) |
631 | return -1; | 641 | return -1; |
@@ -638,26 +648,30 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
638 | return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 648 | return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
639 | } | 649 | } |
640 | 650 | ||
641 | /* Packet handling functions | 651 | /* Packet handling functions, one to handle each types of packets we receive |
642 | One to handle each types of packets we receive | 652 | * Returns 0 if handled correctly, 1 if packet is bad. |
643 | return 0 if handled correctly, 1 if packet is bad. */ | 653 | */ |
644 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 654 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) |
645 | { | 655 | { |
646 | uint64_t ping_id; | 656 | uint64_t ping_id; |
647 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 657 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
648 | return 1; | 658 | return 1; |
659 | |||
649 | /* check if packet is from ourself. */ | 660 | /* check if packet is from ourself. */ |
650 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) | 661 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
651 | return 1; | 662 | return 1; |
652 | 663 | ||
653 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 664 | int len = decrypt_data( packet + 1, |
654 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 665 | self_secret_key, |
655 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 666 | packet + 1 + CLIENT_ID_SIZE, |
667 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | ||
668 | sizeof(ping_id) + ENCRYPTION_PADDING, | ||
669 | (uint8_t *)&ping_id ); | ||
670 | |||
656 | if(len != sizeof(ping_id)) | 671 | if(len != sizeof(ping_id)) |
657 | return 1; | 672 | return 1; |
658 | 673 | ||
659 | pingres(source, packet + 1, ping_id); | 674 | pingres(source, packet + 1, ping_id); |
660 | |||
661 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 675 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
662 | 676 | ||
663 | return 0; | 677 | return 0; |
@@ -668,12 +682,18 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
668 | uint64_t ping_id; | 682 | uint64_t ping_id; |
669 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 683 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
670 | return 1; | 684 | return 1; |
671 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 685 | |
686 | /* check if packet is from ourself. */ | ||
687 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) | ||
672 | return 1; | 688 | return 1; |
673 | 689 | ||
674 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 690 | int len = decrypt_data( packet + 1, |
675 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 691 | self_secret_key, |
676 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 692 | packet + 1 + CLIENT_ID_SIZE, |
693 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | ||
694 | sizeof(ping_id) + ENCRYPTION_PADDING, | ||
695 | (uint8_t *)&ping_id ); | ||
696 | |||
677 | if(len != sizeof(ping_id)) | 697 | if(len != sizeof(ping_id)) |
678 | return 1; | 698 | return 1; |
679 | 699 | ||
@@ -682,25 +702,30 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
682 | return 0; | 702 | return 0; |
683 | } | 703 | } |
684 | return 1; | 704 | return 1; |
685 | |||
686 | } | 705 | } |
687 | 706 | ||
688 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 707 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) |
689 | { | 708 | { |
690 | uint64_t ping_id; | 709 | uint64_t ping_id; |
691 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 710 | |
711 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | ||
712 | + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING )) | ||
692 | return 1; | 713 | return 1; |
714 | |||
693 | /* check if packet is from ourself. */ | 715 | /* check if packet is from ourself. */ |
694 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) | 716 | if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
695 | return 1; | 717 | return 1; |
696 | 718 | ||
697 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 719 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
698 | 720 | ||
699 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 721 | int len = decrypt_data( packet + 1, |
700 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 722 | self_secret_key, |
701 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); | 723 | packet + 1 + CLIENT_ID_SIZE, |
724 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | ||
725 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, | ||
726 | plain ); | ||
702 | 727 | ||
703 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) | 728 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE) |
704 | return 1; | 729 | return 1; |
705 | 730 | ||
706 | memcpy(&ping_id, plain, sizeof(ping_id)); | 731 | memcpy(&ping_id, plain, sizeof(ping_id)); |
@@ -709,29 +734,28 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
709 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 734 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
710 | 735 | ||
711 | return 0; | 736 | return 0; |
712 | |||
713 | } | 737 | } |
714 | 738 | ||
715 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | 739 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) |
716 | { | 740 | { |
717 | uint64_t ping_id; | 741 | uint64_t ping_id; |
718 | /* TODO: make this more readable */ | 742 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
719 | if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 743 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; |
720 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || | 744 | |
721 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 745 | if (length > (cid_size + sizeof(Node_format) * MAX_SENT_NODES) || |
722 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || | 746 | ((length - cid_size) % sizeof(Node_format)) != 0 || |
723 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 747 | (length < cid_size + sizeof(Node_format))) |
724 | + sizeof(Node_format) + ENCRYPTION_PADDING) { | ||
725 | return 1; | 748 | return 1; |
726 | } | ||
727 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | ||
728 | + sizeof(ping_id) + ENCRYPTION_PADDING)) / sizeof(Node_format); | ||
729 | 749 | ||
750 | uint32_t num_nodes = (length - cid_size) / sizeof(Node_format); | ||
730 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 751 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; |
731 | 752 | ||
732 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 753 | int len = decrypt_data( |
733 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 754 | packet + 1, |
734 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); | 755 | self_secret_key, |
756 | packet + 1 + CLIENT_ID_SIZE, | ||
757 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | ||
758 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain ); | ||
735 | 759 | ||
736 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) | 760 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) |
737 | return 1; | 761 | return 1; |