aboutsummaryrefslogtreecommitdiff
path: root/src/social/gnunet-service-social.c
blob: 6fd2383b74b8ade99330d2c64018ba001c1b6c77 (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
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
/*
 * This file is part of GNUnet
 * Copyright (C) 2013 GNUnet e.V.
 *
 * GNUnet is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * 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
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file social/gnunet-service-social.c
 * @brief Social service
 * @author Gabor X Toth
 */

#include <inttypes.h>
#include <strings.h>

#include <gnunet/platform.h>
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_constants.h>
#include <gnunet/gnunet_protocols.h>
#include <gnunet/gnunet_identity_service.h>
#include <gnunet/gnunet_namestore_service.h>
#include <gnunet/gnunet_gns_service.h>
#include <gnunet/gnunet_statistics_service.h>
#include "gnunet_psyc_service.h"
#include "gnunet_psyc_util_lib.h"
#include "gnunet_social_service.h"
#include "social.h"


/**
 * Handle to our current configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Service handle.
 */
static struct GNUNET_SERVICE_Handle *service;

/* Handles to other services */
static struct GNUNET_IDENTITY_Handle *id;
static struct GNUNET_GNS_Handle *gns;
static struct GNUNET_NAMESTORE_Handle *namestore;
static struct GNUNET_STATISTICS_Handle *stats;

/**
 * ID of this peer.
 */
static struct GNUNET_PeerIdentity this_peer;

/**
 * All connected hosts.
 * H(place_pub_key) -> struct Host
 */
static struct GNUNET_CONTAINER_MultiHashMap *hosts;

/**
 * All connected guests.
 * H(place_pub_key) -> struct Guest
 */
static struct GNUNET_CONTAINER_MultiHashMap *guests;

/**
 * Connected guests per place.
 * H(place_pub_key) -> ego_pub_key -> struct Guest
 */
static struct GNUNET_CONTAINER_MultiHashMap *place_guests;

/**
 * Places entered as host or guest.
 * H(place_pub_key) -> struct HostEnterRequest OR struct GuestEnterRequest
 */
static struct GNUNET_CONTAINER_MultiHashMap *places;

/**
 * Places entered per application.
 * H(app_id) -> H(place_pub_key) -> NULL
 */
static struct GNUNET_CONTAINER_MultiHashMap *apps_places;

/**
 * Application subscriptions per place.
 * H(place_pub_key) -> H(app_id)
 */
// static struct GNUNET_CONTAINER_MultiHashMap *places_apps;

/**
 * Connected applications.
 * H(app_id) -> struct Application
 */
static struct GNUNET_CONTAINER_MultiHashMap *apps;

/**
 * All egos.
 * H(ego_pub_key) -> struct Ego
 */
static struct GNUNET_CONTAINER_MultiHashMap *egos;

/**
 * Directory for storing social data.
 * Default: $GNUNET_DATA_HOME/social
 */
static char *dir_social;

/**
 * Directory for storing place data.
 * $dir_social/places
 */
static char *dir_places;

/**
 * Directory for storing app data.
 * $dir_social/apps
 */
static char *dir_apps;


/**
 * Message fragment transmission queue.
 */
struct FragmentTransmitQueue
{
  struct FragmentTransmitQueue *prev;
  struct FragmentTransmitQueue *next;

  struct GNUNET_SERVICE_Client *client;

  /**
   * Pointer to the next message part inside the data after this struct.
   */
  struct GNUNET_MessageHeader *next_part;

  /**
   * Size of message.
   */
  uint16_t size;

  /**
   * @see enum GNUNET_PSYC_MessageState
   */
  uint8_t state;

  /* Followed by one or more message parts. */
};


/**
 * Message transmission queue.
 */
struct MessageTransmitQueue
{
  struct MessageTransmitQueue *prev;
  struct MessageTransmitQueue *next;

  struct FragmentTransmitQueue *frags_head;
  struct FragmentTransmitQueue *frags_tail;

  struct GNUNET_SERVICE_Client *client;
};

/**
 * List of connected clients.
 */
struct ClientListItem
{
  struct ClientListItem *prev;
  struct ClientListItem *next;

  struct GNUNET_SERVICE_Client *client;
};


/**
 * Common part of the client context for both a host and guest.
 */
struct Place
{
  struct ClientListItem *clients_head;
  struct ClientListItem *clients_tail;

  struct MessageTransmitQueue *tmit_msgs_head;
  struct MessageTransmitQueue *tmit_msgs_tail;

  struct GNUNET_PSYC_Channel *channel;

  /**
   * Private key of home in case of a host.
   */
  struct GNUNET_CRYPTO_EddsaPublicKey key;

  /**
   * Public key of place.
   */
  struct GNUNET_CRYPTO_EddsaPublicKey pub_key;

  /**
   * Hash of @a pub_key.
   */
  struct GNUNET_HashCode pub_key_hash;

  /**
   * Private key of ego.
   */
  struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;

  /**
   * Public key of ego.
   */
  struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;

  /**
   * Hash of @a ego_pub_key.
   */
  struct GNUNET_HashCode ego_pub_hash;

  /**
   * Slicer for processing incoming messages.
   */
  struct GNUNET_PSYC_Slicer *slicer;

  /**
   * Last message ID received for the place.
   * 0 if there is no such message.
   */
  uint64_t max_message_id;

  /**
   * Offset where the file is currently being written.
   */
  uint64_t file_offset;

  /**
   * Whether or not to save the file (#GNUNET_YES or #GNUNET_NO)
   */
  uint8_t file_save;

  /**
   * Is this place ready to receive messages from client?
   * #GNUNET_YES or #GNUNET_NO
   */
  uint8_t is_ready;

  /**
   * Is the client disconnecting?
   * #GNUNET_YES or #GNUNET_NO
   */
  uint8_t is_disconnecting;

  /**
   * Is this a host (#GNUNET_YES), or guest (#GNUNET_NO)?
   */
  uint8_t is_host;

  union
  {
    struct Host *host;
    struct Guest *guest;
  };
};


/**
 * Client context for a host.
 */
struct Host
{
  /**
   * Place struct common for Host and Guest
   */
  struct Place place;

  /**
   * Handle for the multicast origin.
   */
  struct GNUNET_PSYC_Master *master;

  /**
   * Transmit handle for multicast.
   */
  struct GNUNET_PSYC_MasterTransmitHandle *tmit_handle;

  /**
   * Incoming join requests.
   * guest_key -> struct GNUNET_PSYC_JoinHandle *
   */
  struct GNUNET_CONTAINER_MultiHashMap *join_reqs;

  /**
   * Messages being relayed.
   */
  struct GNUNET_CONTAINER_MultiHashMap *relay_msgs;

  /**
   * @see enum GNUNET_PSYC_Policy
   */
  enum GNUNET_PSYC_Policy policy;
};


/**
 * Client context for a guest.
 */
struct Guest
{
  /**
   * Place struct common for Host and Guest.
   */
  struct Place place;

  /**
   * Handle for the PSYC slave.
   */
  struct GNUNET_PSYC_Slave *slave;

  /**
   * Transmit handle for multicast.
   */
  struct GNUNET_PSYC_SlaveTransmitHandle *tmit_handle;

  /**
   * Peer identity of the origin.
   */
  struct GNUNET_PeerIdentity origin;

  /**
   * Number of items in @a relays.
   */
  uint32_t relay_count;

  /**
   * Relays that multicast can use to connect.
   */
  struct GNUNET_PeerIdentity *relays;

  /**
   * Join request to be transmitted to the master on join.
   */
  struct GNUNET_MessageHeader *join_req; // FIXME: not used!

  /**
   * Join decision received from PSYC.
   */
  struct GNUNET_PSYC_JoinDecisionMessage *join_dcsn;

  /**
   * Join flags for the PSYC service.
   */
  enum GNUNET_PSYC_SlaveJoinFlags join_flags;
};


/**
 * Context for a client.
 */
struct Client
{
  /**
   * Client handle.
   */
  struct GNUNET_SERVICE_Client *client;

  /**
   * Place where the client entered.
   */
  struct Place *place;

  /**
   * Message queue for the message currently being transmitted
   * by this client.
   */
  struct MessageTransmitQueue *tmit_msg;

  /**
   * ID for application clients.
   */
  char *app_id;
};


struct Application
{
  struct ClientListItem *clients_head;
  struct ClientListItem *clients_tail;
};


struct Ego
{
  struct GNUNET_CRYPTO_EcdsaPrivateKey key;
  char *name;
};


struct OperationClosure
{
  struct Client *client;
  uint64_t op_id;
  uint32_t flags;
};


static int
psyc_transmit_message (struct Place *plc);


/**
 * Clean up place data structures after a client disconnected.
 *
 * @param cls the `struct Place` to clean up
 */
static void
cleanup_place (void *cls);


static struct MessageTransmitQueue *
psyc_transmit_queue_message (struct Place *plc,
                             struct GNUNET_SERVICE_Client *client,
                             size_t data_size,
                             const void *data,
                             uint16_t first_ptype, uint16_t last_ptype,
                             struct MessageTransmitQueue *tmit_msg);


static int
place_entry_cleanup (void *cls,
                     const struct GNUNET_HashCode *key,
                     void *value)
{
  struct Place *plc = value;

  cleanup_place (plc);
  return GNUNET_YES;
}


/**
 * Task run during shutdown.
 *
 * @param cls unused
 */
static void
shutdown_task (void *cls)
{
  GNUNET_CONTAINER_multihashmap_iterate (hosts, place_entry_cleanup, NULL);
  GNUNET_CONTAINER_multihashmap_iterate (guests, place_entry_cleanup, NULL);

  if (NULL != id)
  {
    GNUNET_IDENTITY_disconnect (id);
    id = NULL;
  }
  if (NULL != namestore)
  {
    GNUNET_NAMESTORE_disconnect (namestore);
    namestore = NULL;
  }
  if (NULL != gns)
  {
    GNUNET_GNS_disconnect (gns);
    gns = NULL;
  }
  if (NULL != stats)
  {
    GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
    stats = NULL;
  }
}


/**
 * Clean up host data structures after a client disconnected.
 */
static void
cleanup_host (struct Host *hst)
{
  struct Place *plc = &hst->place;

  GNUNET_CONTAINER_multihashmap_destroy (hst->join_reqs);
  GNUNET_CONTAINER_multihashmap_destroy (hst->relay_msgs);
  GNUNET_CONTAINER_multihashmap_remove (hosts, &plc->pub_key_hash, plc);
}


/**
 * Clean up guest data structures after a client disconnected.
 */
static void
cleanup_guest (struct Guest *gst)
{
  struct Place *plc = &gst->place;
  struct GNUNET_CONTAINER_MultiHashMap *
    plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests,
                                                 &plc->pub_key_hash);
  if (NULL != plc_gst)
  {
    GNUNET_CONTAINER_multihashmap_remove (plc_gst, &plc->ego_pub_hash, gst);

    if (0 == GNUNET_CONTAINER_multihashmap_size (plc_gst))
    {
      GNUNET_CONTAINER_multihashmap_remove (place_guests, &plc->pub_key_hash,
                                            plc_gst);
      GNUNET_CONTAINER_multihashmap_destroy (plc_gst);
    }
  }
  GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, gst);
  if (NULL != gst->join_req)
    GNUNET_free (gst->join_req);
  if (NULL != gst->relays)
    GNUNET_free (gst->relays);
  GNUNET_CONTAINER_multihashmap_remove (guests, &plc->pub_key_hash, plc);
}


/**
 * Clean up place data structures after a client disconnected.
 *
 * @param cls the `struct Place` to clean up
 */
static void
cleanup_place (void *cls)
{
  struct Place *plc = cls;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "cleaning up place %s\n",
              GNUNET_h2s (&plc->pub_key_hash));

  (GNUNET_YES == plc->is_host)
    ? cleanup_host ((struct Host *) plc)
    : cleanup_guest ((struct Guest *) plc);

  GNUNET_PSYC_slicer_destroy (plc->slicer);
  GNUNET_free (plc);
}


/**
 * Called whenever a client is disconnected.
 * Frees our resources associated with that client.
 *
 * @param cls closure
 * @param client identification of the client
 * @param app_ctx must match @a client
 */
