summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c55
1 files changed, 18 insertions, 37 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 5eae147c..057ee9ce 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -932,49 +932,30 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
932} 932}
933 933
934/* Handle a received ping request for */ 934/* Handle a received ping request for */
935static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length) 935static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
936{ 936{
937 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING 937 uint64_t ping_id;
938 || length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 938 memcpy(&ping_id, packet + 1, sizeof(uint64_t));
939 return 1;
940
941 /* check if request is for us. */
942 if (id_equal(packet + 1, self_public_key)) {
943 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
944 uint8_t data[MAX_DATA_SIZE];
945
946 int len = handle_request(public_key, data, packet, length);
947 if (len != sizeof(uint64_t) + 1)
948 return 1;
949
950 uint64_t ping_id;
951 memcpy(&ping_id, data + 1, sizeof(uint64_t));
952 939
953 int friendnumber = friend_number(public_key); 940 int friendnumber = friend_number(source_pubkey);
954 if (friendnumber == -1) 941 if (friendnumber == -1)
955 return 1; 942 return 1;
956 943
957 Friend * friend = &friends_list[friendnumber]; 944 Friend * friend = &friends_list[friendnumber];
958 945
959 if (data[0] == 0) { 946 if (packet[0] == 0) {
960 /* 1 is reply */ 947 /* 1 is reply */
961 send_NATping(public_key, ping_id, 1); 948 send_NATping(source_pubkey, ping_id, 1);
962 friend->recvNATping_timestamp = unix_time(); 949 friend->recvNATping_timestamp = unix_time();
950 return 0;
951 } else if (packet[0] == 1) {
952 if (friend->NATping_id == ping_id) {
953 friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
954 friend->hole_punching = 1;
963 return 0; 955 return 0;
964 } else if (data[0] == 1) {
965 if (friend->NATping_id == ping_id) {
966 friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
967 friend->hole_punching = 1;
968 return 0;
969 }
970 } 956 }
971 return 1;
972 } 957 }
973 958 return 1;
974 /* if request is not for us, try routing it. */
975 route_packet(packet + 1, packet, length);
976
977 return 0;
978} 959}
979 960
980/* Get the most common ip in the ip_portlist 961/* Get the most common ip in the ip_portlist
@@ -1082,7 +1063,7 @@ void DHT_init(void)
1082 networking_registerhandler(1, &handle_ping_response); 1063 networking_registerhandler(1, &handle_ping_response);
1083 networking_registerhandler(2, &handle_getnodes); 1064 networking_registerhandler(2, &handle_getnodes);
1084 networking_registerhandler(3, &handle_sendnodes); 1065 networking_registerhandler(3, &handle_sendnodes);
1085 networking_registerhandler(254, &handle_NATping); 1066 cryptopacket_registerhandler(254, &handle_NATping);
1086} 1067}
1087 1068
1088void doDHT(void) 1069void doDHT(void)