Skip to content

Commit

Permalink
Merge b189637 into 751d094
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jul 9, 2018
2 parents 751d094 + b189637 commit 863eebe
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 101 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/logger.h
toxcore/network.c
toxcore/network.h
toxcore/state.c
toxcore/state.h
toxcore/util.c
toxcore/util.h)

Expand Down
13 changes: 12 additions & 1 deletion toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ cc_library(
deps = [":ccompat"],
)

cc_library(
name = "state",
srcs = ["state.c"],
hdrs = ["state.h"],
deps = [":logger"],
)

cc_library(
name = "network",
srcs = [
Expand Down Expand Up @@ -106,6 +113,7 @@ cc_library(
":crypto_core",
":logger",
":ping_array",
":state",
],
)

Expand Down Expand Up @@ -190,7 +198,10 @@ cc_library(
srcs = ["Messenger.c"],
hdrs = ["Messenger.h"],
visibility = ["//c-toxcore/toxav:__pkg__"],
deps = [":friend_requests"],
deps = [
":friend_requests",
":state",
],
)

cc_library(
Expand Down
7 changes: 4 additions & 3 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "logger.h"
#include "network.h"
#include "ping.h"
#include "state.h"
#include "util.h"

#include <assert.h>
Expand Down Expand Up @@ -2887,7 +2888,7 @@ int dht_connect_after_load(DHT *dht)
return 0;
}

static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
{
DHT *dht = (DHT *)outer;

Expand Down Expand Up @@ -2918,7 +2919,7 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
break;
}

return 0;
return STATE_LOAD_STATUS_CONTINUE;
}

/* Load the DHT from data of size size.
Expand All @@ -2935,7 +2936,7 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length)
lendian_to_host32(&data32, data);

if (data32 == DHT_STATE_COOKIE_GLOBAL) {
return load_state(dht_load_state_callback, dht->log, dht, data + cookie_len,
return state_load(dht->log, dht_load_state_callback, dht, data + cookie_len,
length - cookie_len, DHT_STATE_COOKIE_TYPE);
}
}
Expand Down
2 changes: 2 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ libtoxcore_la_SOURCES = ../toxcore/ccompat.h \
../toxcore/Messenger.c \
../toxcore/ping.h \
../toxcore/ping.c \
../toxcore/state.h \
../toxcore/state.c \
../toxcore/tox.h \
../toxcore/tox.c \
../toxcore/tox_api.c \
Expand Down
15 changes: 8 additions & 7 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "logger.h"
#include "network.h"
#include "state.h"
#include "util.h"

static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
Expand Down Expand Up @@ -3075,7 +3076,7 @@ void messenger_save(const Messenger *m, uint8_t *data)
messenger_save_subheader(data, 0, MESSENGER_STATE_TYPE_END);
}

static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
static State_Load_Status messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
{
Messenger *m = (Messenger *)outer;

Expand All @@ -3086,10 +3087,10 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
load_secret_key(m->net_crypto, (&data[sizeof(uint32_t)]) + CRYPTO_PUBLIC_KEY_SIZE);

if (public_key_cmp((&data[sizeof(uint32_t)]), nc_get_self_public_key(m->net_crypto)) != 0) {
return -1;
return STATE_LOAD_STATUS_ERROR;
}
} else {
return -1; /* critical */
return STATE_LOAD_STATUS_ERROR; /* critical */
}

break;
Expand Down Expand Up @@ -3152,10 +3153,10 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3

case MESSENGER_STATE_TYPE_END: {
if (length != 0) {
return -1;
return STATE_LOAD_STATUS_ERROR;
}

return -2;
return STATE_LOAD_STATUS_END;
}

default:
Expand All @@ -3164,7 +3165,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
break;
}

return 0;
return STATE_LOAD_STATUS_CONTINUE;
}

/* Load the messenger from data of size length. */
Expand All @@ -3181,7 +3182,7 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
lendian_to_host32(data32 + 1, data + sizeof(uint32_t));