static void
client_notify_disconnect (void *cls,
                          struct GNUNET_SERVICE_Client *client,
                          void *app_ctx)
{
  struct Client *c = app_ctx;
  struct Place *plc = c->place;

  if (NULL != c->app_id)
    GNUNET_free (c->app_id);

  GNUNET_free (c);

  if (NULL == plc)
    return; // application client, nothing to do

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Client (%s) disconnected from place %s\n",
              plc, (GNUNET_YES == plc->is_host) ? "host" : "guest",
              GNUNET_h2s (&plc->pub_key_hash));

  struct ClientListItem *cli = plc->clients_head;
  while (NULL != cli)
  {
    if (cli->client == client)
    {
      GNUNET_CONTAINER_DLL_remove (plc->clients_head,
                                   plc->clients_tail,
                                   cli);
      GNUNET_free (cli);
      break;
    }
    cli = cli->next;
  }
  if (GNUNET_YES == plc->is_disconnecting)
  {
    GNUNET_PSYC_slicer_destroy (plc->slicer);
    GNUNET_free (plc);
  }
}


/**
 * A new client connected.
 *
 * @param cls NULL
 * @param client client to add
 * @param mq message queue for @a client
 * @return @a client
 */
static void *
client_notify_connect (void *cls,
                       struct GNUNET_SERVICE_Client *client,
                       struct GNUNET_MQ_Handle *mq)
{
  struct Client *c = GNUNET_new (struct Client);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Client %p connected with queue %p\n",
              client,
              mq);
  c->client = client;
  return c;
}


/**
 * Send message to all clients connected to a place and
 * takes care of freeing @env.
 */
static void
place_send_msg (const struct Place *plc,
                struct GNUNET_MQ_Envelope *env)
{
  struct ClientListItem *cli = plc->clients_head;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending message to clients of place.\n", plc);
  while (NULL != cli)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Sending message to client %p\n",
                cli);
    GNUNET_MQ_send_copy (GNUNET_SERVICE_client_get_mq (cli->client),
                         env);
    cli = cli->next;
  }
  GNUNET_MQ_discard (env);
}


static void
place_send_leave_ack (struct Place *plc)
{
  struct GNUNET_MQ_Envelope *env;

  for (struct ClientListItem *cli = plc->clients_head;
       NULL != cli;
       cli = cli->next)
  {
    env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK);
    GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (cli->client),
                    env);
  }
}


/**
 * Send a result code back to the client.
 *
 * @param client
 *        Client that should receive the result code.
 * @param result_code
 *        Code to transmit.
 * @param op_id
 *        Operation ID in network byte order.
 * @param data
 *        Data payload or NULL.
 * @param data_size
 *        Size of @a data.
 */
static void
client_send_result (struct GNUNET_SERVICE_Client *client, uint64_t op_id,
                    int64_t result_code, const void *data, uint16_t data_size)
{
  struct GNUNET_MQ_Envelope *env;
  struct GNUNET_OperationResultMessage *res;

  env = GNUNET_MQ_msg_extra (res,
                             data_size,
                             GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE);
  res->result_code = GNUNET_htonll (result_code);
  res->op_id = op_id;
  if (0 < data_size)
    GNUNET_memcpy (&res[1], data, data_size);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending result to client for operation #%" PRIu64 ": "
              "%" PRId64 " (size: %u)\n",
              client, GNUNET_ntohll (op_id), result_code, data_size);
  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
}


static void
client_send_host_enter_ack (struct GNUNET_SERVICE_Client *client,
                            struct Host *hst, uint32_t result)
{
  struct GNUNET_MQ_Envelope *env;
  struct HostEnterAck *hack;
  struct Place *plc = &hst->place;

  env = GNUNET_MQ_msg (hack,
                       GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK);
  hack->result_code = htonl (result);
  hack->max_message_id = GNUNET_htonll (plc->max_message_id);
  hack->place_pub_key = plc->pub_key;

  if (NULL != client)
    GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                    env);
  else
    place_send_msg (plc, env);
}


/**
 * Called after a PSYC master is started.
 */
static void
psyc_master_started (void *cls, int result, uint64_t max_message_id)
{
  struct Host *hst = cls;
  struct Place *plc = &hst->place;
  plc->max_message_id = max_message_id;
  plc->is_ready = GNUNET_YES;

  client_send_host_enter_ack (NULL, hst, result);
}


/**
 * Called when a PSYC master receives a join request.
 */
static void
psyc_recv_join_request (void *cls,
                        const struct GNUNET_PSYC_JoinRequestMessage *req,
                        const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
                        const struct GNUNET_PSYC_Message *join_msg,
                        struct GNUNET_PSYC_JoinHandle *jh)
{
  struct Host *hst = cls;
  struct GNUNET_HashCode slave_key_hash;
  GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash);
  GNUNET_CONTAINER_multihashmap_put (hst->join_reqs, &slave_key_hash, jh,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  place_send_msg (&hst->place,
                  GNUNET_MQ_msg_copy (&req->header));
}


/**
 * Called after a PSYC slave is connected.
 */
static void
psyc_slave_connected (void *cls, int result, uint64_t max_message_id)
{
  struct GNUNET_PSYC_CountersResultMessage *res;
  struct GNUNET_MQ_Envelope *env;
  struct Guest *gst = cls;
  struct Place *plc = &gst->place;

  plc->max_message_id = max_message_id;
  plc->is_ready = GNUNET_YES;
  env = GNUNET_MQ_msg (res,
                       GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
  res->result_code =
    (result != GNUNET_SYSERR) ? htonl (GNUNET_OK) : htonl (GNUNET_SYSERR);
  res->max_message_id = GNUNET_htonll (plc->max_message_id);
  place_send_msg (plc, env);
}


static void
slave_parted_after_join_decision (void *cls)
{
  struct Guest *gst = cls;

  GNUNET_assert (NULL != gst->join_dcsn);
  place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
}


/**
 * Called when a PSYC slave receives a join decision.
 */
static void
psyc_recv_join_dcsn (void *cls,
                     const struct GNUNET_PSYC_JoinDecisionMessage *dcsn,
                     int is_admitted,
                     const struct GNUNET_PSYC_Message *join_msg)
{
  struct Guest *gst = cls;

  gst->join_dcsn = GNUNET_malloc (dcsn->header.size);
  GNUNET_memcpy (gst->join_dcsn,
                 dcsn,
                 dcsn->header.size);
  if (GNUNET_NO == is_admitted)
  {
    GNUNET_PSYC_slave_part (gst->slave,
                            GNUNET_NO,
                            &slave_parted_after_join_decision,
                            gst);
    gst->slave = NULL;
    return;
  }
  place_send_msg (&gst->place, GNUNET_MQ_msg_copy (&gst->join_dcsn->header));
}


/**
 * Called when a PSYC master or slave receives a message.
 */
static void
psyc_recv_message (void *cls,
                   const struct GNUNET_PSYC_MessageHeader *msg)
{
  struct Place *plc = cls;

  char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Received PSYC message of size %u from %s.\n",
              plc, ntohs (msg->header.size), str);
  GNUNET_free (str);

  GNUNET_PSYC_slicer_message (plc->slicer, msg);

  place_send_msg (plc, GNUNET_MQ_msg_copy (&msg->header));
}


/**
 * Relay a message part received from a guest to the the place.
 *
 * @param hst
 *        Host.
 * @param pmsg
 *        Message part.
 * @param nym_pub_key
 *        Nym the message is received from.
 */
static void
host_relay_message_part (struct Host *hst,
                         const struct GNUNET_MessageHeader *pmsg,
                         const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key)
{
  /* separate queue per nym */
  struct GNUNET_HashCode nym_pub_hash;
  GNUNET_CRYPTO_hash (nym_pub_key, sizeof (*nym_pub_key), &nym_pub_hash);

  struct MessageTransmitQueue *
    tmit_msg = GNUNET_CONTAINER_multihashmap_get (hst->relay_msgs,
                                                  &nym_pub_hash);

  uint16_t ptype = ntohs (pmsg->type);

  if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
  {
    /* FIXME: last message was unfinished, cancel & remove from queue */
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "FIXME: last message was unfinished.\n");
  }

  tmit_msg = psyc_transmit_queue_message (&hst->place, NULL, ntohs (pmsg->size),
                                          pmsg, ptype, ptype, tmit_msg);

  switch (ptype)
  {
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
    GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put
                     (hst->relay_msgs, &nym_pub_hash, tmit_msg,
                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
    break;
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
    GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove
                     (hst->relay_msgs, &nym_pub_hash, tmit_msg));
    break;
  }
}


/**
 * Received a method to be relayed from a guest.
 */
static void
place_recv_relay_method (void *cls,
                         const struct GNUNET_PSYC_MessageHeader *msg,
                         const struct GNUNET_PSYC_MessageMethod *meth,
                         uint64_t message_id,
                         const char *method_name)
{
  struct Place *plc = cls;

  if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
      && (GNUNET_YES == plc->is_host))
  {
    struct Host *hst = cls;
    host_relay_message_part (hst, &meth->header, &msg->slave_pub_key);
  }
}


/**
 * Received a modifier to be relayed from a guest.
 */
static void
place_recv_relay_modifier (void *cls,
                           const struct GNUNET_PSYC_MessageHeader *msg,
                           const struct GNUNET_MessageHeader *pmsg,
                           uint64_t message_id,
                           enum GNUNET_PSYC_Operator oper,
                           const char *name,
                           const void *value,
                           uint16_t value_size,
                           uint16_t full_value_size)
{
  struct Place *plc = cls;

  if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
      && (GNUNET_YES == plc->is_host))
  {
    struct Host *hst = cls;
    host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
  }
}


/**
 * Received a data fragment to be relayed from a guest.
 */
static void
place_recv_relay_data (void *cls,
                       const struct GNUNET_PSYC_MessageHeader *msg,
                       const struct GNUNET_MessageHeader *pmsg,
                       uint64_t message_id,
                       const void *data,
                       uint16_t data_size)
{
  struct Place *plc = cls;

  if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
      && (GNUNET_YES == plc->is_host))
  {
    struct Host *hst = cls;
    host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
  }
}


/**
 * Received end of message to be relayed from a guest.
 */
static void
place_recv_relay_eom (void *cls,
                      const struct GNUNET_PSYC_MessageHeader *msg,
                      const struct GNUNET_MessageHeader *pmsg,
                      uint64_t message_id,
                      uint8_t is_cancelled)
{
  struct Place *plc = cls;

  if (GNUNET_PSYC_MESSAGE_REQUEST & ntohs (msg->flags)
      && (GNUNET_YES == plc->is_host))
  {
    struct Host *hst = cls;
    host_relay_message_part (hst, pmsg, &msg->slave_pub_key);
  }
}


/**
 * Received a method to be saved to disk.
 *
 * Create a new file for writing the data part of the message into,
 * if the file does not yet exist.
 */
static void
place_recv_save_method (void *cls,
                        const struct GNUNET_PSYC_MessageHeader *msg,
                        const struct GNUNET_PSYC_MessageMethod *meth,
                        uint64_t message_id,
                        const char *method_name)
{
  struct Place *plc = cls;
  plc->file_offset = 0;
  plc->file_save = GNUNET_NO;

  char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (
    &plc->pub_key);
  char *filename = NULL;
  GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
                   dir_social, DIR_SEPARATOR,
                   "files", DIR_SEPARATOR,
                   place_pub_str, DIR_SEPARATOR,
                   GNUNET_ntohll (msg->message_id));
  GNUNET_free (place_pub_str);

  /* save if does not already exist */
  if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  {
    if (GNUNET_OK ==
        GNUNET_DISK_fn_write (filename, NULL, 0,
                              GNUNET_DISK_PERM_USER_READ
                              | GNUNET_DISK_PERM_USER_WRITE))
    {
      plc->file_save = GNUNET_YES;
    }
    else
    {
      GNUNET_break (0);
    }
  }
  GNUNET_free (filename);
}


/**
 * Received a data fragment to be saved to disk.
 *
 * Append data fragment to the file.
 */
