chan_gtalk.c 74 KB

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