aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-dht-new.c
blob: 2fb06457ef0e709bd3d416b9c5e65a7e56df658f (plain) (blame)
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
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
/*
     This file is part of GNUnet.
     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)

     GNUnet 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, or (at your
     option) any later version.

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file dht/gnunet-service-dht.c
 * @brief GNUnet DHT service
 * @author Christian Grothoff
 * @author Nathan Evans
 *
 * TODO:
 * - use OPTION_MULTIPLE instead of linked list for the forward_list.hashmap
 * - use different 'struct DHT_MessageContext' for the different types of
 *   messages (currently rather confusing, especially with things like
 *   peer bloom filters occuring when processing replies).
 */

#include "platform.h"
#include "gnunet_block_lib.h"
#include "gnunet_client_lib.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_os_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_service_lib.h"
#include "gnunet_nse_service.h"
#include "gnunet_core_service.h"
#include "gnunet_signal_lib.h"
#include "gnunet_util_lib.h"
#include "gnunet_datacache_lib.h"
#include "gnunet_transport_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_dht_service.h"
#include "gnunet_statistics_service.h"
#include "dht.h"
#include <fenv.h>


/**
 * Defines whether find peer requests send their HELLO's outgoing,
 * or expect replies to contain hellos.
 */
#define FIND_PEER_WITH_HELLO GNUNET_YES

#define DEFAULT_CORE_QUEUE_SIZE 32

/**
 * Minimum number of peers we need for "good" routing,
 * any less than this and we will allow messages to
 * travel much further through the network!
 */
#define MINIMUM_PEER_THRESHOLD 20

/**
 * Number of requests we track at most (for routing replies).
 */
#define DHT_MAX_RECENT (1024 * 16)

/**
 * How long do we wait at most when queueing messages with core
 * that we are sending on behalf of other peers.
 */
#define DHT_DEFAULT_P2P_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)

/**
 * Default importance for handling messages on behalf of other peers.
 */
#define DHT_DEFAULT_P2P_IMPORTANCE 0

/**
 * How long to keep recent requests around by default.
 */
#define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)

/**
 * Default time to wait to send find peer messages sent by the dht service.
 */
#define DHT_DEFAULT_FIND_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)

/**
 * Default importance for find peer messages sent by the dht service.
 */
#define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8

/**
 * Default replication parameter for find peer messages sent by the dht service.
 */
#define DHT_DEFAULT_FIND_PEER_REPLICATION 4

/**
 * How long at least to wait before sending another find peer request.
 */
#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)

/**
 * How long at most to wait before sending another find peer request.
 */
#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)

/**
 * How often to update our preference levels for peers in our routing tables.
 */
#define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)

/**
 * How long at most on average will we allow a reply forward to take
 * (before we quit sending out new requests)
 */
#define MAX_REQUEST_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)

/**
 * How many time differences between requesting a core send and
 * the actual callback to remember.
 */
#define MAX_REPLY_TIMES 8




/**
 * Context containing information about a DHT message received.
 */
struct DHT_MessageContext
{
  /**
   * The client this request was received from.
   * (NULL if received from another peer)
   */
  struct ClientList *client;

  /**
   * The peer this request was received from.
   */
  struct GNUNET_PeerIdentity peer;

  /**
   * Bloomfilter for this routing request.
   */
  struct GNUNET_CONTAINER_BloomFilter *bloom;

  /**
   * extended query (see gnunet_block_lib.h).
   */
  const void *xquery;

  /**
   * Bloomfilter to filter out duplicate replies.
   */
  struct GNUNET_CONTAINER_BloomFilter *reply_bf;

  /**
   * The key this request was about
   */
  GNUNET_HashCode key;

  /**
   * How long should we wait to transmit this request?
   */
  struct GNUNET_TIME_Relative timeout;

  /**
   * The unique identifier of this request
   */
  uint64_t unique_id;

  /**
   * Number of bytes in xquery.
   */
  size_t xquery_size;

  /**
   * Mutator value for the reply_bf, see gnunet_block_lib.h
   */
  uint32_t reply_bf_mutator;

  /**
   * Desired replication level
   */
  uint32_t replication;

  /**
   * Network size estimate, either ours or the sum of
   * those routed to thus far. =~ Log of number of peers
   * chosen from for this request.
   */
  uint32_t network_size;

  /**
   * Any message options for this request
   */
  uint32_t msg_options;

  /**
   * How many hops has the message already traversed?
   */
  uint32_t hop_count;

  /**
   * How many peer identities are present in the path history?
   */
  uint32_t path_history_len;

  /**
   * Path history.
   */
  char *path_history;

  /**
   * How important is this message?
   */
  unsigned int importance;

  /**
   * Should we (still) forward the request on to other peers?
   */
  int do_forward;

  /**
   * Did we forward this message? (may need to remember it!)
   */
  int forwarded;

  /**
   * Are we the closest known peer to this key (out of our neighbors?)
   */
  int closest;
};


/**
 * Record used for remembering what peers are waiting for what
 * responses (based on search key).
 */
struct DHTRouteSource
{
  /**
   * This is a DLL.
   */
  struct DHTRouteSource *next;

  /**
   * This is a DLL.
   */
  struct DHTRouteSource *prev;

  /**
   * UID of the request, 0 if from another peer.
   */
  uint64_t uid;

  /**
   * Source of the request.  Replies should be forwarded to
   * this peer.
   */
  struct GNUNET_PeerIdentity source;

  /**
   * If this was a local request, remember the client; otherwise NULL.
   */
  struct ClientList *client;

  /**
   * Pointer to this nodes heap location (for removal)
   */
  struct GNUNET_CONTAINER_HeapNode *hnode;

  /**
   * Back pointer to the record storing this information.
   */
  struct DHTQueryRecord *record;

  /**
   * Task to remove this entry on timeout.
   */
  GNUNET_SCHEDULER_TaskIdentifier delete_task;

  /**
   * Bloomfilter of peers we have already sent back as
   * replies to the initial request.  Allows us to not
   * forward the same peer multiple times for a find peer
   * request.
   */
  struct GNUNET_CONTAINER_BloomFilter *find_peers_responded;

};


/**
 * Entry in the DHT routing table.
 */
struct DHTQueryRecord
{
  /**
   * Head of DLL for result forwarding.
   */
  struct DHTRouteSource *head;

  /**
   * Tail of DLL for result forwarding.
   */
  struct DHTRouteSource *tail;

  /**
   * Key that the record concerns.
   */
  GNUNET_HashCode key;

};


/**
 * Context used to calculate the number of find peer messages
 * per X time units since our last scheduled find peer message
 * was sent.  If we have seen too many messages, delay or don't
 * send our own out.
 */
struct FindPeerMessageContext
{
  unsigned int count;

  struct GNUNET_TIME_Absolute start;

};


struct RecentRequest
{
  /**
   * Position of this node in the min heap.
   */
  struct GNUNET_CONTAINER_HeapNode *heap_node;

  /**
   * Bloomfilter containing entries for peers
   * we forwarded this request to.
   */
  struct GNUNET_CONTAINER_BloomFilter *bloom;

  /**
   * Timestamp of this request, for ordering
   * the min heap.
   */
  struct GNUNET_TIME_Absolute timestamp;

  /**
   * Key of this request.
   */
  GNUNET_HashCode key;

  /**
   * Unique identifier for this request, 0 if from another peer.
   */
  uint64_t uid;

  /**
   * Task to remove this entry on timeout.
   */
  GNUNET_SCHEDULER_TaskIdentifier remove_task;
};


/**
 * Recent requests by time inserted.
 */
static struct GNUNET_CONTAINER_Heap *recent_heap;

/**
 * Context to use to calculate find peer rates.
 */
static struct FindPeerMessageContext find_peer_context;

/**
 * How many peers have we added since we sent out our last
 * find peer request?
 */
static unsigned int newly_found_peers;