static void
place_recv_save_data (void *cls,
                      const struct GNUNET_PSYC_MessageHeader *msg,
                      const struct GNUNET_MessageHeader *pmsg,
                      uint64_t message_id,
                      const void *data,
                      uint16_t data_size)
{
  struct Place *plc = cls;
  if (GNUNET_YES != plc->file_save)
    return;

  char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (
    &plc->pub_key);
  char *filename = NULL;
  GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%" PRIu64 ".part",
                   dir_social, DIR_SEPARATOR,
                   "files", DIR_SEPARATOR,
                   place_pub_str, DIR_SEPARATOR,
                   GNUNET_ntohll (msg->message_id));
  GNUNET_free (place_pub_str);
  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "create", filename);
    GNUNET_free (filename);
    return;
  }

  struct GNUNET_DISK_FileHandle *
    fh = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE,
                                GNUNET_DISK_PERM_NONE);
  if (NULL != fh)
  {
    if (plc->file_offset != GNUNET_DISK_file_seek
          (fh, plc->file_offset, GNUNET_DISK_SEEK_SET))
    {
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "seek", filename);
      GNUNET_DISK_file_close (fh);
      GNUNET_free (filename);
      return;
    }
    GNUNET_DISK_file_write (fh, data, data_size);
    GNUNET_DISK_file_close (fh);
    GNUNET_free (filename);
  }
  else
  {
    GNUNET_free (filename);
    GNUNET_break (0);
  }
  plc->file_offset += data_size;
}


/**
 * Received end of message to be saved to disk.
 *
 * Remove .part ending from the filename.
 */
static void
place_recv_save_eom (void *cls,
                     const struct GNUNET_PSYC_MessageHeader *msg,
                     const struct GNUNET_MessageHeader *pmsg,
                     uint64_t message_id,
                     uint8_t is_cancelled)
{
  struct Place *plc = cls;
  if (GNUNET_YES != plc->file_save)
    return;

  char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (
    &plc->pub_key);
  char *fn = NULL;
  GNUNET_asprintf (&fn, "%s%c%s%c%s%c%" PRIu64,
                   dir_social, DIR_SEPARATOR,
                   "files", DIR_SEPARATOR,
                   place_pub_str, DIR_SEPARATOR,
                   GNUNET_ntohll (msg->message_id));
  GNUNET_free (place_pub_str);
  char *fn_part = NULL;
  GNUNET_asprintf (&fn_part, "%s.part", fn);

  if (rename (fn_part, fn))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to rename %s into %s: %s (%d)\n",
                fn_part, fn, strerror (errno), errno);
  }

  GNUNET_free (fn);
  GNUNET_free (fn_part);
}


/**
 * Initialize place data structure.
 */
static void
place_init (struct Place *plc)
{
  plc->slicer = GNUNET_PSYC_slicer_create ();
}


/**
 * Add a place to the @e places hash map.
 *
 * @param ereq
 *        Entry request.
 *
 * @return #GNUNET_OK if the place was added
 *         #GNUNET_NO if the place already exists in the hash map
 *         #GNUNET_SYSERR on error
 */
static int
place_add (const struct PlaceEnterRequest *ereq)
{
  struct EgoPlacePublicKey ego_place_pub_key = {
    .ego_pub_key = ereq->ego_pub_key,
    .place_pub_key = ereq->place_pub_key,
  };
  struct GNUNET_HashCode ego_place_pub_hash;
  GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key),
                      &ego_place_pub_hash);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "  ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));

  struct GNUNET_MessageHeader *
    place_msg = GNUNET_CONTAINER_multihashmap_get (places, &ego_place_pub_hash);
  if (NULL != place_msg)
    return GNUNET_NO;

  place_msg = GNUNET_copy_message (&ereq->header);
  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (places,
                                                      &ego_place_pub_hash,
                                                      place_msg,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    GNUNET_break (0);
    GNUNET_free (place_msg);
    return GNUNET_SYSERR;
  }

  return GNUNET_OK;
}


/**
 * Add a place to the @e app_places hash map.
 *
 * @param app_id
 *        Application ID.
 * @param ereq
 *        Entry request.
 *
 * @return #GNUNET_OK if the place was added
 *         #GNUNET_NO if the place already exists in the hash map
 *         #GNUNET_SYSERR on error
 */
static int
app_place_add (const char *app_id,
               const struct PlaceEnterRequest *ereq)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Adding app place to hashmap:\n");
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "  app_id = %s\n", app_id);

  struct GNUNET_HashCode app_id_hash;
  GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);

  struct EgoPlacePublicKey ego_place_pub_key = {
    .ego_pub_key = ereq->ego_pub_key,
    .place_pub_key = ereq->place_pub_key,
  };
  struct GNUNET_HashCode ego_place_pub_hash;
  GNUNET_CRYPTO_hash (&ego_place_pub_key, sizeof (ego_place_pub_key),
                      &ego_place_pub_hash);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "  ego_place_pub_hash = %s\n", GNUNET_h2s (&ego_place_pub_hash));

  struct GNUNET_CONTAINER_MultiHashMap *
    app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
  if (NULL == app_places)
  {
    app_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
    GNUNET_CONTAINER_multihashmap_put (apps_places, &app_id_hash, app_places,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }

  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (app_places,
                                                            &ego_place_pub_hash))
    return GNUNET_NO;

  if (GNUNET_SYSERR == place_add (ereq))
  {
    return GNUNET_SYSERR;
  }

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (app_places,
                                                      &ego_place_pub_hash, NULL,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}


/**
 * Save place entry message to disk.
 *
 * @param app_id
 *        Application ID.
 * @param ereq
 *        Entry request message.
 */
static int
app_place_save (const char *app_id,
                const struct PlaceEnterRequest *ereq)
{
  if (GNUNET_SYSERR == app_place_add (app_id, ereq))
  {
    GNUNET_assert (0);
  }

  if (NULL == dir_places)
    return GNUNET_SYSERR;

  char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (
    &ereq->ego_pub_key);
  char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (
    &ereq->place_pub_key);
  char *filename = NULL;
  GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
                   dir_social, DIR_SEPARATOR,
                   "places", DIR_SEPARATOR,
                   ego_pub_str, DIR_SEPARATOR,
                   place_pub_str);
  if (GNUNET_OK !=
      GNUNET_DISK_fn_write (filename, ereq, ntohs (ereq->header.size),
                            GNUNET_DISK_PERM_USER_READ
                            | GNUNET_DISK_PERM_USER_WRITE))
  {
    GNUNET_break (0);
    ret = GNUNET_SYSERR;
  }
  GNUNET_free (filename);

  if (ret == GNUNET_OK)
  {
    GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s%c" "%s",
                     dir_social, DIR_SEPARATOR,
                     "apps", DIR_SEPARATOR,
                     app_id, DIR_SEPARATOR,
                     ego_pub_str, DIR_SEPARATOR,
                     place_pub_str);
    ret = GNUNET_DISK_directory_create_for_file (filename);
    if ((GNUNET_OK != ret)
        || (0 > GNUNET_DISK_fn_write (filename, "", 0,
                                      GNUNET_DISK_PERM_USER_READ
                                      | GNUNET_DISK_PERM_USER_WRITE)))
    {
      GNUNET_break (0);
      ret = GNUNET_SYSERR;
    }
    GNUNET_free (filename);
  }
  GNUNET_free (ego_pub_str);
  GNUNET_free (place_pub_str);
  return ret;
}


int
app_place_remove (const char *app_id,
                  const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
                  const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key)
{
  struct GNUNET_HashCode ego_pub_hash;
  struct GNUNET_HashCode place_pub_hash;
  GNUNET_CRYPTO_hash (ego_pub_key, sizeof (*ego_pub_key), &ego_pub_hash);
  GNUNET_CRYPTO_hash (place_pub_key, sizeof (*place_pub_key), &place_pub_hash);

  char *ego_pub_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (ego_pub_key);
  char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (
    place_pub_key);
  char *app_place_filename = NULL;
  GNUNET_asprintf (&app_place_filename,
                   "%s%c" "%s%c" "%s%c" "%s%c" "%s",
                   dir_social, DIR_SEPARATOR,
                   "apps", DIR_SEPARATOR,
                   app_id, DIR_SEPARATOR,
                   ego_pub_str, DIR_SEPARATOR,
                   place_pub_str);
  GNUNET_free (ego_pub_str);
  GNUNET_free (place_pub_str);

  struct GNUNET_HashCode app_id_hash;
  GNUNET_CRYPTO_hash (app_id, strlen (app_id) + 1, &app_id_hash);

  struct GNUNET_CONTAINER_MultiHashMap *
    app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);

  if (NULL != app_places)
    GNUNET_CONTAINER_multihashmap_remove (app_places, &place_pub_hash, NULL);

  int ret = GNUNET_OK;

  if (0 != unlink (app_place_filename))
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error removing app place file: %s: %s (%d)\n",
                app_place_filename, strerror (errno), errno);
    ret = GNUNET_SYSERR;
  }
  GNUNET_free (app_place_filename);

  return ret;
}


/**
 * Enter place as host.
 *
 * @param hreq
 *        Host entry request.
 * @param[out] ret_hst
 *        Returned Host struct.
 *
 * @return #GNUNET_YES if the host entered the place just now,
 *         #GNUNET_NO  if the place is already entered,
 *         #GNUNET_SYSERR if place_pub_key was set
 *                        but its private key was not found
 */
static int
host_enter (const struct HostEnterRequest *hreq, struct Host **ret_hst)
{
  int ret = GNUNET_NO;
  struct GNUNET_HashCode place_pub_hash;
  GNUNET_CRYPTO_hash (&hreq->place_pub_key, sizeof (hreq->place_pub_key),
                      &place_pub_hash);
  struct Host *hst = GNUNET_CONTAINER_multihashmap_get (hosts, &place_pub_hash);

  if (NULL == hst)
  {
    hst = GNUNET_new (struct Host);
    hst->policy = hreq->policy;
    hst->join_reqs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
    hst->relay_msgs = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);

    struct Place *plc = &hst->place;
    place_init (plc);
    plc->is_host = GNUNET_YES;
    plc->pub_key = hreq->place_pub_key;
    plc->pub_key_hash = place_pub_hash;

    GNUNET_CONTAINER_multihashmap_put (hosts, &plc->pub_key_hash, plc,
                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
    hst->master = GNUNET_PSYC_master_start (cfg, &hreq->place_key, hst->policy,
                                            &psyc_master_started,
                                            &psyc_recv_join_request,
                                            &psyc_recv_message, NULL, hst);
    plc->channel = GNUNET_PSYC_master_get_channel (hst->master);
    ret = GNUNET_YES;
  }

  if (NULL != ret_hst)
    *ret_hst = hst;
  return ret;
}


static int
msg_proc_parse (const struct MsgProcRequest *mpreq,
                uint32_t *flags,
                const char **method_prefix,
                struct GNUNET_HashCode *method_hash)
{
  ssize_t method_size = ntohs (mpreq->header.size) - sizeof (*mpreq);
  uint16_t offset;

  if (method_size < 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "MsgProcRequest has invalid size\n");
    return GNUNET_SYSERR;
  }

  offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &mpreq[1],
                                           method_size,
                                           1,
                                           method_prefix);
  if ((0 == offset) || (offset != method_size) || (*method_prefix == NULL))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "MsgProcRequest contains invalid method\n");
    return GNUNET_SYSERR;
  }
  GNUNET_CRYPTO_hash (*method_prefix, (size_t) method_size, method_hash);
  *flags = ntohl (mpreq->flags);
  return GNUNET_OK;
}


void
app_notify_place (const struct GNUNET_MessageHeader *msg,
                  struct GNUNET_SERVICE_Client *client)
{
  struct AppPlaceMessage *amsg;
  struct GNUNET_MQ_Envelope *env;
  uint16_t msg_size = ntohs (msg->size);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending place notification of type %u to client.\n",
              client, ntohs (msg->type));
  switch (ntohs (msg->type))
  {
  case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
    {
      struct HostEnterRequest *hreq = (struct HostEnterRequest *) msg;
      if (msg_size < sizeof (struct HostEnterRequest))
        return;
      env = GNUNET_MQ_msg (amsg,
                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
      // FIXME: also notify about not entered places
      amsg->place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
      amsg->is_host = GNUNET_YES;
      amsg->ego_pub_key = hreq->ego_pub_key;
      amsg->place_pub_key = hreq->place_pub_key;
      GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                      env);
      break;
    }
  case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
    {
      if (msg_size < sizeof (struct GuestEnterRequest))
        return;
      struct GuestEnterRequest *greq = (struct GuestEnterRequest *) msg;
      env = GNUNET_MQ_msg (amsg,
                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE);
      // FIXME: also notify about not entered places
      amsg->place_state = GNUNET_SOCIAL_PLACE_STATE_ENTERED;
      amsg->is_host = GNUNET_NO;
      amsg->ego_pub_key = greq->ego_pub_key;
      amsg->place_pub_key = greq->place_pub_key;
      GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                      env);
      break;
    }
  default:
    return;
  }
}


