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
|
/* $Id$
*
* Copyright (C) 2003-2008 Sippy Software, Inc., http://www.sippysoft.com
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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
*
* History:
* ---------
* 2003-10-09 nat_uac_test introduced (jiri)
*
* 2003-11-06 nat_uac_test permitted from onreply_route (jiri)
*
* 2003-12-01 unforce_rtp_proxy introduced (sobomax)
*
* 2004-01-07 RTP proxy support updated to support new version of the
* RTP proxy (20040107).
*
* force_rtp_proxy() now inserts a special flag
* into the SDP body to indicate that this session already
* proxied and ignores sessions with such flag.
*
* Added run-time check for version of command protocol
* supported by the RTP proxy.
*
* 2004-01-16 Integrated slightly modified patch from Tristan Colgate,
* force_rtp_proxy function with IP as a parameter (janakj)
*
* 2004-01-28 nat_uac_test extended to allow testing SDP body (sobomax)
*
* nat_uac_test extended to allow testing top Via (sobomax)
*
* 2004-02-21 force_rtp_proxy now accepts option argument, which
* consists of string of chars, each of them turns "on"
* some feature, currently supported ones are:
*
* `a' - flags that UA from which message is received
* doesn't support symmetric RTP;
* `l' - force "lookup", that is, only rewrite SDP when
* corresponding session is already exists in the
* RTP proxy. Only makes sense for SIP requests,
* replies are always processed in "lookup" mode;
* `i' - flags that message is received from UA in the
* LAN. Only makes sense when RTP proxy is running
* in the bridge mode.
*
* force_rtp_proxy can now be invoked without any arguments,
* as previously, with one argument - in this case argument
* is treated as option string and with two arguments, in
* which case 1st argument is option string and the 2nd
* one is IP address which have to be inserted into
* SDP (IP address on which RTP proxy listens).
*
* 2004-03-12 Added support for IPv6 addresses in SDPs. Particularly,
* force_rtp_proxy now can work with IPv6-aware RTP proxy,
* replacing IPv4 address in SDP with IPv6 one and vice versa.
* This allows creating full-fledged IPv4<->IPv6 gateway.
* See 4to6.cfg file for example.
*
* Two new options added into force_rtp_proxy:
*
* `f' - instructs nathelper to ignore marks inserted
* by another nathelper in transit to indicate
* that the session is already goes through another
* proxy. Allows creating chain of proxies.
* `r' - flags that IP address in SDP should be trusted.
* Without this flag, nathelper ignores address in the
* SDP and uses source address of the SIP message
* as media address which is passed to the RTP proxy.
*
* Protocol between nathelper and RTP proxy in bridge
* mode has been slightly changed. Now RTP proxy expects SER
* to provide 2 flags when creating or updating session
* to indicate direction of this session. Each of those
* flags can be either `e' or `i'. For example `ei' means
* that we received INVITE from UA on the "external" network
* network and will send it to the UA on "internal" one.
* Also possible `ie' (internal->external), `ii'
* (internal->internal) and `ee' (external->external). See
* example file alg.cfg for details.
*
* 2004-03-15 If the rtp proxy test failed (wrong version or not started)
* retry test from time to time, when some *rtpproxy* function
* is invoked. Minimum interval between retries can be
* configured via rtpproxy_disable_tout module parameter (default
* is 60 seconds). Setting it to -1 will disable periodic
* rechecks completely, setting it to 0 will force checks
* for each *rtpproxy* function call. (andrei)
*
* 2004-03-22 Fix assignment of rtpproxy_retr and rtpproxy_tout module
* parameters.
*
* 2004-03-22 Fix get_body position (should be called before get_callid)
* (andrei)
*
* 2004-03-24 Fix newport for null ip address case (e.g onhold re-INVITE)
* (andrei)
*
* 2004-09-30 added received port != via port test (andrei)
*
* 2004-10-10 force_socket option introduced (jiri)
*
* 2005-02-24 Added support for using more than one rtp proxy, in which
* case traffic will be distributed evenly among them. In addition,
* each such proxy can be assigned a weight, which will specify
* which share of the traffic should be placed to this particular
* proxy.
*
* Introduce failover mechanism, so that if SER detects that one
* of many proxies is no longer available it temporarily decreases
* its weight to 0, so that no traffic will be assigned to it.
* Such "disabled" proxies are periodically checked to see if they
* are back to normal in which case respective weight is restored
* resulting in traffic being sent to that proxy again.
*
* Those features can be enabled by specifying more than one "URI"
* in the rtpproxy_sock parameter, optionally followed by the weight,
* which if absent is assumed to be 1, for example:
*
* rtpproxy_sock="unix:/foo/bar=4 udp:1.2.3.4:3456=3 udp:5.6.7.8:5432=1"
*
* 2005-02-25 Force for pinging the socket returned by USRLOC (bogdan)
*
* 2005-03-22 support for multiple media streams added (netch)
*
* 2005-07-11 SIP ping support added (bogdan)
*
* 2005-07-14 SDP origin (o=) IP may be also changed (bogdan)
*
* 2006-03-08 fix_nated_sdp() may take one more param to force a specific IP;
* force_rtp_proxy() accepts a new flag 's' to swap creation/
* confirmation between requests/replies;
* add_rcv_param() may take as parameter a flag telling if the
* parameter should go to the contact URI or contact header;
* (bogdan)
* 2006-03-28 Support for changing session-level SDP connection (c=) IP when
* media-description also includes connection information (bayan)
* 2007-04-13 Support multiple sets of rtpproxies and set selection added
* (ancuta)
* 2007-04-26 Added some MI commands:
* nh_enable_ping used to enable or disable natping
* nh_enable_rtpp used to enable or disable a specific rtp proxy
* nh_show_rtpp used to display information for all rtp proxies
* (ancuta)
* 2007-05-09 New function start_recording() allowing to start recording RTP
* session in the RTP proxy (Carsten Bock - ported from SER)
* 2007-09-11 Separate timer process and support for multiple timer processes
* (bogdan)
* 2008-12-12 Support for RTCP attribute in the SDP
* (Min Wang/BASIS AudioNet - ported from SER)
* 2010-08-05 Core SDP parser integrated into nathelper
* (osas)
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#ifndef __USE_BSD
#define __USE_BSD
#endif
#include <netinet/ip.h>
#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../../flags.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../error.h"
#include "../../forward.h"
#include "../../ip_addr.h"
#include "../../mem/mem.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_uri.h"
#include "../../parser/parser_f.h"
#include "../../parser/parse_methods.h"
#include "../../parser/sdp/sdp.h"
#include "../../resolve.h"
#include "../../timer.h"
#include "../../trim.h"
#include "../../ut.h"
#include "../../pt.h"
#include "../../timer_proc.h"
#include "../../lib/kmi/attr.h"
#include "../../lib/kmi/mi.h"
#include "../../pvar.h"
#include "../../lvalue.h"
#include "../../msg_translator.h"
#include "../../usr_avp.h"
#include "../../socket_info.h"
#include "../../mod_fix.h"
#include "../../dset.h"
#include "../../select.h"
#include "../registrar/sip_msg.h"
#include "../usrloc/usrloc.h"
#include "nathelper.h"
#include "nhelpr_funcs.h"
#include "sip_pinger.h"
MODULE_VERSION
#if !defined(AF_LOCAL)
#define AF_LOCAL AF_UNIX
#endif
#if !defined(PF_LOCAL)
#define PF_LOCAL PF_UNIX
#endif
/* NAT UAC test constants */
#define NAT_UAC_TEST_C_1918 0x01
#define NAT_UAC_TEST_RCVD 0x02
#define NAT_UAC_TEST_V_1918 0x04
#define NAT_UAC_TEST_S_1918 0x08
#define NAT_UAC_TEST_RPORT 0x10
#define NAT_UAC_TEST_O_1918 0x20
#define NAT_UAC_TEST_WS 0x40
#define NAT_UAC_TEST_C_PORT 0x80
#define DEFAULT_RTPP_SET_ID 0
#define MI_SET_NATPING_STATE "nh_enable_ping"
#define MI_DEFAULT_NATPING_STATE 1
#define MI_ENABLE_RTP_PROXY "nh_enable_rtpp"
#define MI_MIN_RECHECK_TICKS 0
#define MI_MAX_RECHECK_TICKS (unsigned int)-1
#define MI_SHOW_RTP_PROXIES "nh_show_rtpp"
#define MI_RTP_PROXY_NOT_FOUND "RTP proxy not found"
#define MI_RTP_PROXY_NOT_FOUND_LEN (sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
#define MI_PING_DISABLED "NATping disabled from script"
#define MI_PING_DISABLED_LEN (sizeof(MI_PING_DISABLED)-1)
#define MI_SET "set"
#define MI_SET_LEN (sizeof(MI_SET)-1)
#define MI_INDEX "index"
#define MI_INDEX_LEN (sizeof(MI_INDEX)-1)
#define MI_DISABLED "disabled"
#define MI_DISABLED_LEN (sizeof(MI_DISABLED)-1)
#define MI_WEIGHT "weight"
#define MI_WEIGHT_LEN (sizeof(MI_WEIGHT)-1)
#define MI_RECHECK_TICKS "recheck_ticks"
#define MI_RECHECK_T_LEN (sizeof(MI_RECHECK_TICKS)-1)
/* Supported version of the RTP proxy command protocol */
#define SUP_CPROTOVER 20040107
/* Required additional version of the RTP proxy command protocol */
#define REQ_CPROTOVER "20050322"
/* Additional version necessary for re-packetization support */
#define REP_CPROTOVER "20071116"
#define PTL_CPROTOVER "20081102"
#define CPORT "22222"
static int nat_uac_test_f(struct sip_msg* msg, char* str1, char* str2);
static int fix_nated_contact_f(struct sip_msg *, char *, char *);
static int add_contact_alias_0_f(struct sip_msg *, char *, char *);
static int add_contact_alias_3_f(struct sip_msg *, char *, char *, char *);
static int set_contact_alias_f(struct sip_msg* msg, char* str1, char* str2);
static int handle_ruri_alias_f(struct sip_msg *, char *, char *);
static int pv_get_rr_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
static int pv_get_rr_top_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
static int fix_nated_sdp_f(struct sip_msg *, char *, char *);
static int is_rfc1918_f(struct sip_msg *, char *, char *);
static int extract_mediaip(str *, str *, int *, char *);
static int alter_mediaip(struct sip_msg *, str *, str *, int, str *, int, int);
static int fix_nated_register_f(struct sip_msg *, char *, char *);
static int fixup_fix_nated_register(void** param, int param_no);
static int fixup_fix_sdp(void** param, int param_no);
static int fixup_add_contact_alias(void** param, int param_no);
static int add_rcv_param_f(struct sip_msg *, char *, char *);
static int nh_sip_reply_received(sip_msg_t *msg);
static void nh_timer(unsigned int, void *);
static int mod_init(void);
static int child_init(int);
static void mod_destroy(void);
/*mi commands*/
static struct mi_root* mi_enable_natping(struct mi_root* cmd_tree,
void* param );
static usrloc_api_t ul;
static int cblen = 0;
static int natping_interval = 0;
struct socket_info* force_socket = 0;
static struct {
const char *cnetaddr;
uint32_t netaddr;
uint32_t mask;
} nets_1918[] = {
{"10.0.0.0", 0, 0xffffffffu << 24},
{"172.16.0.0", 0, 0xffffffffu << 20},
{"192.168.0.0", 0, 0xffffffffu << 16},
{"100.64.0.0", 0, 0xffffffffu << 22}, /* rfc6598 - cg-nat */
{NULL, 0, 0}
};
/*
* If this parameter is set then the natpinger will ping only contacts
* that have the NAT flag set in user location database
*/
static int ping_nated_only = 0;
static const char sbuf[4] = {0, 0, 0, 0};
static str force_socket_str = STR_NULL;
static pid_t mypid;
static int sipping_flag = -1;
static int natping_disable_flag = -1;
static int natping_processes = 1;
static str nortpproxy_str = str_init("a=nortpproxy:yes");
static char* rcv_avp_param = NULL;
static unsigned short rcv_avp_type = 0;
static int_str rcv_avp_name;
static char *natping_socket = 0;
static int udpping_from_path = 0;
static int raw_sock = -1;
static unsigned int raw_ip = 0;
static unsigned short raw_port = 0;
static int nh_keepalive_timeout = 0;
static request_method_t sipping_method_id = 0;
/*0-> disabled, 1 ->enabled*/
unsigned int *natping_state=0;
static cmd_export_t cmds[] = {
{"fix_nated_contact", (cmd_function)fix_nated_contact_f, 0,
0, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"add_contact_alias", (cmd_function)add_contact_alias_0_f, 0,
0, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"add_contact_alias", (cmd_function)add_contact_alias_3_f, 3,
fixup_add_contact_alias, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
{"set_contact_alias", (cmd_function)set_contact_alias_f, 0,
0, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"handle_ruri_alias", (cmd_function)handle_ruri_alias_f, 0,
0, 0,
REQUEST_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"fix_nated_sdp", (cmd_function)fix_nated_sdp_f, 1,
fixup_fix_sdp, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"fix_nated_sdp", (cmd_function)fix_nated_sdp_f, 2,
fixup_fix_sdp, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"nat_uac_test", (cmd_function)nat_uac_test_f, 1,
fixup_uint_null, 0,
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"fix_nated_register", (cmd_function)fix_nated_register_f, 0,
fixup_fix_nated_register, 0,
REQUEST_ROUTE },
{"add_rcv_param", (cmd_function)add_rcv_param_f, 0,
0, 0,
REQUEST_ROUTE },
{"add_rcv_param", (cmd_function)add_rcv_param_f, 1,
fixup_uint_null, 0,
REQUEST_ROUTE },
{"is_rfc1918", (cmd_function)is_rfc1918_f, 1,
fixup_spve_null, 0,
ANY_ROUTE },
{0, 0, 0, 0, 0, 0}
};
static pv_export_t mod_pvs[] = {
{{"rr_count", (sizeof("rr_count")-1)}, /* number of records routes */
PVT_CONTEXT, pv_get_rr_count_f, 0, 0, 0, 0, 0},
{{"rr_top_count", (sizeof("rr_top_count")-1)}, /* number of topmost rrs */
PVT_CONTEXT, pv_get_rr_top_count_f, 0, 0, 0, 0, 0},
{{0, 0}, 0, 0, 0, 0, 0, 0, 0}
};
static param_export_t params[] = {
{"natping_interval", INT_PARAM, &natping_interval },
{"ping_nated_only", INT_PARAM, &ping_nated_only },
{"nortpproxy_str", PARAM_STR, &nortpproxy_str },
{"received_avp", PARAM_STRING, &rcv_avp_param },
{"force_socket", PARAM_STR, &force_socket_str },
{"sipping_from", PARAM_STR, &sipping_from },
{"sipping_method", PARAM_STR, &sipping_method },
{"sipping_bflag", INT_PARAM, &sipping_flag },
{"natping_disable_bflag", INT_PARAM, &natping_disable_flag },
{"natping_processes", INT_PARAM, &natping_processes },
{"natping_socket", PARAM_STRING, &natping_socket },
{"keepalive_timeout", INT_PARAM, &nh_keepalive_timeout },
{"udpping_from_path", INT_PARAM, &udpping_from_path },
{0, 0, 0}
};
static mi_export_t mi_cmds[] = {
{MI_SET_NATPING_STATE, mi_enable_natping, 0, 0, 0},
{ 0, 0, 0, 0, 0}
};
struct module_exports exports = {
"nathelper",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds,
params,
0, /* exported statistics */
mi_cmds, /* exported MI functions */
mod_pvs, /* exported pseudo-variables */
0, /* extra processes */
mod_init,
nh_sip_reply_received, /* reply processing */
mod_destroy, /* destroy function */
child_init
};
static int
sel_nathelper(str* res, select_t* s, struct sip_msg* msg)
{
/* dummy */
return 0;
}
static int sel_rewrite_contact(str* res, select_t* s, struct sip_msg* msg);
SELECT_F(select_any_nameaddr)
select_row_t sel_declaration[] = {
{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("nathelper"), sel_nathelper, SEL_PARAM_EXPECTED},
{ sel_nathelper, SEL_PARAM_STR, STR_STATIC_INIT("rewrite_contact"), sel_rewrite_contact, CONSUME_NEXT_INT },
{ sel_rewrite_contact, SEL_PARAM_STR, STR_STATIC_INIT("nameaddr"), select_any_nameaddr, NESTED | CONSUME_NEXT_STR},
{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
};
static int
fixup_fix_sdp(void** param, int param_no)
{
pv_elem_t *model;
str s;
if (param_no==1) {
/* flags */
return fixup_uint_null( param, param_no);
}
/* new IP */
model=NULL;
s.s = (char*)(*param); s.len = strlen(s.s);
if(pv_parse_format(&s,&model)<0) {
LM_ERR("wrong format[%s]!\n", (char*)(*param));
return E_UNSPEC;
}
if (model==NULL) {
LM_ERR("empty parameter!\n");
return E_UNSPEC;
}
*param = (void*)model;
return 0;
}
static int fixup_fix_nated_register(void** param, int param_no)
{
if (rcv_avp_name.n == 0) {
LM_ERR("you must set 'received_avp' parameter. Must be same value as"
" parameter 'received_avp' of registrar module\n");
return -1;
}
return 0;
}
static int fixup_add_contact_alias(void** param, int param_no)
{
if ((param_no >= 1) && (param_no <= 3)) return fixup_spve_null(param, 1);
LM_ERR("invalid parameter number <%d>\n", param_no);
return -1;
}
static struct mi_root* mi_enable_natping(struct mi_root* cmd_tree,
void* param )
{
unsigned int value;
struct mi_node* node;
if (natping_state==NULL)
return init_mi_tree( 400, MI_PING_DISABLED, MI_PING_DISABLED_LEN);
node = cmd_tree->node.kids;
if(node == NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
value = 0;
if( strno2int( &node->value, &value) <0)
goto error;
(*natping_state) = value?1:0;
return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
error:
return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
}
static int init_raw_socket(void)
{
int on = 1;
raw_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (raw_sock ==-1) {
LM_ERR("cannot create raw socket\n");
return -1;
}
if (setsockopt(raw_sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) == -1) {
LM_ERR("cannot set socket options\n");
return -1;
}
return raw_sock;
}
static int get_natping_socket(char *socket,
unsigned int *ip, unsigned short *port)
{
struct hostent* he;
str host;
int lport;
int lproto;
if (parse_phostport( socket, &host.s, &host.len, &lport, &lproto)!=0){
LM_CRIT("invalid natping_socket parameter <%s>\n",natping_socket);
return -1;
}
if (lproto!=PROTO_UDP && lproto!=PROTO_NONE) {
LM_CRIT("natping_socket can be only UDP <%s>\n",natping_socket);
return 0;
}
lproto = PROTO_UDP;
*port = lport?(unsigned short)lport:SIP_PORT;
he = sip_resolvehost( &host, port, (char*)(void*)&lproto);
if (he==0) {
LM_ERR("could not resolve hostname:\"%.*s\"\n", host.len, host.s);
return -1;
}
if (he->h_addrtype != AF_INET) {
LM_ERR("only ipv4 addresses allowed in natping_socket\n");
return -1;
}
memcpy( ip, he->h_addr_list[0], he->h_length);
return 0;
}
static int
mod_init(void)
{
int i;
bind_usrloc_t bind_usrloc;
struct in_addr addr;
pv_spec_t avp_spec;
str s;
if(register_mi_mod(exports.name, mi_cmds)!=0)
{
LM_ERR("failed to register MI commands\n");
return -1;
}
if (rcv_avp_param && *rcv_avp_param) {
s.s = rcv_avp_param; s.len = strlen(s.s);
if (pv_parse_spec(&s, &avp_spec)==0
|| avp_spec.type!=PVT_AVP) {
LM_ERR("malformed or non AVP %s AVP definition\n", rcv_avp_param);
return -1;
}
if(pv_get_avp_name(0, &avp_spec.pvp, &rcv_avp_name, &rcv_avp_type)!=0)
{
LM_ERR("[%s]- invalid AVP definition\n", rcv_avp_param);
return -1;
}
} else {
rcv_avp_name.n = 0;
rcv_avp_type = 0;
}
if (force_socket_str.s && force_socket_str.len>0) {
force_socket=grep_sock_info(&force_socket_str,0,0);
}
/* create raw socket? */
if ((natping_socket && natping_socket[0]) || udpping_from_path) {
if ((!udpping_from_path) && get_natping_socket( natping_socket, &raw_ip, &raw_port)!=0)
return -1;
if (init_raw_socket() < 0)
return -1;
}
if (nortpproxy_str.s && nortpproxy_str.len>0) {
while (nortpproxy_str.len > 0 && (nortpproxy_str.s[nortpproxy_str.len - 1] == '\r' ||
nortpproxy_str.s[nortpproxy_str.len - 1] == '\n'))
nortpproxy_str.len--;
}
if (natping_interval > 0) {
bind_usrloc = (bind_usrloc_t)find_export("ul_bind_usrloc", 1, 0);
if (!bind_usrloc) {
LM_ERR("can't find usrloc module\n");
return -1;
}
if (bind_usrloc(&ul) < 0) {
return -1;
}
natping_state =(unsigned int *) shm_malloc(sizeof(unsigned int));
if (!natping_state) {
LM_ERR("no shmem left\n");
return -1;
}
*natping_state = MI_DEFAULT_NATPING_STATE;
if (ping_nated_only && ul.nat_flag==0) {
LM_ERR("bad config - ping_nated_only enabled, but no nat bflag"
" set in usrloc module\n");
return -1;
}
if (natping_processes<0) {
LM_ERR("bad config - natping_processes must be >= 0\n");
return -1;
}
sipping_flag = (sipping_flag==-1)?0:(1<<sipping_flag);
natping_disable_flag = (natping_disable_flag==-1)?0:(1<<natping_disable_flag);
/* set reply function if SIP natping is enabled */
if (sipping_flag) {
if (sipping_from.s==0 || sipping_from.len<=0) {
LM_ERR("SIP ping enabled, but SIP ping FROM is empty!\n");
return -1;
}
if (sipping_method.s==0 || sipping_method.len<=0) {
LM_ERR("SIP ping enabled, but SIP ping method is empty!\n");
return -1;
}
if(nh_keepalive_timeout>0 && ul.set_keepalive_timeout!=NULL) {
ul.set_keepalive_timeout(nh_keepalive_timeout);
}
if(parse_method_name(&sipping_method, &sipping_method_id) < 0) {
LM_ERR("invalid SIP ping method [%.*s]!\n", sipping_method.len,
sipping_method.s);
return -1;
}
exports.response_f = sipping_rpl_filter;
init_sip_ping();
}
register_dummy_timers(natping_processes);
}
/* Prepare 1918 networks list */
for (i = 0; nets_1918[i].cnetaddr != NULL; i++) {
if (inet_aton(nets_1918[i].cnetaddr, &addr) != 1)
abort();
nets_1918[i].netaddr = ntohl(addr.s_addr) & nets_1918[i].mask;
}
register_select_table(sel_declaration);
return 0;
}
static int
child_init(int rank)
{
int i;
if (rank==PROC_MAIN && natping_interval > 0) {
for( i=0 ; i<natping_processes ; i++ ) {
if(fork_dummy_timer(PROC_TIMER, "TIMER NH", 1 /*socks flag*/,
nh_timer, (void*)(unsigned long)i,
1 /*sec*/)<0) {
LM_ERR("failed to register timer routine as process\n");
return -1;
/* error */
}
}
}
if (rank<=0 && rank!=PROC_TIMER)
return 0;
/* Iterate known RTP proxies - create sockets */
mypid = getpid();
return 0;
}
static void mod_destroy(void)
{
/*free the shared memory*/
if (natping_state)
shm_free(natping_state);
}
static int
isnulladdr(str *sx, int pf)
{
char *cp;
if (pf == AF_INET6) {
for(cp = sx->s; cp < sx->s + sx->len; cp++)
if (*cp != '0' && *cp != ':')
return 0;
return 1;
}
return (sx->len == 7 && memcmp("0.0.0.0", sx->s, 7) == 0);
}
/*
* Replaces ip:port pair in the Contact: field with the source address
* of the packet.
*/
static int
fix_nated_contact_f(struct sip_msg* msg, char* str1, char* str2)
{
int offset, len, len1;
char *cp, *buf, temp[2];
contact_t *c;
struct lump *anchor;
struct sip_uri uri;
str hostport;
str params1 = {0};
str params2 = {0};
if (get_contact_uri(msg, &uri, &c) == -1)
return -1;
if ((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
LM_ERR("you can't call fix_nated_contact twice, "
"check your config!\n");
return -1;
}
offset = c->uri.s - msg->buf;
anchor = del_lump(msg, offset, c->uri.len, HDR_CONTACT_T);
if (anchor == 0)
return -1;
hostport = uri.host;
if (uri.port.len > 0)
hostport.len = uri.port.s + uri.port.len - uri.host.s;
cp = ip_addr2a(&msg->rcv.src_ip);
len = c->uri.len + strlen(cp) + 6 /* :port */ - hostport.len + 1;
buf = pkg_malloc(len);
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
temp[0] = hostport.s[0];
temp[1] = c->uri.s[c->uri.len];
c->uri.s[c->uri.len] = hostport.s[0] = '\0';
if(uri.maddr.len<=0) {
len1 = snprintf(buf, len, "%s%s:%d%s", c->uri.s, cp, msg->rcv.src_port,
hostport.s + hostport.len);
} else {
/* skip maddr parameter - makes no sense anymore */
LM_DBG("removing maddr parameter from contact uri: [%.*s]\n",
uri.maddr.len, uri.maddr.s);
params1.s = hostport.s + hostport.len;
params1.len = uri.maddr.s - params1.s;
while(params1.len>0
&& (params1.s[params1.len-1]==' '
|| params1.s[params1.len-1]=='\t'
|| params1.s[params1.len-1]==';'))
params1.len--;
params2.s = uri.maddr.s + uri.maddr.len;
params2.len = c->uri.s + c->uri.len - params2.s;
len1 = snprintf(buf, len, "%s%s:%d%.*s%.*s", c->uri.s, cp, msg->rcv.src_port,
params1.len, params1.s, params2.len, params2.s);
}
if (len1 < len)
len = len1;
hostport.s[0] = temp[0];
c->uri.s[c->uri.len] = temp[1];
if (insert_new_lump_after(anchor, buf, len, HDR_CONTACT_T) == 0) {
pkg_free(buf);
return -1;
}
c->uri.s = buf;
c->uri.len = len;
return 1;
}
/*
* Replaces ip:port pair in the Contact: field with the source address
* of the packet.
*/
static int
set_contact_alias_f(struct sip_msg* msg, char* str1, char* str2)
{
char nbuf[MAX_URI_SIZE];
str nuri;
int br;
int offset, len;
char *buf;
contact_t *c;
struct lump *anchor;
struct sip_uri uri;
nuri.s = nbuf;
nuri.len = MAX_URI_SIZE;
if (get_contact_uri(msg, &uri, &c) == -1)
return -1;
if ((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
LM_ERR("you can't update contact twice, check your config!\n");
return -1;
}
if(uri_add_rcv_alias(msg, &c->uri, &nuri)<0) {
LM_DBG("cannot add the alias parameter\n");
return -1;
}
br = 1;
if(c->uri.s[-1]=='<')
br = 0;
len = nuri.len + 2*br;
buf = pkg_malloc(len + 1);
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
if(br==1) {
buf[0] = '<';
strncpy(buf+1, nuri.s, nuri.len);
buf[len-1] = '>';
} else {
strncpy(buf, nuri.s, nuri.len);
}
buf[len] = '\0';
offset = c->uri.s - msg->buf;
anchor = del_lump(msg, offset, c->uri.len, HDR_CONTACT_T);
if (anchor == 0) {
pkg_free(buf);
return -1;
}
if (insert_new_lump_after(anchor, buf, len, HDR_CONTACT_T) == 0) {
pkg_free(buf);
return -1;
}
c->uri.s = buf;
c->uri.len = len;
return 1;
}
#define SALIAS ";alias="
#define SALIAS_LEN (sizeof(SALIAS) - 1)
/*
* Adds ;alias=ip~port~proto param to contact uri containing received ip,
* port, and transport proto if contact uri ip and port do not match
* received ip and port.
*/
static int
add_contact_alias_0_f(struct sip_msg* msg, char* str1, char* str2)
{
int len, param_len, ip_len;
contact_t *c;
struct lump *anchor;
struct sip_uri uri;
struct ip_addr *ip;
char *bracket, *lt, *param, *at, *port, *start;
/* Do nothing if Contact header does not exist */
if (!msg->contact) {
if (parse_headers(msg, HDR_CONTACT_F, 0) == -1) {
LM_ERR("while parsing headers\n");
return -1;
}
if (!msg->contact) {
LM_DBG("no contact header\n");
return 2;
}
}
if (get_contact_uri(msg, &uri, &c) == -1) {
LM_ERR("failed to get contact uri\n");
return -1;
}
/* Compare source ip and port against contact uri */
if (((ip = str2ip(&(uri.host))) == NULL) &&
((ip = str2ip6(&(uri.host))) == NULL)) {
LM_DBG("contact uri host is not an ip address\n");
} else {
if (ip_addr_cmp(ip, &(msg->rcv.src_ip)) &&
((msg->rcv.src_port == uri.port_no) ||
((uri.port.len == 0) && (msg->rcv.src_port == 5060)))) {
LM_DBG("no need to add alias param\n");
return 2;
}
}
/* Check if function has been called already */
if ((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
LM_ERR("you can't call add_contact_alias twice, check your config!\n");
return -1;
}
/* Check if Contact URI needs to be enclosed in <>s */
lt = param = NULL;
bracket = memchr(msg->contact->body.s, '<', msg->contact->body.len);
if (bracket == NULL) {
/* add opening < */
lt = (char*)pkg_malloc(1);
if (!lt) {
LM_ERR("no pkg memory left for lt sign\n");
goto err;
}
*lt = '<';
anchor = anchor_lump(msg, msg->contact->body.s - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump for beginning of contact body failed\n");
goto err;
}
if (insert_new_lump_before(anchor, lt, 1, 0) == 0) {
LM_ERR("insert_new_lump_before for \"<\" failed\n");
goto err;
}
}
/* Create ;alias param */
param_len = SALIAS_LEN + 1 /* [ */ + IP6_MAX_STR_SIZE + 1 /* ] */ +
1 /* ~ */ + 5 /* port */ + 1 /* ~ */ + 1 /* proto */ + 1 /* > */;
param = (char*)pkg_malloc(param_len);
if (!param) {
LM_ERR("no pkg memory left for alias param\n");
goto err;
}
at = param;
/* ip address */
append_str(at, SALIAS, SALIAS_LEN);
if (msg->rcv.src_ip.af == AF_INET6)
append_chr(at, '[');
ip_len = ip_addr2sbuf(&(msg->rcv.src_ip), at, param_len - SALIAS_LEN);
if (ip_len <= 0) {
LM_ERR("failed to copy source ip\n");
goto err;
}
at = at + ip_len;
if (msg->rcv.src_ip.af == AF_INET6)
append_chr(at, ']');
/* port */
append_chr(at, '~');
port = int2str(msg->rcv.src_port, &len);
append_str(at, port, len);
/* proto */
append_chr(at, '~');
if ((msg->rcv.proto < PROTO_UDP) || (msg->rcv.proto > PROTO_WSS)) {
LM_ERR("invalid transport protocol\n");
goto err;
}
append_chr(at, msg->rcv.proto + '0');
/* closing > */
if (bracket == NULL) {
append_chr(at, '>');
}
param_len = at - param;
/* Add ;alias param */
LM_DBG("adding param <%.*s>\n", param_len, param);
if (uri.port.len > 0) {
start = uri.port.s + uri.port.len;
} else {
start = uri.host.s + uri.host.len;
}
anchor = anchor_lump(msg, start - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump for ;alias param failed\n");
goto err;
}
if (insert_new_lump_after(anchor, param, param_len, 0) == 0) {
LM_ERR("insert_new_lump_after for ;alias param failed\n");
goto err;
}
return 1;
err:
if (lt) pkg_free(lt);
if (param) pkg_free(param);
return -1;
}
static int proto_type_to_int(char *proto) {
if (strcasecmp(proto, "udp") == 0)
return PROTO_UDP;
if (strcasecmp(proto, "tcp") == 0)
return PROTO_TCP;
if (strcasecmp(proto, "tls") == 0)
return PROTO_TLS;
if (strcasecmp(proto, "sctp") == 0)
return PROTO_SCTP;
if (strcasecmp(proto, "ws") == 0)
return PROTO_WS;
if (strcasecmp(proto, "wss") == 0)
return PROTO_WSS;
return PROTO_OTHER;
}
/*
* Adds ;alias=ip~port~proto param to contact uri containing ip, port,
* and encoded proto given as parameters.
*/
static int
add_contact_alias_3_f(struct sip_msg* msg, char* _ip, char* _port, char* _proto)
{
int param_len, proto;
unsigned int tmp;
contact_t *c;
struct lump *anchor;
struct sip_uri uri;
char *bracket, *lt, *param, *at, *start;
str ip_str, port_str, proto_str;
/* Do nothing if Contact header does not exist */
if (!msg->contact) {
if (parse_headers(msg, HDR_CONTACT_F, 0) == -1) {
LM_ERR("while parsing headers\n");
return -1;
}
if (!msg->contact) {
LM_DBG("no contact header\n");
return 2;
}
}
if (get_contact_uri(msg, &uri, &c) == -1) {
LM_ERR("failed to get contact uri\n");
return -1;
}
/* Get and check param values */
if (fixup_get_svalue(msg, (gparam_p)_ip, &ip_str) != 0) {
LM_ERR("cannot get ip param value\n");
return -1;
}
if ((str2ip(&ip_str) == NULL)
&& (str2ip6(&ip_str) == NULL)
) {
LM_ERR("ip param value %s is not valid IP address\n", ip_str.s);
return -1;
}
if (fixup_get_svalue(msg, (gparam_p)_port, &port_str) != 0) {
LM_ERR("cannot get port param value\n");
return -1;
}
if ((str2int(&port_str, &tmp) == -1) || (tmp == 0) || (tmp > 65535)) {
LM_ERR("port param value is not valid port\n");
return -1;
}
if (fixup_get_svalue(msg, (gparam_p)_proto, &proto_str) != 0) {
LM_ERR("cannot get proto param value\n");
return -1;
}
proto = proto_type_to_int(proto_str.s);
if (proto == PROTO_OTHER) {
LM_ERR("proto param value %s is not a known protocol\n", proto_str.s);
return -1;
}
/* Check if function has been called already */
if ((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
LM_ERR("you can't call alias_contact twice, check your config!\n");
return -1;
}
/* Check if Contact URI needs to be enclosed in <>s */
lt = param = NULL;
bracket = memchr(msg->contact->body.s, '<', msg->contact->body.len);
if (bracket == NULL) {
/* add opening < */
lt = (char*)pkg_malloc(1);
if (!lt) {
LM_ERR("no pkg memory left for lt sign\n");
goto err;
}
*lt = '<';
anchor = anchor_lump(msg, msg->contact->body.s - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump for beginning of contact body failed\n");
goto err;
}
if (insert_new_lump_before(anchor, lt, 1, 0) == 0) {
LM_ERR("insert_new_lump_before for \"<\" failed\n");
goto err;
}
}
/* Create ;alias param */
param_len = SALIAS_LEN + IP6_MAX_STR_SIZE + 1 /* ~ */ + 5 /* port */ +
1 /* ~ */ + 1 /* proto */ + 1 /* closing > */;
param = (char*)pkg_malloc(param_len);
if (!param) {
LM_ERR("no pkg memory left for alias param\n");
goto err;
}
at = param;
/* ip address */
append_str(at, SALIAS, SALIAS_LEN);
append_str(at, ip_str.s, ip_str.len);
/* port */
append_chr(at, '~');
append_str(at, port_str.s, port_str.len);
/* proto */
append_chr(at, '~');
append_chr(at, proto + '0');
/* closing > */
if (bracket == NULL) {
append_chr(at, '>');
}
param_len = at - param;
/* Add ;alias param */
LM_DBG("adding param <%.*s>\n", param_len, param);
if (uri.port.len > 0) {
start = uri.port.s + uri.port.len;
} else {
start = uri.host.s + uri.host.len;
}
anchor = anchor_lump(msg, start - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump for ;alias param failed\n");
goto err;
}
if (insert_new_lump_after(anchor, param, param_len, 0) == 0) {
LM_ERR("insert_new_lump_after for ;alias param failed\n");
goto err;
}
return 1;
err:
if (lt) pkg_free(lt);
if (param) pkg_free(param);
return -1;
}
#define ALIAS "alias="
#define ALIAS_LEN (sizeof(ALIAS) - 1)
/*
* Checks if r-uri has alias param and if so, removes it and sets $du
* based on its value.
*/
static int
handle_ruri_alias_f(struct sip_msg* msg, char* str1, char* str2)
{
str uri, proto;
char buf[MAX_URI_SIZE], *val, *sep, *at, *next, *cur_uri, *rest,
*port, *trans;
unsigned int len, rest_len, val_len, alias_len, proto_type, cur_uri_len,
ip_port_len;
if (parse_sip_msg_uri(msg) < 0) {
LM_ERR("while parsing Request-URI\n");
return -1;
}
rest = msg->parsed_uri.sip_params.s;
rest_len = msg->parsed_uri.sip_params.len;
if (rest_len == 0) {
LM_DBG("no params\n");
return 2;
}
while (rest_len >= ALIAS_LEN) {
if (strncmp(rest, ALIAS, ALIAS_LEN) == 0) break;
sep = memchr(rest, 59 /* ; */, rest_len);
if (sep == NULL) {
LM_DBG("no alias param\n");
return 2;
} else {
rest_len = rest_len - (sep - rest + 1);
rest = sep + 1;
}
}
if (rest_len < ALIAS_LEN) {
LM_DBG("no alias param\n");
return 2;
}
/* set dst uri based on alias param value */
val = rest + ALIAS_LEN;
val_len = rest_len - ALIAS_LEN;
port = memchr(val, 126 /* ~ */, val_len);
if (port == NULL) {
LM_ERR("no '~' in alias param value\n");
return -1;
}
*(port++) = ':';
trans = memchr(port, 126 /* ~ */, val_len - (port - val));
if (trans == NULL) {
LM_ERR("no second '~' in alias param value\n");
return -1;
}
at = &(buf[0]);
append_str(at, "sip:", 4);
ip_port_len = trans - val;
alias_len = SALIAS_LEN + ip_port_len + 2 /* ~n */;
memcpy(at, val, ip_port_len);
at = at + ip_port_len;
trans = trans + 1;
if ((ip_port_len + 2 > val_len) || (*trans == ';') || (*trans == '?')) {
LM_ERR("no proto in alias param\n");
return -1;
}
proto_type = *trans - 48 /* char 0 */;
if (proto_type != PROTO_UDP) {
proto_type_to_str(proto_type, &proto);
if (proto.len == 0) {
LM_ERR("unknown proto in alias param\n");
return -1;
}
append_str(at, ";transport=", 11);
memcpy(at, proto.s, proto.len);
at = at + proto.len;
}
next = trans + 1;
if ((ip_port_len + 2 < val_len) && (*next != ';') && (*next != '?')) {
LM_ERR("invalid alias param value\n");
return -1;
}
uri.s = &(buf[0]);
uri.len = at - &(buf[0]);
LM_DBG("setting dst_uri to <%.*s>\n", uri.len, uri.s);
if (set_dst_uri(msg, &uri) == -1) {
LM_ERR("failed to set dst uri\n");
return -1;
}
/* remove alias param */
if (msg->new_uri.s) {
cur_uri = msg->new_uri.s;
cur_uri_len = msg->new_uri.len;
} else {
cur_uri = msg->first_line.u.request.uri.s;
cur_uri_len = msg->first_line.u.request.uri.len;
}
at = &(buf[0]);
len = rest - 1 /* ; */ - cur_uri;
memcpy(at, cur_uri, len);
at = at + len;
len = cur_uri_len - alias_len - len;
memcpy(at, rest + alias_len - 1, len);
uri.s = &(buf[0]);
uri.len = cur_uri_len - alias_len;
LM_DBG("rewriting r-uri to <%.*s>\n", uri.len, uri.s);
return rewrite_uri(msg, &uri);
}
/*
* Counts and return the number of record routes in rr headers of the message.
*/
static int
pv_get_rr_count_f(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
unsigned int count;
struct hdr_field *header;
rr_t *body;
if (msg == NULL) return -1;
if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
LM_ERR("while parsing message\n");
return -1;
}
count = 0;
header = msg->record_route;
while (header) {
if (header->type == HDR_RECORDROUTE_T) {
if (parse_rr(header) == -1) {
LM_ERR("while parsing rr header\n");
return -1;
}
body = (rr_t *)header->parsed;
while (body) {
count++;
body = body->next;
}
}
header = header->next;
}
return pv_get_uintval(msg, param, res, (unsigned int)count);
}
/*
* Return count of topmost record routes in rr headers of the message.
*/
static int
pv_get_rr_top_count_f(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
{
str uri;
struct sip_uri puri;
if (msg == NULL) return -1;
if (!msg->record_route &&
(parse_headers(msg, HDR_RECORDROUTE_F, 0) == -1)) {
LM_ERR("while parsing Record-Route header\n");
return -1;
}
if (!msg->record_route) {
return pv_get_uintval(msg, param, res, 0);
}
if (parse_rr(msg->record_route) == -1) {
LM_ERR("while parsing rr header\n");
return -1;
}
uri = ((rr_t *)msg->record_route->parsed)->nameaddr.uri;
if (parse_uri(uri.s, uri.len, &puri) < 0) {
LM_ERR("while parsing rr uri\n");
return -1;
}
if (puri.r2.len > 0) {
return pv_get_uintval(msg, param, res, 2);
} else {
return pv_get_uintval(msg, param, res, 1);
}
}
/*
* Test if IP address in netaddr belongs to RFC1918 networks
* netaddr in network byte order
*/
static inline int
is1918addr_n(uint32_t netaddr)
{
int i;
uint32_t hl;
hl = ntohl(netaddr);
for (i = 0; nets_1918[i].cnetaddr != NULL; i++) {
if ((hl & nets_1918[i].mask) == nets_1918[i].netaddr) {
return 1;
}
}
return 0;
}
/*
* Test if IP address pointed to by saddr belongs to RFC1918 networks
*/
static inline int
is1918addr(str *saddr)
{
struct in_addr addr;
int rval;
char backup;
rval = -1;
backup = saddr->s[saddr->len];
saddr->s[saddr->len] = '\0';
if (inet_aton(saddr->s, &addr) != 1)
goto theend;
rval = is1918addr_n(addr.s_addr);
theend:
saddr->s[saddr->len] = backup;
return rval;
}
/*
* Test if IP address pointed to by ip belongs to RFC1918 networks
*/
static inline int
is1918addr_ip(struct ip_addr *ip)
{
if (ip->af != AF_INET)
return 0;
return is1918addr_n(ip->u.addr32[0]);
}
/*
* test for occurrence of RFC1918 IP address in Contact HF
*/
static int
contact_1918(struct sip_msg* msg)
{
struct sip_uri uri;
contact_t* c;
if (get_contact_uri(msg, &uri, &c) == -1)
return -1;
return (is1918addr(&(uri.host)) == 1) ? 1 : 0;
}
/*
* test if source port of signaling is different from
* port advertised in Contact
*/
static int
contact_rport(struct sip_msg* msg)
{
struct sip_uri uri;
contact_t* c;
if (get_contact_uri(msg, &uri, &c) == -1) {
return -1;
}
if (msg->rcv.src_port != (uri.port_no ? uri.port_no : SIP_PORT)) {
return 1;
} else {
return 0;
}
}
/*
* test for occurrence of RFC1918 IP address in SDP
*/
static int
sdp_1918(struct sip_msg* msg)
{
str *ip;
int pf;
int ret;
int sdp_session_num, sdp_stream_num;
sdp_session_cell_t* sdp_session;
sdp_stream_cell_t* sdp_stream;
ret = parse_sdp(msg);
if(ret != 0) {
if(ret < 0)
LM_ERR("Unable to parse sdp\n");
return 0;
}
sdp_session_num = 0;
for(;;) {
sdp_session = get_sdp_session(msg, sdp_session_num);
if(!sdp_session) break;
sdp_stream_num = 0;
for(;;) {
sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
if(!sdp_stream) break;
if (sdp_stream->ip_addr.s && sdp_stream->ip_addr.len) {
ip = &(sdp_stream->ip_addr);
pf = sdp_stream->pf;
} else {
ip = &(sdp_session->ip_addr);
pf = sdp_session->pf;
}
if (pf != AF_INET || isnulladdr(ip, pf))
break;
if (is1918addr(ip) == 1)
return 1;
sdp_stream_num++;
}
sdp_session_num++;
}
return 0;
}
/*
* test for occurrence of RFC1918 IP address in top Via
*/
static int
via_1918(struct sip_msg* msg)
{
return (is1918addr(&(msg->via1->host)) == 1) ? 1 : 0;
}
static int
nat_uac_test_f(struct sip_msg* msg, char* str1, char* str2)
{
int tests;
tests = (int)(long)str1;
/* return true if any of the NAT-UAC tests holds */
/* test if the source port is different from the port in Via */
if ((tests & NAT_UAC_TEST_RPORT) &&
(msg->rcv.src_port!=(msg->via1->port?msg->via1->port:SIP_PORT)) ){
return 1;
}
/*
* test if source address of signaling is different from
* address advertised in Via
*/
if ((tests & NAT_UAC_TEST_RCVD) && received_test(msg))
return 1;
/*
* test for occurrences of RFC1918 addresses in Contact
* header field
*/
if ((tests & NAT_UAC_TEST_C_1918) && (contact_1918(msg)>0))
return 1;
/*
* test for occurrences of RFC1918 addresses in SDP body
*/
if ((tests & NAT_UAC_TEST_S_1918) && sdp_1918(msg))
return 1;
/*
* test for occurrences of RFC1918 addresses top Via
*/
if ((tests & NAT_UAC_TEST_V_1918) && via_1918(msg))
return 1;
/*
* test for occurrences of RFC1918 addresses in source address
*/
if ((tests & NAT_UAC_TEST_O_1918) && is1918addr_ip(&msg->rcv.src_ip))
return 1;
/*
* test prototype to check whether the message arrived on a WebSocket
*/
if ((tests & NAT_UAC_TEST_WS)
&& (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS))
return 1;
/*
* test if source port of signaling is different from
* port advertised in Contact
*/
if ((tests & NAT_UAC_TEST_C_PORT) && (contact_rport(msg) > 0))
return 1;
/* no test succeeded */
return -1;
}
static int
is_rfc1918_f(struct sip_msg* msg, char* str1, char* str2)
{
str address;
if(fixup_get_svalue(msg, (gparam_p)str1, &address)!=0 || address.len==0)
{
LM_ERR("invalid address parameter\n");
return -2;
}
return (is1918addr(&address) == 1) ? 1 : -1;
}
#define ADD_ADIRECTION 0x01
#define FIX_MEDIP 0x02
#define ADD_ANORTPPROXY 0x04
#define FIX_ORGIP 0x08
#define ADIRECTION "a=direction:active"
#define ADIRECTION_LEN (sizeof(ADIRECTION) - 1)
#define AOLDMEDIP "a=oldmediaip:"
#define AOLDMEDIP_LEN (sizeof(AOLDMEDIP) - 1)
#define AOLDMEDIP6 "a=oldmediaip6:"
#define AOLDMEDIP6_LEN (sizeof(AOLDMEDIP6) - 1)
#define AOLDMEDPRT "a=oldmediaport:"
#define AOLDMEDPRT_LEN (sizeof(AOLDMEDPRT) - 1)
/* replace ip addresses in SDP and return umber of replacements */
static inline int
replace_sdp_ip(struct sip_msg* msg, str *org_body, char *line, str *ip)
{
str body1, oldip, newip;
str body = *org_body;
unsigned hasreplaced = 0;
int pf, pf1 = 0;
str body2;
char *bodylimit = body.s + body.len;
int ret;
int count = 0;
/* Iterate all lines and replace ips in them. */
if (!ip) {
newip.s = ip_addr2a(&msg->rcv.src_ip);
newip.len = strlen(newip.s);
} else {
newip = *ip;
}
body1 = body;
for(;;) {
if (extract_mediaip(&body1, &oldip, &pf,line) == -1)
break;
if (pf != AF_INET) {
LM_ERR("not an IPv4 address in '%s' SDP\n",line);
return -1;
}
if (!pf1)
pf1 = pf;
else if (pf != pf1) {
LM_ERR("mismatching address families in '%s' SDP\n",line);
return -1;
}
body2.s = oldip.s + oldip.len;
body2.len = bodylimit - body2.s;
ret = alter_mediaip(msg, &body1, &oldip, pf, &newip, pf,1);
if (ret == -1) {
LM_ERR("can't alter '%s' IP\n",line);
return -1;
}
count += ret;
hasreplaced = 1;
body1 = body2;
}
if (!hasreplaced) {
LM_ERR("can't extract '%s' IP from the SDP\n",line);
return -1;
}
return count;
}
static int
fix_nated_sdp_f(struct sip_msg* msg, char* str1, char* str2)
{
str body;
str ip;
int level, rest_len;
char *buf, *m_start, *m_end;
struct lump* anchor;
int ret;
int count = 0;
level = (int)(long)str1;
if (str2 && pv_printf_s( msg, (pv_elem_p)str2, &ip)!=0)
return -1;
if (extract_body(msg, &body) == -1) {
LM_ERR("cannot extract body from msg!\n");
return -1;
}
if (level & (ADD_ADIRECTION | ADD_ANORTPPROXY)) {
msg->msg_flags |= FL_FORCE_ACTIVE;
if (level & ADD_ADIRECTION) {
m_start = ser_memmem(body.s, "\r\nm=", body.len, 4);
while (m_start != NULL) {
m_start += 4;
rest_len = body.len - (m_start - body.s);
m_start = m_end = ser_memmem(m_start, "\r\nm=", rest_len, 4);
if (!m_end)
m_end = body.s + body.len; /* just before the final \r\n */
anchor = anchor_lump(msg, m_end - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump failed\n");
return -1;
}
buf = pkg_malloc((ADIRECTION_LEN + CRLF_LEN) * sizeof(char));
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, CRLF, CRLF_LEN);
memcpy(buf + CRLF_LEN, ADIRECTION, ADIRECTION_LEN);
if (insert_new_lump_after(anchor, buf, ADIRECTION_LEN + CRLF_LEN, 0) == NULL) {
LM_ERR("insert_new_lump_after failed\n");
pkg_free(buf);
return -1;
}
}
}
if ((level & ADD_ANORTPPROXY) && nortpproxy_str.len) {
anchor = anchor_lump(msg, body.s + body.len - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump failed\n");
return -1;
}
buf = pkg_malloc((nortpproxy_str.len + CRLF_LEN) * sizeof(char));
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, CRLF, CRLF_LEN);
memcpy(buf + CRLF_LEN, nortpproxy_str.s, nortpproxy_str.len);
if (insert_new_lump_after(anchor, buf, nortpproxy_str.len + CRLF_LEN, 0) == NULL) {
LM_ERR("insert_new_lump_after failed\n");
pkg_free(buf);
return -1;
}
}
}
if (level & FIX_MEDIP) {
/* Iterate all c= and replace ips in them. */
ret = replace_sdp_ip(msg, &body, "c=", str2?&ip:0);
if (ret==-1)
return -1;
count += ret;
}
if (level & FIX_ORGIP) {
/* Iterate all o= and replace ips in them. */
ret = replace_sdp_ip(msg, &body, "o=", str2?&ip:0);
if (ret==-1)
return -1;
count += ret;
}
return count > 0 ? 1 : 2;
}
static int
extract_mediaip(str *body, str *mediaip, int *pf, char *line)
{
char *cp, *cp1;
int len, nextisip;
cp1 = NULL;
for (cp = body->s; (len = body->s + body->len - cp) > 0;) {
cp1 = ser_memmem(cp, line, len, 2);
if (cp1 == NULL || cp1[-1] == '\n' || cp1[-1] == '\r')
break;
cp = cp1 + 2;
}
if (cp1 == NULL)
return -1;
mediaip->s = cp1 + 2;
mediaip->len = eat_line(mediaip->s, body->s + body->len - mediaip->s) - mediaip->s;
trim_len(mediaip->len, mediaip->s, *mediaip);
nextisip = 0;
for (cp = mediaip->s; cp < mediaip->s + mediaip->len;) {
len = eat_token_end(cp, mediaip->s + mediaip->len) - cp;
if (nextisip == 1) {
mediaip->s = cp;
mediaip->len = len;
nextisip++;
break;
}
if (len == 3 && memcmp(cp, "IP", 2) == 0) {
switch (cp[2]) {
case '4':
nextisip = 1;
*pf = AF_INET;
break;
case '6':
nextisip = 1;
*pf = AF_INET6;
break;
default:
break;
}
}
cp = eat_space_end(cp + len, mediaip->s + mediaip->len);
}
if (nextisip != 2 || mediaip->len == 0) {
LM_ERR("no `IP[4|6]' in `%s' field\n",line);
return -1;
}
return 1;
}
static int
alter_mediaip(struct sip_msg *msg, str *body, str *oldip, int oldpf,
str *newip, int newpf, int preserve)
{
char *buf;
int offset;
struct lump* anchor;
str omip, nip, oip;
/* check that updating mediaip is really necessary */
if (oldpf == newpf && isnulladdr(oldip, oldpf))
return 0;
if (newip->len == oldip->len &&
memcmp(newip->s, oldip->s, newip->len) == 0)
return 0;
if (preserve != 0) {
anchor = anchor_lump(msg, body->s + body->len - msg->buf, 0, 0);
if (anchor == NULL) {
LM_ERR("anchor_lump failed\n");
return -1;
}
if (oldpf == AF_INET6) {
omip.s = AOLDMEDIP6;
omip.len = AOLDMEDIP6_LEN;
} else {
omip.s = AOLDMEDIP;
omip.len = AOLDMEDIP_LEN;
}
buf = pkg_malloc(omip.len + oldip->len + CRLF_LEN);
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, CRLF, CRLF_LEN);
memcpy(buf + CRLF_LEN, omip.s, omip.len);
memcpy(buf + CRLF_LEN + omip.len, oldip->s, oldip->len);
if (insert_new_lump_after(anchor, buf,
omip.len + oldip->len + CRLF_LEN, 0) == NULL) {
LM_ERR("insert_new_lump_after failed\n");
pkg_free(buf);
return -1;
}
}
if (oldpf == newpf) {
nip.len = newip->len;
nip.s = pkg_malloc(nip.len);
if (nip.s == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(nip.s, newip->s, newip->len);
} else {
nip.len = newip->len + 2;
nip.s = pkg_malloc(nip.len);
if (nip.s == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(nip.s + 2, newip->s, newip->len);
nip.s[0] = (newpf == AF_INET6) ? '6' : '4';
nip.s[1] = ' ';
}
oip = *oldip;
if (oldpf != newpf) {
do {
oip.s--;
oip.len++;
} while (*oip.s != '6' && *oip.s != '4');
}
offset = oip.s - msg->buf;
anchor = del_lump(msg, offset, oip.len, 0);
if (anchor == NULL) {
LM_ERR("del_lump failed\n");
pkg_free(nip.s);
return -1;
}
if (insert_new_lump_after(anchor, nip.s, nip.len, 0) == 0) {
LM_ERR("insert_new_lump_after failed\n");
pkg_free(nip.s);
return -1;
}
return 1;
}
static u_short raw_checksum(unsigned char *buffer, int len)
{
u_long sum = 0;
while (len > 1) {
sum += *buffer << 8;
buffer++;
sum += *buffer;
buffer++;
len -= 2;
}
if (len) {
sum += *buffer << 8;
}
sum = (sum >> 16) + (sum & 0xffff);
sum = (sum >> 16) + (sum);
return (u_short) ~sum;
}
static int send_raw(const char *buf, int buf_len, union sockaddr_union *to,
const unsigned int s_ip, const unsigned int s_port)
{
struct ip *ip;
struct udphdr *udp;
unsigned char packet[50];
int len = sizeof(struct ip) + sizeof(struct udphdr) + buf_len;
if (len > sizeof(packet)) {
LM_ERR("payload too big\n");
return -1;
}
ip = (struct ip*) packet;
udp = (struct udphdr *) (packet + sizeof(struct ip));
memcpy(packet + sizeof(struct ip) + sizeof(struct udphdr), buf, buf_len);
ip->ip_v = 4;
ip->ip_hl = sizeof(struct ip) / 4; // no options
ip->ip_tos = 0;
ip->ip_len = htons(len);
ip->ip_id = 23;
ip->ip_off = 0;
ip->ip_ttl = 69;
ip->ip_p = 17;
ip->ip_src.s_addr = s_ip;
ip->ip_dst.s_addr = to->sin.sin_addr.s_addr;
ip->ip_sum = raw_checksum((unsigned char *) ip, sizeof(struct ip));
udp->uh_sport = htons(s_port);
udp->uh_dport = to->sin.sin_port;
udp->uh_ulen = htons((unsigned short) sizeof(struct udphdr) + buf_len);
udp->uh_sum = 0;
return sendto(raw_sock, packet, len, 0, (struct sockaddr *) to, sizeof(struct sockaddr_in));
}
/**
* quick function to extract ip:port from path
*/
static char *extract_last_path_ip(str path)
{
/* used for raw UDP ping which works only on IPv4 */
static char ip[24];
char *start = NULL, *end = NULL, *p;
int i;
int path_depth = 0;
int max_path_depth;
max_path_depth = udpping_from_path - 1;
if (!path.len || !path.s) return NULL;
p = path.s;
for (i = 0; i < path.len; i++) {
if (!strncmp("<sip:", p, 5) && i < path.len - 4) {
start = p + 5;
end = NULL;
}
if ((*p == ';' || *p == '>') && !end) {
end = p;
if (max_path_depth) {
path_depth++;
if (path_depth >= max_path_depth) {
break;
}
}
}
p++;
}
if (start && end) {
int len = end - start;
if (len > sizeof(ip) -1) {
return NULL;
}
memcpy(ip, start, len);
ip[len] = '\0';
return (char *) ip;
} else {
return NULL;
}
}
static void
nh_timer(unsigned int ticks, void *timer_idx)
{
static unsigned int iteration = 0;
int rval;
void *buf, *cp;
str c;
str opt;
str path;
str ruid;
unsigned int aorhash;
struct sip_uri curi;
struct hostent* he;
struct socket_info* send_sock;
unsigned int flags;
char proto;
struct dest_info dst;
char *path_ip_str = NULL;
unsigned int path_ip = 0;
unsigned short path_port = 0;
if((*natping_state) == 0)
goto done;
buf = NULL;
if (cblen > 0) {
buf = pkg_malloc(cblen);
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
goto done;
}
}
rval = ul.get_all_ucontacts(buf, cblen, (ping_nated_only?ul.nat_flag:0),
((unsigned int)(unsigned long)timer_idx)*natping_interval+iteration,
natping_processes*natping_interval);
if (rval<0) {
LM_ERR("failed to fetch contacts\n");
goto done;
}
if (rval > 0) {
if (buf != NULL)
pkg_free(buf);
cblen = rval * 2;
buf = pkg_malloc(cblen);
if (buf == NULL) {
LM_ERR("out of pkg memory\n");
goto done;
}
rval = ul.get_all_ucontacts(buf,cblen,(ping_nated_only?ul.nat_flag:0),
((unsigned int)(unsigned long)timer_idx)*natping_interval+iteration,
natping_processes*natping_interval);
if (rval != 0) {
pkg_free(buf);
goto done;
}
}
if (buf == NULL)
goto done;
cp = buf;
while (1) {
memcpy(&(c.len), cp, sizeof(c.len));
if (c.len == 0)
break;
c.s = (char*)cp + sizeof(c.len);
cp = (char*)cp + sizeof(c.len) + c.len;
memcpy( &send_sock, cp, sizeof(send_sock));
cp = (char*)cp + sizeof(send_sock);
memcpy( &flags, cp, sizeof(flags));
cp = (char*)cp + sizeof(flags);
memcpy( &(path.len), cp, sizeof(path.len));
path.s = path.len ? ((char*)cp + sizeof(path.len)) : NULL ;
cp = (char*)cp + sizeof(path.len) + path.len;
memcpy( &(ruid.len), cp, sizeof(ruid.len));
ruid.s = ruid.len ? ((char*)cp + sizeof(ruid.len)) : NULL ;
cp = (char*)cp + sizeof(ruid.len) + ruid.len;
memcpy( &aorhash, cp, sizeof(aorhash));
cp = (char*)cp + sizeof(aorhash);
if ((flags & natping_disable_flag)) /* always 0 if natping_disable_flag not set */
continue;
/* determin the destination */
if ( path.len && (flags&sipping_flag)!=0 ) {
/* send to first URI in path */
if (get_path_dst_uri( &path, &opt) < 0) {
LM_ERR("failed to get dst_uri for Path\n");
continue;
}
/* send to the contact/received */
if (parse_uri(opt.s, opt.len, &curi) < 0) {
LM_ERR("can't parse contact dst_uri\n");
continue;
}
} else if (path.len && udpping_from_path) {
path_ip_str = extract_last_path_ip(path);
if (path_ip_str == NULL) {
LM_ERR( "ERROR:nathelper:nh_timer: unable to parse path from location\n");
continue;
}
if (get_natping_socket(path_ip_str, &path_ip, &path_port)) {
LM_ERR("could not parse path host for udpping_from_path\n");
continue;
}
if (parse_uri(c.s, c.len, &curi) < 0) {
LM_ERR("can't parse contact uri\n");
continue;
}
} else {
/* send to the contact/received */
if (parse_uri(c.s, c.len, &curi) < 0) {
LM_ERR("can't parse contact uri\n");
continue;
}
}
if (curi.proto != PROTO_UDP && curi.proto != PROTO_NONE)
continue;
if (curi.port_no == 0)
curi.port_no = SIP_PORT;
proto = curi.proto;
/* we sholud get rid of this resolve (to ofen and to slow); for the
* moment we are lucky since the curi is an IP -bogdan */
he = sip_resolvehost(&curi.host, &curi.port_no, &proto);
if (he == NULL){
LM_ERR("can't resolve_host\n");
continue;
}
init_dest_info(&dst);
hostent2su(&dst.to, he, 0, curi.port_no);
if (send_sock==0) {
send_sock=force_socket ? force_socket :
get_send_socket(0, &dst.to, PROTO_UDP);
}
if (send_sock == NULL) {
LM_ERR("can't get sending socket\n");
continue;
}
dst.proto=PROTO_UDP;
dst.send_sock=send_sock;
if ( (flags&sipping_flag)!=0 &&
(opt.s=build_sipping( &c, send_sock, &path, &ruid, aorhash,
&opt.len))!=0 ) {
if (udp_send(&dst, opt.s, opt.len)<0){
LM_ERR("sip udp_send failed\n");
}
} else if (raw_ip) {
if (send_raw((char*)sbuf, sizeof(sbuf), &dst.to, raw_ip,
raw_port)<0) {
LM_ERR("send_raw failed\n");
}
} else if (udpping_from_path) {
if (send_raw((char*)sbuf, sizeof(sbuf), &dst.to, path_ip,
path_port)<0) {
LM_ERR("send_raw from path failed\n");
}
} else {
if (udp_send(&dst, (char *)sbuf, sizeof(sbuf))<0 ) {
LM_ERR("udp_send failed\n");
}
}
}
pkg_free(buf);
done:
iteration++;
if (iteration==natping_interval)
iteration = 0;
}
/*
* Create received SIP uri that will be either
* passed to registrar in an AVP or apended
* to Contact header field as a parameter
*/
static int
create_rcv_uri(str* uri, struct sip_msg* m)
{
static char buf[MAX_URI_SIZE];
char* p;
str ip, port;
int len;
str proto;
if (!uri || !m) {
LM_ERR("invalid parameter value\n");
return -1;
}
ip.s = ip_addr2a(&m->rcv.src_ip);
ip.len = strlen(ip.s);
port.s = int2str(m->rcv.src_port, &port.len);
switch(m->rcv.proto) {
case PROTO_NONE:
case PROTO_UDP:
proto.s = 0; /* Do not add transport parameter, UDP is default */
proto.len = 0;
break;
case PROTO_TCP:
proto.s = "TCP";
proto.len = 3;
break;
case PROTO_TLS:
proto.s = "TLS";
proto.len = 3;
break;
case PROTO_SCTP:
proto.s = "SCTP";
proto.len = 4;
break;
case PROTO_WS:
case PROTO_WSS:
proto.s = "WS";
proto.len = 2;
break;
default:
LM_ERR("unknown transport protocol\n");
return -1;
}
len = 4 + ip.len + 2*(m->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
if (proto.s) {
len += TRANSPORT_PARAM_LEN;
len += proto.len;
}
if (len > MAX_URI_SIZE) {
LM_ERR("buffer too small\n");
return -1;
}
p = buf;
memcpy(p, "sip:", 4);
p += 4;
if (m->rcv.src_ip.af==AF_INET6)
*p++ = '[';
memcpy(p, ip.s, ip.len);
p += ip.len;
if (m->rcv.src_ip.af==AF_INET6)
*p++ = ']';
*p++ = ':';
memcpy(p, port.s, port.len);
p += port.len;
if (proto.s) {
memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
p += TRANSPORT_PARAM_LEN;
memcpy(p, proto.s, proto.len);
p += proto.len;
}
uri->s = buf;
uri->len = len;
return 0;
}
/*
* Add received parameter to Contacts for further
* forwarding of the REGISTER requuest
*/
static int
add_rcv_param_f(struct sip_msg* msg, char* str1, char* str2)
{
contact_t* c;
struct lump* anchor;
char* param;
str uri;
int hdr_param;
hdr_param = str1?0:1;
if (create_rcv_uri(&uri, msg) < 0) {
return -1;
}
if (contact_iterator(&c, msg, 0) < 0) {
return -1;
}
while(c) {
param = (char*)pkg_malloc(RECEIVED_LEN + 2 + uri.len);
if (!param) {
LM_ERR("no pkg memory left\n");
return -1;
}
memcpy(param, RECEIVED, RECEIVED_LEN);
param[RECEIVED_LEN] = '\"';
memcpy(param + RECEIVED_LEN + 1, uri.s, uri.len);
param[RECEIVED_LEN + 1 + uri.len] = '\"';
if (hdr_param) {
/* add the param as header param */
anchor = anchor_lump(msg, c->name.s + c->len - msg->buf, 0, 0);
} else {
/* add the param as uri param */
anchor = anchor_lump(msg, c->uri.s + c->uri.len - msg->buf, 0, 0);
}
if (anchor == NULL) {
LM_ERR("anchor_lump failed\n");
pkg_free(param);
return -1;
}
if (insert_new_lump_after(anchor, param, RECEIVED_LEN + 1 + uri.len + 1, 0) == 0) {
LM_ERR("insert_new_lump_after failed\n");
pkg_free(param);
return -1;
}
if (contact_iterator(&c, msg, c) < 0) {
return -1;
}
}
return 1;
}
/*
* Create an AVP to be used by registrar with the source IP and port
* of the REGISTER
*/
static int
fix_nated_register_f(struct sip_msg* msg, char* str1, char* str2)
{
str uri;
int_str val;
if(rcv_avp_name.n==0)
return 1;
if (create_rcv_uri(&uri, msg) < 0) {
return -1;
}
val.s = uri;
if (add_avp(AVP_VAL_STR|rcv_avp_type, rcv_avp_name, val) < 0) {
LM_ERR("failed to create AVP\n");
return -1;
}
return 1;
}
/**
* handle SIP replies
*/
static int nh_sip_reply_received(sip_msg_t *msg)
{
to_body_t *fb;
str ruid;
str ah;
unsigned int aorhash;
char *p;
if(nh_keepalive_timeout<=0)
return 1;
if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ_F, 0)==-1)
|| (msg->cseq==NULL)))
{
LM_ERR("no CSEQ header\n");
goto done;
}
if(sipping_method_id!=METHOD_UNDEF && sipping_method_id!=METHOD_OTHER)
{
if(get_cseq(msg)->method_id!=sipping_method_id)
goto done;
} else {
if(sipping_method_id==METHOD_OTHER)
{
if(get_cseq(msg)->method.len!=sipping_method.len)
goto done;
if(strncmp(get_cseq(msg)->method.s, sipping_method.s,
sipping_method.len)!=0)
goto done;
} else {
goto done;
}
}
/* there must be no second via */
if ( ! (parse_headers(msg, HDR_VIA2_F, 0)==-1
|| (msg->via2==0) || (msg->via2->error!=PARSE_OK)) )
goto done;
/* from uri check */
if((parse_from_header(msg))<0)
{
LM_ERR("cannot parse From header\n");
goto done;
}
fb = get_from(msg);
if(fb->uri.len!=sipping_from.len
|| strncmp(fb->uri.s, sipping_from.s, sipping_from.len)!=0)
goto done;
/* from-tag is: ruid-aorhash-counter */
if(fb->tag_value.len<=0)
goto done;
LM_DBG("checking nathelper keepalive reply [%.*s]\n", fb->tag_value.len,
fb->tag_value.s);
/* skip counter */
p = q_memrchr(fb->tag_value.s, '-', fb->tag_value.len);
if(p==NULL) {
LM_DBG("from tag format mismatch [%.*s]\n", fb->tag_value.len,
fb->tag_value.s);
goto done;
}
/* aor hash */
ah.len = p - fb->tag_value.s;
aorhash = 0;
p = q_memrchr(fb->tag_value.s, '-', ah.len);
if(p==NULL) {
LM_DBG("from tag format mismatch [%.*s]!\n", fb->tag_value.len,
fb->tag_value.s);
goto done;
}
ah.s = p + 1;
ah.len = fb->tag_value.s + ah.len - ah.s;
LM_DBG("aor hash string is [%.*s] (%d)\n", ah.len, ah.s, ah.len);
if(ah.len<=0 || reverse_hex2int(ah.s, ah.len, &aorhash)<0)
{
LM_DBG("cannot get aor hash in [%.*s]\n", fb->tag_value.len,
fb->tag_value.s);
goto done;
}
LM_DBG("aor hash is [%u] string [%.*s]\n", aorhash, ah.len, ah.s);
ruid.s = fb->tag_value.s;
ruid.len = ah.s - ruid.s - 1;
if(ruid.len<=0)
{
LM_DBG("cannot get ruid in [%.*s]\n", fb->tag_value.len,
fb->tag_value.s);
goto done;
}
LM_DBG("reply for keepalive of [%.*s:%u]\n", ruid.len, ruid.s, aorhash);
ul.refresh_keepalive(aorhash, &ruid);
done:
/* let the core handle further the reply */
return 1;
}
static int
sel_rewrite_contact(str* res, select_t* s, struct sip_msg* msg)
{
static char buf[500];
contact_t* c;
int n, def_port_fl, len;
char *cp;
str hostport;
struct sip_uri uri;
res->len = 0;
n = s->params[2].v.i;
if (n <= 0) {
LOG(L_ERR, "ERROR: rewrite_contact[%d]: zero or negative index not supported\n", n);
return -1;
}
c = 0;
do {
if (contact_iterator(&c, msg, c) < 0 || !c)
return -1;
n--;
} while (n > 0);
if (parse_uri(c->uri.s, c->uri.len, &uri) < 0 || uri.host.len <= 0) {
LOG(L_ERR, "rewrite_contact[%d]: Error while parsing Contact URI\n", s->params[2].v.i);
return -1;
}
len = c->len - uri.host.len;
if (uri.port.len > 0)
len -= uri.port.len;
def_port_fl = (msg->rcv.proto == PROTO_TLS && msg->rcv.src_port == SIPS_PORT) || (msg->rcv.proto != PROTO_TLS && msg->rcv.src_port == SIP_PORT);
if (!def_port_fl)
len += 1/*:*/+5/*port*/;
if (len > sizeof(buf)) {
LOG(L_ERR, "ERROR: rewrite_contact[%d]: contact too long\n", s->params[2].v.i);
return -1;
}
hostport = uri.host;
if (uri.port.len > 0)
hostport.len = uri.port.s + uri.port.len - uri.host.s;
res->s = buf;
res->len = hostport.s - c->name.s;
memcpy(buf, c->name.s, res->len);
cp = ip_addr2a(&msg->rcv.src_ip);
if (def_port_fl) {
res->len+= snprintf(buf+res->len, sizeof(buf)-res->len, "%s", cp);
} else {
res->len+= snprintf(buf+res->len, sizeof(buf)-res->len, "%s:%d", cp, msg->rcv.src_port);
}
memcpy(buf+res->len, hostport.s+hostport.len, c->len-(hostport.s+hostport.len-c->name.s));
res->len+= c->len-(hostport.s+hostport.len-c->name.s);
return 0;
}
|