/**
 * Handle to the datacache service (for inserting/retrieving data)
 */
static struct GNUNET_DATACACHE_Handle *datacache;

/**
 * Handle for the statistics service.
 */
struct GNUNET_STATISTICS_Handle *stats;

/**
 * Handle to get our current HELLO.
 */
static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;

/**
 * The configuration the DHT service is running with
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Handle to the core service
 */
static struct GNUNET_CORE_Handle *coreAPI;

/**
 * Handle to the transport service, for getting our hello
 */
static struct GNUNET_TRANSPORT_Handle *transport_handle;

/**
 * The identity of our peer.
 */
static struct GNUNET_PeerIdentity my_identity;

/**
 * Short id of the peer, for printing
 */
static char *my_short_id;

/**
 * Our HELLO
 */
static struct GNUNET_MessageHeader *my_hello;

/**
 * Task to run when we shut down, cleaning up all our trash
 */
static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;

/**
 * Recently seen find peer requests.
 */
static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;

/**
 * Reply times for requests, if we are busy, don't send any
 * more requests!
 */
static struct GNUNET_TIME_Relative reply_times[MAX_REPLY_TIMES];

/**
 * Current counter for replies.
 */
static unsigned int reply_counter;

/**
 * Our handle to the BLOCK library.
 */
static struct GNUNET_BLOCK_Context *block_context;



/** Declare here so retry_core_send is aware of it */
static size_t
core_transmit_notify (void *cls, size_t size, void *buf);



/**
 * Given the largest send delay, artificially decrease it
 * so the next time around we may have a chance at sending
 * again.
 */
static void
decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
{
  unsigned int i;

  for (i = 0; i < MAX_REPLY_TIMES; i++)
  {
    if (reply_times[i].rel_value == max_time.rel_value)
    {
      reply_times[i].rel_value = reply_times[i].rel_value / 2;
      return;
    }
  }
}


/**
 * Find the maximum send time of the recently sent values.
 *
 * @return the average time between asking core to send a message
 *         and when the buffer for copying it is passed
 */
static struct GNUNET_TIME_Relative
get_max_send_delay ()
{
  unsigned int i;
  struct GNUNET_TIME_Relative max_time;

  max_time = GNUNET_TIME_relative_get_zero ();

  for (i = 0; i < MAX_REPLY_TIMES; i++)
  {
    if (reply_times[i].rel_value > max_time.rel_value)
      max_time.rel_value = reply_times[i].rel_value;
  }
#if DEBUG_DHT
  if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
                (unsigned long long) max_time.rel_value);
#endif
  return max_time;
}


static void
increment_stats (const char *value)
{
  if (stats == NULL)
    return;
  GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
}


static void
decrement_stats (const char *value)
{
  if (stats == NULL)
    return;
  GNUNET_STATISTICS_update (stats, value, -1, GNUNET_NO);
}


/**
 *  Try to send another message from our core send list
 */
static void
try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerInfo *peer = cls;
  struct P2PPendingMessage *pending;
  size_t ssize;

  peer->send_task = GNUNET_SCHEDULER_NO_TASK;

  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
    return;

  if (peer->th != NULL)
    return;                     /* Message send already in progress */

  pending = peer->head;
  if (pending != NULL)
  {
    ssize = ntohs (pending->msg->size);
#if DEBUG_DHT > 1
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
                my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
#endif
    pending->scheduled = GNUNET_TIME_absolute_get ();
    reply_counter++;
    if (reply_counter >= MAX_REPLY_TIMES)
      reply_counter = 0;
    peer->th =
        GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
                                           pending->importance,
                                           pending->timeout, &peer->id, ssize,
                                           &core_transmit_notify, peer);
    if (peer->th == NULL)
      increment_stats ("# notify transmit ready failed");
  }
}


/**
 * Function called to send a request out to another peer.
 * Called both for locally initiated requests and those
 * received from other peers.
 *
 * @param msg the encapsulated message
 * @param peer the peer to forward the message to
 * @param msg_ctx the context of the message (hop count, bloom, etc.)
 */
static void
forward_result_message (const struct GNUNET_MessageHeader *msg,
                        struct PeerInfo *peer,
                        struct DHT_MessageContext *msg_ctx)
{
  struct GNUNET_DHT_P2PRouteResultMessage *result_message;
  struct P2PPendingMessage *pending;
  size_t msize;
  size_t psize;
  char *path_start;
  char *path_offset;

  increment_stats (STAT_RESULT_FORWARDS);
  msize =
      sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size) +
      (sizeof (struct GNUNET_PeerIdentity) * msg_ctx->path_history_len);
  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
  psize = sizeof (struct P2PPendingMessage) + msize;
  pending = GNUNET_malloc (psize);
  pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
  pending->importance = DHT_SEND_PRIORITY;
  pending->timeout = GNUNET_TIME_relative_get_forever ();
  result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
  result_message->header.size = htons (msize);
  result_message->header.type =
      htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
  result_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
  if (msg_ctx->path_history_len > 0)
  {
    /* End of pending is where enc_msg starts */
    path_start = (char *) &pending[1];
    /* Offset by the size of the enc_msg */
    path_start += ntohs (msg->size);
    memcpy (path_start, msg_ctx->path_history,
            msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
  }
  result_message->options = htonl (msg_ctx->msg_options);
  result_message->hop_count = htonl (msg_ctx->hop_count + 1);
  memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
  /* Copy the enc_msg, then the path history as well! */
  memcpy (&result_message[1], msg, ntohs (msg->size));
  path_offset = (char *) &result_message[1];
  path_offset += ntohs (msg->size);
  /* If we have path history, copy it to the end of the whole thing */
  if (msg_ctx->path_history_len > 0)
    memcpy (path_offset, msg_ctx->path_history,
            msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
#if DEBUG_DHT > 1
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
              "DHT", msize, GNUNET_i2s (&peer->id));
#endif
  peer->pending_count++;
  increment_stats ("# pending messages scheduled");
  GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
                                     pending);
  if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
}


/**
 * Called when core is ready to send a message we asked for
 * out to the destination.
 *
 * @param cls closure (NULL)
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */
static size_t
core_transmit_notify (void *cls, size_t size, void *buf)
{
  struct PeerInfo *peer = cls;
  char *cbuf = buf;
  struct P2PPendingMessage *pending;

  size_t off;
  size_t msize;

  peer->th = NULL;
  if (buf == NULL)
  {
    /* client disconnected */
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
                my_short_id, "DHT");
#endif
    return 0;
  }

  if (peer->head == NULL)
    return 0;

  off = 0;
  pending = peer->head;
  while (NULL != pending &&
         (size - off >= (msize = ntohs (pending->msg->size))))
  {
    memcpy (&cbuf[off], pending->msg, msize);
    off += msize;
    peer->pending_count--;
    increment_stats ("# pending messages sent");
    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
    GNUNET_free (pending);
    pending = peer->head;
  }
  if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);

  return off;
}


/**
 * Compute the distance between have and target as a 32-bit value.
 * Differences in the lower bits must count stronger than differences
 * in the higher bits.
 *
 * @return 0 if have==target, otherwise a number
 *           that is larger as the distance between
 *           the two hash codes increases
 */