void
app_notify_place_end (struct GNUNET_SERVICE_Client *client)
{
  struct GNUNET_MQ_Envelope *env;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending end of place list notification to client\n",
              client);
  env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END);
  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                  env);
}


void
app_notify_ego (struct Ego *ego, struct GNUNET_SERVICE_Client *client)
{
  struct AppEgoMessage *emsg;
  struct GNUNET_MQ_Envelope *env;
  size_t name_size = strlen (ego->name) + 1;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending ego notification to client: %s\n",
              client, ego->name);
  env = GNUNET_MQ_msg_extra (emsg,
                             name_size,
                             GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO);
  GNUNET_CRYPTO_ecdsa_key_get_public (&ego->key, &emsg->ego_pub_key);
  GNUNET_memcpy (&emsg[1], ego->name, name_size);
  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                  env);
}


void
app_notify_ego_end (struct GNUNET_SERVICE_Client *client)
{
  struct GNUNET_MQ_Envelope *env;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Sending end of ego list notification to client\n",
              client);
  env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END);
  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                  env);
}


int
app_place_entry_notify (void *cls, const struct GNUNET_HashCode *key,
                        void *value)
{
  struct GNUNET_MessageHeader *
    msg = GNUNET_CONTAINER_multihashmap_get (places, key);
  if (NULL != msg)
    app_notify_place (msg, cls);
  return GNUNET_YES;
}


int
ego_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  app_notify_ego (value, cls);
  return GNUNET_YES;
}


static int
check_client_msg_proc_set (void *cls,
                           const struct MsgProcRequest *mpreq)
{
  return GNUNET_OK;
}


/**
 * Handle a client setting message proccesing flags for a method prefix.
 */
static void
handle_client_msg_proc_set (void *cls,
                            const struct MsgProcRequest *mpreq)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  if (NULL == plc)
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  const char *method_prefix = NULL;
  uint32_t flags = 0;
  struct GNUNET_HashCode method_hash;

  if (GNUNET_OK !=
      msg_proc_parse (mpreq, &flags, &method_prefix, &method_hash))
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }
#if 0
  GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
                                    place_recv_relay_method,
                                    place_recv_relay_modifier,
                                    place_recv_relay_data,
                                    place_recv_relay_eom);
  GNUNET_PSYC_slicer_method_remove (plc->slicer, method_prefix,
                                    place_recv_save_method,
                                    NULL,
                                    place_recv_save_data,
                                    place_recv_save_eom);
#endif
  if (flags & GNUNET_SOCIAL_MSG_PROC_RELAY)
  {
    GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
                                   place_recv_relay_method,
                                   place_recv_relay_modifier,
                                   place_recv_relay_data,
                                   place_recv_relay_eom,
                                   plc);
  }
  if (flags & GNUNET_SOCIAL_MSG_PROC_SAVE)
  {
    GNUNET_PSYC_slicer_method_add (plc->slicer, method_prefix, NULL,
                                   place_recv_save_method,
                                   NULL,
                                   place_recv_save_data,
                                   place_recv_save_eom,
                                   plc);
  }

  /** @todo Save flags to be able to resume relaying/saving after restart */

  GNUNET_SERVICE_client_continue (client);
}


/**
 * Handle a connecting client requesting to clear all relay rules.
 */
static void
handle_client_msg_proc_clear (void *cls,
                              const struct GNUNET_MessageHeader *msg)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  if (NULL == plc)
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  GNUNET_PSYC_slicer_clear (plc->slicer);

  GNUNET_SERVICE_client_continue (client);
}


static int
check_client_host_enter (void *cls,
                         const struct HostEnterRequest *hr)
{
  return GNUNET_OK;
}


/**
 * Handle a connecting client entering a place as host.
 */
static void
handle_client_host_enter (void *cls,
                          const struct HostEnterRequest *hr)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct HostEnterRequest *
    hreq = (struct HostEnterRequest *) GNUNET_copy_message (&hr->header);

  uint8_t app_id_size = ntohs (hreq->header.size) - sizeof (*hreq);
  const char *app_id = NULL;
  uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &hreq[1],
                                                    app_id_size, 1, &app_id);
  if ((0 == offset) || (offset != app_id_size) || (app_id == NULL))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "offset = %u, app_id_size = %u, app_id = %s\n",
                offset, app_id_size, app_id);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  struct Host *hst = NULL;
  struct Place *plc = NULL;
  int ret = GNUNET_OK;

  struct GNUNET_CRYPTO_EddsaPublicKey empty_pub_key;
  memset (&empty_pub_key, 0, sizeof (empty_pub_key));

  if (0 == memcmp (&hreq->place_pub_key, &empty_pub_key,
                   sizeof (empty_pub_key)))
  { // no public key set: create new private key & save the place
    struct GNUNET_CRYPTO_EddsaPrivateKey *
      place_key = GNUNET_CRYPTO_eddsa_key_create ();
    hreq->place_key = *place_key;
    GNUNET_CRYPTO_eddsa_key_get_public (place_key, &hreq->place_pub_key);
    GNUNET_CRYPTO_eddsa_key_clear (place_key);
    GNUNET_free (place_key);

    app_place_save (app_id, (const struct PlaceEnterRequest *) hreq);
  }

  switch (host_enter (hreq, &hst))
  {
  case GNUNET_YES:
    plc = c->place = &hst->place;
    plc->host = hst;
    break;

  case GNUNET_NO:
    {
      plc = c->place = &hst->place;
      plc->host = hst;
      client_send_host_enter_ack (client, hst, GNUNET_OK);
      break;
    }
  case GNUNET_SYSERR:
    ret = GNUNET_SYSERR;
  }

  if (ret != GNUNET_SYSERR)
  {

    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "%p Client connected as host to place %s.\n",
                hst, GNUNET_h2s (&plc->pub_key_hash));

    struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
    cli->client = client;
    GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
    c->place = plc;
    app_notify_place (&hreq->header, client);
  }

  GNUNET_CRYPTO_eddsa_key_clear (&hreq->place_key);
  GNUNET_free (hreq);

  if (GNUNET_OK == ret)
    GNUNET_SERVICE_client_continue (client);
  else
    GNUNET_SERVICE_client_drop (client);
}


/**
 * Enter place as guest.
 *
 * @param greq
 *        Guest entry request.
 * @param[out] ret_gst
 *        Returned Guest struct.
 *
 * @return #GNUNET_YES if the guest entered the place just now,
 *         #GNUNET_NO  if the place is already entered,
 *         #GNUNET_SYSERR on error.
 */
static int
guest_enter (const struct GuestEnterRequest *greq, struct Guest **ret_gst)
{
  int ret = GNUNET_NO;
  uint16_t greq_size = ntohs (greq->header.size);

  struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key = greq->ego_pub_key;
  struct GNUNET_HashCode ego_pub_hash;
  GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);
  struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);

  if (NULL == ego)
  {
    return GNUNET_SYSERR;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "entering as guest\n");
  struct GNUNET_HashCode place_pub_hash;
  GNUNET_CRYPTO_hash (&greq->place_pub_key, sizeof (greq->place_pub_key),
                      &place_pub_hash);

  struct GNUNET_CONTAINER_MultiHashMap *
    plc_gst = GNUNET_CONTAINER_multihashmap_get (place_guests, &place_pub_hash);
  struct Guest *gst = NULL;
  int new_guest;

  if (NULL != plc_gst)
    gst = GNUNET_CONTAINER_multihashmap_get (plc_gst, &ego_pub_hash);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "plc_gst = %p, gst = %p\n",
              plc_gst,
              gst);

  if (NULL == gst)
  {
    gst = GNUNET_new (struct Guest);
    new_guest = GNUNET_YES;
  }
  else
    new_guest = GNUNET_NO;

  if (NULL == gst->slave)
  {
    gst->origin = greq->origin;
    gst->relay_count = ntohl (greq->relay_count);

    uint16_t len;
    uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
    const char *app_id = (const char *) &greq[1];
    const char *p = app_id;

    len = strnlen (app_id, remaining);
    if (len == remaining)
    {
      GNUNET_free (gst);
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    p += len + 1;
    remaining -= len + 1;

    const struct GNUNET_PeerIdentity *relays = NULL;
    uint16_t relay_size = gst->relay_count * sizeof (*relays);
    if (remaining < relay_size)
    {
      GNUNET_free (gst);
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    if (0 < relay_size)
      relays = (const struct GNUNET_PeerIdentity *) p;
    p += relay_size;
    remaining -= relay_size;

    struct GNUNET_PSYC_Message *join_msg = NULL;
    uint16_t join_msg_size = 0;

    if (sizeof (struct GNUNET_MessageHeader) <= remaining)
    {
      join_msg = (struct GNUNET_PSYC_Message *) p;
      join_msg_size = ntohs (join_msg->header.size);
      p += join_msg_size;
      remaining -= join_msg_size;
    }
    if (0 != remaining)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "%zu + %u + %u != %u\n",
                  sizeof (*greq), relay_size, join_msg_size, greq_size);
      GNUNET_free (gst);
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    if (0 < relay_size)
    {
      gst->relays = GNUNET_malloc (relay_size);
      GNUNET_memcpy (gst->relays, relays, relay_size);
    }

    gst->join_flags = ntohl (greq->flags);

    struct Place *plc = &gst->place;
    place_init (plc);
    plc->is_host = GNUNET_NO;
    plc->pub_key = greq->place_pub_key;
    plc->pub_key_hash = place_pub_hash;
    plc->ego_pub_key = ego_pub_key;
    plc->ego_pub_hash = ego_pub_hash;
    plc->ego_key = ego->key;

    if (NULL == plc_gst)
    {
      plc_gst = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
      (void) GNUNET_CONTAINER_multihashmap_put (place_guests,
                                                &plc->pub_key_hash, plc_gst,
                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
    }
    if (GNUNET_YES == new_guest)
    {
      (void) GNUNET_CONTAINER_multihashmap_put (plc_gst, &plc->ego_pub_hash,
                                                gst,
                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
      (void) GNUNET_CONTAINER_multihashmap_put (guests, &plc->pub_key_hash, gst,
                                                GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);

    }
    gst->slave
      = GNUNET_PSYC_slave_join (cfg, &plc->pub_key, &plc->ego_key,
                                gst->join_flags, &gst->origin,
                                gst->relay_count, gst->relays,
                                &psyc_recv_message, NULL,
                                &psyc_slave_connected,
                                &psyc_recv_join_dcsn,
                                gst, join_msg);
    plc->channel = GNUNET_PSYC_slave_get_channel (gst->slave);
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "slave entered channel %p\n",
                plc->channel);
    ret = GNUNET_YES;
  }

  // TODO: explain to automatic code scanners why free(gst) not necessary
  if (NULL != ret_gst)
    *ret_gst = gst;
  return ret;
}


static int
client_guest_enter (struct Client *c,
                    const struct GuestEnterRequest *greq)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "client_guest_enter\n");
  struct GNUNET_PSYC_CountersResultMessage *result_msg;
  struct GNUNET_MQ_Envelope *env;
  struct GNUNET_SERVICE_Client *client = c->client;
  uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
  const char *app_id = NULL;
  uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &greq[1],
                                                    remaining, 1, &app_id);
  struct Guest *gst = NULL;
  struct Place *plc = NULL;

  if (0 == offset)
  {
    return GNUNET_SYSERR;
  }
  switch (guest_enter (greq, &gst))
  {
  case GNUNET_YES:
    {
      plc = c->place = &gst->place;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "guest entered successfully to local place %s\n",
                  GNUNET_h2s (&plc->pub_key_hash));
      plc->guest = gst;
      app_place_save (app_id, (const struct PlaceEnterRequest *) greq);
      app_notify_place (&greq->header, client);
      break;
    }
  case GNUNET_NO:
    {
      plc = c->place = &gst->place;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "guest re-entered successfully to local place %s\n",
                  GNUNET_h2s (&plc->pub_key_hash));
      plc->guest = gst;
      env = GNUNET_MQ_msg (result_msg,
                           GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK);
      result_msg->result_code = htonl (GNUNET_OK);
      result_msg->max_message_id = GNUNET_htonll (plc->max_message_id);
      GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                      env);
      if (NULL != gst->join_dcsn)
      {
        env = GNUNET_MQ_msg_copy (&gst->join_dcsn->header);
        GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                        env);
      }
      break;
    }
  case GNUNET_SYSERR:
    {
      return GNUNET_SYSERR;
    }
  }

  struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
  cli->client = client;
  GNUNET_CONTAINER_DLL_insert (plc->clients_head, plc->clients_tail, cli);
  return GNUNET_OK;
}


