summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorMichael Rose <michael_rose@gmx.de>2013-08-15 11:24:40 +0200
committerMichael Rose <michael_rose@gmx.de>2013-08-15 11:24:40 +0200
commit52d3194e8714a5041503ba953cbe5dfce0cb2e9b (patch)
treedb11ae02180ffdeb3d40a2d8e002fc56248f5903 /core/DHT.c
parent5c7245fecdbb83cd6833e0fad3ea5a035d37a1e4 (diff)
parentbf6c04b2cafe0a132aa54c6134217a0f4369a054 (diff)
Merge official
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c74
1 files changed, 26 insertions, 48 deletions
diff --git a/core/DHT.c b/core/DHT.c
index d359076d..1d13aa73 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -65,17 +65,6 @@
65 65
66typedef struct { 66typedef struct {
67 uint8_t client_id[CLIENT_ID_SIZE]; 67 uint8_t client_id[CLIENT_ID_SIZE];
68 IP_Port ip_port;
69 uint64_t timestamp;
70 uint64_t last_pinged;
71
72 /* Returned by this node. Either our friend or us */
73 IP_Port ret_ip_port;
74 uint64_t ret_timestamp;
75} Client_data;
76
77typedef struct {
78 uint8_t client_id[CLIENT_ID_SIZE];
79 Client_data client_list[MAX_FRIEND_CLIENTS]; 68 Client_data client_list[MAX_FRIEND_CLIENTS];
80 69
81 /* time at which the last get_nodes request was sent. */ 70 /* time at which the last get_nodes request was sent. */
@@ -115,6 +104,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
115 104
116/*----------------------------------------------------------------------------------*/ 105/*----------------------------------------------------------------------------------*/
117 106
107
108Client_data * DHT_get_close_list(void)
109{
110 return close_clientlist;
111}
112
118/* Compares client_id1 and client_id2 with client_id 113/* Compares client_id1 and client_id2 with client_id
119 * return 0 if both are same distance 114 * return 0 if both are same distance
120 * return 1 if client_id1 is closer 115 * return 1 if client_id1 is closer
@@ -633,6 +628,8 @@ static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
633 628
634int DHT_addfriend(uint8_t * client_id) 629int DHT_addfriend(uint8_t * client_id)
635{ 630{
631 if(friend_number(client_id) != -1) /*Is friend already in DHT?*/
632 return 1;
636 Friend * temp; 633 Friend * temp;
637 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); 634 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
638 if (temp == NULL) 635 if (temp == NULL)
@@ -930,49 +927,30 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
930} 927}
931 928
932/* Handle a received ping request for */ 929/* Handle a received ping request for */
933static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length) 930static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length)
934{ 931{
935 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING 932 uint64_t ping_id;
936 || length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 933 memcpy(&ping_id, packet + 1, sizeof(uint64_t));
937 return 1;
938
939 /* check if request is for us. */
940 if (id_equal(packet + 1, self_public_key)) {
941 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
942 uint8_t data[MAX_DATA_SIZE];
943
944 int len = handle_request(public_key, data, packet, length);
945 if (len != sizeof(uint64_t) + 1)
946 return 1;
947
948 uint64_t ping_id;
949 memcpy(&ping_id, data + 1, sizeof(uint64_t));
950 934
951 int friendnumber = friend_number(public_key); 935 int friendnumber = friend_number(source_pubkey);
952 if (friendnumber == -1) 936 if (friendnumber == -1)
953 return 1; 937 return 1;
954 938
955 Friend * friend = &friends_list[friendnumber]; 939 Friend * friend = &friends_list[friendnumber];
956 940
957 if (data[0] == 0) { 941 if (packet[0] == 0) {
958 /* 1 is reply */ 942 /* 1 is reply */
959 send_NATping(public_key, ping_id, 1); 943 send_NATping(source_pubkey, ping_id, 1);
960 friend->recvNATping_timestamp = unix_time(); 944 friend->recvNATping_timestamp = unix_time();
945 return 0;
946 } else if (packet[0] == 1) {
947 if (friend->NATping_id == ping_id) {
948 friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
949 friend->hole_punching = 1;
961 return 0; 950 return 0;
962 } else if (data[0] == 1) {
963 if (friend->NATping_id == ping_id) {
964 friend->NATping_id = ((uint64_t)random_int() << 32) + random_int();
965 friend->hole_punching = 1;
966 return 0;
967 }
968 } 951 }
969 return 1;
970 } 952 }
971 953 return 1;
972 /* if request is not for us, try routing it. */
973 route_packet(packet + 1, packet, length);
974
975 return 0;
976} 954}
977 955
978/* Get the most common ip in the ip_portlist 956/* Get the most common ip in the ip_portlist
@@ -1080,7 +1058,7 @@ void DHT_init(void)
1080 networking_registerhandler(1, &handle_ping_response); 1058 networking_registerhandler(1, &handle_ping_response);
1081 networking_registerhandler(2, &handle_getnodes); 1059 networking_registerhandler(2, &handle_getnodes);
1082 networking_registerhandler(3, &handle_sendnodes); 1060 networking_registerhandler(3, &handle_sendnodes);
1083 networking_registerhandler(254, &handle_NATping); 1061 cryptopacket_registerhandler(254, &handle_NATping);
1084} 1062}
1085 1063
1086void doDHT(void) 1064void doDHT(void)