static unsigned int
distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
{
  unsigned int bucket;
  unsigned int msb;
  unsigned int lsb;
  unsigned int i;

  /* We have to represent the distance between two 2^9 (=512)-bit
   * numbers as a 2^5 (=32)-bit number with "0" being used for the
   * two numbers being identical; furthermore, we need to
   * guarantee that a difference in the number of matching
   * bits is always represented in the result.
   *
   * We use 2^32/2^9 numerical values to distinguish between
   * hash codes that have the same LSB bit distance and
   * use the highest 2^9 bits of the result to signify the
   * number of (mis)matching LSB bits; if we have 0 matching
   * and hence 512 mismatching LSB bits we return -1 (since
   * 512 itself cannot be represented with 9 bits) */

  /* first, calculate the most significant 9 bits of our
   * result, aka the number of LSBs */
  bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
  /* bucket is now a value between 0 and 512 */
  if (bucket == 512)
    return 0;                   /* perfect match */
  if (bucket == 0)
    return (unsigned int) -1;   /* LSB differs; use max (if we did the bit-shifting
                                 * below, we'd end up with max+1 (overflow)) */

  /* calculate the most significant bits of the final result */
  msb = (512 - bucket) << (32 - 9);
  /* calculate the 32-9 least significant bits of the final result by
   * looking at the differences in the 32-9 bits following the
   * mismatching bit at 'bucket' */
  lsb = 0;
  for (i = bucket + 1;
       (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
  {
    if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
        GNUNET_CRYPTO_hash_get_bit (have, i))
      lsb |= (1 << (bucket + 32 - 9 - i));      /* first bit set will be 10,
                                                 * last bit set will be 31 -- if
                                                 * i does not reach 512 first... */
  }
  return msb | lsb;
}


/**
 * Return a number that is larger the closer the
 * "have" GNUNET_hash code is to the "target".
 *
 * @return inverse distance metric, non-zero.
 *         Must fudge the value if NO bits match.
 */
static unsigned int
inverse_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
{
  if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
    return 1;                   /* Never return 0! */
  return ((unsigned int) -1) - distance (target, have);
}


/**
 * Find which k-bucket this peer should go into,
 * taking into account the size of the k-bucket
 * array.  This means that if more bits match than
 * there are currently buckets, lowest_bucket will
 * be returned.
 *
 * @param hc GNUNET_HashCode we are finding the bucket for.
 *
 * @return the proper bucket index for this key,
 *         or GNUNET_SYSERR on error (same hashcode)
 */
static int
find_current_bucket (const GNUNET_HashCode * hc)
{
  int actual_bucket;

  actual_bucket = find_bucket (hc);
  if (actual_bucket == GNUNET_SYSERR)   /* hc and our peer identity match! */
    return lowest_bucket;
  if (actual_bucket < lowest_bucket)    /* actual_bucket not yet used */
    return lowest_bucket;
  return actual_bucket;
}


/**
 * Find a routing table entry from a peer identity
 *
 * @param peer the peer identity to look up
 *
 * @return the routing table entry, or NULL if not found
 */
static struct PeerInfo *
find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
{
  int bucket;
  struct PeerInfo *pos;

  bucket = find_current_bucket (&peer->hashPubKey);

  if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
    return NULL;

  pos = k_buckets[bucket].head;
  while (pos != NULL)
  {
    if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
      return pos;
    pos = pos->next;
  }
  return NULL;                  /* No such peer. */
}

/* Forward declaration */
static void
update_core_preference (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc);


/**
 * Function called with statistics about the given peer.
 *
 * @param cls closure
 * @param peer identifies the peer
 * @param bpm_out set to the current bandwidth limit (sending) for this peer
 * @param amount set to the amount that was actually reserved or unreserved;
 *               either the full requested amount or zero (no partial reservations)
 * @param res_delay if the reservation could not be satisfied (amount was 0), how
 *        long should the client wait until re-trying?
 * @param preference current traffic preference for the given peer
 */
static void
update_core_preference_finish (void *cls,
                               const struct GNUNET_PeerIdentity *peer,
                               struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
                               int32_t amount,
                               struct GNUNET_TIME_Relative res_delay,
                               uint64_t preference)
{
  struct PeerInfo *peer_info = cls;

  peer_info->info_ctx = NULL;
  GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
                                &update_core_preference, peer_info);
}

static void
update_core_preference (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct PeerInfo *peer = cls;
  uint64_t preference;
  unsigned int matching;

  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
  {
    return;
  }
  matching =
      GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
                                        &peer->id.hashPubKey);
  if (matching >= 64)
  {
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Peer identifier matches by %u bits, only shifting as much as we can!\n",
                matching);
#endif
    matching = 63;
  }
  preference = 1LL << matching;
  peer->info_ctx =
      GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
                                          GNUNET_TIME_UNIT_FOREVER_REL,
                                          GNUNET_BANDWIDTH_VALUE_MAX, 0,
                                          preference,
                                          &update_core_preference_finish, peer);
}

/**
 * Find the closest peer in our routing table to the
 * given hashcode.
 *
 * @return The closest peer in our routing table to the
 *         key, or NULL on error.
 */
static struct PeerInfo *
find_closest_peer (const GNUNET_HashCode * hc)
{
  struct PeerInfo *pos;
  struct PeerInfo *current_closest;
  unsigned int lowest_distance;
  unsigned int temp_distance;
  int bucket;
  int count;

  lowest_distance = -1;

  if (k_buckets[lowest_bucket].peers_size == 0)
    return NULL;

  current_closest = NULL;
  for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
  {
    pos = k_buckets[bucket].head;
    count = 0;
    while ((pos != NULL) && (count < bucket_size))
    {
      temp_distance = distance (&pos->id.hashPubKey, hc);
      if (temp_distance <= lowest_distance)
      {
        lowest_distance = temp_distance;
        current_closest = pos;
      }
      pos = pos->next;
      count++;
    }
  }
  GNUNET_assert (current_closest != NULL);
  return current_closest;
}


/**
 * Function called to send a request out to another peer.
 * Called both for locally initiated requests and those
 * received from other peers.
 *
 * @param msg the encapsulated message
 * @param peer the peer to forward the message to
 * @param msg_ctx the context of the message (hop count, bloom, etc.)
 */
static void
forward_message (const struct GNUNET_MessageHeader *msg, struct PeerInfo *peer,
                 struct DHT_MessageContext *msg_ctx)
{
  struct GNUNET_DHT_P2PRouteMessage *route_message;
  struct P2PPendingMessage *pending;
  size_t msize;
  size_t psize;
  char *route_path;

  increment_stats (STAT_ROUTE_FORWARDS);
  GNUNET_assert (peer != NULL);
  if ((msg_ctx->closest != GNUNET_YES) &&
      (peer == find_closest_peer (&msg_ctx->key)))
    increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);

  msize =
      sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size) +
      (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
  psize = sizeof (struct P2PPendingMessage) + msize;
  pending = GNUNET_malloc (psize);
  pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
  pending->importance = msg_ctx->importance;
  pending->timeout = msg_ctx->timeout;
  route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
  route_message->header.size = htons (msize);
  route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
  route_message->options = htonl (msg_ctx->msg_options);
  route_message->hop_count = htonl (msg_ctx->hop_count + 1);
  route_message->network_size = htonl (msg_ctx->network_size);
  route_message->desired_replication_level = htonl (msg_ctx->replication);
  if (msg_ctx->bloom != NULL)
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
                                                              route_message->
                                                              bloomfilter,
                                                              DHT_BLOOM_SIZE));
  memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
  memcpy (&route_message[1], msg, ntohs (msg->size));
  if (GNUNET_DHT_RO_RECORD_ROUTE ==
      (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
  {
    route_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
    /* Set pointer to start of enc_msg */
    route_path = (char *) &route_message[1];
    /* Offset to the end of the enc_msg */
    route_path += ntohs (msg->size);
    /* Copy the route_path after enc_msg */
    memcpy (route_path, msg_ctx->path_history,
            msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
  }
#if DEBUG_DHT > 1
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
              "DHT", msize, GNUNET_i2s (&peer->id));
#endif
  peer->pending_count++;
  increment_stats ("# pending messages scheduled");
  GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
                                     pending);
  if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
}




/**
 * Called when a reply needs to be sent to a client, as
 * a result it found to a GET or FIND PEER request.
 *
 * @param client the client to send the reply to
 * @param message the encapsulated message to send
 * @param msg_ctx the context of the received message
 */
