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