static int
check_client_guest_enter (void *cls,
                          const struct GuestEnterRequest *greq)
{
  return GNUNET_OK;
}


/**
 * Handle a connecting client entering a place as guest.
 */
static void
handle_client_guest_enter (void *cls,
                           const struct GuestEnterRequest *greq)
{
  struct Client *c = cls;

  if (GNUNET_SYSERR == client_guest_enter (c, greq))
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (c->client);
    return;
  }
  GNUNET_SERVICE_client_continue (c->client);
}


struct GuestEnterByNameClosure
{
  struct Client *client;
  char *app_id;
  char *password;
  struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
  struct GNUNET_MessageHeader *join_msg;
};


/**
 * Result of a GNS name lookup for entering a place.
 *
 * @see GNUNET_SOCIAL_guest_enter_by_name
 */
static void
gns_result_guest_enter (void *cls, uint32_t rd_count,
                        const struct GNUNET_GNSRECORD_Data *rd)
{
  struct GuestEnterByNameClosure *gcls = cls;
  struct Client *c = gcls->client;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p GNS result: %u records.\n",
              c, rd_count);

  const struct GNUNET_GNSRECORD_PlaceData *
    rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;

  if ((0 == rd_count) || (rd->data_size < sizeof (*rec)))
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (c->client);
    return;
  }

  uint16_t relay_count = ntohl (rec->relay_count);
  struct GNUNET_PeerIdentity *relays = NULL;

  if (0 < relay_count)
  {
    if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct
                                                               GNUNET_PeerIdentity))
    {
      relays = (struct GNUNET_PeerIdentity *) &rec[1];
    }
    else
    {
      relay_count = 0;
      GNUNET_break_op (0);
    }
  }

  uint16_t app_id_size = strlen (gcls->app_id) + 1;
  uint16_t relay_size = relay_count * sizeof (*relays);
  uint16_t join_msg_size = 0;
  if (NULL != gcls->join_msg)
    join_msg_size = ntohs (gcls->join_msg->size);
  uint16_t greq_size = sizeof (struct GuestEnterRequest)
                       + app_id_size + relay_size + join_msg_size;
  struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
  greq->header.size = htons (greq_size);
  greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
  greq->ego_pub_key = gcls->ego_pub_key;
  greq->place_pub_key = rec->place_pub_key;
  greq->origin = rec->origin;
  greq->relay_count = rec->relay_count;

  void *p = &greq[1];
  GNUNET_memcpy (p, gcls->app_id, app_id_size);
  p += app_id_size;
  GNUNET_memcpy (p, relays, relay_size);
  p += relay_size;
  GNUNET_memcpy (p, gcls->join_msg, join_msg_size);

  client_guest_enter (c, greq);

  GNUNET_free (gcls->app_id);
  if (NULL != gcls->password)
    GNUNET_free (gcls->password);
  if (NULL != gcls->join_msg)
    GNUNET_free (gcls->join_msg);
  GNUNET_free (gcls);
  GNUNET_free (greq);
}


static int
check_client_guest_enter_by_name (void *cls,
                                  const struct GuestEnterByNameRequest *greq)
{
  return GNUNET_OK;
}


/**
 * Handle a connecting client entering a place as guest using a GNS address.
 *
 * Look up GNS address and generate a GuestEnterRequest from that.
 */
static void
handle_client_guest_enter_by_name (void *cls,
                                   const struct GuestEnterByNameRequest *greq)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;

  struct GuestEnterByNameClosure *gcls = GNUNET_malloc (sizeof (*gcls));
  gcls->client = c;
  gcls->ego_pub_key = greq->ego_pub_key;

  const char *p = (const char *) &greq[1];
  const char *app_id = NULL, *password = NULL, *gns_name = NULL;
  uint16_t remaining = ntohs (greq->header.size) - sizeof (*greq);
  uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 3,
                                                    &app_id,
                                                    &gns_name,
                                                    &password);
  p += offset;
  remaining -= offset;

  if ((0 != offset) && (sizeof (*gcls->join_msg) <= remaining))
  {
    gcls->join_msg = GNUNET_copy_message ((struct GNUNET_MessageHeader *) p);
    remaining -= ntohs (gcls->join_msg->size);
  }

  if ((0 == offset) || (0 != remaining))
  {
    if (NULL != gcls->join_msg)
      GNUNET_free (gcls->join_msg);
    GNUNET_free (gcls);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  uint16_t app_id_size = strlen (app_id) + 1;
  gcls->app_id = GNUNET_malloc (app_id_size);
  GNUNET_memcpy (gcls->app_id, app_id, app_id_size);

  uint16_t password_size = strlen (password);
  if (0 < password_size++)
  {
    gcls->password = GNUNET_malloc (password_size);
    GNUNET_memcpy (gcls->password, password, password_size);
  }

  GNUNET_GNS_lookup (gns, gns_name,
                     &greq->ego_pub_key,
                     GNUNET_GNSRECORD_TYPE_PLACE,
                     GNUNET_GNS_LO_DEFAULT,
                     &gns_result_guest_enter, gcls);
  GNUNET_SERVICE_client_continue (client);
}


static int
check_client_app_connect (void *cls,
                          const struct AppConnectRequest *creq)
{
  return GNUNET_OK;
}


/**
 * Handle application connection.
 */
static void
handle_client_app_connect (void *cls,
                           const struct AppConnectRequest *creq)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  ssize_t app_id_size = ntohs (creq->header.size) - sizeof (*creq);
  const char *app_id = NULL;
  uint16_t offset;

  if (app_id_size < 0)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "AppConnectRequest has invalid size\n");
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &creq[1],
                                           (size_t) app_id_size,
                                           1,
                                           &app_id);
  if ((0 == offset) || (offset != app_id_size))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "AppConnectRequest contains invalid app ID\n");
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  struct GNUNET_HashCode app_id_hash;
  GNUNET_CRYPTO_hash (app_id, (size_t) app_id_size, &app_id_hash);

  GNUNET_CONTAINER_multihashmap_iterate (egos, ego_entry, client);
  app_notify_ego_end (client);

  struct GNUNET_CONTAINER_MultiHashMap *
    app_places = GNUNET_CONTAINER_multihashmap_get (apps_places, &app_id_hash);
  if (NULL != app_places)
    GNUNET_CONTAINER_multihashmap_iterate (app_places, app_place_entry_notify,
                                           client);
  app_notify_place_end (client);

  struct ClientListItem *cli = GNUNET_new (struct ClientListItem);
  cli->client = client;
  struct Application *app = GNUNET_CONTAINER_multihashmap_get (apps,
                                                               &app_id_hash);
  if (NULL == app)
  {
    app = GNUNET_malloc (sizeof (*app));
    (void) GNUNET_CONTAINER_multihashmap_put (apps, &app_id_hash, app,
                                              GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }
  GNUNET_CONTAINER_DLL_insert (app->clients_head, app->clients_tail, cli);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Application %s connected.\n", app, app_id);

  c->app_id = GNUNET_malloc ((size_t) app_id_size);
  GNUNET_memcpy (c->app_id, app_id, (size_t) app_id_size);

  GNUNET_SERVICE_client_continue (client);
}


/**
 * Handle application detach request.
 */
static void
handle_client_app_detach (void *cls,
                          const struct AppDetachRequest *req)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;

  int ret = app_place_remove (c->app_id, &req->ego_pub_key,
                              &req->place_pub_key);
  client_send_result (client, req->op_id, ret, NULL, 0);

  GNUNET_SERVICE_client_continue (client);
}


static void
place_leave_cb (void *cls)
{
  struct Place *plc = cls;

  place_send_leave_ack (plc);
  (GNUNET_YES == plc->is_host)
    ? cleanup_host ((struct Host *) plc)
    : cleanup_guest ((struct Guest *) plc);
}


/**
 * Handle application leave request.
 */
static void
handle_client_place_leave (void *cls,
                           const struct GNUNET_MessageHeader *msg)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "got leave request from %s for place %s",
              plc->is_host? "host" : "slave",
              GNUNET_h2s (&plc->pub_key_hash));
  if (NULL == plc)
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  if (GNUNET_YES != plc->is_disconnecting)
  {
    plc->is_disconnecting = GNUNET_YES;
    if (plc->is_host)
    {
      struct Host *host = plc->host;
      GNUNET_assert (NULL != host);
      GNUNET_PSYC_master_stop (host->master, GNUNET_NO, &place_leave_cb, plc);
    }
    else
    {
      struct Guest *guest = plc->guest;
      GNUNET_assert (NULL != guest);
      GNUNET_PSYC_slave_part (guest->slave, GNUNET_NO, &place_leave_cb, plc);
    }
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "got leave request but place is already leaving\n");
  }
  GNUNET_SERVICE_client_continue (client);
}


struct JoinDecisionClosure
{
  int32_t is_admitted;
  struct GNUNET_PSYC_Message *msg;
};


/**
 * Iterator callback for responding to join requests.
 */
static int
psyc_send_join_decision (void *cls, const struct GNUNET_HashCode *pub_key_hash,
                         void *value)
{
  struct JoinDecisionClosure *jcls = cls;
  struct GNUNET_PSYC_JoinHandle *jh = value;
  // FIXME: add relays
  GNUNET_PSYC_join_decision (jh, jcls->is_admitted, 0, NULL, jcls->msg);
  return GNUNET_YES;
}


static int
check_client_join_decision (void *cls,
                            const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
{
  return GNUNET_OK;
}


/**
 * Handle an entry decision from a host client.
 */
static void
handle_client_join_decision (void *cls,
                             const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  if ((NULL == plc) || (GNUNET_YES != plc->is_host))
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }
  struct Host *hst = plc->host;

  struct JoinDecisionClosure jcls;
  jcls.is_admitted = ntohl (dcsn->is_admitted);
  jcls.msg
    = (sizeof (*dcsn) + sizeof (*jcls.msg) <= ntohs (dcsn->header.size))
    ? (struct GNUNET_PSYC_Message *) &dcsn[1]
    : NULL;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "jcls.msg = %p\n",
              jcls.msg);
  struct GNUNET_HashCode slave_pub_hash;
  GNUNET_CRYPTO_hash (&dcsn->slave_pub_key, sizeof (dcsn->slave_pub_key),
                      &slave_pub_hash);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Got join decision (%d) from client for place %s..\n",
              hst, jcls.is_admitted, GNUNET_h2s (&plc->pub_key_hash));
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p ..and slave %s.\n",
              hst, GNUNET_h2s (&slave_pub_hash));

  GNUNET_CONTAINER_multihashmap_get_multiple (hst->join_reqs, &slave_pub_hash,
                                              &psyc_send_join_decision, &jcls);
  GNUNET_CONTAINER_multihashmap_remove_all (hst->join_reqs, &slave_pub_hash);

  GNUNET_SERVICE_client_continue (client);
}


/**
 * Send acknowledgement to a client.
 *
 * Sent after a message fragment has been passed on to multicast.
 *
 * @param plc The place struct for the client.
 */
static void
send_message_ack (struct Place *plc, struct GNUNET_SERVICE_Client *client)
{
  struct GNUNET_MQ_Envelope *env;

  env = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK);
  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
                  env);
}


/**
 * Proceed to the next message part in the transmission queue.
 *
 * @param plc
 *        Place where the transmission is going on.
 * @param tmit_msg
 *        Currently transmitted message.
 * @param tmit_frag
 *        Currently transmitted message fragment.
 *
 * @return @a tmit_frag, or NULL if reached the end of fragment.
 */