static void
send_reply_to_client (struct ClientList *client,
                      const struct GNUNET_MessageHeader *message,
                      struct DHT_MessageContext *msg_ctx)
{
  struct GNUNET_DHT_RouteResultMessage *reply;
  struct PendingMessage *pending_message;
  uint16_t msize;
  size_t tsize;
  char *reply_offset;

#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Sending reply to client.\n",
              my_short_id, "DHT");
#endif
  msize = ntohs (message->size);
  tsize =
      sizeof (struct GNUNET_DHT_RouteResultMessage) + msize +
      (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
  if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  {
    GNUNET_break_op (0);
    return;
  }
  pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
  pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
  reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
  reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
  reply->header.size = htons (tsize);
  reply->outgoing_path_length = htonl (msg_ctx->path_history_len);
  reply->unique_id = GNUNET_htonll (msg_ctx->unique_id);
  memcpy (&reply->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
  reply_offset = (char *) &reply[1];
  memcpy (&reply[1], message, msize);
  if (msg_ctx->path_history_len > 0)
  {
    reply_offset += msize;
    memcpy (reply_offset, msg_ctx->path_history,
            msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
  }
  add_pending_message (client, pending_message);
}

/**
 * Consider whether or not we would like to have this peer added to
 * our routing table.  Check whether bucket for this peer is full,
 * if so return negative; if not return positive.  Since peers are
 * only added on CORE level connect, this doesn't actually add the
 * peer to the routing table.
 *
 * @param peer the peer we are considering adding
 *
 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
 *         already full)
 */
static int
consider_peer (struct GNUNET_PeerIdentity *peer)
{
  int bucket;

  if ((GNUNET_YES ==
       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
                                               &peer->hashPubKey)) ||
      (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
    return GNUNET_NO;           /* We already know this peer (are connected even!) */
  bucket = find_current_bucket (&peer->hashPubKey);

  if ((k_buckets[bucket].peers_size < bucket_size) ||
      ((bucket == lowest_bucket) && (lowest_bucket > 0)))
    return GNUNET_YES;

  return GNUNET_NO;
}


/**
 * Task used to remove forwarding entries, either
 * after timeout, when full, or on shutdown.
 *
 * @param cls the entry to remove
 * @param tc context, reason, etc.
 */
static void
remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct DHTRouteSource *source_info = cls;
  struct DHTQueryRecord *record;

  source_info = GNUNET_CONTAINER_heap_remove_node (source_info->hnode);
  record = source_info->record;
  GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);

  if (record->head == NULL)     /* No more entries in DLL */
  {
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
                                                         &record->key, record));
    GNUNET_free (record);
  }
  if (source_info->find_peers_responded != NULL)
    GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
  GNUNET_free (source_info);
}

/**
 * Main function that handles whether or not to route a result
 * message to other peers, or to send to our local client.
 *
 * @param msg the result message to be routed
 * @param msg_ctx context of the message we are routing
 *
 * @return the number of peers the message was routed to,
 *         GNUNET_SYSERR on failure
 */
static int
route_result_message (struct GNUNET_MessageHeader *msg,
                      struct DHT_MessageContext *msg_ctx)
{
  struct GNUNET_PeerIdentity new_peer;
  struct DHTQueryRecord *record;
  struct DHTRouteSource *pos;
  struct PeerInfo *peer_info;
  const struct GNUNET_MessageHeader *hello_msg;

#if DEBUG_DHT > 1
  unsigned int i;
#endif

  increment_stats (STAT_RESULTS);
  /**
   * If a find peer result message is received and contains a valid
   * HELLO for another peer, offer it to the transport service.
   */
  if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
  {
    if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
      GNUNET_break_op (0);

    hello_msg = &msg[1];
    if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
        (GNUNET_SYSERR ==
         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello_msg,
                              &new_peer)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "%s:%s Received non-HELLO message type in find peer result message!\n",
                  my_short_id, "DHT");
      GNUNET_break_op (0);
      return GNUNET_NO;
    }
    else                        /* We have a valid hello, and peer id stored in new_peer */
    {
      find_peer_context.count++;
      increment_stats (STAT_FIND_PEER_REPLY);
      if (GNUNET_YES == consider_peer (&new_peer))
      {
        increment_stats (STAT_HELLOS_PROVIDED);
        GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg, NULL, NULL);
        GNUNET_CORE_peer_request_connect (coreAPI, &new_peer, NULL, NULL);
      }
    }
  }

  record =
    GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);

  if (record == NULL)           /* No record of this message! */
  {
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "`%s:%s': Have no record of response key %s uid %llu\n",
                my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
                msg_ctx->unique_id);
#endif
    return 0;
  }

  pos = record->head;
  while (pos != NULL)
  {
    if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity)))  /* Local client (or DHT) initiated request! */
    {
#if DEBUG_DHT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "`%s:%s': Sending response key %s uid %llu to client\n",
                  my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
                  msg_ctx->unique_id);
#endif
      increment_stats (STAT_RESULTS_TO_CLIENT);
      if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
        increment_stats (STAT_GET_REPLY);
#if DEBUG_DHT > 1
      for (i = 0; i < msg_ctx->path_history_len; i++)
      {
        char *path_offset;

        path_offset =
            &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "(before client) Key %s Found peer %d:%s\n",
                    GNUNET_h2s (&msg_ctx->key), i,
                    GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
      }
#endif
      send_reply_to_client (pos->client, msg, msg_ctx);
    }
    else                        /* Send to peer */
    {
      peer_info = find_peer_by_id (&pos->source);
      if (peer_info == NULL)    /* Didn't find the peer in our routing table, perhaps peer disconnected! */
      {
        pos = pos->next;
        continue;
      }
#if DEBUG_DHT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
		  my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
		  msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
#endif
      forward_result_message (msg, peer_info, msg_ctx);
      /* Try removing forward entries after sending once, only allows ONE response per request */
      if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
      {
	GNUNET_SCHEDULER_cancel (pos->delete_task);
	pos->delete_task =
	  GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);        
      }
    }
    pos = pos->next;
  }
  return 0;
}


/**
 * Iterator for local get request results,
 *
 * @param cls closure for iterator, a DatacacheGetContext
 * @param exp when does this value expire?
 * @param key the key this data is stored under
 * @param size the size of the data identified by key
 * @param data the actual data
 * @param type the type of the data
 *
 * @return GNUNET_OK to continue iteration, anything else
 * to stop iteration.
 */
static int
datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
                        const GNUNET_HashCode * key, size_t size,
                        const char *data, enum GNUNET_BLOCK_Type type)
{
  struct DHT_MessageContext *msg_ctx = cls;
  struct DHT_MessageContext new_msg_ctx;
  struct GNUNET_DHT_GetResultMessage *get_result;
  enum GNUNET_BLOCK_EvaluationResult eval;
  const struct DHTPutEntry *put_entry;
  int get_size;
  char *path_offset;

#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Received `%s' response from datacache\n", my_short_id,
              "DHT", "GET");
