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.c77
1 files changed, 63 insertions, 14 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index fbb473e7..46b0bed1 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,53 @@ int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
61 * 59 *
62 * TODO: make this better 60 * TODO: make this better
63 */ 61 */
62
63static uint8_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value)
64{
65 if (ludp->randtable[index][value] == 0)
66 ludp->randtable[index][value] = random_int();
67
68 return ludp->randtable[index][value];
69}
70
64static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) 71static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
65{ 72{
66 uint32_t id = 0, i; 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++;
67 81
68 for (i = 0; i < 6; ++i) { 82#ifdef TOX_ENABLE_IPV6
69 if (ludp->randtable[i][source.uint8[i]] == 0)
70 ludp->randtable[i][source.uint8[i]] = random_int();
71 83
72 id ^= ludp->randtable[i][source.uint8[i]]; 84 if (source.ip.family == AF_INET) {
85 IP4 ip4 = source.ip.ip4;
86#else
87 IP4 ip4 = source.ip;
88#endif
89 int k;
90
91 for (k = 0; k < 4; k++) {
92 id ^= randtable_initget(ludp, i++, ip4.uint8[k]);
93 }
94
95#ifdef TOX_ENABLE_IPV6
73 } 96 }
74 97
98 if (source.ip.family == AF_INET6)
99 {
100 int k;
101
102 for (k = 0; k < 16; k++) {
103 id ^= randtable_initget(ludp, i++, source.ip.ip6.uint8[k]);
104 }
105 }
106
107#endif
108
75 /* id can't be zero. */ 109 /* id can't be zero. */
76 if (id == 0) 110 if (id == 0)
77 id = 1; 111 id = 1;
@@ -86,8 +120,21 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
86 */ 120 */
87static void change_handshake(Lossless_UDP *ludp, IP_Port source) 121static void change_handshake(Lossless_UDP *ludp, IP_Port source)
88{ 122{
89 uint8_t rand = random_int() % 4; 123#ifdef TOX_ENABLE_IPV6
90 ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int(); 124 uint8_t rand;
125
126 if (source.ip.family == AF_INET) {
127 rand = 2 + random_int() % 4;
128 } else if (source.ip.family == AF_INET6) {
129 rand = 2 + random_int() % 16;
130 } else {
131 return;
132 }
133
134#else
135 uint8_t rand = 2 + random_int() % 4;
136#endif
137 ludp->randtable[rand][((uint8_t *)&source.ip)[rand]] = random_int();
91} 138}
92 139
93/* 140/*
@@ -290,7 +337,9 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
290 if ((unsigned int)connection_id < ludp->connections.len) 337 if ((unsigned int)connection_id < ludp->connections.len)
291 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 338 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
292 339
293 IP_Port zero = {{{{0}}, 0, 0}}; 340 IP_Port zero;
341 ip_reset(&zero.ip);
342 zero.port = 0;
294 return zero; 343 return zero;
295} 344}
296 345
@@ -429,7 +478,7 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak
429 temp = htonl(handshake_id2); 478 temp = htonl(handshake_id2);
430 memcpy(packet + 5, &temp, 4); 479 memcpy(packet + 5, &temp, 4);
431 480
432 return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); 481 return sendpacket(ludp->net, ip_port, packet, sizeof(packet));
433} 482}
434 483
435static int send_SYNC(Lossless_UDP *ludp, int connection_id) 484static int send_SYNC(Lossless_UDP *ludp, int connection_id)
@@ -456,7 +505,7 @@ static int send_SYNC(Lossless_UDP *ludp, int connection_id)
456 index += 4; 505 index += 4;
457 memcpy(packet + index, requested, 4 * number); 506 memcpy(packet + index, requested, 4 * number);
458 507
459 return sendpacket(ludp->net->sock, ip_port, packet, (number * 4 + 4 + 4 + 2)); 508 return sendpacket(ludp->net, ip_port, packet, (number * 4 + 4 + 4 + 2));
460 509
461} 510}
462 511
@@ -471,7 +520,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack
471 temp = htonl(packet_num); 520 temp = htonl(packet_num);
472 memcpy(packet + 1, &temp, 4); 521 memcpy(packet + 1, &temp, 4);
473 memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); 522 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); 523 return sendpacket(ludp->net, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
475} 524}
476 525
477/* Sends 1 data packet. */ 526/* Sends 1 data packet. */