chan_motif.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Joshua Colp <jcolp@digium.com>
  21. *
  22. * \brief Motif Jingle Channel Driver
  23. *
  24. * Iksemel http://iksemel.jabberstudio.org/
  25. *
  26. * \ingroup channel_drivers
  27. */
  28. /*! \li \ref chan_motif.c uses the configuration file \ref motif.conf
  29. * \addtogroup configuration_file
  30. */
  31. /*! \page motif.conf motif.conf
  32. * \verbinclude motif.conf.sample
  33. */
  34. /*** MODULEINFO
  35. <depend>iksemel</depend>
  36. <depend>res_xmpp</depend>
  37. <use type="external">openssl</use>
  38. <support_level>core</support_level>
  39. ***/
  40. #include "asterisk.h"
  41. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  42. #include <sys/socket.h>
  43. #include <fcntl.h>
  44. #include <netdb.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <sys/signal.h>
  48. #include <iksemel.h>
  49. #include <pthread.h>
  50. #include "asterisk/lock.h"
  51. #include "asterisk/channel.h"
  52. #include "asterisk/config_options.h"
  53. #include "asterisk/module.h"
  54. #include "asterisk/pbx.h"
  55. #include "asterisk/sched.h"
  56. #include "asterisk/io.h"
  57. #include "asterisk/rtp_engine.h"
  58. #include "asterisk/acl.h"
  59. #include "asterisk/callerid.h"
  60. #include "asterisk/file.h"
  61. #include "asterisk/cli.h"
  62. #include "asterisk/app.h"
  63. #include "asterisk/musiconhold.h"
  64. #include "asterisk/manager.h"
  65. #include "asterisk/stringfields.h"
  66. #include "asterisk/utils.h"
  67. #include "asterisk/causes.h"
  68. #include "asterisk/abstract_jb.h"
  69. #include "asterisk/xmpp.h"
  70. #include "asterisk/endpoints.h"
  71. #include "asterisk/stasis_channels.h"
  72. #include "asterisk/format_cache.h"
  73. /*** DOCUMENTATION
  74. <configInfo name="chan_motif" language="en_US">
  75. <synopsis>Jingle Channel Driver</synopsis>
  76. <description>
  77. <para><emphasis>Transports</emphasis></para>
  78. <para>There are three different transports and protocol derivatives
  79. supported by <literal>chan_motif</literal>. They are in order of
  80. preference: Jingle using ICE-UDP, Google Jingle, and Google-V1.</para>
  81. <para>Jingle as defined in XEP-0166 supports the widest range of
  82. features. It is referred to as <literal>ice-udp</literal>. This is
  83. the specification that Jingle clients implement.</para>
  84. <para>Google Jingle follows the Jingle specification for signaling
  85. but uses a custom transport for media. It is supported by the
  86. Google Talk Plug-in in Gmail and by some other Jingle clients. It
  87. is referred to as <literal>google</literal> in this file.</para>
  88. <para>Google-V1 is the original Google Talk signaling protocol
  89. which uses an initial preliminary version of Jingle. It also uses
  90. the same custom transport as Google Jingle for media. It is
  91. supported by Google Voice, some other Jingle clients, and the
  92. Windows Google Talk client. It is referred to as <literal>google-v1</literal>
  93. in this file.</para>
  94. <para>Incoming sessions will automatically switch to the correct
  95. transport once it has been determined.</para>
  96. <para>Outgoing sessions are capable of determining if the target
  97. is capable of Jingle or a Google transport if the target is in the
  98. roster. Unfortunately it is not possible to differentiate between
  99. a Google Jingle or Google-V1 capable resource until a session
  100. initiate attempt occurs. If a resource is determined to use a
  101. Google transport it will initially use Google Jingle but will fall
  102. back to Google-V1 if required.</para>
  103. <para>If an outgoing session attempt fails due to failure to
  104. support the given transport <literal>chan_motif</literal> will
  105. fall back in preference order listed previously until all
  106. transports have been exhausted.</para>
  107. <para><emphasis>Dialing and Resource Selection Strategy</emphasis></para>
  108. <para>Placing a call through an endpoint can be accomplished using the
  109. following dial string:</para>
  110. <para><literal>Motif/[endpoint name]/[target]</literal></para>
  111. <para>When placing an outgoing call through an endpoint the requested
  112. target is searched for in the roster list. If present the first Jingle
  113. or Google Jingle capable resource is specifically targeted. Since the
  114. capabilities of the resource are known the outgoing session initiation
  115. will disregard the configured transport and use the determined one.</para>
  116. <para>If the target is not found in the roster the target will be used
  117. as-is and a session will be initiated using the transport specified
  118. in this configuration file. If no transport has been specified the
  119. endpoint defaults to <literal>ice-udp</literal>.</para>
  120. <para><emphasis>Video Support</emphasis></para>
  121. <para>Support for video does not need to be explicitly enabled.
  122. Configuring any video codec on your endpoint will automatically enable
  123. it.</para>
  124. <para><emphasis>DTMF</emphasis></para>
  125. <para>The only supported method for DTMF is RFC2833. This is always
  126. enabled on audio streams and negotiated if possible.</para>
  127. <para><emphasis>Incoming Calls</emphasis></para>
  128. <para>Incoming calls will first look for the extension matching the
  129. name of the endpoint in the configured context. If no such extension
  130. exists the call will automatically fall back to the <literal>s</literal> extension.</para>
  131. <para><emphasis>CallerID</emphasis></para>
  132. <para>The incoming caller id number is populated with the username of
  133. the caller and the name is populated with the full identity of the
  134. caller. If you would like to perform authentication or filtering
  135. of incoming calls it is recommended that you use these fields to do so.</para>
  136. <para>Outgoing caller id can <emphasis>not</emphasis> be set.</para>
  137. <warning>
  138. <para>Multiple endpoints using the
  139. same connection is <emphasis>NOT</emphasis> supported. Doing so
  140. may result in broken calls.</para>
  141. </warning>
  142. </description>
  143. <configFile name="motif.conf">
  144. <configObject name="endpoint">
  145. <synopsis>The configuration for an endpoint.</synopsis>
  146. <configOption name="context">
  147. <synopsis>Default dialplan context that incoming sessions will be routed to</synopsis>
  148. </configOption>
  149. <configOption name="callgroup">
  150. <synopsis>A callgroup to assign to this endpoint.</synopsis>
  151. </configOption>
  152. <configOption name="pickupgroup">
  153. <synopsis>A pickup group to assign to this endpoint.</synopsis>
  154. </configOption>
  155. <configOption name="language">
  156. <synopsis>The default language for this endpoint.</synopsis>
  157. </configOption>
  158. <configOption name="musicclass">
  159. <synopsis>Default music on hold class for this endpoint.</synopsis>
  160. </configOption>
  161. <configOption name="parkinglot">
  162. <synopsis>Default parking lot for this endpoint.</synopsis>
  163. </configOption>
  164. <configOption name="accountcode">
  165. <synopsis>Accout code for CDR purposes</synopsis>
  166. </configOption>
  167. <configOption name="allow">
  168. <synopsis>Codecs to allow</synopsis>
  169. </configOption>
  170. <configOption name="disallow">
  171. <synopsis>Codecs to disallow</synopsis>
  172. </configOption>
  173. <configOption name="connection">
  174. <synopsis>Connection to accept traffic on and on which to send traffic out</synopsis>
  175. </configOption>
  176. <configOption name="transport">
  177. <synopsis>The transport to use for the endpoint.</synopsis>
  178. <description>
  179. <para>The default outbound transport for this endpoint. Inbound
  180. messages are inferred. Allowed transports are <literal>ice-udp</literal>,
  181. <literal>google</literal>, or <literal>google-v1</literal>. Note
  182. that <literal>chan_motif</literal> will fall back to transport
  183. preference order if the transport value chosen here fails.</para>
  184. <enumlist>
  185. <enum name="ice-udp">
  186. <para>The Jingle protocol, as defined in XEP 0166.</para>
  187. </enum>
  188. <enum name="google">
  189. <para>The Google Jingle protocol, which follows the Jingle
  190. specification for signaling but uses a custom transport for
  191. media.</para>
  192. </enum>
  193. <enum name="google-v1">
  194. <para>Google-V1 is the original Google Talk signaling
  195. protocol which uses an initial preliminary version of Jingle.
  196. It also uses the same custom transport as <literal>google</literal> for media.</para>
  197. </enum>
  198. </enumlist>
  199. </description>
  200. </configOption>
  201. <configOption name="maxicecandidates">
  202. <synopsis>Maximum number of ICE candidates to offer</synopsis>
  203. </configOption>
  204. <configOption name="maxpayloads">
  205. <synopsis>Maximum number of pyaloads to offer</synopsis>
  206. </configOption>
  207. </configObject>
  208. </configFile>
  209. </configInfo>
  210. ***/
  211. /*! \brief Default maximum number of ICE candidates we will offer */
  212. #define DEFAULT_MAX_ICE_CANDIDATES "10"
  213. /*! \brief Default maximum number of payloads we will offer */
  214. #define DEFAULT_MAX_PAYLOADS "30"
  215. /*! \brief Number of buckets for endpoints */
  216. #define ENDPOINT_BUCKETS 37
  217. /*! \brief Number of buckets for sessions, on a per-endpoint basis */
  218. #define SESSION_BUCKETS 37
  219. /*! \brief Namespace for Jingle itself */
  220. #define JINGLE_NS "urn:xmpp:jingle:1"
  221. /*! \brief Namespace for Jingle RTP sessions */
  222. #define JINGLE_RTP_NS "urn:xmpp:jingle:apps:rtp:1"
  223. /*! \brief Namespace for Jingle RTP info */
  224. #define JINGLE_RTP_INFO_NS "urn:xmpp:jingle:apps:rtp:info:1"
  225. /*! \brief Namespace for Jingle ICE-UDP */
  226. #define JINGLE_ICE_UDP_NS "urn:xmpp:jingle:transports:ice-udp:1"
  227. /*! \brief Namespace for Google Talk ICE-UDP */
  228. #define GOOGLE_TRANSPORT_NS "http://www.google.com/transport/p2p"
  229. /*! \brief Namespace for Google Talk Raw UDP */
  230. #define GOOGLE_TRANSPORT_RAW_NS "http://www.google.com/transport/raw-udp"
  231. /*! \brief Namespace for Google Session */
  232. #define GOOGLE_SESSION_NS "http://www.google.com/session"
  233. /*! \brief Namespace for Google Phone description */
  234. #define GOOGLE_PHONE_NS "http://www.google.com/session/phone"
  235. /*! \brief Namespace for Google Video description */
  236. #define GOOGLE_VIDEO_NS "http://www.google.com/session/video"
  237. /*! \brief Namespace for XMPP stanzas */
  238. #define XMPP_STANZAS_NS "urn:ietf:params:xml:ns:xmpp-stanzas"
  239. /*! \brief The various transport methods supported, from highest priority to lowest priority when doing fallback */
  240. enum jingle_transport {
  241. JINGLE_TRANSPORT_ICE_UDP = 3, /*!< XEP-0176 */
  242. JINGLE_TRANSPORT_GOOGLE_V2 = 2, /*!< https://developers.google.com/talk/call_signaling */
  243. JINGLE_TRANSPORT_GOOGLE_V1 = 1, /*!< Undocumented initial Google specification */
  244. JINGLE_TRANSPORT_NONE = 0, /*!< No transport specified */
  245. };
  246. /*! \brief Endpoint state information */
  247. struct jingle_endpoint_state {
  248. struct ao2_container *sessions; /*!< Active sessions to or from the endpoint */
  249. };
  250. /*! \brief Endpoint which contains configuration information and active sessions */
  251. struct jingle_endpoint {
  252. AST_DECLARE_STRING_FIELDS(
  253. AST_STRING_FIELD(name); /*!< Name of the endpoint */
  254. AST_STRING_FIELD(context); /*!< Context to place incoming calls into */
  255. AST_STRING_FIELD(accountcode); /*!< Account code */
  256. AST_STRING_FIELD(language); /*!< Default language for prompts */
  257. AST_STRING_FIELD(musicclass); /*!< Configured music on hold class */
  258. AST_STRING_FIELD(parkinglot); /*!< Configured parking lot */
  259. );
  260. struct ast_xmpp_client *connection; /*!< Connection to use for traffic */
  261. iksrule *rule; /*!< Active matching rule */
  262. unsigned int maxicecandidates; /*!< Maximum number of ICE candidates we will offer */
  263. unsigned int maxpayloads; /*!< Maximum number of payloads we will offer */
  264. struct ast_format_cap *cap; /*!< Formats to use */
  265. ast_group_t callgroup; /*!< Call group */
  266. ast_group_t pickupgroup; /*!< Pickup group */
  267. enum jingle_transport transport; /*!< Default transport to use on outgoing sessions */
  268. struct jingle_endpoint_state *state; /*!< Endpoint state information */
  269. };
  270. /*! \brief Session which contains information about an active session */
  271. struct jingle_session {
  272. AST_DECLARE_STRING_FIELDS(
  273. AST_STRING_FIELD(sid); /*!< Session identifier */
  274. AST_STRING_FIELD(audio_name); /*!< Name of the audio content */
  275. AST_STRING_FIELD(video_name); /*!< Name of the video content */
  276. );
  277. struct jingle_endpoint_state *state; /*!< Endpoint we are associated with */
  278. struct ast_xmpp_client *connection; /*!< Connection to use for traffic */
  279. enum jingle_transport transport; /*!< Transport type to use for this session */
  280. unsigned int maxicecandidates; /*!< Maximum number of ICE candidates we will offer */
  281. unsigned int maxpayloads; /*!< Maximum number of payloads we will offer */
  282. char remote_original[XMPP_MAX_JIDLEN];/*!< Identifier of the original remote party (remote may have changed due to redirect) */
  283. char remote[XMPP_MAX_JIDLEN]; /*!< Identifier of the remote party */
  284. iksrule *rule; /*!< Session matching rule */
  285. struct ast_channel *owner; /*!< Master Channel */
  286. struct ast_rtp_instance *rtp; /*!< RTP audio session */
  287. struct ast_rtp_instance *vrtp; /*!< RTP video session */
  288. struct ast_format_cap *cap; /*!< Local codec capabilities */
  289. struct ast_format_cap *jointcap; /*!< Joint codec capabilities */
  290. struct ast_format_cap *peercap; /*!< Peer codec capabilities */
  291. unsigned int outgoing:1; /*!< Whether this is an outgoing leg or not */
  292. unsigned int gone:1; /*!< In the eyes of Jingle this session is already gone */
  293. struct ast_callid *callid; /*!< Bound session call-id */
  294. };
  295. static const char channel_type[] = "Motif";
  296. struct jingle_config {
  297. struct ao2_container *endpoints; /*!< Configured endpoints */
  298. };
  299. static AO2_GLOBAL_OBJ_STATIC(globals);
  300. static struct ast_sched_context *sched; /*!< Scheduling context for RTCP */
  301. /* \brief Asterisk core interaction functions */
  302. static struct ast_channel *jingle_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  303. static int jingle_sendtext(struct ast_channel *ast, const char *text);
  304. static int jingle_digit_begin(struct ast_channel *ast, char digit);
  305. static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  306. static int jingle_call(struct ast_channel *ast, const char *dest, int timeout);
  307. static int jingle_hangup(struct ast_channel *ast);
  308. static int jingle_answer(struct ast_channel *ast);
  309. static struct ast_frame *jingle_read(struct ast_channel *ast);
  310. static int jingle_write(struct ast_channel *ast, struct ast_frame *f);
  311. static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  312. static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  313. static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid);
  314. /*! \brief Action handlers */
  315. static void jingle_action_session_initiate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  316. static void jingle_action_transport_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  317. static void jingle_action_session_accept(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  318. static void jingle_action_session_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  319. static void jingle_action_session_terminate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  320. /*! \brief PBX interface structure for channel registration */
  321. static struct ast_channel_tech jingle_tech = {
  322. .type = "Motif",
  323. .description = "Motif Jingle Channel Driver",
  324. .requester = jingle_request,
  325. .send_text = jingle_sendtext,
  326. .send_digit_begin = jingle_digit_begin,
  327. .send_digit_end = jingle_digit_end,
  328. .call = jingle_call,
  329. .hangup = jingle_hangup,
  330. .answer = jingle_answer,
  331. .read = jingle_read,
  332. .write = jingle_write,
  333. .write_video = jingle_write,
  334. .exception = jingle_read,
  335. .indicate = jingle_indicate,
  336. .fixup = jingle_fixup,
  337. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
  338. };
  339. /*! \brief Defined handlers for different Jingle actions */
  340. static const struct jingle_action_handler {
  341. const char *action;
  342. void (*handler)(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
  343. } jingle_action_handlers[] = {
  344. /* Jingle actions */
  345. { "session-initiate", jingle_action_session_initiate, },
  346. { "transport-info", jingle_action_transport_info, },
  347. { "session-accept", jingle_action_session_accept, },
  348. { "session-info", jingle_action_session_info, },
  349. { "session-terminate", jingle_action_session_terminate, },
  350. /* Google-V1 actions */
  351. { "initiate", jingle_action_session_initiate, },
  352. { "candidates", jingle_action_transport_info, },
  353. { "accept", jingle_action_session_accept, },
  354. { "terminate", jingle_action_session_terminate, },
  355. { "reject", jingle_action_session_terminate, },
  356. };
  357. /*! \brief Reason text <-> cause code mapping */
  358. static const struct jingle_reason_mapping {
  359. const char *reason;
  360. int cause;
  361. } jingle_reason_mappings[] = {
  362. { "busy", AST_CAUSE_BUSY, },
  363. { "cancel", AST_CAUSE_CALL_REJECTED, },
  364. { "connectivity-error", AST_CAUSE_INTERWORKING, },
  365. { "decline", AST_CAUSE_CALL_REJECTED, },
  366. { "expired", AST_CAUSE_NO_USER_RESPONSE, },
  367. { "failed-transport", AST_CAUSE_PROTOCOL_ERROR, },
  368. { "failed-application", AST_CAUSE_SWITCH_CONGESTION, },
  369. { "general-error", AST_CAUSE_CONGESTION, },
  370. { "gone", AST_CAUSE_NORMAL_CLEARING, },
  371. { "incompatible-parameters", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
  372. { "media-error", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
  373. { "security-error", AST_CAUSE_PROTOCOL_ERROR, },
  374. { "success", AST_CAUSE_NORMAL_CLEARING, },
  375. { "timeout", AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, },
  376. { "unsupported-applications", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
  377. { "unsupported-transports", AST_CAUSE_FACILITY_NOT_IMPLEMENTED, },
  378. };
  379. /*! \brief Hashing function for Jingle sessions */
  380. static int jingle_session_hash(const void *obj, const int flags)
  381. {
  382. const struct jingle_session *session = obj;
  383. const char *sid = obj;
  384. return ast_str_hash(flags & OBJ_KEY ? sid : session->sid);
  385. }
  386. /*! \brief Comparator function for Jingle sessions */
  387. static int jingle_session_cmp(void *obj, void *arg, int flags)
  388. {
  389. struct jingle_session *session1 = obj, *session2 = arg;
  390. const char *sid = arg;
  391. return !strcmp(session1->sid, flags & OBJ_KEY ? sid : session2->sid) ? CMP_MATCH | CMP_STOP : 0;
  392. }
  393. /*! \brief Destructor for Jingle endpoint state */
  394. static void jingle_endpoint_state_destructor(void *obj)
  395. {
  396. struct jingle_endpoint_state *state = obj;
  397. ao2_ref(state->sessions, -1);
  398. }
  399. /*! \brief Destructor for Jingle endpoints */
  400. static void jingle_endpoint_destructor(void *obj)
  401. {
  402. struct jingle_endpoint *endpoint = obj;
  403. if (endpoint->rule) {
  404. iks_filter_remove_rule(endpoint->connection->filter, endpoint->rule);
  405. }
  406. if (endpoint->connection) {
  407. ast_xmpp_client_unref(endpoint->connection);
  408. }
  409. ao2_cleanup(endpoint->cap);
  410. ao2_ref(endpoint->state, -1);
  411. ast_string_field_free_memory(endpoint);
  412. }
  413. /*! \brief Find function for Jingle endpoints */
  414. static void *jingle_endpoint_find(struct ao2_container *tmp_container, const char *category)
  415. {
  416. return ao2_find(tmp_container, category, OBJ_KEY);
  417. }
  418. /*! \brief Allocator function for Jingle endpoint state */
  419. static struct jingle_endpoint_state *jingle_endpoint_state_create(void)
  420. {
  421. struct jingle_endpoint_state *state;
  422. if (!(state = ao2_alloc(sizeof(*state), jingle_endpoint_state_destructor))) {
  423. return NULL;
  424. }
  425. state->sessions = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  426. SESSION_BUCKETS, jingle_session_hash, NULL, jingle_session_cmp);
  427. if (!state->sessions) {
  428. ao2_ref(state, -1);
  429. return NULL;
  430. }
  431. return state;
  432. }
  433. /*! \brief State find/create function */
  434. static struct jingle_endpoint_state *jingle_endpoint_state_find_or_create(const char *category)
  435. {
  436. RAII_VAR(struct jingle_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  437. RAII_VAR(struct jingle_endpoint *, endpoint, NULL, ao2_cleanup);
  438. if (!cfg || !cfg->endpoints || !(endpoint = jingle_endpoint_find(cfg->endpoints, category))) {
  439. return jingle_endpoint_state_create();
  440. }
  441. ao2_ref(endpoint->state, +1);
  442. return endpoint->state;
  443. }
  444. /*! \brief Allocator function for Jingle endpoints */
  445. static void *jingle_endpoint_alloc(const char *cat)
  446. {
  447. struct jingle_endpoint *endpoint;
  448. if (!(endpoint = ao2_alloc(sizeof(*endpoint), jingle_endpoint_destructor))) {
  449. return NULL;
  450. }
  451. if (ast_string_field_init(endpoint, 512)) {
  452. ao2_ref(endpoint, -1);
  453. return NULL;
  454. }
  455. if (!(endpoint->state = jingle_endpoint_state_find_or_create(cat))) {
  456. ao2_ref(endpoint, -1);
  457. return NULL;
  458. }
  459. ast_string_field_set(endpoint, name, cat);
  460. endpoint->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  461. endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;
  462. return endpoint;
  463. }
  464. /*! \brief Hashing function for Jingle endpoints */
  465. static int jingle_endpoint_hash(const void *obj, const int flags)
  466. {
  467. const struct jingle_endpoint *endpoint = obj;
  468. const char *name = obj;
  469. return ast_str_hash(flags & OBJ_KEY ? name : endpoint->name);
  470. }
  471. /*! \brief Comparator function for Jingle endpoints */
  472. static int jingle_endpoint_cmp(void *obj, void *arg, int flags)
  473. {
  474. struct jingle_endpoint *endpoint1 = obj, *endpoint2 = arg;
  475. const char *name = arg;
  476. return !strcmp(endpoint1->name, flags & OBJ_KEY ? name : endpoint2->name) ? CMP_MATCH | CMP_STOP : 0;
  477. }
  478. static struct aco_type endpoint_option = {
  479. .type = ACO_ITEM,
  480. .name = "endpoint",
  481. .category_match = ACO_BLACKLIST_EXACT,
  482. .category = "general",
  483. .item_alloc = jingle_endpoint_alloc,
  484. .item_find = jingle_endpoint_find,
  485. .item_offset = offsetof(struct jingle_config, endpoints),
  486. };
  487. struct aco_type *endpoint_options[] = ACO_TYPES(&endpoint_option);
  488. struct aco_file jingle_conf = {
  489. .filename = "motif.conf",
  490. .types = ACO_TYPES(&endpoint_option),
  491. };
  492. /*! \brief Destructor for Jingle sessions */
  493. static void jingle_session_destructor(void *obj)
  494. {
  495. struct jingle_session *session = obj;
  496. if (session->rule) {
  497. iks_filter_remove_rule(session->connection->filter, session->rule);
  498. }
  499. if (session->connection) {
  500. ast_xmpp_client_unref(session->connection);
  501. }
  502. if (session->rtp) {
  503. ast_rtp_instance_stop(session->rtp);
  504. ast_rtp_instance_destroy(session->rtp);
  505. }
  506. if (session->vrtp) {
  507. ast_rtp_instance_stop(session->vrtp);
  508. ast_rtp_instance_destroy(session->vrtp);
  509. }
  510. ao2_cleanup(session->cap);
  511. ao2_cleanup(session->jointcap);
  512. ao2_cleanup(session->peercap);
  513. if (session->callid) {
  514. ast_callid_unref(session->callid);
  515. }
  516. ast_string_field_free_memory(session);
  517. }
  518. /*! \brief Destructor called when module configuration goes away */
  519. static void jingle_config_destructor(void *obj)
  520. {
  521. struct jingle_config *cfg = obj;
  522. ao2_cleanup(cfg->endpoints);
  523. }
  524. /*! \brief Allocator called when module configuration should appear */
  525. static void *jingle_config_alloc(void)
  526. {
  527. struct jingle_config *cfg;
  528. if (!(cfg = ao2_alloc(sizeof(*cfg), jingle_config_destructor))) {
  529. return NULL;
  530. }
  531. cfg->endpoints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  532. ENDPOINT_BUCKETS, jingle_endpoint_hash, NULL, jingle_endpoint_cmp);
  533. if (!cfg->endpoints) {
  534. ao2_ref(cfg, -1);
  535. return NULL;
  536. }
  537. return cfg;
  538. }
  539. CONFIG_INFO_STANDARD(cfg_info, globals, jingle_config_alloc,
  540. .files = ACO_FILES(&jingle_conf),
  541. );
  542. /*! \brief Function called by RTP engine to get local RTP peer */
  543. static enum ast_rtp_glue_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  544. {
  545. struct jingle_session *session = ast_channel_tech_pvt(chan);
  546. enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
  547. if (!session->rtp) {
  548. return AST_RTP_GLUE_RESULT_FORBID;
  549. }
  550. ao2_ref(session->rtp, +1);
  551. *instance = session->rtp;
  552. return res;
  553. }
  554. /*! \brief Function called by RTP engine to get peer capabilities */
  555. static void jingle_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
  556. {
  557. }
  558. /*! \brief Function called by RTP engine to change where the remote party should send media */
  559. 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)
  560. {
  561. return -1;
  562. }
  563. /*! \brief Local glue for interacting with the RTP engine core */
  564. static struct ast_rtp_glue jingle_rtp_glue = {
  565. .type = "Motif",
  566. .get_rtp_info = jingle_get_rtp_peer,
  567. .get_codec = jingle_get_codec,
  568. .update_peer = jingle_set_rtp_peer,
  569. };
  570. /*! \brief Set the channel owner on the \ref jingle_session object and related objects */
  571. static void jingle_set_owner(struct jingle_session *session, struct ast_channel *chan)
  572. {
  573. session->owner = chan;
  574. if (session->rtp) {
  575. ast_rtp_instance_set_channel_id(session->rtp, session->owner ? ast_channel_uniqueid(session->owner) : "");
  576. }
  577. if (session->vrtp) {
  578. ast_rtp_instance_set_channel_id(session->vrtp, session->owner ? ast_channel_uniqueid(session->owner) : "");
  579. }
  580. }
  581. /*! \brief Internal helper function which enables video support on a sesson if possible */
  582. static void jingle_enable_video(struct jingle_session *session)
  583. {
  584. struct ast_sockaddr tmp;
  585. struct ast_rtp_engine_ice *ice;
  586. /* If video is already present don't do anything */
  587. if (session->vrtp) {
  588. return;
  589. }
  590. /* If there are no configured video codecs do not turn video support on, it just won't work */
  591. if (!ast_format_cap_has_type(session->cap, AST_MEDIA_TYPE_VIDEO)) {
  592. return;
  593. }
  594. ast_sockaddr_parse(&tmp, "0.0.0.0", 0);
  595. if (!(session->vrtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
  596. return;
  597. }
  598. ast_rtp_instance_set_prop(session->vrtp, AST_RTP_PROPERTY_RTCP, 1);
  599. ast_rtp_instance_set_channel_id(session->vrtp, ast_channel_uniqueid(session->owner));
  600. ast_channel_set_fd(session->owner, 2, ast_rtp_instance_fd(session->vrtp, 0));
  601. ast_channel_set_fd(session->owner, 3, ast_rtp_instance_fd(session->vrtp, 1));
  602. ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(session->vrtp),
  603. ast_format_cap_get_framing(session->cap));
  604. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2 && (ice = ast_rtp_instance_get_ice(session->vrtp))) {
  605. ice->stop(session->vrtp);
  606. }
  607. }
  608. /*! \brief Internal helper function used to allocate Jingle session on an endpoint */
  609. static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid)
  610. {
  611. struct jingle_session *session;
  612. struct ast_callid *callid;
  613. struct ast_sockaddr tmp;
  614. if (!(session = ao2_alloc(sizeof(*session), jingle_session_destructor))) {
  615. return NULL;
  616. }
  617. callid = ast_read_threadstorage_callid();
  618. session->callid = (callid ? callid : ast_create_callid());
  619. if (ast_string_field_init(session, 512)) {
  620. ao2_ref(session, -1);
  621. return NULL;
  622. }
  623. if (!ast_strlen_zero(from)) {
  624. ast_copy_string(session->remote_original, from, sizeof(session->remote_original));
  625. ast_copy_string(session->remote, from, sizeof(session->remote));
  626. }
  627. if (ast_strlen_zero(sid)) {
  628. ast_string_field_build(session, sid, "%08lx%08lx", (unsigned long)ast_random(), (unsigned long)ast_random());
  629. session->outgoing = 1;
  630. ast_string_field_set(session, audio_name, "audio");
  631. ast_string_field_set(session, video_name, "video");
  632. } else {
  633. ast_string_field_set(session, sid, sid);
  634. }
  635. ao2_ref(endpoint->state, +1);
  636. session->state = endpoint->state;
  637. ao2_ref(endpoint->connection, +1);
  638. session->connection = endpoint->connection;
  639. session->transport = endpoint->transport;
  640. if (!(session->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  641. !(session->jointcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  642. !(session->peercap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  643. !session->callid) {
  644. ao2_ref(session, -1);
  645. return NULL;
  646. }
  647. ast_format_cap_append_from_cap(session->cap, endpoint->cap, AST_MEDIA_TYPE_UNKNOWN);
  648. /* While we rely on res_xmpp for communication we still need a temporary ast_sockaddr to tell the RTP engine
  649. * that we want IPv4 */
  650. ast_sockaddr_parse(&tmp, "0.0.0.0", 0);
  651. /* Sessions always carry audio, but video is optional so don't enable it here */
  652. if (!(session->rtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
  653. ao2_ref(session, -1);
  654. return NULL;
  655. }
  656. ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_RTCP, 1);
  657. ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_DTMF, 1);
  658. session->maxicecandidates = endpoint->maxicecandidates;
  659. session->maxpayloads = endpoint->maxpayloads;
  660. return session;
  661. }
  662. /*! \brief Function called to create a new Jingle Asterisk channel */
  663. static struct ast_channel *jingle_new(struct jingle_endpoint *endpoint, struct jingle_session *session, int state, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
  664. {
  665. struct ast_channel *chan;
  666. const char *str = S_OR(title, session->remote);
  667. struct ast_format_cap *caps;
  668. struct ast_format *tmpfmt;
  669. if (!ast_format_cap_count(session->cap)) {
  670. return NULL;
  671. }
  672. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  673. if (!caps) {
  674. return NULL;
  675. }
  676. if (!(chan = ast_channel_alloc_with_endpoint(1, state, S_OR(title, ""), S_OR(cid_name, ""), "", "", "", assignedids, requestor, 0, endpoint->connection->endpoint, "Motif/%s-%04lx", str, (unsigned long)(ast_random() & 0xffff)))) {
  677. ao2_ref(caps, -1);
  678. return NULL;
  679. }
  680. ast_channel_stage_snapshot(chan);
  681. ast_channel_tech_set(chan, &jingle_tech);
  682. ast_channel_tech_pvt_set(chan, session);
  683. jingle_set_owner(session, chan);
  684. ast_channel_callid_set(chan, session->callid);
  685. ast_format_cap_append_from_cap(caps, session->cap, AST_MEDIA_TYPE_UNKNOWN);
  686. ast_channel_nativeformats_set(chan, caps);
  687. ao2_ref(caps, -1);
  688. if (session->rtp) {
  689. struct ast_rtp_engine_ice *ice;
  690. ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(session->rtp, 0));
  691. ast_channel_set_fd(chan, 1, ast_rtp_instance_fd(session->rtp, 1));
  692. ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(session->rtp),
  693. ast_format_cap_get_framing(session->cap));
  694. if (((session->transport == JINGLE_TRANSPORT_GOOGLE_V2) ||
  695. (session->transport == JINGLE_TRANSPORT_GOOGLE_V1)) &&
  696. (ice = ast_rtp_instance_get_ice(session->rtp))) {
  697. /* We stop built in ICE support because we need to fall back to old old old STUN support */
  698. ice->stop(session->rtp);
  699. }
  700. }
  701. if (state == AST_STATE_RING) {
  702. ast_channel_rings_set(chan, 1);
  703. }
  704. ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
  705. tmpfmt = ast_format_cap_get_format(session->cap, 0);
  706. ast_channel_set_writeformat(chan, tmpfmt);
  707. ast_channel_set_rawwriteformat(chan, tmpfmt);
  708. ast_channel_set_readformat(chan, tmpfmt);
  709. ast_channel_set_rawreadformat(chan, tmpfmt);
  710. ao2_ref(tmpfmt, -1);
  711. ao2_lock(endpoint);
  712. ast_channel_callgroup_set(chan, endpoint->callgroup);
  713. ast_channel_pickupgroup_set(chan, endpoint->pickupgroup);
  714. if (!ast_strlen_zero(endpoint->accountcode)) {
  715. ast_channel_accountcode_set(chan, endpoint->accountcode);
  716. }
  717. if (!ast_strlen_zero(endpoint->language)) {
  718. ast_channel_language_set(chan, endpoint->language);
  719. }
  720. if (!ast_strlen_zero(endpoint->musicclass)) {
  721. ast_channel_musicclass_set(chan, endpoint->musicclass);
  722. }
  723. ast_channel_context_set(chan, endpoint->context);
  724. if (ast_exists_extension(NULL, endpoint->context, endpoint->name, 1, NULL)) {
  725. ast_channel_exten_set(chan, endpoint->name);
  726. } else {
  727. ast_channel_exten_set(chan, "s");
  728. }
  729. ast_channel_priority_set(chan, 1);
  730. ao2_unlock(endpoint);
  731. ast_channel_stage_snapshot_done(chan);
  732. ast_channel_unlock(chan);
  733. return chan;
  734. }
  735. /*! \brief Internal helper function which sends a response */
  736. static void jingle_send_response(struct ast_xmpp_client *connection, ikspak *pak)
  737. {
  738. iks *response;
  739. if (!(response = iks_new("iq"))) {
  740. ast_log(LOG_ERROR, "Unable to allocate an IKS response stanza\n");
  741. return;
  742. }
  743. iks_insert_attrib(response, "type", "result");
  744. iks_insert_attrib(response, "from", connection->jid->full);
  745. iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
  746. iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
  747. ast_xmpp_client_send(connection, response);
  748. iks_delete(response);
  749. }
  750. /*! \brief Internal helper function which sends an error response */
  751. static void jingle_send_error_response(struct ast_xmpp_client *connection, ikspak *pak, const char *type, const char *reasonstr, const char *reasonstr2)
  752. {
  753. iks *response, *error = NULL, *reason = NULL, *reason2 = NULL;
  754. if (!(response = iks_new("iq")) ||
  755. !(error = iks_new("error")) ||
  756. !(reason = iks_new(reasonstr))) {
  757. ast_log(LOG_ERROR, "Unable to allocate IKS error response stanzas\n");
  758. goto end;
  759. }
  760. iks_insert_attrib(response, "type", "error");
  761. iks_insert_attrib(response, "from", connection->jid->full);
  762. iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
  763. iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));
  764. iks_insert_attrib(error, "type", type);
  765. iks_insert_node(error, reason);
  766. if (!ast_strlen_zero(reasonstr2) && (reason2 = iks_new(reasonstr2))) {
  767. iks_insert_node(error, reason2);
  768. }
  769. iks_insert_node(response, error);
  770. ast_xmpp_client_send(connection, response);
  771. end:
  772. iks_delete(reason2);
  773. iks_delete(reason);
  774. iks_delete(error);
  775. iks_delete(response);
  776. }
  777. /*! \brief Internal helper function which adds ICE-UDP candidates to a transport node */
  778. static int jingle_add_ice_udp_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates, unsigned int maximum)
  779. {
  780. struct ast_rtp_engine_ice *ice;
  781. struct ao2_container *local_candidates;
  782. struct ao2_iterator it;
  783. struct ast_rtp_engine_ice_candidate *candidate;
  784. int i = 0, res = 0;
  785. if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
  786. ast_log(LOG_ERROR, "Unable to add ICE-UDP candidates as ICE support not available or no candidates available\n");
  787. return -1;
  788. }
  789. iks_insert_attrib(transport, "xmlns", JINGLE_ICE_UDP_NS);
  790. iks_insert_attrib(transport, "pwd", ice->get_password(rtp));
  791. iks_insert_attrib(transport, "ufrag", ice->get_ufrag(rtp));
  792. it = ao2_iterator_init(local_candidates, 0);
  793. while ((candidate = ao2_iterator_next(&it)) && (i < maximum)) {
  794. iks *local_candidate;
  795. char tmp[30];
  796. if (!(local_candidate = iks_new("candidate"))) {
  797. res = -1;
  798. ast_log(LOG_ERROR, "Unable to allocate IKS candidate stanza for ICE-UDP transport\n");
  799. break;
  800. }
  801. snprintf(tmp, sizeof(tmp), "%u", candidate->id);
  802. iks_insert_attrib(local_candidate, "component", tmp);
  803. snprintf(tmp, sizeof(tmp), "%d", ast_str_hash(candidate->foundation));
  804. iks_insert_attrib(local_candidate, "foundation", tmp);
  805. iks_insert_attrib(local_candidate, "generation", "0");
  806. iks_insert_attrib(local_candidate, "network", "0");
  807. snprintf(tmp, sizeof(tmp), "%04lx", (unsigned long)(ast_random() & 0xffff));
  808. iks_insert_attrib(local_candidate, "id", tmp);
  809. iks_insert_attrib(local_candidate, "ip", ast_sockaddr_stringify_host(&candidate->address));
  810. iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));
  811. snprintf(tmp, sizeof(tmp), "%d", candidate->priority);
  812. iks_insert_attrib(local_candidate, "priority", tmp);
  813. iks_insert_attrib(local_candidate, "protocol", "udp");
  814. if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
  815. iks_insert_attrib(local_candidate, "type", "host");
  816. } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
  817. iks_insert_attrib(local_candidate, "type", "srflx");
  818. } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
  819. iks_insert_attrib(local_candidate, "type", "relay");
  820. }
  821. iks_insert_node(transport, local_candidate);
  822. candidates[i++] = local_candidate;
  823. }
  824. ao2_iterator_destroy(&it);
  825. ao2_ref(local_candidates, -1);
  826. return res;
  827. }
  828. /*! \brief Internal helper function which adds Google candidates to a transport node */
  829. static int jingle_add_google_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates, unsigned int video, enum jingle_transport transport_type, unsigned int maximum)
  830. {
  831. struct ast_rtp_engine_ice *ice;
  832. struct ao2_container *local_candidates;
  833. struct ao2_iterator it;
  834. struct ast_rtp_engine_ice_candidate *candidate;
  835. int i = 0, res = 0;
  836. if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
  837. ast_log(LOG_ERROR, "Unable to add Google ICE candidates as ICE support not available or no candidates available\n");
  838. return -1;
  839. }
  840. if (transport_type != JINGLE_TRANSPORT_GOOGLE_V1) {
  841. iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
  842. }
  843. it = ao2_iterator_init(local_candidates, 0);
  844. while ((candidate = ao2_iterator_next(&it)) && (i < maximum)) {
  845. iks *local_candidate;
  846. /* In Google land a username is 16 bytes, explicitly */
  847. char ufrag[17] = "";
  848. if (!(local_candidate = iks_new("candidate"))) {
  849. res = -1;
  850. ast_log(LOG_ERROR, "Unable to allocate IKS candidate stanza for Google ICE transport\n");
  851. break;
  852. }
  853. if (candidate->id == 1) {
  854. iks_insert_attrib(local_candidate, "name", !video ? "rtp" : "video_rtp");
  855. } else if (candidate->id == 2) {
  856. iks_insert_attrib(local_candidate, "name", !video ? "rtcp" : "video_rtcp");
  857. } else {
  858. iks_delete(local_candidate);
  859. continue;
  860. }
  861. iks_insert_attrib(local_candidate, "address", ast_sockaddr_stringify_host(&candidate->address));
  862. iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));
  863. if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
  864. iks_insert_attrib(local_candidate, "preference", "0.95");
  865. iks_insert_attrib(local_candidate, "type", "local");
  866. } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
  867. iks_insert_attrib(local_candidate, "preference", "0.9");
  868. iks_insert_attrib(local_candidate, "type", "stun");
  869. }
  870. iks_insert_attrib(local_candidate, "protocol", "udp");
  871. iks_insert_attrib(local_candidate, "network", "0");
  872. snprintf(ufrag, sizeof(ufrag), "%s", ice->get_ufrag(rtp));
  873. iks_insert_attrib(local_candidate, "username", ufrag);
  874. iks_insert_attrib(local_candidate, "generation", "0");
  875. if (transport_type == JINGLE_TRANSPORT_GOOGLE_V1) {
  876. iks_insert_attrib(local_candidate, "password", "");
  877. iks_insert_attrib(local_candidate, "foundation", "0");
  878. iks_insert_attrib(local_candidate, "component", "1");
  879. } else {
  880. iks_insert_attrib(local_candidate, "password", ice->get_password(rtp));
  881. }
  882. /* You may notice a lack of relay support up above - this is because we don't support it for use with
  883. * the Google talk transport due to their arcane support. */
  884. iks_insert_node(transport, local_candidate);
  885. candidates[i++] = local_candidate;
  886. }
  887. ao2_iterator_destroy(&it);
  888. ao2_ref(local_candidates, -1);
  889. return res;
  890. }
  891. /*! \brief Internal function which sends a session-terminate message */
  892. static void jingle_send_session_terminate(struct jingle_session *session, const char *reasontext)
  893. {
  894. iks *iq = NULL, *jingle = NULL, *reason = NULL, *text = NULL;
  895. if (!(iq = iks_new("iq")) || !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle")) ||
  896. !(reason = iks_new("reason")) || !(text = iks_new(reasontext))) {
  897. ast_log(LOG_ERROR, "Failed to allocate stanzas for session-terminate message on session '%s'\n", session->sid);
  898. goto end;
  899. }
  900. iks_insert_attrib(iq, "to", session->remote);
  901. iks_insert_attrib(iq, "type", "set");
  902. iks_insert_attrib(iq, "id", session->connection->mid);
  903. ast_xmpp_increment_mid(session->connection->mid);
  904. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  905. iks_insert_attrib(jingle, "type", "terminate");
  906. iks_insert_attrib(jingle, "id", session->sid);
  907. iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
  908. iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
  909. } else {
  910. iks_insert_attrib(jingle, "action", "session-terminate");
  911. iks_insert_attrib(jingle, "sid", session->sid);
  912. iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
  913. }
  914. iks_insert_node(iq, jingle);
  915. iks_insert_node(jingle, reason);
  916. iks_insert_node(reason, text);
  917. ast_xmpp_client_send(session->connection, iq);
  918. end:
  919. iks_delete(text);
  920. iks_delete(reason);
  921. iks_delete(jingle);
  922. iks_delete(iq);
  923. }
  924. /*! \brief Internal function which sends a session-info message */
  925. static void jingle_send_session_info(struct jingle_session *session, const char *info)
  926. {
  927. iks *iq = NULL, *jingle = NULL, *text = NULL;
  928. /* Google-V1 has no way to send informational messages so don't even bother trying */
  929. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  930. return;
  931. }
  932. if (!(iq = iks_new("iq")) || !(jingle = iks_new("jingle")) || !(text = iks_new(info))) {
  933. ast_log(LOG_ERROR, "Failed to allocate stanzas for session-info message on session '%s'\n", session->sid);
  934. goto end;
  935. }
  936. iks_insert_attrib(iq, "to", session->remote);
  937. iks_insert_attrib(iq, "type", "set");
  938. iks_insert_attrib(iq, "id", session->connection->mid);
  939. ast_xmpp_increment_mid(session->connection->mid);
  940. iks_insert_attrib(jingle, "action", "session-info");
  941. iks_insert_attrib(jingle, "sid", session->sid);
  942. iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
  943. iks_insert_node(iq, jingle);
  944. iks_insert_node(jingle, text);
  945. ast_xmpp_client_send(session->connection, iq);
  946. end:
  947. iks_delete(text);
  948. iks_delete(jingle);
  949. iks_delete(iq);
  950. }
  951. /*! \internal
  952. *
  953. * \brief Locks both pvt and pvt owner if owner is present.
  954. *
  955. * \note This function gives a ref to pvt->owner if it is present and locked.
  956. * This reference must be decremented after pvt->owner is unlocked.
  957. *
  958. * \note This function will never give you up,
  959. * \note This function will never let you down.
  960. * \note This function will run around and desert you.
  961. *
  962. * \pre pvt is not locked
  963. * \post pvt is locked
  964. * \post pvt->owner is locked and its reference count is increased (if pvt->owner is not NULL)
  965. *
  966. * \returns a pointer to the locked and reffed pvt->owner channel if it exists.
  967. */
  968. static struct ast_channel *jingle_session_lock_full(struct jingle_session *pvt)
  969. {
  970. struct ast_channel *chan;
  971. /* Locking is simple when it is done right. If you see a deadlock resulting
  972. * in this function, it is not this function's fault, Your problem exists elsewhere.
  973. * This function is perfect... seriously. */
  974. for (;;) {
  975. /* First, get the channel and grab a reference to it */
  976. ao2_lock(pvt);
  977. chan = pvt->owner;
  978. if (chan) {
  979. /* The channel can not go away while we hold the pvt lock.
  980. * Give the channel a ref so it will not go away after we let
  981. * the pvt lock go. */
  982. ast_channel_ref(chan);
  983. } else {
  984. /* no channel, return pvt locked */
  985. return NULL;
  986. }
  987. /* We had to hold the pvt lock while getting a ref to the owner channel
  988. * but now we have to let this lock go in order to preserve proper
  989. * locking order when grabbing the channel lock */
  990. ao2_unlock(pvt);
  991. /* Look, no deadlock avoidance, hooray! */
  992. ast_channel_lock(chan);
  993. ao2_lock(pvt);
  994. if (pvt->owner == chan) {
  995. /* done */
  996. break;
  997. }
  998. /* If the owner changed while everything was unlocked, no problem,
  999. * just start over and everthing will work. This is rare, do not be
  1000. * confused by this loop and think this it is an expensive operation.
  1001. * The majority of the calls to this function will never involve multiple
  1002. * executions of this loop. */
  1003. ast_channel_unlock(chan);
  1004. ast_channel_unref(chan);
  1005. ao2_unlock(pvt);
  1006. }
  1007. /* If owner exists, it is locked and reffed */
  1008. return pvt->owner;
  1009. }
  1010. /*! \brief Helper function which queues a hangup frame with cause code */
  1011. static void jingle_queue_hangup_with_cause(struct jingle_session *session, int cause)
  1012. {
  1013. struct ast_channel *chan;
  1014. if ((chan = jingle_session_lock_full(session))) {
  1015. ast_debug(3, "Hanging up channel '%s' with cause '%d'\n", ast_channel_name(chan), cause);
  1016. ast_queue_hangup_with_cause(chan, cause);
  1017. ast_channel_unlock(chan);
  1018. ast_channel_unref(chan);
  1019. }
  1020. ao2_unlock(session);
  1021. }
  1022. /*! \brief Internal function which sends a transport-info message */
  1023. static void jingle_send_transport_info(struct jingle_session *session, const char *from)
  1024. {
  1025. iks *iq, *jingle = NULL, *audio = NULL, *audio_transport = NULL, *video = NULL, *video_transport = NULL;
  1026. iks *audio_candidates[session->maxicecandidates], *video_candidates[session->maxicecandidates];
  1027. int i, res = 0;
  1028. if (!(iq = iks_new("iq")) ||
  1029. !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
  1030. iks_delete(iq);
  1031. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1032. ast_log(LOG_ERROR, "Failed to allocate stanzas for transport-info message, hanging up session '%s'\n", session->sid);
  1033. return;
  1034. }
  1035. memset(audio_candidates, 0, sizeof(audio_candidates));
  1036. memset(video_candidates, 0, sizeof(video_candidates));
  1037. iks_insert_attrib(iq, "from", session->connection->jid->full);
  1038. iks_insert_attrib(iq, "to", from);
  1039. iks_insert_attrib(iq, "type", "set");
  1040. iks_insert_attrib(iq, "id", session->connection->mid);
  1041. ast_xmpp_increment_mid(session->connection->mid);
  1042. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1043. iks_insert_attrib(jingle, "type", "candidates");
  1044. iks_insert_attrib(jingle, "id", session->sid);
  1045. iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
  1046. iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : from);
  1047. } else {
  1048. iks_insert_attrib(jingle, "action", "transport-info");
  1049. iks_insert_attrib(jingle, "sid", session->sid);
  1050. iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
  1051. }
  1052. iks_insert_node(iq, jingle);
  1053. if (session->rtp) {
  1054. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1055. /* V1 protocol has the candidates directly in the session */
  1056. res = jingle_add_google_candidates_to_transport(session->rtp, jingle, audio_candidates, 0, session->transport, session->maxicecandidates);
  1057. } else if ((audio = iks_new("content")) && (audio_transport = iks_new("transport"))) {
  1058. iks_insert_attrib(audio, "creator", session->outgoing ? "initiator" : "responder");
  1059. iks_insert_attrib(audio, "name", session->audio_name);
  1060. iks_insert_node(jingle, audio);
  1061. iks_insert_node(audio, audio_transport);
  1062. if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
  1063. res = jingle_add_ice_udp_candidates_to_transport(session->rtp, audio_transport, audio_candidates, session->maxicecandidates);
  1064. } else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
  1065. res = jingle_add_google_candidates_to_transport(session->rtp, audio_transport, audio_candidates, 0, session->transport,
  1066. session->maxicecandidates);
  1067. }
  1068. } else {
  1069. res = -1;
  1070. }
  1071. }
  1072. if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
  1073. if ((video = iks_new("content")) && (video_transport = iks_new("transport"))) {
  1074. iks_insert_attrib(video, "creator", session->outgoing ? "initiator" : "responder");
  1075. iks_insert_attrib(video, "name", session->video_name);
  1076. iks_insert_node(jingle, video);
  1077. iks_insert_node(video, video_transport);
  1078. if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
  1079. res = jingle_add_ice_udp_candidates_to_transport(session->vrtp, video_transport, video_candidates, session->maxicecandidates);
  1080. } else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
  1081. res = jingle_add_google_candidates_to_transport(session->vrtp, video_transport, video_candidates, 1, session->transport,
  1082. session->maxicecandidates);
  1083. }
  1084. } else {
  1085. res = -1;
  1086. }
  1087. }
  1088. if (!res) {
  1089. ast_xmpp_client_send(session->connection, iq);
  1090. } else {
  1091. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1092. }
  1093. /* Clean up after ourselves */
  1094. for (i = 0; i < session->maxicecandidates; i++) {
  1095. iks_delete(video_candidates[i]);
  1096. iks_delete(audio_candidates[i]);
  1097. }
  1098. iks_delete(video_transport);
  1099. iks_delete(video);
  1100. iks_delete(audio_transport);
  1101. iks_delete(audio);
  1102. iks_delete(jingle);
  1103. iks_delete(iq);
  1104. }
  1105. /*! \brief Internal helper function which adds payloads to a description */
  1106. static int jingle_add_payloads_to_description(struct jingle_session *session, struct ast_rtp_instance *rtp, iks *description, iks **payloads, enum ast_media_type type)
  1107. {
  1108. int x = 0, i = 0, res = 0;
  1109. for (x = 0; (x < ast_format_cap_count(session->jointcap)) && (i < (session->maxpayloads - 2)); x++) {
  1110. struct ast_format *format = ast_format_cap_get_format(session->jointcap, x);
  1111. int rtp_code;
  1112. iks *payload;
  1113. char tmp[32];
  1114. if (ast_format_get_type(format) != type) {
  1115. ao2_ref(format, -1);
  1116. continue;
  1117. }
  1118. if (((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1, format, 0)) == -1) ||
  1119. (!(payload = iks_new("payload-type")))) {
  1120. ao2_ref(format, -1);
  1121. return -1;
  1122. }
  1123. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1124. iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
  1125. }
  1126. snprintf(tmp, sizeof(tmp), "%d", rtp_code);
  1127. iks_insert_attrib(payload, "id", tmp);
  1128. iks_insert_attrib(payload, "name", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
  1129. iks_insert_attrib(payload, "channels", "1");
  1130. if ((ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) &&
  1131. ((session->transport == JINGLE_TRANSPORT_GOOGLE_V1) || (session->transport == JINGLE_TRANSPORT_GOOGLE_V2))) {
  1132. iks_insert_attrib(payload, "clockrate", "16000");
  1133. } else {
  1134. snprintf(tmp, sizeof(tmp), "%u", ast_rtp_lookup_sample_rate2(1, format, 0));
  1135. iks_insert_attrib(payload, "clockrate", tmp);
  1136. }
  1137. if ((type == AST_MEDIA_TYPE_VIDEO) && (session->transport == JINGLE_TRANSPORT_GOOGLE_V2)) {
  1138. iks *parameter;
  1139. /* Google requires these parameters to be set, but alas we can not give accurate values so use some safe defaults */
  1140. if ((parameter = iks_new("parameter"))) {
  1141. iks_insert_attrib(parameter, "name", "width");
  1142. iks_insert_attrib(parameter, "value", "640");
  1143. iks_insert_node(payload, parameter);
  1144. }
  1145. if ((parameter = iks_new("parameter"))) {
  1146. iks_insert_attrib(parameter, "name", "height");
  1147. iks_insert_attrib(parameter, "value", "480");
  1148. iks_insert_node(payload, parameter);
  1149. }
  1150. if ((parameter = iks_new("parameter"))) {
  1151. iks_insert_attrib(parameter, "name", "framerate");
  1152. iks_insert_attrib(parameter, "value", "30");
  1153. iks_insert_node(payload, parameter);
  1154. }
  1155. }
  1156. iks_insert_node(description, payload);
  1157. payloads[i++] = payload;
  1158. ao2_ref(format, -1);
  1159. }
  1160. /* If this is for audio and there is room for RFC2833 add it in */
  1161. if ((type == AST_MEDIA_TYPE_AUDIO) && (i < session->maxpayloads)) {
  1162. iks *payload;
  1163. if ((payload = iks_new("payload-type"))) {
  1164. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1165. iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
  1166. }
  1167. iks_insert_attrib(payload, "id", "101");
  1168. iks_insert_attrib(payload, "name", "telephone-event");
  1169. iks_insert_attrib(payload, "channels", "1");
  1170. iks_insert_attrib(payload, "clockrate", "8000");
  1171. iks_insert_node(description, payload);
  1172. payloads[i++] = payload;
  1173. }
  1174. }
  1175. return res;
  1176. }
  1177. /*! \brief Helper function which adds content to a description */
  1178. static int jingle_add_content(struct jingle_session *session, iks *jingle, iks *content, iks *description, iks *transport,
  1179. const char *name, enum ast_media_type type, struct ast_rtp_instance *rtp, iks **payloads)
  1180. {
  1181. int res = 0;
  1182. if (session->transport != JINGLE_TRANSPORT_GOOGLE_V1) {
  1183. iks_insert_attrib(content, "creator", session->outgoing ? "initiator" : "responder");
  1184. iks_insert_attrib(content, "name", name);
  1185. iks_insert_node(jingle, content);
  1186. iks_insert_attrib(description, "xmlns", JINGLE_RTP_NS);
  1187. if (type == AST_MEDIA_TYPE_AUDIO) {
  1188. iks_insert_attrib(description, "media", "audio");
  1189. } else if (type == AST_MEDIA_TYPE_VIDEO) {
  1190. iks_insert_attrib(description, "media", "video");
  1191. } else {
  1192. return -1;
  1193. }
  1194. iks_insert_node(content, description);
  1195. } else {
  1196. iks_insert_attrib(description, "xmlns", GOOGLE_PHONE_NS);
  1197. iks_insert_node(jingle, description);
  1198. }
  1199. if (!(res = jingle_add_payloads_to_description(session, rtp, description, payloads, type))) {
  1200. if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
  1201. iks_insert_attrib(transport, "xmlns", JINGLE_ICE_UDP_NS);
  1202. iks_insert_node(content, transport);
  1203. } else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
  1204. iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
  1205. iks_insert_node(content, transport);
  1206. }
  1207. }
  1208. return res;
  1209. }
  1210. /*! \brief Internal function which sends a complete session message */
  1211. static void jingle_send_session_action(struct jingle_session *session, const char *action)
  1212. {
  1213. iks *iq, *jingle, *audio = NULL, *audio_description = NULL, *video = NULL, *video_description = NULL;
  1214. iks *audio_payloads[session->maxpayloads], *video_payloads[session->maxpayloads];
  1215. iks *audio_transport = NULL, *video_transport = NULL;
  1216. int i, res = 0;
  1217. if (!(iq = iks_new("iq")) ||
  1218. !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
  1219. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1220. iks_delete(iq);
  1221. return;
  1222. }
  1223. memset(audio_payloads, 0, sizeof(audio_payloads));
  1224. memset(video_payloads, 0, sizeof(video_payloads));
  1225. iks_insert_attrib(iq, "from", session->connection->jid->full);
  1226. iks_insert_attrib(iq, "to", session->remote);
  1227. iks_insert_attrib(iq, "type", "set");
  1228. iks_insert_attrib(iq, "id", session->connection->mid);
  1229. ast_xmpp_increment_mid(session->connection->mid);
  1230. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1231. iks_insert_attrib(jingle, "type", action);
  1232. iks_insert_attrib(jingle, "id", session->sid);
  1233. iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
  1234. } else {
  1235. iks_insert_attrib(jingle, "action", action);
  1236. iks_insert_attrib(jingle, "sid", session->sid);
  1237. iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
  1238. }
  1239. if (!strcasecmp(action, "session-initiate") || !strcasecmp(action, "initiate") || !strcasecmp(action, "accept")) {
  1240. iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
  1241. }
  1242. iks_insert_node(iq, jingle);
  1243. if (session->rtp && (audio = iks_new("content")) && (audio_description = iks_new("description")) &&
  1244. (audio_transport = iks_new("transport"))) {
  1245. res = jingle_add_content(session, jingle, audio, audio_description, audio_transport, session->audio_name,
  1246. AST_MEDIA_TYPE_AUDIO, session->rtp, audio_payloads);
  1247. } else {
  1248. ast_log(LOG_ERROR, "Failed to allocate audio content stanzas for session '%s', hanging up\n", session->sid);
  1249. res = -1;
  1250. }
  1251. if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
  1252. if ((video = iks_new("content")) && (video_description = iks_new("description")) &&
  1253. (video_transport = iks_new("transport"))) {
  1254. res = jingle_add_content(session, jingle, video, video_description, video_transport, session->video_name,
  1255. AST_MEDIA_TYPE_VIDEO, session->vrtp, video_payloads);
  1256. } else {
  1257. ast_log(LOG_ERROR, "Failed to allocate video content stanzas for session '%s', hanging up\n", session->sid);
  1258. res = -1;
  1259. }
  1260. }
  1261. if (!res) {
  1262. ast_xmpp_client_send(session->connection, iq);
  1263. } else {
  1264. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1265. }
  1266. iks_delete(video_transport);
  1267. iks_delete(audio_transport);
  1268. for (i = 0; i < session->maxpayloads; i++) {
  1269. iks_delete(video_payloads[i]);
  1270. iks_delete(audio_payloads[i]);
  1271. }
  1272. iks_delete(video_description);
  1273. iks_delete(video);
  1274. iks_delete(audio_description);
  1275. iks_delete(audio);
  1276. iks_delete(jingle);
  1277. iks_delete(iq);
  1278. }
  1279. /*! \brief Internal function which sends a session-inititate message */
  1280. static void jingle_send_session_initiate(struct jingle_session *session)
  1281. {
  1282. jingle_send_session_action(session, session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "initiate" : "session-initiate");
  1283. }
  1284. /*! \brief Internal function which sends a session-accept message */
  1285. static void jingle_send_session_accept(struct jingle_session *session)
  1286. {
  1287. jingle_send_session_action(session, session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "accept" : "session-accept");
  1288. }
  1289. /*! \brief Callback for when a response is received for an outgoing session-initiate message */
  1290. static int jingle_outgoing_hook(void *data, ikspak *pak)
  1291. {
  1292. struct jingle_session *session = data;
  1293. iks *error = iks_find(pak->x, "error"), *redirect;
  1294. /* In all cases this hook is done with */
  1295. iks_filter_remove_rule(session->connection->filter, session->rule);
  1296. session->rule = NULL;
  1297. ast_callid_threadassoc_add(session->callid);
  1298. /* If no error occurred they accepted our session-initiate message happily */
  1299. if (!error) {
  1300. struct ast_channel *chan;
  1301. if ((chan = jingle_session_lock_full(session))) {
  1302. ast_queue_control(chan, AST_CONTROL_PROCEEDING);
  1303. ast_channel_unlock(chan);
  1304. ast_channel_unref(chan);
  1305. }
  1306. ao2_unlock(session);
  1307. jingle_send_transport_info(session, iks_find_attrib(pak->x, "from"));
  1308. goto end;
  1309. }
  1310. /* Assume that because this is an error the session is gone, there is only one case where this is incorrect - a redirect */
  1311. session->gone = 1;
  1312. /* Map the error we received to an appropriate cause code and hang up the channel */
  1313. if ((redirect = iks_find_with_attrib(error, "redirect", "xmlns", XMPP_STANZAS_NS))) {
  1314. iks *to = iks_child(redirect);
  1315. char *target;
  1316. if (to && (target = iks_name(to)) && !ast_strlen_zero(target)) {
  1317. /* Make the xmpp: go away if it is present */
  1318. if (!strncmp(target, "xmpp:", 5)) {
  1319. target += 5;
  1320. }
  1321. /* This is actually a fairly simple operation - we update the remote and send another session-initiate */
  1322. ast_copy_string(session->remote, target, sizeof(session->remote));
  1323. /* Add a new hook so we can get the status of redirected session */
  1324. session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
  1325. IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);
  1326. jingle_send_session_initiate(session);
  1327. session->gone = 0;
  1328. } else {
  1329. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1330. }
  1331. } else if (iks_find_with_attrib(error, "service-unavailable", "xmlns", XMPP_STANZAS_NS)) {
  1332. jingle_queue_hangup_with_cause(session, AST_CAUSE_CONGESTION);
  1333. } else if (iks_find_with_attrib(error, "resource-constraint", "xmlns", XMPP_STANZAS_NS)) {
  1334. jingle_queue_hangup_with_cause(session, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
  1335. } else if (iks_find_with_attrib(error, "bad-request", "xmlns", XMPP_STANZAS_NS)) {
  1336. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1337. } else if (iks_find_with_attrib(error, "remote-server-not-found", "xmlns", XMPP_STANZAS_NS)) {
  1338. jingle_queue_hangup_with_cause(session, AST_CAUSE_NO_ROUTE_DESTINATION);
  1339. } else if (iks_find_with_attrib(error, "feature-not-implemented", "xmlns", XMPP_STANZAS_NS)) {
  1340. /* Assume that this occurred because the remote side does not support our transport, so drop it down one and try again */
  1341. session->transport--;
  1342. /* If we still have a viable transport mechanism re-send the session-initiate */
  1343. if (session->transport != JINGLE_TRANSPORT_NONE) {
  1344. struct ast_rtp_engine_ice *ice;
  1345. if (((session->transport == JINGLE_TRANSPORT_GOOGLE_V2) ||
  1346. (session->transport == JINGLE_TRANSPORT_GOOGLE_V1)) &&
  1347. (ice = ast_rtp_instance_get_ice(session->rtp))) {
  1348. /* We stop built in ICE support because we need to fall back to old old old STUN support */
  1349. ice->stop(session->rtp);
  1350. }
  1351. /* Re-send the message to the *original* target and not a redirected one */
  1352. ast_copy_string(session->remote, session->remote_original, sizeof(session->remote));
  1353. session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
  1354. IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);
  1355. jingle_send_session_initiate(session);
  1356. session->gone = 0;
  1357. } else {
  1358. /* Otherwise we have exhausted all transports */
  1359. jingle_queue_hangup_with_cause(session, AST_CAUSE_FACILITY_NOT_IMPLEMENTED);
  1360. }
  1361. } else {
  1362. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1363. }
  1364. end:
  1365. ast_callid_threadassoc_remove();
  1366. return IKS_FILTER_EAT;
  1367. }
  1368. /*! \brief Function called by core when we should answer a Jingle session */
  1369. static int jingle_answer(struct ast_channel *ast)
  1370. {
  1371. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1372. /* The channel has already been answered so we don't need to do anything */
  1373. if (ast_channel_state(ast) == AST_STATE_UP) {
  1374. return 0;
  1375. }
  1376. jingle_send_session_accept(session);
  1377. return 0;
  1378. }
  1379. /*! \brief Function called by core to read any waiting frames */
  1380. static struct ast_frame *jingle_read(struct ast_channel *ast)
  1381. {
  1382. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1383. struct ast_frame *frame = &ast_null_frame;
  1384. switch (ast_channel_fdno(ast)) {
  1385. case 0:
  1386. if (session->rtp) {
  1387. frame = ast_rtp_instance_read(session->rtp, 0);
  1388. }
  1389. break;
  1390. case 1:
  1391. if (session->rtp) {
  1392. frame = ast_rtp_instance_read(session->rtp, 1);
  1393. }
  1394. break;
  1395. case 2:
  1396. if (session->vrtp) {
  1397. frame = ast_rtp_instance_read(session->vrtp, 0);
  1398. }
  1399. break;
  1400. case 3:
  1401. if (session->vrtp) {
  1402. frame = ast_rtp_instance_read(session->vrtp, 1);
  1403. }
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. if (frame && frame->frametype == AST_FRAME_VOICE &&
  1409. ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  1410. if (ast_format_cap_iscompatible_format(session->jointcap, frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  1411. ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
  1412. ast_format_get_name(frame->subclass.format), ast_channel_name(ast));
  1413. ast_frfree(frame);
  1414. frame = &ast_null_frame;
  1415. } else {
  1416. struct ast_format_cap *caps;
  1417. ast_debug(1, "Oooh, format changed to %s\n",
  1418. ast_format_get_name(frame->subclass.format));
  1419. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1420. if (caps) {
  1421. ast_format_cap_append(caps, frame->subclass.format, 0);
  1422. ast_channel_nativeformats_set(ast, caps);
  1423. ao2_ref(caps, -1);
  1424. }
  1425. ast_set_read_format(ast, ast_channel_readformat(ast));
  1426. ast_set_write_format(ast, ast_channel_writeformat(ast));
  1427. }
  1428. }
  1429. return frame;
  1430. }
  1431. /*! \brief Function called by core to write frames */
  1432. static int jingle_write(struct ast_channel *ast, struct ast_frame *frame)
  1433. {
  1434. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1435. int res = 0;
  1436. switch (frame->frametype) {
  1437. case AST_FRAME_VOICE:
  1438. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  1439. struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  1440. ast_log(LOG_WARNING,
  1441. "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
  1442. ast_format_get_name(frame->subclass.format),
  1443. ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
  1444. ast_format_get_name(ast_channel_readformat(ast)),
  1445. ast_format_get_name(ast_channel_writeformat(ast)));
  1446. return 0;
  1447. }
  1448. if (session && session->rtp) {
  1449. res = ast_rtp_instance_write(session->rtp, frame);
  1450. }
  1451. break;
  1452. case AST_FRAME_VIDEO:
  1453. if (session && session->vrtp) {
  1454. res = ast_rtp_instance_write(session->vrtp, frame);
  1455. }
  1456. break;
  1457. default:
  1458. ast_log(LOG_WARNING, "Can't send %u type frames with Jingle write\n",
  1459. frame->frametype);
  1460. return 0;
  1461. }
  1462. return res;
  1463. }
  1464. /*! \brief Function called by core to change the underlying owner channel */
  1465. static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1466. {
  1467. struct jingle_session *session = ast_channel_tech_pvt(newchan);
  1468. ao2_lock(session);
  1469. jingle_set_owner(session, newchan);
  1470. ao2_unlock(session);
  1471. return 0;
  1472. }
  1473. /*! \brief Function called by core to ask the channel to indicate some sort of condition */
  1474. static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  1475. {
  1476. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1477. int res = 0;
  1478. switch (condition) {
  1479. case AST_CONTROL_RINGING:
  1480. if (ast_channel_state(ast) == AST_STATE_RING) {
  1481. jingle_send_session_info(session, "ringing xmlns='urn:xmpp:jingle:apps:rtp:info:1'");
  1482. } else {
  1483. res = -1;
  1484. }
  1485. break;
  1486. case AST_CONTROL_BUSY:
  1487. if (ast_channel_state(ast) != AST_STATE_UP) {
  1488. ast_channel_hangupcause_set(ast, AST_CAUSE_BUSY);
  1489. ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
  1490. } else {
  1491. res = -1;
  1492. }
  1493. break;
  1494. case AST_CONTROL_CONGESTION:
  1495. if (ast_channel_state(ast) != AST_STATE_UP) {
  1496. ast_channel_hangupcause_set(ast, AST_CAUSE_CONGESTION);
  1497. ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
  1498. } else {
  1499. res = -1;
  1500. }
  1501. break;
  1502. case AST_CONTROL_INCOMPLETE:
  1503. if (ast_channel_state(ast) != AST_STATE_UP) {
  1504. ast_channel_hangupcause_set(ast, AST_CAUSE_CONGESTION);
  1505. ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
  1506. }
  1507. break;
  1508. case AST_CONTROL_HOLD:
  1509. ast_moh_start(ast, data, NULL);
  1510. break;
  1511. case AST_CONTROL_UNHOLD:
  1512. ast_moh_stop(ast);
  1513. break;
  1514. case AST_CONTROL_SRCUPDATE:
  1515. if (session->rtp) {
  1516. ast_rtp_instance_update_source(session->rtp);
  1517. }
  1518. break;
  1519. case AST_CONTROL_SRCCHANGE:
  1520. if (session->rtp) {
  1521. ast_rtp_instance_change_source(session->rtp);
  1522. }
  1523. break;
  1524. case AST_CONTROL_VIDUPDATE:
  1525. case AST_CONTROL_UPDATE_RTP_PEER:
  1526. case AST_CONTROL_CONNECTED_LINE:
  1527. break;
  1528. case AST_CONTROL_PVT_CAUSE_CODE:
  1529. case AST_CONTROL_MASQUERADE_NOTIFY:
  1530. case -1:
  1531. res = -1;
  1532. break;
  1533. default:
  1534. ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
  1535. res = -1;
  1536. }
  1537. return res;
  1538. }
  1539. /*! \brief Function called by core to send text to the remote party of the Jingle session */
  1540. static int jingle_sendtext(struct ast_channel *chan, const char *text)
  1541. {
  1542. struct jingle_session *session = ast_channel_tech_pvt(chan);
  1543. return ast_xmpp_client_send_message(session->connection, session->remote, text);
  1544. }
  1545. /*! \brief Function called by core to start a DTMF digit */
  1546. static int jingle_digit_begin(struct ast_channel *chan, char digit)
  1547. {
  1548. struct jingle_session *session = ast_channel_tech_pvt(chan);
  1549. if (session->rtp) {
  1550. ast_rtp_instance_dtmf_begin(session->rtp, digit);
  1551. }
  1552. return 0;
  1553. }
  1554. /*! \brief Function called by core to stop a DTMF digit */
  1555. static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1556. {
  1557. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1558. if (session->rtp) {
  1559. ast_rtp_instance_dtmf_end_with_duration(session->rtp, digit, duration);
  1560. }
  1561. return 0;
  1562. }
  1563. /*! \brief Function called by core to actually start calling a remote party */
  1564. static int jingle_call(struct ast_channel *ast, const char *dest, int timeout)
  1565. {
  1566. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1567. ast_setstate(ast, AST_STATE_RING);
  1568. /* Since we have no idea of the remote capabilities use ours for now */
  1569. ast_format_cap_append_from_cap(session->jointcap, session->cap, AST_MEDIA_TYPE_UNKNOWN);
  1570. /* We set up a hook so we can know when our session-initiate message was accepted or rejected */
  1571. session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
  1572. IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);
  1573. jingle_send_session_initiate(session);
  1574. return 0;
  1575. }
  1576. /*! \brief Function called by core to hang up a Jingle session */
  1577. static int jingle_hangup(struct ast_channel *ast)
  1578. {
  1579. struct jingle_session *session = ast_channel_tech_pvt(ast);
  1580. ao2_lock(session);
  1581. if ((ast_channel_state(ast) != AST_STATE_DOWN) && !session->gone) {
  1582. int cause = (session->owner ? ast_channel_hangupcause(session->owner) : AST_CAUSE_CONGESTION);
  1583. const char *reason = "success";
  1584. int i;
  1585. /* Get the appropriate reason and send a session-terminate */
  1586. for (i = 0; i < ARRAY_LEN(jingle_reason_mappings); i++) {
  1587. if (jingle_reason_mappings[i].cause == cause) {
  1588. reason = jingle_reason_mappings[i].reason;
  1589. break;
  1590. }
  1591. }
  1592. jingle_send_session_terminate(session, reason);
  1593. }
  1594. ast_channel_tech_pvt_set(ast, NULL);
  1595. jingle_set_owner(session, NULL);
  1596. ao2_unlink(session->state->sessions, session);
  1597. ao2_ref(session->state, -1);
  1598. ao2_unlock(session);
  1599. ao2_ref(session, -1);
  1600. return 0;
  1601. }
  1602. /*! \brief Function called by core to create a new outgoing Jingle session */
  1603. static struct ast_channel *jingle_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  1604. {
  1605. RAII_VAR(struct jingle_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1606. RAII_VAR(struct jingle_endpoint *, endpoint, NULL, ao2_cleanup);
  1607. char *dialed, target[1024] = "";
  1608. struct ast_xmpp_buddy *buddy;
  1609. struct jingle_session *session;
  1610. struct ast_channel *chan;
  1611. enum jingle_transport transport = JINGLE_TRANSPORT_NONE;
  1612. struct ast_rtp_engine_ice *ice;
  1613. AST_DECLARE_APP_ARGS(args,
  1614. AST_APP_ARG(name);
  1615. AST_APP_ARG(target);
  1616. );
  1617. /* We require at a minimum one audio format to be requested */
  1618. if (!ast_format_cap_has_type(cap, AST_MEDIA_TYPE_AUDIO)) {
  1619. ast_log(LOG_ERROR, "Motif channel driver requires an audio format when dialing a destination\n");
  1620. *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  1621. return NULL;
  1622. }
  1623. if (ast_strlen_zero(data) || !(dialed = ast_strdupa(data))) {
  1624. ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
  1625. *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1626. return NULL;
  1627. }
  1628. /* Parse the given dial string and validate the results */
  1629. AST_NONSTANDARD_APP_ARGS(args, dialed, '/');
  1630. if (ast_strlen_zero(args.name) || ast_strlen_zero(args.target)) {
  1631. ast_log(LOG_ERROR, "Unable to determine endpoint name and target.\n");
  1632. *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1633. return NULL;
  1634. }
  1635. if (!(endpoint = jingle_endpoint_find(cfg->endpoints, args.name))) {
  1636. ast_log(LOG_ERROR, "Endpoint '%s' does not exist.\n", args.name);
  1637. *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1638. return NULL;
  1639. }
  1640. ao2_lock(endpoint->state);
  1641. /* If we don't have a connection for the endpoint we can't exactly start a session on it */
  1642. if (!endpoint->connection) {
  1643. ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s' as no valid connection exists\n", args.name);
  1644. *cause = AST_CAUSE_SWITCH_CONGESTION;
  1645. ao2_unlock(endpoint->state);
  1646. return NULL;
  1647. }
  1648. /* Find the target in the roster so we can choose a resource */
  1649. if ((buddy = ao2_find(endpoint->connection->buddies, args.target, OBJ_KEY))) {
  1650. struct ao2_iterator res;
  1651. struct ast_xmpp_resource *resource;
  1652. /* Iterate through finding the first viable Jingle capable resource */
  1653. res = ao2_iterator_init(buddy->resources, 0);
  1654. while ((resource = ao2_iterator_next(&res))) {
  1655. if (resource->caps.jingle) {
  1656. snprintf(target, sizeof(target), "%s/%s", args.target, resource->resource);
  1657. transport = JINGLE_TRANSPORT_ICE_UDP;
  1658. break;
  1659. } else if (resource->caps.google) {
  1660. snprintf(target, sizeof(target), "%s/%s", args.target, resource->resource);
  1661. transport = JINGLE_TRANSPORT_GOOGLE_V2;
  1662. break;
  1663. }
  1664. ao2_ref(resource, -1);
  1665. }
  1666. ao2_iterator_destroy(&res);
  1667. ao2_ref(buddy, -1);
  1668. } else {
  1669. /* If the target is NOT in the roster use the provided target as-is */
  1670. ast_copy_string(target, args.target, sizeof(target));
  1671. }
  1672. ao2_unlock(endpoint->state);
  1673. /* If no target was found we can't set up a session */
  1674. if (ast_strlen_zero(target)) {
  1675. ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s' as no capable resource for target '%s' was found\n", args.name, args.target);
  1676. *cause = AST_CAUSE_SWITCH_CONGESTION;
  1677. return NULL;
  1678. }
  1679. if (!(session = jingle_alloc(endpoint, target, NULL))) {
  1680. ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s'\n", args.name);
  1681. *cause = AST_CAUSE_SWITCH_CONGESTION;
  1682. return NULL;
  1683. }
  1684. /* Update the transport if we learned what we should actually use */
  1685. if (transport != JINGLE_TRANSPORT_NONE) {
  1686. session->transport = transport;
  1687. /* Note that for Google-V1 and Google-V2 we don't stop built-in ICE support, this will happen in jingle_new */
  1688. }
  1689. if (!(chan = jingle_new(endpoint, session, AST_STATE_DOWN, target, assignedids, requestor, NULL))) {
  1690. ast_log(LOG_ERROR, "Unable to create Jingle channel on endpoint '%s'\n", args.name);
  1691. *cause = AST_CAUSE_SWITCH_CONGESTION;
  1692. ao2_ref(session, -1);
  1693. return NULL;
  1694. }
  1695. /* If video was requested try to enable it on the session */
  1696. if (ast_format_cap_has_type(cap, AST_MEDIA_TYPE_VIDEO)) {
  1697. jingle_enable_video(session);
  1698. }
  1699. /* As this is outgoing set ourselves as controlling */
  1700. if (session->rtp && (ice = ast_rtp_instance_get_ice(session->rtp))) {
  1701. ice->ice_lite(session->rtp);
  1702. }
  1703. if (session->vrtp && (ice = ast_rtp_instance_get_ice(session->vrtp))) {
  1704. ice->ice_lite(session->vrtp);
  1705. }
  1706. /* We purposely don't decrement the session here as there is a reference on the channel */
  1707. ao2_link(endpoint->state->sessions, session);
  1708. return chan;
  1709. }
  1710. /*! \brief Helper function which handles content descriptions */
  1711. static int jingle_interpret_description(struct jingle_session *session, iks *description, const char *name, struct ast_rtp_instance **rtp)
  1712. {
  1713. char *media = iks_find_attrib(description, "media");
  1714. struct ast_rtp_codecs codecs;
  1715. iks *codec;
  1716. int othercapability = 0;
  1717. /* Google-V1 is always carrying audio, but just doesn't tell us so */
  1718. if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
  1719. media = "audio";
  1720. } else if (ast_strlen_zero(media)) {
  1721. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1722. ast_log(LOG_ERROR, "Received a content description on session '%s' without a name\n", session->sid);
  1723. return -1;
  1724. }
  1725. /* Determine the type of media that is being carried and update the RTP instance, as well as the name */
  1726. if (!strcasecmp(media, "audio")) {
  1727. if (!ast_strlen_zero(name)) {
  1728. ast_string_field_set(session, audio_name, name);
  1729. }
  1730. *rtp = session->rtp;
  1731. ast_format_cap_remove_by_type(session->peercap, AST_MEDIA_TYPE_AUDIO);
  1732. ast_format_cap_remove_by_type(session->jointcap, AST_MEDIA_TYPE_AUDIO);
  1733. } else if (!strcasecmp(media, "video")) {
  1734. if (!ast_strlen_zero(name)) {
  1735. ast_string_field_set(session, video_name, name);
  1736. }
  1737. jingle_enable_video(session);
  1738. *rtp = session->vrtp;
  1739. /* If video is not present cancel this session */
  1740. if (!session->vrtp) {
  1741. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1742. ast_log(LOG_ERROR, "Received a video content description on session '%s' but could not enable video\n", session->sid);
  1743. return -1;
  1744. }
  1745. ast_format_cap_remove_by_type(session->peercap, AST_MEDIA_TYPE_VIDEO);
  1746. ast_format_cap_remove_by_type(session->jointcap, AST_MEDIA_TYPE_VIDEO);
  1747. } else {
  1748. /* Unknown media type */
  1749. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1750. ast_log(LOG_ERROR, "Unsupported media type '%s' received in content description on session '%s'\n", media, session->sid);
  1751. return -1;
  1752. }
  1753. if (ast_rtp_codecs_payloads_initialize(&codecs)) {
  1754. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1755. ast_log(LOG_ERROR, "Could not initialize codecs for negotiation on session '%s'\n", session->sid);
  1756. return -1;
  1757. }
  1758. /* Iterate the codecs updating the relevant RTP instance as we go */
  1759. for (codec = iks_child(description); codec; codec = iks_next(codec)) {
  1760. char *id = iks_find_attrib(codec, "id"), *name = iks_find_attrib(codec, "name");
  1761. char *clockrate = iks_find_attrib(codec, "clockrate");
  1762. int rtp_id, rtp_clockrate;
  1763. if (!ast_strlen_zero(id) && !ast_strlen_zero(name) && (sscanf(id, "%30d", &rtp_id) == 1)) {
  1764. if (!ast_strlen_zero(clockrate) && (sscanf(clockrate, "%30d", &rtp_clockrate) == 1)) {
  1765. ast_rtp_codecs_payloads_set_rtpmap_type_rate(&codecs, NULL, rtp_id, media, name, 0, rtp_clockrate);
  1766. } else {
  1767. ast_rtp_codecs_payloads_set_rtpmap_type(&codecs, NULL, rtp_id, media, name, 0);
  1768. }
  1769. }
  1770. }
  1771. ast_rtp_codecs_payload_formats(&codecs, session->peercap, &othercapability);
  1772. ast_format_cap_get_compatible(session->cap, session->peercap, session->jointcap);
  1773. if (!ast_format_cap_count(session->jointcap)) {
  1774. /* We have no compatible codecs, so terminate the session appropriately */
  1775. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1776. ast_rtp_codecs_payloads_destroy(&codecs);
  1777. return -1;
  1778. }
  1779. ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(*rtp), *rtp);
  1780. ast_rtp_codecs_payloads_destroy(&codecs);
  1781. return 0;
  1782. }
  1783. /*! \brief Helper function which handles ICE-UDP transport information */
  1784. static int jingle_interpret_ice_udp_transport(struct jingle_session *session, iks *transport, struct ast_rtp_instance *rtp)
  1785. {
  1786. struct ast_rtp_engine_ice *ice = ast_rtp_instance_get_ice(rtp);
  1787. char *ufrag = iks_find_attrib(transport, "ufrag"), *pwd = iks_find_attrib(transport, "pwd");
  1788. iks *candidate;
  1789. if (!ice) {
  1790. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1791. ast_log(LOG_ERROR, "Received ICE-UDP transport information on session '%s' but ICE support not available\n", session->sid);
  1792. return -1;
  1793. }
  1794. if (!ast_strlen_zero(ufrag) && !ast_strlen_zero(pwd)) {
  1795. ice->set_authentication(rtp, ufrag, pwd);
  1796. }
  1797. for (candidate = iks_child(transport); candidate; candidate = iks_next(candidate)) {
  1798. char *component = iks_find_attrib(candidate, "component"), *foundation = iks_find_attrib(candidate, "foundation");
  1799. char *generation = iks_find_attrib(candidate, "generation"), *id = iks_find_attrib(candidate, "id");
  1800. char *ip = iks_find_attrib(candidate, "ip"), *port = iks_find_attrib(candidate, "port");
  1801. char *priority = iks_find_attrib(candidate, "priority"), *protocol = iks_find_attrib(candidate, "protocol");
  1802. char *type = iks_find_attrib(candidate, "type");
  1803. struct ast_rtp_engine_ice_candidate local_candidate = { 0, };
  1804. int real_port;
  1805. struct ast_sockaddr remote_address = { { 0, } };
  1806. /* If this candidate is incomplete skip it */
  1807. if (ast_strlen_zero(component) || ast_strlen_zero(foundation) || ast_strlen_zero(generation) || ast_strlen_zero(id) ||
  1808. ast_strlen_zero(ip) || ast_strlen_zero(port) || ast_strlen_zero(priority) ||
  1809. ast_strlen_zero(protocol) || ast_strlen_zero(type)) {
  1810. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1811. ast_log(LOG_ERROR, "Incomplete ICE-UDP candidate received on session '%s'\n", session->sid);
  1812. return -1;
  1813. }
  1814. if ((sscanf(component, "%30u", &local_candidate.id) != 1) ||
  1815. (sscanf(priority, "%30u", (unsigned *)&local_candidate.priority) != 1) ||
  1816. (sscanf(port, "%30d", &real_port) != 1)) {
  1817. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1818. ast_log(LOG_ERROR, "Invalid ICE-UDP candidate information received on session '%s'\n", session->sid);
  1819. return -1;
  1820. }
  1821. local_candidate.foundation = foundation;
  1822. local_candidate.transport = protocol;
  1823. ast_sockaddr_parse(&local_candidate.address, ip, PARSE_PORT_FORBID);
  1824. /* We only support IPv4 right now */
  1825. if (!ast_sockaddr_is_ipv4(&local_candidate.address)) {
  1826. continue;
  1827. }
  1828. ast_sockaddr_set_port(&local_candidate.address, real_port);
  1829. if (!strcasecmp(type, "host")) {
  1830. local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
  1831. } else if (!strcasecmp(type, "srflx")) {
  1832. local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
  1833. } else if (!strcasecmp(type, "relay")) {
  1834. local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
  1835. } else {
  1836. continue;
  1837. }
  1838. /* Worst case use the first viable address */
  1839. ast_rtp_instance_get_remote_address(rtp, &remote_address);
  1840. if (ast_sockaddr_is_ipv4(&local_candidate.address) && ast_sockaddr_isnull(&remote_address)) {
  1841. ast_rtp_instance_set_remote_address(rtp, &local_candidate.address);
  1842. }
  1843. ice->add_remote_candidate(rtp, &local_candidate);
  1844. }
  1845. ice->start(rtp);
  1846. return 0;
  1847. }
  1848. /*! \brief Helper function which handles Google transport information */
  1849. static int jingle_interpret_google_transport(struct jingle_session *session, iks *transport, struct ast_rtp_instance *rtp)
  1850. {
  1851. struct ast_rtp_engine_ice *ice = ast_rtp_instance_get_ice(rtp);
  1852. iks *candidate;
  1853. if (!ice) {
  1854. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1855. ast_log(LOG_ERROR, "Received Google transport information on session '%s' but ICE support not available\n", session->sid);
  1856. return -1;
  1857. }
  1858. /* If this session has not transitioned to the Google transport do so now */
  1859. if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V2) &&
  1860. (session->transport != JINGLE_TRANSPORT_GOOGLE_V1)) {
  1861. /* Stop built-in ICE support... we need to fall back to the old old old STUN */
  1862. ice->stop(rtp);
  1863. session->transport = JINGLE_TRANSPORT_GOOGLE_V2;
  1864. }
  1865. for (candidate = iks_child(transport); candidate; candidate = iks_next(candidate)) {
  1866. char *address = iks_find_attrib(candidate, "address"), *port = iks_find_attrib(candidate, "port");
  1867. char *username = iks_find_attrib(candidate, "username"), *name = iks_find_attrib(candidate, "name");
  1868. char *protocol = iks_find_attrib(candidate, "protocol");
  1869. int real_port;
  1870. struct ast_sockaddr target = { { 0, } };
  1871. /* In Google land the combined value is 32 bytes */
  1872. char combined[33] = "";
  1873. /* If this is NOT actually a candidate just skip it */
  1874. if (strcasecmp(iks_name(candidate), "candidate") &&
  1875. strcasecmp(iks_name(candidate), "p:candidate") &&
  1876. strcasecmp(iks_name(candidate), "ses:candidate")) {
  1877. continue;
  1878. }
  1879. /* If this candidate is incomplete skip it */
  1880. if (ast_strlen_zero(address) || ast_strlen_zero(port) || ast_strlen_zero(username) ||
  1881. ast_strlen_zero(name)) {
  1882. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1883. ast_log(LOG_ERROR, "Incomplete Google candidate received on session '%s'\n", session->sid);
  1884. return -1;
  1885. }
  1886. /* We only support UDP so skip any other protocols */
  1887. if (!ast_strlen_zero(protocol) && strcasecmp(protocol, "udp")) {
  1888. continue;
  1889. }
  1890. /* We only permit audio and video, not RTCP */
  1891. if (strcasecmp(name, "rtp") && strcasecmp(name, "video_rtp")) {
  1892. continue;
  1893. }
  1894. /* Parse the target information so we can send a STUN request to the candidate */
  1895. if (sscanf(port, "%30d", &real_port) != 1) {
  1896. jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
  1897. ast_log(LOG_ERROR, "Invalid Google candidate port '%s' received on session '%s'\n", port, session->sid);
  1898. return -1;
  1899. }
  1900. ast_sockaddr_parse(&target, address, PARSE_PORT_FORBID);
  1901. ast_sockaddr_set_port(&target, real_port);
  1902. /* Per the STUN support Google talk uses combine the two usernames */
  1903. snprintf(combined, sizeof(combined), "%s%s", username, ice->get_ufrag(rtp));
  1904. /* This should appease the masses... we will actually change the remote address when we get their STUN packet */
  1905. ast_rtp_instance_stun_request(rtp, &target, combined);
  1906. }
  1907. return 0;
  1908. }
  1909. /*!
  1910. * \brief Helper function which locates content stanzas and interprets them
  1911. *
  1912. * \note The session *must not* be locked before calling this
  1913. */
  1914. static int jingle_interpret_content(struct jingle_session *session, ikspak *pak)
  1915. {
  1916. iks *content;
  1917. unsigned int changed = 0;
  1918. struct ast_channel *chan;
  1919. /* Look at the content in the session initiation */
  1920. for (content = iks_child(iks_child(pak->x)); content; content = iks_next(content)) {
  1921. char *name;
  1922. struct ast_rtp_instance *rtp = NULL;
  1923. iks *description, *transport;
  1924. /* Ignore specific parts if they are known not to be useful */
  1925. if (!strcmp(iks_name(content), "conference-info")) {
  1926. continue;
  1927. }
  1928. name = iks_find_attrib(content, "name");
  1929. if (session->transport != JINGLE_TRANSPORT_GOOGLE_V1) {
  1930. /* If this content stanza has no name consider it invalid and move on */
  1931. if (ast_strlen_zero(name) && !(name = iks_find_attrib(content, "jin:name"))) {
  1932. jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
  1933. ast_log(LOG_ERROR, "Received content without a name on session '%s'\n", session->sid);
  1934. return -1;
  1935. }
  1936. /* Try to pre-populate which RTP instance this content is relevant to */
  1937. if (!strcmp(session->audio_name, name)) {
  1938. rtp = session->rtp;
  1939. } else if (!strcmp(session->video_name, name)) {
  1940. rtp = session->vrtp;
  1941. }
  1942. } else {
  1943. /* Google-V1 has no concept of assocating things like the above does, so since we only support audio over it assume they want audio */
  1944. rtp = session->rtp;
  1945. }
  1946. /* If description information is available use it */
  1947. if ((description = iks_find_with_attrib(content, "description", "xmlns", JINGLE_RTP_NS)) ||
  1948. (description = iks_find_with_attrib(content, "rtp:description", "xmlns:rtp", JINGLE_RTP_NS)) ||
  1949. (description = iks_find_with_attrib(content, "pho:description", "xmlns:pho", GOOGLE_PHONE_NS)) ||
  1950. (description = iks_find_with_attrib(pak->query, "description", "xmlns", GOOGLE_PHONE_NS)) ||
  1951. (description = iks_find_with_attrib(pak->query, "pho:description", "xmlns:pho", GOOGLE_PHONE_NS)) ||
  1952. (description = iks_find_with_attrib(pak->query, "vid:description", "xmlns", GOOGLE_VIDEO_NS))) {
  1953. /* If we failed to do something with the content description abort immediately */
  1954. if (jingle_interpret_description(session, description, name, &rtp)) {
  1955. return -1;
  1956. }
  1957. /* If we successfully interpret the description then the codecs need updating */
  1958. changed = 1;
  1959. }
  1960. /* If we get past the description handling and we still don't know what RTP instance this is for... it is unknown content */
  1961. if (!rtp) {
  1962. ast_log(LOG_ERROR, "Received a content stanza but have no RTP instance for it on session '%s'\n", session->sid);
  1963. jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
  1964. return -1;
  1965. }
  1966. /* If ICE UDP transport information is available use it */
  1967. if ((transport = iks_find_with_attrib(content, "transport", "xmlns", JINGLE_ICE_UDP_NS))) {
  1968. if (jingle_interpret_ice_udp_transport(session, transport, rtp)) {
  1969. return -1;
  1970. }
  1971. } else if ((transport = iks_find_with_attrib(content, "transport", "xmlns", GOOGLE_TRANSPORT_NS)) ||
  1972. (transport = iks_find_with_attrib(content, "p:transport", "xmlns:p", GOOGLE_TRANSPORT_NS)) ||
  1973. (transport = iks_find_with_attrib(pak->x, "session", "xmlns", GOOGLE_SESSION_NS)) ||
  1974. (transport = iks_find_with_attrib(pak->x, "ses:session", "xmlns:ses", GOOGLE_SESSION_NS))) {
  1975. /* If Google transport support is available use it */
  1976. if (jingle_interpret_google_transport(session, transport, rtp)) {
  1977. return -1;
  1978. }
  1979. } else if (iks_find(content, "transport")) {
  1980. /* If this is a transport we do not support terminate the session as it probably won't work out in the end */
  1981. jingle_queue_hangup_with_cause(session, AST_CAUSE_FACILITY_NOT_IMPLEMENTED);
  1982. ast_log(LOG_ERROR, "Unsupported transport type received on session '%s'\n", session->sid);
  1983. return -1;
  1984. }
  1985. }
  1986. if (!changed) {
  1987. return 0;
  1988. }
  1989. if ((chan = jingle_session_lock_full(session))) {
  1990. struct ast_format_cap *caps;
  1991. struct ast_format *fmt;
  1992. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1993. if (caps) {
  1994. ast_format_cap_append_from_cap(caps, session->jointcap, AST_MEDIA_TYPE_UNKNOWN);
  1995. ast_channel_nativeformats_set(chan, caps);
  1996. ao2_ref(caps, -1);
  1997. }
  1998. fmt = ast_format_cap_get_format(session->jointcap, 0);
  1999. ast_set_read_format(chan, fmt);
  2000. ast_set_write_format(chan, fmt);
  2001. ao2_ref(fmt, -1);
  2002. ast_channel_unlock(chan);
  2003. ast_channel_unref(chan);
  2004. }
  2005. ao2_unlock(session);
  2006. return 0;
  2007. }
  2008. /*! \brief Handler function for the 'session-initiate' action */
  2009. static void jingle_action_session_initiate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
  2010. {
  2011. char *sid;
  2012. enum jingle_transport transport = JINGLE_TRANSPORT_NONE;
  2013. struct ast_channel *chan;
  2014. int res;
  2015. if (session) {
  2016. /* This is a duplicate session setup, so respond accordingly */
  2017. jingle_send_error_response(endpoint->connection, pak, "result", "out-of-order", NULL);
  2018. return;
  2019. }
  2020. /* Retrieve the session identifier from the message, note that this may alter the transport */
  2021. if ((sid = iks_find_attrib(pak->query, "id"))) {
  2022. /* The presence of the session identifier in the 'id' attribute tells us that this is Google-V1 as everything else uses 'sid' */
  2023. transport = JINGLE_TRANSPORT_GOOGLE_V1;
  2024. } else if (!(sid = iks_find_attrib(pak->query, "sid"))) {
  2025. jingle_send_error_response(endpoint->connection, pak, "bad-request", NULL, NULL);
  2026. return;
  2027. }
  2028. /* Create a new local session */
  2029. if (!(session = jingle_alloc(endpoint, pak->from->full, sid))) {
  2030. jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
  2031. return;
  2032. }
  2033. /* If we determined that the transport should change as a result of how we got the SID change it */
  2034. if (transport != JINGLE_TRANSPORT_NONE) {
  2035. session->transport = transport;
  2036. }
  2037. /* Create a new Asterisk channel using the above local session */
  2038. if (!(chan = jingle_new(endpoint, session, AST_STATE_DOWN, pak->from->user, NULL, NULL, pak->from->full))) {
  2039. ao2_ref(session, -1);
  2040. jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
  2041. return;
  2042. }
  2043. ao2_link(endpoint->state->sessions, session);
  2044. ast_channel_lock(chan);
  2045. ast_setstate(chan, AST_STATE_RING);
  2046. ast_channel_unlock(chan);
  2047. res = ast_pbx_start(chan);
  2048. switch (res) {
  2049. case AST_PBX_FAILED:
  2050. ast_log(LOG_WARNING, "Failed to start PBX :(\n");
  2051. jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
  2052. session->gone = 1;
  2053. ast_hangup(chan);
  2054. break;
  2055. case AST_PBX_CALL_LIMIT:
  2056. ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
  2057. jingle_send_error_response(endpoint->connection, pak, "wait", "resource-constraint xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
  2058. ast_hangup(chan);
  2059. break;
  2060. case AST_PBX_SUCCESS:
  2061. jingle_send_response(endpoint->connection, pak);
  2062. /* Only send a transport-info message if we successfully interpreted the available content */
  2063. if (!jingle_interpret_content(session, pak)) {
  2064. jingle_send_transport_info(session, iks_find_attrib(pak->x, "from"));
  2065. }
  2066. break;
  2067. }
  2068. }
  2069. /*! \brief Handler function for the 'transport-info' action */
  2070. static void jingle_action_transport_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
  2071. {
  2072. if (!session) {
  2073. jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
  2074. "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
  2075. return;
  2076. }
  2077. jingle_interpret_content(session, pak);
  2078. jingle_send_response(endpoint->connection, pak);
  2079. }
  2080. /*! \brief Handler function for the 'session-accept' action */
  2081. static void jingle_action_session_accept(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
  2082. {
  2083. struct ast_channel *chan;
  2084. if (!session) {
  2085. jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
  2086. "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
  2087. return;
  2088. }
  2089. jingle_interpret_content(session, pak);
  2090. if ((chan = jingle_session_lock_full(session))) {
  2091. ast_queue_control(chan, AST_CONTROL_ANSWER);
  2092. ast_channel_unlock(chan);
  2093. ast_channel_unref(chan);
  2094. }
  2095. ao2_unlock(session);
  2096. jingle_send_response(endpoint->connection, pak);
  2097. }
  2098. /*! \brief Handler function for the 'session-info' action */
  2099. static void jingle_action_session_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
  2100. {
  2101. struct ast_channel *chan;
  2102. if (!session) {
  2103. jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
  2104. "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
  2105. return;
  2106. }
  2107. if (!(chan = jingle_session_lock_full(session))) {
  2108. ao2_unlock(session);
  2109. jingle_send_response(endpoint->connection, pak);
  2110. return;
  2111. }
  2112. if (iks_find_with_attrib(pak->query, "ringing", "xmlns", JINGLE_RTP_INFO_NS)) {
  2113. ast_queue_control(chan, AST_CONTROL_RINGING);
  2114. if (ast_channel_state(chan) != AST_STATE_UP) {
  2115. ast_setstate(chan, AST_STATE_RINGING);
  2116. }
  2117. } else if (iks_find_with_attrib(pak->query, "hold", "xmlns", JINGLE_RTP_INFO_NS)) {
  2118. ast_queue_hold(chan, NULL);
  2119. } else if (iks_find_with_attrib(pak->query, "unhold", "xmlns", JINGLE_RTP_INFO_NS)) {
  2120. ast_queue_unhold(chan);
  2121. }
  2122. ast_channel_unlock(chan);
  2123. ast_channel_unref(chan);
  2124. ao2_unlock(session);
  2125. jingle_send_response(endpoint->connection, pak);
  2126. }
  2127. /*! \brief Handler function for the 'session-terminate' action */
  2128. static void jingle_action_session_terminate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
  2129. {
  2130. struct ast_channel *chan;
  2131. iks *reason, *text;
  2132. int cause = AST_CAUSE_NORMAL;
  2133. struct ast_control_pvt_cause_code *cause_code;
  2134. int data_size = sizeof(*cause_code);
  2135. if (!session) {
  2136. jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
  2137. "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
  2138. return;
  2139. }
  2140. if (!(chan = jingle_session_lock_full(session))) {
  2141. ao2_unlock(session);
  2142. jingle_send_response(endpoint->connection, pak);
  2143. return;
  2144. }
  2145. /* Pull the reason text from the session-terminate message and translate it into a cause code */
  2146. if ((reason = iks_find(pak->query, "reason")) && (text = iks_child(reason))) {
  2147. int i;
  2148. /* Size of the string making up the cause code is "Motif " + text */
  2149. data_size += 6 + strlen(iks_name(text));
  2150. cause_code = ast_alloca(data_size);
  2151. memset(cause_code, 0, data_size);
  2152. /* Get the appropriate cause code mapping for this reason */
  2153. for (i = 0; i < ARRAY_LEN(jingle_reason_mappings); i++) {
  2154. if (!strcasecmp(jingle_reason_mappings[i].reason, iks_name(text))) {
  2155. cause = jingle_reason_mappings[i].cause;
  2156. break;
  2157. }
  2158. }
  2159. /* Store the technology specific information */
  2160. snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "Motif %s", iks_name(text));
  2161. } else {
  2162. /* No technology specific information is available */
  2163. cause_code = ast_alloca(data_size);
  2164. memset(cause_code, 0, data_size);
  2165. }
  2166. ast_copy_string(cause_code->chan_name, ast_channel_name(chan), AST_CHANNEL_NAME);
  2167. cause_code->ast_cause = cause;
  2168. ast_queue_control_data(chan, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
  2169. ast_channel_hangupcause_hash_set(chan, cause_code, data_size);
  2170. ast_debug(3, "Hanging up channel '%s' due to session terminate message with cause '%d'\n", ast_channel_name(chan), cause);
  2171. ast_queue_hangup_with_cause(chan, cause);
  2172. session->gone = 1;
  2173. ast_channel_unlock(chan);
  2174. ast_channel_unref(chan);
  2175. ao2_unlock(session);
  2176. jingle_send_response(endpoint->connection, pak);
  2177. }
  2178. /*! \brief Callback for when a Jingle action is received from an endpoint */
  2179. static int jingle_action_hook(void *data, ikspak *pak)
  2180. {
  2181. char *action;
  2182. const char *sid = NULL;
  2183. struct jingle_session *session = NULL;
  2184. struct jingle_endpoint *endpoint = data;
  2185. int i, handled = 0;
  2186. /* We accept both Jingle and Google-V1 */
  2187. if (!(action = iks_find_attrib(pak->query, "action")) &&
  2188. !(action = iks_find_attrib(pak->query, "type"))) {
  2189. /* This occurs if either receive a packet masquerading as Jingle or Google-V1 that is actually not OR we receive a response
  2190. * to a message that has no response hook. */
  2191. return IKS_FILTER_EAT;
  2192. }
  2193. /* Bump the endpoint reference count up in case a reload occurs. Unfortunately the available synchronization between iksemel and us
  2194. * does not permit us to make this completely safe. */
  2195. ao2_ref(endpoint, +1);
  2196. /* If a Jingle session identifier is present use it */
  2197. if (!(sid = iks_find_attrib(pak->query, "sid"))) {
  2198. /* If a Google-V1 session identifier is present use it */
  2199. sid = iks_find_attrib(pak->query, "id");
  2200. }
  2201. /* If a session identifier was present in the message attempt to find the session, it is up to the action handler whether
  2202. * this is required or not */
  2203. if (!ast_strlen_zero(sid)) {
  2204. session = ao2_find(endpoint->state->sessions, sid, OBJ_KEY);
  2205. }
  2206. /* If a session is present associate the callid with this thread */
  2207. if (session) {
  2208. ast_callid_threadassoc_add(session->callid);
  2209. }
  2210. /* Iterate through supported action handlers looking for one that is able to handle this */
  2211. for (i = 0; i < ARRAY_LEN(jingle_action_handlers); i++) {
  2212. if (!strcasecmp(jingle_action_handlers[i].action, action)) {
  2213. jingle_action_handlers[i].handler(endpoint, session, pak);
  2214. handled = 1;
  2215. break;
  2216. }
  2217. }
  2218. /* If no action handler is present for the action they sent us make it evident */
  2219. if (!handled) {
  2220. ast_log(LOG_NOTICE, "Received action '%s' for session '%s' that has no handler\n", action, sid);
  2221. }
  2222. /* If a session was successfully found for this message deref it now since the handler is done */
  2223. if (session) {
  2224. ast_callid_threadassoc_remove();
  2225. ao2_ref(session, -1);
  2226. }
  2227. ao2_ref(endpoint, -1);
  2228. return IKS_FILTER_EAT;
  2229. }
  2230. /*! \brief Custom handler for groups */
  2231. static int custom_group_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2232. {
  2233. struct jingle_endpoint *endpoint = obj;
  2234. if (!strcasecmp(var->name, "callgroup")) {
  2235. endpoint->callgroup = ast_get_group(var->value);
  2236. } else if (!strcasecmp(var->name, "pickupgroup")) {
  2237. endpoint->pickupgroup = ast_get_group(var->value);
  2238. } else {
  2239. return -1;
  2240. }
  2241. return 0;
  2242. }
  2243. /*! \brief Custom handler for connection */
  2244. static int custom_connection_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2245. {
  2246. struct jingle_endpoint *endpoint = obj;
  2247. /* You might think... but Josh, shouldn't you do this in a prelink callback? Well I *could* but until the original is destroyed
  2248. * this will not actually get called, so even if the config turns out to be bogus this is harmless.
  2249. */
  2250. if (!(endpoint->connection = ast_xmpp_client_find(var->value))) {
  2251. ast_log(LOG_ERROR, "Connection '%s' configured on endpoint '%s' could not be found\n", var->value, endpoint->name);
  2252. return -1;
  2253. }
  2254. if (!(endpoint->rule = iks_filter_add_rule(endpoint->connection->filter, jingle_action_hook, endpoint,
  2255. IKS_RULE_TYPE, IKS_PAK_IQ,
  2256. IKS_RULE_NS, JINGLE_NS,
  2257. IKS_RULE_NS, GOOGLE_SESSION_NS,
  2258. IKS_RULE_DONE))) {
  2259. ast_log(LOG_ERROR, "Action hook could not be added to connection '%s' on endpoint '%s'\n", var->value, endpoint->name);
  2260. return -1;
  2261. }
  2262. return 0;
  2263. }
  2264. /*! \brief Custom handler for transport */
  2265. static int custom_transport_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  2266. {
  2267. struct jingle_endpoint *endpoint = obj;
  2268. if (!strcasecmp(var->value, "ice-udp")) {
  2269. endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;
  2270. } else if (!strcasecmp(var->value, "google")) {
  2271. endpoint->transport = JINGLE_TRANSPORT_GOOGLE_V2;
  2272. } else if (!strcasecmp(var->value, "google-v1")) {
  2273. endpoint->transport = JINGLE_TRANSPORT_GOOGLE_V1;
  2274. } else {
  2275. ast_log(LOG_WARNING, "Unknown transport type '%s' on endpoint '%s', defaulting to 'ice-udp'\n", var->value, endpoint->name);
  2276. endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;
  2277. }
  2278. return 0;
  2279. }
  2280. /*!
  2281. * \brief Load the module
  2282. *
  2283. * Module loading including tests for configuration or dependencies.
  2284. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  2285. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  2286. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  2287. * configuration file or other non-critical problem return
  2288. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  2289. */
  2290. static int load_module(void)
  2291. {
  2292. if (!(jingle_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  2293. return AST_MODULE_LOAD_DECLINE;
  2294. }
  2295. if (aco_info_init(&cfg_info)) {
  2296. ast_log(LOG_ERROR, "Unable to intialize configuration for chan_motif.\n");
  2297. goto end;
  2298. }
  2299. aco_option_register(&cfg_info, "context", ACO_EXACT, endpoint_options, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, context));
  2300. aco_option_register_custom(&cfg_info, "callgroup", ACO_EXACT, endpoint_options, NULL, custom_group_handler, 0);
  2301. aco_option_register_custom(&cfg_info, "pickupgroup", ACO_EXACT, endpoint_options, NULL, custom_group_handler, 0);
  2302. aco_option_register(&cfg_info, "language", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, language));
  2303. aco_option_register(&cfg_info, "musicclass", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, musicclass));
  2304. aco_option_register(&cfg_info, "parkinglot", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, parkinglot));
  2305. aco_option_register(&cfg_info, "accountcode", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, accountcode));
  2306. aco_option_register(&cfg_info, "allow", ACO_EXACT, endpoint_options, "ulaw,alaw", OPT_CODEC_T, 1, FLDSET(struct jingle_endpoint, cap));
  2307. aco_option_register(&cfg_info, "disallow", ACO_EXACT, endpoint_options, "all", OPT_CODEC_T, 0, FLDSET(struct jingle_endpoint, cap));
  2308. aco_option_register_custom(&cfg_info, "connection", ACO_EXACT, endpoint_options, NULL, custom_connection_handler, 0);
  2309. aco_option_register_custom(&cfg_info, "transport", ACO_EXACT, endpoint_options, NULL, custom_transport_handler, 0);
  2310. aco_option_register(&cfg_info, "maxicecandidates", ACO_EXACT, endpoint_options, DEFAULT_MAX_ICE_CANDIDATES, OPT_UINT_T, PARSE_DEFAULT,
  2311. FLDSET(struct jingle_endpoint, maxicecandidates), DEFAULT_MAX_ICE_CANDIDATES);
  2312. aco_option_register(&cfg_info, "maxpayloads", ACO_EXACT, endpoint_options, DEFAULT_MAX_PAYLOADS, OPT_UINT_T, PARSE_DEFAULT,
  2313. FLDSET(struct jingle_endpoint, maxpayloads), DEFAULT_MAX_PAYLOADS);
  2314. ast_format_cap_append_by_type(jingle_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
  2315. if (aco_process_config(&cfg_info, 0)) {
  2316. ast_log(LOG_ERROR, "Unable to read config file motif.conf. Module loaded but not running.\n");
  2317. aco_info_destroy(&cfg_info);
  2318. ao2_cleanup(jingle_tech.capabilities);
  2319. jingle_tech.capabilities = NULL;
  2320. return AST_MODULE_LOAD_DECLINE;
  2321. }
  2322. if (!(sched = ast_sched_context_create())) {
  2323. ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
  2324. goto end;
  2325. }
  2326. if (ast_sched_start_thread(sched)) {
  2327. ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
  2328. goto end;
  2329. }
  2330. ast_rtp_glue_register(&jingle_rtp_glue);
  2331. if (ast_channel_register(&jingle_tech)) {
  2332. ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
  2333. goto end;
  2334. }
  2335. return 0;
  2336. end:
  2337. ast_rtp_glue_unregister(&jingle_rtp_glue);
  2338. if (sched) {
  2339. ast_sched_context_destroy(sched);
  2340. }
  2341. aco_info_destroy(&cfg_info);
  2342. ao2_global_obj_release(globals);
  2343. ao2_cleanup(jingle_tech.capabilities);
  2344. jingle_tech.capabilities = NULL;
  2345. return AST_MODULE_LOAD_DECLINE;
  2346. }
  2347. /*! \brief Reload module */
  2348. static int reload(void)
  2349. {
  2350. if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
  2351. return -1;
  2352. }
  2353. return 0;
  2354. }
  2355. /*! \brief Unload the jingle channel from Asterisk */
  2356. static int unload_module(void)
  2357. {
  2358. ast_channel_unregister(&jingle_tech);
  2359. ao2_cleanup(jingle_tech.capabilities);
  2360. jingle_tech.capabilities = NULL;
  2361. ast_rtp_glue_unregister(&jingle_rtp_glue);
  2362. ast_sched_context_destroy(sched);
  2363. aco_info_destroy(&cfg_info);
  2364. ao2_global_obj_release(globals);
  2365. return 0;
  2366. }
  2367. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Motif Jingle Channel Driver",
  2368. .support_level = AST_MODULE_SUPPORT_CORE,
  2369. .load = load_module,
  2370. .unload = unload_module,
  2371. .reload = reload,
  2372. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  2373. );