#endif

  put_entry = (const struct DHTPutEntry *) data;

  if (size !=
      sizeof (struct DHTPutEntry) + put_entry->data_size +
      (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Path + data size doesn't add up for data inserted into datacache!\nData size %d, path length %d, expected %d, got %d\n",
                put_entry->data_size, put_entry->path_length,
                sizeof (struct DHTPutEntry) + put_entry->data_size +
                (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)),
                size);
    msg_ctx->do_forward = GNUNET_NO;
    return GNUNET_OK;
  }

  eval =
      GNUNET_BLOCK_evaluate (block_context, type, key, &msg_ctx->reply_bf,
                             msg_ctx->reply_bf_mutator, msg_ctx->xquery,
                             msg_ctx->xquery_size, &put_entry[1],
                             put_entry->data_size);

  switch (eval)
  {
  case GNUNET_BLOCK_EVALUATION_OK_LAST:
    msg_ctx->do_forward = GNUNET_NO;
  case GNUNET_BLOCK_EVALUATION_OK_MORE:
    memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
    if (GNUNET_DHT_RO_RECORD_ROUTE ==
        (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
    {
      new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
    }

    get_size =
        sizeof (struct GNUNET_DHT_GetResultMessage) + put_entry->data_size +
        (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity));
    get_result = GNUNET_malloc (get_size);
    get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
    get_result->header.size = htons (get_size);
    get_result->expiration = GNUNET_TIME_absolute_hton (exp);
    get_result->type = htons (type);
    get_result->put_path_length = htons (put_entry->path_length);
    path_offset = (char *) &put_entry[1];
    path_offset += put_entry->data_size;
    /* Copy the actual data and the path_history to the end of the get result */
    memcpy (&get_result[1], &put_entry[1],
            put_entry->data_size +
            (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)));
    new_msg_ctx.peer = my_identity;
    new_msg_ctx.bloom = NULL;
    new_msg_ctx.hop_count = 0;
    new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;   /* Make result routing a higher priority */
    new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
    increment_stats (STAT_GET_RESPONSE_START);
    route_result_message (&get_result->header, &new_msg_ctx);
    GNUNET_free (get_result);
    break;
  case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Duplicate block error\n",
                my_short_id, "DHT");
#endif
    break;
  case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "`%s:%s': Invalid request error\n",
                my_short_id, "DHT");
#endif
    break;
  case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "`%s:%s': Valid request, no results.\n", my_short_id, "DHT");
#endif
    GNUNET_break (0);
    break;
  case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
    GNUNET_break_op (0);
    msg_ctx->do_forward = GNUNET_NO;
    break;
  case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "`%s:%s': Unsupported block type (%u) in response!\n",
                my_short_id, "DHT", type);
#endif
    /* msg_ctx->do_forward = GNUNET_NO;  // not sure... */
    break;
  }
  return GNUNET_OK;
}


/**
 * Main function that handles whether or not to route a message to other
 * peers.
 *
 * @param msg the message to be routed
 * @param msg_ctx the context containing all pertinent information about the message
 */
static void
route_message (const struct GNUNET_MessageHeader *msg,
               struct DHT_MessageContext *msg_ctx);


/**
 * Server handler for all dht get requests, look for data,
 * if found, send response either to clients or other peers.
 *
 * @param msg the actual get message
 * @param msg_ctx struct containing pertinent information about the get request
 *
 * @return number of items found for GET request
 */
static unsigned int
handle_dht_get (const struct GNUNET_MessageHeader *msg,
                struct DHT_MessageContext *msg_ctx)
{
  const struct GNUNET_DHT_GetMessage *get_msg;
  uint16_t msize;
  uint16_t bf_size;
  unsigned int results;
  const char *end;
  enum GNUNET_BLOCK_Type type;

  msize = ntohs (msg->size);
  if (msize < sizeof (struct GNUNET_DHT_GetMessage))
  {
    GNUNET_break (0);
    return 0;
  }
  get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
  bf_size = ntohs (get_msg->bf_size);
  msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
  msg_ctx->reply_bf_mutator = get_msg->bf_mutator;
  if (msize !=
      sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
  {
    GNUNET_break_op (0);
    return 0;
  }
  end = (const char *) &get_msg[1];
  if (msg_ctx->xquery_size == 0)
  {
    msg_ctx->xquery = NULL;
  }
  else
  {
    msg_ctx->xquery = (const void *) end;
    end += msg_ctx->xquery_size;
  }
  if (bf_size == 0)
  {
    msg_ctx->reply_bf = NULL;
  }
  else
  {
    msg_ctx->reply_bf =
        GNUNET_CONTAINER_bloomfilter_init (end, bf_size,
                                           GNUNET_DHT_GET_BLOOMFILTER_K);
  }
  type = (enum GNUNET_BLOCK_Type) ntohl (get_msg->type);
#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
              my_short_id, "DHT", "GET", type, GNUNET_h2s (&msg_ctx->key),
              msg_ctx->unique_id);
#endif
  increment_stats (STAT_GETS);
  results = 0;
  msg_ctx->do_forward = GNUNET_YES;
  if (datacache != NULL)
    results =
        GNUNET_DATACACHE_get (datacache, &msg_ctx->key, type,
                              &datacache_get_iterator, msg_ctx);
#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Found %d results for `%s' request uid %llu\n",
              my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
#endif
  if (results >= 1)
  {
  }
  else
  {
    /* check query valid */
    if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID ==
        GNUNET_BLOCK_evaluate (block_context, type, &msg_ctx->key,
                               &msg_ctx->reply_bf, msg_ctx->reply_bf_mutator,
                               msg_ctx->xquery, msg_ctx->xquery_size, NULL, 0))
    {
      GNUNET_break_op (0);
      msg_ctx->do_forward = GNUNET_NO;
    }
  }

  if (msg_ctx->do_forward == GNUNET_YES)
    route_message (msg, msg_ctx);
  GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
  return results;
}


static void
remove_recent_find_peer (void *cls,
                         const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GNUNET_HashCode *key = cls;

  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove
                 (recent_find_peer_requests, key, NULL));
  GNUNET_free (key);
}


/**
 * Server handler for initiating local dht find peer requests
 *
 * @param find_msg the actual find peer message
 * @param msg_ctx struct containing pertinent information about the request
 *
 */
static void
handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
                      struct DHT_MessageContext *msg_ctx)
{
  struct GNUNET_MessageHeader *find_peer_result;
  struct GNUNET_DHT_FindPeerMessage *find_peer_message;
  struct DHT_MessageContext *new_msg_ctx;
  struct GNUNET_CONTAINER_BloomFilter *incoming_bloom;
  size_t hello_size;
  size_t tsize;
  GNUNET_HashCode *recent_hash;
  struct GNUNET_MessageHeader *other_hello;
  size_t other_hello_size;
  struct GNUNET_PeerIdentity peer_id;

  find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
  GNUNET_break_op (ntohs (find_msg->size) >=
                   (sizeof (struct GNUNET_DHT_FindPeerMessage)));
  if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
    return;
  other_hello = NULL;
  other_hello_size = 0;
  if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
  {
    other_hello_size =
        ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
    other_hello = GNUNET_malloc (other_hello_size);
    memcpy (other_hello, &find_peer_message[1], other_hello_size);
    if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) == 0)
        || (GNUNET_OK !=
            GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *) other_hello,
                                 &peer_id)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Received invalid HELLO message in find peer request!\n");
      GNUNET_free (other_hello);
      return;
    }
#if FIND_PEER_WITH_HELLO
    if (GNUNET_YES == consider_peer (&peer_id))
    {
      increment_stats (STAT_HELLOS_PROVIDED);
      GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello, NULL, NULL);
      GNUNET_CORE_peer_request_connect (coreAPI, &peer_id, NULL, NULL);
      route_message (find_msg, msg_ctx);
      GNUNET_free (other_hello);
      return;
    }
    else                        /* We don't want this peer! */
    {
      route_message (find_msg, msg_ctx);
      GNUNET_free (other_hello);
      return;
    }
#endif
  }

#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
              my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
              ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
