summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
blob: 470826f0c480c3d6d9439e8ae029ba37136c726d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
/* DHT.c
 *
 * An implementation of the DHT as seen in http://wiki.tox.im/index.php/DHT
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*----------------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "DHT.h"
#include "network.h"
#include "ping.h"
#include "misc_tools.h"
#include "util.h"
#include "LAN_discovery.h"

/* The number of seconds for a non responsive node to become bad. */
#define BAD_NODE_TIMEOUT 70

/* The max number of nodes to send with send nodes. */
#define MAX_SENT_NODES 8

/* Ping timeout in seconds */
#define PING_TIMEOUT 5

/* The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT 300

/* Ping interval in seconds for each node in our lists. */
#define PING_INTERVAL 60

/* Ping interval in seconds for each random sending of a get nodes request. */
#define GET_NODE_INTERVAL 5

#define MAX_PUNCHING_PORTS 128

/* Interval in seconds between punching attempts*/
#define PUNCH_INTERVAL 10

#define NAT_PING_REQUEST    0
#define NAT_PING_RESPONSE   1

/* Used in the comparison function for sorting lists of Client_data. */
typedef struct {
    Client_data c1;
    Client_data c2;
} ClientPair;

/* Create the declaration for a quick sort for ClientPair structures. */
declare_quick_sort(ClientPair);
/* Create the quicksort function. See misc_tools.h for the definition. */
make_quick_sort(ClientPair);

Client_data *DHT_get_close_list(DHT *dht)
{
    return dht->close_clientlist;
}

/* Compares client_id1 and client_id2 with client_id.
 *
 *  return 0 if both are same distance.
 *  return 1 if client_id1 is closer.
 *  return 2 if client_id2 is closer.
 */
int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
{
    size_t   i;
    uint8_t distance1, distance2;

    for (i = 0; i < CLIENT_ID_SIZE; ++i) {

        distance1 = abs(((int8_t *)id)[i] ^ ((int8_t *)id1)[i]);
        distance2 = abs(((int8_t *)id)[i] ^ ((int8_t *)id2)[i]);

        if (distance1 < distance2)
            return 1;

        if (distance1 > distance2)
            return 2;
    }

    return 0;
}

/* Turns the result of id_closest into something quick_sort can use.
 * Assumes p1->c1 == p2->c1.
 */
static int client_id_cmp(ClientPair p1, ClientPair p2)
{
    int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id);

    if (c == 2)
        return -1;

    return c;
}

static int id_equal(uint8_t *a, uint8_t *b)
{
    return memcmp(a, b, CLIENT_ID_SIZE) == 0;
}

static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
{
    return timestamp + timeout <= time_now;
}

static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < length; i++)
#ifdef CLIENT_ONETOONE_IP /* Dead nodes are considered dead (not in the list)*/
        if (!is_timeout(temp_time, list[i].assoc.timestamp, KILL_NODE_TIMEOUT))
#else
        if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) ||
                !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT))
#endif
            if (id_equal(list[i].client_id, client_id))
                return 1;

    return 0;
}

/* Check if client with client_id is already in list of length length.
 * If it is then set its corresponding timestamp to current time.
 * If the id is already in the list with a different ip_port, update it.
 *  TODO: Maybe optimize this.
 *
 *  return True(1) or False(0)
 */
static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

#ifdef CLIENT_ONETOONE_IP
    uint8_t candropipv4 = 1;

    if (ip_port.ip.family == AF_INET6) {
        uint8_t ipv6cnt = 0;

        /* ipv6: count how many spots are used */
        for (i = 0; i < length; i++)
            if (list[i].assoc.ip_port.ip.family == AF_INET6)
                ipv6cnt++;

        /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
        if (ipv6cnt > length / 2)
            candropipv4 = 0;
    }

#endif

    /* if client_id is in list, find it and maybe overwrite ip_port */
    for (i = 0; i < length; ++i)
        if (id_equal(list[i].client_id, client_id)) {
            /* Refresh the client timestamp. */
#ifdef CLIENT_ONETOONE_IP
            /* if we got "too many" ipv6 addresses already, keep the ipv4 address */
            if (!candropipv4 && (list[i].assoc.ip_port.ip.family == AF_INET))
                return 1;

            if (LAN_ip(list[i].assoc.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
                return 1;

            list[i].assoc.ip_port = ip_port;
            list[i].assoc.timestamp = temp_time;
#else

            if (ip_port.ip.family == AF_INET) {
#ifdef LOGGING

                if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
                    size_t x;
                    x = sprintf(logbuffer, "coipil[%u]: switching ipv4 from %s:%u ", i,
                                ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port));
                    sprintf(logbuffer + x, "to %s:%u\n",
                            ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
                    loglog(logbuffer);
                }

#endif

                if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
                    return 1;

                list[i].assoc4.ip_port = ip_port;
                list[i].assoc4.timestamp = temp_time;
            } else if (ip_port.ip.family == AF_INET6) {
#ifdef LOGGING

                if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
                    size_t x;
                    x = sprintf(logbuffer, "coipil[%u]: switching ipv6 from %s:%u ", i,
                                ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port));
                    sprintf(logbuffer + x, "to %s:%u\n",
                            ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
                    loglog(logbuffer);
                }

#endif

                if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
                    return 1;

                list[i].assoc6.ip_port = ip_port;
                list[i].assoc6.timestamp = temp_time;
            }

#endif
            return 1;
        }

    /* client_id not in list yet: see if we can find an identical ip_port, in
     * that case we kill the old client_id by overwriting it with the new one
     * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list
     * and the one who is the actual friend's client_id/address set? */
    for (i = 0; i < length; ++i) {
#ifdef CLIENT_ONETOONE_IP

        if (ipport_equal(&list[i].assoc.ip_port, &ip_port)) {
            /* Initialize client timestamp. */
            list[i].assoc.timestamp = temp_time;
            memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
#ifdef LOGGING
            sprintf(logbuffer, "coipil[%u]: switching client_id\n", i);
            loglog(logbuffer);
#endif
            return 1;
        }

#else

        /* MAYBE: check the other address, if valid, don't nuke? */
        if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
            /* Initialize client timestamp. */
            list[i].assoc4.timestamp = temp_time;
            memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
#ifdef LOGGING
            sprintf(logbuffer, "coipil[%u]: switching client_id (ipv4) \n", i);
            loglog(logbuffer);
#endif
            /* kill the other address, if it was set */
            memset(&list[i].assoc6, 0, sizeof(list[i].assoc6));
            return 1;
        } else if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
            /* Initialize client timestamp. */
            list[i].assoc6.timestamp = temp_time;
            memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
#ifdef LOGGING
            sprintf(logbuffer, "coipil[%u]: switching client_id (ipv6) \n", i);
            loglog(logbuffer);