if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) {
return load_state(messenger_load_state_callback, m->log, m, data + cookie_len,
return state_load(m->log, messenger_load_state_callback, m, data + cookie_len,
length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
}

Expand Down
90 changes: 90 additions & 0 deletions toxcore/state.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "state.h"

#include <string.h>

/* state load/save */
int state_load(const Logger *log, state_load_cb *state_load_callback, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner)
{
if (state_load_callback == nullptr || data == nullptr) {
LOGGER_ERROR(log, "state_load() called with invalid args.\n");
return -1;
}


const uint32_t size_head = sizeof(uint32_t) * 2;

while (length >= size_head) {
uint32_t length_sub;
lendian_to_host32(&length_sub, data);

uint32_t cookie_type;
lendian_to_host32(&cookie_type, data + sizeof(uint32_t));

data += size_head;
length -= size_head;

if (length < length_sub) {
/* file truncated */
LOGGER_ERROR(log, "state file too short: %u < %u\n", length, length_sub);
return -1;
}

if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
/* something is not matching up in a bad way, give up */
LOGGER_ERROR(log, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
return -1;
}

const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF);

switch (state_load_callback(outer, data, length_sub, type)) {
case STATE_LOAD_STATUS_CONTINUE:
data += length_sub;
length -= length_sub;
break;

case STATE_LOAD_STATUS_ERROR:
return -1;

case STATE_LOAD_STATUS_END:
return 0;
}
}

if (length != 0) {
LOGGER_ERROR(log, "unparsed data in state file of length %u\n", length);
return -1;
}

return 0;
}

uint16_t lendian_to_host16(uint16_t lendian)
{
#ifdef WORDS_BIGENDIAN
return (lendian << 8) | (lendian >> 8);
#else
return lendian;
#endif
}

void host_to_lendian32(uint8_t *dest, uint32_t num)
{
#ifdef WORDS_BIGENDIAN
num = ((num << 8) & 0xFF00FF00) | ((num >> 8) & 0xFF00FF);
num = (num << 16) | (num >> 16);
#endif
memcpy(dest, &num, sizeof(uint32_t));
}

void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
{
uint32_t d;
memcpy(&d, lendian, sizeof(uint32_t));
#ifdef WORDS_BIGENDIAN
d = ((d << 8) & 0xFF00FF00) | ((d >> 8) & 0xFF00FF);
d = (d << 16) | (d >> 16);
#endif
*dest = d;
}
38 changes: 38 additions & 0 deletions toxcore/state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef C_TOXCORE_TOXCORE_STATE_H
#define C_TOXCORE_TOXCORE_STATE_H

#include "logger.h"

#ifdef __cplusplus
extern "C" {
#endif

// Returned by the state_load_cb to instruct the loader on what to do next.
typedef enum State_Load_Status {
// Continue loading state data sections.
STATE_LOAD_STATUS_CONTINUE,
// An error occurred. Stop loading sections.
STATE_LOAD_STATUS_ERROR,
// We're at the end of the save data, terminate loading successfully.
STATE_LOAD_STATUS_END,
} State_Load_Status;

typedef State_Load_Status state_load_cb(void *outer, const uint8_t *data, uint32_t len, uint16_t type);

// state load/save
int state_load(const Logger *log, state_load_cb *state_load_callback, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner);

// Utilities for state data serialisation.

uint16_t lendian_to_host16(uint16_t lendian);
#define host_tolendian16(x) lendian_to_host16(x)

void host_to_lendian32(uint8_t *dest, uint32_t num);
void lendian_to_host32(uint32_t *dest, const uint8_t *lendian);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // C_TOXCORE_TOXCORE_STATE_H
79 changes: 0 additions & 79 deletions toxcore/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,85 +92,6 @@ void host_to_net(uint8_t *num, uint16_t numbytes)
#endif
}

uint16_t lendian_to_host16(uint16_t lendian)
{
#ifdef WORDS_BIGENDIAN
return (lendian << 8) | (lendian >> 8);
#else
return lendian;
#endif
}

void host_to_lendian32(uint8_t *dest, uint32_t num)
{
#ifdef WORDS_BIGENDIAN
num = ((num << 8) & 0xFF00FF00) | ((num >> 8) & 0xFF00FF);
num = (num << 16) | (num >> 16);
#endif
memcpy(dest, &num, sizeof(uint32_t));
}

void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
{
uint32_t d;
memcpy(&d, lendian, sizeof(uint32_t));
#ifdef WORDS_BIGENDIAN
d = ((d << 8) & 0xFF00FF00) | ((d >> 8) & 0xFF00FF);
d = (d << 16) | (d >> 16);
#endif
*dest = d;
}

/* state load/save */
int load_state(load_state_callback_func load_state_callback, const Logger *log, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner)
{
if (!load_state_callback || !data) {
LOGGER_ERROR(log, "load_state() called with invalid args.\n");
return -1;
}


uint32_t length_sub, cookie_type;
uint32_t size_head = sizeof(uint32_t) * 2;

while (length >= size_head) {
lendian_to_host32(&length_sub, data);
lendian_to_host32(&cookie_type, data + sizeof(length_sub));
data += size_head;
length -= size_head;

if (length < length_sub) {
/* file truncated */
LOGGER_ERROR(log, "state file too short: %u < %u\n", length, length_sub);
return -1;
}

if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
/* something is not matching up in a bad way, give up */
LOGGER_ERROR(log, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
return -1;
}

const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF);
const int ret = load_state_callback(outer, data, length_sub, type);

if (ret == -1) {
return -1;
}

/* -2 means end of save. */
if (ret == -2) {
return 0;
}

data += length_sub;
length -= length_sub;
}

return length == 0 ? 0 : -1;
}

int create_recursive_mutex(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr;
Expand Down
11 changes: 0 additions & 11 deletions toxcore/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_I
void host_to_net(uint8_t *num, uint16_t numbytes);
#define net_to_host(x, y) host_to_net(x, y)

uint16_t lendian_to_host16(uint16_t lendian);
#define host_tolendian16(x) lendian_to_host16(x)

void host_to_lendian32(uint8_t *dest, uint32_t num);
void lendian_to_host32(uint32_t *dest, const uint8_t *lendian);

/* state load/save */
typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type);
int load_state(load_state_callback_func load_state_callback, const Logger *log, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner);

/* Returns -1 if failed or 0 if success */
int create_recursive_mutex(pthread_mutex_t *mutex);

Expand Down

0 comments on commit 863eebe

Please sign in to comment.