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 3811 3812 3813 3814
|
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2026 The TokTok team.
* Copyright © 2014 Tox project.
*/
/**
* Slightly better groupchats implementation.
*/
#include "group.h"
#include <assert.h>
#include <string.h>
#include "DHT.h"
#include "Messenger.h"
#include "attributes.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "friend_connection.h"
#include "group_common.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "network.h"
#include "sort.h"
#include "state.h"
#include "util.h"
enum {
/** Connection is to one of the closest DESIRED_CLOSEST peers */
GROUPCHAT_CONNECTION_REASON_CLOSEST = 1 << 0,
/** Connection is to a peer we are introducing to the conference */
GROUPCHAT_CONNECTION_REASON_INTRODUCING = 1 << 1,
/** Connection is to a peer who is introducing us to the conference */
GROUPCHAT_CONNECTION_REASON_INTRODUCER = 1 << 2,
};
typedef enum Groupchat_Connection_Type {
GROUPCHAT_CONNECTION_NONE,
GROUPCHAT_CONNECTION_CONNECTING,
GROUPCHAT_CONNECTION_ONLINE,
} Groupchat_Connection_Type;
typedef enum Groupchat_Status {
GROUPCHAT_STATUS_NONE,
GROUPCHAT_STATUS_VALID,
GROUPCHAT_STATUS_CONNECTED,
} Groupchat_Status;
#define GROUP_ID_LENGTH CRYPTO_SYMMETRIC_KEY_SIZE
#define DESIRED_CLOSEST 4
#define MAX_GROUP_CONNECTIONS 16
#define MAX_LAST_MESSAGE_INFOS 8
#define MAX_LOSSY_COUNT 256
/** Maximum number of frozen peers to store; `group_set_max_frozen()` overrides. */
#define MAX_FROZEN_DEFAULT 128
typedef struct Message_Info {
uint32_t message_number;
uint8_t message_id;
} Message_Info;
typedef struct Group_Peer {
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
bool temp_pk_updated;
bool is_friend;
uint64_t last_active;
Message_Info
last_message_infos[MAX_LAST_MESSAGE_INFOS]; /* received messages, strictly decreasing in message_number */
uint8_t num_last_message_infos;
uint8_t nick[MAX_NAME_LENGTH];
uint8_t nick_len;
bool nick_updated;
uint16_t peer_number;
uint8_t recv_lossy[MAX_LOSSY_COUNT];
uint16_t bottom_lossy_number;
uint16_t top_lossy_number;
void *_Nullable object;
} Group_Peer;
typedef struct Groupchat_Connection {
uint8_t type; /* `GROUPCHAT_CONNECTION_*` */
uint8_t reasons; /* bit field with flags `GROUPCHAT_CONNECTION_REASON_*` */
uint32_t number;
uint16_t group_number;
} Groupchat_Connection;
typedef struct Groupchat_Closest {
/**
* Whether this peer is active in the closest_peers array.
*/
bool active;
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
} Groupchat_Closest;
typedef struct Group_c {
uint8_t status;
bool need_send_name;
bool title_fresh;
Group_Peer *_Nullable group;
uint32_t numpeers;
Group_Peer *_Nullable frozen;
uint32_t numfrozen;
uint32_t maxfrozen;
Groupchat_Connection connections[MAX_GROUP_CONNECTIONS];
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
Groupchat_Closest closest_peers[DESIRED_CLOSEST];
uint8_t changed;
uint8_t type;
uint8_t id[GROUP_ID_LENGTH];
uint8_t title[MAX_NAME_LENGTH];
uint8_t title_len;
uint32_t message_number;
uint16_t lossy_message_number;
uint16_t peer_number;
uint64_t last_sent_ping;
uint32_t num_introducer_connections;
void *_Nullable object;
peer_on_join_cb *_Nullable peer_on_join;
peer_on_leave_cb *_Nullable peer_on_leave;
group_on_delete_cb *_Nullable group_on_delete;
} Group_c;
struct Group_Chats {
const Memory *_Nonnull mem;
const Mono_Time *_Nonnull mono_time;
Messenger *_Nonnull m;
Friend_Connections *_Nonnull fr_c;
Group_c *_Nullable chats;
uint16_t num_chats;
g_conference_invite_cb *_Nullable invite_callback;
g_conference_connected_cb *_Nullable connected_callback;
g_conference_message_cb *_Nullable message_callback;
peer_name_cb *_Nullable peer_name_callback;
peer_list_changed_cb *_Nullable peer_list_changed_callback;
title_cb *_Nullable title_callback;
lossy_packet_cb *_Nullable lossy_packethandlers[256];
};
static const Group_c empty_group_c = {0};
static const Group_Peer empty_group_peer = {{0}};
/**
* Packet type IDs as per the protocol specification.
*/
typedef enum Group_Message_Id {
GROUP_MESSAGE_PING_ID = 0,
GROUP_MESSAGE_NEW_PEER_ID = 16,
GROUP_MESSAGE_KILL_PEER_ID = 17,
GROUP_MESSAGE_FREEZE_PEER_ID = 18,
GROUP_MESSAGE_NAME_ID = 48,
GROUP_MESSAGE_TITLE_ID = 49,
} Group_Message_Id;
#define GROUP_MESSAGE_NEW_PEER_LENGTH (sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2)
#define GROUP_MESSAGE_KILL_PEER_LENGTH (sizeof(uint16_t))
#define MAX_GROUP_MESSAGE_DATA_LEN (MAX_CRYPTO_DATA_SIZE - (1 + MIN_MESSAGE_PACKET_LEN))
typedef enum Invite_Id {
INVITE_ID = 0,
INVITE_ACCEPT_ID = 1,
INVITE_MEMBER_ID = 2,
} Invite_Id;
#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + 1 + GROUP_ID_LENGTH)
#define INVITE_ACCEPT_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH)
#define INVITE_MEMBER_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH + sizeof(uint16_t))
#define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + 1 + GROUP_ID_LENGTH)
typedef enum Peer_Id {
PEER_INTRODUCED_ID = 1,
PEER_QUERY_ID = 8,
PEER_RESPONSE_ID = 9,
PEER_TITLE_ID = 10,
} Peer_Id;
#define MIN_MESSAGE_PACKET_LEN (sizeof(uint16_t) * 2 + sizeof(uint32_t) + 1)
static_assert(GROUP_ID_LENGTH == CRYPTO_PUBLIC_KEY_SIZE,
"GROUP_ID_LENGTH should be equal to CRYPTO_PUBLIC_KEY_SIZE");
const Mono_Time *g_mono_time(const Group_Chats *g_c)
{
return g_c->mono_time;
}
static bool group_id_eq(const uint8_t *_Nonnull a, const uint8_t *_Nonnull b)
{
return pk_equal(a, b);
}
static bool g_title_eq(const Group_c *_Nonnull g, const uint8_t *_Nonnull title, uint8_t title_len)
{
return memeq(g->title, g->title_len, title, title_len);
}
static bool g_peer_nick_eq(const Group_Peer *_Nonnull peer, const uint8_t *_Nonnull nick, uint8_t nick_len)
{
return memeq(peer->nick, peer->nick_len, nick, nick_len);
}
/**
* @retval false if the groupnumber is not valid.
* @retval true if the groupnumber is valid.
*/
static bool is_groupnumber_valid(const Group_Chats *_Nonnull g_c, uint32_t groupnumber)
{
return groupnumber < g_c->num_chats
&& g_c->chats != nullptr
&& g_c->chats[groupnumber].status != GROUPCHAT_STATUS_NONE;
}
/** @brief Set the size of the groupchat list to num.
*
* @retval false if mem_vrealloc fails.
* @retval true if it succeeds.
*/
static bool realloc_conferences(Group_Chats *_Nonnull g_c, uint16_t num)
{
if (num == 0) {
mem_delete(g_c->mem, g_c->chats);
g_c->chats = nullptr;
return true;
}
Group_c *newgroup_chats = (Group_c *)mem_vrealloc(g_c->mem, g_c->chats, num, sizeof(Group_c));
if (newgroup_chats == nullptr) {
return false;
}
g_c->chats = newgroup_chats;
return true;
}
static void setup_conference(Group_c *_Nonnull g)
{
*g = empty_group_c;
g->maxfrozen = MAX_FROZEN_DEFAULT;
}
/** @brief Create a new empty groupchat connection.
*
* @retval -1 on failure.
* @return groupnumber on success.
*/
static int32_t create_group_chat(Group_Chats *_Nonnull g_c)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) {
return i;
}
}
if (realloc_conferences(g_c, g_c->num_chats + 1)) {
const uint16_t id = g_c->num_chats;
++g_c->num_chats;
setup_conference(&g_c->chats[id]);
return id;
}
return -1;
}
static void wipe_group_c(const Memory *_Nonnull mem, Group_c *_Nonnull g)
{
mem_delete(mem, g->frozen);
mem_delete(mem, g->group);
crypto_memzero(g, sizeof(Group_c));
}
/** @brief Wipe a groupchat.
*
* @retval true on success.
*/
static bool wipe_group_chat(Group_Chats *_Nonnull g_c, uint32_t groupnumber)
{
if (groupnumber >= g_c->num_chats || g_c->chats == nullptr) {
return false;
}
wipe_group_c(g_c->mem, &g_c->chats[groupnumber]);
uint16_t i;
for (i = g_c->num_chats; i != 0; --i) {
if (g_c->chats[i - 1].status != GROUPCHAT_STATUS_NONE) {
break;
}
}
if (g_c->num_chats != i) {
g_c->num_chats = i;
realloc_conferences(g_c, g_c->num_chats);
}
return true;
}
static Group_c *get_group_c(const Group_Chats *_Nonnull g_c, uint32_t groupnumber)
{
if (!is_groupnumber_valid(g_c, groupnumber)) {
return nullptr;
}
return &g_c->chats[groupnumber];
}
/**
* check if peer with real_pk is in peer array.
*
* @return peer index if peer is in group.
* @retval -1 if peer is not in group.
*
* TODO(irungentoo): make this more efficient.
*/
static int peer_in_group(const Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk)
{
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (pk_equal(g->group[i].real_pk, real_pk)) {
return i;
}
}
return -1;
}
static int frozen_in_group(const Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk)
{
for (uint32_t i = 0; i < g->numfrozen; ++i) {
if (pk_equal(g->frozen[i].real_pk, real_pk)) {
return i;
}
}
return -1;
}
/**
* check if group with the given type and id is in group array.
*
* @return group number if peer is in list.
* @retval -1 if group is not in list.
*
* TODO(irungentoo): make this more efficient and maybe use constant time comparisons?
*/
static int32_t get_group_num(const Group_Chats *_Nonnull g_c, const uint8_t type, const uint8_t *_Nonnull id)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].type == type && group_id_eq(g_c->chats[i].id, id)) {
return i;
}
}
return -1;
}
int32_t conference_by_id(const Group_Chats *g_c, const uint8_t *id)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (group_id_eq(g_c->chats[i].id, id)) {
return i;
}
}
return -1;
}
/**
* check if peer with peer_number is in peer array.
*
* @return peer index if peer is in chat.
* @retval -1 if peer is not in chat.
*
* TODO(irungentoo): make this more efficient.
*/
static int get_peer_index(const Group_c *_Nonnull g, uint16_t peer_number)
{
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->group[i].peer_number == peer_number) {
return i;
}
}
return -1;
}
static uint64_t calculate_comp_value(const uint8_t *_Nonnull pk1, const uint8_t *_Nonnull pk2)
{
uint64_t cmp1 = 0;
uint64_t cmp2 = 0;
for (size_t i = 0; i < sizeof(uint64_t); ++i) {
cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
cmp2 = (cmp2 << 8) + (uint64_t)pk2[i];
}
return cmp1 - cmp2;
}
typedef enum Groupchat_Closest_Change {
GROUPCHAT_CLOSEST_CHANGE_NONE,
GROUPCHAT_CLOSEST_CHANGE_ADDED,
GROUPCHAT_CLOSEST_CHANGE_REMOVED,
} Groupchat_Closest_Change;
static bool add_to_closest(Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk, const uint8_t *_Nonnull temp_pk)
{
if (pk_equal(g->real_pk, real_pk)) {
return false;
}
unsigned int index = DESIRED_CLOSEST;
for (unsigned int i = 0; i < DESIRED_CLOSEST; ++i) {
if (g->closest_peers[i].active && pk_equal(real_pk, g->closest_peers[i].real_pk)) {
return true;
}
}
for (unsigned int i = 0; i < DESIRED_CLOSEST; ++i) {
if (!g->closest_peers[i].active) {
index = i;
break;
}
}
if (index == DESIRED_CLOSEST) {
uint64_t comp_val = calculate_comp_value(g->real_pk, real_pk);
uint64_t comp_d = 0;
for (unsigned int i = 0; i < (DESIRED_CLOSEST / 2); ++i) {
const uint64_t comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk);
if (comp > comp_val && comp > comp_d) {
index = i;
comp_d = comp;
}
}
comp_val = calculate_comp_value(real_pk, g->real_pk);
for (unsigned int i = DESIRED_CLOSEST / 2; i < DESIRED_CLOSEST; ++i) {
const uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
if (comp > comp_val && comp > comp_d) {
index = i;
comp_d = comp;
}
}
}
if (index == DESIRED_CLOSEST) {
return false;
}
uint8_t old_real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t old_temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
bool old = false;
if (g->closest_peers[index].active) {
memcpy(old_real_pk, g->closest_peers[index].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(old_temp_pk, g->closest_peers[index].temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
old = true;
}
g->closest_peers[index].active = true;
memcpy(g->closest_peers[index].real_pk, real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(g->closest_peers[index].temp_pk, temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
if (old) {
add_to_closest(g, old_real_pk, old_temp_pk);
}
if (g->changed == GROUPCHAT_CLOSEST_CHANGE_NONE) {
g->changed = GROUPCHAT_CLOSEST_CHANGE_ADDED;
}
return true;
}
static bool pk_in_closest_peers(const Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk)
{
for (unsigned int i = 0; i < DESIRED_CLOSEST; ++i) {
if (!g->closest_peers[i].active) {
continue;
}
if (pk_equal(g->closest_peers[i].real_pk, real_pk)) {
return true;
}
}
return false;
}
static void remove_connection_reason(Group_Chats *_Nonnull g_c, Group_c *_Nonnull g, uint16_t i, uint8_t reason);
static void purge_closest(Group_Chats *_Nonnull g_c, uint32_t groupnumber)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
continue;
}
if ((g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_CLOSEST) == 0) {
continue;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[i].number);
if (!pk_in_closest_peers(g, real_pk)) {
remove_connection_reason(g_c, g, i, GROUPCHAT_CONNECTION_REASON_CLOSEST);
}
}
}
static bool send_packet_online(const Friend_Connections *_Nonnull fr_c, int friendcon_id, uint16_t group_num, uint8_t type, const uint8_t *_Nonnull id);
static int add_conn_to_groupchat(Group_Chats *_Nonnull g_c, int friendcon_id, Group_c *_Nonnull g, uint8_t reason, bool lock);
static void add_closest_connections(Group_Chats *_Nonnull g_c, uint32_t groupnumber, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
for (uint32_t i = 0; i < DESIRED_CLOSEST; ++i) {
if (!g->closest_peers[i].active) {
continue;
}
int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, g->closest_peers[i].real_pk);
bool fresh = false;
if (friendcon_id == -1) {
friendcon_id = new_friend_connection(g_c->fr_c, g->closest_peers[i].real_pk);
fresh = true;
if (friendcon_id == -1) {
continue;
}
set_dht_temp_pk(g_c->fr_c, friendcon_id, g->closest_peers[i].temp_pk, userdata);
}
const int connection_index = add_conn_to_groupchat(g_c, friendcon_id, g,
GROUPCHAT_CONNECTION_REASON_CLOSEST, !fresh);
if (connection_index == -1) {
if (fresh) {
kill_friend_connection(g_c->fr_c, friendcon_id);
}
continue;
}
if (friend_con_connected(g_c->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED
&& g->connections[connection_index].type == GROUPCHAT_CONNECTION_CONNECTING) {
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id);
}
}
}
static bool connect_to_closest(Group_Chats *_Nonnull g_c, uint32_t groupnumber, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
if (g->changed == GROUPCHAT_CLOSEST_CHANGE_NONE) {
return true;
}
if (g->changed == GROUPCHAT_CLOSEST_CHANGE_REMOVED) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
add_to_closest(g, g->group[i].real_pk, g->group[i].temp_pk);
}
}
purge_closest(g_c, groupnumber);
add_closest_connections(g_c, groupnumber, userdata);
g->changed = GROUPCHAT_CLOSEST_CHANGE_NONE;
return true;
}
static int get_frozen_index(const Group_c *_Nonnull g, uint16_t peer_number)
{
for (uint32_t i = 0; i < g->numfrozen; ++i) {
if (g->frozen[i].peer_number == peer_number) {
return i;
}
}
return -1;
}
static bool delete_frozen(const Memory *_Nonnull mem, Group_c *_Nonnull g, uint32_t frozen_index)
{
if (frozen_index >= g->numfrozen) {
return false;
}
--g->numfrozen;
if (g->numfrozen == 0) {
mem_delete(mem, g->frozen);
g->frozen = nullptr;
} else {
if (g->numfrozen != frozen_index) {
g->frozen[frozen_index] = g->frozen[g->numfrozen];
}
Group_Peer *const frozen_temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->numfrozen, sizeof(Group_Peer));
if (frozen_temp == nullptr) {
return false;
}
g->frozen = frozen_temp;
}
return true;
}
/** @brief Update last_active timestamp on peer, and thaw the peer if it is frozen.
*
* @return peer index if peer is in the conference.
* @retval -1 otherwise, and on error.
*/
static int note_peer_active(Group_Chats *_Nonnull g_c, uint32_t groupnumber, uint16_t peer_number, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const int peer_index = get_peer_index(g, peer_number);
if (peer_index != -1) {
g->group[peer_index].last_active = mono_time_get(g_c->mono_time);
return peer_index;
}
const int frozen_index = get_frozen_index(g, peer_number);
if (frozen_index == -1) {
return -1;
}
/* Now thaw the peer */
Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer));
if (temp == nullptr) {
return -1;
}
const uint32_t thawed_index = g->numpeers;
g->group = temp;
g->group[thawed_index] = g->frozen[frozen_index];
g->group[thawed_index].temp_pk_updated = false;
g->group[thawed_index].last_active = mono_time_get(g_c->mono_time);
add_to_closest(g, g->group[thawed_index].real_pk, g->group[thawed_index].temp_pk);
++g->numpeers;
delete_frozen(g_c->mem, g, frozen_index);
if (g_c->peer_list_changed_callback != nullptr) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_join != nullptr) {
g->peer_on_join(g->object, groupnumber, thawed_index);
}
g->need_send_name = true;
return thawed_index;
}
static bool delpeer(Group_Chats *_Nonnull g_c, uint32_t groupnumber, int peer_index, void *_Nullable userdata);
static void delete_any_peer_with_pk(Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull real_pk, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
const int peer_index = peer_in_group(g, real_pk);
if (peer_index >= 0) {
delpeer(g_c, groupnumber, peer_index, userdata);
}
const int frozen_index = frozen_in_group(g, real_pk);
if (frozen_index >= 0) {
delete_frozen(g_c->mem, g, frozen_index);
}
}
/** @brief Add a peer to the group chat, or update an existing peer.
*
* fresh indicates whether we should consider this information on the peer to
* be current, and so should update temp_pk and consider the peer active.
*
* do_gc_callback indicates whether we want to trigger callbacks set by the client
* via the public API. This should be set to false if this function is called
* from outside of the `tox_iterate()` loop.
*
* @return peer_index if success or peer already in chat.
* @retval -1 if error.
*/
static int addpeer(Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull real_pk, const uint8_t *_Nonnull temp_pk,
uint16_t peer_number, void *_Nullable userdata, bool fresh, bool do_gc_callback)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const int peer_index = fresh ?
note_peer_active(g_c, groupnumber, peer_number, userdata) :
get_peer_index(g, peer_number);
if (peer_index != -1) {
if (!pk_equal(g->group[peer_index].real_pk, real_pk)) {
LOGGER_ERROR(g_c->m->log, "peer public key is incorrect for peer %d", peer_number);
return -1;
}
if (fresh || !g->group[peer_index].temp_pk_updated) {
pk_copy(g->group[peer_index].temp_pk, temp_pk);
g->group[peer_index].temp_pk_updated = true;
}
return peer_index;
}
if (!fresh) {
const int frozen_index = get_frozen_index(g, peer_number);
if (frozen_index != -1) {
if (!pk_equal(g->frozen[frozen_index].real_pk, real_pk)) {
return -1;
}
pk_copy(g->frozen[frozen_index].temp_pk, temp_pk);
return -1;
}
}
delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata);
Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer));
if (temp == nullptr) {
return -1;
}
temp[g->numpeers] = empty_group_peer;
g->group = temp;
const uint32_t new_index = g->numpeers;
pk_copy(g->group[new_index].real_pk, real_pk);
pk_copy(g->group[new_index].temp_pk, temp_pk);
g->group[new_index].temp_pk_updated = true;
g->group[new_index].peer_number = peer_number;
g->group[new_index].last_active = mono_time_get(g_c->mono_time);
g->group[new_index].is_friend = getfriend_id(g_c->m, real_pk) != -1;
++g->numpeers;
add_to_closest(g, real_pk, temp_pk);
if (do_gc_callback && g_c->peer_list_changed_callback != nullptr) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_join != nullptr) {
g->peer_on_join(g->object, groupnumber, new_index);
}
return new_index;
}
static void remove_connection(Group_Chats *_Nonnull g_c, Group_c *_Nonnull g, uint16_t i)
{
if ((g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_INTRODUCER) != 0) {
--g->num_introducer_connections;
}
kill_friend_connection(g_c->fr_c, g->connections[i].number);
g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
}
static void remove_from_closest(Group_c *_Nonnull g, int peer_index)
{
for (uint32_t i = 0; i < DESIRED_CLOSEST; ++i) {
if (g->closest_peers[i].active
&& pk_equal(g->closest_peers[i].real_pk, g->group[peer_index].real_pk)) {
g->closest_peers[i].active = false;
g->changed = GROUPCHAT_CLOSEST_CHANGE_REMOVED;
break;
}
}
}
/**
* Delete a peer from the group chat.
*
* return true on success
*/
static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
remove_from_closest(g, peer_index);
const int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, g->group[peer_index].real_pk);
if (friendcon_id != -1) {
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
continue;
}
if (g->connections[i].number == (unsigned int)friendcon_id) {
remove_connection(g_c, g, i);
break;
}
}
}
--g->numpeers;
void *peer_object = g->group[peer_index].object;
if (g->numpeers == 0) {
mem_delete(g_c->mem, g->group);
g->group = nullptr;
} else {
if (g->numpeers != (uint32_t)peer_index) {
g->group[peer_index] = g->group[g->numpeers];
}
Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers, sizeof(Group_Peer));
if (temp == nullptr) {
return false;
}
g->group = temp;
}
if (g_c->peer_list_changed_callback != nullptr) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_leave != nullptr) {
g->peer_on_leave(g->object, groupnumber, peer_object);
}
return true;
}
/** Order peers with friends first and with more recently active earlier */
static bool group_peer_less_handler(const void *_Nonnull object, const void *_Nonnull a, const void *_Nonnull b)
{
const Group_Peer *pa = (const Group_Peer *)a;
const Group_Peer *pb = (const Group_Peer *)b;
if (((pa->is_friend ? 1 : 0) ^ (pb->is_friend ? 1 : 0)) != 0) {
return pa->is_friend;
}
return cmp_uint(pb->last_active, pa->last_active) < 0;
}
static const void *group_peer_get_handler(const void *_Nonnull arr, uint32_t index)
{
const Group_Peer *entries = (const Group_Peer *)arr;
return &entries[index];
}
static void group_peer_set_handler(void *_Nonnull arr, uint32_t index, const void *_Nonnull val)
{
Group_Peer *entries = (Group_Peer *)arr;
const Group_Peer *entry = (const Group_Peer *)val;
entries[index] = *entry;
}
static void *group_peer_subarr_handler(void *_Nonnull arr, uint32_t index, uint32_t size)
{
Group_Peer *entries = (Group_Peer *)arr;
return &entries[index];
}
static void *group_peer_alloc_handler(const void *_Nonnull object, uint32_t size)
{
const Memory *mem = (const Memory *)object;
Group_Peer *tmp = (Group_Peer *)mem_valloc(mem, size, sizeof(Group_Peer));
if (tmp == nullptr) {
return nullptr;
}
return tmp;
}
static void group_peer_delete_handler(const void *_Nonnull object, void *_Nonnull arr, uint32_t size)
{
const Memory *mem = (const Memory *)object;
mem_delete(mem, arr);
}
static const Sort_Funcs group_peer_cmp_funcs = {
group_peer_less_handler,
group_peer_get_handler,
group_peer_set_handler,
group_peer_subarr_handler,
group_peer_alloc_handler,
group_peer_delete_handler,
};
/** @brief Delete frozen peers as necessary to ensure at most `g->maxfrozen` remain.
*
* @retval true if any frozen peers are removed.
*/
static bool delete_old_frozen(Group_c *_Nonnull g, const Memory *_Nonnull mem)
{
if (g->numfrozen <= g->maxfrozen) {
return false;
}
if (g->maxfrozen == 0) {
mem_delete(mem, g->frozen);
g->frozen = nullptr;
g->numfrozen = 0;
return true;
}
merge_sort(g->frozen, g->numfrozen, mem, &group_peer_cmp_funcs);
Group_Peer *temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->maxfrozen, sizeof(Group_Peer));
if (temp == nullptr) {
return false;
}
g->frozen = temp;
g->numfrozen = g->maxfrozen;
return true;
}
static bool try_send_rejoin(Group_Chats *_Nonnull g_c, Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk);
static bool freeze_peer(Group_Chats *_Nonnull g_c, uint32_t groupnumber, int peer_index, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer));
if (temp == nullptr) {
return false;
}
g->frozen = temp;
g->frozen[g->numfrozen] = g->group[peer_index];
g->frozen[g->numfrozen].object = nullptr;
if (!delpeer(g_c, groupnumber, peer_index, userdata)) {
return false;
}
try_send_rejoin(g_c, g, g->frozen[g->numfrozen].real_pk);
++g->numfrozen;
delete_old_frozen(g, g_c->mem);
return true;
}
/** @brief Set the nick for a peer.
*
* do_gc_callback indicates whether we want to trigger callbacks set by the client
* via the public API. This should be set to false if this function is called
* from outside of the `tox_iterate()` loop.
*
* @retval true on success.
*/
static bool setnick(Group_Chats *_Nonnull g_c, uint32_t groupnumber, int peer_index, const uint8_t *_Nonnull nick, uint16_t nick_len,
void *_Nullable userdata, bool do_gc_callback)
{
if (nick_len > MAX_NAME_LENGTH) {
return false;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
g->group[peer_index].nick_updated = true;
if (g_peer_nick_eq(&g->group[peer_index], nick, nick_len)) {
/* same name as already stored */
return true;
}
if (nick_len > 0) {
memcpy(g->group[peer_index].nick, nick, nick_len);
}
g->group[peer_index].nick_len = nick_len;
if (do_gc_callback && g_c->peer_name_callback != nullptr) {
g_c->peer_name_callback(g_c->m, groupnumber, peer_index, nick, nick_len, userdata);
}
return true;
}
/** @brief Set the title for a group.
*
* @retval true on success.
*/
static bool settitle(Group_Chats *_Nonnull g_c, uint32_t groupnumber, int peer_index, const uint8_t *_Nonnull title, uint8_t title_len,
void *_Nullable userdata)
{
if (title_len > MAX_NAME_LENGTH || title_len == 0) {
return false;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
if (g_title_eq(g, title, title_len)) {
/* same title as already set */
return true;
}
memcpy(g->title, title, title_len);
g->title_len = title_len;
g->title_fresh = true;
if (g_c->title_callback != nullptr) {
g_c->title_callback(g_c->m, groupnumber, peer_index, title, title_len, userdata);
}
return true;
}
/** Check if the group has no online connection, and freeze all peers if so */
static void check_disconnected(Group_Chats *_Nonnull g_c, uint32_t groupnumber, void *_Nullable userdata)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_ONLINE) {
return;
}
}
for (uint32_t i = 0; i < g->numpeers; ++i) {
while (i < g->numpeers && !pk_equal(g->group[i].real_pk, g->real_pk)) {
freeze_peer(g_c, groupnumber, i, userdata);
}
}
}
static void set_conns_type_connections(Group_Chats *_Nonnull g_c, uint32_t groupnumber, int friendcon_id, uint8_t type,
void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
continue;
}
if (g->connections[i].number != (unsigned int)friendcon_id) {
continue;
}
if (type == GROUPCHAT_CONNECTION_ONLINE) {
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id);
} else {
g->connections[i].type = type;
check_disconnected(g_c, groupnumber, userdata);
}
}
}
/** Set the type for all connections with friendcon_id */
static void set_conns_status_groups(Group_Chats *_Nonnull g_c, int friendcon_id, uint8_t type, void *_Nullable userdata)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
set_conns_type_connections(g_c, i, friendcon_id, type, userdata);
}
}
static void rejoin_frozen_friend(Group_Chats *_Nonnull g_c, int friendcon_id)
{
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, friendcon_id);
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (g == nullptr) {
continue;
}
for (uint32_t j = 0; j < g->numfrozen; ++j) {
if (pk_equal(g->frozen[j].real_pk, real_pk)) {
try_send_rejoin(g_c, g, real_pk);
break;
}
}
}
}
static int g_handle_any_status(void *_Nonnull object, int friendcon_id, bool status, void *_Nullable userdata)
{
Group_Chats *g_c = (Group_Chats *)object;
if (status) {
rejoin_frozen_friend(g_c, friendcon_id);
}
return 0;
}
static int g_handle_status(void *_Nonnull object, int friendcon_id, bool status, void *_Nullable userdata)
{
Group_Chats *g_c = (Group_Chats *)object;
if (status) { /* Went online */
set_conns_status_groups(g_c, friendcon_id, GROUPCHAT_CONNECTION_ONLINE, userdata);
} else { /* Went offline */
set_conns_status_groups(g_c, friendcon_id, GROUPCHAT_CONNECTION_CONNECTING, userdata);
// TODO(irungentoo): remove timedout connections?
}
return 0;
}
static int g_handle_packet(void *_Nonnull object, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata);
static int handle_lossy(void *_Nonnull object, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata);
/** @brief Add friend to group chat.
*
* @return connections index on success
* @retval -1 on failure.
*/
static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, Group_c *g, uint8_t reason,
bool lock)
{
uint16_t empty = MAX_GROUP_CONNECTIONS;
uint16_t ind = MAX_GROUP_CONNECTIONS;
for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
empty = i;
continue;
}
if (g->connections[i].number == (uint32_t)friendcon_id) {
ind = i; /* Already in list. */
break;
}
}
if (ind == MAX_GROUP_CONNECTIONS) {
if (empty == MAX_GROUP_CONNECTIONS) {
return -1;
}
if (lock) {
friend_connection_lock(g_c->fr_c, friendcon_id);
}
g->connections[empty].type = GROUPCHAT_CONNECTION_CONNECTING;
g->connections[empty].number = friendcon_id;
g->connections[empty].reasons = 0;
// TODO(irungentoo):
friend_connection_callbacks(g_c->m->fr_c, friendcon_id, GROUPCHAT_CALLBACK_INDEX, &g_handle_status, &g_handle_packet,
&handle_lossy, g_c, friendcon_id);
ind = empty;
}
if ((g->connections[ind].reasons & reason) == 0) {
g->connections[ind].reasons |= reason;
if (reason == GROUPCHAT_CONNECTION_REASON_INTRODUCER) {
++g->num_introducer_connections;
}
}
return ind;
}
static bool send_peer_introduced(const Group_Chats *_Nonnull g_c, int friendcon_id, uint16_t group_num);
/** @brief Removes reason for keeping connection.
*
* Kills connection if this was the last reason.
*/
static void remove_connection_reason(Group_Chats *g_c, Group_c *g, uint16_t i, uint8_t reason)
{
if ((g->connections[i].reasons & reason) == 0) {
return;
}
g->connections[i].reasons &= ~reason;
if (reason == GROUPCHAT_CONNECTION_REASON_INTRODUCER) {
--g->num_introducer_connections;
if (g->connections[i].type == GROUPCHAT_CONNECTION_ONLINE) {
send_peer_introduced(g_c, g->connections[i].number, g->connections[i].group_number);
}
}
if (g->connections[i].reasons == 0) {
kill_friend_connection(g_c->fr_c, g->connections[i].number);
g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
}
}
/** @brief Creates a new groupchat and puts it in the chats array.
*
* @param rng Random number generator used for generating the group ID.
* @param type is one of `GROUPCHAT_TYPE_*`
*
* @return group number on success.
* @retval -1 on failure.
*/
int add_groupchat(Group_Chats *g_c, const Random *rng, uint8_t type)
{
const int32_t groupnumber = create_group_chat(g_c);
if (groupnumber == -1) {
return -1;
}
Group_c *g = &g_c->chats[groupnumber];
g->status = GROUPCHAT_STATUS_CONNECTED;
g->type = type;
new_symmetric_key(rng, g->id);
g->peer_number = 0; /* Founder is peer 0. */
memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE);
const int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), 0, nullptr, true,
false);
if (peer_index == -1) {
return -1;
}
setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length, nullptr, false);
return groupnumber;
}
static bool group_leave(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, bool permanent);
/** @brief Delete a groupchat from the chats array, informing the group first as
* appropriate.
*
* @retval true on success.
* @retval false if groupnumber is invalid.
*/
bool del_groupchat(Group_Chats *g_c, uint32_t groupnumber, bool leave_permanently)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
group_leave(g_c, groupnumber, leave_permanently);
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
continue;
}
g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
kill_friend_connection(g_c->fr_c, g->connections[i].number);
}
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->peer_on_leave != nullptr) {
g->peer_on_leave(g->object, groupnumber, g->group[i].object);
}
}
if (g->group_on_delete != nullptr) {
g->group_on_delete(g->object, groupnumber);
}
return wipe_group_chat(g_c, groupnumber);
}
static const Group_Peer *peer_in_list(const Group_c *_Nonnull g, uint32_t peernumber, bool frozen)
{
const Group_Peer *list = frozen ? g->frozen : g->group;
const uint32_t num = frozen ? g->numfrozen : g->numpeers;
if (peernumber >= num) {
return nullptr;
}
return &list[peernumber];
}
/**
* @brief Copy the public key of (frozen, if frozen is true) peernumber who is in
* groupnumber to pk.
*
* @param pk must be CRYPTO_PUBLIC_KEY_SIZE long.
*
* @retval 0 on success
* @retval -1 if groupnumber is invalid.
* @retval -2 if peernumber is invalid.
*/
int group_peer_pubkey(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, uint8_t *pk, bool frozen)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const Group_Peer *peer = peer_in_list(g, peernumber, frozen);
if (peer == nullptr) {
return -2;
}
memcpy(pk, peer->real_pk, CRYPTO_PUBLIC_KEY_SIZE);
return 0;
}
/**
* @brief Return the size of (frozen, if frozen is true) peernumber's name.
*
* @retval -1 if groupnumber is invalid.
* @retval -2 if peernumber is invalid.
*/
int group_peername_size(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, bool frozen)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const Group_Peer *peer = peer_in_list(g, peernumber, frozen);
if (peer == nullptr) {
return -2;
}
return peer->nick_len;
}
/**
* @brief Copy the name of (frozen, if frozen is true) peernumber who is in
* groupnumber to name.
*
* @param name must be at least MAX_NAME_LENGTH long.
*
* @return length of name if success
* @retval -1 if groupnumber is invalid.
* @retval -2 if peernumber is invalid.
*/
int group_peername(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, uint8_t *name, bool frozen)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const Group_Peer *peer = peer_in_list(g, peernumber, frozen);
if (peer == nullptr) {
return -2;
}
if (peer->nick_len > 0) {
memcpy(name, peer->nick, peer->nick_len);
}
return peer->nick_len;
}
/**
* @brief Copy last active timestamp of frozen peernumber who is in groupnumber to
* last_active.
*
* @retval 0 on success.
* @retval -1 if groupnumber is invalid.
* @retval -2 if peernumber is invalid.
*/
int group_frozen_last_active(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber,
uint64_t *last_active)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (peernumber >= g->numfrozen) {
return -2;
}
*last_active = g->frozen[peernumber].last_active;
return 0;
}
/** @brief Set maximum number of frozen peers.
*
* @retval 0 on success.
* @retval -1 if groupnumber is invalid.
*/
int group_set_max_frozen(const Group_Chats *g_c, uint32_t groupnumber, uint32_t maxfrozen)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
g->maxfrozen = maxfrozen;
delete_old_frozen(g, g_c->mem);
return 0;
}
/**
* @return the number of (frozen, if frozen is true) peers in the group chat on success.
* @retval -1 if groupnumber is invalid.
*/
int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber, bool frozen)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
return frozen ? g->numfrozen : g->numpeers;
}
/**
* @retval 1 if the peernumber corresponds to ours.
* @retval 0 if the peernumber is not ours.
* @retval -1 if groupnumber is invalid.
* @retval -2 if peernumber is invalid.
* @retval -3 if we are not connected to the group chat.
*/
int group_peernumber_is_ours(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (peernumber >= g->numpeers) {
return -2;
}
if (g->status != GROUPCHAT_STATUS_CONNECTED) {
return -3;
}
return (g->peer_number == g->group[peernumber].peer_number) ? 1 : 0;
}
/** @brief return the type of groupchat (GROUPCHAT_TYPE_) that groupnumber is.
*
* @retval -1 on failure.
* @return type on success.
*/
int group_get_type(const Group_Chats *g_c, uint32_t groupnumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
return g->type;
}
/** @brief Copies the unique id of `group_chat[groupnumber]` into `id`.
*
* @retval false on failure.
* @retval true on success.
*/
bool conference_get_id(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *id)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
if (id != nullptr) {
memcpy(id, g->id, sizeof(g->id));
}
return true;
}
/** @brief Send a group packet to friendcon_id.
*
* @retval true on success
* @retval false on failure
*/
static bool send_packet_group_peer(const Friend_Connections *_Nonnull fr_c, int friendcon_id, uint8_t packet_id, uint16_t group_num, const uint8_t *_Nonnull data, uint16_t length)
{
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return false;
}
group_num = net_htons(group_num);
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
VLA(uint8_t, packet, packet_size);
packet[0] = packet_id;
memcpy(packet + 1, &group_num, sizeof(uint16_t));
memcpy(packet + 1 + sizeof(uint16_t), data, length);
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
packet, packet_size, false) != -1;
}
/** @brief Send a group lossy packet to friendcon_id.
*
* @retval true on success
* @retval false on failure
*/
static bool send_lossy_group_peer(const Friend_Connections *_Nonnull fr_c, int friendcon_id, uint8_t packet_id, uint16_t group_num, const uint8_t *_Nonnull data, uint16_t length)
{
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return false;
}
group_num = net_htons(group_num);
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
VLA(uint8_t, packet, packet_size);
packet[0] = packet_id;
memcpy(packet + 1, &group_num, sizeof(uint16_t));
memcpy(packet + 1 + sizeof(uint16_t), data, length);
return send_lossy_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
packet, packet_size) != -1;
}
/** @brief invite friendnumber to groupnumber.
*
* @retval 0 on success.
* @retval -1 if groupnumber is invalid.
* @retval -2 if invite packet failed to send.
* @retval -3 if we are not connected to the group chat.
*/
int invite_friend(const Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (g->status != GROUPCHAT_STATUS_CONNECTED) {
return -3;
}
uint8_t invite[INVITE_PACKET_SIZE];
invite[0] = INVITE_ID;
const uint16_t groupchat_num = net_htons((uint16_t)groupnumber);
memcpy(invite + 1, &groupchat_num, sizeof(groupchat_num));
invite[1 + sizeof(groupchat_num)] = g->type;
memcpy(invite + 1 + sizeof(groupchat_num) + 1, g->id, GROUP_ID_LENGTH);
if (send_conference_invite_packet(g_c->m, friendnumber, invite, sizeof(invite))) {
return 0;
}
return -2;
}
/** @brief Send a rejoin packet to a peer if we have a friend connection to the peer.
* @retval true if a packet was sent.
* @retval false otherwise.
*/
static bool try_send_rejoin(Group_Chats *g_c, Group_c *g, const uint8_t *real_pk)
{
const int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, real_pk);
if (friendcon_id == -1) {
return false;
}
uint8_t packet[1 + 1 + GROUP_ID_LENGTH];
packet[0] = PACKET_ID_REJOIN_CONFERENCE;
packet[1] = g->type;
memcpy(packet + 2, g->id, GROUP_ID_LENGTH);
if (write_cryptpacket(friendconn_net_crypto(g_c->fr_c), friend_connection_crypt_connection_id(g_c->fr_c, friendcon_id),
packet, sizeof(packet), false) == -1) {
return false;
}
add_conn_to_groupchat(g_c, friendcon_id, g, GROUPCHAT_CONNECTION_REASON_INTRODUCER, true);
return true;
}
static bool send_peer_query(const Group_Chats *_Nonnull g_c, int friendcon_id, uint16_t group_num);
static bool send_invite_response(Group_Chats *_Nonnull g_c, int groupnumber, uint32_t friendnumber, const uint8_t *_Nonnull data, uint16_t length);
/** @brief Join a group (we need to have been invited first).
*
* @param expected_type is the groupchat type we expect the chat we are joining
* to have.
*
* @return group number on success.
* @retval -1 if data length is invalid.
* @retval -2 if group is not the expected type.
* @retval -3 if friendnumber is invalid.
* @retval -4 if client is already in this group.
* @retval -5 if group instance failed to initialize.
* @retval -6 if join packet fails to send.
*/
int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length)
{
if (length != sizeof(uint16_t) + 1 + GROUP_ID_LENGTH) {
return -1;
}
if (data[sizeof(uint16_t)] != expected_type) {
return -2;
}
const int friendcon_id = getfriendcon_id(g_c->m, friendnumber);
if (friendcon_id == -1) {
return -3;
}
if (get_group_num(g_c, data[sizeof(uint16_t)], data + sizeof(uint16_t) + 1) != -1) {
return -4;
}
const int groupnumber = create_group_chat(g_c);
if (groupnumber == -1) {
return -5;
}
Group_c *g = &g_c->chats[groupnumber];
g->status = GROUPCHAT_STATUS_VALID;
memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE);
if (!send_invite_response(g_c, groupnumber, friendnumber, data, length)) {
g->status = GROUPCHAT_STATUS_NONE;
return -6;
}
return groupnumber;
}
static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t friendnumber, const uint8_t *data,
uint16_t length)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
const bool member = g->status == GROUPCHAT_STATUS_CONNECTED;
const uint32_t response_size = member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE;
VLA(uint8_t, response, response_size);
response[0] = member ? INVITE_MEMBER_ID : INVITE_ACCEPT_ID;
net_pack_u16(response + 1, groupnumber);
memcpy(response + 1 + sizeof(uint16_t), data, length);
if (member) {
net_pack_u16(response + 1 + sizeof(uint16_t) + length, g->peer_number);
}
if (!send_conference_invite_packet(g_c->m, friendnumber, response, response_size)) {
return false;
}
if (!member) {
g->type = data[sizeof(uint16_t)];
memcpy(g->id, data + sizeof(uint16_t) + 1, GROUP_ID_LENGTH);
}
uint16_t other_groupnum;
net_unpack_u16(data, &other_groupnum);
const int friendcon_id = getfriendcon_id(g_c->m, friendnumber);
if (friendcon_id == -1) {
return false;
}
const int connection_index = add_conn_to_groupchat(g_c, friendcon_id, g, GROUPCHAT_CONNECTION_REASON_INTRODUCER, true);
if (member) {
add_conn_to_groupchat(g_c, friendcon_id, g, GROUPCHAT_CONNECTION_REASON_INTRODUCING, false);
}
if (connection_index != -1) {
g->connections[connection_index].group_number = other_groupnum;
g->connections[connection_index].type = GROUPCHAT_CONNECTION_ONLINE;
}
send_peer_query(g_c, friendcon_id, other_groupnum);
return true;
}
/** Set handlers for custom lossy packets. */
void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, lossy_packet_cb *function)
{
g_c->lossy_packethandlers[byte] = function;
}
/** Set the callback for group invites. */
void g_callback_group_invite(Group_Chats *g_c, g_conference_invite_cb *function)
{
g_c->invite_callback = function;
}
/** Set the callback for group connection. */
void g_callback_group_connected(Group_Chats *g_c, g_conference_connected_cb *function)
{
g_c->connected_callback = function;
}
/** Set the callback for group messages. */
void g_callback_group_message(Group_Chats *g_c, g_conference_message_cb *function)
{
g_c->message_callback = function;
}
/** @brief Set callback function for peer nickname changes.
*
* It gets called every time a peer changes their nickname.
*/
void g_callback_peer_name(Group_Chats *g_c, peer_name_cb *function)
{
g_c->peer_name_callback = function;
}
/** @brief Set callback function for peer list changes.
*
* It gets called every time the name list changes(new peer, deleted peer)
*/
void g_callback_peer_list_changed(Group_Chats *g_c, peer_list_changed_cb *function)
{
g_c->peer_list_changed_callback = function;
}
/** Set callback function for title changes. */
void g_callback_group_title(Group_Chats *g_c, title_cb *function)
{
g_c->title_callback = function;
}
/** @brief Set a function to be called when a new peer joins a group chat.
*
* @retval 0 on success.
* @retval -1 on failure.
*/
int callback_groupchat_peer_new(const Group_Chats *g_c, uint32_t groupnumber, peer_on_join_cb *function)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
g->peer_on_join = function;
return 0;
}
/** @brief Set a function to be called when a peer leaves a group chat.
*
* @retval 0 on success.
* @retval -1 on failure.
*/
int callback_groupchat_peer_delete(const Group_Chats *g_c, uint32_t groupnumber, peer_on_leave_cb *function)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
g->peer_on_leave = function;
return 0;
}
/** @brief Set a function to be called when the group chat is deleted.
*
* @retval 0 on success.
* @retval -1 on failure.
*/
int callback_groupchat_delete(const Group_Chats *g_c, uint32_t groupnumber, group_on_delete_cb *function)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
g->group_on_delete = function;
return 0;
}
static int send_message_group(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *_Nullable data,
uint16_t len);
/** @brief send a ping message
* return true on success
*/
static bool group_ping_send(const Group_Chats *_Nonnull g_c, uint32_t groupnumber)
{
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0;
}
/** @brief send a new_peer message
* return true on success
*/
static bool group_new_peer_send(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, uint16_t peer_num, const uint8_t *_Nonnull real_pk, const uint8_t *_Nonnull temp_pk)
{
uint8_t packet[GROUP_MESSAGE_NEW_PEER_LENGTH];
peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t));
memcpy(packet + sizeof(uint16_t), real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(packet + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet)) > 0;
}
/** @brief send a kill_peer message
* return true on success
*/
static bool group_kill_peer_send(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, uint16_t peer_num)
{
uint8_t packet[GROUP_MESSAGE_KILL_PEER_LENGTH];
peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t));
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0;
}
/** @brief send a freeze_peer message
* return true on success
*/
static bool group_freeze_peer_send(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, uint16_t peer_num)
{
uint8_t packet[GROUP_MESSAGE_KILL_PEER_LENGTH];
peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t));
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_FREEZE_PEER_ID, packet, sizeof(packet)) > 0;
}
/** @brief send a name message
* return true on success
*/
static bool group_name_send(const Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull nick, uint16_t nick_len)
{
if (nick_len > MAX_NAME_LENGTH) {
return false;
}
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len) > 0;
}
/** @brief send message to announce leaving group
* return true on success
*/
static bool group_leave(const Group_Chats *g_c, uint32_t groupnumber, bool permanent)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
if (permanent) {
return group_kill_peer_send(g_c, groupnumber, g->peer_number);
} else {
return group_freeze_peer_send(g_c, groupnumber, g->peer_number);
}
}
/** @brief set the group's title, limited to MAX_NAME_LENGTH.
* @retval 0 on success
* @retval -1 if groupnumber is invalid.
* @retval -2 if title is too long or empty.
* @retval -3 if packet fails to send.
*/
int group_title_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *title, uint8_t title_len)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (title_len > MAX_NAME_LENGTH || title_len == 0) {
return -2;
}
/* same as already set? */
if (g_title_eq(g, title, title_len)) {
return 0;
}
memcpy(g->title, title, title_len);
g->title_len = title_len;
if (g->numpeers == 1) {
return 0;
}
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_TITLE_ID, title, title_len) > 0) {
return 0;
}
return -3;
}
/** @brief return the group's title size.
* @retval -1 of groupnumber is invalid.
* @retval -2 if title is too long or empty.
*/
int group_title_get_size(const Group_Chats *g_c, uint32_t groupnumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (g->title_len > MAX_NAME_LENGTH || g->title_len == 0) {
return -2;
}
return g->title_len;
}
/** @brief Get group title from groupnumber and put it in title.
*
* Title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
*
* @return length of copied title if success.
* @retval -1 if groupnumber is invalid.
* @retval -2 if title is too long or empty.
*/
int group_title_get(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *title)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (g->title_len > MAX_NAME_LENGTH || g->title_len == 0) {
return -2;
}
memcpy(title, g->title, g->title_len);
return g->title_len;
}
static bool get_peer_number(const Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk, uint16_t *_Nonnull peer_number)
{
const int peer_index = peer_in_group(g, real_pk);
if (peer_index >= 0) {
*peer_number = g->group[peer_index].peer_number;
return true;
}
const int frozen_index = frozen_in_group(g, real_pk);
if (frozen_index >= 0) {
*peer_number = g->frozen[frozen_index].peer_number;
return true;
}
return false;
}
static void handle_friend_invite_packet(Messenger *_Nonnull m, uint32_t friend_number, const uint8_t *_Nonnull cookie, uint16_t length,
void *_Nullable user_data)
{
Group_Chats *g_c = m->conferences_object;
if (length <= 1) {
return;
}
switch (cookie[0]) {
case INVITE_ID: {
if (length != INVITE_PACKET_SIZE) {
return;
}
const int groupnumber = get_group_num(g_c, cookie[1 + sizeof(uint16_t)], cookie + 1 + sizeof(uint16_t) + 1);
const uint8_t *invite_data = cookie + 1;
const uint16_t invite_length = length - 1;
if (groupnumber == -1) {
if (g_c->invite_callback != nullptr) {
g_c->invite_callback(m, friend_number, invite_data[sizeof(uint16_t)], invite_data, invite_length, user_data);
}
return;
} else {
const Group_c *g = get_group_c(g_c, groupnumber);
if (g != nullptr && g->status == GROUPCHAT_STATUS_CONNECTED) {
send_invite_response(g_c, groupnumber, friend_number, invite_data, invite_length);
}
}
break;
}
case INVITE_ACCEPT_ID:
case INVITE_MEMBER_ID: {
const bool member = cookie[0] == INVITE_MEMBER_ID;
if (length != (member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE)) {
return;
}
uint16_t other_groupnum;
uint16_t groupnum;
net_unpack_u16(cookie + 1, &other_groupnum);
net_unpack_u16(cookie + 1 + sizeof(uint16_t), &groupnum);
Group_c *g = get_group_c(g_c, groupnum);
if (g == nullptr) {
return;
}
if (cookie[1 + sizeof(uint16_t) * 2] != g->type) {
return;
}
if (!group_id_eq(cookie + 1 + sizeof(uint16_t) * 2 + 1, g->id)) {
return;
}
uint16_t peer_number;
if (member) {
net_unpack_u16(cookie + 1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH, &peer_number);
} else {
/* TODO(irungentoo): what if two people enter the group at the
* same time and are given the same peer_number by different
* nodes? */
peer_number = random_u16(m->rng);
unsigned int tries = 0;
while (get_peer_index(g, peer_number) != -1 || get_frozen_index(g, peer_number) != -1) {
peer_number = random_u16(m->rng);
++tries;
if (tries > 32) {
return;
}
}
}
const int friendcon_id = getfriendcon_id(m, friend_number);
if (friendcon_id == -1) {
// TODO(iphydf): Log something?
return;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
addpeer(g_c, groupnum, real_pk, temp_pk, peer_number, user_data, true, true);
const int connection_index = add_conn_to_groupchat(g_c, friendcon_id, g,
GROUPCHAT_CONNECTION_REASON_INTRODUCING, true);
if (member) {
add_conn_to_groupchat(g_c, friendcon_id, g, GROUPCHAT_CONNECTION_REASON_INTRODUCER, false);
send_peer_query(g_c, friendcon_id, other_groupnum);
}
if (connection_index != -1) {
g->connections[connection_index].group_number = other_groupnum;
g->connections[connection_index].type = GROUPCHAT_CONNECTION_ONLINE;
}
group_new_peer_send(g_c, groupnum, peer_number, real_pk, temp_pk);
break;
}
default: {
return;
}
}
}
/** @brief Find index of friend in the connections list.
*
* return index on success
* return -1 on failure.
*/
static int friend_in_connections(const Group_c *_Nonnull g, int friendcon_id)
{
for (unsigned int i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
continue;
}
if (g->connections[i].number == (uint32_t)friendcon_id) {
return i;
}
}
return -1;
}
/** return number of connections. */
static unsigned int count_connected(const Group_c *_Nonnull g)
{
unsigned int count = 0;
for (unsigned int i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_ONLINE) {
++count;
}
}
return count;
}
static bool send_packet_online(const Friend_Connections *fr_c, int friendcon_id, uint16_t group_num,
uint8_t type, const uint8_t *id)
{
uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE];
group_num = net_htons(group_num);
packet[0] = PACKET_ID_ONLINE_PACKET;
memcpy(packet + 1, &group_num, sizeof(uint16_t));
packet[1 + sizeof(uint16_t)] = type;
memcpy(packet + 1 + sizeof(uint16_t) + 1, id, GROUP_ID_LENGTH);
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id), packet,
sizeof(packet), false) != -1;
}
static bool ping_groupchat(const Group_Chats *_Nonnull g_c, uint32_t groupnumber);
static int handle_packet_online(const Group_Chats *_Nonnull g_c, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length)
{
if (length != ONLINE_PACKET_DATA_SIZE) {
return -1;
}
const int groupnumber = get_group_num(g_c, data[sizeof(uint16_t)], data + sizeof(uint16_t) + 1);
if (groupnumber == -1) {
return -1;
}
uint16_t other_groupnum;
memcpy(&other_groupnum, data, sizeof(uint16_t));
other_groupnum = net_ntohs(other_groupnum);
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const int index = friend_in_connections(g, friendcon_id);
if (index == -1) {
return -1;
}
if (g->connections[index].type == GROUPCHAT_CONNECTION_ONLINE) {
return -1;
}
if (count_connected(g) == 0 || (g->connections[index].reasons & GROUPCHAT_CONNECTION_REASON_INTRODUCER) != 0) {
send_peer_query(g_c, friendcon_id, other_groupnum);
}
g->connections[index].group_number = other_groupnum;
g->connections[index].type = GROUPCHAT_CONNECTION_ONLINE;
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id);
if ((g->connections[index].reasons & GROUPCHAT_CONNECTION_REASON_INTRODUCING) != 0) {
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
const int peer_index = peer_in_group(g, real_pk);
if (peer_index != -1) {
group_new_peer_send(g_c, groupnumber, g->group[peer_index].peer_number, real_pk, temp_pk);
}
g->need_send_name = true;
}
ping_groupchat(g_c, groupnumber);
return 0;
}
static int handle_packet_rejoin(Group_Chats *_Nonnull g_c, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length,
void *_Nullable userdata)
{
if (length < 1 + GROUP_ID_LENGTH) {
return -1;
}
const int32_t groupnum = get_group_num(g_c, *data, data + 1);
Group_c *g = get_group_c(g_c, groupnum);
if (g == nullptr) {
return -1;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
uint16_t peer_number;
if (!get_peer_number(g, real_pk, &peer_number)) {
return -1;
}
addpeer(g_c, groupnum, real_pk, temp_pk, peer_number, userdata, true, true);
const int connection_index = add_conn_to_groupchat(g_c, friendcon_id, g,
GROUPCHAT_CONNECTION_REASON_INTRODUCING, true);
if (connection_index != -1) {
send_packet_online(g_c->fr_c, friendcon_id, groupnum, g->type, g->id);
}
return 0;
}
// we could send title with invite, but then if it changes between sending and accepting inv, joinee won't see it
/**
* @retval true on success.
* @retval false on failure
*/
static bool send_peer_introduced(const Group_Chats *g_c, int friendcon_id, uint16_t group_num)
{
uint8_t packet[1];
packet[0] = PEER_INTRODUCED_ID;
return send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, packet, sizeof(packet));
}
/**
* @retval true on success.
* @retval false on failure
*/
static bool send_peer_query(const Group_Chats *g_c, int friendcon_id, uint16_t group_num)
{
uint8_t packet[1];
packet[0] = PEER_QUERY_ID;
return send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, packet, sizeof(packet));
}
/**
* @return number of peers sent on success.
* @retval 0 on failure.
*/
static unsigned int send_peers(const Group_Chats *_Nonnull g_c, const Group_c *_Nonnull g, int friendcon_id, uint16_t group_num)
{
uint8_t response_packet[MAX_CRYPTO_DATA_SIZE - (1 + sizeof(uint16_t))];
response_packet[0] = PEER_RESPONSE_ID;
uint8_t *p = response_packet + 1;
uint16_t sent = 0;
for (uint32_t i = 0; i <= g->numpeers; ++i) {
if (i == g->numpeers
|| (p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len >
sizeof(response_packet)) {
if (send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, response_packet,
p - response_packet)) {
sent = i;
} else {
return sent;
}
if (i == g->numpeers) {
break;
}
p = response_packet + 1;
}
const uint16_t peer_num = net_htons(g->group[i].peer_number);
memcpy(p, &peer_num, sizeof(peer_num));
p += sizeof(peer_num);
memcpy(p, g->group[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
p += CRYPTO_PUBLIC_KEY_SIZE;
memcpy(p, g->group[i].temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
p += CRYPTO_PUBLIC_KEY_SIZE;
*p = g->group[i].nick_len;
p += 1;
memcpy(p, g->group[i].nick, g->group[i].nick_len);
p += g->group[i].nick_len;
}
if (g->title_len > 0) {
const uint32_t title_packet_size = 1 + g->title_len;
VLA(uint8_t, title_packet, title_packet_size);
title_packet[0] = PEER_TITLE_ID;
memcpy(title_packet + 1, g->title, g->title_len);
send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num,
title_packet, title_packet_size);
}
return sent;
}
static int handle_send_peers(Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull data, uint16_t length,
void *_Nullable userdata)
{
if (length == 0) {
return -1;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const uint8_t *d = data;
while ((unsigned int)(length - (d - data)) >= sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1) {
uint16_t peer_num;
memcpy(&peer_num, d, sizeof(peer_num));
peer_num = net_ntohs(peer_num);
d += sizeof(uint16_t);
if (g->status == GROUPCHAT_STATUS_VALID
&& pk_equal(d, nc_get_self_public_key(g_c->m->net_crypto))) {
g->peer_number = peer_num;
g->status = GROUPCHAT_STATUS_CONNECTED;
if (g_c->connected_callback != nullptr) {
g_c->connected_callback(g_c->m, groupnumber, userdata);
}
g->need_send_name = true;
}
const int peer_index = addpeer(g_c, groupnumber, d, d + CRYPTO_PUBLIC_KEY_SIZE, peer_num, userdata, false, true);
if (peer_index == -1) {
return -1;
}
d += CRYPTO_PUBLIC_KEY_SIZE * 2;
const uint8_t name_length = *d;
d += 1;
if (name_length > (length - (d - data)) || name_length > MAX_NAME_LENGTH) {
return -1;
}
if (!g->group[peer_index].nick_updated) {
setnick(g_c, groupnumber, peer_index, d, name_length, userdata, true);
}
d += name_length;
}
return 0;
}
static void handle_direct_packet(Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull data, uint16_t length,
uint32_t connection_index, void *_Nullable userdata)
{
if (length == 0) {
return;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
switch (data[0]) {
case PEER_INTRODUCED_ID: {
remove_connection_reason(g_c, g, connection_index, GROUPCHAT_CONNECTION_REASON_INTRODUCING);
break;
}
case PEER_QUERY_ID: {
if (g->connections[connection_index].type != GROUPCHAT_CONNECTION_ONLINE) {
return;
}
send_peers(g_c, g, g->connections[connection_index].number, g->connections[connection_index].group_number);
break;
}
case PEER_RESPONSE_ID: {
handle_send_peers(g_c, groupnumber, data + 1, length - 1, userdata);
break;
}
case PEER_TITLE_ID: {
if (!g->title_fresh) {
settitle(g_c, groupnumber, -1, data + 1, length - 1, userdata);
}
break;
}
}
}
/** @brief Send message to all connections except receiver (if receiver isn't -1)
*
* NOTE: this function appends the group chat number to the data passed to it.
*
* @return number of messages sent.
*/
static unsigned int send_message_all_connections(const Group_Chats *_Nonnull g_c, const Group_c *_Nonnull g, const uint8_t *_Nonnull data, uint16_t length, int receiver)
{
uint16_t sent = 0;
for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type != GROUPCHAT_CONNECTION_ONLINE) {
continue;
}
if ((int)i == receiver) {
continue;
}
if (send_packet_group_peer(g_c->fr_c, g->connections[i].number, PACKET_ID_MESSAGE_CONFERENCE,
g->connections[i].group_number, data, length)) {
++sent;
}
}
return sent;
}
/** @brief Send lossy message to all connections except receiver (if receiver isn't -1)
*
* NOTE: this function appends the group chat number to the data passed to it.
*
* @return number of messages sent.
*/
static unsigned int send_lossy_all_connections(const Group_Chats *_Nonnull g_c, const Group_c *_Nonnull g, const uint8_t *_Nonnull data, uint16_t length, int receiver)
{
unsigned int sent = 0;
unsigned int num_connected_closest = 0;
unsigned int connected_closest[DESIRED_CLOSEST] = {0};
for (unsigned int i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type != GROUPCHAT_CONNECTION_ONLINE) {
continue;
}
if ((int)i == receiver) {
continue;
}
if ((g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_CLOSEST) != 0) {
connected_closest[num_connected_closest] = i;
++num_connected_closest;
continue;
}
if (send_lossy_group_peer(g_c->fr_c, g->connections[i].number, PACKET_ID_LOSSY_CONFERENCE,
g->connections[i].group_number, data, length)) {
++sent;
}
}
if (num_connected_closest == 0) {
return sent;
}
unsigned int to_send[2] = {0, 0};
uint64_t comp_val_old[2] = {(uint64_t) -1, (uint64_t) -1};
for (unsigned int i = 0; i < num_connected_closest; ++i) {
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE] = {0};
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[connected_closest[i]].number);
const uint64_t comp_val = calculate_comp_value(g->real_pk, real_pk);
for (uint8_t j = 0; j < 2; ++j) {
if (j > 0 ? (comp_val > comp_val_old[j]) : (comp_val < comp_val_old[j])) {
to_send[j] = connected_closest[i];
comp_val_old[j] = comp_val;
}
}
}
for (uint8_t j = 0; j < 2; ++j) {
if (j > 0 && to_send[1] == to_send[0]) {
break;
}
if (send_lossy_group_peer(g_c->fr_c, g->connections[to_send[j]].number, PACKET_ID_LOSSY_CONFERENCE,
g->connections[to_send[j]].group_number, data, length)) {
++sent;
}
}
return sent;
}
/** @brief Send data of len with message_id to groupnumber.
*
* @return number of peers it was sent to on success.
* @retval -1 if groupnumber is invalid.
* @retval -2 if message is too long.
* @retval -3 if we are not connected to the group.
* @retval -4 if message failed to send.
*/
static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *data,
uint16_t len)
{
assert(len == 0 || data != nullptr);
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (len > MAX_GROUP_MESSAGE_DATA_LEN) {
return -2;
}
if (g->status != GROUPCHAT_STATUS_CONNECTED || count_connected(g) == 0) {
return -3;
}
const uint16_t packet_size = sizeof(uint16_t) + sizeof(uint32_t) + 1 + len;
VLA(uint8_t, packet, packet_size);
const uint16_t peer_num = net_htons(g->peer_number);
memcpy(packet, &peer_num, sizeof(peer_num));
++g->message_number;
if (g->message_number == 0) {
++g->message_number;
}
const uint32_t message_num = net_htonl(g->message_number);
memcpy(packet + sizeof(uint16_t), &message_num, sizeof(message_num));
packet[sizeof(uint16_t) + sizeof(uint32_t)] = message_id;
if (len != 0) {
memcpy(packet + sizeof(uint16_t) + sizeof(uint32_t) + 1, data, len);
}
const unsigned int ret = send_message_all_connections(g_c, g, packet, packet_size, -1);
if (ret == 0) {
return -4;
}
return ret;
}
/** @brief send a group message
* @retval 0 on success
* @see send_message_group for error codes.
*/
int group_message_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *message, uint16_t length)
{
const int ret = send_message_group(g_c, groupnumber, PACKET_ID_MESSAGE, message, length);
if (ret > 0) {
return 0;
}
return ret;
}
/** @brief send a group action
* @retval 0 on success
* @see send_message_group for error codes.
*/
int group_action_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *action, uint16_t length)
{
const int ret = send_message_group(g_c, groupnumber, PACKET_ID_ACTION, action, length);
if (ret > 0) {
return 0;
}
return ret;
}
/** @brief High level function to send custom lossy packets.
*
* @retval -1 on failure.
* @retval 0 on success.
*/
int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length)
{
// TODO(irungentoo): length check here?
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const uint16_t packet_size = sizeof(uint16_t) * 2 + length;
VLA(uint8_t, packet, packet_size);
const uint16_t peer_number = net_htons(g->peer_number);
memcpy(packet, &peer_number, sizeof(uint16_t));
const uint16_t message_num = net_htons(g->lossy_message_number);
memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t));
memcpy(packet + sizeof(uint16_t) * 2, data, length);
if (send_lossy_all_connections(g_c, g, packet, packet_size, -1) == 0) {
return -1;
}
++g->lossy_message_number;
return 0;
}
static Message_Info *find_message_slot_or_reject(uint32_t message_number, uint8_t message_id, Group_Peer *_Nonnull peer)
{
const bool ignore_older = message_id == GROUP_MESSAGE_NAME_ID || message_id == GROUP_MESSAGE_TITLE_ID;
Message_Info *i;
for (i = peer->last_message_infos; i < peer->last_message_infos + peer->num_last_message_infos; ++i) {
if (message_number - (i->message_number + 1) <= ((uint32_t)1 << 31)) {
break;
}
if (message_number == i->message_number) {
return nullptr;
}
if (ignore_older && message_id == i->message_id) {
return nullptr;
}
}
return i;
}
/** @brief Stores message info in `peer->last_message_infos`.
*
* @retval true if message should be processed.
* @retval false otherwise.
*/
static bool check_message_info(uint32_t message_number, uint8_t message_id, Group_Peer *_Nonnull peer)
{
Message_Info *const i = find_message_slot_or_reject(message_number, message_id, peer);
if (i == nullptr) {
return false;
}
if (i == peer->last_message_infos + MAX_LAST_MESSAGE_INFOS) {
return false;
}
if (peer->num_last_message_infos < MAX_LAST_MESSAGE_INFOS) {
++peer->num_last_message_infos;
}
memmove(i + 1, i, (&peer->last_message_infos[peer->num_last_message_infos - 1] - i) * sizeof(Message_Info));
i->message_number = message_number;
i->message_id = message_id;
return true;
}
static void handle_message_packet_group(Group_Chats *_Nonnull g_c, uint32_t groupnumber, const uint8_t *_Nonnull data, uint16_t length,
uint32_t connection_index, void *_Nullable userdata)
{
if (length < sizeof(uint16_t) + sizeof(uint32_t) + 1) {
return;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return;
}
uint16_t peer_number;
memcpy(&peer_number, data, sizeof(uint16_t));
peer_number = net_ntohs(peer_number);
uint32_t message_number;
memcpy(&message_number, data + sizeof(uint16_t), sizeof(message_number));
message_number = net_ntohl(message_number);
const uint8_t message_id = data[sizeof(uint16_t) + sizeof(message_number)];
const uint8_t *msg_data = data + sizeof(uint16_t) + sizeof(message_number) + 1;
const uint16_t msg_data_len = length - (sizeof(uint16_t) + sizeof(message_number) + 1);
const bool ignore_frozen = message_id == GROUP_MESSAGE_FREEZE_PEER_ID;
const int index = ignore_frozen ? get_peer_index(g, peer_number)
: note_peer_active(g_c, groupnumber, peer_number, userdata);
if (index == -1) {
if (ignore_frozen) {
return;
}
if (g->connections[connection_index].type != GROUPCHAT_CONNECTION_ONLINE) {
return;
}
/* If we don't know the peer this packet came from, then we query the
* list of peers from the relaying peer.
* (They wouldn't have relayed it if they didn't know the peer.) */
send_peer_query(g_c, g->connections[connection_index].number, g->connections[connection_index].group_number);
return;
}
if (g->num_introducer_connections > 0 && count_connected(g) > DESIRED_CLOSEST) {
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE
|| (g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_INTRODUCER) == 0
|| i == connection_index) {
continue;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[i].number);
if (pk_equal(g->group[index].real_pk, real_pk)) {
/* Received message from peer relayed via another peer, so
* the introduction was successful */
remove_connection_reason(g_c, g, i, GROUPCHAT_CONNECTION_REASON_INTRODUCER);
}
}
}
if (!check_message_info(message_number, message_id, &g->group[index])) {
return;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[connection_index].number);
const bool direct_from_sender = pk_equal(g->group[index].real_pk, real_pk);
switch (message_id) {
case GROUP_MESSAGE_PING_ID: {
break;
}
case GROUP_MESSAGE_NEW_PEER_ID: {
if (msg_data_len != GROUP_MESSAGE_NEW_PEER_LENGTH) {
return;
}
uint16_t new_peer_number;
memcpy(&new_peer_number, msg_data, sizeof(uint16_t));
new_peer_number = net_ntohs(new_peer_number);
addpeer(g_c, groupnumber, msg_data + sizeof(uint16_t), msg_data + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE,
new_peer_number, userdata, true, true);
break;
}
case GROUP_MESSAGE_KILL_PEER_ID:
case GROUP_MESSAGE_FREEZE_PEER_ID: {
if (msg_data_len != GROUP_MESSAGE_KILL_PEER_LENGTH) {
return;
}
uint16_t kill_peer_number;
memcpy(&kill_peer_number, msg_data, sizeof(uint16_t));
kill_peer_number = net_ntohs(kill_peer_number);
if (peer_number == kill_peer_number) {
if (message_id == GROUP_MESSAGE_KILL_PEER_ID) {
delpeer(g_c, groupnumber, index, userdata);
} else {
freeze_peer(g_c, groupnumber, index, userdata);
}
} else {
return;
// TODO(irungentoo):
}
break;
}
case GROUP_MESSAGE_NAME_ID: {
if (!setnick(g_c, groupnumber, index, msg_data, msg_data_len, userdata, true)) {
return;
}
break;
}
case GROUP_MESSAGE_TITLE_ID: {
if (!settitle(g_c, groupnumber, index, msg_data, msg_data_len, userdata)) {
return;
}
break;
}
case PACKET_ID_MESSAGE: {
if (msg_data_len == 0) {
return;
}
VLA(uint8_t, newmsg, msg_data_len + 1);
memcpy(newmsg, msg_data, msg_data_len);
newmsg[msg_data_len] = 0;
// TODO(irungentoo):
if (g_c->message_callback != nullptr) {
g_c->message_callback(g_c->m, groupnumber, index, 0, newmsg, msg_data_len, userdata);
}
break;
}
case PACKET_ID_ACTION: {
if (msg_data_len == 0) {
return;
}
VLA(uint8_t, newmsg, msg_data_len + 1);
memcpy(newmsg, msg_data, msg_data_len);
newmsg[msg_data_len] = 0;
// TODO(irungentoo):
if (g_c->message_callback != nullptr) {
g_c->message_callback(g_c->m, groupnumber, index, 1, newmsg, msg_data_len, userdata);
}
break;
}
default: {
return;
}
}
/* If the packet was received from the peer who sent the message, relay it
* back. When the sender only has one group connection (e.g. because there
* are only two peers in the group), this is the only way for them to
* receive their own message. */
send_message_all_connections(g_c, g, data, length, direct_from_sender ? -1 : connection_index);
}
static int g_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
{
Group_Chats *g_c = (Group_Chats *)object;
if (length < 1 + sizeof(uint16_t) + 1) {
return -1;
}
if (data[0] == PACKET_ID_ONLINE_PACKET) {
return handle_packet_online(g_c, friendcon_id, data + 1, length - 1);
}
if (data[0] == PACKET_ID_REJOIN_CONFERENCE) {
return handle_packet_rejoin(g_c, friendcon_id, data + 1, length - 1, userdata);
}
uint16_t groupnumber;
memcpy(&groupnumber, data + 1, sizeof(uint16_t));
groupnumber = net_ntohs(groupnumber);
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
const int index = friend_in_connections(g, friendcon_id);
if (index == -1) {
return -1;
}
if (data[0] == PACKET_ID_DIRECT_CONFERENCE) {
handle_direct_packet(g_c, groupnumber, data + 1 + sizeof(uint16_t),
length - (1 + sizeof(uint16_t)), index, userdata);
return 0;
}
if (data[0] == PACKET_ID_MESSAGE_CONFERENCE) {
handle_message_packet_group(g_c, groupnumber, data + 1 + sizeof(uint16_t),
length - (1 + sizeof(uint16_t)), index, userdata);
return 0;
}
return -1;
}
/** @brief Did we already receive the lossy packet or not.
*
* @retval -1 on failure.
* @retval 0 if packet was not received.
* @retval 1 if packet was received.
*
* TODO(irungentoo): test this
*/
static int lossy_packet_not_received(const Group_c *_Nonnull g, int peer_index, uint16_t message_number)
{
if (peer_index == -1) {
return -1;
}
if (g->group[peer_index].bottom_lossy_number == g->group[peer_index].top_lossy_number) {
g->group[peer_index].top_lossy_number = message_number;
g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1;
g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1;
return 0;
}
if ((uint16_t)(message_number - g->group[peer_index].bottom_lossy_number) < MAX_LOSSY_COUNT) {
if (g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] != 0) {
return 1;
}
g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1;
return 0;
}
if ((uint16_t)(message_number - g->group[peer_index].bottom_lossy_number) > (1 << 15)) {
return -1;
}
const uint16_t top_distance = message_number - g->group[peer_index].top_lossy_number;
if (top_distance >= MAX_LOSSY_COUNT) {
crypto_memzero(g->group[peer_index].recv_lossy, sizeof(g->group[peer_index].recv_lossy));
} else { // top_distance < MAX_LOSSY_COUNT
for (unsigned int i = g->group[peer_index].bottom_lossy_number;
i != g->group[peer_index].bottom_lossy_number + top_distance;
++i) {
g->group[peer_index].recv_lossy[i % MAX_LOSSY_COUNT] = 0;
}
}
g->group[peer_index].top_lossy_number = message_number;
g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1;
g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1;
return 0;
}
/** Does this group type make use of lossy packets? */
static bool type_uses_lossy(uint8_t type)
{
return type == GROUPCHAT_TYPE_AV;
}
static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
{
const Group_Chats *g_c = (const Group_Chats *)object;
if (data[0] != PACKET_ID_LOSSY_CONFERENCE) {
return -1;
}
if (length < 1 + sizeof(uint16_t) * 3 + 1) {
return -1;
}
uint16_t groupnumber;
uint16_t peer_number;
uint16_t message_number;
memcpy(&groupnumber, data + 1, sizeof(uint16_t));
memcpy(&peer_number, data + 1 + sizeof(uint16_t), sizeof(uint16_t));
memcpy(&message_number, data + 1 + sizeof(uint16_t) * 2, sizeof(uint16_t));
groupnumber = net_ntohs(groupnumber);
peer_number = net_ntohs(peer_number);
message_number = net_ntohs(message_number);
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (!type_uses_lossy(g->type)) {
return -1;
}
const int index = friend_in_connections(g, friendcon_id);
if (index == -1) {
return -1;
}
if (peer_number == g->peer_number) {
return -1;
}
const int peer_index = get_peer_index(g, peer_number);
if (peer_index == -1) {
return -1;
}
if (lossy_packet_not_received(g, peer_index, message_number) != 0) {
return -1;
}
const uint8_t *lossy_data = data + 1 + sizeof(uint16_t) * 3;
uint16_t lossy_length = length - (1 + sizeof(uint16_t) * 3);
const uint8_t message_id = lossy_data[0];
++lossy_data;
--lossy_length;
send_lossy_all_connections(g_c, g, data + 1 + sizeof(uint16_t), length - (1 + sizeof(uint16_t)), index);
if (g_c->lossy_packethandlers[message_id] == nullptr) {
return -1;
}
if (g_c->lossy_packethandlers[message_id](g->object, groupnumber, peer_index, g->group[peer_index].object,
lossy_data, lossy_length) == -1) {
return -1;
}
return 0;
}
/** @brief Set the object that is tied to the group chat.
*
* @retval 0 on success.
* @retval -1 on failure
*/
int group_set_object(const Group_Chats *g_c, uint32_t groupnumber, void *object)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
g->object = object;
return 0;
}
/** @brief Set the object that is tied to the group peer.
*
* @retval 0 on success.
* @retval -1 on failure
*/
int group_peer_set_object(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, void *object)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return -1;
}
if (peernumber >= g->numpeers) {
return -1;
}
g->group[peernumber].object = object;
return 0;
}
/** @brief Return the object tied to the group chat previously set by group_set_object.
*
* @retval NULL on failure.
* @return object on success.
*/
void *group_get_object(const Group_Chats *g_c, uint32_t groupnumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return nullptr;
}
return g->object;
}
/** @brief Return the object tied to the group chat peer previously set by group_peer_set_object.
*
* @retval NULL on failure.
* @return object on success.
*/
void *group_peer_get_object(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber)
{
const Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return nullptr;
}
if (peernumber >= g->numpeers) {
return nullptr;
}
return g->group[peernumber].object;
}
/** Interval in seconds to send ping messages */
#define GROUP_PING_INTERVAL 20
static bool ping_groupchat(const Group_Chats *g_c, uint32_t groupnumber)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
if (mono_time_is_timeout(g_c->mono_time, g->last_sent_ping, GROUP_PING_INTERVAL)) {
if (group_ping_send(g_c, groupnumber)) {
g->last_sent_ping = mono_time_get(g_c->mono_time);
}
}
return true;
}
/** Seconds of inactivity after which to freeze a peer */
#define FREEZE_TIMEOUT (GROUP_PING_INTERVAL * 3)
static bool groupchat_freeze_timedout(Group_Chats *_Nonnull g_c, uint32_t groupnumber, void *_Nullable userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (g == nullptr) {
return false;
}
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->group[i].peer_number == g->peer_number) {
continue;
}
if (mono_time_is_timeout(g_c->mono_time, g->group[i].last_active, FREEZE_TIMEOUT)) {
freeze_peer(g_c, groupnumber, i, userdata);
}
}
if (g->numpeers <= 1) {
g->title_fresh = false;
}
return true;
}
/** Push non-empty slots to start. */
static void squash_connections(Group_c *_Nonnull g)
{
uint16_t num_connected = 0;
for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type != GROUPCHAT_CONNECTION_NONE) {
g->connections[num_connected] = g->connections[i];
++num_connected;
}
}
for (uint16_t i = num_connected; i < MAX_GROUP_CONNECTIONS; ++i) {
g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
}
}
#define MIN_EMPTY_CONNECTIONS (1 + MAX_GROUP_CONNECTIONS / 10)
static uint16_t empty_connection_count(const Group_c *_Nonnull g)
{
uint16_t to_clear = MIN_EMPTY_CONNECTIONS;
for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type == GROUPCHAT_CONNECTION_NONE) {
--to_clear;
if (to_clear == 0) {
break;
}
}
}
return to_clear;
}
/**
* @brief Remove old connections as necessary to ensure we have space for new
* connections.
*
* This invalidates connections array indices (which is
* why we do this periodically rather than on adding a connection).
*/
static void clean_connections(Group_Chats *_Nonnull g_c, Group_c *_Nonnull g)
{
for (uint16_t to_clear = empty_connection_count(g); to_clear > 0; --to_clear) {
// Remove a connection. Prefer non-closest connections, and given
// that prefer non-online connections, and given that prefer earlier
// slots.
uint16_t i = 0;
while (i < MAX_GROUP_CONNECTIONS
&& (g->connections[i].type != GROUPCHAT_CONNECTION_CONNECTING
|| (g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_CLOSEST) != 0)) {
++i;
}
if (i == MAX_GROUP_CONNECTIONS) {
i = 0;
while (i < MAX_GROUP_CONNECTIONS - to_clear
&& (g->connections[i].type != GROUPCHAT_CONNECTION_ONLINE
|| (g->connections[i].reasons & GROUPCHAT_CONNECTION_REASON_CLOSEST) != 0)) {
++i;
}
}
if (g->connections[i].type != GROUPCHAT_CONNECTION_NONE) {
remove_connection(g_c, g, i);
}
}
squash_connections(g);
}
/** Send current name (set in messenger) to all online groups. */
void send_name_all_groups(const Group_Chats *g_c)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (g == nullptr) {
continue;
}
if (g->status == GROUPCHAT_STATUS_CONNECTED) {
group_name_send(g_c, i, g_c->m->name, g_c->m->name_length);
g->need_send_name = false;
}
}
}
#define SAVED_PEER_SIZE_CONSTANT (2 * CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint16_t) + sizeof(uint64_t) + 1)
static uint32_t saved_peer_size(const Group_Peer *_Nonnull peer)
{
return SAVED_PEER_SIZE_CONSTANT + peer->nick_len;
}
static uint8_t *save_peer(const Group_Peer *_Nonnull peer, uint8_t *_Nonnull data)
{
memcpy(data, peer->real_pk, CRYPTO_PUBLIC_KEY_SIZE);
data += CRYPTO_PUBLIC_KEY_SIZE;
memcpy(data, peer->temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
data += CRYPTO_PUBLIC_KEY_SIZE;
host_to_lendian_bytes16(data, peer->peer_number);
data += sizeof(uint16_t);
host_to_lendian_bytes64(data, peer->last_active);
data += sizeof(uint64_t);
// TODO(iphydf): This looks broken: nick_len can be > 255.
*data = peer->nick_len;
++data;
memcpy(data, peer->nick, peer->nick_len);
data += peer->nick_len;
return data;
}
#define SAVED_CONF_SIZE_CONSTANT (1 + GROUP_ID_LENGTH + sizeof(uint32_t) \
+ sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t) + 1)
static uint32_t saved_conf_size(const Group_c *_Nonnull g)
{
uint32_t len = SAVED_CONF_SIZE_CONSTANT + g->title_len;
for (uint32_t j = 0; j < g->numpeers + g->numfrozen; ++j) {
const Group_Peer *peer = (j < g->numpeers) ? &g->group[j] : &g->frozen[j - g->numpeers];
if (pk_equal(peer->real_pk, g->real_pk)) {
continue;
}
len += saved_peer_size(peer);
}
return len;
}
/**
* Save a future message number. The save will remain valid until we have sent
* this many more messages.
*/
#define SAVE_OFFSET_MESSAGE_NUMBER (1 << 16)
#define SAVE_OFFSET_LOSSY_MESSAGE_NUMBER (1 << 13)
static uint8_t *save_conf(const Group_c *_Nonnull g, uint8_t *_Nonnull data)
{
*data = g->type;
++data;
memcpy(data, g->id, GROUP_ID_LENGTH);
data += GROUP_ID_LENGTH;
host_to_lendian_bytes32(data, g->message_number + SAVE_OFFSET_MESSAGE_NUMBER);
data += sizeof(uint32_t);
host_to_lendian_bytes16(data, g->lossy_message_number + SAVE_OFFSET_LOSSY_MESSAGE_NUMBER);
data += sizeof(uint16_t);
host_to_lendian_bytes16(data, g->peer_number);
data += sizeof(uint16_t);
uint8_t *const numsaved_location = data;
data += sizeof(uint32_t);
*data = g->title_len;
++data;
memcpy(data, g->title, g->title_len);
data += g->title_len;
uint32_t numsaved = 0;
for (uint32_t j = 0; j < g->numpeers + g->numfrozen; ++j) {
const Group_Peer *peer = (j < g->numpeers) ? &g->group[j] : &g->frozen[j - g->numpeers];
if (pk_equal(peer->real_pk, g->real_pk)) {
continue;
}
data = save_peer(peer, data);
++numsaved;
}
host_to_lendian_bytes32(numsaved_location, numsaved);
return data;
}
static uint32_t conferences_section_size(const Group_Chats *_Nonnull g_c)
{
uint32_t len = 0;
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
const Group_c *g = get_group_c(g_c, i);
if (g == nullptr || g->status != GROUPCHAT_STATUS_CONNECTED) {
continue;
}
len += saved_conf_size(g);
}
return len;
}
uint32_t conferences_size(const Group_Chats *g_c)
{
return 2 * sizeof(uint32_t) + conferences_section_size(g_c);
}
uint8_t *conferences_save(const Group_Chats *g_c, uint8_t *data)
{
const uint32_t len = conferences_section_size(g_c);
data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_CONFERENCES);
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
const Group_c *g = get_group_c(g_c, i);
if (g == nullptr || g->status != GROUPCHAT_STATUS_CONNECTED) {
continue;
}
data = save_conf(g, data);
}
return data;
}
/**
* @brief load_group Load a Group section from a save file
* @param g Group to load
* @param g_c Reference to all groupchats, need for utility functions
* @param data Start of the data to deserialze
* @param length Length of data
* @return 0 on error, number of consumed bytes otherwise
*/
static uint32_t load_group(Group_c *_Nonnull g, const Group_Chats *_Nonnull g_c, const uint8_t *_Nonnull data, uint32_t length)
{
const uint8_t *init_data = data;
// Initialize to default values so we can unconditionally free in case of an error
setup_conference(g);
g->type = *data;
++data;
memcpy(g->id, data, GROUP_ID_LENGTH);
data += GROUP_ID_LENGTH;
lendian_bytes_to_host32(&g->message_number, data);
data += sizeof(uint32_t);
lendian_bytes_to_host16(&g->lossy_message_number, data);
data += sizeof(uint16_t);
lendian_bytes_to_host16(&g->peer_number, data);
data += sizeof(uint16_t);
lendian_bytes_to_host32(&g->numfrozen, data);
data += sizeof(uint32_t);
g->title_len = *data;
if (g->title_len > MAX_NAME_LENGTH) {
return 0;
}
++data;
assert((data - init_data) < UINT32_MAX);
if (length < (uint32_t)(data - init_data) + g->title_len) {
return 0;
}
memcpy(g->title, data, g->title_len);
data += g->title_len;
for (uint32_t j = 0; j < g->numfrozen; ++j) {
assert((data - init_data) < UINT32_MAX);
if (length < (uint32_t)(data - init_data) + SAVED_PEER_SIZE_CONSTANT) {
return 0;
}
// This is inefficient, but allows us to check data consistency before allocating memory
Group_Peer *tmp_frozen = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, j + 1, sizeof(Group_Peer));
if (tmp_frozen == nullptr) {
// Memory allocation failure
return 0;
}
g->frozen = tmp_frozen;
Group_Peer *peer = &g->frozen[j];
*peer = empty_group_peer;
pk_copy(peer->real_pk, data);
data += CRYPTO_PUBLIC_KEY_SIZE;
pk_copy(peer->temp_pk, data);
data += CRYPTO_PUBLIC_KEY_SIZE;
lendian_bytes_to_host16(&peer->peer_number, data);
data += sizeof(uint16_t);
lendian_bytes_to_host64(&peer->last_active, data);
data += sizeof(uint64_t);
peer->nick_len = *data;
if (peer->nick_len > MAX_NAME_LENGTH) {
return 0;
}
++data;
assert((data - init_data) < UINT32_MAX);
if (length < (uint32_t)(data - init_data) + peer->nick_len) {
return 0;
}
memcpy(peer->nick, data, peer->nick_len);
data += peer->nick_len;
// NOTE: this relies on friends being loaded before conferences.
peer->is_friend = getfriend_id(g_c->m, peer->real_pk) != -1;
}
if (g->numfrozen > g->maxfrozen) {
g->maxfrozen = g->numfrozen;
}
g->status = GROUPCHAT_STATUS_CONNECTED;
pk_copy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto));
assert((data - init_data) < UINT32_MAX);
return (uint32_t)(data - init_data);
}
static State_Load_Status load_conferences_helper(Group_Chats *_Nonnull g_c, const uint8_t *_Nonnull data, uint32_t length)
{
const uint8_t *init_data = data;
while (length >= (uint32_t)(data - init_data) + SAVED_CONF_SIZE_CONSTANT) {
const int groupnumber = create_group_chat(g_c);
// Helpful for testing
assert(groupnumber != -1);
if (groupnumber == -1) {
// If this fails there's a serious problem, don't bother with cleanup
LOGGER_ERROR(g_c->m->log, "conference creation failed");
return STATE_LOAD_STATUS_ERROR;
}
Group_c *g = &g_c->chats[groupnumber];
const uint32_t consumed = load_group(g, g_c, data, length - (uint32_t)(data - init_data));
if (consumed == 0) {
// remove partially loaded stuff, wipe_group_chat must be able to wipe a partially loaded group
const bool ret = wipe_group_chat(g_c, groupnumber);
// HACK: suppress unused variable warning
if (!ret) {
// wipe_group_chat(...) must be able to wipe partially allocated groups
assert(ret);
}
LOGGER_ERROR(g_c->m->log, "conference loading failed");
return STATE_LOAD_STATUS_ERROR;
}
data += consumed;
const int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), g->peer_number,
nullptr, true, false);
if (peer_index == -1) {
LOGGER_ERROR(g_c->m->log, "adding peer %d failed", g->peer_number);
return STATE_LOAD_STATUS_ERROR;
}
setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length, nullptr, false);
}
return STATE_LOAD_STATUS_CONTINUE;
}
static State_Load_Status load_conferences(Group_Chats *_Nonnull g_c, const uint8_t *_Nonnull data, uint32_t length)
{
const State_Load_Status res = load_conferences_helper(g_c, data, length);
if (res == STATE_LOAD_STATUS_CONTINUE) {
return res;
}
// Loading failed, cleanup all Group_c
// save locally, because wipe_group_chat(...) modifies it
const uint16_t num_groups = g_c->num_chats;
for (uint16_t i = 0; i < num_groups; ++i) {
wipe_group_chat(g_c, i);
}
return res;
}
bool conferences_load_state_section(Group_Chats *g_c, const uint8_t *data, uint32_t length, uint16_t type,
State_Load_Status *status)
{
if (type != STATE_TYPE_CONFERENCES) {
return false;
}
*status = load_conferences(g_c, data, length);
return true;
}
/** Create new groupchat instance. */
Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messenger *m)
{
if (m == nullptr) {
return nullptr;
}
Group_Chats *temp = (Group_Chats *)mem_alloc(mem, sizeof(Group_Chats));
if (temp == nullptr) {
return nullptr;
}
temp->mem = mem;
temp->mono_time = mono_time;
temp->m = m;
temp->fr_c = m->fr_c;
m_callback_conference_invite(m, &handle_friend_invite_packet);
set_global_status_callback(m->fr_c, &g_handle_any_status, temp);
return temp;
}
/** main groupchats loop. */
void do_groupchats(Group_Chats *g_c, void *userdata)
{
if (g_c == nullptr) {
return;
}
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (g == nullptr) {
continue;
}
if (g->status == GROUPCHAT_STATUS_CONNECTED) {
connect_to_closest(g_c, i, userdata);
ping_groupchat(g_c, i);
groupchat_freeze_timedout(g_c, i, userdata);
clean_connections(g_c, g);
if (g->need_send_name) {
group_name_send(g_c, i, g_c->m->name, g_c->m->name_length);
g->need_send_name = false;
}
}
}
// TODO(irungentoo):
}
/** Free everything related with group chats. */
void kill_groupchats(Group_Chats *g_c)
{
if (g_c == nullptr) {
return;
}
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
del_groupchat(g_c, i, false);
}
m_callback_conference_invite(g_c->m, nullptr);
set_global_status_callback(g_c->m->fr_c, nullptr, nullptr);
g_c->m->conferences_object = nullptr;
mem_delete(g_c->mem, g_c);
}
/**
* @brief Return the number of chats in the instance m.
*
* You should use this to determine how much memory to allocate
* for copy_chatlist.
*/
uint32_t count_chatlist(const Group_Chats *g_c)
{
uint32_t ret = 0;
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status != GROUPCHAT_STATUS_NONE) {
++ret;
}
}
return ret;
}
/** @brief Copy a list of valid chat IDs into the array out_list.
*
* If out_list is NULL, returns 0.
* Otherwise, returns the number of elements copied.
* If the array was too small, the contents
* of out_list will be truncated to list_size.
*/
uint32_t copy_chatlist(const Group_Chats *g_c, uint32_t *out_list, uint32_t list_size)
{
if (out_list == nullptr) {
return 0;
}
if (g_c->num_chats == 0) {
return 0;
}
uint32_t ret = 0;
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (ret >= list_size) {
break; /* Abandon ship */
}
if (g_c->chats[i].status > GROUPCHAT_STATUS_NONE) {
out_list[ret] = i;
++ret;
}
}
return ret;
}
|