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 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276
|
/*
* libpri: An implementation of Primary Rate ISDN
*
* Written by Mark Spencer <markster@digium.com>
*
* Copyright (C) 2001-2005, Digium, Inc.
* All Rights Reserved.
*/
/*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*
* In addition, when this program is distributed with Asterisk in
* any form that would qualify as a 'combined work' or as a
* 'derivative work' (but not mere aggregation), you can redistribute
* and/or modify the combination under the terms of the license
* provided with that copy of Asterisk, instead of the license
* terms granted here.
*/
#include "compat.h"
#include "libpri.h"
#include "pri_internal.h"
#include "pri_facility.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
enum mandatory_ie_status {
MAND_STATUS_OK,
MAND_STATUS_CONTENT_ERROR,
MAND_STATUS_MISSING,
};
#define MAX_MAND_IES 10
struct msgtype {
int msgnum;
char *name;
int mandies[MAX_MAND_IES];
};
static struct msgtype msgs[] = {
/* Call establishment messages */
{ Q931_ALERTING, "ALERTING", { Q931_CHANNEL_IDENT } },
{ Q931_CALL_PROCEEDING, "CALL PROCEEDING", { Q931_CHANNEL_IDENT } },
{ Q931_CONNECT, "CONNECT", { Q931_CHANNEL_IDENT } },
{ Q931_CONNECT_ACKNOWLEDGE, "CONNECT ACKNOWLEDGE" },
/*
* Very early in libpri SVN history (SVN -r154) a change was explicitly
* made to not care about a missing Progress Indicator ie. There is no
* explanation in the commit message why this was done even though
* Q.931 and ECMA-143 clearly show that the ie is mandatory for a
* PROGRESS message.
*
* Hazzarding a guess, I think it was because of the
* ALERTING_NO_PROGRESS support for Mitel switches. (SVN -r44)
*/
#ifdef ALERTING_NO_PROGRESS
{ Q931_PROGRESS, "PROGRESS" },
#else
{ Q931_PROGRESS, "PROGRESS", { Q931_PROGRESS_INDICATOR } },
#endif
{ Q931_SETUP, "SETUP", { Q931_BEARER_CAPABILITY, Q931_CHANNEL_IDENT } },
{ Q931_SETUP_ACKNOWLEDGE, "SETUP ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
/* Call disestablishment messages */
{ Q931_DISCONNECT, "DISCONNECT", { Q931_CAUSE } },
{ Q931_RELEASE, "RELEASE" },
{ Q931_RELEASE_COMPLETE, "RELEASE COMPLETE" },
{ Q931_RESTART, "RESTART", { Q931_RESTART_INDICATOR } },
{ Q931_RESTART_ACKNOWLEDGE, "RESTART ACKNOWLEDGE", { Q931_RESTART_INDICATOR } },
/* Miscellaneous */
{ Q931_STATUS, "STATUS", { Q931_CAUSE, Q931_IE_CALL_STATE } },
{ Q931_STATUS_ENQUIRY, "STATUS ENQUIRY" },
{ Q931_USER_INFORMATION, "USER_INFORMATION" },
{ Q931_SEGMENT, "SEGMENT" },
{ Q931_CONGESTION_CONTROL, "CONGESTION CONTROL" },
{ Q931_INFORMATION, "INFORMATION" },
{ Q931_FACILITY, "FACILITY" },
{ Q931_REGISTER, "REGISTER" },
{ Q931_NOTIFY, "NOTIFY", { Q931_IE_NOTIFY_IND } },
/* Call Management */
{ Q931_HOLD, "HOLD" },
{ Q931_HOLD_ACKNOWLEDGE, "HOLD ACKNOWLEDGE" },
{ Q931_HOLD_REJECT, "HOLD REJECT", { Q931_CAUSE } },
{ Q931_RETRIEVE, "RETRIEVE" },
{ Q931_RETRIEVE_ACKNOWLEDGE, "RETRIEVE ACKNOWLEDGE" },
{ Q931_RETRIEVE_REJECT, "RETRIEVE REJECT", { Q931_CAUSE } },
{ Q931_RESUME, "RESUME" },
{ Q931_RESUME_ACKNOWLEDGE, "RESUME ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
{ Q931_RESUME_REJECT, "RESUME REJECT", { Q931_CAUSE } },
{ Q931_SUSPEND, "SUSPEND" },
{ Q931_SUSPEND_ACKNOWLEDGE, "SUSPEND ACKNOWLEDGE" },
{ Q931_SUSPEND_REJECT, "SUSPEND REJECT" },
{ Q931_ANY_MESSAGE, "ANY MESSAGE" },
};
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, enum mandatory_ie_status mand_status);
static void nt_ptmp_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, int *allow_event, int *allow_posthandle);
struct msgtype att_maintenance_msgs[] = {
{ ATT_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
{ ATT_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
};
struct msgtype national_maintenance_msgs[] = {
{ NATIONAL_SERVICE, "SERVICE", { Q931_CHANNEL_IDENT } },
{ NATIONAL_SERVICE_ACKNOWLEDGE, "SERVICE ACKNOWLEDGE", { Q931_CHANNEL_IDENT } },
};
static int post_handle_maintenance_message(struct pri *ctrl, int protodisc, struct q931_mh *mh, struct q931_call *c);
static struct msgtype causes[] = {
{ PRI_CAUSE_UNALLOCATED, "Unallocated (unassigned) number" },
{ PRI_CAUSE_NO_ROUTE_TRANSIT_NET, "No route to specified transmit network" },
{ PRI_CAUSE_NO_ROUTE_DESTINATION, "No route to destination" },
{ PRI_CAUSE_CHANNEL_UNACCEPTABLE, "Channel unacceptable" },
{ PRI_CAUSE_CALL_AWARDED_DELIVERED, "Call awarded and being delivered in an established channel" },
{ PRI_CAUSE_NORMAL_CLEARING, "Normal Clearing" },
{ PRI_CAUSE_USER_BUSY, "User busy" },
{ PRI_CAUSE_NO_USER_RESPONSE, "No user responding" },
{ PRI_CAUSE_NO_ANSWER, "User alerting, no answer" },
{ PRI_CAUSE_CALL_REJECTED, "Call Rejected" },
{ PRI_CAUSE_NUMBER_CHANGED, "Number changed" },
{ PRI_CAUSE_NONSELECTED_USER_CLEARING, "Non-selected user clearing" },
{ PRI_CAUSE_DESTINATION_OUT_OF_ORDER, "Destination out of order" },
{ PRI_CAUSE_INVALID_NUMBER_FORMAT, "Invalid number format" },
{ PRI_CAUSE_FACILITY_REJECTED, "Facility rejected" },
{ PRI_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "Response to STATus ENQuiry" },
{ PRI_CAUSE_NORMAL_UNSPECIFIED, "Normal, unspecified" },
{ PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION, "Circuit/channel congestion" },
{ PRI_CAUSE_NETWORK_OUT_OF_ORDER, "Network out of order" },
{ PRI_CAUSE_NORMAL_TEMPORARY_FAILURE, "Temporary failure" },
{ PRI_CAUSE_SWITCH_CONGESTION, "Switching equipment congestion" },
{ PRI_CAUSE_ACCESS_INFO_DISCARDED, "Access information discarded" },
{ PRI_CAUSE_REQUESTED_CHAN_UNAVAIL, "Requested channel not available" },
{ PRI_CAUSE_PRE_EMPTED, "Pre-empted" },
{ PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED, "Resource unavailable, unspecified" },
{ PRI_CAUSE_FACILITY_NOT_SUBSCRIBED, "Facility not subscribed" },
{ PRI_CAUSE_OUTGOING_CALL_BARRED, "Outgoing call barred" },
{ PRI_CAUSE_INCOMING_CALL_BARRED, "Incoming call barred" },
{ PRI_CAUSE_BEARERCAPABILITY_NOTAUTH, "Bearer capability not authorized" },
{ PRI_CAUSE_BEARERCAPABILITY_NOTAVAIL, "Bearer capability not available" },
{ PRI_CAUSE_SERVICEOROPTION_NOTAVAIL, "Service or option not available, unspecified" },
{ PRI_CAUSE_BEARERCAPABILITY_NOTIMPL, "Bearer capability not implemented" },
{ PRI_CAUSE_CHAN_NOT_IMPLEMENTED, "Channel not implemented" },
{ PRI_CAUSE_FACILITY_NOT_IMPLEMENTED, "Facility not implemented" },
{ PRI_CAUSE_INVALID_CALL_REFERENCE, "Invalid call reference value" },
{ PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST, "Identified channel does not exist" },
{ PRI_CAUSE_INCOMPATIBLE_DESTINATION, "Incompatible destination" },
{ PRI_CAUSE_INVALID_MSG_UNSPECIFIED, "Invalid message unspecified" },
{ PRI_CAUSE_MANDATORY_IE_MISSING, "Mandatory information element is missing" },
{ PRI_CAUSE_MESSAGE_TYPE_NONEXIST, "Message type nonexist." },
{ PRI_CAUSE_WRONG_MESSAGE, "Wrong message" },
{ PRI_CAUSE_IE_NONEXIST, "Info. element nonexist or not implemented" },
{ PRI_CAUSE_INVALID_IE_CONTENTS, "Invalid information element contents" },
{ PRI_CAUSE_WRONG_CALL_STATE, "Message not compatible with call state" },
{ PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "Recover on timer expiry" },
{ PRI_CAUSE_MANDATORY_IE_LENGTH_ERROR, "Mandatory IE length error" },
{ PRI_CAUSE_PROTOCOL_ERROR, "Protocol error, unspecified" },
{ PRI_CAUSE_INTERWORKING, "Interworking, unspecified" },
};
static struct msgtype facilities[] = {
{ PRI_NSF_SID_PREFERRED, "CPN (SID) preferred" },
{ PRI_NSF_ANI_PREFERRED, "BN (ANI) preferred" },
{ PRI_NSF_SID_ONLY, "CPN (SID) only" },
{ PRI_NSF_ANI_ONLY, "BN (ANI) only" },
{ PRI_NSF_CALL_ASSOC_TSC, "Call Associated TSC" },
{ PRI_NSF_NOTIF_CATSC_CLEARING, "Notification of CATSC Clearing or Resource Unavailable" },
{ PRI_NSF_OPERATOR, "Operator" },
{ PRI_NSF_PCCO, "Pre-subscribed Common Carrier Operator (PCCO)" },
{ PRI_NSF_SDN, "SDN (including GSDN)" },
{ PRI_NSF_TOLL_FREE_MEGACOM, "Toll Free MEGACOM" },
{ PRI_NSF_MEGACOM, "MEGACOM" },
{ PRI_NSF_ACCUNET, "ACCUNET Switched Digital Service" },
{ PRI_NSF_LONG_DISTANCE_SERVICE, "Long Distance Service" },
{ PRI_NSF_INTERNATIONAL_TOLL_FREE, "International Toll Free Service" },
{ PRI_NSF_ATT_MULTIQUEST, "AT&T MultiQuest" },
{ PRI_NSF_CALL_REDIRECTION_SERVICE, "Call Redirection Service" }
};
#define FLAG_WHOLE_INTERFACE 0x01
#define FLAG_PREFERRED 0x02
#define FLAG_EXCLUSIVE 0x04
#define RESET_INDICATOR_CHANNEL 0
#define RESET_INDICATOR_DS1 6
#define RESET_INDICATOR_PRI 7
#define TRANS_MODE_64_CIRCUIT 0x10
#define TRANS_MODE_2x64_CIRCUIT 0x11
#define TRANS_MODE_384_CIRCUIT 0x13
#define TRANS_MODE_1536_CIRCUIT 0x15
#define TRANS_MODE_1920_CIRCUIT 0x17
#define TRANS_MODE_MULTIRATE 0x18
#define TRANS_MODE_PACKET 0x40
#define RATE_ADAPT_56K 0x0f
#define LAYER_2_LAPB 0x46
#define LAYER_3_X25 0x66
/* The 4ESS uses a different audio field */
#define PRI_TRANS_CAP_AUDIO_4ESS 0x08
/* Don't forget to update PRI_PROG_xxx at libpri.h */
#define Q931_PROG_CALL_NOT_E2E_ISDN 0x01
#define Q931_PROG_CALLED_NOT_ISDN 0x02
#define Q931_PROG_CALLER_NOT_ISDN 0x03
#define Q931_PROG_CALLER_RETURNED_TO_ISDN 0x04
#define Q931_PROG_INBAND_AVAILABLE 0x08
#define Q931_PROG_DELAY_AT_INTERF 0x0a
#define Q931_PROG_INTERWORKING_WITH_PUBLIC 0x10
#define Q931_PROG_INTERWORKING_NO_RELEASE 0x11
#define Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER 0x12
#define Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER 0x13
#define CODE_CCITT 0x0
#define CODE_INTERNATIONAL 0x1
#define CODE_NATIONAL 0x2
#define CODE_NETWORK_SPECIFIC 0x3
#define LOC_USER 0x0
#define LOC_PRIV_NET_LOCAL_USER 0x1
#define LOC_PUB_NET_LOCAL_USER 0x2
#define LOC_TRANSIT_NET 0x3
#define LOC_PUB_NET_REMOTE_USER 0x4
#define LOC_PRIV_NET_REMOTE_USER 0x5
#define LOC_INTERNATIONAL_NETWORK 0x7
#define LOC_NETWORK_BEYOND_INTERWORKING 0xa
static char *ie2str(int ie);
#define FUNC_DUMP(name) void (name)(int full_ie, struct pri *pri, q931_ie *ie, int len, char prefix)
#define FUNC_RECV(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len)
#define FUNC_SEND(name) int (name)(int full_ie, struct pri *pri, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
#if 1
/* Update call state with transition trace. */
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) \
do { \
if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) && (call)->ourcallstate != (newstate)) { \
pri_message((ctrl), \
DBGHEAD "%s %d enters state %d (%s). Hold state: %s\n", \
DBGINFO, ((call) == (call)->master_call) ? "Call" : "Subcall", \
(call)->cr, (newstate), q931_call_state_str(newstate), \
q931_hold_state_str((call)->master_call->hold_state)); \
} \
(call)->ourcallstate = (newstate); \
} while (0)
#else
/* Update call state with no trace. */
#define UPDATE_OURCALLSTATE(ctrl, call, newstate) (call)->ourcallstate = (newstate)
#endif
#if 1
/* Update hold state with transition trace. */
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) \
do { \
if (((ctrl)->debug & PRI_DEBUG_Q931_STATE) \
&& (master_call)->hold_state != (newstate)) { \
pri_message((ctrl), \
DBGHEAD "Call %d in state %d (%s) enters Hold state: %s\n", \
DBGINFO, (master_call)->cr, (master_call)->ourcallstate, \
q931_call_state_str((master_call)->ourcallstate), \
q931_hold_state_str(newstate)); \
} \
(master_call)->hold_state = (newstate); \
} while (0)
#else
/* Update hold state with no trace. */
#define UPDATE_HOLD_STATE(ctrl, master_call, newstate) (master_call)->hold_state = (newstate)
#endif
struct ie {
/* Maximal count of same IEs at the message (0 - any, 1..n - limited) */
int max_count;
/* IE code */
int ie;
/* IE friendly name */
char *name;
/* Dump an IE for debugging (preceed all lines by prefix) */
FUNC_DUMP(*dump);
/* Handle IE returns 0 on success, -1 on failure */
FUNC_RECV(*receive);
/* Add IE to a message, return the # of bytes added or -1 on failure */
FUNC_SEND(*transmit);
};
/*!
* \internal
* \brief Encode the channel id information to pass to upper level.
*
* \param call Q.931 call leg
*
* \return Encoded channel value.
*/
static int q931_encode_channel(const q931_call *call)
{
int held_call;
int channelno;
int ds1no;
switch (call->master_call->hold_state) {
case Q931_HOLD_STATE_CALL_HELD:
case Q931_HOLD_STATE_RETRIEVE_REQ:
case Q931_HOLD_STATE_RETRIEVE_IND:
held_call = 1 << 18;
break;
default:
held_call = 0;
break;
}
if (held_call || call->cis_call) {
/* So a -1 does not wipe out the held_call or cis_call flags. */
channelno = call->channelno & 0xFF;
ds1no = call->ds1no & 0xFF;
} else {
channelno = call->channelno;
ds1no = call->ds1no;
}
return channelno | (ds1no << 8) | (call->ds1explicit << 16) | (call->cis_call << 17)
| held_call;
}
/*!
* \brief Check if the given call ptr is valid.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg.
*
* \retval TRUE if call ptr is valid.
* \retval FALSE if call ptr is invalid.
*/
int q931_is_call_valid(struct pri *ctrl, struct q931_call *call)
{
struct q931_call *cur;
struct q921_link *link;
int idx;
if (!call) {
return 0;
}
if (!ctrl) {
/* Must use suspect ctrl from call ptr. */
if (!call->pri) {
/* Definitely a bad call pointer. */
return 0;
}
ctrl = call->pri;
}
/* Check real call records. */
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (call == cur) {
/* Found it. */
return 1;
}
if (cur->outboundbroadcast) {
/* Check subcalls for call ptr. */
for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
if (call == cur->subcalls[idx]) {
/* Found it. */
return 1;
}
}
}
}
/* Check dummy call records. */
for (link = &ctrl->link; link; link = link->next) {
if (link->dummy_call == call) {
/* Found it. */
return 1;
}
}
/* Well it looks like this is a stale call ptr. */
return 0;
}
/*!
* \brief Check if the given call ptr is valid and gripe if not.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg.
* \param func_name Calling function name for debug tracing. (__PRETTY_FUNCTION__)
* \param func_line Calling function line number for debug tracing. (__LINE__)
*
* \retval TRUE if call ptr is valid.
* \retval FALSE if call ptr is invalid.
*/
int q931_is_call_valid_gripe(struct pri *ctrl, struct q931_call *call, const char *func_name, unsigned long func_line)
{
int res;
if (!call) {
/* Let's not gripe about this invalid call pointer. */
return 0;
}
res = q931_is_call_valid(ctrl, call);
if (!res) {
pri_message(ctrl, "!! %s() line:%lu Called with invalid call ptr (%p)\n",
func_name, func_line, call);
}
return res;
}
/*!
* \brief Initialize the given struct q931_party_name
*
* \param name Structure to initialize
*
* \return Nothing
*/
void q931_party_name_init(struct q931_party_name *name)
{
name->valid = 0;
name->presentation = PRI_PRES_UNAVAILABLE;
name->char_set = PRI_CHAR_SET_ISO8859_1;
name->str[0] = '\0';
}
/*!
* \brief Initialize the given struct q931_party_number
*
* \param number Structure to initialize
*
* \return Nothing
*/
void q931_party_number_init(struct q931_party_number *number)
{
number->valid = 0;
number->presentation = PRES_NUMBER_NOT_AVAILABLE;
number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
number->str[0] = '\0';
}
/*!
* \brief Initialize the given struct q931_party_subaddress
*
* \param subaddress Structure to initialize
*
* \return Nothing
*/
void q931_party_subaddress_init(struct q931_party_subaddress *subaddress)
{
subaddress->valid = 0;
subaddress->type = 0;
subaddress->odd_even_indicator = 0;
subaddress->length = 0;
subaddress->data[0] = '\0';
}
/*!
* \brief Initialize the given struct q931_party_address
*
* \param address Structure to initialize
*
* \return Nothing
*/
void q931_party_address_init(struct q931_party_address *address)
{
q931_party_number_init(&address->number);
q931_party_subaddress_init(&address->subaddress);
}
/*!
* \brief Initialize the given struct q931_party_id
*
* \param id Structure to initialize
*
* \return Nothing
*/
void q931_party_id_init(struct q931_party_id *id)
{
q931_party_name_init(&id->name);
q931_party_number_init(&id->number);
q931_party_subaddress_init(&id->subaddress);
}
/*!
* \brief Initialize the given struct q931_party_redirecting
*
* \param redirecting Structure to initialize
*
* \return Nothing
*/
void q931_party_redirecting_init(struct q931_party_redirecting *redirecting)
{
q931_party_id_init(&redirecting->from);
q931_party_id_init(&redirecting->to);
q931_party_id_init(&redirecting->orig_called);
redirecting->state = Q931_REDIRECTING_STATE_IDLE;
redirecting->count = 0;
redirecting->orig_reason = PRI_REDIR_UNKNOWN;
redirecting->reason = PRI_REDIR_UNKNOWN;
}
/*!
* \brief Compare the left and right party name.
*
* \param left Left parameter party name.
* \param right Right parameter party name.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_name_cmp(const struct q931_party_name *left, const struct q931_party_name *right)
{
int cmp;
if (!left->valid) {
if (!right->valid) {
return 0;
}
return -1;
} else if (!right->valid) {
return 1;
}
cmp = left->char_set - right->char_set;
if (cmp) {
return cmp;
}
cmp = strcmp(left->str, right->str);
if (cmp) {
return cmp;
}
cmp = left->presentation - right->presentation;
return cmp;
}
/*!
* \brief Compare the left and right party number.
*
* \param left Left parameter party number.
* \param right Right parameter party number.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_number_cmp(const struct q931_party_number *left, const struct q931_party_number *right)
{
int cmp;
if (!left->valid) {
if (!right->valid) {
return 0;
}
return -1;
} else if (!right->valid) {
return 1;
}
cmp = left->plan - right->plan;
if (cmp) {
return cmp;
}
cmp = strcmp(left->str, right->str);
if (cmp) {
return cmp;
}
cmp = left->presentation - right->presentation;
return cmp;
}
/*!
* \brief Compare the left and right party subaddress.
*
* \param left Left parameter party subaddress.
* \param right Right parameter party subaddress.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_subaddress_cmp(const struct q931_party_subaddress *left, const struct q931_party_subaddress *right)
{
int cmp;
if (!left->valid) {
if (!right->valid) {
return 0;
}
return -1;
} else if (!right->valid) {
return 1;
}
cmp = left->type - right->type;
if (cmp) {
return cmp;
}
cmp = memcmp(left->data, right->data,
(left->length < right->length) ? left->length : right->length);
if (cmp) {
return cmp;
}
cmp = left->length - right->length;
if (cmp) {
return cmp;
}
cmp = left->odd_even_indicator - right->odd_even_indicator;
return cmp;
}
/*!
* \brief Compare the left and right party address.
*
* \param left Left parameter party address.
* \param right Right parameter party address.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_address_cmp(const struct q931_party_address *left, const struct q931_party_address *right)
{
int cmp;
cmp = q931_party_number_cmp(&left->number, &right->number);
if (cmp) {
return cmp;
}
cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
return cmp;
}
/*!
* \brief Compare the left and right party id.
*
* \param left Left parameter party id.
* \param right Right parameter party id.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_id_cmp(const struct q931_party_id *left, const struct q931_party_id *right)
{
int cmp;
cmp = q931_party_number_cmp(&left->number, &right->number);
if (cmp) {
return cmp;
}
cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
if (cmp) {
return cmp;
}
cmp = q931_party_name_cmp(&left->name, &right->name);
return cmp;
}
/*!
* \brief Compare the left and right party id addresses.
*
* \param left Left parameter party id.
* \param right Right parameter party id.
*
* \retval < 0 when left < right.
* \retval == 0 when left == right.
* \retval > 0 when left > right.
*/
int q931_party_id_cmp_address(const struct q931_party_id *left, const struct q931_party_id *right)
{
int cmp;
cmp = q931_party_number_cmp(&left->number, &right->number);
if (cmp) {
return cmp;
}
cmp = q931_party_subaddress_cmp(&left->subaddress, &right->subaddress);
return cmp;
}
/*!
* \brief Compare the party id to the party address.
*
* \param id Party id.
* \param address Party address.
*
* \retval < 0 when id < address.
* \retval == 0 when id == address.
* \retval > 0 when id > address.
*/
int q931_cmp_party_id_to_address(const struct q931_party_id *id, const struct q931_party_address *address)
{
int cmp;
cmp = q931_party_number_cmp(&id->number, &address->number);
if (cmp) {
return cmp;
}
cmp = q931_party_subaddress_cmp(&id->subaddress, &address->subaddress);
return cmp;
}
/*!
* \brief Copy a party id into a party address.
*
* \param address Party address.
* \param id Party id.
*
* \return Nothing
*/
void q931_party_id_copy_to_address(struct q931_party_address *address, const struct q931_party_id *id)
{
address->number = id->number;
address->subaddress = id->subaddress;
}
/*!
* \brief Copy the Q.931 party name to the PRI party name structure.
*
* \param pri_name PRI party name structure
* \param q931_name Q.931 party name structure
*
* \return Nothing
*/
void q931_party_name_copy_to_pri(struct pri_party_name *pri_name, const struct q931_party_name *q931_name)
{
if (q931_name->valid) {
pri_name->valid = 1;
pri_name->presentation = q931_name->presentation;
pri_name->char_set = q931_name->char_set;
libpri_copy_string(pri_name->str, q931_name->str, sizeof(pri_name->str));
} else {
pri_name->valid = 0;
pri_name->presentation = PRI_PRES_UNAVAILABLE;
pri_name->char_set = PRI_CHAR_SET_ISO8859_1;
pri_name->str[0] = '\0';
}
}
/*!
* \brief Copy the Q.931 party number to the PRI party number structure.
*
* \param pri_number PRI party number structure
* \param q931_number Q.931 party number structure
*
* \return Nothing
*/
void q931_party_number_copy_to_pri(struct pri_party_number *pri_number, const struct q931_party_number *q931_number)
{
if (q931_number->valid) {
pri_number->valid = 1;
pri_number->presentation = q931_number->presentation;
pri_number->plan = q931_number->plan;
libpri_copy_string(pri_number->str, q931_number->str, sizeof(pri_number->str));
} else {
pri_number->valid = 0;
pri_number->presentation = PRES_NUMBER_NOT_AVAILABLE;
pri_number->plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
pri_number->str[0] = '\0';
}
}
/*!
* \brief Copy the Q.931 party subaddress to the PRI party subaddress structure.
*
* \param pri_subaddress PRI party subaddress structure
* \param q931_subaddress Q.931 party subaddress structure
*
* \return Nothing
*/
void q931_party_subaddress_copy_to_pri(struct pri_party_subaddress *pri_subaddress, const struct q931_party_subaddress *q931_subaddress)
{
int length;
if (!q931_subaddress->valid) {
pri_subaddress->valid = 0;
pri_subaddress->type = 0;
pri_subaddress->odd_even_indicator = 0;
pri_subaddress->length = 0;
pri_subaddress->data[0] = '\0';
return;
}
pri_subaddress->valid = 1;
pri_subaddress->type = q931_subaddress->type;
pri_subaddress->odd_even_indicator = q931_subaddress->odd_even_indicator;
/*
* The size of pri_subaddress->data[] is not the same as the size of
* q931_subaddress->data[].
*/
length = q931_subaddress->length;
pri_subaddress->length = length;
memcpy(pri_subaddress->data, q931_subaddress->data, length);
pri_subaddress->data[length] = '\0';
}
/*!
* \brief Copy the Q.931 party address to the PRI party address structure.
*
* \param pri_address PRI party address structure
* \param q931_address Q.931 party address structure
*
* \return Nothing
*/
void q931_party_address_copy_to_pri(struct pri_party_address *pri_address, const struct q931_party_address *q931_address)
{
q931_party_number_copy_to_pri(&pri_address->number, &q931_address->number);
q931_party_subaddress_copy_to_pri(&pri_address->subaddress, &q931_address->subaddress);
}
/*!
* \brief Copy the Q.931 party id to the PRI party id structure.
*
* \param pri_id PRI party id structure
* \param q931_id Q.931 party id structure
*
* \return Nothing
*/
void q931_party_id_copy_to_pri(struct pri_party_id *pri_id, const struct q931_party_id *q931_id)
{
q931_party_name_copy_to_pri(&pri_id->name, &q931_id->name);
q931_party_number_copy_to_pri(&pri_id->number, &q931_id->number);
q931_party_subaddress_copy_to_pri(&pri_id->subaddress, &q931_id->subaddress);
}
/*!
* \brief Copy the Q.931 redirecting data to the PRI redirecting structure.
*
* \param pri_redirecting PRI redirecting structure
* \param q931_redirecting Q.931 redirecting structure
*
* \return Nothing
*/
void q931_party_redirecting_copy_to_pri(struct pri_party_redirecting *pri_redirecting, const struct q931_party_redirecting *q931_redirecting)
{
q931_party_id_copy_to_pri(&pri_redirecting->from, &q931_redirecting->from);
q931_party_id_copy_to_pri(&pri_redirecting->to, &q931_redirecting->to);
q931_party_id_copy_to_pri(&pri_redirecting->orig_called,
&q931_redirecting->orig_called);
pri_redirecting->count = q931_redirecting->count;
pri_redirecting->orig_reason = q931_redirecting->orig_reason;
pri_redirecting->reason = q931_redirecting->reason;
}
/*!
* \brief Fixup some values in the q931_party_id that may be objectionable by switches.
*
* \param ctrl D channel controller.
* \param id Party ID to tweak.
*
* \return Nothing
*/
void q931_party_id_fixup(const struct pri *ctrl, struct q931_party_id *id)
{
switch (ctrl->switchtype) {
case PRI_SWITCH_DMS100:
case PRI_SWITCH_ATT4ESS:
/* Doesn't like certain presentation types */
if (id->number.valid
&& (id->number.presentation & PRI_PRES_RESTRICTION) == PRI_PRES_ALLOWED) {
/* i.e., If presentation is allowed it must be a network number */
id->number.presentation = PRES_ALLOWED_NETWORK_NUMBER;
}
break;
default:
break;
}
}
/*!
* \brief Determine the overall presentation value for the given party.
*
* \param id Party to determine the overall presentation value.
*
* \return Overall presentation value for the given party.
*/
int q931_party_id_presentation(const struct q931_party_id *id)
{
int number_priority;
int number_value;
int number_screening;
int name_priority;
int name_value;
/* Determine name presentation priority. */
if (!id->name.valid) {
name_value = PRI_PRES_UNAVAILABLE;
name_priority = 3;
} else {
name_value = id->name.presentation & PRI_PRES_RESTRICTION;
switch (name_value) {
case PRI_PRES_RESTRICTED:
name_priority = 0;
break;
case PRI_PRES_ALLOWED:
name_priority = 1;
break;
case PRI_PRES_UNAVAILABLE:
name_priority = 2;
break;
default:
name_value = PRI_PRES_UNAVAILABLE;
name_priority = 3;
break;
}
}
/* Determine number presentation priority. */
if (!id->number.valid) {
number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
number_value = PRI_PRES_UNAVAILABLE;
number_priority = 3;
} else {
number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
number_value = id->number.presentation & PRI_PRES_RESTRICTION;
switch (number_value) {
case PRI_PRES_RESTRICTED:
number_priority = 0;
break;
case PRI_PRES_ALLOWED:
number_priority = 1;
break;
case PRI_PRES_UNAVAILABLE:
number_priority = 2;
break;
default:
number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
number_value = PRI_PRES_UNAVAILABLE;
number_priority = 3;
break;
}
}
/* Select the wining presentation value. */
if (name_priority < number_priority) {
number_value = name_value;
}
if (number_value == PRI_PRES_UNAVAILABLE) {
return PRES_NUMBER_NOT_AVAILABLE;
}
return number_value | number_screening;
}
/*!
* \internal
* \brief Get binary buffer contents into the destination buffer.
*
* \param dst Destination buffer.
* \param dst_size Destination buffer sizeof()
* \param src Source buffer.
* \param src_len Source buffer length to copy.
*
* \note The destination buffer is nul terminated just in case
* the contents are used as a string anyway.
*
* \retval 0 on success.
* \retval -1 on error. The copy did not happen.
*/
static int q931_memget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
{
if (src_len < 0 || src_len > dst_size - 1) {
dst[0] = 0;
return -1;
}
memcpy(dst, src, src_len);
dst[src_len] = 0;
return 0;
}
/*!
* \internal
* \brief Get source buffer contents into the destination buffer for a string.
*
* \param dst Destination buffer.
* \param dst_size Destination buffer sizeof()
* \param src Source buffer.
* \param src_len Source buffer length to copy.
*
* \note The destination buffer is nul terminated.
* \note Nul bytes from the source buffer are not copied.
*
* \retval 0 on success.
* \retval -1 if nul bytes were found in the source data.
*/
static int q931_strget(unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
{
int saw_nul;
if (src_len < 1) {
dst[0] = '\0';
return 0;
}
saw_nul = 0;
--dst_size;
while (dst_size && src_len) {
if (*src) {
*dst++ = *src;
--dst_size;
} else {
/* Skip nul bytes in the source buffer. */
saw_nul = -1;
}
++src;
--src_len;
}
*dst = '\0';
return saw_nul;
}
/*!
* \internal
* \brief Get source buffer contents into the destination buffer for a string.
*
* \param ctrl D channel controller.
* \param ie_name IE name to report nul bytes found in.
* \param dst Destination buffer.
* \param dst_size Destination buffer sizeof()
* \param src Source buffer.
* \param src_len Source buffer length to copy.
*
* \note The destination buffer is nul terminated.
* \note Nul bytes from the source buffer are not copied.
*
* \retval 0 on success.
* \retval -1 if nul bytes were found in the source data.
*/
static int q931_strget_gripe(struct pri *ctrl, const char *ie_name, unsigned char *dst, size_t dst_size, const unsigned char *src, int src_len)
{
int saw_nul;
/* To quietly remove nul octets just comment out the following line. */
#define UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS 1
saw_nul = q931_strget(dst, dst_size, src, src_len);
if (saw_nul
#if !defined(UNCONDITIONALLY_REPORT_REMOVED_NUL_OCTETS)
&& (ctrl->debug & PRI_DEBUG_Q931_STATE)
#endif
) {
pri_message(ctrl, "!! Removed nul octets from IE '%s' and returning '%s'.\n",
ie_name, dst);
}
return saw_nul;
}
/*!
* \internal
* \brief Clear the display text.
*
* \param call Q.931 call to clear display text.
*
* \return Nothing
*/
static void q931_display_clear(struct q931_call *call)
{
call->display.text = NULL;
}
/*!
* \internal
* \brief Set the display text for the party name.
*
* \param call Q.931 call to set display text to the party name.
*
* \return Nothing
*/
static void q931_display_name_send(struct q931_call *call, const struct q931_party_name *name)
{
if (name->valid) {
switch (name->presentation & PRI_PRES_RESTRICTION) {
case PRI_PRES_ALLOWED:
call->display.text = (unsigned char *) name->str;
call->display.full_ie = 0;
call->display.length = strlen(name->str);
call->display.char_set = name->char_set;
break;
default:
call->display.text = NULL;
break;
}
} else {
call->display.text = NULL;
}
}
/*!
* \brief Get the display text into the party name.
*
* \param call Q.931 call to get the display text into the party name.
* \param name Party name to fill if there is display text.
*
* \note
* The party name is not touched if there is no display text.
*
* \note
* The display text is consumed.
*
* \return TRUE if party name filled.
*/
int q931_display_name_get(struct q931_call *call, struct q931_party_name *name)
{
if (!call->display.text) {
return 0;
}
name->valid = 1;
name->char_set = call->display.char_set;
q931_strget_gripe(call->pri, ie2str(call->display.full_ie),
(unsigned char *) name->str, sizeof(name->str), call->display.text,
call->display.length);
if (name->str[0]) {
name->presentation = PRI_PRES_ALLOWED;
} else {
name->presentation = PRI_PRES_RESTRICTED;
}
/* Mark the display text as consumed. */
call->display.text = NULL;
return 1;
}
/*!
* \internal
* \brief Fill a subcmd with any display text.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg.
*
* \note
* The display text is consumed.
*
* \return Nothing
*/
static void q931_display_subcmd(struct pri *ctrl, struct q931_call *call)
{
struct pri_subcommand *subcmd;
if (call->display.text && call->display.length
&& (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_TEXT)) {
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
/* Setup display text subcommand */
subcmd->cmd = PRI_SUBCMD_DISPLAY_TEXT;
subcmd->u.display.char_set = call->display.char_set;
q931_strget_gripe(ctrl, ie2str(call->display.full_ie),
(unsigned char *) subcmd->u.display.text, sizeof(subcmd->u.display.text),
call->display.text, call->display.length);
subcmd->u.display.length = strlen(subcmd->u.display.text);
}
}
/* Mark the display text as consumed. */
call->display.text = NULL;
}
/*!
* \brief Find the winning subcall if it exists or current call if not outboundbroadcast.
*
* \param call Starting Q.931 call record of search.
*
* \retval winning-call or given call if not outboundbroadcast.
* \retval NULL if no winning call yet.
*/
struct q931_call *q931_find_winning_call(struct q931_call *call)
{
struct q931_call *master;
master = call->master_call;
if (master->outboundbroadcast) {
/* We have potential subcalls. Now get the winning call if declared yet. */
if (master->pri_winner < 0) {
/* Winner not declared yet.*/
call = NULL;
} else {
call = master->subcalls[master->pri_winner];
}
}
return call;
}
/*!
* \internal
* \brief Append the given ie contents to the save ie location.
*
* \param save_ie Saved ie contents to append new ie.
* \param ie Contents to append.
*
* \return Nothing
*/
static void q931_append_ie_contents(struct q931_saved_ie_contents *save_ie, struct q931_ie *ie)
{
int size;
size = ie->len + 2;
if (size < sizeof(save_ie->data) - save_ie->length) {
/* Contents will fit so append it. */
memcpy(&save_ie->data[save_ie->length], ie, size);
save_ie->length += size;
}
}
static void q931_clr_subcommands(struct pri *ctrl)
{
ctrl->subcmds.counter_subcmd = 0;
}
struct pri_subcommand *q931_alloc_subcommand(struct pri *ctrl)
{
if (ctrl->subcmds.counter_subcmd < PRI_MAX_SUBCOMMANDS) {
return &ctrl->subcmds.subcmd[ctrl->subcmds.counter_subcmd++];
}
pri_error(ctrl, "ERROR: Too many facility subcommands\n");
return NULL;
}
static char *code2str(int code, struct msgtype *codes, int max)
{
int x;
for (x=0;x<max; x++)
if (codes[x].msgnum == code)
return codes[x].name;
return "Unknown";
}
static char *pritype(int type)
{
switch (type) {
case PRI_CPE:
return "CPE";
break;
case PRI_NETWORK:
return "NET";
break;
default:
return "UNKNOWN";
}
}
static char *binary(int b, int len) {
static char res[33];
int x;
memset(res, 0, sizeof(res));
if (len > 32)
len = 32;
for (x=1;x<=len;x++)
res[x-1] = b & (1 << (len - x)) ? '1' : '0';
return res;
}
static int receive_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int x;
int pos = 0;
int need_extended_channel_octets;/*!< TRUE if octets 3.2 and 3.3 need to be present. */
call->channel_id_ie_mandatory = 0;
if (ie->len < 1) {
/* Must have at least one payload octet. */
pri_error(ctrl, "!! Channel Identification ie too short\n");
return -1;
}
call->restart.count = 0;
if (ie->data[0] & 0x08) {
call->chanflags = FLAG_EXCLUSIVE;
} else {
call->chanflags = FLAG_PREFERRED;
}
need_extended_channel_octets = 0;
if (ie->data[0] & 0x20) {
/* PRI encoded interface type */
switch (ie->data[0] & 0x03) {
case 0x00:
/* No channel */
call->channelno = 0;
call->chanflags = FLAG_PREFERRED;
break;
case 0x01:
/* As indicated in following octets */
need_extended_channel_octets = 1;
break;
case 0x03:
/* Any channel */
call->chanflags = FLAG_PREFERRED;
break;
default:
pri_error(ctrl, "!! Unexpected Channel selection %d\n", ie->data[0] & 0x03);
return -1;
}
} else {
/* BRI encoded interface type */
switch (ie->data[0] & 0x03) {
case 0x00:
/* No channel */
call->channelno = 0;
call->chanflags = FLAG_PREFERRED;
break;
case 0x03:
/* Any channel */
call->chanflags = FLAG_PREFERRED;
break;
default:
/* Specified B channel (B1 or B2) */
call->channelno = ie->data[0] & 0x03;
break;
}
}
pos++;
if (ie->data[0] & 0x40) {
/* Explicitly defined DS1 (One or more 3.1 octets expected) */
if (ctrl->switchtype == PRI_SWITCH_QSIG) {
pri_error(ctrl, "!! Q.SIG does not support Channel Identification octet 3.1\n");
return -1;
}
if (ie->len <= pos) {
pri_error(ctrl, "!! Missing expected Channel Identification octet 3.1\n");
return -1;
}
/* Extract explicit DS1 reference. */
call->ds1no = ie->data[pos] & 0x7f;
call->ds1explicit = 1;
/* Advance to 3.2 octet if present. */
do {
++pos;
} while (pos < ie->len && !(ie->data[pos - 1] & 0x80));
} else {
call->ds1explicit = 0;
}
if (ie->data[0] & 0x04) {
/* D channel call. Signaling only. */
call->cis_call = 1;
call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
call->channelno = 0;
return 0;
}
if (need_extended_channel_octets && pos + 2 <= ie->len) {
/*
* Octet 3.2 and at least one octet 3.3 are present.
*
* Process octet 3.2
*/
if ((ie->data[pos] & 0x0f) != 3) {
/* Channel type/mapping is not for B channel units. */
pri_error(ctrl, "!! Unexpected Channel Type %d\n", ie->data[pos] & 0x0f);
return -1;
}
if ((ie->data[pos] & 0x60) != 0) {
pri_error(ctrl, "!! Invalid CCITT coding %d\n", (ie->data[pos] & 0x60) >> 5);
return -1;
}
if (ie->data[pos++] & 0x10) {
/* Process 3.3 octets as a Slot Map */
call->slotmap = 0;
call->slotmap_size = (ie->len - pos > 3) ? 1 : 0;
/*
* We will be tolerant of partial slot maps that do not
* specify the upper channels. We will assume they
* are not requested.
*
* We will only read up to 24 or 32 bit channel maps.
*/
for (x = call->slotmap_size ? 4 : 3; x--;) {
call->slotmap <<= 8;
call->slotmap |= ie->data[pos++];
if (ie->len <= pos) {
/* No more ie contents. */
break;
}
}
if (msgtype == Q931_RESTART) {
int bit;
/* Convert the slotmap to a channel list for RESTART support. */
for (bit = 0; bit < ARRAY_LEN(call->restart.chan_no); ++bit) {
if (call->slotmap & (1UL << bit)) {
call->restart.chan_no[call->restart.count++] = bit
+ (call->slotmap_size ? 0 : 1);
}
}
}
} else {
/*
* Process 3.3 octets as either a single channel or a channel list
*
* Get single channel
*/
call->channelno = ie->data[pos] & 0x7f;
if (ctrl->chan_mapping_logical && call->channelno > 15) {
call->channelno++;
}
if (msgtype == Q931_RESTART) {
/* Read in channel list for RESTART support. */
while (call->restart.count < ARRAY_LEN(call->restart.chan_no)) {
int chan_no;
chan_no = ie->data[pos] & 0x7f;
if (ctrl->chan_mapping_logical && chan_no > 15) {
++chan_no;
}
call->restart.chan_no[call->restart.count++] = chan_no;
if (ie->data[pos++] & 0x80) {
/* Channel list finished. */
break;
}
if (ie->len <= pos) {
/* No more ie contents. */
break;
}
}
}
}
} else if (need_extended_channel_octets) {
pri_error(ctrl, "!! Missing expected Channel Identification octets 3.2 or 3.3\n");
return -1;
}
return 0;
}
static int transmit_channel_id(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
int pos = 0;
/* We are ready to transmit single IE only */
if (order > 1)
return 0;
if (call->cis_call) {
/*
* Read the standards docs to figure this out.
* Q.SIG ECMA-165 section 7.3
* ITU Q.931 section 4.5.13
*/
ie->data[pos++] = ctrl->bri ? 0x8c : 0xac;
return pos + 2;
}
/* Start with standard stuff */
if (ctrl->switchtype == PRI_SWITCH_GR303_TMC)
ie->data[pos] = 0x69;
else if (ctrl->bri) {
ie->data[pos] = 0x80;
ie->data[pos] |= (call->channelno & 0x3);
} else {
/* PRI */
if (call->slotmap != -1 || (call->chanflags & FLAG_WHOLE_INTERFACE)) {
/* Specified channel */
ie->data[pos] = 0xa1;
} else if (call->channelno < 0 || call->channelno == 0xff) {
/* Any channel */
ie->data[pos] = 0xa3;
} else if (!call->channelno) {
/* No channel */
ie->data[pos] = 0xa0;
} else {
/* Specified channel */
ie->data[pos] = 0xa1;
}
}
if (call->chanflags & FLAG_EXCLUSIVE) {
/* Channel is exclusive */
if (!(ie->data[pos] & 0x03)) {
/* An exclusive no channel id ie is to be discarded. */
return 0;
}
ie->data[pos] |= 0x08;
} else if (!call->chanflags) {
/* Don't need this IE */
return 0;
}
if (!ctrl->bri && (((ctrl->switchtype != PRI_SWITCH_QSIG) && (call->ds1no > 0)) || call->ds1explicit)) {
/* We are specifying the interface. Octet 3.1 */
ie->data[pos++] |= 0x40;
ie->data[pos++] = 0x80 | call->ds1no;
} else {
++pos;
}
if (!ctrl->bri && (ie->data[0] & 0x03) == 0x01 /* Specified channel */
&& !(call->chanflags & FLAG_WHOLE_INTERFACE)) {
/* The 3.2 and 3.3 octets need to be present */
ie->data[pos] = 0x83;
if (0 < call->channelno && call->channelno != 0xff) {
/* Channel number specified and preferred over slot map if we have one. */
++pos;
if (msgtype == Q931_RESTART_ACKNOWLEDGE && call->restart.count) {
int chan_no;
int idx;
/* Build RESTART_ACKNOWLEDGE channel list */
for (idx = 0; idx < call->restart.count; ++idx) {
chan_no = call->restart.chan_no[idx];
if (ctrl->chan_mapping_logical && chan_no > 16) {
--chan_no;
}
if (call->restart.count <= idx + 1) {
/* Last channel list channel. */
chan_no |= 0x80;
}
ie->data[pos++] = chan_no;
}
} else {
if (ctrl->chan_mapping_logical && call->channelno > 16) {
ie->data[pos++] = 0x80 | (call->channelno - 1);
} else {
ie->data[pos++] = 0x80 | call->channelno;
}
}
} else if (call->slotmap != -1) {
int octet;
/* We have to send a slot map */
ie->data[pos++] |= 0x10;
for (octet = call->slotmap_size ? 4 : 3; octet--;) {
ie->data[pos++] = (call->slotmap >> (8 * octet)) & 0xff;
}
} else {
pri_error(ctrl, "XXX We need either a channelno or slotmap but have neither!\n");
/* Discard this malformed ie. */
return 0;
}
}
return pos + 2;
}
static void dump_channel_id(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int pos;
int x;
int res;
static const char *msg_chan_sel[] = {
"No channel selected", "B1 channel", "B2 channel", "Any channel selected",
"No channel selected", "As indicated in following octets", "Reserved", "Any channel selected"
};
pri_message(ctrl,
"%c %s (len=%2d) [ Ext: %d IntID: %s %s Spare: %d %s Dchan: %d\n",
prefix, ie2str(full_ie), len,
(ie->data[0] & 0x80) ? 1 : 0,
(ie->data[0] & 0x40) ? "Explicit" : "Implicit",
(ie->data[0] & 0x20) ? "Other(PRI)" : "BRI",
(ie->data[0] & 0x10) ? 1 : 0,
(ie->data[0] & 0x08) ? "Exclusive" : "Preferred",
(ie->data[0] & 0x04) ? 1 : 0);
pri_message(ctrl, "%c ChanSel: %s\n",
prefix, msg_chan_sel[(ie->data[0] & 0x03) | ((ie->data[0] & 0x20) >> 3)]);
pos = 1;
len -= 2;
if (ie->data[0] & 0x40) {
/* Explicitly defined DS1 (One or more 3.1 octets expected) */
if (ctrl->switchtype == PRI_SWITCH_QSIG) {
pri_message(ctrl, "%c Octet 3.1 specified when Q.SIG!\n",
prefix);
}
if (pos < len) {
do {
pri_message(ctrl, "%c Ext: %d DS1 Identifier: %d\n",
prefix, (ie->data[pos] & 0x80) >> 7, ie->data[pos] & 0x7f);
++pos;
} while (!(ie->data[pos - 1] & 0x80) && pos < len);
} else {
pri_message(ctrl, "%c Octet 3.1 is missing!\n", prefix);
}
} else {
/* Implicitly defined DS1 */
}
if (pos < len) {
/* Octet 3.2 present */
pri_message(ctrl,
"%c Ext: %d Coding: %d %s Specified Channel Type: %d\n",
prefix, (ie->data[pos] & 0x80) >> 7, (ie->data[pos] & 60) >> 5,
(ie->data[pos] & 0x10) ? "Slot Map" : "Number", ie->data[pos] & 0x0f);
++pos;
}
if (pos < len) {
/* One or more 3.3 octets present */
if (!(ie->data[pos - 1] & 0x10)) {
/* Number specified */
do {
pri_message(ctrl,
"%c Ext: %d Channel: %d Type: %s%c\n",
prefix, (ie->data[pos] & 0x80) >> 7,
(ie->data[pos]) & 0x7f, pritype(ctrl->localtype),
(pos + 1 < len) ? ' ' : ']');
++pos;
} while (pos < len);
} else {
/* Map specified */
res = 0;
x = 0;
do {
res <<= 8;
res |= ie->data[pos++];
++x;
} while (pos < len);
pri_message(ctrl, "%c Map len: %d Map: %s ]\n", prefix,
x, binary(res, x << 3));
}
} else {
pri_message(ctrl, "%c ]\n", prefix);
}
}
static char *ri2str(int ri)
{
static struct msgtype ris[] = {
{ 0, "Indicated Channel" },
{ 6, "Single DS1 Facility" },
{ 7, "All DS1 Facilities" },
};
return code2str(ri, ris, sizeof(ris) / sizeof(ris[0]));
}
static void dump_restart_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d Spare: %d Resetting %s (%d) ]\n",
prefix, ie2str(full_ie), len, (ie->data[0] & 0x80) >> 7,
(ie->data[0] & 0x78) >> 3, ri2str(ie->data[0] & 0x7), ie->data[0] & 0x7);
}
static int receive_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
/* Pretty simple */
call->ri = ie->data[0] & 0x7;
return 0;
}
static int transmit_restart_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
/* Pretty simple */
switch(call->ri) {
case 0:
case 6:
case 7:
ie->data[0] = 0x80 | (call->ri & 0x7);
break;
case 5:
/* Switch compatibility */
ie->data[0] = 0xA0 | (call->ri & 0x7);
break;
default:
pri_error(ctrl, "!! Invalid restart indicator value %d\n", call->ri);
return-1;
}
return 3;
}
static char *redirection_reason2str(int mode)
{
static struct msgtype modes[] = {
{ PRI_REDIR_UNKNOWN, "Unknown" },
{ PRI_REDIR_FORWARD_ON_BUSY, "Forwarded on busy" },
{ PRI_REDIR_FORWARD_ON_NO_REPLY, "Forwarded on no reply" },
{ PRI_REDIR_DEFLECTION, "Call deflected" },
{ PRI_REDIR_DTE_OUT_OF_ORDER, "Called DTE out of order" },
{ PRI_REDIR_FORWARDED_BY_DTE, "Forwarded by called DTE" },
{ PRI_REDIR_UNCONDITIONAL, "Forwarded unconditionally" },
};
return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
}
static char *cap2str(int mode)
{
static struct msgtype modes[] = {
{ PRI_TRANS_CAP_SPEECH, "Speech" },
{ PRI_TRANS_CAP_DIGITAL, "Unrestricted digital information" },
{ PRI_TRANS_CAP_RESTRICTED_DIGITAL, "Restricted digital information" },
{ PRI_TRANS_CAP_3_1K_AUDIO, "3.1kHz audio" },
{ PRI_TRANS_CAP_DIGITAL_W_TONES, "Unrestricted digital information with tones/announcements" },
{ PRI_TRANS_CAP_VIDEO, "Video" },
{ PRI_TRANS_CAP_AUDIO_4ESS, "3.1khz audio (4ESS)" },
};
return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
}
static char *mode2str(int mode)
{
static struct msgtype modes[] = {
{ TRANS_MODE_64_CIRCUIT, "64kbps, circuit-mode" },
{ TRANS_MODE_2x64_CIRCUIT, "2x64kbps, circuit-mode" },
{ TRANS_MODE_384_CIRCUIT, "384kbps, circuit-mode" },
{ TRANS_MODE_1536_CIRCUIT, "1536kbps, circuit-mode" },
{ TRANS_MODE_1920_CIRCUIT, "1920kbps, circuit-mode" },
{ TRANS_MODE_MULTIRATE, "Multirate (Nx64kbps)" },
{ TRANS_MODE_PACKET, "Packet Mode" },
};
return code2str(mode, modes, sizeof(modes) / sizeof(modes[0]));
}
static char *l12str(int proto)
{
static struct msgtype protos[] = {
{ PRI_LAYER_1_ITU_RATE_ADAPT, "V.110 Rate Adaption" },
{ PRI_LAYER_1_ULAW, "u-Law" },
{ PRI_LAYER_1_ALAW, "A-Law" },
{ PRI_LAYER_1_G721, "G.721 ADPCM" },
{ PRI_LAYER_1_G722_G725, "G.722/G.725 7kHz Audio" },
{ PRI_LAYER_1_H223_H245, "H.223/H.245 Multimedia" },
{ PRI_LAYER_1_NON_ITU_ADAPT, "Non-ITU Rate Adaption" },
{ PRI_LAYER_1_V120_RATE_ADAPT, "V.120 Rate Adaption" },
{ PRI_LAYER_1_X31_RATE_ADAPT, "X.31 Rate Adaption" },
};
return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
}
static char *ra2str(int proto)
{
static struct msgtype protos[] = {
{ PRI_RATE_ADAPT_9K6, "9.6 kbit/s" },
};
return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
}
static char *l22str(int proto)
{
static struct msgtype protos[] = {
{ LAYER_2_LAPB, "LAPB" },
};
return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
}
static char *l32str(int proto)
{
static struct msgtype protos[] = {
{ LAYER_3_X25, "X.25" },
};
return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
}
static char *int_rate2str(int proto)
{
static struct msgtype protos[] = {
{ PRI_INT_RATE_8K, "8 kbit/s" },
{ PRI_INT_RATE_16K, "16 kbit/s" },
{ PRI_INT_RATE_32K, "32 kbit/s" },
};
return code2str(proto, protos, sizeof(protos) / sizeof(protos[0]));
}
static void dump_bearer_capability(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int pos=2;
pri_message(ctrl,
"%c %s (len=%2d) [ Ext: %d Coding-Std: %d Info transfer capability: %s (%d)\n",
prefix, ie2str(full_ie), len, (ie->data[0] & 0x80 ) >> 7,
(ie->data[0] & 0x60) >> 5, cap2str(ie->data[0] & 0x1f),
(ie->data[0] & 0x1f));
pri_message(ctrl, "%c Ext: %d Trans mode/rate: %s (%d)\n", prefix, (ie->data[1] & 0x80) >> 7, mode2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
/* octet 4.1 exists if mode/rate is multirate */
if ((ie->data[1] & 0x7f) == 0x18) {
pri_message(ctrl, "%c Ext: %d Transfer rate multiplier: %d x 64\n", prefix, (ie->data[2] & 0x80) >> 7, ie->data[2] & 0x7f);
pos++;
}
/* don't count the IE num and length as part of the data */
len -= 2;
/* Look for octet 5; this is identified by bits 5,6 == 01 */
if (pos < len &&
(ie->data[pos] & 0x60) == 0x20) {
/* although the layer1 is only the bottom 5 bits of the byte,
previous versions of this library passed bits 5&6 through
too, so we have to do the same for binary compatability */
u_int8_t layer1 = ie->data[pos] & 0x7f;
pri_message(ctrl, "%c User information layer 1: %s (%d)\n",
prefix, l12str(layer1), layer1);
pos++;
/* octet 5a? */
if (pos < len && !(ie->data[pos-1] & 0x80)) {
int ra = ie->data[pos] & 0x7f;
pri_message(ctrl, "%c Async: %d, Negotiation: %d, "
"User rate: %s (%#x)\n",
prefix,
ra & PRI_RATE_ADAPT_ASYNC ? 1 : 0,
ra & PRI_RATE_ADAPT_NEGOTIATION_POSS ? 1 : 0,
ra2str(ra & PRI_RATE_USER_RATE_MASK),
ra & PRI_RATE_USER_RATE_MASK);
pos++;
}
/* octet 5b? */
if (pos < len && !(ie->data[pos-1] & 0x80)) {
u_int8_t data = ie->data[pos];
if (layer1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
pri_message(ctrl, "%c Intermediate rate: %s (%d), "
"NIC on Tx: %d, NIC on Rx: %d, "
"Flow control on Tx: %d, "
"Flow control on Rx: %d\n",
prefix, int_rate2str((data & 0x60)>>5),
(data & 0x60)>>5,
(data & 0x10)?1:0,
(data & 0x08)?1:0,
(data & 0x04)?1:0,
(data & 0x02)?1:0);
} else if (layer1 == PRI_LAYER_1_V120_RATE_ADAPT) {
pri_message(ctrl, "%c Hdr: %d, Multiframe: %d, Mode: %d, "
"LLI negot: %d, Assignor: %d, "
"In-band neg: %d\n", prefix,
(data & 0x40)?1:0,
(data & 0x20)?1:0,
(data & 0x10)?1:0,
(data & 0x08)?1:0,
(data & 0x04)?1:0,
(data & 0x02)?1:0);
} else {
pri_message(ctrl, "%c Unknown octet 5b: 0x%x\n",
prefix, data);
}
pos++;
}
/* octet 5c? */
if (pos < len && !(ie->data[pos-1] & 0x80)) {
u_int8_t data = ie->data[pos];
const char *stop_bits[] = {"?","1","1.5","2"};
const char *data_bits[] = {"?","5","7","8"};
const char *parity[] = {"Odd","?","Even","None",
"zero","one","?","?"};
pri_message(ctrl, "%c Stop bits: %s, data bits: %s, "
"parity: %s\n", prefix,
stop_bits[(data & 0x60) >> 5],
data_bits[(data & 0x18) >> 3],
parity[(data & 0x7)]);
pos++;
}
/* octet 5d? */
if (pos < len && !(ie->data[pos-1] & 0x80)) {
u_int8_t data = ie->data[pos];
pri_message(ctrl, "%c Duplex mode: %d, modem type: %d\n",
prefix, (data & 0x40) ? 1 : 0,data & 0x3F);
pos++;
}
}
/* Look for octet 6; this is identified by bits 5,6 == 10 */
if (pos < len &&
(ie->data[pos] & 0x60) == 0x40) {
pri_message(ctrl, "%c User information layer 2: %s (%d)\n",
prefix, l22str(ie->data[pos] & 0x1f),
ie->data[pos] & 0x1f);
pos++;
}
/* Look for octet 7; this is identified by bits 5,6 == 11 */
if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
pri_message(ctrl, "%c User information layer 3: %s (%d)\n",
prefix, l32str(ie->data[pos] & 0x1f),
ie->data[pos] & 0x1f);
pos++;
/* octets 7a and 7b? */
if (pos + 1 < len && !(ie->data[pos-1] & 0x80) &&
!(ie->data[pos] & 0x80)) {
unsigned int proto;
proto = ((ie->data[pos] & 0xF) << 4 ) |
(ie->data[pos+1] & 0xF);
pri_message(ctrl, "%c Network layer: 0x%x\n", prefix,
proto );
pos += 2;
}
}
}
static int receive_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int pos = 2;
switch (ie->data[0] & 0x60) {
case 0x00:/* ITU-T standardized coding */
call->bc.transcapability = ie->data[0] & 0x1f;
call->bc.transmoderate = ie->data[1] & 0x7f;
/* octet 4.1 exists if mode/rate is multirate */
if (call->bc.transmoderate == TRANS_MODE_MULTIRATE) {
call->bc.transmultiple = ie->data[pos++] & 0x7f;
}
/* Look for octet 5; this is identified by bits 5,6 == 01 */
if (pos < len && (ie->data[pos] & 0x60) == 0x20) {
/* although the layer1 is only the bottom 5 bits of the byte,
previous versions of this library passed bits 5&6 through
too, so we have to do the same for binary compatability */
call->bc.userl1 = ie->data[pos] & 0x7f;
pos++;
/* octet 5a? */
if (pos < len && !(ie->data[pos-1] & 0x80)) {
call->bc.rateadaption = ie->data[pos] & 0x7f;
pos++;
}
/* octets 5b through 5d? */
while (pos < len && !(ie->data[pos-1] & 0x80)) {
pos++;
}
}
/* Look for octet 6; this is identified by bits 5,6 == 10 */
if (pos < len && (ie->data[pos] & 0x60) == 0x40) {
call->bc.userl2 = ie->data[pos++] & 0x1f;
}
/* Look for octet 7; this is identified by bits 5,6 == 11 */
if (pos < len && (ie->data[pos] & 0x60) == 0x60) {
call->bc.userl3 = ie->data[pos++] & 0x1f;
}
break;
case 0x20:/* ISO/IEC standard */
if (ie->data[0] == 0xa8 && ie->data[1] == 0x80) {
/*
* Q.SIG uses for CIS calls. ECMA-165 Section 11.3.1
* This mandatory ie is more or less a place holder in this case.
*/
call->bc.transcapability = PRI_TRANS_CAP_DIGITAL;
call->bc.transmoderate = TRANS_MODE_64_CIRCUIT;
break;
}
/* Fall through */
default:
pri_error(ctrl, "!! Coding-standard field is not Q.931.\n");
return -1;
}
return 0;
}
static int transmit_bearer_capability(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
int tc;
int pos;
/* We are ready to transmit single IE only */
if(order > 1)
return 0;
if (ctrl->link.next && !ctrl->bri) {
/* Bearer capability is *hard coded* in GR-303 */
ie->data[0] = 0x88;
ie->data[1] = 0x90;
return 4;
}
if (call->cis_call) {
ie->data[0] = 0xa8;
ie->data[1] = 0x80;
return 4;
}
tc = call->bc.transcapability;
ie->data[0] = 0x80 | tc;
ie->data[1] = call->bc.transmoderate | 0x80;
pos = 2;
/* octet 4.1 exists if mode/rate is multirate */
if (call->bc.transmoderate == TRANS_MODE_MULTIRATE) {
ie->data[pos++] = call->bc.transmultiple | 0x80;
}
if ((tc & PRI_TRANS_CAP_DIGITAL) && (ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) &&
(call->bc.transmoderate == TRANS_MODE_PACKET)) {
/* Apparently EuroISDN switches don't seem to like user layer 2/3 */
return 4;
}
if ((tc & PRI_TRANS_CAP_DIGITAL) && (call->bc.transmoderate == TRANS_MODE_64_CIRCUIT)) {
/* Unrestricted digital 64k data calls don't use user layer 2/3 */
return 4;
}
if (call->bc.transmoderate != TRANS_MODE_PACKET) {
/* If you have an AT&T 4ESS, you don't send any more info */
if ((ctrl->switchtype != PRI_SWITCH_ATT4ESS) && (call->bc.userl1 > -1)) {
ie->data[pos++] = call->bc.userl1 | 0x80; /* XXX Ext bit? XXX */
if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
ie->data[pos++] = call->bc.rateadaption | 0x80;
}
return pos + 2;
}
ie->data[pos++] = 0xa0 | (call->bc.userl1 & 0x1f);
if (call->bc.userl1 == PRI_LAYER_1_ITU_RATE_ADAPT) {
ie->data[pos-1] &= ~0x80; /* clear EXT bit in octet 5 */
ie->data[pos++] = call->bc.rateadaption | 0x80;
}
}
if (call->bc.userl2 != -1)
ie->data[pos++] = 0xc0 | (call->bc.userl2 & 0x1f);
if (call->bc.userl3 != -1)
ie->data[pos++] = 0xe0 | (call->bc.userl3 & 0x1f);
return pos + 2;
}
char *pri_plan2str(int plan)
{
static struct msgtype plans[] = {
{ PRI_INTERNATIONAL_ISDN, "International number in ISDN" },
{ PRI_NATIONAL_ISDN, "National number in ISDN" },
{ PRI_LOCAL_ISDN, "Local number in ISDN" },
{ PRI_PRIVATE, "Private numbering plan" },
{ PRI_UNKNOWN, "Unknown numbering plan" },
};
return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
}
static char *npi2str(int plan)
{
static struct msgtype plans[] = {
{ PRI_NPI_UNKNOWN, "Unknown Number Plan" },
{ PRI_NPI_E163_E164, "ISDN/Telephony Numbering Plan (E.164/E.163)" },
{ PRI_NPI_X121, "Data Numbering Plan (X.121)" },
{ PRI_NPI_F69, "Telex Numbering Plan (F.69)" },
{ PRI_NPI_NATIONAL, "National Standard Numbering Plan" },
{ PRI_NPI_PRIVATE, "Private Numbering Plan" },
{ PRI_NPI_RESERVED, "Reserved Number Plan" },
};
return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
}
static char *ton2str(int plan)
{
static struct msgtype plans[] = {
{ PRI_TON_UNKNOWN, "Unknown Number Type" },
{ PRI_TON_INTERNATIONAL, "International Number" },
{ PRI_TON_NATIONAL, "National Number" },
{ PRI_TON_NET_SPECIFIC, "Network Specific Number" },
{ PRI_TON_SUBSCRIBER, "Subscriber Number" },
{ PRI_TON_ABBREVIATED, "Abbreviated number" },
{ PRI_TON_RESERVED, "Reserved Number" },
};
return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
}
static char *subaddrtype2str(int plan)
{
static struct msgtype plans[] = {
{ 0, "NSAP (X.213/ISO 8348 AD2)" },
{ 2, "User Specified" },
};
return code2str(plan, plans, sizeof(plans) / sizeof(plans[0]));
}
/* Calling Party Category (Definitions from Q.763) */
static char *cpc2str(int plan)
{
static struct msgtype plans[] = {
{ 0, "Unknown Source" },
{ 1, "Operator French" },
{ 2, "Operator English" },
{ 3, "Operator German" },
{ 4, "Operator Russian" },
{ 5, "Operator Spanish" },
{ 6, "Mut Agree Chinese" },
{ 7, "Mut Agreement" },
{ 8, "Mut Agree Japanese" },
{ 9, "National Operator" },
{ 10, "Ordinary Toll Caller" },
{ 11, "Priority Toll Caller" },
{ 12, "Data Call" },
{ 13, "Test Call" },
{ 14, "Spare" },
{ 15, "Pay Phone" },
};
return code2str(plan, plans, ARRAY_LEN(plans));
}
char *pri_pres2str(int pres)
{
static struct msgtype press[] = {
{ PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED, "Presentation allowed, User-provided, not screened" },
{ PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN, "Presentation allowed, User-provided, verified and passed" },
{ PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN, "Presentation allowed, User-provided, verified and failed" },
{ PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER, "Presentation allowed, Network provided" },
{ PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED, "Presentation restricted, User-provided, not screened" },
{ PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN, "Presentation restricted, User-provided, verified and passed" },
{ PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN, "Presentation restricted, User-provided, verified and failed" },
{ PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER, "Presentation restricted, Network provided" },
{ PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED, "Number not available, User-provided, not screened" },
{ PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_PASSED_SCREEN, "Number not available, User-provided, verified and passed" },
{ PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_FAILED_SCREEN, "Number not available, User-provided, verified and failed" },
{ PRI_PRES_UNAVAILABLE | PRI_PRES_NETWORK_NUMBER, "Number not available, Network provided" },
{ PRI_PRES_RESERVED | PRI_PRES_USER_NUMBER_UNSCREENED, "Reserved, User-provided, not screened" },
{ PRI_PRES_RESERVED | PRI_PRES_USER_NUMBER_PASSED_SCREEN, "Reserved, User-provided, verified and passed" },
{ PRI_PRES_RESERVED | PRI_PRES_USER_NUMBER_FAILED_SCREEN, "Reserved, User-provided, verified and failed" },
{ PRI_PRES_RESERVED | PRI_PRES_NETWORK_NUMBER, "Reserved, Network provided" },
};
pres &= (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
return code2str(pres, press, sizeof(press) / sizeof(press[0]));
}
static void q931_get_subaddr_specific(unsigned char *num, int maxlen, unsigned char *src, int len, char oddflag)
{
/* User Specified */
int x;
char *ptr = (char *) num;
if (len <= 0) {
num[0] = '\0';
return;
}
if (((len * 2) + 1) > maxlen) {
len = (maxlen / 2) - 1;
}
for (x = 0; x < (len - 1); ++x) {
ptr += sprintf(ptr, "%02x", src[x]);
}
if (oddflag) {
/* ODD */
sprintf(ptr, "%01x", (src[len - 1]) >> 4);
} else {
/* EVEN */
sprintf(ptr, "%02x", src[len - 1]);
}
}
static int transmit_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len, int order)
{
size_t datalen;
if (!q931_subaddress->valid) {
return 0;
}
datalen = q931_subaddress->length;
if (!q931_subaddress->type) {
/* 0 = NSAP */
/* 0 = Odd/Even indicator */
ie->data[0] = 0x80;
} else {
/* 2 = User Specified */
ie->data[0] = q931_subaddress->odd_even_indicator ? 0xA8 : 0xA0;
}
memcpy(ie->data + offset, q931_subaddress->data, datalen);
return datalen + (offset + 2);
}
static int receive_subaddr_helper(int full_ie, struct pri *ctrl, struct q931_party_subaddress *q931_subaddress, int msgtype, q931_ie *ie, int offset, int len)
{
if (len <= 0) {
return -1;
}
q931_subaddress->valid = 1;
q931_subaddress->length = len;
/* type: 0 = NSAP, 2 = User Specified */
q931_subaddress->type = ((ie->data[0] & 0x70) >> 4);
q931_subaddress->odd_even_indicator = (ie->data[0] & 0x08) ? 1 : 0;
q931_memget(q931_subaddress->data, sizeof(q931_subaddress->data),
ie->data + offset, len);
return 0;
}
static void dump_subaddr_helper(int full_ie, struct pri *ctrl, q931_ie *ie, int offset, int len, int datalen, char prefix)
{
unsigned char cnum[256];
if (!(ie->data[0] & 0x70)) {
/* NSAP Get it as a string for dump display purposes only. */
q931_strget(cnum, sizeof(cnum), ie->data + offset, datalen);
} else {
/* User Specified */
q931_get_subaddr_specific(cnum, sizeof(cnum), ie->data + offset, datalen,
ie->data[0] & 0x08);
}
pri_message(ctrl,
"%c %s (len=%2d) [ Ext: %d Type: %s (%d) O: %d '%s' ]\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
subaddrtype2str((ie->data[0] & 0x70) >> 4), (ie->data[0] & 0x70) >> 4,
(ie->data[0] & 0x08) >> 3, cnum);
}
static void dump_called_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char cnum[256];
q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d) '%s' ]\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f, cnum);
}
static void dump_called_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
}
static void dump_calling_party_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char cnum[256];
if (ie->data[0] & 0x80) {
q931_strget(cnum, sizeof(cnum), ie->data + 1, len - 3);
} else {
q931_strget(cnum, sizeof(cnum), ie->data + 2, len - 4);
}
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
if (ie->data[0] & 0x80) {
pri_message(ctrl, "%c Presentation: %s (%d) '%s' ]\n",
prefix, pri_pres2str(0), 0, cnum);
} else {
pri_message(ctrl, "%c Presentation: %s (%d) '%s' ]\n",
prefix, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f, cnum);
}
}
static void dump_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
}
static void dump_calling_party_category(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d Cat: %s (%d) ]\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
cpc2str(ie->data[0] & 0x0F), ie->data[0] & 0x0F);
}
static void dump_redirecting_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char cnum[256];
int i = 0;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch(i) {
case 0: /* Octet 3 */
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
break;
case 1: /* Octet 3a */
pri_message(ctrl, "\n");
pri_message(ctrl, "%c Ext: %d Presentation: %s (%d)",
prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
break;
case 2: /* Octet 3b */
pri_message(ctrl, "\n");
pri_message(ctrl, "%c Ext: %d Reason: %s (%d)",
prefix, ie->data[2] >> 7, redirection_reason2str(ie->data[2] & 0x7f), ie->data[2] & 0x7f);
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
pri_message(ctrl, " '%s' ]\n", cnum);
}
static void dump_redirection_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char cnum[256];
int i = 0;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch (i) {
case 0: /* Octet 3 */
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
break;
case 1: /* Octet 3a */
pri_message(ctrl, "\n");
pri_message(ctrl, "%c Ext: %d Presentation: %s (%d)",
prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
pri_message(ctrl, " '%s' ]\n", cnum);
}
static int receive_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int i = 0;
/* Reference Q.951 Section 5.4.1 for ie */
call->connected_number_in_message = 1;
call->remote_id.number.valid = 1;
call->remote_id.number.presentation =
PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch (i) {
case 0:
call->remote_id.number.plan = ie->data[i] & 0x7f;
/*
* Work around a bug in a Lucent switch implementation that
* sets the extension bit in octet 3 even though octet 3a
* is present.
*
* The same issue was seen in a NI2 switch implementation.
* It was probably a Lucent switch configured for NI2
* operation. To avoid further surprises, I'm going to
* enable the work around for all North American switch
* types.
*/
if (ie->data[i] & 0x80) {
/* Octet 3 extension bit is set */
if (ctrl->switchtype != PRI_SWITCH_LUCENT5E
&& ctrl->switchtype != PRI_SWITCH_ATT4ESS
&& ctrl->switchtype != PRI_SWITCH_NI1
&& ctrl->switchtype != PRI_SWITCH_NI2
&& ctrl->switchtype != PRI_SWITCH_DMS100) {
/* Not a potentially buggy switch type. */
break;
}
if (!(ie->data[i + 1] & 0x80)) {
/*
* The possible octet 3a doesn't have the extension
* bit set. It is likely not the erroneous octet 3a.
*/
break;
}
}
/* Octet 3a is present */
++i;
/* Fall through */
case 1:
/* Keep only the presentation and screening fields */
call->remote_id.number.presentation =
ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->remote_id.number.str,
sizeof(call->remote_id.number.str), ie->data + i, ie->len - i);
return 0;
}
static int transmit_connected_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
if (!call->local_id.number.valid) {
return 0;
}
datalen = strlen(call->local_id.number.str);
ie->data[0] = call->local_id.number.plan;
ie->data[1] = 0x80 | call->local_id.number.presentation;
memcpy(ie->data + 2, call->local_id.number.str, datalen);
return datalen + (2 + 2);
}
static void dump_connected_number(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char cnum[256];
int i = 0;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch(i) {
case 0: /* Octet 3 */
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d TON: %s (%d) NPI: %s (%d)",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
ton2str((ie->data[0] >> 4) & 0x07), (ie->data[0] >> 4) & 0x07,
npi2str(ie->data[0] & 0x0f), ie->data[0] & 0x0f);
/*
* Work around a bug in a Lucent switch implementation that
* sets the extension bit in octet 3 even though octet 3a
* is present.
*
* The same issue was seen in a NI2 switch implementation.
* It was probably a Lucent switch configured for NI2
* operation. To avoid further surprises, I'm going to
* enable the work around for all North American switch
* types.
*/
if (ie->data[i] & 0x80) {
/* Octet 3 extension bit is set */
if (ctrl->switchtype != PRI_SWITCH_LUCENT5E
&& ctrl->switchtype != PRI_SWITCH_ATT4ESS
&& ctrl->switchtype != PRI_SWITCH_NI1
&& ctrl->switchtype != PRI_SWITCH_NI2
&& ctrl->switchtype != PRI_SWITCH_DMS100) {
/* Not a potentially buggy switch type. */
break;
}
if (!(ie->data[i + 1] & 0x80)) {
/*
* The possible octet 3a doesn't have the extension
* bit set. It is likely not the erroneous octet 3a.
*/
break;
}
pri_message(ctrl, "\n");
pri_message(ctrl, "%c Switch bug workaround.\n",
prefix);
pri_message(ctrl, "%c Assuming octet 3a is present.",
prefix);
}
/* Octet 3a is present */
++i;
/* Fall through */
case 1: /* Octet 3a */
pri_message(ctrl, "\n");
pri_message(ctrl, "%c Ext: %d Presentation: %s (%d)",
prefix, ie->data[1] >> 7, pri_pres2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
break;
}
} while(!(ie->data[i++]& 0x80));
q931_strget(cnum, sizeof(cnum), ie->data + i, ie->len - i);
pri_message(ctrl, " '%s' ]\n", cnum);
}
static int receive_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
if (len < 3) {
return -1;
}
return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
1, len - 3);
}
static int transmit_connected_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
1, len, order);
}
static void dump_connected_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
dump_subaddr_helper(full_ie, ctrl, ie, 1, len, len - 3, prefix);
}
static int receive_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int i = 0;
/* Reference Q.952 Section 4.1.2 for ie */
call->redirecting_number_in_message = 1;
call->redirecting.from.number.valid = 1;
call->redirecting.from.number.presentation =
PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
call->redirecting.reason = PRI_REDIR_UNKNOWN;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch (i) {
case 0:
call->redirecting.from.number.plan = ie->data[i] & 0x7f;
break;
case 1:
/* Keep only the presentation and screening fields */
call->redirecting.from.number.presentation =
ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
break;
case 2:
call->redirecting.reason = ie->data[i] & 0x0f;
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget_gripe(ctrl, ie2str(full_ie),
(unsigned char *) call->redirecting.from.number.str,
sizeof(call->redirecting.from.number.str), ie->data + i, ie->len - i);
return 0;
}
static int transmit_redirecting_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
if (order > 1)
return 0;
if (!call->redirecting.from.number.valid) {
return 0;
}
if (BRI_TE_PTMP(ctrl)) {
/*
* We should not send this ie to the network if we are the TE
* PTMP side since phones do not redirect calls within
* themselves. Well... If you consider someone else dialing the
* handset a redirection then how is the network to know?
*/
return 0;
}
if (call->redirecting.state != Q931_REDIRECTING_STATE_IDLE) {
/*
* There was a DivertingLegInformation2 in the message so the
* Q931_REDIRECTING_NUMBER ie is redundant. Some networks
* (Deutsche Telekom) complain about it.
*/
return 0;
}
datalen = strlen(call->redirecting.from.number.str);
ie->data[0] = call->redirecting.from.number.plan;
#if 1
/* ETSI and Q.952 do not define the screening field */
ie->data[1] = call->redirecting.from.number.presentation & PRI_PRES_RESTRICTION;
#else
/* Q.931 defines the screening field */
ie->data[1] = call->redirecting.from.number.presentation;
#endif
ie->data[2] = (call->redirecting.reason & 0x0f) | 0x80;
memcpy(ie->data + 3, call->redirecting.from.number.str, datalen);
return datalen + (3 + 2);
}
static void dump_redirecting_subaddr(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
dump_subaddr_helper(full_ie, ctrl, ie, 2, len, len - 4, prefix);
}
static int receive_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int i = 0;
/* Reference Q.952 Section 4.1.3 for ie */
call->redirection_number.valid = 1;
call->redirection_number.presentation =
PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch (i) {
case 0:
call->redirection_number.plan = ie->data[i] & 0x7f;
break;
case 1:
/* Keep only the presentation and screening fields */
call->redirection_number.presentation =
ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget_gripe(ctrl, ie2str(full_ie),
(unsigned char *) call->redirection_number.str,
sizeof(call->redirection_number.str), ie->data + i, ie->len - i);
return 0;
}
static int transmit_redirection_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
if (order > 1) {
return 0;
}
if (!call->redirection_number.valid) {
return 0;
}
datalen = strlen(call->redirection_number.str);
ie->data[0] = call->redirection_number.plan;
ie->data[1] = (call->redirection_number.presentation & PRI_PRES_RESTRICTION) | 0x80;
memcpy(ie->data + 2, call->redirection_number.str, datalen);
return datalen + (2 + 2);
}
static int receive_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
if (len < 3) {
return -1;
}
return receive_subaddr_helper(full_ie, ctrl, &call->remote_id.subaddress, msgtype, ie,
1, len - 3);
}
static int transmit_calling_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
return transmit_subaddr_helper(full_ie, ctrl, &call->local_id.subaddress, msgtype, ie,
1, len, order);
}
static int receive_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
if (len < 3) {
return -1;
}
return receive_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie, 1,
len - 3);
}
static int transmit_called_party_subaddr(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
return transmit_subaddr_helper(full_ie, ctrl, &call->called.subaddress, msgtype, ie,
1, len, order);
}
static int receive_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
size_t called_len;
size_t max_len;
char *called_end;
if (len < 3) {
return -1;
}
switch (msgtype) {
case Q931_FACILITY:
if (!q931_is_dummy_call(call)) {
/* Discard the number. */
return 0;
}
/* Fall through */
case Q931_REGISTER:
/* Accept the number for REGISTER only because it is so similar to SETUP. */
case Q931_SETUP:
q931_strget((unsigned char *) call->called.number.str,
sizeof(call->called.number.str), ie->data + 1, len - 3);
break;
case Q931_INFORMATION:
if (call->ourcallstate == Q931_CALL_STATE_OVERLAP_RECEIVING) {
/*
* Since we are receiving overlap digits now, we need to append
* them to any previously received digits in call->called.number.str.
*/
called_len = strlen(call->called.number.str);
called_end = call->called.number.str + called_len;
max_len = (sizeof(call->called.number.str) - 1) - called_len;
if (max_len < len - 3) {
called_len = max_len;
} else {
called_len = len - 3;
}
strncat(called_end, (char *) ie->data + 1, called_len);
}
break;
default:
/* Discard the number. */
return 0;
}
call->called.number.valid = 1;
call->called.number.plan = ie->data[0] & 0x7f;
q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->overlap_digits,
sizeof(call->overlap_digits), ie->data + 1, len - 3);
return 0;
}
static int transmit_called_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
if (!call->called.number.valid) {
return 0;
}
datalen = strlen(call->overlap_digits);
ie->data[0] = 0x80 | call->called.number.plan;
memcpy(ie->data + 1, call->overlap_digits, datalen);
return datalen + (1 + 2);
}
static int receive_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int i = 0;
struct q931_party_number number;
/* Reference Q.931 Section 4.5.10 for ie */
q931_party_number_init(&number);
number.valid = 1;
number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
/* To follow Q.931 (4.5.1), we must search for start of octet 4 by
walking through all bytes until one with ext bit (8) set to 1 */
do {
switch (i) {
case 0:
number.plan = ie->data[i] & 0x7f;
break;
case 1:
/* Keep only the presentation and screening fields */
number.presentation =
ie->data[i] & (PRI_PRES_RESTRICTION | PRI_PRES_NUMBER_TYPE);
break;
}
} while (!(ie->data[i++] & 0x80));
q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) number.str,
sizeof(number.str), ie->data + i, ie->len - i);
/* There can be more than one calling party number ie in the SETUP message. */
if (number.presentation == (PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER)
|| number.presentation == (PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER)) {
/* The number is network provided so it is an ANI number. */
call->ani = number;
if (!call->remote_id.number.valid) {
/* Copy ANI to CallerID if CallerID is not already set. */
call->remote_id.number = number;
}
} else {
call->remote_id.number = number;
}
return 0;
}
static int transmit_calling_party_number(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
if (!call->local_id.number.valid) {
return 0;
}
datalen = strlen(call->local_id.number.str);
ie->data[0] = call->local_id.number.plan;
ie->data[1] = 0x80 | call->local_id.number.presentation;
memcpy(ie->data + 2, call->local_id.number.str, datalen);
return datalen + (2 + 2);
}
static void dump_user_user(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int x;
pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
for (x = 0; x < ie->len; ++x) {
pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
}
pri_message(ctrl, " ]\n");
}
static int receive_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->useruserprotocoldisc = ie->data[0] & 0xff;
if (call->useruserprotocoldisc == 4) { /* IA5 */
q931_memget((unsigned char *) call->useruserinfo, sizeof(call->useruserinfo), ie->data + 1, len - 3);
}
return 0;
}
static int transmit_user_user(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
int datalen = strlen(call->useruserinfo);
if (datalen > 0) {
/* Restricted to 35 characters */
if (msgtype == Q931_USER_INFORMATION) {
if (datalen > 260)
datalen = 260;
} else {
if (datalen > 35)
datalen = 35;
}
ie->data[0] = 4; /* IA5 characters */
memcpy(&ie->data[1], call->useruserinfo, datalen);
call->useruserinfo[0] = '\0';
return datalen + 3;
}
return 0;
}
static void dump_change_status(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int x;
pri_message(ctrl, "%c %s (len=%2d) [", prefix, ie2str(full_ie), len);
for (x = 0; x < ie->len; ++x) {
pri_message(ctrl, " %02x", ie->data[x] & 0x7f);
}
pri_message(ctrl, " ]\n");
switch (ctrl->switchtype) {
case PRI_SWITCH_NI2:
case PRI_SWITCH_DMS100:
case PRI_SWITCH_LUCENT5E:
case PRI_SWITCH_ATT4ESS:
break;
default:
/* ie not supported by this switch type */
pri_message(ctrl, "%c %s is treated as unknown by current switch type.\n",
prefix, ie2str(full_ie));
break;
}
}
static int receive_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
switch (ctrl->switchtype) {
case PRI_SWITCH_NI2:
case PRI_SWITCH_DMS100:
case PRI_SWITCH_LUCENT5E:
case PRI_SWITCH_ATT4ESS:
break;
default:
/* ie not supported by this switch type */
return -1;
}
call->changestatus = ie->data[0] & 0x0f;
return 0;
}
static int transmit_change_status(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
ie->data[0] = 0xc0 | call->changestatus;
return 3;
}
static char *prog2str(int prog)
{
static struct msgtype progs[] = {
{ Q931_PROG_CALL_NOT_E2E_ISDN, "Call is not end-to-end ISDN; further call progress information may be available inband." },
{ Q931_PROG_CALLED_NOT_ISDN, "Called equipment is non-ISDN." },
{ Q931_PROG_CALLER_NOT_ISDN, "Calling equipment is non-ISDN." },
{ Q931_PROG_INBAND_AVAILABLE, "Inband information or appropriate pattern now available." },
{ Q931_PROG_DELAY_AT_INTERF, "Delay in response at called Interface." },
{ Q931_PROG_INTERWORKING_WITH_PUBLIC, "Interworking with a public network." },
{ Q931_PROG_INTERWORKING_NO_RELEASE, "Interworking with a network unable to supply a release signal." },
{ Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER, "Interworking with a network unable to supply a release signal before answer." },
{ Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER, "Interworking with a network unable to supply a release signal after answer." },
};
return code2str(prog, progs, sizeof(progs) / sizeof(progs[0]));
}
static char *coding2str(int cod)
{
static struct msgtype cods[] = {
{ CODE_CCITT, "CCITT (ITU) standard" },
{ CODE_INTERNATIONAL, "Non-ITU international standard" },
{ CODE_NATIONAL, "National standard" },
{ CODE_NETWORK_SPECIFIC, "Network specific standard" },
};
return code2str(cod, cods, sizeof(cods) / sizeof(cods[0]));
}
static char *loc2str(int loc)
{
static struct msgtype locs[] = {
{ LOC_USER, "User" },
{ LOC_PRIV_NET_LOCAL_USER, "Private network serving the local user" },
{ LOC_PUB_NET_LOCAL_USER, "Public network serving the local user" },
{ LOC_TRANSIT_NET, "Transit network" },
{ LOC_PUB_NET_REMOTE_USER, "Public network serving the remote user" },
{ LOC_PRIV_NET_REMOTE_USER, "Private network serving the remote user" },
{ LOC_INTERNATIONAL_NETWORK, "International network" },
{ LOC_NETWORK_BEYOND_INTERWORKING, "Network beyond the interworking point" },
};
return code2str(loc, locs, sizeof(locs) / sizeof(locs[0]));
}
static void dump_progress_indicator(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl,
"%c %s (len=%2d) [ Ext: %d Coding: %s (%d) 0: %d Location: %s (%d)\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
(ie->data[0] & 0x10) >> 4, loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
pri_message(ctrl, "%c Ext: %d Progress Description: %s (%d) ]\n",
prefix, ie->data[1] >> 7, prog2str(ie->data[1] & 0x7f), ie->data[1] & 0x7f);
}
static int receive_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
u_int8_t *data;
if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_BLOCK) {
return 0;
}
data = ie->data;
if (data[0] & 0x80) {
/* Skip over character set */
data++;
len--;
}
call->display.text = data;
call->display.full_ie = full_ie;
call->display.length = len - 2;
call->display.char_set = PRI_CHAR_SET_ISO8859_1;
return 0;
}
static int transmit_display(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
size_t datalen;
int i;
if (!call->display.text || !call->display.length) {
return 0;
}
if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_BLOCK) {
return 0;
}
i = 0;
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
case PRI_SWITCH_EUROISDN_E1:
case PRI_SWITCH_EUROISDN_T1:
break;
default:
/* Prefix text with character set indicator. */
ie->data[0] = 0xb1;
++i;
break;
}
datalen = call->display.length;
if (MAX_DISPLAY_TEXT < datalen + i) {
datalen = MAX_DISPLAY_TEXT - i;
}
memcpy(ie->data + i, call->display.text, datalen);
return 2 + i + datalen;
}
static int receive_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->progloc = ie->data[0] & 0xf;
call->progcode = (ie->data[0] & 0x60) >> 5;
call->progress = (ie->data[1] & 0x7f);
switch (call->progress) {
case Q931_PROG_CALL_NOT_E2E_ISDN:
call->progressmask |= PRI_PROG_CALL_NOT_E2E_ISDN;
break;
case Q931_PROG_CALLED_NOT_ISDN:
call->progressmask |= PRI_PROG_CALLED_NOT_ISDN;
break;
case Q931_PROG_CALLER_NOT_ISDN:
call->progressmask |= PRI_PROG_CALLER_NOT_ISDN;
break;
case Q931_PROG_CALLER_RETURNED_TO_ISDN:
call->progressmask |= PRI_PROG_CALLER_RETURNED_TO_ISDN;
break;
case Q931_PROG_INBAND_AVAILABLE:
call->progressmask |= PRI_PROG_INBAND_AVAILABLE;
break;
case Q931_PROG_DELAY_AT_INTERF:
call->progressmask |= PRI_PROG_DELAY_AT_INTERF;
break;
case Q931_PROG_INTERWORKING_WITH_PUBLIC:
call->progressmask |= PRI_PROG_INTERWORKING_WITH_PUBLIC;
break;
case Q931_PROG_INTERWORKING_NO_RELEASE:
call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE;
break;
case Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER:
call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
break;
case Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER:
call->progressmask |= PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
break;
default:
pri_error(ctrl, "XXX Invalid Progress indicator value received: %02x\n",(ie->data[1] & 0x7f));
return -1;
}
return 0;
}
static void q931_apdu_timeout(void *data);
static int transmit_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
struct apdu_event **prev;
struct apdu_event *cur;
int apdu_len;
for (prev = &call->apdus, cur = call->apdus;
cur;
prev = &cur->next, cur = cur->next) {
if (!cur->sent && (cur->message == msgtype || cur->message == Q931_ANY_MESSAGE)) {
break;
}
}
if (!cur) {
/* No APDU found */
return 0;
}
if (ctrl->debug & PRI_DEBUG_APDU) {
pri_message(ctrl, "Adding facility ie contents to send in %s message:\n",
msg2str(msgtype));
facility_decode_dump(ctrl, cur->apdu, cur->apdu_len);
}
if (len < cur->apdu_len) {
pri_error(ctrl,
"Could not fit facility ie in message. Size needed:%d Available space:%d\n",
cur->apdu_len + 2, len);
/* Remove APDU from list. */
*prev = cur->next;
if (cur->response.callback) {
/* Indicate to callback that the APDU had a problem getting sent. */
cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
}
free(cur);
return 0;
}
memcpy(ie->data, cur->apdu, cur->apdu_len);
apdu_len = cur->apdu_len;
cur->sent = 1;
if (cur->response.callback && cur->response.timeout_time) {
int failed;
if (0 < cur->response.timeout_time) {
/* Sender specified a timeout duration. */
cur->timer = pri_schedule_event(ctrl, cur->response.timeout_time,
q931_apdu_timeout, cur);
failed = !cur->timer;
} else {
/* Sender wants to "timeout" only when specified messages are received. */
failed = !cur->response.num_messages;
}
if (failed) {
/* Remove APDU from list. */
*prev = cur->next;
/* Indicate to callback that the APDU had a problem getting sent. */
cur->response.callback(APDU_CALLBACK_REASON_ERROR, ctrl, call, cur, NULL);
free(cur);
}
} else {
/* Remove APDU from list. */
*prev = cur->next;
free(cur);
}
return apdu_len + 2;
}
static int receive_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
/* Delay processing facility ie's till after all other ie's are processed. */
if (MAX_FACILITY_IES <= ctrl->facility.count) {
pri_message(ctrl, "!! Too many facility ie's to delay.\n");
return -1;
}
/* Make sure we have enough room for the protocol profile ie octet(s) */
if (ie->data + ie->len < ie->data + 2) {
return -1;
}
/* Save the facility ie location for delayed decode. */
ctrl->facility.ie[ctrl->facility.count] = ie;
ctrl->facility.codeset[ctrl->facility.count] = Q931_IE_CODESET((unsigned) full_ie);
++ctrl->facility.count;
return 0;
}
static int process_facility(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie)
{
struct fac_extension_header header;
struct rose_message rose;
const unsigned char *pos;
const unsigned char *end;
pos = ie->data;
end = ie->data + ie->len;
/* Make sure we have enough room for the protocol profile ie octet(s) */
if (end < pos + 2) {
return -1;
}
switch (*pos & Q932_PROTOCOL_MASK) {
case Q932_PROTOCOL_ROSE:
case Q932_PROTOCOL_EXTENSIONS:
break;
default:
case Q932_PROTOCOL_CMIP:
case Q932_PROTOCOL_ACSE:
if (ctrl->debug & PRI_DEBUG_APDU) {
pri_message(ctrl,
"!! Don't know how to handle Q.932 Protocol Profile type 0x%X\n",
*pos & Q932_PROTOCOL_MASK);
}
return -1;
}
if (!(*pos & 0x80)) {
/* DMS-100 Service indicator octet - Just ignore for now */
++pos;
}
++pos;
if (ctrl->debug & PRI_DEBUG_APDU) {
asn1_dump(ctrl, pos, end);
}
pos = fac_dec_extension_header(ctrl, pos, end, &header);
if (!pos) {
return -1;
}
if (header.npp_present) {
if (ctrl->debug & PRI_DEBUG_APDU) {
pri_message(ctrl,
"!! Don't know how to handle Network Protocol Profile type 0x%X\n",
header.npp);
}
return -1;
}
/* Process all components in the facility. */
while (pos < end) {
pos = rose_decode(ctrl, pos, end, &rose);
if (!pos) {
return -1;
}
switch (rose.type) {
case ROSE_COMP_TYPE_INVOKE:
rose_handle_invoke(ctrl, call, msgtype, ie, &header, &rose.component.invoke);
break;
case ROSE_COMP_TYPE_RESULT:
rose_handle_result(ctrl, call, msgtype, ie, &header, &rose.component.result);
break;
case ROSE_COMP_TYPE_ERROR:
rose_handle_error(ctrl, call, msgtype, ie, &header, &rose.component.error);
break;
case ROSE_COMP_TYPE_REJECT:
rose_handle_reject(ctrl, call, msgtype, ie, &header, &rose.component.reject);
break;
default:
return -1;
}
}
return 0;
}
static void q931_handle_facilities(struct pri *ctrl, q931_call *call, int msgtype)
{
unsigned idx;
unsigned codeset;
unsigned full_ie;
q931_ie *ie;
for (idx = 0; idx < ctrl->facility.count; ++idx) {
ie = ctrl->facility.ie[idx];
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
codeset = ctrl->facility.codeset[idx];
full_ie = Q931_FULL_IE(codeset, ie->ie);
pri_message(ctrl, "-- Delayed processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
}
process_facility(ctrl, call, msgtype, ie);
}
}
/*!
* \internal
* \brief Check if any APDU responses "timeout" with the current Q.931 message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg.
* \param msgtype Q.931 message type received.
*
* \return Nothing
*/
static void q931_apdu_msg_expire(struct pri *ctrl, struct q931_call *call, int msgtype)
{
struct apdu_event **prev;
struct apdu_event **prev_next;
struct apdu_event *cur;
unsigned idx;
for (prev = &call->apdus; *prev; prev = prev_next) {
cur = *prev;
prev_next = &cur->next;
if (cur->sent) {
for (idx = 0; idx < cur->response.num_messages; ++idx) {
if (cur->response.message_type[idx] == msgtype) {
/*
* APDU response message "timeout".
*
* Extract the APDU from the list so it cannot be
* deleted from under us by the callback.
*/
prev_next = prev;
*prev = cur->next;
/* Stop any response timeout. */
pri_schedule_del(ctrl, cur->timer);
cur->timer = 0;
cur->response.callback(APDU_CALLBACK_REASON_TIMEOUT, ctrl, call, cur, NULL);
free(cur);
break;
}
}
}
}
}
static int transmit_progress_indicator(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
int code;
int mask;
/* Can't send progress indicator on GR-303 -- EVER! */
if (ctrl->link.next && !ctrl->bri)
return 0;
if (call->progressmask > 0) {
if (call->progressmask & (mask = PRI_PROG_CALL_NOT_E2E_ISDN))
code = Q931_PROG_CALL_NOT_E2E_ISDN;
else if (call->progressmask & (mask = PRI_PROG_CALLED_NOT_ISDN))
code = Q931_PROG_CALLED_NOT_ISDN;
else if (call->progressmask & (mask = PRI_PROG_CALLER_NOT_ISDN))
code = Q931_PROG_CALLER_NOT_ISDN;
else if (call->progressmask & (mask = PRI_PROG_INBAND_AVAILABLE))
code = Q931_PROG_INBAND_AVAILABLE;
else if (call->progressmask & (mask = PRI_PROG_DELAY_AT_INTERF))
code = Q931_PROG_DELAY_AT_INTERF;
else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_WITH_PUBLIC))
code = Q931_PROG_INTERWORKING_WITH_PUBLIC;
else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE))
code = Q931_PROG_INTERWORKING_NO_RELEASE;
else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER))
code = Q931_PROG_INTERWORKING_NO_RELEASE_PRE_ANSWER;
else if (call->progressmask & (mask = PRI_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER))
code = Q931_PROG_INTERWORKING_NO_RELEASE_POST_ANSWER;
else {
code = 0;
pri_error(ctrl, "XXX Undefined progress bit: %x\n", call->progressmask);
}
if (code) {
ie->data[0] = 0x80 | (call->progcode << 5) | (call->progloc);
ie->data[1] = 0x80 | code;
call->progressmask &= ~mask;
return 4;
}
}
/* Leave off */
return 0;
}
static int transmit_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
ie->data[0] = Q931_CALL_STATE_NULL;
switch (call->ourcallstate) {
case Q931_CALL_STATE_NULL:
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_PRESENT:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
case Q931_CALL_STATE_DISCONNECT_REQUEST:
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_SUSPEND_REQUEST:
case Q931_CALL_STATE_RESUME_REQUEST:
case Q931_CALL_STATE_RELEASE_REQUEST:
case Q931_CALL_STATE_CALL_ABORT:
case Q931_CALL_STATE_OVERLAP_RECEIVING:
case Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE:
case Q931_CALL_STATE_RESTART_REQUEST:
case Q931_CALL_STATE_RESTART:
ie->data[0] = call->ourcallstate;
break;
case Q931_CALL_STATE_NOT_SET:
break;
}
return 3;
}
static int receive_call_state(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->sugcallstate = ie->data[0] & 0x3f;
return 0;
}
/*!
* \brief Convert the internal Q.931 call state to a string.
*
* \param callstate Internal Q.931 call state.
*
* \return String equivalent of the given Q.931 call state.
*/
const char *q931_call_state_str(enum Q931_CALL_STATE callstate)
{
static struct msgtype callstates[] = {
/* *INDENT-OFF* */
{ Q931_CALL_STATE_NULL, "Null" },
{ Q931_CALL_STATE_CALL_INITIATED, "Call Initiated" },
{ Q931_CALL_STATE_OVERLAP_SENDING, "Overlap Sending" },
{ Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING, "Outgoing Call Proceeding" },
{ Q931_CALL_STATE_CALL_DELIVERED, "Call Delivered" },
{ Q931_CALL_STATE_CALL_PRESENT, "Call Present" },
{ Q931_CALL_STATE_CALL_RECEIVED, "Call Received" },
{ Q931_CALL_STATE_CONNECT_REQUEST, "Connect Request" },
{ Q931_CALL_STATE_INCOMING_CALL_PROCEEDING, "Incoming Call Proceeding" },
{ Q931_CALL_STATE_ACTIVE, "Active" },
{ Q931_CALL_STATE_DISCONNECT_REQUEST, "Disconnect Request" },
{ Q931_CALL_STATE_DISCONNECT_INDICATION, "Disconnect Indication" },
{ Q931_CALL_STATE_SUSPEND_REQUEST, "Suspend Request" },
{ Q931_CALL_STATE_RESUME_REQUEST, "Resume Request" },
{ Q931_CALL_STATE_RELEASE_REQUEST, "Release Request" },
{ Q931_CALL_STATE_CALL_ABORT, "Call Abort" },
{ Q931_CALL_STATE_OVERLAP_RECEIVING, "Overlap Receiving" },
{ Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE, "Call Independent Service" },
{ Q931_CALL_STATE_RESTART_REQUEST, "Restart Request" },
{ Q931_CALL_STATE_RESTART, "Restart" },
{ Q931_CALL_STATE_NOT_SET, "Not set. Internal use only." },
/* *INDENT-ON* */
};
return code2str(callstate, callstates, ARRAY_LEN(callstates));
}
/*!
* \internal
* \brief Convert the Q.932 supplementary hold state to a string.
*
* \param state Q.932 supplementary hold state.
*
* \return String equivalent of the given hold state.
*/
static const char *q931_hold_state_str(enum Q931_HOLD_STATE state)
{
static struct msgtype hold_states[] = {
/* *INDENT-OFF* */
{ Q931_HOLD_STATE_IDLE, "Idle" },
{ Q931_HOLD_STATE_HOLD_REQ, "Hold Request" },
{ Q931_HOLD_STATE_HOLD_IND, "Hold Indication" },
{ Q931_HOLD_STATE_CALL_HELD, "Call Held" },
{ Q931_HOLD_STATE_RETRIEVE_REQ, "Retrieve Request" },
{ Q931_HOLD_STATE_RETRIEVE_IND, "Retrieve Indication" },
/* *INDENT-ON* */
};
return code2str(state, hold_states, ARRAY_LEN(hold_states));
}
static void dump_call_state(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d) [ Ext: %d Coding: %s (%d) Call state: %s (%d)\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
coding2str((ie->data[0] & 0xC0) >> 6), (ie->data[0] & 0xC0) >> 6,
q931_call_state_str(ie->data[0] & 0x3f), ie->data[0] & 0x3f);
}
static void dump_call_identity(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int x;
pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
for (x = 0; x < ie->len; ++x) {
pri_message(ctrl, "0x%02X ", ie->data[x]);
}
pri_message(ctrl, " ]\n");
}
static void dump_time_date(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), len);
if (ie->len > 0)
pri_message(ctrl, "%02d", ie->data[0]);
if (ie->len > 1)
pri_message(ctrl, "-%02d", ie->data[1]);
if (ie->len > 2)
pri_message(ctrl, "-%02d", ie->data[2]);
if (ie->len > 3)
pri_message(ctrl, " %02d", ie->data[3]);
if (ie->len > 4)
pri_message(ctrl, ":%02d", ie->data[4]);
if (ie->len > 5)
pri_message(ctrl, ":%02d", ie->data[5]);
pri_message(ctrl, " ]\n");
}
static int receive_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
/* Ignore incoming Date/Time since we have no use for it at this time. */
return 0;
}
static int transmit_time_date(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
time_t now;
struct tm timedate;
int ie_len;
do {
if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE) {
ie_len = 0;
break;
}
/* Send the current date/time. */
time(&now);
localtime_r(&now, &timedate);
ie->data[0] = timedate.tm_year - 100; /* 1900+ */
ie->data[1] = timedate.tm_mon + 1;
ie->data[2] = timedate.tm_mday;
ie_len = 2 + 3;
if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HH) {
break;
}
/* Add optional hour. */
ie->data[3] = timedate.tm_hour;
++ie_len;
if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMM) {
break;
}
/* Add optional minutes. */
ie->data[4] = timedate.tm_min;
++ie_len;
if (ctrl->date_time_send < PRI_DATE_TIME_SEND_DATE_HHMMSS) {
break;
}
/* Add optional seconds. */
ie->data[5] = timedate.tm_sec;
++ie_len;
} while (0);
return ie_len;
}
static void dump_keypad_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
unsigned char tmp[64];
q931_strget(tmp, sizeof(tmp), ie->data, ie->len);
pri_message(ctrl, "%c %s (len=%2d) [ %s ]\n", prefix, ie2str(full_ie), ie->len, tmp);
}
static int receive_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
if (ie->len == 0)
return -1;
q931_strget_gripe(ctrl, ie2str(full_ie), (unsigned char *) call->keypad_digits,
sizeof(call->keypad_digits), ie->data, ie->len);
return 0;
}
static int transmit_keypad_facility(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
int sublen;
sublen = strlen(call->keypad_digits);
if (sublen) {
libpri_copy_string((char *) ie->data, call->keypad_digits, sizeof(call->keypad_digits));
return sublen + 2;
} else
return 0;
}
static void dump_display(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int x;
unsigned char buf[2*80 + 1];
char tmp[20 + 1];
x = 0;
if (ie->len && (ie->data[x] & 0x80)) {
snprintf(tmp, sizeof(tmp), "Charset: %02x ", ie->data[x] & 0x7f);
++x;
} else {
tmp[0] = '\0';
}
q931_strget(buf, sizeof(buf), &ie->data[x], ie->len - x);
pri_message(ctrl, "%c %s (len=%2d) %s[ %s ]\n",
prefix, ie2str(full_ie), ie->len, tmp, buf);
}
#define CHECK_OVERFLOW(limit) \
if (tmpptr - tmp + limit >= sizeof(tmp)) { \
*tmpptr = '\0'; \
pri_message(ctrl, "%s", tmpptr = tmp); \
}
static void dump_ie_data(struct pri *ctrl, unsigned char *c, int len)
{
static char hexs[16] = "0123456789ABCDEF";
char tmp[1024], *tmpptr;
int lastascii = 0;
tmpptr = tmp;
for (; len; --len, ++c) {
CHECK_OVERFLOW(7);
if (isprint(*c)) {
if (!lastascii) {
if (tmpptr != tmp) {
*tmpptr++ = ',';
*tmpptr++ = ' ';
}
*tmpptr++ = '\'';
lastascii = 1;
}
*tmpptr++ = *c;
} else {
if (lastascii) {
*tmpptr++ = '\'';
lastascii = 0;
}
if (tmpptr != tmp) {
*tmpptr++ = ',';
*tmpptr++ = ' ';
}
*tmpptr++ = '0';
*tmpptr++ = 'x';
*tmpptr++ = hexs[(*c >> 4) & 0x0f];
*tmpptr++ = hexs[(*c) & 0x0f];
}
}
if (lastascii)
*tmpptr++ = '\'';
*tmpptr = '\0';
pri_message(ctrl, "%s", tmp);
}
static void dump_facility(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d, codeset=%d) [ ",
prefix, ie2str(full_ie), len, Q931_IE_CODESET(full_ie));
dump_ie_data(ctrl, ie->data, ie->len);
pri_message(ctrl, " ]\n");
#if 0 /* Lets not dump parse of facility contents here anymore. */
/*
* The ASN.1 decode dump has already been done when the facility ie was added to the outgoing
* message or the ASN.1 decode dump will be done when the facility ie is processed on incoming
* messages. This dump is redundant and very noisy.
*/
if (ie->len > 1) {
int dataat = (ie->data[0] & 0x80) ? 1 : 2;
pri_message(ctrl, "PROTOCOL %02X\n", ie->data[0] & Q932_PROTOCOL_MASK);
asn1_dump(ctrl, ie->data + dataat, ie->data + ie->len);
}
#endif /* Lets not dump parse of facility contents here anymore. */
}
static void dump_network_spec_fac(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d) [ ", prefix, ie2str(full_ie), ie->len);
if (ie->data[0] == 0x00) {
pri_message(ctrl, "%s", code2str(ie->data[1], facilities, ARRAY_LEN(facilities)));
} else {
dump_ie_data(ctrl, ie->data, ie->len);
}
pri_message(ctrl, " ]\n");
}
static int receive_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
return 0;
}
static int transmit_network_spec_fac(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
/* We are ready to transmit single IE only */
if (order > 1)
return 0;
if (ctrl->nsf != PRI_NSF_NONE) {
ie->data[0] = 0x00;
ie->data[1] = ctrl->nsf;
return 4;
}
/* Leave off */
return 0;
}
char *pri_cause2str(int cause)
{
return code2str(cause, causes, sizeof(causes) / sizeof(causes[0]));
}
static char *pri_causeclass2str(int cause)
{
static struct msgtype causeclasses[] = {
{ 0, "Normal Event" },
{ 1, "Normal Event" },
{ 2, "Network Congestion (resource unavailable)" },
{ 3, "Service or Option not Available" },
{ 4, "Service or Option not Implemented" },
{ 5, "Invalid message (e.g. parameter out of range)" },
{ 6, "Protocol Error (e.g. unknown message)" },
{ 7, "Interworking" },
};
return code2str(cause, causeclasses, sizeof(causeclasses) / sizeof(causeclasses[0]));
}
static void dump_cause(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int x;
int cause_ind = 1;
int data_ind = 2;
pri_message(ctrl,
"%c %s (len=%2d) [ Ext: %d Coding: %s (%d) Spare: %d Location: %s (%d)\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
coding2str((ie->data[0] & 0x60) >> 5), (ie->data[0] & 0x60) >> 5,
(ie->data[0] & 0x10) >> 4, loc2str(ie->data[0] & 0xf), ie->data[0] & 0xf);
if ((ie->data[0] & 0x80) == 0) {
const char *recommendation;
switch (ie->data[1] & 0x7f) {
case 0x00:
recommendation = "Q.931";
break;
case 0x03:
recommendation = "X.21";
break;
case 0x04:
recommendation = "X.25";
break;
case 0x05:
recommendation = "Q.1031/Q.1051";
break;
default:
recommendation = "Reserved";
break;
}
pri_message(ctrl, "%c Ext: %d Recommendation: %s (%d)\n",
prefix, (ie->data[1] >> 7), recommendation, ie->data[1] & 0x7f);
cause_ind = 2;
data_ind = 3;
}
pri_message(ctrl, "%c Ext: %d Cause: %s (%d), class = %s (%d) ]\n",
prefix, (ie->data[cause_ind] >> 7), pri_cause2str(ie->data[cause_ind] & 0x7f), ie->data[cause_ind] & 0x7f,
pri_causeclass2str((ie->data[cause_ind] & 0x7f) >> 4), (ie->data[cause_ind] & 0x7f) >> 4);
if (ie->len <= data_ind) {
return;
}
/* Dump cause data in readable form */
switch(ie->data[cause_ind] & 0x7f) {
case PRI_CAUSE_IE_NONEXIST:
for (x = data_ind; x < ie->len; x++) {
pri_message(ctrl, "%c Cause data %d: %02x (%d, %s IE)\n", prefix, x-1, ie->data[x], ie->data[x], ie2str(ie->data[x]));
}
break;
case PRI_CAUSE_WRONG_CALL_STATE:
for (x = data_ind; x < ie->len; x++) {
pri_message(ctrl, "%c Cause data %d: %02x (%d, %s message)\n", prefix, x-1, ie->data[x], ie->data[x], msg2str(ie->data[x]));
}
break;
case PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE:
pri_message(ctrl, "%c Cause data:", prefix);
for (x = data_ind; x < ie->len; x++) {
pri_message(ctrl, " %02x", ie->data[x]);
}
pri_message(ctrl, " (Timer T");
for (x = data_ind; x < ie->len; x++) {
pri_message(ctrl, "%c", ((ie->data[x] >= ' ') && (ie->data[x] < 0x7f)) ? ie->data[x] : '.');
}
pri_message(ctrl, ")\n");
break;
default:
for (x = data_ind; x < ie->len; x++) {
pri_message(ctrl, "%c Cause data %d: %02x (%d)\n", prefix, x-1, ie->data[x], ie->data[x]);
}
break;
}
}
static int receive_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->causeloc = ie->data[0] & 0xf;
call->causecode = (ie->data[0] & 0x60) >> 5;
if (ie->data[0] & 0x80) {
call->cause = (ie->data[1] & 0x7f);
} else {
call->cause = (ie->data[2] & 0x7f);
}
return 0;
}
static int transmit_cause(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
/* We are ready to transmit single IE only */
if (order > 1)
return 0;
if (call->cause > 0) {
ie->data[0] = 0x80 | (call->causecode << 5) | (call->causeloc);
ie->data[1] = 0x80 | (call->cause);
return 4;
} else {
/* Leave off */
return 0;
}
}
static void dump_sending_complete(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d)\n", prefix, ie2str(full_ie), len);
}
static int receive_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
/* We've got a "Complete" message: Exect no further digits. */
call->complete = 1;
return 0;
}
static int transmit_sending_complete(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
if ((ctrl->overlapdial && call->complete) || /* Explicit */
(!ctrl->overlapdial && ((ctrl->switchtype == PRI_SWITCH_EUROISDN_E1) ||
/* Implicit */ (ctrl->switchtype == PRI_SWITCH_EUROISDN_T1)))) {
/* Include this single-byte IE */
return 1;
}
return 0;
}
static char *notify2str(int info)
{
/* ITU-T Q.763 */
static struct msgtype notifies[] = {
{ PRI_NOTIFY_USER_SUSPENDED, "User suspended" },
{ PRI_NOTIFY_USER_RESUMED, "User resumed" },
{ PRI_NOTIFY_BEARER_CHANGE, "Bearer service change (DSS1)" },
{ PRI_NOTIFY_ASN1_COMPONENT, "ASN.1 encoded component (DSS1)" },
{ PRI_NOTIFY_COMPLETION_DELAY, "Call completion delay" },
{ PRI_NOTIFY_CONF_ESTABLISHED, "Conference established" },
{ PRI_NOTIFY_CONF_DISCONNECTED, "Conference disconnected" },
{ PRI_NOTIFY_CONF_PARTY_ADDED, "Other party added" },
{ PRI_NOTIFY_CONF_ISOLATED, "Isolated" },
{ PRI_NOTIFY_CONF_REATTACHED, "Reattached" },
{ PRI_NOTIFY_CONF_OTHER_ISOLATED, "Other party isolated" },
{ PRI_NOTIFY_CONF_OTHER_REATTACHED, "Other party reattached" },
{ PRI_NOTIFY_CONF_OTHER_SPLIT, "Other party split" },
{ PRI_NOTIFY_CONF_OTHER_DISCONNECTED, "Other party disconnected" },
{ PRI_NOTIFY_CONF_FLOATING, "Conference floating" },
{ PRI_NOTIFY_WAITING_CALL, "Call is waiting call" },
{ PRI_NOTIFY_DIVERSION_ACTIVATED, "Diversion activated (DSS1)" },
{ PRI_NOTIFY_TRANSFER_ALERTING, "Call transfer, alerting" },
{ PRI_NOTIFY_TRANSFER_ACTIVE, "Call transfer, active" },
{ PRI_NOTIFY_REMOTE_HOLD, "Remote hold" },
{ PRI_NOTIFY_REMOTE_RETRIEVAL, "Remote retrieval" },
{ PRI_NOTIFY_CALL_DIVERTING, "Call is diverting" },
};
return code2str(info, notifies, sizeof(notifies) / sizeof(notifies[0]));
}
static void dump_notify(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%2d): Ext: %d %s (%d)\n",
prefix, ie2str(full_ie), len, ie->data[0] >> 7,
notify2str(ie->data[0] & 0x7f), ie->data[0] & 0x7f);
}
static int receive_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->notify = ie->data[0] & 0x7F;
return 0;
}
static int transmit_notify(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
if (call->notify >= 0) {
ie->data[0] = 0x80 | call->notify;
return 3;
}
return 0;
}
static void dump_shift(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %sLocking Shift (len=%02d): Requested codeset %d\n",
prefix, (full_ie & 8) ? "Non-" : "", len, full_ie & 7);
}
static char *lineinfo2str(int info)
{
/* NAPNA ANI II digits */
static struct msgtype lineinfo[] = {
{ 0, "Plain Old Telephone Service (POTS)" },
{ 1, "Multiparty line (more than 2)" },
{ 2, "ANI failure" },
{ 6, "Station Level Rating" },
{ 7, "Special Operator Handling Required" },
{ 20, "Automatic Identified Outward Dialing (AIOD)" },
{ 23, "Coing or Non-Coin" },
{ 24, "Toll free translated to POTS originated for non-pay station" },
{ 25, "Toll free translated to POTS originated from pay station" },
{ 27, "Pay station with coin control signalling" },
{ 29, "Prison/Inmate Service" },
{ 30, "Intercept (blank)" },
{ 31, "Intercept (trouble)" },
{ 32, "Intercept (regular)" },
{ 34, "Telco Operator Handled Call" },
{ 52, "Outward Wide Area Telecommunications Service (OUTWATS)" },
{ 60, "TRS call from unrestricted line" },
{ 61, "Cellular/Wireless PCS (Type 1)" },
{ 62, "Cellular/Wireless PCS (Type 2)" },
{ 63, "Cellular/Wireless PCS (Roaming)" },
{ 66, "TRS call from hotel/motel" },
{ 67, "TRS call from restricted line" },
{ 70, "Line connected to pay station" },
{ 93, "Private virtual network call" },
};
return code2str(info, lineinfo, sizeof(lineinfo) / sizeof(lineinfo[0]));
}
static void dump_line_information(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%02d): %s (%d)\n",
prefix, ie2str(full_ie), len, lineinfo2str(ie->data[0]), ie->data[0]);
}
static int receive_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->ani2 = ie->data[0];
return 0;
}
static int transmit_line_information(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
#if 0 /* XXX Is this IE possible for 4ESS only? XXX */
if(ctrl->switchtype == PRI_SWITCH_ATT4ESS) {
ie->data[0] = 0;
return 3;
}
#endif
return 0;
}
static char *gdencoding2str(int encoding)
{
static struct msgtype gdencoding[] = {
{ 0, "BCD even" },
{ 1, "BCD odd" },
{ 2, "IA5" },
{ 3, "Binary" },
};
return code2str(encoding, gdencoding, sizeof(gdencoding) / sizeof(gdencoding[0]));
}
static char *gdtype2str(int type)
{
static struct msgtype gdtype[] = {
{ 0, "Account Code" },
{ 1, "Auth Code" },
{ 2, "Customer ID" },
{ 3, "Universal Access" },
{ 4, "Info Digits" },
{ 5, "Callid" },
{ 6, "Opart" },
{ 7, "TCN" },
{ 9, "Adin" },
};
return code2str(type, gdtype, sizeof(gdtype) / sizeof(gdtype[0]));
}
static void dump_generic_digits(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
int encoding;
int type;
int idx;
int value;
if (len < 3) {
pri_message(ctrl, "%c %s (len=%02d): Invalid length\n",
prefix, ie2str(full_ie), len);
return;
}
encoding = (ie->data[0] >> 5) & 7;
type = ie->data[0] & 0x1F;
pri_message(ctrl, "%c %s (len=%02d): Encoding %s Type %s\n",
prefix, ie2str(full_ie), len, gdencoding2str(encoding), gdtype2str(type));
if (encoding == 3) { /* Binary */
pri_message(ctrl, "%c Don't know how to handle binary encoding\n",
prefix);
return;
}
if (len == 3) /* No number information */
return;
pri_message(ctrl, "%c Digits: ", prefix);
value = 0;
for(idx = 3; idx < len; ++idx) {
switch(encoding) {
case 0: /* BCD even */
case 1: /* BCD odd */
pri_message(ctrl, "%d", ie->data[idx-2] & 0x0f);
value = value * 10 + (ie->data[idx-2] & 0x0f);
if(!encoding || (idx+1 < len)) { /* Special handling for BCD odd */
pri_message(ctrl, "%d", (ie->data[idx-2] >> 4) & 0x0f);
value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
}
break;
case 2: /* IA5 */
pri_message(ctrl, "%c", ie->data[idx-2]);
value = value * 10 + ie->data[idx-2] - '0';
break;
}
}
switch(type) {
case 4: /* Info Digits */
pri_message(ctrl, " - %s", lineinfo2str(value));
break;
}
pri_message(ctrl, "\n");
}
static int receive_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
int encoding;
int type;
int idx;
int value;
int num_idx;
char number[260];
if (len < 3) {
pri_error(ctrl, "Invalid length of Generic Digits IE\n");
return -1;
}
encoding = (ie->data[0] >> 5) & 7;
type = ie->data[0] & 0x1F;
if (encoding == 3) { /* Binary */
pri_message(ctrl, "!! Unable to handle binary encoded Generic Digits IE\n");
return 0;
}
if (len == 3) /* No number information */
return 0;
value = 0;
switch(type) {
/* Integer value handling */
case 4: /* Info Digits */
for(idx = 3; idx < len; ++idx) {
switch(encoding) {
case 0: /* BCD even */
case 1: /* BCD odd */
value = value * 10 + (ie->data[idx-2] & 0x0f);
if(!encoding || (idx+1 < len)) /* Special handling for BCD odd */
value = value * 10 + ((ie->data[idx-2] >> 4) & 0x0f);
break;
case 2: /* IA5 */
value = value * 10 + (ie->data[idx-2] - '0');
break;
}
}
break;
/* String value handling */
case 5: /* Callid */
num_idx = 0;
for(idx = 3; (idx < len) && (num_idx < sizeof(number) - 4); ++idx) {
switch(encoding) {
case 0: /* BCD even */
case 1: /* BCD odd */
number[num_idx++] = '0' + (ie->data[idx-2] & 0x0f);
if(!encoding || (idx+1 < len)) /* Special handling for BCD odd */
number[num_idx++] = '0' + ((ie->data[idx-2] >> 4) & 0x0f);
break;
case 2:
number[num_idx++] = ie->data[idx-2];
break;
}
}
number[num_idx] = '\0';
break;
}
switch(type) {
case 4: /* Info Digits */
call->ani2 = value;
break;
#if 0
case 5: /* Callid */
if (!call->remote_id.number.valid) {
call->remote_id.number.valid = 1;
call->remote_id.number.presentation =
PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
call->remote_id.number.plan = PRI_UNKNOWN;
libpri_copy_string(call->remote_id.number.str, number,
sizeof(call->remote_id.number.str));
}
break;
#endif
}
return 0;
}
static int transmit_generic_digits(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
#if 0 /* XXX Is this IE possible for other switches? XXX */
if (order > 1)
return 0;
if(ctrl->switchtype == PRI_SWITCH_NI1) {
ie->data[0] = 0x04; /* BCD even, Info Digits */
ie->data[1] = 0x00; /* POTS */
return 4;
}
#endif
return 0;
}
static char *signal2str(int signal)
{
/* From Q.931 4.5.8 Table 4-24 */
static struct msgtype mtsignal[] = {
{ 0, "Dial tone" },
{ 1, "Ring back tone" },
{ 2, "Intercept tone" },
{ 3, "Network congestion tone" },
{ 4, "Busy tone" },
{ 5, "Confirm tone" },
{ 6, "Answer tone" },
{ 7, "Call waiting tone" },
{ 8, "Off-hook warning tone" },
{ 9, "Pre-emption tone" },
{ 63, "Tones off" },
{ 64, "Alerting on - pattern 0" },
{ 65, "Alerting on - pattern 1" },
{ 66, "Alerting on - pattern 2" },
{ 67, "Alerting on - pattern 3" },
{ 68, "Alerting on - pattern 4" },
{ 69, "Alerting on - pattern 5" },
{ 70, "Alerting on - pattern 6" },
{ 71, "Alerting on - pattern 7" },
{ 79, "Alerting off" },
};
return code2str(signal, mtsignal, sizeof(mtsignal) / sizeof(mtsignal[0]));
}
static void dump_signal(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
if (len < 3) {
pri_message(ctrl, "Invalid length\n");
return;
}
pri_message(ctrl, "Signal %s (%d)\n", signal2str(ie->data[0]), ie->data[0]);
}
static void dump_transit_count(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
/* Defined in ECMA-225 */
pri_message(ctrl, "%c %s (len=%02d): ", prefix, ie2str(full_ie), len);
if (len < 3) {
pri_message(ctrl, "Invalid length\n");
return;
}
pri_message(ctrl, "Count=%d (0x%02x)\n", ie->data[0] & 0x1f, ie->data[0] & 0x1f);
}
static void dump_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_ie *ie, int len, char prefix)
{
pri_message(ctrl, "%c %s (len=%02d): %d\n", prefix, ie2str(full_ie), len, ie->data[0] & 0x7);
}
static int receive_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len)
{
call->reversecharge = ie->data[0] & 0x7;
return 0;
}
static int transmit_reverse_charging_indication(int full_ie, struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie, int len, int order)
{
if (call->reversecharge != PRI_REVERSECHARGE_NONE) {
ie->data[0] = 0x80 | (call->reversecharge & 0x7);
return 3;
}
return 0;
}
static struct ie ies[] = {
/* Codeset 0 - Comprehension required ie's with varying support. */
{ 1, Q931_IE_SEGMENTED_MSG, "Segmented Message" }, /* Not supported. Just ie name is available for message decode. */
{ 1, Q931_IE_CHANGE_STATUS, "Change Status Information", dump_change_status, receive_change_status, transmit_change_status },
/* Codeset 0 - Common */
{ 0, Q931_LOCKING_SHIFT, "Locking Shift", dump_shift },
{ 0, Q931_BEARER_CAPABILITY, "Bearer Capability", dump_bearer_capability, receive_bearer_capability, transmit_bearer_capability },
{ 0, Q931_CAUSE, "Cause", dump_cause, receive_cause, transmit_cause },
{ 1, Q931_IE_CALL_STATE, "Call State", dump_call_state, receive_call_state, transmit_call_state },
{ 0, Q931_CHANNEL_IDENT, "Channel ID", dump_channel_id, receive_channel_id, transmit_channel_id },
{ 0, Q931_PROGRESS_INDICATOR, "Progress Indicator", dump_progress_indicator, receive_progress_indicator, transmit_progress_indicator },
{ 0, Q931_NETWORK_SPEC_FAC, "Network-Specific Facilities", dump_network_spec_fac, receive_network_spec_fac, transmit_network_spec_fac },
{ 1, Q931_INFORMATION_RATE, "Information Rate" },
{ 1, Q931_TRANSIT_DELAY, "End-to-End Transit Delay" },
{ 1, Q931_TRANS_DELAY_SELECT, "Transmit Delay Selection and Indication" },
{ 1, Q931_BINARY_PARAMETERS, "Packet-layer Binary Parameters" },
{ 1, Q931_WINDOW_SIZE, "Packet-layer Window Size" },
{ 1, Q931_CLOSED_USER_GROUP, "Closed User Group" },
{ 1, Q931_REVERSE_CHARGE_INDIC, "Reverse Charging Indication", dump_reverse_charging_indication, receive_reverse_charging_indication, transmit_reverse_charging_indication },
{ 1, Q931_CALLING_PARTY_NUMBER, "Calling Party Number", dump_calling_party_number, receive_calling_party_number, transmit_calling_party_number },
{ 1, Q931_CALLING_PARTY_SUBADDR, "Calling Party Subaddress", dump_calling_party_subaddr, receive_calling_party_subaddr, transmit_calling_party_subaddr },
{ 1, Q931_CALLED_PARTY_NUMBER, "Called Party Number", dump_called_party_number, receive_called_party_number, transmit_called_party_number },
{ 1, Q931_CALLED_PARTY_SUBADDR, "Called Party Subaddress", dump_called_party_subaddr, receive_called_party_subaddr, transmit_called_party_subaddr },
{ 0, Q931_REDIRECTING_NUMBER, "Redirecting Number", dump_redirecting_number, receive_redirecting_number, transmit_redirecting_number },
{ 1, Q931_REDIRECTING_SUBADDR, "Redirecting Subaddress", dump_redirecting_subaddr },
{ 0, Q931_TRANSIT_NET_SELECT, "Transit Network Selection" },
{ 1, Q931_RESTART_INDICATOR, "Restart Indicator", dump_restart_indicator, receive_restart_indicator, transmit_restart_indicator },
{ 0, Q931_LOW_LAYER_COMPAT, "Low-layer Compatibility" },
{ 0, Q931_HIGH_LAYER_COMPAT, "High-layer Compatibility" },
{ 1, Q931_PACKET_SIZE, "Packet Size" },
{ 0, Q931_IE_FACILITY, "Facility" , dump_facility, receive_facility, transmit_facility },
{ 1, Q931_IE_REDIRECTION_NUMBER, "Redirection Number", dump_redirection_number, receive_redirection_number, transmit_redirection_number },
{ 1, Q931_IE_REDIRECTION_SUBADDR, "Redirection Subaddress" },
{ 1, Q931_IE_FEATURE_ACTIVATE, "Feature Activation" },
{ 1, Q931_IE_INFO_REQUEST, "Feature Request" },
{ 1, Q931_IE_FEATURE_IND, "Feature Indication" },
{ 1, Q931_IE_CALL_IDENTITY, "Call Identity", dump_call_identity },
{ 1, Q931_IE_ENDPOINT_ID, "Endpoint Identification" },
{ 1, Q931_IE_NOTIFY_IND, "Notification Indicator", dump_notify, receive_notify, transmit_notify },
{ 1, Q931_DISPLAY, "Display", dump_display, receive_display, transmit_display },
{ 1, Q931_IE_TIME_DATE, "Date/Time", dump_time_date, receive_time_date, transmit_time_date },
{ 1, Q931_IE_KEYPAD_FACILITY, "Keypad Facility", dump_keypad_facility, receive_keypad_facility, transmit_keypad_facility },
{ 0, Q931_IE_SIGNAL, "Signal", dump_signal },
{ 1, Q931_IE_SWITCHHOOK, "Switch-hook" },
{ 1, Q931_IE_USER_USER, "User-User Information", dump_user_user, receive_user_user, transmit_user_user },
{ 1, Q931_IE_ESCAPE_FOR_EXT, "Escape for Extension" },
{ 1, Q931_IE_CALL_STATUS, "Call Status" },
{ 1, Q931_IE_CONNECTED_ADDR, "Connected Address", dump_connected_number, receive_connected_number, transmit_connected_number },
{ 1, Q931_IE_CONNECTED_NUM, "Connected Number", dump_connected_number, receive_connected_number, transmit_connected_number },
{ 1, Q931_IE_CONNECTED_SUBADDR, "Connected Subaddress", dump_connected_subaddr, receive_connected_subaddr, transmit_connected_subaddr },
{ 1, Q931_IE_ORIGINAL_CALLED_NUMBER, "Original Called Number", dump_redirecting_number, receive_redirecting_number, transmit_redirecting_number },
{ 1, Q931_IE_USER_USER_FACILITY, "User-User Facility" },
{ 1, Q931_IE_UPDATE, "Update" },
{ 1, Q931_SENDING_COMPLETE, "Sending Complete", dump_sending_complete, receive_sending_complete, transmit_sending_complete },
/* Codeset 4 - Q.SIG specific */
{ 1, QSIG_IE_TRANSIT_COUNT | Q931_CODESET(4), "Transit Count", dump_transit_count },
/* Codeset 5 - National specific (ETSI PISN specific) */
{ 1, Q931_CALLING_PARTY_CATEGORY, "Calling Party Category", dump_calling_party_category },
/* Codeset 6 - Network specific */
{ 1, Q931_IE_ORIGINATING_LINE_INFO, "Originating Line Information", dump_line_information, receive_line_information, transmit_line_information },
{ 1, Q931_IE_FACILITY | Q931_CODESET(6), "Facility", dump_facility, receive_facility, transmit_facility },
{ 1, Q931_DISPLAY | Q931_CODESET(6), "Display (CS6)", dump_display, receive_display, transmit_display },
{ 0, Q931_IE_GENERIC_DIGITS, "Generic Digits", dump_generic_digits, receive_generic_digits, transmit_generic_digits },
/* Codeset 7 */
};
static char *ie2str(int ie)
{
unsigned int x;
/* Special handling for Locking/Non-Locking Shifts */
switch (ie & 0xf8) {
case Q931_LOCKING_SHIFT:
switch (ie & 7) {
case 0:
return "!! INVALID Locking Shift To Codeset 0";
case 1:
return "Locking Shift To Codeset 1";
case 2:
return "Locking Shift To Codeset 2";
case 3:
return "Locking Shift To Codeset 3";
case 4:
return "Locking Shift To Codeset 4";
case 5:
return "Locking Shift To Codeset 5";
case 6:
return "Locking Shift To Codeset 6";
case 7:
return "Locking Shift To Codeset 7";
default:
break;
}
break;
case Q931_NON_LOCKING_SHIFT:
switch (ie & 7) {
case 0:
return "Non-Locking Shift To Codeset 0";
case 1:
return "Non-Locking Shift To Codeset 1";
case 2:
return "Non-Locking Shift To Codeset 2";
case 3:
return "Non-Locking Shift To Codeset 3";
case 4:
return "Non-Locking Shift To Codeset 4";
case 5:
return "Non-Locking Shift To Codeset 5";
case 6:
return "Non-Locking Shift To Codeset 6";
case 7:
return "Non-Locking Shift To Codeset 7";
default:
break;
}
break;
default:
break;
}
for (x = 0; x < ARRAY_LEN(ies); ++x) {
if (ie == ies[x].ie) {
return ies[x].name;
}
}
return "Unknown Information Element";
}
static inline unsigned int ielen(q931_ie *ie)
{
if ((ie->ie & 0x80) != 0)
return 1;
else
return 2 + ie->len;
}
static inline int ielen_checked(q931_ie *ie, int len_remaining)
{
int len;
if (ie->ie & 0x80) {
len = 1;
} else if (len_remaining < 2) {
/* There is no length octet when there should be. */
len = -1;
} else {
len = 2 + ie->len;
if (len_remaining < len) {
/* There is not enough room left in the packet for this ie. */
len = -1;
}
}
return len;
}
const char *msg2str(int msg)
{
unsigned int x;
for (x=0;x<sizeof(msgs) / sizeof(msgs[0]); x++)
if (msgs[x].msgnum == msg)
return msgs[x].name;
return "Unknown Message Type";
}
static char *maintenance_msg2str(int msg, int pd)
{
unsigned int x, max;
struct msgtype *m = NULL;
if (pd == MAINTENANCE_PROTOCOL_DISCRIMINATOR_1) {
m = att_maintenance_msgs;
max = ARRAY_LEN(att_maintenance_msgs);
} else {
m = national_maintenance_msgs;
max = ARRAY_LEN(national_maintenance_msgs);
}
for (x = 0; x < max; x++) {
if (m[x].msgnum == msg) {
return m[x].name;
}
}
return "Unknown Message Type";
}
/* Decode the call reference */
static inline int q931_cr(q931_h *h)
{
int cr;
int x;
if (h->crlen > 3) {
pri_error(NULL, "Call Reference Length Too long: %d\n", h->crlen);
return Q931_DUMMY_CALL_REFERENCE;
}
switch (h->crlen) {
case 2:
cr = 0;
for (x = 0; x < h->crlen; ++x) {
cr <<= 8;
cr |= h->crv[x];
}
break;
case 1:
cr = h->crv[0];
if (cr & 0x80) {
cr &= ~0x80;
cr |= Q931_CALL_REFERENCE_FLAG;
}
break;
case 0:
cr = Q931_DUMMY_CALL_REFERENCE;
break;
default:
pri_error(NULL, "Call Reference Length not supported: %d\n", h->crlen);
cr = Q931_DUMMY_CALL_REFERENCE;
break;
}
return cr;
}
static inline void q931_dumpie(struct pri *ctrl, int codeset, q931_ie *ie, char prefix)
{
unsigned int x;
int full_ie = Q931_FULL_IE(codeset, ie->ie);
int base_ie;
char *buf = malloc(ielen(ie) * 3 + 1);
int buflen = 0;
buf[0] = '\0';
if (!(ie->ie & 0x80)) {
buflen += sprintf(buf, " %02x", ielen(ie)-2);
for (x = 0; x + 2 < ielen(ie); ++x)
buflen += sprintf(buf + buflen, " %02x", ie->data[x]);
}
pri_message(ctrl, "%c [%02x%s]\n", prefix, ie->ie, buf);
free(buf);
/* Special treatment for shifts */
if((full_ie & 0xf0) == Q931_LOCKING_SHIFT)
full_ie &= 0xff;
base_ie = (((full_ie & ~0x7f) == Q931_FULL_IE(0, 0x80)) && ((full_ie & 0x70) != 0x20)) ? full_ie & ~0x0f : full_ie;
for (x = 0; x < ARRAY_LEN(ies); ++x)
if (ies[x].ie == base_ie) {
if (ies[x].dump)
ies[x].dump(full_ie, ctrl, ie, ielen(ie), prefix);
else
pri_message(ctrl, "%c IE: %s (len = %d)\n", prefix, ies[x].name, ielen(ie));
return;
}
pri_error(ctrl, "!! %c Unknown IE %d (cs%d, len = %d)\n", prefix, Q931_IE_IE(base_ie), Q931_IE_CODESET(base_ie), ielen(ie));
}
/*!
* \brief Initialize the call record.
*
* \param link Q.921 link associated with the call.
* \param call Q.931 call leg.
* \param cr Call Reference identifier.
*
* \note The call record is assumed to already be memset() to zero.
*
* \return Nothing
*/
void q931_init_call_record(struct q921_link *link, struct q931_call *call, int cr)
{
struct pri *ctrl;
call->cr = cr;
call->slotmap = -1;
call->channelno = -1;
if (cr != Q931_DUMMY_CALL_REFERENCE) {
call->newcall = 1;
}
call->ourcallstate = Q931_CALL_STATE_NULL;
call->peercallstate = Q931_CALL_STATE_NULL;
call->sugcallstate = Q931_CALL_STATE_NOT_SET;
call->ri = -1;
call->bc.transcapability = -1;
call->bc.transmoderate = -1;
call->bc.transmultiple = -1;
call->bc.userl1 = -1;
call->bc.userl2 = -1;
call->bc.userl3 = -1;
call->bc.rateadaption = -1;
call->progress = -1;
call->causecode = -1;
call->causeloc = -1;
call->cause = -1;
call->useruserprotocoldisc = -1;
call->aoc_units = -1;
call->changestatus = -1;
call->reversecharge = -1;
call->pri_winner = -1;
call->master_call = call;
q931_party_number_init(&call->redirection_number);
q931_party_address_init(&call->called);
q931_party_id_init(&call->local_id);
q931_party_id_init(&call->remote_id);
q931_party_number_init(&call->ani);
q931_party_redirecting_init(&call->redirecting);
/* The call is now attached to whoever called us */
ctrl = link->ctrl;
call->pri = ctrl;
if (cr == Q931_DUMMY_CALL_REFERENCE) {
/* Dummy calls are always for the given link. */
call->link = link;
} else if (BRI_TE_PTMP(ctrl)) {
/* Always uses the specific TEI link. */
call->link = ctrl->link.next;
} else {
call->link = link;
}
}
/*!
* \internal
* \brief Create a new call record.
*
* \param link Q.921 link associated with the call.
* \param cr Call Reference identifier.
*
* \retval record on success.
* \retval NULL on error.
*/
static struct q931_call *q931_create_call_record(struct q921_link *link, int cr)
{
struct q931_call *call;
struct q931_call *prev;
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "-- Making new call for cref %d\n", cr);
}
call = calloc(1, sizeof(*call));
if (!call) {
return NULL;
}
/* Initialize call structure. */
q931_init_call_record(link, call, cr);
/* Append to the list end */
if (*ctrl->callpool) {
/* Find the list end. */
for (prev = *ctrl->callpool; prev->next; prev = prev->next) {
}
prev->next = call;
} else {
/* List was empty. */
*ctrl->callpool = call;
}
return call;
}
/*!
* \internal
* \brief Find a call in the active call pool.
*
* \param link Q.921 link associated with the call.
* \param cr Call Reference identifier.
*
* \retval call if found.
* \retval NULL if not found.
*/
static struct q931_call *q931_find_call(struct q921_link *link, int cr)
{
struct q931_call *cur;
struct pri *ctrl;
if (cr == Q931_DUMMY_CALL_REFERENCE) {
return link->dummy_call;
}
ctrl = link->ctrl;
if (BRI_NT_PTMP(ctrl) && !(cr & Q931_CALL_REFERENCE_FLAG)) {
if (link->tei == Q921_TEI_GROUP) {
/* Broadcast TEI. This is bad. We are using the wrong link structure. */
pri_error(ctrl, "Looking for cref %d when using broadcast TEI.\n", cr);
return NULL;
}
/* We are looking for a call reference value that the other side allocated. */
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->cr == cr && cur->link == link) {
/* Found existing call. The call reference and link matched. */
break;
}
}
} else {
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->cr == cr) {
/* Found existing call. */
switch (ctrl->switchtype) {
case PRI_SWITCH_GR303_EOC:
case PRI_SWITCH_GR303_TMC:
break;
default:
if (!ctrl->bri) {
/* The call is now attached to whoever called us */
cur->pri = ctrl;
cur->link = link;
}
break;
}
break;
}
}
}
return cur;
}
static struct q931_call *q931_getcall(struct q921_link *link, int cr)
{
struct q931_call *cur;
struct pri *ctrl;
cur = q931_find_call(link, cr);
if (cur) {
return cur;
}
if (cr == Q931_DUMMY_CALL_REFERENCE) {
/* Do not create new dummy call records. */
return NULL;
}
ctrl = link->ctrl;
if (link->tei == Q921_TEI_GROUP
&& BRI_NT_PTMP(ctrl)) {
/* Do not create NT PTMP broadcast call records here. */
pri_error(ctrl,
"NT PTMP cannot create call record for cref %d on the broadcast TEI.\n", cr);
return NULL;
}
/* No call record exists, make a new one */
return q931_create_call_record(link, cr);
}
/*!
* \brief Create a new call record for an outgoing call.
*
* \param ctrl D channel controller.
*
* \retval call on success.
* \retval NULL on error.
*/
struct q931_call *q931_new_call(struct pri *ctrl)
{
struct q931_call *cur;
struct q921_link *link;
int first_cref;
int cref;
/* Find a new call reference value. */
first_cref = ctrl->cref;
do {
cref = Q931_CALL_REFERENCE_FLAG | ctrl->cref;
/* Next call reference. */
++ctrl->cref;
if (!ctrl->bri) {
if (ctrl->cref > 32767) {
ctrl->cref = 1;
}
} else {
if (ctrl->cref > 127) {
ctrl->cref = 1;
}
}
/* Is the call reference value in use? */
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->cr == cref) {
/* Yes it is in use. */
if (first_cref == ctrl->cref) {
/* All call reference values are in use! */
return NULL;
}
break;
}
}
} while (cur);
link = &ctrl->link;
return q931_create_call_record(link, cref);
}
static void stop_t303(struct q931_call *call);
static void stop_t312(struct q931_call *call)
{
/* T312 should only be running on the master call */
pri_schedule_del(call->pri, call->t312_timer);
call->t312_timer = 0;
}
static void cleanup_and_free_call(struct q931_call *cur)
{
struct pri *ctrl;
ctrl = cur->pri;
pri_schedule_del(ctrl, cur->restart.timer);
pri_schedule_del(ctrl, cur->restart_tx.t316_timer);
pri_schedule_del(ctrl, cur->retranstimer);
pri_schedule_del(ctrl, cur->hold_timer);
pri_schedule_del(ctrl, cur->fake_clearing_timer);
stop_t303(cur);
stop_t312(cur);
pri_call_apdu_queue_cleanup(cur);
if (cur->cc.record) {
/* Unlink CC associations. */
if (cur->cc.record->original_call == cur) {
cur->cc.record->original_call = NULL;
}
if (cur->cc.record->signaling == cur) {
cur->cc.record->signaling = NULL;
}
}
free(cur);
}
int q931_get_subcall_count(struct q931_call *master)
{
int count = 0;
int idx;
for (idx = 0; idx < ARRAY_LEN(master->subcalls); ++idx) {
if (master->subcalls[idx]) {
++count;
}
}
return count;
}
static int pri_internal_clear(struct q931_call *call);
/*!
* \brief Fake RELEASE for NT-PTMP initiated SETUPs w/o response
*
* \param param call Call is not a subcall call record.
*/
static void pri_fake_clearing(struct q931_call *call)
{
struct pri *ctrl;
ctrl = call->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Fake clearing. cref:%d\n", call->cr);
}
/*
* This does not need to be running since this is what we are
* doing right now anyway.
*/
pri_schedule_del(ctrl, call->fake_clearing_timer);
call->fake_clearing_timer = 0;
if (call->cause == -1) {
/* Ensure that there is a resonable cause code. */
call->cause = PRI_CAUSE_NO_USER_RESPONSE;
}
if (pri_internal_clear(call) == Q931_RES_HAVEEVENT) {
ctrl->schedev = 1;
}
}
static void pri_fake_clearing_expiry(void *data)
{
struct q931_call *master = data;
master->fake_clearing_timer = 0;
pri_fake_clearing(master);
}
static void pri_create_fake_clearing(struct pri *ctrl, struct q931_call *master)
{
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Fake clearing requested. cref:%d\n", master->cr);
}
pri_schedule_del(ctrl, master->fake_clearing_timer);
master->fake_clearing_timer = pri_schedule_event(ctrl, 0, pri_fake_clearing_expiry,
master);
}
static void t312_expiry(void *data)
{
struct q931_call *master = data;
struct pri *ctrl;
ctrl = master->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "T312 timed out. cref:%d\n", master->cr);
}
master->t312_timer = 0;
if (!q931_get_subcall_count(master)) {
/* No subcalls remain. */
switch (master->ourcallstate) {
case Q931_CALL_STATE_CALL_ABORT:
/* We can destroy the master. */
q931_destroycall(ctrl, master);
break;
default:
/* Let the upper layer know about the lack of call prospects. */
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_CALL_ABORT);
pri_fake_clearing(master);
break;
}
}
}
/*! \param master Master call record for PTMP NT call. */
static void start_t312(struct q931_call *master)
{
struct pri *ctrl;
ctrl = master->pri;
pri_schedule_del(ctrl, master->t312_timer);
master->t312_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T312],
t312_expiry, master);
}
/*!
* \internal
* \brief Helper function to destroy a subcall.
*
* \param master Q.931 master call of subcall to destroy.
* \param idx Master subcall index to destroy.
*
* \return Nothing
*/
static void q931_destroy_subcall(struct q931_call *master, int idx)
{
struct pri *ctrl = master->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Destroying subcall %p of call %p at %d\n",
master->subcalls[idx], master, idx);
}
cleanup_and_free_call(master->subcalls[idx]);
if (master->pri_winner == idx) {
/* This was the winning subcall. */
master->pri_winner = -1;
}
master->subcalls[idx] = NULL;
}
void q931_destroycall(struct pri *ctrl, q931_call *c)
{
struct q931_call *cur;
struct q931_call *prev;
struct q931_call *slave;
int i;
int slavesleft;
if (q931_is_dummy_call(c)) {
/* Cannot destroy the dummy call. */
return;
}
if (c->master_call != c) {
slave = c;
c = slave->master_call;
} else {
slave = NULL;
}
prev = NULL;
cur = *ctrl->callpool;
while (cur) {
if (cur == c) {
if (slave) {
/* Destroying a slave. */
for (i = 0; i < ARRAY_LEN(cur->subcalls); ++i) {
if (cur->subcalls[i] == slave) {
q931_destroy_subcall(cur, i);
break;
}
}
/* How many slaves are left? */
slavesleft = 0;
for (i = 0; i < ARRAY_LEN(cur->subcalls); ++i) {
if (cur->subcalls[i]) {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Subcall still present at %d\n", i);
}
++slavesleft;
}
}
if (slavesleft || cur->t312_timer || cur->master_hanging_up) {
return;
}
/* No slaves left. */
switch (cur->ourcallstate) {
case Q931_CALL_STATE_CALL_ABORT:
break;
default:
/* Let the upper layer know about the call clearing. */
UPDATE_OURCALLSTATE(ctrl, cur, Q931_CALL_STATE_CALL_ABORT);
pri_create_fake_clearing(ctrl, cur);
return;
}
/* We can try to destroy the master now. */
} else {
/* Destroy any slaves that may be present as well. */
slavesleft = 0;
for (i = 0; i < ARRAY_LEN(cur->subcalls); ++i) {
if (cur->subcalls[i]) {
++slavesleft;
q931_destroy_subcall(cur, i);
}
}
}
if (cur->fake_clearing_timer) {
/*
* Must wait for the fake clearing to complete before destroying
* the master call record.
*/
return;
}
if (slavesleft) {
/* This is likely not good. */
pri_error(ctrl,
"Destroyed %d subcalls unconditionally with the master. cref:%d\n",
slavesleft, cur->cr);
}
/* Master call or normal call destruction. */
if (prev)
prev->next = cur->next;
else
*ctrl->callpool = cur->next;
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl,
"Destroying call %p, ourstate %s, peerstate %s, hold-state %s\n",
cur,
q931_call_state_str(cur->ourcallstate),
q931_call_state_str(cur->peercallstate),
q931_hold_state_str(cur->hold_state));
cleanup_and_free_call(cur);
return;
}
prev = cur;
cur = cur->next;
}
pri_error(ctrl, "Can't destroy call %p cref:%d!\n", c, c->cr);
}
static int add_ie(struct pri *ctrl, q931_call *call, int msgtype, int ie, q931_ie *iet, int maxlen, int *codeset)
{
unsigned int x;
int res, total_res;
int have_shift;
int ies_count, order;
for (x = 0; x < ARRAY_LEN(ies); ++x) {
if (ies[x].ie == ie) {
/* This is our baby */
if (ies[x].transmit) {
/* Prepend with CODE SHIFT IE if required */
if (*codeset != Q931_IE_CODESET(ies[x].ie)) {
/* Locking shift to codeset 0 isn't possible */
iet->ie = Q931_IE_CODESET(ies[x].ie) | (Q931_IE_CODESET(ies[x].ie) ? Q931_LOCKING_SHIFT : Q931_NON_LOCKING_SHIFT);
have_shift = 1;
iet = (q931_ie *)((char *)iet + 1);
maxlen--;
}
else
have_shift = 0;
ies_count = ies[x].max_count;
if (ies_count == 0)
ies_count = INT_MAX;
order = 0;
total_res = 0;
do {
iet->ie = ie;
res = ies[x].transmit(ie, ctrl, call, msgtype, iet, maxlen, ++order);
/* Error if res < 0 or ignored if res == 0 */
if (res < 0)
return res;
if (res > 0) {
if ((iet->ie & 0x80) == 0) /* Multibyte IE */
iet->len = res - 2;
if (msgtype == Q931_SETUP && *codeset == 0) {
switch (iet->ie) {
case Q931_BEARER_CAPABILITY:
if (!(call->cc.saved_ie_flags & CC_SAVED_IE_BC)) {
/* Save first BC ie contents for possible CC. */
call->cc.saved_ie_flags |= CC_SAVED_IE_BC;
q931_append_ie_contents(&call->cc.saved_ie_contents,
iet);
}
break;
case Q931_LOW_LAYER_COMPAT:
if (!(call->cc.saved_ie_flags & CC_SAVED_IE_LLC)) {
/* Save first LLC ie contents for possible CC. */
call->cc.saved_ie_flags |= CC_SAVED_IE_LLC;
q931_append_ie_contents(&call->cc.saved_ie_contents,
iet);
}
break;
case Q931_HIGH_LAYER_COMPAT:
if (!(call->cc.saved_ie_flags & CC_SAVED_IE_HLC)) {
/* Save first HLC ie contents for possible CC. */
call->cc.saved_ie_flags |= CC_SAVED_IE_HLC;
q931_append_ie_contents(&call->cc.saved_ie_contents,
iet);
}
break;
default:
break;
}
}
total_res += res;
maxlen -= res;
iet = (q931_ie *)((char *)iet + res);
}
} while (res > 0 && order < ies_count);
if (have_shift && total_res) {
if (Q931_IE_CODESET(ies[x].ie))
*codeset = Q931_IE_CODESET(ies[x].ie);
return total_res + 1; /* Shift is single-byte IE */
}
return total_res;
} else {
pri_error(ctrl, "!! Don't know how to add IE %d (%s)\n", ie, ie2str(ie));
return -1;
}
}
}
pri_error(ctrl, "!! Unknown IE %d (%s)\n", ie, ie2str(ie));
return -1;
}
static char *disc2str(int disc)
{
static struct msgtype discs[] = {
{ Q931_PROTOCOL_DISCRIMINATOR, "Q.931" },
{ GR303_PROTOCOL_DISCRIMINATOR, "GR-303" },
{ MAINTENANCE_PROTOCOL_DISCRIMINATOR_1, "AT&T Maintenance" },
{ MAINTENANCE_PROTOCOL_DISCRIMINATOR_2, "New AT&T Maintenance" },
};
return code2str(disc, discs, sizeof(discs) / sizeof(discs[0]));
}
/*!
* \internal
* \brief Dump the Q.931 message header.
*
* \param ctrl D channel controller.
* \param tei TEI the packet is associated with.
* \param h Q.931 packet contents/header.
* \param len Received length of the Q.931 packet.
* \param c Message direction prefix character.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_dump_header(struct pri *ctrl, int tei, q931_h *h, int len, char c)
{
q931_mh *mh;
int cref;
pri_message(ctrl, "%c Protocol Discriminator: %s (%d) len=%d\n", c, disc2str(h->pd), h->pd, len);
if (len < 2 || len < 2 + h->crlen) {
pri_message(ctrl, "%c Message too short for call reference. len=%d\n",
c, len);
return -1;
}
cref = q931_cr(h);
pri_message(ctrl, "%c TEI=%d Call Ref: len=%2d (reference %d/0x%X) (%s)\n",
c, tei, h->crlen, cref & ~Q931_CALL_REFERENCE_FLAG,
cref & ~Q931_CALL_REFERENCE_FLAG, (cref == Q931_DUMMY_CALL_REFERENCE)
? "Dummy"
: (cref & Q931_CALL_REFERENCE_FLAG)
? "Sent to originator" : "Sent from originator");
if (len < 3 + h->crlen) {
pri_message(ctrl, "%c Message too short for supported protocols. len=%d\n",
c, len);
return -1;
}
/* Message header begins at the end of the call reference number */
mh = (q931_mh *)(h->contents + h->crlen);
switch (h->pd) {
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_1:
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_2:
pri_message(ctrl, "%c Message Type: %s (%d)\n", c, maintenance_msg2str(mh->msg, h->pd), mh->msg);
break;
default:
/* Unknown protocol discriminator but we will treat it as Q.931 anyway. */
case GR303_PROTOCOL_DISCRIMINATOR:
case Q931_PROTOCOL_DISCRIMINATOR:
pri_message(ctrl, "%c Message Type: %s (%d)\n", c, msg2str(mh->msg), mh->msg);
break;
}
return 0;
}
/*!
* \internal
* \brief Q.931 is passing this message to Q.921 debug indication.
*
* \param ctrl D channel controller.
* \param tei TEI the packet is associated with.
* \param h Q.931 packet contents/header.
* \param len Received length of the Q.931 packet
*
* \return Nothing
*/
static void q931_to_q921_passing_dump(struct pri *ctrl, int tei, q931_h *h, int len)
{
char c;
c = '>';
pri_message(ctrl, "\n");
pri_message(ctrl, "%c DL-DATA request\n", c);
q931_dump_header(ctrl, tei, h, len, c);
}
/*!
* \brief Debug dump the given Q.931 packet.
*
* \param ctrl D channel controller.
* \param tei TEI the packet is associated with.
* \param h Q.931 packet contents/header.
* \param len Received length of the Q.931 packet
* \param txrx TRUE if packet is transmitted/outgoing
*
* \return Nothing
*/
void q931_dump(struct pri *ctrl, int tei, q931_h *h, int len, int txrx)
{
q931_mh *mh;
char c;
int x;
int r;
int cur_codeset;
int codeset;
c = txrx ? '>' : '<';
if (!(ctrl->debug & (PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW))) {
/* Put out a blank line if Q.921 is not dumping. */
pri_message(ctrl, "\n");
}
if (q931_dump_header(ctrl, tei, h, len, c)) {
return;
}
/* Drop length of header, including call reference */
mh = (q931_mh *)(h->contents + h->crlen);
len -= (h->crlen + 3);
codeset = cur_codeset = 0;
for (x = 0; x < len; x += r) {
r = ielen_checked((q931_ie *) (mh->data + x), len - x);
if (r < 0) {
/* We have garbage on the end of the packet. */
pri_message(ctrl, "Not enough room for codeset:%d ie:%d(%02x)\n", cur_codeset,
mh->data[x], mh->data[x]);
break;
}
q931_dumpie(ctrl, cur_codeset, (q931_ie *)(mh->data + x), c);
switch (mh->data[x] & 0xf8) {
case Q931_LOCKING_SHIFT:
if ((mh->data[x] & 7) > 0)
codeset = cur_codeset = mh->data[x] & 7;
break;
case Q931_NON_LOCKING_SHIFT:
cur_codeset = mh->data[x] & 7;
break;
default:
/* Reset temporary codeset change */
cur_codeset = codeset;
break;
}
}
}
static int q931_handle_ie(int codeset, struct pri *ctrl, q931_call *c, int msg, q931_ie *ie)
{
unsigned int x;
int full_ie = Q931_FULL_IE(codeset, ie->ie);
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "-- Processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
if (msg == Q931_SETUP && codeset == 0) {
switch (ie->ie) {
case Q931_BEARER_CAPABILITY:
if (!(c->cc.saved_ie_flags & CC_SAVED_IE_BC)) {
/* Save first BC ie contents for possible CC. */
c->cc.saved_ie_flags |= CC_SAVED_IE_BC;
q931_append_ie_contents(&c->cc.saved_ie_contents, ie);
}
break;
case Q931_LOW_LAYER_COMPAT:
if (!(c->cc.saved_ie_flags & CC_SAVED_IE_LLC)) {
/* Save first LLC ie contents for possible CC. */
c->cc.saved_ie_flags |= CC_SAVED_IE_LLC;
q931_append_ie_contents(&c->cc.saved_ie_contents, ie);
}
break;
case Q931_HIGH_LAYER_COMPAT:
if (!(c->cc.saved_ie_flags & CC_SAVED_IE_HLC)) {
/* Save first HLC ie contents for possible CC. */
c->cc.saved_ie_flags |= CC_SAVED_IE_HLC;
q931_append_ie_contents(&c->cc.saved_ie_contents, ie);
}
break;
default:
break;
}
}
for (x = 0; x < ARRAY_LEN(ies); ++x) {
if (full_ie == ies[x].ie) {
if (ies[x].receive)
return ies[x].receive(full_ie, ctrl, c, msg, ie, ielen(ie));
else {
if (ctrl->debug & PRI_DEBUG_Q931_ANOMALY)
pri_message(ctrl, "!! No handler for IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
return -1;
}
}
}
pri_message(ctrl, "!! Unknown IE %d (cs%d)\n", ie->ie, codeset);
return -1;
}
/* Returns header and message header and modifies length in place */
static void init_header(struct pri *ctrl, q931_call *call, unsigned char *buf, q931_h **hb, q931_mh **mhb, int *len, int protodisc)
{
q931_h *h = (q931_h *) buf;
q931_mh *mh;
unsigned crv;
if (protodisc) {
h->pd = protodisc;
} else {
h->pd = ctrl->protodisc;
}
h->x0 = 0; /* Reserved 0 */
if (q931_is_dummy_call(call)) {
h->crlen = 0;
} else if (!ctrl->bri) {
/* Two bytes of Call Reference. */
h->crlen = 2;
if (ctrl->link.next) {
/* On GR-303, Q931_CALL_REFERENCE_FLAG is always 0 */
crv = ((unsigned) call->cr) & ~Q931_CALL_REFERENCE_FLAG;
} else {
/* Invert the Q931_CALL_REFERENCE_FLAG to make it from our sense */
crv = ((unsigned) call->cr) ^ Q931_CALL_REFERENCE_FLAG;
}
h->crv[0] = (crv >> 8) & 0xff;
h->crv[1] = crv & 0xff;
} else {
h->crlen = 1;
/* Invert the Q931_CALL_REFERENCE_FLAG to make it from our sense */
crv = ((unsigned) call->cr) ^ Q931_CALL_REFERENCE_FLAG;
h->crv[0] = ((crv >> 8) & 0x80) | (crv & 0x7f);
}
*hb = h;
*len -= 3;/* Protocol discriminator, call reference length, message type id */
*len -= h->crlen;
mh = (q931_mh *) (h->contents + h->crlen);
mh->f = 0;
*mhb = mh;
}
static void q931_xmit(struct q921_link *link, q931_h *h, int len, int cr, int uiframe)
{
struct pri *ctrl;
ctrl = link->ctrl;
ctrl->q931_txcount++;
if (uiframe) {
if (link->tei != Q921_TEI_GROUP) {
pri_error(ctrl, "Huh?! Attempting to send UI-frame on TEI %d\n", link->tei);
return;
}
q921_transmit_uiframe(link, h, len);
if (ctrl->debug & PRI_DEBUG_Q931_DUMP) {
/*
* The transmit operation might dump the Q.921 header, so logging
* the Q.931 message body after the transmit puts the sections of
* the message in the right order in the log,
*/
q931_dump(ctrl, link->tei, h, len, 1);
}
} else {
/*
* Indicate passing the Q.931 message to Q.921 first. Q.921 may
* have to request a TEI or bring the connection up before it can
* actually send the message. Therefore, the Q.931 message may
* actually get sent a few seconds later. Q.921 will dump the
* Q.931 message as appropriate at that time.
*/
if (ctrl->debug & PRI_DEBUG_Q931_DUMP) {
q931_to_q921_passing_dump(ctrl, link->tei, h, len);
}
q921_transmit_iframe(link, h, len, cr);
}
}
/*!
* \internal
* \brief Build and send the requested message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param msgtype Q.931 message type to build.
* \param ies List of ie's to put in the message.
*
* \note The ie's in the ie list must be in numerical order.
* See Q.931 section 4.5.1 coding rules.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int send_message(struct pri *ctrl, q931_call *call, int msgtype, int ies[])
{
unsigned char buf[1024];
q931_h *h;
q931_mh *mh;
int len;
int res;
int offset=0;
int x;
int codeset;
int uiframe;
if (call->outboundbroadcast && call->master_call == call && msgtype != Q931_SETUP) {
pri_error(ctrl,
"Attempting to use master call record to send %s on BRI PTMP NT %p\n",
msg2str(msgtype), ctrl);
return -1;
}
if (!call->link) {
pri_error(ctrl,
"Call w/ cref:%d is not associated with a link. TEI removed due to error conditions?\n",
call->cr);
return -1;
}
memset(buf, 0, sizeof(buf));
len = sizeof(buf);
init_header(ctrl, call, buf, &h, &mh, &len, (msgtype >> 8));
mh->msg = msgtype & 0x00ff;
x=0;
codeset = 0;
while(ies[x] > -1) {
res = add_ie(ctrl, call, mh->msg, ies[x], (q931_ie *)(mh->data + offset), len, &codeset);
if (res < 0) {
pri_error(ctrl, "!! Unable to add IE '%s'\n", ie2str(ies[x]));
return -1;
}
offset += res;
len -= res;
x++;
}
/* Invert the logic */
len = sizeof(buf) - len;
uiframe = 0;
if (BRI_NT_PTMP(ctrl)) {
/* NT PTMP is the only mode that can broadcast Q.931 messages. */
switch (msgtype) {
case Q931_SETUP:
/*
* For NT-PTMP mode, we need to check the following:
* MODE = NT-PTMP
* MESSAGE = SETUP
*
* If those are true, we need to send the SETUP in a UI frame
* instead of an I-frame.
*/
uiframe = 1;
break;
case Q931_FACILITY:
if (call->link->tei == Q921_TEI_GROUP) {
/* Broadcast TEI. */
if (q931_is_dummy_call(call)) {
/*
* This is a FACILITY message on the dummy call reference
* for the broadcast TEI.
*/
uiframe = 1;
} else {
pri_error(ctrl,
"Attempting to broadcast %s on cref %d\n",
msg2str(msgtype), call->cr);
return -1;
}
}
break;
default:
break;
}
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
/* This message is only interesting for NT PTMP mode. */
pri_message(ctrl,
"Sending message for call %p on call->link: %p with TEI/SAPI %d/%d\n",
call, call->link, call->link->tei, call->link->sapi);
}
}
q931_xmit(call->link, h, len, 1, uiframe);
call->acked = 1;
return 0;
}
static int maintenance_service_ies[] = { Q931_IE_CHANGE_STATUS, Q931_CHANNEL_IDENT, -1 };
static int maintenance_service_ack(struct pri *ctrl, q931_call *c)
{
int pd = MAINTENANCE_PROTOCOL_DISCRIMINATOR_1;
int mt = ATT_SERVICE_ACKNOWLEDGE;
if (ctrl->switchtype == PRI_SWITCH_NI2) {
pd = MAINTENANCE_PROTOCOL_DISCRIMINATOR_2;
mt = NATIONAL_SERVICE_ACKNOWLEDGE;
}
return send_message(ctrl, c, (pd << 8) | mt, maintenance_service_ies);
}
/*!
* \note Maintenance service messages only supported in PRI mode.
*/
int maintenance_service(struct pri *ctrl, int span, int channel, int changestatus)
{
struct q931_call *c;
int pd = MAINTENANCE_PROTOCOL_DISCRIMINATOR_1;
int mt = ATT_SERVICE;
c = q931_getcall(&ctrl->link, 0 | Q931_CALL_REFERENCE_FLAG);
if (!c) {
return -1;
}
if (channel > -1) {
c->channelno = channel & 0xff;
c->chanflags = FLAG_EXCLUSIVE;
} else {
c->channelno = channel;
c->chanflags = FLAG_EXCLUSIVE | FLAG_WHOLE_INTERFACE;
}
c->ds1no = span;
c->ds1explicit = 0;
c->changestatus = changestatus;
if (ctrl->switchtype == PRI_SWITCH_NI2) {
pd = MAINTENANCE_PROTOCOL_DISCRIMINATOR_2;
mt = NATIONAL_SERVICE;
}
return send_message(ctrl, c, (pd << 8) | mt, maintenance_service_ies);
}
static int q931_status(struct pri *ctrl, q931_call *call, int cause)
{
static int status_ies[] = {
Q931_CAUSE,
Q931_IE_CALL_STATE,
-1
};
call->cause = cause;
call->causecode = CODE_CCITT;
call->causeloc = LOC_USER;
return send_message(ctrl, call, Q931_STATUS, status_ies);
}
int q931_information(struct pri *ctrl, q931_call *c, char digit)
{
static int information_ies[] = {
Q931_CALLED_PARTY_NUMBER,
-1
};
c->overlap_digits[0] = digit;
c->overlap_digits[1] = '\0';
/*
* Since we are doing overlap dialing now, we need to accumulate
* the digits into call->called.number.str.
*/
c->called.number.valid = 1;
if (strlen(c->called.number.str) < sizeof(c->called.number.str) - 1) {
/* There is enough room for the new digit. */
strcat(c->called.number.str, c->overlap_digits);
}
return send_message(ctrl, c, Q931_INFORMATION, information_ies);
}
/*!
* \internal
* \brief Actually send display text if in the right call state.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param display Display text to send.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_display_text_helper(struct pri *ctrl, struct q931_call *call, const struct pri_subcmd_display_txt *display)
{
int status;
static int information_display_ies[] = {
Q931_DISPLAY,
-1
};
switch (call->ourcallstate) {
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
case Q931_CALL_STATE_OVERLAP_RECEIVING:
call->display.text = (unsigned char *) display->text;
call->display.full_ie = 0;
call->display.length = display->length;
call->display.char_set = display->char_set;
status = send_message(ctrl, call, Q931_INFORMATION, information_display_ies);
q931_display_clear(call);
break;
default:
status = 0;
break;
}
return status;
}
/*!
* \brief Send display text during a call.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param display Display text to send.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_display_text(struct pri *ctrl, struct q931_call *call, const struct pri_subcmd_display_txt *display)
{
int status;
unsigned idx;
struct q931_call *subcall;
if ((ctrl->display_flags.send & (PRI_DISPLAY_OPTION_BLOCK | PRI_DISPLAY_OPTION_TEXT))
!= PRI_DISPLAY_OPTION_TEXT) {
/* Not enabled */
return 0;
}
if (call->outboundbroadcast && call->master_call == call) {
status = 0;
for (idx = 0; idx < ARRAY_LEN(call->subcalls); ++idx) {
subcall = call->subcalls[idx];
if (subcall && q931_display_text_helper(ctrl, subcall, display)) {
status = -1;
}
}
} else {
status = q931_display_text_helper(ctrl, call, display);
}
return status;
}
static int keypad_facility_ies[] = { Q931_IE_KEYPAD_FACILITY, -1 };
int q931_keypad_facility(struct pri *ctrl, q931_call *call, const char *digits)
{
libpri_copy_string(call->keypad_digits, digits, sizeof(call->keypad_digits));
return send_message(ctrl, call, Q931_INFORMATION, keypad_facility_ies);
}
static int restart_ack_ies[] = { Q931_CHANNEL_IDENT, Q931_RESTART_INDICATOR, -1 };
static int restart_ack(struct pri *ctrl, q931_call *c)
{
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
return send_message(ctrl, c, Q931_RESTART_ACKNOWLEDGE, restart_ack_ies);
}
int q931_facility(struct pri *ctrl, struct q931_call *call)
{
static int facility_ies[] = {
Q931_IE_FACILITY,
-1
};
return send_message(ctrl, call, Q931_FACILITY, facility_ies);
}
/*!
* \brief Send a FACILITY message with the called party number and subaddress ies.
*
* \param ctrl D channel controller.
* \param call Call leg to send message over.
* \param called Called party information to send.
*
* \note
* This function can only be used by the dummy call because the call's called
* structure is used by normal calls to contain persistent information.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_facility_called(struct pri *ctrl, struct q931_call *call, const struct q931_party_id *called)
{
static int facility_called_ies[] = {
Q931_IE_FACILITY,
Q931_CALLED_PARTY_NUMBER,
Q931_CALLED_PARTY_SUBADDR,
-1
};
q931_party_id_copy_to_address(&call->called, called);
libpri_copy_string(call->overlap_digits, call->called.number.str,
sizeof(call->overlap_digits));
return send_message(ctrl, call, Q931_FACILITY, facility_called_ies);
}
int q931_facility_display_name(struct pri *ctrl, struct q931_call *call, const struct q931_party_name *name)
{
int status;
static int facility_display_ies[] = {
Q931_IE_FACILITY,
Q931_DISPLAY,
-1
};
q931_display_name_send(call, name);
status = send_message(ctrl, call, Q931_FACILITY, facility_display_ies);
q931_display_clear(call);
return status;
}
/*!
* \brief Send a FACILITY RequestSubaddress with optional redirection name and number.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param notify Notification indicator
* \param name Redirection name to send if not NULL.
* \param number Redirection number to send if not NULL.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_request_subaddress(struct pri *ctrl, struct q931_call *call, int notify, const struct q931_party_name *name, const struct q931_party_number *number)
{
int status;
struct q931_call *winner;
static int facility_notify_ies[] = {
Q931_IE_FACILITY,
Q931_IE_NOTIFY_IND,
Q931_DISPLAY,
Q931_IE_REDIRECTION_NUMBER,
-1
};
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
q931_display_clear(winner);
if (number) {
winner->redirection_number = *number;
if (number->valid && name
&& (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_UPDATE)) {
q931_display_name_send(winner, name);
}
} else {
q931_party_number_init(&winner->redirection_number);
}
winner->notify = notify;
if (rose_request_subaddress_encode(ctrl, winner)
|| send_message(ctrl, winner, Q931_FACILITY, facility_notify_ies)) {
pri_message(ctrl,
"Could not schedule facility message for request subaddress.\n");
status = -1;
} else {
status = 0;
}
q931_display_clear(winner);
return status;
}
/*!
* \brief Send a FACILITY SubaddressTransfer to all parties.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_subaddress_transfer(struct pri *ctrl, struct q931_call *call)
{
int status;
unsigned idx;
struct q931_call *subcall;
if (call->outboundbroadcast && call->master_call == call) {
status = 0;
for (idx = 0; idx < ARRAY_LEN(call->subcalls); ++idx) {
subcall = call->subcalls[idx];
if (subcall) {
/* Send to all subcalls that have given a positive response. */
switch (subcall->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
if (send_subaddress_transfer(ctrl, subcall)) {
status = -1;
}
break;
default:
break;
}
}
}
} else {
status = send_subaddress_transfer(ctrl, call);
}
return status;
}
/*!
* \internal
* \brief Actually send a NOTIFY message with optional redirection name and number.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param notify Notification indicator
* \param name Redirection display name to send if not NULL.
* \param number Redirection number to send if not NULL.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_notify_redirection_helper(struct pri *ctrl, struct q931_call *call, int notify, const struct q931_party_name *name, const struct q931_party_number *number)
{
int status;
static int notify_ies[] = {
Q931_IE_NOTIFY_IND,
Q931_DISPLAY,
Q931_IE_REDIRECTION_NUMBER,
-1
};
q931_display_clear(call);
if (number) {
call->redirection_number = *number;
if (number->valid && name
&& (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_UPDATE)) {
q931_display_name_send(call, name);
}
} else {
q931_party_number_init(&call->redirection_number);
}
call->notify = notify;
status = send_message(ctrl, call, Q931_NOTIFY, notify_ies);
q931_display_clear(call);
return status;
}
/*!
* \brief Send a NOTIFY message with optional redirection name and number.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
* \param notify Notification indicator
* \param name Redirection display name to send if not NULL.
* \param number Redirection number to send if not NULL.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_notify_redirection(struct pri *ctrl, struct q931_call *call, int notify, const struct q931_party_name *name, const struct q931_party_number *number)
{
int status;
unsigned idx;
struct q931_call *subcall;
if (call->outboundbroadcast && call->master_call == call) {
status = 0;
for (idx = 0; idx < ARRAY_LEN(call->subcalls); ++idx) {
subcall = call->subcalls[idx];
if (subcall) {
/* Send to all subcalls that have given a positive response. */
switch (subcall->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
if (q931_notify_redirection_helper(ctrl, subcall, notify, name, number)) {
status = -1;
}
break;
default:
break;
}
}
}
} else {
status = q931_notify_redirection_helper(ctrl, call, notify, name, number);
}
return status;
}
int q931_notify(struct pri *ctrl, q931_call *c, int channel, int info)
{
switch (ctrl->switchtype) {
case PRI_SWITCH_EUROISDN_T1:
case PRI_SWITCH_EUROISDN_E1:
break;
default:
if ((info > 0x2) || (info < 0x00)) {
return -1;
}
break;
}
if (info >= 0) {
info &= 0x7F;
} else {
/* Cannot send NOTIFY message if the mandatory ie is not going to be present. */
return -1;
}
return q931_notify_redirection(ctrl, c, info, NULL, NULL);
}
static int call_progress_ies[] = {
#ifndef ALERTING_NO_PROGRESS
Q931_PROGRESS_INDICATOR,
#endif
-1
};
int q931_call_progress(struct pri *ctrl, q931_call *c, int channel, int info)
{
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (channel) {
c->ds1no = (channel & 0xff00) >> 8;
c->ds1explicit = (channel & 0x10000) >> 16;
c->channelno = channel & 0xff;
}
if (info) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask = PRI_PROG_INBAND_AVAILABLE;
} else {
/* PI is mandatory IE for PROGRESS message - Q.931 3.1.8 */
pri_error(ctrl, "XXX Progress message requested but no information is provided\n");
c->progressmask = 0;
}
c->alive = 1;
return send_message(ctrl, c, Q931_PROGRESS, call_progress_ies);
}
static int call_progress_with_cause_ies[] = {
Q931_CAUSE,
#ifndef ALERTING_NO_PROGRESS
Q931_PROGRESS_INDICATOR,
#endif
-1
};
int q931_call_progress_with_cause(struct pri *ctrl, q931_call *c, int channel, int info, int cause)
{
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (channel) {
c->ds1no = (channel & 0xff00) >> 8;
c->ds1explicit = (channel & 0x10000) >> 16;
c->channelno = channel & 0xff;
}
if (info) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask = PRI_PROG_INBAND_AVAILABLE;
} else {
/* PI is mandatory IE for PROGRESS message - Q.931 3.1.8 */
pri_error(ctrl, "XXX Progress message requested but no information is provided\n");
c->progressmask = 0;
}
c->cause = cause;
c->causecode = CODE_CCITT;
c->causeloc = LOC_PRIV_NET_LOCAL_USER;
c->alive = 1;
return send_message(ctrl, c, Q931_PROGRESS, call_progress_with_cause_ies);
}
static int call_proceeding_ies[] = {
Q931_CHANNEL_IDENT,
#ifndef ALERTING_NO_PROGRESS
Q931_PROGRESS_INDICATOR,
#endif
-1
};
int q931_call_proceeding(struct pri *ctrl, q931_call *c, int channel, int info)
{
if (c->proc) {
/* We have already sent a PROCEEDING message. Don't send another one. */
return 0;
}
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (channel) {
c->ds1no = (channel & 0xff00) >> 8;
c->ds1explicit = (channel & 0x10000) >> 16;
c->channelno = channel & 0xff;
}
c->chanflags &= ~FLAG_PREFERRED;
c->chanflags |= FLAG_EXCLUSIVE;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_INCOMING_CALL_PROCEEDING);
c->peercallstate = Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING;
if (info) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask = PRI_PROG_INBAND_AVAILABLE;
} else
c->progressmask = 0;
c->proc = 1;
c->alive = 1;
return send_message(ctrl, c, Q931_CALL_PROCEEDING, call_proceeding_ies);
}
static int alerting_ies[] = {
Q931_IE_FACILITY,
#ifndef ALERTING_NO_PROGRESS
Q931_PROGRESS_INDICATOR,
#endif
Q931_IE_USER_USER,
-1
};
int q931_alerting(struct pri *ctrl, q931_call *c, int channel, int info)
{
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (!c->proc)
q931_call_proceeding(ctrl, c, channel, 0);
if (info) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask = PRI_PROG_INBAND_AVAILABLE;
} else
c->progressmask = 0;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_RECEIVED);
c->peercallstate = Q931_CALL_STATE_CALL_DELIVERED;
c->alive = 1;
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
if (c->local_id.name.valid) {
/* Send calledName with ALERTING */
rose_called_name_encode(ctrl, c, Q931_ALERTING);
}
break;
default:
break;
}
if (c->cc.record) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_MSG_ALERTING);
}
return send_message(ctrl, c, Q931_ALERTING, alerting_ies);
}
static int setup_ack_ies[] = {
Q931_CHANNEL_IDENT,
Q931_IE_FACILITY,
Q931_PROGRESS_INDICATOR,
-1
};
int q931_setup_ack(struct pri *ctrl, q931_call *c, int channel, int nonisdn, int inband)
{
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (channel) {
c->ds1no = (channel & 0xff00) >> 8;
c->ds1explicit = (channel & 0x10000) >> 16;
c->channelno = channel & 0xff;
}
c->chanflags &= ~FLAG_PREFERRED;
c->chanflags |= FLAG_EXCLUSIVE;
c->progressmask = 0;
if (nonisdn && (ctrl->switchtype != PRI_SWITCH_DMS100)) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask |= PRI_PROG_CALLED_NOT_ISDN;
}
if (inband) {
/* Inband audio is present (i.e. dialtone) */
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask |= PRI_PROG_INBAND_AVAILABLE;
}
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_OVERLAP_RECEIVING);
c->peercallstate = Q931_CALL_STATE_OVERLAP_SENDING;
c->alive = 1;
return send_message(ctrl, c, Q931_SETUP_ACKNOWLEDGE, setup_ack_ies);
}
/* T313 expiry, first time */
static void pri_connect_timeout(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "Timed out looking for connect acknowledge\n");
c->retranstimer = 0;
q931_disconnect(ctrl, c, PRI_CAUSE_NORMAL_CLEARING);
}
/* T308 expiry, first time */
static void pri_release_timeout(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "Timed out looking for release complete\n");
c->t308_timedout++;
c->retranstimer = 0;
c->alive = 1;
/* The call to q931_release will re-schedule T308 */
q931_release(ctrl, c, c->cause);
}
/* T308 expiry, second time */
static void pri_release_finaltimeout(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
c->retranstimer = 0;
c->alive = 1;
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "Final time-out looking for release complete\n");
c->t308_timedout++;
c->ourcallstate = Q931_CALL_STATE_NULL;
c->peercallstate = Q931_CALL_STATE_NULL;
q931_clr_subcommands(ctrl);
ctrl->schedev = 1;
ctrl->ev.e = PRI_EVENT_HANGUP_ACK;
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
pri_hangup(ctrl, c, c->cause);
}
/* T305 expiry, first time */
static void pri_disconnect_timeout(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "Timed out looking for release\n");
c->retranstimer = 0;
c->alive = 1;
q931_release(ctrl, c, PRI_CAUSE_NORMAL_CLEARING);
}
static int connect_ies[] = {
Q931_CHANNEL_IDENT,
Q931_IE_FACILITY,
Q931_PROGRESS_INDICATOR,
Q931_DISPLAY,
Q931_IE_TIME_DATE,
Q931_IE_CONNECTED_NUM,
Q931_IE_CONNECTED_SUBADDR,
-1
};
int q931_connect(struct pri *ctrl, q931_call *c, int channel, int nonisdn)
{
if (c->ourcallstate == Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE) {
/* Cannot send this message when in this state */
return 0;
}
if (channel) {
c->ds1no = (channel & 0xff00) >> 8;
c->ds1explicit = (channel & 0x10000) >> 16;
c->channelno = channel & 0xff;
}
c->chanflags &= ~FLAG_PREFERRED;
c->chanflags |= FLAG_EXCLUSIVE;
if (nonisdn && (ctrl->switchtype != PRI_SWITCH_DMS100)) {
c->progloc = LOC_PRIV_NET_LOCAL_USER;
c->progcode = CODE_CCITT;
c->progressmask = PRI_PROG_CALLED_NOT_ISDN;
} else
c->progressmask = 0;
if(ctrl->localtype == PRI_NETWORK || ctrl->switchtype == PRI_SWITCH_QSIG)
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_ACTIVE);
else
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CONNECT_REQUEST);
c->peercallstate = Q931_CALL_STATE_ACTIVE;
c->alive = 1;
/* Connect request timer */
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = 0;
if ((c->ourcallstate == Q931_CALL_STATE_CONNECT_REQUEST) && (ctrl->bri || (!ctrl->link.next)))
c->retranstimer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T313], pri_connect_timeout, c);
if (c->redirecting.state == Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3) {
c->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
/* Send DivertingLegInformation3 with CONNECT. */
c->redirecting.to = c->local_id;
if (!c->redirecting.to.number.valid) {
q931_party_number_init(&c->redirecting.to.number);
c->redirecting.to.number.valid = 1;
c->redirecting.to.number.presentation =
PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
}
rose_diverting_leg_information3_encode(ctrl, c, Q931_CONNECT);
}
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
if (c->local_id.name.valid) {
/* Send connectedName with CONNECT */
rose_connected_name_encode(ctrl, c, Q931_CONNECT);
}
break;
default:
break;
}
if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_INITIAL) {
q931_display_name_send(c, &c->local_id.name);
} else {
q931_display_clear(c);
}
return send_message(ctrl, c, Q931_CONNECT, connect_ies);
}
static int release_ies[] = { Q931_CAUSE, Q931_IE_FACILITY, Q931_IE_USER_USER, -1 };
int q931_release(struct pri *ctrl, q931_call *c, int cause)
{
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_RELEASE_REQUEST);
/* c->peercallstate stays the same */
if (c->alive) {
c->alive = 0;
c->cause = cause;
c->causecode = CODE_CCITT;
c->causeloc = LOC_PRIV_NET_LOCAL_USER;
if (c->acked) {
pri_schedule_del(ctrl, c->retranstimer);
if (!c->t308_timedout) {
c->retranstimer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T308], pri_release_timeout, c);
} else {
c->retranstimer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T308], pri_release_finaltimeout, c);
}
if (c->cc.record) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_MSG_RELEASE);
}
return send_message(ctrl, c, Q931_RELEASE, release_ies);
} else {
if (c->cc.record) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_MSG_RELEASE_COMPLETE);
}
return send_message(ctrl, c, Q931_RELEASE_COMPLETE, release_ies); /* Yes, release_ies, not release_complete_ies */
}
} else
return 0;
}
static int restart_ies[] = { Q931_CHANNEL_IDENT, Q931_RESTART_INDICATOR, -1 };
static void t316_expire(void *vcall);
/*!
* \internal
* \brief Send the RESTART message to the peer.
*
* \param call Q.931 call leg
* \param channel Encoded channel id to use.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_send_restart(struct q931_call *call)
{
struct pri *ctrl = call->pri;
int channel = call->restart_tx.channel;
/* Start timer T316 if enabled. */
if (0 < ctrl->timers[PRI_TIMER_T316]) {
call->restart_tx.t316_timer =
pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T316], t316_expire, call);
--call->restart_tx.remain;
}
call->ri = 0;
call->ds1no = (channel >> 8) & 0xFF;
call->ds1explicit = (channel >> 16) & 0x1;
call->channelno = channel & 0xFF;
call->chanflags &= ~FLAG_PREFERRED;
call->chanflags |= FLAG_EXCLUSIVE;
UPDATE_OURCALLSTATE(ctrl, call, Q931_CALL_STATE_RESTART);
call->peercallstate = Q931_CALL_STATE_RESTART_REQUEST;
return send_message(ctrl, call, Q931_RESTART, restart_ies);
}
/*!
* \internal
* \brief T316 expired.
*
* \param vcall Q.931 call leg
*
* \return Nothing
*/
static void t316_expire(void *vcall)
{
struct q931_call *call = vcall;
call->restart_tx.t316_timer = 0;
if (call->restart_tx.remain) {
/* Retransmit the RESTART */
q931_send_restart(call);
} else {
int channel = call->restart_tx.channel;
pri_message(call->pri,
"!! Peer failed to ack our RESTART request for ds1/channel:%d/%d.\n",
(channel >> 8) & 0xFF, channel & 0xFF);
}
}
/*!
* \internal
* \brief Stop timer T316.
*
* \param call Q.931 call leg
*
* \return Nothing
*/
static void stop_t316(struct q931_call *call)
{
pri_schedule_del(call->pri, call->restart_tx.t316_timer);
call->restart_tx.t316_timer = 0;
}
/*!
* \brief Send the RESTART message to the peer.
*
* \param ctrl D channel controller.
* \param channel Encoded channel id to use.
*
* \note
* Sending RESTART in NT PTMP mode is not supported at the
* present time.
*
* \note
* NT PTMP should broadcast the RESTART if there is a TEI
* allocated. Otherwise it should immediately ACK the RESTART
* itself to avoid the T316 timeout delay (2 minutes) since
* there might not be anything connected. The broadcast could
* be handled in a similar manner to the broadcast SETUP.
*
* \todo NT PTMP mode should implement some protection from
* receiving a RESTART on channels in use by another TEI.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_restart(struct pri *ctrl, int channel)
{
struct q931_call *call;
if (!channel) {
return -1;
}
call = q931_getcall(&ctrl->link, 0 | Q931_CALL_REFERENCE_FLAG);
if (!call) {
return -1;
}
stop_t316(call);
call->restart_tx.remain = (0 < ctrl->timers[PRI_TIMER_N316])
? ctrl->timers[PRI_TIMER_N316] : 1;
call->restart_tx.channel = channel;
return q931_send_restart(call);
}
static int disconnect_ies[] = { Q931_CAUSE, Q931_IE_FACILITY, Q931_IE_USER_USER, -1 };
int q931_disconnect(struct pri *ctrl, q931_call *c, int cause)
{
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_DISCONNECT_REQUEST);
c->peercallstate = Q931_CALL_STATE_DISCONNECT_INDICATION;
if (c->alive) {
c->alive = 0;
c->cause = cause;
c->causecode = CODE_CCITT;
c->causeloc = LOC_PRIV_NET_LOCAL_USER;
c->sendhangupack = 1;
if (c->cc.record) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_MSG_DISCONNECT);
}
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T305], pri_disconnect_timeout, c);
return send_message(ctrl, c, Q931_DISCONNECT, disconnect_ies);
} else
return 0;
}
static int setup_ies[] = {
Q931_BEARER_CAPABILITY,
Q931_CHANNEL_IDENT,
Q931_IE_FACILITY,
Q931_PROGRESS_INDICATOR,
Q931_NETWORK_SPEC_FAC,
Q931_DISPLAY,
Q931_IE_KEYPAD_FACILITY,
Q931_REVERSE_CHARGE_INDIC,
Q931_CALLING_PARTY_NUMBER,
Q931_CALLING_PARTY_SUBADDR,
Q931_CALLED_PARTY_NUMBER,
Q931_CALLED_PARTY_SUBADDR,
Q931_REDIRECTING_NUMBER,
Q931_IE_USER_USER,
Q931_SENDING_COMPLETE,
Q931_IE_ORIGINATING_LINE_INFO,
Q931_IE_GENERIC_DIGITS,
-1
};
static int gr303_setup_ies[] = {
Q931_BEARER_CAPABILITY,
Q931_CHANNEL_IDENT,
-1
};
/*! Call Independent Signalling SETUP ie's */
static int cis_setup_ies[] = {
Q931_BEARER_CAPABILITY,
Q931_CHANNEL_IDENT,
Q931_IE_FACILITY,
Q931_IE_KEYPAD_FACILITY,
Q931_CALLING_PARTY_NUMBER,
Q931_CALLING_PARTY_SUBADDR,
Q931_CALLED_PARTY_NUMBER,
Q931_CALLED_PARTY_SUBADDR,
Q931_SENDING_COMPLETE,
-1
};
static void stop_t303(struct q931_call *call)
{
/* T303 should only be running on the master call */
pri_schedule_del(call->pri, call->t303_timer);
call->t303_timer = 0;
}
static void t303_expiry(void *data);
/*! \param call Call is not a subcall call record. */
static void start_t303(struct q931_call *call)
{
struct pri *ctrl;
ctrl = call->pri;
pri_schedule_del(ctrl, call->t303_timer);
call->t303_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T303], t303_expiry,
call);
}
static void t303_expiry(void *data)
{
struct q931_call *c = data;/* Call is not a subcall call record. */
struct pri *ctrl = c->pri;
int res;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "T303 timed out. cref:%d\n", c->cr);
}
c->t303_expirycnt++;
c->t303_timer = 0;
if (c->cause != -1) {
/* We got a DISCONNECT, RELEASE, or RELEASE_COMPLETE and no other responses. */
if (c->outboundbroadcast) {
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_ABORT);
} else {
/* This should never happen. T303 should not be running in this case. */
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
}
pri_fake_clearing(c);
} else if (c->t303_expirycnt < 2) {
/*!
* \todo XXX Resending the SETUP message loses any facility ies
* that the original may have had that were not added by
* pri_call_add_standard_apdus(). Actually any message Q.931
* retransmits will lose the facility ies.
*/
pri_call_add_standard_apdus(ctrl, c);
if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_INITIAL) {
q931_display_name_send(c, &c->local_id.name);
} else {
q931_display_clear(c);
}
c->cc.saved_ie_contents.length = 0;
c->cc.saved_ie_flags = 0;
if (ctrl->link.next && !ctrl->bri)
res = send_message(ctrl, c, Q931_SETUP, gr303_setup_ies);
else if (c->cis_call)
res = send_message(ctrl, c, Q931_SETUP, cis_setup_ies);
else
res = send_message(ctrl, c, Q931_SETUP, setup_ies);
if (res) {
pri_error(ctrl, "Error resending setup message!\n");
}
start_t303(c);
if (c->outboundbroadcast) {
start_t312(c);
}
} else {
/*
* We never got any response for a normal call or an
* establishment response from any TEI for a master/subcall
* call.
*/
c->cause = PRI_CAUSE_NO_USER_RESPONSE;
if (c->outboundbroadcast) {
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_ABORT);
} else {
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
}
pri_fake_clearing(c);
}
}
int q931_setup(struct pri *ctrl, q931_call *c, struct pri_sr *req)
{
int res;
if (!req->called.number.valid && (!req->keypad_digits || !req->keypad_digits[0])) {
/* No called number or keypad digits to send. */
return -1;
}
c->called = req->called;
libpri_copy_string(c->overlap_digits, req->called.number.str, sizeof(c->overlap_digits));
if (req->keypad_digits) {
libpri_copy_string(c->keypad_digits, req->keypad_digits,
sizeof(c->keypad_digits));
} else {
c->keypad_digits[0] = '\0';
}
c->bc.transcapability = req->transmode;
c->bc.transmoderate = TRANS_MODE_64_CIRCUIT;
if (!req->userl1)
req->userl1 = PRI_LAYER_1_ULAW;
c->bc.userl1 = req->userl1;
c->bc.userl2 = -1;
c->bc.userl3 = -1;
c->ds1no = (req->channel & 0xff00) >> 8;
c->ds1explicit = (req->channel & 0x10000) >> 16;
if ((ctrl->localtype == PRI_CPE) && ctrl->link.next && !ctrl->bri) {
c->channelno = 0;
c->chanflags = 0;
} else {
c->channelno = req->channel & 0xff;
if (req->exclusive) {
c->chanflags = FLAG_EXCLUSIVE;
} else {
c->chanflags = FLAG_PREFERRED;
}
}
if (ctrl->localtype == PRI_CPE) {
c->channel_id_ie_mandatory = 1;
}
c->slotmap = -1;
c->nonisdn = req->nonisdn;
c->newcall = 0;
c->cis_call = req->cis_call;
c->cis_recognized = req->cis_call;
c->cis_auto_disconnect = req->cis_auto_disconnect;
c->complete = req->numcomplete;
if (req->caller.number.valid) {
c->local_id = req->caller;
q931_party_id_fixup(ctrl, &c->local_id);
}
if (req->redirecting.from.number.valid) {
c->redirecting = req->redirecting;
q931_party_id_fixup(ctrl, &c->redirecting.from);
q931_party_id_fixup(ctrl, &c->redirecting.to);
q931_party_id_fixup(ctrl, &c->redirecting.orig_called);
}
if (req->useruserinfo)
libpri_copy_string(c->useruserinfo, req->useruserinfo, sizeof(c->useruserinfo));
else
c->useruserinfo[0] = '\0';
if (req->nonisdn && (ctrl->switchtype == PRI_SWITCH_NI2))
c->progressmask = PRI_PROG_CALLER_NOT_ISDN;
else
c->progressmask = 0;
c->reversecharge = req->reversecharge;
c->aoc_charging_request = req->aoc_charging_request;
pri_call_add_standard_apdus(ctrl, c);
if (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_INITIAL) {
q931_display_name_send(c, &c->local_id.name);
} else {
q931_display_clear(c);
}
/* Save the initial cc-parties. */
c->cc.party_a = c->local_id;
c->cc.originated = 1;
if (c->redirecting.from.number.valid) {
c->cc.initially_redirected = 1;
}
c->cc.saved_ie_contents.length = 0;
c->cc.saved_ie_flags = 0;
if (BRI_NT_PTMP(ctrl)) {
c->outboundbroadcast = 1;
}
if (ctrl->link.next && !ctrl->bri)
res = send_message(ctrl, c, Q931_SETUP, gr303_setup_ies);
else if (c->cis_call)
res = send_message(ctrl, c, Q931_SETUP, cis_setup_ies);
else
res = send_message(ctrl, c, Q931_SETUP, setup_ies);
if (!res) {
c->alive = 1;
/* make sure we call PRI_EVENT_HANGUP_ACK once we send/receive RELEASE_COMPLETE */
c->sendhangupack = 1;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_INITIATED);
c->peercallstate = Q931_CALL_STATE_CALL_PRESENT;
c->t303_expirycnt = 0;
start_t303(c);
if (c->outboundbroadcast) {
start_t312(c);
}
}
return res;
}
static int register_ies[] = { Q931_IE_FACILITY, -1 };
/*!
* \brief Build and send the REGISTER message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_register(struct pri *ctrl, q931_call *call)
{
int res;
call->newcall = 0;
call->cis_call = 1;
call->cis_recognized = 1;
call->cis_auto_disconnect = 0;
call->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
call->channelno = 0;
res = send_message(ctrl, call, Q931_REGISTER, register_ies);
if (!res) {
call->alive = 1;
UPDATE_OURCALLSTATE(ctrl, call, Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE);
call->peercallstate = Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE;
}
return res;
}
static int release_complete_ies[] = { Q931_IE_FACILITY, Q931_IE_USER_USER, -1 };
static int q931_release_complete(struct pri *ctrl, q931_call *c, int cause)
{
int res = 0;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
if (c->cc.record) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_MSG_RELEASE_COMPLETE);
}
if (cause > -1) {
c->cause = cause;
c->causecode = CODE_CCITT;
c->causeloc = LOC_PRIV_NET_LOCAL_USER;
/* release_ies has CAUSE in it */
res = send_message(ctrl, c, Q931_RELEASE_COMPLETE, release_ies);
} else
res = send_message(ctrl, c, Q931_RELEASE_COMPLETE, release_complete_ies);
c->alive = 0;
/* release the structure */
res += pri_hangup(ctrl, c, cause);
return res;
}
static int connect_ack_ies[] = { -1 };
static int connect_ack_w_chan_id_ies[] = { Q931_CHANNEL_IDENT, -1 };
static int gr303_connect_ack_ies[] = { Q931_CHANNEL_IDENT, -1 };
int q931_connect_acknowledge(struct pri *ctrl, q931_call *call, int channel)
{
int *use_ies;
struct q931_call *winner;
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
if (winner != call) {
UPDATE_OURCALLSTATE(ctrl, call, Q931_CALL_STATE_ACTIVE);
call->peercallstate = Q931_CALL_STATE_ACTIVE;
}
UPDATE_OURCALLSTATE(ctrl, winner, Q931_CALL_STATE_ACTIVE);
winner->peercallstate = Q931_CALL_STATE_ACTIVE;
if (channel) {
winner->ds1no = (channel & 0xff00) >> 8;
winner->ds1explicit = (channel & 0x10000) >> 16;
winner->channelno = channel & 0xff;
winner->chanflags &= ~FLAG_PREFERRED;
winner->chanflags |= FLAG_EXCLUSIVE;
}
use_ies = NULL;
if (ctrl->link.next && !ctrl->bri) {
if (ctrl->localtype == PRI_CPE) {
use_ies = gr303_connect_ack_ies;
}
} else if (channel) {
use_ies = connect_ack_w_chan_id_ies;
} else {
use_ies = connect_ack_ies;
}
if (use_ies) {
return send_message(ctrl, winner, Q931_CONNECT_ACKNOWLEDGE, use_ies);
}
return 0;
}
/*!
* \internal
* \brief Send HOLD message response wait timeout.
*
* \param data Q.931 call leg. (Master Q.931 subcall structure)
*
* \return Nothing
*/
static void q931_hold_timeout(void *data)
{
struct q931_call *call = data;
struct q931_call *master = call->master_call;
struct pri *ctrl = call->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Time-out waiting for HOLD response\n");
}
/* Ensure that the timer is deleted. */
pri_schedule_del(ctrl, master->hold_timer);
master->hold_timer = 0;
/* Don't change the hold state if there was HOLD a collision. */
switch (master->hold_state) {
case Q931_HOLD_STATE_HOLD_REQ:
UPDATE_HOLD_STATE(ctrl, master, Q931_HOLD_STATE_IDLE);
break;
default:
break;
}
q931_clr_subcommands(ctrl);
ctrl->schedev = 1;
ctrl->ev.e = PRI_EVENT_HOLD_REJ;
ctrl->ev.hold_rej.channel = q931_encode_channel(call);
ctrl->ev.hold_rej.call = master;
ctrl->ev.hold_rej.cause = PRI_CAUSE_MESSAGE_TYPE_NONEXIST;
ctrl->ev.hold_rej.subcmds = &ctrl->subcmds;
}
/*!
* \internal
* \brief Determine if a hold request is allowed now.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
*
* \retval TRUE if we can send a HOLD request.
* \retval FALSE if not allowed.
*/
static int q931_is_hold_allowed(const struct pri *ctrl, const struct q931_call *call)
{
int allowed;
allowed = 0;
switch (call->ourcallstate) {
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
if (PTMP_MODE(ctrl)) {
/* HOLD request only allowed in these states if point-to-point mode. */
break;
}
/* Fall through */
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
switch (call->hold_state) {
case Q931_HOLD_STATE_IDLE:
allowed = 1;
break;
default:
break;
}
break;
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
/* Ignore HOLD request in these states. */
break;
default:
break;
}
return allowed;
}
static int hold_ies[] = {
-1
};
/*!
* \brief Send the HOLD message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_hold(struct pri *ctrl, struct q931_call *call)
{
struct q931_call *winner;
winner = q931_find_winning_call(call);
if (!winner || !q931_is_hold_allowed(ctrl, call)) {
return -1;
}
pri_schedule_del(ctrl, call->hold_timer);
call->hold_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T_HOLD],
q931_hold_timeout, winner);
if (!call->hold_timer || send_message(ctrl, winner, Q931_HOLD, hold_ies)) {
pri_schedule_del(ctrl, call->hold_timer);
call->hold_timer = 0;
return -1;
}
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_HOLD_REQ);
return 0;
}
static int hold_ack_ies[] = {
-1
};
/*!
* \brief Send the HOLD ACKNOWLEDGE message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_hold_ack(struct pri *ctrl, struct q931_call *call)
{
struct q931_call *winner;
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_CALL_HELD);
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
/* Call is now on hold so forget the channel. */
winner->channelno = 0;/* No channel */
winner->ds1no = 0;
winner->ds1explicit = 0;
winner->chanflags = 0;
return send_message(ctrl, winner, Q931_HOLD_ACKNOWLEDGE, hold_ack_ies);
}
static int hold_reject_ies[] = {
Q931_CAUSE,
-1
};
/*!
* \internal
* \brief Send the HOLD REJECT message only.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (subcall)
* \param cause Q.931 cause code for rejecting the hold request.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_send_hold_rej_msg(struct pri *ctrl, struct q931_call *call, int cause)
{
call->cause = cause;
call->causecode = CODE_CCITT;
call->causeloc = LOC_PRIV_NET_LOCAL_USER;
return send_message(ctrl, call, Q931_HOLD_REJECT, hold_reject_ies);
}
/*!
* \brief Send the HOLD REJECT message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
* \param cause Q.931 cause code for rejecting the hold request.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_hold_rej(struct pri *ctrl, struct q931_call *call, int cause)
{
struct q931_call *winner;
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_IDLE);
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
return q931_send_hold_rej_msg(ctrl, winner, cause);
}
/*!
* \internal
* \brief Send RETRIEVE message response wait timeout.
*
* \param data Q.931 call leg. (Master Q.931 subcall structure)
*
* \return Nothing
*/
static void q931_retrieve_timeout(void *data)
{
struct q931_call *call = data;
struct q931_call *master = call->master_call;
struct pri *ctrl = call->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Time-out waiting for RETRIEVE response\n");
}
/* Ensure that the timer is deleted. */
pri_schedule_del(ctrl, master->hold_timer);
master->hold_timer = 0;
/* Don't change the hold state if there was RETRIEVE a collision. */
switch (master->hold_state) {
case Q931_HOLD_STATE_CALL_HELD:
case Q931_HOLD_STATE_RETRIEVE_REQ:
UPDATE_HOLD_STATE(ctrl, master, Q931_HOLD_STATE_CALL_HELD);
/* Call is still on hold so forget the channel. */
call->channelno = 0;/* No channel */
call->ds1no = 0;
call->ds1explicit = 0;
call->chanflags = 0;
break;
default:
break;
}
q931_clr_subcommands(ctrl);
ctrl->schedev = 1;
ctrl->ev.e = PRI_EVENT_RETRIEVE_REJ;
ctrl->ev.retrieve_rej.channel = q931_encode_channel(call);
ctrl->ev.retrieve_rej.call = master;
ctrl->ev.retrieve_rej.cause = PRI_CAUSE_MESSAGE_TYPE_NONEXIST;
ctrl->ev.retrieve_rej.subcmds = &ctrl->subcmds;
}
/*!
* \internal
* \brief Determine if a retrieve request is allowed now.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
*
* \retval TRUE if we can send a RETRIEVE request.
* \retval FALSE if not allowed.
*/
static int q931_is_retrieve_allowed(const struct pri *ctrl, const struct q931_call *call)
{
int allowed;
allowed = 0;
switch (call->ourcallstate) {
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
if (PTMP_MODE(ctrl)) {
/* RETRIEVE request only allowed in these states if point-to-point mode. */
break;
}
/* Fall through */
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
switch (call->hold_state) {
case Q931_HOLD_STATE_CALL_HELD:
allowed = 1;
break;
default:
break;
}
break;
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
/* Ignore RETRIEVE request in these states. */
break;
default:
break;
}
return allowed;
}
static int retrieve_ies[] = {
Q931_CHANNEL_IDENT,
-1
};
/*!
* \brief Send the RETRIEVE message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
* \param channel Encoded channel id to use. If zero do not send channel id.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_retrieve(struct pri *ctrl, struct q931_call *call, int channel)
{
struct q931_call *winner;
winner = q931_find_winning_call(call);
if (!winner || !q931_is_retrieve_allowed(ctrl, call)) {
return -1;
}
if (channel) {
winner->ds1no = (channel & 0xff00) >> 8;
winner->ds1explicit = (channel & 0x10000) >> 16;
winner->channelno = channel & 0xff;
if (ctrl->localtype == PRI_NETWORK && winner->channelno != 0xFF) {
winner->chanflags = FLAG_EXCLUSIVE;
} else {
winner->chanflags = FLAG_PREFERRED;
}
} else {
/* Do not send Q931_CHANNEL_IDENT */
winner->chanflags = 0;
}
pri_schedule_del(ctrl, call->hold_timer);
call->hold_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T_RETRIEVE],
q931_retrieve_timeout, winner);
if (!call->hold_timer || send_message(ctrl, winner, Q931_RETRIEVE, retrieve_ies)) {
pri_schedule_del(ctrl, call->hold_timer);
call->hold_timer = 0;
/* Call is still on hold so forget the channel. */
winner->channelno = 0;/* No channel */
winner->ds1no = 0;
winner->ds1explicit = 0;
winner->chanflags = 0;
return -1;
}
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_RETRIEVE_REQ);
return 0;
}
static int retrieve_ack_ies[] = {
Q931_CHANNEL_IDENT,
-1
};
/*!
* \brief Send the RETRIEVE ACKNOWLEDGE message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
* \param channel Encoded channel id to use.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_retrieve_ack(struct pri *ctrl, struct q931_call *call, int channel)
{
struct q931_call *winner;
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
winner->ds1no = (channel & 0xff00) >> 8;
winner->ds1explicit = (channel & 0x10000) >> 16;
winner->channelno = channel & 0xff;
winner->chanflags = FLAG_EXCLUSIVE;
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_IDLE);
return send_message(ctrl, winner, Q931_RETRIEVE_ACKNOWLEDGE, retrieve_ack_ies);
}
static int retrieve_reject_ies[] = {
Q931_CAUSE,
-1
};
/*!
* \internal
* \brief Send the RETRIEVE REJECT message only.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (subcall)
* \param cause Q.931 cause code for rejecting the retrieve request.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int q931_send_retrieve_rej_msg(struct pri *ctrl, struct q931_call *call, int cause)
{
call->cause = cause;
call->causecode = CODE_CCITT;
call->causeloc = LOC_PRIV_NET_LOCAL_USER;
return send_message(ctrl, call, Q931_RETRIEVE_REJECT, retrieve_reject_ies);
}
/*!
* \brief Send the RETRIEVE REJECT message.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg. (Master Q.931 subcall structure)
* \param cause Q.931 cause code for rejecting the retrieve request.
*
* \retval 0 on success.
* \retval -1 on error.
*/
int q931_send_retrieve_rej(struct pri *ctrl, struct q931_call *call, int cause)
{
struct q931_call *winner;
UPDATE_HOLD_STATE(ctrl, call, Q931_HOLD_STATE_CALL_HELD);
winner = q931_find_winning_call(call);
if (!winner) {
return -1;
}
/* Call is still on hold so forget the channel. */
winner->channelno = 0;/* No channel */
winner->ds1no = 0;
winner->ds1explicit = 0;
winner->chanflags = 0;
return q931_send_retrieve_rej_msg(ctrl, winner, cause);
}
static int __q931_hangup(struct pri *ctrl, q931_call *c, int cause)
{
int disconnect = 1;
int release_compl = 0;
if (!ctrl || !c) {
return -1;
}
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl,
DBGHEAD "ourstate %s, peerstate %s, hold-state %s\n", DBGINFO,
q931_call_state_str(c->ourcallstate),
q931_call_state_str(c->peercallstate),
q931_hold_state_str(c->master_call->hold_state));
}
/* If mandatory IE was missing, insist upon that cause code */
if (c->cause == PRI_CAUSE_MANDATORY_IE_MISSING)
cause = c->cause;
switch (cause) {
case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
case PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST:
case PRI_CAUSE_UNALLOCATED:
if (!ctrl->hangup_fix_enabled) {
/* Legacy: We'll send RELEASE_COMPLETE with these causes */
disconnect = 0;
release_compl = 1;
break;
}
/* Fall through */
case PRI_CAUSE_INCOMPATIBLE_DESTINATION:
/* See Q.931 Section 5.3.2 a) */
switch (c->ourcallstate) {
case Q931_CALL_STATE_NULL:
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_CALL_PRESENT:
/*
* Send RELEASE_COMPLETE because some other message
* has not been sent previously.
*/
disconnect = 0;
release_compl = 1;
break;
case Q931_CALL_STATE_CONNECT_REQUEST:
/*
* Send RELEASE because the B channel negotiation failed
* for call waiting.
*/
disconnect = 0;
break;
default:
/*
* Send DISCONNECT because some other message
* has been sent previously.
*/
break;
}
break;
case PRI_CAUSE_INVALID_CALL_REFERENCE:
/* We'll send RELEASE_COMPLETE with these causes */
disconnect = 0;
release_compl = 1;
break;
case PRI_CAUSE_CHANNEL_UNACCEPTABLE:
case PRI_CAUSE_CALL_AWARDED_DELIVERED:
case PRI_CAUSE_NONSELECTED_USER_CLEARING:
/* We'll send RELEASE with these causes */
disconnect = 0;
break;
default:
break;
}
if (c->cis_call) {
disconnect = 0;
}
c->hangupinitiated = 1;
stop_t303(c);
/* All other causes we send with DISCONNECT */
switch(c->ourcallstate) {
case Q931_CALL_STATE_NULL:
if (c->peercallstate == Q931_CALL_STATE_NULL)
/* free the resources if we receive or send REL_COMPL */
pri_destroycall(ctrl, c);
else if (c->peercallstate == Q931_CALL_STATE_RELEASE_REQUEST)
q931_release_complete(ctrl,c,cause);
break;
case Q931_CALL_STATE_CALL_INITIATED:
/* we sent SETUP */
case Q931_CALL_STATE_OVERLAP_SENDING:
/* received SETUP_ACKNOWLEDGE */
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
/* received CALL_PROCEEDING */
case Q931_CALL_STATE_CALL_DELIVERED:
/* received ALERTING */
case Q931_CALL_STATE_CALL_PRESENT:
/* received SETUP */
case Q931_CALL_STATE_CALL_RECEIVED:
/* sent ALERTING */
case Q931_CALL_STATE_CONNECT_REQUEST:
/* sent CONNECT */
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
/* we sent CALL_PROCEEDING */
case Q931_CALL_STATE_OVERLAP_RECEIVING:
/* received SETUP_ACKNOWLEDGE */
/* send DISCONNECT in general */
switch (c->peercallstate) {
default:
if (disconnect)
q931_disconnect(ctrl,c,cause);
else if (release_compl)
q931_release_complete(ctrl,c,cause);
else
q931_release(ctrl,c,cause);
break;
case Q931_CALL_STATE_NULL:
case Q931_CALL_STATE_DISCONNECT_REQUEST:
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
case Q931_CALL_STATE_RESTART_REQUEST:
case Q931_CALL_STATE_RESTART:
pri_error(ctrl,
"Weird, doing nothing but this shouldn't happen, ourstate %s, peerstate %s\n",
q931_call_state_str(c->ourcallstate),
q931_call_state_str(c->peercallstate));
break;
}
break;
case Q931_CALL_STATE_ACTIVE:
/* received CONNECT */
if (c->cis_call) {
q931_release(ctrl, c, cause);
break;
}
q931_disconnect(ctrl,c,cause);
break;
case Q931_CALL_STATE_DISCONNECT_REQUEST:
/* sent DISCONNECT */
q931_release(ctrl,c,cause);
break;
case Q931_CALL_STATE_CALL_ABORT:
/* Don't do anything, waiting for T312 to expire. */
break;
case Q931_CALL_STATE_DISCONNECT_INDICATION:
/* received DISCONNECT */
if (c->peercallstate == Q931_CALL_STATE_DISCONNECT_REQUEST) {
c->alive = 1;
q931_release(ctrl,c,cause);
}
break;
case Q931_CALL_STATE_RELEASE_REQUEST:
/* sent RELEASE */
/* don't do anything, waiting for RELEASE_COMPLETE */
break;
case Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE:
/* we sent or received REGISTER */
q931_release_complete(ctrl, c, cause);
break;
case Q931_CALL_STATE_RESTART:
case Q931_CALL_STATE_RESTART_REQUEST:
/* sent RESTART */
pri_error(ctrl,
"q931_hangup shouldn't be called in this state, ourstate %s, peerstate %s\n",
q931_call_state_str(c->ourcallstate),
q931_call_state_str(c->peercallstate));
break;
default:
pri_error(ctrl,
"We're not yet handling hanging up when our state is %d, contact support@digium.com, ourstate %s, peerstate %s\n",
c->ourcallstate,
q931_call_state_str(c->ourcallstate),
q931_call_state_str(c->peercallstate));
return -1;
}
/* we did handle hangup properly at this point */
return 0;
}
static void initiate_hangup_if_needed(struct q931_call *master, int idx, int cause);
int q931_hangup(struct pri *ctrl, q931_call *call, int cause)
{
int i;
if (call->master_call->outboundbroadcast) {
if (call->master_call == call) {
int slaves;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "Hangup master cref:%d\n", DBGINFO, call->cr);
}
UPDATE_OURCALLSTATE(ctrl, call, Q931_CALL_STATE_CALL_ABORT);
if (call->pri_winner < 0 && call->alive) {
/*
* Fake clearing if we have no winner to get rid of the upper
* layer.
*/
pri_create_fake_clearing(ctrl, call);
} else if (call->fake_clearing_timer) {
/*
* No need for fake clearing to be running anymore.
* Will this actually happen?
*/
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Fake clearing request cancelled. cref:%d\n",
call->cr);
}
pri_schedule_del(ctrl, call->fake_clearing_timer);
call->fake_clearing_timer = 0;
}
/* Initiate hangup of slaves */
call->master_hanging_up = 1;
for (i = 0; i < ARRAY_LEN(call->subcalls); ++i) {
if (call->subcalls[i]) {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "Hanging up %d, winner:%d subcall:%p\n",
DBGINFO, i, call->pri_winner, call->subcalls[i]);
}
if (i == call->pri_winner) {
q931_hangup(ctrl, call->subcalls[i], cause);
} else {
initiate_hangup_if_needed(call, i, cause);
}
}
}
call->master_hanging_up = 0;
slaves = q931_get_subcall_count(call);
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "Remaining slaves %d\n", DBGINFO, slaves);
}
stop_t303(call);
if (!call->t312_timer && !slaves) {
/*
* T312 has expired and no slaves are left so we can safely
* destroy the master.
*/
q931_destroycall(ctrl, call);
}
return 0;
} else {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "Hangup slave cref:%d\n", DBGINFO, call->cr);
}
return __q931_hangup(ctrl, call, cause);
}
} else {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "Hangup other cref:%d\n", DBGINFO, call->cr);
}
return __q931_hangup(ctrl, call, cause);
}
return 0;
}
static int prepare_to_handle_maintenance_message(struct pri *ctrl, q931_mh *mh, q931_call *c)
{
if ((!ctrl) || (!mh) || (!c)) {
return -1;
}
/* SERVICE messages are a superset of messages that can take b-channels
* or entire d-channels in and out of service */
switch(mh->msg) {
/* the ATT_SERVICE/ATT_SERVICE_ACKNOWLEDGE and NATIONAL_SERVICE/NATIONAL_SERVICE_ACKNOWLEDGE
* are mirrors of each other. We only have to check for one type because they are pre-handled
* the same way as each other */
case ATT_SERVICE:
case ATT_SERVICE_ACKNOWLEDGE:
c->channelno = -1;
c->slotmap = -1;
c->chanflags = 0;
c->ds1explicit = 0;
c->ds1no = 0;
c->cis_call = 0;
c->ri = -1;
c->changestatus = -1;
break;
default:
pri_error(ctrl,
"!! Don't know how to pre-handle maintenance message type '0x%X'\n",
mh->msg);
return -1;
}
return 0;
}
static int prepare_to_handle_q931_message(struct pri *ctrl, q931_mh *mh, q931_call *c)
{
if ((!ctrl) || (!mh) || (!c)) {
return -1;
}
switch(mh->msg) {
case Q931_RESTART:
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "-- Processing Q.931 Restart\n");
/* Reset information */
c->channelno = -1;
c->slotmap = -1;
c->chanflags = 0;
c->ds1no = 0;
c->ds1explicit = 0;
c->cis_call = 0;
c->ri = -1;
break;
case Q931_FACILITY:
c->notify = -1;
q931_party_number_init(&c->redirection_number);
if (q931_is_dummy_call(c)) {
q931_party_address_init(&c->called);
}
break;
case Q931_SETUP:
if (ctrl->debug & PRI_DEBUG_Q931_STATE)
pri_message(ctrl, "-- Processing Q.931 Call Setup\n");
c->cc.saved_ie_contents.length = 0;
c->cc.saved_ie_flags = 0;
/* Fall through */
case Q931_REGISTER:
c->channelno = -1;
c->slotmap = -1;
c->chanflags = 0;
c->ds1no = 0;
c->ri = -1;
c->bc.transcapability = -1;
c->bc.transmoderate = -1;
c->bc.transmultiple = -1;
c->bc.userl1 = -1;
c->bc.userl2 = -1;
c->bc.userl3 = -1;
c->bc.rateadaption = -1;
q931_party_address_init(&c->called);
q931_party_id_init(&c->local_id);
q931_party_id_init(&c->remote_id);
q931_party_number_init(&c->ani);
q931_party_redirecting_init(&c->redirecting);
/*
* Make sure that keypad and overlap digit buffers are empty in
* case they are not in the message.
*/
c->keypad_digits[0] = '\0';
c->overlap_digits[0] = '\0';
c->useruserprotocoldisc = -1;
c->useruserinfo[0] = '\0';
c->complete = 0;
c->nonisdn = 0;
c->aoc_units = -1;
c->reversecharge = -1;
/* Fall through */
case Q931_CONNECT:
case Q931_ALERTING:
case Q931_PROGRESS:
c->useruserinfo[0] = '\0';
c->cause = -1;
/* Fall through */
case Q931_SETUP_ACKNOWLEDGE:
case Q931_CALL_PROCEEDING:
c->progress = -1;
c->progressmask = 0;
break;
case Q931_CONNECT_ACKNOWLEDGE:
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = 0;
break;
case Q931_RELEASE:
case Q931_DISCONNECT:
c->cause = -1;
c->causecode = -1;
c->causeloc = -1;
c->progress = -1;
c->progressmask = 0;
c->aoc_units = -1;
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = 0;
c->useruserinfo[0] = '\0';
break;
case Q931_RELEASE_COMPLETE:
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = 0;
c->useruserinfo[0] = '\0';
/* Fall through */
case Q931_STATUS:
c->cause = -1;
c->causecode = -1;
c->causeloc = -1;
c->sugcallstate = Q931_CALL_STATE_NOT_SET;
c->aoc_units = -1;
break;
case Q931_RESTART_ACKNOWLEDGE:
c->channelno = -1;
c->ds1no = 0;
c->ds1explicit = 0;
c->cis_call = 0;
break;
case Q931_INFORMATION:
/*
* Make sure that keypad and overlap digit buffers are empty in
* case they are not in the message.
*/
c->keypad_digits[0] = '\0';
c->overlap_digits[0] = '\0';
break;
case Q931_STATUS_ENQUIRY:
break;
case Q931_NOTIFY:
c->notify = -1;
q931_party_number_init(&c->redirection_number);
break;
case Q931_HOLD:
break;
case Q931_HOLD_ACKNOWLEDGE:
break;
case Q931_HOLD_REJECT:
c->cause = -1;
break;
case Q931_RETRIEVE:
c->channelno = 0xFF;
c->ds1no = 0;
c->ds1explicit = 0;
break;
case Q931_RETRIEVE_ACKNOWLEDGE:
break;
case Q931_RETRIEVE_REJECT:
c->cause = -1;
break;
case Q931_USER_INFORMATION:
case Q931_SEGMENT:
case Q931_CONGESTION_CONTROL:
case Q931_RESUME:
case Q931_RESUME_ACKNOWLEDGE:
case Q931_RESUME_REJECT:
case Q931_SUSPEND:
case Q931_SUSPEND_ACKNOWLEDGE:
case Q931_SUSPEND_REJECT:
pri_error(ctrl, "!! Not yet handling pre-handle message type %s (0x%X)\n",
msg2str(mh->msg), mh->msg);
/* Fall through */
default:
pri_error(ctrl, "!! Don't know how to pre-handle message type %s (0x%X)\n",
msg2str(mh->msg), mh->msg);
q931_status(ctrl,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
return -1;
}
return 0;
}
static struct q931_call *q931_get_subcall_winner(struct q931_call *master)
{
if (master->pri_winner < 0) {
return NULL;
} else {
return master->subcalls[master->pri_winner];
}
}
static void initiate_hangup_if_needed(struct q931_call *master, int idx, int cause)
{
struct pri *ctrl;
struct q931_call *subcall;
ctrl = master->pri;
subcall = master->subcalls[idx];
if (!subcall->hangupinitiated) {
q931_hangup(ctrl, subcall, cause);
if (master->subcalls[idx] == subcall) {
/* The subcall was not destroyed. */
subcall->alive = 0;
}
}
}
static void q931_set_subcall_winner(struct q931_call *subcall)
{
struct q931_call *master = subcall->master_call;
int i;
/* Set the winner first */
for (i = 0; ; ++i) {
if (ARRAY_LEN(master->subcalls) <= i) {
pri_error(subcall->pri, "We should always find the winner in the list!\n");
return;
}
if (master->subcalls[i] == subcall) {
master->pri_winner = i;
break;
}
}
/* Start tear down of calls that were not chosen */
for (i = 0; i < ARRAY_LEN(master->subcalls); ++i) {
if (master->subcalls[i] && master->subcalls[i] != subcall) {
initiate_hangup_if_needed(master, i, PRI_CAUSE_NONSELECTED_USER_CLEARING);
}
}
}
static struct q931_call *q931_get_subcall(struct q921_link *link, struct q931_call *master_call)
{
int i;
struct q931_call *cur;
struct pri *ctrl;
int firstfree = -1;
ctrl = link->ctrl;
/* First try to locate our subcall */
for (i = 0; i < ARRAY_LEN(master_call->subcalls); ++i) {
if (master_call->subcalls[i]) {
if (master_call->subcalls[i]->link == link) {
return master_call->subcalls[i];
}
} else if (firstfree == -1) {
firstfree = i;
}
}
if (firstfree < 0) {
pri_error(ctrl, "Tried to add more than %d TEIs to call and failed\n",
(int) ARRAY_LEN(master_call->subcalls));
return NULL;
}
/* Create new subcall. */
cur = malloc(sizeof(*cur));
if (!cur) {
pri_error(ctrl, "Unable to allocate call\n");
return NULL;
}
*cur = *master_call;
//cur->pri = ctrl;/* We get this assignment for free. */
cur->link = link;
cur->next = NULL;
cur->apdus = NULL;
cur->bridged_call = NULL;
//cur->master_call = master_call; /* We get this assignment for free. */
for (i = 0; i < ARRAY_LEN(cur->subcalls); ++i) {
cur->subcalls[i] = NULL;
}
cur->t303_timer = 0;/* T303 should only be on on the master call */
cur->t312_timer = 0;/* T312 should only be on on the master call */
cur->fake_clearing_timer = 0;/* Fake clearing should only be on on the master call */
cur->hold_timer = 0;
cur->retranstimer = 0;
/*
* Mark this subcall as a newcall until it is determined if the
* subcall can compete for the call.
*/
cur->newcall = 1;
/* Assume we sent a SETUP and this is the first response to it from this peer. */
cur->ourcallstate = Q931_CALL_STATE_CALL_INITIATED;
cur->peercallstate = Q931_CALL_STATE_CALL_PRESENT;
master_call->subcalls[firstfree] = cur;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Adding subcall %p for TEI %d to call %p at position %d\n",
cur, link->tei, master_call, firstfree);
}
/* Should only get here if the TEI is not found */
return cur;
}
int q931_receive(struct q921_link *link, q931_h *h, int len)
{
q931_mh *mh;
struct q931_call *c;
struct pri *ctrl;
q931_ie *ie;
unsigned int x;
int y;
int res;
int r;
int mandies[MAX_MAND_IES];
int is_mandatory;
int codeset, cur_codeset;
int last_ie[8];
int cref;
int allow_event;
int allow_posthandle;
enum mandatory_ie_status mand_status;
ctrl = link->ctrl;
memset(last_ie, 0, sizeof(last_ie));
ctrl->q931_rxcount++;
if (len < 3 || len < 3 + h->crlen) {
/* Message too short for supported protocols. */
return -1;
}
switch (h->pd) {
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_1:
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_2:
if (!ctrl->service_message_support) {
/* Real service message support has not been enabled (and is OFF in libpri by default),
* so we have to revert to the 'traditional' KLUDGE of changing byte 4 from a 0xf (SERVICE)
* to a 0x7 (SERVICE ACKNOWLEDGE) */
/* This is the weird maintenance stuff. We majorly
KLUDGE this by changing byte 4 from a 0xf (SERVICE)
to a 0x7 (SERVICE ACKNOWLEDGE) */
h->raw[h->crlen + 2] -= 0x8;
q931_xmit(link, h, len, 1, 0);
return 0;
}
break;
default:
if (h->pd != ctrl->protodisc) {
pri_error(ctrl,
"Warning: unknown/inappropriate protocol discriminator received (%02x/%d)\n",
h->pd, h->pd);
return 0;
}
break;
}
cref = q931_cr(h);
c = q931_getcall(link, cref);
if (!c) {
pri_error(ctrl, "Unable to locate call %d\n", cref);
return -1;
}
if (c->master_call->outboundbroadcast && link != &ctrl->link) {
c = q931_get_subcall(link, c->master_call);
if (!c) {
pri_error(ctrl, "Unable to locate subcall for %d\n", cref);
return -1;
}
}
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl,
"Received message for call %p on link %p TEI/SAPI %d/%d\n",
c, link, link->tei, link->sapi);
}
/* Preliminary handling */
ctrl->facility.count = 0;
c->connected_number_in_message = 0;
c->redirecting_number_in_message = 0;
mh = (q931_mh *)(h->contents + h->crlen);
switch (h->pd) {
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_1:
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_2:
prepare_to_handle_maintenance_message(ctrl, mh, c);
break;
default:
/* Unknown protocol discriminator but we will treat it as Q.931 anyway. */
case GR303_PROTOCOL_DISCRIMINATOR:
case Q931_PROTOCOL_DISCRIMINATOR:
if (prepare_to_handle_q931_message(ctrl, mh, c)) {
/* Discard message. We don't know how to handle it. */
if (c->newcall) {
pri_destroycall(ctrl, c);
}
return 0;
}
break;
}
q931_clr_subcommands(ctrl);
q931_display_clear(c);
/* Determine which ies are mandatory for this message. */
memset(mandies, 0, sizeof(mandies));
for (x = 0; x < ARRAY_LEN(msgs); ++x) {
if (msgs[x].msgnum == mh->msg) {
memcpy(mandies, msgs[x].mandies, sizeof(mandies));
break;
}
}
for (x = 0; x < ARRAY_LEN(mandies); ++x) {
if (mandies[x]) {
/* Check mandatory channel identification ie exceptions */
if (mandies[x] != Q931_CHANNEL_IDENT
/* Always mandatory for RESUME_ACKNOWLEDGE */
|| mh->msg == Q931_RESUME_ACKNOWLEDGE
/* Mandatory in Net -> CPE direction for SETUP */
|| (mh->msg == Q931_SETUP && ctrl->localtype == PRI_CPE)
/* Mandatory for first SETUP response message in Net -> CPE direction. */
|| c->channel_id_ie_mandatory) {
/* ie is mandatory for this message */
continue;
}
/* ie is not mandatory for this message */
mandies[x] = 0;
}
}
mand_status = MAND_STATUS_OK;
/* Do real IE processing */
len -= (h->crlen + 3);
codeset = cur_codeset = 0;
for (x = 0; x < len; x += r) {
ie = (q931_ie *)(mh->data + x);
r = ielen_checked(ie, len - x);
if (r < 0) {
/* We have garbage on the end of the packet. */
pri_error(ctrl, "XXX Message longer than it should be?? XXX\n");
if (x) {
/* Allow the message anyway. We have already processed an ie. */
break;
}
q931_display_clear(c);
return -1;
}
/* Check if processing a mandatory ie. */
is_mandatory = 0;
for (y = 0; y < ARRAY_LEN(mandies); ++y) {
if (mandies[y] == Q931_FULL_IE(cur_codeset, ie->ie)) {
mandies[y] = 0;
is_mandatory = 1;
}
}
/* Special processing for codeset shifts */
switch (ie->ie & 0xf8) {
case Q931_LOCKING_SHIFT:
y = ie->ie & 7; /* Requested codeset */
/* Locking shifts couldn't go to lower codeset, and couldn't follows non-locking shifts - verify this */
if ((cur_codeset != codeset) && (ctrl->debug & PRI_DEBUG_Q931_ANOMALY))
pri_message(ctrl, "XXX Locking shift immediately follows non-locking shift (from %d through %d to %d) XXX\n", codeset, cur_codeset, y);
if (y > 0) {
if ((y < codeset) && (ctrl->debug & PRI_DEBUG_Q931_ANOMALY))
pri_error(ctrl, "!! Trying to locked downshift codeset from %d to %d !!\n", codeset, y);
codeset = cur_codeset = y;
}
else {
/* Locking shift to codeset 0 is forbidden by all specifications */
pri_error(ctrl, "!! Invalid locking shift to codeset 0 !!\n");
}
break;
case Q931_NON_LOCKING_SHIFT:
cur_codeset = ie->ie & 7;
break;
default:
/* Sanity check for IE code order */
if (!(ie->ie & 0x80)) {
if (last_ie[cur_codeset] > ie->ie) {
if ((ctrl->debug & PRI_DEBUG_Q931_ANOMALY))
pri_message(ctrl, "XXX Out-of-order IE %d at codeset %d (last was %d)\n", ie->ie, cur_codeset, last_ie[cur_codeset]);
}
else
last_ie[cur_codeset] = ie->ie;
}
/* Ignore non-locking shifts for TR41459-based signalling */
switch (ctrl->switchtype) {
case PRI_SWITCH_LUCENT5E:
case PRI_SWITCH_ATT4ESS:
if (cur_codeset != codeset) {
if ((ctrl->debug & PRI_DEBUG_Q931_DUMP))
pri_message(ctrl, "XXX Ignoring IE %d for temporary codeset %d XXX\n", ie->ie, cur_codeset);
break;
}
/* Fall through */
default:
y = q931_handle_ie(cur_codeset, ctrl, c, mh->msg, ie);
if (y < 0 && is_mandatory) {
/* Error processing mandatory ie. */
mand_status = MAND_STATUS_CONTENT_ERROR;
pri_error(ctrl, "%s: Error in mandatory IE %d (cs%d, %s)\n",
msg2str(mh->msg),
ie->ie,
cur_codeset,
ie2str(Q931_FULL_IE(codeset, ie->ie)));
}
/* XXX Applicable to codeset 0 only? XXX */
if (!cur_codeset && !(ie->ie & 0xf0) && (y < 0)) {
/*
* Q.931 Section 5.8.7.1
* Unhandled ies in codeset 0 with the
* upper nybble zero are mandatory.
*/
mandies[MAX_MAND_IES - 1] = Q931_FULL_IE(cur_codeset, ie->ie);
}
break;
}
/* Reset current codeset */
cur_codeset = codeset;
break;
}
}
/* Check for missing mandatory ies. */
for (x = 0; x < ARRAY_LEN(mandies); ++x) {
if (mandies[x]) {
/* This mandatory ie was not processed. */
mand_status = MAND_STATUS_MISSING;
pri_error(ctrl, "%s: Missing mandatory IE %d (cs%d, %s)\n",
msg2str(mh->msg),
Q931_IE_IE(mandies[x]),
Q931_IE_CODESET(mandies[x]),
ie2str(mandies[x]));
}
}
if (mand_status == MAND_STATUS_OK) {
switch (mh->msg) {
case Q931_SETUP:
case Q931_CONNECT:
if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_INITIAL) {
q931_display_name_get(c, &c->remote_id.name);
}
break;
default:
break;
}
/* Now handle the facility ie's after all the other ie's were processed. */
q931_handle_facilities(ctrl, c, mh->msg);
}
q931_apdu_msg_expire(ctrl, c, mh->msg);
/* Post handling */
switch (h->pd) {
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_1:
case MAINTENANCE_PROTOCOL_DISCRIMINATOR_2:
res = post_handle_maintenance_message(ctrl, h->pd, mh, c);
q931_display_clear(c);
break;
default:
allow_event = 1;
allow_posthandle = 1;
if (c->master_call->outboundbroadcast) {
nt_ptmp_handle_q931_message(ctrl, mh, c, &allow_event, &allow_posthandle);
if (allow_event) {
q931_apdu_msg_expire(ctrl, c->master_call, mh->msg);
}
}
if (allow_posthandle) {
res = post_handle_q931_message(ctrl, mh, c, mand_status);
if (res == Q931_RES_HAVEEVENT && !allow_event) {
res = 0;
}
} else {
q931_display_clear(c);
res = 0;
}
break;
}
return res;
}
static int post_handle_maintenance_message(struct pri *ctrl, int protodisc, struct q931_mh *mh, struct q931_call *c)
{
/* Do some maintenance stuff */
if (((protodisc == MAINTENANCE_PROTOCOL_DISCRIMINATOR_1) && (mh->msg == ATT_SERVICE))
|| ((protodisc == MAINTENANCE_PROTOCOL_DISCRIMINATOR_2) && (mh->msg == NATIONAL_SERVICE))) {
if (c->channelno > 0) {
ctrl->ev.e = PRI_EVENT_SERVICE;
ctrl->ev.service.channel = q931_encode_channel(c);
ctrl->ev.service.changestatus = 0x0f & c->changestatus;
} else {
switch (0x0f & c->changestatus) {
case SERVICE_CHANGE_STATUS_INSERVICE:
ctrl->ev.e = PRI_EVENT_DCHAN_UP;
//q921_dchannel_up(ctrl);
break;
case SERVICE_CHANGE_STATUS_OUTOFSERVICE:
ctrl->ev.e = PRI_EVENT_DCHAN_DOWN;
//q921_dchannel_down(ctrl);
break;
default:
pri_error(ctrl, "!! Don't know how to handle span service change status '%d'\n", (0x0f & c->changestatus));
return -1;
}
}
maintenance_service_ack(ctrl, c);
return Q931_RES_HAVEEVENT;
}
if (((protodisc == MAINTENANCE_PROTOCOL_DISCRIMINATOR_1) && (mh->msg == ATT_SERVICE_ACKNOWLEDGE))
|| ((protodisc == MAINTENANCE_PROTOCOL_DISCRIMINATOR_2) && (mh->msg == NATIONAL_SERVICE_ACKNOWLEDGE))) {
if (c->channelno > 0) {
ctrl->ev.e = PRI_EVENT_SERVICE_ACK;
ctrl->ev.service_ack.channel = q931_encode_channel(c);
ctrl->ev.service_ack.changestatus = 0x0f & c->changestatus;
} else {
switch (0x0f & c->changestatus) {
case SERVICE_CHANGE_STATUS_INSERVICE:
ctrl->ev.e = PRI_EVENT_DCHAN_UP;
//q921_dchannel_up(ctrl);
break;
case SERVICE_CHANGE_STATUS_OUTOFSERVICE:
ctrl->ev.e = PRI_EVENT_DCHAN_DOWN;
//q921_dchannel_down(ctrl);
break;
default:
pri_error(ctrl, "!! Don't know how to handle span service change status '%d'\n", (0x0f & c->changestatus));
return -1;
}
}
return Q931_RES_HAVEEVENT;
}
pri_error(ctrl, "!! Don't know how to post-handle maintenance message type 0x%X\n",
mh->msg);
return -1;
}
/*!
* \internal
* \brief Rank the given Q.931 call state for call etablishment.
*
* \param state Q.931 call state to rank for competing PTMP NT calls.
*
* \return Call establishment state ranking.
*/
static enum Q931_RANKED_CALL_STATE q931_rank_state(enum Q931_CALL_STATE state)
{
enum Q931_RANKED_CALL_STATE rank;
switch (state) {
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_CALL_PRESENT:
rank = Q931_RANKED_CALL_STATE_PRESENT;
break;
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OVERLAP_RECEIVING:
rank = Q931_RANKED_CALL_STATE_OVERLAP;
break;
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
rank = Q931_RANKED_CALL_STATE_PROCEEDING;
break;
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
rank = Q931_RANKED_CALL_STATE_ALERTING;
break;
case Q931_CALL_STATE_ACTIVE:
case Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE:
rank = Q931_RANKED_CALL_STATE_CONNECT;
break;
case Q931_CALL_STATE_CALL_ABORT:
rank = Q931_RANKED_CALL_STATE_ABORT;
break;
default:
rank = Q931_RANKED_CALL_STATE_OTHER;
break;
}
return rank;
}
/*!
* \brief Determine if the master will pass an event to the upper layer.
*
* \param ctrl D channel controller.
* \param subcall Q.931 call leg.
* \param msg_type Current message type being processed.
*
* \note This function must parallel nt_ptmp_handle_q931_message().
*
* \retval TRUE if the master will pass an event to the upper layer.
* \retval FALSE if the event will be blocked.
*/
int q931_master_pass_event(struct pri *ctrl, struct q931_call *subcall, int msg_type)
{
struct q931_call *winner;
struct q931_call *master;
enum Q931_RANKED_CALL_STATE master_rank;
enum Q931_RANKED_CALL_STATE subcall_rank;
int will_pass; /*!< TRUE if the master will pass an event to the upper layer. */
master = subcall->master_call;
if (subcall == master) {
/* We are the master call so of course the master will pass an event. */
return 1;
}
winner = q931_get_subcall_winner(master);
if (winner && subcall == winner) {
/* We are the winner so of course the master will pass an event. */
return 1;
}
master_rank = q931_rank_state(master->ourcallstate);
will_pass = 0;
switch (msg_type) {
case Q931_SETUP_ACKNOWLEDGE:
#if 0 /* Overlap dialing in PTMP NT mode not supported at the present time. */
if (master_rank < Q931_RANKED_CALL_STATE_OVERLAP) {
will_pass = 1;
}
#endif /* Overlap dialing in PTMP NT mode not supported at the present time. */
break;
case Q931_CALL_PROCEEDING:
if (master_rank < Q931_RANKED_CALL_STATE_PROCEEDING) {
will_pass = 1;
}
break;
case Q931_PROGRESS:
/*
* We will just ignore this message since there could be multiple devices
* competing for this call. Who has access to the B channel at this time
* to give in-band signals anyway?
*/
break;
case Q931_ALERTING:
if (master_rank < Q931_RANKED_CALL_STATE_ALERTING) {
will_pass = 1;
}
break;
case Q931_CONNECT:
if (master_rank < Q931_RANKED_CALL_STATE_CONNECT) {
/* We are expected to be the winner for the next message. */
will_pass = 1;
}
break;
case Q931_DISCONNECT:
case Q931_RELEASE:
case Q931_RELEASE_COMPLETE:
/* Only deal with the winner. */
break;
case Q931_FACILITY:
case Q931_NOTIFY:
if (!winner) {
/* The overlap rank does not count here. */
if (master_rank == Q931_RANKED_CALL_STATE_OVERLAP) {
master_rank = Q931_RANKED_CALL_STATE_PRESENT;
}
subcall_rank = q931_rank_state(subcall->ourcallstate);
if (subcall_rank == Q931_RANKED_CALL_STATE_OVERLAP) {
subcall_rank = Q931_RANKED_CALL_STATE_PRESENT;
}
if (master_rank == subcall_rank) {
/*
* No winner yet but the subcall is as advanced as the master.
* Allow the supplementary service event to pass.
*/
will_pass = 1;
}
}
break;
default:
/* Only deal with the winner. */
break;
}
return will_pass;
}
/*!
* \internal
* \brief Handle outboundbroadcast incoming messages for the master_call's state.
*
* \param ctrl D channel controller.
* \param mh Q.931 message type header.
* \param subcall Q.931 call leg.
* \param allow_event Where to set the allow event to upper layer flag.
* \param allow_posthandle Where to set the allow post handle event flag.
*
* \details
* This is where we interact the subcalls state with the master_call's state.
*
* \note This function must parallel q931_master_pass_event().
*
* \return Nothing
*/
static void nt_ptmp_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *subcall, int *allow_event, int *allow_posthandle)
{
struct q931_call *master = subcall->master_call;
struct q931_call *winner = q931_get_subcall_winner(master);
enum Q931_RANKED_CALL_STATE master_rank;
enum Q931_RANKED_CALL_STATE subcall_rank;
/* For broadcast calls, we default to not allowing events to keep events received to a minimum
* and to allow post processing, since that is where hangup and subcall state handling and other processing is done */
*allow_event = 0;
*allow_posthandle = 1;
master_rank = q931_rank_state(master->ourcallstate);
if (master_rank < Q931_RANKED_CALL_STATE_CONNECT) {
/* This subcall can compete for the call. */
subcall->newcall = 0;
}
switch (mh->msg) {
case Q931_SETUP_ACKNOWLEDGE:
#if 0 /* Overlap dialing in PTMP NT mode not supported at the present time. */
if (master_rank < Q931_RANKED_CALL_STATE_OVERLAP) {
*allow_event = 1;
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_OVERLAP_SENDING);
}
#endif /* Overlap dialing in PTMP NT mode not supported at the present time. */
break;
case Q931_CALL_PROCEEDING:
if (master_rank < Q931_RANKED_CALL_STATE_PROCEEDING) {
*allow_event = 1;
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING);
}
break;
case Q931_PROGRESS:
/*
* We will just ignore this message since there could be multiple devices
* competing for this call. Who has access to the B channel at this time
* to give in-band signals anyway?
*/
break;
case Q931_ALERTING:
if (master_rank < Q931_RANKED_CALL_STATE_ALERTING) {
*allow_event = 1;
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_CALL_DELIVERED);
}
break;
case Q931_CONNECT:
if (master_rank < Q931_RANKED_CALL_STATE_CONNECT) {
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_ACTIVE);
q931_set_subcall_winner(subcall);
*allow_event = 1;
} else {
/* Call clearing of non selected calls occurs in
* q931_set_subcall_winner() - All we need to do is make sure
* that this connect is not acknowledged */
*allow_posthandle = 0;
}
break;
case Q931_DISCONNECT:
case Q931_RELEASE:
case Q931_RELEASE_COMPLETE:
if (!winner) {
int master_priority;
int slave_priority;
/* Pass up the cause on a priority basis. */
switch (master->cause) {
case PRI_CAUSE_USER_BUSY:
master_priority = 2;
break;
case PRI_CAUSE_CALL_REJECTED:
master_priority = 1;
break;
default:
master_priority = 0;
break;
case -1:
/* First time priority. */
master_priority = -2;
break;
}
switch (subcall->cause) {
case PRI_CAUSE_USER_BUSY:
slave_priority = 2;
break;
case PRI_CAUSE_CALL_REJECTED:
slave_priority = 1;
break;
default:
slave_priority = 0;
break;
case PRI_CAUSE_INCOMPATIBLE_DESTINATION:
/* Cause explicitly ignored */
slave_priority = -1;
break;
}
if (master_priority < slave_priority) {
/* Pass up the cause to the master. */
master->cause = subcall->cause;
}
} else {
/* There *is* a winner */
if (subcall == winner) {
/* .. and we're it: */
*allow_event = 1;
UPDATE_OURCALLSTATE(ctrl, master, Q931_CALL_STATE_CALL_ABORT);
}
}
break;
case Q931_FACILITY:
case Q931_NOTIFY:
if (winner) {
if (subcall == winner) {
/* Only deal with the winner. */
*allow_event = 1;
}
} else {
/* The overlap rank does not count here. */
if (master_rank == Q931_RANKED_CALL_STATE_OVERLAP) {
master_rank = Q931_RANKED_CALL_STATE_PRESENT;
}
subcall_rank = q931_rank_state(subcall->ourcallstate);
if (subcall_rank == Q931_RANKED_CALL_STATE_OVERLAP) {
subcall_rank = Q931_RANKED_CALL_STATE_PRESENT;
}
if (master_rank == subcall_rank) {
/*
* No winner yet but the subcall is as advanced as the master.
* Allow the supplementary service event to pass.
*/
*allow_event = 1;
}
}
break;
default:
if (winner && subcall == winner) {
/* Only deal with the winner. */
*allow_event = 1;
}
break;
}
}
/*!
* \internal
* \brief Fill in the RING event fields.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg
*
* \return Nothing
*/
static void q931_fill_ring_event(struct pri *ctrl, struct q931_call *call)
{
struct pri_subcommand *subcmd;
if (call->redirecting.from.number.valid && !call->redirecting.count) {
/*
* This is most likely because the redirecting number came in
* with the redirecting ie only and not a DivertingLegInformation2.
*/
call->redirecting.count = 1;
}
if (call->redirecting.state == Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3) {
/*
* Valid for Q.SIG and ETSI PRI/BRI-PTP modes:
* Setup the redirecting.to informtion so we can identify
* if the user wants to manually supply the COLR for this
* redirected to number if further redirects could happen.
*
* All the user needs to do is set the REDIRECTING(to-pres)
* to the COLR and REDIRECTING(to-num) = complete-dialed-number
* (i.e. CALLERID(dnid)) to be safe after determining that the
* incoming call was redirected by checking if the
* REDIRECTING(count) is nonzero.
*/
call->redirecting.to.number = call->called.number;
call->redirecting.to.number.presentation =
PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
}
ctrl->ev.e = PRI_EVENT_RING;
ctrl->ev.ring.subcmds = &ctrl->subcmds;
ctrl->ev.ring.channel = q931_encode_channel(call);
/* Calling party information */
ctrl->ev.ring.callingpres = q931_party_id_presentation(&call->remote_id);
ctrl->ev.ring.callingplan = call->remote_id.number.plan;
if (call->ani.valid) {
ctrl->ev.ring.callingplanani = call->ani.plan;
libpri_copy_string(ctrl->ev.ring.callingani, call->ani.str,
sizeof(ctrl->ev.ring.callingani));
} else {
ctrl->ev.ring.callingplanani = -1;
ctrl->ev.ring.callingani[0] = '\0';
}
libpri_copy_string(ctrl->ev.ring.callingnum, call->remote_id.number.str,
sizeof(ctrl->ev.ring.callingnum));
libpri_copy_string(ctrl->ev.ring.callingname, call->remote_id.name.str,
sizeof(ctrl->ev.ring.callingname));
q931_party_id_copy_to_pri(&ctrl->ev.ring.calling, &call->remote_id);
/* for backwards compatibility, still need ctrl->ev.ring.callingsubaddr */
if (!call->remote_id.subaddress.type) {
/* NSAP: Type = 0 */
libpri_copy_string(ctrl->ev.ring.callingsubaddr,
(char *) call->remote_id.subaddress.data,
sizeof(ctrl->ev.ring.callingsubaddr));
} else {
ctrl->ev.ring.callingsubaddr[0] = '\0';
}
ctrl->ev.ring.ani2 = call->ani2;
/* Called party information */
ctrl->ev.ring.calledplan = call->called.number.plan;
libpri_copy_string(ctrl->ev.ring.callednum, call->called.number.str,
sizeof(ctrl->ev.ring.callednum));
q931_party_subaddress_copy_to_pri(&ctrl->ev.ring.called_subaddress,
&call->called.subaddress);
/* Original called party information (For backward compatibility) */
libpri_copy_string(ctrl->ev.ring.origcalledname,
call->redirecting.orig_called.name.str, sizeof(ctrl->ev.ring.origcalledname));
libpri_copy_string(ctrl->ev.ring.origcallednum,
call->redirecting.orig_called.number.str, sizeof(ctrl->ev.ring.origcallednum));
ctrl->ev.ring.callingplanorigcalled = call->redirecting.orig_called.number.plan;
if (call->redirecting.orig_called.number.valid
|| call->redirecting.orig_called.name.valid) {
ctrl->ev.ring.origredirectingreason = call->redirecting.orig_reason;
} else {
ctrl->ev.ring.origredirectingreason = -1;
}
/* Redirecting from party information (For backward compatibility) */
ctrl->ev.ring.callingplanrdnis = call->redirecting.from.number.plan;
libpri_copy_string(ctrl->ev.ring.redirectingnum, call->redirecting.from.number.str,
sizeof(ctrl->ev.ring.redirectingnum));
libpri_copy_string(ctrl->ev.ring.redirectingname, call->redirecting.from.name.str,
sizeof(ctrl->ev.ring.redirectingname));
ctrl->ev.ring.redirectingreason = call->redirecting.reason;
libpri_copy_string(ctrl->ev.ring.useruserinfo, call->useruserinfo,
sizeof(ctrl->ev.ring.useruserinfo));
call->useruserinfo[0] = '\0';
libpri_copy_string(ctrl->ev.ring.keypad_digits, call->keypad_digits,
sizeof(ctrl->ev.ring.keypad_digits));
ctrl->ev.ring.flexible = !(call->chanflags & FLAG_EXCLUSIVE);
ctrl->ev.ring.cref = call->cr;
ctrl->ev.ring.call = call->master_call;
ctrl->ev.ring.layer1 = call->bc.userl1;
ctrl->ev.ring.complete = call->complete;
ctrl->ev.ring.ctype = call->bc.transcapability;
ctrl->ev.ring.progress = call->progress;
ctrl->ev.ring.progressmask = call->progressmask;
ctrl->ev.ring.reversecharge = call->reversecharge;
if (call->redirecting.count) {
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
/* Setup redirecting subcommand */
subcmd->cmd = PRI_SUBCMD_REDIRECTING;
q931_party_redirecting_copy_to_pri(&subcmd->u.redirecting,
&call->redirecting);
}
}
}
/*!
* \internal
* \brief Fill in the FACILITY event fields.
*
* \param ctrl D channel controller.
* \param call Q.931 call leg.
*
* \return Nothing
*/
static void q931_fill_facility_event(struct pri *ctrl, struct q931_call *call)
{
ctrl->ev.e = PRI_EVENT_FACILITY;
ctrl->ev.facility.subcmds = &ctrl->subcmds;
ctrl->ev.facility.channel = q931_encode_channel(call);
ctrl->ev.facility.cref = call->cr;
if (q931_is_dummy_call(call)) {
ctrl->ev.facility.call = NULL;
} else {
ctrl->ev.facility.call = call->master_call;
}
ctrl->ev.facility.subcall = call;
/* Need to do this for backward compatibility with struct pri_event_facname */
libpri_copy_string(ctrl->ev.facility.callingname, call->remote_id.name.str,
sizeof(ctrl->ev.facility.callingname));
libpri_copy_string(ctrl->ev.facility.callingnum, call->remote_id.number.str,
sizeof(ctrl->ev.facility.callingnum));
ctrl->ev.facility.callingpres = q931_party_id_presentation(&call->remote_id);
ctrl->ev.facility.callingplan = call->remote_id.number.plan;
}
/*!
* \internal
* \brief APDU wait for response message timeout.
*
* \param data Callback data pointer.
*
* \return Nothing
*/
static void q931_apdu_timeout(void *data)
{
struct apdu_event *apdu;
struct pri *ctrl;
struct q931_call *call;
int free_it;
apdu = data;
call = apdu->call;
ctrl = call->pri;
/*
* Extract the APDU from the list so it cannot be
* deleted from under us by the callback.
*/
free_it = pri_call_apdu_extract(call, apdu);
q931_clr_subcommands(ctrl);
apdu->response.callback(APDU_CALLBACK_REASON_TIMEOUT, ctrl, call, apdu, NULL);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, call);
ctrl->schedev = 1;
}
if (free_it) {
free(apdu);
}
}
/*!
* \brief Generic call-completion timeout event handler.
*
* \param ctrl D channel controller.
* \param cc_record Call completion record to process event.
* \param event Event to process.
*
* \retval nonzero if cc record destroyed because FSM completed.
*/
int q931_cc_timeout(struct pri *ctrl, struct pri_cc_record *cc_record, enum CC_EVENTS event)
{
q931_call *call;
q931_call *dummy;
int fsm_complete;
q931_clr_subcommands(ctrl);
dummy = ctrl->link.dummy_call;
call = cc_record->signaling;
if (!call) {
/* Substitute the broadcast dummy call reference call. */
call = dummy;
}
fsm_complete = pri_cc_event(ctrl, call, cc_record, event);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, dummy);
ctrl->schedev = 1;
}
return fsm_complete;
}
/*!
* \brief Generic call-completion indirect event creation.
*
* \param ctrl D channel controller.
* \param cc_record Call completion record to process event.
* \param func Function to call that will generate a libpri event.
*
* \return Nothing
*/
void q931_cc_indirect(struct pri *ctrl, struct pri_cc_record *cc_record, void (*func)(struct pri *ctrl, q931_call *call, struct pri_cc_record *cc_record))
{
q931_call *call;
q931_call *dummy;
q931_clr_subcommands(ctrl);
dummy = ctrl->link.dummy_call;
call = cc_record->signaling;
if (!call) {
/* Substitute the broadcast dummy call reference call. */
call = dummy;
}
func(ctrl, call, cc_record);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, dummy);
ctrl->schedev = 1;
}
}
/*!
* \brief Find the transfer call indicated by the given link_id.
*
* \param ctrl D channel controller.
* \param link_id Link id of the other call involved in the transfer.
*
* \retval found-master-call on success.
* \retval NULL on error.
*/
struct q931_call *q931_find_link_id_call(struct pri *ctrl, int link_id)
{
struct q931_call *cur;
struct q931_call *winner;
struct q931_call *match;
match = NULL;
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->is_link_id_valid && cur->link_id == link_id) {
/* Found the link_id call. */
winner = q931_find_winning_call(cur);
if (!winner) {
/* There is no winner. */
break;
}
switch (winner->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
/* The link_id call is in a state suitable for transfer. */
match = cur;
break;
default:
/* The link_id call is not in a good state to transfer. */
break;
}
break;
}
}
return match;
}
/*!
* \brief Find the active call given the held call.
*
* \param ctrl D channel controller.
* \param held_call Held call to help locate a compatible active call.
*
* \retval master-active-call on success.
* \retval NULL on error.
*/
struct q931_call *q931_find_held_active_call(struct pri *ctrl, struct q931_call *held_call)
{
struct q931_call *cur;
struct q931_call *winner;
struct q931_call *match;
if (!held_call->link) {
/* Held call does not have an active link. */
return NULL;
}
match = NULL;
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->hold_state == Q931_HOLD_STATE_IDLE) {
/* Found an active call. */
winner = q931_find_winning_call(cur);
if (!winner || (BRI_NT_PTMP(ctrl) && winner->link != held_call->link)) {
/* There is no winner or the active call does not go to the same TEI. */
continue;
}
switch (winner->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
break;
default:
/* Active call not in a good state to transfer. */
continue;
}
if (q931_party_number_cmp(&winner->remote_id.number,
&held_call->remote_id.number)) {
/* The remote party number does not match. This is a weak match. */
match = cur;
continue;
}
/* Found an exact match. */
match = cur;
break;
}
}
return match;
}
/*!
* \internal
* \brief Find the held call given the active call.
*
* \param ctrl D channel controller.
* \param active_call Active call to help locate a compatible held call.
*
* \retval master-held-call on success.
* \retval NULL on error.
*/
static struct q931_call *q931_find_held_call(struct pri *ctrl, struct q931_call *active_call)
{
struct q931_call *cur;
struct q931_call *winner;
struct q931_call *match;
if (!active_call->link) {
/* Active call does not have an active link. */
return NULL;
}
match = NULL;
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->hold_state == Q931_HOLD_STATE_CALL_HELD) {
/* Found a held call. */
winner = q931_find_winning_call(cur);
if (!winner || (BRI_NT_PTMP(ctrl) && winner->link != active_call->link)) {
/* There is no winner or the held call does not go to the same TEI. */
continue;
}
switch (winner->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
break;
default:
/* Held call not in a good state to transfer. */
continue;
}
if (q931_party_number_cmp(&winner->remote_id.number,
&active_call->remote_id.number)) {
/* The remote party number does not match. This is a weak match. */
match = cur;
continue;
}
/* Found an exact match. */
match = cur;
break;
}
}
return match;
}
/*!
* \internal
* \brief Determine RELEASE_COMPLETE cause code for newcall rejection.
*
* \param call Q.931 call leg.
*
* \return Cause code for RELEASE_COMPLETE.
*/
static int newcall_rel_comp_cause(struct q931_call *call)
{
struct q931_call *master;
int cause;
cause = PRI_CAUSE_INVALID_CALL_REFERENCE;
master = call->master_call;
if (master != call && master->t312_timer) {
switch (master->ourcallstate) {
case Q931_CALL_STATE_CALL_ABORT:
cause = PRI_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
break;
default:
break;
}
}
return cause;
}
/*!
* \internal
* \brief Restart channel notify event for upper layer notify chain timeout.
*
* \param data Callback data pointer.
*
* \return Nothing
*/
static void q931_restart_notify_timeout(void *data)
{
struct q931_call *call = data;
struct pri *ctrl = call->pri;
/* Create channel restart event to upper layer. */
call->channelno = call->restart.chan_no[call->restart.idx++];
ctrl->ev.e = PRI_EVENT_RESTART;
ctrl->ev.restart.channel = q931_encode_channel(call);
ctrl->schedev = 1;
/* Reschedule for next channel restart event needed. */
if (call->restart.idx < call->restart.count) {
call->restart.timer = pri_schedule_event(ctrl, 0, q931_restart_notify_timeout,
call);
} else {
/* No more restart events needed. */
call->restart.timer = 0;
/* Send back the Restart Acknowledge. All channels are now restarted. */
if (call->slotmap != -1) {
/* Send slotmap format. */
call->channelno = -1;
}
restart_ack(ctrl, call);
}
}
/*!
* \internal
* \brief Setup restart channel notify events for upper layer.
*
* \param call Q.931 call leg.
*
* \return Nothing
*/
static void q931_restart_notify(struct q931_call *call)
{
struct pri *ctrl = call->pri;
/* Start notify chain. */
pri_schedule_del(ctrl, call->restart.timer);
call->restart.idx = 0;
q931_restart_notify_timeout(call);
}
/*!
* \internal
* \brief Process the decoded information in the Q.931 message.
*
* \param ctrl D channel controller.
* \param mh Q.931 message header.
* \param c Q.931 call leg.
* \param mand_status Mandatory ie status
*
* \note
* When this function returns c may be destroyed so you can no
* longer dereference it.
*
* \retval 0 if no error or event.
* \retval Q931_RES_HAVEEVENT if have an event.
* \retval -1 on error.
*/
static int post_handle_q931_message(struct pri *ctrl, struct q931_mh *mh, struct q931_call *c, enum mandatory_ie_status mand_status)
{
int res;
int changed;
int mand_cause;
enum Q931_CALL_STATE ourcallstate_orig;
struct apdu_event *cur = NULL;
struct pri_subcommand *subcmd;
struct q931_call *master_call;
mand_cause = 0;
switch (mand_status) {
case MAND_STATUS_OK:
break;
case MAND_STATUS_CONTENT_ERROR:
mand_cause = PRI_CAUSE_INVALID_IE_CONTENTS;
break;
case MAND_STATUS_MISSING:
mand_cause = PRI_CAUSE_MANDATORY_IE_MISSING;
break;
}
switch(mh->msg) {
case Q931_RESTART:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
pri_destroycall(ctrl, c);
break;
}
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_RESTART);
c->peercallstate = Q931_CALL_STATE_RESTART_REQUEST;
/* Notify upper layer of restart event */
if ((c->channelno == -1 && c->slotmap == -1) || !c->restart.count) {
/*
* Whole link restart or channel not identified by Channel ID ie
* 3.3 octets.
*
* Send back the Restart Acknowledge
*/
restart_ack(ctrl, c);
/* Create channel restart event to upper layer. */
ctrl->ev.e = PRI_EVENT_RESTART;
ctrl->ev.restart.channel = q931_encode_channel(c);
} else {
/* Start notify chain. */
q931_restart_notify(c);
}
return Q931_RES_HAVEEVENT;
case Q931_REGISTER:
q931_display_subcmd(ctrl, c);
/* Must be new call */
if (!c->newcall) {
q931_status(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
c->newcall = 0;
c->alive = 1;
c->cis_call = 1;
c->chanflags = FLAG_EXCLUSIVE;/* For safety mark this channel as exclusive. */
c->channelno = 0;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE);
c->peercallstate = Q931_CALL_STATE_CALL_INDEPENDENT_SERVICE;
if (c->cc.hangup_call) {
q931_release_complete(ctrl, c, PRI_CAUSE_NORMAL_CLEARING);
break;
}
if (/* c->cis_call && */ !c->cis_recognized) {
pri_message(ctrl,
"-- CIS connection not marked as handled. Disconnecting it.\n");
q931_release_complete(ctrl, c, PRI_CAUSE_FACILITY_NOT_IMPLEMENTED);
break;
}
q931_fill_ring_event(ctrl, c);
return Q931_RES_HAVEEVENT;
case Q931_SETUP:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_release_complete(ctrl, c, mand_cause);
break;
}
/* Must be new call */
if (!c->newcall) {
break;
}
if (c->progressmask & PRI_PROG_CALLER_NOT_ISDN)
c->nonisdn = 1;
c->newcall = 0;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_PRESENT);
c->peercallstate = Q931_CALL_STATE_CALL_INITIATED;
if (c->cis_call) {
/*
* Make call alive so any message events clearing this
* signaling call can pass up any subcmds.
*/
c->alive = 1;
} else {
/* it's not yet a call since higher level can respond with RELEASE or RELEASE_COMPLETE */
c->alive = 0;
}
if (c->bc.transmoderate != TRANS_MODE_64_CIRCUIT) {
q931_release_complete(ctrl, c, PRI_CAUSE_BEARERCAPABILITY_NOTIMPL);
break;
}
if (c->cc.hangup_call) {
q931_release_complete(ctrl, c, PRI_CAUSE_NORMAL_CLEARING);
break;
}
if (c->cis_call && !c->cis_recognized) {
pri_message(ctrl, "-- CIS call not marked as handled. Disconnecting it.\n");
q931_release_complete(ctrl, c, PRI_CAUSE_FACILITY_NOT_IMPLEMENTED);
break;
}
/* Save the initial cc-parties. (Incoming SETUP can only be a master call.) */
c->cc.party_a = c->remote_id;
q931_fill_ring_event(ctrl, c);
return Q931_RES_HAVEEVENT;
case Q931_ALERTING:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
break;
}
stop_t303(c->master_call);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
switch (c->ourcallstate) {
default:
q931_status(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
return 0;
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
break;
}
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CALL_DELIVERED);
c->peercallstate = Q931_CALL_STATE_CALL_RECEIVED;
ctrl->ev.e = PRI_EVENT_RINGING;
ctrl->ev.ringing.subcmds = &ctrl->subcmds;
ctrl->ev.ringing.channel = q931_encode_channel(c);
ctrl->ev.ringing.cref = c->cr;
ctrl->ev.ringing.call = c->master_call;
ctrl->ev.ringing.progress = c->progress;
ctrl->ev.ringing.progressmask = c->progressmask;
libpri_copy_string(ctrl->ev.ringing.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.ringing.useruserinfo));
c->useruserinfo[0] = '\0';
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
pri_cc_qsig_determine_available(ctrl, c);
break;
default:
break;
}
for (cur = c->apdus; cur; cur = cur->next) {
if (!cur->sent && cur->message == Q931_FACILITY) {
q931_facility(ctrl, c);
break;
}
}
return Q931_RES_HAVEEVENT;
case Q931_CONNECT:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
break;
}
stop_t303(c->master_call);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
switch (c->ourcallstate) {
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_PRESENT:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_OVERLAP_RECEIVING:
/* Accept CONNECT in these states. */
break;
default:
q931_status(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
return 0;
}
ctrl->ev.e = PRI_EVENT_ANSWER;
ctrl->ev.answer.subcmds = &ctrl->subcmds;
ctrl->ev.answer.channel = q931_encode_channel(c);
ctrl->ev.answer.cref = c->cr;
ctrl->ev.answer.call = c->master_call;
ctrl->ev.answer.progress = c->progress;
ctrl->ev.answer.progressmask = c->progressmask;
libpri_copy_string(ctrl->ev.answer.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.answer.useruserinfo));
c->useruserinfo[0] = '\0';
if (!ctrl->manual_connect_ack) {
q931_connect_acknowledge(ctrl, c, 0);
} else {
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_CONNECT_REQUEST);
c->peercallstate = Q931_CALL_STATE_CONNECT_REQUEST;
}
if (c->cis_auto_disconnect && c->cis_call) {
/* Make sure WE release when we initiate a signalling only connection */
q931_hangup(ctrl, c, PRI_CAUSE_NORMAL_CLEARING);
} else {
c->incoming_ct_state = INCOMING_CT_STATE_IDLE;
/* Setup connected line subcommand */
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
q931_party_id_copy_to_pri(&subcmd->u.connected_line.id, &c->remote_id);
}
return Q931_RES_HAVEEVENT;
}
break;
case Q931_FACILITY:
q931_display_subcmd(ctrl, c);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
switch (c->incoming_ct_state) {
case INCOMING_CT_STATE_POST_CONNECTED_LINE:
c->incoming_ct_state = INCOMING_CT_STATE_IDLE;
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
q931_party_id_copy_to_pri(&subcmd->u.connected_line.id, &c->remote_id);
}
break;
default:
break;
}
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, c);
return Q931_RES_HAVEEVENT;
}
break;
case Q931_PROGRESS:
ctrl->ev.e = PRI_EVENT_PROGRESS;
ctrl->ev.proceeding.cause = c->cause;
/* Fall through */
case Q931_CALL_PROCEEDING:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
break;
}
stop_t303(c->master_call);
ctrl->ev.proceeding.subcmds = &ctrl->subcmds;
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
if ((c->ourcallstate != Q931_CALL_STATE_CALL_INITIATED) &&
(c->ourcallstate != Q931_CALL_STATE_OVERLAP_SENDING) &&
(c->ourcallstate != Q931_CALL_STATE_CALL_DELIVERED) &&
(c->ourcallstate != Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING)) {
if (mh->msg == Q931_PROGRESS
&& c->ourcallstate == Q931_CALL_STATE_ACTIVE
&& ctrl->switchtype == PRI_SWITCH_QSIG) {
/*
* Q.SIG is odd to allow PROGRESS when in the Active state since
* the media path is already open. Ignore it since it doesn't
* convey anything very useful. Maybe they will stop doing it.
*
* See ECMA-143 Section 10.1.7.2.
*/
break;
}
q931_status(ctrl,c,PRI_CAUSE_WRONG_MESSAGE);
break;
}
ctrl->ev.proceeding.channel = q931_encode_channel(c);
if (mh->msg == Q931_CALL_PROCEEDING) {
ctrl->ev.e = PRI_EVENT_PROCEEDING;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING);
c->peercallstate = Q931_CALL_STATE_INCOMING_CALL_PROCEEDING;
}
ctrl->ev.proceeding.progress = c->progress;
ctrl->ev.proceeding.progressmask = c->progressmask;
ctrl->ev.proceeding.cref = c->cr;
ctrl->ev.proceeding.call = c->master_call;
for (cur = c->apdus; cur; cur = cur->next) {
if (!cur->sent && cur->message == Q931_FACILITY) {
q931_facility(ctrl, c);
break;
}
}
return Q931_RES_HAVEEVENT;
case Q931_CONNECT_ACKNOWLEDGE:
q931_display_subcmd(ctrl, c);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
switch (c->ourcallstate) {
default:
q931_status(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
return 0;
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_ACTIVE:
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_ACTIVE);
c->peercallstate = Q931_CALL_STATE_ACTIVE;
if (ctrl->manual_connect_ack) {
ctrl->ev.e = PRI_EVENT_CONNECT_ACK;
ctrl->ev.connect_ack.subcmds = &ctrl->subcmds;
ctrl->ev.connect_ack.channel = q931_encode_channel(c);
ctrl->ev.connect_ack.call = c->master_call;
return Q931_RES_HAVEEVENT;
}
break;
}
break;
case Q931_STATUS:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
break;
}
if (c->newcall) {
if (c->cr & 0x7fff)
q931_release_complete(ctrl,c,PRI_CAUSE_WRONG_CALL_STATE);
break;
}
/* Do nothing */
/* Also when the STATUS asks for the call of an unexisting reference send RELEASE_COMPLETE */
if ((ctrl->debug & PRI_DEBUG_Q931_ANOMALY) &&
(c->cause != PRI_CAUSE_INTERWORKING))
pri_error(ctrl, "Received unsolicited status: %s\n", pri_cause2str(c->cause));
if (
#if 0
/*
* Workaround for S-12 ver 7.3 - it responds to
* invalid/non-implemented IEs in SETUP with NULL call state.
*/
!c->sugcallstate && (c->ourcallstate != Q931_CALL_STATE_CALL_INITIATED)
#else
/*
* Remove "workaround" since it breaks certification testing. If
* we receive a STATUS message of call state NULL and we are not
* in the call state NULL we must clear resources and return to
* the call state to pass testing. See section 5.8.11 of Q.931.
*/
!c->sugcallstate
#endif
) {
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
/* Free resources */
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
if (c->outboundbroadcast && (c != q931_get_subcall_winner(c->master_call))) {
/* Complete clearing the disconnecting non-winning subcall. */
pri_hangup(ctrl, c, -1);
return 0;
}
/* Free resources */
if (c->alive) {
ctrl->ev.e = PRI_EVENT_HANGUP;
c->alive = 0;
} else if (c->sendhangupack) {
ctrl->ev.e = PRI_EVENT_HANGUP_ACK;
pri_hangup(ctrl, c, c->cause);
} else {
pri_hangup(ctrl, c, c->cause);
return 0;
}
return Q931_RES_HAVEEVENT;
}
break;
case Q931_RELEASE_COMPLETE:
q931_display_subcmd(ctrl, c);
c->hangupinitiated = 1;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
c->useruserinfo[0] = '\0';
if (c->cc.record && c->cc.record->signaling == c) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_SIGNALING_GONE);
}
if (c->outboundbroadcast && (c != q931_get_subcall_winner(c->master_call))) {
/* Complete clearing the disconnecting non-winning subcall. */
pri_hangup(ctrl, c, -1);
return 0;
}
/* Free resources */
if (c->alive) {
ctrl->ev.e = PRI_EVENT_HANGUP;
c->alive = 0;
} else if (c->sendhangupack) {
ctrl->ev.e = PRI_EVENT_HANGUP_ACK;
pri_hangup(ctrl, c, c->cause);
} else {
pri_hangup(ctrl, c, c->cause);
return 0;
}
return Q931_RES_HAVEEVENT;
case Q931_RELEASE:
q931_display_subcmd(ctrl, c);
c->hangupinitiated = 1;
if (mand_status != MAND_STATUS_OK) {
/* Force mandatory ie cause */
c->cause = mand_cause;
}
/*
* Don't send RELEASE_COMPLETE if they sent us RELEASE while we
* were waiting for RELEASE_COMPLETE from them, assume a NULL state.
*/
if (c->ourcallstate == Q931_CALL_STATE_RELEASE_REQUEST)
c->peercallstate = Q931_CALL_STATE_NULL;
else {
c->peercallstate = Q931_CALL_STATE_RELEASE_REQUEST;
}
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
ctrl->ev.e = PRI_EVENT_HANGUP;
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
c->useruserinfo[0] = '\0';
if (c->cc.record && c->cc.record->signaling == c) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_SIGNALING_GONE);
}
if (c->outboundbroadcast && (c != q931_get_subcall_winner(c->master_call))) {
/* Complete clearing the disconnecting non-winning subcall. */
pri_hangup(ctrl, c, -1);
return 0;
}
return Q931_RES_HAVEEVENT;
case Q931_DISCONNECT:
q931_display_subcmd(ctrl, c);
c->hangupinitiated = 1;
if (mand_status != MAND_STATUS_OK) {
/* Still let user call release */
c->cause = mand_cause;
}
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
/*
* Determine if there are any calls that can be proposed for
* a transfer of held call on disconnect.
*/
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
switch (c->ourcallstate) {
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
case Q931_CALL_STATE_ACTIVE:
if (c->master_call->hold_state == Q931_HOLD_STATE_CALL_HELD) {
/* Held call is being disconnected first. */
ctrl->ev.hangup.call_held = c->master_call;
ctrl->ev.hangup.call_active = q931_find_held_active_call(ctrl, c);
} else {
/* Active call is being disconnected first. */
if (q931_find_winning_call(c) == c) {
/*
* Only a normal call or the winning call of a broadcast SETUP
* can participate in a transfer of held call on disconnet.
*/
ctrl->ev.hangup.call_active = c->master_call;
ctrl->ev.hangup.call_held = q931_find_held_call(ctrl, c);
}
}
break;
default:
break;
}
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
if (ctrl->ev.hangup.call_held) {
pri_message(ctrl, "-- Found held call: %p cref:%d\n",
ctrl->ev.hangup.call_held, ctrl->ev.hangup.call_held->cr);
}
if (ctrl->ev.hangup.call_active) {
pri_message(ctrl, "-- Found active call: %p cref:%d\n",
ctrl->ev.hangup.call_active, ctrl->ev.hangup.call_active->cr);
}
if (ctrl->ev.hangup.call_held && ctrl->ev.hangup.call_active) {
pri_message(ctrl, "-- Transfer held call on disconnect possible.\n");
}
}
ourcallstate_orig = c->ourcallstate;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_DISCONNECT_INDICATION);
c->peercallstate = Q931_CALL_STATE_DISCONNECT_REQUEST;
c->sendhangupack = 1;
/* wait for a RELEASE so that sufficient time has passed
for the inband audio to be heard */
if (ctrl->acceptinbanddisconnect
&& (c->progressmask & PRI_PROG_INBAND_AVAILABLE)) {
switch (ourcallstate_orig) {
case Q931_CALL_STATE_CALL_INITIATED:
case Q931_CALL_STATE_OVERLAP_SENDING:
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
/*
* Open the media path if it isn't already open so
* the user can hear the inband audio.
*/
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Report the DISCONNECT as a PROGRESS instead.\n");
}
ctrl->ev.e = PRI_EVENT_PROGRESS;
ctrl->ev.proceeding.cause = c->cause;
ctrl->ev.proceeding.subcmds = &ctrl->subcmds;
ctrl->ev.proceeding.channel = q931_encode_channel(c);
ctrl->ev.proceeding.progress = c->progress;
ctrl->ev.proceeding.progressmask = c->progressmask;
ctrl->ev.proceeding.cref = c->cr;
ctrl->ev.proceeding.call = c->master_call;
return Q931_RES_HAVEEVENT;
default:
break;
}
/*
* Suppress reporting DISCONNECT to the upper layer. The
* media path should already be open and we cannot report
* a PROGRESS at this time anyway.
*/
break;
}
/* Return such an event */
ctrl->ev.e = PRI_EVENT_HANGUP_REQ;
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
c->useruserinfo[0] = '\0';
if (c->outboundbroadcast && (c != q931_get_subcall_winner(c->master_call))) {
/* Complete clearing the disconnecting non-winning subcall. */
pri_hangup(ctrl, c, -1);
return 0;
}
if (c->alive) {
switch (c->cause) {
case PRI_CAUSE_USER_BUSY:
case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
pri_cc_qsig_determine_available(ctrl, c);
break;
default:
break;
}
break;
default:
break;
}
return Q931_RES_HAVEEVENT;
} else {
pri_hangup(ctrl,c,c->cause);
}
break;
case Q931_RESTART_ACKNOWLEDGE:
q931_display_subcmd(ctrl, c);
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
ctrl->ev.e = PRI_EVENT_RESTART_ACK;
ctrl->ev.restartack.channel = q931_encode_channel(c);
if (c->restart_tx.t316_timer
/*
* Since the DS1 value can vary, only check the channel number.
* We're only supposed to have one RESTART request outstanding
* at a time anyway.
*/
&& (c->restart_tx.channel & 0xFF) == (ctrl->ev.restartack.channel & 0xFF)) {
/* This is the RESTART ACKNOWLEDGE we are expecting. */
stop_t316(c);
}
return Q931_RES_HAVEEVENT;
case Q931_INFORMATION:
/* XXX We're handling only INFORMATION messages that contain
overlap dialing received digit
+ the "Complete" msg which is basically an EOF on further digits
XXX */
q931_display_subcmd(ctrl, c);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
if (strlen(c->keypad_digits)) {
ctrl->ev.e = PRI_EVENT_KEYPAD_DIGIT;
ctrl->ev.digit.subcmds = &ctrl->subcmds;
ctrl->ev.digit.call = c->master_call;
ctrl->ev.digit.channel = q931_encode_channel(c);
libpri_copy_string(ctrl->ev.digit.digits, c->keypad_digits, sizeof(ctrl->ev.digit.digits));
return Q931_RES_HAVEEVENT;
}
ctrl->ev.e = PRI_EVENT_INFO_RECEIVED;
ctrl->ev.ring.subcmds = &ctrl->subcmds;
ctrl->ev.ring.call = c->master_call;
ctrl->ev.ring.channel = q931_encode_channel(c);
libpri_copy_string(ctrl->ev.ring.callednum, c->overlap_digits, sizeof(ctrl->ev.ring.callednum));
q931_party_id_copy_to_pri(&ctrl->ev.ring.calling, &c->remote_id);
/* for backwards compatibility, still need ctrl->ev.ring.callingsubaddr */
if (!c->remote_id.subaddress.type) { /* NSAP: Type = 0 */
libpri_copy_string(ctrl->ev.ring.callingsubaddr, (char *) c->remote_id.subaddress.data, sizeof(ctrl->ev.ring.callingsubaddr));
} else {
ctrl->ev.ring.callingsubaddr[0] = '\0';
}
ctrl->ev.ring.complete = c->complete; /* this covers IE 33 (Sending Complete) */
return Q931_RES_HAVEEVENT;
case Q931_STATUS_ENQUIRY:
q931_display_clear(c);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
} else
q931_status(ctrl,c, PRI_CAUSE_RESPONSE_TO_STATUS_ENQUIRY);
break;
case Q931_SETUP_ACKNOWLEDGE:
q931_display_subcmd(ctrl, c);
if (mand_status != MAND_STATUS_OK) {
q931_status(ctrl, c, mand_cause);
break;
}
stop_t303(c->master_call);
if (c->newcall) {
q931_release_complete(ctrl, c, newcall_rel_comp_cause(c));
break;
}
switch (c->ourcallstate) {
default:
q931_status(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
return 0;
case Q931_CALL_STATE_CALL_INITIATED:
break;
}
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_OVERLAP_SENDING);
c->peercallstate = Q931_CALL_STATE_OVERLAP_RECEIVING;
ctrl->ev.e = PRI_EVENT_SETUP_ACK;
ctrl->ev.setup_ack.subcmds = &ctrl->subcmds;
ctrl->ev.setup_ack.channel = q931_encode_channel(c);
ctrl->ev.setup_ack.call = c->master_call;
ctrl->ev.setup_ack.progressmask = c->progressmask;
for (cur = c->apdus; cur; cur = cur->next) {
if (!cur->sent && cur->message == Q931_FACILITY) {
q931_facility(ctrl, c);
break;
}
}
return Q931_RES_HAVEEVENT;
case Q931_NOTIFY:
res = 0;
changed = 0;
switch (c->notify) {
case PRI_NOTIFY_CALL_DIVERTING:
if (c->redirection_number.valid) {
c->redirecting.to.number = c->redirection_number;
if (c->redirecting.count < PRI_MAX_REDIRECTS) {
++c->redirecting.count;
}
switch (c->ourcallstate) {
case Q931_CALL_STATE_CALL_DELIVERED:
/* Call is deflecting after we have seen an ALERTING message */
c->redirecting.reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
break;
default:
/* Call is deflecting for call forwarding unconditional or busy reason. */
c->redirecting.reason = PRI_REDIR_UNKNOWN;
break;
}
/* Setup redirecting subcommand */
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
subcmd->cmd = PRI_SUBCMD_REDIRECTING;
q931_party_redirecting_copy_to_pri(&subcmd->u.redirecting,
&c->redirecting);
}
}
q931_display_subcmd(ctrl, c);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, c);
res = Q931_RES_HAVEEVENT;
}
break;
case PRI_NOTIFY_TRANSFER_ACTIVE:
if (q931_party_number_cmp(&c->remote_id.number, &c->redirection_number)) {
/* The remote party number information changed. */
c->remote_id.number = c->redirection_number;
changed = 1;
}
/* Fall through */
case PRI_NOTIFY_TRANSFER_ALERTING:
if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
struct q931_party_name name;
if (q931_display_name_get(c, &name)) {
if (q931_party_name_cmp(&c->remote_id.name, &name)) {
/* The remote party name information changed. */
c->remote_id.name = name;
changed = 1;
}
}
}
if (c->redirection_number.valid
&& q931_party_number_cmp(&c->remote_id.number, &c->redirection_number)) {
/* The remote party number information changed. */
c->remote_id.number = c->redirection_number;
changed = 1;
}
if (c->remote_id.subaddress.valid) {
/*
* Clear the subaddress as the remote party has been changed.
* Any new subaddress will arrive later.
*/
q931_party_subaddress_init(&c->remote_id.subaddress);
changed = 1;
}
if (changed) {
/* Setup connected line subcommand */
subcmd = q931_alloc_subcommand(ctrl);
if (subcmd) {
subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
q931_party_id_copy_to_pri(&subcmd->u.connected_line.id,
&c->remote_id);
}
}
q931_display_subcmd(ctrl, c);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, c);
res = Q931_RES_HAVEEVENT;
}
break;
default:
ctrl->ev.e = PRI_EVENT_NOTIFY;
ctrl->ev.notify.subcmds = &ctrl->subcmds;
ctrl->ev.notify.channel = q931_encode_channel(c);
ctrl->ev.notify.info = c->notify;
ctrl->ev.notify.call = c->master_call;
res = Q931_RES_HAVEEVENT;
break;
}
q931_display_subcmd(ctrl, c);
return res;
case Q931_HOLD:
q931_display_subcmd(ctrl, c);
res = 0;
if (!ctrl->hold_support) {
/*
* Blocking any calls from getting on HOLD effectively
* disables HOLD/RETRIEVE.
*/
q931_send_hold_rej_msg(ctrl, c, PRI_CAUSE_FACILITY_NOT_IMPLEMENTED);
break;
}
switch (c->ourcallstate) {
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
if (PTMP_MODE(ctrl)) {
/* HOLD request only allowed in these states if point-to-point mode. */
q931_send_hold_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
/* Fall through */
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
if (!q931_find_winning_call(c)) {
/*
* Only the winning call of a broadcast SETUP can do hold since the
* call must be answered first.
*/
q931_send_hold_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_HOLD_REQ:
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "HOLD collision\n");
}
if (ctrl->localtype == PRI_NETWORK) {
/* The network ignores HOLD request on a hold collision. */
break;
}
/* Fall through */
case Q931_HOLD_STATE_IDLE:
ctrl->ev.e = PRI_EVENT_HOLD;
ctrl->ev.hold.channel = q931_encode_channel(c);
ctrl->ev.hold.call = master_call;
ctrl->ev.hold.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_HOLD_IND);
break;
default:
q931_send_hold_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
break;
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
/* Ignore HOLD request in these states. */
break;
default:
q931_send_hold_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
return res;
case Q931_HOLD_ACKNOWLEDGE:
q931_display_subcmd(ctrl, c);
res = 0;
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_HOLD_REQ:
ctrl->ev.e = PRI_EVENT_HOLD_ACK;
ctrl->ev.hold_ack.channel = q931_encode_channel(c);
ctrl->ev.hold_ack.call = master_call;
ctrl->ev.hold_ack.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_CALL_HELD);
/* Call is now on hold so forget the channel. */
c->channelno = 0;/* No channel */
c->ds1no = 0;
c->ds1explicit = 0;
c->chanflags = 0;
/* Stop T-HOLD timer */
pri_schedule_del(ctrl, master_call->hold_timer);
master_call->hold_timer = 0;
break;
default:
/* Ignore response. Response is late or spurrious. */
break;
}
return res;
case Q931_HOLD_REJECT:
q931_display_subcmd(ctrl, c);
res = 0;
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_HOLD_REQ:
if (mand_status != MAND_STATUS_OK) {
/* Still, let hold rejection continue. */
c->cause = mand_cause;
}
ctrl->ev.e = PRI_EVENT_HOLD_REJ;
ctrl->ev.hold_rej.channel = q931_encode_channel(c);
ctrl->ev.hold_rej.call = master_call;
ctrl->ev.hold_rej.cause = c->cause;
ctrl->ev.hold_rej.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_IDLE);
/* Stop T-HOLD timer */
pri_schedule_del(ctrl, master_call->hold_timer);
master_call->hold_timer = 0;
break;
default:
/* Ignore response. Response is late or spurrious. */
break;
}
return res;
case Q931_RETRIEVE:
q931_display_subcmd(ctrl, c);
res = 0;
switch (c->ourcallstate) {
case Q931_CALL_STATE_CALL_RECEIVED:
case Q931_CALL_STATE_CONNECT_REQUEST:
case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
if (PTMP_MODE(ctrl)) {
/* RETRIEVE request only allowed in these states if point-to-point mode. */
q931_send_retrieve_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
/* Fall through */
case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
case Q931_CALL_STATE_CALL_DELIVERED:
case Q931_CALL_STATE_ACTIVE:
if (!q931_find_winning_call(c)) {
/*
* Only the winning call of a broadcast SETUP can do hold since the
* call must be answered first.
*/
q931_send_retrieve_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_RETRIEVE_REQ:
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "RETRIEVE collision\n");
}
if (ctrl->localtype == PRI_NETWORK) {
/* The network ignores RETRIEVE request on a retrieve collision. */
break;
}
/* Fall through */
case Q931_HOLD_STATE_CALL_HELD:
ctrl->ev.e = PRI_EVENT_RETRIEVE;
ctrl->ev.retrieve.channel = q931_encode_channel(c);
ctrl->ev.retrieve.call = master_call;
ctrl->ev.retrieve.flexible = !(c->chanflags & FLAG_EXCLUSIVE);
ctrl->ev.retrieve.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_RETRIEVE_IND);
break;
default:
q931_send_retrieve_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
break;
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
/* Ignore RETRIEVE request in these states. */
break;
default:
q931_send_retrieve_rej_msg(ctrl, c, PRI_CAUSE_WRONG_CALL_STATE);
break;
}
return res;
case Q931_RETRIEVE_ACKNOWLEDGE:
q931_display_subcmd(ctrl, c);
res = 0;
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_RETRIEVE_REQ:
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_IDLE);
/* Stop T-RETRIEVE timer */
pri_schedule_del(ctrl, master_call->hold_timer);
master_call->hold_timer = 0;
ctrl->ev.e = PRI_EVENT_RETRIEVE_ACK;
ctrl->ev.retrieve_ack.channel = q931_encode_channel(c);
ctrl->ev.retrieve_ack.call = master_call;
ctrl->ev.retrieve_ack.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
break;
default:
/* Ignore response. Response is late or spurrious. */
break;
}
return res;
case Q931_RETRIEVE_REJECT:
q931_display_subcmd(ctrl, c);
res = 0;
master_call = c->master_call;
switch (master_call->hold_state) {
case Q931_HOLD_STATE_CALL_HELD:
/* In this state likely because of a RETRIEVE collision. */
case Q931_HOLD_STATE_RETRIEVE_REQ:
UPDATE_HOLD_STATE(ctrl, master_call, Q931_HOLD_STATE_CALL_HELD);
/* Call is still on hold so forget the channel. */
c->channelno = 0;/* No channel */
c->ds1no = 0;
c->ds1explicit = 0;
c->chanflags = 0;
/* Stop T-RETRIEVE timer */
pri_schedule_del(ctrl, master_call->hold_timer);
master_call->hold_timer = 0;
if (mand_status != MAND_STATUS_OK) {
/* Still, let retrive rejection continue. */
c->cause = mand_cause;
}
ctrl->ev.e = PRI_EVENT_RETRIEVE_REJ;
ctrl->ev.retrieve_rej.channel = q931_encode_channel(c);
ctrl->ev.retrieve_rej.call = master_call;
ctrl->ev.retrieve_rej.cause = c->cause;
ctrl->ev.retrieve_rej.subcmds = &ctrl->subcmds;
res = Q931_RES_HAVEEVENT;
break;
default:
/* Ignore response. Response is late or spurrious. */
break;
}
return res;
case Q931_USER_INFORMATION:
case Q931_SEGMENT:
case Q931_CONGESTION_CONTROL:
case Q931_RESUME:
case Q931_RESUME_ACKNOWLEDGE:
case Q931_RESUME_REJECT:
case Q931_SUSPEND:
case Q931_SUSPEND_ACKNOWLEDGE:
case Q931_SUSPEND_REJECT:
pri_error(ctrl, "!! Not yet handling post-handle message type %s (0x%X)\n",
msg2str(mh->msg), mh->msg);
/* Fall through */
default:
pri_error(ctrl, "!! Don't know how to post-handle message type %s (0x%X)\n",
msg2str(mh->msg), mh->msg);
q931_display_clear(c);
q931_status(ctrl,c, PRI_CAUSE_MESSAGE_TYPE_NONEXIST);
if (c->newcall) {
pri_destroycall(ctrl, c);
}
return -1;
}
return 0;
}
/* Clear a call, although we did not receive any hangup notification. */
static int pri_internal_clear(struct q931_call *c)
{
struct pri *ctrl = c->pri;
int res;
pri_schedule_del(ctrl, c->retranstimer);
c->retranstimer = 0;
c->useruserinfo[0] = '\0';
//c->cause = -1;
c->causecode = -1;
c->causeloc = -1;
c->sugcallstate = Q931_CALL_STATE_NOT_SET;
c->aoc_units = -1;
if (c->master_call->outboundbroadcast
&& c == q931_find_winning_call(c)) {
/* Pass the hangup cause to the master_call. */
c->master_call->cause = c->cause;
}
q931_clr_subcommands(ctrl);
ctrl->ev.hangup.subcmds = &ctrl->subcmds;
ctrl->ev.hangup.channel = q931_encode_channel(c);
ctrl->ev.hangup.cause = c->cause;
ctrl->ev.hangup.cref = c->cr;
ctrl->ev.hangup.call = c->master_call;
ctrl->ev.hangup.aoc_units = c->aoc_units;
ctrl->ev.hangup.call_held = NULL;
ctrl->ev.hangup.call_active = NULL;
libpri_copy_string(ctrl->ev.hangup.useruserinfo, c->useruserinfo, sizeof(ctrl->ev.hangup.useruserinfo));
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, DBGHEAD "alive %d, hangupack %d\n", DBGINFO, c->alive,
c->sendhangupack);
}
if (c->cc.record) {
if (c->cc.record->signaling == c) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_SIGNALING_GONE);
} else if (c->cc.record->original_call == c) {
pri_cc_event(ctrl, c, c->cc.record, CC_EVENT_INTERNAL_CLEARING);
}
}
/* Free resources */
if (c->alive) {
c->alive = 0;
ctrl->ev.e = PRI_EVENT_HANGUP;
res = Q931_RES_HAVEEVENT;
} else if (c->sendhangupack) {
pri_hangup(ctrl, c, c->cause);
ctrl->ev.e = PRI_EVENT_HANGUP_ACK;
res = Q931_RES_HAVEEVENT;
} else {
pri_hangup(ctrl, c, c->cause);
if (ctrl->subcmds.counter_subcmd) {
q931_fill_facility_event(ctrl, ctrl->link.dummy_call);
res = Q931_RES_HAVEEVENT;
} else {
res = 0;
}
}
return res;
}
/* Handle T309 timeout for an active call. */
static void pri_dl_down_timeout(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "T309 timed out waiting for data link re-establishment\n");
}
c->retranstimer = 0;
c->cause = PRI_CAUSE_DESTINATION_OUT_OF_ORDER;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
if (pri_internal_clear(c) == Q931_RES_HAVEEVENT) {
ctrl->schedev = 1;
}
}
/* Handle Layer 2 down event for a non active call. */
static void pri_dl_down_cancelcall(void *data)
{
struct q931_call *c = data;
struct pri *ctrl = c->pri;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Cancel call after data link failure\n");
}
c->retranstimer = 0;
c->cause = PRI_CAUSE_DESTINATION_OUT_OF_ORDER;
UPDATE_OURCALLSTATE(ctrl, c, Q931_CALL_STATE_NULL);
c->peercallstate = Q931_CALL_STATE_NULL;
if (pri_internal_clear(c) == Q931_RES_HAVEEVENT) {
ctrl->schedev = 1;
}
}
/*!
* \internal
* \brief Convert the DL event to a string.
*
* \param event Data-link event to convert to a string.
*
* \return DL event string
*/
static const char *q931_dl_event2str(enum Q931_DL_EVENT event)
{
const char *str;
str = "Unknown";
switch (event) {
case Q931_DL_EVENT_NONE:
str = "Q931_DL_EVENT_NONE";
break;
case Q931_DL_EVENT_DL_ESTABLISH_IND:
str = "Q931_DL_EVENT_DL_ESTABLISH_IND";
break;
case Q931_DL_EVENT_DL_ESTABLISH_CONFIRM:
str = "Q931_DL_EVENT_DL_ESTABLISH_CONFIRM";
break;
case Q931_DL_EVENT_DL_RELEASE_IND:
str = "Q931_DL_EVENT_DL_RELEASE_IND";
break;
case Q931_DL_EVENT_DL_RELEASE_CONFIRM:
str = "Q931_DL_EVENT_DL_RELEASE_CONFIRM";
break;
case Q931_DL_EVENT_TEI_REMOVAL:
str = "Q931_DL_EVENT_TEI_REMOVAL";
break;
}
return str;
}
/*!
* \brief Receive a DL event from layer 2.
*
* \param link Q.921 link event occurred on.
* \param event Data-link event reporting.
*
* \return Nothing
*/
void q931_dl_event(struct q921_link *link, enum Q931_DL_EVENT event)
{
struct q931_call *cur;
struct q931_call *cur_next;
struct q931_call *call;
struct pri *ctrl;
int idx;
if (!link) {
return;
}
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "TEI=%d DL event: %s(%d)\n", link->tei,
q931_dl_event2str(event), event);
}
switch (event) {
case Q931_DL_EVENT_TEI_REMOVAL:
if (!BRI_NT_PTMP(ctrl)) {
/* Only NT PTMP has anything to worry about when the TEI is removed. */
break;
}
/*
* For NT PTMP, this deviation from the specifications is needed
* because we have no way to re-associate any T309 calls on the
* removed TEI.
*/
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (cur->outboundbroadcast) {
/* Does this master call have a subcall on the link that went down? */
call = NULL;
for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
if (cur->subcalls[idx] && cur->subcalls[idx]->link == link) {
/* This subcall is on the link that went down. */
call = cur->subcalls[idx];
break;
}
}
if (!call) {
/* No subcall is on the link that went down. */
continue;
}
} else if (cur->link != link) {
/* This call is not on the link that went down. */
continue;
} else {
call = cur;
}
if (!(cur->cr & ~Q931_CALL_REFERENCE_FLAG)) {
/* Simply destroy the global call reference call record. */
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "TEI=%d Destroying global call record\n",
link->tei);
}
q931_destroycall(ctrl, call);
continue;
}
/*
* NOTE: We are gambling that no T309 timer's have had a chance
* to expire. They should not expire since we are either called
* immediately after the Q931_DL_EVENT_DL_RELEASE_xxx or after a
* timeout of 0.
*/
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Cancel call cref=%d on channel %d in state %d (%s)\n",
call->cr, call->channelno, call->ourcallstate,
q931_call_state_str(call->ourcallstate));
}
call->link = NULL;
pri_schedule_del(ctrl, call->retranstimer);
call->retranstimer = pri_schedule_event(ctrl, 0, pri_dl_down_cancelcall,
call);
}
break;
case Q931_DL_EVENT_DL_RELEASE_IND:
case Q931_DL_EVENT_DL_RELEASE_CONFIRM:
for (cur = *ctrl->callpool; cur; cur = cur_next) {
/* The master call could get destroyed if the last subcall dies. */
cur_next = cur->next;
if (!(cur->cr & ~Q931_CALL_REFERENCE_FLAG)) {
/* Don't do anything on the global call reference call record. */
continue;
}
if (cur->outboundbroadcast) {
/* Does this master call have a subcall on the link that went down? */
call = NULL;
for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
if (cur->subcalls[idx] && cur->subcalls[idx]->link == link) {
/* This subcall is on the link that went down. */
call = cur->subcalls[idx];
break;
}
}
if (!call) {
/* No subcall is on the link that went down. */
continue;
}
} else if (cur->link != link) {
/* This call is not on the link that went down. */
continue;
} else {
call = cur;
}
switch (call->ourcallstate) {
case Q931_CALL_STATE_ACTIVE:
/* NOTE: Only a winning subcall can get to the active state. */
if (ctrl->nfas) {
/*
* The upper layer should handle T309 for NFAS since the calls
* could be maintained by a backup D channel if the B channel
* for the call did not go into alarm.
*/
break;
}
/*
* For a call in Active state, activate T309 only if there is
* no timer already running.
*
* NOTE: cur != call when we have a winning subcall.
*/
if (!cur->retranstimer || !call->retranstimer) {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Start T309 for call cref=%d on channel %d\n",
call->cr, call->channelno);
}
call->retranstimer = pri_schedule_event(ctrl,
ctrl->timers[PRI_TIMER_T309], pri_dl_down_timeout, call);
}
break;
case Q931_CALL_STATE_NULL:
break;
default:
/*
* For a call that is not in Active state, schedule internal
* clearing of the call 'ASAP' (delay 0).
*
* NOTE: We are killing NFAS calls that are not connected yet
* because there are likely messages in flight when this link
* went down that could leave the call in an unknown/stuck state.
*/
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl,
"Cancel call cref=%d on channel %d in state %d (%s)\n",
call->cr, call->channelno, call->ourcallstate,
q931_call_state_str(call->ourcallstate));
}
if (cur->outboundbroadcast) {
/* Simply destroy non-winning subcalls. */
q931_destroycall(ctrl, call);
continue;
}
pri_schedule_del(ctrl, call->retranstimer);
call->retranstimer = pri_schedule_event(ctrl, 0, pri_dl_down_cancelcall,
call);
break;
}
}
break;
case Q931_DL_EVENT_DL_ESTABLISH_IND:
case Q931_DL_EVENT_DL_ESTABLISH_CONFIRM:
for (cur = *ctrl->callpool; cur; cur = cur->next) {
if (!(cur->cr & ~Q931_CALL_REFERENCE_FLAG)) {
/* Don't do anything on the global call reference call record. */
continue;
}
if (cur->outboundbroadcast) {
/* Does this master call have a subcall on the link that came up? */
call = NULL;
for (idx = 0; idx < ARRAY_LEN(cur->subcalls); ++idx) {
if (cur->subcalls[idx] && cur->subcalls[idx]->link == link) {
/* This subcall is on the link that came up. */
call = cur->subcalls[idx];
break;
}
}
if (!call) {
/* No subcall is on the link that came up. */
continue;
}
} else if (cur->link != link) {
/* This call is not on the link that came up. */
continue;
} else {
call = cur;
}
switch (call->ourcallstate) {
case Q931_CALL_STATE_ACTIVE:
/* NOTE: Only a winning subcall can get to the active state. */
if (pri_schedule_check(ctrl, call->retranstimer, pri_dl_down_timeout, call)) {
if (ctrl->debug & PRI_DEBUG_Q931_STATE) {
pri_message(ctrl, "Stop T309 for call cref=%d on channel %d\n",
call->cr, call->channelno);
}
pri_schedule_del(ctrl, call->retranstimer);
call->retranstimer = 0;
}
q931_status(ctrl, call, PRI_CAUSE_NORMAL_UNSPECIFIED);
break;
case Q931_CALL_STATE_NULL:
case Q931_CALL_STATE_DISCONNECT_REQUEST:
case Q931_CALL_STATE_DISCONNECT_INDICATION:
case Q931_CALL_STATE_RELEASE_REQUEST:
break;
default:
if (event == Q931_DL_EVENT_DL_ESTABLISH_CONFIRM) {
/*
* Lets not send a STATUS message for this call as we
* requested the link to be established as a likely
* result of this call.
*/
break;
}
/*
* The STATUS message sent here is not required by Q.931,
* but it may help anyway.
* This looks like a new call started while the link was down.
*/
q931_status(ctrl, call, PRI_CAUSE_NORMAL_UNSPECIFIED);
break;
}
}
break;
default:
pri_message(ctrl, DBGHEAD "unexpected event %d.\n", DBGINFO, event);
break;
}
}
int q931_call_getcrv(struct pri *ctrl, q931_call *call, int *callmode)
{
if (callmode)
*callmode = call->cr & 0x7;
return ((call->cr & 0x7fff) >> 3);
}
int q931_call_setcrv(struct pri *ctrl, q931_call *call, int crv, int callmode)
{
/* Do not allow changing the dummy call reference */
if (!q931_is_dummy_call(call)) {
call->cr = (crv << 3) & 0x7fff;
call->cr |= (callmode & 0x7);
}
return 0;
}
|