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