#endif
  if (my_hello == NULL)
  {
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "`%s': Our HELLO is null, can't return.\n", "DHT");
#endif
    GNUNET_free_non_null (other_hello);
    route_message (find_msg, msg_ctx);
    return;
  }

  incoming_bloom =
      GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
                                         DHT_BLOOM_SIZE, DHT_BLOOM_K);
  if (GNUNET_YES ==
      GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
                                         &my_identity.hashPubKey))
  {
    increment_stats (STAT_BLOOM_FIND_PEER);
    GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
    GNUNET_free_non_null (other_hello);
    route_message (find_msg, msg_ctx);
    return;                     /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
  }
  GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);

  /**
   * Ignore any find peer requests from a peer we have seen very recently.
   */
  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent_find_peer_requests, &msg_ctx->key))  /* We have recently responded to a find peer request for this peer! */
  {
    increment_stats ("# dht find peer requests ignored (recently seen!)");
    GNUNET_free_non_null (other_hello);
    return;
  }

  /**
   * Use this check to only allow the peer to respond to find peer requests if
   * it would be beneficial to have the requesting peer in this peers routing
   * table.  Can be used to thwart peers flooding the network with find peer
   * requests that we don't care about.  However, if a new peer is joining
   * the network and has no other peers this is a problem (assume all buckets
   * full, no one will respond!).
   */
  memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
  if (GNUNET_NO == consider_peer (&peer_id))
  {
    increment_stats ("# dht find peer requests ignored (do not need!)");
    GNUNET_free_non_null (other_hello);
    route_message (find_msg, msg_ctx);
    return;
  }

  recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
  memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
  if (GNUNET_SYSERR !=
      GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
                                         &msg_ctx->key, NULL,
                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  {
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Adding recent remove task for key `%s`!\n",
                GNUNET_h2s (&msg_ctx->key));
#endif
    /* Only add a task if there wasn't one for this key already! */
    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                  (GNUNET_TIME_UNIT_SECONDS, 30),
                                  &remove_recent_find_peer, recent_hash);
  }
  else
  {
    GNUNET_free (recent_hash);
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received duplicate find peer request too soon!\n");
#endif
  }

  /* Simplistic find_peer functionality, always return our hello */
  hello_size = ntohs (my_hello->size);
  tsize = hello_size + sizeof (struct GNUNET_MessageHeader);

  if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  {
    GNUNET_break_op (0);
    GNUNET_free_non_null (other_hello);
    return;
  }

  find_peer_result = GNUNET_malloc (tsize);
  find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
  find_peer_result->size = htons (tsize);
  memcpy (&find_peer_result[1], my_hello, hello_size);
#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s': Sending hello size %d to requesting peer.\n", "DHT",
              hello_size);