static struct FragmentTransmitQueue *
psyc_transmit_queue_next_part (struct Place *plc,
                               struct MessageTransmitQueue *tmit_msg,
                               struct FragmentTransmitQueue *tmit_frag)
{
  uint16_t psize = ntohs (tmit_frag->next_part->size);
  if ((char *) tmit_frag->next_part + psize - ((char *) &tmit_frag[1])
      < tmit_frag->size)
  {
    tmit_frag->next_part
      = (struct GNUNET_MessageHeader *) ((char *) tmit_frag->next_part + psize);
  }
  else /* Reached end of current fragment. */
  {
    if (NULL != tmit_frag->client)
      send_message_ack (plc, tmit_frag->client);
    GNUNET_CONTAINER_DLL_remove (tmit_msg->frags_head, tmit_msg->frags_tail,
                                 tmit_frag);
    GNUNET_free (tmit_frag);
    tmit_frag = NULL;
  }
  return tmit_frag;
}


/**
 * Proceed to next message in transmission queue.
 *
 * @param plc
 *        Place where the transmission is going on.
 * @param tmit_msg
 *        Currently transmitted message.
 *
 * @return The next message in queue, or NULL if queue is empty.
 */
static struct MessageTransmitQueue *
psyc_transmit_queue_next_msg (struct Place *plc,
                              struct MessageTransmitQueue *tmit_msg)
{
  GNUNET_CONTAINER_DLL_remove (plc->tmit_msgs_head, plc->tmit_msgs_tail,
                               tmit_msg);
  GNUNET_free (tmit_msg);
  return plc->tmit_msgs_head;
}


/**
 * Callback for data transmission to PSYC.
 */
static int
psyc_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
{
  struct Place *plc = cls;
  struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
  GNUNET_assert (NULL != tmit_msg);
  struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
  if (NULL == tmit_frag)
  { /* Rest of the message have not arrived yet, pause transmission */
    *data_size = 0;
    return GNUNET_NO;
  }
  struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
  if (NULL == pmsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "%p psyc_transmit_notify_data: nothing to send.\n", plc);
    *data_size = 0;
    return GNUNET_NO;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p psyc_transmit_notify_data()\n", plc);
  GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);

  uint16_t ptype = ntohs (pmsg->type);
  uint16_t pdata_size = ntohs (pmsg->size) - sizeof (*pmsg);
  int ret;

  switch (ptype)
  {
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
    if (*data_size < pdata_size)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "%p psyc_transmit_notify_data: buffer size too small for data.\n",
                  plc);
      *data_size = 0;
      return GNUNET_NO;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "%p psyc_transmit_notify_data: sending %u bytes.\n",
                plc, pdata_size);

    *data_size = pdata_size;
    GNUNET_memcpy (data, &pmsg[1], *data_size);
    ret = GNUNET_NO;
    break;

  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
    *data_size = 0;
    ret = GNUNET_YES;
    break;

  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
    *data_size = 0;
    ret = GNUNET_SYSERR;
    break;

  default:
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%p psyc_transmit_notify_data: unexpected message part of type %u.\n",
                plc, ptype);
    ret = GNUNET_SYSERR;
  }

  if ((GNUNET_SYSERR == ret) && (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL !=
                                 ptype) )
  {
    *data_size = 0;
    tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
    GNUNET_SERVICE_client_drop (tmit_frag->client);
    GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
    return ret;
  }
  else
  {
    tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
    if (NULL != tmit_frag)
    {
      struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
      ptype = ntohs (pmsg->type);
      switch (ptype)
      {
      case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
        ret = GNUNET_YES;
        break;
      case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
        ret = GNUNET_SYSERR;
        break;
      }
      switch (ptype)
      {
      case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
      case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
        tmit_frag = psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
      }
    }

    if ((NULL == tmit_msg->frags_head)
        && (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype))
    { /* Reached end of current message. */
      tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
    }
  }

  if (ret != GNUNET_NO)
  {
    if (NULL != tmit_msg)
    {
      psyc_transmit_message (plc);
    }
    /* FIXME: handle partial message (when still in_transmit) */
  }
  return ret;
}


/**
 * Callback for modifier transmission to PSYC.
 */
static int
psyc_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
                          uint8_t *oper, uint32_t *full_value_size)
{
  struct Place *plc = cls;
  struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
  GNUNET_assert (NULL != tmit_msg);
  struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
  if (NULL == tmit_frag)
  { /* Rest of the message have not arrived yet, pause transmission */
    *data_size = 0;
    return GNUNET_NO;
  }
  struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
  if (NULL == pmsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "%p psyc_transmit_notify_mod: nothing to send.\n", plc);
    *data_size = 0;
    return GNUNET_NO;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p psyc_transmit_notify_mod()\n", plc);
  GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);

  uint16_t ptype = ntohs (pmsg->type);
  int ret;

  switch (ptype)
  {
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
    {
      if (NULL == oper)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "%p psyc_transmit_notify_mod: oper is NULL.\n", plc);
        ret = GNUNET_SYSERR;
        break;
      }
      struct GNUNET_PSYC_MessageModifier *
        pmod = (struct GNUNET_PSYC_MessageModifier *) tmit_frag->next_part;
      uint16_t mod_size = ntohs (pmod->header.size) - sizeof (*pmod);

      if (*data_size < mod_size)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "%p psyc_transmit_notify_mod: buffer size too small for data.\n",
                    plc);
        *data_size = 0;
        return GNUNET_NO;
      }

      *full_value_size = ntohl (pmod->value_size);
      *oper = pmod->oper;
      *data_size = mod_size;
      GNUNET_memcpy (data, &pmod[1], mod_size);
      ret = GNUNET_NO;
      break;
    }

  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
    {
      if (NULL != oper)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "%p psyc_transmit_notify_mod: oper is not NULL.\n", plc);
        ret = GNUNET_SYSERR;
        break;
      }
      uint16_t mod_size = ntohs (pmsg->size) - sizeof (*pmsg);
      if (*data_size < mod_size)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "%p psyc_transmit_notify_mod: buffer size too small for data.\n",
                    plc);
        *data_size = 0;
        return GNUNET_NO;
      }
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "%p psyc_transmit_notify_mod: sending %u bytes.\n", plc,
                  mod_size);

      *data_size = mod_size;
      GNUNET_memcpy (data, &pmsg[1], *data_size);
      ret = GNUNET_NO;
      break;
    }

  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
  case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
    *data_size = 0;
    ret = GNUNET_YES;
    break;

  default:
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%p psyc_transmit_notify_mod: unexpected message part of type %u.\n",
                plc, ptype);
    ret = GNUNET_SYSERR;
  }

  if (GNUNET_SYSERR == ret)
  {
    *data_size = 0;
    ret = GNUNET_SYSERR;
    tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
    GNUNET_SERVICE_client_drop (tmit_frag->client);
    GNUNET_SCHEDULER_add_now (&cleanup_place, plc);
  }
  else
  {
    if (GNUNET_YES != ret)
      psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);

    if ((NULL == tmit_msg->frags_head)
        && (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype))
    { /* Reached end of current message. */
      tmit_msg = psyc_transmit_queue_next_msg (plc, tmit_msg);
    }
  }
  return ret;
}


/**
 * Callback for data transmission from a host to PSYC.
 */
static int
host_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
{
  int ret = psyc_transmit_notify_data (cls, data_size, data);

  if (GNUNET_NO != ret)
  {
    struct Host *hst = cls;
    hst->tmit_handle = NULL;
  }
  return ret;
}


/**
 * Callback for the transmit functions of multicast.
 */
static int
guest_transmit_notify_data (void *cls, uint16_t *data_size, void *data)
{
  int ret = psyc_transmit_notify_data (cls, data_size, data);

  if (GNUNET_NO != ret)
  {
    struct Guest *gst = cls;
    gst->tmit_handle = NULL;
  }
  return ret;
}


/**
 * Callback for modifier transmission from a host to PSYC.
 */
static int
host_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
                          uint8_t *oper, uint32_t *full_value_size)
{
  int ret = psyc_transmit_notify_mod (cls, data_size, data,
                                      oper, full_value_size);
  if (GNUNET_SYSERR == ret)
  {
    struct Host *hst = cls;
    hst->tmit_handle = NULL;
  }
  return ret;
}


/**
 * Callback for modifier transmission from a guest to PSYC.
 */
static int
guest_transmit_notify_mod (void *cls, uint16_t *data_size, void *data,
                           uint8_t *oper, uint32_t *full_value_size)
{
  int ret = psyc_transmit_notify_mod (cls, data_size, data,
                                      oper, full_value_size);
  if (GNUNET_SYSERR == ret)
  {
    struct Guest *gst = cls;
    gst->tmit_handle = NULL;
  }
  return ret;
}


/**
 * Get method part of next message from transmission queue.
 *
 * @param plc
 *        Place
 *
 * @return #GNUNET_OK on success
 *         #GNUNET_NO if there are no more messages in queue.
 *         #GNUNET_SYSERR if the next message is malformed.
 */
static struct GNUNET_PSYC_MessageMethod *
psyc_transmit_queue_next_method (struct Place *plc)
{
  struct MessageTransmitQueue *tmit_msg = plc->tmit_msgs_head;
  if (NULL == tmit_msg)
    return GNUNET_NO;

  struct FragmentTransmitQueue *tmit_frag = tmit_msg->frags_head;
  if (NULL == tmit_frag)
  {
    GNUNET_break (0);
    return GNUNET_NO;
  }

  struct GNUNET_MessageHeader *pmsg = tmit_frag->next_part;
  if ((NULL == pmsg)
      || (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD != ntohs (pmsg->type)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%p psyc_transmit_queue_next_method: unexpected message part of type %u.\n",
                plc, NULL != pmsg ? ntohs (pmsg->type) : 0);
    GNUNET_break (0);
    return NULL;
  }

  uint16_t psize = ntohs (pmsg->size);
  struct GNUNET_PSYC_MessageMethod *
    pmeth = (struct GNUNET_PSYC_MessageMethod *) GNUNET_copy_message (pmsg);

  if ((psize < sizeof (*pmeth) + 1) || ('\0' != *((char *) pmeth + psize - 1)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%p psyc_transmit_queue_next_method: invalid method name.\n",
                plc);
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%zu <= %u || NUL != %u\n",
                sizeof (*pmeth), psize, *((char *) pmeth + psize - 1));
    GNUNET_break (0);
    GNUNET_free (pmeth);
    return NULL;
  }

  psyc_transmit_queue_next_part (plc, tmit_msg, tmit_frag);
  return pmeth;
}


/**
 * Transmit the next message in queue from the host to the PSYC channel.
 */
static int
psyc_master_transmit_message (struct Host *hst)
{
  struct Place *plc = &hst->place;

  if (NULL == hst->tmit_handle)
  {
    struct GNUNET_PSYC_MessageMethod *
      pmeth = psyc_transmit_queue_next_method (plc);
    if (NULL == pmeth)
      return GNUNET_SYSERR;

    hst->tmit_handle = (void *) &hst->tmit_handle;
    struct GNUNET_PSYC_MasterTransmitHandle *
      tmit_handle = GNUNET_PSYC_master_transmit (hst->master, (const
                                                               char *) &pmeth[1],
                                                 &host_transmit_notify_mod,
                                                 &host_transmit_notify_data,
                                                 hst,
                                                 pmeth->flags);
    if (NULL != hst->tmit_handle)
      hst->tmit_handle = tmit_handle;
    GNUNET_free (pmeth);
  }
  else
  {
    GNUNET_PSYC_master_transmit_resume (hst->tmit_handle);
  }
  return GNUNET_OK;
}


/**
 * Transmit the next message in queue from a guest to the PSYC channel.
 */
static int
psyc_slave_transmit_message (struct Guest *gst)
{
  struct Place *plc = &gst->place;

  if (NULL == gst->tmit_handle)
  {
    struct GNUNET_PSYC_MessageMethod *
      pmeth = psyc_transmit_queue_next_method (plc);
    if (NULL == pmeth)
      return GNUNET_SYSERR;

    gst->tmit_handle = (void *) &gst->tmit_handle;
    struct GNUNET_PSYC_SlaveTransmitHandle *
      tmit_handle = GNUNET_PSYC_slave_transmit (gst->slave, (const
                                                             char *) &pmeth[1],
                                                &guest_transmit_notify_mod,
                                                &guest_transmit_notify_data,
                                                gst,
                                                pmeth->flags);
    if (NULL != gst->tmit_handle)
      gst->tmit_handle = tmit_handle;
    GNUNET_free (pmeth);
  }
  else
  {
    GNUNET_PSYC_slave_transmit_resume (gst->tmit_handle);
  }
  return GNUNET_OK;
}


