chan_gtalk.c 63 KB

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