#endif

  new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
  memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
  new_msg_ctx->peer = my_identity;
  new_msg_ctx->bloom =
      GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
  new_msg_ctx->hop_count = 0;
  new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;     /* Make find peer requests a higher priority */
  new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
  increment_stats (STAT_FIND_PEER_ANSWER);
  if (GNUNET_DHT_RO_RECORD_ROUTE ==
      (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
  {
    new_msg_ctx->msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
    new_msg_ctx->path_history_len = msg_ctx->path_history_len;
    /* Assign to previous msg_ctx path history, caller should free after our return */
    new_msg_ctx->path_history = msg_ctx->path_history;
  }
  route_result_message (find_peer_result, new_msg_ctx);
  GNUNET_free (new_msg_ctx);
  GNUNET_free_non_null (other_hello);
  GNUNET_free (find_peer_result);
  route_message (find_msg, msg_ctx);
}


/**
 * Server handler for initiating local dht put requests
 *
 * @param msg the actual put message
 * @param msg_ctx struct containing pertinent information about the request
 */
static void
handle_dht_put (const struct GNUNET_MessageHeader *msg,
                struct DHT_MessageContext *msg_ctx)
{
  const struct GNUNET_DHT_PutMessage *put_msg;
  struct DHTPutEntry *put_entry;
  unsigned int put_size;
  char *path_offset;
  enum GNUNET_BLOCK_Type put_type;
  size_t data_size;
  int ret;
  GNUNET_HashCode key;
  struct DHTQueryRecord *record;

  GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));

  put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
  put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
  data_size =
      ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
  ret =
      GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
                            &key);
  if (GNUNET_NO == ret)
  {
    /* invalid reply */
    GNUNET_break_op (0);
    return;
  }
  if ((GNUNET_YES == ret) &&
      (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
  {
    /* invalid wrapper: key mismatch! */
    GNUNET_break_op (0);
    return;
  }
  /* ret == GNUNET_SYSERR means that there is no known relationship between
   * data and the key, so we cannot check it */
#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
              msg_ctx->unique_id);
#endif

  record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap,
                                             &msg_ctx->key);
  if (NULL != record)
  {
    struct DHTRouteSource *pos;
    struct GNUNET_DHT_GetResultMessage *get_result;
    struct DHT_MessageContext new_msg_ctx;
    size_t get_size;

    pos = record->head;
    while (pos != NULL)
    {
      /* TODO: do only for local started requests? or also for remote peers? */
      /* TODO: include this in statistics? under what? */
      /* TODO: reverse order of path_history? */
      if (NULL == pos->client)
      {
        pos = pos->next;
        continue;
      }

      memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
      if (GNUNET_DHT_RO_RECORD_ROUTE ==
          (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
      {
        new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
      }

      get_size =
          sizeof (struct GNUNET_DHT_GetResultMessage) + data_size +
          (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
      get_result = GNUNET_malloc (get_size);
      get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
      get_result->header.size = htons (get_size);
      get_result->expiration = put_msg->expiration;
      get_result->type = put_msg->type;
      get_result->put_path_length = htons (msg_ctx->path_history_len);

      /* Copy the actual data and the path_history to the end of the get result */
      memcpy (&get_result[1], &put_msg[1], data_size);
      path_offset = (char *) &get_result[1];
      path_offset += data_size;
      memcpy (path_offset, msg_ctx->path_history,
              msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
      new_msg_ctx.peer = my_identity;
      new_msg_ctx.bloom = NULL;
      new_msg_ctx.hop_count = 0;
      /* Make result routing a higher priority */
      new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;
      new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
      new_msg_ctx.unique_id = pos->uid;
      send_reply_to_client(pos->client, &get_result->header, &new_msg_ctx);
      GNUNET_free (get_result);
      pos = pos->next;
    }
  }

  if (msg_ctx->closest != GNUNET_YES)
  {
    route_message (msg, msg_ctx);
    return;
  }

#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
              msg_ctx->unique_id);
#endif

  increment_stats (STAT_PUTS_INSERTED);
  if (datacache != NULL)
  {
    /* Put size is actual data size plus struct overhead plus path length (if any) */
    put_size =
        data_size + sizeof (struct DHTPutEntry) +
        (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
    put_entry = GNUNET_malloc (put_size);
    put_entry->data_size = data_size;
    put_entry->path_length = msg_ctx->path_history_len;
    /* Copy data to end of put entry */
    memcpy (&put_entry[1], &put_msg[1], data_size);
    if (msg_ctx->path_history_len > 0)
    {
      /* Copy path after data */
      path_offset = (char *) &put_entry[1];
      path_offset += data_size;
      memcpy (path_offset, msg_ctx->path_history,
              msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
    }

    ret =
        GNUNET_DATACACHE_put (datacache, &msg_ctx->key, put_size,
                              (const char *) put_entry, put_type,
                              GNUNET_TIME_absolute_ntoh (put_msg->expiration));
    GNUNET_free (put_entry);
  }
  else
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "`%s:%s': %s request received, but have no datacache!\n",
                my_short_id, "DHT", "PUT");

  route_message (msg, msg_ctx);
}


/**
 * To how many peers should we (on average)
 * forward the request to obtain the desired
 * target_replication count (on average).
 *
 * returns: target_replication / (est. hops) + (target_replication * hop_count)
 * where est. hops is typically 2 * the routing table depth
 *
 * @param hop_count number of hops the message has traversed
 * @param target_replication the number of total paths desired
 *
 * @return Some number of peers to forward the message to
 */
static unsigned int
get_forward_count (unsigned int hop_count, size_t target_replication)
{
  uint32_t random_value;
  unsigned int forward_count;
  float target_value;

  if (hop_count > log_of_network_size_estimate * 4.0)
  {
    /* forcefully terminate */
    return 0;
  }

  if (hop_count > log_of_network_size_estimate * 2.0)
  {
    /* keep forwarding, but no more replication */
    return 1;
  }

  target_value =
    1 + (target_replication - 1.0) / (log_of_network_size_estimate +
				      ((float) (target_replication - 1.0) *
				       hop_count));
  /* Set forward count to floor of target_value */
  forward_count = (unsigned int) target_value;
  /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
  target_value = target_value - forward_count;
  random_value =
    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX); 
  if (random_value < (target_value * UINT32_MAX))
    forward_count++;
  return forward_count;
}


/**
 * Check whether my identity is closer than any known peers.
 * If a non-null bloomfilter is given, check if this is the closest
 * peer that hasn't already been routed to.
 *
 * @param target hash code to check closeness to
 * @param bloom bloomfilter, exclude these entries from the decision
 * @return GNUNET_YES if node location is closest,
 *         GNUNET_NO otherwise.
 */
static int
am_closest_peer (const GNUNET_HashCode * target,
                 struct GNUNET_CONTAINER_BloomFilter *bloom)
{
  int bits;
  int other_bits;
  int bucket_num;
  int count;
  struct PeerInfo *pos;
  unsigned int my_distance;

  if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
    return GNUNET_YES;

  bucket_num = find_current_bucket (target);

  bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
  my_distance = distance (&my_identity.hashPubKey, target);
  pos = k_buckets[bucket_num].head;
  count = 0;
  while ((pos != NULL) && (count < bucket_size))
  {
    if ((bloom != NULL) &&
        (GNUNET_YES ==
         GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
    {
      pos = pos->next;
      continue;                 /* Skip already checked entries */
    }

    other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
    if (other_bits > bits)
      return GNUNET_NO;
    else if (other_bits == bits)        /* We match the same number of bits */
    {
      if (distance (&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
        return GNUNET_NO;
    }
    pos = pos->next;
  }

  /* No peers closer, we are the closest! */
  return GNUNET_YES;
}


/**
 * Select a peer from the routing table that would be a good routing
 * destination for sending a message for "target".  The resulting peer
 * must not be in the set of blocked peers.<p>
 *
 * Note that we should not ALWAYS select the closest peer to the
 * target, peers further away from the target should be chosen with
 * exponentially declining probability.
 *
 * @param target the key we are selecting a peer to route to
 * @param bloom a bloomfilter containing entries this request has seen already
 * @param hops how many hops has this message traversed thus far
 *
 * @return Peer to route to, or NULL on error
 */
static struct PeerInfo *
select_peer (const GNUNET_HashCode * target,
             struct GNUNET_CONTAINER_BloomFilter *bloom, unsigned int hops)
{
  unsigned int bc;
  unsigned int count;
  unsigned int selected;
  struct PeerInfo *pos;
  unsigned int distance;
  unsigned int largest_distance;
  struct PeerInfo *chosen;

  if (hops >= log_of_network_size_estimate)
  {
    /* greedy selection (closest peer that is not in bloomfilter) */
    largest_distance = 0;
    chosen = NULL;
    for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
    {
      pos = k_buckets[bc].head;
      count = 0;
      while ((pos != NULL) && (count < bucket_size))
      {
        /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
        if (GNUNET_NO ==
            GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
        {
          distance = inverse_distance (target, &pos->id.hashPubKey);
          if (distance > largest_distance)
          {
            chosen = pos;
            largest_distance = distance;
          }
        }
        count++;
        pos = pos->next;
      }
    }
    if ((largest_distance > 0) && (chosen != NULL))
    {
      GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
      return chosen;
    }
    return NULL;                /* no peer available or we are the closest */
  }


  /* select "random" peer */
  /* count number of peers that are available and not filtered */
  count = 0;
  for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
  {
    pos = k_buckets[bc].head;
    while ((pos != NULL) && (count < bucket_size))
    {
      if (GNUNET_YES ==
          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
      {
        pos = pos->next;
	increment_stats ("# peer blocked from selection by Bloom filter");
        continue;               /* Ignore bloomfiltered peers */
      }
      count++;
      pos = pos->next;
    }
  }
  if (count == 0)               /* No peers to select from! */
  {
    increment_stats ("# failed to select peer");
    return NULL;
  }
  /* Now actually choose a peer */
  selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
  count = 0;
  for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
  {
    pos = k_buckets[bc].head;
    while ((pos != NULL) && (count < bucket_size))
    {
      if (GNUNET_YES ==
          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
      {
        pos = pos->next;
        continue;               /* Ignore bloomfiltered peers */
      }
      if (0 == selected--)
        return pos;
      pos = pos->next;
    }
  }
  GNUNET_break (0);
  return NULL;
}


/**
 * Remember this routing request so that if a reply is
 * received we can either forward it to the correct peer
 * or return the result locally.
 *
 * @param msg_ctx Context of the route request
 *
 * @return GNUNET_YES if this response was cached, GNUNET_NO if not
 */
static int
cache_response (struct DHT_MessageContext *msg_ctx)
{
  struct DHTQueryRecord *record;
  struct DHTRouteSource *source_info;
  struct DHTRouteSource *pos;
  struct GNUNET_TIME_Absolute now;
  unsigned int current_size;

  current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);

  while (current_size >= MAX_OUTSTANDING_FORWARDS)
  {
    source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
    GNUNET_assert (source_info != NULL);
    record = source_info->record;
    GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
    if (record->head == NULL)   /* No more entries in DLL */
    {
      GNUNET_assert (GNUNET_YES ==
                     GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
                                                           &record->key,
                                                           record));
      GNUNET_free (record);
    }
    if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
    {
      GNUNET_SCHEDULER_cancel (source_info->delete_task);
      source_info->delete_task = GNUNET_SCHEDULER_NO_TASK;
    }
    if (source_info->find_peers_responded != NULL)
      GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
    GNUNET_free (source_info);
    current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
  }

  /** Non-local request and have too many outstanding forwards, discard! */
  if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
    return GNUNET_NO;

  now = GNUNET_TIME_absolute_get ();
  record =
      GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
  if (record != NULL)           /* Already know this request! */
  {
    pos = record->head;
    while (pos != NULL)
    {
      if (0 ==
	  memcmp (&msg_ctx->peer, &pos->source,
		  sizeof (struct GNUNET_PeerIdentity)))
	break;                  /* Already have this peer in reply list! */
      pos = pos->next;
    }
    if ((pos != NULL) && (pos->client == msg_ctx->client))      /* Seen this already */
    {
      GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
                                         now.abs_value);
      return GNUNET_NO;
    }
  }
  else
  {
    record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
                                                      &msg_ctx->key, record,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
    memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
  }

  source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
  source_info->record = record;
  source_info->delete_task =
      GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
                                    source_info);
  source_info->find_peers_responded =
      GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
  source_info->source = msg_ctx->peer;
  GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
                                     source_info);
  if (msg_ctx->client != NULL)  /* For local request, set timeout so high it effectively never gets pushed out */
  {
    source_info->client = msg_ctx->client;
    now = GNUNET_TIME_absolute_get_forever ();
  }
  source_info->hnode =
      GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
                                    now.abs_value);
  source_info->uid = msg_ctx->unique_id;
#if DEBUG_DHT > 1
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "`%s:%s': Created new forward source info for %s uid %llu\n",
              my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
              msg_ctx->unique_id);
#endif
  return GNUNET_YES;
}


/**
 * Main function that handles whether or not to route a message to other
 * peers.
 *
 * @param msg the message to be routed
 * @param msg_ctx the context containing all pertinent information about the message
 */
static void
route_message (const struct GNUNET_MessageHeader *msg,
               struct DHT_MessageContext *msg_ctx)
{
  int i;
  struct PeerInfo *selected;
  unsigned int target_forward_count;
  unsigned int forward_count;
  struct RecentRequest *recent_req;
  char *stat_forward_count;
  char *temp_stat_str;

  increment_stats (STAT_ROUTES);
  target_forward_count =
      get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
  GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
                   target_forward_count);
  increment_stats (stat_forward_count);
  GNUNET_free (stat_forward_count);
  if (msg_ctx->bloom == NULL)
    msg_ctx->bloom =
        GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);

  if (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT)
  {
    recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
    GNUNET_assert (recent_req != NULL);
    GNUNET_SCHEDULER_cancel (recent_req->remove_task);
    GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
    GNUNET_CONTAINER_bloomfilter_free (recent_req->bloom);
    GNUNET_free (recent_req);
  }

  recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
  recent_req->uid = msg_ctx->unique_id;
  memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
  recent_req->heap_node =
    GNUNET_CONTAINER_heap_insert (recent_heap, recent_req,
				  GNUNET_TIME_absolute_get ().abs_value);
  recent_req->bloom =
    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);

  forward_count = 0;
  for (i = 0; i < target_forward_count; i++)
  {
    selected = select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
    if (selected == NULL)
      break;    
    forward_count++;
    if (GNUNET_CRYPTO_hash_matching_bits
	(&selected->id.hashPubKey,
	 &msg_ctx->key) >=
	GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
					  &msg_ctx->key))
      GNUNET_asprintf (&temp_stat_str,
		       "# requests routed to close(r) peer hop %u",
		       msg_ctx->hop_count);
    else
      GNUNET_asprintf (&temp_stat_str,
		       "# requests routed to less close peer hop %u",
		       msg_ctx->hop_count);
    if (temp_stat_str != NULL)
    {
      increment_stats (temp_stat_str);
      GNUNET_free (temp_stat_str);
    }
    GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
				      &selected->id.hashPubKey);
    forward_message (msg, selected, msg_ctx);    
  }

  if (msg_ctx->bloom != NULL)
  {
    GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
                                      DHT_BLOOM_SIZE);
    GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
    msg_ctx->bloom = NULL;
  }
}