#endif
            /* kill the other address, if it was set */
            memset(&list[i].assoc4, 0, sizeof(list[i].assoc4));
            return 1;
        }

#endif
    }

    return 0;
}

/* Check if client with client_id is already in node format list of length length.
 *
 *  return 1 if true.
 *  return 2 if false.
 */
static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id)
{
    uint32_t i;

    for (i = 0; i < length; ++i) {
        if (id_equal(list[i].client_id, client_id))
            return 1;
    }

    return 0;
}

/*  return friend number from the client_id.
 *  return -1 if a failure occurs.
 */
static int friend_number(DHT *dht, uint8_t *client_id)
{
    uint32_t i;

    for (i = 0; i < dht->num_friends; ++i) {
        if (id_equal(dht->friends_list[i].client_id, client_id))
            return i;
    }

    return -1;
}

/*
 * helper for get_close_nodes(). argument list is a monster :D
 */
static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nodes_list,
                                  sa_family_t sa_family, Client_data *client_list, uint32_t client_list_length,
                                  time_t timestamp, int *num_nodes_ptr, uint8_t is_LAN)
{
    if ((sa_family != AF_INET) && (sa_family != AF_INET6))
        return;

    int num_nodes = *num_nodes_ptr;
    int ipv46x, j, closest;
    uint32_t i;

    for (i = 0; i < client_list_length; i++) {
        Client_data *client = &client_list[i];

        /* node already in list? */
        if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->client_id))
            continue;

        IPPTsPng *ipptp = NULL;
#ifdef CLIENT_ONETOONE_IP
        ipptp = &client->assoc;
#else

        if (sa_family == AF_INET)
            ipptp = &client->assoc4;
        else
            ipptp = &client->assoc6;

#endif

        /* node not in a good condition? */
        if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT))
            continue;

#ifdef TOX_ENABLE_IPV6
        IP *client_ip = &ipptp->ip_port.ip;

        /*
         * Careful: AF_INET isn't seen as AF_INET on dual-stack sockets for
         * our connections, instead we have to look if it is an embedded
         * IPv4-in-IPv6 here and convert it down in sendnodes().
         */
        sa_family_t ip_treat_as_family = client_ip->family;

        if ((dht->c->lossless_udp->net->family == AF_INET6) &&
                (client_ip->family == AF_INET6)) {
            /* socket is AF_INET6, address claims AF_INET6:
             * check for embedded IPv4-in-IPv6 (shouldn't happen anymore,
             * all storing functions should already convert down to IPv4) */
            if (IN6_IS_ADDR_V4MAPPED(&client_ip->ip6.in6_addr))
                ip_treat_as_family = AF_INET;
        }

        ipv46x = !(sa_family == ip_treat_as_family);
#else
        ipv46x = !(sa_family == AF_INET);
#endif

        /* node address of the wrong family? */
        if (ipv46x)
            continue;

        if (!LAN_ip(ipptp->ip_port.ip) && !is_LAN)
            continue;

        if (num_nodes < MAX_SENT_NODES) {
            memcpy(nodes_list[num_nodes].client_id,
                   client->client_id,
                   CLIENT_ID_SIZE );

            nodes_list[num_nodes].ip_port = ipptp->ip_port;
            num_nodes++;
        } else {
            /* see if node_list contains a client_id that's "further away"
             * compared to the one we're looking at at the moment, if there
             * is, replace it
             */
            for (j = 0; j < MAX_SENT_NODES; ++j) {
                closest = id_closest(   client_id,
                                        nodes_list[j].client_id,
                                        client->client_id );

                /* second client_id is closer than current: change to it */
                if (closest == 2) {
                    memcpy( nodes_list[j].client_id,
                            client->client_id,
                            CLIENT_ID_SIZE);

                    nodes_list[j].ip_port = ipptp->ip_port;
                    break;
                }
            }
        }
    }

    *num_nodes_ptr = num_nodes;
}

/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
 * put them in the nodes_list and return how many were found.
 *
 * TODO: For the love of based <your favorite deity, in doubt use "love"> make
 * this function cleaner and much more efficient.
 */
static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN)
{
    time_t timestamp = unix_time();
    int num_nodes = 0, i;
    get_close_nodes_inner(dht, client_id, nodes_list, sa_family,
                          dht->close_clientlist, LCLIENT_LIST, timestamp, &num_nodes, is_LAN);

    for (i = 0; i < dht->num_friends; ++i)
        get_close_nodes_inner(dht, client_id, nodes_list, sa_family,
                              dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
                              timestamp, &num_nodes, is_LAN);

    return num_nodes;
}

/* Replace first bad (or empty) node with this one.
 *
 *  return 0 if successful.
 *  return 1 if not (list contains no bad nodes).
 */
static int replace_bad(    Client_data    *list,
                           uint32_t        length,
                           uint8_t        *client_id,
                           IP_Port         ip_port )
{
    if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6))
        return 1;

    uint32_t i;
    uint64_t temp_time = unix_time();

#ifdef CLIENT_ONETOONE_IP
    uint8_t candropipv4 = 1;

    if (ip_port.ip.family == AF_INET6) {
        uint32_t ipv6cnt = 0;

        /* ipv6: count how many spots are used */
        for (i = 0; i < length; i++)
            if (list[i].assoc.ip_port.ip.family == AF_INET6)
                ipv6cnt++;

        /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
        if (ipv6cnt > length / 2)
            candropipv4 = 0;
    }

#endif

    for (i = 0; i < length; ++i) {
        /* If node is bad */
        Client_data *client = &list[i];
        IPPTsPng *ipptp = NULL;

#ifdef CLIENT_ONETOONE_IP
        ipptp = &client->assoc;

        if ((candropipv4 || (ipptp->ip_port.ip.family == AF_INET6)) &&
                is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) {
#else

        if (ip_port.ip.family == AF_INET)
            ipptp = &client->assoc4;
        else
            ipptp = &client->assoc6;

        if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) {
#endif
            memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
            ipptp->ip_port = ip_port;
            ipptp->timestamp = temp_time;

            ip_reset(&ipptp->ret_ip_port.ip);
            ipptp->ret_ip_port.port = 0;
            ipptp->ret_timestamp = 0;

            return 0;
        }
    }

    return 1;
}

/* Sort the list. It will be sorted from furthest to closest.
 *  Turns list into data that quick sort can use and reverts it back.
 */
static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
{
    Client_data cd;
    ClientPair pairs[length];
    uint32_t i;

    memcpy(cd.client_id, comp_client_id, CLIENT_ID_SIZE);

    for (i = 0; i < length; ++i) {
        pairs[i].c1 = cd;
        pairs[i].c2 = list[i];
    }

    ClientPair_quick_sort(pairs, length, client_id_cmp);

    for (i = 0; i < length; ++i)
        list[i] = pairs[i].c2;
}

