diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-01 17:35:31 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-01 17:35:31 -0700 |
commit | 368ba8ed01cb7ddf2596884cb5b501cbb4b434d9 (patch) | |
tree | 5bf1a6ed2554bfa602fd1f1b2fb921db87dc9d47 /core | |
parent | e3c47d90b95f580933a496c562fba4a5e346b4e2 (diff) | |
parent | 4f2146c08580e250ec7d28fc8c687e277f104c00 (diff) |
Merge pull request #247 from Jeffail/master
Refactored most of DHT.c, added local vim config
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 150 |
1 files changed, 81 insertions, 69 deletions
@@ -21,67 +21,98 @@ | |||
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include "DHT.h" | 24 | /*----------------------------------------------------------------------------------*/ |
25 | 25 | ||
26 | typedef struct { | 26 | #include "DHT.h" |
27 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
28 | IP_Port ip_port; | ||
29 | uint32_t timestamp; | ||
30 | uint32_t last_pinged; | ||
31 | IP_Port ret_ip_port;/* The ip_port returned by this node for the friend | ||
32 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ | ||
33 | uint32_t ret_timestamp; | ||
34 | } Client_data; | ||
35 | 27 | ||
36 | /* maximum number of clients stored per friend. */ | 28 | /* maximum number of clients stored per friend. */ |
37 | #define MAX_FRIEND_CLIENTS 8 | 29 | #define MAX_FRIEND_CLIENTS 8 |
38 | 30 | ||
39 | typedef struct { | 31 | /* A list of the clients mathematically closest to ours. */ |
40 | uint8_t client_id[CLIENT_ID_SIZE]; | 32 | #define LCLIENT_LIST 32 |
41 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 33 | |
42 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ | 34 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ |
43 | 35 | #define LPING_ARRAY 256 | |
44 | /*Symetric NAT hole punching stuff*/ | 36 | |
45 | uint8_t hole_punching; /*0 if not hole punching, 1 if currently hole punching */ | 37 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 |
46 | uint32_t punching_index; | 38 | |
47 | uint32_t punching_timestamp; | 39 | /* the number of seconds for a non responsive node to become bad. */ |
48 | uint32_t recvNATping_timestamp; | 40 | #define BAD_NODE_TIMEOUT 70 |
49 | uint64_t NATping_id; | 41 | |
50 | uint32_t NATping_timestamp; | 42 | /* the max number of nodes to send with send nodes. */ |
51 | } Friend; | 43 | #define MAX_SENT_NODES 8 |
44 | |||
45 | /* ping timeout in seconds */ | ||
46 | #define PING_TIMEOUT 5 | ||
47 | |||
48 | /* The timeout after which a node is discarded completely. */ | ||
49 | #define Kill_NODE_TIMEOUT 300 | ||
50 | |||
51 | /* ping interval in seconds for each node in our lists. */ | ||
52 | #define PING_INTERVAL 60 | ||
53 | |||
54 | /* ping interval in seconds for each random sending of a get nodes request. */ | ||
55 | #define GET_NODE_INTERVAL 10 | ||
56 | |||
57 | #define MAX_PUNCHING_PORTS 32 | ||
58 | |||
59 | /*Interval in seconds between punching attempts*/ | ||
60 | #define PUNCH_INTERVAL 10 | ||
61 | |||
62 | /*----------------------------------------------------------------------------------*/ | ||
52 | 63 | ||
53 | typedef struct { | 64 | typedef struct { |
54 | uint8_t client_id[CLIENT_ID_SIZE]; | 65 | uint8_t client_id[CLIENT_ID_SIZE]; |
55 | IP_Port ip_port; | 66 | IP_Port ip_port; |
56 | } Node_format; | 67 | uint32_t timestamp; |
68 | uint32_t last_pinged; | ||
69 | |||
70 | /* Returned by this node. Either our friend or us */ | ||
71 | IP_Port ret_ip_port; | ||
72 | uint32_t ret_timestamp; | ||
73 | } Client_data; | ||
57 | 74 | ||
58 | typedef struct { | 75 | typedef struct { |
59 | IP_Port ip_port; | 76 | uint8_t client_id[CLIENT_ID_SIZE]; |
60 | uint64_t ping_id; | 77 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
61 | uint32_t timestamp; | ||
62 | 78 | ||
63 | } Pinged; | 79 | /* time at which the last get_nodes request was sent. */ |
80 | uint32_t lastgetnode; | ||
64 | 81 | ||
65 | /* Our client id/public key */ | 82 | /* Symetric NAT hole punching stuff */ |
66 | uint8_t self_public_key[CLIENT_ID_SIZE]; | ||
67 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | ||
68 | 83 | ||
69 | /* TODO: Move these out of here and put them into the .c file. | 84 | /* 1 if currently hole punching, otherwise 0 */ |
70 | A list of the clients mathematically closest to ours. */ | 85 | uint8_t hole_punching; |
71 | #define LCLIENT_LIST 32 | 86 | uint32_t punching_index; |
72 | static Client_data close_clientlist[LCLIENT_LIST]; | 87 | uint32_t punching_timestamp; |
88 | uint32_t recvNATping_timestamp; | ||
89 | uint64_t NATping_id; | ||
90 | uint32_t NATping_timestamp; | ||
91 | } Friend; | ||
73 | 92 | ||
74 | static Friend * friends_list; | 93 | typedef struct { |
75 | static uint16_t num_friends; | 94 | uint8_t client_id[CLIENT_ID_SIZE]; |
95 | IP_Port ip_port; | ||
96 | } Node_format; | ||
76 | 97 | ||
77 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | 98 | typedef struct { |
78 | #define LPING_ARRAY 256 | 99 | IP_Port ip_port; |
100 | uint64_t ping_id; | ||
101 | uint32_t timestamp; | ||
102 | } Pinged; | ||
79 | 103 | ||
80 | static Pinged pings[LPING_ARRAY]; | 104 | /*----------------------------------------------------------------------------------*/ |
81 | 105 | ||
82 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | 106 | /* Our client id/public key */ |
107 | uint8_t self_public_key[CLIENT_ID_SIZE]; | ||
108 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | ||
109 | static Client_data close_clientlist[LCLIENT_LIST]; | ||
110 | static Friend * friends_list; | ||
111 | static uint16_t num_friends; | ||
112 | static Pinged pings[LPING_ARRAY]; | ||
113 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | ||
83 | 114 | ||
84 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | 115 | /*----------------------------------------------------------------------------------*/ |
85 | 116 | ||
86 | /* Compares client_id1 and client_id2 with client_id | 117 | /* Compares client_id1 and client_id2 with client_id |
87 | return 0 if both are same distance | 118 | return 0 if both are same distance |
@@ -150,11 +181,6 @@ static int friend_number(uint8_t * client_id) | |||
150 | return -1; | 181 | return -1; |
151 | } | 182 | } |
152 | 183 | ||
153 | /* the number of seconds for a non responsive node to become bad. */ | ||
154 | #define BAD_NODE_TIMEOUT 70 | ||
155 | /* the max number of nodes to send with send nodes. */ | ||
156 | #define MAX_SENT_NODES 8 | ||
157 | |||
158 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: | 184 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: |
159 | put them in the nodes_list and return how many were found. | 185 | put them in the nodes_list and return how many were found. |
160 | TODO: Make this function much more efficient. */ | 186 | TODO: Make this function much more efficient. */ |
@@ -281,9 +307,6 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
281 | } | 307 | } |
282 | } | 308 | } |
283 | 309 | ||
284 | /* ping timeout in seconds */ | ||
285 | #define PING_TIMEOUT 5 | ||
286 | |||
287 | /* check if we are currently pinging an ip_port and/or a ping_id | 310 | /* check if we are currently pinging an ip_port and/or a ping_id |
288 | variables with values of zero will not be checked. | 311 | variables with values of zero will not be checked. |
289 | if we are already, return 1 | 312 | if we are already, return 1 |
@@ -619,7 +642,8 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
619 | return 0; | 642 | return 0; |
620 | } | 643 | } |
621 | 644 | ||
622 | /* END of packet handling functions */ | 645 | /*----------------------------------------------------------------------------------*/ |
646 | /*------------------------END of packet handling functions--------------------------*/ | ||
623 | 647 | ||
624 | int DHT_addfriend(uint8_t * client_id) | 648 | int DHT_addfriend(uint8_t * client_id) |
625 | { | 649 | { |
@@ -676,15 +700,6 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
676 | 700 | ||
677 | } | 701 | } |
678 | 702 | ||
679 | /* The timeout after which a node is discarded completely. */ | ||
680 | #define Kill_NODE_TIMEOUT 300 | ||
681 | |||
682 | /* ping interval in seconds for each node in our lists. */ | ||
683 | #define PING_INTERVAL 60 | ||
684 | |||
685 | /* ping interval in seconds for each random sending of a get nodes request. */ | ||
686 | #define GET_NODE_INTERVAL 10 | ||
687 | |||
688 | /* Ping each client in the "friends" list every 60 seconds. | 703 | /* Ping each client in the "friends" list every 60 seconds. |
689 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ | 704 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ |
690 | void doDHTFriends() | 705 | void doDHTFriends() |
@@ -855,7 +870,8 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) | |||
855 | return -1; | 870 | return -1; |
856 | } | 871 | } |
857 | 872 | ||
858 | /*BEGINNING OF NAT PUNCHING FUNCTIONS*/ | 873 | /*----------------------------------------------------------------------------------*/ |
874 | /*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ | ||
859 | 875 | ||
860 | int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) | 876 | int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) |
861 | { | 877 | { |
@@ -955,8 +971,6 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ | |||
955 | return num; | 971 | return num; |
956 | } | 972 | } |
957 | 973 | ||
958 | #define MAX_PUNCHING_PORTS 32 | ||
959 | |||
960 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) | 974 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) |
961 | { | 975 | { |
962 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) | 976 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) |
@@ -972,9 +986,6 @@ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t | |||
972 | friends_list[friend_num].punching_index = i; | 986 | friends_list[friend_num].punching_index = i; |
973 | } | 987 | } |
974 | 988 | ||
975 | /*Interval in seconds between punching attempts*/ | ||
976 | #define PUNCH_INTERVAL 10 | ||
977 | |||
978 | static void doNAT() | 989 | static void doNAT() |
979 | { | 990 | { |
980 | uint32_t i; | 991 | uint32_t i; |
@@ -1008,7 +1019,8 @@ static void doNAT() | |||
1008 | } | 1019 | } |
1009 | } | 1020 | } |
1010 | 1021 | ||
1011 | /*END OF NAT PUNCHING FUNCTIONS*/ | 1022 | /*----------------------------------------------------------------------------------*/ |
1023 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ | ||
1012 | 1024 | ||
1013 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 1025 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
1014 | { | 1026 | { |