summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c26
-rw-r--r--toxcore/Messenger.c33
-rw-r--r--toxcore/network.c6
-rw-r--r--toxcore/util.c10
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
202 int num_nodes = *num_nodes_ptr; 202 int num_nodes = *num_nodes_ptr;
203 int tout, inlist, ipv46x, j, closest; 203 int tout, inlist, ipv46x, j, closest;
204 uint32_t i; 204 uint32_t i;
205
205 for (i = 0; i < client_list_length; i++) { 206 for (i = 0; i < client_list_length; i++) {
206 Client_data *client = &client_list[i]; 207 Client_data *client = &client_list[i];
207 tout = is_timeout(timestamp, client->timestamp, BAD_NODE_TIMEOUT); 208 tout = is_timeout(timestamp, client->timestamp, BAD_NODE_TIMEOUT);
@@ -1477,14 +1478,18 @@ void DHT_save_old(DHT *dht, uint8_t *data)
1477int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size) 1478int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size)
1478{ 1479{
1479 if (size < sizeof(dht->close_clientlist)) { 1480 if (size < sizeof(dht->close_clientlist)) {
1481#ifdef DEBUG
1480 fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size); 1482 fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size);
1483#endif
1481 return -1; 1484 return -1;
1482 } 1485 }
1483 1486
1484 uint32_t friendlistsize = size - sizeof(dht->close_clientlist); 1487 uint32_t friendlistsize = size - sizeof(dht->close_clientlist);
1485 1488
1486 if (friendlistsize % sizeof(DHT_Friend) != 0) { 1489 if (friendlistsize % sizeof(DHT_Friend) != 0) {
1490#ifdef DEBUG
1487 fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize); 1491 fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize);
1492#endif
1488 return -1; 1493 return -1;
1489 } 1494 }
1490 1495
@@ -1531,14 +1536,15 @@ int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size)
1531uint32_t DHT_size(DHT *dht) 1536uint32_t DHT_size(DHT *dht)
1532{ 1537{
1533 uint32_t num = 0, i; 1538 uint32_t num = 0, i;
1539
1534 for (i = 0; i < LCLIENT_LIST; ++i) 1540 for (i = 0; i < LCLIENT_LIST; ++i)
1535 if (dht->close_clientlist[i].timestamp != 0) 1541 if (dht->close_clientlist[i].timestamp != 0)
1536 num++; 1542 num++;
1537 1543
1538 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 1544 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
1539 return size32 1545 return size32
1540 + sizesubhead + sizeof(DHT_Friend) * dht->num_friends 1546 + sizesubhead + sizeof(DHT_Friend) * dht->num_friends
1541 + sizesubhead + sizeof(Client_data) * num; 1547 + sizesubhead + sizeof(Client_data) * num;
1542} 1548}
1543 1549
1544static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 1550static 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)
1565 data += len; 1571 data += len;
1566 1572
1567 uint32_t num = 0, i; 1573 uint32_t num = 0, i;
1574
1568 for (i = 0; i < LCLIENT_LIST; ++i) 1575 for (i = 0; i < LCLIENT_LIST; ++i)
1569 if (dht->close_clientlist[i].timestamp != 0) 1576 if (dht->close_clientlist[i].timestamp != 0)
1570 num++; 1577 num++;
@@ -1576,9 +1583,11 @@ void DHT_save(DHT *dht, uint8_t *data)
1576 type = DHT_STATE_TYPE_CLIENTS; 1583 type = DHT_STATE_TYPE_CLIENTS;
1577 data = z_state_save_subheader(data, len, type); 1584 data = z_state_save_subheader(data, len, type);
1578 Client_data *clients = (Client_data *)data; 1585 Client_data *clients = (Client_data *)data;
1586
1579 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) 1587 for (num = 0, i = 0; i < LCLIENT_LIST; ++i)
1580 if (dht->close_clientlist[i].timestamp != 0) 1588 if (dht->close_clientlist[i].timestamp != 0)
1581 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); 1589 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
1590
1582 data += len; 1591 data += len;
1583} 1592}
1584 1593
@@ -1586,17 +1595,21 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
1586{ 1595{
1587 DHT *dht = outer; 1596 DHT *dht = outer;
1588 uint32_t num, i, j; 1597 uint32_t num, i, j;
1589 switch(type) { 1598
1599 switch (type) {
1590 case DHT_STATE_TYPE_FRIENDS: 1600 case DHT_STATE_TYPE_FRIENDS:
1591 if (length % sizeof(DHT_Friend) != 0) 1601 if (length % sizeof(DHT_Friend) != 0)
1592 break; 1602 break;
1593 1603
1594 DHT_Friend *friend_list = (DHT_Friend *)data; 1604 DHT_Friend *friend_list = (DHT_Friend *)data;
1595 num = length / sizeof(DHT_Friend); 1605 num = length / sizeof(DHT_Friend);
1606
1596 for (i = 0; i < num; ++i) { 1607 for (i = 0; i < num; ++i) {
1597 DHT_addfriend(dht, friend_list[i].client_id); 1608 DHT_addfriend(dht, friend_list[i].client_id);
1609
1598 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 1610 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
1599 Client_data *client = &friend_list[i].client_list[j]; 1611 Client_data *client = &friend_list[i].client_list[j];
1612
1600 if (client->timestamp != 0) 1613 if (client->timestamp != 0)
1601 getnodes(dht, client->ip_port, client->client_id, friend_list[i].client_id); 1614 getnodes(dht, client->ip_port, client->client_id, friend_list[i].client_id);
1602 } 1615 }
@@ -1610,6 +1623,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
1610 1623
1611 num = length / sizeof(Client_data); 1624 num = length / sizeof(Client_data);
1612 Client_data *client_list = (Client_data *)data; 1625 Client_data *client_list = (Client_data *)data;
1626
1613 for (i = 0; i < num; ++i) 1627 for (i = 0; i < num; ++i)
1614 if (client_list[i].timestamp != 0) 1628 if (client_list[i].timestamp != 0)
1615 DHT_bootstrap(dht, client_list[i].ip_port, client_list[i].client_id); 1629 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,
1618 1632
1619 default: 1633 default:
1620 fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", 1634 fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
1621 length, type); 1635 length, type);
1622 } 1636 }
1623 1637
1624 return 0; 1638 return 0;
@@ -1632,11 +1646,13 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
1632int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) 1646int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length)
1633{ 1647{
1634 uint32_t cookie_len = sizeof(uint32_t); 1648 uint32_t cookie_len = sizeof(uint32_t);
1649
1635 if (length > cookie_len) { 1650 if (length > cookie_len) {
1636 uint32_t *data32 = (uint32_t *)data; 1651 uint32_t *data32 = (uint32_t *)data;
1652
1637 if (data32[0] == DHT_STATE_COOKIE_GLOBAL) 1653 if (data32[0] == DHT_STATE_COOKIE_GLOBAL)
1638 return load_state(dht_load_state_callback, dht, data + cookie_len, 1654 return load_state(dht_load_state_callback, dht, data + cookie_len,
1639 length - cookie_len, DHT_STATE_COOKIE_TYPE); 1655 length - cookie_len, DHT_STATE_COOKIE_TYPE);
1640 } 1656 }
1641 1657
1642 return DHT_load_old(dht, data, length); 1658 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)
1393 length -= sizeof(nospam); 1393 length -= sizeof(nospam);
1394 1394
1395 uint32_t size; 1395 uint32_t size;
1396
1396 if (length < sizeof(size)) 1397 if (length < sizeof(size))
1397 return -1; 1398 return -1;
1398 1399
@@ -1404,8 +1405,11 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
1404 return -1; 1405 return -1;
1405 1406
1406 if (DHT_load_old(m->dht, data, size) == -1) 1407 if (DHT_load_old(m->dht, data, size) == -1)
1408#ifdef DEBUG
1407 fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); 1409 fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n");
1408 /* DO go on, friends/name still might be intact */ 1410
1411#endif
1412 /* DO go on, friends/name still might be intact */
1409 1413
1410 data += size; 1414 data += size;
1411 length -= size; 1415 length -= size;
@@ -1425,6 +1429,7 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
1425 Friend *friend_list = (Friend *)data; 1429 Friend *friend_list = (Friend *)data;
1426 1430
1427 uint32_t i; 1431 uint32_t i;
1432
1428 for (i = 0; i < num; ++i) { 1433 for (i = 0; i < num; ++i) {
1429 if (friend_list[i].status >= 3) { 1434 if (friend_list[i].status >= 3) {
1430 int fnum = m_addfriend_norequest(m, friend_list[i].client_id); 1435 int fnum = m_addfriend_norequest(m, friend_list[i].client_id);
@@ -1478,11 +1483,11 @@ uint32_t Messenger_size(Messenger *m)
1478{ 1483{
1479 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 1484 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
1480 return size32 * 2 // global cookie 1485 return size32 * 2 // global cookie
1481 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 1486 + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
1482 + sizesubhead + DHT_size(m->dht) // DHT 1487 + sizesubhead + DHT_size(m->dht) // DHT
1483 + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself. 1488 + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself.
1484 + sizesubhead + m->name_length // Own nickname. 1489 + sizesubhead + m->name_length // Own nickname.
1485 ; 1490 ;
1486} 1491}
1487 1492
1488static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 1493static 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)
1538static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) 1543static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type)
1539{ 1544{
1540 Messenger *m = outer; 1545 Messenger *m = outer;
1541 switch(type) { 1546
1547 switch (type) {
1542 case MESSENGER_STATE_TYPE_NOSPAMKEYS: 1548 case MESSENGER_STATE_TYPE_NOSPAMKEYS:
1543 if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) { 1549 if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) {
1544 set_nospam(&(m->fr), *(uint32_t *)data); 1550 set_nospam(&(m->fr), *(uint32_t *)data);
1545 load_keys(m->net_crypto, &data[sizeof(uint32_t)]); 1551 load_keys(m->net_crypto, &data[sizeof(uint32_t)]);
1546 } 1552 } else
1547 else
1548 return -1; /* critical */ 1553 return -1; /* critical */
1554
1549 break; 1555 break;
1550 1556
1551 case MESSENGER_STATE_TYPE_DHT: 1557 case MESSENGER_STATE_TYPE_DHT:
@@ -1557,6 +1563,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
1557 uint16_t num = length / sizeof(Friend); 1563 uint16_t num = length / sizeof(Friend);
1558 Friend *friends = (Friend *)data; 1564 Friend *friends = (Friend *)data;
1559 uint32_t i; 1565 uint32_t i;
1566
1560 for (i = 0; i < num; ++i) { 1567 for (i = 0; i < num; ++i) {
1561 if (friends[i].status >= 3) { 1568 if (friends[i].status >= 3) {
1562 int fnum = m_addfriend_norequest(m, friends[i].client_id); 1569 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
1573 } 1580 }
1574 } 1581 }
1575 } 1582 }
1583
1576 break; 1584 break;
1577 1585
1578 case MESSENGER_STATE_TYPE_NAME: 1586 case MESSENGER_STATE_TYPE_NAME:
1579 if ((length > 0) && (length < MAX_NAME_LENGTH)) { 1587 if ((length > 0) && (length < MAX_NAME_LENGTH)) {
1580 setname(m, data, length); 1588 setname(m, data, length);
1581 } 1589 }
1590
1582 break; 1591 break;
1583 1592
1584 default: 1593 default:
1594#ifdef DEBUG
1585 fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", 1595 fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
1586 length, type); 1596 length, type);
1597#endif
1587 } 1598 }
1588 1599
1589 return 0; 1600 return 0;
@@ -1593,10 +1604,12 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
1593int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 1604int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1594{ 1605{
1595 uint32_t cookie_len = 2 * sizeof(uint32_t); 1606 uint32_t cookie_len = 2 * sizeof(uint32_t);
1607
1596 if (length < cookie_len) 1608 if (length < cookie_len)
1597 return -1; 1609 return -1;
1598 1610
1599 uint32_t *data32 = (uint32_t *)data; 1611 uint32_t *data32 = (uint32_t *)data;
1612
1600 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) 1613 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL))
1601 return load_state(messenger_load_state_callback, m, data + cookie_len, 1614 return load_state(messenger_load_state_callback, m, data + cookie_len,
1602 length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); 1615 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)
277 277
278 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 278 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
279 if (ip.family != AF_INET && ip.family != AF_INET6) { 279 if (ip.family != AF_INET && ip.family != AF_INET6) {
280#ifdef DEBUG
280 fprintf(stderr, "Invalid address family: %u\n", ip.family); 281 fprintf(stderr, "Invalid address family: %u\n", ip.family);
282#endif
281 return NULL; 283 return NULL;
282 } 284 }
283 285
@@ -313,7 +315,9 @@ Networking_Core *new_networking(IP ip, uint16_t port)
313#else 315#else
314 316
315 if (temp->sock < 0) { 317 if (temp->sock < 0) {
318#ifdef DEBUG
316 fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno)); 319 fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
320#endif
317 free(temp); 321 free(temp);
318 return NULL; 322 return NULL;
319 } 323 }
@@ -478,8 +482,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
478 *portptr = htons(port_to_try); 482 *portptr = htons(port_to_try);
479 } 483 }
480 484
485#ifdef DEBUG
481 fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, 486 fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno,
482 strerror(errno), ip_ntoa(&ip), port); 487 strerror(errno), ip_ntoa(&ip), port);
488#endif
483 kill_networking(temp); 489 kill_networking(temp);
484 return NULL; 490 return NULL;
485} 491}
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)
44} 44}
45 45
46int load_state(load_state_callback_func load_state_callback, void *outer, 46int load_state(load_state_callback_func load_state_callback, void *outer,
47 uint8_t *data, uint32_t length, uint16_t cookie_inner) 47 uint8_t *data, uint32_t length, uint16_t cookie_inner)
48{ 48{
49 if (!load_state_callback || !data) { 49 if (!load_state_callback || !data) {
50#ifdef DEBUG
50 fprintf(stderr, "load_state() called with invalid args.\n"); 51 fprintf(stderr, "load_state() called with invalid args.\n");
52#endif
51 return -1; 53 return -1;
52 } 54 }
53 55
@@ -55,6 +57,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
55 uint16_t type; 57 uint16_t type;
56 uint32_t length_sub, cookie_type; 58 uint32_t length_sub, cookie_type;
57 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2; 59 uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2;
60
58 while (length > size_head) { 61 while (length > size_head) {
59 length_sub = *(uint32_t *)data; 62 length_sub = *(uint32_t *)data;
60 cookie_type = *(uint32_t *)(data + size32); 63 cookie_type = *(uint32_t *)(data + size32);
@@ -63,17 +66,22 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
63 66
64 if (length < length_sub) { 67 if (length < length_sub) {
65 /* file truncated */ 68 /* file truncated */
69#ifdef DEBUG
66 fprintf(stderr, "state file too short: %u < %u\n", length, length_sub); 70 fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
71#endif
67 return -1; 72 return -1;
68 } 73 }
69 74
70 if ((cookie_type >> 16) != cookie_inner) { 75 if ((cookie_type >> 16) != cookie_inner) {
71 /* something is not matching up in a bad way, give up */ 76 /* something is not matching up in a bad way, give up */
77#ifdef DEBUG
72 fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner); 78 fprintf(stderr, "state file garbeled: %04hx != %04hx\n", (cookie_type >> 16), cookie_inner);
79#endif
73 return -1; 80 return -1;
74 } 81 }
75 82
76 type = cookie_type & 0xFFFF; 83 type = cookie_type & 0xFFFF;
84
77 if (-1 == load_state_callback(outer, data, length_sub, type)) 85 if (-1 == load_state_callback(outer, data, length_sub, type))
78 return -1; 86 return -1;
79 87