summaryrefslogtreecommitdiff
path: root/toxcore/Lossless_UDP.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Lossless_UDP.c')
-rw-r--r--toxcore/Lossless_UDP.c58
1 files changed, 45 insertions, 13 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index fbb473e7..ca874562 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -44,9 +44,7 @@
44int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) 44int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
45{ 45{
46 tox_array_for_each(&ludp->connections, Connection, tmp) { 46 tox_array_for_each(&ludp->connections, Connection, tmp) {
47 if (tmp->ip_port.ip.uint32 == ip_port.ip.uint32 && 47 if (tmp-> status > 0 && ipport_equal(&tmp->ip_port, &ip_port)) {
48 tmp->ip_port.port == ip_port.port &&
49 tmp->status > 0) {
50 return tmp_i; 48 return tmp_i;
51 } 49 }
52 } 50 }
@@ -61,17 +59,49 @@ int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
61 * 59 *
62 * TODO: make this better 60 * TODO: make this better
63 */ 61 */
64static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) 62
63static uint8_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value)
65{ 64{
66 uint32_t id = 0, i; 65 if (ludp->randtable[index][value] == 0)
66 ludp->randtable[index][value] = random_int();
67 67
68 for (i = 0; i < 6; ++i) { 68 return ludp->randtable[index][value];
69 if (ludp->randtable[i][source.uint8[i]] == 0) 69}
70 ludp->randtable[i][source.uint8[i]] = random_int();
71 70
72 id ^= ludp->randtable[i][source.uint8[i]]; 71static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
72{
73 uint32_t id = 0, i = 0;
74
75 uint8_t *uint8;
76 uint8 = (uint8_t *)&source.port;
77 id ^= randtable_initget(ludp, i, *uint8);
78 i++, uint8++;
79 id ^= randtable_initget(ludp, i, *uint8);
80 i++;
81
82#ifdef TOX_ENABLE_IPV6
83 if (source.ip.family == AF_INET)
84 {
85 IP4 ip4 = source.ip.ip4;
86#else
87 IP4 ip4 = source.ip;
88#endif
89 int k;
90 for (k = 0; k < 4; k++) {
91 id ^= randtable_initget(ludp, i++, ip4.uint8[k]);
92 }
93#ifdef TOX_ENABLE_IPV6
73 } 94 }
74 95
96 if (source.ip.family == AF_INET6)
97 {
98 int k;
99 for (k = 0; k < 16; k++) {
100 id ^= randtable_initget(ludp, i++, source.ip.ip6.s6_addr[k]);
101 }
102 }
103#endif
104
75 /* id can't be zero. */ 105 /* id can't be zero. */
76 if (id == 0) 106 if (id == 0)
77 id = 1; 107 id = 1;
@@ -290,7 +320,9 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
290 if ((unsigned int)connection_id < ludp->connections.len) 320 if ((unsigned int)connection_id < ludp->connections.len)
291 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 321 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
292 322
293 IP_Port zero = {{{{0}}, 0, 0}}; 323 IP_Port zero;
324 ip_reset(&zero.ip);
325 zero.port = 0;
294 return zero; 326 return zero;
295} 327}
296 328
@@ -429,7 +461,7 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak
429 temp = htonl(handshake_id2); 461 temp = htonl(handshake_id2);
430 memcpy(packet + 5, &temp, 4); 462 memcpy(packet + 5, &temp, 4);
431 463
432 return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); 464 return sendpacket(ludp->net, ip_port, packet, sizeof(packet));
433} 465}
434 466
435static int send_SYNC(Lossless_UDP *ludp, int connection_id) 467static int send_SYNC(Lossless_UDP *ludp, int connection_id)
@@ -456,7 +488,7 @@ static int send_SYNC(Lossless_UDP *ludp, int connection_id)
456 index += 4; 488 index += 4;
457 memcpy(packet + index, requested, 4 * number); 489 memcpy(packet + index, requested, 4 * number);
458 490
459 return sendpacket(ludp->net->sock, ip_port, packet, (number * 4 + 4 + 4 + 2)); 491 return sendpacket(ludp->net, ip_port, packet, (number * 4 + 4 + 4 + 2));
460 492
461} 493}
462 494
@@ -471,7 +503,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack
471 temp = htonl(packet_num); 503 temp = htonl(packet_num);
472 memcpy(packet + 1, &temp, 4); 504 memcpy(packet + 1, &temp, 4);
473 memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); 505 memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size);
474 return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); 506 return sendpacket(ludp->net, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
475} 507}
476 508
477/* Sends 1 data packet. */ 509/* Sends 1 data packet. */