summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Lossless_UDP.c222
1 files changed, 63 insertions, 159 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 717882c1..db0fe201 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -38,15 +38,6 @@
38 */ 38 */
39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) 39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
40{ 40{
41 /*vx-t uint32_t i;
42
43 for (i = 0; i < ludp->connections_length; ++i) {
44 if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i &&
45 ludp->connections[i].ip_port.port == ip_port.port &&
46 ludp->connections[i].status > 0)
47 return i;
48 }*/
49
50 tox_array_for_each(&ludp->connections, Connection, tmp) { 41 tox_array_for_each(&ludp->connections, Connection, tmp) {
51 if (tmp.ip_port.ip.i == ip_port.ip.i && 42 if (tmp.ip_port.ip.i == ip_port.ip.i &&
52 tmp.ip_port.port == ip_port.port && 43 tmp.ip_port.port == ip_port.port &&
@@ -107,79 +98,43 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
107 if (connect != -1) 98 if (connect != -1)
108 return connect; 99 return connect;
109 100
110 /* TODO: this allocates space regardless of whether it's needed or not 101 uint32_t connection_id = -1;
111 * Rewrite so it will first scan for empty, and if not found make a new one.
112 */
113 tox_array_push_ptr(&ludp->connections, 0);
114 memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
115 /*vx-t if (ludp->connections_number == ludp->connections_length) {
116 Connection *temp;
117 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
118
119 if (temp == NULL)
120 return -1;
121
122 memset(&temp[ludp->connections_length], 0, sizeof(Connection));
123 ++ludp->connections_length;
124 ludp->connections = temp;
125 }*/
126
127 tox_array_for_each(&ludp->connections, Connection, tmp) { 102 tox_array_for_each(&ludp->connections, Connection, tmp) {
128 if (tmp.status == 0) { 103 if (tmp.status == 0) {
129 memset(&tox_array_get(&ludp->connections, tmp_i, Connection), 0, sizeof(Connection)); 104 connection_id = tmp_i;
130 uint32_t handshake_id1 = handshake_id(ludp, ip_port); 105 break;
131
132 tox_array_get(&ludp->connections, tmp_i, Connection) = (Connection) {
133 .ip_port = ip_port,
134 .status = 1,
135 .inbound = 0,
136 .handshake_id1 = handshake_id1,
137 .sent_packetnum = handshake_id1,
138 .sendbuff_packetnum = handshake_id1,
139 .successful_sent = handshake_id1,
140 .SYNC_rate = SYNC_RATE,
141 .data_rate = DATA_SYNC_RATE,
142 .last_recvSYNC = current_time(),
143 .last_sent = current_time(),
144 .killat = ~0,
145 .send_counter = 0,
146 /* add randomness to timeout to prevent connections getting stuck in a loop. */
147 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
148 };
149 return tmp_i;
150 } 106 }
151 } 107 }
152 /*vx-t uint32_t i;
153
154 for (i = 0; i < ludp->connections_length; ++i) {
155 if (ludp->connections[i].status == 0) {
156 memset(&ludp->connections[i], 0, sizeof(Connection));
157 uint32_t handshake_id1 = handshake_id(ludp, ip_port);
158
159 ludp->connections[i] = (Connection) {
160 .ip_port = ip_port,
161 .status = 1,
162 .inbound = 0,
163 .handshake_id1 = handshake_id1,
164 .sent_packetnum = handshake_id1,
165 .sendbuff_packetnum = handshake_id1,
166 .successful_sent = handshake_id1,
167 .SYNC_rate = SYNC_RATE,
168 .data_rate = DATA_SYNC_RATE,
169 .last_recvSYNC = current_time(),
170 .last_sent = current_time(),
171 .killat = ~0,
172 .send_counter = 0,
173 /* add randomness to timeout to prevent connections getting stuck in a loop. /
174 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
175 };
176 ++ludp->connections_number;
177
178 return i;
179 }
180 }*/
181 108
182 return -1; 109 if (connection_id == -1) {
110 tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */
111 connection_id = ludp->connections.len-1;
112 }
113 Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection);
114
115 memset(connection, 0, sizeof(Connection));
116
117 uint32_t handshake_id1 = handshake_id(ludp, ip_port);
118
119 *connection = (Connection) {
120 .ip_port = ip_port,
121 .status = 1,
122 .inbound = 0,
123 .handshake_id1 = handshake_id1,
124 .sent_packetnum = handshake_id1,
125 .sendbuff_packetnum = handshake_id1,
126 .successful_sent = handshake_id1,
127 .SYNC_rate = SYNC_RATE,
128 .data_rate = DATA_SYNC_RATE,
129 .last_recvSYNC = current_time(),
130 .last_sent = current_time(),
131 .killat = ~0,
132 .send_counter = 0,
133 /* add randomness to timeout to prevent connections getting stuck in a loop. */
134 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
135 };
136
137 return connection_id;
183} 138}
184 139
185/* 140/*
@@ -192,51 +147,41 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
192 if (getconnection_id(ludp, ip_port) != -1) 147 if (getconnection_id(ludp, ip_port) != -1)
193 return -1; 148 return -1;
194 149
195 /* TODO: See comment in int new_connection(Lossless_UDP *ludp, IP_Port ip_port) */ 150 uint32_t connection_id = -1;
196 tox_array_push_ptr(&ludp->connections, 0); 151 tox_array_for_each(&ludp->connections, Connection, tmp) {
197 memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection)); 152 if (tmp.status == 0) {
153 connection_id = tmp_i;
154 break;
155 }
156 }
198 157
199 /*vx-t if (ludp->connections_number == ludp->connections_length) { 158 if (connection_id == -1) {
200 Connection *temp; 159 tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */
201 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1)); 160 connection_id = ludp->connections.len-1;
161 }
162 Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection);
163 memset(connection, 0, sizeof(Connection));
202 164
203 if (temp == NULL) 165 uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
204 return -1;
205 166
206 memset(&temp[ludp->connections_length], 0, sizeof(Connection)); 167 *connection = (Connection) {
207 ++ludp->connections_length; 168 .ip_port = ip_port,
208 ludp->connections = temp; 169 .status = 2,
209 }*/ 170 .inbound = 2,
210/* vx-k: IMPORTANT!!!!!!!!! DO THIS 171 .SYNC_rate = SYNC_RATE,
211 uint32_t i; 172 .data_rate = DATA_SYNC_RATE,
173 .last_recvSYNC = current_time(),
174 .last_sent = current_time(),
175 .send_counter = 127,
212 176
213 for (i = 0; i < ludp->connections_length; ++i) { 177 /* add randomness to timeout to prevent connections getting stuck in a loop. */
214 if (ludp->connections[i].status == 0) { 178 .timeout = timeout,
215 memset(&ludp->connections[i], 0, sizeof(Connection)); 179
216 uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; 180 /* if this connection isn't handled within the timeout kill it. */
217 181 .killat = current_time() + 1000000UL * timeout
218 ludp->connections[i] = (Connection) { 182 };
219 .ip_port = ip_port, 183
220 .status = 2, 184 return connection_id;
221 .inbound = 2,
222 .SYNC_rate = SYNC_RATE,
223 .data_rate = DATA_SYNC_RATE,
224 .last_recvSYNC = current_time(),
225 .last_sent = current_time(),
226 .send_counter = 127,
227
228 /* add randomness to timeout to prevent connections getting stuck in a loop. /
229 .timeout = timeout,
230
231 /* if this connection isn't handled within the timeout kill it. /
232 .killat = current_time() + 1000000UL * timeout
233 };
234 ++ludp->connections_number;
235 return i;
236 }
237 }
238*/
239 return -1;
240} 185}
241 186
242/* 187/*
@@ -252,49 +197,9 @@ int incoming_connection(Lossless_UDP *ludp)
252 } 197 }
253 } 198 }
254 199
255 /*vx-k uint32_t i;
256
257 for (i = 0; i < ludp->connections_length; ++i) {
258 if (ludp->connections[i].inbound == 2) {
259 ludp->connections[i].inbound = 1;
260 return i;
261 }
262 }*/
263
264 return -1; 200 return -1;
265} 201}
266 202
267/* Try to free some memory from the connections array. */
268static void free_connections(Lossless_UDP *ludp)
269{
270 /*vx-k: fix this to work with new tox_array
271 uint32_t i;
272
273 for (i = ludp->connections_length; i != 0; --i)
274 if (ludp->connections[i - 1].status != 0)
275 break;
276
277 if (ludp->connections_length == i)
278 return;
279
280 if (i == 0) {
281 free(ludp->connections);
282 ludp->connections = NULL;
283 ludp->connections_length = i;
284 return;
285 }
286
287 Connection *temp;
288 temp = realloc(ludp->connections, sizeof(Connection) * i);
289
290 if (temp == NULL && i != 0)
291 return;
292
293 ludp->connections = temp;
294 ludp->connections_length = i;
295 */
296}
297
298/* 203/*
299 * Return -1 if it could not kill the connection. 204 * Return -1 if it could not kill the connection.
300 * Return 0 if killed successfully 205 * Return 0 if killed successfully
@@ -306,7 +211,6 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
306 tox_array_get(&ludp->connections, connection_id, Connection).status = 0; 211 tox_array_get(&ludp->connections, connection_id, Connection).status = 0;
307 change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port); 212 change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port);
308 tox_array_pop(&ludp->connections, 0); 213 tox_array_pop(&ludp->connections, 0);
309 free_connections(ludp);
310 return 0; 214 return 0;
311 } 215 }
312 } 216 }