toxcore/toxcore/util.c
Coren[m] a0f08839bd Main: Eliminate TOX_ENABLE_IPV6 (then always on), CLIENT_ONETOONE_IP (then always off).
Additionally (besides cleanups):

network.h/tox.h:
- WIN32: fix a strange sa_family_t definition
- WIN32: define EWOULDBLOCK to WSAEWOULDBLOCK
- WIN32: kill macro for an existing function (IN6_ADDR_EQUAL)

network.c:
- use EWOULDBLOCK instead of EAGAIN (same value, but EWOULDBLOCK is more "popular")
- new_networking(): only try to enable IPv4-in-IPv6 if it's not already enabled per default
- inet_ntop()/inet_pton(): WIN32: remove partial initializers in favor of a simple memset()
- ip_equal(): WIN32: use an existing function
- logging: networking_wait_execute(): only dump result if not timeout
- logging: loglogdata(): kill an unused variable

LAN_discovery.c:
- send_broadcasts(): re-enabled, can only support IPv4 by principle, split into fetch_broadcast_info() (to fetch the addresses once) and send_broadcasts() (actual sending)

DHT.c:
- DHT_load_state_callback(): enclosed a fprintf(stderr, ...) into #ifdef DEBUG

Lossless_UDP.c:
- change_handshake(): harden against strange sa_family_t definitions

Messenger.c:
- logging: fix ID to string conversion

util.c:
- logging: eliminate a signed-warning
2013-10-20 16:56:12 +02:00

174 lines
3.8 KiB
C

/*
* util.c -- Utilities.
*
* This file is donated to the Tox Project.
* Copyright 2013 plutooo
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <time.h>
/* for CLIENT_ID_SIZE */
#include "DHT.h"
#include "util.h"
uint64_t now()
{
return time(NULL);
}
uint64_t random_64b()
{
uint64_t r;
/* This is probably not random enough? */
r = random_int();
r <<= 32;
r |= random_int();
return r;
}
bool id_eq(uint8_t *dest, uint8_t *src)
{
return memcmp(dest, src, CLIENT_ID_SIZE) == 0;
}
void id_cpy(uint8_t *dest, uint8_t *src)
{
memcpy(dest, src, CLIENT_ID_SIZE);
}
int load_state(load_state_callback_func load_state_callback, void *outer,
uint8_t *data, uint32_t length, uint16_t cookie_inner)
{
if (!load_state_callback || !data) {
#ifdef DEBUG
fprintf(stderr, "load_state() called with invalid args.\n");
#endif
return -1;
}
uint16_t type;
uint32_t length_sub, cookie_type;
uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2;
while (length >= size_head) {
length_sub = *(uint32_t *)data;
cookie_type = *(uint32_t *)(data + size32);
data += size_head;
length -= size_head;
if (length < length_sub) {
/* file truncated */
#ifdef DEBUG
fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
#endif
return -1;
}
if ((cookie_type >> 16) != cookie_inner) {
/* something is not matching up in a bad way, give up */
#ifdef DEBUG
fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner);
#endif
return -1;
}
type = cookie_type & 0xFFFF;
if (-1 == load_state_callback(outer, data, length_sub, type))
return -1;
data += length_sub;
length -= length_sub;
}
return length == 0 ? 0 : -1;
};
#ifdef LOGGING
time_t starttime = 0;
size_t logbufferprelen = 0;
char *logbufferpredata = NULL;
char *logbufferprehead = NULL;
char logbuffer[512];
static FILE *logfile = NULL;
void loginit(uint16_t port)
{
if (logfile)
fclose(logfile);
if (!starttime)
starttime = now();
struct tm *tm = localtime(&starttime);
/* "%F %T" might not be Windows compatible */
if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm))
sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32);
else
sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime);
logfile = fopen(logbuffer, "w");
if (logbufferpredata) {
if (logfile)
fprintf(logfile, "%s", logbufferpredata);
free(logbufferpredata);
logbufferpredata = NULL;
}
};
void loglog(char *text)
{
if (logfile) {
fprintf(logfile, "%4u %s", (uint32_t)(now() - starttime), text);
fflush(logfile);
return;
}
/* log messages before file was opened: store */
size_t len = strlen(text);
if (!starttime) {
starttime = now();
logbufferprelen = 1024 + len - (len % 1024);
logbufferpredata = malloc(logbufferprelen);
logbufferprehead = logbufferpredata;
}
/* loginit() called meanwhile? (but failed to open) */
if (!logbufferpredata)
return;
if (len + (logbufferprehead - logbufferpredata) + 16U < logbufferprelen) {
size_t logpos = logbufferprehead - logbufferpredata;
size_t lennew = logbufferprelen * 1.4;
logbufferpredata = realloc(logbufferpredata, lennew);
logbufferprehead = logbufferpredata + logpos;
logbufferprelen = lennew;
}
int written = sprintf(logbufferprehead, "%4u %s", (uint32_t)(now() - starttime), text);
logbufferprehead += written;
}
void logexit()
{
if (logfile) {
fclose(logfile);
logfile = NULL;
}
};
#endif