/* Replace the first good node that is further to the comp_client_id than that of the client_id in the list */
static int replace_good(   Client_data    *list,
                           uint32_t        length,
                           uint8_t        *client_id,
                           IP_Port         ip_port,
                           uint8_t        *comp_client_id )
{
    if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6))
        return 1;

    sort_list(list, length, comp_client_id);

    int8_t replace = -1;

#ifdef CLIENT_ONETOONE_IP
    uint8_t candropipv4 = 1;

    if (ip_port.ip.family == AF_INET6) {
        uint32_t i, ipv6cnt = 0;

        /* ipv6: count how many spots are used */
        for (i = 0; i < length; i++)
            if (list[i].assoc.ip_port.ip.family == AF_INET6)
                ipv6cnt++;

        /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
        if (ipv6cnt > length / 2)
            candropipv4 = 0;
    }

    uint32_t i;

    if (candropipv4) {
        /* either we got an ipv4 address, or we're "allowed" to push out an ipv4
         * address in favor of an ipv6 one
         *
         * because the list is sorted, we can simply check the client_id at the
         * border, either it is closer, then every other one is as well, or it is
         * further, then it gets pushed out in favor of the new address, which
         * will with the next sort() move to its "rightful" position
         *
         * CAVEAT: weirdly enough, the list is sorted DESCENDING in distance
         * so the furthest element is the first, NOT the last (at least that's
         * what the comment above sort_list() claims)
         */
#endif
        if (id_closest(comp_client_id, list[0].client_id, client_id) == 2)
            replace = 0;

#ifdef CLIENT_ONETOONE_IP
    } else {
        /* ipv6 case without a right to push out an ipv4: only look for ipv6
         * addresses, the first one we find is either closer (then we can skip
         * out like above) or further (then we can replace it, like above)
         */
        for (i = 0; i < length; i++) {
            Client_data *client = &list[i];

            if (client->assoc.ip_port.ip.family == AF_INET6) {
                if (id_closest(comp_client_id, list[i].client_id, client_id) == 2)
                    replace = i;

                break;
            }
        }
    }

#endif

    if (replace != -1) {
#ifdef DEBUG
        assert(replace >= 0 && replace < length);
#endif
        Client_data *client = &list[replace];
        IPPTsPng *ipptp = NULL;
#ifdef CLIENT_ONETOONE_IP
        ipptp = &client->assoc;
#else

        if (ip_port.ip.family == AF_INET)
            ipptp = &client->assoc4;
        else
            ipptp = &client->assoc6;

#endif
        memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
        ipptp->ip_port = ip_port;
        ipptp->timestamp = unix_time();

        ip_reset(&ipptp->ret_ip_port.ip);
        ipptp->ret_ip_port.port = 0;
        ipptp->ret_timestamp = 0;
        return 0;
    }

    return 1;
}

/* Attempt to add client with ip_port and client_id to the friends client list
 * and close_clientlist.
 */
void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
{
    uint32_t i;

    /* convert IPv4-in-IPv6 to IPv4 */
    if ((ip_port.ip.family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&ip_port.ip.ip6.in6_addr)) {
        ip_port.ip.family = AF_INET;
        ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3];
    }

    /* NOTE: Current behavior if there are two clients with the same id is
     * to replace the first ip by the second.
     */
    if (!client_or_ip_port_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
        if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
            /* If we can't replace bad nodes we try replacing good ones. */
            replace_good(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port,
                         dht->c->self_public_key);
        }
    }

    for (i = 0; i < dht->num_friends; ++i) {
        if (!client_or_ip_port_in_list(dht->friends_list[i].client_list,
                                       MAX_FRIEND_CLIENTS, client_id, ip_port)) {

            if (replace_bad(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
                            client_id, ip_port)) {
                /* If we can't replace bad nodes we try replacing good ones. */
                replace_good(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
                             client_id, ip_port, dht->friends_list[i].client_id);
            }
        }
    }
}

/* If client_id is a friend or us, update ret_ip_port
 * nodeclient_id is the id of the node that sent us this info.
 */
static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id)
{
    uint32_t i, j;
    uint64_t temp_time = unix_time();

    /* convert IPv4-in-IPv6 to IPv4 */
    if ((ip_port.ip.family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&ip_port.ip.ip6.in6_addr)) {
        ip_port.ip.family = AF_INET;
        ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3];
    }

    if (id_equal(client_id, dht->c->self_public_key)) {
        for (i = 0; i < LCLIENT_LIST; ++i) {
            if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) {
#ifdef CLIENT_ONETOONE_IP
                dht->close_clientlist[i].assoc.ret_ip_port = ip_port;
                dht->close_clientlist[i].assoc.ret_timestamp = temp_time;
#else

                if (ip_port.ip.family == AF_INET) {
                    dht->close_clientlist[i].assoc4.ret_ip_port = ip_port;
                    dht->close_clientlist[i].assoc4.ret_timestamp = temp_time;
                } else if (ip_port.ip.family == AF_INET6) {
                    dht->close_clientlist[i].assoc6.ret_ip_port = ip_port;
                    dht->close_clientlist[i].assoc6.ret_timestamp = temp_time;
                }

#endif
                return;
            }
        }

    } else {
        for (i = 0; i < dht->num_friends; ++i) {
            if (id_equal(client_id, dht->friends_list[i].client_id)) {
                for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
                    if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) {
#ifdef CLIENT_ONETOONE_IP
                        dht->friends_list[i].client_list[j].assoc.ret_ip_port = ip_port;
                        dht->friends_list[i].client_list[j].assoc.ret_timestamp = temp_time;
#else

                        if (ip_port.ip.family == AF_INET) {
                            dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port;
                            dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time;
                        } else if (ip_port.ip.family == AF_INET6) {
                            dht->friends_list[i].client_list[j].assoc6.ret_ip_port = ip_port;
                            dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time;
                        }

#endif
                        return;
                    }
                }
            }
        }

    }
}

static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id)
{
    uint32_t i;
    uint8_t pinging;
    uint64_t temp_time = unix_time();

    for (i = 0; i < LSEND_NODES_ARRAY; ++i ) {
        if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) {
            pinging = 0;

            if (ping_id != 0 && dht->send_nodes[i].id == ping_id)
                ++pinging;

            if (ip_isset(&ip_port.ip) && ipport_equal(&dht->send_nodes[i].ip_port, &ip_port))
                ++pinging;

            if (pinging == (ping_id != 0) + ip_isset(&ip_port.ip))
                return 1;
        }
    }

    return 0;
}

/* Same but for get node requests. */
static uint64_t add_gettingnodes(DHT *dht, IP_Port ip_port)
{
    uint32_t i, j;
    uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
    uint64_t temp_time = unix_time();

    for (i = 0; i < PING_TIMEOUT; ++i ) {
        for (j = 0; j < LSEND_NODES_ARRAY; ++j ) {
            if (is_timeout(temp_time, dht->send_nodes[j].timestamp, PING_TIMEOUT - i)) {
                dht->send_nodes[j].timestamp = temp_time;
                dht->send_nodes[j].ip_port = ip_port;
                dht->send_nodes[j].id = ping_id;
                return ping_id;
            }
        }
    }

    return 0;
}

