diff options
author | irungentoo <irungentoo@gmail.com> | 2013-10-24 18:59:00 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-10-24 18:59:00 -0400 |
commit | 55692665dce4554a5f989aae0743a66ad112951a (patch) | |
tree | bafe319e923e621c3a6a1cf299babb9850f13850 /toxcore/DHT.c | |
parent | f7b90f10602e054544649920832548d8f55d3e48 (diff) |
Removed now useless ping_id from send/get node packets.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 160 |
1 files changed, 46 insertions, 114 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ca78816d..7c3894b3 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -324,7 +324,8 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
324 | if (ipv46x) | 324 | if (ipv46x) |
325 | continue; | 325 | continue; |
326 | 326 | ||
327 | if (!LAN_ip(ipptp->ip_port.ip) && !is_LAN) | 327 | /* don't send LAN ips to non LAN peers */ |
328 | if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) | ||
328 | continue; | 329 | continue; |
329 | 330 | ||
330 | if (num_nodes < MAX_SENT_NODES) { | 331 | if (num_nodes < MAX_SENT_NODES) { |
@@ -585,51 +586,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
585 | } | 586 | } |
586 | } | 587 | } |
587 | 588 | ||
588 | static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) | ||
589 | { | ||
590 | uint32_t i; | ||
591 | uint8_t pinging; | ||
592 | uint64_t temp_time = unix_time(); | ||
593 | |||
594 | for (i = 0; i < LSEND_NODES_ARRAY; ++i ) { | ||
595 | if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) { | ||
596 | pinging = 0; | ||
597 | |||
598 | if (ping_id != 0 && dht->send_nodes[i].id == ping_id) | ||
599 | ++pinging; | ||
600 | |||
601 | if (ip_isset(&ip_port.ip) && ipport_equal(&dht->send_nodes[i].ip_port, &ip_port)) | ||
602 | ++pinging; | ||
603 | |||
604 | if (pinging == (ping_id != 0) + ip_isset(&ip_port.ip)) | ||
605 | return 1; | ||
606 | } | ||
607 | } | ||
608 | |||
609 | return 0; | ||
610 | } | ||
611 | |||
612 | /* Same but for get node requests. */ | ||
613 | static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) | ||
614 | { | ||
615 | uint32_t i, j; | ||
616 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | ||
617 | uint64_t temp_time = unix_time(); | ||
618 | |||
619 | for (i = 0; i < PING_TIMEOUT; ++i ) { | ||
620 | for (j = 0; j < LSEND_NODES_ARRAY; ++j ) { | ||
621 | if (is_timeout(temp_time, dht->send_nodes[j].timestamp, PING_TIMEOUT - i)) { | ||
622 | dht->send_nodes[j].timestamp = temp_time; | ||
623 | dht->send_nodes[j].ip_port = ip_port; | ||
624 | dht->send_nodes[j].id = ping_id; | ||
625 | return ping_id; | ||
626 | } | ||
627 | } | ||
628 | } | ||
629 | |||
630 | return 0; | ||
631 | } | ||
632 | |||
633 | #define NODES_ENCRYPTED_MESSAGE_LENGTH (crypto_secretbox_NONCEBYTES + sizeof(uint64_t) + sizeof(Node_format) + sizeof(Node_format) + crypto_secretbox_MACBYTES) | 589 | #define NODES_ENCRYPTED_MESSAGE_LENGTH (crypto_secretbox_NONCEBYTES + sizeof(uint64_t) + sizeof(Node_format) + sizeof(Node_format) + crypto_secretbox_MACBYTES) |
634 | 590 | ||
635 | /* Send a getnodes request. | 591 | /* Send a getnodes request. |
@@ -637,15 +593,9 @@ static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port) | |||
637 | static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, Node_format *sendback_node) | 593 | static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, Node_format *sendback_node) |
638 | { | 594 | { |
639 | /* Check if packet is going to be sent to ourself. */ | 595 | /* Check if packet is going to be sent to ourself. */ |
640 | if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0)) | 596 | if (id_equal(public_key, dht->c->self_public_key)) |
641 | return -1; | ||
642 | |||
643 | uint64_t ping_id = add_gettingnodes(dht, ip_port); | ||
644 | |||
645 | if (ping_id == 0) | ||
646 | return -1; | 597 | return -1; |
647 | 598 | ||
648 | |||
649 | uint8_t plain_message[NODES_ENCRYPTED_MESSAGE_LENGTH] = {0}; | 599 | uint8_t plain_message[NODES_ENCRYPTED_MESSAGE_LENGTH] = {0}; |
650 | uint8_t encrypted_message[NODES_ENCRYPTED_MESSAGE_LENGTH]; | 600 | uint8_t encrypted_message[NODES_ENCRYPTED_MESSAGE_LENGTH]; |
651 | uint8_t nonce[crypto_box_NONCEBYTES]; | 601 | uint8_t nonce[crypto_box_NONCEBYTES]; |
@@ -672,23 +622,22 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
672 | if (len_m != NODES_ENCRYPTED_MESSAGE_LENGTH - crypto_secretbox_NONCEBYTES) | 622 | if (len_m != NODES_ENCRYPTED_MESSAGE_LENGTH - crypto_secretbox_NONCEBYTES) |
673 | return -1; | 623 | return -1; |
674 | 624 | ||
675 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 625 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
676 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 626 | uint8_t plain[CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
677 | uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 627 | uint8_t encrypt[CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
678 | 628 | ||
679 | 629 | ||
680 | memcpy(plain, &ping_id, sizeof(ping_id)); | 630 | memcpy(plain, client_id, CLIENT_ID_SIZE); |
681 | memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE); | 631 | memcpy(plain + CLIENT_ID_SIZE, encrypted_message, NODES_ENCRYPTED_MESSAGE_LENGTH); |
682 | memcpy(plain + sizeof(ping_id) + CLIENT_ID_SIZE, encrypted_message, NODES_ENCRYPTED_MESSAGE_LENGTH); | ||
683 | 632 | ||
684 | int len = encrypt_data( public_key, | 633 | int len = encrypt_data( public_key, |
685 | dht->c->self_secret_key, | 634 | dht->c->self_secret_key, |
686 | nonce, | 635 | nonce, |
687 | plain, | 636 | plain, |
688 | sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH, | 637 | CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH, |
689 | encrypt ); | 638 | encrypt ); |
690 | 639 | ||
691 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES) | 640 | if (len != CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES) |
692 | return -1; | 641 | return -1; |
693 | 642 | ||
694 | data[0] = NET_PACKET_GET_NODES; | 643 | data[0] = NET_PACKET_GET_NODES; |
@@ -704,15 +653,14 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
704 | /* because of BINARY compatibility, the Node_format MUST BE Node4_format, | 653 | /* because of BINARY compatibility, the Node_format MUST BE Node4_format, |
705 | * IPv6 nodes are sent in a different message | 654 | * IPv6 nodes are sent in a different message |
706 | * encrypted_data must be of size NODES_ENCRYPTED_MESSAGE_LENGTH */ | 655 | * encrypted_data must be of size NODES_ENCRYPTED_MESSAGE_LENGTH */ |
707 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id, | 656 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint8_t *encrypted_data) |
708 | uint8_t *encrypted_data) | ||
709 | { | 657 | { |
710 | /* Check if packet is going to be sent to ourself. */ | 658 | /* Check if packet is going to be sent to ourself. */ |
711 | if (id_equal(public_key, dht->c->self_public_key)) | 659 | if (id_equal(public_key, dht->c->self_public_key)) |
712 | return -1; | 660 | return -1; |
713 | 661 | ||
714 | size_t Node4_format_size = sizeof(Node4_format); | 662 | size_t Node4_format_size = sizeof(Node4_format); |
715 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 663 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
716 | + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 664 | + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
717 | 665 | ||
718 | Node_format nodes_list[MAX_SENT_NODES]; | 666 | Node_format nodes_list[MAX_SENT_NODES]; |
@@ -721,14 +669,12 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
721 | if (num_nodes == 0) | 669 | if (num_nodes == 0) |
722 | return 0; | 670 | return 0; |
723 | 671 | ||
724 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 672 | uint8_t plain[Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
725 | uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 673 | uint8_t encrypt[Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
726 | uint8_t nonce[crypto_box_NONCEBYTES]; | 674 | uint8_t nonce[crypto_box_NONCEBYTES]; |
727 | new_nonce(nonce); | 675 | new_nonce(nonce); |
728 | 676 | ||
729 | memcpy(plain, &ping_id, sizeof(ping_id)); | 677 | Node4_format *nodes4_list = (Node4_format *)(plain); |
730 | |||
731 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | ||
732 | int i, num_nodes_ok = 0; | 678 | int i, num_nodes_ok = 0; |
733 | 679 | ||
734 | for (i = 0; i < num_nodes; i++) { | 680 | for (i = 0; i < num_nodes; i++) { |
@@ -753,15 +699,15 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
753 | num_nodes = num_nodes_ok; | 699 | num_nodes = num_nodes_ok; |
754 | } | 700 | } |
755 | 701 | ||
756 | memcpy(plain + sizeof(ping_id) + num_nodes * Node4_format_size, encrypted_data, NODES_ENCRYPTED_MESSAGE_LENGTH); | 702 | memcpy(plain + num_nodes * Node4_format_size, encrypted_data, NODES_ENCRYPTED_MESSAGE_LENGTH); |
757 | int len = encrypt_data( public_key, | 703 | int len = encrypt_data( public_key, |
758 | dht->c->self_secret_key, | 704 | dht->c->self_secret_key, |
759 | nonce, | 705 | nonce, |
760 | plain, | 706 | plain, |
761 | sizeof(ping_id) + num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH, | 707 | num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH, |
762 | encrypt ); | 708 | encrypt ); |
763 | 709 | ||
764 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + | 710 | if ((unsigned int)len != num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + |
765 | crypto_box_MACBYTES) | 711 | crypto_box_MACBYTES) |
766 | return -1; | 712 | return -1; |
767 | 713 | ||
@@ -774,15 +720,14 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
774 | } | 720 | } |
775 | 721 | ||
776 | /* Send a send nodes response: message for IPv6 nodes */ | 722 | /* Send a send nodes response: message for IPv6 nodes */ |
777 | static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id, | 723 | static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint8_t *encrypted_data) |
778 | uint8_t *encrypted_data) | ||
779 | { | 724 | { |
780 | /* Check if packet is going to be sent to ourself. */ | 725 | /* Check if packet is going to be sent to ourself. */ |
781 | if (id_equal(public_key, dht->c->self_public_key)) | 726 | if (id_equal(public_key, dht->c->self_public_key)) |
782 | return -1; | 727 | return -1; |
783 | 728 | ||
784 | size_t Node_format_size = sizeof(Node_format); | 729 | size_t Node_format_size = sizeof(Node_format); |
785 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 730 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
786 | + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 731 | + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
787 | 732 | ||
788 | Node_format nodes_list[MAX_SENT_NODES]; | 733 | Node_format nodes_list[MAX_SENT_NODES]; |
@@ -791,23 +736,21 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
791 | if (num_nodes == 0) | 736 | if (num_nodes == 0) |
792 | return 0; | 737 | return 0; |
793 | 738 | ||
794 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 739 | uint8_t plain[Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
795 | uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 740 | uint8_t encrypt[Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
796 | uint8_t nonce[crypto_box_NONCEBYTES]; | 741 | uint8_t nonce[crypto_box_NONCEBYTES]; |
797 | new_nonce(nonce); | 742 | new_nonce(nonce); |
798 | 743 | ||
799 | memcpy(plain, &ping_id, sizeof(ping_id)); | 744 | memcpy(plain, nodes_list, num_nodes * Node_format_size); |
800 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node_format_size); | 745 | memcpy(plain + num_nodes * Node_format_size, encrypted_data, NODES_ENCRYPTED_MESSAGE_LENGTH); |
801 | memcpy(plain + sizeof(ping_id) + num_nodes * Node_format_size, encrypted_data, NODES_ENCRYPTED_MESSAGE_LENGTH); | ||
802 | int len = encrypt_data( public_key, | 746 | int len = encrypt_data( public_key, |
803 | dht->c->self_secret_key, | 747 | dht->c->self_secret_key, |
804 | nonce, | 748 | nonce, |
805 | plain, | 749 | plain, |
806 | sizeof(ping_id) + num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH, | 750 | num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH, |
807 | encrypt ); | 751 | encrypt ); |
808 | 752 | ||
809 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + | 753 | if ((unsigned int)len != num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES) |
810 | crypto_box_MACBYTES) | ||
811 | return -1; | 754 | return -1; |
812 | 755 | ||
813 | data[0] = NET_PACKET_SEND_NODES_IPV6; | 756 | data[0] = NET_PACKET_SEND_NODES_IPV6; |
@@ -821,32 +764,28 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
821 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 764 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
822 | { | 765 | { |
823 | DHT *dht = object; | 766 | DHT *dht = object; |
824 | uint64_t ping_id; | ||
825 | 767 | ||
826 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 768 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES )) |
827 | + sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES )) | ||
828 | return 1; | 769 | return 1; |
829 | 770 | ||
830 | /* Check if packet is from ourself. */ | 771 | /* Check if packet is from ourself. */ |
831 | if (id_equal(packet + 1, dht->c->self_public_key)) | 772 | if (id_equal(packet + 1, dht->c->self_public_key)) |
832 | return 1; | 773 | return 1; |
833 | 774 | ||
834 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 775 | uint8_t plain[CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
835 | 776 | ||
836 | int len = decrypt_data( packet + 1, | 777 | int len = decrypt_data( packet + 1, |
837 | dht->c->self_secret_key, | 778 | dht->c->self_secret_key, |
838 | packet + 1 + CLIENT_ID_SIZE, | 779 | packet + 1 + CLIENT_ID_SIZE, |
839 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 780 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
840 | sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, | 781 | CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, |
841 | plain ); | 782 | plain ); |
842 | 783 | ||
843 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH) | 784 | if (len != CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH) |
844 | return 1; | 785 | return 1; |
845 | 786 | ||
846 | memcpy(&ping_id, plain, sizeof(ping_id)); | 787 | sendnodes(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE); |
847 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id, plain + sizeof(ping_id) + CLIENT_ID_SIZE); | 788 | sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE); /* TODO: prevent possible amplification attacks */ |
848 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), | ||
849 | ping_id, plain + sizeof(ping_id) + CLIENT_ID_SIZE); /* TODO: prevent possible amplification attacks */ | ||
850 | 789 | ||
851 | add_toping(dht->ping, packet + 1, source); | 790 | add_toping(dht->ping, packet + 1, source); |
852 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ | 791 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ |
@@ -867,7 +806,8 @@ static uint8_t sent_getnode_to_node(DHT *dht, uint8_t *client_id, IP_Port node_i | |||
867 | return 0; | 806 | return 0; |
868 | uint64_t comp_time; | 807 | uint64_t comp_time; |
869 | memcpy(&comp_time, plain_message, sizeof(uint64_t)); | 808 | memcpy(&comp_time, plain_message, sizeof(uint64_t)); |
870 | if (comp_time + PING_TIMEOUT > unix_time() || unix_time() < comp_time) | 809 | uint64_t temp_time = unix_time(); |
810 | if (comp_time + PING_TIMEOUT < temp_time || temp_time < comp_time) | ||
871 | return 0; | 811 | return 0; |
872 | Node_format test; | 812 | Node_format test; |
873 | memcpy(&test, plain_message + sizeof(uint64_t), sizeof(Node_format)); | 813 | memcpy(&test, plain_message + sizeof(uint64_t), sizeof(Node_format)); |
@@ -881,9 +821,8 @@ static uint8_t sent_getnode_to_node(DHT *dht, uint8_t *client_id, IP_Port node_i | |||
881 | static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 821 | static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
882 | { | 822 | { |
883 | DHT *dht = object; | 823 | DHT *dht = object; |
884 | uint64_t ping_id; | ||
885 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 824 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
886 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES; | 825 | cid_size += crypto_box_NONCEBYTES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES; |
887 | 826 | ||
888 | size_t Node4_format_size = sizeof(Node4_format); | 827 | size_t Node4_format_size = sizeof(Node4_format); |
889 | 828 | ||
@@ -893,26 +832,23 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
893 | return 1; | 832 | return 1; |
894 | 833 | ||
895 | uint32_t num_nodes = (length - cid_size) / Node4_format_size; | 834 | uint32_t num_nodes = (length - cid_size) / Node4_format_size; |
896 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 835 | uint8_t plain[Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
897 | 836 | ||
898 | int len = decrypt_data( | 837 | int len = decrypt_data( |
899 | packet + 1, | 838 | packet + 1, |
900 | dht->c->self_secret_key, | 839 | dht->c->self_secret_key, |
901 | packet + 1 + CLIENT_ID_SIZE, | 840 | packet + 1 + CLIENT_ID_SIZE, |
902 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 841 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
903 | sizeof(ping_id) + num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, plain ); | 842 | num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, plain ); |
904 | 843 | ||
905 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH) | 844 | if ((unsigned int)len != num_nodes * Node4_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH) |
906 | return 1; | 845 | return 1; |
907 | 846 | ||
908 | memcpy(&ping_id, plain, sizeof(ping_id)); | ||
909 | |||
910 | //if (!is_gettingnodes(dht, source, ping_id)) | ||
911 | Node_format sendback_node; | 847 | Node_format sendback_node; |
912 | if (!sent_getnode_to_node(dht, packet + 1, source, plain + sizeof(ping_id) + num_nodes * Node4_format_size, &sendback_node)) | 848 | if (!sent_getnode_to_node(dht, packet + 1, source, plain + num_nodes * Node4_format_size, &sendback_node)) |
913 | return 1; | 849 | return 1; |
914 | 850 | ||
915 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | 851 | Node4_format *nodes4_list = (Node4_format *)(plain); |
916 | Node_format nodes_list[MAX_SENT_NODES]; | 852 | Node_format nodes_list[MAX_SENT_NODES]; |
917 | uint32_t i, num_nodes_ok = 0; | 853 | uint32_t i, num_nodes_ok = 0; |
918 | 854 | ||
@@ -945,9 +881,8 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
945 | static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 881 | static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
946 | { | 882 | { |
947 | DHT *dht = object; | 883 | DHT *dht = object; |
948 | uint64_t ping_id; | ||
949 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 884 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
950 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES; | 885 | cid_size += crypto_box_NONCEBYTES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES; |
951 | 886 | ||
952 | size_t Node_format_size = sizeof(Node_format); | 887 | size_t Node_format_size = sizeof(Node_format); |
953 | 888 | ||
@@ -957,28 +892,25 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
957 | return 1; | 892 | return 1; |
958 | 893 | ||
959 | uint32_t num_nodes = (length - cid_size) / Node_format_size; | 894 | uint32_t num_nodes = (length - cid_size) / Node_format_size; |
960 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; | 895 | uint8_t plain[Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH]; |
961 | 896 | ||
962 | int len = decrypt_data( | 897 | int len = decrypt_data( |
963 | packet + 1, | 898 | packet + 1, |
964 | dht->c->self_secret_key, | 899 | dht->c->self_secret_key, |
965 | packet + 1 + CLIENT_ID_SIZE, | 900 | packet + 1 + CLIENT_ID_SIZE, |
966 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 901 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
967 | sizeof(ping_id) + num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, plain ); | 902 | num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, plain ); |
968 | 903 | ||
969 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH) | 904 | if ((unsigned int)len != num_nodes * Node_format_size + NODES_ENCRYPTED_MESSAGE_LENGTH) |
970 | return 1; | 905 | return 1; |
971 | 906 | ||
972 | memcpy(&ping_id, plain, sizeof(ping_id)); | ||
973 | |||
974 | //if (!is_gettingnodes(dht, source, ping_id)) | ||
975 | Node_format sendback_node; | 907 | Node_format sendback_node; |
976 | if (!sent_getnode_to_node(dht, packet + 1, source, plain + sizeof(ping_id) + num_nodes * Node_format_size, &sendback_node)) | 908 | if (!sent_getnode_to_node(dht, packet + 1, source, plain + num_nodes * Node_format_size, &sendback_node)) |
977 | return 1; | 909 | return 1; |
978 | 910 | ||
979 | uint32_t i; | 911 | uint32_t i; |
980 | Node_format nodes_list[MAX_SENT_NODES]; | 912 | Node_format nodes_list[MAX_SENT_NODES]; |
981 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | 913 | memcpy(nodes_list, plain, num_nodes * sizeof(Node_format)); |
982 | 914 | ||
983 | addto_lists(dht, source, packet + 1); | 915 | addto_lists(dht, source, packet + 1); |
984 | 916 | ||