chan_jingle.c 62 KB

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