/* Send a getnodes request. */
static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id)
{
    /* Check if packet is going to be sent to ourself. */
    if (id_equal(public_key, dht->c->self_public_key) || is_gettingnodes(dht, ip_port, 0))
        return -1;

    uint64_t ping_id = add_gettingnodes(dht, ip_port);

    if (ping_id == 0)
        return -1;

    uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
    uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
    uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
    uint8_t nonce[crypto_box_NONCEBYTES];
    new_nonce(nonce);

    memcpy(plain, &ping_id, sizeof(ping_id));
    memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE);

    int len = encrypt_data( public_key,
                            dht->c->self_secret_key,
                            nonce,
                            plain,
                            sizeof(ping_id) + CLIENT_ID_SIZE,
                            encrypt );

    if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
        return -1;

    data[0] = NET_PACKET_GET_NODES;
    memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE);
    memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
    memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);

    return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data));
}

/* Send a send nodes response. */
/* because of BINARY compatibility, the Node_format MUST BE Node4_format,
 * IPv6 nodes are sent in a different message */
static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
{
    /* Check if packet is going to be sent to ourself. */
    if (id_equal(public_key, dht->c->self_public_key))
        return -1;

    size_t Node4_format_size = sizeof(Node4_format);
    uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
                 + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];

    Node_format nodes_list[MAX_SENT_NODES];
    int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0);

    if (num_nodes == 0)
        return 0;

    uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES];
    uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
    uint8_t nonce[crypto_box_NONCEBYTES];
    new_nonce(nonce);

    memcpy(plain, &ping_id, sizeof(ping_id));
#ifdef TOX_ENABLE_IPV6
    Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id));
    int i, num_nodes_ok = 0;

    for (i = 0; i < num_nodes; i++) {
        memcpy(nodes4_list[num_nodes_ok].client_id, nodes_list[i].client_id, CLIENT_ID_SIZE);
        nodes4_list[num_nodes_ok].ip_port.port = nodes_list[i].ip_port.port;

        IP *node_ip = &nodes_list[i].ip_port.ip;

        if ((node_ip->family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&node_ip->ip6.in6_addr))
            /* embedded IPv4-in-IPv6 address: return it in regular sendnodes packet */
            nodes4_list[num_nodes_ok].ip_port.ip.uint32 = node_ip->ip6.uint32[3];
        else if (node_ip->family == AF_INET)
            nodes4_list[num_nodes_ok].ip_port.ip.uint32 = node_ip->ip4.uint32;
        else /* shouldn't happen */
            continue;

        num_nodes_ok++;
    }

    if (num_nodes_ok < num_nodes) {
        /* shouldn't happen */
        num_nodes = num_nodes_ok;
    }

#else
    memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node4_format_size);
#endif

    int len = encrypt_data( public_key,
                            dht->c->self_secret_key,
                            nonce,
                            plain,
                            sizeof(ping_id) + num_nodes * Node4_format_size,
                            encrypt );

    if (len == -1)
        return -1;

    if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING)
        return -1;

    data[0] = NET_PACKET_SEND_NODES;
    memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE);
    memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
    memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);

    return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
}

#ifdef TOX_ENABLE_IPV6
/* Send a send nodes response: message for IPv6 nodes */
static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
{
    /* Check if packet is going to be sent to ourself. */
    if (id_equal(public_key, dht->c->self_public_key))
        return -1;

    size_t Node_format_size = sizeof(Node_format);
    uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
                 + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];

    Node_format nodes_list[MAX_SENT_NODES];
    int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0);

    if (num_nodes == 0)
        return 0;

    uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES];
    uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
    uint8_t nonce[crypto_box_NONCEBYTES];
    new_nonce(nonce);

    memcpy(plain, &ping_id, sizeof(ping_id));
    memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node_format_size);

    int len = encrypt_data( public_key,
                            dht->c->self_secret_key,
                            nonce,
                            plain,
                            sizeof(ping_id) + num_nodes * Node_format_size,
                            encrypt );

    if (len == -1)
        return -1;

    if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING)
        return -1;

    data[0] = NET_PACKET_SEND_NODES_IPV6;
    memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE);
    memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
    memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);

    return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
}
#endif

static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;
    uint64_t ping_id;

    if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES
                    + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING ))
        return 1;

    /* Check if packet is from ourself. */
    if (id_equal(packet + 1, dht->c->self_public_key))
        return 1;

    uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];

    int len = decrypt_data( packet + 1,
                            dht->c->self_secret_key,
                            packet + 1 + CLIENT_ID_SIZE,
                            packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
                            sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING,
                            plain );

    if (len != sizeof(ping_id) + CLIENT_ID_SIZE)
        return 1;

    memcpy(&ping_id, plain, sizeof(ping_id));
    sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id);
#ifdef TOX_ENABLE_IPV6
    sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id),
                   ping_id); /* TODO: prevent possible amplification attacks */
#endif

    add_toping(dht->ping, packet + 1, source);
    //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */

    return 0;
}

static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;
    uint64_t ping_id;
    uint32_t cid_size = 1 + CLIENT_ID_SIZE;
    cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;

    size_t Node4_format_size = sizeof(Node4_format);

    if (length > (cid_size + Node4_format_size * MAX_SENT_NODES) ||
            ((length - cid_size) % Node4_format_size) != 0 ||
            (length < cid_size + Node4_format_size))
        return 1;

    uint32_t num_nodes = (length - cid_size) / Node4_format_size;
    uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES];

    int len = decrypt_data(
                  packet + 1,
                  dht->c->self_secret_key,
                  packet + 1 + CLIENT_ID_SIZE,
                  packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
                  sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING, plain );

    if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size)
        return 1;

    memcpy(&ping_id, plain, sizeof(ping_id));

    if (!is_gettingnodes(dht, source, ping_id))
        return 1;

    uint32_t i;
    Node_format nodes_list[MAX_SENT_NODES];

#ifdef TOX_ENABLE_IPV6
    Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id));

    uint32_t num_nodes_ok = 0;

    for (i = 0; i < num_nodes; i++)
        if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != (uint32_t)~0)) {
            memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE);
            nodes_list[num_nodes_ok].ip_port.ip.family = AF_INET;
            nodes_list[num_nodes_ok].ip_port.ip.ip4.uint32 = nodes4_list[i].ip_port.ip.uint32;
            nodes_list[num_nodes_ok].ip_port.port = nodes4_list[i].ip_port.port;

            num_nodes_ok++;
        }

    if (num_nodes_ok < num_nodes) {
        /* shouldn't happen */
        num_nodes = num_nodes_ok;
    }

