From 1d2f4465bf634f45704eb69791c45bd623154909 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 16 Sep 2013 11:22:01 -0400 Subject: Toxcore should never print anything when built normally. --- toxcore/DHT.c | 26 +++++++++++++++++++++----- toxcore/Messenger.c | 33 +++++++++++++++++++++++---------- toxcore/network.c | 6 ++++++ toxcore/util.c | 10 +++++++++- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index bb746fb0..ddd1e1e0 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -202,6 +202,7 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod int num_nodes = *num_nodes_ptr; int tout, inlist, ipv46x, j, closest; uint32_t i; + for (i = 0; i < client_list_length; i++) { Client_data *client = &client_list[i]; tout = is_timeout(timestamp, client->timestamp, BAD_NODE_TIMEOUT); @@ -1477,14 +1478,18 @@ void DHT_save_old(DHT *dht, uint8_t *data) int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size) { if (size < sizeof(dht->close_clientlist)) { +#ifdef DEBUG fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size); +#endif return -1; } uint32_t friendlistsize = size - sizeof(dht->close_clientlist); if (friendlistsize % sizeof(DHT_Friend) != 0) { +#ifdef DEBUG fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize); +#endif return -1; } @@ -1531,14 +1536,15 @@ int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size) uint32_t DHT_size(DHT *dht) { uint32_t num = 0, i; + for (i = 0; i < LCLIENT_LIST; ++i) if (dht->close_clientlist[i].timestamp != 0) num++; uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; return size32 - + sizesubhead + sizeof(DHT_Friend) * dht->num_friends - + sizesubhead + sizeof(Client_data) * num; + + sizesubhead + sizeof(DHT_Friend) * dht->num_friends + + sizesubhead + sizeof(Client_data) * num; } static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) @@ -1565,6 +1571,7 @@ void DHT_save(DHT *dht, uint8_t *data) data += len; uint32_t num = 0, i; + for (i = 0; i < LCLIENT_LIST; ++i) if (dht->close_clientlist[i].timestamp != 0) num++; @@ -1576,9 +1583,11 @@ void DHT_save(DHT *dht, uint8_t *data) type = DHT_STATE_TYPE_CLIENTS; data = z_state_save_subheader(data, len, type); Client_data *clients = (Client_data *)data; + for (num = 0, i = 0; i < LCLIENT_LIST; ++i) if (dht->close_clientlist[i].timestamp != 0) memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); + data += len; } @@ -1586,17 +1595,21 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, { DHT *dht = outer; uint32_t num, i, j; - switch(type) { + + switch (type) { case DHT_STATE_TYPE_FRIENDS: if (length % sizeof(DHT_Friend) != 0) break; DHT_Friend *friend_list = (DHT_Friend *)data; num = length / sizeof(DHT_Friend); + for (i = 0; i < num; ++i) { DHT_addfriend(dht, friend_list[i].client_id); + for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { Client_data *client = &friend_list[i].client_list[j]; + if (client->timestamp != 0) getnodes(dht, client->ip_port, client->client_id, friend_list[i].client_id); } @@ -1610,6 +1623,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, num = length / sizeof(Client_data); Client_data *client_list = (Client_data *)data; + for (i = 0; i < num; ++i) if (client_list[i].timestamp != 0) DHT_bootstrap(dht, client_list[i].ip_port, client_list[i].client_id); @@ -1618,7 +1632,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, default: fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", - length, type); + length, type); } return 0; @@ -1632,11 +1646,13 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) { uint32_t cookie_len = sizeof(uint32_t); + if (length > cookie_len) { uint32_t *data32 = (uint32_t *)data; + if (data32[0] == DHT_STATE_COOKIE_GLOBAL) return load_state(dht_load_state_callback, dht, data + cookie_len, - length - cookie_len, DHT_STATE_COOKIE_TYPE); + length - cookie_len, DHT_STATE_COOKIE_TYPE); } return DHT_load_old(dht, data, length); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index f2be6008..cdc133d0 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1393,6 +1393,7 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length) length -= sizeof(nospam); uint32_t size; + if (length < sizeof(size)) return -1; @@ -1404,8 +1405,11 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length) return -1; if (DHT_load_old(m->dht, data, size) == -1) +#ifdef DEBUG fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); - /* DO go on, friends/name still might be intact */ + +#endif + /* DO go on, friends/name still might be intact */ data += size; length -= size; @@ -1425,6 +1429,7 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length) Friend *friend_list = (Friend *)data; uint32_t i; + for (i = 0; i < num; ++i) { if (friend_list[i].status >= 3) { int fnum = m_addfriend_norequest(m, friend_list[i].client_id); @@ -1478,11 +1483,11 @@ uint32_t Messenger_size(Messenger *m) { uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; return size32 * 2 // global cookie - + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES - + sizesubhead + DHT_size(m->dht) // DHT - + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself. - + sizesubhead + m->name_length // Own nickname. - ; + + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + + sizesubhead + DHT_size(m->dht) // DHT + + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself. + + sizesubhead + m->name_length // Own nickname. + ; } static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) @@ -1538,14 +1543,15 @@ void Messenger_save(Messenger *m, uint8_t *data) static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) { Messenger *m = outer; - switch(type) { + + switch (type) { case MESSENGER_STATE_TYPE_NOSPAMKEYS: if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) { set_nospam(&(m->fr), *(uint32_t *)data); load_keys(m->net_crypto, &data[sizeof(uint32_t)]); - } - else + } else return -1; /* critical */ + break; case MESSENGER_STATE_TYPE_DHT: @@ -1557,6 +1563,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le uint16_t num = length / sizeof(Friend); Friend *friends = (Friend *)data; uint32_t i; + for (i = 0; i < num; ++i) { if (friends[i].status >= 3) { int fnum = m_addfriend_norequest(m, friends[i].client_id); @@ -1573,17 +1580,21 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le } } } + break; case MESSENGER_STATE_TYPE_NAME: if ((length > 0) && (length < MAX_NAME_LENGTH)) { setname(m, data, length); } + break; default: +#ifdef DEBUG fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", - length, type); + length, type); +#endif } return 0; @@ -1593,10 +1604,12 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) { uint32_t cookie_len = 2 * sizeof(uint32_t); + if (length < cookie_len) return -1; uint32_t *data32 = (uint32_t *)data; + if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) return load_state(messenger_load_state_callback, m, data + cookie_len, length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); diff --git a/toxcore/network.c b/toxcore/network.c index 4f6b0aae..a44ef4c4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -277,7 +277,9 @@ Networking_Core *new_networking(IP ip, uint16_t port) /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ if (ip.family != AF_INET && ip.family != AF_INET6) { +#ifdef DEBUG fprintf(stderr, "Invalid address family: %u\n", ip.family); +#endif return NULL; } @@ -313,7 +315,9 @@ Networking_Core *new_networking(IP ip, uint16_t port) #else if (temp->sock < 0) { +#ifdef DEBUG fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno)); +#endif free(temp); return NULL; } @@ -478,8 +482,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) *portptr = htons(port_to_try); } +#ifdef DEBUG fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, strerror(errno), ip_ntoa(&ip), port); +#endif kill_networking(temp); return NULL; } diff --git a/toxcore/util.c b/toxcore/util.c index 28f7d26e..51b31e7b 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -44,10 +44,12 @@ void id_cpy(uint8_t *dest, uint8_t *src) } int load_state(load_state_callback_func load_state_callback, void *outer, - uint8_t *data, uint32_t length, uint16_t cookie_inner) + 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; } @@ -55,6 +57,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, 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); @@ -63,17 +66,22 @@ int load_state(load_state_callback_func load_state_callback, void *outer, 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; -- cgit v1.2.3