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
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Matt O'Gorman <mogorman@digium.com>
* Philippe Sultan <philippe.sultan@gmail.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \author Matt O'Gorman <mogorman@digium.com>
* \author Philippe Sultan <philippe.sultan@gmail.com>
*
* \brief Gtalk Channel Driver, until google/libjingle works with jingle spec
*
* \ingroup channel_drivers
*
* ********** General TODO:s
* \todo Support config reloading.
* \todo Fix native bridging.
*/
/*** MODULEINFO
<depend>iksemel</depend>
<depend>res_jabber</depend>
<use>openssl</use>
<support_level>extended</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 361471 $")
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>
#include <iksemel.h>
#include <pthread.h>
#include <ctype.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/stun.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/stringfields.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/astobj.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/jabber.h"
#include "asterisk/jingle.h"
#define GOOGLE_CONFIG "gtalk.conf"
/*! Global jitterbuffer configuration - by default, jb is disabled */
static struct ast_jb_conf default_jbconf =
{
.flags = 0,
.max_size = -1,
.resync_threshold = -1,
.impl = "",
.target_extra = -1,
};
static struct ast_jb_conf global_jbconf;
enum gtalk_protocol {
AJI_PROTOCOL_UDP = 1,
AJI_PROTOCOL_SSLTCP = 2,
};
enum gtalk_connect_type {
AJI_CONNECT_STUN = 1,
AJI_CONNECT_LOCAL = 2,
AJI_CONNECT_RELAY = 3,
};
struct gtalk_pvt {
ast_mutex_t lock; /*!< Channel private lock */
time_t laststun;
struct gtalk *parent; /*!< Parent client */
char sid[100];
char us[AJI_MAX_JIDLEN];
char them[AJI_MAX_JIDLEN];
char ring[10]; /*!< Message ID of ring */
iksrule *ringrule; /*!< Rule for matching RING request */
int initiator; /*!< If we're the initiator */
int alreadygone;
int capability;
struct ast_codec_pref prefs;
struct gtalk_candidate *theircandidates;
struct gtalk_candidate *ourcandidates;
char cid_num[80]; /*!< Caller ID num */
char cid_name[80]; /*!< Caller ID name */
char exten[80]; /*!< Called extension */
struct ast_channel *owner; /*!< Master Channel */
struct ast_rtp_instance *rtp; /*!< RTP audio session */
struct ast_rtp_instance *vrtp; /*!< RTP video session */
format_t jointcapability; /*!< Supported capability at both ends (codecs ) */
format_t peercapability;
struct gtalk_pvt *next; /* Next entity */
};
struct gtalk_candidate {
char name[100];
enum gtalk_protocol protocol;
double preference;
char username[100];
char password[100];
enum gtalk_connect_type type;
char network[6];
int generation;
char ip[16];
int port;
int receipt;
struct gtalk_candidate *next;
};
struct gtalk {
ASTOBJ_COMPONENTS(struct gtalk);
struct aji_client *connection;
struct aji_buddy *buddy;
struct gtalk_pvt *p;
struct ast_codec_pref prefs;
int amaflags; /*!< AMA Flags */
char user[AJI_MAX_JIDLEN];
char context[AST_MAX_CONTEXT];
char parkinglot[AST_MAX_CONTEXT]; /*!< Parkinglot */
char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
format_t capability;
ast_group_t callgroup; /*!< Call group */
ast_group_t pickupgroup; /*!< Pickup group */
int callingpres; /*!< Calling presentation */
int allowguest;
char language[MAX_LANGUAGE]; /*!< Default language for prompts */
char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */
};
struct gtalk_container {
ASTOBJ_CONTAINER_COMPONENTS(struct gtalk);
};
static const char desc[] = "Gtalk Channel";
static format_t global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pvt's) */
/* Forward declarations */
static struct ast_channel *gtalk_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
/*static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);*/
static int gtalk_sendtext(struct ast_channel *ast, const char *text);
static int gtalk_digit_begin(struct ast_channel *ast, char digit);
static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int gtalk_call(struct ast_channel *ast, char *dest, int timeout);
static int gtalk_hangup(struct ast_channel *ast);
static int gtalk_answer(struct ast_channel *ast);
static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action);
static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p);
static int gtalk_newcall(struct gtalk *client, ikspak *pak);
static struct ast_frame *gtalk_read(struct ast_channel *ast);
static int gtalk_write(struct ast_channel *ast, struct ast_frame *f);
static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid);
static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p);
/* static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); */
static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static int gtalk_update_externip(void);
static int gtalk_parser(void *data, ikspak *pak);
static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to);
/*! \brief PBX interface structure for channel registration */
static const struct ast_channel_tech gtalk_tech = {
.type = "Gtalk",
.description = "Gtalk Channel Driver",
.capabilities = AST_FORMAT_AUDIO_MASK,
.requester = gtalk_request,
.send_text = gtalk_sendtext,
.send_digit_begin = gtalk_digit_begin,
.send_digit_end = gtalk_digit_end,
/* XXX TODO native bridging is causing odd problems with DTMF pass-through with
* the gtalk servers. Enable native bridging once the source of this problem has
* been identified.
.bridge = ast_rtp_instance_bridge, */
.call = gtalk_call,
.hangup = gtalk_hangup,
.answer = gtalk_answer,
.read = gtalk_read,
.write = gtalk_write,
.exception = gtalk_read,
.indicate = gtalk_indicate,
.fixup = gtalk_fixup,
.send_html = gtalk_sendhtml,
.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
};
static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */
static struct sched_context *sched; /*!< The scheduling context */
static struct io_context *io; /*!< The IO context */
static struct in_addr __ourip;
static struct ast_cli_entry gtalk_cli[] = {
/* AST_CLI_DEFINE(gtalk_do_reload, "Reload GoogleTalk configuration"), XXX TODO reloads are not possible yet. */
AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
};
static char externip[16];
static struct sockaddr_in stunaddr; /*!< the stun server we get the externip from */
static struct gtalk_container gtalk_list;
static void gtalk_member_destroy(struct gtalk *obj)
{
ast_free(obj);
}
static struct gtalk *find_gtalk(char *name, char *connection)
{
struct gtalk *gtalk = NULL;
char *domain = NULL , *s = NULL;
if (strchr(connection, '@')) {
s = ast_strdupa(connection);
domain = strsep(&s, "@");
ast_verbose("OOOOH domain = %s\n", domain);
}
gtalk = ASTOBJ_CONTAINER_FIND(>alk_list, name);
if (!gtalk && strchr(name, '@'))
gtalk = ASTOBJ_CONTAINER_FIND_FULL(>alk_list, name, user,,, strcasecmp);
if (!gtalk) {
/* guest call */
ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, {
ASTOBJ_RDLOCK(iterator);
if (!strcasecmp(iterator->name, "guest")) {
gtalk = iterator;
}
ASTOBJ_UNLOCK(iterator);
if (gtalk)
break;
});
}
return gtalk;
}
static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodecs)
{
int res = 0;
char *format = ast_getformatname(codec);
if (!strcasecmp("ulaw", format)) {
iks *payload_eg711u, *payload_pcmu;
payload_pcmu = iks_new("payload-type");
payload_eg711u = iks_new("payload-type");
if(!payload_eg711u || !payload_pcmu) {
iks_delete(payload_pcmu);
iks_delete(payload_eg711u);
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_pcmu, "id", "0");
iks_insert_attrib(payload_pcmu, "name", "PCMU");
iks_insert_attrib(payload_pcmu, "clockrate","8000");
iks_insert_attrib(payload_pcmu, "bitrate","64000");
iks_insert_attrib(payload_eg711u, "id", "100");
iks_insert_attrib(payload_eg711u, "name", "EG711U");
iks_insert_attrib(payload_eg711u, "clockrate","8000");
iks_insert_attrib(payload_eg711u, "bitrate","64000");
iks_insert_node(dcodecs, payload_pcmu);
iks_insert_node(dcodecs, payload_eg711u);
res ++;
}
if (!strcasecmp("alaw", format)) {
iks *payload_eg711a, *payload_pcma;
payload_pcma = iks_new("payload-type");
payload_eg711a = iks_new("payload-type");
if(!payload_eg711a || !payload_pcma) {
iks_delete(payload_eg711a);
iks_delete(payload_pcma);
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_pcma, "id", "8");
iks_insert_attrib(payload_pcma, "name", "PCMA");
iks_insert_attrib(payload_pcma, "clockrate","8000");
iks_insert_attrib(payload_pcma, "bitrate","64000");
payload_eg711a = iks_new("payload-type");
iks_insert_attrib(payload_eg711a, "id", "101");
iks_insert_attrib(payload_eg711a, "name", "EG711A");
iks_insert_attrib(payload_eg711a, "clockrate","8000");
iks_insert_attrib(payload_eg711a, "bitrate","64000");
iks_insert_node(dcodecs, payload_pcma);
iks_insert_node(dcodecs, payload_eg711a);
res ++;
}
if (!strcasecmp("ilbc", format)) {
iks *payload_ilbc = iks_new("payload-type");
if(!payload_ilbc) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_ilbc, "id", "97");
iks_insert_attrib(payload_ilbc, "name", "iLBC");
iks_insert_attrib(payload_ilbc, "clockrate","8000");
iks_insert_attrib(payload_ilbc, "bitrate","13300");
iks_insert_node(dcodecs, payload_ilbc);
res ++;
}
if (!strcasecmp("g723", format)) {
iks *payload_g723 = iks_new("payload-type");
if(!payload_g723) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_g723, "id", "4");
iks_insert_attrib(payload_g723, "name", "G723");
iks_insert_attrib(payload_g723, "clockrate","8000");
iks_insert_attrib(payload_g723, "bitrate","6300");
iks_insert_node(dcodecs, payload_g723);
res ++;
}
if (!strcasecmp("speex", format)) {
iks *payload_speex = iks_new("payload-type");
if(!payload_speex) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_speex, "id", "110");
iks_insert_attrib(payload_speex, "name", "speex");
iks_insert_attrib(payload_speex, "clockrate","8000");
iks_insert_attrib(payload_speex, "bitrate","11000");
iks_insert_node(dcodecs, payload_speex);
res++;
}
if (!strcasecmp("gsm", format)) {
iks *payload_gsm = iks_new("payload-type");
if(!payload_gsm) {
ast_log(LOG_WARNING,"Failed to allocate iks node\n");
return -1;
}
iks_insert_attrib(payload_gsm, "id", "103");
iks_insert_attrib(payload_gsm, "name", "gsm");
iks_insert_node(dcodecs, payload_gsm);
res++;
}
return res;
}
static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator)
{
struct gtalk *client = p->parent;
iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
int x;
int pref_codec = 0;
int alreadysent = 0;
int codecs_num = 0;
char *lowerto = NULL;
iq = iks_new("iq");
gtalk = iks_new("session");
dcodecs = iks_new("description");
transport = iks_new("transport");
payload_telephone = iks_new("payload-type");
if (!(iq && gtalk && dcodecs && transport && payload_telephone)){
iks_delete(iq);
iks_delete(gtalk);
iks_delete(dcodecs);
iks_delete(transport);
iks_delete(payload_telephone);
ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n");
return 0;
}
iks_insert_attrib(dcodecs, "xmlns", GOOGLE_AUDIO_NS);
iks_insert_attrib(dcodecs, "xml:lang", "en");
for (x = 0; x < 64; x++) {
if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
break;
if (!(client->capability & pref_codec))
continue;
if (alreadysent & pref_codec)
continue;
codecs_num = add_codec_to_answer(p, pref_codec, dcodecs);
alreadysent |= pref_codec;
}
if (codecs_num) {
/* only propose DTMF within an audio session */
iks_insert_attrib(payload_telephone, "id", "101");
iks_insert_attrib(payload_telephone, "name", "telephone-event");
iks_insert_attrib(payload_telephone, "clockrate", "8000");
}
iks_insert_attrib(transport,"xmlns",GOOGLE_TRANSPORT_NS);
iks_insert_attrib(iq, "type", "set");
iks_insert_attrib(iq, "to", to);
iks_insert_attrib(iq, "from", from);
iks_insert_attrib(iq, "id", client->connection->mid);
ast_aji_increment_mid(client->connection->mid);
iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept");
/* put the initiator attribute to lower case if we receive the call
* otherwise GoogleTalk won't establish the session */
if (!initiator) {
char c;
char *t = lowerto = ast_strdupa(to);
while (((c = *t) != '/') && (*t++ = tolower(c)));
}
iks_insert_attrib(gtalk, "initiator", initiator ? from : lowerto);
iks_insert_attrib(gtalk, "id", sid);
iks_insert_node(iq, gtalk);
iks_insert_node(gtalk, dcodecs);
iks_insert_node(dcodecs, payload_telephone);
ast_aji_send(client->connection, iq);
iks_delete(payload_telephone);
iks_delete(transport);
iks_delete(dcodecs);
iks_delete(gtalk);
iks_delete(iq);
return 1;
}
static int gtalk_ringing_ack(void *data, ikspak *pak)
{
struct gtalk_pvt *p = data;
struct ast_channel *owner;
ast_mutex_lock(&p->lock);
if (p->ringrule) {
iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
}
p->ringrule = NULL;
/* this may be a redirect */
if (!strcmp(S_OR(iks_find_attrib(pak->x, "type"), ""), "error")) {
char *name = NULL;
char *redirect = NULL;
iks *traversenodes = NULL;
traversenodes = pak->query;
while (traversenodes) {
if (!(name = iks_name(traversenodes))) {
break;
}
if (!strcasecmp(name, "error") &&
((redirect = iks_find_cdata(traversenodes, "redirect")) ||
(redirect = iks_find_cdata(traversenodes, "sta:redirect"))) &&
(redirect = strstr(redirect, "xmpp:"))) {
redirect += 5;
ast_log(LOG_DEBUG, "redirect %s\n", redirect);
ast_copy_string(p->them, redirect, sizeof(p->them));
gtalk_invite(p, p->them, p->us, p->sid, 1);
break;
}
traversenodes = iks_next_tag(traversenodes);
}
}
gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
owner = p->owner;
ast_mutex_unlock(&p->lock);
if (owner) {
ast_queue_control(owner, AST_CONTROL_RINGING);
}
return IKS_FILTER_EAT;
}
static int gtalk_answer(struct ast_channel *ast)
{
struct gtalk_pvt *p = ast->tech_pvt;
int res = 0;
ast_debug(1, "Answer!\n");
ast_mutex_lock(&p->lock);
gtalk_invite(p, p->them, p->us,p->sid, 0);
manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
ast->name, "GTALK", p->sid);
ast_mutex_unlock(&p->lock);
return res;
}
static enum ast_rtp_glue_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
{
struct gtalk_pvt *p = chan->tech_pvt;
enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
if (!p)
return res;
ast_mutex_lock(&p->lock);
if (p->rtp){
ao2_ref(p->rtp, +1);
*instance = p->rtp;
res = AST_RTP_GLUE_RESULT_LOCAL;
}
ast_mutex_unlock(&p->lock);
return res;
}
static format_t gtalk_get_codec(struct ast_channel *chan)
{
struct gtalk_pvt *p = chan->tech_pvt;
return p->peercapability;
}
static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, format_t codecs, int nat_active)
{
struct gtalk_pvt *p;
p = chan->tech_pvt;
if (!p)
return -1;
ast_mutex_lock(&p->lock);
/* if (rtp)
ast_rtp_get_peer(rtp, &p->redirip);
else
memset(&p->redirip, 0, sizeof(p->redirip));
p->redircodecs = codecs; */
/* Reset lastrtprx timer */
ast_mutex_unlock(&p->lock);
return 0;
}
static struct ast_rtp_glue gtalk_rtp_glue = {
.type = "Gtalk",
.get_rtp_info = gtalk_get_rtp_peer,
.get_codec = gtalk_get_codec,
.update_peer = gtalk_set_rtp_peer,
};
static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2)
{
iks *response = NULL, *error = NULL, *reason = NULL;
int res = -1;
response = iks_new("iq");
if (response) {
iks_insert_attrib(response, "type", "result");
iks_insert_attrib(response, "from", from);
iks_insert_attrib(response, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
iks_insert_attrib(response, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
if (reasonstr) {
error = iks_new("error");
if (error) {
iks_insert_attrib(error, "type", "cancel");
reason = iks_new(reasonstr);
if (reason)
iks_insert_node(error, reason);
iks_insert_node(response, error);
}
}
ast_aji_send(client->connection, response);
res = 0;
}
iks_delete(reason);
iks_delete(error);
iks_delete(response);
return res;
}
static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp = NULL;
char *from;
iks *codec;
char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
int peernoncodeccapability;
ast_log(LOG_DEBUG, "The client is %s\n", client->name);
/* Make sure our new call does exist */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
break;
} else if (iks_find_with_attrib(pak->x, "ses:session", "id", tmp->sid)) {
break;
}
}
if (!tmp) {
ast_log(LOG_WARNING, "Could not find session in iq\n");
return -1;
}
/* codec points to the first <payload-type/> tag */
codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
while (codec) {
char *codec_id = iks_find_attrib(codec, "id");
char *codec_name = iks_find_attrib(codec, "name");
if (!codec_id || !codec_name) {
codec = iks_next_tag(codec);
continue;
}
ast_rtp_codecs_payloads_set_m_type(
ast_rtp_instance_get_codecs(tmp->rtp),
tmp->rtp,
atoi(codec_id));
ast_rtp_codecs_payloads_set_rtpmap_type(
ast_rtp_instance_get_codecs(tmp->rtp),
tmp->rtp,
atoi(codec_id),
"audio",
codec_name,
0);
codec = iks_next_tag(codec);
}
/* Now gather all of the codecs that we are asked for */
ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(tmp->rtp), &tmp->peercapability, &peernoncodeccapability);
/* at this point, we received an awser from the remote Gtalk client,
which allows us to compare capabilities */
tmp->jointcapability = tmp->capability & tmp->peercapability;
if (!tmp->jointcapability) {
ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->capability),
ast_getformatname_multiple(s2, BUFSIZ, tmp->peercapability),
ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcapability));
/* close session if capabilities don't match */
ast_queue_hangup(tmp->owner);
return -1;
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp->owner) {
ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
}
gtalk_update_stun(tmp->parent, tmp);
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
char *from;
ast_log(LOG_DEBUG, "The client is %s\n", client->name);
/* find corresponding call */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
break;
}
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
gtalk_update_stun(tmp->parent, tmp);
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call during accept?!\n");
}
/* answer 'iq' packet to let the remote peer know that we're alive */
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
iks *dtmfnode = NULL, *dtmfchild = NULL;
char *dtmf;
char *from;
/* Make sure our new call doesn't exist yet */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
break;
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
gtalk_response(client, from, pak,
"feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
"unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
return -1;
}
if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
struct ast_frame f = {AST_FRAME_DTMF_END, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
struct ast_frame f = {AST_FRAME_DTMF, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
}
}
} else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
struct ast_frame f = {AST_FRAME_DTMF_END, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
} else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
f.subclass.integer = dtmf[0];
ast_queue_frame(tmp->owner, &f);
ast_verbose("GOOGLE! DTMF-relay event received: %c\n", (int) f.subclass.integer);
}
}
}
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *tmp;
char *from;
ast_debug(1, "The client is %s\n", client->name);
/* Make sure our new call doesn't exist yet */
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
(iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
break;
}
}
from = iks_find_attrib(pak->x, "to");
if (!from) {
from = client->connection->jid->full;
}
if (tmp) {
tmp->alreadygone = 1;
if (tmp->owner) {
ast_queue_hangup(tmp->owner);
}
} else {
ast_log(LOG_NOTICE, "Whoa, didn't find call during hangup!\n");
}
gtalk_response(client, from, pak, NULL, NULL);
return 1;
}
static int gtalk_get_local_ip(struct ast_sockaddr *ourip)
{
struct ast_sockaddr root;
struct ast_sockaddr bindaddr_tmp;
struct ast_sockaddr *addrs;
int addrs_cnt;
/* If bind address is not 0.0.0.0, then bindaddr is our local ip. */
ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
if (!ast_sockaddr_is_any(&bindaddr_tmp)) {
ast_sockaddr_copy(ourip, &bindaddr_tmp);
return 0;
}
/* If no bind address was provided, lets see what ip we would use to connect to google.com and use that.
* If you can't resolve google.com from your network, then this module is useless for you anyway. */
if ((addrs_cnt = ast_sockaddr_resolve(&addrs, "google.com", PARSE_PORT_FORBID, AF_INET)) > 0) {
ast_sockaddr_copy(&root, &addrs[0]);
ast_free(addrs);
if (!ast_ouraddrfor(&root, ourip)) {
return 0;
}
}
/* As a last resort, use this function to find our local address. */
return ast_find_ourip(ourip, &bindaddr_tmp, AF_INET);
}
static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
{
struct gtalk_candidate *tmp;
struct aji_client *c = client->connection;
struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
struct sockaddr_in sin = { 0, };
struct ast_sockaddr sin_tmp;
struct ast_sockaddr us;
iks *iq, *gtalk, *candidate, *transport;
char user[17], pass[17], preference[5], port[7];
char *lowerfrom = NULL;
iq = iks_new("iq");
gtalk = iks_new("session");
candidate = iks_new("candidate");
transport = iks_new("transport");
if (!iq || !gtalk || !candidate || !transport) {
ast_log(LOG_ERROR, "Memory allocation error\n");
goto safeout;
}
ours1 = ast_calloc(1, sizeof(*ours1));
ours2 = ast_calloc(1, sizeof(*ours2));
if (!ours1 || !ours2)
goto safeout;
iks_insert_attrib(transport, "xmlns",GOOGLE_TRANSPORT_NS);
iks_insert_node(iq, gtalk);
iks_insert_node(gtalk,candidate);
iks_insert_node(gtalk,transport);
for (; p; p = p->next) {
if (!strcasecmp(p->sid, sid))
break;
}
if (!p) {
ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid);
goto safeout;
}
ast_rtp_instance_get_local_address(p->rtp, &sin_tmp);
ast_sockaddr_to_sin(&sin_tmp, &sin);
gtalk_get_local_ip(&us);
if (!strcmp(ast_sockaddr_stringify_addr(&us), "127.0.0.1")) {
ast_log(LOG_WARNING, "Found a loopback IP on the system, check your network configuration or set the bindaddr attribute.\n");
}
/* Setup our gtalk candidates */
ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
ours1->port = ntohs(sin.sin_port);
ours1->preference = 1;
snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
ast_copy_string(ours1->username, user, sizeof(ours1->username));
ast_copy_string(ours1->password, pass, sizeof(ours1->password));
ast_copy_string(ours1->ip, ast_sockaddr_stringify_addr(&us),
sizeof(ours1->ip));
ours1->protocol = AJI_PROTOCOL_UDP;
ours1->type = AJI_CONNECT_LOCAL;
ours1->generation = 0;
p->ourcandidates = ours1;
/* XXX this is a blocking action. We send a STUN request to the server
* and wait for the response. If blocking here is a problem the STUN requests/responses
* for the externip may need to be done differently. */
gtalk_update_externip();
if (!ast_strlen_zero(externip)) {
ast_copy_string(ours2->username, user, sizeof(ours2->username));
ast_copy_string(ours2->password, pass, sizeof(ours2->password));
ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
ours2->port = ntohs(sin.sin_port);
ours2->preference = 0.9;
ours2->protocol = AJI_PROTOCOL_UDP;
ours2->type = AJI_CONNECT_STUN;
ours2->generation = 0;
ours1->next = ours2;
ours2 = NULL;
}
ours1 = NULL;
for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
snprintf(port, sizeof(port), "%d", tmp->port);
snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
iks_insert_attrib(iq, "from", to);
iks_insert_attrib(iq, "to", from);
iks_insert_attrib(iq, "type", "set");
iks_insert_attrib(iq, "id", c->mid);
ast_aji_increment_mid(c->mid);
iks_insert_attrib(gtalk, "type", "candidates");
iks_insert_attrib(gtalk, "id", sid);
/* put the initiator attribute to lower case if we receive the call
* otherwise GoogleTalk won't establish the session */
if (!p->initiator) {
char c;
char *t = lowerfrom = ast_strdupa(from);
while (((c = *t) != '/') && (*t++ = tolower(c)));
}
iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : lowerfrom);
iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
iks_insert_attrib(candidate, "name", tmp->name);
iks_insert_attrib(candidate, "address", tmp->ip);
iks_insert_attrib(candidate, "port", port);
iks_insert_attrib(candidate, "username", tmp->username);
iks_insert_attrib(candidate, "password", tmp->password);
iks_insert_attrib(candidate, "preference", preference);
if (tmp->protocol == AJI_PROTOCOL_UDP)
iks_insert_attrib(candidate, "protocol", "udp");
if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
iks_insert_attrib(candidate, "protocol", "ssltcp");
if (tmp->type == AJI_CONNECT_STUN)
iks_insert_attrib(candidate, "type", "stun");
if (tmp->type == AJI_CONNECT_LOCAL)
iks_insert_attrib(candidate, "type", "local");
if (tmp->type == AJI_CONNECT_RELAY)
iks_insert_attrib(candidate, "type", "relay");
iks_insert_attrib(candidate, "network", "0");
iks_insert_attrib(candidate, "generation", "0");
ast_aji_send(c, iq);
}
p->laststun = 0;
safeout:
if (ours1)
ast_free(ours1);
if (ours2)
ast_free(ours2);
iks_delete(iq);
iks_delete(gtalk);
iks_delete(candidate);
iks_delete(transport);
return 1;
}
static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid)
{
struct gtalk_pvt *tmp = NULL;
struct aji_resource *resources = NULL;
struct aji_buddy *buddy = NULL;
char idroster[200] = "";
char *data, *exten = NULL;
struct ast_sockaddr bindaddr_tmp;
ast_debug(1, "The client is %s for alloc\n", client->name);
if (!sid && !strchr(them, '/')) { /* I started call! */
if (!strcasecmp(client->name, "guest")) {
buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them);
if (buddy) {
resources = buddy->resources;
}
} else if (client->buddy) {
resources = client->buddy->resources;
}
while (resources) {
if (resources->cap->jingle) {
break;
}
resources = resources->next;
}
if (resources) {
snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource);
} else if ((*them == '+') || (strstr(them, "@voice.google.com"))) {
snprintf(idroster, sizeof(idroster), "%s", them);
} else {
ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n");
if (buddy) {
ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
}
return NULL;
}
if (buddy) {
ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
}
}
if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
return NULL;
}
memcpy(&tmp->prefs, &client->prefs, sizeof(struct ast_codec_pref));
if (sid) {
ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
ast_copy_string(tmp->them, them, sizeof(tmp->them));
ast_copy_string(tmp->us, us, sizeof(tmp->us));
} else {
snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
ast_copy_string(tmp->them, idroster, sizeof(tmp->them));
ast_copy_string(tmp->us, us, sizeof(tmp->us));
tmp->initiator = 1;
}
/* clear codecs */
bindaddr.sin_family = AF_INET;
ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
if (!(tmp->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL))) {
ast_log(LOG_ERROR, "Failed to create a new RTP instance (possibly an invalid bindaddr?)\n");
ast_free(tmp);
return NULL;
}
ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_RTCP, 1);
ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_STUN, 1);
ast_rtp_instance_set_prop(tmp->rtp, AST_RTP_PROPERTY_DTMF, 1);
ast_rtp_instance_dtmf_mode_set(tmp->rtp, AST_RTP_DTMF_MODE_RFC2833);
ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(tmp->rtp), tmp->rtp);
/* add user configured codec capabilites */
if (client->capability) {
tmp->capability = client->capability;
} else if (global_capability) {
tmp->capability = global_capability;
}
tmp->parent = client;
if (!tmp->rtp) {
ast_log(LOG_WARNING, "Out of RTP sessions?\n");
ast_free(tmp);
return NULL;
}
/* Set CALLERID(name) to the full JID of the remote peer */
ast_copy_string(tmp->cid_name, tmp->them, sizeof(tmp->cid_name));
if(strchr(tmp->us, '/')) {
data = ast_strdupa(tmp->us);
exten = strsep(&data, "/");
} else {
exten = tmp->us;
}
ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
ast_mutex_init(&tmp->lock);
ast_mutex_lock(>alklock);
tmp->next = client->p;
client->p = tmp;
ast_mutex_unlock(>alklock);
return tmp;
}
/*! \brief Start new gtalk channel */
static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i, int state, const char *title, const char *linkedid)
{
struct ast_channel *tmp;
int fmt;
int what;
const char *n2;
if (title)
n2 = title;
else
n2 = i->us;
tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, linkedid, client->accountcode, i->exten, client->context, client->amaflags, "Gtalk/%s-%04lx", n2, ast_random() & 0xffff);
if (!tmp) {
ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n");
return NULL;
}
tmp->tech = >alk_tech;
/* Select our native format based on codec preference until we receive
something from another device to the contrary. */
if (i->jointcapability)
what = i->jointcapability;
else if (i->capability)
what = i->capability;
else
what = global_capability;
/* Set Frame packetization */
if (i->rtp) {
ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(i->rtp), i->rtp, &i->prefs);
}
tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
fmt = ast_best_codec(tmp->nativeformats);
if (i->rtp) {
ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1));
}
if (i->vrtp) {
ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0));
ast_channel_set_fd(tmp, 3, ast_rtp_instance_fd(i->vrtp, 1));
}
if (state == AST_STATE_RING)
tmp->rings = 1;
tmp->adsicpe = AST_ADSI_UNAVAILABLE;
tmp->writeformat = fmt;
tmp->rawwriteformat = fmt;
tmp->readformat = fmt;
tmp->rawreadformat = fmt;
tmp->tech_pvt = i;
tmp->callgroup = client->callgroup;
tmp->pickupgroup = client->pickupgroup;
tmp->caller.id.name.presentation = client->callingpres;
tmp->caller.id.number.presentation = client->callingpres;
if (!ast_strlen_zero(client->accountcode))
ast_string_field_set(tmp, accountcode, client->accountcode);
if (client->amaflags)
tmp->amaflags = client->amaflags;
if (!ast_strlen_zero(client->language))
ast_string_field_set(tmp, language, client->language);
if (!ast_strlen_zero(client->musicclass))
ast_string_field_set(tmp, musicclass, client->musicclass);
if (!ast_strlen_zero(client->parkinglot))
ast_string_field_set(tmp, parkinglot, client->parkinglot);
i->owner = tmp;
ast_module_ref(ast_module_info->self);
ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) {
tmp->dialed.number.str = ast_strdup(i->exten);
}
tmp->priority = 1;
if (i->rtp)
ast_jb_configure(tmp, &global_jbconf);
if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
ast_hangup(tmp);
tmp = NULL;
} else {
manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
"Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
i->owner ? i->owner->name : "", "Gtalk", i->sid);
}
return tmp;
}
static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action)
{
iks *request, *session = NULL;
int res = -1;
char *lowerthem = NULL;
request = iks_new("iq");
if (request) {
iks_insert_attrib(request, "type", "set");
iks_insert_attrib(request, "from", p->us);
iks_insert_attrib(request, "to", p->them);
iks_insert_attrib(request, "id", client->connection->mid);
ast_aji_increment_mid(client->connection->mid);
session = iks_new("session");
if (session) {
iks_insert_attrib(session, "type", action);
iks_insert_attrib(session, "id", p->sid);
/* put the initiator attribute to lower case if we receive the call
* otherwise GoogleTalk won't establish the session */
if (!p->initiator) {
char c;
char *t = lowerthem = ast_strdupa(p->them);
while (((c = *t) != '/') && (*t++ = tolower(c)));
}
iks_insert_attrib(session, "initiator", p->initiator ? p->us : lowerthem);
iks_insert_attrib(session, "xmlns", GOOGLE_NS);
iks_insert_node(request, session);
ast_aji_send(client->connection, request);
res = 0;
}
}
iks_delete(session);
iks_delete(request);
return res;
}
static void gtalk_free_candidates(struct gtalk_candidate *candidate)
{
struct gtalk_candidate *last;
while (candidate) {
last = candidate;
candidate = candidate->next;
ast_free(last);
}
}
static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p)
{
struct gtalk_pvt *cur, *prev = NULL;
cur = client->p;
while (cur) {
if (cur == p) {
if (prev)
prev->next = p->next;
else
client->p = p->next;
break;
}
prev = cur;
cur = cur->next;
}
if (p->ringrule)
iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
if (p->owner)
ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
if (p->rtp)
ast_rtp_instance_destroy(p->rtp);
if (p->vrtp)
ast_rtp_instance_destroy(p->vrtp);
gtalk_free_candidates(p->theircandidates);
ast_free(p);
}
static int gtalk_newcall(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *p, *tmp = client->p;
struct ast_channel *chan;
int res;
iks *codec;
char *from = NULL;
char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
int peernoncodeccapability;
char *sid;
/* Make sure our new call doesn't exist yet */
from = iks_find_attrib(pak->x,"to");
if (!from) {
from = client->connection->jid->full;
}
while (tmp) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
(iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
gtalk_response(client, from, pak, "out-of-order", NULL);
return -1;
}
tmp = tmp->next;
}
if (!strcasecmp(client->name, "guest")){
/* the guest account is not tied to any configured XMPP client,
let's set it now */
if (client->connection) {
ASTOBJ_UNREF(client->connection, ast_aji_client_destroy);
}
client->connection = ast_aji_get_client(from);
if (!client->connection) {
ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", from);
return -1;
}
}
if (!(sid = iks_find_attrib(pak->query, "id"))) {
ast_log(LOG_WARNING, "Received Initiate without id attribute. Can not start call.\n");
return -1;
}
p = gtalk_alloc(client, from, pak->from->full, sid);
if (!p) {
ast_log(LOG_WARNING, "Unable to allocate gtalk structure!\n");
return -1;
}
chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user, NULL);
if (!chan) {
gtalk_free_pvt(client, p);
return -1;
}
ast_mutex_lock(&p->lock);
ast_copy_string(p->them, pak->from->full, sizeof(p->them));
ast_copy_string(p->sid, sid, sizeof(p->sid));
/* codec points to the first <payload-type/> tag */
codec = iks_first_tag(iks_first_tag(pak->query));
while (codec) {
char *codec_id = iks_find_attrib(codec, "id");
char *codec_name = iks_find_attrib(codec, "name");
if (!codec_id || !codec_name) {
codec = iks_next_tag(codec);
continue;
}
if (!strcmp(S_OR(iks_name(codec), ""), "vid:payload-type") && p->vrtp) {
ast_rtp_codecs_payloads_set_m_type(
ast_rtp_instance_get_codecs(p->vrtp),
p->vrtp,
atoi(codec_id));
ast_rtp_codecs_payloads_set_rtpmap_type(
ast_rtp_instance_get_codecs(p->vrtp),
p->vrtp,
atoi(codec_id),
"video",
codec_name,
0);
} else {
ast_rtp_codecs_payloads_set_m_type(
ast_rtp_instance_get_codecs(p->rtp),
p->rtp,
atoi(codec_id));
ast_rtp_codecs_payloads_set_rtpmap_type(
ast_rtp_instance_get_codecs(p->rtp),
p->rtp,
atoi(codec_id),
"audio",
codec_name,
0);
}
codec = iks_next_tag(codec);
}
/* Now gather all of the codecs that we are asked for */
ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(p->rtp), &p->peercapability, &peernoncodeccapability);
p->jointcapability = p->capability & p->peercapability;
ast_mutex_unlock(&p->lock);
ast_setstate(chan, AST_STATE_RING);
if (!p->jointcapability) {
ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->capability),
ast_getformatname_multiple(s2, BUFSIZ, p->peercapability),
ast_getformatname_multiple(s3, BUFSIZ, p->jointcapability));
/* close session if capabilities don't match */
gtalk_action(client, p, "reject");
p->alreadygone = 1;
gtalk_hangup(chan);
ast_channel_release(chan);
return -1;
}
res = ast_pbx_start(chan);
switch (res) {
case AST_PBX_FAILED:
ast_log(LOG_WARNING, "Failed to start PBX :(\n");
gtalk_response(client, from, pak, "service-unavailable", NULL);
break;
case AST_PBX_CALL_LIMIT:
ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
gtalk_response(client, from, pak, "service-unavailable", NULL);
break;
case AST_PBX_SUCCESS:
gtalk_response(client, from, pak, NULL, NULL);
gtalk_create_candidates(client, p, p->sid, p->them, p->us);
/* nothing to do */
break;
}
return 1;
}
static int gtalk_update_externip(void)
{
int sock;
char *newaddr;
struct sockaddr_in answer = { 0, };
struct sockaddr_in *dst;
struct ast_sockaddr tmp_dst;
if (!stunaddr.sin_addr.s_addr) {
return -1;
}
dst = &stunaddr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
ast_log(LOG_WARNING, "Unable to create STUN socket: %s\n", strerror(errno));
return -1;
}
ast_sockaddr_from_sin(&tmp_dst, dst);
if (ast_connect(sock, &tmp_dst) != 0) {
ast_log(LOG_WARNING, "STUN Failed to connect to %s\n", ast_sockaddr_stringify(&tmp_dst));
close(sock);
return -1;
}
if ((ast_stun_request(sock, &stunaddr, NULL, &answer))) {
close(sock);
return -1;
}
newaddr = ast_strdupa(ast_inet_ntoa(answer.sin_addr));
memcpy(externip, newaddr, sizeof(externip));
close(sock);
return 0;
}
static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
{
struct gtalk_candidate *tmp;
struct hostent *hp;
struct ast_hostent ahp;
struct sockaddr_in sin = { 0, };
struct sockaddr_in aux = { 0, };
struct ast_sockaddr sin_tmp;
struct ast_sockaddr aux_tmp;
if (time(NULL) == p->laststun)
return 0;
tmp = p->theircandidates;
p->laststun = time(NULL);
while (tmp) {
char username[256];
/* Find the IP address of the host */
if (!(hp = ast_gethostbyname(tmp->ip, &ahp))) {
ast_log(LOG_WARNING, "Could not get host by name for %s\n", tmp->ip);
tmp = tmp->next;
continue;
}
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
sin.sin_port = htons(tmp->port);
snprintf(username, sizeof(username), "%s%s", tmp->username, p->ourcandidates->username);
/* Find out the result of the STUN */
ast_rtp_instance_get_remote_address(p->rtp, &aux_tmp);
ast_sockaddr_to_sin(&aux_tmp, &aux);
/* If the STUN result is different from the IP of the hostname,
* lock on the stun IP of the hostname advertised by the
* remote client */
if (aux.sin_addr.s_addr && (aux.sin_addr.s_addr != sin.sin_addr.s_addr)) {
ast_rtp_instance_stun_request(p->rtp, &aux_tmp, username);
} else {
ast_sockaddr_from_sin(&sin_tmp, &sin);
ast_rtp_instance_stun_request(p->rtp, &sin_tmp, username);
}
if (aux.sin_addr.s_addr) {
ast_debug(4, "Receiving RTP traffic from IP %s, matches with remote candidate's IP %s\n", ast_inet_ntoa(aux.sin_addr), tmp->ip);
ast_debug(4, "Sending STUN request to %s\n", tmp->ip);
}
tmp = tmp->next;
}
return 1;
}
static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
{
struct gtalk_pvt *p = NULL, *tmp = NULL;
struct aji_client *c = client->connection;
struct gtalk_candidate *newcandidate = NULL;
iks *traversenodes = NULL, *receipt = NULL;
char *from;
from = iks_find_attrib(pak->x,"to");
if (!from) {
from = c->jid->full;
}
for (tmp = client->p; tmp; tmp = tmp->next) {
if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) ||
(iks_find_attrib(pak->query, "id") && !strcmp(iks_find_attrib(pak->query, "id"), tmp->sid))) {
p = tmp;
break;
}
}
if (!p) {
return -1;
}
traversenodes = pak->query;
while(traversenodes) {
if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "session")) {
traversenodes = iks_first_tag(traversenodes);
continue;
}
if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:session")) {
traversenodes = iks_child(traversenodes);
continue;
}
if(!strcasecmp(S_OR(iks_name(traversenodes), ""), "candidate") || !strcasecmp(S_OR(iks_name(traversenodes), ""), "ses:candidate")) {
newcandidate = ast_calloc(1, sizeof(*newcandidate));
if (!newcandidate)
return 0;
ast_copy_string(newcandidate->name,
S_OR(iks_find_attrib(traversenodes, "name"), ""),
sizeof(newcandidate->name));
ast_copy_string(newcandidate->ip,
S_OR(iks_find_attrib(traversenodes, "address"), ""),
sizeof(newcandidate->ip));
newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
ast_copy_string(newcandidate->username,
S_OR(iks_find_attrib(traversenodes, "username"), ""),
sizeof(newcandidate->username));
ast_copy_string(newcandidate->password,
S_OR(iks_find_attrib(traversenodes, "password"), ""),
sizeof(newcandidate->password));
newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "protocol"), ""), "udp"))
newcandidate->protocol = AJI_PROTOCOL_UDP;
if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "protocol"), ""), "ssltcp"))
newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "stun"))
newcandidate->type = AJI_CONNECT_STUN;
if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "local"))
newcandidate->type = AJI_CONNECT_LOCAL;
if (!strcasecmp(S_OR(iks_find_attrib(traversenodes, "type"), ""), "relay"))
newcandidate->type = AJI_CONNECT_RELAY;
ast_copy_string(newcandidate->network,
S_OR(iks_find_attrib(traversenodes, "network"), ""),
sizeof(newcandidate->network));
newcandidate->generation = atoi(S_OR(iks_find_attrib(traversenodes, "generation"), "0"));
newcandidate->next = NULL;
newcandidate->next = p->theircandidates;
p->theircandidates = newcandidate;
p->laststun = 0;
gtalk_update_stun(p->parent, p);
newcandidate = NULL;
}
traversenodes = iks_next_tag(traversenodes);
}
receipt = iks_new("iq");
iks_insert_attrib(receipt, "type", "result");
iks_insert_attrib(receipt, "from", from);
iks_insert_attrib(receipt, "to", S_OR(iks_find_attrib(pak->x, "from"), ""));
iks_insert_attrib(receipt, "id", S_OR(iks_find_attrib(pak->x, "id"), ""));
ast_aji_send(c, receipt);
iks_delete(receipt);
return 1;
}
static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pvt *p)
{
struct ast_frame *f;
if (!p->rtp) {
return &ast_null_frame;
}
f = ast_rtp_instance_read(p->rtp, 0);
gtalk_update_stun(p->parent, p);
if (p->owner) {
/* We already hold the channel lock */
if (f->frametype == AST_FRAME_VOICE) {
if (f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(f->subclass.codec));
p->owner->nativeformats =
(p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass.codec;
ast_set_read_format(p->owner, p->owner->readformat);
ast_set_write_format(p->owner, p->owner->writeformat);
}
/* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
f = ast_dsp_process(p->owner, p->vad, f);
if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
} */
}
}
return f;
}
static struct ast_frame *gtalk_read(struct ast_channel *ast)
{
struct ast_frame *fr;
struct gtalk_pvt *p = ast->tech_pvt;
ast_mutex_lock(&p->lock);
fr = gtalk_rtp_read(ast, p);
ast_mutex_unlock(&p->lock);
return fr;
}
/*! \brief Send frame to media channel (rtp) */
static int gtalk_write(struct ast_channel *ast, struct ast_frame *frame)
{
struct gtalk_pvt *p = ast->tech_pvt;
int res = 0;
char buf[256];
switch (frame->frametype) {
case AST_FRAME_VOICE:
if (!(frame->subclass.codec & ast->nativeformats)) {
ast_log(LOG_WARNING,
"Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
ast_getformatname(frame->subclass.codec),
ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
ast_getformatname(ast->readformat),
ast_getformatname(ast->writeformat));
return 0;
}
if (p) {
ast_mutex_lock(&p->lock);
if (p->rtp) {
res = ast_rtp_instance_write(p->rtp, frame);
}
ast_mutex_unlock(&p->lock);
}
break;
case AST_FRAME_VIDEO:
if (p) {
ast_mutex_lock(&p->lock);
if (p->vrtp) {
res = ast_rtp_instance_write(p->vrtp, frame);
}
ast_mutex_unlock(&p->lock);
}
break;
case AST_FRAME_IMAGE:
return 0;
break;
default:
ast_log(LOG_WARNING, "Can't send %d type frames with Gtalk write\n",
frame->frametype);
return 0;
}
return res;
}
static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
struct gtalk_pvt *p = newchan->tech_pvt;
ast_mutex_lock(&p->lock);
if ((p->owner != oldchan)) {
ast_mutex_unlock(&p->lock);
return -1;
}
if (p->owner == oldchan)
p->owner = newchan;
ast_mutex_unlock(&p->lock);
return 0;
}
static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
{
int res = 0;
switch (condition) {
case AST_CONTROL_HOLD:
ast_moh_start(ast, data, NULL);
break;
case AST_CONTROL_UNHOLD:
ast_moh_stop(ast);
break;
default:
ast_debug(3, "Don't know how to indicate condition '%d'\n", condition);
res = -1;
}
return res;
}
static int gtalk_sendtext(struct ast_channel *chan, const char *text)
{
int res = 0;
struct aji_client *client = NULL;
struct gtalk_pvt *p = chan->tech_pvt;
if (!p->parent) {
ast_log(LOG_ERROR, "Parent channel not found\n");
return -1;
}
if (!p->parent->connection) {
ast_log(LOG_ERROR, "XMPP client not found\n");
return -1;
}
client = p->parent->connection;
res = ast_aji_send_chat(client, p->them, text);
return res;
}
static int gtalk_digit_begin(struct ast_channel *chan, char digit)
{
struct gtalk_pvt *p = chan->tech_pvt;
int res = 0;
ast_mutex_lock(&p->lock);
if (p->rtp) {
ast_rtp_instance_dtmf_begin(p->rtp, digit);
} else {
res = -1;
}
ast_mutex_unlock(&p->lock);
return res;
}
static int gtalk_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
{
struct gtalk_pvt *p = chan->tech_pvt;
int res = 0;
ast_mutex_lock(&p->lock);
if (p->rtp) {
ast_rtp_instance_dtmf_end_with_duration(p->rtp, digit, duration);
} else {
res = -1;
}
ast_mutex_unlock(&p->lock);
return res;
}
/* This function is of not in use at the moment, but I am choosing to leave this
* within the code base as a reference to how DTMF is possible through
* jingle signaling. However, google currently does DTMF through the RTP. */
#if 0
static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration)
{
struct gtalk_pvt *p = ast->tech_pvt;
struct gtalk *client = p->parent;
iks *iq, *gtalk, *dtmf;
char buffer[2] = {digit, '\0'};
char *lowerthem = NULL;
iq = iks_new("iq");
gtalk = iks_new("gtalk");
dtmf = iks_new("dtmf");
if(!iq || !gtalk || !dtmf) {
iks_delete(iq);
iks_delete(gtalk);
iks_delete(dtmf);
ast_log(LOG_ERROR, "Did not send dtmf do to memory issue\n");
return -1;
}
iks_insert_attrib(iq, "type", "set");
iks_insert_attrib(iq, "to", p->them);
iks_insert_attrib(iq, "from", p->us);
iks_insert_attrib(iq, "id", client->connection->mid);
ast_aji_increment_mid(client->connection->mid);
iks_insert_attrib(gtalk, "xmlns", "http://jabber.org/protocol/gtalk");
iks_insert_attrib(gtalk, "action", "session-info");
// put the initiator attribute to lower case if we receive the call
// otherwise GoogleTalk won't establish the session
if (!p->initiator) {
char c;
char *t = lowerthem = ast_strdupa(p->them);
while (((c = *t) != '/') && (*t++ = tolower(c)));
}
iks_insert_attrib(gtalk, "initiator", p->initiator ? p->us: lowerthem);
iks_insert_attrib(gtalk, "sid", p->sid);
iks_insert_attrib(dtmf, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
iks_insert_attrib(dtmf, "code", buffer);
iks_insert_node(iq, gtalk);
iks_insert_node(gtalk, dtmf);
ast_mutex_lock(&p->lock);
if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN || duration == 0) {
iks_insert_attrib(dtmf, "action", "button-down");
} else if (ast->dtmff.frametype == AST_FRAME_DTMF_END || duration != 0) {
iks_insert_attrib(dtmf, "action", "button-up");
}
ast_aji_send(client->connection, iq);
iks_delete(iq);
iks_delete(gtalk);
iks_delete(dtmf);
ast_mutex_unlock(&p->lock);
return 0;
}
#endif
static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
{
ast_log(LOG_NOTICE, "XXX Implement gtalk sendhtml XXX\n");
return -1;
}
/*!\brief Initiate new call, part of PBX interface
* dest is the dial string */
static int gtalk_call(struct ast_channel *ast, char *dest, int timeout)
{
struct gtalk_pvt *p = ast->tech_pvt;
if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
ast_log(LOG_WARNING, "gtalk_call called on %s, neither down nor reserved\n", ast->name);
return -1;
}
ast_setstate(ast, AST_STATE_RING);
if (!p->ringrule) {
ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p,
IKS_RULE_ID, p->ring, IKS_RULE_DONE);
} else {
ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
}
gtalk_invite(p, p->them, p->us, p->sid, 1);
return 0;
}
/*! \brief Hangup a call through the gtalk proxy channel */
static int gtalk_hangup(struct ast_channel *ast)
{
struct gtalk_pvt *p = ast->tech_pvt;
struct gtalk *client;
ast_mutex_lock(&p->lock);
client = p->parent;
p->owner = NULL;
ast->tech_pvt = NULL;
if (!p->alreadygone) {
gtalk_action(client, p, "terminate");
}
ast_mutex_unlock(&p->lock);
gtalk_free_pvt(client, p);
ast_module_unref(ast_module_info->self);
return 0;
}
/*!\brief Part of PBX interface */
static struct ast_channel *gtalk_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
{
struct gtalk_pvt *p = NULL;
struct gtalk *client = NULL;
char *sender = NULL, *to = NULL, *s = NULL;
struct ast_channel *chan = NULL;
if (data) {
s = ast_strdupa(data);
if (s) {
sender = strsep(&s, "/");
if (sender && (sender[0] != '\0')) {
to = strsep(&s, "/");
}
if (!to) {
ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
return NULL;
}
}
}
client = find_gtalk(to, sender);
if (!client) {
ast_log(LOG_WARNING, "Could not find recipient.\n");
return NULL;
}
if (!strcasecmp(client->name, "guest")){
/* the guest account is not tied to any configured XMPP client,
let's set it now */
if (client->connection) {
ASTOBJ_UNREF(client->connection, ast_aji_client_destroy);
}
client->connection = ast_aji_get_client(sender);
if (!client->connection) {
ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", sender);
ASTOBJ_UNREF(client, gtalk_member_destroy);
return NULL;
}
}
ASTOBJ_WRLOCK(client);
p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL);
if (p) {
chan = gtalk_new(client, p, AST_STATE_DOWN, to, requestor ? requestor->linkedid : NULL);
}
ASTOBJ_UNLOCK(client);
return chan;
}
/*! \brief CLI command "gtalk show channels" */
static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
struct gtalk_pvt *p;
struct ast_channel *chan;
int numchans = 0;
char them[AJI_MAX_JIDLEN];
char *jid = NULL;
char *resource = NULL;
switch (cmd) {
case CLI_INIT:
e->command = "gtalk show channels";
e->usage =
"Usage: gtalk show channels\n"
" Shows current state of the Gtalk channels.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3)
return CLI_SHOWUSAGE;
ast_mutex_lock(>alklock);
ast_cli(a->fd, FORMAT, "Channel", "Jabber ID", "Resource", "Read", "Write");
ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, {
ASTOBJ_WRLOCK(iterator);
p = iterator->p;
while(p) {
chan = p->owner;
ast_copy_string(them, p->them, sizeof(them));
jid = them;
resource = strchr(them, '/');
if (!resource)
resource = "None";
else {
*resource = '\0';
resource ++;
}
if (chan)
ast_cli(a->fd, FORMAT,
chan->name,
jid,
resource,
ast_getformatname(chan->readformat),
ast_getformatname(chan->writeformat)
);
else
ast_log(LOG_WARNING, "No available channel\n");
numchans ++;
p = p->next;
}
ASTOBJ_UNLOCK(iterator);
});
ast_mutex_unlock(>alklock);
ast_cli(a->fd, "%d active gtalk channel%s\n", numchans, (numchans != 1) ? "s" : "");
return CLI_SUCCESS;
#undef FORMAT
}
/*! \brief CLI command "gtalk reload"
* \todo XXX TODO make this work. */
#if 0
static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
switch (cmd) {
case CLI_INIT:
e->command = "gtalk reload";
e->usage =
"Usage: gtalk reload\n"
" Reload gtalk channel driver.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
ast_verbose("IT DOES WORK!\n");
return CLI_SUCCESS;
}
#endif
static int gtalk_parser(void *data, ikspak *pak)
{
struct gtalk *client = ASTOBJ_REF((struct gtalk *) data);
int res;
iks *tmp;
if (!strcasecmp(iks_name(pak->query), "jin:jingle") && (tmp = iks_next(pak->query)) && !strcasecmp(iks_name(tmp), "ses:session")) {
ast_debug(1, "New method detected. Skipping jingle offer and using old gtalk method.\n");
pak->query = tmp;
}
if (!strcmp(S_OR(iks_find_attrib(pak->x, "type"), ""), "error")) {
ast_log(LOG_NOTICE, "Remote peer reported an error, trying to establish the call anyway\n");
}
if (ast_strlen_zero(iks_find_attrib(pak->query, "type"))) {
ast_log(LOG_NOTICE, "No attribute \"type\" found. Ignoring message.\n");
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "initiate")) {
/* New call */
gtalk_newcall(client, pak);
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "candidates") || !strcmp(iks_find_attrib(pak->query, "type"), "transport-info")) {
ast_debug(3, "About to add candidate!\n");
res = gtalk_add_candidate(client, pak);
if (!res) {
ast_log(LOG_WARNING, "Could not add any candidate\n");
} else {
ast_debug(3, "Candidate Added!\n");
}
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "accept")) {
gtalk_is_answered(client, pak);
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "transport-accept")) {
gtalk_is_accepted(client, pak);
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "content-info") || iks_find_with_attrib(pak->x, "gtalk", "action", "session-info")) {
gtalk_handle_dtmf(client, pak);
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "terminate")) {
gtalk_hangup_farend(client, pak);
} else if (!strcmp(iks_find_attrib(pak->query, "type"), "reject")) {
gtalk_hangup_farend(client, pak);
}
ASTOBJ_UNREF(client, gtalk_member_destroy);
return IKS_FILTER_EAT;
}
static int gtalk_create_member(char *label, struct ast_variable *var, int allowguest,
struct ast_codec_pref prefs, char *context,
struct gtalk *member)
{
struct aji_client *client;
if (!member)
ast_log(LOG_WARNING, "Out of memory.\n");
ast_copy_string(member->name, label, sizeof(member->name));
ast_copy_string(member->user, label, sizeof(member->user));
ast_copy_string(member->context, context, sizeof(member->context));
member->allowguest = allowguest;
member->prefs = prefs;
while (var) {
if (!strcasecmp(var->name, "username"))
ast_copy_string(member->user, var->value, sizeof(member->user));
else if (!strcasecmp(var->name, "disallow"))
ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
else if (!strcasecmp(var->name, "allow"))
ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
else if (!strcasecmp(var->name, "context"))
ast_copy_string(member->context, var->value, sizeof(member->context));
else if (!strcasecmp(var->name, "parkinglot"))
ast_copy_string(member->parkinglot, var->value, sizeof(member->parkinglot));
else if (!strcasecmp(var->name, "connection")) {
if ((client = ast_aji_get_client(var->value))) {
member->connection = client;
iks_filter_add_rule(client->f, gtalk_parser, member,
IKS_RULE_TYPE, IKS_PAK_IQ,
IKS_RULE_FROM_PARTIAL, member->user,
IKS_RULE_NS, GOOGLE_NS,
IKS_RULE_DONE);
} else {
ast_log(LOG_ERROR, "connection referenced not found!\n");
return 0;
}
}
var = var->next;
}
if (member->connection && member->user)
member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
else {
ast_log(LOG_ERROR, "No Connection or Username!\n");
}
return 1;
}
static int gtalk_load_config(void)
{
char *cat = NULL;
struct ast_config *cfg = NULL;
char context[AST_MAX_CONTEXT] = "";
char parkinglot[AST_MAX_CONTEXT] = "";
int allowguest = 1;
struct ast_variable *var;
struct gtalk *member;
struct ast_codec_pref prefs = { "", };
struct aji_client_container *clients;
struct gtalk_candidate *global_candidates = NULL;
struct hostent *hp;
struct ast_hostent ahp;
struct ast_flags config_flags = { 0 };
cfg = ast_config_load(GOOGLE_CONFIG, config_flags);
if (!cfg) {
return 0;
} else if (cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", GOOGLE_CONFIG);
return 0;
}
/* Copy the default jb config over global_jbconf */
memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
/* set defaults */
memset(&stunaddr, 0, sizeof(stunaddr));
cat = ast_category_browse(cfg, NULL);
for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
/* handle jb conf */
if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
continue;
if (!strcasecmp(var->name, "allowguest")) {
allowguest = (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
} else if (!strcasecmp(var->name, "disallow")) {
ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
} else if (!strcasecmp(var->name, "allow")) {
ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
} else if (!strcasecmp(var->name, "context")) {
ast_copy_string(context, var->value, sizeof(context));
} else if (!strcasecmp(var->name, "externip")) {
ast_copy_string(externip, var->value, sizeof(externip));
} else if (!strcasecmp(var->name, "parkinglot")) {
ast_copy_string(parkinglot, var->value, sizeof(parkinglot));
} else if (!strcasecmp(var->name, "bindaddr")) {
if (!(hp = ast_gethostbyname(var->value, &ahp))) {
ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
} else {
memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
}
} else if (!strcasecmp(var->name, "stunaddr")) {
stunaddr.sin_port = htons(STANDARD_STUN_PORT);
if (ast_parse_arg(var->value, PARSE_INADDR, &stunaddr)) {
ast_log(LOG_WARNING, "Invalid STUN server address: %s\n", var->value);
}
}
}
while (cat) {
if (strcasecmp(cat, "general")) {
var = ast_variable_browse(cfg, cat);
member = ast_calloc(1, sizeof(*member));
ASTOBJ_INIT(member);
ASTOBJ_WRLOCK(member);
if (!strcasecmp(cat, "guest")) {
ast_copy_string(member->name, "guest", sizeof(member->name));
ast_copy_string(member->user, "guest", sizeof(member->user));
ast_copy_string(member->context, context, sizeof(member->context));
ast_copy_string(member->parkinglot, parkinglot, sizeof(member->parkinglot));
member->allowguest = allowguest;
member->prefs = prefs;
while (var) {
if (!strcasecmp(var->name, "disallow")) {
ast_parse_allow_disallow(&member->prefs, &member->capability,
var->value, 0);
} else if (!strcasecmp(var->name, "allow")) {
ast_parse_allow_disallow(&member->prefs, &member->capability,
var->value, 1);
} else if (!strcasecmp(var->name, "context")) {
ast_copy_string(member->context, var->value,
sizeof(member->context));
} else if (!strcasecmp(var->name, "parkinglot")) {
ast_copy_string(member->parkinglot, var->value,
sizeof(member->parkinglot));
}
var = var->next;
}
ASTOBJ_UNLOCK(member);
clients = ast_aji_get_clients();
if (clients) {
ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
ASTOBJ_WRLOCK(iterator);
ASTOBJ_WRLOCK(member);
if (member->connection) {
ASTOBJ_UNREF(member->connection, ast_aji_client_destroy);
}
member->connection = NULL;
iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, GOOGLE_NS, IKS_RULE_DONE);
iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, GOOGLE_JINGLE_NS, IKS_RULE_DONE);
iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, "http://jabber.org/protocol/gtalk", IKS_RULE_DONE);
ASTOBJ_UNLOCK(member);
ASTOBJ_UNLOCK(iterator);
});
ASTOBJ_CONTAINER_LINK(>alk_list, member);
ASTOBJ_UNREF(member, gtalk_member_destroy);
} else {
ASTOBJ_UNLOCK(member);
ASTOBJ_UNREF(member, gtalk_member_destroy);
}
} else {
ASTOBJ_UNLOCK(member);
if (gtalk_create_member(cat, var, allowguest, prefs, context, member))
ASTOBJ_CONTAINER_LINK(>alk_list, member);
ASTOBJ_UNREF(member, gtalk_member_destroy);
}
}
cat = ast_category_browse(cfg, cat);
}
ast_config_destroy(cfg);
gtalk_update_externip();
gtalk_free_candidates(global_candidates);
return 1;
}
/*! \brief Load module into PBX, register channel */
static int load_module(void)
{
struct ast_sockaddr bindaddr_tmp;
struct ast_sockaddr ourip_tmp;
char *jabber_loaded = ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
free(jabber_loaded);
if (!jabber_loaded) {
/* If embedded, check for a different module name */
jabber_loaded = ast_module_helper("", "res_jabber", 0, 0, 0, 0);
free(jabber_loaded);
if (!jabber_loaded) {
ast_log(LOG_ERROR, "chan_gtalk.so depends upon res_jabber.so\n");
return AST_MODULE_LOAD_DECLINE;
}
}
ASTOBJ_CONTAINER_INIT(>alk_list);
if (!gtalk_load_config()) {
ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG);
return 0;
}
sched = sched_context_create();
if (!sched) {
ast_log(LOG_WARNING, "Unable to create schedule context\n");
}
io = io_context_create();
if (!io) {
ast_log(LOG_WARNING, "Unable to create I/O context\n");
}
ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
if (gtalk_get_local_ip(&ourip_tmp)) {
ast_log(LOG_WARNING, "Unable to get own IP address, Gtalk disabled\n");
return 0;
}
__ourip.s_addr = htonl(ast_sockaddr_ipv4(&ourip_tmp));
ast_rtp_glue_register(>alk_rtp_glue);
ast_cli_register_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
/* Make sure we can register our channel type */
if (ast_channel_register(>alk_tech)) {
ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type);
return -1;
}
return 0;
}
/*! \brief Reload module
* \todo XXX TODO make this work. */
#if 0
static int reload(void)
{
return 0;
}
#endif
/*! \brief Unload the gtalk channel from Asterisk */
static int unload_module(void)
{
struct gtalk_pvt *privates = NULL;
ast_cli_unregister_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
/* First, take us out of the channel loop */
ast_channel_unregister(>alk_tech);
ast_rtp_glue_unregister(>alk_rtp_glue);
if (!ast_mutex_lock(>alklock)) {
/* Hangup all interfaces if they have an owner */
ASTOBJ_CONTAINER_TRAVERSE(>alk_list, 1, {
ASTOBJ_WRLOCK(iterator);
privates = iterator->p;
while(privates) {
if (privates->owner)
ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD);
privates = privates->next;
}
iterator->p = NULL;
ASTOBJ_UNLOCK(iterator);
});
ast_mutex_unlock(>alklock);
} else {
ast_log(LOG_WARNING, "Unable to lock the monitor\n");
return -1;
}
ASTOBJ_CONTAINER_DESTROYALL(>alk_list, gtalk_member_destroy);
ASTOBJ_CONTAINER_DESTROY(>alk_list);
return 0;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gtalk Channel Driver",
.load = load_module,
.unload = unload_module,
/* .reload = reload, */
.load_pri = AST_MODPRI_CHANNEL_DRIVER,
);
|