mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge branch 'udp_connections' of https://github.com/vx-k/ProjectTox-Core into vx-k-udp_connections
This commit is contained in:
commit
7441a234b7
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
#include "../toxcore/DHT.h"
|
#include "../toxcore/DHT.h"
|
||||||
#include "../toxcore/friend_requests.h"
|
#include "../toxcore/friend_requests.h"
|
||||||
#include "../testing/misc_tools.h"
|
#include "../toxcore/misc_tools.h"
|
||||||
|
|
||||||
/* Sleep function (x = milliseconds) */
|
/* Sleep function (x = milliseconds) */
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
|
@ -3,8 +3,7 @@ noinst_PROGRAMS += DHT_bootstrap
|
||||||
DHT_bootstrap_SOURCES = $(top_srcdir)/other/DHT_bootstrap.c \
|
DHT_bootstrap_SOURCES = $(top_srcdir)/other/DHT_bootstrap.c \
|
||||||
$(top_srcdir)/toxcore/DHT.h \
|
$(top_srcdir)/toxcore/DHT.h \
|
||||||
$(top_srcdir)/toxcore/friend_requests.h \
|
$(top_srcdir)/toxcore/friend_requests.h \
|
||||||
$(top_srcdir)/testing/misc_tools.h \
|
$(top_srcdir)/toxcore/misc_tools.h
|
||||||
$(top_srcdir)/testing/misc_tools.c
|
|
||||||
|
|
||||||
DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \
|
DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \
|
||||||
$(LIBSODIUM_CFLAGS)
|
$(LIBSODIUM_CFLAGS)
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
//#include "../core/network.h"
|
//#include "../core/network.h"
|
||||||
#include "../toxcore/DHT.h"
|
#include "../toxcore/DHT.h"
|
||||||
#include "../toxcore/friend_requests.h"
|
#include "../toxcore/friend_requests.h"
|
||||||
#include "misc_tools.h"
|
#include "../toxcore/misc_tools.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -62,11 +62,13 @@ void print_clientlist(DHT *dht)
|
||||||
}
|
}
|
||||||
|
|
||||||
p_ip = dht->close_clientlist[i].ip_port;
|
p_ip = dht->close_clientlist[i].ip_port;
|
||||||
printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], ntohs(p_ip.port));
|
printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
|
||||||
|
ntohs(p_ip.port));
|
||||||
printf("\nTimestamp: %llu", (long long unsigned int) dht->close_clientlist[i].timestamp);
|
printf("\nTimestamp: %llu", (long long unsigned int) dht->close_clientlist[i].timestamp);
|
||||||
printf("\nLast pinged: %llu\n", (long long unsigned int) dht->close_clientlist[i].last_pinged);
|
printf("\nLast pinged: %llu\n", (long long unsigned int) dht->close_clientlist[i].last_pinged);
|
||||||
p_ip = dht->close_clientlist[i].ret_ip_port;
|
p_ip = dht->close_clientlist[i].ret_ip_port;
|
||||||
printf("OUR IP: %u.%u.%u.%u Port: %u\n", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], ntohs(p_ip.port));
|
printf("OUR IP: %u.%u.%u.%u Port: %u\n", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
|
||||||
|
ntohs(p_ip.port));
|
||||||
printf("Timestamp: %llu\n", (long long unsigned int) dht->close_clientlist[i].ret_timestamp);
|
printf("Timestamp: %llu\n", (long long unsigned int) dht->close_clientlist[i].ret_timestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,7 +88,8 @@ void print_friendlist(DHT *dht)
|
||||||
}
|
}
|
||||||
|
|
||||||
p_ip = DHT_getfriendip(dht, dht->friends_list[k].client_id);
|
p_ip = DHT_getfriendip(dht, dht->friends_list[k].client_id);
|
||||||
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], ntohs(p_ip.port));
|
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
|
||||||
|
ntohs(p_ip.port));
|
||||||
|
|
||||||
printf("\nCLIENTS IN LIST:\n\n");
|
printf("\nCLIENTS IN LIST:\n\n");
|
||||||
|
|
||||||
|
@ -101,11 +104,13 @@ void print_friendlist(DHT *dht)
|
||||||
}
|
}
|
||||||
|
|
||||||
p_ip = dht->friends_list[k].client_list[i].ip_port;
|
p_ip = dht->friends_list[k].client_list[i].ip_port;
|
||||||
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], ntohs(p_ip.port));
|
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
|
||||||
|
ntohs(p_ip.port));
|
||||||
printf("\nTimestamp: %llu", (long long unsigned int) dht->friends_list[k].client_list[i].timestamp);
|
printf("\nTimestamp: %llu", (long long unsigned int) dht->friends_list[k].client_list[i].timestamp);
|
||||||
printf("\nLast pinged: %llu\n", (long long unsigned int) dht->friends_list[k].client_list[i].last_pinged);
|
printf("\nLast pinged: %llu\n", (long long unsigned int) dht->friends_list[k].client_list[i].last_pinged);
|
||||||
p_ip = dht->friends_list[k].client_list[i].ret_ip_port;
|
p_ip = dht->friends_list[k].client_list[i].ret_ip_port;
|
||||||
printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3], ntohs(p_ip.port));
|
printf("ret IP: %u.%u.%u.%u:%u\n", p_ip.ip.uint8[0], p_ip.ip.uint8[1], p_ip.ip.uint8[2], p_ip.ip.uint8[3],
|
||||||
|
ntohs(p_ip.port));
|
||||||
printf("Timestamp: %llu\n", (long long unsigned int)dht->friends_list[k].client_list[i].ret_timestamp);
|
printf("Timestamp: %llu\n", (long long unsigned int)dht->friends_list[k].client_list[i].ret_timestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,7 @@ if BUILD_NTOX
|
||||||
|
|
||||||
bin_PROGRAMS += nTox
|
bin_PROGRAMS += nTox
|
||||||
|
|
||||||
nTox_SOURCES = $(top_srcdir)/testing/misc_tools.h \
|
nTox_SOURCES = $(top_srcdir)/testing/nTox.h \
|
||||||
$(top_srcdir)/testing/misc_tools.c \
|
|
||||||
$(top_srcdir)/testing/nTox.h \
|
|
||||||
$(top_srcdir)/testing/nTox.c
|
$(top_srcdir)/testing/nTox.c
|
||||||
|
|
||||||
nTox_CFLAGS = $(LIBSODIUM_CFLAGS) \
|
nTox_CFLAGS = $(LIBSODIUM_CFLAGS) \
|
||||||
|
@ -24,9 +22,7 @@ noinst_PROGRAMS += DHT_test \
|
||||||
Messenger_test \
|
Messenger_test \
|
||||||
crypto_speed_test
|
crypto_speed_test
|
||||||
|
|
||||||
DHT_test_SOURCES = $(top_srcdir)/testing/DHT_test.c \
|
DHT_test_SOURCES = $(top_srcdir)/testing/DHT_test.c
|
||||||
$(top_srcdir)/testing/misc_tools.h \
|
|
||||||
$(top_srcdir)/testing/misc_tools.c
|
|
||||||
|
|
||||||
DHT_test_CFLAGS = $(LIBSODIUM_CFLAGS)
|
DHT_test_CFLAGS = $(LIBSODIUM_CFLAGS)
|
||||||
|
|
||||||
|
@ -63,9 +59,7 @@ Lossless_UDP_testserver_LDADD = \
|
||||||
|
|
||||||
|
|
||||||
Messenger_test_SOURCES = \
|
Messenger_test_SOURCES = \
|
||||||
$(top_srcdir)/testing/Messenger_test.c \
|
$(top_srcdir)/testing/Messenger_test.c
|
||||||
$(top_srcdir)/testing/misc_tools.h \
|
|
||||||
$(top_srcdir)/testing/misc_tools.c
|
|
||||||
|
|
||||||
Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS)
|
Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../toxcore/Messenger.h"
|
#include "../toxcore/Messenger.h"
|
||||||
#include "misc_tools.h"
|
#include "../toxcore/misc_tools.h"
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
|
|
|
@ -21,24 +21,24 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __WIN32__
|
#ifdef __WIN32__
|
||||||
#define _WIN32_WINNT 0x501
|
#define _WIN32_WINNT 0x501
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#include "nTox.h"
|
#include "nTox.h"
|
||||||
#include "misc_tools.h"
|
#include "../toxcore/misc_tools.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -93,10 +93,11 @@ uint32_t resolve_addr(const char *address)
|
||||||
WSADATA wsa_data;
|
WSADATA wsa_data;
|
||||||
|
|
||||||
res = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
res = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
||||||
if (res != 0)
|
|
||||||
{
|
if (res != 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
rc = getaddrinfo(address, "echo", &hints, &server);
|
rc = getaddrinfo(address, "echo", &hints, &server);
|
||||||
|
|
|
@ -38,13 +38,12 @@
|
||||||
*/
|
*/
|
||||||
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
|
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
|
if (tmp->ip_port.ip.i == ip_port.ip.i &&
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
tmp->ip_port.port == ip_port.port &&
|
||||||
if (ludp->connections[i].ip_port.ip.uint32 == ip_port.ip.uint32 &&
|
tmp->status > 0) {
|
||||||
ludp->connections[i].ip_port.port == ip_port.port &&
|
return tmp_i;
|
||||||
ludp->connections[i].status > 0)
|
}
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -87,38 +86,37 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize a new connection to ip_port.
|
* Initialize a new connection to ip_port
|
||||||
* return an integer corresponding to the connection idt.
|
* Returns an integer corresponding to the connection id.
|
||||||
* return -1 if it could not initialize the connectiont
|
* Return -1 if it could not initialize the connectiont
|
||||||
* If there already was an existing connection to that ip_port return its number.
|
* If there already was an existing connection to that ip_port return its number.
|
||||||
*/
|
*/
|
||||||
int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
{
|
{
|
||||||
int connect = getconnection_id(ludp, ip_port);
|
int connection_id = getconnection_id(ludp, ip_port);
|
||||||
|
|
||||||
if (connect != -1)
|
if (connection_id != -1)
|
||||||
return connect;
|
return connection_id;
|
||||||
|
|
||||||
if (ludp->connections_number == ludp->connections_length) {
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
Connection *temp;
|
if (tmp->status == 0) {
|
||||||
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
|
connection_id = tmp_i;
|
||||||
|
break;
|
||||||
if (temp == NULL)
|
}
|
||||||
return -1;
|
|
||||||
|
|
||||||
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
|
|
||||||
++ludp->connections_length;
|
|
||||||
ludp->connections = temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t i;
|
if (connection_id == -1) {
|
||||||
|
tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */
|
||||||
|
connection_id = ludp->connections.len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
|
||||||
|
memset(connection, 0, sizeof(Connection));
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
|
||||||
if (ludp->connections[i].status == 0) {
|
|
||||||
memset(&ludp->connections[i], 0, sizeof(Connection));
|
|
||||||
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
|
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
|
||||||
|
|
||||||
ludp->connections[i] = (Connection) {
|
*connection = (Connection) {
|
||||||
.ip_port = ip_port,
|
.ip_port = ip_port,
|
||||||
.status = 1,
|
.status = 1,
|
||||||
.inbound = 0,
|
.inbound = 0,
|
||||||
|
@ -132,16 +130,11 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
.last_sent = current_time(),
|
.last_sent = current_time(),
|
||||||
.killat = ~0,
|
.killat = ~0,
|
||||||
.send_counter = 0,
|
.send_counter = 0,
|
||||||
/* Add randomness to timeout to prevent connections getting stuck in a loop. */
|
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||||
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
|
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
|
||||||
};
|
};
|
||||||
++ludp->connections_number;
|
|
||||||
|
|
||||||
return i;
|
return connection_id;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -152,28 +145,27 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
{
|
{
|
||||||
if (getconnection_id(ludp, ip_port) != -1)
|
if (getconnection_id(ludp, ip_port) != -1)
|
||||||
return -1;
|
return -1; /* TODO: return existing connection instead? */
|
||||||
|
|
||||||
if (ludp->connections_number == ludp->connections_length) {
|
int connection_id = -1;
|
||||||
Connection *temp;
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
|
if (tmp->status == 0) {
|
||||||
|
connection_id = tmp_i;
|
||||||
if (temp == NULL)
|
break;
|
||||||
return -1;
|
}
|
||||||
|
|
||||||
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
|
|
||||||
++ludp->connections_length;
|
|
||||||
ludp->connections = temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t i;
|
if (connection_id == -1) {
|
||||||
|
tox_array_push_ptr(&ludp->connections, 0); /* TODO: check return value */
|
||||||
|
connection_id = ludp->connections.len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
memset(connection, 0, sizeof(Connection));
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
|
||||||
if (ludp->connections[i].status == 0) {
|
|
||||||
memset(&ludp->connections[i], 0, sizeof(Connection));
|
|
||||||
uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
||||||
|
|
||||||
ludp->connections[i] = (Connection) {
|
*connection = (Connection) {
|
||||||
.ip_port = ip_port,
|
.ip_port = ip_port,
|
||||||
.status = 2,
|
.status = 2,
|
||||||
.inbound = 2,
|
.inbound = 2,
|
||||||
|
@ -183,18 +175,14 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
.last_sent = current_time(),
|
.last_sent = current_time(),
|
||||||
.send_counter = 127,
|
.send_counter = 127,
|
||||||
|
|
||||||
/* Add randomness to timeout to prevent connections getting stuck in a loop. */
|
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||||
.timeout = timeout,
|
.timeout = timeout,
|
||||||
|
|
||||||
/* If this connection isn't handled within the timeout kill it. */
|
/* if this connection isn't handled within the timeout kill it. */
|
||||||
.killat = current_time() + 1000000UL * timeout
|
.killat = current_time() + 1000000UL * timeout
|
||||||
};
|
};
|
||||||
++ludp->connections_number;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return connection_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -203,59 +191,28 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||||
*/
|
*/
|
||||||
int incoming_connection(Lossless_UDP *ludp)
|
int incoming_connection(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
|
if (tmp->inbound == 2) {
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
tmp->inbound = 1;
|
||||||
if (ludp->connections[i].inbound == 2) {
|
return tmp_i;
|
||||||
ludp->connections[i].inbound = 1;
|
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Try to free some memory from the connections array. */
|
|
||||||
static void free_connections(Lossless_UDP *ludp)
|
|
||||||
{
|
|
||||||
uint32_t i;
|
|
||||||
|
|
||||||
for (i = ludp->connections_length; i != 0; --i)
|
|
||||||
if (ludp->connections[i - 1].status != 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (ludp->connections_length == i)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (i == 0) {
|
|
||||||
free(ludp->connections);
|
|
||||||
ludp->connections = NULL;
|
|
||||||
ludp->connections_length = i;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection *temp;
|
|
||||||
temp = realloc(ludp->connections, sizeof(Connection) * i);
|
|
||||||
|
|
||||||
if (temp == NULL && i != 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
ludp->connections = temp;
|
|
||||||
ludp->connections_length = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* return -1 if it could not kill the connection.
|
* return -1 if it could not kill the connection.
|
||||||
* return 0 if killed successfully.
|
* return 0 if killed successfully.
|
||||||
*/
|
*/
|
||||||
int kill_connection(Lossless_UDP *ludp, int connection_id)
|
int kill_connection(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
if (connection_id >= 0 && connection_id < ludp->connections.len) {
|
||||||
if (ludp->connections[connection_id].status > 0) {
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
ludp->connections[connection_id].status = 0;
|
|
||||||
change_handshake(ludp, ludp->connections[connection_id].ip_port);
|
if (connection->status > 0) {
|
||||||
--ludp->connections_number;
|
connection->status = 0;
|
||||||
free_connections(ludp);
|
change_handshake(ludp, connection->ip_port);
|
||||||
|
memset(connection, 0, sizeof(Connection));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,9 +227,11 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
|
||||||
*/
|
*/
|
||||||
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
||||||
{
|
{
|
||||||
if (connection_id >= 0 && connection_id < ludp->connections_length) {
|
if (connection_id >= 0 && connection_id < ludp->connections.len) {
|
||||||
if (ludp->connections[connection_id].status > 0) {
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
ludp->connections[connection_id].killat = current_time() + 1000000UL * seconds;
|
|
||||||
|
if (connection->status > 0) {
|
||||||
|
connection->killat = current_time() + 1000000UL * seconds;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,8 +249,8 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
|
||||||
*/
|
*/
|
||||||
int is_connected(Lossless_UDP *ludp, int connection_id)
|
int is_connected(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
if (connection_id >= 0 && connection_id < ludp->connections.len)
|
||||||
return ludp->connections[connection_id].status;
|
return tox_array_get(&ludp->connections, connection_id, Connection).status;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -299,8 +258,8 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
|
||||||
/* return the ip_port of the corresponding connection. */
|
/* return the ip_port of the corresponding connection. */
|
||||||
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id >= 0 && connection_id < ludp->connections_length)
|
if (connection_id >= 0 && connection_id < ludp->connections.len)
|
||||||
return ludp->connections[connection_id].ip_port;
|
return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
|
||||||
|
|
||||||
IP_Port zero = { .ip = {0}, .port = 0, .padding = 0 };
|
IP_Port zero = { .ip = {0}, .port = 0, .padding = 0 };
|
||||||
return zero;
|
return zero;
|
||||||
|
@ -309,19 +268,21 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
|
||||||
/* returns the number of packets in the queue waiting to be successfully sent. */
|
/* returns the number of packets in the queue waiting to be successfully sent. */
|
||||||
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
|
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
if (connection_id < 0 || connection_id >= ludp->connections.len)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent;
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
return connection->sendbuff_packetnum - connection->successful_sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
|
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
|
||||||
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
if (connection_id < 0 || connection_id >= ludp->connections.len)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read;
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
return connection->recv_packetnum - connection->successful_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* returns the id of the next packet in the queue.
|
/* returns the id of the next packet in the queue.
|
||||||
|
@ -329,12 +290,13 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
|
||||||
*/
|
*/
|
||||||
char id_packet(Lossless_UDP *ludp, int connection_id)
|
char id_packet(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
if (connection_id < 0 || connection_id >= ludp->connections_length)
|
if (connection_id < 0 || connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0)
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read %
|
|
||||||
MAX_QUEUE_NUM].data[0];
|
if (connection->status != 0)
|
||||||
|
return connection->recvbuffer[connection->successful_read % MAX_QUEUE_NUM].data[0];
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -344,16 +306,17 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
|
||||||
*/
|
*/
|
||||||
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
||||||
{
|
{
|
||||||
if (recvqueue(ludp, connection_id) != 0) {
|
if (recvqueue(ludp, connection_id) == 0)
|
||||||
uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM;
|
|
||||||
uint16_t size = ludp->connections[connection_id].recvbuffer[index].size;
|
|
||||||
memcpy(data, ludp->connections[connection_id].recvbuffer[index].data, size);
|
|
||||||
++ludp->connections[connection_id].successful_read;
|
|
||||||
ludp->connections[connection_id].recvbuffer[index].size = 0;
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
uint16_t index = connection->successful_read % MAX_QUEUE_NUM;
|
||||||
|
uint16_t size = connection->recvbuffer[index].size;
|
||||||
|
memcpy(data, connection->recvbuffer[index].data, size);
|
||||||
|
++connection->successful_read;
|
||||||
|
connection->recvbuffer[index].size = 0;
|
||||||
|
return size;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -362,33 +325,34 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
|
||||||
*/
|
*/
|
||||||
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
|
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
|
||||||
{
|
{
|
||||||
if (length > MAX_DATA_SIZE || length == 0)
|
if (length > MAX_DATA_SIZE || length == 0 || sendqueue(ludp, connection_id) >= BUFFER_PACKET_NUM)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
uint32_t index = ludp->connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
|
uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM;
|
||||||
memcpy(ludp->connections[connection_id].sendbuffer[index].data, data, length);
|
memcpy(connection->sendbuffer[index].data, data, length);
|
||||||
ludp->connections[connection_id].sendbuffer[index].size = length;
|
connection->sendbuffer[index].size = length;
|
||||||
ludp->connections[connection_id].sendbuff_packetnum++;
|
connection->sendbuff_packetnum++;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Put the packet numbers the we are missing in requested and return the number. */
|
/* Put the packet numbers the we are missing in requested and return the number. */
|
||||||
uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
|
uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
|
||||||
{
|
{
|
||||||
|
/* don't request packets if the buffer is full. */
|
||||||
|
if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
|
||||||
|
return 0;
|
||||||
|
|
||||||
uint32_t number = 0;
|
uint32_t number = 0;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
|
|
||||||
/* Don't request packets if the buffer is full. */
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
for (i = ludp->connections[connection_id].recv_packetnum; i != ludp->connections[connection_id].osent_packetnum; i++) {
|
for (i = connection->recv_packetnum;
|
||||||
if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
i != connection->osent_packetnum;
|
||||||
|
i++) {
|
||||||
|
if (connection->recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
||||||
temp = htonl(i);
|
temp = htonl(i);
|
||||||
memcpy(requested + number, &temp, 4);
|
memcpy(requested + number, &temp, 4);
|
||||||
++number;
|
++number;
|
||||||
|
@ -396,7 +360,7 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
|
||||||
}
|
}
|
||||||
|
|
||||||
if (number == 0)
|
if (number == 0)
|
||||||
ludp->connections[connection_id].recv_packetnum = ludp->connections[connection_id].osent_packetnum;
|
connection->recv_packetnum = connection->osent_packetnum;
|
||||||
|
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
@ -421,15 +385,16 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak
|
||||||
return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet));
|
return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
|
static int send_SYNC(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
|
uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
|
||||||
uint16_t index = 0;
|
uint16_t index = 0;
|
||||||
|
|
||||||
IP_Port ip_port = ludp->connections[connection_id].ip_port;
|
IP_Port ip_port = connection->ip_port;
|
||||||
uint8_t counter = ludp->connections[connection_id].send_counter;
|
uint8_t counter = connection->send_counter;
|
||||||
uint32_t recv_packetnum = htonl(ludp->connections[connection_id].recv_packetnum);
|
uint32_t recv_packetnum = htonl(connection->recv_packetnum);
|
||||||
uint32_t sent_packetnum = htonl(ludp->connections[connection_id].sent_packetnum);
|
uint32_t sent_packetnum = htonl(connection->sent_packetnum);
|
||||||
|
|
||||||
uint32_t requested[BUFFER_PACKET_NUM];
|
uint32_t requested[BUFFER_PACKET_NUM];
|
||||||
uint32_t number = missing_packets(ludp, connection_id, requested);
|
uint32_t number = missing_packets(ludp, connection_id, requested);
|
||||||
|
@ -448,37 +413,38 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num)
|
static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t packet_num)
|
||||||
{
|
{
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
|
||||||
uint32_t index = packet_num % MAX_QUEUE_NUM;
|
uint32_t index = packet_num % MAX_QUEUE_NUM;
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
uint8_t packet[1 + 4 + MAX_DATA_SIZE];
|
uint8_t packet[1 + 4 + MAX_DATA_SIZE];
|
||||||
packet[0] = NET_PACKET_DATA;
|
packet[0] = NET_PACKET_DATA;
|
||||||
temp = htonl(packet_num);
|
temp = htonl(packet_num);
|
||||||
memcpy(packet + 1, &temp, 4);
|
memcpy(packet + 1, &temp, 4);
|
||||||
memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data,
|
memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size);
|
||||||
ludp->connections[connection_id].sendbuffer[index].size);
|
return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size);
|
||||||
return sendpacket(ludp->net->sock, ludp->connections[connection_id].ip_port, packet,
|
|
||||||
1 + 4 + ludp->connections[connection_id].sendbuffer[index].size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sends 1 data packet. */
|
/* sends 1 data packet */
|
||||||
static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
|
static int send_DATA(Lossless_UDP *ludp, int connection_id)
|
||||||
{
|
{
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
int ret;
|
int ret;
|
||||||
uint32_t buffer[BUFFER_PACKET_NUM];
|
uint32_t buffer[BUFFER_PACKET_NUM];
|
||||||
|
|
||||||
if (ludp->connections[connection_id].num_req_paquets > 0) {
|
if (connection->num_req_paquets > 0) {
|
||||||
ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].req_packets[0]);
|
ret = send_data_packet(ludp, connection_id, connection->req_packets[0]);
|
||||||
ludp->connections[connection_id].num_req_paquets--;
|
connection->num_req_paquets--;
|
||||||
memcpy(buffer, ludp->connections[connection_id].req_packets + 1, ludp->connections[connection_id].num_req_paquets * 4);
|
memcpy(buffer, connection->req_packets + 1, connection->num_req_paquets * 4);
|
||||||
memcpy(ludp->connections[connection_id].req_packets, buffer, ludp->connections[connection_id].num_req_paquets * 4);
|
memcpy(connection->req_packets, buffer, connection->num_req_paquets * 4);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ludp->connections[connection_id].sendbuff_packetnum != ludp->connections[connection_id].sent_packetnum) {
|
if (connection->sendbuff_packetnum != connection->sent_packetnum) {
|
||||||
ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].sent_packetnum);
|
ret = send_data_packet(ludp, connection_id, connection->sent_packetnum);
|
||||||
ludp->connections[connection_id].sent_packetnum++;
|
connection->sent_packetnum++;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,31 +472,33 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
|
||||||
|
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
uint32_t handshake_id1, handshake_id2;
|
uint32_t handshake_id1, handshake_id2;
|
||||||
|
int connection_id = getconnection_id(ludp, source);
|
||||||
|
|
||||||
int connection = getconnection_id(ludp, source);
|
|
||||||
memcpy(&temp, packet + 1, 4);
|
memcpy(&temp, packet + 1, 4);
|
||||||
handshake_id1 = ntohl(temp);
|
handshake_id1 = ntohl(temp);
|
||||||
memcpy(&temp, packet + 5, 4);
|
memcpy(&temp, packet + 5, 4);
|
||||||
handshake_id2 = ntohl(temp);
|
handshake_id2 = ntohl(temp);
|
||||||
|
|
||||||
if (handshake_id2 == 0 && is_connected(ludp, connection) < 3) {
|
|
||||||
|
if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) {
|
||||||
send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
|
send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_connected(ludp, connection) != 1)
|
if (is_connected(ludp, connection_id) != 1)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* If handshake_id2 is what we sent previously as handshake_id1. */
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
if (handshake_id2 == ludp->connections[connection].handshake_id1) {
|
|
||||||
ludp->connections[connection].status = 2;
|
/* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||||
|
if (handshake_id2 == connection->handshake_id1) {
|
||||||
|
connection->status = 2;
|
||||||
/* NOTE: is this necessary?
|
/* NOTE: is this necessary?
|
||||||
* ludp->connections[connection].handshake_id2 = handshake_id1;
|
connection->handshake_id2 = handshake_id1; */
|
||||||
*/
|
connection->orecv_packetnum = handshake_id2;
|
||||||
ludp->connections[connection].orecv_packetnum = handshake_id2;
|
connection->osent_packetnum = handshake_id1;
|
||||||
ludp->connections[connection].osent_packetnum = handshake_id1;
|
connection->recv_packetnum = handshake_id1;
|
||||||
ludp->connections[connection].recv_packetnum = handshake_id1;
|
connection->successful_read = handshake_id1;
|
||||||
ludp->connections[connection].successful_read = handshake_id1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -553,18 +521,19 @@ static int SYNC_valid(uint32_t length)
|
||||||
static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
||||||
{
|
{
|
||||||
if (handshake_id(ludp, source) == recv_packetnum) {
|
if (handshake_id(ludp, source) == recv_packetnum) {
|
||||||
int x = new_inconnection(ludp, source);
|
int connection_id = new_inconnection(ludp, source);
|
||||||
|
|
||||||
if (x != -1) {
|
if (connection_id != -1) {
|
||||||
ludp->connections[x].orecv_packetnum = recv_packetnum;
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
ludp->connections[x].sent_packetnum = recv_packetnum;
|
connection->orecv_packetnum = recv_packetnum;
|
||||||
ludp->connections[x].sendbuff_packetnum = recv_packetnum;
|
connection->sent_packetnum = recv_packetnum;
|
||||||
ludp->connections[x].successful_sent = recv_packetnum;
|
connection->sendbuff_packetnum = recv_packetnum;
|
||||||
ludp->connections[x].osent_packetnum = sent_packetnum;
|
connection->successful_sent = recv_packetnum;
|
||||||
ludp->connections[x].recv_packetnum = sent_packetnum;
|
connection->osent_packetnum = sent_packetnum;
|
||||||
ludp->connections[x].successful_read = sent_packetnum;
|
connection->recv_packetnum = sent_packetnum;
|
||||||
|
connection->successful_read = sent_packetnum;
|
||||||
|
|
||||||
return x;
|
return connection_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -575,11 +544,13 @@ static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packet
|
||||||
static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
|
||||||
uint32_t sent_packetnum)
|
uint32_t sent_packetnum)
|
||||||
{
|
{
|
||||||
if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) {
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
/* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */
|
|
||||||
ludp->connections[connection_id].status = 3;
|
if (recv_packetnum == connection->orecv_packetnum) {
|
||||||
ludp->connections[connection_id].recv_counter = counter;
|
/* && sent_packetnum == connection->osent_packetnum) */
|
||||||
++ludp->connections[connection_id].send_counter;
|
connection->status = 3;
|
||||||
|
connection->recv_counter = counter;
|
||||||
|
++connection->send_counter;
|
||||||
send_SYNC(ludp, connection_id);
|
send_SYNC(ludp, connection_id);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -592,32 +563,33 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter,
|
||||||
uint32_t *req_packets,
|
uint32_t *req_packets,
|
||||||
uint16_t number)
|
uint16_t number)
|
||||||
{
|
{
|
||||||
uint8_t comp_counter = (counter - ludp->connections[connection_id].recv_counter );
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
|
|
||||||
|
uint8_t comp_counter = (counter - connection->recv_counter);
|
||||||
uint32_t i, temp;
|
uint32_t i, temp;
|
||||||
/* uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].successful_sent);
|
/* uint32_t comp_1 = (recv_packetnum - connection->successful_sent);
|
||||||
uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].successful_read); */
|
uint32_t comp_2 = (sent_packetnum - connection->successful_read); */
|
||||||
uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum);
|
uint32_t comp_1 = (recv_packetnum - connection->orecv_packetnum);
|
||||||
uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum);
|
uint32_t comp_2 = (sent_packetnum - connection->osent_packetnum);
|
||||||
|
|
||||||
/* Packet valid. */
|
/* Packet valid. */
|
||||||
if (comp_1 <= BUFFER_PACKET_NUM &&
|
if (comp_1 <= BUFFER_PACKET_NUM &&
|
||||||
comp_2 <= BUFFER_PACKET_NUM &&
|
comp_2 <= BUFFER_PACKET_NUM &&
|
||||||
comp_counter < 10 && comp_counter != 0) {
|
comp_counter < 10 && comp_counter != 0) {
|
||||||
|
connection->orecv_packetnum = recv_packetnum;
|
||||||
|
connection->osent_packetnum = sent_packetnum;
|
||||||
|
connection->successful_sent = recv_packetnum;
|
||||||
|
connection->last_recvSYNC = current_time();
|
||||||
|
connection->recv_counter = counter;
|
||||||
|
|
||||||
ludp->connections[connection_id].orecv_packetnum = recv_packetnum;
|
++connection->send_counter;
|
||||||
ludp->connections[connection_id].osent_packetnum = sent_packetnum;
|
|
||||||
ludp->connections[connection_id].successful_sent = recv_packetnum;
|
|
||||||
ludp->connections[connection_id].last_recvSYNC = current_time();
|
|
||||||
ludp->connections[connection_id].recv_counter = counter;
|
|
||||||
|
|
||||||
++ludp->connections[connection_id].send_counter;
|
|
||||||
|
|
||||||
for (i = 0; i < number; ++i) {
|
for (i = 0; i < number; ++i) {
|
||||||
temp = ntohl(req_packets[i]);
|
temp = ntohl(req_packets[i]);
|
||||||
memcpy(ludp->connections[connection_id].req_packets + i, &temp, 4 * number);
|
memcpy(connection->req_packets + i, &temp, 4 * number);
|
||||||
}
|
}
|
||||||
|
|
||||||
ludp->connections[connection_id].num_req_paquets = number;
|
connection->num_req_paquets = number;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -631,7 +603,6 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
||||||
if (!SYNC_valid(length))
|
if (!SYNC_valid(length))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
int connection = getconnection_id(ludp, source);
|
|
||||||
uint8_t counter;
|
uint8_t counter;
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
uint32_t recv_packetnum, sent_packetnum;
|
uint32_t recv_packetnum, sent_packetnum;
|
||||||
|
@ -647,15 +618,19 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
||||||
if (number != 0)
|
if (number != 0)
|
||||||
memcpy(req_packets, packet + 10, 4 * number);
|
memcpy(req_packets, packet + 10, 4 * number);
|
||||||
|
|
||||||
if (connection == -1)
|
int connection_id = getconnection_id(ludp, source);
|
||||||
|
|
||||||
|
if (connection_id == -1)
|
||||||
return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum);
|
return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum);
|
||||||
|
|
||||||
if (ludp->connections[connection].status == 2)
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
return handle_SYNC2(ludp, connection, counter,
|
|
||||||
|
if (connection->status == 2)
|
||||||
|
return handle_SYNC2(ludp, connection_id, counter,
|
||||||
recv_packetnum, sent_packetnum);
|
recv_packetnum, sent_packetnum);
|
||||||
|
|
||||||
if (ludp->connections[connection].status == 3)
|
if (connection->status == 3)
|
||||||
return handle_SYNC3(ludp, connection, counter, recv_packetnum,
|
return handle_SYNC3(ludp, connection_id, counter, recv_packetnum,
|
||||||
sent_packetnum, req_packets, number);
|
sent_packetnum, req_packets, number);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -672,28 +647,28 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui
|
||||||
if (size > MAX_DATA_SIZE)
|
if (size > MAX_DATA_SIZE)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
uint32_t maxnum = ludp->connections[connection_id].successful_read + BUFFER_PACKET_NUM;
|
uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM;
|
||||||
uint32_t sent_packet = data_num - ludp->connections[connection_id].osent_packetnum;
|
uint32_t sent_packet = data_num - connection->osent_packetnum;
|
||||||
|
|
||||||
for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
for (i = connection->recv_packetnum; i != maxnum; ++i) {
|
||||||
if (i == data_num) {
|
if (i == data_num) {
|
||||||
memcpy(ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
memcpy(connection->recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
||||||
|
|
||||||
ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
connection->recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
||||||
ludp->connections[connection_id].last_recvdata = current_time();
|
connection->last_recvdata = current_time();
|
||||||
|
|
||||||
if (sent_packet < BUFFER_PACKET_NUM) {
|
if (sent_packet < BUFFER_PACKET_NUM)
|
||||||
ludp->connections[connection_id].osent_packetnum = data_num;
|
connection->osent_packetnum = data_num;
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
for (i = connection->recv_packetnum; i != maxnum; ++i) {
|
||||||
if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
if (connection->recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
||||||
ludp->connections[connection_id].recv_packetnum = i;
|
connection->recv_packetnum = i;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -704,13 +679,10 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui
|
||||||
static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
||||||
{
|
{
|
||||||
Lossless_UDP *ludp = object;
|
Lossless_UDP *ludp = object;
|
||||||
int connection = getconnection_id(ludp, source);
|
int connection_id = getconnection_id(ludp, source);
|
||||||
|
|
||||||
if (connection == -1)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
/* Drop the data packet if connection is not connected. */
|
/* Drop the data packet if connection is not connected. */
|
||||||
if (ludp->connections[connection].status != 3)
|
if (connection_id == -1 || tox_array_get(&ludp->connections, connection_id, Connection).status != 3)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
||||||
|
@ -723,7 +695,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l
|
||||||
memcpy(&temp, packet + 1, 4);
|
memcpy(&temp, packet + 1, 4);
|
||||||
number = ntohl(temp);
|
number = ntohl(temp);
|
||||||
|
|
||||||
return add_recv(ludp, connection, number, packet + 5, size);
|
return add_recv(ludp, connection_id, number, packet + 5, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -740,6 +712,8 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net)
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
tox_array_init(&temp->connections, sizeof(Connection));
|
||||||
|
|
||||||
temp->net = net;
|
temp->net = net;
|
||||||
networking_registerhandler(net, NET_PACKET_HANDSHAKE, &handle_handshake, temp);
|
networking_registerhandler(net, NET_PACKET_HANDSHAKE, &handle_handshake, temp);
|
||||||
networking_registerhandler(net, NET_PACKET_SYNC, &handle_SYNC, temp);
|
networking_registerhandler(net, NET_PACKET_SYNC, &handle_SYNC, temp);
|
||||||
|
@ -753,56 +727,52 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net)
|
||||||
*/
|
*/
|
||||||
static void do_new(Lossless_UDP *ludp)
|
static void do_new(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
|
||||||
uint64_t temp_time = current_time();
|
uint64_t temp_time = current_time();
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
if (ludp->connections[i].status == 1)
|
if (tmp->status == 1 && (tmp->last_sent + (1000000UL / tmp->SYNC_rate)) <= temp_time) {
|
||||||
if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) {
|
send_handshake(ludp, tmp->ip_port, tmp->handshake_id1, 0);
|
||||||
send_handshake(ludp, ludp->connections[i].ip_port, ludp->connections[i].handshake_id1, 0);
|
tmp->last_sent = temp_time;
|
||||||
ludp->connections[i].last_sent = temp_time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Kill all timed out connections. */
|
/* kill all timed out connections */
|
||||||
if (ludp->connections[i].status > 0 &&
|
if (tmp->status > 0 && (tmp->last_recvSYNC + tmp->timeout * 1000000UL) < temp_time && tmp->status != 4) {
|
||||||
(ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time &&
|
tmp->status = 4;
|
||||||
ludp->connections[i].status != 4) {
|
|
||||||
ludp->connections[i].status = 4;
|
|
||||||
/* kill_connection(i); */
|
/* kill_connection(i); */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ludp->connections[i].status > 0 && ludp->connections[i].killat < temp_time)
|
if (tmp->status > 0 && tmp->killat < temp_time)
|
||||||
kill_connection(ludp, i);
|
kill_connection(ludp, tmp_i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_SYNC(Lossless_UDP *ludp)
|
static void do_SYNC(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
|
||||||
uint64_t temp_time = current_time();
|
uint64_t temp_time = current_time();
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
if (ludp->connections[i].status == 2 || ludp->connections[i].status == 3)
|
if (tmp->status == 2 || tmp->status == 3)
|
||||||
if ((ludp->connections[i].last_SYNC + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) {
|
if ((tmp->last_SYNC + (1000000UL / tmp->SYNC_rate)) <= temp_time) {
|
||||||
send_SYNC(ludp, i);
|
send_SYNC(ludp, tmp_i);
|
||||||
ludp->connections[i].last_SYNC = temp_time;
|
tmp->last_SYNC = temp_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_data(Lossless_UDP *ludp)
|
static void do_data(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
|
||||||
uint64_t j;
|
uint64_t j;
|
||||||
uint64_t temp_time = current_time();
|
uint64_t temp_time = current_time();
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i)
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
if (ludp->connections[i].status == 3 && sendqueue(ludp, i) != 0)
|
if (tmp->status == 3 && sendqueue(ludp, tmp_i) != 0 &&
|
||||||
if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].data_rate)) <= temp_time) {
|
(tmp->last_sent + (1000000UL / tmp->data_rate)) <= temp_time) {
|
||||||
for (j = ludp->connections[i].last_sent; j < temp_time; j += (1000000UL / ludp->connections[i].data_rate))
|
for (j = tmp->last_sent; j < temp_time; j += (1000000UL / tmp->data_rate))
|
||||||
send_DATA(ludp, i);
|
send_DATA(ludp, tmp_i);
|
||||||
|
|
||||||
ludp->connections[i].last_sent = temp_time;
|
tmp->last_sent = temp_time;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -815,21 +785,20 @@ static void do_data(Lossless_UDP *ludp)
|
||||||
*/
|
*/
|
||||||
static void adjust_rates(Lossless_UDP *ludp)
|
static void adjust_rates(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
|
||||||
uint64_t temp_time = current_time();
|
uint64_t temp_time = current_time();
|
||||||
|
|
||||||
for (i = 0; i < ludp->connections_length; ++i) {
|
tox_array_for_each(&ludp->connections, Connection, tmp) {
|
||||||
if (ludp->connections[i].status == 1 || ludp->connections[i].status == 2)
|
if (tmp->status == 1 || tmp->status == 2)
|
||||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
tmp->SYNC_rate = MAX_SYNC_RATE;
|
||||||
|
|
||||||
if (ludp->connections[i].status == 3) {
|
if (tmp->status == 3) {
|
||||||
if (sendqueue(ludp, i) != 0) {
|
if (sendqueue(ludp, tmp_i) != 0) {
|
||||||
ludp->connections[i].data_rate = (BUFFER_PACKET_NUM - ludp->connections[i].num_req_paquets) * MAX_SYNC_RATE;
|
tmp->data_rate = (BUFFER_PACKET_NUM - tmp->num_req_paquets) * MAX_SYNC_RATE;
|
||||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
tmp->SYNC_rate = MAX_SYNC_RATE;
|
||||||
} else if (ludp->connections[i].last_recvdata + 1000000UL > temp_time)
|
} else if (tmp->last_recvdata + 1000000UL > temp_time)
|
||||||
ludp->connections[i].SYNC_rate = MAX_SYNC_RATE;
|
tmp->SYNC_rate = MAX_SYNC_RATE;
|
||||||
else
|
else
|
||||||
ludp->connections[i].SYNC_rate = SYNC_RATE;
|
tmp->SYNC_rate = SYNC_RATE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -845,6 +814,6 @@ void do_lossless_udp(Lossless_UDP *ludp)
|
||||||
|
|
||||||
void kill_lossless_udp(Lossless_UDP *ludp)
|
void kill_lossless_udp(Lossless_UDP *ludp)
|
||||||
{
|
{
|
||||||
free(ludp->connections);
|
tox_array_delete(&ludp->connections);
|
||||||
free(ludp);
|
free(ludp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#define LOSSLESS_UDP_H
|
#define LOSSLESS_UDP_H
|
||||||
|
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
#include "misc_tools.h"
|
||||||
|
|
||||||
|
|
||||||
/* Maximum length of the data in the data packets. */
|
/* Maximum length of the data in the data packets. */
|
||||||
|
@ -118,10 +119,8 @@ typedef struct {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Networking_Core *net;
|
Networking_Core *net;
|
||||||
Connection *connections;
|
|
||||||
|
|
||||||
uint32_t connections_length; /* Length of connections array. */
|
tox_array connections;
|
||||||
uint32_t connections_number; /* Number of connections in connections array. */
|
|
||||||
|
|
||||||
/* Table of random numbers used in handshake_id. */
|
/* Table of random numbers used in handshake_id. */
|
||||||
uint32_t randtable[6][256];
|
uint32_t randtable[6][256];
|
||||||
|
|
|
@ -24,7 +24,9 @@ libtoxcore_la_SOURCES = $(top_srcdir)/toxcore/DHT.h \
|
||||||
$(top_srcdir)/toxcore/tox.h \
|
$(top_srcdir)/toxcore/tox.h \
|
||||||
$(top_srcdir)/toxcore/tox.c \
|
$(top_srcdir)/toxcore/tox.c \
|
||||||
$(top_srcdir)/toxcore/util.h \
|
$(top_srcdir)/toxcore/util.h \
|
||||||
$(top_srcdir)/toxcore/util.c
|
$(top_srcdir)/toxcore/util.c \
|
||||||
|
$(top_srcdir)/toxcore/misc_tools.h \
|
||||||
|
$(top_srcdir)/toxcore/misc_tools.c
|
||||||
|
|
||||||
libtoxcore_la_CFLAGS = -I$(top_srcdir) \
|
libtoxcore_la_CFLAGS = -I$(top_srcdir) \
|
||||||
-I$(top_srcdir)/toxcore \
|
-I$(top_srcdir)/toxcore \
|
||||||
|
|
|
@ -34,36 +34,36 @@ unsigned char *hex_string_to_bin(char hex_string[]);
|
||||||
* wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
|
* wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
|
||||||
*********************************************************/
|
*********************************************************/
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define DEBUG_PRINT(str, ...) do { \
|
#define DEBUG_PRINT(str, ...) do { \
|
||||||
char msg[1000]; \
|
char msg[1000]; \
|
||||||
sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \
|
sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \
|
||||||
fprintf(stderr, msg, __VA_ARGS__); \
|
fprintf(stderr, msg, __VA_ARGS__); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define WARNING(...) do { \
|
#define WARNING(...) do { \
|
||||||
fprintf(stderr, "warning in "); \
|
fprintf(stderr, "warning in "); \
|
||||||
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define INFO(...) do { \
|
#define INFO(...) do { \
|
||||||
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#undef ERROR
|
#undef ERROR
|
||||||
#define ERROR(exit_status, ...) do { \
|
#define ERROR(exit_status, ...) do { \
|
||||||
fprintf(stderr, "error in "); \
|
fprintf(stderr, "error in "); \
|
||||||
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
||||||
exit(exit_status); \
|
exit(exit_status); \
|
||||||
} while (0)
|
} while (0)
|
||||||
#else
|
#else
|
||||||
#define WARNING(...)
|
#define WARNING(...)
|
||||||
#define INFO(...)
|
#define INFO(...)
|
||||||
#undef ERROR
|
#undef ERROR
|
||||||
#define ERROR(...)
|
#define ERROR(...)
|
||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
|
|
||||||
/************************Linked List***********************
|
/************************Linked List***********************
|
||||||
|
@ -117,7 +117,7 @@ static inline void tox_list_remove(tox_list *lst)
|
||||||
************************************************************/
|
************************************************************/
|
||||||
|
|
||||||
typedef struct tox_array {
|
typedef struct tox_array {
|
||||||
void *data;
|
uint8_t *data; /* last elem is data[len-1] */
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
size_t elem_size; /* in bytes */
|
size_t elem_size; /* in bytes */
|
||||||
} tox_array;
|
} tox_array;
|
||||||
|
@ -135,24 +135,37 @@ static inline void tox_array_delete(tox_array *arr)
|
||||||
arr->len = arr->elem_size = 0;
|
arr->len = arr->elem_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void _tox_array_push(tox_array *arr, uint8_t *item)
|
static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item)
|
||||||
{
|
{
|
||||||
arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
|
arr->data = realloc(arr->data, arr->elem_size * (arr->len + 1));
|
||||||
|
|
||||||
|
if (item != NULL)
|
||||||
|
memcpy(arr->data + arr->elem_size * arr->len, item, arr->elem_size);
|
||||||
|
|
||||||
memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
|
|
||||||
arr->len++;
|
arr->len++;
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
|
#define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item)))
|
||||||
|
|
||||||
/* Deletes num items from array.
|
/* Deletes num items from array.
|
||||||
* Not same as pop in stacks, because to access elements you use data.
|
* Not same as pop in stacks, because to access elements you use data.
|
||||||
*/
|
*/
|
||||||
static inline void tox_array_pop(tox_array *arr, uint32_t num)
|
static inline void tox_array_pop(tox_array *arr, uint32_t num)
|
||||||
{
|
{
|
||||||
arr->len--;
|
if (num == 0)
|
||||||
arr->data = realloc(arr->data, arr->elem_size*arr->len);
|
num = 1;
|
||||||
|
|
||||||
|
arr->len -= num;
|
||||||
|
arr->data = realloc(arr->data, arr->elem_size * arr->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
|
/* TODO: return ptr and do not take type */
|
||||||
|
#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i])
|
||||||
|
|
||||||
|
|
||||||
|
#define tox_array_for_each(arr, type, tmp_name) \
|
||||||
|
type *tmp_name = &tox_array_get(arr, 0, type); uint32_t tmp_name ## _i = 0; \
|
||||||
|
for (; tmp_name ## _i < (arr)->len; tmp_name = &tox_array_get(arr, ++ tmp_name ## _i, type))
|
||||||
|
|
||||||
#endif // MISC_TOOLS_H
|
#endif // MISC_TOOLS_H
|
Loading…
Reference in New Issue
Block a user