diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 60 | ||||
-rw-r--r-- | core/DHT.h | 10 | ||||
-rw-r--r-- | core/ping.c | 2 |
3 files changed, 70 insertions, 2 deletions
@@ -61,6 +61,10 @@ | |||
61 | /*Interval in seconds between punching attempts*/ | 61 | /*Interval in seconds between punching attempts*/ |
62 | #define PUNCH_INTERVAL 10 | 62 | #define PUNCH_INTERVAL 10 |
63 | 63 | ||
64 | /*Maximum newly announced nodes to ping per TIME_TOPING seconds*/ | ||
65 | #define MAX_TOPING 16 | ||
66 | |||
67 | #define TIME_TOPING 5 | ||
64 | /*----------------------------------------------------------------------------------*/ | 68 | /*----------------------------------------------------------------------------------*/ |
65 | 69 | ||
66 | typedef struct { | 70 | typedef struct { |
@@ -101,6 +105,8 @@ static Client_data close_clientlist[LCLIENT_LIST]; | |||
101 | static Friend * friends_list; | 105 | static Friend * friends_list; |
102 | static uint16_t num_friends; | 106 | static uint16_t num_friends; |
103 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | 107 | static Pinged send_nodes[LSEND_NODES_ARRAY]; |
108 | static Node_format toping[MAX_TOPING]; | ||
109 | static uint64_t last_toping; | ||
104 | 110 | ||
105 | /*----------------------------------------------------------------------------------*/ | 111 | /*----------------------------------------------------------------------------------*/ |
106 | 112 | ||
@@ -599,7 +605,7 @@ static int handle_getnodes(IP_Port source, uint8_t * packet, uint32_t length) | |||
599 | memcpy(&ping_id, plain, sizeof(ping_id)); | 605 | memcpy(&ping_id, plain, sizeof(ping_id)); |
600 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); | 606 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); |
601 | 607 | ||
602 | send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ | 608 | //send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ |
603 | 609 | ||
604 | return 0; | 610 | return 0; |
605 | } | 611 | } |
@@ -1075,6 +1081,57 @@ static void doNAT(void) | |||
1075 | /*----------------------------------------------------------------------------------*/ | 1081 | /*----------------------------------------------------------------------------------*/ |
1076 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ | 1082 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ |
1077 | 1083 | ||
1084 | |||
1085 | /* Add nodes to the toping list | ||
1086 | all nodes in this list are pinged every TIME_TOPING seconds | ||
1087 | and are then removed from the list. | ||
1088 | if the list is full the nodes farthest from our client_id are replaced | ||
1089 | the purpose of this list is to enable quick integration of new nodes into the | ||
1090 | network while preventing amplification attacks. | ||
1091 | return 0 if node was added | ||
1092 | return -1 if node was not added */ | ||
1093 | int add_toping(uint8_t *client_id, IP_Port ip_port) | ||
1094 | { | ||
1095 | if (ip_port.ip.i == 0) | ||
1096 | return -1; | ||
1097 | uint32_t i; | ||
1098 | for (i = 0; i < MAX_TOPING; ++i) { | ||
1099 | if (toping[i].ip_port.ip.i == 0) { | ||
1100 | memcpy(toping[i].client_id, client_id, CLIENT_ID_SIZE); | ||
1101 | toping[i].ip_port.ip.i = ip_port.ip.i; | ||
1102 | toping[i].ip_port.port = ip_port.port; | ||
1103 | return 0; | ||
1104 | } | ||
1105 | } | ||
1106 | for (i = 0; i < MAX_TOPING; ++i) { | ||
1107 | if (id_closest(self_public_key, toping[i].client_id, client_id) == 2) { | ||
1108 | memcpy(toping[i].client_id, client_id, CLIENT_ID_SIZE); | ||
1109 | toping[i].ip_port.ip.i = ip_port.ip.i; | ||
1110 | toping[i].ip_port.port = ip_port.port; | ||
1111 | return 0; | ||
1112 | } | ||
1113 | } | ||
1114 | return -1; | ||
1115 | } | ||
1116 | |||
1117 | /*Ping all the valid nodes in the toping list every TIME_TOPING seconds | ||
1118 | this function must be run at least once every TIME_TOPING seconds*/ | ||
1119 | static void do_toping() | ||
1120 | { | ||
1121 | uint64_t temp_time = unix_time(); | ||
1122 | if (!is_timeout(temp_time, last_toping, TIME_TOPING)) | ||
1123 | return; | ||
1124 | last_toping = temp_time; | ||
1125 | uint32_t i; | ||
1126 | for (i = 0; i < MAX_TOPING; ++i) { | ||
1127 | if (toping[i].ip_port.ip.i == 0) | ||
1128 | return; | ||
1129 | send_ping_request(toping[i].ip_port, (clientid_t *) toping[i].client_id); | ||
1130 | toping[i].ip_port.ip.i = 0; | ||
1131 | } | ||
1132 | } | ||
1133 | |||
1134 | |||
1078 | void DHT_init(void) | 1135 | void DHT_init(void) |
1079 | { | 1136 | { |
1080 | networking_registerhandler(0, &handle_ping_request); | 1137 | networking_registerhandler(0, &handle_ping_request); |
@@ -1089,6 +1146,7 @@ void doDHT(void) | |||
1089 | doClose(); | 1146 | doClose(); |
1090 | doDHTFriends(); | 1147 | doDHTFriends(); |
1091 | doNAT(); | 1148 | doNAT(); |
1149 | do_toping(); | ||
1092 | } | 1150 | } |
1093 | 1151 | ||
1094 | /* get the size of the DHT (for saving) */ | 1152 | /* get the size of the DHT (for saving) */ |
@@ -75,6 +75,16 @@ void doDHT(void); | |||
75 | Sends a get nodes request to the given node with ip port and public_key */ | 75 | Sends a get nodes request to the given node with ip port and public_key */ |
76 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); | 76 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); |
77 | 77 | ||
78 | /* Add nodes to the toping list | ||
79 | all nodes in this list are pinged every TIME_TOPING seconds | ||
80 | and are then removed from the list. | ||
81 | if the list is full the nodes farthest from our client_id are replaced | ||
82 | the purpose of this list is to enable quick integration of new nodes into the | ||
83 | network while preventing amplification attacks. | ||
84 | return 0 if node was added | ||
85 | return -1 if node was not added */ | ||
86 | int add_toping(uint8_t *client_id, IP_Port ip_port); | ||
87 | |||
78 | /* ROUTING FUNCTIONS */ | 88 | /* ROUTING FUNCTIONS */ |
79 | 89 | ||
80 | /* send the given packet to node with client_id | 90 | /* send the given packet to node with client_id |
diff --git a/core/ping.c b/core/ping.c index 24ece06a..ee05d9fa 100644 --- a/core/ping.c +++ b/core/ping.c | |||
@@ -185,7 +185,7 @@ int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length) | |||
185 | 185 | ||
186 | // Send response | 186 | // Send response |
187 | send_ping_response(source, &p->client_id, ping_id); | 187 | send_ping_response(source, &p->client_id, ping_id); |
188 | send_ping_request(source, &p->client_id); // Make this smarter? | 188 | add_toping((uint8_t*) &p->client_id, source); |
189 | 189 | ||
190 | return 0; | 190 | return 0; |
191 | } | 191 | } |