chan_gtalk.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Matt O'Gorman <mogorman@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \author Matt O'Gorman <mogorman@digium.com>
  21. *
  22. * \brief Gtalk Channel Driver, until google/libjingle works with jingle spec
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*** MODULEINFO
  27. <depend>iksemel</depend>
  28. <depend>res_jabber</depend>
  29. <use>openssl</use>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <sys/socket.h>
  34. #include <fcntl.h>
  35. #include <netdb.h>
  36. #include <netinet/in.h>
  37. #include <arpa/inet.h>
  38. #include <sys/signal.h>
  39. #include <iksemel.h>
  40. #include <pthread.h>
  41. #include <ctype.h>
  42. #include "asterisk/lock.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/config.h"
  45. #include "asterisk/module.h"
  46. #include "asterisk/pbx.h"
  47. #include "asterisk/sched.h"
  48. #include "asterisk/io.h"
  49. #include "asterisk/rtp.h"
  50. #include "asterisk/acl.h"
  51. #include "asterisk/callerid.h"
  52. #include "asterisk/file.h"
  53. #include "asterisk/cli.h"
  54. #include "asterisk/app.h"
  55. #include "asterisk/musiconhold.h"
  56. #include "asterisk/manager.h"
  57. #include "asterisk/stringfields.h"
  58. #include "asterisk/utils.h"
  59. #include "asterisk/causes.h"
  60. #include "asterisk/astobj.h"
  61. #include "asterisk/abstract_jb.h"
  62. #include "asterisk/jabber.h"
  63. #define GOOGLE_CONFIG "gtalk.conf"
  64. #define GOOGLE_NS "http://www.google.com/session"
  65. /*! Global jitterbuffer configuration - by default, jb is disabled */
  66. static struct ast_jb_conf default_jbconf =
  67. {
  68. .flags = 0,
  69. .max_size = -1,
  70. .resync_threshold = -1,
  71. .impl = "",
  72. .target_extra = -1,
  73. };
  74. static struct ast_jb_conf global_jbconf;
  75. enum gtalk_protocol {
  76. AJI_PROTOCOL_UDP = 1,
  77. AJI_PROTOCOL_SSLTCP = 2,
  78. };
  79. enum gtalk_connect_type {
  80. AJI_CONNECT_STUN = 1,
  81. AJI_CONNECT_LOCAL = 2,
  82. AJI_CONNECT_RELAY = 3,
  83. };
  84. struct gtalk_pvt {
  85. ast_mutex_t lock; /*!< Channel private lock */
  86. time_t laststun;
  87. struct gtalk *parent; /*!< Parent client */
  88. char sid[100];
  89. char us[AJI_MAX_JIDLEN];
  90. char them[AJI_MAX_JIDLEN];
  91. char ring[10]; /*!< Message ID of ring */
  92. iksrule *ringrule; /*!< Rule for matching RING request */
  93. int initiator; /*!< If we're the initiator */
  94. int alreadygone;
  95. int capability;
  96. struct ast_codec_pref prefs;
  97. struct gtalk_candidate *theircandidates;
  98. struct gtalk_candidate *ourcandidates;
  99. char cid_num[80]; /*!< Caller ID num */
  100. char cid_name[80]; /*!< Caller ID name */
  101. char exten[80]; /*!< Called extension */
  102. struct ast_channel *owner; /*!< Master Channel */
  103. struct ast_rtp *rtp; /*!< RTP audio session */
  104. struct ast_rtp *vrtp; /*!< RTP video session */
  105. int jointcapability; /*!< Supported capability at both ends (codecs ) */
  106. int peercapability;
  107. struct gtalk_pvt *next; /* Next entity */
  108. };
  109. struct gtalk_candidate {
  110. char name[100];
  111. enum gtalk_protocol protocol;
  112. double preference;
  113. char username[100];
  114. char password[100];
  115. enum gtalk_connect_type type;
  116. char network[6];
  117. int generation;
  118. char ip[16];
  119. int port;
  120. int receipt;
  121. struct gtalk_candidate *next;
  122. };
  123. struct gtalk {
  124. ASTOBJ_COMPONENTS(struct gtalk);
  125. struct aji_client *connection;
  126. struct aji_buddy *buddy;
  127. struct gtalk_pvt *p;
  128. struct ast_codec_pref prefs;
  129. int amaflags; /*!< AMA Flags */
  130. char user[AJI_MAX_JIDLEN];
  131. char context[AST_MAX_CONTEXT];
  132. char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
  133. int capability;
  134. ast_group_t callgroup; /*!< Call group */
  135. ast_group_t pickupgroup; /*!< Pickup group */
  136. int callingpres; /*!< Calling presentation */
  137. int allowguest;
  138. char language[MAX_LANGUAGE]; /*!< Default language for prompts */
  139. char musicclass[MAX_MUSICCLASS]; /*!< Music on Hold class */
  140. };
  141. struct gtalk_container {
  142. ASTOBJ_CONTAINER_COMPONENTS(struct gtalk);
  143. };
  144. static const char desc[] = "Gtalk Channel";
  145. static int global_capability = AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_GSM | AST_FORMAT_H263;
  146. AST_MUTEX_DEFINE_STATIC(gtalklock); /*!< Protect the interface list (of gtalk_pvt's) */
  147. /* Forward declarations */
  148. static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause);
  149. static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration);
  150. static int gtalk_digit_begin(struct ast_channel *ast, char digit);
  151. static int gtalk_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  152. static int gtalk_call(struct ast_channel *ast, char *dest, int timeout);
  153. static int gtalk_hangup(struct ast_channel *ast);
  154. static int gtalk_answer(struct ast_channel *ast);
  155. static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action);
  156. static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p);
  157. static int gtalk_newcall(struct gtalk *client, ikspak *pak);
  158. static struct ast_frame *gtalk_read(struct ast_channel *ast);
  159. static int gtalk_write(struct ast_channel *ast, struct ast_frame *f);
  160. static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  161. static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  162. static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
  163. static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid);
  164. static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  165. static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  166. /*----- RTP interface functions */
  167. static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp,
  168. struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active);
  169. static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp);
  170. static int gtalk_get_codec(struct ast_channel *chan);
  171. /*! \brief PBX interface structure for channel registration */
  172. static const struct ast_channel_tech gtalk_tech = {
  173. .type = "Gtalk",
  174. .description = "Gtalk Channel Driver",
  175. .capabilities = AST_FORMAT_AUDIO_MASK,
  176. .requester = gtalk_request,
  177. .send_digit_begin = gtalk_digit_begin,
  178. .send_digit_end = gtalk_digit_end,
  179. .bridge = ast_rtp_bridge,
  180. .call = gtalk_call,
  181. .hangup = gtalk_hangup,
  182. .answer = gtalk_answer,
  183. .read = gtalk_read,
  184. .write = gtalk_write,
  185. .exception = gtalk_read,
  186. .indicate = gtalk_indicate,
  187. .fixup = gtalk_fixup,
  188. .send_html = gtalk_sendhtml,
  189. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
  190. };
  191. static struct sockaddr_in bindaddr = { 0, }; /*!< The address we bind to */
  192. static struct sched_context *sched; /*!< The scheduling context */
  193. static struct io_context *io; /*!< The IO context */
  194. static struct in_addr __ourip;
  195. /*! \brief RTP driver interface */
  196. static struct ast_rtp_protocol gtalk_rtp = {
  197. type: "Gtalk",
  198. get_rtp_info: gtalk_get_rtp_peer,
  199. set_rtp_peer: gtalk_set_rtp_peer,
  200. get_codec: gtalk_get_codec,
  201. };
  202. static struct ast_cli_entry gtalk_cli[] = {
  203. AST_CLI_DEFINE(gtalk_do_reload, "Reload GoogleTalk configuration"),
  204. AST_CLI_DEFINE(gtalk_show_channels, "Show GoogleTalk channels"),
  205. };
  206. static char externip[16];
  207. static struct gtalk_container gtalk_list;
  208. static void gtalk_member_destroy(struct gtalk *obj)
  209. {
  210. ast_free(obj);
  211. }
  212. static struct gtalk *find_gtalk(char *name, char *connection)
  213. {
  214. struct gtalk *gtalk = NULL;
  215. char *domain = NULL , *s = NULL;
  216. if (strchr(connection, '@')) {
  217. s = ast_strdupa(connection);
  218. domain = strsep(&s, "@");
  219. ast_verbose("OOOOH domain = %s\n", domain);
  220. }
  221. gtalk = ASTOBJ_CONTAINER_FIND(&gtalk_list, name);
  222. if (!gtalk && strchr(name, '@'))
  223. gtalk = ASTOBJ_CONTAINER_FIND_FULL(&gtalk_list, name, user,,, strcasecmp);
  224. if (!gtalk) {
  225. /* guest call */
  226. ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
  227. ASTOBJ_RDLOCK(iterator);
  228. if (!strcasecmp(iterator->name, "guest")) {
  229. gtalk = iterator;
  230. }
  231. ASTOBJ_UNLOCK(iterator);
  232. if (gtalk)
  233. break;
  234. });
  235. }
  236. return gtalk;
  237. }
  238. static int add_codec_to_answer(const struct gtalk_pvt *p, int codec, iks *dcodecs)
  239. {
  240. int res = 0;
  241. char *format = ast_getformatname(codec);
  242. if (!strcasecmp("ulaw", format)) {
  243. iks *payload_eg711u, *payload_pcmu;
  244. payload_pcmu = iks_new("payload-type");
  245. payload_eg711u = iks_new("payload-type");
  246. if(!payload_eg711u || !payload_pcmu) {
  247. iks_delete(payload_pcmu);
  248. iks_delete(payload_eg711u);
  249. ast_log(LOG_WARNING,"Failed to allocate iks node");
  250. return -1;
  251. }
  252. iks_insert_attrib(payload_pcmu, "id", "0");
  253. iks_insert_attrib(payload_pcmu, "name", "PCMU");
  254. iks_insert_attrib(payload_pcmu, "clockrate","8000");
  255. iks_insert_attrib(payload_pcmu, "bitrate","64000");
  256. iks_insert_attrib(payload_eg711u, "id", "100");
  257. iks_insert_attrib(payload_eg711u, "name", "EG711U");
  258. iks_insert_attrib(payload_eg711u, "clockrate","8000");
  259. iks_insert_attrib(payload_eg711u, "bitrate","64000");
  260. iks_insert_node(dcodecs, payload_pcmu);
  261. iks_insert_node(dcodecs, payload_eg711u);
  262. res ++;
  263. }
  264. if (!strcasecmp("alaw", format)) {
  265. iks *payload_eg711a, *payload_pcma;
  266. payload_pcma = iks_new("payload-type");
  267. payload_eg711a = iks_new("payload-type");
  268. if(!payload_eg711a || !payload_pcma) {
  269. iks_delete(payload_eg711a);
  270. iks_delete(payload_pcma);
  271. ast_log(LOG_WARNING,"Failed to allocate iks node");
  272. return -1;
  273. }
  274. iks_insert_attrib(payload_pcma, "id", "8");
  275. iks_insert_attrib(payload_pcma, "name", "PCMA");
  276. iks_insert_attrib(payload_pcma, "clockrate","8000");
  277. iks_insert_attrib(payload_pcma, "bitrate","64000");
  278. payload_eg711a = iks_new("payload-type");
  279. iks_insert_attrib(payload_eg711a, "id", "101");
  280. iks_insert_attrib(payload_eg711a, "name", "EG711A");
  281. iks_insert_attrib(payload_eg711a, "clockrate","8000");
  282. iks_insert_attrib(payload_eg711a, "bitrate","64000");
  283. iks_insert_node(dcodecs, payload_pcma);
  284. iks_insert_node(dcodecs, payload_eg711a);
  285. res ++;
  286. }
  287. if (!strcasecmp("ilbc", format)) {
  288. iks *payload_ilbc = iks_new("payload-type");
  289. if(!payload_ilbc) {
  290. ast_log(LOG_WARNING,"Failed to allocate iks node");
  291. return -1;
  292. }
  293. iks_insert_attrib(payload_ilbc, "id", "97");
  294. iks_insert_attrib(payload_ilbc, "name", "iLBC");
  295. iks_insert_attrib(payload_ilbc, "clockrate","8000");
  296. iks_insert_attrib(payload_ilbc, "bitrate","13300");
  297. iks_insert_node(dcodecs, payload_ilbc);
  298. res ++;
  299. }
  300. if (!strcasecmp("g723", format)) {
  301. iks *payload_g723 = iks_new("payload-type");
  302. if(!payload_g723) {
  303. ast_log(LOG_WARNING,"Failed to allocate iks node");
  304. return -1;
  305. }
  306. iks_insert_attrib(payload_g723, "id", "4");
  307. iks_insert_attrib(payload_g723, "name", "G723");
  308. iks_insert_attrib(payload_g723, "clockrate","8000");
  309. iks_insert_attrib(payload_g723, "bitrate","6300");
  310. iks_insert_node(dcodecs, payload_g723);
  311. res ++;
  312. }
  313. if (!strcasecmp("speex", format)) {
  314. iks *payload_speex = iks_new("payload-type");
  315. if(!payload_speex) {
  316. ast_log(LOG_WARNING,"Failed to allocate iks node");
  317. return -1;
  318. }
  319. iks_insert_attrib(payload_speex, "id", "110");
  320. iks_insert_attrib(payload_speex, "name", "speex");
  321. iks_insert_attrib(payload_speex, "clockrate","8000");
  322. iks_insert_attrib(payload_speex, "bitrate","11000");
  323. iks_insert_node(dcodecs, payload_speex);
  324. res++;
  325. }
  326. if (!strcasecmp("gsm", format)) {
  327. iks *payload_gsm = iks_new("payload-type");
  328. if(!payload_gsm) {
  329. ast_log(LOG_WARNING,"Failed to allocate iks node");
  330. return -1;
  331. }
  332. iks_insert_attrib(payload_gsm, "id", "103");
  333. iks_insert_attrib(payload_gsm, "name", "gsm");
  334. iks_insert_node(dcodecs, payload_gsm);
  335. res++;
  336. }
  337. ast_rtp_lookup_code(p->rtp, 1, codec);
  338. return res;
  339. }
  340. static int gtalk_invite(struct gtalk_pvt *p, char *to, char *from, char *sid, int initiator)
  341. {
  342. struct gtalk *client = p->parent;
  343. iks *iq, *gtalk, *dcodecs, *payload_telephone, *transport;
  344. int x;
  345. int pref_codec = 0;
  346. int alreadysent = 0;
  347. int codecs_num = 0;
  348. char *lowerto = NULL;
  349. iq = iks_new("iq");
  350. gtalk = iks_new("session");
  351. dcodecs = iks_new("description");
  352. transport = iks_new("transport");
  353. payload_telephone = iks_new("payload-type");
  354. if (!(iq && gtalk && dcodecs && transport && payload_telephone)){
  355. iks_delete(iq);
  356. iks_delete(gtalk);
  357. iks_delete(dcodecs);
  358. iks_delete(transport);
  359. iks_delete(payload_telephone);
  360. ast_log(LOG_ERROR, "Could not allocate iksemel nodes\n");
  361. return 0;
  362. }
  363. iks_insert_attrib(dcodecs, "xmlns", "http://www.google.com/session/phone");
  364. iks_insert_attrib(dcodecs, "xml:lang", "en");
  365. for (x = 0; x < 32; x++) {
  366. if (!(pref_codec = ast_codec_pref_index(&client->prefs, x)))
  367. break;
  368. if (!(client->capability & pref_codec))
  369. continue;
  370. if (alreadysent & pref_codec)
  371. continue;
  372. codecs_num = add_codec_to_answer(p, pref_codec, dcodecs);
  373. alreadysent |= pref_codec;
  374. }
  375. if (codecs_num) {
  376. /* only propose DTMF within an audio session */
  377. iks_insert_attrib(payload_telephone, "id", "106");
  378. iks_insert_attrib(payload_telephone, "name", "telephone-event");
  379. iks_insert_attrib(payload_telephone, "clockrate", "8000");
  380. }
  381. iks_insert_attrib(transport,"xmlns","http://www.google.com/transport/p2p");
  382. iks_insert_attrib(iq, "type", "set");
  383. iks_insert_attrib(iq, "to", to);
  384. iks_insert_attrib(iq, "from", from);
  385. iks_insert_attrib(iq, "id", client->connection->mid);
  386. ast_aji_increment_mid(client->connection->mid);
  387. iks_insert_attrib(gtalk, "xmlns", "http://www.google.com/session");
  388. iks_insert_attrib(gtalk, "type",initiator ? "initiate": "accept");
  389. /* put the initiator attribute to lower case if we receive the call
  390. * otherwise GoogleTalk won't establish the session */
  391. if (!initiator) {
  392. char c;
  393. char *t = lowerto = ast_strdupa(to);
  394. while (((c = *t) != '/') && (*t++ = tolower(c)));
  395. }
  396. iks_insert_attrib(gtalk, "initiator", initiator ? from : lowerto);
  397. iks_insert_attrib(gtalk, "id", sid);
  398. iks_insert_node(iq, gtalk);
  399. iks_insert_node(gtalk, dcodecs);
  400. iks_insert_node(gtalk, transport);
  401. iks_insert_node(dcodecs, payload_telephone);
  402. ast_aji_send(client->connection, iq);
  403. iks_delete(payload_telephone);
  404. iks_delete(transport);
  405. iks_delete(dcodecs);
  406. iks_delete(gtalk);
  407. iks_delete(iq);
  408. return 1;
  409. }
  410. static int gtalk_invite_response(struct gtalk_pvt *p, char *to , char *from, char *sid, int initiator)
  411. {
  412. iks *iq, *session, *transport;
  413. char *lowerto = NULL;
  414. iq = iks_new("iq");
  415. session = iks_new("session");
  416. transport = iks_new("transport");
  417. if(!(iq && session && transport)) {
  418. iks_delete(iq);
  419. iks_delete(session);
  420. iks_delete(transport);
  421. ast_log(LOG_ERROR, " Unable to allocate IKS node\n");
  422. return -1;
  423. }
  424. iks_insert_attrib(iq, "from", from);
  425. iks_insert_attrib(iq, "to", to);
  426. iks_insert_attrib(iq, "type", "set");
  427. iks_insert_attrib(iq, "id",p->parent->connection->mid);
  428. ast_aji_increment_mid(p->parent->connection->mid);
  429. iks_insert_attrib(session, "type", "transport-accept");
  430. iks_insert_attrib(session, "id", sid);
  431. /* put the initiator attribute to lower case if we receive the call
  432. * otherwise GoogleTalk won't establish the session */
  433. if (!initiator) {
  434. char c;
  435. char *t = lowerto = ast_strdupa(to);
  436. while (((c = *t) != '/') && (*t++ = tolower(c)));
  437. }
  438. iks_insert_attrib(session, "initiator", initiator ? from : lowerto);
  439. iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
  440. iks_insert_attrib(transport, "xmlns", "http://www.google.com/transport/p2p");
  441. iks_insert_node(iq,session);
  442. iks_insert_node(session,transport);
  443. ast_aji_send(p->parent->connection, iq);
  444. iks_delete(transport);
  445. iks_delete(session);
  446. iks_delete(iq);
  447. return 1;
  448. }
  449. static int gtalk_ringing_ack(void *data, ikspak *pak)
  450. {
  451. struct gtalk_pvt *p = data;
  452. if (p->ringrule)
  453. iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
  454. p->ringrule = NULL;
  455. if (p->owner)
  456. ast_queue_control(p->owner, AST_CONTROL_RINGING);
  457. return IKS_FILTER_EAT;
  458. }
  459. static int gtalk_answer(struct ast_channel *ast)
  460. {
  461. struct gtalk_pvt *p = ast->tech_pvt;
  462. int res = 0;
  463. ast_debug(1, "Answer!\n");
  464. ast_mutex_lock(&p->lock);
  465. gtalk_invite(p, p->them, p->us,p->sid, 0);
  466. manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
  467. ast->name, "GTALK", p->sid);
  468. ast_mutex_unlock(&p->lock);
  469. return res;
  470. }
  471. static enum ast_rtp_get_result gtalk_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
  472. {
  473. struct gtalk_pvt *p = chan->tech_pvt;
  474. enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
  475. if (!p)
  476. return res;
  477. ast_mutex_lock(&p->lock);
  478. if (p->rtp){
  479. *rtp = p->rtp;
  480. res = AST_RTP_TRY_PARTIAL;
  481. }
  482. ast_mutex_unlock(&p->lock);
  483. return res;
  484. }
  485. static int gtalk_get_codec(struct ast_channel *chan)
  486. {
  487. struct gtalk_pvt *p = chan->tech_pvt;
  488. return p->peercapability;
  489. }
  490. static int gtalk_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, struct ast_rtp *trtp, int codecs, int nat_active)
  491. {
  492. struct gtalk_pvt *p;
  493. p = chan->tech_pvt;
  494. if (!p)
  495. return -1;
  496. ast_mutex_lock(&p->lock);
  497. /* if (rtp)
  498. ast_rtp_get_peer(rtp, &p->redirip);
  499. else
  500. memset(&p->redirip, 0, sizeof(p->redirip));
  501. p->redircodecs = codecs; */
  502. /* Reset lastrtprx timer */
  503. ast_mutex_unlock(&p->lock);
  504. return 0;
  505. }
  506. static int gtalk_response(struct gtalk *client, char *from, ikspak *pak, const char *reasonstr, const char *reasonstr2)
  507. {
  508. iks *response = NULL, *error = NULL, *reason = NULL;
  509. int res = -1;
  510. response = iks_new("iq");
  511. if (response) {
  512. iks_insert_attrib(response, "type", "result");
  513. iks_insert_attrib(response, "from", from);
  514. iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
  515. iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
  516. if (reasonstr) {
  517. error = iks_new("error");
  518. if (error) {
  519. iks_insert_attrib(error, "type", "cancel");
  520. reason = iks_new(reasonstr);
  521. if (reason)
  522. iks_insert_node(error, reason);
  523. iks_insert_node(response, error);
  524. }
  525. }
  526. ast_aji_send(client->connection, response);
  527. res = 0;
  528. }
  529. iks_delete(reason);
  530. iks_delete(error);
  531. iks_delete(response);
  532. return res;
  533. }
  534. static int gtalk_is_answered(struct gtalk *client, ikspak *pak)
  535. {
  536. struct gtalk_pvt *tmp;
  537. char *from;
  538. iks *codec;
  539. char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
  540. int peernoncodeccapability;
  541. ast_log(LOG_DEBUG, "The client is %s\n", client->name);
  542. /* Make sure our new call doesn't exist yet */
  543. for (tmp = client->p; tmp; tmp = tmp->next) {
  544. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
  545. break;
  546. }
  547. /* codec points to the first <payload-type/> tag */
  548. codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
  549. while (codec) {
  550. ast_rtp_set_m_type(tmp->rtp, atoi(iks_find_attrib(codec, "id")));
  551. ast_rtp_set_rtpmap_type(tmp->rtp, atoi(iks_find_attrib(codec, "id")), "audio", iks_find_attrib(codec, "name"), 0);
  552. codec = iks_next_tag(codec);
  553. }
  554. /* Now gather all of the codecs that we are asked for */
  555. ast_rtp_get_current_formats(tmp->rtp, &tmp->peercapability, &peernoncodeccapability);
  556. /* at this point, we received an awser from the remote Gtalk client,
  557. which allows us to compare capabilities */
  558. tmp->jointcapability = tmp->capability & tmp->peercapability;
  559. if (!tmp->jointcapability) {
  560. ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, tmp->capability),
  561. ast_getformatname_multiple(s2, BUFSIZ, tmp->peercapability),
  562. ast_getformatname_multiple(s3, BUFSIZ, tmp->jointcapability));
  563. /* close session if capabilities don't match */
  564. ast_queue_hangup(tmp->owner);
  565. return -1;
  566. }
  567. from = iks_find_attrib(pak->x, "to");
  568. if(!from)
  569. from = client->connection->jid->full;
  570. if (tmp) {
  571. if (tmp->owner)
  572. ast_queue_control(tmp->owner, AST_CONTROL_ANSWER);
  573. } else
  574. ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
  575. gtalk_response(client, from, pak, NULL, NULL);
  576. return 1;
  577. }
  578. static int gtalk_is_accepted(struct gtalk *client, ikspak *pak)
  579. {
  580. struct gtalk_pvt *tmp;
  581. char *from;
  582. ast_log(LOG_DEBUG, "The client is %s\n", client->name);
  583. /* find corresponding call */
  584. for (tmp = client->p; tmp; tmp = tmp->next) {
  585. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
  586. break;
  587. }
  588. from = iks_find_attrib(pak->x, "to");
  589. if(!from)
  590. from = client->connection->jid->full;
  591. if (!tmp)
  592. ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
  593. /* answer 'iq' packet to let the remote peer know that we're alive */
  594. gtalk_response(client, from, pak, NULL, NULL);
  595. return 1;
  596. }
  597. static int gtalk_handle_dtmf(struct gtalk *client, ikspak *pak)
  598. {
  599. struct gtalk_pvt *tmp;
  600. iks *dtmfnode = NULL, *dtmfchild = NULL;
  601. char *dtmf;
  602. char *from;
  603. /* Make sure our new call doesn't exist yet */
  604. for (tmp = client->p; tmp; tmp = tmp->next) {
  605. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid) || iks_find_with_attrib(pak->x, "gtalk", "sid", tmp->sid))
  606. break;
  607. }
  608. from = iks_find_attrib(pak->x, "to");
  609. if(!from)
  610. from = client->connection->jid->full;
  611. if (tmp) {
  612. if(iks_find_with_attrib(pak->x, "dtmf-method", "method", "rtp")) {
  613. gtalk_response(client, from, pak,
  614. "feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
  615. "unsupported-dtmf-method xmlns='http://jabber.org/protocol/gtalk/info/dtmf#errors'");
  616. return -1;
  617. }
  618. if ((dtmfnode = iks_find(pak->x, "dtmf"))) {
  619. if((dtmf = iks_find_attrib(dtmfnode, "code"))) {
  620. if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-up")) {
  621. struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
  622. f.subclass = dtmf[0];
  623. ast_queue_frame(tmp->owner, &f);
  624. ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
  625. } else if(iks_find_with_attrib(pak->x, "dtmf", "action", "button-down")) {
  626. struct ast_frame f = {AST_FRAME_DTMF_END, };
  627. f.subclass = dtmf[0];
  628. ast_queue_frame(tmp->owner, &f);
  629. ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
  630. } else if(iks_find_attrib(pak->x, "dtmf")) { /* 250 millasecond default */
  631. struct ast_frame f = {AST_FRAME_DTMF, };
  632. f.subclass = dtmf[0];
  633. ast_queue_frame(tmp->owner, &f);
  634. ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
  635. }
  636. }
  637. } else if ((dtmfnode = iks_find_with_attrib(pak->x, "gtalk", "action", "session-info"))) {
  638. if((dtmfchild = iks_find(dtmfnode, "dtmf"))) {
  639. if((dtmf = iks_find_attrib(dtmfchild, "code"))) {
  640. if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-up")) {
  641. struct ast_frame f = {AST_FRAME_DTMF_END, };
  642. f.subclass = dtmf[0];
  643. ast_queue_frame(tmp->owner, &f);
  644. ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
  645. } else if(iks_find_with_attrib(dtmfnode, "dtmf", "action", "button-down")) {
  646. struct ast_frame f = {AST_FRAME_DTMF_BEGIN, };
  647. f.subclass = dtmf[0];
  648. ast_queue_frame(tmp->owner, &f);
  649. ast_verbose("GOOGLE! DTMF-relay event received: %c\n", f.subclass);
  650. }
  651. }
  652. }
  653. }
  654. gtalk_response(client, from, pak, NULL, NULL);
  655. return 1;
  656. } else
  657. ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
  658. gtalk_response(client, from, pak, NULL, NULL);
  659. return 1;
  660. }
  661. static int gtalk_hangup_farend(struct gtalk *client, ikspak *pak)
  662. {
  663. struct gtalk_pvt *tmp;
  664. char *from;
  665. ast_debug(1, "The client is %s\n", client->name);
  666. /* Make sure our new call doesn't exist yet */
  667. for (tmp = client->p; tmp; tmp = tmp->next) {
  668. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid))
  669. break;
  670. }
  671. from = iks_find_attrib(pak->x, "to");
  672. if(!from)
  673. from = client->connection->jid->full;
  674. if (tmp) {
  675. tmp->alreadygone = 1;
  676. if (tmp->owner)
  677. ast_queue_hangup(tmp->owner);
  678. } else
  679. ast_log(LOG_NOTICE, "Whoa, didn't find call!\n");
  680. gtalk_response(client, from, pak, NULL, NULL);
  681. return 1;
  682. }
  683. static int gtalk_create_candidates(struct gtalk *client, struct gtalk_pvt *p, char *sid, char *from, char *to)
  684. {
  685. struct gtalk_candidate *tmp;
  686. struct aji_client *c = client->connection;
  687. struct gtalk_candidate *ours1 = NULL, *ours2 = NULL;
  688. struct sockaddr_in sin;
  689. struct sockaddr_in dest;
  690. struct in_addr us;
  691. iks *iq, *gtalk, *candidate, *transport;
  692. char user[17], pass[17], preference[5], port[7];
  693. char *lowerfrom = NULL;
  694. iq = iks_new("iq");
  695. gtalk = iks_new("session");
  696. candidate = iks_new("candidate");
  697. transport = iks_new("transport");
  698. if (!iq || !gtalk || !candidate || !transport) {
  699. ast_log(LOG_ERROR, "Memory allocation error\n");
  700. goto safeout;
  701. }
  702. ours1 = ast_calloc(1, sizeof(*ours1));
  703. ours2 = ast_calloc(1, sizeof(*ours2));
  704. if (!ours1 || !ours2)
  705. goto safeout;
  706. iks_insert_attrib(transport, "xmlns","http://www.google.com/transport/p2p");
  707. iks_insert_node(iq, gtalk);
  708. iks_insert_node(gtalk,transport);
  709. iks_insert_node(transport, candidate);
  710. for (; p; p = p->next) {
  711. if (!strcasecmp(p->sid, sid))
  712. break;
  713. }
  714. if (!p) {
  715. ast_log(LOG_NOTICE, "No matching gtalk session - SID %s!\n", sid);
  716. goto safeout;
  717. }
  718. ast_rtp_get_us(p->rtp, &sin);
  719. ast_find_ourip(&us, bindaddr);
  720. if (!strcmp(ast_inet_ntoa(us), "127.0.0.1")) {
  721. ast_log(LOG_WARNING, "Found a loopback IP on the system, check your network configuration or set the bindaddr attribute.");
  722. }
  723. /* Setup our gtalk candidates */
  724. ast_copy_string(ours1->name, "rtp", sizeof(ours1->name));
  725. ours1->port = ntohs(sin.sin_port);
  726. ours1->preference = 1;
  727. snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
  728. snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
  729. ast_copy_string(ours1->username, user, sizeof(ours1->username));
  730. ast_copy_string(ours1->password, pass, sizeof(ours1->password));
  731. ast_copy_string(ours1->ip, ast_inet_ntoa(us), sizeof(ours1->ip));
  732. ours1->protocol = AJI_PROTOCOL_UDP;
  733. ours1->type = AJI_CONNECT_LOCAL;
  734. ours1->generation = 0;
  735. p->ourcandidates = ours1;
  736. if (!ast_strlen_zero(externip)) {
  737. /* XXX We should really stun for this one not just go with externip XXX */
  738. snprintf(user, sizeof(user), "%08lx%08lx", ast_random(), ast_random());
  739. snprintf(pass, sizeof(pass), "%08lx%08lx", ast_random(), ast_random());
  740. ast_copy_string(ours2->username, user, sizeof(ours2->username));
  741. ast_copy_string(ours2->password, pass, sizeof(ours2->password));
  742. ast_copy_string(ours2->ip, externip, sizeof(ours2->ip));
  743. ast_copy_string(ours2->name, "rtp", sizeof(ours1->name));
  744. ours2->port = ntohs(sin.sin_port);
  745. ours2->preference = 0.9;
  746. ours2->protocol = AJI_PROTOCOL_UDP;
  747. ours2->type = AJI_CONNECT_STUN;
  748. ours2->generation = 0;
  749. ours1->next = ours2;
  750. ours2 = NULL;
  751. }
  752. ours1 = NULL;
  753. dest.sin_addr = __ourip;
  754. dest.sin_port = sin.sin_port;
  755. for (tmp = p->ourcandidates; tmp; tmp = tmp->next) {
  756. snprintf(port, sizeof(port), "%d", tmp->port);
  757. snprintf(preference, sizeof(preference), "%.2f", tmp->preference);
  758. iks_insert_attrib(iq, "from", to);
  759. iks_insert_attrib(iq, "to", from);
  760. iks_insert_attrib(iq, "type", "set");
  761. iks_insert_attrib(iq, "id", c->mid);
  762. ast_aji_increment_mid(c->mid);
  763. iks_insert_attrib(gtalk, "type", "transport-info");
  764. iks_insert_attrib(gtalk, "id", sid);
  765. /* put the initiator attribute to lower case if we receive the call
  766. * otherwise GoogleTalk won't establish the session */
  767. if (!p->initiator) {
  768. char c;
  769. char *t = lowerfrom = ast_strdupa(from);
  770. while (((c = *t) != '/') && (*t++ = tolower(c)));
  771. }
  772. iks_insert_attrib(gtalk, "initiator", (p->initiator) ? to : lowerfrom);
  773. iks_insert_attrib(gtalk, "xmlns", GOOGLE_NS);
  774. iks_insert_attrib(candidate, "name", tmp->name);
  775. iks_insert_attrib(candidate, "address", tmp->ip);
  776. iks_insert_attrib(candidate, "port", port);
  777. iks_insert_attrib(candidate, "username", tmp->username);
  778. iks_insert_attrib(candidate, "password", tmp->password);
  779. iks_insert_attrib(candidate, "preference", preference);
  780. if (tmp->protocol == AJI_PROTOCOL_UDP)
  781. iks_insert_attrib(candidate, "protocol", "udp");
  782. if (tmp->protocol == AJI_PROTOCOL_SSLTCP)
  783. iks_insert_attrib(candidate, "protocol", "ssltcp");
  784. if (tmp->type == AJI_CONNECT_STUN)
  785. iks_insert_attrib(candidate, "type", "stun");
  786. if (tmp->type == AJI_CONNECT_LOCAL)
  787. iks_insert_attrib(candidate, "type", "local");
  788. if (tmp->type == AJI_CONNECT_RELAY)
  789. iks_insert_attrib(candidate, "type", "relay");
  790. iks_insert_attrib(candidate, "network", "0");
  791. iks_insert_attrib(candidate, "generation", "0");
  792. ast_aji_send(c, iq);
  793. }
  794. p->laststun = 0;
  795. safeout:
  796. if (ours1)
  797. ast_free(ours1);
  798. if (ours2)
  799. ast_free(ours2);
  800. iks_delete(iq);
  801. iks_delete(gtalk);
  802. iks_delete(candidate);
  803. iks_delete(transport);
  804. return 1;
  805. }
  806. static struct gtalk_pvt *gtalk_alloc(struct gtalk *client, const char *us, const char *them, const char *sid)
  807. {
  808. struct gtalk_pvt *tmp = NULL;
  809. struct aji_resource *resources = NULL;
  810. struct aji_buddy *buddy;
  811. char idroster[200];
  812. char *data, *exten = NULL;
  813. ast_debug(1, "The client is %s for alloc\n", client->name);
  814. if (!sid && !strchr(them, '/')) { /* I started call! */
  815. if (!strcasecmp(client->name, "guest")) {
  816. buddy = ASTOBJ_CONTAINER_FIND(&client->connection->buddies, them);
  817. if (buddy)
  818. resources = buddy->resources;
  819. } else if (client->buddy)
  820. resources = client->buddy->resources;
  821. while (resources) {
  822. if (resources->cap->jingle) {
  823. break;
  824. }
  825. resources = resources->next;
  826. }
  827. if (resources)
  828. snprintf(idroster, sizeof(idroster), "%s/%s", them, resources->resource);
  829. else {
  830. ast_log(LOG_ERROR, "no gtalk capable clients to talk to.\n");
  831. return NULL;
  832. }
  833. }
  834. if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
  835. return NULL;
  836. }
  837. memcpy(&tmp->prefs, &client->prefs, sizeof(struct ast_codec_pref));
  838. if (sid) {
  839. ast_copy_string(tmp->sid, sid, sizeof(tmp->sid));
  840. ast_copy_string(tmp->them, them, sizeof(tmp->them));
  841. ast_copy_string(tmp->us, us, sizeof(tmp->us));
  842. } else {
  843. snprintf(tmp->sid, sizeof(tmp->sid), "%08lx%08lx", ast_random(), ast_random());
  844. ast_copy_string(tmp->them, idroster, sizeof(tmp->them));
  845. ast_copy_string(tmp->us, us, sizeof(tmp->us));
  846. tmp->initiator = 1;
  847. }
  848. /* clear codecs */
  849. tmp->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, bindaddr.sin_addr);
  850. ast_rtp_pt_clear(tmp->rtp);
  851. /* add user configured codec capabilites */
  852. if (client->capability)
  853. tmp->capability = client->capability;
  854. else if (global_capability)
  855. tmp->capability = global_capability;
  856. tmp->parent = client;
  857. if (!tmp->rtp) {
  858. ast_log(LOG_WARNING, "Out of RTP sessions?\n");
  859. ast_free(tmp);
  860. return NULL;
  861. }
  862. /* Set CALLERID(name) to the full JID of the remote peer */
  863. ast_copy_string(tmp->cid_name, tmp->them, sizeof(tmp->cid_name));
  864. if(strchr(tmp->us, '/')) {
  865. data = ast_strdupa(tmp->us);
  866. exten = strsep(&data, "/");
  867. } else
  868. exten = tmp->us;
  869. ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
  870. ast_mutex_init(&tmp->lock);
  871. ast_mutex_lock(&gtalklock);
  872. tmp->next = client->p;
  873. client->p = tmp;
  874. ast_mutex_unlock(&gtalklock);
  875. return tmp;
  876. }
  877. /*! \brief Start new gtalk channel */
  878. static struct ast_channel *gtalk_new(struct gtalk *client, struct gtalk_pvt *i, int state, const char *title)
  879. {
  880. struct ast_channel *tmp;
  881. int fmt;
  882. int what;
  883. const char *n2;
  884. if (title)
  885. n2 = title;
  886. else
  887. n2 = i->us;
  888. tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, client->accountcode, i->exten, client->context, client->amaflags, "Gtalk/%s-%04lx", n2, ast_random() & 0xffff);
  889. if (!tmp) {
  890. ast_log(LOG_WARNING, "Unable to allocate Gtalk channel structure!\n");
  891. return NULL;
  892. }
  893. tmp->tech = &gtalk_tech;
  894. /* Select our native format based on codec preference until we receive
  895. something from another device to the contrary. */
  896. if (i->jointcapability)
  897. what = i->jointcapability;
  898. else if (i->capability)
  899. what = i->capability;
  900. else
  901. what = global_capability;
  902. /* Set Frame packetization */
  903. if (i->rtp)
  904. ast_rtp_codec_setpref(i->rtp, &i->prefs);
  905. tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | (i->jointcapability & AST_FORMAT_VIDEO_MASK);
  906. fmt = ast_best_codec(tmp->nativeformats);
  907. if (i->rtp) {
  908. ast_rtp_setstun(i->rtp, 1);
  909. ast_channel_set_fd(tmp, 0, ast_rtp_fd(i->rtp));
  910. ast_channel_set_fd(tmp, 1, ast_rtcp_fd(i->rtp));
  911. }
  912. if (i->vrtp) {
  913. ast_rtp_setstun(i->rtp, 1);
  914. ast_channel_set_fd(tmp, 2, ast_rtp_fd(i->vrtp));
  915. ast_channel_set_fd(tmp, 3, ast_rtcp_fd(i->vrtp));
  916. }
  917. if (state == AST_STATE_RING)
  918. tmp->rings = 1;
  919. tmp->adsicpe = AST_ADSI_UNAVAILABLE;
  920. tmp->writeformat = fmt;
  921. tmp->rawwriteformat = fmt;
  922. tmp->readformat = fmt;
  923. tmp->rawreadformat = fmt;
  924. tmp->tech_pvt = i;
  925. tmp->callgroup = client->callgroup;
  926. tmp->pickupgroup = client->pickupgroup;
  927. tmp->cid.cid_pres = client->callingpres;
  928. if (!ast_strlen_zero(client->accountcode))
  929. ast_string_field_set(tmp, accountcode, client->accountcode);
  930. if (client->amaflags)
  931. tmp->amaflags = client->amaflags;
  932. if (!ast_strlen_zero(client->language))
  933. ast_string_field_set(tmp, language, client->language);
  934. if (!ast_strlen_zero(client->musicclass))
  935. ast_string_field_set(tmp, musicclass, client->musicclass);
  936. i->owner = tmp;
  937. ast_module_ref(ast_module_info->self);
  938. ast_copy_string(tmp->context, client->context, sizeof(tmp->context));
  939. ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
  940. if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s"))
  941. tmp->cid.cid_dnid = ast_strdup(i->exten);
  942. tmp->priority = 1;
  943. if (i->rtp)
  944. ast_jb_configure(tmp, &global_jbconf);
  945. if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
  946. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  947. tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
  948. ast_hangup(tmp);
  949. tmp = NULL;
  950. } else {
  951. manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
  952. "Channel: %s\r\nChanneltype: %s\r\nGtalk-SID: %s\r\n",
  953. i->owner ? i->owner->name : "", "Gtalk", i->sid);
  954. }
  955. return tmp;
  956. }
  957. static int gtalk_action(struct gtalk *client, struct gtalk_pvt *p, const char *action)
  958. {
  959. iks *request, *session = NULL;
  960. int res = -1;
  961. char *lowerthem = NULL;
  962. request = iks_new("iq");
  963. if (request) {
  964. iks_insert_attrib(request, "type", "set");
  965. iks_insert_attrib(request, "from", p->us);
  966. iks_insert_attrib(request, "to", p->them);
  967. iks_insert_attrib(request, "id", client->connection->mid);
  968. ast_aji_increment_mid(client->connection->mid);
  969. session = iks_new("session");
  970. if (session) {
  971. iks_insert_attrib(session, "type", action);
  972. iks_insert_attrib(session, "id", p->sid);
  973. /* put the initiator attribute to lower case if we receive the call
  974. * otherwise GoogleTalk won't establish the session */
  975. if (!p->initiator) {
  976. char c;
  977. char *t = lowerthem = ast_strdupa(p->them);
  978. while (((c = *t) != '/') && (*t++ = tolower(c)));
  979. }
  980. iks_insert_attrib(session, "initiator", p->initiator ? p->us : lowerthem);
  981. iks_insert_attrib(session, "xmlns", "http://www.google.com/session");
  982. iks_insert_node(request, session);
  983. ast_aji_send(client->connection, request);
  984. res = 0;
  985. }
  986. }
  987. iks_delete(session);
  988. iks_delete(request);
  989. return res;
  990. }
  991. static void gtalk_free_candidates(struct gtalk_candidate *candidate)
  992. {
  993. struct gtalk_candidate *last;
  994. while (candidate) {
  995. last = candidate;
  996. candidate = candidate->next;
  997. ast_free(last);
  998. }
  999. }
  1000. static void gtalk_free_pvt(struct gtalk *client, struct gtalk_pvt *p)
  1001. {
  1002. struct gtalk_pvt *cur, *prev = NULL;
  1003. cur = client->p;
  1004. while (cur) {
  1005. if (cur == p) {
  1006. if (prev)
  1007. prev->next = p->next;
  1008. else
  1009. client->p = p->next;
  1010. break;
  1011. }
  1012. prev = cur;
  1013. cur = cur->next;
  1014. }
  1015. if (p->ringrule)
  1016. iks_filter_remove_rule(p->parent->connection->f, p->ringrule);
  1017. if (p->owner)
  1018. ast_log(LOG_WARNING, "Uh oh, there's an owner, this is going to be messy.\n");
  1019. if (p->rtp)
  1020. ast_rtp_destroy(p->rtp);
  1021. if (p->vrtp)
  1022. ast_rtp_destroy(p->vrtp);
  1023. gtalk_free_candidates(p->theircandidates);
  1024. ast_free(p);
  1025. }
  1026. static int gtalk_newcall(struct gtalk *client, ikspak *pak)
  1027. {
  1028. struct gtalk_pvt *p, *tmp = client->p;
  1029. struct ast_channel *chan;
  1030. int res;
  1031. iks *codec;
  1032. char *from = NULL;
  1033. char s1[BUFSIZ], s2[BUFSIZ], s3[BUFSIZ];
  1034. int peernoncodeccapability;
  1035. /* Make sure our new call doesn't exist yet */
  1036. from = iks_find_attrib(pak->x,"to");
  1037. if(!from)
  1038. from = client->connection->jid->full;
  1039. while (tmp) {
  1040. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
  1041. ast_log(LOG_NOTICE, "Ignoring duplicate call setup on SID %s\n", tmp->sid);
  1042. gtalk_response(client, from, pak, "out-of-order", NULL);
  1043. return -1;
  1044. }
  1045. tmp = tmp->next;
  1046. }
  1047. if (!strcasecmp(client->name, "guest")){
  1048. /* the guest account is not tied to any configured XMPP client,
  1049. let's set it now */
  1050. client->connection = ast_aji_get_client(from);
  1051. if (!client->connection) {
  1052. ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", from);
  1053. return -1;
  1054. }
  1055. }
  1056. p = gtalk_alloc(client, from, pak->from->full, iks_find_attrib(pak->query, "id"));
  1057. if (!p) {
  1058. ast_log(LOG_WARNING, "Unable to allocate gtalk structure!\n");
  1059. return -1;
  1060. }
  1061. chan = gtalk_new(client, p, AST_STATE_DOWN, pak->from->user);
  1062. if (!chan) {
  1063. gtalk_free_pvt(client, p);
  1064. return -1;
  1065. }
  1066. ast_mutex_lock(&p->lock);
  1067. ast_copy_string(p->them, pak->from->full, sizeof(p->them));
  1068. if (iks_find_attrib(pak->query, "id")) {
  1069. ast_copy_string(p->sid, iks_find_attrib(pak->query, "id"),
  1070. sizeof(p->sid));
  1071. }
  1072. /* codec points to the first <payload-type/> tag */
  1073. codec = iks_first_tag(iks_first_tag(iks_first_tag(pak->x)));
  1074. while (codec) {
  1075. ast_rtp_set_m_type(p->rtp, atoi(iks_find_attrib(codec, "id")));
  1076. ast_rtp_set_rtpmap_type(p->rtp, atoi(iks_find_attrib(codec, "id")), "audio", iks_find_attrib(codec, "name"), 0);
  1077. codec = iks_next_tag(codec);
  1078. }
  1079. /* Now gather all of the codecs that we are asked for */
  1080. ast_rtp_get_current_formats(p->rtp, &p->peercapability, &peernoncodeccapability);
  1081. p->jointcapability = p->capability & p->peercapability;
  1082. ast_mutex_unlock(&p->lock);
  1083. ast_setstate(chan, AST_STATE_RING);
  1084. if (!p->jointcapability) {
  1085. ast_log(LOG_WARNING, "Capabilities don't match : us - %s, peer - %s, combined - %s \n", ast_getformatname_multiple(s1, BUFSIZ, p->capability),
  1086. ast_getformatname_multiple(s2, BUFSIZ, p->peercapability),
  1087. ast_getformatname_multiple(s3, BUFSIZ, p->jointcapability));
  1088. /* close session if capabilities don't match */
  1089. gtalk_action(client, p, "reject");
  1090. p->alreadygone = 1;
  1091. gtalk_hangup(chan);
  1092. ast_channel_free(chan);
  1093. return -1;
  1094. }
  1095. res = ast_pbx_start(chan);
  1096. switch (res) {
  1097. case AST_PBX_FAILED:
  1098. ast_log(LOG_WARNING, "Failed to start PBX :(\n");
  1099. gtalk_response(client, from, pak, "service-unavailable", NULL);
  1100. break;
  1101. case AST_PBX_CALL_LIMIT:
  1102. ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
  1103. gtalk_response(client, from, pak, "service-unavailable", NULL);
  1104. break;
  1105. case AST_PBX_SUCCESS:
  1106. gtalk_response(client, from, pak, NULL, NULL);
  1107. gtalk_invite_response(p, p->them, p->us,p->sid, 0);
  1108. gtalk_create_candidates(client, p, p->sid, p->them, p->us);
  1109. /* nothing to do */
  1110. break;
  1111. }
  1112. return 1;
  1113. }
  1114. static int gtalk_update_stun(struct gtalk *client, struct gtalk_pvt *p)
  1115. {
  1116. struct gtalk_candidate *tmp;
  1117. struct hostent *hp;
  1118. struct ast_hostent ahp;
  1119. struct sockaddr_in sin;
  1120. struct sockaddr_in aux;
  1121. if (time(NULL) == p->laststun)
  1122. return 0;
  1123. tmp = p->theircandidates;
  1124. p->laststun = time(NULL);
  1125. while (tmp) {
  1126. char username[256];
  1127. /* Find the IP address of the host */
  1128. hp = ast_gethostbyname(tmp->ip, &ahp);
  1129. sin.sin_family = AF_INET;
  1130. memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
  1131. sin.sin_port = htons(tmp->port);
  1132. snprintf(username, sizeof(username), "%s%s", tmp->username,
  1133. p->ourcandidates->username);
  1134. /* Find out the result of the STUN */
  1135. ast_rtp_get_peer(p->rtp, &aux);
  1136. /* If the STUN result is different from the IP of the hostname,
  1137. lock on the stun IP of the hostname advertised by the
  1138. remote client */
  1139. if (aux.sin_addr.s_addr &&
  1140. aux.sin_addr.s_addr != sin.sin_addr.s_addr)
  1141. ast_rtp_stun_request(p->rtp, &aux, username);
  1142. else
  1143. ast_rtp_stun_request(p->rtp, &sin, username);
  1144. if (aux.sin_addr.s_addr) {
  1145. 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);
  1146. ast_debug(4, "Sending STUN request to %s\n", tmp->ip);
  1147. }
  1148. tmp = tmp->next;
  1149. }
  1150. return 1;
  1151. }
  1152. static int gtalk_add_candidate(struct gtalk *client, ikspak *pak)
  1153. {
  1154. struct gtalk_pvt *p = NULL, *tmp = NULL;
  1155. struct aji_client *c = client->connection;
  1156. struct gtalk_candidate *newcandidate = NULL;
  1157. iks *traversenodes = NULL, *receipt = NULL;
  1158. char *from;
  1159. from = iks_find_attrib(pak->x,"to");
  1160. if(!from)
  1161. from = c->jid->full;
  1162. for (tmp = client->p; tmp; tmp = tmp->next) {
  1163. if (iks_find_with_attrib(pak->x, "session", "id", tmp->sid)) {
  1164. p = tmp;
  1165. break;
  1166. }
  1167. }
  1168. if (!p)
  1169. return -1;
  1170. traversenodes = pak->query;
  1171. while(traversenodes) {
  1172. if(!strcasecmp(iks_name(traversenodes), "session")) {
  1173. traversenodes = iks_first_tag(traversenodes);
  1174. continue;
  1175. }
  1176. if(!strcasecmp(iks_name(traversenodes), "transport")) {
  1177. traversenodes = iks_first_tag(traversenodes);
  1178. continue;
  1179. }
  1180. if(!strcasecmp(iks_name(traversenodes), "candidate")) {
  1181. newcandidate = ast_calloc(1, sizeof(*newcandidate));
  1182. if (!newcandidate)
  1183. return 0;
  1184. ast_copy_string(newcandidate->name, iks_find_attrib(traversenodes, "name"),
  1185. sizeof(newcandidate->name));
  1186. ast_copy_string(newcandidate->ip, iks_find_attrib(traversenodes, "address"),
  1187. sizeof(newcandidate->ip));
  1188. newcandidate->port = atoi(iks_find_attrib(traversenodes, "port"));
  1189. ast_copy_string(newcandidate->username, iks_find_attrib(traversenodes, "username"),
  1190. sizeof(newcandidate->username));
  1191. ast_copy_string(newcandidate->password, iks_find_attrib(traversenodes, "password"),
  1192. sizeof(newcandidate->password));
  1193. newcandidate->preference = atof(iks_find_attrib(traversenodes, "preference"));
  1194. if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "udp"))
  1195. newcandidate->protocol = AJI_PROTOCOL_UDP;
  1196. if (!strcasecmp(iks_find_attrib(traversenodes, "protocol"), "ssltcp"))
  1197. newcandidate->protocol = AJI_PROTOCOL_SSLTCP;
  1198. if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "stun"))
  1199. newcandidate->type = AJI_CONNECT_STUN;
  1200. if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "local"))
  1201. newcandidate->type = AJI_CONNECT_LOCAL;
  1202. if (!strcasecmp(iks_find_attrib(traversenodes, "type"), "relay"))
  1203. newcandidate->type = AJI_CONNECT_RELAY;
  1204. ast_copy_string(newcandidate->network, iks_find_attrib(traversenodes, "network"),
  1205. sizeof(newcandidate->network));
  1206. newcandidate->generation = atoi(iks_find_attrib(traversenodes, "generation"));
  1207. newcandidate->next = NULL;
  1208. newcandidate->next = p->theircandidates;
  1209. p->theircandidates = newcandidate;
  1210. p->laststun = 0;
  1211. gtalk_update_stun(p->parent, p);
  1212. newcandidate = NULL;
  1213. }
  1214. traversenodes = iks_next_tag(traversenodes);
  1215. }
  1216. receipt = iks_new("iq");
  1217. iks_insert_attrib(receipt, "type", "result");
  1218. iks_insert_attrib(receipt, "from", from);
  1219. iks_insert_attrib(receipt, "to", iks_find_attrib(pak->x, "from"));
  1220. iks_insert_attrib(receipt, "id", iks_find_attrib(pak->x, "id"));
  1221. ast_aji_send(c, receipt);
  1222. iks_delete(receipt);
  1223. return 1;
  1224. }
  1225. static struct ast_frame *gtalk_rtp_read(struct ast_channel *ast, struct gtalk_pvt *p)
  1226. {
  1227. struct ast_frame *f;
  1228. if (!p->rtp)
  1229. return &ast_null_frame;
  1230. f = ast_rtp_read(p->rtp);
  1231. gtalk_update_stun(p->parent, p);
  1232. if (p->owner) {
  1233. /* We already hold the channel lock */
  1234. if (f->frametype == AST_FRAME_VOICE) {
  1235. if (f->subclass != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
  1236. ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
  1237. p->owner->nativeformats =
  1238. (p->owner->nativeformats & AST_FORMAT_VIDEO_MASK) | f->subclass;
  1239. ast_set_read_format(p->owner, p->owner->readformat);
  1240. ast_set_write_format(p->owner, p->owner->writeformat);
  1241. }
  1242. /* if ((ast_test_flag(p, SIP_DTMF) == SIP_DTMF_INBAND) && p->vad) {
  1243. f = ast_dsp_process(p->owner, p->vad, f);
  1244. if (option_debug && f && (f->frametype == AST_FRAME_DTMF))
  1245. ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass);
  1246. } */
  1247. }
  1248. }
  1249. return f;
  1250. }
  1251. static struct ast_frame *gtalk_read(struct ast_channel *ast)
  1252. {
  1253. struct ast_frame *fr;
  1254. struct gtalk_pvt *p = ast->tech_pvt;
  1255. ast_mutex_lock(&p->lock);
  1256. fr = gtalk_rtp_read(ast, p);
  1257. ast_mutex_unlock(&p->lock);
  1258. return fr;
  1259. }
  1260. /*! \brief Send frame to media channel (rtp) */
  1261. static int gtalk_write(struct ast_channel *ast, struct ast_frame *frame)
  1262. {
  1263. struct gtalk_pvt *p = ast->tech_pvt;
  1264. int res = 0;
  1265. switch (frame->frametype) {
  1266. case AST_FRAME_VOICE:
  1267. if (!(frame->subclass & ast->nativeformats)) {
  1268. ast_log(LOG_WARNING,
  1269. "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
  1270. frame->subclass, ast->nativeformats, ast->readformat,
  1271. ast->writeformat);
  1272. return 0;
  1273. }
  1274. if (p) {
  1275. ast_mutex_lock(&p->lock);
  1276. if (p->rtp) {
  1277. res = ast_rtp_write(p->rtp, frame);
  1278. }
  1279. ast_mutex_unlock(&p->lock);
  1280. }
  1281. break;
  1282. case AST_FRAME_VIDEO:
  1283. if (p) {
  1284. ast_mutex_lock(&p->lock);
  1285. if (p->vrtp) {
  1286. res = ast_rtp_write(p->vrtp, frame);
  1287. }
  1288. ast_mutex_unlock(&p->lock);
  1289. }
  1290. break;
  1291. case AST_FRAME_IMAGE:
  1292. return 0;
  1293. break;
  1294. default:
  1295. ast_log(LOG_WARNING, "Can't send %d type frames with Gtalk write\n",
  1296. frame->frametype);
  1297. return 0;
  1298. }
  1299. return res;
  1300. }
  1301. static int gtalk_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1302. {
  1303. struct gtalk_pvt *p = newchan->tech_pvt;
  1304. ast_mutex_lock(&p->lock);
  1305. if ((p->owner != oldchan)) {
  1306. ast_mutex_unlock(&p->lock);
  1307. return -1;
  1308. }
  1309. if (p->owner == oldchan)
  1310. p->owner = newchan;
  1311. ast_mutex_unlock(&p->lock);
  1312. return 0;
  1313. }
  1314. static int gtalk_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  1315. {
  1316. int res = 0;
  1317. switch (condition) {
  1318. case AST_CONTROL_HOLD:
  1319. ast_moh_start(ast, data, NULL);
  1320. break;
  1321. case AST_CONTROL_UNHOLD:
  1322. ast_moh_stop(ast);
  1323. break;
  1324. default:
  1325. ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
  1326. res = -1;
  1327. }
  1328. return res;
  1329. }
  1330. static int gtalk_digit_begin(struct ast_channel *chan, char digit)
  1331. {
  1332. return gtalk_digit(chan, digit, 0);
  1333. }
  1334. static int gtalk_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
  1335. {
  1336. return gtalk_digit(chan, digit, duration);
  1337. }
  1338. static int gtalk_digit(struct ast_channel *ast, char digit, unsigned int duration)
  1339. {
  1340. struct gtalk_pvt *p = ast->tech_pvt;
  1341. struct gtalk *client = p->parent;
  1342. iks *iq, *gtalk, *dtmf;
  1343. char buffer[2] = {digit, '\0'};
  1344. char *lowerthem = NULL;
  1345. iq = iks_new("iq");
  1346. gtalk = iks_new("gtalk");
  1347. dtmf = iks_new("dtmf");
  1348. if(!iq || !gtalk || !dtmf) {
  1349. iks_delete(iq);
  1350. iks_delete(gtalk);
  1351. iks_delete(dtmf);
  1352. ast_log(LOG_ERROR, "Did not send dtmf do to memory issue\n");
  1353. return -1;
  1354. }
  1355. iks_insert_attrib(iq, "type", "set");
  1356. iks_insert_attrib(iq, "to", p->them);
  1357. iks_insert_attrib(iq, "from", p->us);
  1358. iks_insert_attrib(iq, "id", client->connection->mid);
  1359. ast_aji_increment_mid(client->connection->mid);
  1360. iks_insert_attrib(gtalk, "xmlns", "http://jabber.org/protocol/gtalk");
  1361. iks_insert_attrib(gtalk, "action", "session-info");
  1362. /* put the initiator attribute to lower case if we receive the call
  1363. * otherwise GoogleTalk won't establish the session */
  1364. if (!p->initiator) {
  1365. char c;
  1366. char *t = lowerthem = ast_strdupa(p->them);
  1367. while (((c = *t) != '/') && (*t++ = tolower(c)));
  1368. }
  1369. iks_insert_attrib(gtalk, "initiator", p->initiator ? p->us: lowerthem);
  1370. iks_insert_attrib(gtalk, "sid", p->sid);
  1371. iks_insert_attrib(dtmf, "xmlns", "http://jabber.org/protocol/gtalk/info/dtmf");
  1372. iks_insert_attrib(dtmf, "code", buffer);
  1373. iks_insert_node(iq, gtalk);
  1374. iks_insert_node(gtalk, dtmf);
  1375. ast_mutex_lock(&p->lock);
  1376. if (ast->dtmff.frametype == AST_FRAME_DTMF_BEGIN || duration == 0) {
  1377. iks_insert_attrib(dtmf, "action", "button-down");
  1378. } else if (ast->dtmff.frametype == AST_FRAME_DTMF_END || duration != 0) {
  1379. iks_insert_attrib(dtmf, "action", "button-up");
  1380. }
  1381. ast_aji_send(client->connection, iq);
  1382. iks_delete(iq);
  1383. iks_delete(gtalk);
  1384. iks_delete(dtmf);
  1385. ast_mutex_unlock(&p->lock);
  1386. return 0;
  1387. }
  1388. static int gtalk_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
  1389. {
  1390. ast_log(LOG_NOTICE, "XXX Implement gtalk sendhtml XXX\n");
  1391. return -1;
  1392. }
  1393. /* Not in use right now.
  1394. static int gtalk_auto_congest(void *nothing)
  1395. {
  1396. struct gtalk_pvt *p = nothing;
  1397. ast_mutex_lock(&p->lock);
  1398. if (p->owner) {
  1399. if (!ast_channel_trylock(p->owner)) {
  1400. ast_log(LOG_NOTICE, "Auto-congesting %s\n", p->owner->name);
  1401. ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
  1402. ast_channel_unlock(p->owner);
  1403. }
  1404. }
  1405. ast_mutex_unlock(&p->lock);
  1406. return 0;
  1407. }
  1408. */
  1409. /*! \brief Initiate new call, part of PBX interface
  1410. * dest is the dial string */
  1411. static int gtalk_call(struct ast_channel *ast, char *dest, int timeout)
  1412. {
  1413. struct gtalk_pvt *p = ast->tech_pvt;
  1414. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  1415. ast_log(LOG_WARNING, "gtalk_call called on %s, neither down nor reserved\n", ast->name);
  1416. return -1;
  1417. }
  1418. ast_setstate(ast, AST_STATE_RING);
  1419. if (!p->ringrule) {
  1420. ast_copy_string(p->ring, p->parent->connection->mid, sizeof(p->ring));
  1421. p->ringrule = iks_filter_add_rule(p->parent->connection->f, gtalk_ringing_ack, p,
  1422. IKS_RULE_ID, p->ring, IKS_RULE_DONE);
  1423. } else
  1424. ast_log(LOG_WARNING, "Whoa, already have a ring rule!\n");
  1425. gtalk_invite(p, p->them, p->us, p->sid, 1);
  1426. gtalk_create_candidates(p->parent, p, p->sid, p->them, p->us);
  1427. return 0;
  1428. }
  1429. /*! \brief Hangup a call through the gtalk proxy channel */
  1430. static int gtalk_hangup(struct ast_channel *ast)
  1431. {
  1432. struct gtalk_pvt *p = ast->tech_pvt;
  1433. struct gtalk *client;
  1434. ast_mutex_lock(&p->lock);
  1435. client = p->parent;
  1436. p->owner = NULL;
  1437. ast->tech_pvt = NULL;
  1438. if (!p->alreadygone)
  1439. gtalk_action(client, p, "terminate");
  1440. ast_mutex_unlock(&p->lock);
  1441. gtalk_free_pvt(client, p);
  1442. ast_module_unref(ast_module_info->self);
  1443. return 0;
  1444. }
  1445. /*! \brief Part of PBX interface */
  1446. static struct ast_channel *gtalk_request(const char *type, int format, void *data, int *cause)
  1447. {
  1448. struct gtalk_pvt *p = NULL;
  1449. struct gtalk *client = NULL;
  1450. char *sender = NULL, *to = NULL, *s = NULL;
  1451. struct ast_channel *chan = NULL;
  1452. if (data) {
  1453. s = ast_strdupa(data);
  1454. if (s) {
  1455. sender = strsep(&s, "/");
  1456. if (sender && (sender[0] != '\0'))
  1457. to = strsep(&s, "/");
  1458. if (!to) {
  1459. ast_log(LOG_ERROR, "Bad arguments in Gtalk Dialstring: %s\n", (char*) data);
  1460. return NULL;
  1461. }
  1462. }
  1463. }
  1464. client = find_gtalk(to, sender);
  1465. if (!client) {
  1466. ast_log(LOG_WARNING, "Could not find recipient.\n");
  1467. return NULL;
  1468. }
  1469. if (!strcasecmp(client->name, "guest")){
  1470. /* the guest account is not tied to any configured XMPP client,
  1471. let's set it now */
  1472. client->connection = ast_aji_get_client(sender);
  1473. if (!client->connection) {
  1474. ast_log(LOG_ERROR, "No XMPP client to talk to, us (partial JID) : %s\n", sender);
  1475. ASTOBJ_UNREF(client, gtalk_member_destroy);
  1476. return NULL;
  1477. }
  1478. }
  1479. ASTOBJ_WRLOCK(client);
  1480. p = gtalk_alloc(client, strchr(sender, '@') ? sender : client->connection->jid->full, strchr(to, '@') ? to : client->user, NULL);
  1481. if (p)
  1482. chan = gtalk_new(client, p, AST_STATE_DOWN, to);
  1483. ASTOBJ_UNLOCK(client);
  1484. return chan;
  1485. }
  1486. /*! \brief CLI command "gtalk show channels" */
  1487. static char *gtalk_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1488. {
  1489. #define FORMAT "%-30.30s %-30.30s %-15.15s %-5.5s %-5.5s \n"
  1490. struct gtalk_pvt *p;
  1491. struct ast_channel *chan;
  1492. int numchans = 0;
  1493. char them[AJI_MAX_JIDLEN];
  1494. char *jid = NULL;
  1495. char *resource = NULL;
  1496. switch (cmd) {
  1497. case CLI_INIT:
  1498. e->command = "gtalk show channels";
  1499. e->usage =
  1500. "Usage: gtalk show channels\n"
  1501. " Shows current state of the Gtalk channels.\n";
  1502. return NULL;
  1503. case CLI_GENERATE:
  1504. return NULL;
  1505. }
  1506. if (a->argc != 3)
  1507. return CLI_SHOWUSAGE;
  1508. ast_mutex_lock(&gtalklock);
  1509. ast_cli(a->fd, FORMAT, "Channel", "Jabber ID", "Resource", "Read", "Write");
  1510. ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
  1511. ASTOBJ_WRLOCK(iterator);
  1512. p = iterator->p;
  1513. while(p) {
  1514. chan = p->owner;
  1515. ast_copy_string(them, p->them, sizeof(them));
  1516. jid = them;
  1517. resource = strchr(them, '/');
  1518. if (!resource)
  1519. resource = "None";
  1520. else {
  1521. *resource = '\0';
  1522. resource ++;
  1523. }
  1524. if (chan)
  1525. ast_cli(a->fd, FORMAT,
  1526. chan->name,
  1527. jid,
  1528. resource,
  1529. ast_getformatname(chan->readformat),
  1530. ast_getformatname(chan->writeformat)
  1531. );
  1532. else
  1533. ast_log(LOG_WARNING, "No available channel\n");
  1534. numchans ++;
  1535. p = p->next;
  1536. }
  1537. ASTOBJ_UNLOCK(iterator);
  1538. });
  1539. ast_mutex_unlock(&gtalklock);
  1540. ast_cli(a->fd, "%d active gtalk channel%s\n", numchans, (numchans != 1) ? "s" : "");
  1541. return CLI_SUCCESS;
  1542. #undef FORMAT
  1543. }
  1544. /*! \brief CLI command "gtalk reload" */
  1545. static char *gtalk_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1546. {
  1547. switch (cmd) {
  1548. case CLI_INIT:
  1549. e->command = "gtalk reload";
  1550. e->usage =
  1551. "Usage: gtalk reload\n"
  1552. " Reload gtalk channel driver.\n";
  1553. return NULL;
  1554. case CLI_GENERATE:
  1555. return NULL;
  1556. }
  1557. ast_verbose("IT DOES WORK!\n");
  1558. return CLI_SUCCESS;
  1559. }
  1560. static int gtalk_parser(void *data, ikspak *pak)
  1561. {
  1562. struct gtalk *client = ASTOBJ_REF((struct gtalk *) data);
  1563. if (iks_find_attrib(pak->x, "type") && !strcmp(iks_find_attrib (pak->x, "type"),"error")) {
  1564. ast_log(LOG_NOTICE, "Remote peer reported an error, trying to establish the call anyway\n");
  1565. }
  1566. else if (iks_find_with_attrib(pak->x, "session", "type", "initiate")) {
  1567. /* New call */
  1568. gtalk_newcall(client, pak);
  1569. } else if (iks_find_with_attrib(pak->x, "session", "type", "candidates") || iks_find_with_attrib(pak->x, "session", "type", "transport-info")) {
  1570. ast_debug(3, "About to add candidate!\n");
  1571. gtalk_add_candidate(client, pak);
  1572. ast_debug(3, "Candidate Added!\n");
  1573. } else if (iks_find_with_attrib(pak->x, "session", "type", "accept")) {
  1574. gtalk_is_answered(client, pak);
  1575. } else if (iks_find_with_attrib(pak->x, "session", "type", "transport-accept")) {
  1576. gtalk_is_accepted(client, pak);
  1577. } else if (iks_find_with_attrib(pak->x, "session", "type", "content-info") || iks_find_with_attrib(pak->x, "gtalk", "action", "session-info")) {
  1578. gtalk_handle_dtmf(client, pak);
  1579. } else if (iks_find_with_attrib(pak->x, "session", "type", "terminate")) {
  1580. gtalk_hangup_farend(client, pak);
  1581. } else if (iks_find_with_attrib(pak->x, "session", "type", "reject")) {
  1582. gtalk_hangup_farend(client, pak);
  1583. }
  1584. ASTOBJ_UNREF(client, gtalk_member_destroy);
  1585. return IKS_FILTER_EAT;
  1586. }
  1587. /* Not using this anymore probably take out soon
  1588. static struct gtalk_candidate *gtalk_create_candidate(char *args)
  1589. {
  1590. char *name, *type, *preference, *protocol;
  1591. struct gtalk_candidate *res;
  1592. res = ast_calloc(1, sizeof(*res));
  1593. if (args)
  1594. name = args;
  1595. if ((args = strchr(args, ','))) {
  1596. *args = '\0';
  1597. args++;
  1598. preference = args;
  1599. }
  1600. if ((args = strchr(args, ','))) {
  1601. *args = '\0';
  1602. args++;
  1603. protocol = args;
  1604. }
  1605. if ((args = strchr(args, ','))) {
  1606. *args = '\0';
  1607. args++;
  1608. type = args;
  1609. }
  1610. if (name)
  1611. ast_copy_string(res->name, name, sizeof(res->name));
  1612. if (preference) {
  1613. res->preference = atof(preference);
  1614. }
  1615. if (protocol) {
  1616. if (!strcasecmp("udp", protocol))
  1617. res->protocol = AJI_PROTOCOL_UDP;
  1618. if (!strcasecmp("ssltcp", protocol))
  1619. res->protocol = AJI_PROTOCOL_SSLTCP;
  1620. }
  1621. if (type) {
  1622. if (!strcasecmp("stun", type))
  1623. res->type = AJI_CONNECT_STUN;
  1624. if (!strcasecmp("local", type))
  1625. res->type = AJI_CONNECT_LOCAL;
  1626. if (!strcasecmp("relay", type))
  1627. res->type = AJI_CONNECT_RELAY;
  1628. }
  1629. return res;
  1630. }
  1631. */
  1632. static int gtalk_create_member(char *label, struct ast_variable *var, int allowguest,
  1633. struct ast_codec_pref prefs, char *context,
  1634. struct gtalk *member)
  1635. {
  1636. struct aji_client *client;
  1637. if (!member)
  1638. ast_log(LOG_WARNING, "Out of memory.\n");
  1639. ast_copy_string(member->name, label, sizeof(member->name));
  1640. ast_copy_string(member->user, label, sizeof(member->user));
  1641. ast_copy_string(member->context, context, sizeof(member->context));
  1642. member->allowguest = allowguest;
  1643. member->prefs = prefs;
  1644. while (var) {
  1645. #if 0
  1646. struct gtalk_candidate *candidate = NULL;
  1647. #endif
  1648. if (!strcasecmp(var->name, "username"))
  1649. ast_copy_string(member->user, var->value, sizeof(member->user));
  1650. else if (!strcasecmp(var->name, "disallow"))
  1651. ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 0);
  1652. else if (!strcasecmp(var->name, "allow"))
  1653. ast_parse_allow_disallow(&member->prefs, &member->capability, var->value, 1);
  1654. else if (!strcasecmp(var->name, "context"))
  1655. ast_copy_string(member->context, var->value, sizeof(member->context));
  1656. #if 0
  1657. else if (!strcasecmp(var->name, "candidate")) {
  1658. candidate = gtalk_create_candidate(var->value);
  1659. if (candidate) {
  1660. candidate->next = member->ourcandidates;
  1661. member->ourcandidates = candidate;
  1662. }
  1663. }
  1664. #endif
  1665. else if (!strcasecmp(var->name, "connection")) {
  1666. if ((client = ast_aji_get_client(var->value))) {
  1667. member->connection = client;
  1668. iks_filter_add_rule(client->f, gtalk_parser, member,
  1669. IKS_RULE_TYPE, IKS_PAK_IQ,
  1670. IKS_RULE_FROM_PARTIAL, member->user,
  1671. IKS_RULE_NS, "http://www.google.com/session",
  1672. IKS_RULE_DONE);
  1673. } else {
  1674. ast_log(LOG_ERROR, "connection referenced not found!\n");
  1675. return 0;
  1676. }
  1677. }
  1678. var = var->next;
  1679. }
  1680. if (member->connection && member->user)
  1681. member->buddy = ASTOBJ_CONTAINER_FIND(&member->connection->buddies, member->user);
  1682. else {
  1683. ast_log(LOG_ERROR, "No Connection or Username!\n");
  1684. }
  1685. return 1;
  1686. }
  1687. static int gtalk_load_config(void)
  1688. {
  1689. char *cat = NULL;
  1690. struct ast_config *cfg = NULL;
  1691. char context[AST_MAX_CONTEXT];
  1692. int allowguest = 1;
  1693. struct ast_variable *var;
  1694. struct gtalk *member;
  1695. struct ast_codec_pref prefs;
  1696. struct aji_client_container *clients;
  1697. struct gtalk_candidate *global_candidates = NULL;
  1698. struct hostent *hp;
  1699. struct ast_hostent ahp;
  1700. struct ast_flags config_flags = { 0 };
  1701. cfg = ast_config_load(GOOGLE_CONFIG, config_flags);
  1702. if (!cfg)
  1703. return 0;
  1704. /* Copy the default jb config over global_jbconf */
  1705. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  1706. cat = ast_category_browse(cfg, NULL);
  1707. for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
  1708. /* handle jb conf */
  1709. if (!ast_jb_read_conf(&global_jbconf, var->name, var->value))
  1710. continue;
  1711. if (!strcasecmp(var->name, "allowguest"))
  1712. allowguest =
  1713. (ast_true(ast_variable_retrieve(cfg, "general", "allowguest"))) ? 1 : 0;
  1714. else if (!strcasecmp(var->name, "disallow"))
  1715. ast_parse_allow_disallow(&prefs, &global_capability, var->value, 0);
  1716. else if (!strcasecmp(var->name, "allow"))
  1717. ast_parse_allow_disallow(&prefs, &global_capability, var->value, 1);
  1718. else if (!strcasecmp(var->name, "context"))
  1719. ast_copy_string(context, var->value, sizeof(context));
  1720. else if (!strcasecmp(var->name, "bindaddr")) {
  1721. if (!(hp = ast_gethostbyname(var->value, &ahp))) {
  1722. ast_log(LOG_WARNING, "Invalid address: %s\n", var->value);
  1723. } else {
  1724. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  1725. }
  1726. }
  1727. /* Idea to allow for custom candidates */
  1728. /*
  1729. else if (!strcasecmp(var->name, "candidate")) {
  1730. candidate = gtalk_create_candidate(var->value);
  1731. if (candidate) {
  1732. candidate->next = global_candidates;
  1733. global_candidates = candidate;
  1734. }
  1735. }
  1736. */
  1737. }
  1738. while (cat) {
  1739. if (strcasecmp(cat, "general")) {
  1740. var = ast_variable_browse(cfg, cat);
  1741. member = ast_calloc(1, sizeof(*member));
  1742. ASTOBJ_INIT(member);
  1743. ASTOBJ_WRLOCK(member);
  1744. if (!strcasecmp(cat, "guest")) {
  1745. ast_copy_string(member->name, "guest", sizeof(member->name));
  1746. ast_copy_string(member->user, "guest", sizeof(member->user));
  1747. ast_copy_string(member->context, context, sizeof(member->context));
  1748. member->allowguest = allowguest;
  1749. member->prefs = prefs;
  1750. while (var) {
  1751. if (!strcasecmp(var->name, "disallow"))
  1752. ast_parse_allow_disallow(&member->prefs, &member->capability,
  1753. var->value, 0);
  1754. else if (!strcasecmp(var->name, "allow"))
  1755. ast_parse_allow_disallow(&member->prefs, &member->capability,
  1756. var->value, 1);
  1757. else if (!strcasecmp(var->name, "context"))
  1758. ast_copy_string(member->context, var->value,
  1759. sizeof(member->context));
  1760. /* Idea to allow for custom candidates */
  1761. /*
  1762. else if (!strcasecmp(var->name, "candidate")) {
  1763. candidate = gtalk_create_candidate(var->value);
  1764. if (candidate) {
  1765. candidate->next = member->ourcandidates;
  1766. member->ourcandidates = candidate;
  1767. }
  1768. }
  1769. */
  1770. var = var->next;
  1771. }
  1772. ASTOBJ_UNLOCK(member);
  1773. clients = ast_aji_get_clients();
  1774. if (clients) {
  1775. ASTOBJ_CONTAINER_TRAVERSE(clients, 1, {
  1776. ASTOBJ_WRLOCK(iterator);
  1777. ASTOBJ_WRLOCK(member);
  1778. member->connection = NULL;
  1779. iks_filter_add_rule(iterator->f, gtalk_parser, member, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_NS, "http://www.google.com/session", IKS_RULE_DONE);
  1780. 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);
  1781. ASTOBJ_UNLOCK(member);
  1782. ASTOBJ_UNLOCK(iterator);
  1783. });
  1784. ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
  1785. ASTOBJ_UNREF(member, gtalk_member_destroy);
  1786. } else {
  1787. ASTOBJ_UNLOCK(member);
  1788. ASTOBJ_UNREF(member, gtalk_member_destroy);
  1789. }
  1790. } else {
  1791. ASTOBJ_UNLOCK(member);
  1792. if (gtalk_create_member(cat, var, allowguest, prefs, context, member))
  1793. ASTOBJ_CONTAINER_LINK(&gtalk_list, member);
  1794. ASTOBJ_UNREF(member, gtalk_member_destroy);
  1795. }
  1796. }
  1797. cat = ast_category_browse(cfg, cat);
  1798. }
  1799. gtalk_free_candidates(global_candidates);
  1800. return 1;
  1801. }
  1802. /*! \brief Load module into PBX, register channel */
  1803. static int load_module(void)
  1804. {
  1805. char *jabber_loaded = ast_module_helper("", "res_jabber.so", 0, 0, 0, 0);
  1806. free(jabber_loaded);
  1807. if (!jabber_loaded) {
  1808. /* If embedded, check for a different module name */
  1809. jabber_loaded = ast_module_helper("", "res_jabber", 0, 0, 0, 0);
  1810. free(jabber_loaded);
  1811. if (!jabber_loaded) {
  1812. ast_log(LOG_ERROR, "chan_gtalk.so depends upon res_jabber.so\n");
  1813. return AST_MODULE_LOAD_DECLINE;
  1814. }
  1815. }
  1816. ASTOBJ_CONTAINER_INIT(&gtalk_list);
  1817. if (!gtalk_load_config()) {
  1818. ast_log(LOG_ERROR, "Unable to read config file %s. Not loading module.\n", GOOGLE_CONFIG);
  1819. return 0;
  1820. }
  1821. sched = sched_context_create();
  1822. if (!sched)
  1823. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  1824. io = io_context_create();
  1825. if (!io)
  1826. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  1827. if (ast_find_ourip(&__ourip, bindaddr)) {
  1828. ast_log(LOG_WARNING, "Unable to get own IP address, Gtalk disabled\n");
  1829. return 0;
  1830. }
  1831. ast_rtp_proto_register(&gtalk_rtp);
  1832. ast_cli_register_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
  1833. /* Make sure we can register our channel type */
  1834. if (ast_channel_register(&gtalk_tech)) {
  1835. ast_log(LOG_ERROR, "Unable to register channel class %s\n", gtalk_tech.type);
  1836. return -1;
  1837. }
  1838. return 0;
  1839. }
  1840. /*! \brief Reload module */
  1841. static int reload(void)
  1842. {
  1843. return 0;
  1844. }
  1845. /*! \brief Unload the gtalk channel from Asterisk */
  1846. static int unload_module(void)
  1847. {
  1848. struct gtalk_pvt *privates = NULL;
  1849. ast_cli_unregister_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
  1850. /* First, take us out of the channel loop */
  1851. ast_channel_unregister(&gtalk_tech);
  1852. ast_rtp_proto_unregister(&gtalk_rtp);
  1853. if (!ast_mutex_lock(&gtalklock)) {
  1854. /* Hangup all interfaces if they have an owner */
  1855. ASTOBJ_CONTAINER_TRAVERSE(&gtalk_list, 1, {
  1856. ASTOBJ_WRLOCK(iterator);
  1857. privates = iterator->p;
  1858. while(privates) {
  1859. if (privates->owner)
  1860. ast_softhangup(privates->owner, AST_SOFTHANGUP_APPUNLOAD);
  1861. privates = privates->next;
  1862. }
  1863. iterator->p = NULL;
  1864. ASTOBJ_UNLOCK(iterator);
  1865. });
  1866. ast_mutex_unlock(&gtalklock);
  1867. } else {
  1868. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1869. return -1;
  1870. }
  1871. ASTOBJ_CONTAINER_DESTROYALL(&gtalk_list, gtalk_member_destroy);
  1872. ASTOBJ_CONTAINER_DESTROY(&gtalk_list);
  1873. return 0;
  1874. }
  1875. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Gtalk Channel Driver",
  1876. .load = load_module,
  1877. .unload = unload_module,
  1878. .reload = reload,
  1879. );