#else
    memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
#endif

    addto_lists(dht, source, packet + 1);

    for (i = 0; i < num_nodes; ++i)  {
        send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id);
        returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
    }

    return 0;
}

#ifdef TOX_ENABLE_IPV6
static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;
    uint64_t ping_id;
    uint32_t cid_size = 1 + CLIENT_ID_SIZE;
    cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;

    size_t Node_format_size = sizeof(Node_format);

    if (length > (cid_size + Node_format_size * MAX_SENT_NODES) ||
            ((length - cid_size) % Node_format_size) != 0 ||
            (length < cid_size + Node_format_size))
        return 1;

    uint32_t num_nodes = (length - cid_size) / Node_format_size;
    uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES];

    int len = decrypt_data(
                  packet + 1,
                  dht->c->self_secret_key,
                  packet + 1 + CLIENT_ID_SIZE,
                  packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
                  sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING, plain );

    if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size)
        return 1;

    memcpy(&ping_id, plain, sizeof(ping_id));

    if (!is_gettingnodes(dht, source, ping_id))
        return 1;

    uint32_t i;
    Node_format nodes_list[MAX_SENT_NODES];
    memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));

    addto_lists(dht, source, packet + 1);

    for (i = 0; i < num_nodes; ++i)  {
        send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id);
        returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
    }

    return 0;
}
#endif

/*----------------------------------------------------------------------------------*/
/*------------------------END of packet handling functions--------------------------*/

/*
 * Send get nodes requests with client_id to max_num peers in list of length length
 */
static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_t max_num, uint8_t *client_id)
{
    uint64_t temp_time = unix_time();
    uint32_t i, num = 0;

    for (i = 0; i < length; ++i) {
        IPPTsPng *assoc;
#ifdef CLIENT_ONETOONE_IP
        assoc = &list[i].assoc;
#else
        uint32_t a;

        for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4)
#endif
        if (ipport_isset(&(assoc->ip_port)) &&
                !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
            getnodes(dht, assoc->ip_port, list[i].client_id, client_id);
            ++num;

            if (num >= max_num)
                return;
        }
    }
}

int DHT_addfriend(DHT *dht, uint8_t *client_id)
{
    if (friend_number(dht, client_id) != -1) /* Is friend already in DHT? */
        return 1;

    DHT_Friend *temp;
    temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1));

    if (temp == NULL)
        return 1;

    dht->friends_list = temp;
    memset(&dht->friends_list[dht->num_friends], 0, sizeof(DHT_Friend));
    memcpy(dht->friends_list[dht->num_friends].client_id, client_id, CLIENT_ID_SIZE);

    dht->friends_list[dht->num_friends].nat.NATping_id = ((uint64_t)random_int() << 32) + random_int();
    ++dht->num_friends;
    get_bunchnodes(dht, dht->close_clientlist, LCLIENT_LIST, MAX_FRIEND_CLIENTS, client_id);/*TODO: make this better?*/
    return 0;
}

int DHT_delfriend(DHT *dht, uint8_t *client_id)
{
    uint32_t i;
    DHT_Friend *temp;

    for (i = 0; i < dht->num_friends; ++i) {
        /* Equal */
        if (id_equal(dht->friends_list[i].client_id, client_id)) {
            --dht->num_friends;

            if (dht->num_friends != i) {
                memcpy( dht->friends_list[i].client_id,
                        dht->friends_list[dht->num_friends].client_id,
                        CLIENT_ID_SIZE );
            }

            if (dht->num_friends == 0) {
                free(dht->friends_list);
                dht->friends_list = NULL;
                return 0;
            }

            temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends));

            if (temp == NULL)
                return 1;

            dht->friends_list = temp;
            return 0;
        }
    }

    return 1;
}

/* TODO: Optimize this. */
int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port)
{
    uint32_t i, j;
    uint64_t temp_time = unix_time();

    ip_reset(&ip_port->ip);
    ip_port->port = 0;

    for (i = 0; i < dht->num_friends; ++i) {
        /* Equal */
        if (id_equal(dht->friends_list[i].client_id, client_id)) {
            for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
                Client_data *client = &dht->friends_list[i].client_list[j];

                if (id_equal(client->client_id, client_id)) {
                    IPPTsPng *assoc = NULL;
#ifdef CLIENT_ONETOONE_IP
                    assoc = &client->assoc;
#else
                    uint32_t a;

                    for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
#endif
                    if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
                        *ip_port = assoc->ip_port;
                        return 1;
                    }
                }
            }

            return 0;
        }
    }

    return -1;
}

static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id,
        Client_data *list, uint32_t list_count)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    uint32_t num_nodes = 0;
    Client_data *client_list[list_count * 2];
    IPPTsPng    *assoc_list[list_count * 2];

    for (i = 0; i < list_count; i++) {
        /* If node is not dead. */
        Client_data *client = &list[i];
        IPPTsPng *assoc;
#ifdef CLIENT_ONETOONE_IP
        assoc = &client->assoc;
#else
        uint32_t a;

        for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
#endif

        if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) {
            if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) {
                send_ping_request(dht->ping, assoc->ip_port, client->client_id );
                assoc->last_pinged = temp_time;
            }

            /* If node is good. */
            if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
                client_list[num_nodes] = client;
                assoc_list[num_nodes] = assoc;
                ++num_nodes;
            }
        }
    }

    if ((num_nodes != 0) &&
            is_timeout(temp_time, *lastgetnode, GET_NODE_INTERVAL)) {
        uint32_t rand_node = rand() % num_nodes;
        getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->client_id,
                 client_id);
        *lastgetnode = temp_time;
    }
}

/* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request
 * every GET_NODE_INTERVAL seconds to a random good node for each "friend" in our "friends" list.
 */
static void do_DHT_friends(DHT *dht)
{
    uint32_t i;

    for (i = 0; i < dht->num_friends; ++i)
        do_ping_and_sendnode_requests(dht, &dht->friends_list[i].lastgetnode, dht->friends_list[i].client_id,
                                      dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS);
}

/* Ping each client in the close nodes list every PING_INTERVAL seconds.
 * Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list.
 */
static void do_Close(DHT *dht)
{
    do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->c->self_public_key,
                                  dht->close_clientlist, LCLIENT_LIST);
}

