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
|
/*
* $Id$
*
* Copyright (C) 2013 Robert Boisvert
*
* This file is part of the mohqueue module for sip-router, a free SIP server.
*
* The mohqueue module is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* The mohqueue module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdarg.h>
#include "mohq.h"
#include "mohq_db.h"
#include "mohq_funcs.h"
/**********
* definitions
**********/
#define ALLOWHDR "Allow: INVITE, ACK, BYE, CANCEL, NOTIFY, PRACK"
#define CLENHDR "Content-Length"
#define SIPEOL "\r\n"
#define USRAGNT "Kamailio MOH Queue v1.0"
/**********
* local constants
**********/
str p100rel [1] = {STR_STATIC_INIT ("100rel")};
str pallq [1] = {STR_STATIC_INIT ("*")};
str paudio [1] = {STR_STATIC_INIT ("audio")};
str pbye [1] = {STR_STATIC_INIT ("BYE")};
str pinvite [1] = {STR_STATIC_INIT ("INVITE")};
str pmi_nolock [1] = {STR_STATIC_INIT ("Unable to lock queue")};
str pmi_noqueue [1] = {STR_STATIC_INIT ("No matching queue name found")};
str prefer [1] = {STR_STATIC_INIT ("REFER")};
str presp_noaccept [1] = {STR_STATIC_INIT ("Not Acceptable Here")};
str presp_noallow [1] = {STR_STATIC_INIT ("Method Not Allowed")};
str presp_nocall [1] = {STR_STATIC_INIT ("Call/Transaction Does Not Exist")};
str presp_ok [1] = {STR_STATIC_INIT ("OK")};
str presp_reqpend [1] = {STR_STATIC_INIT ("Request Pending")};
str presp_reqterm [1] = {STR_STATIC_INIT ("Request Terminated")};
str presp_ring [1] = {STR_STATIC_INIT ("Ringing")};
str psipfrag [1] = {STR_STATIC_INIT ("message/sipfrag")};
str presp_srverr [1] = {STR_STATIC_INIT ("Server Internal Error")};
str presp_unsupp [1] = {STR_STATIC_INIT ("Unsupported Media Type")};
rtpmap prtpmap [] =
{
{9, "G722/8000"},
{0, "PCMU/8000"},
{8, "PCMA/8000"},
{18, "G729/8000"},
{3, "GSM/8000"},
{4, "G723/8000"},
{15, "G728/8000"},
{5, "DVI4/8000"},
{7, "LPC/8000"},
{12, "QCELP/8000"},
{13, "CN/8000"},
{16, "DVI4/11025"},
{6, "DVI4/16000"},
{17, "DVI4/22050"},
{10, "L16/44100"},
{11, "L16/44100"},
{14, "MPA/90000"},
{0, 0}
};
rtpmap *pmohfiles [30]; // element count should be equal or greater than prtpmap
str pallowhdr [1] = { STR_STATIC_INIT (ALLOWHDR SIPEOL) };
char pbyemsg [] =
{
"%s"
"Max-Forwards: 70" SIPEOL
"Contact: <%s>" SIPEOL
"User-Agent: " USRAGNT SIPEOL
};
str pextrahdr [1] =
{
STR_STATIC_INIT (
ALLOWHDR SIPEOL
"Supported: 100rel" SIPEOL
"Accept-Language: en" SIPEOL
"Content-Type: application/sdp" SIPEOL
"User-Agent: " USRAGNT SIPEOL
)
};
char pinvitesdp [] =
{
"v=0" SIPEOL
"o=- %d %d IN %s" SIPEOL
"s=" USRAGNT SIPEOL
"c=IN %s" SIPEOL
"t=0 0" SIPEOL
"a=send%s" SIPEOL
"m=audio %d RTP/AVP "
};
char prefermsg [] =
{
"%s"
"Max-Forwards: 70" SIPEOL
"Refer-To: <%s>" SIPEOL
"Referred-By: <%.*s>" SIPEOL
"User-Agent: " USRAGNT SIPEOL
};
char preinvitemsg [] =
{
"%s"
"Max-Forwards: 70" SIPEOL
"Contact: <%s>" SIPEOL
ALLOWHDR SIPEOL
"Supported: 100rel" SIPEOL
"User-Agent: " USRAGNT SIPEOL
"Accept-Language: en" SIPEOL
"Content-Type: application/sdp" SIPEOL
};
char prtpsdp [] =
{
"v=0" SIPEOL
// IP address and audio port faked since they will be replaced
"o=- 1 1 IN IP4 1.1.1.1" SIPEOL
"s=" USRAGNT SIPEOL
"c=IN IP4 1.1.1.1" SIPEOL
"t=0 0" SIPEOL
"a=sendrecv" SIPEOL
"m=audio 1 RTP/AVP"
};
/**********
* local function declarations
**********/
void delete_call (call_lst *);
void drop_call (sip_msg_t *, call_lst *);
int find_call (sip_msg_t *, call_lst **);
dlg_t *form_dialog (call_lst *, struct to_body *);
int form_rtp_SDP (str *, call_lst *, char *);
static void invite_cb (struct cell *, int, struct tmcb_params *);
int refer_call (call_lst *, mohq_lock *);
static void refer_cb (struct cell *, int, struct tmcb_params *);
int send_prov_rsp (sip_msg_t *, call_lst *);
int send_rtp_answer (sip_msg_t *, call_lst *);
int search_hdr_ext (struct hdr_field *, str *);
int start_stream (sip_msg_t *, call_lst *, int);
/**********
* local functions
**********/
/**********
* Process ACK Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int ack_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* part of INVITE?
**********/
char *pfncname = "ack_msg: ";
struct cell *ptrans;
tm_api_t *ptm = pmod_data->ptm;
if (pcall->call_state != CLSTA_INVITED)
{
/**********
* ignore if from rejected re-INVITE
**********/
if (pcall->call_state != CLSTA_INQUEUE)
{ LM_ERR ("%sUnexpected ACK (%s)!", pfncname, pcall->call_from); }
else
{
mohq_debug (pcall->pmohq, "%sACK from refused re-INVITE (%s)!",
pfncname, pcall->call_from);
}
return 1;
}
/**********
* o release INVITE transaction
* o save SDP address info
* o put in queue
**********/
if (ptm->t_lookup_ident (&ptrans, pcall->call_hash, pcall->call_label) < 0)
{
LM_ERR ("%sINVITE transaction missing for call (%s)!",
pfncname, pcall->call_from);
return 1;
}
else
{
if (ptm->t_release (pcall->call_pmsg) < 0)
{
LM_ERR ("%sRelease transaction failed for call (%s)!",
pfncname, pcall->call_from);
return 1;
}
}
pcall->call_hash = pcall->call_label = 0;
sprintf (pcall->call_addr, "%s %s",
pmsg->rcv.dst_ip.af == AF_INET ? "IP4" : "IP6",
ip_addr2a (&pmsg->rcv.dst_ip));
pcall->call_state = CLSTA_INQUEUE;
update_call_rec (pcall);
pcall->call_cseq = 1;
mohq_debug (pcall->pmohq,
"%sACK received for call (%s); placed in queue (%s)",
pfncname, pcall->call_from, pcall->pmohq->mohq_name);
return 1;
}
/**********
* BYE Callback
*
* INPUT:
* Arg (1) = cell pointer
* Arg (2) = callback type
* Arg (3) = callback parms
* OUTPUT: none
**********/
static void bye_cb
(struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
{
/**********
* o error means must have hung after REFER
* o delete the call
**********/
char *pfncname = "bye_cb: ";
call_lst *pcall = (call_lst *)*pcbp->param;
if (ntype == TMCB_ON_FAILURE)
{
LM_ERR ("%sCall (%s) did not respond to BYE", pfncname,
pcall->call_from);
}
else
{
int nreply = pcbp->code;
if ((nreply / 100) != 2)
{
LM_ERR ("%sCall (%s) BYE error (%d)", pfncname,
pcall->call_from, nreply);
}
else
{
mohq_debug (pcall->pmohq, "%sCall (%s) BYE reply=%d", pfncname,
pcall->call_from, nreply);
}
}
delete_call (pcall);
return;
}
/**********
* Process BYE Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int bye_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* o send OK
* o teardown call
**********/
char *pfncname = "bye_msg: ";
if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
{
LM_ERR ("%sUnable to create reply to call (%s)", pfncname,
pcall->call_from);
return 1;
}
if (pcall->call_state >= CLSTA_INQUEUE)
{ drop_call (pmsg, pcall); }
else
{
LM_ERR ("%sEnding call (%s) before placed in queue!",
pfncname, pcall->call_from);
delete_call (pcall);
}
return 1;
}
/**********
* Process CANCEL Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int cancel_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* still in INVITE dialog?
**********/
char *pfncname = "cancel_msg: ";
if (pcall->call_state < CLSTA_INQUEUE)
{
pcall->call_state = CLSTA_CANCEL;
mohq_debug (pcall->pmohq, "%sCANCELed call (%s)",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 487, presp_reqterm) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
}
else
{
LM_ERR ("%sUnable to CANCEL because accepted INVITE for call (%s)!",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
}
return 1;
}
/**********
* Close the Call
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: none
**********/
void close_call (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* o destroy proxy connection
* o create dialog
**********/
char *pfncname = "close_call: ";
int bsent = 0;
char *phdr = 0;
if (pmsg != FAKED_REPLY)
{
mohq_debug (pcall->pmohq, "%sDestroying RTP link for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->fn_rtp_destroy (pmsg, 0, 0) != 1)
{
LM_ERR ("%srtpproxy_destroy refused for call (%s)!",
pfncname, pcall->call_from);
}
}
struct to_body ptob [2];
dlg_t *pdlg = form_dialog (pcall, ptob);
if (!pdlg)
{ goto bye_err; }
pdlg->state = DLG_CONFIRMED;
/**********
* form BYE header
* o calculate size
* o create buffer
**********/
tm_api_t *ptm = pmod_data->ptm;
char *pquri = pcall->pmohq->mohq_uri;
int npos1 = sizeof (pbyemsg) // BYE template
+ strlen (pcall->call_via) // Via
+ strlen (pquri); // Contact
phdr = pkg_malloc (npos1);
if (!phdr)
{
LM_ERR ("%sNo more memory!", pfncname);
goto bye_err;
}
sprintf (phdr, pbyemsg,
pcall->call_via, // Via
pquri); // Contact
str phdrs [1];
phdrs->s = phdr;
phdrs->len = strlen (phdr);
/**********
* send BYE request
**********/
uac_req_t puac [1];
set_uac_req (puac, pbye, phdrs, 0, pdlg,
TMCB_LOCAL_COMPLETED | TMCB_ON_FAILURE, bye_cb, pcall);
pcall->call_state = CLSTA_BYE;
if (ptm->t_request_within (puac) < 0)
{
LM_ERR ("%sUnable to create BYE request for call (%s)!",
pfncname, pcall->call_from);
goto bye_err;
}
mohq_debug (pcall->pmohq, "%sSent BYE request for call (%s)",
pfncname, pcall->call_from);
bsent = 1;
/**********
* o free memory
* o delete call
**********/
bye_err:
if (pdlg)
{ pkg_free (pdlg); }
if (phdr)
{ pkg_free (phdr); }
if (!bsent)
{ delete_call (pcall); }
return;
}
/**********
* Create New Call Record
*
* INPUT:
* Arg (1) = queue index
* Arg (2) = SIP message pointer
* OUTPUT: call index; -1 if unable to create
**********/
int create_call (int mohq_idx, sip_msg_t *pmsg)
{
/**********
* o lock calls
* o already in use?
* o find inactive slot
**********/
char *pfncname = "create_call: ";
if (!mohq_lock_set (pmod_data->pcall_lock, 1, 2000))
{
LM_ERR ("%sUnable to lock calls!", pfncname);
return -1;
}
call_lst *pcall;
int ncall_idx = find_call (pmsg, &pcall);
if (pcall)
{
mohq_lock_release (pmod_data->pcall_lock);
LM_ERR ("%sCall already in use (%s)!", pfncname, pcall->call_from);
return -1;
}
for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
{
if (!pmod_data->pcall_lst [ncall_idx].call_active)
{ break; }
}
if (ncall_idx == pmod_data->call_cnt)
{
mohq_lock_release (pmod_data->pcall_lock);
LM_ERR ("%sNo call slots available!", pfncname);
return -1;
}
/**********
* add values to new entry
**********/
pcall = &pmod_data->pcall_lst [ncall_idx];
pcall->call_active = 1;
pcall->pmohq = &pmod_data->pmohq_lst [mohq_idx];
pcall->call_state = 0;
str *pstr = &pmsg->callid->body;
strncpy (pcall->call_id, pstr->s, pstr->len);
pcall->call_id [pstr->len] = '\0';
pstr = &pmsg->from->body;
strncpy (pcall->call_from, pstr->s, pstr->len);
pcall->call_from [pstr->len] = '\0';
*pcall->call_tag = '\0';
if (!pmsg->contact)
{ *pcall->call_contact = '\0'; }
else
{
pstr = &pmsg->contact->body;
strncpy (pcall->call_contact, pstr->s, pstr->len);
pcall->call_contact [pstr->len] = '\0';
}
/**********
* extract Via headers
**********/
hdr_field_t *phdr = pmsg->h_via1;
if (phdr)
{
int npos1 = 0;
while ((phdr = next_sibling_hdr (phdr)))
{
struct via_body *pvia;
char *pviabuf;
int bovrflow = 0;
int npos2;
int nvia_max = sizeof (pcall->call_via);
for (pvia = (struct via_body *)phdr->parsed; pvia; pvia = pvia->next)
{
/**********
* o skip trailing whitespace
* o check if overflow
**********/
npos2 = pvia->bsize;
pviabuf = pvia->name.s;
while (npos2)
{
--npos2;
if (pviabuf [npos2] == ' ' || pviabuf [npos2] == '\r'
|| pviabuf [npos2] == '\n' || pviabuf [npos2] == '\t' || pviabuf [npos2] == ',')
{ continue; }
break;
}
if ((npos2 + npos1 + 7) >= nvia_max)
{
LM_WARN ("%sVia buffer overflowed!", pfncname);
bovrflow = 1;
break;
}
/**********
* copy via
**********/
strcpy (&pcall->call_via [npos1], "Via: ");
npos1 += 5;
strncpy (&pcall->call_via [npos1], pviabuf, npos2);
npos1 += npos2;
strcpy (&pcall->call_via [npos1], SIPEOL);
npos1 += 2;
}
if (bovrflow)
{ break; }
}
}
/**********
* o release call lock
* o update DB
* o lock MOH queue
**********/
pcall->call_state = CLSTA_ENTER;
mohq_lock_release (pmod_data->pcall_lock);
add_call_rec (ncall_idx);
mohq_lock_set (pmod_data->pmohq_lock, 0, 0);
mohq_debug (pcall->pmohq, "%sAdded call (%s) to queue (%s)",
pfncname, pcall->call_from, pcall->pmohq->mohq_name);
return ncall_idx;
}
/**********
* Delete Call
*
* INPUT:
* Arg (1) = call pointer
* OUTPUT: none
**********/
void delete_call (call_lst *pcall)
{
/**********
* release transaction
**********/
char *pfncname = "delete_call: ";
struct cell *ptrans;
tm_api_t *ptm = pmod_data->ptm;
if (pcall->call_hash || pcall->call_label)
{
if (ptm->t_lookup_ident (&ptrans, pcall->call_hash, pcall->call_label) < 0)
{
LM_ERR ("%sLookup transaction failed for call (%s)!", pfncname,
pcall->call_from);
}
else
{
if (ptm->t_release (pcall->call_pmsg) < 0)
{
LM_ERR ("%sRelease transaction failed for call (%s)!",
pfncname, pcall->call_from);
}
}
pcall->call_hash = pcall->call_label = 0;
}
/**********
* o update DB
* o inactivate slot
* o release MOH queue
**********/
mohq_debug (pcall->pmohq, "delete_call: Deleting call (%s) from queue (%s)",
pcall->call_from, pcall->pmohq->mohq_name);
delete_call_rec (pcall);
pcall->call_active = 0;
mohq_lock_release (pmod_data->pmohq_lock);
return;
}
/**********
* Deny Method
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: none
**********/
void deny_method (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* RFC 3261 section 8.2.1
* o get transaction
* o respond with 405 and Allow header
**********/
char *pfncname = "deny_method: ";
tm_api_t *ptm = pmod_data->ptm;
if (ptm->t_newtran (pmsg) < 0)
{
LM_ERR ("%sUnable to create new transaction!", pfncname);
if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
{
LM_ERR ("%sUnable to create reply to %.*s!", pfncname,
STR_FMT (&REQ_LINE (pmsg).method));
}
return;
}
if (!add_lump_rpl2 (pmsg, pallowhdr->s, pallowhdr->len, LUMP_RPL_HDR))
{ LM_ERR ("%sUnable to add Allow header!", pfncname); }
LM_ERR ("%sRefused %.*s for call (%s)!", pfncname,
STR_FMT (&REQ_LINE (pmsg).method), pcall->call_from);
if (ptm->t_reply (pmsg, 405, presp_noallow->s) < 0)
{
LM_ERR ("%sUnable to create reply to %.*s!", pfncname,
STR_FMT (&REQ_LINE (pmsg).method));
}
return;
}
/**********
* Drop the Call
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: none
**********/
void drop_call (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* o destroy proxy connection
* o delete call
**********/
char *pfncname = "drop_call: ";
if (pmsg != FAKED_REPLY)
{
mohq_debug (pcall->pmohq, "%sDestroying RTP link for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->fn_rtp_destroy (pmsg, 0, 0) != 1)
{
LM_ERR ("%srtpproxy_destroy refused for call (%s)!",
pfncname, pcall->call_from);
}
}
delete_call (pcall);
return;
}
/**********
* Find Call
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = pointer to call pointer
* OUTPUT: queue index; -1 if unable to find
**********/
int find_call (sip_msg_t *pmsg, call_lst **ppcall)
{
/**********
* o find current RURI
* o strip off parms or headers
* o search MOH queue
**********/
str *pruri =
pmsg->new_uri.s ? &pmsg->new_uri : &pmsg->first_line.u.request.uri;
int nidx;
str pstr [1];
pstr->s = pruri->s;
pstr->len = pruri->len;
for (nidx = 0; nidx < pruri->len; nidx++)
{
if (pstr->s [nidx] == ';' || pstr->s [nidx] == '?')
{
pstr->len = nidx;
break;
}
}
mohq_lst *pqlst = pmod_data->pmohq_lst;
int nqidx;
for (nqidx = 0; nqidx < pmod_data->mohq_cnt; nqidx++)
{
str pmohstr [1];
pmohstr->s = pqlst [nqidx].mohq_uri;
pmohstr->len = strlen (pmohstr->s);
if (STR_EQ (*pmohstr, *pstr))
{ break; }
}
*ppcall = 0;
if (nqidx == pmod_data->mohq_cnt)
{ return -1;}
/**********
* o get to tag
* o get callID
* o ignore to tag if CANCEL on first INVITE
* o search call queue
**********/
str *ptotag = &(get_to (pmsg)->tag_value);
if (!ptotag->len)
{ ptotag = 0; }
if (!pmsg->callid)
{ return -1; }
str *pcallid = &pmsg->callid->body;
if (!pcallid)
{ return -1; }
for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
{
/**********
* o call active?
* o call timed out on ACK?
* o callID matches?
* o to tag matches?
* o return call pointer
**********/
call_lst *pcall = &pmod_data->pcall_lst [nidx];
if (!pcall->call_active)
{ continue; }
if (pcall->call_time && (pcall->call_state < CLSTA_INQUEUE))
{
if ((pcall->call_time + 32) < time (0))
{
LM_ERR ("find_call: No ACK response for call (%s)", pcall->call_from);
delete_call (pcall);
continue;
}
}
str tmpstr [1];
tmpstr->s = pcall->call_id;
tmpstr->len = strlen (tmpstr->s);
if (!STR_EQ (*tmpstr, *pcallid))
{ continue; }
if (ptotag)
{
tmpstr->s = pcall->call_tag;
tmpstr->len = strlen (tmpstr->s);
if (!STR_EQ (*tmpstr, *ptotag))
{ continue; }
}
*ppcall = pcall;
return nqidx;
}
/**********
* first INVITE?
**********/
if (pmsg->REQ_METHOD == METHOD_INVITE)
{ return 0; }
return -1;
}
/**********
* Find Queue
*
* INPUT:
* Arg (1) = queue name str pointer
* OUTPUT: queue index; -1 if unable to find
**********/
int find_queue (str *pqname)
{
char *pfncname = "find_queue: ";
int nidx;
str tmpstr;
if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 500))
{
LM_ERR ("%sUnable to lock queues!", pfncname);
return -1;
}
for (nidx = 0; nidx < pmod_data->mohq_cnt; nidx++)
{
tmpstr.s = pmod_data->pmohq_lst [nidx].mohq_name;
tmpstr.len = strlen (tmpstr.s);
if (STR_EQ (tmpstr, *pqname))
{ break; }
}
if (nidx == pmod_data->mohq_cnt)
{
LM_ERR ("%sUnable to find queue (%.*s)!", pfncname, STR_FMT (pqname));
nidx = -1;
}
mohq_lock_release (pmod_data->pmohq_lock);
return nidx;
}
/**********
* Find Referred Call
*
* INPUT:
* Arg (1) = referred-by value
* OUTPUT: call index; -1 if unable to find
**********/
int find_referred_call (str *pvalue)
{
/**********
* get URI
**********/
char *pfncname = "find_referred_call: ";
struct to_body pref [1];
parse_to (pvalue->s, &pvalue->s [pvalue->len + 1], pref);
if (pref->error != PARSE_OK)
{
// should never happen
LM_ERR ("%sInvalid Referred-By URI (%.*s)!", pfncname, STR_FMT (pvalue));
return -1;
}
if (pref->param_lst)
{ free_to_params (pref); }
/**********
* search calls for matching
**********/
int nidx;
str tmpstr;
struct to_body pfrom [1];
for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
{
if (!pmod_data->pcall_lst [nidx].call_active)
{ continue; }
tmpstr.s = pmod_data->pcall_lst [nidx].call_from;
tmpstr.len = strlen (tmpstr.s);
parse_to (tmpstr.s, &tmpstr.s [tmpstr.len + 1], pfrom);
if (pfrom->error != PARSE_OK)
{
// should never happen
LM_ERR ("%sInvalid From URI (%.*s)!", pfncname, STR_FMT (&tmpstr));
continue;
}
if (pfrom->param_lst)
{ free_to_params (pfrom); }
if (STR_EQ (pfrom->uri, pref->uri))
{ return nidx; }
}
return -1;
}
/**********
* Process First INVITE Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = queue index
* OUTPUT: 0=failed
**********/
int first_invite_msg (sip_msg_t *pmsg, int mohq_idx)
{
/**********
* create call record
**********/
char *pfncname = "first_invite_msg: ";
int ncall_idx = create_call (mohq_idx, pmsg);
if (ncall_idx == -1)
{ return 0; }
call_lst *pcall = &pmod_data->pcall_lst [ncall_idx];
/**********
* o SDP exists?
* o accepts REFER?
* o send rtpproxy offer
**********/
if (!(pmsg->msg_flags & FL_SDP_BODY))
{
if (parse_sdp (pmsg))
{
LM_ERR ("%sINVITE lacks SDP (%s)!", pfncname, pcall->call_from);
delete_call (pcall);
return 0;
}
}
if (pmsg->allow)
{
if (!search_hdr_ext (pmsg->allow, prefer))
{
LM_ERR ("%sMissing REFER support (%s)!", pfncname, pcall->call_from);
delete_call (pcall);
return 0;
}
}
mohq_debug (pcall->pmohq, "%sMaking offer for RTP link for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->fn_rtp_offer (pmsg, 0, 0) != 1)
{
LM_ERR ("%srtpproxy_offer refused for call (%s)!",
pfncname, pcall->call_from);
delete_call (pcall);
return 0;
}
/**********
* o create new transaction
* o save To tag
* o catch failures
* o save transaction data
**********/
tm_api_t *ptm = pmod_data->ptm;
if (ptm->t_newtran (pmsg) < 0)
{
LM_ERR ("%sUnable to create new transaction for call (%s)!",
pfncname, pcall->call_from);
delete_call (pcall);
return 0;
}
struct cell *ptrans = ptm->t_gett ();
pcall->call_hash = ptrans->hash_index;
pcall->call_label = ptrans->label;
str ptotag [1];
if (ptm->t_get_reply_totag (pmsg, ptotag) != 1)
{
LM_ERR ("%sUnable to create totag for call (%s)!",
pfncname, pcall->call_from);
if (ptm->t_reply (pmsg, 500, presp_srverr->s) < 0)
{ LM_ERR ("%sUnable to reply to INVITE!", pfncname); }
delete_call (pcall);
return 1;
}
strncpy (pcall->call_tag, ptotag->s, ptotag->len);
pcall->call_tag [ptotag->len] = '\0';
pcall->call_cseq = 1;
if (ptm->register_tmcb (pmsg, 0, TMCB_DESTROY | TMCB_ON_FAILURE,
invite_cb, pcall, 0) < 0)
{
LM_ERR ("%sUnable to set callback for call (%s)!",
pfncname, pcall->call_from);
if (ptm->t_reply (pmsg, 500, presp_srverr->s) < 0)
{ LM_ERR ("%sUnable to reply to INVITE!", pfncname); }
delete_call (pcall);
return 1;
}
/**********
* o add contact to reply
* o supports/requires PRACK? (RFC 3262 section 3)
* o exit if not ringing
**********/
str pcontact [1];
char *pcontacthdr = "Contact: <%s>" SIPEOL;
pcontact->s = pkg_malloc (strlen (pmod_data->pmohq_lst [mohq_idx].mohq_uri)
+ strlen (pcontacthdr));
if (!pcontact->s)
{
LM_ERR ("%sNo more memory!", pfncname);
delete_call (pcall);
return 1;
}
sprintf (pcontact->s, pcontacthdr, pmod_data->pmohq_lst [mohq_idx].mohq_uri);
pcontact->len = strlen (pcontact->s);
if (!add_lump_rpl2 (pmsg, pcontact->s, pcontact->len, LUMP_RPL_HDR))
{
LM_ERR ("%sUnable to add contact (%s) to call (%s)!",
pfncname, pcontact->s, pcall->call_from);
}
pkg_free (pcontact->s);
pcall->call_pmsg = pmsg;
if (search_hdr_ext (pmsg->require, p100rel))
{
if (!send_prov_rsp (pmsg, pcall))
{
delete_call (pcall);
return 1;
}
}
else
{
if (ptm->t_reply (pmsg, 180, presp_ring->s) < 0)
{
LM_ERR ("%sUnable to reply to INVITE!", pfncname);
return 1;
}
else
{
pcall->call_state = CLSTA_RINGING;
mohq_debug (pcall->pmohq, "%sSent RINGING for call (%s)",
pfncname, pcall->call_from);
}
}
/**********
* o call cancelled?
* o accept call with RTP
**********/
if (pcall->call_state == CLSTA_CANCEL)
{
delete_call (pcall);
return 1;
}
if (!send_rtp_answer (pmsg, pcall))
{
if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
delete_call (pcall);
}
return 1;
}
/**********
* Form Dialog
*
* INPUT:
* Arg (1) = call pointer
* Arg (2) = to_body [2] pointer
* OUTPUT: dlg_t * if successful; 0=if not
**********/
dlg_t *form_dialog (call_lst *pcall, struct to_body *pto_body)
{
/**********
* get from/to values
**********/
char *pfncname = "form_dialog: ";
struct to_body *ptob = &pto_body [0];
struct to_body *pcontact = &pto_body [1];
parse_to (pcall->call_from,
&pcall->call_from [strlen (pcall->call_from) + 1], ptob);
if (ptob->error != PARSE_OK)
{
// should never happen
LM_ERR ("%sInvalid from URI (%s)!", pfncname, pcall->call_from);
return 0;
}
if (ptob->param_lst)
{ free_to_params (ptob); }
str ptarget [1];
if (!*pcall->call_contact)
{
ptarget->s = ptob->uri.s;
ptarget->len = ptob->uri.len;
}
else
{
parse_to (pcall->call_contact,
&pcall->call_contact [strlen (pcall->call_contact) + 1], pcontact);
if (pcontact->error != PARSE_OK)
{
// should never happen
LM_ERR ("%sInvalid contact (%s) for call (%s)!", pfncname,
pcall->call_contact, pcall->call_from);
return 0;
}
if (pcontact->param_lst)
{ free_to_params (pcontact); }
ptarget->s = pcontact->uri.s;
ptarget->len = pcontact->uri.len;
}
/**********
* create dialog
**********/
dlg_t *pdlg = (dlg_t *)pkg_malloc (sizeof (dlg_t));
if (!pdlg)
{
LM_ERR ("%sNo more memory!", pfncname);
return 0;
}
memset (pdlg, 0, sizeof (dlg_t));
pdlg->loc_seq.value = pcall->call_cseq++;
pdlg->loc_seq.is_set = 1;
pdlg->id.call_id.s = pcall->call_id;
pdlg->id.call_id.len = strlen (pcall->call_id);
pdlg->id.loc_tag.s = pcall->call_tag;
pdlg->id.loc_tag.len = strlen (pcall->call_tag);
pdlg->id.rem_tag.s = ptob->tag_value.s;
pdlg->id.rem_tag.len = ptob->tag_value.len;
pdlg->rem_target.s = ptarget->s;
pdlg->rem_target.len = ptarget->len;
pdlg->loc_uri.s = pcall->pmohq->mohq_uri;
pdlg->loc_uri.len = strlen (pdlg->loc_uri.s);
pdlg->rem_uri.s = ptob->uri.s;
pdlg->rem_uri.len = ptob->uri.len;
return pdlg;
}
/**********
* Form RTP SDP String
*
* INPUT:
* Arg (1) = string pointer
* Arg (2) = call pointer
* Arg (3) = SDP body pointer
* OUTPUT: 0 if failed
**********/
int form_rtp_SDP (str *pstr, call_lst *pcall, char *pSDP)
{
/**********
* o find available files
* o calculate size of SDP
**********/
char *pfncname = "form_rtp_SDP: ";
rtpmap **pmohfiles = find_MOH (pcall->pmohq->mohq_mohdir,
pcall->pmohq->mohq_mohfile);
if (!pmohfiles [0])
{
LM_ERR ("%sUnable to find any MOH files for queue (%s)!", pfncname,
pcall->pmohq->mohq_name);
return 0;
}
int nsize = strlen (pSDP) + 2;
int nidx;
for (nidx = 0; pmohfiles [nidx]; nidx++)
{
nsize += strlen (pmohfiles [nidx]->pencode) // encode length
+ 19; // space, type number, "a=rtpmap:%d ", EOL
}
/**********
* o allocate memory
* o form SDP
**********/
pstr->s = pkg_malloc (nsize + 1);
if (!pstr->s)
{
LM_ERR ("%sNo more memory!", pfncname);
return 0;
}
strcpy (pstr->s, pSDP);
nsize = strlen (pstr->s);
for (nidx = 0; pmohfiles [nidx]; nidx++)
{
/**********
* add payload types to media description
**********/
sprintf (&pstr->s [nsize], " %d", pmohfiles [nidx]->ntype);
nsize += strlen (&pstr->s [nsize]);
}
strcpy (&pstr->s [nsize], SIPEOL);
nsize += 2;
for (nidx = 0; pmohfiles [nidx]; nidx++)
{
/**********
* add rtpmap attributes
**********/
sprintf (&pstr->s [nsize], "a=rtpmap:%d %s %s",
pmohfiles [nidx]->ntype, pmohfiles [nidx]->pencode, SIPEOL);
nsize += strlen (&pstr->s [nsize]);
}
pstr->len = nsize;
return 1;
}
/**********
* Invite Callback
*
* INPUT:
* Arg (1) = cell pointer
* Arg (2) = callback type
* Arg (3) = callback parms
* OUTPUT: none
**********/
static void
invite_cb (struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
{
call_lst *pcall = (call_lst *)*pcbp->param;
if (ntype == TMCB_DESTROY)
{ pcall->call_hash = pcall->call_label = 0; }
LM_ERR ("invite_cb: INVITE failed for call (%s)!", pcall->call_from);
delete_call (pcall);
return;
}
/**********
* Process NOTIFY Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int notify_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* waiting on REFER?
**********/
char *pfncname = "notify_msg: ";
if (pcall->call_state != CLSTA_RFRWAIT)
{
LM_ERR ("%sNot waiting on a REFER for call (%s)!", pfncname,
pcall->call_from);
if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
/**********
* o sipfrag?
* o get status from body
* o add CRLF so parser can go beyond first line
**********/
if (!search_hdr_ext (pmsg->content_type, psipfrag))
{
LM_ERR ("%sNot a %s type for call (%s)!", pfncname,
psipfrag->s, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
char *pfrag = get_body (pmsg);
if (!pfrag)
{
LM_ERR ("%s%s body missing for call (%s)!", pfncname,
psipfrag->s, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
str pbody [1];
pbody->len = pmsg->len - (int)(pfrag - pmsg->buf);
pbody->s = pkg_malloc (pbody->len + 2);
if (!pbody->s)
{
LM_ERR ("%sNo more memory!", pfncname);
return 1;
}
strncpy (pbody->s, pfrag, pbody->len);
if (pbody->s [pbody->len - 1] != '\n')
{
strncpy (&pbody->s [pbody->len], SIPEOL, 2);
pbody->len += 2;
}
struct msg_start pstart [1];
parse_first_line (pbody->s, pbody->len + 1, pstart);
pkg_free (pbody->s);
if (pstart->type != SIP_REPLY)
{
LM_ERR ("%sReply missing for call (%s)!", pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 415, presp_unsupp) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
/**********
* o send OK
* o REFER done?
**********/
if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
{
LM_ERR ("%sUnable to create reply for call (%s)!",
pfncname, pcall->call_from);
return 1;
}
int nreply = pstart->u.reply.statuscode;
mohq_debug (pcall->pmohq, "%sNOTIFY received reply (%d) for call (%s)",
pfncname, nreply, pcall->call_from);
switch (nreply / 100)
{
case 1:
break;
case 2:
close_call (pmsg, pcall);
break;
default:
LM_WARN ("%sUnable to redirect call (%s)!", pfncname, pcall->call_from);
if (nreply == 487)
{
/**********
* call was canceled
**********/
drop_call (pmsg, pcall);
return 1;
}
/**********
* return call to queue
**********/
pcall->call_state = CLSTA_INQUEUE;
update_call_rec (pcall);
break;
}
return 1;
}
/**********
* Process PRACK Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int prack_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* waiting on PRACK?
**********/
char *pfncname = "prack_msg: ";
tm_api_t *ptm = pmod_data->ptm;
if (pcall->call_state != CLSTA_PRACKSTRT)
{
LM_ERR ("%sUnexpected PRACK (%s)!", pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 481, presp_nocall) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
/**********
* o check RAck ??? need to check
* o accept PRACK
**********/
if (ptm->t_newtran (pmsg) < 0)
{
LM_ERR ("%sUnable to create new transaction for call (%s)!",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
if (ptm->t_reply (pmsg, 200, presp_ok->s) < 0)
{
LM_ERR ("%sUnable to reply to PRACK for call (%s)!",
pfncname, pcall->call_from);
return 1;
}
pcall->call_state = CLSTA_PRACKRPLY;
return 1;
}
/**********
* Refer Call
*
* INPUT:
* Arg (1) = call pointer
* Arg (2) = lock pointer
* OUTPUT: 0 if failed
**********/
int refer_call (call_lst *pcall, mohq_lock *plock)
{
/**********
* create dialog
**********/
char *pfncname = "refer_call: ";
int nret = 0;
struct to_body ptob [2];
dlg_t *pdlg = form_dialog (pcall, ptob);
if (!pdlg)
{
mohq_lock_release (plock);
return 0;
}
pdlg->state = DLG_CONFIRMED;
/**********
* form REFER message
* o calculate basic size
* o create buffer
**********/
str puri [1];
puri->s = pcall->call_referto;
puri->len = strlen (puri->s);
int npos1 = sizeof (prefermsg) // REFER template
+ strlen (pcall->call_via) // Via
+ puri->len // Refer-To
+ ptob->uri.len; // Referred-By
char *pbuf = pkg_malloc (npos1);
if (!pbuf)
{
LM_ERR ("%sNo more memory!", pfncname);
goto refererr;
}
sprintf (pbuf, prefermsg,
pcall->call_via, // Via
puri->s, // Refer-To
STR_FMT (&ptob->uri)); // Referred-By
/**********
* send REFER request
**********/
tm_api_t *ptm = pmod_data->ptm;
uac_req_t puac [1];
str phdrs [1];
phdrs->s = pbuf;
phdrs->len = strlen (pbuf);
set_uac_req (puac, prefer, phdrs, 0, pdlg,
TMCB_LOCAL_COMPLETED | TMCB_ON_FAILURE, refer_cb, pcall);
pcall->call_state = CLSTA_REFER;
update_call_rec (pcall);
mohq_lock_release (plock);
if (ptm->t_request_within (puac) < 0)
{
pcall->call_state = CLSTA_INQUEUE;
LM_ERR ("%sUnable to create REFER request for call (%s)!",
pfncname, pcall->call_from);
update_call_rec (pcall);
goto refererr;
}
mohq_debug (pcall->pmohq, "%sSent REFER request for call (%s) to %s",
pfncname, pcall->call_from, pcall->call_referto);
nret = -1;
refererr:
if (pdlg)
{ pkg_free (pdlg); }
pkg_free (pbuf);
return nret;
}
/**********
* REFER Callback
*
* INPUT:
* Arg (1) = cell pointer
* Arg (2) = callback type
* Arg (3) = callback parms
* OUTPUT: none
**********/
static void refer_cb
(struct cell *ptrans, int ntype, struct tmcb_params *pcbp)
{
char *pfncname = "refer_cb: ";
call_lst *pcall = (call_lst *)*pcbp->param;
if ((ntype == TMCB_ON_FAILURE) || (pcbp->req == FAKED_REPLY))
{
LM_ERR ("%sCall (%s) did not respond to REFER", pfncname,
pcall->call_from);
drop_call (pcbp->req, pcall);
return;
}
int nreply = pcbp->code;
if ((nreply / 100) == 2)
{
pcall->call_state = CLSTA_RFRWAIT;
mohq_debug (pcall->pmohq, "%sCall (%s) REFER reply=%d",
pfncname, pcall->call_from, nreply);
}
else
{
LM_ERR ("%sCall (%s) REFER error (%d)", pfncname,
pcall->call_from, nreply);
if (nreply == 481)
{ delete_call (pcall); }
else
{
pcall->call_state = CLSTA_INQUEUE;
update_call_rec (pcall);
}
}
return;
}
/**********
* Process re-INVITE Message
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=failed
**********/
int reinvite_msg (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* RFC 3261 section 14.2
* o dialog pending?
* o get SDP
**********/
char *pfncname = "reinvite_msg: ";
if ((pcall->call_state / 100) < 2)
{
mohq_debug (pcall->pmohq, "%sINVITE still pending for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 491, presp_reqpend) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
if (!(pmsg->msg_flags & FL_SDP_BODY))
{
if (parse_sdp (pmsg))
{
LM_ERR ("%sre-INVITE lacks SDP (%s)!", pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 488, presp_noaccept) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 1;
}
}
/**********
* o find available MOH files
* o look for hold condition and matching payload type
**********/
rtpmap **pmohfiles = find_MOH (pcall->pmohq->mohq_mohdir,
pcall->pmohq->mohq_mohfile);
int bhold = 0;
int bmatch = 0;
int nsession;
sdp_session_cell_t *psession;
for (nsession = 0; (psession = get_sdp_session (pmsg, nsession)); nsession++)
{
int nstream;
sdp_stream_cell_t *pstream;
for (nstream = 0; (pstream = get_sdp_stream (pmsg, nsession, nstream));
nstream++)
{
/**********
* o RTP?
* o audio?
* o hold?
* o at least one payload matches?
**********/
if (!pstream->is_rtp)
{ continue; }
if (!STR_EQ (*paudio, pstream->media))
{ continue; }
if (pstream->is_on_hold)
{
bhold = 1;
break;
}
if (bmatch)
{ continue; }
/**********
* check payload types for a match
**********/
sdp_payload_attr_t *ppayload;
for (ppayload = pstream->payload_attr; ppayload; ppayload = ppayload->next)
{
int ntype = atoi (ppayload->rtp_payload.s);
int nidx;
for (nidx = 0; pmohfiles [nidx]; nidx++)
{
if (pmohfiles [nidx]->ntype == ntype)
{
bmatch = 1;
break;
}
}
}
}
}
/**********
* if no hold, allow re-INVITE if matching file
**********/
if (!bhold)
{
if (!bmatch)
{
LM_ERR ("%sre-INVITE refused because no matching payload for call (%s)!",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 488, presp_noaccept) < 0)
{
LM_ERR ("%sUnable to create reply!", pfncname);
return 1;
}
}
else
{
mohq_debug (pcall->pmohq, "%sAccepted re-INVITE for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
{
LM_ERR ("%sUnable to create reply!", pfncname);
return 1;
}
}
return 1;
}
/**********
* hold not allowed, say good-bye
**********/
LM_ERR ("%sTerminating call (%s) because hold not allowed!",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 200, presp_ok) < 0)
{
LM_ERR ("%sUnable to create reply!", pfncname);
return 1;
}
close_call (pmsg, pcall);
return 1;
}
/**********
* Search Header for Extension
*
* INPUT:
* Arg (1) = header field pointer
* Arg (2) = extension str pointer
* OUTPUT: 0=not found
**********/
int search_hdr_ext (struct hdr_field *phdr, str *pext)
{
if (!phdr)
{ return 0; }
str *pstr = &phdr->body;
int npos1, npos2;
for (npos1 = 0; npos1 < pstr->len; npos1++)
{
/**********
* o find non-space
* o search to end, space or comma
* o same size?
* o same name?
**********/
if (pstr->s [npos1] == ' ')
{ continue; }
for (npos2 = npos1++; npos1 < pstr->len; npos1++)
{
if (pstr->s [npos1] == ' ' || pstr->s [npos1] == ',')
{ break; }
}
if (npos1 - npos2 != pext->len)
{ continue; }
if (!strncasecmp (&pstr->s [npos2], pext->s, pext->len))
{ return 1; }
}
return 0;
}
/**********
* Send Provisional Response
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=unable to process; 1=processed
**********/
int send_prov_rsp (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* o send ringing response with require
* o update record
**********/
char *pfncname = "send_prov_rsp: ";
tm_api_t *ptm = pmod_data->ptm;
pcall->call_cseq = rand ();
char phdrtmp [200];
char *phdrtmplt =
"Accept-Language: en" SIPEOL
"Require: 100rel" SIPEOL
"RSeq: %d" SIPEOL
"User-Agent: " USRAGNT SIPEOL
;
sprintf (phdrtmp, phdrtmplt, pcall->call_cseq);
struct lump_rpl **phdrlump = add_lump_rpl2 (pmsg, phdrtmp,
strlen (phdrtmp), LUMP_RPL_HDR);
if (!phdrlump)
{
LM_ERR ("%sUnable to create new header for call (%s)!",
pfncname, pcall->call_from);
if (pmod_data->psl->freply (pmsg, 500, presp_srverr) < 0)
{ LM_ERR ("%sUnable to create reply!", pfncname); }
return 0;
}
if (ptm->t_reply (pmsg, 180, presp_ring->s) < 0)
{
LM_ERR ("%sUnable to reply to INVITE for call (%s)",
pfncname, pcall->call_from);
return 0;
}
pcall->call_state = CLSTA_PRACKSTRT;
mohq_debug (pcall->pmohq, "%sSent PRACK RINGING for call (%s)",
pfncname, pcall->call_from);
/**********
* o wait until PRACK (64*T1 RFC 3261 section 7.1.1)
* o remove header lump
**********/
time_t nstart = time (0) + 32;
while (1)
{
usleep (USLEEP_LEN);
if (pcall->call_state != CLSTA_PRACKSTRT)
{ break; }
if (nstart < time (0))
{
LM_ERR ("%sNo PRACK response for call (%s)",
pfncname, pcall->call_from);
break;
}
}
unlink_lump_rpl (pmsg, *phdrlump);
if (pcall->call_state != CLSTA_PRACKRPLY)
{ return 0; }
return 1;
}
/**********
* Send RTPProxy Answer
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* OUTPUT: 0=unable to process; 1=processed
**********/
int send_rtp_answer (sip_msg_t *pmsg, call_lst *pcall)
{
/**********
* build response from request
**********/
char *pfncname = "send_rtp_answer: ";
int nret = 0;
tm_api_t *ptm = pmod_data->ptm;
struct cell *ptrans = ptm->t_gett ();
str ptotag [1];
ptotag->s = pcall->call_tag;
ptotag->len = strlen (pcall->call_tag);
str pbuf [1];
struct bookmark pBM [1];
pbuf->s = build_res_buf_from_sip_req (200, presp_ok, ptotag, ptrans->uas.request,
(unsigned int *)&pbuf->len, pBM);
if (!pbuf->s || !pbuf->len)
{
LM_ERR ("%sUnable to create SDP response for call (%s)!",
pfncname, pcall->call_from);
return 0;
}
/**********
* parse out first line and headers
**********/
char *pclenhdr = CLENHDR;
str pparse [20];
int npos1, npos2;
int nhdrcnt = 0;
for (npos1 = 0; npos1 < pbuf->len; npos1++)
{
/**********
* find EOL
**********/
for (npos2 = npos1++; npos1 < pbuf->len; npos1++)
{
/**********
* o not EOL? (CRLF assumed)
* o next line a continuation? (RFC 3261 section 7.3.1)
**********/
if (pbuf->s [npos1] != '\n')
{ continue; }
if (npos1 + 1 == pbuf->len)
{ break; }
if (pbuf->s [npos1 + 1] == ' '
|| pbuf->s [npos1 + 1] == '\t')
{ continue; }
break;
}
/**********
* o blank is end of header (RFC 3261 section 7)
* o ignore Content-Length (assume followed by colon)
* o save header
**********/
if (npos1 - npos2 == 1)
{ break; }
if (npos1 - npos2 > 14)
{
if (!strncasecmp (&pbuf->s [npos2], pclenhdr, 14))
{ continue; }
}
pparse [nhdrcnt].s = &pbuf->s [npos2];
pparse [nhdrcnt++].len = npos1 - npos2 + 1;
}
/**********
* recreate buffer with extra headers and SDP
* o form SDP
* o count hdrs, extra hdrs, content-length hdr, SDP
* o alloc new buffer
* o form new buffer
* o replace orig buffer
**********/
str pSDP [1] = {STR_NULL};
if (!form_rtp_SDP (pSDP, pcall, prtpsdp))
{ goto answer_done; }
for (npos1 = npos2 = 0; npos2 < nhdrcnt; npos2++)
{ npos1 += pparse [npos2].len; }
char pbodylen [30];
sprintf (pbodylen, "%s: %d\r\n\r\n", pclenhdr, pSDP->len);
npos1 += pextrahdr->len + strlen (pbodylen) + pSDP->len + 1;
char *pnewbuf = pkg_malloc (npos1);
if (!pnewbuf)
{
LM_ERR ("%sNo more memory!", pfncname);
goto answer_done;
}
for (npos1 = npos2 = 0; npos2 < nhdrcnt; npos2++)
{
memcpy (&pnewbuf [npos1], pparse [npos2].s, pparse [npos2].len);
npos1 += pparse [npos2].len;
}
npos2 = pextrahdr->len;
memcpy (&pnewbuf [npos1], pextrahdr->s, npos2);
npos1 += npos2;
npos2 = strlen (pbodylen);
memcpy (&pnewbuf [npos1], pbodylen, npos2);
npos1 += npos2;
npos2 = pSDP->len;
memcpy (&pnewbuf [npos1], pSDP->s, npos2);
npos1 += npos2;
pkg_free (pbuf->s);
pbuf->s = pnewbuf;
pbuf->len = npos1;
/**********
* build SIP msg
**********/
struct sip_msg pnmsg [1];
build_sip_msg_from_buf (pnmsg, pbuf->s, pbuf->len, 0);
memcpy (&pnmsg->rcv, &pmsg->rcv, sizeof (struct receive_info));
/**********
* o send rtpproxy answer
* o form stream file
* o send stream
**********/
mohq_debug (pcall->pmohq, "%sAnswering RTP link for call (%s)",
pfncname, pcall->call_from);
if (pmod_data->fn_rtp_answer (pnmsg, 0, 0) != 1)
{
LM_ERR ("%srtpproxy_answer refused for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
if (!start_stream (pnmsg, pcall, 0))
{ goto answer_done; }
/**********
* o create buffer from response
* o find SDP
**********/
pbuf->s = build_res_buf_from_sip_res (pnmsg, (unsigned int *)&pbuf->len);
pkg_free (pnewbuf);
free_sip_msg (pnmsg);
if (!pbuf->s || !pbuf->len)
{
LM_ERR ("%sUnable to create SDP response for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
str pnewSDP [1];
for (npos1 = 0; npos1 < pbuf->len; npos1++)
{
if (pbuf->s [npos1] != '\n')
{ continue; }
if (pbuf->s [npos1 - 3] == '\r')
{ break; }
}
pnewSDP->s = &pbuf->s [npos1 + 1];
pnewSDP->len = pbuf->len - npos1 - 1;
/**********
* o save media port number
* o send adjusted reply
**********/
char *pfnd = strstr (pnewSDP->s, "m=audio ");
if (!pfnd)
{
// should not happen
LM_ERR ("%sUnable to find audio port for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
pcall->call_aport = strtol (pfnd + 8, NULL, 10);
if (!add_lump_rpl2 (pmsg, pextrahdr->s, pextrahdr->len, LUMP_RPL_HDR))
{
LM_ERR ("%sUnable to add header for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
if (!add_lump_rpl2 (pmsg, pnewSDP->s, pnewSDP->len, LUMP_RPL_BODY))
{
LM_ERR ("%sUnable to add SDP body for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
if (ptm->t_reply (pmsg, 200, presp_ok->s) < 0)
{
LM_ERR ("%sUnable to reply to INVITE for call (%s)!",
pfncname, pcall->call_from);
goto answer_done;
}
pcall->call_state = CLSTA_INVITED;
mohq_debug (pcall->pmohq, "%sResponded to INVITE with RTP for call (%s)",
pfncname, pcall->call_from);
nret = 1;
/**********
* free buffer and return
**********/
answer_done:
if (pSDP->s)
{ pkg_free (pSDP->s); }
pkg_free (pbuf->s);
return nret;
}
/**********
* Start Streaming
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = call pointer
* Arg (3) = server flag
* OUTPUT: 0 if failed
**********/
int start_stream (sip_msg_t *pmsg, call_lst *pcall, int bserver)
{
char *pfncname = "start_stream: ";
char pfile [MOHDIRLEN + MOHFILELEN + 2];
strcpy (pfile, pcall->pmohq->mohq_mohdir);
int npos = strlen (pfile);
pfile [npos++] = '/';
strcpy (&pfile [npos], pcall->pmohq->mohq_mohfile);
npos += strlen (&pfile [npos]);
str pMOH [1] = {{pfile, npos}};
pv_elem_t *pmodel;
pv_parse_format (pMOH, &pmodel);
cmd_function fn_stream = bserver ? pmod_data->fn_rtp_stream_s
: pmod_data->fn_rtp_stream_c;
mohq_debug (pcall->pmohq, "%sStarting RTP link for call (%s)",
pfncname, pcall->call_from);
if (fn_stream (pmsg, (char *)pmodel, (char *)-1) != 1)
{
LM_ERR ("%srtpproxy_stream refused for call (%s)!",
pfncname, pcall->call_from);
return 0;
}
return 1;
}
/**********
* Form Char Array from STR
*
* INPUT:
* Arg (1) = str pointer
* OUTPUT: char pointer; NULL if unable to allocate
**********/
char *form_tmpstr (str *pstr)
{
char *pcstr = malloc (pstr->len + 1);
if (!pcstr)
{
LM_ERR ("No more memory!");
return NULL;
}
memcpy (pcstr, pstr->s, pstr->len);
pcstr [pstr->len] = 0;
return pcstr;
}
/**********
* Release Char Array
*
* INPUT:
* Arg (1) = char pointer
* OUTPUT: none
**********/
void free_tmpstr (char *pcstr)
{
if (pcstr)
{ free (pcstr); }
return;
}
/**********
* external functions
**********/
/**********
* Find MOH Files
*
* INPUT:
* Arg (1) = mohdir pointer
* Arg (2) = mohfile pointer
* OUTPUT: array of pointers for matching files; last element=0
**********/
rtpmap **find_MOH (char *pmohdir, char *pmohfile)
{
/**********
* form base file name
**********/
char pfile [MOHDIRLEN + MOHFILELEN + 6];
strcpy (pfile, pmohdir);
int nflen = strlen (pfile);
pfile [nflen++] = '/';
strcpy (&pfile [nflen], pmohfile);
nflen += strlen (&pfile [nflen]);
pfile [nflen++] = '.';
/**********
* find available files based on RTP payload type
**********/
int nidx;
int nfound = 0;
for (nidx = 0; prtpmap [nidx].pencode; nidx++)
{
/**********
* o form file name based on payload type
* o exists?
**********/
sprintf (&pfile [nflen], "%d", prtpmap [nidx].ntype);
struct stat psb [1];
if (lstat (pfile, psb))
{ continue; }
pmohfiles [nfound++] = &prtpmap [nidx];
}
pmohfiles [nfound] = 0;
return pmohfiles;
}
/**********
* MI Debug
*
* PARAMETERS:
* queue name = queue to use
* state = 0=off, <>0=on
*
* INPUT:
* Arg (1) = command tree pointer
* Arg (2) = parms pointer
* OUTPUT: root pointer
**********/
struct mi_root *mi_debug (struct mi_root *pcmd_tree, void *parms)
{
/**********
* o parm count correct?
* o find queue
* o lock queue
**********/
struct mi_node *pnode = pcmd_tree->node.kids;
if (!pnode || !pnode->next || pnode->next->next)
{ return init_mi_tree (400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); }
int nq_idx = find_queue (&pnode->value);
if (nq_idx == -1)
{ return init_mi_tree (400, pmi_noqueue->s, pmi_noqueue->len); }
char pint [20];
int nsize = (pnode->next->value.len >= sizeof (pint))
? sizeof (pint) - 1 : pnode->next->value.len;
strncpy (pint, pnode->next->value.s, nsize);
pint [nsize] = '\0';
int bdebug = atoi (pint) ? 1 : 0;
if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 5000))
{ return init_mi_tree (400, pmi_nolock->s, pmi_nolock->len); }
/**********
* o set flag
* o update queue table
* o release lock
**********/
mohq_lst *pqueue = &pmod_data->pmohq_lst [nq_idx];
if (bdebug)
{ pqueue->mohq_flags |= MOHQF_DBG; }
else
{ pqueue->mohq_flags &= ~MOHQF_DBG; }
update_debug (pqueue, bdebug);
mohq_lock_release (pmod_data->pmohq_lock);
return init_mi_tree (200, MI_OK_S, MI_OK_LEN);
}
/**********
* MI Drop Call
*
* PARAMETERS:
* queue name = queue to use
* callID = *=all, otherwise callID
*
* INPUT:
* Arg (1) = command tree pointer
* Arg (2) = parms pointer
* OUTPUT: root pointer
**********/
struct mi_root *mi_drop_call (struct mi_root *pcmd_tree, void *parms)
{
/**********
* o parm count correct?
* o find queue
* o lock calls
**********/
struct mi_node *pnode = pcmd_tree->node.kids;
if (!pnode || !pnode->next || pnode->next->next)
{ return init_mi_tree (400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); }
int nq_idx = find_queue (&pnode->value);
if (nq_idx == -1)
{ return init_mi_tree (400, pmi_noqueue->s, pmi_noqueue->len); }
if (!mohq_lock_set (pmod_data->pcall_lock, 0, 5000))
{ return init_mi_tree (400, pmi_nolock->s, pmi_nolock->len); }
/**********
* o find matching calls
* o release lock
**********/
mohq_lst *pqueue = &pmod_data->pmohq_lst [nq_idx];
int nidx;
str *pcallid = &pnode->next->value;
for (nidx = 0; nidx < pmod_data->call_cnt; nidx++)
{
/**********
* o call active?
* o callID matches?
* o close call
**********/
call_lst *pcall = &pmod_data->pcall_lst [nidx];
if (!pcall->call_active)
{ continue; }
if (pqueue->mohq_id != pcall->pmohq->mohq_id)
{ continue; }
str tmpstr [1];
if (!STR_EQ (*pcallid, *pallq))
{
tmpstr->s = pcall->call_id;
tmpstr->len = strlen (tmpstr->s);
if (!STR_EQ (*tmpstr, *pcallid))
{ continue; }
}
close_call (FAKED_REPLY, pcall);
}
mohq_lock_release (pmod_data->pcall_lock);
return init_mi_tree (200, MI_OK_S, MI_OK_LEN);
}
/**********
* Count Messages
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = queue name
* Arg (3) = pv result name
* OUTPUT: -1 if no items in queue; else result = count
**********/
int mohq_count (sip_msg_t *pmsg, char *pqueue, pv_spec_t *presult)
{
/**********
* get queue and pv names
**********/
char *pfncname = "mohq_count: ";
str pqname [1];
if (!pqueue || !presult)
{
LM_ERR ("%sParameters missing!", pfncname);
return -1;
}
if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
{
LM_ERR ("%sInvalid queue name!", pfncname);
return -1;
}
/**********
* o find queue
* o lock calls
* o count items in queue
**********/
int nq_idx = find_queue (pqname);
int ncount = 0;
call_lst *pcalls = pmod_data->pcall_lst;
int ncall_idx, mohq_id;
if (!mohq_lock_set (pmod_data->pcall_lock, 0, 200))
{ LM_ERR ("%sUnable to lock calls!", pfncname); }
else
{
if (nq_idx != -1)
{
mohq_id = pmod_data->pmohq_lst [nq_idx].mohq_id;
for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
{
if (!pcalls [ncall_idx].call_active)
{ continue; }
if (pcalls [ncall_idx].pmohq->mohq_id == mohq_id
&& pcalls [ncall_idx].call_state == CLSTA_INQUEUE)
{ ncount++; }
}
}
mohq_lock_release (pmod_data->pcall_lock);
}
/**********
* o set pv result
* o exit with result
**********/
pv_value_t pavp_val [1];
memset (pavp_val, 0, sizeof (pv_value_t));
pavp_val->ri = ncount;
pavp_val->flags = PV_TYPE_INT | PV_VAL_INT;
if (presult->setf (pmsg, &presult->pvp, (int)EQ_T, pavp_val) < 0)
{
LM_ERR ("%sUnable to set pv value for mohq_count ()!", pfncname);
return -1;
}
return 1;
}
/**********
* Log Debug Statement
*
* INPUT:
* Arg (1) = MOH queue pointer
* Arg (2) = format pointer
* Arg (...) = optional format values
* OUTPUT: outputs debugging values
**********/
void mohq_debug (mohq_lst *pmohq, char *pfmt, ...)
{
/**********
* o get system and MOHQ log level
* o exit if no debug printing
* o force local debug
* o form message and log
* o reset log level
**********/
int nsys_log = get_debug_level (LOG_MNAME, LOG_MNAME_LEN);
int nmohq_log = (pmohq->mohq_flags & MOHQF_DBG) ? L_DBG : L_INFO;
if (nmohq_log < L_DBG && nsys_log < L_DBG)
{ return; }
if (nsys_log < nmohq_log)
{ set_local_debug_level (nmohq_log); }
char ptext [1024];
va_list ap;
va_start (ap, pfmt);
vsnprintf (ptext, sizeof (ptext), pfmt, ap);
va_end (ap);
LM_DBG ("%s", ptext);
if (nsys_log < nmohq_log)
{ reset_local_debug_level (); }
return;
}
/**********
* Process Message
*
* INPUT:
* Arg (1) = SIP message pointer
* OUTPUT: -1=not directed to queue; 1=successfully processed
**********/
int mohq_process (sip_msg_t *pmsg)
{
/**********
* o parse headers
* o lock MOH queue
* o directed to message queue?
* o connect to database
**********/
char *pfncname = "mohq_process: ";
if (parse_headers (pmsg, HDR_EOH_F, 0) < 0)
{
LM_ERR ("%sUnable to parse header!", pfncname);
return -1;
}
if (!mohq_lock_set (pmod_data->pmohq_lock, 0, 2000))
{
LM_ERR ("%sUnable to lock calls!", pfncname);
return -1;
}
call_lst *pcall;
int mohq_idx = find_call (pmsg, &pcall);
db1_con_t *pconn = mohq_dbconnect ();
if (pconn)
{
/**********
* o last update older than 1 minute?
* o exclusively lock MOH queue
* o update queue
**********/
if (pmod_data->mohq_update + 60 < time (0))
{
if (mohq_lock_change (pmod_data->pmohq_lock, 1))
{
update_mohq_lst (pconn);
mohq_lock_change (pmod_data->pmohq_lock, 0);
pmod_data->mohq_update = time (0);
}
}
mohq_dbdisconnect (pconn);
}
if (mohq_idx < 0)
{
mohq_lock_release (pmod_data->pmohq_lock);
return -1;
}
/**********
* o process message
* o release MOH queue
**********/
mohq_debug (&pmod_data->pmohq_lst [mohq_idx],
"%sProcessing %.*s, queue (%s)", pfncname,
STR_FMT (&REQ_LINE (pmsg).method),
pmod_data->pmohq_lst [mohq_idx].mohq_name);
int ret;
switch (pmsg->REQ_METHOD)
{
case METHOD_INVITE:
/**********
* initial INVITE?
**********/
if (!pcall)
{ ret = first_invite_msg (pmsg, mohq_idx); }
else
{ ret = reinvite_msg (pmsg, pcall); }
break;
case METHOD_NOTIFY:
ret = notify_msg (pmsg, pcall);
break;
case METHOD_PRACK:
ret = prack_msg (pmsg, pcall);
break;
case METHOD_ACK:
ret = ack_msg (pmsg, pcall);
break;
case METHOD_BYE:
ret = bye_msg (pmsg, pcall);
break;
case METHOD_CANCEL:
ret = cancel_msg (pmsg, pcall);
break;
default:
deny_method (pmsg, pcall);
ret = 1;
break;
}
mohq_lock_release (pmod_data->pmohq_lock);
return ret ? 1 : -1;
}
/**********
* Retrieve Oldest Queued Call
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = queue name
* Arg (3) = redirect URI
* OUTPUT: -1 if no items in queue or error; 1 redirects oldest call
**********/
int mohq_retrieve (sip_msg_t *pmsg, char *pqueue, char *pURI)
{
/**********
* o get queue name and URI
* o check URI
**********/
char *pfncname = "mohq_retrieve: ";
str puri [1], pqname [1];
if (!pqueue || !pURI)
{
LM_ERR ("%sParameters missing!", pfncname);
return -1;
}
if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
{
LM_ERR ("%sInvalid queue name!", pfncname);
return -1;
}
if (fixup_get_svalue (pmsg, (gparam_p)pURI, puri))
{
LM_ERR ("%sInvalid URI!", pfncname);
return -1;
}
if (puri->len > URI_LEN)
{
LM_ERR ("%sURI too long!", pfncname);
return -1;
}
struct sip_uri puri_parsed [1];
if (parse_uri (puri->s, puri->len, puri_parsed))
{
LM_ERR ("%sInvalid URI (%.*s)!", pfncname, STR_FMT (puri));
return -1;
}
/**********
* o find queue
* o lock calls
* o find oldest call
**********/
int nq_idx = find_queue (pqname);
if (nq_idx == -1)
{ return -1; }
if (!mohq_lock_set (pmod_data->pcall_lock, 0, 200))
{
LM_ERR ("%sUnable to lock calls!", pfncname);
return -1;
}
call_lst *pcall = 0;
int ncall_idx;
time_t ntime = 0;
int nfound = -1;
int mohq_id = pmod_data->pmohq_lst [nq_idx].mohq_id;
for (ncall_idx = 0; ncall_idx < pmod_data->call_cnt; ncall_idx++)
{
/**********
* o active call?
* o matching queue?
* o in queue?
* o check age
**********/
pcall = &pmod_data->pcall_lst [ncall_idx];
if (!pcall->call_active)
{ continue; }
if (pcall->pmohq->mohq_id != mohq_id)
{ continue; }
if (pcall->call_state != CLSTA_INQUEUE)
{ continue; }
if (!ntime)
{
nfound = ncall_idx;
ntime = pcall->call_time;
}
else
{
if (pcall->call_time < ntime)
{
nfound = ncall_idx;
ntime = pcall->call_time;
}
}
}
if (nfound == -1)
{
LM_WARN ("%sNo calls in queue (%.*s)", pfncname, STR_FMT (pqname));
mohq_lock_release (pmod_data->pcall_lock);
return -1;
}
pcall = &pmod_data->pcall_lst [nfound];
/**********
* o save refer-to URI
* o send refer
**********/
strncpy (pcall->call_referto, puri->s, puri->len);
pcall->call_referto [puri->len] = '\0';
if (refer_call (pcall, pmod_data->pcall_lock))
{ return 1; }
LM_ERR ("%sUnable to refer call (%s)!", pfncname, pcall->call_from);
return -1;
}
/**********
* Send Message to Queue
*
* INPUT:
* Arg (1) = SIP message pointer
* Arg (2) = queue name
* OUTPUT: -1 if no items in queue; 1 if successfull
**********/
int mohq_send (sip_msg_t *pmsg, char *pqueue)
{
/**********
* o first INVITE?
* o get queue name
**********/
char *pfncname = "mohq_send: ";
if (pmsg->REQ_METHOD != METHOD_INVITE)
{
LM_ERR ("%sNot an INVITE message!", pfncname);
return -1;
}
to_body_t *pto_body = get_to (pmsg);
if (pto_body->tag_value.len)
{
LM_ERR ("%sNot a first INVITE message!", pfncname);
return -1;
}
str pqname [1];
if (!pqueue)
{
LM_ERR ("%sParameters missing!", pfncname);
return -1;
}
if (fixup_get_svalue (pmsg, (gparam_p)pqueue, pqname))
{
LM_ERR ("%sInvalid queue name!", pfncname);
return -1;
}
/**********
* o find queue
* o change RURI
* o relay message
**********/
int nq_idx = find_queue (pqname);
if (nq_idx == -1)
{ return -1; }
str pruri [1] = {{0, strlen (pmod_data->pmohq_lst [nq_idx].mohq_uri)}};
pruri->s = pkg_malloc (pruri->len + 1);
if (!pruri->s)
{
LM_ERR ("%sNo more memory!", pfncname);
return -1;
}
strcpy (pruri->s, pmod_data->pmohq_lst [nq_idx].mohq_uri);
if (pmsg->new_uri.s)
{ pkg_free (pmsg->new_uri.s); }
pmsg->new_uri.s = pruri->s;
pmsg->new_uri.len = pruri->len;
pmsg->parsed_uri_ok = 0;
pmsg->parsed_orig_ruri_ok = 0;
if (pmod_data->ptm->t_relay (pmsg, 0, 0) < 0)
{
LM_ERR ("%sUnable to relay INVITE!", pfncname);
return -1;
}
return 1;
}
|