diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 26 |
1 files changed, 21 insertions, 5 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) | |||
1477 | int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size) | 1478 | int 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) | |||
1531 | uint32_t DHT_size(DHT *dht) | 1536 | uint32_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 | ||
1544 | static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) | 1550 | 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) | |||
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, | |||
1632 | int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) | 1646 | int 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); |