/**
 * Transmit a message to PSYC.
 */
static int
psyc_transmit_message (struct Place *plc)
{
  return
    (plc->is_host)
    ? psyc_master_transmit_message ((struct Host *) plc)
    : psyc_slave_transmit_message ((struct Guest *) plc);
}


/**
 * Queue message parts for sending to PSYC.
 *
 * @param plc          Place to send to.
 * @param client       Client the message originates from.
 * @param data_size    Size of @a data.
 * @param data         Concatenated message parts.
 * @param first_ptype  First message part type in @a data.
 * @param last_ptype   Last message part type in @a data.
 */
static struct MessageTransmitQueue *
psyc_transmit_queue_message (struct Place *plc,
                             struct GNUNET_SERVICE_Client *client,
                             size_t data_size,
                             const void *data,
                             uint16_t first_ptype, uint16_t last_ptype,
                             struct MessageTransmitQueue *tmit_msg)
{
  if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == first_ptype)
  {
    tmit_msg = GNUNET_malloc (sizeof (*tmit_msg));
    GNUNET_CONTAINER_DLL_insert_tail (plc->tmit_msgs_head, plc->tmit_msgs_tail,
                                      tmit_msg);
  }
  else if (NULL == tmit_msg)
  {
    return NULL;
  }

  struct FragmentTransmitQueue *
    tmit_frag = GNUNET_malloc (sizeof (*tmit_frag) + data_size);
  GNUNET_memcpy (&tmit_frag[1], data, data_size);
  tmit_frag->next_part = (struct GNUNET_MessageHeader *) &tmit_frag[1];
  tmit_frag->client = client;
  tmit_frag->size = data_size;

  GNUNET_CONTAINER_DLL_insert_tail (tmit_msg->frags_head, tmit_msg->frags_tail,
                                    tmit_frag);
  tmit_msg->client = client;
  return tmit_msg;
}


///**
// * Cancel transmission of current message to PSYC.
// *
// * @param plc	  Place to send to.
// * @param client  Client the message originates from.
// */
// static void
// psyc_transmit_cancel (struct Place *plc, struct GNUNET_SERVICE_Client *client)
// {
//  uint16_t type = GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL;
//
//  struct GNUNET_MessageHeader msg;
//  msg.size = htons (sizeof (msg));
//  msg.type = htons (type);
//
//  psyc_transmit_queue_message (plc, client, sizeof (msg), &msg, type, type, NULL);
//  psyc_transmit_message (plc);
//
//  /* FIXME: cleanup */
// }


static int
check_client_psyc_message (void *cls,
                           const struct GNUNET_MessageHeader *msg)
{
  return GNUNET_OK;
}


/**
 * Handle an incoming message from a client, to be transmitted to the place.
 */
static void
handle_client_psyc_message (void *cls,
                            const struct GNUNET_MessageHeader *msg)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  int ret;

  if (NULL == plc)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "received PSYC message for non-existing client %p\n",
                client);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Received message of type %d from client.\n", plc, ntohs (
                msg->type));
  GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, msg);

  if (GNUNET_YES != plc->is_ready)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "%p Place is not ready yet, disconnecting client.\n", plc);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  uint16_t size = ntohs (msg->size);
  uint16_t psize = size - sizeof (*msg);
  if ((psize < sizeof (struct GNUNET_MessageHeader))
      || (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < psize))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%p Received message with invalid payload size (%u) from client.\n",
                plc, psize);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  uint16_t first_ptype = 0;
  uint16_t last_ptype = 0;
  if (GNUNET_SYSERR ==
      GNUNET_PSYC_receive_check_parts (psize, (const char *) &msg[1],
                                       &first_ptype, &last_ptype))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%p Received invalid message part from client.\n", plc);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Received message with first part type %u and last part type %u.\n",
              plc, first_ptype, last_ptype);

  c->tmit_msg
    = psyc_transmit_queue_message (plc, client, psize, &msg[1],
                                   first_ptype, last_ptype, c->tmit_msg);
  if (NULL != c->tmit_msg)
  {
    if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= last_ptype)
      c->tmit_msg = NULL;
    ret = psyc_transmit_message (plc);
  }
  else
  {
    ret = GNUNET_SYSERR;
  }
  if (GNUNET_OK != ret)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%p Received invalid message part from client.\n", plc);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }
  GNUNET_SERVICE_client_continue (client);
}


/**
 * A historic message arrived from PSYC.
 */
static void
psyc_recv_history_message (void *cls, const struct
                           GNUNET_PSYC_MessageHeader *msg)
{
  struct OperationClosure *opcls = cls;
  struct Client *c = opcls->client;
  struct Place *plc = c->place;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Received historic message #%" PRId64 " (flags: %x)\n",
              plc, GNUNET_ntohll (msg->message_id), ntohl (msg->flags));

  uint16_t size = ntohs (msg->header.size);

  struct GNUNET_OperationResultMessage *
    res = GNUNET_malloc (sizeof (*res) + size);
  res->header.size = htons (sizeof (*res) + size);
  res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT);
  res->op_id = opcls->op_id;
  res->result_code = GNUNET_htonll (GNUNET_OK);

  GNUNET_memcpy (&res[1], msg, size);

  /** @todo FIXME: send only to requesting client */
  place_send_msg (plc, GNUNET_MQ_msg_copy (&res->header));

  GNUNET_free (res);
}


/**
 * Result of message history replay from PSYC.
 */
static void
psyc_recv_history_result (void *cls, int64_t result,
                          const void *err_msg, uint16_t err_msg_size)
{
  struct OperationClosure *opcls = cls;
  struct Client *c = opcls->client;
  struct Place *plc = c->place;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p History replay #%" PRIu64 ": "
              "PSYCstore returned %" PRId64 " (%.*s)\n",
              plc, GNUNET_ntohll (opcls->op_id), result,
              err_msg_size, (const char *) err_msg);

  // FIXME: place might have been destroyed
  client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
}


static int
check_client_history_replay (void *cls,
                             const struct
                             GNUNET_PSYC_HistoryRequestMessage *req)
{
  return GNUNET_OK;
}


/**
 * Client requests channel history.
 */
static void
handle_client_history_replay (void *cls,
                              const struct
                              GNUNET_PSYC_HistoryRequestMessage *req)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  if (NULL == plc)
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  uint16_t size = ntohs (req->header.size);
  const char *method_prefix = (const char *) &req[1];

  if ((size < sizeof (*req) + 1)
      || ('\0' != method_prefix[size - sizeof (*req) - 1]))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%p History replay #%" PRIu64 ": "
                "invalid method prefix. size: %u < %zu?\n",
                plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
  opcls->client = c;
  opcls->op_id = req->op_id;
  opcls->flags = ntohl (req->flags);

  if (0 == req->message_limit)
    GNUNET_PSYC_channel_history_replay (plc->channel,
                                        GNUNET_ntohll (req->start_message_id),
                                        GNUNET_ntohll (req->end_message_id),
                                        method_prefix, opcls->flags,
                                        psyc_recv_history_message, NULL,
                                        psyc_recv_history_result, opcls);
  else
    GNUNET_PSYC_channel_history_replay_latest (plc->channel,
                                               GNUNET_ntohll (
                                                 req->message_limit),
                                               method_prefix, opcls->flags,
                                               psyc_recv_history_message, NULL,
                                               psyc_recv_history_result, opcls);

  GNUNET_SERVICE_client_continue (client);
}


/**
 * A state variable part arrived from PSYC.
 */
void
psyc_recv_state_var (void *cls,
                     const struct GNUNET_MessageHeader *mod,
                     const char *name,
                     const void *value,
                     uint32_t value_size,
                     uint32_t full_value_size)
{
  struct GNUNET_OperationResultMessage *result_msg;
  struct GNUNET_MQ_Envelope *env;
  struct OperationClosure *opcls = cls;
  struct Client *c = opcls->client;
  struct Place *plc = c->place;
  uint16_t size = ntohs (mod->size);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p Received state variable %s from PSYC\n",
              plc, name);
  env = GNUNET_MQ_msg_extra (result_msg,
                             size,
                             GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT);
  result_msg->op_id = opcls->op_id;
  result_msg->result_code = GNUNET_htonll (GNUNET_OK);
  GNUNET_memcpy (&result_msg[1], mod, size);
  /** @todo FIXME: send only to requesting client */
  place_send_msg (plc, env);
}


/**
 * Result of retrieving state variable from PSYC.
 */
static void
psyc_recv_state_result (void *cls, int64_t result,
                        const void *err_msg, uint16_t err_msg_size)
{
  struct OperationClosure *opcls = cls;
  struct Client *c = opcls->client;
  struct Place *plc = c->place;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p State get #%" PRIu64 ": "
              "PSYCstore returned %" PRId64 " (%.*s)\n",
              plc, GNUNET_ntohll (opcls->op_id), result,
              err_msg_size, (const char *) err_msg);

  // FIXME: place might have been destroyed
  client_send_result (c->client, opcls->op_id, result, err_msg, err_msg_size);
}


static int
check_client_state_get (void *cls,
                        const struct GNUNET_PSYC_StateRequestMessage *req)
{
  return GNUNET_OK;
}


/**
 * Client requests channel history.
 */
static void
handle_client_state_get (void *cls,
                         const struct GNUNET_PSYC_StateRequestMessage *req)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;
  struct Place *plc = c->place;
  if (NULL == plc)
  {
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  uint16_t size = ntohs (req->header.size);
  const char *name = (const char *) &req[1];

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "%p State get #%" PRIu64 ": %s\n",
              plc, GNUNET_ntohll (req->op_id), name);

  if ((size < sizeof (*req) + 1)
      || ('\0' != name[size - sizeof (*req) - 1]))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%p State get #%" PRIu64 ": "
                "invalid name. size: %u < %zu?\n",
                plc, GNUNET_ntohll (req->op_id), size, sizeof (*req) + 1);
    GNUNET_break (0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
  opcls->client = c;
  opcls->op_id = req->op_id;

  switch (ntohs (req->header.type))
  {
  case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET:
    GNUNET_PSYC_channel_state_get (plc->channel, name,
                                   psyc_recv_state_var,
                                   psyc_recv_state_result, opcls);
    break;

  case GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX:
    GNUNET_PSYC_channel_state_get_prefix (plc->channel, name,
                                          psyc_recv_state_var,
                                          psyc_recv_state_result, opcls);
    break;

  default:
    GNUNET_assert (0);
  }

  GNUNET_SERVICE_client_continue (client);
}


#define check_client_state_get_prefix check_client_state_get
#define handle_client_state_get_prefix handle_client_state_get


static void
namestore_recv_records_store_result (void *cls, int32_t result,
                                     const char *err_msg)
{
  struct OperationClosure *opcls = cls;
  struct Client *c = opcls->client;

  // FIXME: client might have been disconnected
  client_send_result (c->client, opcls->op_id, result, err_msg,
                      (NULL != err_msg) ? strlen (err_msg) : 0);
  GNUNET_free (opcls);
}


static int
check_client_zone_add_place (void *cls,
                             const struct ZoneAddPlaceRequest *preq)
{
  return GNUNET_OK;
}


/**
 * Handle request to add PLACE record to GNS zone.
 */
