chan_jingle.c 59 KB

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