void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
{
    getnodes(dht, ip_port, public_key, dht->c->self_public_key);
}
int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
                               uint16_t port, uint8_t *public_key)
{
    IP_Port ip_port_v64;
    IP *ip_extra = NULL;
#ifdef TOX_ENABLE_IPV6
    IP_Port ip_port_v4;
    ip_init(&ip_port_v64.ip, ipv6enabled);

    if (ipv6enabled) {
        ip_port_v64.ip.family = AF_UNSPEC;
        ip_reset(&ip_port_v4.ip);
        ip_extra = &ip_port_v4.ip;
    }

#else
    ip_init(&ip_port_v64.ip, 0);
#endif

    if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) {
        ip_port_v64.port = port;
        DHT_bootstrap(dht, ip_port_v64, public_key);
#ifdef TOX_ENABLE_IPV6

        if ((ip_extra != NULL) && ip_isset(ip_extra)) {
            ip_port_v4.port = port;
            DHT_bootstrap(dht, ip_port_v4, public_key);
        }

#endif
        return 1;
    } else
        return 0;
}

/* Send the given packet to node with client_id
 *
 *  return -1 if failure.
 */
int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
{
    uint32_t i;

    for (i = 0; i < LCLIENT_LIST; ++i) {
        if (id_equal(client_id, dht->close_clientlist[i].client_id)) {
            Client_data *client = &dht->close_clientlist[i];
#ifdef CLIENT_ONETOONE_IP

            if (ip_isset(&client->assoc.ip_port.ip))
                return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].assoc.ip_port, packet, length);

#else

            if (ip_isset(&client->assoc6.ip_port.ip))
                return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length);
            else if (ip_isset(&client->assoc4.ip_port.ip))
                return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length);
            else
                break;

#endif
        }
    }

    return -1;
}

/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist.
 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
 * unless CLIENT_ONETOONE_IP is defined, returns an array with ips of the same family.
 *
 *  return the number of ips returned.
 *  return 0 if we are connected to friend or if no ips were found.
 *  return -1 if no such friend.
 */
static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
{
    int i;
    uint64_t temp_time = unix_time();

    if (friend_num >= dht->num_friends)
        return -1;

    DHT_Friend *friend = &dht->friends_list[friend_num];
    Client_data *client;
    IP_Port ipv4s[MAX_FRIEND_CLIENTS];
    int num_ipv4s = 0;
    IP_Port ipv6s[MAX_FRIEND_CLIENTS];
    int num_ipv6s = 0;

    for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
        client = &(friend->client_list[i]);

        IPPTsPng *assoc = NULL;
#ifdef CLIENT_ONETOONE_IP
        assoc = &client->assoc;
#else
        assoc = &client->assoc4;
#endif

        if (id_equal(client->client_id, friend->client_id) &&
                !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT))
            return 0;

        /* If ip is not zero and node is good. */
        if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
            ipv4s[num_ipv4s] = assoc->ret_ip_port;
            ++num_ipv4s;
        }
    }

#ifndef CLIENT_ONETOONE_IP

    for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
        client = &(friend->client_list[i]);

        IPPTsPng *assoc = NULL;
        assoc = &client->assoc6;

        if (id_equal(client->client_id, friend->client_id) &&
                !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT))
            return 0;

        /* If ip is not zero and node is good. */
        if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
            ipv6s[num_ipv6s] = assoc->ret_ip_port;
            ++num_ipv6s;
        }
    }

    if (num_ipv6s >= num_ipv4s) {
        memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port));
        return num_ipv6s;
    }

#endif
    memcpy(ip_portlist, ipv4s, num_ipv4s * sizeof(IP_Port));
    return num_ipv4s;
}


/* Send the following packet to everyone who tells us they are connected to friend_id.
 *
 *  return ip for friend.
 *  return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 2).
 */
int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
{
    int num = friend_number(dht, friend_id);

    if (num == -1)
        return 0;

    uint32_t i, sent = 0;
    uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0};

    IP_Port ip_list[MAX_FRIEND_CLIENTS];
    int ip_num = friend_iplist(dht, ip_list, num);

    if (ip_num < (MAX_FRIEND_CLIENTS / 2))
        return 0; /* Reason for that? */

    uint64_t temp_time = unix_time();
    DHT_Friend *friend = &dht->friends_list[num];
    Client_data *client;

#ifndef CLIENT_ONETOONE_IP
    /* extra legwork, because having the outside allocating the space for us
     * is *usually* good(tm) (bites us in the behind in this case though) */
    uint32_t a;

    for (a = 0; a < 2; a++)
#endif
        for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
            if (friend_sent[i])/* Send one packet per client.*/
                continue;

            client = &friend->client_list[i];
            IPPTsPng *assoc = NULL;
#ifdef CLIENT_ONETOONE_IP
            assoc = &client->assoc;
#else

            if (!a)
                assoc = &client->assoc4;
            else
                assoc = &client->assoc6;

#endif

            /* If ip is not zero and node is good. */
            if (ip_isset(&assoc->ret_ip_port.ip) &&
                    !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
                int retval = sendpacket(dht->c->lossless_udp->net, assoc->ip_port, packet, length);

                if ((unsigned int)retval == length) {
                    ++sent;
                    friend_sent[i] = 1;
                }
            }
        }

    return sent;
}

/* Send the following packet to one random person who tells us they are connected to friend_id.
 *
 *  return number of nodes the packet was sent to.
 */
static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length)
{
    int num = friend_number(dht, friend_id);

    if (num == -1)
        return 0;

    DHT_Friend *friend = &dht->friends_list[num];
    Client_data *client;

    IP_Port ip_list[MAX_FRIEND_CLIENTS * 2];
    int n = 0;
    uint32_t i;
    uint64_t temp_time = unix_time();

#ifndef CLIENT_ONETOONE_IP
    /* extra legwork, because having the outside allocating the space for us
     * is *usually* good(tm) (bites us in the behind in this case though) */
    uint32_t a;

    for (a = 0; a < 2; a++)
#endif
        for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
            client = &friend->client_list[i];
            IPPTsPng *assoc = NULL;
#ifdef CLIENT_ONETOONE_IP
            assoc = &client->assoc;
#else

            if (!a)
                assoc = &client->assoc4;
            else
                assoc = &client->assoc6;

#endif

            /* If ip is not zero and node is good. */
            if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
                ip_list[n] = assoc->ip_port;
                ++n;
            }
        }

    if (n < 1)
        return 0;

    int retval = sendpacket(dht->c->lossless_udp->net, ip_list[rand() % n], packet, length);

    if ((unsigned int)retval == length)
        return 1;

    return 0;
}

/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist.
 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
 *
 *  return number of ips returned.
 *  return 0 if we are connected to friend or if no ips were found.
 *  return -1 if no such friend.
 */
int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id)
{
    uint32_t i;

    for (i = 0; i < dht->num_friends; ++i) {
        /* Equal */
        if (id_equal(dht->friends_list[i].client_id, friend_id))
            return friend_iplist(dht, ip_portlist, i);
    }

    return -1;
}