static void
handle_client_zone_add_place (void *cls,
                              const struct ZoneAddPlaceRequest *preq)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;

  uint16_t remaining = ntohs (preq->header.size) - sizeof (*preq);
  const char *p = (const char *) &preq[1];
  const char *name = NULL, *password = NULL;
  uint16_t offset = GNUNET_STRINGS_buffer_tokenize (p, remaining, 2,
                                                    &name, &password);
  remaining -= offset;
  p += offset;
  const struct GNUNET_PeerIdentity *
    relays = (const struct GNUNET_PeerIdentity *) p;
  uint16_t relay_size = ntohl (preq->relay_count) * sizeof (*relays);

  if ((0 == offset) || (remaining != relay_size))
  {
    GNUNET_break (0);
    client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
    GNUNET_SERVICE_client_drop (client);
    return;
  }

  struct GNUNET_GNSRECORD_Data rd = { };
  rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
  rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  rd.expiration_time = GNUNET_ntohll (preq->expiration_time);

  struct GNUNET_GNSRECORD_PlaceData *
    rec = GNUNET_malloc (sizeof (*rec) + relay_size);
  rec->place_pub_key = preq->place_pub_key;
  rec->origin = this_peer;
  rec->relay_count = preq->relay_count;
  GNUNET_memcpy (&rec[1], relays, relay_size);

  rd.data = rec;
  rd.data_size = sizeof (*rec) + relay_size;

  struct GNUNET_HashCode ego_pub_hash;
  GNUNET_CRYPTO_hash (&preq->ego_pub_key, sizeof (preq->ego_pub_key),
                      &ego_pub_hash);
  struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
  if (NULL == ego)
  {
    client_send_result (client, preq->op_id, GNUNET_SYSERR, NULL, 0);
  }
  else
  {
    struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
    opcls->client = c;
    opcls->op_id = preq->op_id;
    GNUNET_NAMESTORE_records_store (namestore, &ego->key,
                                    name, 1, &rd,
                                    namestore_recv_records_store_result, opcls);
    /** @todo refresh stored records later */
  }
  GNUNET_SERVICE_client_continue (client);
}


static int
check_client_zone_add_nym (void *cls,
                           const struct ZoneAddNymRequest *nreq)
{
  return GNUNET_OK;
}


/**
 * Handle request to add PLACE record to GNS zone.
 */
static void
handle_client_zone_add_nym (void *cls,
                            const struct ZoneAddNymRequest *nreq)
{
  struct Client *c = cls;
  struct GNUNET_SERVICE_Client *client = c->client;

  uint16_t name_size = ntohs (nreq->header.size) - sizeof (*nreq);
  const char *name = NULL;
  uint16_t offset = GNUNET_STRINGS_buffer_tokenize ((const char *) &nreq[1],
                                                    name_size, 1, &name);
  if ((0 == offset) || (offset != name_size))
  {
    GNUNET_break (0);
    client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
    GNUNET_SERVICE_client_continue (client);
    return;
  }

  struct GNUNET_GNSRECORD_Data rd = { };
  rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  rd.expiration_time = GNUNET_ntohll (nreq->expiration_time);
  rd.data = &nreq->nym_pub_key;
  rd.data_size = sizeof (nreq->nym_pub_key);

  struct GNUNET_HashCode ego_pub_hash;
  GNUNET_CRYPTO_hash (&nreq->ego_pub_key, sizeof (nreq->ego_pub_key),
                      &ego_pub_hash);
  struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
  if (NULL == ego)
  {
    client_send_result (client, nreq->op_id, GNUNET_SYSERR, NULL, 0);
  }
  else
  {
    struct OperationClosure *opcls = GNUNET_malloc (sizeof (*opcls));
    opcls->client = c;
    opcls->op_id = nreq->op_id;
    GNUNET_NAMESTORE_records_store (namestore, &ego->key,
                                    name, 1, &rd,
                                    namestore_recv_records_store_result, opcls);
    /** @todo refresh stored records later */
  }
  GNUNET_SERVICE_client_continue (client);
}


const char *
path_basename (const char *path)
{
  const char *basename = strrchr (path, DIR_SEPARATOR);
  if (NULL != basename)
    basename++;

  if ((NULL == basename) || ('\0' == *basename))
    return NULL;

  return basename;
}


struct PlaceLoadClosure
{
  const char *app_id;
  const char *ego_pub_str;
};


/** Load a place file */
int
file_place_load (void *cls, const char *place_filename)
{
  struct PlaceLoadClosure *plcls = cls;

  const char *place_pub_str = path_basename (place_filename);
  if (NULL == place_pub_str)
  {
    GNUNET_break (0);
    return GNUNET_OK;
  }

  char *filename = NULL;
  GNUNET_asprintf (&filename, "%s%c" "%s%c" "%s%c" "%s",
                   dir_social, DIR_SEPARATOR,
                   "places", DIR_SEPARATOR,
                   plcls->ego_pub_str, DIR_SEPARATOR,
                   place_pub_str);

  uint64_t file_size = 0;
  if ((GNUNET_OK !=
       GNUNET_DISK_file_size (filename, &file_size, GNUNET_YES, GNUNET_YES))
      || (file_size < sizeof (struct PlaceEnterRequest)))
  {
    GNUNET_free (filename);
    return GNUNET_OK;
  }

  struct PlaceEnterRequest *ereq = GNUNET_malloc (file_size);
  ssize_t read_size = GNUNET_DISK_fn_read (filename, ereq, file_size);
  GNUNET_free (filename);
  if ((read_size < 0) || (read_size < sizeof (*ereq)))
  {
    GNUNET_free (ereq);
    return GNUNET_OK;
  }

  uint16_t ereq_size = ntohs (ereq->header.size);
  if (read_size != ereq_size)
  {
    GNUNET_free (ereq);
    return GNUNET_OK;
  }

  switch (ntohs (ereq->header.type))
  {
  case GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER:
    if (ereq_size < sizeof (struct HostEnterRequest))
    {
      GNUNET_free (ereq);
      return GNUNET_OK;
    }
    struct HostEnterRequest *hreq = (struct HostEnterRequest *) ereq;
    host_enter (hreq, NULL);
    break;

  case GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER:
    if (ereq_size < sizeof (struct GuestEnterRequest))
    {
      GNUNET_free (ereq);
      return GNUNET_OK;
    }
    struct GuestEnterRequest *greq = (struct GuestEnterRequest *) ereq;
    guest_enter (greq, NULL);
    break;

  default:
    GNUNET_free (ereq);
    return GNUNET_OK;
  }

  if (GNUNET_SYSERR == app_place_add (plcls->app_id, ereq))
  {
    GNUNET_assert (0);
  }
  GNUNET_free (ereq);
  return GNUNET_OK;
}


/**
 * Read @e place_pub_str entries in @a dir_ego
 *
 * @param dir_ego
 *        Data directory of an application ego.
 *        $GNUNET_DATA_HOME/social/apps/$app_id/$ego_pub_str/
 */
int
scan_app_ego_dir (void *cls, const char *dir_ego)
{
  struct PlaceLoadClosure *plcls = cls;
  plcls->ego_pub_str = path_basename (dir_ego);

  if (NULL != plcls->ego_pub_str)
    GNUNET_DISK_directory_scan (dir_ego, file_place_load, plcls);

  return GNUNET_OK;
}


/**
 * Read @e ego_pub_str entries in @a dir_app
 *
 * @param dir_app
 *        Data directory of an application.
 *        $GNUNET_DATA_HOME/social/apps/$app_id/
 */
int
scan_app_dir (void *cls, const char *dir_app)
{
  if (GNUNET_YES != GNUNET_DISK_directory_test (dir_app, GNUNET_YES))
    return GNUNET_OK;

  struct PlaceLoadClosure plcls;
  plcls.app_id = path_basename (dir_app);

  if (NULL != plcls.app_id)
    GNUNET_DISK_directory_scan (dir_app, scan_app_ego_dir, &plcls);

  return GNUNET_OK;
}


static void
identity_recv_ego (void *cls, struct GNUNET_IDENTITY_Ego *id_ego,
                   void **ctx, const char *name)
{
  if (NULL == id_ego) // end of initial list of egos
    return;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "social service received ego %s\n",
              name);

  struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
  GNUNET_IDENTITY_ego_get_public_key (id_ego, &ego_pub_key);

  struct GNUNET_HashCode ego_pub_hash;
  GNUNET_CRYPTO_hash (&ego_pub_key, sizeof (ego_pub_key), &ego_pub_hash);

  struct Ego *ego = GNUNET_CONTAINER_multihashmap_get (egos, &ego_pub_hash);
  if ((NULL == ego) && (NULL == name))
  {
    // an ego that is none of our business has been deleted
    return;
  }
  if (NULL != ego)
  {
    // one of our egos has been changed
    GNUNET_free (ego->name);
    if (NULL == name)
    {
      // one of our egos has been deleted
      GNUNET_CONTAINER_multihashmap_remove (egos, &ego_pub_hash, ego);
      GNUNET_free (ego);
      return;
    }
  }
  else
  {
    ego = GNUNET_malloc (sizeof (*ego));
  }
  ego->key = *(GNUNET_IDENTITY_ego_get_private_key (id_ego));
  size_t name_size = strlen (name) + 1;
  ego->name = GNUNET_malloc (name_size);
  GNUNET_memcpy (ego->name, name, name_size);

  GNUNET_CONTAINER_multihashmap_put (egos, &ego_pub_hash, ego,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);

  // FIXME: notify clients about changed ego
}


/**
 * Initialize the PSYC service.
 *
 * @param cls Closure.
 * @param server The initialized server.
 * @param c Configuration to use.
 */
static void
run (void *cls,
     const struct GNUNET_CONFIGURATION_Handle *c,
     struct GNUNET_SERVICE_Handle *svc)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "starting social service\n");

  cfg = c;
  service = svc;
  GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);

  hosts = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
  guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
  place_guests = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);

  egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
  apps = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
  places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
  apps_places = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
  // places_apps = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);

  id = GNUNET_IDENTITY_connect (cfg, &identity_recv_ego, NULL);
  gns = GNUNET_GNS_connect (cfg);
  namestore = GNUNET_NAMESTORE_connect (cfg);
  stats = GNUNET_STATISTICS_create ("social", cfg);

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (cfg, "social", "DATA_HOME",
                                               &dir_social))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "social", "DATA_HOME");
    GNUNET_break (0);
    return;
  }
  GNUNET_asprintf (&dir_places, "%s%c%s",
                   dir_social, DIR_SEPARATOR, "places");
  GNUNET_asprintf (&dir_apps, "%s%c%s",
                   dir_social, DIR_SEPARATOR, "apps");

  GNUNET_DISK_directory_scan (dir_apps, scan_app_dir, NULL);

  GNUNET_SCHEDULER_add_shutdown (shutdown_task, NULL);
}


/**
 * Define "main" method using service macro.
 */
GNUNET_SERVICE_MAIN
  ("social",
  GNUNET_SERVICE_OPTION_NONE,
  run,
  client_notify_connect,
  client_notify_disconnect,
  NULL,
  GNUNET_MQ_hd_var_size (client_host_enter,
                         GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
                         struct HostEnterRequest,
                         NULL),
  GNUNET_MQ_hd_var_size (client_guest_enter,
                         GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
                         struct GuestEnterRequest,
                         NULL),
  GNUNET_MQ_hd_var_size (client_guest_enter_by_name,
                         GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME,
                         struct GuestEnterByNameRequest,
                         NULL),
  GNUNET_MQ_hd_var_size (client_join_decision,
                         GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
                         struct GNUNET_PSYC_JoinDecisionMessage,
                         NULL),
  GNUNET_MQ_hd_var_size (client_psyc_message,
                         GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
                         struct GNUNET_MessageHeader,
                         NULL),
  GNUNET_MQ_hd_var_size (client_history_replay,
                         GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY,
                         struct GNUNET_PSYC_HistoryRequestMessage,
                         NULL),
  GNUNET_MQ_hd_var_size (client_state_get,
                         GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
                         struct GNUNET_PSYC_StateRequestMessage,
                         NULL),
  GNUNET_MQ_hd_var_size (client_state_get_prefix,
                         GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
                         struct GNUNET_PSYC_StateRequestMessage,
                         NULL),
  GNUNET_MQ_hd_var_size (client_zone_add_place,
                         GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE,
                         struct ZoneAddPlaceRequest,
                         NULL),
  GNUNET_MQ_hd_var_size (client_zone_add_nym,
                         GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM,
                         struct ZoneAddNymRequest,
                         NULL),
  GNUNET_MQ_hd_var_size (client_app_connect,
                         GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT,
                         struct AppConnectRequest,
                         NULL),
  GNUNET_MQ_hd_fixed_size (client_app_detach,
                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH,
                           struct AppDetachRequest,
                           NULL),
  GNUNET_MQ_hd_fixed_size (client_place_leave,
                           GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE,
                           struct GNUNET_MessageHeader,
                           NULL),
  GNUNET_MQ_hd_var_size (client_msg_proc_set,
                         GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET,
                         struct MsgProcRequest,
                         NULL),
  GNUNET_MQ_hd_fixed_size (client_msg_proc_clear,
                           GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR,
                           struct GNUNET_MessageHeader,
                           NULL));

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