chan_gtalk.c 69 KB

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