/*----------------------------------------------------------------------------------*/
/*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/

static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type)
{
    uint8_t data[sizeof(uint64_t) + 1];
    uint8_t packet[MAX_DATA_SIZE];

    int num = 0;

    data[0] = type;
    memcpy(data + 1, &ping_id, sizeof(uint64_t));
    /* 254 is NAT ping request packet id */
    int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data,
                             sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING);

    if (len == -1)
        return -1;

    if (type == 0) /* If packet is request use many people to route it. */
        num = route_tofriend(dht, public_key, packet, len);
    else if (type == 1) /* If packet is response use only one person to route it */
        num = routeone_tofriend(dht, public_key, packet, len);

    if (num == 0)
        return -1;

    return num;
}

/* Handle a received ping request for. */
static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
{
    if (length != sizeof(uint64_t) + 1)
        return 1;

    DHT *dht = object;
    uint64_t ping_id;
    memcpy(&ping_id, packet + 1, sizeof(uint64_t));

    int friendnumber = friend_number(dht, source_pubkey);

    if (friendnumber == -1)
        return 1;

    DHT_Friend *friend = &dht->friends_list[friendnumber];

    if (packet[0] == NAT_PING_REQUEST) {
        /* 1 is reply */
        send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE);
        friend->nat.recvNATping_timestamp = unix_time();
        return 0;
    } else if (packet[0] == NAT_PING_RESPONSE) {
        if (friend->nat.NATping_id == ping_id) {
            friend->nat.NATping_id = ((uint64_t)random_int() << 32) + random_int();
            friend->nat.hole_punching = 1;
            return 0;
        }
    }

    return 1;
}

/* Get the most common ip in the ip_portlist.
 * Only return ip if it appears in list min_num or more.
 * len must not be bigger than MAX_FRIEND_CLIENTS.
 *
 *  return ip of 0 if failure.
 */
static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
{
    IP zero;
    ip_reset(&zero);

    if (len > MAX_FRIEND_CLIENTS)
        return zero;

    uint32_t i, j;
    uint16_t numbers[MAX_FRIEND_CLIENTS] = {0};

    for (i = 0; i < len; ++i) {
        for (j = 0; j < len; ++j) {
            if (ip_equal(&ip_portlist[i].ip, &ip_portlist[j].ip))
                ++numbers[i];
        }

        if (numbers[i] >= min_num)
            return ip_portlist[i].ip;
    }

    return zero;
}

/* Return all the ports for one ip in a list.
 * portlist must be at least len long,
 * where len is the length of ip_portlist.
 *
 *  return number of ports and puts the list of ports in portlist.
 */
static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip)
{
    uint32_t i;
    uint16_t num = 0;

    for (i = 0; i < len; ++i) {
        if (ip_equal(&ip_portlist[i].ip, &ip)) {
            portlist[num] = ntohs(ip_portlist[i].port);
            ++num;
        }
    }

    return num;
}

static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num)
{
    if (numports > MAX_FRIEND_CLIENTS || numports == 0)
        return;

    uint32_t i;
    uint32_t top = dht->friends_list[friend_num].nat.punching_index + MAX_PUNCHING_PORTS;
    uint16_t firstport = port_list[0];

    for (i = 0; i < numports; ++i) {
        if (firstport != port_list[0])
            break;
    }

    if (i == numports) { /* If all ports are the same, only try that one port. */
        IP_Port pinging;
        ip_copy(&pinging.ip, &ip);
        pinging.port = htons(firstport);
        send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id);
    } else {
        for (i = dht->friends_list[friend_num].nat.punching_index; i != top; i++) {
            /* TODO: Improve port guessing algorithm. */
            uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
            IP_Port pinging;
            ip_copy(&pinging.ip, &ip);
            pinging.port = htons(port);
            send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id);
        }

        dht->friends_list[friend_num].nat.punching_index = i;
    }
}

static void do_NAT(DHT *dht)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < dht->num_friends; ++i) {
        IP_Port ip_list[MAX_FRIEND_CLIENTS];
        int num = friend_iplist(dht, ip_list, i);

        /* If already connected or friend is not online don't try to hole punch. */
        if (num < MAX_FRIEND_CLIENTS / 2)
            continue;

        if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) {
            send_NATping(dht, dht->friends_list[i].client_id, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST);
            dht->friends_list[i].nat.NATping_timestamp = temp_time;
        }

        if (dht->friends_list[i].nat.hole_punching == 1 &&
                dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time &&
                dht->friends_list[i].nat.recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) {

            IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2);

            if (!ip_isset(&ip))
                continue;

            uint16_t port_list[MAX_FRIEND_CLIENTS];
            uint16_t numports = NAT_getports(port_list, ip_list, num, ip);
            punch_holes(dht, ip, port_list, numports, i);

            dht->friends_list[i].nat.punching_timestamp = temp_time;
            dht->friends_list[i].nat.hole_punching = 0;
        }
    }
}

/*----------------------------------------------------------------------------------*/
/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/

DHT *new_DHT(Net_Crypto *c)
{
    if (c == NULL)
        return NULL;

    DHT *dht = calloc(1, sizeof(DHT));

    if (dht == NULL)
        return NULL;

    dht->ping = new_ping(dht, c);

    if (dht->ping == NULL) {
        kill_DHT(dht);
        return NULL;
    }

    dht->c = c;
    networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
    networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, dht);
#ifdef TOX_ENABLE_IPV6
    networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht);
#endif

    init_cryptopackets(dht);
    cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht);

    return dht;
}

void do_DHT(DHT *dht)
{
    do_Close(dht);
    do_DHT_friends(dht);
    do_NAT(dht);
    do_toping(dht->ping);
}
void kill_DHT(DHT *dht)
{
    kill_ping(dht->ping);
    free(dht->friends_list);
    free(dht);
}

/* Get the size of the DHT (for saving). */
uint32_t DHT_size_old(DHT *dht)
{
    return sizeof(dht->close_clientlist) + sizeof(DHT_Friend) * dht->num_friends;
}

/* Save the DHT in data where data is an array of size DHT_size(). */
void DHT_save_old(DHT *dht, uint8_t *data)
{
    memcpy(data, dht->close_clientlist, sizeof(dht->close_clientlist));
    memcpy(data + sizeof(dht->close_clientlist), dht->friends_list, sizeof(DHT_Friend) * dht->num_friends);
}

/* Load the DHT from data of size size.
 *
 *  return -1 if failure.
 *  return 0 if success.
 */
