summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorJeffail <ash.jeffs@gmail.com>2013-08-02 10:53:28 +0100
committerJeffail <ash.jeffs@gmail.com>2013-08-02 10:53:28 +0100
commit1708dc1a884bcaa64ac2b95b2a9a2801f192b070 (patch)
tree535d5ce0204b611d320bc604a8b8896a8c0c7a47 /core/DHT.c
parentf9816b43f329045b1ea6e699f87f3e3b13a37d0d (diff)
Refactored DHT.c down to line 1048 (beginning of NAT)
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c145
1 files changed, 87 insertions, 58 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 53c0e9f9..b7d76d39 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -883,8 +883,9 @@ void doDHTFriends()
883static uint32_t close_lastgetnodes; 883static uint32_t close_lastgetnodes;
884 884
885/* Ping each client in the close nodes list every 60 seconds. 885/* Ping each client in the close nodes list every 60 seconds.
886 Send a get nodes request every 20 seconds to a random good node in the list. */ 886 * Send a get nodes request every 20 seconds to a random good node in the list.
887void doClose() /* tested */ 887 */
888void doClose()
888{ 889{
889 uint32_t i; 890 uint32_t i;
890 uint32_t temp_time = unix_time(); 891 uint32_t temp_time = unix_time();
@@ -892,25 +893,27 @@ void doClose() /* tested */
892 uint32_t rand_node; 893 uint32_t rand_node;
893 uint32_t index[LCLIENT_LIST]; 894 uint32_t index[LCLIENT_LIST];
894 895
895 for(i = 0; i < LCLIENT_LIST; ++i) 896 for (i = 0; i < LCLIENT_LIST; ++i) {
896 /* if node is not dead. */ 897 /* if node is not dead. */
897 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { 898 if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) {
898 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { 899 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
899 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); 900 pingreq( close_clientlist[i].ip_port,
901 close_clientlist[i].client_id );
900 close_clientlist[i].last_pinged = temp_time; 902 close_clientlist[i].last_pinged = temp_time;
901 } 903 }
902 /* if node is good. */ 904 /* if node is good. */
903 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { 905 if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) {
904 index[num_nodes] = i; 906 index[num_nodes] = i;
905 ++num_nodes; 907 ++num_nodes;
906 } 908 }
907 } 909 }
910 }
908 911
909 if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { 912 if (close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
910 rand_node = rand() % num_nodes; 913 rand_node = rand() % num_nodes;
911 getnodes(close_clientlist[index[rand_node]].ip_port, 914 getnodes( close_clientlist[index[rand_node]].ip_port,
912 close_clientlist[index[rand_node]].client_id, 915 close_clientlist[index[rand_node]].client_id,
913 self_public_key); 916 self_public_key );
914 close_lastgetnodes = temp_time; 917 close_lastgetnodes = temp_time;
915 } 918 }
916} 919}
@@ -921,100 +924,126 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
921} 924}
922 925
923/* send the given packet to node with client_id 926/* send the given packet to node with client_id
924 returns -1 if failure */ 927 * returns -1 if failure
928 */
925int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) 929int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
926{ 930{
927 uint32_t i; 931 uint32_t i;
928 for(i = 0; i < LCLIENT_LIST; ++i) 932 for (i = 0; i < LCLIENT_LIST; ++i) {
929 if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) 933 if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0)
930 return sendpacket(close_clientlist[i].ip_port, packet, length); 934 return sendpacket(close_clientlist[i].ip_port, packet, length);
935 }
931 return -1; 936 return -1;
932} 937}
933 938
934/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist 939/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist
935 ip_portlist must be at least MAX_FRIEND_CLIENTS big 940 * ip_portlist must be at least MAX_FRIEND_CLIENTS big
936 returns the number of ips returned 941 * returns the number of ips returned
937 return 0 if we are connected to friend or if no ips were found. 942 * return 0 if we are connected to friend or if no ips were found.
938 returns -1 if no such friend*/ 943 * returns -1 if no such friend
944 */
939static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) 945static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
940{ 946{
941 int num_ips = 0; 947 int num_ips = 0;
942 uint32_t i; 948 uint32_t i, temp_time = unix_time();
943 uint32_t temp_time = unix_time(); 949
944 if(friend_num >= num_friends) 950 if (friend_num >= num_friends)
945 return -1; 951 return -1;
946 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) 952
953 Friend * friend = &friends_list[friend_num];
954 Client_data * client;
955
956 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
957 client = &friend->client_list[i];
958
947 /*If ip is not zero and node is good */ 959 /*If ip is not zero and node is good */
948 if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && 960 if (client->ret_ip_port.ip.i != 0 &&
949 friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { 961 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
950 if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) 962
963 if (memcmp(client->client_id, friend->client_id, CLIENT_ID_SIZE) == 0)
951 return 0; 964 return 0;
952 ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; 965
966 ip_portlist[num_ips] = client->ret_ip_port;
953 ++num_ips; 967 ++num_ips;
954 } 968 }
969 }
955 return num_ips; 970 return num_ips;
956} 971}
957 972
958/* Send the following packet to everyone who tells us they are connected to friend_id 973/* Send the following packet to everyone who tells us they are connected to friend_id
959 returns the number of nodes it sent the packet to */ 974 * returns the number of nodes it sent the packet to
975 */
960int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 976int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
961{ 977{
962 uint32_t i, j; 978 int num = friend_number(friend_id);
963 uint32_t sent = 0; 979 if (num == -1)
964 uint32_t temp_time = unix_time(); 980 return 0;
965 for(i = 0; i < num_friends; ++i) 981
966 /* Equal */ 982 uint32_t i, sent = 0, temp_time = unix_time();
967 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { 983 Friend * friend = &friends_list[num];
968 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 984 Client_data * client;
969 /*If ip is not zero and node is good */ 985
970 if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && 986 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
971 friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) 987 client = &friend->client_list[i];
972 if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) 988
973 ++sent; 989 /*If ip is not zero and node is good */
974 return sent; 990 if (client->ret_ip_port.ip.i != 0 &&
991 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
992
993 if (sendpacket(client->ip_port, packet, length) == length)
994 ++sent;
975 } 995 }
976 return 0; 996 }
997 return sent;
977} 998}
978 999
979/* Send the following packet to one random person who tells us they are connected to friend_id 1000/* Send the following packet to one random person who tells us they are connected to friend_id
980 returns the number of nodes it sent the packet to */ 1001* returns the number of nodes it sent the packet to
1002*/
981int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) 1003int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
982{ 1004{
983 int num = friend_number(friend_id); 1005 int num = friend_number(friend_id);
984 if(num == -1) 1006 if (num == -1)
985 return 0; 1007 return 0;
986 1008
1009 Friend * friend = &friends_list[num];
1010 Client_data * client;
1011
987 IP_Port ip_list[MAX_FRIEND_CLIENTS]; 1012 IP_Port ip_list[MAX_FRIEND_CLIENTS];
988 int n = 0; 1013 int n = 0;
989 uint32_t i; 1014 uint32_t i, temp_time = unix_time();
990 uint32_t temp_time = unix_time(); 1015
991 for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) 1016 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1017 client = &friend->client_list[i];
1018
992 /*If ip is not zero and node is good */ 1019 /*If ip is not zero and node is good */
993 if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && 1020 if(client->ret_ip_port.ip.i != 0 &&
994 friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { 1021 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
995 ip_list[n] = friends_list[num].client_list[i].ip_port; 1022 ip_list[n] = client->ip_port;
996 ++n; 1023 ++n;
997 } 1024 }
998 if(n < 1) 1025 }
1026 if (n < 1)
999 return 0; 1027 return 0;
1000 if(sendpacket(ip_list[rand() % n], packet, length) == length) 1028 if (sendpacket(ip_list[rand() % n], packet, length) == length)
1001 return 1; 1029 return 1;
1002 return 0; 1030 return 0;
1003} 1031}
1004 1032
1005/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist 1033/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
1006 ip_portlist must be at least MAX_FRIEND_CLIENTS big 1034 * ip_portlist must be at least MAX_FRIEND_CLIENTS big
1007 returns the number of ips returned 1035 * returns the number of ips returned
1008 return 0 if we are connected to friend or if no ips were found. 1036 * return 0 if we are connected to friend or if no ips were found.
1009 returns -1 if no such friend*/ 1037 * returns -1 if no such friend
1038 */
1010int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) 1039int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
1011{ 1040{
1012
1013 uint32_t i; 1041 uint32_t i;
1014 for(i = 0; i < num_friends; ++i) 1042 for (i = 0; i < num_friends; ++i) {
1015 /* Equal */ 1043 /* Equal */
1016 if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) 1044 if (memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0)
1017 return friend_iplist(ip_portlist, i); 1045 return friend_iplist(ip_portlist, i);
1046 }
1018 return -1; 1047 return -1;
1019} 1048}
1020 1049