/**
 * Main function that handles whether or not to route a message to other
 * peers.
 *
 * @param msg the message to be routed
 * @param msg_ctx the context containing all pertinent information about the message
 */
static void
demultiplex_message (const struct GNUNET_MessageHeader *msg,
                     struct DHT_MessageContext *msg_ctx)
{
  /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
  msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);

  switch (ntohs (msg->type))
  {
  case GNUNET_MESSAGE_TYPE_DHT_GET:    /* Add to hashmap of requests seen, search for data (always) */
    cache_response (msg_ctx);
    handle_dht_get (msg, msg_ctx);
    break;
  case GNUNET_MESSAGE_TYPE_DHT_PUT:    /* Check if closest, if so insert data. */
    increment_stats (STAT_PUTS);
    handle_dht_put (msg, msg_ctx);
    break;
  case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:      /* Check if closest and not started by us, check options, add to requests seen */
    increment_stats (STAT_FIND_PEER);
    if (((msg_ctx->hop_count > 0) &&
         (0 !=
          memcmp (&msg_ctx->peer, &my_identity,
                  sizeof (struct GNUNET_PeerIdentity)))) ||
        (msg_ctx->client != NULL))
    {
      cache_response (msg_ctx);
      if ((msg_ctx->closest == GNUNET_YES) ||
          (msg_ctx->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
        handle_dht_find_peer (msg, msg_ctx);
    }
    else
      route_message (msg, msg_ctx);
    break;
  default:
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "`%s': Message type (%d) not handled, forwarding anyway!\n",
                "DHT", ntohs (msg->type));
    route_message (msg, msg_ctx);
  }
}


/**
 * Receive the HELLO from transport service,
 * free current and replace if necessary.
 *
 * @param cls NULL
 * @param message HELLO message of peer
 */
static void
process_hello (void *cls, const struct GNUNET_MessageHeader *message)
{
#if DEBUG_DHT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received our `%s' from transport service\n", "HELLO");
#endif
  GNUNET_assert (message != NULL);
  GNUNET_free_non_null (my_hello);
  my_hello = GNUNET_malloc (ntohs (message->size));
  memcpy (my_hello, message, ntohs (message->size));
}


/**
 * Task run during shutdown.
 *
 * @param cls unused
 * @param tc unused
 */
static void
shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  int bucket_count;
  struct PeerInfo *pos;

  if (NULL != ghh)
  {
    GNUNET_TRANSPORT_get_hello_cancel (ghh);
    ghh = NULL;
  }
  if (transport_handle != NULL)
  {
    GNUNET_free_non_null (my_hello);
    GNUNET_TRANSPORT_disconnect (transport_handle);
    transport_handle = NULL;
  }
  GDS_NEIGHBOURS_done ();
  GDS_NSE_done ();
  for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
  {
    while (k_buckets[bucket_count].head != NULL)
    {
      pos = k_buckets[bucket_count].head;
#if DEBUG_DHT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
                  "DHT", GNUNET_i2s (&pos->id), bucket_count);
#endif
      delete_peer (pos, bucket_count);
    }
  }
  if (datacache != NULL)
  {
#if DEBUG_DHT
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Destroying datacache!\n",
                my_short_id, "DHT");
#endif
    GNUNET_DATACACHE_destroy (datacache);
    datacache = NULL;
  }
  if (stats != NULL)
  {
    GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
    stats = NULL;
  }
  if (block_context != NULL)
  {
    GNUNET_BLOCK_context_destroy (block_context);
    block_context = NULL;
  }
}



/**
 * Process dht requests.
 *
 * @param cls closure
 * @param server the initialized server
 * @param c configuration to use
 */
static void
run (void *cls, struct GNUNET_SERVER_Handle *server,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  struct GNUNET_TIME_Relative next_send_time;
  unsigned long long temp_config_num;

  cfg = c;
  datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
  coreAPI = GNUNET_CORE_connect (cfg,   /* Main configuration */
                                 DEFAULT_CORE_QUEUE_SIZE,       /* queue size */
                                 NULL,  /* Closure passed to DHT functions */
                                 &core_init,    /* Call core_init once connected */
                                 &handle_core_connect,  /* Handle connects */
                                 &handle_core_disconnect,       /* remove peers on disconnects */
                                 NULL,  /* Do we care about "status" updates? */
                                 NULL,  /* Don't want notified about all incoming messages */
                                 GNUNET_NO,     /* For header only inbound notification */
                                 NULL,  /* Don't want notified about all outbound messages */
                                 GNUNET_NO,     /* For header only outbound notification */
                                 core_handlers);        /* Register these handlers */

  if (coreAPI == NULL)
    return;
  transport_handle =
      GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
  if (transport_handle != NULL)
    ghh = GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
  else
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Failed to connect to transport service!\n");
  block_context = GNUNET_BLOCK_context_create (cfg);
  lowest_bucket = MAX_BUCKETS - 1;
  all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
  GNUNET_assert (all_known_peers != NULL);

  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
                                             &temp_config_num))
  {
    bucket_size = (unsigned int) temp_config_num;
  }

  stats = GNUNET_STATISTICS_create ("dht", cfg);
  next_send_time.rel_value =
    DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
			      (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
			       2) -
			      DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
  find_peer_context.start = GNUNET_TIME_absolute_get ();
  GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
				&find_peer_context);  

  /* Scheduled the task to clean up when shutdown is called */
  cleanup_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                    &shutdown_task, NULL);
}


/**
 * The main function for the dht service.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  int ret;
  struct RecentRequest *recent_req;

  recent_heap =
    GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  recent_find_peer_requests =
      GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
  ret =
      (GNUNET_OK ==
       GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
                           NULL)) ? 0 : 1;
  while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0)
  {
    recent_req = GNUNET_CONTAINER_heap_peek (recent_heap);
    GNUNET_assert (recent_req != NULL);
    GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node);
    GNUNET_CONTAINER_bloomfilter_free (recent_req->bloom);
    GNUNET_free (recent_req);
  }
  GNUNET_CONTAINER_heap_destroy (recent_heap);
  recent_heap = NULL;
  GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
  recent_find_peer_requests = NULL;
  return ret;
}

/* end of gnunet-service-dht.c */