int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size)
{
    size_t clientlist_oldsize = sizeof(Client_data_old) * LCLIENT_LIST;

    if (size < clientlist_oldsize) {
#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 - clientlist_oldsize;

    if (friendlistsize % sizeof(DHT_Friend_old) != 0) {
#ifdef DEBUG
        fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize);
#endif
        return -1;
    }

    uint32_t i, j;
    Client_data_old *client;
    uint16_t friends_num = friendlistsize / sizeof(DHT_Friend_old);

    if (friends_num != 0) {
        DHT_Friend_old *tempfriends_list = (DHT_Friend_old *)(data + sizeof(dht->close_clientlist));

        for (i = 0; i < friends_num; ++i) {
            DHT_addfriend(dht, tempfriends_list[i].client_id);

            for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
                client = &tempfriends_list[i].client_list[j];

                if (client->assoc.timestamp != 0)
                    getnodes(dht, client->assoc.ip_port, client->client_id, tempfriends_list[i].client_id);
            }
        }
    }

    Client_data_old *tempclose_clientlist = (Client_data_old *)data;

    for (i = 0; i < LCLIENT_LIST; ++i) {
        if (tempclose_clientlist[i].assoc.timestamp != 0)
            DHT_bootstrap(dht, tempclose_clientlist[i].assoc.ip_port,
                          tempclose_clientlist[i].client_id );
    }

    return 0;
}


/* new DHT format for load/save, more robust and forward compatible */

#define DHT_STATE_COOKIE_GLOBAL 0x159000d

#define DHT_STATE_COOKIE_TYPE      0x11ce
#define DHT_STATE_TYPE_FRIENDS          1
#define DHT_STATE_TYPE_CLIENTS          2
#define DHT_STATE_TYPE_FRIENDS_ASSOC46  3
#define DHT_STATE_TYPE_CLIENTS_ASSOC46  4

/* Get the size of the DHT (for saving). */
uint32_t DHT_size(DHT *dht)
{
    uint32_t num = 0, i;

    for (i = 0; i < LCLIENT_LIST; ++i)
#ifdef CLIENT_ONETOONE_IP
        if (dht->close_clientlist[i].assoc.timestamp != 0)
#else
        if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
                (dht->close_clientlist[i].assoc6.timestamp != 0))
#endif
            num++;

    uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
    return size32
           + 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)
{
    uint32_t *data32 = (uint32_t *)data;
    data32[0] = len;
    data32[1] = (DHT_STATE_COOKIE_TYPE << 16) | type;
    data += sizeof(uint32_t) * 2;
    return data;
}

/* Save the DHT in data where data is an array of size DHT_size(). */
void DHT_save(DHT *dht, uint8_t *data)
{
    uint32_t len;
    uint16_t type;
    *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL;
    data += sizeof(uint32_t);

    len = sizeof(DHT_Friend) * dht->num_friends;
#ifdef CLIENT_ONETOONE_IP
    type = DHT_STATE_TYPE_FRIENDS;
#else
    type = DHT_STATE_TYPE_FRIENDS_ASSOC46;
#endif
    data = z_state_save_subheader(data, len, type);
    memcpy(data, dht->friends_list, len);
    data += len;

    uint32_t num = 0, i;

    for (i = 0; i < LCLIENT_LIST; ++i)
#ifdef CLIENT_ONETOONE_IP
        if (dht->close_clientlist[i].assoc.timestamp != 0)
#else
        if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
                (dht->close_clientlist[i].assoc6.timestamp != 0))
#endif
            num++;

    len = num * sizeof(Client_data);
#ifdef CLIENT_ONETOONE_IP
    type = DHT_STATE_TYPE_CLIENTS;
#else
    type = DHT_STATE_TYPE_CLIENTS_ASSOC46;
#endif
    data = z_state_save_subheader(data, len, type);

    if (num) {
        Client_data *clients = (Client_data *)data;

        for (num = 0, i = 0; i < LCLIENT_LIST; ++i)
#ifdef CLIENT_ONETOONE_IP
            if (dht->close_clientlist[i].assoc.timestamp != 0)
#else
            if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
                    (dht->close_clientlist[i].assoc6.timestamp != 0))
#endif
                memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
    }

    data += len;
}

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

    switch (type) {
        case DHT_STATE_TYPE_FRIENDS:
            if (length % sizeof(DHT_Friend_old) != 0)
                break;

            { /* localize declarations */
                DHT_Friend_old *friend_list = (DHT_Friend_old *)data;
                num = length / sizeof(DHT_Friend_old);

                for (i = 0; i < num; ++i) {
                    DHT_addfriend(dht, friend_list[i].client_id);

                    for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
                        Client_data_old *client = &friend_list[i].client_list[j];

                        if (client->assoc.timestamp != 0)
                            getnodes(dht, client->assoc.ip_port, client->client_id, friend_list[i].client_id);
                    }
                }
            } /* localize declarations */

            break;

        case DHT_STATE_TYPE_CLIENTS:
            if ((length % sizeof(Client_data_old)) != 0)
                break;

            { /* localize declarations */
                num = length / sizeof(Client_data_old);
                Client_data_old *client_list = (Client_data_old *)data;

                for (i = 0; i < num; ++i)
                    if (client_list[i].assoc.timestamp != 0)
                        DHT_bootstrap(dht, client_list[i].assoc.ip_port, client_list[i].client_id);
            } /* localize declarations */

            break;

        case DHT_STATE_TYPE_FRIENDS_ASSOC46:
            if (length % sizeof(DHT_Friend_new) != 0)
                break;

            { /* localize declarations */
                DHT_Friend_new *friend_list = (DHT_Friend_new *)data;
                num = length / sizeof(DHT_Friend_new);

                for (i = 0; i < num; ++i) {
                    DHT_addfriend(dht, friend_list[i].client_id);

                    for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
                        Client_data_new *client = &friend_list[i].client_list[j];

                        if (client->assoc4.timestamp != 0)
                            getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id);

                        if (client->assoc6.timestamp != 0)
                            getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id);
                    }
                }
            } /* localize declarations */

            break;

        case DHT_STATE_TYPE_CLIENTS_ASSOC46:
            if ((length % sizeof(Client_data_new)) != 0)
                break;

            { /* localize declarations */
                num = length / sizeof(Client_data_new);
                Client_data_new *client_list = (Client_data_new *)data;

                for (i = 0; i < num; ++i) {
                    if (client_list[i].assoc4.timestamp != 0)
                        DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id);

                    if (client_list[i].assoc6.timestamp != 0)
                        DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id);
                }
            } /* localize declarations */

            break;

        default:
            fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
                    length, type);
    }

    return 0;
}

/* Load the DHT from data of size size.
 *
 *  return -1 if failure.
 *  return 0 if success.
 */
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);
    }

    return DHT_load_old(dht, data, length);
}
/*  return 0 if we are not connected to the DHT.
 *  return 1 if we are.
 */
int DHT_isconnected(DHT *dht)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < LCLIENT_LIST; ++i) {
        Client_data *client = &dht->close_clientlist[i];
#ifdef CLIENT_ONETOONE_IP

        if (!is_timeout(temp_time, client->assoc.timestamp, BAD_NODE_TIMEOUT))
#else
        if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) ||
                !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT))
#endif
            return 1;
    }

    return 0;
}