summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-04-13 22:54:05 +0300
committeraunsane <aunsane@gmail.com>2018-04-13 22:54:27 +0300
commit430f999f00224a68a967e7122894b469d5ee60bf (patch)
treeb552ff57e5c8a0f1de49208eb511f4edb38f2032 /protocols/Tox/libtox/src
parent81dd07b3ae0c7f31da0c6766b8b325e2601e4195 (diff)
Tox: added logging from toxcore
- toxcore updated to 0.2.1 - toxcore now bootstraped with random two nodes - version bump
Diffstat (limited to 'protocols/Tox/libtox/src')
-rw-r--r--protocols/Tox/libtox/src/toxcore/DHT.c59
-rw-r--r--protocols/Tox/libtox/src/toxcore/DHT.h26
-rw-r--r--protocols/Tox/libtox/src/toxcore/Messenger.c29
-rw-r--r--protocols/Tox/libtox/src/toxcore/ccompat.h4
-rw-r--r--protocols/Tox/libtox/src/toxcore/crypto_core.api.h5
-rw-r--r--protocols/Tox/libtox/src/toxcore/crypto_core.c7
-rw-r--r--protocols/Tox/libtox/src/toxcore/crypto_core.h5
-rw-r--r--protocols/Tox/libtox/src/toxcore/logger.c51
-rw-r--r--protocols/Tox/libtox/src/toxcore/logger.h15
-rw-r--r--protocols/Tox/libtox/src/toxcore/net_crypto.c7
-rw-r--r--protocols/Tox/libtox/src/toxcore/network.c121
-rw-r--r--protocols/Tox/libtox/src/toxcore/network.h28
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox.api.h2
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox.h2
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox_api.c1
15 files changed, 260 insertions, 102 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.c b/protocols/Tox/libtox/src/toxcore/DHT.c
index 4acfa1617d..f611fca61d 100644
--- a/protocols/Tox/libtox/src/toxcore/DHT.c
+++ b/protocols/Tox/libtox/src/toxcore/DHT.c
@@ -57,7 +57,30 @@
/* Number of get node requests to send to quickly find close nodes. */
#define MAX_BOOTSTRAP_TIMES 5
-#define ASSOC_COUNT 2
+#define ARRAY_SIZE(ARR) (sizeof (ARR) / sizeof (ARR)[0])
+
+struct DHT_Friend {
+ uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
+ Client_data client_list[MAX_FRIEND_CLIENTS];
+
+ /* Time at which the last get_nodes request was sent. */
+ uint64_t lastgetnode;
+ /* number of times get_node packets were sent. */
+ uint32_t bootstrap_times;
+
+ /* Symetric NAT hole punching stuff. */
+ NAT nat;
+
+ uint16_t lock_count;
+ struct {
+ void (*ip_callback)(void *, int32_t, IP_Port);
+ void *data;
+ int32_t number;
+ } callbacks[DHT_FRIEND_MAX_LOCKS];
+
+ Node_format to_bootstrap[MAX_SENT_NODES];
+ unsigned int num_to_bootstrap;
+};
struct DHT {
Logger *log;
@@ -94,6 +117,16 @@ struct DHT {
unsigned int num_to_bootstrap;
};
+const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend)
+{
+ return dht_friend->public_key;
+}
+
+const Client_data *dht_friend_client(const DHT_Friend *dht_friend, size_t index)
+{
+ return &dht_friend->client_list[index];
+}
+
const uint8_t *dht_get_self_public_key(const DHT *dht)
{
return dht->self_public_key;
@@ -1624,9 +1657,9 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
}
const Client_data *const client = &frnd->client_list[client_index];
- const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 };
+ const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 };
- for (size_t i = 0; i < ASSOC_COUNT; i++) {
+ for (size_t i = 0; i < ARRAY_SIZE(assocs); i++) {
const IPPTsPng *const assoc = assocs[i];
if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) {
@@ -1655,9 +1688,9 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
/* If node is not dead. */
Client_data *client = &list[i];
- IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 };
+ IPPTsPng *assocs[] = { &client->assoc6, &client->assoc4 };
- for (size_t j = 0; j < ASSOC_COUNT; j++) {
+ for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) {
IPPTsPng *assoc = assocs[j];
if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) {
@@ -1757,9 +1790,9 @@ static void do_Close(DHT *dht)
for (size_t i = 0; i < LCLIENT_LIST; i++) {
Client_data *const client = &dht->close_clientlist[i];
- IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 };
+ IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 };
- for (size_t j = 0; j < ASSOC_COUNT; j++) {
+ for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) {
IPPTsPng *const assoc = assocs[j];
if (assoc->timestamp) {
@@ -1817,9 +1850,9 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
if (id_equal(public_key, dht->close_clientlist[i].public_key)) {
const Client_data *const client = &dht->close_clientlist[i];
- const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 };
+ const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 };
- for (size_t j = 0; j < ASSOC_COUNT; j++) {
+ for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) {
const IPPTsPng *const assoc = assocs[j];
if (ip_isset(&assoc->ip_port.ip)) {
@@ -1942,9 +1975,9 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack
}
const Client_data *const client = &dht_friend->client_list[i];
- const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 };
+ const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6 };
- for (size_t j = 0; j < ASSOC_COUNT; j++) {
+ for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) {
const IPPTsPng *const assoc = assocs[j];
/* If ip is not zero and node is good. */
@@ -1984,9 +2017,9 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *
for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
const Client_data *const client = &dht_friend->client_list[i];
- const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 };
+ const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6 };
- for (size_t j = 0; j < ASSOC_COUNT; j++) {
+ for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) {
const IPPTsPng *assoc = assocs[j];
/* If ip is not zero and node is good. */
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.h b/protocols/Tox/libtox/src/toxcore/DHT.h
index 284aa8c02f..3c87804fdc 100644
--- a/protocols/Tox/libtox/src/toxcore/DHT.h
+++ b/protocols/Tox/libtox/src/toxcore/DHT.h
@@ -152,28 +152,10 @@ typedef struct {
}
Node_format;
-typedef struct {
- uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
- Client_data client_list[MAX_FRIEND_CLIENTS];
-
- /* Time at which the last get_nodes request was sent. */
- uint64_t lastgetnode;
- /* number of times get_node packets were sent. */
- uint32_t bootstrap_times;
-
- /* Symetric NAT hole punching stuff. */
- NAT nat;
-
- uint16_t lock_count;
- struct {
- void (*ip_callback)(void *, int32_t, IP_Port);
- void *data;
- int32_t number;
- } callbacks[DHT_FRIEND_MAX_LOCKS];
-
- Node_format to_bootstrap[MAX_SENT_NODES];
- unsigned int num_to_bootstrap;
-} DHT_Friend;
+typedef struct DHT_Friend DHT_Friend;
+
+const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend);
+const Client_data *dht_friend_client(const DHT_Friend *dht_friend, size_t index);
/* Return packet size of packed node with ip_family on success.
* Return -1 on failure.
diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.c b/protocols/Tox/libtox/src/toxcore/Messenger.c
index 6e209f4db3..2408c84484 100644
--- a/protocols/Tox/libtox/src/toxcore/Messenger.c
+++ b/protocols/Tox/libtox/src/toxcore/Messenger.c
@@ -1977,26 +1977,24 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
return nullptr;
}
- Logger *log = nullptr;
+ m->log = logger_new();
- if (options->log_callback) {
- log = logger_new();
-
- if (log != nullptr) {
- logger_callback_log(log, options->log_callback, m, options->log_user_data);
- }
+ if (m->log == nullptr) {
+ friendreq_kill(m->fr);
+ free(m);
+ return nullptr;
}
- m->log = log;
+ logger_callback_log(m->log, options->log_callback, m, options->log_user_data);
unsigned int net_err = 0;
if (options->udp_disabled) {
- m->net = new_networking_no_udp(log);
+ m->net = new_networking_no_udp(m->log);
} else {
IP ip;
ip_init(&ip, options->ipv6enabled);
- m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err);
+ m->net = new_networking_ex(m->log, ip, options->port_range[0], options->port_range[1], &net_err);
}
if (m->net == nullptr) {
@@ -2702,15 +2700,16 @@ void do_messenger(Messenger *m, void *userdata)
} else {
char id_str[IDSTRING_LEN];
LOGGER_TRACE(m->log, "F[--:%2u] %s", friend_idx,
- id_to_string(dhtfptr->public_key, id_str, sizeof(id_str)));
+ id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str)));
}
for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
- Client_data *cptr = &dhtfptr->client_list[client];
- IPPTsPng *assoc = nullptr;
- uint32_t a;
+ const Client_data *cptr = dht_friend_client(dhtfptr, client);
+ const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6};
+
+ for (size_t a = 0; a < sizeof(assocs) / sizeof(assocs[0]); a++) {
+ const IPPTsPng *const assoc = assocs[a];
- for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) {
if (ip_isset(&assoc->ip_port.ip)) {
last_pinged = m->lastdump - assoc->last_pinged;
diff --git a/protocols/Tox/libtox/src/toxcore/ccompat.h b/protocols/Tox/libtox/src/toxcore/ccompat.h
index 1ceb5a5a87..b70850d2d3 100644
--- a/protocols/Tox/libtox/src/toxcore/ccompat.h
+++ b/protocols/Tox/libtox/src/toxcore/ccompat.h
@@ -43,9 +43,9 @@
#endif
#ifdef __GNUC__
-#define GNU_PRINTF __attribute__((__format__(__printf__, 6, 7)))
+#define GNU_PRINTF(f, a) __attribute__((__format__(__printf__, f, a)))
#else
-#define GNU_PRINTF
+#define GNU_PRINTF(f, a)
#endif
#endif /* CCOMPAT_H */
diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.api.h b/protocols/Tox/libtox/src/toxcore/crypto_core.api.h
index bb2c0a13b8..e9e8aeb273 100644
--- a/protocols/Tox/libtox/src/toxcore/crypto_core.api.h
+++ b/protocols/Tox/libtox/src/toxcore/crypto_core.api.h
@@ -117,6 +117,11 @@ static int32_t public_key_cmp(
namespace random {
/**
+ * Return a random 8 bit integer.
+ */
+static uint8_t u08();
+
+/**
* Return a random 16 bit integer.
*/
static uint16_t u16();
diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.c b/protocols/Tox/libtox/src/toxcore/crypto_core.c
index 26589219ed..b2f0e5f0d3 100644
--- a/protocols/Tox/libtox/src/toxcore/crypto_core.c
+++ b/protocols/Tox/libtox/src/toxcore/crypto_core.c
@@ -86,6 +86,13 @@ int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2)
return crypto_verify_32(pk1, pk2);
}
+uint8_t random_u08(void)
+{
+ uint8_t randnum;
+ randombytes(&randnum, 1);
+ return randnum;
+}
+
uint16_t random_u16(void)
{
uint16_t randnum;
diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.h b/protocols/Tox/libtox/src/toxcore/crypto_core.h
index 2c83fd255a..e7e913b681 100644
--- a/protocols/Tox/libtox/src/toxcore/crypto_core.h
+++ b/protocols/Tox/libtox/src/toxcore/crypto_core.h
@@ -127,6 +127,11 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length);
int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2);
/**
+ * Return a random 8 bit integer.
+ */
+uint8_t random_u08(void);
+
+/**
* Return a random 16 bit integer.
*/
uint16_t random_u16(void);
diff --git a/protocols/Tox/libtox/src/toxcore/logger.c b/protocols/Tox/libtox/src/toxcore/logger.c
index 18b765a385..ff34f994ea 100644
--- a/protocols/Tox/libtox/src/toxcore/logger.c
+++ b/protocols/Tox/libtox/src/toxcore/logger.c
@@ -27,6 +27,7 @@
#include "logger.h"
+#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -38,6 +39,42 @@ struct Logger {
void *userdata;
};
+#ifdef USE_STDERR_LOGGER
+static const char *logger_level_name(LOGGER_LEVEL level)
+{
+ switch (level) {
+ case LOG_TRACE:
+ return "TRACE";
+
+ case LOG_DEBUG:
+ return "DEBUG";
+
+ case LOG_INFO:
+ return "INFO";
+
+ case LOG_WARNING:
+ return "WARNING";
+
+ case LOG_ERROR:
+ return "ERROR";
+ }
+
+ return "<unknown>";
+}
+
+static void logger_stderr_handler(void *context, LOGGER_LEVEL level, const char *file, int line, const char *func,
+ const char *message, void *userdata)
+{
+ // GL stands for "global logger".
+ fprintf(stderr, "[GL] %s %s:%d(%s): %s\n", logger_level_name(level), file, line, func, message);
+}
+
+static const Logger logger_stderr = {
+ logger_stderr_handler,
+ nullptr,
+ nullptr,
+};
+#endif
/**
* Public Functions
@@ -59,10 +96,18 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void *
log->userdata = userdata;
}
-void logger_write(Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format,
- ...)
+void logger_write(const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func,
+ const char *format, ...)
{
- if (!log || !log->callback) {
+ if (!log) {
+#ifdef USE_STDERR_LOGGER
+ log = &logger_stderr;
+#else
+ assert(!"NULL logger not permitted");
+#endif
+ }
+
+ if (!log->callback) {
return;
}
diff --git a/protocols/Tox/libtox/src/toxcore/logger.h b/protocols/Tox/libtox/src/toxcore/logger.h
index b3a8f7dc28..acd21fb803 100644
--- a/protocols/Tox/libtox/src/toxcore/logger.h
+++ b/protocols/Tox/libtox/src/toxcore/logger.h
@@ -50,6 +50,9 @@ typedef void logger_cb(void *context, LOGGER_LEVEL level, const char *file, int
*/
Logger *logger_new(void);
+/**
+ * Frees all resources associated with the logger.
+ */
void logger_kill(Logger *log);
/**
@@ -59,10 +62,18 @@ void logger_kill(Logger *log);
void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata);
/**
- * Main write function. If logging disabled does nothing.
+ * Main write function. If logging is disabled, this does nothing.
+ *
+ * If the logger is NULL, this writes to stderr. This behaviour should not be
+ * used in production code, but can be useful for temporarily debugging a
+ * function that does not have a logger available. It's essentially
+ * fprintf(stderr, ...), but with timestamps and source location. Toxcore must
+ * be built with -DUSE_STDERR_LOGGER for this to work. It will cause an
+ * assertion failure otherwise.
*/
void logger_write(
- Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format, ...) GNU_PRINTF;
+ const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func,
+ const char *format, ...) GNU_PRINTF(6, 7);
#define LOGGER_WRITE(log, level, ...) \
diff --git a/protocols/Tox/libtox/src/toxcore/net_crypto.c b/protocols/Tox/libtox/src/toxcore/net_crypto.c
index 65c81d92a0..2e8299f10b 100644
--- a/protocols/Tox/libtox/src/toxcore/net_crypto.c
+++ b/protocols/Tox/libtox/src/toxcore/net_crypto.c
@@ -2755,6 +2755,13 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
*
* return -1 on failure.
* return 0 on success.
+ *
+ * Note: The condition `buffer_end - buffer_start < packet_number - buffer_start` is
+ * a trick which handles situations `buffer_end >= buffer_start` and
+ * `buffer_end < buffer_start`(when buffer_end overflowed) both correctly
+ *
+ * It CANNOT be simplified to `packet_number < buffer_start`, as it will fail
+ * when `buffer_end < buffer_start`.
*/
int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet_number)
{
diff --git a/protocols/Tox/libtox/src/toxcore/network.c b/protocols/Tox/libtox/src/toxcore/network.c
index e9c82c3930..6e19a58610 100644
--- a/protocols/Tox/libtox/src/toxcore/network.c
+++ b/protocols/Tox/libtox/src/toxcore/network.c
@@ -63,15 +63,15 @@
#include <sys/time.h>
#include <sys/types.h>
+#define TOX_EWOULDBLOCK EWOULDBLOCK
+
#else
#ifndef IPV6_V6ONLY
#define IPV6_V6ONLY 27
#endif
-#ifndef EWOULDBLOCK
-#define EWOULDBLOCK WSAEWOULDBLOCK
-#endif
+#define TOX_EWOULDBLOCK WSAEWOULDBLOCK
static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize)
{
@@ -376,10 +376,13 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer,
char ip_str[IP_NTOA_LEN];
if (res < 0) { /* Windows doesn't necessarily know %zu */
+ int error = net_error();
+ char *strerror = net_new_strerror(error);
LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
buffer[0], message, (buflen < 999 ? buflen : 999), 'E',
- ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), errno,
- strerror(errno), data_0(buflen, buffer), data_1(buflen, buffer));
+ ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error,
+ strerror, data_0(buflen, buffer), data_1(buflen, buffer));
+ net_kill_strerror(strerror);
} else if ((res > 0) && ((size_t)res <= buflen)) {
LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='),
@@ -423,7 +426,7 @@ uint16_t net_port(const Networking_Core *net)
*/
int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length)
{
- if (net->family == 0) { /* Socket not initialized */
+ if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */
return -1;
}
@@ -436,42 +439,35 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
size_t addrsize = 0;
+ if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) {
+ /* must convert to IPV4-in-IPV6 address */
+ IP6 ip6;
+
+ /* there should be a macro for this in a standards compliant
+ * environment, not found */
+ ip6.uint32[0] = 0;
+ ip6.uint32[1] = 0;
+ ip6.uint32[2] = net_htonl(0xFFFF);
+ ip6.uint32[3] = ip_port.ip.ip.v4.uint32;
+
+ ip_port.ip.family = TOX_AF_INET6;
+ ip_port.ip.ip.v6 = ip6;
+ }
+
if (ip_port.ip.family == TOX_AF_INET) {
- if (net->family == TOX_AF_INET6) {
- /* must convert to IPV4-in-IPV6 address */
- struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
-
- addrsize = sizeof(struct sockaddr_in6);
- addr6->sin6_family = AF_INET6;
- addr6->sin6_port = ip_port.port;
-
- /* there should be a macro for this in a standards compliant
- * environment, not found */
- IP6 ip6;
-
- ip6.uint32[0] = 0;
- ip6.uint32[1] = 0;
- ip6.uint32[2] = net_htonl(0xFFFF);
- ip6.uint32[3] = ip_port.ip.ip.v4.uint32;
- fill_addr6(ip6, &addr6->sin6_addr);
-
- addr6->sin6_flowinfo = 0;
- addr6->sin6_scope_id = 0;
- } else {
- struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
+ struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
- addrsize = sizeof(struct sockaddr_in);
- addr4->sin_family = AF_INET;
- fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr);
- addr4->sin_port = ip_port.port;
- }
+ addrsize = sizeof(struct sockaddr_in);
+ fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr);
+ addr4->sin_family = AF_INET;
+ addr4->sin_port = ip_port.port;
} else if (ip_port.ip.family == TOX_AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
addrsize = sizeof(struct sockaddr_in6);
+ fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr);
addr6->sin6_family = AF_INET6;
addr6->sin6_port = ip_port.port;
- fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr);
addr6->sin6_flowinfo = 0;
addr6->sin6_scope_id = 0;
@@ -480,7 +476,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
return -1;
}
- int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
+ const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
loglogdata(net->log, "O=>", data, length, ip_port, res);
@@ -505,9 +501,12 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da
int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (fail_or_len < 0) {
+ int error = net_error();
- if (fail_or_len < 0 && errno != EWOULDBLOCK) {
- LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
+ if (fail_or_len < 0 && error != TOX_EWOULDBLOCK) {
+ char *strerror = net_new_strerror(error);
+ LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s", error, strerror);
+ net_kill_strerror(strerror);
}
return -1; /* Nothing received. */
@@ -664,7 +663,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
/* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) {
- LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family);
+ LOGGER_ERROR(log, "Invalid address family: %u", ip.family);
return nullptr;
}
@@ -688,7 +687,10 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
/* Check for socket error. */
if (!sock_valid(temp->sock)) {
- LOGGER_ERROR(log, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
+ int neterror = net_error();
+ char *strerror = net_new_strerror(neterror);
+ LOGGER_ERROR(log, "Failed to get a socket?! %d, %s", neterror, strerror);
+ net_kill_strerror(strerror);
free(temp);
if (error) {
@@ -776,8 +778,11 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
mreq.ipv6mr_interface = 0;
int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
- LOGGER_DEBUG(log, res < 0 ? "Failed to activate local multicast membership. (%u, %s)" :
- "Local multicast group FF02::1 joined successfully", errno, strerror(errno));
+ int neterror = net_error();
+ char *strerror = net_new_strerror(neterror);
+ LOGGER_DEBUG(log, res < 0 ? "Failed to activate local multicast membership. (%d, %s)" :
+ "Local multicast group FF02::1 joined successfully", neterror, strerror);
+ net_kill_strerror(strerror);
}
/* a hanging program or a different user might block the standard port;
@@ -834,9 +839,11 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
}
char ip_str[IP_NTOA_LEN];
- LOGGER_ERROR(log, "Failed to bind socket: %u, %s IP: %s port_from: %u port_to: %u", errno, strerror(errno),
+ int neterror = net_error();
+ char *strerror = net_new_strerror(neterror);
+ LOGGER_ERROR(log, "Failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", neterror, strerror,
ip_ntoa(&ip, ip_str, sizeof(ip_str)), port_from, port_to);
-
+ net_kill_strerror(strerror);
kill_networking(temp);
if (error) {
@@ -1527,3 +1534,31 @@ size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
*v = ((uint64_t)hi << 32) | lo;
return p - bytes;
}
+
+int net_error(void)
+{
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+ return WSAGetLastError();
+#else
+ return errno;
+#endif
+}
+
+char *net_new_strerror(int error)
+{
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+ char *str = nullptr;
+ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
+ error, 0, (char *)&str, 0, nullptr);
+ return str;
+#else
+ return strerror(error);
+#endif
+}
+
+void net_kill_strerror(char *strerror)
+{
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+ LocalFree(strerror);
+#endif
+}
diff --git a/protocols/Tox/libtox/src/toxcore/network.h b/protocols/Tox/libtox/src/toxcore/network.h
index 405721b290..193a02faf8 100644
--- a/protocols/Tox/libtox/src/toxcore/network.h
+++ b/protocols/Tox/libtox/src/toxcore/network.h
@@ -404,6 +404,34 @@ void net_freeipport(IP_Port *ip_ports);
*/
int bind_to_port(Socket sock, int family, uint16_t port);
+/* Get the last networking error code.
+ *
+ * Similar to Unix's errno, but cross-platform, as not all platforms use errno
+ * to indicate networking errors.
+ *
+ * Note that different platforms may return different codes for the same error,
+ * so you likely shouldn't be checking the value returned by this function
+ * unless you know what you are doing, you likely just want to use it in
+ * combination with net_new_strerror() to print the error.
+ *
+ * return platform-dependent network error code, if any.
+ */
+int net_error(void);
+
+/* Get a text explanation for the error code from net_error().
+ *
+ * return NULL on failure.
+ * return pointer to a NULL-terminated string describing the error code on
+ * success. The returned string must be freed using net_kill_strerror().
+ */
+char *net_new_strerror(int error);
+
+/* Frees the string returned by net_new_strerror().
+ * It's valid to pass NULL as the argument, the function does nothing in this
+ * case.
+ */
+void net_kill_strerror(char *strerror);
+
/* Initialize networking.
* bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001).
diff --git a/protocols/Tox/libtox/src/toxcore/tox.api.h b/protocols/Tox/libtox/src/toxcore/tox.api.h
index 59af69b9c3..d6148414be 100644
--- a/protocols/Tox/libtox/src/toxcore/tox.api.h
+++ b/protocols/Tox/libtox/src/toxcore/tox.api.h
@@ -179,7 +179,7 @@ const VERSION_MINOR = 2;
* The patch or revision number. Incremented when bugfixes are applied without
* changing any functionality or API or ABI.
*/
-const VERSION_PATCH = 0;
+const VERSION_PATCH = 1;
/**
* A macro to check at preprocessing time whether the client code is compatible
diff --git a/protocols/Tox/libtox/src/toxcore/tox.h b/protocols/Tox/libtox/src/toxcore/tox.h
index 41a0994ed6..c65f1e2566 100644
--- a/protocols/Tox/libtox/src/toxcore/tox.h
+++ b/protocols/Tox/libtox/src/toxcore/tox.h
@@ -180,7 +180,7 @@ uint32_t tox_version_minor(void);
* The patch or revision number. Incremented when bugfixes are applied without
* changing any functionality or API or ABI.
*/
-#define TOX_VERSION_PATCH 0
+#define TOX_VERSION_PATCH 1
uint32_t tox_version_patch(void);
diff --git a/protocols/Tox/libtox/src/toxcore/tox_api.c b/protocols/Tox/libtox/src/toxcore/tox_api.c
index 6c0bd71fca..8fee8c804a 100644
--- a/protocols/Tox/libtox/src/toxcore/tox_api.c
+++ b/protocols/Tox/libtox/src/toxcore/tox_api.c
@@ -19,6 +19,7 @@ CONST_FUNCTION(version_minor, VERSION_MINOR)
CONST_FUNCTION(version_patch, VERSION_PATCH)
CONST_FUNCTION(public_key_size, PUBLIC_KEY_SIZE)
CONST_FUNCTION(secret_key_size, SECRET_KEY_SIZE)
+CONST_FUNCTION(nospam_size, NOSPAM_SIZE)
CONST_FUNCTION(address_size, ADDRESS_SIZE)
CONST_FUNCTION(max_name_length, MAX_NAME_LENGTH)
CONST_FUNCTION(max_status_message_length, MAX_STATUS_MESSAGE_LENGTH)